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
26 #include "qemu-common.h"
28 #include "hw/usb/desc.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qemu/queue.h"
32 #include "qemu/config-file.h"
33 #include "sysemu/sysemu.h"
36 /*#define TRAFFIC_DEBUG*/
37 /* Thanks to NetChip Technologies for donating this product ID.
38 * It's for devices with only CDC Ethernet configurations.
40 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
41 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
42 /* For hardware that can talk RNDIS and either of the above protocols,
43 * use this ID ... the windows INF files will know it.
45 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
46 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
49 STRING_MANUFACTURER
= 1,
61 #define DEV_CONFIG_VALUE 1 /* CDC or a subset */
62 #define DEV_RNDIS_CONFIG_VALUE 2 /* RNDIS; optional */
64 #define USB_CDC_SUBCLASS_ACM 0x02
65 #define USB_CDC_SUBCLASS_ETHERNET 0x06
67 #define USB_CDC_PROTO_NONE 0
68 #define USB_CDC_ACM_PROTO_VENDOR 0xff
70 #define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
71 #define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
72 #define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
73 #define USB_CDC_UNION_TYPE 0x06 /* union_desc */
74 #define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
76 #define USB_CDC_SEND_ENCAPSULATED_COMMAND 0x00
77 #define USB_CDC_GET_ENCAPSULATED_RESPONSE 0x01
78 #define USB_CDC_REQ_SET_LINE_CODING 0x20
79 #define USB_CDC_REQ_GET_LINE_CODING 0x21
80 #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
81 #define USB_CDC_REQ_SEND_BREAK 0x23
82 #define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40
83 #define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41
84 #define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42
85 #define USB_CDC_SET_ETHERNET_PACKET_FILTER 0x43
86 #define USB_CDC_GET_ETHERNET_STATISTIC 0x44
88 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
89 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
91 #define ETH_FRAME_LEN 1514 /* Max. octets in frame sans FCS */
93 static const USBDescStrings usb_net_stringtable
= {
94 [STRING_MANUFACTURER
] = "QEMU",
95 [STRING_PRODUCT
] = "RNDIS/QEMU USB Network Device",
96 [STRING_ETHADDR
] = "400102030405",
97 [STRING_DATA
] = "QEMU USB Net Data Interface",
98 [STRING_CONTROL
] = "QEMU USB Net Control Interface",
99 [STRING_RNDIS_CONTROL
] = "QEMU USB Net RNDIS Control Interface",
100 [STRING_CDC
] = "QEMU USB Net CDC",
101 [STRING_SUBSET
] = "QEMU USB Net Subset",
102 [STRING_RNDIS
] = "QEMU USB Net RNDIS",
103 [STRING_SERIALNUMBER
] = "1",
106 static const USBDescIface desc_iface_rndis
[] = {
108 /* RNDIS Control Interface */
109 .bInterfaceNumber
= 0,
111 .bInterfaceClass
= USB_CLASS_COMM
,
112 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
113 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_VENDOR
,
114 .iInterface
= STRING_RNDIS_CONTROL
,
116 .descs
= (USBDescOther
[]) {
118 /* Header Descriptor */
119 .data
= (uint8_t[]) {
120 0x05, /* u8 bLength */
121 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
122 USB_CDC_HEADER_TYPE
, /* u8 bDescriptorSubType */
123 0x10, 0x01, /* le16 bcdCDC */
126 /* Call Management Descriptor */
127 .data
= (uint8_t[]) {
128 0x05, /* u8 bLength */
129 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
130 USB_CDC_CALL_MANAGEMENT_TYPE
, /* u8 bDescriptorSubType */
131 0x00, /* u8 bmCapabilities */
132 0x01, /* u8 bDataInterface */
136 .data
= (uint8_t[]) {
137 0x04, /* u8 bLength */
138 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
139 USB_CDC_ACM_TYPE
, /* u8 bDescriptorSubType */
140 0x00, /* u8 bmCapabilities */
143 /* Union Descriptor */
144 .data
= (uint8_t[]) {
145 0x05, /* u8 bLength */
146 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
147 USB_CDC_UNION_TYPE
, /* u8 bDescriptorSubType */
148 0x00, /* u8 bMasterInterface0 */
149 0x01, /* u8 bSlaveInterface0 */
153 .eps
= (USBDescEndpoint
[]) {
155 .bEndpointAddress
= USB_DIR_IN
| 0x01,
156 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
157 .wMaxPacketSize
= STATUS_BYTECOUNT
,
158 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
162 /* RNDIS Data Interface */
163 .bInterfaceNumber
= 1,
165 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
166 .iInterface
= STRING_DATA
,
167 .eps
= (USBDescEndpoint
[]) {
169 .bEndpointAddress
= USB_DIR_IN
| 0x02,
170 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
171 .wMaxPacketSize
= 0x40,
173 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
174 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
175 .wMaxPacketSize
= 0x40,
181 static const USBDescIface desc_iface_cdc
[] = {
183 /* CDC Control Interface */
184 .bInterfaceNumber
= 0,
186 .bInterfaceClass
= USB_CLASS_COMM
,
187 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
188 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
189 .iInterface
= STRING_CONTROL
,
191 .descs
= (USBDescOther
[]) {
193 /* Header Descriptor */
194 .data
= (uint8_t[]) {
195 0x05, /* u8 bLength */
196 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
197 USB_CDC_HEADER_TYPE
, /* u8 bDescriptorSubType */
198 0x10, 0x01, /* le16 bcdCDC */
201 /* Union Descriptor */
202 .data
= (uint8_t[]) {
203 0x05, /* u8 bLength */
204 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
205 USB_CDC_UNION_TYPE
, /* u8 bDescriptorSubType */
206 0x00, /* u8 bMasterInterface0 */
207 0x01, /* u8 bSlaveInterface0 */
210 /* Ethernet Descriptor */
211 .data
= (uint8_t[]) {
212 0x0d, /* u8 bLength */
213 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
214 USB_CDC_ETHERNET_TYPE
, /* u8 bDescriptorSubType */
215 STRING_ETHADDR
, /* u8 iMACAddress */
216 0x00, 0x00, 0x00, 0x00, /* le32 bmEthernetStatistics */
217 ETH_FRAME_LEN
& 0xff,
218 ETH_FRAME_LEN
>> 8, /* le16 wMaxSegmentSize */
219 0x00, 0x00, /* le16 wNumberMCFilters */
220 0x00, /* u8 bNumberPowerFilters */
224 .eps
= (USBDescEndpoint
[]) {
226 .bEndpointAddress
= USB_DIR_IN
| 0x01,
227 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
228 .wMaxPacketSize
= STATUS_BYTECOUNT
,
229 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
233 /* CDC Data Interface (off) */
234 .bInterfaceNumber
= 1,
235 .bAlternateSetting
= 0,
237 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
239 /* CDC Data Interface */
240 .bInterfaceNumber
= 1,
241 .bAlternateSetting
= 1,
243 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
244 .iInterface
= STRING_DATA
,
245 .eps
= (USBDescEndpoint
[]) {
247 .bEndpointAddress
= USB_DIR_IN
| 0x02,
248 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
249 .wMaxPacketSize
= 0x40,
251 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
252 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
253 .wMaxPacketSize
= 0x40,
259 static const USBDescDevice desc_device_net
= {
261 .bDeviceClass
= USB_CLASS_COMM
,
262 .bMaxPacketSize0
= 0x40,
263 .bNumConfigurations
= 2,
264 .confs
= (USBDescConfig
[]) {
267 .bConfigurationValue
= DEV_RNDIS_CONFIG_VALUE
,
268 .iConfiguration
= STRING_RNDIS
,
269 .bmAttributes
= 0xc0,
271 .nif
= ARRAY_SIZE(desc_iface_rndis
),
272 .ifs
= desc_iface_rndis
,
275 .bConfigurationValue
= DEV_CONFIG_VALUE
,
276 .iConfiguration
= STRING_CDC
,
277 .bmAttributes
= 0xc0,
279 .nif
= ARRAY_SIZE(desc_iface_cdc
),
280 .ifs
= desc_iface_cdc
,
285 static const USBDesc desc_net
= {
287 .idVendor
= RNDIS_VENDOR_NUM
,
288 .idProduct
= RNDIS_PRODUCT_NUM
,
290 .iManufacturer
= STRING_MANUFACTURER
,
291 .iProduct
= STRING_PRODUCT
,
292 .iSerialNumber
= STRING_SERIALNUMBER
,
294 .full
= &desc_device_net
,
295 .str
= usb_net_stringtable
,
299 * RNDIS Definitions - in theory not specific to USB.
301 #define RNDIS_MAXIMUM_FRAME_SIZE 1518
302 #define RNDIS_MAX_TOTAL_SIZE 1558
304 /* Remote NDIS Versions */
305 #define RNDIS_MAJOR_VERSION 1
306 #define RNDIS_MINOR_VERSION 0
309 #define RNDIS_STATUS_SUCCESS 0x00000000U /* Success */
310 #define RNDIS_STATUS_FAILURE 0xc0000001U /* Unspecified error */
311 #define RNDIS_STATUS_INVALID_DATA 0xc0010015U /* Invalid data */
312 #define RNDIS_STATUS_NOT_SUPPORTED 0xc00000bbU /* Unsupported request */
313 #define RNDIS_STATUS_MEDIA_CONNECT 0x4001000bU /* Device connected */
314 #define RNDIS_STATUS_MEDIA_DISCONNECT 0x4001000cU /* Device disconnected */
316 /* Message Set for Connectionless (802.3) Devices */
318 RNDIS_PACKET_MSG
= 1,
319 RNDIS_INITIALIZE_MSG
= 2, /* Initialize device */
324 RNDIS_INDICATE_STATUS_MSG
= 7,
325 RNDIS_KEEPALIVE_MSG
= 8,
328 /* Message completion */
330 RNDIS_INITIALIZE_CMPLT
= 0x80000002U
,
331 RNDIS_QUERY_CMPLT
= 0x80000004U
,
332 RNDIS_SET_CMPLT
= 0x80000005U
,
333 RNDIS_RESET_CMPLT
= 0x80000006U
,
334 RNDIS_KEEPALIVE_CMPLT
= 0x80000008U
,
339 RNDIS_DF_CONNECTIONLESS
= 1,
340 RNDIS_DF_CONNECTIONORIENTED
= 2,
343 #define RNDIS_MEDIUM_802_3 0x00000000U
345 /* from drivers/net/sk98lin/h/skgepnmi.h */
346 #define OID_PNP_CAPABILITIES 0xfd010100
347 #define OID_PNP_SET_POWER 0xfd010101
348 #define OID_PNP_QUERY_POWER 0xfd010102
349 #define OID_PNP_ADD_WAKE_UP_PATTERN 0xfd010103
350 #define OID_PNP_REMOVE_WAKE_UP_PATTERN 0xfd010104
351 #define OID_PNP_ENABLE_WAKE_UP 0xfd010106
353 typedef uint32_t le32
;
355 typedef struct rndis_init_msg_type
{
361 le32 MaxTransferSize
;
362 } rndis_init_msg_type
;
364 typedef struct rndis_init_cmplt_type
{
373 le32 MaxPacketsPerTransfer
;
374 le32 MaxTransferSize
;
375 le32 PacketAlignmentFactor
;
378 } rndis_init_cmplt_type
;
380 typedef struct rndis_halt_msg_type
{
384 } rndis_halt_msg_type
;
386 typedef struct rndis_query_msg_type
{
391 le32 InformationBufferLength
;
392 le32 InformationBufferOffset
;
394 } rndis_query_msg_type
;
396 typedef struct rndis_query_cmplt_type
{
401 le32 InformationBufferLength
;
402 le32 InformationBufferOffset
;
403 } rndis_query_cmplt_type
;
405 typedef struct rndis_set_msg_type
{
410 le32 InformationBufferLength
;
411 le32 InformationBufferOffset
;
413 } rndis_set_msg_type
;
415 typedef struct rndis_set_cmplt_type
{
420 } rndis_set_cmplt_type
;
422 typedef struct rndis_reset_msg_type
{
426 } rndis_reset_msg_type
;
428 typedef struct rndis_reset_cmplt_type
{
432 le32 AddressingReset
;
433 } rndis_reset_cmplt_type
;
435 typedef struct rndis_indicate_status_msg_type
{
439 le32 StatusBufferLength
;
440 le32 StatusBufferOffset
;
441 } rndis_indicate_status_msg_type
;
443 typedef struct rndis_keepalive_msg_type
{
447 } rndis_keepalive_msg_type
;
449 typedef struct rndis_keepalive_cmplt_type
{
454 } rndis_keepalive_cmplt_type
;
456 struct rndis_packet_msg_type
{
463 le32 NumOOBDataElements
;
464 le32 PerPacketInfoOffset
;
465 le32 PerPacketInfoLength
;
470 struct rndis_config_parameter
{
471 le32 ParameterNameOffset
;
472 le32 ParameterNameLength
;
474 le32 ParameterValueOffset
;
475 le32 ParameterValueLength
;
478 /* implementation specific */
483 RNDIS_DATA_INITIALIZED
,
488 /* Required Object IDs (OIDs) */
489 OID_GEN_SUPPORTED_LIST
= 0x00010101,
490 OID_GEN_HARDWARE_STATUS
= 0x00010102,
491 OID_GEN_MEDIA_SUPPORTED
= 0x00010103,
492 OID_GEN_MEDIA_IN_USE
= 0x00010104,
493 OID_GEN_MAXIMUM_LOOKAHEAD
= 0x00010105,
494 OID_GEN_MAXIMUM_FRAME_SIZE
= 0x00010106,
495 OID_GEN_LINK_SPEED
= 0x00010107,
496 OID_GEN_TRANSMIT_BUFFER_SPACE
= 0x00010108,
497 OID_GEN_RECEIVE_BUFFER_SPACE
= 0x00010109,
498 OID_GEN_TRANSMIT_BLOCK_SIZE
= 0x0001010a,
499 OID_GEN_RECEIVE_BLOCK_SIZE
= 0x0001010b,
500 OID_GEN_VENDOR_ID
= 0x0001010c,
501 OID_GEN_VENDOR_DESCRIPTION
= 0x0001010d,
502 OID_GEN_CURRENT_PACKET_FILTER
= 0x0001010e,
503 OID_GEN_CURRENT_LOOKAHEAD
= 0x0001010f,
504 OID_GEN_DRIVER_VERSION
= 0x00010110,
505 OID_GEN_MAXIMUM_TOTAL_SIZE
= 0x00010111,
506 OID_GEN_PROTOCOL_OPTIONS
= 0x00010112,
507 OID_GEN_MAC_OPTIONS
= 0x00010113,
508 OID_GEN_MEDIA_CONNECT_STATUS
= 0x00010114,
509 OID_GEN_MAXIMUM_SEND_PACKETS
= 0x00010115,
510 OID_GEN_VENDOR_DRIVER_VERSION
= 0x00010116,
511 OID_GEN_SUPPORTED_GUIDS
= 0x00010117,
512 OID_GEN_NETWORK_LAYER_ADDRESSES
= 0x00010118,
513 OID_GEN_TRANSPORT_HEADER_OFFSET
= 0x00010119,
514 OID_GEN_MACHINE_NAME
= 0x0001021a,
515 OID_GEN_RNDIS_CONFIG_PARAMETER
= 0x0001021b,
516 OID_GEN_VLAN_ID
= 0x0001021c,
519 OID_GEN_MEDIA_CAPABILITIES
= 0x00010201,
520 OID_GEN_PHYSICAL_MEDIUM
= 0x00010202,
522 /* Required statistics OIDs */
523 OID_GEN_XMIT_OK
= 0x00020101,
524 OID_GEN_RCV_OK
= 0x00020102,
525 OID_GEN_XMIT_ERROR
= 0x00020103,
526 OID_GEN_RCV_ERROR
= 0x00020104,
527 OID_GEN_RCV_NO_BUFFER
= 0x00020105,
529 /* Optional statistics OIDs */
530 OID_GEN_DIRECTED_BYTES_XMIT
= 0x00020201,
531 OID_GEN_DIRECTED_FRAMES_XMIT
= 0x00020202,
532 OID_GEN_MULTICAST_BYTES_XMIT
= 0x00020203,
533 OID_GEN_MULTICAST_FRAMES_XMIT
= 0x00020204,
534 OID_GEN_BROADCAST_BYTES_XMIT
= 0x00020205,
535 OID_GEN_BROADCAST_FRAMES_XMIT
= 0x00020206,
536 OID_GEN_DIRECTED_BYTES_RCV
= 0x00020207,
537 OID_GEN_DIRECTED_FRAMES_RCV
= 0x00020208,
538 OID_GEN_MULTICAST_BYTES_RCV
= 0x00020209,
539 OID_GEN_MULTICAST_FRAMES_RCV
= 0x0002020a,
540 OID_GEN_BROADCAST_BYTES_RCV
= 0x0002020b,
541 OID_GEN_BROADCAST_FRAMES_RCV
= 0x0002020c,
542 OID_GEN_RCV_CRC_ERROR
= 0x0002020d,
543 OID_GEN_TRANSMIT_QUEUE_LENGTH
= 0x0002020e,
544 OID_GEN_GET_TIME_CAPS
= 0x0002020f,
545 OID_GEN_GET_NETCARD_TIME
= 0x00020210,
546 OID_GEN_NETCARD_LOAD
= 0x00020211,
547 OID_GEN_DEVICE_PROFILE
= 0x00020212,
548 OID_GEN_INIT_TIME_MS
= 0x00020213,
549 OID_GEN_RESET_COUNTS
= 0x00020214,
550 OID_GEN_MEDIA_SENSE_COUNTS
= 0x00020215,
551 OID_GEN_FRIENDLY_NAME
= 0x00020216,
552 OID_GEN_MINIPORT_INFO
= 0x00020217,
553 OID_GEN_RESET_VERIFY_PARAMETERS
= 0x00020218,
555 /* IEEE 802.3 (Ethernet) OIDs */
556 OID_802_3_PERMANENT_ADDRESS
= 0x01010101,
557 OID_802_3_CURRENT_ADDRESS
= 0x01010102,
558 OID_802_3_MULTICAST_LIST
= 0x01010103,
559 OID_802_3_MAXIMUM_LIST_SIZE
= 0x01010104,
560 OID_802_3_MAC_OPTIONS
= 0x01010105,
561 OID_802_3_RCV_ERROR_ALIGNMENT
= 0x01020101,
562 OID_802_3_XMIT_ONE_COLLISION
= 0x01020102,
563 OID_802_3_XMIT_MORE_COLLISIONS
= 0x01020103,
564 OID_802_3_XMIT_DEFERRED
= 0x01020201,
565 OID_802_3_XMIT_MAX_COLLISIONS
= 0x01020202,
566 OID_802_3_RCV_OVERRUN
= 0x01020203,
567 OID_802_3_XMIT_UNDERRUN
= 0x01020204,
568 OID_802_3_XMIT_HEARTBEAT_FAILURE
= 0x01020205,
569 OID_802_3_XMIT_TIMES_CRS_LOST
= 0x01020206,
570 OID_802_3_XMIT_LATE_COLLISIONS
= 0x01020207,
573 static const uint32_t oid_supported_list
[] =
575 /* the general stuff */
576 OID_GEN_SUPPORTED_LIST
,
577 OID_GEN_HARDWARE_STATUS
,
578 OID_GEN_MEDIA_SUPPORTED
,
579 OID_GEN_MEDIA_IN_USE
,
580 OID_GEN_MAXIMUM_FRAME_SIZE
,
582 OID_GEN_TRANSMIT_BLOCK_SIZE
,
583 OID_GEN_RECEIVE_BLOCK_SIZE
,
585 OID_GEN_VENDOR_DESCRIPTION
,
586 OID_GEN_VENDOR_DRIVER_VERSION
,
587 OID_GEN_CURRENT_PACKET_FILTER
,
588 OID_GEN_MAXIMUM_TOTAL_SIZE
,
589 OID_GEN_MEDIA_CONNECT_STATUS
,
590 OID_GEN_PHYSICAL_MEDIUM
,
592 /* the statistical stuff */
597 OID_GEN_RCV_NO_BUFFER
,
600 /* the general stuff */
601 OID_802_3_PERMANENT_ADDRESS
,
602 OID_802_3_CURRENT_ADDRESS
,
603 OID_802_3_MULTICAST_LIST
,
604 OID_802_3_MAC_OPTIONS
,
605 OID_802_3_MAXIMUM_LIST_SIZE
,
607 /* the statistical stuff */
608 OID_802_3_RCV_ERROR_ALIGNMENT
,
609 OID_802_3_XMIT_ONE_COLLISION
,
610 OID_802_3_XMIT_MORE_COLLISIONS
,
613 #define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA (1 << 0)
614 #define NDIS_MAC_OPTION_RECEIVE_SERIALIZED (1 << 1)
615 #define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND (1 << 2)
616 #define NDIS_MAC_OPTION_NO_LOOPBACK (1 << 3)
617 #define NDIS_MAC_OPTION_FULL_DUPLEX (1 << 4)
618 #define NDIS_MAC_OPTION_EOTX_INDICATION (1 << 5)
619 #define NDIS_MAC_OPTION_8021P_PRIORITY (1 << 6)
621 struct rndis_response
{
622 QTAILQ_ENTRY(rndis_response
) entries
;
627 typedef struct USBNetState
{
630 enum rndis_state rndis_state
;
633 uint32_t media_state
;
637 unsigned int out_ptr
;
638 uint8_t out_buf
[2048];
641 unsigned int in_ptr
, in_len
;
642 uint8_t in_buf
[2048];
646 char usbstring_mac
[13];
649 QTAILQ_HEAD(rndis_resp_head
, rndis_response
) rndis_resp
;
652 static int is_rndis(USBNetState
*s
)
654 return s
->dev
.config
->bConfigurationValue
== DEV_RNDIS_CONFIG_VALUE
;
657 static int ndis_query(USBNetState
*s
, uint32_t oid
,
658 uint8_t *inbuf
, unsigned int inlen
, uint8_t *outbuf
,
664 /* general oids (table 4-1) */
666 case OID_GEN_SUPPORTED_LIST
:
667 for (i
= 0; i
< ARRAY_SIZE(oid_supported_list
); i
++)
668 ((le32
*) outbuf
)[i
] = cpu_to_le32(oid_supported_list
[i
]);
669 return sizeof(oid_supported_list
);
672 case OID_GEN_HARDWARE_STATUS
:
673 *((le32
*) outbuf
) = cpu_to_le32(0);
677 case OID_GEN_MEDIA_SUPPORTED
:
678 *((le32
*) outbuf
) = cpu_to_le32(s
->medium
);
682 case OID_GEN_MEDIA_IN_USE
:
683 *((le32
*) outbuf
) = cpu_to_le32(s
->medium
);
687 case OID_GEN_MAXIMUM_FRAME_SIZE
:
688 *((le32
*) outbuf
) = cpu_to_le32(ETH_FRAME_LEN
);
692 case OID_GEN_LINK_SPEED
:
693 *((le32
*) outbuf
) = cpu_to_le32(s
->speed
);
697 case OID_GEN_TRANSMIT_BLOCK_SIZE
:
698 *((le32
*) outbuf
) = cpu_to_le32(ETH_FRAME_LEN
);
702 case OID_GEN_RECEIVE_BLOCK_SIZE
:
703 *((le32
*) outbuf
) = cpu_to_le32(ETH_FRAME_LEN
);
707 case OID_GEN_VENDOR_ID
:
708 *((le32
*) outbuf
) = cpu_to_le32(s
->vendorid
);
712 case OID_GEN_VENDOR_DESCRIPTION
:
713 pstrcpy((char *)outbuf
, outlen
, "QEMU USB RNDIS Net");
714 return strlen((char *)outbuf
) + 1;
716 case OID_GEN_VENDOR_DRIVER_VERSION
:
717 *((le32
*) outbuf
) = cpu_to_le32(1);
721 case OID_GEN_CURRENT_PACKET_FILTER
:
722 *((le32
*) outbuf
) = cpu_to_le32(s
->filter
);
726 case OID_GEN_MAXIMUM_TOTAL_SIZE
:
727 *((le32
*) outbuf
) = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE
);
731 case OID_GEN_MEDIA_CONNECT_STATUS
:
732 *((le32
*) outbuf
) = cpu_to_le32(s
->media_state
);
735 case OID_GEN_PHYSICAL_MEDIUM
:
736 *((le32
*) outbuf
) = cpu_to_le32(0);
739 case OID_GEN_MAC_OPTIONS
:
740 *((le32
*) outbuf
) = cpu_to_le32(
741 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
|
742 NDIS_MAC_OPTION_FULL_DUPLEX
);
745 /* statistics OIDs (table 4-2) */
747 case OID_GEN_XMIT_OK
:
748 *((le32
*) outbuf
) = cpu_to_le32(0);
753 *((le32
*) outbuf
) = cpu_to_le32(0);
757 case OID_GEN_XMIT_ERROR
:
758 *((le32
*) outbuf
) = cpu_to_le32(0);
762 case OID_GEN_RCV_ERROR
:
763 *((le32
*) outbuf
) = cpu_to_le32(0);
767 case OID_GEN_RCV_NO_BUFFER
:
768 *((le32
*) outbuf
) = cpu_to_le32(0);
771 /* ieee802.3 OIDs (table 4-3) */
773 case OID_802_3_PERMANENT_ADDRESS
:
774 memcpy(outbuf
, s
->conf
.macaddr
.a
, 6);
778 case OID_802_3_CURRENT_ADDRESS
:
779 memcpy(outbuf
, s
->conf
.macaddr
.a
, 6);
783 case OID_802_3_MULTICAST_LIST
:
784 *((le32
*) outbuf
) = cpu_to_le32(0xe0000000);
788 case OID_802_3_MAXIMUM_LIST_SIZE
:
789 *((le32
*) outbuf
) = cpu_to_le32(1);
792 case OID_802_3_MAC_OPTIONS
:
795 /* ieee802.3 statistics OIDs (table 4-4) */
797 case OID_802_3_RCV_ERROR_ALIGNMENT
:
798 *((le32
*) outbuf
) = cpu_to_le32(0);
802 case OID_802_3_XMIT_ONE_COLLISION
:
803 *((le32
*) outbuf
) = cpu_to_le32(0);
807 case OID_802_3_XMIT_MORE_COLLISIONS
:
808 *((le32
*) outbuf
) = cpu_to_le32(0);
812 fprintf(stderr
, "usbnet: unknown OID 0x%08x\n", oid
);
818 static int ndis_set(USBNetState
*s
, uint32_t oid
,
819 uint8_t *inbuf
, unsigned int inlen
)
822 case OID_GEN_CURRENT_PACKET_FILTER
:
823 s
->filter
= le32_to_cpup((le32
*) inbuf
);
825 s
->rndis_state
= RNDIS_DATA_INITIALIZED
;
827 s
->rndis_state
= RNDIS_INITIALIZED
;
831 case OID_802_3_MULTICAST_LIST
:
837 static int rndis_get_response(USBNetState
*s
, uint8_t *buf
)
840 struct rndis_response
*r
= s
->rndis_resp
.tqh_first
;
845 QTAILQ_REMOVE(&s
->rndis_resp
, r
, entries
);
847 memcpy(buf
, r
->buf
, r
->length
);
853 static void *rndis_queue_response(USBNetState
*s
, unsigned int length
)
855 struct rndis_response
*r
=
856 g_malloc0(sizeof(struct rndis_response
) + length
);
858 if (QTAILQ_EMPTY(&s
->rndis_resp
)) {
859 usb_wakeup(s
->intr
, 0);
862 QTAILQ_INSERT_TAIL(&s
->rndis_resp
, r
, entries
);
868 static void rndis_clear_responsequeue(USBNetState
*s
)
870 struct rndis_response
*r
;
872 while ((r
= s
->rndis_resp
.tqh_first
)) {
873 QTAILQ_REMOVE(&s
->rndis_resp
, r
, entries
);
878 static int rndis_init_response(USBNetState
*s
, rndis_init_msg_type
*buf
)
880 rndis_init_cmplt_type
*resp
=
881 rndis_queue_response(s
, sizeof(rndis_init_cmplt_type
));
884 return USB_RET_STALL
;
886 resp
->MessageType
= cpu_to_le32(RNDIS_INITIALIZE_CMPLT
);
887 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_init_cmplt_type
));
888 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
889 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
890 resp
->MajorVersion
= cpu_to_le32(RNDIS_MAJOR_VERSION
);
891 resp
->MinorVersion
= cpu_to_le32(RNDIS_MINOR_VERSION
);
892 resp
->DeviceFlags
= cpu_to_le32(RNDIS_DF_CONNECTIONLESS
);
893 resp
->Medium
= cpu_to_le32(RNDIS_MEDIUM_802_3
);
894 resp
->MaxPacketsPerTransfer
= cpu_to_le32(1);
895 resp
->MaxTransferSize
= cpu_to_le32(ETH_FRAME_LEN
+
896 sizeof(struct rndis_packet_msg_type
) + 22);
897 resp
->PacketAlignmentFactor
= cpu_to_le32(0);
898 resp
->AFListOffset
= cpu_to_le32(0);
899 resp
->AFListSize
= cpu_to_le32(0);
903 static int rndis_query_response(USBNetState
*s
,
904 rndis_query_msg_type
*buf
, unsigned int length
)
906 rndis_query_cmplt_type
*resp
;
907 /* oid_supported_list is the largest data reply */
908 uint8_t infobuf
[sizeof(oid_supported_list
)];
909 uint32_t bufoffs
, buflen
;
911 unsigned int resplen
;
913 bufoffs
= le32_to_cpu(buf
->InformationBufferOffset
) + 8;
914 buflen
= le32_to_cpu(buf
->InformationBufferLength
);
915 if (bufoffs
+ buflen
> length
)
916 return USB_RET_STALL
;
918 infobuflen
= ndis_query(s
, le32_to_cpu(buf
->OID
),
919 bufoffs
+ (uint8_t *) buf
, buflen
, infobuf
,
921 resplen
= sizeof(rndis_query_cmplt_type
) +
922 ((infobuflen
< 0) ? 0 : infobuflen
);
923 resp
= rndis_queue_response(s
, resplen
);
925 return USB_RET_STALL
;
927 resp
->MessageType
= cpu_to_le32(RNDIS_QUERY_CMPLT
);
928 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
929 resp
->MessageLength
= cpu_to_le32(resplen
);
931 if (infobuflen
< 0) {
932 /* OID not supported */
933 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
934 resp
->InformationBufferLength
= cpu_to_le32(0);
935 resp
->InformationBufferOffset
= cpu_to_le32(0);
939 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
940 resp
->InformationBufferOffset
=
941 cpu_to_le32(infobuflen
? sizeof(rndis_query_cmplt_type
) - 8 : 0);
942 resp
->InformationBufferLength
= cpu_to_le32(infobuflen
);
943 memcpy(resp
+ 1, infobuf
, infobuflen
);
948 static int rndis_set_response(USBNetState
*s
,
949 rndis_set_msg_type
*buf
, unsigned int length
)
951 rndis_set_cmplt_type
*resp
=
952 rndis_queue_response(s
, sizeof(rndis_set_cmplt_type
));
953 uint32_t bufoffs
, buflen
;
957 return USB_RET_STALL
;
959 bufoffs
= le32_to_cpu(buf
->InformationBufferOffset
) + 8;
960 buflen
= le32_to_cpu(buf
->InformationBufferLength
);
961 if (bufoffs
+ buflen
> length
)
962 return USB_RET_STALL
;
964 ret
= ndis_set(s
, le32_to_cpu(buf
->OID
),
965 bufoffs
+ (uint8_t *) buf
, buflen
);
966 resp
->MessageType
= cpu_to_le32(RNDIS_SET_CMPLT
);
967 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
968 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_set_cmplt_type
));
970 /* OID not supported */
971 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
974 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
979 static int rndis_reset_response(USBNetState
*s
, rndis_reset_msg_type
*buf
)
981 rndis_reset_cmplt_type
*resp
=
982 rndis_queue_response(s
, sizeof(rndis_reset_cmplt_type
));
985 return USB_RET_STALL
;
987 resp
->MessageType
= cpu_to_le32(RNDIS_RESET_CMPLT
);
988 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_reset_cmplt_type
));
989 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
990 resp
->AddressingReset
= cpu_to_le32(1); /* reset information */
995 static int rndis_keepalive_response(USBNetState
*s
,
996 rndis_keepalive_msg_type
*buf
)
998 rndis_keepalive_cmplt_type
*resp
=
999 rndis_queue_response(s
, sizeof(rndis_keepalive_cmplt_type
));
1002 return USB_RET_STALL
;
1004 resp
->MessageType
= cpu_to_le32(RNDIS_KEEPALIVE_CMPLT
);
1005 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_keepalive_cmplt_type
));
1006 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
1007 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
1012 /* Prepare to receive the next packet */
1013 static void usb_net_reset_in_buf(USBNetState
*s
)
1015 s
->in_ptr
= s
->in_len
= 0;
1016 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
1019 static int rndis_parse(USBNetState
*s
, uint8_t *data
, int length
)
1022 le32
*tmp
= (le32
*) data
;
1024 msg_type
= le32_to_cpup(tmp
);
1027 case RNDIS_INITIALIZE_MSG
:
1028 s
->rndis_state
= RNDIS_INITIALIZED
;
1029 return rndis_init_response(s
, (rndis_init_msg_type
*) data
);
1031 case RNDIS_HALT_MSG
:
1032 s
->rndis_state
= RNDIS_UNINITIALIZED
;
1035 case RNDIS_QUERY_MSG
:
1036 return rndis_query_response(s
, (rndis_query_msg_type
*) data
, length
);
1039 return rndis_set_response(s
, (rndis_set_msg_type
*) data
, length
);
1041 case RNDIS_RESET_MSG
:
1042 rndis_clear_responsequeue(s
);
1044 usb_net_reset_in_buf(s
);
1045 return rndis_reset_response(s
, (rndis_reset_msg_type
*) data
);
1047 case RNDIS_KEEPALIVE_MSG
:
1048 /* For USB: host does this every 5 seconds */
1049 return rndis_keepalive_response(s
, (rndis_keepalive_msg_type
*) data
);
1052 return USB_RET_STALL
;
1055 static void usb_net_handle_reset(USBDevice
*dev
)
1059 static void usb_net_handle_control(USBDevice
*dev
, USBPacket
*p
,
1060 int request
, int value
, int index
, int length
, uint8_t *data
)
1062 USBNetState
*s
= (USBNetState
*) dev
;
1065 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
1071 case ClassInterfaceOutRequest
| USB_CDC_SEND_ENCAPSULATED_COMMAND
:
1072 if (!is_rndis(s
) || value
|| index
!= 0) {
1075 #ifdef TRAFFIC_DEBUG
1078 fprintf(stderr
, "SEND_ENCAPSULATED_COMMAND:");
1079 for (i
= 0; i
< length
; i
++) {
1081 fprintf(stderr
, "\n%04x:", i
);
1082 fprintf(stderr
, " %02x", data
[i
]);
1084 fprintf(stderr
, "\n\n");
1087 ret
= rndis_parse(s
, data
, length
);
1093 case ClassInterfaceRequest
| USB_CDC_GET_ENCAPSULATED_RESPONSE
:
1094 if (!is_rndis(s
) || value
|| index
!= 0) {
1097 p
->actual_length
= rndis_get_response(s
, data
);
1098 if (p
->actual_length
== 0) {
1100 p
->actual_length
= 1;
1102 #ifdef TRAFFIC_DEBUG
1105 fprintf(stderr
, "GET_ENCAPSULATED_RESPONSE:");
1106 for (i
= 0; i
< p
->actual_length
; i
++) {
1108 fprintf(stderr
, "\n%04x:", i
);
1109 fprintf(stderr
, " %02x", data
[i
]);
1111 fprintf(stderr
, "\n\n");
1118 fprintf(stderr
, "usbnet: failed control transaction: "
1119 "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1120 request
, value
, index
, length
);
1121 p
->status
= USB_RET_STALL
;
1126 static void usb_net_handle_statusin(USBNetState
*s
, USBPacket
*p
)
1130 if (p
->iov
.size
< 8) {
1131 p
->status
= USB_RET_STALL
;
1135 buf
[0] = cpu_to_le32(1);
1136 buf
[1] = cpu_to_le32(0);
1137 usb_packet_copy(p
, buf
, 8);
1138 if (!s
->rndis_resp
.tqh_first
) {
1139 p
->status
= USB_RET_NAK
;
1142 #ifdef TRAFFIC_DEBUG
1143 fprintf(stderr
, "usbnet: interrupt poll len %zu return %d",
1144 p
->iov
.size
, p
->status
);
1145 iov_hexdump(p
->iov
.iov
, p
->iov
.niov
, stderr
, "usbnet", p
->status
);
1149 static void usb_net_handle_datain(USBNetState
*s
, USBPacket
*p
)
1153 if (s
->in_ptr
> s
->in_len
) {
1154 usb_net_reset_in_buf(s
);
1155 p
->status
= USB_RET_NAK
;
1159 p
->status
= USB_RET_NAK
;
1162 len
= s
->in_len
- s
->in_ptr
;
1163 if (len
> p
->iov
.size
) {
1166 usb_packet_copy(p
, &s
->in_buf
[s
->in_ptr
], len
);
1168 if (s
->in_ptr
>= s
->in_len
&&
1169 (is_rndis(s
) || (s
->in_len
& (64 - 1)) || !len
)) {
1170 /* no short packet necessary */
1171 usb_net_reset_in_buf(s
);
1174 #ifdef TRAFFIC_DEBUG
1175 fprintf(stderr
, "usbnet: data in len %zu return %d", p
->iov
.size
, len
);
1176 iov_hexdump(p
->iov
.iov
, p
->iov
.niov
, stderr
, "usbnet", len
);
1180 static void usb_net_handle_dataout(USBNetState
*s
, USBPacket
*p
)
1182 int sz
= sizeof(s
->out_buf
) - s
->out_ptr
;
1183 struct rndis_packet_msg_type
*msg
=
1184 (struct rndis_packet_msg_type
*) s
->out_buf
;
1187 #ifdef TRAFFIC_DEBUG
1188 fprintf(stderr
, "usbnet: data out len %zu\n", p
->iov
.size
);
1189 iov_hexdump(p
->iov
.iov
, p
->iov
.niov
, stderr
, "usbnet", p
->iov
.size
);
1192 if (sz
> p
->iov
.size
) {
1195 usb_packet_copy(p
, &s
->out_buf
[s
->out_ptr
], sz
);
1199 if (p
->iov
.size
< 64) {
1200 qemu_send_packet(qemu_get_queue(s
->nic
), s
->out_buf
, s
->out_ptr
);
1205 len
= le32_to_cpu(msg
->MessageLength
);
1206 if (s
->out_ptr
< 8 || s
->out_ptr
< len
) {
1209 if (le32_to_cpu(msg
->MessageType
) == RNDIS_PACKET_MSG
) {
1210 uint32_t offs
= 8 + le32_to_cpu(msg
->DataOffset
);
1211 uint32_t size
= le32_to_cpu(msg
->DataLength
);
1212 if (offs
+ size
<= len
)
1213 qemu_send_packet(qemu_get_queue(s
->nic
), s
->out_buf
+ offs
, size
);
1216 memmove(s
->out_buf
, &s
->out_buf
[len
], s
->out_ptr
);
1219 static void usb_net_handle_data(USBDevice
*dev
, USBPacket
*p
)
1221 USBNetState
*s
= (USBNetState
*) dev
;
1225 switch (p
->ep
->nr
) {
1227 usb_net_handle_statusin(s
, p
);
1231 usb_net_handle_datain(s
, p
);
1240 switch (p
->ep
->nr
) {
1242 usb_net_handle_dataout(s
, p
);
1252 p
->status
= USB_RET_STALL
;
1256 if (p
->status
== USB_RET_STALL
) {
1257 fprintf(stderr
, "usbnet: failed data transaction: "
1258 "pid 0x%x ep 0x%x len 0x%zx\n",
1259 p
->pid
, p
->ep
->nr
, p
->iov
.size
);
1263 static ssize_t
usbnet_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
1265 USBNetState
*s
= qemu_get_nic_opaque(nc
);
1266 uint8_t *in_buf
= s
->in_buf
;
1267 size_t total_size
= size
;
1270 if (s
->rndis_state
!= RNDIS_DATA_INITIALIZED
) {
1273 total_size
+= sizeof(struct rndis_packet_msg_type
);
1275 if (total_size
> sizeof(s
->in_buf
)) {
1279 /* Only accept packet if input buffer is empty */
1280 if (s
->in_len
> 0) {
1285 struct rndis_packet_msg_type
*msg
;
1287 msg
= (struct rndis_packet_msg_type
*)in_buf
;
1288 memset(msg
, 0, sizeof(struct rndis_packet_msg_type
));
1289 msg
->MessageType
= cpu_to_le32(RNDIS_PACKET_MSG
);
1290 msg
->MessageLength
= cpu_to_le32(size
+ sizeof(*msg
));
1291 msg
->DataOffset
= cpu_to_le32(sizeof(*msg
) - 8);
1292 msg
->DataLength
= cpu_to_le32(size
);
1293 /* msg->OOBDataOffset;
1294 * msg->OOBDataLength;
1295 * msg->NumOOBDataElements;
1296 * msg->PerPacketInfoOffset;
1297 * msg->PerPacketInfoLength;
1301 in_buf
+= sizeof(*msg
);
1304 memcpy(in_buf
, buf
, size
);
1305 s
->in_len
= total_size
;
1310 static int usbnet_can_receive(NetClientState
*nc
)
1312 USBNetState
*s
= qemu_get_nic_opaque(nc
);
1314 if (is_rndis(s
) && s
->rndis_state
!= RNDIS_DATA_INITIALIZED
) {
1321 static void usbnet_cleanup(NetClientState
*nc
)
1323 USBNetState
*s
= qemu_get_nic_opaque(nc
);
1328 static void usb_net_handle_destroy(USBDevice
*dev
)
1330 USBNetState
*s
= (USBNetState
*) dev
;
1332 /* TODO: remove the nd_table[] entry */
1333 rndis_clear_responsequeue(s
);
1334 qemu_del_nic(s
->nic
);
1337 static NetClientInfo net_usbnet_info
= {
1338 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
1339 .size
= sizeof(NICState
),
1340 .can_receive
= usbnet_can_receive
,
1341 .receive
= usbnet_receive
,
1342 .cleanup
= usbnet_cleanup
,
1345 static int usb_net_initfn(USBDevice
*dev
)
1347 USBNetState
*s
= DO_UPCAST(USBNetState
, dev
, dev
);
1349 usb_desc_create_serial(dev
);
1352 s
->rndis_state
= RNDIS_UNINITIALIZED
;
1353 QTAILQ_INIT(&s
->rndis_resp
);
1355 s
->medium
= 0; /* NDIS_MEDIUM_802_3 */
1356 s
->speed
= 1000000; /* 100MBps, in 100Bps units */
1357 s
->media_state
= 0; /* NDIS_MEDIA_STATE_CONNECTED */;
1359 s
->vendorid
= 0x1234;
1360 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
1362 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
1363 s
->nic
= qemu_new_nic(&net_usbnet_info
, &s
->conf
,
1364 object_get_typename(OBJECT(s
)), s
->dev
.qdev
.id
, s
);
1365 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
1366 snprintf(s
->usbstring_mac
, sizeof(s
->usbstring_mac
),
1367 "%02x%02x%02x%02x%02x%02x",
1369 s
->conf
.macaddr
.a
[1],
1370 s
->conf
.macaddr
.a
[2],
1371 s
->conf
.macaddr
.a
[3],
1372 s
->conf
.macaddr
.a
[4],
1373 s
->conf
.macaddr
.a
[5]);
1374 usb_desc_set_string(dev
, STRING_ETHADDR
, s
->usbstring_mac
);
1376 add_boot_device_path(s
->conf
.bootindex
, &dev
->qdev
, "/ethernet@0");
1380 static USBDevice
*usb_net_init(USBBus
*bus
, const char *cmdline
)
1382 Error
*local_err
= NULL
;
1387 opts
= qemu_opts_parse(qemu_find_opts("net"), cmdline
, 0);
1391 qemu_opt_set(opts
, "type", "nic");
1392 qemu_opt_set(opts
, "model", "usb");
1394 idx
= net_client_init(opts
, 0, &local_err
);
1395 if (error_is_set(&local_err
)) {
1396 qerror_report_err(local_err
);
1397 error_free(local_err
);
1401 dev
= usb_create(bus
, "usb-net");
1405 qdev_set_nic_properties(&dev
->qdev
, &nd_table
[idx
]);
1406 qdev_init_nofail(&dev
->qdev
);
1410 static const VMStateDescription vmstate_usb_net
= {
1415 static Property net_properties
[] = {
1416 DEFINE_NIC_PROPERTIES(USBNetState
, conf
),
1417 DEFINE_PROP_END_OF_LIST(),
1420 static void usb_net_class_initfn(ObjectClass
*klass
, void *data
)
1422 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1423 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1425 uc
->init
= usb_net_initfn
;
1426 uc
->product_desc
= "QEMU USB Network Interface";
1427 uc
->usb_desc
= &desc_net
;
1428 uc
->handle_reset
= usb_net_handle_reset
;
1429 uc
->handle_control
= usb_net_handle_control
;
1430 uc
->handle_data
= usb_net_handle_data
;
1431 uc
->handle_destroy
= usb_net_handle_destroy
;
1432 dc
->fw_name
= "network";
1433 dc
->vmsd
= &vmstate_usb_net
;
1434 dc
->props
= net_properties
;
1437 static const TypeInfo net_info
= {
1439 .parent
= TYPE_USB_DEVICE
,
1440 .instance_size
= sizeof(USBNetState
),
1441 .class_init
= usb_net_class_initfn
,
1444 static void usb_net_register_types(void)
1446 type_register_static(&net_info
);
1447 usb_legacy_register("usb-net", "net", usb_net_init
);
1450 type_init(usb_net_register_types
)