net: reorganize headers
[qemu/ar7.git] / hw / usb / dev-network.c
blobbf289ff5a601d1db8b88093cf95c8cf2d54b6666
1 /*
2 * QEMU USB Net devices
4 * Copyright (c) 2006 Thomas Sailer
5 * Copyright (c) 2008 Andrzej Zaborowski
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "qemu-common.h"
27 #include "hw/usb.h"
28 #include "hw/usb/desc.h"
29 #include "net/net.h"
30 #include "qemu-queue.h"
31 #include "qemu-config.h"
32 #include "sysemu.h"
33 #include "iov.h"
35 /*#define TRAFFIC_DEBUG*/
36 /* Thanks to NetChip Technologies for donating this product ID.
37 * It's for devices with only CDC Ethernet configurations.
39 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
40 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
41 /* For hardware that can talk RNDIS and either of the above protocols,
42 * use this ID ... the windows INF files will know it.
44 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
45 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
47 enum usbstring_idx {
48 STRING_MANUFACTURER = 1,
49 STRING_PRODUCT,
50 STRING_ETHADDR,
51 STRING_DATA,
52 STRING_CONTROL,
53 STRING_RNDIS_CONTROL,
54 STRING_CDC,
55 STRING_SUBSET,
56 STRING_RNDIS,
57 STRING_SERIALNUMBER,
60 #define DEV_CONFIG_VALUE 1 /* CDC or a subset */
61 #define DEV_RNDIS_CONFIG_VALUE 2 /* RNDIS; optional */
63 #define USB_CDC_SUBCLASS_ACM 0x02
64 #define USB_CDC_SUBCLASS_ETHERNET 0x06
66 #define USB_CDC_PROTO_NONE 0
67 #define USB_CDC_ACM_PROTO_VENDOR 0xff
69 #define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
70 #define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
71 #define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
72 #define USB_CDC_UNION_TYPE 0x06 /* union_desc */
73 #define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
75 #define USB_CDC_SEND_ENCAPSULATED_COMMAND 0x00
76 #define USB_CDC_GET_ENCAPSULATED_RESPONSE 0x01
77 #define USB_CDC_REQ_SET_LINE_CODING 0x20
78 #define USB_CDC_REQ_GET_LINE_CODING 0x21
79 #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
80 #define USB_CDC_REQ_SEND_BREAK 0x23
81 #define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40
82 #define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41
83 #define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42
84 #define USB_CDC_SET_ETHERNET_PACKET_FILTER 0x43
85 #define USB_CDC_GET_ETHERNET_STATISTIC 0x44
87 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
88 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
90 #define ETH_FRAME_LEN 1514 /* Max. octets in frame sans FCS */
92 static const USBDescStrings usb_net_stringtable = {
93 [STRING_MANUFACTURER] = "QEMU",
94 [STRING_PRODUCT] = "RNDIS/QEMU USB Network Device",
95 [STRING_ETHADDR] = "400102030405",
96 [STRING_DATA] = "QEMU USB Net Data Interface",
97 [STRING_CONTROL] = "QEMU USB Net Control Interface",
98 [STRING_RNDIS_CONTROL] = "QEMU USB Net RNDIS Control Interface",
99 [STRING_CDC] = "QEMU USB Net CDC",
100 [STRING_SUBSET] = "QEMU USB Net Subset",
101 [STRING_RNDIS] = "QEMU USB Net RNDIS",
102 [STRING_SERIALNUMBER] = "1",
105 static const USBDescIface desc_iface_rndis[] = {
107 /* RNDIS Control Interface */
108 .bInterfaceNumber = 0,
109 .bNumEndpoints = 1,
110 .bInterfaceClass = USB_CLASS_COMM,
111 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
112 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
113 .iInterface = STRING_RNDIS_CONTROL,
114 .ndesc = 4,
115 .descs = (USBDescOther[]) {
117 /* Header Descriptor */
118 .data = (uint8_t[]) {
119 0x05, /* u8 bLength */
120 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
121 USB_CDC_HEADER_TYPE, /* u8 bDescriptorSubType */
122 0x10, 0x01, /* le16 bcdCDC */
125 /* Call Management Descriptor */
126 .data = (uint8_t[]) {
127 0x05, /* u8 bLength */
128 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
129 USB_CDC_CALL_MANAGEMENT_TYPE, /* u8 bDescriptorSubType */
130 0x00, /* u8 bmCapabilities */
131 0x01, /* u8 bDataInterface */
134 /* ACM Descriptor */
135 .data = (uint8_t[]) {
136 0x04, /* u8 bLength */
137 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
138 USB_CDC_ACM_TYPE, /* u8 bDescriptorSubType */
139 0x00, /* u8 bmCapabilities */
142 /* Union Descriptor */
143 .data = (uint8_t[]) {
144 0x05, /* u8 bLength */
145 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
146 USB_CDC_UNION_TYPE, /* u8 bDescriptorSubType */
147 0x00, /* u8 bMasterInterface0 */
148 0x01, /* u8 bSlaveInterface0 */
152 .eps = (USBDescEndpoint[]) {
154 .bEndpointAddress = USB_DIR_IN | 0x01,
155 .bmAttributes = USB_ENDPOINT_XFER_INT,
156 .wMaxPacketSize = STATUS_BYTECOUNT,
157 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
161 /* RNDIS Data Interface */
162 .bInterfaceNumber = 1,
163 .bNumEndpoints = 2,
164 .bInterfaceClass = USB_CLASS_CDC_DATA,
165 .iInterface = STRING_DATA,
166 .eps = (USBDescEndpoint[]) {
168 .bEndpointAddress = USB_DIR_IN | 0x02,
169 .bmAttributes = USB_ENDPOINT_XFER_BULK,
170 .wMaxPacketSize = 0x40,
172 .bEndpointAddress = USB_DIR_OUT | 0x02,
173 .bmAttributes = USB_ENDPOINT_XFER_BULK,
174 .wMaxPacketSize = 0x40,
180 static const USBDescIface desc_iface_cdc[] = {
182 /* CDC Control Interface */
183 .bInterfaceNumber = 0,
184 .bNumEndpoints = 1,
185 .bInterfaceClass = USB_CLASS_COMM,
186 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
187 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
188 .iInterface = STRING_CONTROL,
189 .ndesc = 3,
190 .descs = (USBDescOther[]) {
192 /* Header Descriptor */
193 .data = (uint8_t[]) {
194 0x05, /* u8 bLength */
195 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
196 USB_CDC_HEADER_TYPE, /* u8 bDescriptorSubType */
197 0x10, 0x01, /* le16 bcdCDC */
200 /* Union Descriptor */
201 .data = (uint8_t[]) {
202 0x05, /* u8 bLength */
203 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
204 USB_CDC_UNION_TYPE, /* u8 bDescriptorSubType */
205 0x00, /* u8 bMasterInterface0 */
206 0x01, /* u8 bSlaveInterface0 */
209 /* Ethernet Descriptor */
210 .data = (uint8_t[]) {
211 0x0d, /* u8 bLength */
212 USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
213 USB_CDC_ETHERNET_TYPE, /* u8 bDescriptorSubType */
214 STRING_ETHADDR, /* u8 iMACAddress */
215 0x00, 0x00, 0x00, 0x00, /* le32 bmEthernetStatistics */
216 ETH_FRAME_LEN & 0xff,
217 ETH_FRAME_LEN >> 8, /* le16 wMaxSegmentSize */
218 0x00, 0x00, /* le16 wNumberMCFilters */
219 0x00, /* u8 bNumberPowerFilters */
223 .eps = (USBDescEndpoint[]) {
225 .bEndpointAddress = USB_DIR_IN | 0x01,
226 .bmAttributes = USB_ENDPOINT_XFER_INT,
227 .wMaxPacketSize = STATUS_BYTECOUNT,
228 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
232 /* CDC Data Interface (off) */
233 .bInterfaceNumber = 1,
234 .bAlternateSetting = 0,
235 .bNumEndpoints = 0,
236 .bInterfaceClass = USB_CLASS_CDC_DATA,
238 /* CDC Data Interface */
239 .bInterfaceNumber = 1,
240 .bAlternateSetting = 1,
241 .bNumEndpoints = 2,
242 .bInterfaceClass = USB_CLASS_CDC_DATA,
243 .iInterface = STRING_DATA,
244 .eps = (USBDescEndpoint[]) {
246 .bEndpointAddress = USB_DIR_IN | 0x02,
247 .bmAttributes = USB_ENDPOINT_XFER_BULK,
248 .wMaxPacketSize = 0x40,
250 .bEndpointAddress = USB_DIR_OUT | 0x02,
251 .bmAttributes = USB_ENDPOINT_XFER_BULK,
252 .wMaxPacketSize = 0x40,
258 static const USBDescDevice desc_device_net = {
259 .bcdUSB = 0x0200,
260 .bDeviceClass = USB_CLASS_COMM,
261 .bMaxPacketSize0 = 0x40,
262 .bNumConfigurations = 2,
263 .confs = (USBDescConfig[]) {
265 .bNumInterfaces = 2,
266 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
267 .iConfiguration = STRING_RNDIS,
268 .bmAttributes = 0xc0,
269 .bMaxPower = 0x32,
270 .nif = ARRAY_SIZE(desc_iface_rndis),
271 .ifs = desc_iface_rndis,
273 .bNumInterfaces = 2,
274 .bConfigurationValue = DEV_CONFIG_VALUE,
275 .iConfiguration = STRING_CDC,
276 .bmAttributes = 0xc0,
277 .bMaxPower = 0x32,
278 .nif = ARRAY_SIZE(desc_iface_cdc),
279 .ifs = desc_iface_cdc,
284 static const USBDesc desc_net = {
285 .id = {
286 .idVendor = RNDIS_VENDOR_NUM,
287 .idProduct = RNDIS_PRODUCT_NUM,
288 .bcdDevice = 0,
289 .iManufacturer = STRING_MANUFACTURER,
290 .iProduct = STRING_PRODUCT,
291 .iSerialNumber = STRING_SERIALNUMBER,
293 .full = &desc_device_net,
294 .str = usb_net_stringtable,
298 * RNDIS Definitions - in theory not specific to USB.
300 #define RNDIS_MAXIMUM_FRAME_SIZE 1518
301 #define RNDIS_MAX_TOTAL_SIZE 1558
303 /* Remote NDIS Versions */
304 #define RNDIS_MAJOR_VERSION 1
305 #define RNDIS_MINOR_VERSION 0
307 /* Status Values */
308 #define RNDIS_STATUS_SUCCESS 0x00000000U /* Success */
309 #define RNDIS_STATUS_FAILURE 0xc0000001U /* Unspecified error */
310 #define RNDIS_STATUS_INVALID_DATA 0xc0010015U /* Invalid data */
311 #define RNDIS_STATUS_NOT_SUPPORTED 0xc00000bbU /* Unsupported request */
312 #define RNDIS_STATUS_MEDIA_CONNECT 0x4001000bU /* Device connected */
313 #define RNDIS_STATUS_MEDIA_DISCONNECT 0x4001000cU /* Device disconnected */
315 /* Message Set for Connectionless (802.3) Devices */
316 enum {
317 RNDIS_PACKET_MSG = 1,
318 RNDIS_INITIALIZE_MSG = 2, /* Initialize device */
319 RNDIS_HALT_MSG = 3,
320 RNDIS_QUERY_MSG = 4,
321 RNDIS_SET_MSG = 5,
322 RNDIS_RESET_MSG = 6,
323 RNDIS_INDICATE_STATUS_MSG = 7,
324 RNDIS_KEEPALIVE_MSG = 8,
327 /* Message completion */
328 enum {
329 RNDIS_INITIALIZE_CMPLT = 0x80000002U,
330 RNDIS_QUERY_CMPLT = 0x80000004U,
331 RNDIS_SET_CMPLT = 0x80000005U,
332 RNDIS_RESET_CMPLT = 0x80000006U,
333 RNDIS_KEEPALIVE_CMPLT = 0x80000008U,
336 /* Device Flags */
337 enum {
338 RNDIS_DF_CONNECTIONLESS = 1,
339 RNDIS_DF_CONNECTIONORIENTED = 2,
342 #define RNDIS_MEDIUM_802_3 0x00000000U
344 /* from drivers/net/sk98lin/h/skgepnmi.h */
345 #define OID_PNP_CAPABILITIES 0xfd010100
346 #define OID_PNP_SET_POWER 0xfd010101
347 #define OID_PNP_QUERY_POWER 0xfd010102
348 #define OID_PNP_ADD_WAKE_UP_PATTERN 0xfd010103
349 #define OID_PNP_REMOVE_WAKE_UP_PATTERN 0xfd010104
350 #define OID_PNP_ENABLE_WAKE_UP 0xfd010106
352 typedef uint32_t le32;
354 typedef struct rndis_init_msg_type {
355 le32 MessageType;
356 le32 MessageLength;
357 le32 RequestID;
358 le32 MajorVersion;
359 le32 MinorVersion;
360 le32 MaxTransferSize;
361 } rndis_init_msg_type;
363 typedef struct rndis_init_cmplt_type {
364 le32 MessageType;
365 le32 MessageLength;
366 le32 RequestID;
367 le32 Status;
368 le32 MajorVersion;
369 le32 MinorVersion;
370 le32 DeviceFlags;
371 le32 Medium;
372 le32 MaxPacketsPerTransfer;
373 le32 MaxTransferSize;
374 le32 PacketAlignmentFactor;
375 le32 AFListOffset;
376 le32 AFListSize;
377 } rndis_init_cmplt_type;
379 typedef struct rndis_halt_msg_type {
380 le32 MessageType;
381 le32 MessageLength;
382 le32 RequestID;
383 } rndis_halt_msg_type;
385 typedef struct rndis_query_msg_type {
386 le32 MessageType;
387 le32 MessageLength;
388 le32 RequestID;
389 le32 OID;
390 le32 InformationBufferLength;
391 le32 InformationBufferOffset;
392 le32 DeviceVcHandle;
393 } rndis_query_msg_type;
395 typedef struct rndis_query_cmplt_type {
396 le32 MessageType;
397 le32 MessageLength;
398 le32 RequestID;
399 le32 Status;
400 le32 InformationBufferLength;
401 le32 InformationBufferOffset;
402 } rndis_query_cmplt_type;
404 typedef struct rndis_set_msg_type {
405 le32 MessageType;
406 le32 MessageLength;
407 le32 RequestID;
408 le32 OID;
409 le32 InformationBufferLength;
410 le32 InformationBufferOffset;
411 le32 DeviceVcHandle;
412 } rndis_set_msg_type;
414 typedef struct rndis_set_cmplt_type {
415 le32 MessageType;
416 le32 MessageLength;
417 le32 RequestID;
418 le32 Status;
419 } rndis_set_cmplt_type;
421 typedef struct rndis_reset_msg_type {
422 le32 MessageType;
423 le32 MessageLength;
424 le32 Reserved;
425 } rndis_reset_msg_type;
427 typedef struct rndis_reset_cmplt_type {
428 le32 MessageType;
429 le32 MessageLength;
430 le32 Status;
431 le32 AddressingReset;
432 } rndis_reset_cmplt_type;
434 typedef struct rndis_indicate_status_msg_type {
435 le32 MessageType;
436 le32 MessageLength;
437 le32 Status;
438 le32 StatusBufferLength;
439 le32 StatusBufferOffset;
440 } rndis_indicate_status_msg_type;
442 typedef struct rndis_keepalive_msg_type {
443 le32 MessageType;
444 le32 MessageLength;
445 le32 RequestID;
446 } rndis_keepalive_msg_type;
448 typedef struct rndis_keepalive_cmplt_type {
449 le32 MessageType;
450 le32 MessageLength;
451 le32 RequestID;
452 le32 Status;
453 } rndis_keepalive_cmplt_type;
455 struct rndis_packet_msg_type {
456 le32 MessageType;
457 le32 MessageLength;
458 le32 DataOffset;
459 le32 DataLength;
460 le32 OOBDataOffset;
461 le32 OOBDataLength;
462 le32 NumOOBDataElements;
463 le32 PerPacketInfoOffset;
464 le32 PerPacketInfoLength;
465 le32 VcHandle;
466 le32 Reserved;
469 struct rndis_config_parameter {
470 le32 ParameterNameOffset;
471 le32 ParameterNameLength;
472 le32 ParameterType;
473 le32 ParameterValueOffset;
474 le32 ParameterValueLength;
477 /* implementation specific */
478 enum rndis_state
480 RNDIS_UNINITIALIZED,
481 RNDIS_INITIALIZED,
482 RNDIS_DATA_INITIALIZED,
485 /* from ndis.h */
486 enum ndis_oid {
487 /* Required Object IDs (OIDs) */
488 OID_GEN_SUPPORTED_LIST = 0x00010101,
489 OID_GEN_HARDWARE_STATUS = 0x00010102,
490 OID_GEN_MEDIA_SUPPORTED = 0x00010103,
491 OID_GEN_MEDIA_IN_USE = 0x00010104,
492 OID_GEN_MAXIMUM_LOOKAHEAD = 0x00010105,
493 OID_GEN_MAXIMUM_FRAME_SIZE = 0x00010106,
494 OID_GEN_LINK_SPEED = 0x00010107,
495 OID_GEN_TRANSMIT_BUFFER_SPACE = 0x00010108,
496 OID_GEN_RECEIVE_BUFFER_SPACE = 0x00010109,
497 OID_GEN_TRANSMIT_BLOCK_SIZE = 0x0001010a,
498 OID_GEN_RECEIVE_BLOCK_SIZE = 0x0001010b,
499 OID_GEN_VENDOR_ID = 0x0001010c,
500 OID_GEN_VENDOR_DESCRIPTION = 0x0001010d,
501 OID_GEN_CURRENT_PACKET_FILTER = 0x0001010e,
502 OID_GEN_CURRENT_LOOKAHEAD = 0x0001010f,
503 OID_GEN_DRIVER_VERSION = 0x00010110,
504 OID_GEN_MAXIMUM_TOTAL_SIZE = 0x00010111,
505 OID_GEN_PROTOCOL_OPTIONS = 0x00010112,
506 OID_GEN_MAC_OPTIONS = 0x00010113,
507 OID_GEN_MEDIA_CONNECT_STATUS = 0x00010114,
508 OID_GEN_MAXIMUM_SEND_PACKETS = 0x00010115,
509 OID_GEN_VENDOR_DRIVER_VERSION = 0x00010116,
510 OID_GEN_SUPPORTED_GUIDS = 0x00010117,
511 OID_GEN_NETWORK_LAYER_ADDRESSES = 0x00010118,
512 OID_GEN_TRANSPORT_HEADER_OFFSET = 0x00010119,
513 OID_GEN_MACHINE_NAME = 0x0001021a,
514 OID_GEN_RNDIS_CONFIG_PARAMETER = 0x0001021b,
515 OID_GEN_VLAN_ID = 0x0001021c,
517 /* Optional OIDs */
518 OID_GEN_MEDIA_CAPABILITIES = 0x00010201,
519 OID_GEN_PHYSICAL_MEDIUM = 0x00010202,
521 /* Required statistics OIDs */
522 OID_GEN_XMIT_OK = 0x00020101,
523 OID_GEN_RCV_OK = 0x00020102,
524 OID_GEN_XMIT_ERROR = 0x00020103,
525 OID_GEN_RCV_ERROR = 0x00020104,
526 OID_GEN_RCV_NO_BUFFER = 0x00020105,
528 /* Optional statistics OIDs */
529 OID_GEN_DIRECTED_BYTES_XMIT = 0x00020201,
530 OID_GEN_DIRECTED_FRAMES_XMIT = 0x00020202,
531 OID_GEN_MULTICAST_BYTES_XMIT = 0x00020203,
532 OID_GEN_MULTICAST_FRAMES_XMIT = 0x00020204,
533 OID_GEN_BROADCAST_BYTES_XMIT = 0x00020205,
534 OID_GEN_BROADCAST_FRAMES_XMIT = 0x00020206,
535 OID_GEN_DIRECTED_BYTES_RCV = 0x00020207,
536 OID_GEN_DIRECTED_FRAMES_RCV = 0x00020208,
537 OID_GEN_MULTICAST_BYTES_RCV = 0x00020209,
538 OID_GEN_MULTICAST_FRAMES_RCV = 0x0002020a,
539 OID_GEN_BROADCAST_BYTES_RCV = 0x0002020b,
540 OID_GEN_BROADCAST_FRAMES_RCV = 0x0002020c,
541 OID_GEN_RCV_CRC_ERROR = 0x0002020d,
542 OID_GEN_TRANSMIT_QUEUE_LENGTH = 0x0002020e,
543 OID_GEN_GET_TIME_CAPS = 0x0002020f,
544 OID_GEN_GET_NETCARD_TIME = 0x00020210,
545 OID_GEN_NETCARD_LOAD = 0x00020211,
546 OID_GEN_DEVICE_PROFILE = 0x00020212,
547 OID_GEN_INIT_TIME_MS = 0x00020213,
548 OID_GEN_RESET_COUNTS = 0x00020214,
549 OID_GEN_MEDIA_SENSE_COUNTS = 0x00020215,
550 OID_GEN_FRIENDLY_NAME = 0x00020216,
551 OID_GEN_MINIPORT_INFO = 0x00020217,
552 OID_GEN_RESET_VERIFY_PARAMETERS = 0x00020218,
554 /* IEEE 802.3 (Ethernet) OIDs */
555 OID_802_3_PERMANENT_ADDRESS = 0x01010101,
556 OID_802_3_CURRENT_ADDRESS = 0x01010102,
557 OID_802_3_MULTICAST_LIST = 0x01010103,
558 OID_802_3_MAXIMUM_LIST_SIZE = 0x01010104,
559 OID_802_3_MAC_OPTIONS = 0x01010105,
560 OID_802_3_RCV_ERROR_ALIGNMENT = 0x01020101,
561 OID_802_3_XMIT_ONE_COLLISION = 0x01020102,
562 OID_802_3_XMIT_MORE_COLLISIONS = 0x01020103,
563 OID_802_3_XMIT_DEFERRED = 0x01020201,
564 OID_802_3_XMIT_MAX_COLLISIONS = 0x01020202,
565 OID_802_3_RCV_OVERRUN = 0x01020203,
566 OID_802_3_XMIT_UNDERRUN = 0x01020204,
567 OID_802_3_XMIT_HEARTBEAT_FAILURE = 0x01020205,
568 OID_802_3_XMIT_TIMES_CRS_LOST = 0x01020206,
569 OID_802_3_XMIT_LATE_COLLISIONS = 0x01020207,
572 static const uint32_t oid_supported_list[] =
574 /* the general stuff */
575 OID_GEN_SUPPORTED_LIST,
576 OID_GEN_HARDWARE_STATUS,
577 OID_GEN_MEDIA_SUPPORTED,
578 OID_GEN_MEDIA_IN_USE,
579 OID_GEN_MAXIMUM_FRAME_SIZE,
580 OID_GEN_LINK_SPEED,
581 OID_GEN_TRANSMIT_BLOCK_SIZE,
582 OID_GEN_RECEIVE_BLOCK_SIZE,
583 OID_GEN_VENDOR_ID,
584 OID_GEN_VENDOR_DESCRIPTION,
585 OID_GEN_VENDOR_DRIVER_VERSION,
586 OID_GEN_CURRENT_PACKET_FILTER,
587 OID_GEN_MAXIMUM_TOTAL_SIZE,
588 OID_GEN_MEDIA_CONNECT_STATUS,
589 OID_GEN_PHYSICAL_MEDIUM,
591 /* the statistical stuff */
592 OID_GEN_XMIT_OK,
593 OID_GEN_RCV_OK,
594 OID_GEN_XMIT_ERROR,
595 OID_GEN_RCV_ERROR,
596 OID_GEN_RCV_NO_BUFFER,
598 /* IEEE 802.3 */
599 /* the general stuff */
600 OID_802_3_PERMANENT_ADDRESS,
601 OID_802_3_CURRENT_ADDRESS,
602 OID_802_3_MULTICAST_LIST,
603 OID_802_3_MAC_OPTIONS,
604 OID_802_3_MAXIMUM_LIST_SIZE,
606 /* the statistical stuff */
607 OID_802_3_RCV_ERROR_ALIGNMENT,
608 OID_802_3_XMIT_ONE_COLLISION,
609 OID_802_3_XMIT_MORE_COLLISIONS,
612 #define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA (1 << 0)
613 #define NDIS_MAC_OPTION_RECEIVE_SERIALIZED (1 << 1)
614 #define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND (1 << 2)
615 #define NDIS_MAC_OPTION_NO_LOOPBACK (1 << 3)
616 #define NDIS_MAC_OPTION_FULL_DUPLEX (1 << 4)
617 #define NDIS_MAC_OPTION_EOTX_INDICATION (1 << 5)
618 #define NDIS_MAC_OPTION_8021P_PRIORITY (1 << 6)
620 struct rndis_response {
621 QTAILQ_ENTRY(rndis_response) entries;
622 uint32_t length;
623 uint8_t buf[0];
626 typedef struct USBNetState {
627 USBDevice dev;
629 enum rndis_state rndis_state;
630 uint32_t medium;
631 uint32_t speed;
632 uint32_t media_state;
633 uint16_t filter;
634 uint32_t vendorid;
636 unsigned int out_ptr;
637 uint8_t out_buf[2048];
639 USBPacket *inpkt;
640 unsigned int in_ptr, in_len;
641 uint8_t in_buf[2048];
643 USBEndpoint *intr;
645 char usbstring_mac[13];
646 NICState *nic;
647 NICConf conf;
648 QTAILQ_HEAD(rndis_resp_head, rndis_response) rndis_resp;
649 } USBNetState;
651 static int is_rndis(USBNetState *s)
653 return s->dev.config->bConfigurationValue == DEV_RNDIS_CONFIG_VALUE;
656 static int ndis_query(USBNetState *s, uint32_t oid,
657 uint8_t *inbuf, unsigned int inlen, uint8_t *outbuf,
658 size_t outlen)
660 unsigned int i;
662 switch (oid) {
663 /* general oids (table 4-1) */
664 /* mandatory */
665 case OID_GEN_SUPPORTED_LIST:
666 for (i = 0; i < ARRAY_SIZE(oid_supported_list); i++)
667 ((le32 *) outbuf)[i] = cpu_to_le32(oid_supported_list[i]);
668 return sizeof(oid_supported_list);
670 /* mandatory */
671 case OID_GEN_HARDWARE_STATUS:
672 *((le32 *) outbuf) = cpu_to_le32(0);
673 return sizeof(le32);
675 /* mandatory */
676 case OID_GEN_MEDIA_SUPPORTED:
677 *((le32 *) outbuf) = cpu_to_le32(s->medium);
678 return sizeof(le32);
680 /* mandatory */
681 case OID_GEN_MEDIA_IN_USE:
682 *((le32 *) outbuf) = cpu_to_le32(s->medium);
683 return sizeof(le32);
685 /* mandatory */
686 case OID_GEN_MAXIMUM_FRAME_SIZE:
687 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
688 return sizeof(le32);
690 /* mandatory */
691 case OID_GEN_LINK_SPEED:
692 *((le32 *) outbuf) = cpu_to_le32(s->speed);
693 return sizeof(le32);
695 /* mandatory */
696 case OID_GEN_TRANSMIT_BLOCK_SIZE:
697 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
698 return sizeof(le32);
700 /* mandatory */
701 case OID_GEN_RECEIVE_BLOCK_SIZE:
702 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
703 return sizeof(le32);
705 /* mandatory */
706 case OID_GEN_VENDOR_ID:
707 *((le32 *) outbuf) = cpu_to_le32(s->vendorid);
708 return sizeof(le32);
710 /* mandatory */
711 case OID_GEN_VENDOR_DESCRIPTION:
712 pstrcpy((char *)outbuf, outlen, "QEMU USB RNDIS Net");
713 return strlen((char *)outbuf) + 1;
715 case OID_GEN_VENDOR_DRIVER_VERSION:
716 *((le32 *) outbuf) = cpu_to_le32(1);
717 return sizeof(le32);
719 /* mandatory */
720 case OID_GEN_CURRENT_PACKET_FILTER:
721 *((le32 *) outbuf) = cpu_to_le32(s->filter);
722 return sizeof(le32);
724 /* mandatory */
725 case OID_GEN_MAXIMUM_TOTAL_SIZE:
726 *((le32 *) outbuf) = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
727 return sizeof(le32);
729 /* mandatory */
730 case OID_GEN_MEDIA_CONNECT_STATUS:
731 *((le32 *) outbuf) = cpu_to_le32(s->media_state);
732 return sizeof(le32);
734 case OID_GEN_PHYSICAL_MEDIUM:
735 *((le32 *) outbuf) = cpu_to_le32(0);
736 return sizeof(le32);
738 case OID_GEN_MAC_OPTIONS:
739 *((le32 *) outbuf) = cpu_to_le32(
740 NDIS_MAC_OPTION_RECEIVE_SERIALIZED |
741 NDIS_MAC_OPTION_FULL_DUPLEX);
742 return sizeof(le32);
744 /* statistics OIDs (table 4-2) */
745 /* mandatory */
746 case OID_GEN_XMIT_OK:
747 *((le32 *) outbuf) = cpu_to_le32(0);
748 return sizeof(le32);
750 /* mandatory */
751 case OID_GEN_RCV_OK:
752 *((le32 *) outbuf) = cpu_to_le32(0);
753 return sizeof(le32);
755 /* mandatory */
756 case OID_GEN_XMIT_ERROR:
757 *((le32 *) outbuf) = cpu_to_le32(0);
758 return sizeof(le32);
760 /* mandatory */
761 case OID_GEN_RCV_ERROR:
762 *((le32 *) outbuf) = cpu_to_le32(0);
763 return sizeof(le32);
765 /* mandatory */
766 case OID_GEN_RCV_NO_BUFFER:
767 *((le32 *) outbuf) = cpu_to_le32(0);
768 return sizeof(le32);
770 /* ieee802.3 OIDs (table 4-3) */
771 /* mandatory */
772 case OID_802_3_PERMANENT_ADDRESS:
773 memcpy(outbuf, s->conf.macaddr.a, 6);
774 return 6;
776 /* mandatory */
777 case OID_802_3_CURRENT_ADDRESS:
778 memcpy(outbuf, s->conf.macaddr.a, 6);
779 return 6;
781 /* mandatory */
782 case OID_802_3_MULTICAST_LIST:
783 *((le32 *) outbuf) = cpu_to_le32(0xe0000000);
784 return sizeof(le32);
786 /* mandatory */
787 case OID_802_3_MAXIMUM_LIST_SIZE:
788 *((le32 *) outbuf) = cpu_to_le32(1);
789 return sizeof(le32);
791 case OID_802_3_MAC_OPTIONS:
792 return 0;
794 /* ieee802.3 statistics OIDs (table 4-4) */
795 /* mandatory */
796 case OID_802_3_RCV_ERROR_ALIGNMENT:
797 *((le32 *) outbuf) = cpu_to_le32(0);
798 return sizeof(le32);
800 /* mandatory */
801 case OID_802_3_XMIT_ONE_COLLISION:
802 *((le32 *) outbuf) = cpu_to_le32(0);
803 return sizeof(le32);
805 /* mandatory */
806 case OID_802_3_XMIT_MORE_COLLISIONS:
807 *((le32 *) outbuf) = cpu_to_le32(0);
808 return sizeof(le32);
810 default:
811 fprintf(stderr, "usbnet: unknown OID 0x%08x\n", oid);
812 return 0;
814 return -1;
817 static int ndis_set(USBNetState *s, uint32_t oid,
818 uint8_t *inbuf, unsigned int inlen)
820 switch (oid) {
821 case OID_GEN_CURRENT_PACKET_FILTER:
822 s->filter = le32_to_cpup((le32 *) inbuf);
823 if (s->filter) {
824 s->rndis_state = RNDIS_DATA_INITIALIZED;
825 } else {
826 s->rndis_state = RNDIS_INITIALIZED;
828 return 0;
830 case OID_802_3_MULTICAST_LIST:
831 return 0;
833 return -1;
836 static int rndis_get_response(USBNetState *s, uint8_t *buf)
838 int ret = 0;
839 struct rndis_response *r = s->rndis_resp.tqh_first;
841 if (!r)
842 return ret;
844 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
845 ret = r->length;
846 memcpy(buf, r->buf, r->length);
847 g_free(r);
849 return ret;
852 static void *rndis_queue_response(USBNetState *s, unsigned int length)
854 struct rndis_response *r =
855 g_malloc0(sizeof(struct rndis_response) + length);
857 if (QTAILQ_EMPTY(&s->rndis_resp)) {
858 usb_wakeup(s->intr);
861 QTAILQ_INSERT_TAIL(&s->rndis_resp, r, entries);
862 r->length = length;
864 return &r->buf[0];
867 static void rndis_clear_responsequeue(USBNetState *s)
869 struct rndis_response *r;
871 while ((r = s->rndis_resp.tqh_first)) {
872 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
873 g_free(r);
877 static int rndis_init_response(USBNetState *s, rndis_init_msg_type *buf)
879 rndis_init_cmplt_type *resp =
880 rndis_queue_response(s, sizeof(rndis_init_cmplt_type));
882 if (!resp)
883 return USB_RET_STALL;
885 resp->MessageType = cpu_to_le32(RNDIS_INITIALIZE_CMPLT);
886 resp->MessageLength = cpu_to_le32(sizeof(rndis_init_cmplt_type));
887 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
888 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
889 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
890 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
891 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
892 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
893 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
894 resp->MaxTransferSize = cpu_to_le32(ETH_FRAME_LEN +
895 sizeof(struct rndis_packet_msg_type) + 22);
896 resp->PacketAlignmentFactor = cpu_to_le32(0);
897 resp->AFListOffset = cpu_to_le32(0);
898 resp->AFListSize = cpu_to_le32(0);
899 return 0;
902 static int rndis_query_response(USBNetState *s,
903 rndis_query_msg_type *buf, unsigned int length)
905 rndis_query_cmplt_type *resp;
906 /* oid_supported_list is the largest data reply */
907 uint8_t infobuf[sizeof(oid_supported_list)];
908 uint32_t bufoffs, buflen;
909 int infobuflen;
910 unsigned int resplen;
912 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
913 buflen = le32_to_cpu(buf->InformationBufferLength);
914 if (bufoffs + buflen > length)
915 return USB_RET_STALL;
917 infobuflen = ndis_query(s, le32_to_cpu(buf->OID),
918 bufoffs + (uint8_t *) buf, buflen, infobuf,
919 sizeof(infobuf));
920 resplen = sizeof(rndis_query_cmplt_type) +
921 ((infobuflen < 0) ? 0 : infobuflen);
922 resp = rndis_queue_response(s, resplen);
923 if (!resp)
924 return USB_RET_STALL;
926 resp->MessageType = cpu_to_le32(RNDIS_QUERY_CMPLT);
927 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
928 resp->MessageLength = cpu_to_le32(resplen);
930 if (infobuflen < 0) {
931 /* OID not supported */
932 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
933 resp->InformationBufferLength = cpu_to_le32(0);
934 resp->InformationBufferOffset = cpu_to_le32(0);
935 return 0;
938 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
939 resp->InformationBufferOffset =
940 cpu_to_le32(infobuflen ? sizeof(rndis_query_cmplt_type) - 8 : 0);
941 resp->InformationBufferLength = cpu_to_le32(infobuflen);
942 memcpy(resp + 1, infobuf, infobuflen);
944 return 0;
947 static int rndis_set_response(USBNetState *s,
948 rndis_set_msg_type *buf, unsigned int length)
950 rndis_set_cmplt_type *resp =
951 rndis_queue_response(s, sizeof(rndis_set_cmplt_type));
952 uint32_t bufoffs, buflen;
953 int ret;
955 if (!resp)
956 return USB_RET_STALL;
958 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
959 buflen = le32_to_cpu(buf->InformationBufferLength);
960 if (bufoffs + buflen > length)
961 return USB_RET_STALL;
963 ret = ndis_set(s, le32_to_cpu(buf->OID),
964 bufoffs + (uint8_t *) buf, buflen);
965 resp->MessageType = cpu_to_le32(RNDIS_SET_CMPLT);
966 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
967 resp->MessageLength = cpu_to_le32(sizeof(rndis_set_cmplt_type));
968 if (ret < 0) {
969 /* OID not supported */
970 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
971 return 0;
973 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
975 return 0;
978 static int rndis_reset_response(USBNetState *s, rndis_reset_msg_type *buf)
980 rndis_reset_cmplt_type *resp =
981 rndis_queue_response(s, sizeof(rndis_reset_cmplt_type));
983 if (!resp)
984 return USB_RET_STALL;
986 resp->MessageType = cpu_to_le32(RNDIS_RESET_CMPLT);
987 resp->MessageLength = cpu_to_le32(sizeof(rndis_reset_cmplt_type));
988 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
989 resp->AddressingReset = cpu_to_le32(1); /* reset information */
991 return 0;
994 static int rndis_keepalive_response(USBNetState *s,
995 rndis_keepalive_msg_type *buf)
997 rndis_keepalive_cmplt_type *resp =
998 rndis_queue_response(s, sizeof(rndis_keepalive_cmplt_type));
1000 if (!resp)
1001 return USB_RET_STALL;
1003 resp->MessageType = cpu_to_le32(RNDIS_KEEPALIVE_CMPLT);
1004 resp->MessageLength = cpu_to_le32(sizeof(rndis_keepalive_cmplt_type));
1005 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
1006 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
1008 return 0;
1011 /* Prepare to receive the next packet */
1012 static void usb_net_reset_in_buf(USBNetState *s)
1014 s->in_ptr = s->in_len = 0;
1015 qemu_flush_queued_packets(&s->nic->nc);
1018 static int rndis_parse(USBNetState *s, uint8_t *data, int length)
1020 uint32_t msg_type;
1021 le32 *tmp = (le32 *) data;
1023 msg_type = le32_to_cpup(tmp);
1025 switch (msg_type) {
1026 case RNDIS_INITIALIZE_MSG:
1027 s->rndis_state = RNDIS_INITIALIZED;
1028 return rndis_init_response(s, (rndis_init_msg_type *) data);
1030 case RNDIS_HALT_MSG:
1031 s->rndis_state = RNDIS_UNINITIALIZED;
1032 return 0;
1034 case RNDIS_QUERY_MSG:
1035 return rndis_query_response(s, (rndis_query_msg_type *) data, length);
1037 case RNDIS_SET_MSG:
1038 return rndis_set_response(s, (rndis_set_msg_type *) data, length);
1040 case RNDIS_RESET_MSG:
1041 rndis_clear_responsequeue(s);
1042 s->out_ptr = 0;
1043 usb_net_reset_in_buf(s);
1044 return rndis_reset_response(s, (rndis_reset_msg_type *) data);
1046 case RNDIS_KEEPALIVE_MSG:
1047 /* For USB: host does this every 5 seconds */
1048 return rndis_keepalive_response(s, (rndis_keepalive_msg_type *) data);
1051 return USB_RET_STALL;
1054 static void usb_net_handle_reset(USBDevice *dev)
1058 static void usb_net_handle_control(USBDevice *dev, USBPacket *p,
1059 int request, int value, int index, int length, uint8_t *data)
1061 USBNetState *s = (USBNetState *) dev;
1062 int ret;
1064 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
1065 if (ret >= 0) {
1066 return;
1069 switch(request) {
1070 case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
1071 if (!is_rndis(s) || value || index != 0) {
1072 goto fail;
1074 #ifdef TRAFFIC_DEBUG
1076 unsigned int i;
1077 fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");
1078 for (i = 0; i < length; i++) {
1079 if (!(i & 15))
1080 fprintf(stderr, "\n%04x:", i);
1081 fprintf(stderr, " %02x", data[i]);
1083 fprintf(stderr, "\n\n");
1085 #endif
1086 ret = rndis_parse(s, data, length);
1087 if (ret < 0) {
1088 p->status = ret;
1090 break;
1092 case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
1093 if (!is_rndis(s) || value || index != 0) {
1094 goto fail;
1096 p->actual_length = rndis_get_response(s, data);
1097 if (p->actual_length == 0) {
1098 data[0] = 0;
1099 p->actual_length = 1;
1101 #ifdef TRAFFIC_DEBUG
1103 unsigned int i;
1104 fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");
1105 for (i = 0; i < p->actual_length; i++) {
1106 if (!(i & 15))
1107 fprintf(stderr, "\n%04x:", i);
1108 fprintf(stderr, " %02x", data[i]);
1110 fprintf(stderr, "\n\n");
1112 #endif
1113 break;
1115 default:
1116 fail:
1117 fprintf(stderr, "usbnet: failed control transaction: "
1118 "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1119 request, value, index, length);
1120 p->status = USB_RET_STALL;
1121 break;
1125 static void usb_net_handle_statusin(USBNetState *s, USBPacket *p)
1127 le32 buf[2];
1129 if (p->iov.size < 8) {
1130 p->status = USB_RET_STALL;
1131 return;
1134 buf[0] = cpu_to_le32(1);
1135 buf[1] = cpu_to_le32(0);
1136 usb_packet_copy(p, buf, 8);
1137 if (!s->rndis_resp.tqh_first) {
1138 p->status = USB_RET_NAK;
1141 #ifdef TRAFFIC_DEBUG
1142 fprintf(stderr, "usbnet: interrupt poll len %zu return %d",
1143 p->iov.size, p->status);
1144 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->status);
1145 #endif
1148 static void usb_net_handle_datain(USBNetState *s, USBPacket *p)
1150 int len;
1152 if (s->in_ptr > s->in_len) {
1153 usb_net_reset_in_buf(s);
1154 p->status = USB_RET_NAK;
1155 return;
1157 if (!s->in_len) {
1158 p->status = USB_RET_NAK;
1159 return;
1161 len = s->in_len - s->in_ptr;
1162 if (len > p->iov.size) {
1163 len = p->iov.size;
1165 usb_packet_copy(p, &s->in_buf[s->in_ptr], len);
1166 s->in_ptr += len;
1167 if (s->in_ptr >= s->in_len &&
1168 (is_rndis(s) || (s->in_len & (64 - 1)) || !len)) {
1169 /* no short packet necessary */
1170 usb_net_reset_in_buf(s);
1173 #ifdef TRAFFIC_DEBUG
1174 fprintf(stderr, "usbnet: data in len %zu return %d", p->iov.size, len);
1175 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", len);
1176 #endif
1179 static void usb_net_handle_dataout(USBNetState *s, USBPacket *p)
1181 int sz = sizeof(s->out_buf) - s->out_ptr;
1182 struct rndis_packet_msg_type *msg =
1183 (struct rndis_packet_msg_type *) s->out_buf;
1184 uint32_t len;
1186 #ifdef TRAFFIC_DEBUG
1187 fprintf(stderr, "usbnet: data out len %zu\n", p->iov.size);
1188 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->iov.size);
1189 #endif
1191 if (sz > p->iov.size) {
1192 sz = p->iov.size;
1194 usb_packet_copy(p, &s->out_buf[s->out_ptr], sz);
1195 s->out_ptr += sz;
1197 if (!is_rndis(s)) {
1198 if (p->iov.size < 64) {
1199 qemu_send_packet(&s->nic->nc, s->out_buf, s->out_ptr);
1200 s->out_ptr = 0;
1202 return;
1204 len = le32_to_cpu(msg->MessageLength);
1205 if (s->out_ptr < 8 || s->out_ptr < len) {
1206 return;
1208 if (le32_to_cpu(msg->MessageType) == RNDIS_PACKET_MSG) {
1209 uint32_t offs = 8 + le32_to_cpu(msg->DataOffset);
1210 uint32_t size = le32_to_cpu(msg->DataLength);
1211 if (offs + size <= len)
1212 qemu_send_packet(&s->nic->nc, s->out_buf + offs, size);
1214 s->out_ptr -= len;
1215 memmove(s->out_buf, &s->out_buf[len], s->out_ptr);
1218 static void usb_net_handle_data(USBDevice *dev, USBPacket *p)
1220 USBNetState *s = (USBNetState *) dev;
1222 switch(p->pid) {
1223 case USB_TOKEN_IN:
1224 switch (p->ep->nr) {
1225 case 1:
1226 usb_net_handle_statusin(s, p);
1227 break;
1229 case 2:
1230 usb_net_handle_datain(s, p);
1231 break;
1233 default:
1234 goto fail;
1236 break;
1238 case USB_TOKEN_OUT:
1239 switch (p->ep->nr) {
1240 case 2:
1241 usb_net_handle_dataout(s, p);
1242 break;
1244 default:
1245 goto fail;
1247 break;
1249 default:
1250 fail:
1251 p->status = USB_RET_STALL;
1252 break;
1255 if (p->status == USB_RET_STALL) {
1256 fprintf(stderr, "usbnet: failed data transaction: "
1257 "pid 0x%x ep 0x%x len 0x%zx\n",
1258 p->pid, p->ep->nr, p->iov.size);
1262 static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1264 USBNetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
1265 uint8_t *in_buf = s->in_buf;
1266 size_t total_size = size;
1268 if (is_rndis(s)) {
1269 if (s->rndis_state != RNDIS_DATA_INITIALIZED) {
1270 return -1;
1272 total_size += sizeof(struct rndis_packet_msg_type);
1274 if (total_size > sizeof(s->in_buf)) {
1275 return -1;
1278 /* Only accept packet if input buffer is empty */
1279 if (s->in_len > 0) {
1280 return 0;
1283 if (is_rndis(s)) {
1284 struct rndis_packet_msg_type *msg;
1286 msg = (struct rndis_packet_msg_type *)in_buf;
1287 memset(msg, 0, sizeof(struct rndis_packet_msg_type));
1288 msg->MessageType = cpu_to_le32(RNDIS_PACKET_MSG);
1289 msg->MessageLength = cpu_to_le32(size + sizeof(*msg));
1290 msg->DataOffset = cpu_to_le32(sizeof(*msg) - 8);
1291 msg->DataLength = cpu_to_le32(size);
1292 /* msg->OOBDataOffset;
1293 * msg->OOBDataLength;
1294 * msg->NumOOBDataElements;
1295 * msg->PerPacketInfoOffset;
1296 * msg->PerPacketInfoLength;
1297 * msg->VcHandle;
1298 * msg->Reserved;
1300 in_buf += sizeof(*msg);
1303 memcpy(in_buf, buf, size);
1304 s->in_len = total_size;
1305 s->in_ptr = 0;
1306 return size;
1309 static int usbnet_can_receive(NetClientState *nc)
1311 USBNetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
1313 if (is_rndis(s) && s->rndis_state != RNDIS_DATA_INITIALIZED) {
1314 return 1;
1317 return !s->in_len;
1320 static void usbnet_cleanup(NetClientState *nc)
1322 USBNetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
1324 s->nic = NULL;
1327 static void usb_net_handle_destroy(USBDevice *dev)
1329 USBNetState *s = (USBNetState *) dev;
1331 /* TODO: remove the nd_table[] entry */
1332 rndis_clear_responsequeue(s);
1333 qemu_del_net_client(&s->nic->nc);
1336 static NetClientInfo net_usbnet_info = {
1337 .type = NET_CLIENT_OPTIONS_KIND_NIC,
1338 .size = sizeof(NICState),
1339 .can_receive = usbnet_can_receive,
1340 .receive = usbnet_receive,
1341 .cleanup = usbnet_cleanup,
1344 static int usb_net_initfn(USBDevice *dev)
1346 USBNetState *s = DO_UPCAST(USBNetState, dev, dev);
1348 usb_desc_create_serial(dev);
1349 usb_desc_init(dev);
1351 s->rndis_state = RNDIS_UNINITIALIZED;
1352 QTAILQ_INIT(&s->rndis_resp);
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;
1359 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
1361 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1362 s->nic = qemu_new_nic(&net_usbnet_info, &s->conf,
1363 object_get_typename(OBJECT(s)), s->dev.qdev.id, s);
1364 qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
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]);
1373 usb_desc_set_string(dev, STRING_ETHADDR, s->usbstring_mac);
1375 add_boot_device_path(s->conf.bootindex, &dev->qdev, "/ethernet@0");
1376 return 0;
1379 static USBDevice *usb_net_init(USBBus *bus, const char *cmdline)
1381 Error *local_err = NULL;
1382 USBDevice *dev;
1383 QemuOpts *opts;
1384 int idx;
1386 opts = qemu_opts_parse(qemu_find_opts("net"), cmdline, 0);
1387 if (!opts) {
1388 return NULL;
1390 qemu_opt_set(opts, "type", "nic");
1391 qemu_opt_set(opts, "model", "usb");
1393 idx = net_client_init(opts, 0, &local_err);
1394 if (error_is_set(&local_err)) {
1395 qerror_report_err(local_err);
1396 error_free(local_err);
1397 return NULL;
1400 dev = usb_create(bus, "usb-net");
1401 if (!dev) {
1402 return NULL;
1404 qdev_set_nic_properties(&dev->qdev, &nd_table[idx]);
1405 qdev_init_nofail(&dev->qdev);
1406 return dev;
1409 static const VMStateDescription vmstate_usb_net = {
1410 .name = "usb-net",
1411 .unmigratable = 1,
1414 static Property net_properties[] = {
1415 DEFINE_NIC_PROPERTIES(USBNetState, conf),
1416 DEFINE_PROP_END_OF_LIST(),
1419 static void usb_net_class_initfn(ObjectClass *klass, void *data)
1421 DeviceClass *dc = DEVICE_CLASS(klass);
1422 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1424 uc->init = usb_net_initfn;
1425 uc->product_desc = "QEMU USB Network Interface";
1426 uc->usb_desc = &desc_net;
1427 uc->handle_reset = usb_net_handle_reset;
1428 uc->handle_control = usb_net_handle_control;
1429 uc->handle_data = usb_net_handle_data;
1430 uc->handle_destroy = usb_net_handle_destroy;
1431 dc->fw_name = "network";
1432 dc->vmsd = &vmstate_usb_net;
1433 dc->props = net_properties;
1436 static TypeInfo net_info = {
1437 .name = "usb-net",
1438 .parent = TYPE_USB_DEVICE,
1439 .instance_size = sizeof(USBNetState),
1440 .class_init = usb_net_class_initfn,
1443 static void usb_net_register_types(void)
1445 type_register_static(&net_info);
1446 usb_legacy_register("usb-net", "net", usb_net_init);
1449 type_init(usb_net_register_types)