]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/dev-network.c
Include migration/vmstate.h less
[mirror_qemu.git] / hw / usb / dev-network.c
CommitLineData
6c9f886c
AZ
1/*
2 * QEMU USB Net devices
3 *
4 * Copyright (c) 2006 Thomas Sailer
5 * Copyright (c) 2008 Andrzej Zaborowski
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
e532b2e0 26#include "qemu/osdep.h"
da34e65c 27#include "qapi/error.h"
f1ae32a1 28#include "hw/usb.h"
d6454270 29#include "migration/vmstate.h"
463581a8 30#include "desc.h"
1422e32d 31#include "net/net.h"
d73ad359 32#include "qemu/error-report.h"
1de7afc9
PB
33#include "qemu/queue.h"
34#include "qemu/config-file.h"
9c17d615 35#include "sysemu/sysemu.h"
1de7afc9 36#include "qemu/iov.h"
0b8fa32f 37#include "qemu/module.h"
f348b6d1 38#include "qemu/cutils.h"
6c9f886c
AZ
39
40/*#define TRAFFIC_DEBUG*/
41/* Thanks to NetChip Technologies for donating this product ID.
42 * It's for devices with only CDC Ethernet configurations.
43 */
44#define CDC_VENDOR_NUM 0x0525 /* NetChip */
45#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
46/* For hardware that can talk RNDIS and either of the above protocols,
47 * use this ID ... the windows INF files will know it.
48 */
49#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
50#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
51
52enum usbstring_idx {
53 STRING_MANUFACTURER = 1,
54 STRING_PRODUCT,
55 STRING_ETHADDR,
56 STRING_DATA,
57 STRING_CONTROL,
58 STRING_RNDIS_CONTROL,
59 STRING_CDC,
60 STRING_SUBSET,
61 STRING_RNDIS,
62 STRING_SERIALNUMBER,
63};
64
65#define DEV_CONFIG_VALUE 1 /* CDC or a subset */
66#define DEV_RNDIS_CONFIG_VALUE 2 /* RNDIS; optional */
67
68#define USB_CDC_SUBCLASS_ACM 0x02
69#define USB_CDC_SUBCLASS_ETHERNET 0x06
70
71#define USB_CDC_PROTO_NONE 0
72#define USB_CDC_ACM_PROTO_VENDOR 0xff
73
74#define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
75#define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
76#define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
77#define USB_CDC_UNION_TYPE 0x06 /* union_desc */
78#define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
79
6c9f886c
AZ
80#define USB_CDC_SEND_ENCAPSULATED_COMMAND 0x00
81#define USB_CDC_GET_ENCAPSULATED_RESPONSE 0x01
82#define USB_CDC_REQ_SET_LINE_CODING 0x20
83#define USB_CDC_REQ_GET_LINE_CODING 0x21
84#define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
85#define USB_CDC_REQ_SEND_BREAK 0x23
86#define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40
87#define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41
88#define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42
89#define USB_CDC_SET_ETHERNET_PACKET_FILTER 0x43
90#define USB_CDC_GET_ETHERNET_STATISTIC 0x44
91
92#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
93#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
94
95#define ETH_FRAME_LEN 1514 /* Max. octets in frame sans FCS */
96
30c7d32a
GH
97static const USBDescStrings usb_net_stringtable = {
98 [STRING_MANUFACTURER] = "QEMU",
99 [STRING_PRODUCT] = "RNDIS/QEMU USB Network Device",
100 [STRING_ETHADDR] = "400102030405",
101 [STRING_DATA] = "QEMU USB Net Data Interface",
102 [STRING_CONTROL] = "QEMU USB Net Control Interface",
103 [STRING_RNDIS_CONTROL] = "QEMU USB Net RNDIS Control Interface",
104 [STRING_CDC] = "QEMU USB Net CDC",
105 [STRING_SUBSET] = "QEMU USB Net Subset",
106 [STRING_RNDIS] = "QEMU USB Net RNDIS",
107 [STRING_SERIALNUMBER] = "1",
6c9f886c
AZ
108};
109
30c7d32a
GH
110static const USBDescIface desc_iface_rndis[] = {
111 {
112 /* RNDIS Control Interface */
113 .bInterfaceNumber = 0,
114 .bNumEndpoints = 1,
115 .bInterfaceClass = USB_CLASS_COMM,
116 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
117 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
118 .iInterface = STRING_RNDIS_CONTROL,
119 .ndesc = 4,
120 .descs = (USBDescOther[]) {
121 {
122 /* Header Descriptor */
123 .data = (uint8_t[]) {
124 0x05, /* u8 bLength */
125 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
126 USB_CDC_HEADER_TYPE, /* u8 bDescriptorSubType */
127 0x10, 0x01, /* le16 bcdCDC */
128 },
129 },{
130 /* Call Management Descriptor */
131 .data = (uint8_t[]) {
132 0x05, /* u8 bLength */
133 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
134 USB_CDC_CALL_MANAGEMENT_TYPE, /* u8 bDescriptorSubType */
135 0x00, /* u8 bmCapabilities */
136 0x01, /* u8 bDataInterface */
137 },
138 },{
139 /* ACM Descriptor */
140 .data = (uint8_t[]) {
141 0x04, /* u8 bLength */
142 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
143 USB_CDC_ACM_TYPE, /* u8 bDescriptorSubType */
144 0x00, /* u8 bmCapabilities */
145 },
146 },{
147 /* Union Descriptor */
148 .data = (uint8_t[]) {
149 0x05, /* u8 bLength */
150 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
151 USB_CDC_UNION_TYPE, /* u8 bDescriptorSubType */
152 0x00, /* u8 bMasterInterface0 */
153 0x01, /* u8 bSlaveInterface0 */
154 },
155 },
156 },
157 .eps = (USBDescEndpoint[]) {
158 {
159 .bEndpointAddress = USB_DIR_IN | 0x01,
160 .bmAttributes = USB_ENDPOINT_XFER_INT,
161 .wMaxPacketSize = STATUS_BYTECOUNT,
162 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
163 },
164 }
165 },{
166 /* RNDIS Data Interface */
167 .bInterfaceNumber = 1,
168 .bNumEndpoints = 2,
169 .bInterfaceClass = USB_CLASS_CDC_DATA,
170 .iInterface = STRING_DATA,
171 .eps = (USBDescEndpoint[]) {
172 {
173 .bEndpointAddress = USB_DIR_IN | 0x02,
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 .wMaxPacketSize = 0x40,
176 },{
177 .bEndpointAddress = USB_DIR_OUT | 0x02,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179 .wMaxPacketSize = 0x40,
180 }
181 }
182 }
6c9f886c
AZ
183};
184
30c7d32a
GH
185static const USBDescIface desc_iface_cdc[] = {
186 {
187 /* CDC Control Interface */
188 .bInterfaceNumber = 0,
189 .bNumEndpoints = 1,
190 .bInterfaceClass = USB_CLASS_COMM,
191 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
192 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
193 .iInterface = STRING_CONTROL,
194 .ndesc = 3,
195 .descs = (USBDescOther[]) {
196 {
197 /* Header Descriptor */
198 .data = (uint8_t[]) {
199 0x05, /* u8 bLength */
200 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
201 USB_CDC_HEADER_TYPE, /* u8 bDescriptorSubType */
202 0x10, 0x01, /* le16 bcdCDC */
203 },
204 },{
205 /* Union Descriptor */
206 .data = (uint8_t[]) {
207 0x05, /* u8 bLength */
208 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
209 USB_CDC_UNION_TYPE, /* u8 bDescriptorSubType */
210 0x00, /* u8 bMasterInterface0 */
211 0x01, /* u8 bSlaveInterface0 */
212 },
213 },{
214 /* Ethernet Descriptor */
215 .data = (uint8_t[]) {
216 0x0d, /* u8 bLength */
217 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
218 USB_CDC_ETHERNET_TYPE, /* u8 bDescriptorSubType */
219 STRING_ETHADDR, /* u8 iMACAddress */
220 0x00, 0x00, 0x00, 0x00, /* le32 bmEthernetStatistics */
221 ETH_FRAME_LEN & 0xff,
222 ETH_FRAME_LEN >> 8, /* le16 wMaxSegmentSize */
223 0x00, 0x00, /* le16 wNumberMCFilters */
224 0x00, /* u8 bNumberPowerFilters */
225 },
226 },
227 },
228 .eps = (USBDescEndpoint[]) {
229 {
230 .bEndpointAddress = USB_DIR_IN | 0x01,
231 .bmAttributes = USB_ENDPOINT_XFER_INT,
232 .wMaxPacketSize = STATUS_BYTECOUNT,
233 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
234 },
235 }
236 },{
237 /* CDC Data Interface (off) */
238 .bInterfaceNumber = 1,
239 .bAlternateSetting = 0,
240 .bNumEndpoints = 0,
241 .bInterfaceClass = USB_CLASS_CDC_DATA,
242 },{
243 /* CDC Data Interface */
244 .bInterfaceNumber = 1,
245 .bAlternateSetting = 1,
246 .bNumEndpoints = 2,
247 .bInterfaceClass = USB_CLASS_CDC_DATA,
248 .iInterface = STRING_DATA,
249 .eps = (USBDescEndpoint[]) {
250 {
251 .bEndpointAddress = USB_DIR_IN | 0x02,
252 .bmAttributes = USB_ENDPOINT_XFER_BULK,
253 .wMaxPacketSize = 0x40,
254 },{
255 .bEndpointAddress = USB_DIR_OUT | 0x02,
256 .bmAttributes = USB_ENDPOINT_XFER_BULK,
257 .wMaxPacketSize = 0x40,
258 }
259 }
260 }
261};
262
263static const USBDescDevice desc_device_net = {
264 .bcdUSB = 0x0200,
265 .bDeviceClass = USB_CLASS_COMM,
266 .bMaxPacketSize0 = 0x40,
267 .bNumConfigurations = 2,
268 .confs = (USBDescConfig[]) {
269 {
270 .bNumInterfaces = 2,
271 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
272 .iConfiguration = STRING_RNDIS,
bd93976a 273 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
30c7d32a
GH
274 .bMaxPower = 0x32,
275 .nif = ARRAY_SIZE(desc_iface_rndis),
276 .ifs = desc_iface_rndis,
277 },{
278 .bNumInterfaces = 2,
279 .bConfigurationValue = DEV_CONFIG_VALUE,
280 .iConfiguration = STRING_CDC,
bd93976a 281 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
30c7d32a
GH
282 .bMaxPower = 0x32,
283 .nif = ARRAY_SIZE(desc_iface_cdc),
284 .ifs = desc_iface_cdc,
285 }
286 },
287};
288
289static const USBDesc desc_net = {
290 .id = {
291 .idVendor = RNDIS_VENDOR_NUM,
292 .idProduct = RNDIS_PRODUCT_NUM,
293 .bcdDevice = 0,
294 .iManufacturer = STRING_MANUFACTURER,
295 .iProduct = STRING_PRODUCT,
296 .iSerialNumber = STRING_SERIALNUMBER,
297 },
298 .full = &desc_device_net,
299 .str = usb_net_stringtable,
6c9f886c
AZ
300};
301
302/*
303 * RNDIS Definitions - in theory not specific to USB.
304 */
305#define RNDIS_MAXIMUM_FRAME_SIZE 1518
306#define RNDIS_MAX_TOTAL_SIZE 1558
307
308/* Remote NDIS Versions */
309#define RNDIS_MAJOR_VERSION 1
310#define RNDIS_MINOR_VERSION 0
311
312/* Status Values */
313#define RNDIS_STATUS_SUCCESS 0x00000000U /* Success */
314#define RNDIS_STATUS_FAILURE 0xc0000001U /* Unspecified error */
315#define RNDIS_STATUS_INVALID_DATA 0xc0010015U /* Invalid data */
316#define RNDIS_STATUS_NOT_SUPPORTED 0xc00000bbU /* Unsupported request */
317#define RNDIS_STATUS_MEDIA_CONNECT 0x4001000bU /* Device connected */
318#define RNDIS_STATUS_MEDIA_DISCONNECT 0x4001000cU /* Device disconnected */
319
320/* Message Set for Connectionless (802.3) Devices */
321enum {
322 RNDIS_PACKET_MSG = 1,
323 RNDIS_INITIALIZE_MSG = 2, /* Initialize device */
324 RNDIS_HALT_MSG = 3,
325 RNDIS_QUERY_MSG = 4,
326 RNDIS_SET_MSG = 5,
327 RNDIS_RESET_MSG = 6,
328 RNDIS_INDICATE_STATUS_MSG = 7,
329 RNDIS_KEEPALIVE_MSG = 8,
330};
331
332/* Message completion */
333enum {
334 RNDIS_INITIALIZE_CMPLT = 0x80000002U,
335 RNDIS_QUERY_CMPLT = 0x80000004U,
336 RNDIS_SET_CMPLT = 0x80000005U,
337 RNDIS_RESET_CMPLT = 0x80000006U,
338 RNDIS_KEEPALIVE_CMPLT = 0x80000008U,
339};
340
341/* Device Flags */
342enum {
343 RNDIS_DF_CONNECTIONLESS = 1,
344 RNDIS_DF_CONNECTIONORIENTED = 2,
345};
346
347#define RNDIS_MEDIUM_802_3 0x00000000U
348
349/* from drivers/net/sk98lin/h/skgepnmi.h */
350#define OID_PNP_CAPABILITIES 0xfd010100
351#define OID_PNP_SET_POWER 0xfd010101
352#define OID_PNP_QUERY_POWER 0xfd010102
353#define OID_PNP_ADD_WAKE_UP_PATTERN 0xfd010103
354#define OID_PNP_REMOVE_WAKE_UP_PATTERN 0xfd010104
355#define OID_PNP_ENABLE_WAKE_UP 0xfd010106
356
357typedef uint32_t le32;
358
359typedef struct rndis_init_msg_type {
360 le32 MessageType;
361 le32 MessageLength;
362 le32 RequestID;
363 le32 MajorVersion;
364 le32 MinorVersion;
365 le32 MaxTransferSize;
366} rndis_init_msg_type;
367
368typedef struct rndis_init_cmplt_type {
369 le32 MessageType;
370 le32 MessageLength;
371 le32 RequestID;
372 le32 Status;
373 le32 MajorVersion;
374 le32 MinorVersion;
375 le32 DeviceFlags;
376 le32 Medium;
377 le32 MaxPacketsPerTransfer;
378 le32 MaxTransferSize;
379 le32 PacketAlignmentFactor;
380 le32 AFListOffset;
381 le32 AFListSize;
382} rndis_init_cmplt_type;
383
384typedef struct rndis_halt_msg_type {
385 le32 MessageType;
386 le32 MessageLength;
387 le32 RequestID;
388} rndis_halt_msg_type;
389
390typedef struct rndis_query_msg_type {
391 le32 MessageType;
392 le32 MessageLength;
393 le32 RequestID;
394 le32 OID;
395 le32 InformationBufferLength;
396 le32 InformationBufferOffset;
397 le32 DeviceVcHandle;
398} rndis_query_msg_type;
399
400typedef struct rndis_query_cmplt_type {
401 le32 MessageType;
402 le32 MessageLength;
403 le32 RequestID;
404 le32 Status;
405 le32 InformationBufferLength;
406 le32 InformationBufferOffset;
407} rndis_query_cmplt_type;
408
409typedef struct rndis_set_msg_type {
410 le32 MessageType;
411 le32 MessageLength;
412 le32 RequestID;
413 le32 OID;
414 le32 InformationBufferLength;
415 le32 InformationBufferOffset;
416 le32 DeviceVcHandle;
417} rndis_set_msg_type;
418
419typedef struct rndis_set_cmplt_type {
420 le32 MessageType;
421 le32 MessageLength;
422 le32 RequestID;
423 le32 Status;
424} rndis_set_cmplt_type;
425
426typedef struct rndis_reset_msg_type {
427 le32 MessageType;
428 le32 MessageLength;
429 le32 Reserved;
430} rndis_reset_msg_type;
431
432typedef struct rndis_reset_cmplt_type {
433 le32 MessageType;
434 le32 MessageLength;
435 le32 Status;
436 le32 AddressingReset;
437} rndis_reset_cmplt_type;
438
439typedef struct rndis_indicate_status_msg_type {
440 le32 MessageType;
441 le32 MessageLength;
442 le32 Status;
443 le32 StatusBufferLength;
444 le32 StatusBufferOffset;
445} rndis_indicate_status_msg_type;
446
447typedef struct rndis_keepalive_msg_type {
448 le32 MessageType;
449 le32 MessageLength;
450 le32 RequestID;
451} rndis_keepalive_msg_type;
452
453typedef struct rndis_keepalive_cmplt_type {
454 le32 MessageType;
455 le32 MessageLength;
456 le32 RequestID;
457 le32 Status;
458} rndis_keepalive_cmplt_type;
459
460struct rndis_packet_msg_type {
461 le32 MessageType;
462 le32 MessageLength;
463 le32 DataOffset;
464 le32 DataLength;
465 le32 OOBDataOffset;
466 le32 OOBDataLength;
467 le32 NumOOBDataElements;
468 le32 PerPacketInfoOffset;
469 le32 PerPacketInfoLength;
470 le32 VcHandle;
471 le32 Reserved;
472};
473
474struct rndis_config_parameter {
475 le32 ParameterNameOffset;
476 le32 ParameterNameLength;
477 le32 ParameterType;
478 le32 ParameterValueOffset;
479 le32 ParameterValueLength;
480};
481
482/* implementation specific */
483enum rndis_state
484{
485 RNDIS_UNINITIALIZED,
486 RNDIS_INITIALIZED,
487 RNDIS_DATA_INITIALIZED,
488};
489
490/* from ndis.h */
491enum ndis_oid {
492 /* Required Object IDs (OIDs) */
493 OID_GEN_SUPPORTED_LIST = 0x00010101,
494 OID_GEN_HARDWARE_STATUS = 0x00010102,
495 OID_GEN_MEDIA_SUPPORTED = 0x00010103,
496 OID_GEN_MEDIA_IN_USE = 0x00010104,
497 OID_GEN_MAXIMUM_LOOKAHEAD = 0x00010105,
498 OID_GEN_MAXIMUM_FRAME_SIZE = 0x00010106,
499 OID_GEN_LINK_SPEED = 0x00010107,
500 OID_GEN_TRANSMIT_BUFFER_SPACE = 0x00010108,
501 OID_GEN_RECEIVE_BUFFER_SPACE = 0x00010109,
502 OID_GEN_TRANSMIT_BLOCK_SIZE = 0x0001010a,
503 OID_GEN_RECEIVE_BLOCK_SIZE = 0x0001010b,
504 OID_GEN_VENDOR_ID = 0x0001010c,
505 OID_GEN_VENDOR_DESCRIPTION = 0x0001010d,
506 OID_GEN_CURRENT_PACKET_FILTER = 0x0001010e,
507 OID_GEN_CURRENT_LOOKAHEAD = 0x0001010f,
508 OID_GEN_DRIVER_VERSION = 0x00010110,
509 OID_GEN_MAXIMUM_TOTAL_SIZE = 0x00010111,
510 OID_GEN_PROTOCOL_OPTIONS = 0x00010112,
511 OID_GEN_MAC_OPTIONS = 0x00010113,
512 OID_GEN_MEDIA_CONNECT_STATUS = 0x00010114,
513 OID_GEN_MAXIMUM_SEND_PACKETS = 0x00010115,
514 OID_GEN_VENDOR_DRIVER_VERSION = 0x00010116,
515 OID_GEN_SUPPORTED_GUIDS = 0x00010117,
516 OID_GEN_NETWORK_LAYER_ADDRESSES = 0x00010118,
517 OID_GEN_TRANSPORT_HEADER_OFFSET = 0x00010119,
518 OID_GEN_MACHINE_NAME = 0x0001021a,
519 OID_GEN_RNDIS_CONFIG_PARAMETER = 0x0001021b,
520 OID_GEN_VLAN_ID = 0x0001021c,
521
522 /* Optional OIDs */
523 OID_GEN_MEDIA_CAPABILITIES = 0x00010201,
524 OID_GEN_PHYSICAL_MEDIUM = 0x00010202,
525
526 /* Required statistics OIDs */
527 OID_GEN_XMIT_OK = 0x00020101,
528 OID_GEN_RCV_OK = 0x00020102,
529 OID_GEN_XMIT_ERROR = 0x00020103,
530 OID_GEN_RCV_ERROR = 0x00020104,
531 OID_GEN_RCV_NO_BUFFER = 0x00020105,
532
533 /* Optional statistics OIDs */
534 OID_GEN_DIRECTED_BYTES_XMIT = 0x00020201,
535 OID_GEN_DIRECTED_FRAMES_XMIT = 0x00020202,
536 OID_GEN_MULTICAST_BYTES_XMIT = 0x00020203,
537 OID_GEN_MULTICAST_FRAMES_XMIT = 0x00020204,
538 OID_GEN_BROADCAST_BYTES_XMIT = 0x00020205,
539 OID_GEN_BROADCAST_FRAMES_XMIT = 0x00020206,
540 OID_GEN_DIRECTED_BYTES_RCV = 0x00020207,
541 OID_GEN_DIRECTED_FRAMES_RCV = 0x00020208,
542 OID_GEN_MULTICAST_BYTES_RCV = 0x00020209,
543 OID_GEN_MULTICAST_FRAMES_RCV = 0x0002020a,
544 OID_GEN_BROADCAST_BYTES_RCV = 0x0002020b,
545 OID_GEN_BROADCAST_FRAMES_RCV = 0x0002020c,
546 OID_GEN_RCV_CRC_ERROR = 0x0002020d,
547 OID_GEN_TRANSMIT_QUEUE_LENGTH = 0x0002020e,
548 OID_GEN_GET_TIME_CAPS = 0x0002020f,
549 OID_GEN_GET_NETCARD_TIME = 0x00020210,
550 OID_GEN_NETCARD_LOAD = 0x00020211,
551 OID_GEN_DEVICE_PROFILE = 0x00020212,
552 OID_GEN_INIT_TIME_MS = 0x00020213,
553 OID_GEN_RESET_COUNTS = 0x00020214,
554 OID_GEN_MEDIA_SENSE_COUNTS = 0x00020215,
555 OID_GEN_FRIENDLY_NAME = 0x00020216,
556 OID_GEN_MINIPORT_INFO = 0x00020217,
557 OID_GEN_RESET_VERIFY_PARAMETERS = 0x00020218,
558
559 /* IEEE 802.3 (Ethernet) OIDs */
560 OID_802_3_PERMANENT_ADDRESS = 0x01010101,
561 OID_802_3_CURRENT_ADDRESS = 0x01010102,
562 OID_802_3_MULTICAST_LIST = 0x01010103,
563 OID_802_3_MAXIMUM_LIST_SIZE = 0x01010104,
564 OID_802_3_MAC_OPTIONS = 0x01010105,
565 OID_802_3_RCV_ERROR_ALIGNMENT = 0x01020101,
566 OID_802_3_XMIT_ONE_COLLISION = 0x01020102,
567 OID_802_3_XMIT_MORE_COLLISIONS = 0x01020103,
568 OID_802_3_XMIT_DEFERRED = 0x01020201,
569 OID_802_3_XMIT_MAX_COLLISIONS = 0x01020202,
570 OID_802_3_RCV_OVERRUN = 0x01020203,
571 OID_802_3_XMIT_UNDERRUN = 0x01020204,
572 OID_802_3_XMIT_HEARTBEAT_FAILURE = 0x01020205,
573 OID_802_3_XMIT_TIMES_CRS_LOST = 0x01020206,
574 OID_802_3_XMIT_LATE_COLLISIONS = 0x01020207,
575};
576
577static const uint32_t oid_supported_list[] =
578{
579 /* the general stuff */
580 OID_GEN_SUPPORTED_LIST,
581 OID_GEN_HARDWARE_STATUS,
582 OID_GEN_MEDIA_SUPPORTED,
583 OID_GEN_MEDIA_IN_USE,
584 OID_GEN_MAXIMUM_FRAME_SIZE,
585 OID_GEN_LINK_SPEED,
586 OID_GEN_TRANSMIT_BLOCK_SIZE,
587 OID_GEN_RECEIVE_BLOCK_SIZE,
588 OID_GEN_VENDOR_ID,
589 OID_GEN_VENDOR_DESCRIPTION,
590 OID_GEN_VENDOR_DRIVER_VERSION,
591 OID_GEN_CURRENT_PACKET_FILTER,
592 OID_GEN_MAXIMUM_TOTAL_SIZE,
593 OID_GEN_MEDIA_CONNECT_STATUS,
594 OID_GEN_PHYSICAL_MEDIUM,
595
596 /* the statistical stuff */
597 OID_GEN_XMIT_OK,
598 OID_GEN_RCV_OK,
599 OID_GEN_XMIT_ERROR,
600 OID_GEN_RCV_ERROR,
601 OID_GEN_RCV_NO_BUFFER,
602
603 /* IEEE 802.3 */
604 /* the general stuff */
605 OID_802_3_PERMANENT_ADDRESS,
606 OID_802_3_CURRENT_ADDRESS,
607 OID_802_3_MULTICAST_LIST,
608 OID_802_3_MAC_OPTIONS,
609 OID_802_3_MAXIMUM_LIST_SIZE,
610
611 /* the statistical stuff */
612 OID_802_3_RCV_ERROR_ALIGNMENT,
613 OID_802_3_XMIT_ONE_COLLISION,
614 OID_802_3_XMIT_MORE_COLLISIONS,
615};
616
617#define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA (1 << 0)
618#define NDIS_MAC_OPTION_RECEIVE_SERIALIZED (1 << 1)
619#define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND (1 << 2)
620#define NDIS_MAC_OPTION_NO_LOOPBACK (1 << 3)
621#define NDIS_MAC_OPTION_FULL_DUPLEX (1 << 4)
622#define NDIS_MAC_OPTION_EOTX_INDICATION (1 << 5)
623#define NDIS_MAC_OPTION_8021P_PRIORITY (1 << 6)
624
625struct rndis_response {
72cf2d4f 626 QTAILQ_ENTRY(rndis_response) entries;
6c9f886c
AZ
627 uint32_t length;
628 uint8_t buf[0];
629};
630
631typedef struct USBNetState {
632 USBDevice dev;
633
6c9f886c
AZ
634 enum rndis_state rndis_state;
635 uint32_t medium;
636 uint32_t speed;
637 uint32_t media_state;
638 uint16_t filter;
639 uint32_t vendorid;
6c9f886c
AZ
640
641 unsigned int out_ptr;
642 uint8_t out_buf[2048];
643
6c9f886c
AZ
644 unsigned int in_ptr, in_len;
645 uint8_t in_buf[2048];
646
8beba930
HG
647 USBEndpoint *intr;
648
6c9f886c 649 char usbstring_mac[13];
e0394b8b
MM
650 NICState *nic;
651 NICConf conf;
b58deb34 652 QTAILQ_HEAD(, rndis_response) rndis_resp;
6c9f886c
AZ
653} USBNetState;
654
fe47db72
GA
655#define TYPE_USB_NET "usb-net"
656#define USB_NET(obj) OBJECT_CHECK(USBNetState, (obj), TYPE_USB_NET)
657
a980a065
GH
658static int is_rndis(USBNetState *s)
659{
80eecda8
PP
660 return s->dev.config ?
661 s->dev.config->bConfigurationValue == DEV_RNDIS_CONFIG_VALUE : 0;
a980a065
GH
662}
663
6c9f886c 664static int ndis_query(USBNetState *s, uint32_t oid,
363a37d5
BS
665 uint8_t *inbuf, unsigned int inlen, uint8_t *outbuf,
666 size_t outlen)
6c9f886c 667{
b1503cda 668 unsigned int i;
6c9f886c
AZ
669
670 switch (oid) {
671 /* general oids (table 4-1) */
672 /* mandatory */
673 case OID_GEN_SUPPORTED_LIST:
ec9125bc
PM
674 for (i = 0; i < ARRAY_SIZE(oid_supported_list); i++) {
675 stl_le_p(outbuf + (i * sizeof(le32)), oid_supported_list[i]);
676 }
6c9f886c
AZ
677 return sizeof(oid_supported_list);
678
679 /* mandatory */
680 case OID_GEN_HARDWARE_STATUS:
ec9125bc 681 stl_le_p(outbuf, 0);
6c9f886c
AZ
682 return sizeof(le32);
683
684 /* mandatory */
685 case OID_GEN_MEDIA_SUPPORTED:
ec9125bc 686 stl_le_p(outbuf, s->medium);
6c9f886c
AZ
687 return sizeof(le32);
688
689 /* mandatory */
690 case OID_GEN_MEDIA_IN_USE:
ec9125bc 691 stl_le_p(outbuf, s->medium);
6c9f886c
AZ
692 return sizeof(le32);
693
694 /* mandatory */
695 case OID_GEN_MAXIMUM_FRAME_SIZE:
ec9125bc 696 stl_le_p(outbuf, ETH_FRAME_LEN);
6c9f886c
AZ
697 return sizeof(le32);
698
699 /* mandatory */
700 case OID_GEN_LINK_SPEED:
ec9125bc 701 stl_le_p(outbuf, s->speed);
6c9f886c
AZ
702 return sizeof(le32);
703
704 /* mandatory */
705 case OID_GEN_TRANSMIT_BLOCK_SIZE:
ec9125bc 706 stl_le_p(outbuf, ETH_FRAME_LEN);
6c9f886c
AZ
707 return sizeof(le32);
708
709 /* mandatory */
710 case OID_GEN_RECEIVE_BLOCK_SIZE:
ec9125bc 711 stl_le_p(outbuf, ETH_FRAME_LEN);
6c9f886c
AZ
712 return sizeof(le32);
713
714 /* mandatory */
715 case OID_GEN_VENDOR_ID:
ec9125bc 716 stl_le_p(outbuf, s->vendorid);
6c9f886c
AZ
717 return sizeof(le32);
718
719 /* mandatory */
720 case OID_GEN_VENDOR_DESCRIPTION:
b55266b5
BS
721 pstrcpy((char *)outbuf, outlen, "QEMU USB RNDIS Net");
722 return strlen((char *)outbuf) + 1;
6c9f886c
AZ
723
724 case OID_GEN_VENDOR_DRIVER_VERSION:
ec9125bc 725 stl_le_p(outbuf, 1);
6c9f886c
AZ
726 return sizeof(le32);
727
728 /* mandatory */
729 case OID_GEN_CURRENT_PACKET_FILTER:
ec9125bc 730 stl_le_p(outbuf, s->filter);
6c9f886c
AZ
731 return sizeof(le32);
732
733 /* mandatory */
734 case OID_GEN_MAXIMUM_TOTAL_SIZE:
ec9125bc 735 stl_le_p(outbuf, RNDIS_MAX_TOTAL_SIZE);
6c9f886c
AZ
736 return sizeof(le32);
737
738 /* mandatory */
739 case OID_GEN_MEDIA_CONNECT_STATUS:
ec9125bc 740 stl_le_p(outbuf, s->media_state);
6c9f886c
AZ
741 return sizeof(le32);
742
743 case OID_GEN_PHYSICAL_MEDIUM:
ec9125bc 744 stl_le_p(outbuf, 0);
6c9f886c
AZ
745 return sizeof(le32);
746
747 case OID_GEN_MAC_OPTIONS:
ec9125bc
PM
748 stl_le_p(outbuf, NDIS_MAC_OPTION_RECEIVE_SERIALIZED |
749 NDIS_MAC_OPTION_FULL_DUPLEX);
6c9f886c
AZ
750 return sizeof(le32);
751
752 /* statistics OIDs (table 4-2) */
753 /* mandatory */
754 case OID_GEN_XMIT_OK:
ec9125bc 755 stl_le_p(outbuf, 0);
6c9f886c
AZ
756 return sizeof(le32);
757
758 /* mandatory */
759 case OID_GEN_RCV_OK:
ec9125bc 760 stl_le_p(outbuf, 0);
6c9f886c
AZ
761 return sizeof(le32);
762
763 /* mandatory */
764 case OID_GEN_XMIT_ERROR:
ec9125bc 765 stl_le_p(outbuf, 0);
6c9f886c
AZ
766 return sizeof(le32);
767
768 /* mandatory */
769 case OID_GEN_RCV_ERROR:
ec9125bc 770 stl_le_p(outbuf, 0);
6c9f886c
AZ
771 return sizeof(le32);
772
773 /* mandatory */
774 case OID_GEN_RCV_NO_BUFFER:
ec9125bc 775 stl_le_p(outbuf, 0);
6c9f886c
AZ
776 return sizeof(le32);
777
778 /* ieee802.3 OIDs (table 4-3) */
779 /* mandatory */
780 case OID_802_3_PERMANENT_ADDRESS:
e0394b8b 781 memcpy(outbuf, s->conf.macaddr.a, 6);
6c9f886c
AZ
782 return 6;
783
784 /* mandatory */
785 case OID_802_3_CURRENT_ADDRESS:
e0394b8b 786 memcpy(outbuf, s->conf.macaddr.a, 6);
6c9f886c
AZ
787 return 6;
788
789 /* mandatory */
790 case OID_802_3_MULTICAST_LIST:
ec9125bc 791 stl_le_p(outbuf, 0xe0000000);
6c9f886c
AZ
792 return sizeof(le32);
793
794 /* mandatory */
795 case OID_802_3_MAXIMUM_LIST_SIZE:
ec9125bc 796 stl_le_p(outbuf, 1);
6c9f886c
AZ
797 return sizeof(le32);
798
799 case OID_802_3_MAC_OPTIONS:
800 return 0;
801
802 /* ieee802.3 statistics OIDs (table 4-4) */
803 /* mandatory */
804 case OID_802_3_RCV_ERROR_ALIGNMENT:
ec9125bc 805 stl_le_p(outbuf, 0);
6c9f886c
AZ
806 return sizeof(le32);
807
808 /* mandatory */
809 case OID_802_3_XMIT_ONE_COLLISION:
ec9125bc 810 stl_le_p(outbuf, 0);
6c9f886c
AZ
811 return sizeof(le32);
812
813 /* mandatory */
814 case OID_802_3_XMIT_MORE_COLLISIONS:
ec9125bc 815 stl_le_p(outbuf, 0);
6c9f886c
AZ
816 return sizeof(le32);
817
818 default:
819 fprintf(stderr, "usbnet: unknown OID 0x%08x\n", oid);
820 return 0;
821 }
822 return -1;
823}
824
825static int ndis_set(USBNetState *s, uint32_t oid,
826 uint8_t *inbuf, unsigned int inlen)
827{
828 switch (oid) {
829 case OID_GEN_CURRENT_PACKET_FILTER:
ec9125bc 830 s->filter = ldl_le_p(inbuf);
6c9f886c
AZ
831 if (s->filter) {
832 s->rndis_state = RNDIS_DATA_INITIALIZED;
833 } else {
834 s->rndis_state = RNDIS_INITIALIZED;
835 }
836 return 0;
837
838 case OID_802_3_MULTICAST_LIST:
839 return 0;
840 }
841 return -1;
842}
843
844static int rndis_get_response(USBNetState *s, uint8_t *buf)
845{
846 int ret = 0;
847 struct rndis_response *r = s->rndis_resp.tqh_first;
848
849 if (!r)
850 return ret;
851
72cf2d4f 852 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
6c9f886c
AZ
853 ret = r->length;
854 memcpy(buf, r->buf, r->length);
7267c094 855 g_free(r);
6c9f886c
AZ
856
857 return ret;
858}
859
860static void *rndis_queue_response(USBNetState *s, unsigned int length)
861{
862 struct rndis_response *r =
7267c094 863 g_malloc0(sizeof(struct rndis_response) + length);
6c9f886c 864
8beba930 865 if (QTAILQ_EMPTY(&s->rndis_resp)) {
8550a02d 866 usb_wakeup(s->intr, 0);
8beba930
HG
867 }
868
72cf2d4f 869 QTAILQ_INSERT_TAIL(&s->rndis_resp, r, entries);
6c9f886c
AZ
870 r->length = length;
871
872 return &r->buf[0];
873}
874
875static void rndis_clear_responsequeue(USBNetState *s)
876{
877 struct rndis_response *r;
878
879 while ((r = s->rndis_resp.tqh_first)) {
72cf2d4f 880 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
7267c094 881 g_free(r);
6c9f886c
AZ
882 }
883}
884
885static int rndis_init_response(USBNetState *s, rndis_init_msg_type *buf)
886{
887 rndis_init_cmplt_type *resp =
888 rndis_queue_response(s, sizeof(rndis_init_cmplt_type));
889
890 if (!resp)
891 return USB_RET_STALL;
892
893 resp->MessageType = cpu_to_le32(RNDIS_INITIALIZE_CMPLT);
894 resp->MessageLength = cpu_to_le32(sizeof(rndis_init_cmplt_type));
895 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
896 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
897 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
898 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
899 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
900 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
901 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
902 resp->MaxTransferSize = cpu_to_le32(ETH_FRAME_LEN +
903 sizeof(struct rndis_packet_msg_type) + 22);
904 resp->PacketAlignmentFactor = cpu_to_le32(0);
905 resp->AFListOffset = cpu_to_le32(0);
906 resp->AFListSize = cpu_to_le32(0);
907 return 0;
908}
909
910static int rndis_query_response(USBNetState *s,
911 rndis_query_msg_type *buf, unsigned int length)
912{
913 rndis_query_cmplt_type *resp;
914 /* oid_supported_list is the largest data reply */
915 uint8_t infobuf[sizeof(oid_supported_list)];
916 uint32_t bufoffs, buflen;
917 int infobuflen;
918 unsigned int resplen;
919
920 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
921 buflen = le32_to_cpu(buf->InformationBufferLength);
fe3c546c 922 if (buflen > length || bufoffs >= length || bufoffs + buflen > length) {
6c9f886c 923 return USB_RET_STALL;
fe3c546c 924 }
6c9f886c
AZ
925
926 infobuflen = ndis_query(s, le32_to_cpu(buf->OID),
363a37d5
BS
927 bufoffs + (uint8_t *) buf, buflen, infobuf,
928 sizeof(infobuf));
6c9f886c
AZ
929 resplen = sizeof(rndis_query_cmplt_type) +
930 ((infobuflen < 0) ? 0 : infobuflen);
931 resp = rndis_queue_response(s, resplen);
932 if (!resp)
933 return USB_RET_STALL;
934
935 resp->MessageType = cpu_to_le32(RNDIS_QUERY_CMPLT);
936 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
937 resp->MessageLength = cpu_to_le32(resplen);
938
939 if (infobuflen < 0) {
940 /* OID not supported */
941 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
942 resp->InformationBufferLength = cpu_to_le32(0);
943 resp->InformationBufferOffset = cpu_to_le32(0);
944 return 0;
945 }
946
947 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
948 resp->InformationBufferOffset =
949 cpu_to_le32(infobuflen ? sizeof(rndis_query_cmplt_type) - 8 : 0);
950 resp->InformationBufferLength = cpu_to_le32(infobuflen);
951 memcpy(resp + 1, infobuf, infobuflen);
952
953 return 0;
954}
955
956static int rndis_set_response(USBNetState *s,
957 rndis_set_msg_type *buf, unsigned int length)
958{
959 rndis_set_cmplt_type *resp =
960 rndis_queue_response(s, sizeof(rndis_set_cmplt_type));
961 uint32_t bufoffs, buflen;
962 int ret;
963
964 if (!resp)
965 return USB_RET_STALL;
966
967 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
968 buflen = le32_to_cpu(buf->InformationBufferLength);
fe3c546c 969 if (buflen > length || bufoffs >= length || bufoffs + buflen > length) {
6c9f886c 970 return USB_RET_STALL;
fe3c546c 971 }
6c9f886c
AZ
972
973 ret = ndis_set(s, le32_to_cpu(buf->OID),
974 bufoffs + (uint8_t *) buf, buflen);
975 resp->MessageType = cpu_to_le32(RNDIS_SET_CMPLT);
976 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
977 resp->MessageLength = cpu_to_le32(sizeof(rndis_set_cmplt_type));
978 if (ret < 0) {
979 /* OID not supported */
980 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
981 return 0;
982 }
983 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
984
985 return 0;
986}
987
988static int rndis_reset_response(USBNetState *s, rndis_reset_msg_type *buf)
989{
990 rndis_reset_cmplt_type *resp =
991 rndis_queue_response(s, sizeof(rndis_reset_cmplt_type));
992
993 if (!resp)
994 return USB_RET_STALL;
995
996 resp->MessageType = cpu_to_le32(RNDIS_RESET_CMPLT);
997 resp->MessageLength = cpu_to_le32(sizeof(rndis_reset_cmplt_type));
998 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
999 resp->AddressingReset = cpu_to_le32(1); /* reset information */
1000
1001 return 0;
1002}
1003
1004static int rndis_keepalive_response(USBNetState *s,
1005 rndis_keepalive_msg_type *buf)
1006{
1007 rndis_keepalive_cmplt_type *resp =
1008 rndis_queue_response(s, sizeof(rndis_keepalive_cmplt_type));
1009
1010 if (!resp)
1011 return USB_RET_STALL;
1012
1013 resp->MessageType = cpu_to_le32(RNDIS_KEEPALIVE_CMPLT);
1014 resp->MessageLength = cpu_to_le32(sizeof(rndis_keepalive_cmplt_type));
1015 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
1016 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
1017
1018 return 0;
1019}
1020
190563f9
SH
1021/* Prepare to receive the next packet */
1022static void usb_net_reset_in_buf(USBNetState *s)
1023{
1024 s->in_ptr = s->in_len = 0;
b356f76d 1025 qemu_flush_queued_packets(qemu_get_queue(s->nic));
190563f9
SH
1026}
1027
6c9f886c
AZ
1028static int rndis_parse(USBNetState *s, uint8_t *data, int length)
1029{
ec9125bc 1030 uint32_t msg_type = ldl_le_p(data);
6c9f886c
AZ
1031
1032 switch (msg_type) {
1033 case RNDIS_INITIALIZE_MSG:
1034 s->rndis_state = RNDIS_INITIALIZED;
1035 return rndis_init_response(s, (rndis_init_msg_type *) data);
1036
1037 case RNDIS_HALT_MSG:
1038 s->rndis_state = RNDIS_UNINITIALIZED;
1039 return 0;
1040
1041 case RNDIS_QUERY_MSG:
1042 return rndis_query_response(s, (rndis_query_msg_type *) data, length);
1043
1044 case RNDIS_SET_MSG:
1045 return rndis_set_response(s, (rndis_set_msg_type *) data, length);
1046
1047 case RNDIS_RESET_MSG:
1048 rndis_clear_responsequeue(s);
190563f9
SH
1049 s->out_ptr = 0;
1050 usb_net_reset_in_buf(s);
6c9f886c
AZ
1051 return rndis_reset_response(s, (rndis_reset_msg_type *) data);
1052
1053 case RNDIS_KEEPALIVE_MSG:
1054 /* For USB: host does this every 5 seconds */
1055 return rndis_keepalive_response(s, (rndis_keepalive_msg_type *) data);
1056 }
1057
1058 return USB_RET_STALL;
1059}
1060
1061static void usb_net_handle_reset(USBDevice *dev)
1062{
1063}
1064
9a77a0f5 1065static void usb_net_handle_control(USBDevice *dev, USBPacket *p,
007fd62f 1066 int request, int value, int index, int length, uint8_t *data)
6c9f886c
AZ
1067{
1068 USBNetState *s = (USBNetState *) dev;
30c7d32a 1069 int ret;
6c9f886c 1070
007fd62f 1071 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
30c7d32a 1072 if (ret >= 0) {
9a77a0f5 1073 return;
30c7d32a
GH
1074 }
1075
6c9f886c 1076 switch(request) {
6c9f886c 1077 case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
a980a065 1078 if (!is_rndis(s) || value || index != 0) {
6c9f886c 1079 goto fail;
a980a065 1080 }
eb38c52c 1081#ifdef TRAFFIC_DEBUG
6c9f886c
AZ
1082 {
1083 unsigned int i;
1084 fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");
1085 for (i = 0; i < length; i++) {
1086 if (!(i & 15))
1087 fprintf(stderr, "\n%04x:", i);
1088 fprintf(stderr, " %02x", data[i]);
1089 }
1090 fprintf(stderr, "\n\n");
1091 }
1092#endif
1093 ret = rndis_parse(s, data, length);
9a77a0f5
HG
1094 if (ret < 0) {
1095 p->status = ret;
1096 }
6c9f886c
AZ
1097 break;
1098
1099 case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
a980a065 1100 if (!is_rndis(s) || value || index != 0) {
6c9f886c 1101 goto fail;
a980a065 1102 }
9a77a0f5
HG
1103 p->actual_length = rndis_get_response(s, data);
1104 if (p->actual_length == 0) {
6c9f886c 1105 data[0] = 0;
9a77a0f5 1106 p->actual_length = 1;
6c9f886c 1107 }
eb38c52c 1108#ifdef TRAFFIC_DEBUG
6c9f886c
AZ
1109 {
1110 unsigned int i;
1111 fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");
9a77a0f5 1112 for (i = 0; i < p->actual_length; i++) {
6c9f886c
AZ
1113 if (!(i & 15))
1114 fprintf(stderr, "\n%04x:", i);
1115 fprintf(stderr, " %02x", data[i]);
1116 }
1117 fprintf(stderr, "\n\n");
1118 }
1119#endif
1120 break;
1121
6c9f886c
AZ
1122 default:
1123 fail:
1124 fprintf(stderr, "usbnet: failed control transaction: "
1125 "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1126 request, value, index, length);
9a77a0f5 1127 p->status = USB_RET_STALL;
6c9f886c
AZ
1128 break;
1129 }
6c9f886c
AZ
1130}
1131
9a77a0f5 1132static void usb_net_handle_statusin(USBNetState *s, USBPacket *p)
6c9f886c 1133{
4f4321c1 1134 le32 buf[2];
6c9f886c 1135
4f4321c1 1136 if (p->iov.size < 8) {
9a77a0f5
HG
1137 p->status = USB_RET_STALL;
1138 return;
4f4321c1 1139 }
6c9f886c 1140
4f4321c1
GH
1141 buf[0] = cpu_to_le32(1);
1142 buf[1] = cpu_to_le32(0);
1143 usb_packet_copy(p, buf, 8);
9a77a0f5
HG
1144 if (!s->rndis_resp.tqh_first) {
1145 p->status = USB_RET_NAK;
1146 }
6c9f886c 1147
eb38c52c 1148#ifdef TRAFFIC_DEBUG
4f4321c1 1149 fprintf(stderr, "usbnet: interrupt poll len %zu return %d",
9a77a0f5
HG
1150 p->iov.size, p->status);
1151 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->status);
6c9f886c 1152#endif
6c9f886c
AZ
1153}
1154
9a77a0f5 1155static void usb_net_handle_datain(USBNetState *s, USBPacket *p)
6c9f886c 1156{
9a77a0f5 1157 int len;
6c9f886c
AZ
1158
1159 if (s->in_ptr > s->in_len) {
190563f9 1160 usb_net_reset_in_buf(s);
9a77a0f5
HG
1161 p->status = USB_RET_NAK;
1162 return;
6c9f886c
AZ
1163 }
1164 if (!s->in_len) {
9a77a0f5
HG
1165 p->status = USB_RET_NAK;
1166 return;
6c9f886c 1167 }
9a77a0f5
HG
1168 len = s->in_len - s->in_ptr;
1169 if (len > p->iov.size) {
1170 len = p->iov.size;
4f4321c1 1171 }
9a77a0f5
HG
1172 usb_packet_copy(p, &s->in_buf[s->in_ptr], len);
1173 s->in_ptr += len;
6c9f886c 1174 if (s->in_ptr >= s->in_len &&
9a77a0f5 1175 (is_rndis(s) || (s->in_len & (64 - 1)) || !len)) {
6c9f886c 1176 /* no short packet necessary */
190563f9 1177 usb_net_reset_in_buf(s);
6c9f886c
AZ
1178 }
1179
eb38c52c 1180#ifdef TRAFFIC_DEBUG
9a77a0f5
HG
1181 fprintf(stderr, "usbnet: data in len %zu return %d", p->iov.size, len);
1182 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", len);
6c9f886c 1183#endif
6c9f886c
AZ
1184}
1185
9a77a0f5 1186static void usb_net_handle_dataout(USBNetState *s, USBPacket *p)
6c9f886c 1187{
6c9f886c
AZ
1188 int sz = sizeof(s->out_buf) - s->out_ptr;
1189 struct rndis_packet_msg_type *msg =
1190 (struct rndis_packet_msg_type *) s->out_buf;
1191 uint32_t len;
1192
eb38c52c 1193#ifdef TRAFFIC_DEBUG
4f4321c1
GH
1194 fprintf(stderr, "usbnet: data out len %zu\n", p->iov.size);
1195 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->iov.size);
6c9f886c
AZ
1196#endif
1197
9a77a0f5
HG
1198 if (sz > p->iov.size) {
1199 sz = p->iov.size;
1200 }
4f4321c1 1201 usb_packet_copy(p, &s->out_buf[s->out_ptr], sz);
6c9f886c
AZ
1202 s->out_ptr += sz;
1203
a980a065 1204 if (!is_rndis(s)) {
9a77a0f5 1205 if (p->iov.size < 64) {
b356f76d 1206 qemu_send_packet(qemu_get_queue(s->nic), s->out_buf, s->out_ptr);
6c9f886c
AZ
1207 s->out_ptr = 0;
1208 }
9a77a0f5 1209 return;
6c9f886c
AZ
1210 }
1211 len = le32_to_cpu(msg->MessageLength);
9a77a0f5
HG
1212 if (s->out_ptr < 8 || s->out_ptr < len) {
1213 return;
1214 }
6c9f886c
AZ
1215 if (le32_to_cpu(msg->MessageType) == RNDIS_PACKET_MSG) {
1216 uint32_t offs = 8 + le32_to_cpu(msg->DataOffset);
1217 uint32_t size = le32_to_cpu(msg->DataLength);
fe3c546c 1218 if (offs < len && size < len && offs + size <= len) {
b356f76d 1219 qemu_send_packet(qemu_get_queue(s->nic), s->out_buf + offs, size);
fe3c546c 1220 }
6c9f886c
AZ
1221 }
1222 s->out_ptr -= len;
1223 memmove(s->out_buf, &s->out_buf[len], s->out_ptr);
6c9f886c
AZ
1224}
1225
9a77a0f5 1226static void usb_net_handle_data(USBDevice *dev, USBPacket *p)
6c9f886c
AZ
1227{
1228 USBNetState *s = (USBNetState *) dev;
6c9f886c
AZ
1229
1230 switch(p->pid) {
1231 case USB_TOKEN_IN:
079d0b7f 1232 switch (p->ep->nr) {
6c9f886c 1233 case 1:
9a77a0f5 1234 usb_net_handle_statusin(s, p);
6c9f886c
AZ
1235 break;
1236
1237 case 2:
9a77a0f5 1238 usb_net_handle_datain(s, p);
6c9f886c
AZ
1239 break;
1240
1241 default:
1242 goto fail;
1243 }
1244 break;
1245
1246 case USB_TOKEN_OUT:
079d0b7f 1247 switch (p->ep->nr) {
6c9f886c 1248 case 2:
9a77a0f5 1249 usb_net_handle_dataout(s, p);
6c9f886c
AZ
1250 break;
1251
1252 default:
1253 goto fail;
1254 }
1255 break;
1256
1257 default:
1258 fail:
9a77a0f5 1259 p->status = USB_RET_STALL;
6c9f886c
AZ
1260 break;
1261 }
9a77a0f5
HG
1262
1263 if (p->status == USB_RET_STALL) {
6c9f886c 1264 fprintf(stderr, "usbnet: failed data transaction: "
4f4321c1 1265 "pid 0x%x ep 0x%x len 0x%zx\n",
079d0b7f 1266 p->pid, p->ep->nr, p->iov.size);
9a77a0f5 1267 }
6c9f886c
AZ
1268}
1269
4e68f7a0 1270static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t size)
6c9f886c 1271{
cc1f0f45 1272 USBNetState *s = qemu_get_nic_opaque(nc);
f237ddbb
SH
1273 uint8_t *in_buf = s->in_buf;
1274 size_t total_size = size;
6c9f886c 1275
91344024
FZ
1276 if (!s->dev.config) {
1277 return -1;
1278 }
1279
a980a065 1280 if (is_rndis(s)) {
98d23704 1281 if (s->rndis_state != RNDIS_DATA_INITIALIZED) {
4f1c942b 1282 return -1;
98d23704 1283 }
f237ddbb
SH
1284 total_size += sizeof(struct rndis_packet_msg_type);
1285 }
1286 if (total_size > sizeof(s->in_buf)) {
1287 return -1;
1288 }
6c9f886c 1289
190563f9
SH
1290 /* Only accept packet if input buffer is empty */
1291 if (s->in_len > 0) {
1292 return 0;
1293 }
1294
f237ddbb
SH
1295 if (is_rndis(s)) {
1296 struct rndis_packet_msg_type *msg;
1297
1298 msg = (struct rndis_packet_msg_type *)in_buf;
6c9f886c
AZ
1299 memset(msg, 0, sizeof(struct rndis_packet_msg_type));
1300 msg->MessageType = cpu_to_le32(RNDIS_PACKET_MSG);
f237ddbb
SH
1301 msg->MessageLength = cpu_to_le32(size + sizeof(*msg));
1302 msg->DataOffset = cpu_to_le32(sizeof(*msg) - 8);
6c9f886c
AZ
1303 msg->DataLength = cpu_to_le32(size);
1304 /* msg->OOBDataOffset;
1305 * msg->OOBDataLength;
1306 * msg->NumOOBDataElements;
1307 * msg->PerPacketInfoOffset;
1308 * msg->PerPacketInfoLength;
1309 * msg->VcHandle;
1310 * msg->Reserved;
1311 */
f237ddbb 1312 in_buf += sizeof(*msg);
6c9f886c 1313 }
f237ddbb
SH
1314
1315 memcpy(in_buf, buf, size);
1316 s->in_len = total_size;
6c9f886c 1317 s->in_ptr = 0;
4f1c942b 1318 return size;
6c9f886c
AZ
1319}
1320
4e68f7a0 1321static void usbnet_cleanup(NetClientState *nc)
b946a153 1322{
cc1f0f45 1323 USBNetState *s = qemu_get_nic_opaque(nc);
b946a153 1324
42be86ce 1325 s->nic = NULL;
b946a153
AL
1326}
1327
c4fe9700 1328static void usb_net_unrealize(USBDevice *dev, Error **errp)
6c9f886c
AZ
1329{
1330 USBNetState *s = (USBNetState *) dev;
1331
dcf414d6 1332 /* TODO: remove the nd_table[] entry */
42be86ce 1333 rndis_clear_responsequeue(s);
948ecf21 1334 qemu_del_nic(s->nic);
6c9f886c
AZ
1335}
1336
42be86ce 1337static NetClientInfo net_usbnet_info = {
f394b2e2 1338 .type = NET_CLIENT_DRIVER_NIC,
42be86ce 1339 .size = sizeof(NICState),
42be86ce
GH
1340 .receive = usbnet_receive,
1341 .cleanup = usbnet_cleanup,
1342};
1343
d73ad359 1344static void usb_net_realize(USBDevice *dev, Error **errrp)
6c9f886c 1345{
fe47db72 1346 USBNetState *s = USB_NET(dev);
6c9f886c 1347
9d55d1ad 1348 usb_desc_create_serial(dev);
a980a065 1349 usb_desc_init(dev);
6c9f886c 1350
6c9f886c 1351 s->rndis_state = RNDIS_UNINITIALIZED;
72cf2d4f 1352 QTAILQ_INIT(&s->rndis_resp);
806b6024 1353
6c9f886c
AZ
1354 s->medium = 0; /* NDIS_MEDIUM_802_3 */
1355 s->speed = 1000000; /* 100MBps, in 100Bps units */
1356 s->media_state = 0; /* NDIS_MEDIA_STATE_CONNECTED */;
1357 s->filter = 0;
1358 s->vendorid = 0x1234;
8beba930 1359 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
42be86ce
GH
1360
1361 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1362 s->nic = qemu_new_nic(&net_usbnet_info, &s->conf,
f79f2bfc 1363 object_get_typename(OBJECT(s)), s->dev.qdev.id, s);
b356f76d 1364 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
42be86ce
GH
1365 snprintf(s->usbstring_mac, sizeof(s->usbstring_mac),
1366 "%02x%02x%02x%02x%02x%02x",
1367 0x40,
1368 s->conf.macaddr.a[1],
1369 s->conf.macaddr.a[2],
1370 s->conf.macaddr.a[3],
1371 s->conf.macaddr.a[4],
1372 s->conf.macaddr.a[5]);
30c7d32a 1373 usb_desc_set_string(dev, STRING_ETHADDR, s->usbstring_mac);
806b6024
GH
1374}
1375
c11f4bc9
GA
1376static void usb_net_instance_init(Object *obj)
1377{
1378 USBDevice *dev = USB_DEVICE(obj);
fe47db72 1379 USBNetState *s = USB_NET(dev);
c11f4bc9
GA
1380
1381 device_add_bootindex_property(obj, &s->conf.bootindex,
1382 "bootindex", "/ethernet-phy@0",
1383 &dev->qdev, NULL);
1384}
1385
4ab0ba9e
GH
1386static const VMStateDescription vmstate_usb_net = {
1387 .name = "usb-net",
1388 .unmigratable = 1,
1389};
1390
39bffca2
AL
1391static Property net_properties[] = {
1392 DEFINE_NIC_PROPERTIES(USBNetState, conf),
1393 DEFINE_PROP_END_OF_LIST(),
1394};
1395
62aed765
AL
1396static void usb_net_class_initfn(ObjectClass *klass, void *data)
1397{
39bffca2 1398 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
1399 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1400
d73ad359 1401 uc->realize = usb_net_realize;
62aed765
AL
1402 uc->product_desc = "QEMU USB Network Interface";
1403 uc->usb_desc = &desc_net;
62aed765
AL
1404 uc->handle_reset = usb_net_handle_reset;
1405 uc->handle_control = usb_net_handle_control;
1406 uc->handle_data = usb_net_handle_data;
c4fe9700 1407 uc->unrealize = usb_net_unrealize;
125ee0ed 1408 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
39bffca2
AL
1409 dc->fw_name = "network";
1410 dc->vmsd = &vmstate_usb_net;
1411 dc->props = net_properties;
62aed765
AL
1412}
1413
8c43a6f0 1414static const TypeInfo net_info = {
fe47db72 1415 .name = TYPE_USB_NET,
39bffca2
AL
1416 .parent = TYPE_USB_DEVICE,
1417 .instance_size = sizeof(USBNetState),
1418 .class_init = usb_net_class_initfn,
c11f4bc9 1419 .instance_init = usb_net_instance_init,
806b6024
GH
1420};
1421
83f7d43a 1422static void usb_net_register_types(void)
806b6024 1423{
39bffca2 1424 type_register_static(&net_info);
806b6024 1425}
83f7d43a
AF
1426
1427type_init(usb_net_register_types)