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"
29 #include "qemu-queue.h"
32 /*#define TRAFFIC_DEBUG*/
33 /* Thanks to NetChip Technologies for donating this product ID.
34 * It's for devices with only CDC Ethernet configurations.
36 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
37 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
38 /* For hardware that can talk RNDIS and either of the above protocols,
39 * use this ID ... the windows INF files will know it.
41 #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
42 #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
45 STRING_MANUFACTURER
= 1,
57 #define DEV_CONFIG_VALUE 1 /* CDC or a subset */
58 #define DEV_RNDIS_CONFIG_VALUE 2 /* RNDIS; optional */
60 #define USB_CDC_SUBCLASS_ACM 0x02
61 #define USB_CDC_SUBCLASS_ETHERNET 0x06
63 #define USB_CDC_PROTO_NONE 0
64 #define USB_CDC_ACM_PROTO_VENDOR 0xff
66 #define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
67 #define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
68 #define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
69 #define USB_CDC_UNION_TYPE 0x06 /* union_desc */
70 #define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
72 #define USB_DT_CS_INTERFACE 0x24
73 #define USB_DT_CS_ENDPOINT 0x25
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 */
93 * mostly the same descriptor as the linux gadget rndis driver
95 static const uint8_t qemu_net_dev_descriptor
[] = {
96 0x12, /* u8 bLength; */
97 USB_DT_DEVICE
, /* u8 bDescriptorType; Device */
98 0x00, 0x02, /* u16 bcdUSB; v2.0 */
99 USB_CLASS_COMM
, /* u8 bDeviceClass; */
100 0x00, /* u8 bDeviceSubClass; */
101 0x00, /* u8 bDeviceProtocol; [ low/full only ] */
102 0x40, /* u8 bMaxPacketSize0 */
103 RNDIS_VENDOR_NUM
& 0xff, RNDIS_VENDOR_NUM
>> 8, /* u16 idVendor; */
104 RNDIS_PRODUCT_NUM
& 0xff, RNDIS_PRODUCT_NUM
>> 8, /* u16 idProduct; */
105 0x00, 0x00, /* u16 bcdDevice */
106 STRING_MANUFACTURER
, /* u8 iManufacturer; */
107 STRING_PRODUCT
, /* u8 iProduct; */
108 STRING_SERIALNUMBER
, /* u8 iSerialNumber; */
109 0x02, /* u8 bNumConfigurations; */
112 static const uint8_t qemu_net_rndis_config_descriptor
[] = {
113 /* Configuration Descriptor */
114 0x09, /* u8 bLength */
115 USB_DT_CONFIG
, /* u8 bDescriptorType */
116 0x43, 0x00, /* le16 wTotalLength */
117 0x02, /* u8 bNumInterfaces */
118 DEV_RNDIS_CONFIG_VALUE
, /* u8 bConfigurationValue */
119 STRING_RNDIS
, /* u8 iConfiguration */
120 0xc0, /* u8 bmAttributes */
121 0x32, /* u8 bMaxPower */
122 /* RNDIS Control Interface */
123 0x09, /* u8 bLength */
124 USB_DT_INTERFACE
, /* u8 bDescriptorType */
125 0x00, /* u8 bInterfaceNumber */
126 0x00, /* u8 bAlternateSetting */
127 0x01, /* u8 bNumEndpoints */
128 USB_CLASS_COMM
, /* u8 bInterfaceClass */
129 USB_CDC_SUBCLASS_ACM
, /* u8 bInterfaceSubClass */
130 USB_CDC_ACM_PROTO_VENDOR
, /* u8 bInterfaceProtocol */
131 STRING_RNDIS_CONTROL
, /* u8 iInterface */
132 /* Header Descriptor */
133 0x05, /* u8 bLength */
134 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
135 USB_CDC_HEADER_TYPE
, /* u8 bDescriptorSubType */
136 0x10, 0x01, /* le16 bcdCDC */
137 /* Call Management Descriptor */
138 0x05, /* u8 bLength */
139 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
140 USB_CDC_CALL_MANAGEMENT_TYPE
, /* u8 bDescriptorSubType */
141 0x00, /* u8 bmCapabilities */
142 0x01, /* u8 bDataInterface */
144 0x04, /* u8 bLength */
145 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
146 USB_CDC_ACM_TYPE
, /* u8 bDescriptorSubType */
147 0x00, /* u8 bmCapabilities */
148 /* Union Descriptor */
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 /* Status Descriptor */
155 0x07, /* u8 bLength */
156 USB_DT_ENDPOINT
, /* u8 bDescriptorType */
157 USB_DIR_IN
| 1, /* u8 bEndpointAddress */
158 USB_ENDPOINT_XFER_INT
, /* u8 bmAttributes */
159 STATUS_BYTECOUNT
& 0xff, STATUS_BYTECOUNT
>> 8, /* le16 wMaxPacketSize */
160 1 << LOG2_STATUS_INTERVAL_MSEC
, /* u8 bInterval */
161 /* RNDIS Data Interface */
162 0x09, /* u8 bLength */
163 USB_DT_INTERFACE
, /* u8 bDescriptorType */
164 0x01, /* u8 bInterfaceNumber */
165 0x00, /* u8 bAlternateSetting */
166 0x02, /* u8 bNumEndpoints */
167 USB_CLASS_CDC_DATA
, /* u8 bInterfaceClass */
168 0x00, /* u8 bInterfaceSubClass */
169 0x00, /* u8 bInterfaceProtocol */
170 STRING_DATA
, /* u8 iInterface */
171 /* Source Endpoint */
172 0x07, /* u8 bLength */
173 USB_DT_ENDPOINT
, /* u8 bDescriptorType */
174 USB_DIR_IN
| 2, /* u8 bEndpointAddress */
175 USB_ENDPOINT_XFER_BULK
, /* u8 bmAttributes */
176 0x40, 0x00, /* le16 wMaxPacketSize */
177 0x00, /* u8 bInterval */
179 0x07, /* u8 bLength */
180 USB_DT_ENDPOINT
, /* u8 bDescriptorType */
181 USB_DIR_OUT
| 2, /* u8 bEndpointAddress */
182 USB_ENDPOINT_XFER_BULK
, /* u8 bmAttributes */
183 0x40, 0x00, /* le16 wMaxPacketSize */
184 0x00 /* u8 bInterval */
187 static const uint8_t qemu_net_cdc_config_descriptor
[] = {
188 /* Configuration Descriptor */
189 0x09, /* u8 bLength */
190 USB_DT_CONFIG
, /* u8 bDescriptorType */
191 0x50, 0x00, /* le16 wTotalLength */
192 0x02, /* u8 bNumInterfaces */
193 DEV_CONFIG_VALUE
, /* u8 bConfigurationValue */
194 STRING_CDC
, /* u8 iConfiguration */
195 0xc0, /* u8 bmAttributes */
196 0x32, /* u8 bMaxPower */
197 /* CDC Control Interface */
198 0x09, /* u8 bLength */
199 USB_DT_INTERFACE
, /* u8 bDescriptorType */
200 0x00, /* u8 bInterfaceNumber */
201 0x00, /* u8 bAlternateSetting */
202 0x01, /* u8 bNumEndpoints */
203 USB_CLASS_COMM
, /* u8 bInterfaceClass */
204 USB_CDC_SUBCLASS_ETHERNET
, /* u8 bInterfaceSubClass */
205 USB_CDC_PROTO_NONE
, /* u8 bInterfaceProtocol */
206 STRING_CONTROL
, /* u8 iInterface */
207 /* Header Descriptor */
208 0x05, /* u8 bLength */
209 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
210 USB_CDC_HEADER_TYPE
, /* u8 bDescriptorSubType */
211 0x10, 0x01, /* le16 bcdCDC */
212 /* Union Descriptor */
213 0x05, /* u8 bLength */
214 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
215 USB_CDC_UNION_TYPE
, /* u8 bDescriptorSubType */
216 0x00, /* u8 bMasterInterface0 */
217 0x01, /* u8 bSlaveInterface0 */
218 /* Ethernet Descriptor */
219 0x0d, /* u8 bLength */
220 USB_DT_CS_INTERFACE
, /* u8 bDescriptorType */
221 USB_CDC_ETHERNET_TYPE
, /* u8 bDescriptorSubType */
222 STRING_ETHADDR
, /* u8 iMACAddress */
223 0x00, 0x00, 0x00, 0x00, /* le32 bmEthernetStatistics */
224 ETH_FRAME_LEN
& 0xff, ETH_FRAME_LEN
>> 8, /* le16 wMaxSegmentSize */
225 0x00, 0x00, /* le16 wNumberMCFilters */
226 0x00, /* u8 bNumberPowerFilters */
227 /* Status Descriptor */
228 0x07, /* u8 bLength */
229 USB_DT_ENDPOINT
, /* u8 bDescriptorType */
230 USB_DIR_IN
| 1, /* u8 bEndpointAddress */
231 USB_ENDPOINT_XFER_INT
, /* u8 bmAttributes */
232 STATUS_BYTECOUNT
& 0xff, STATUS_BYTECOUNT
>> 8, /* le16 wMaxPacketSize */
233 1 << LOG2_STATUS_INTERVAL_MSEC
, /* u8 bInterval */
234 /* CDC Data (nop) Interface */
235 0x09, /* u8 bLength */
236 USB_DT_INTERFACE
, /* u8 bDescriptorType */
237 0x01, /* u8 bInterfaceNumber */
238 0x00, /* u8 bAlternateSetting */
239 0x00, /* u8 bNumEndpoints */
240 USB_CLASS_CDC_DATA
, /* u8 bInterfaceClass */
241 0x00, /* u8 bInterfaceSubClass */
242 0x00, /* u8 bInterfaceProtocol */
243 0x00, /* u8 iInterface */
244 /* CDC Data Interface */
245 0x09, /* u8 bLength */
246 USB_DT_INTERFACE
, /* u8 bDescriptorType */
247 0x01, /* u8 bInterfaceNumber */
248 0x01, /* u8 bAlternateSetting */
249 0x02, /* u8 bNumEndpoints */
250 USB_CLASS_CDC_DATA
, /* u8 bInterfaceClass */
251 0x00, /* u8 bInterfaceSubClass */
252 0x00, /* u8 bInterfaceProtocol */
253 STRING_DATA
, /* u8 iInterface */
254 /* Source Endpoint */
255 0x07, /* u8 bLength */
256 USB_DT_ENDPOINT
, /* u8 bDescriptorType */
257 USB_DIR_IN
| 2, /* u8 bEndpointAddress */
258 USB_ENDPOINT_XFER_BULK
, /* u8 bmAttributes */
259 0x40, 0x00, /* le16 wMaxPacketSize */
260 0x00, /* u8 bInterval */
262 0x07, /* u8 bLength */
263 USB_DT_ENDPOINT
, /* u8 bDescriptorType */
264 USB_DIR_OUT
| 2, /* u8 bEndpointAddress */
265 USB_ENDPOINT_XFER_BULK
, /* u8 bmAttributes */
266 0x40, 0x00, /* le16 wMaxPacketSize */
267 0x00 /* u8 bInterval */
271 * RNDIS Definitions - in theory not specific to USB.
273 #define RNDIS_MAXIMUM_FRAME_SIZE 1518
274 #define RNDIS_MAX_TOTAL_SIZE 1558
276 /* Remote NDIS Versions */
277 #define RNDIS_MAJOR_VERSION 1
278 #define RNDIS_MINOR_VERSION 0
281 #define RNDIS_STATUS_SUCCESS 0x00000000U /* Success */
282 #define RNDIS_STATUS_FAILURE 0xc0000001U /* Unspecified error */
283 #define RNDIS_STATUS_INVALID_DATA 0xc0010015U /* Invalid data */
284 #define RNDIS_STATUS_NOT_SUPPORTED 0xc00000bbU /* Unsupported request */
285 #define RNDIS_STATUS_MEDIA_CONNECT 0x4001000bU /* Device connected */
286 #define RNDIS_STATUS_MEDIA_DISCONNECT 0x4001000cU /* Device disconnected */
288 /* Message Set for Connectionless (802.3) Devices */
290 RNDIS_PACKET_MSG
= 1,
291 RNDIS_INITIALIZE_MSG
= 2, /* Initialize device */
296 RNDIS_INDICATE_STATUS_MSG
= 7,
297 RNDIS_KEEPALIVE_MSG
= 8,
300 /* Message completion */
302 RNDIS_INITIALIZE_CMPLT
= 0x80000002U
,
303 RNDIS_QUERY_CMPLT
= 0x80000004U
,
304 RNDIS_SET_CMPLT
= 0x80000005U
,
305 RNDIS_RESET_CMPLT
= 0x80000006U
,
306 RNDIS_KEEPALIVE_CMPLT
= 0x80000008U
,
311 RNDIS_DF_CONNECTIONLESS
= 1,
312 RNDIS_DF_CONNECTIONORIENTED
= 2,
315 #define RNDIS_MEDIUM_802_3 0x00000000U
317 /* from drivers/net/sk98lin/h/skgepnmi.h */
318 #define OID_PNP_CAPABILITIES 0xfd010100
319 #define OID_PNP_SET_POWER 0xfd010101
320 #define OID_PNP_QUERY_POWER 0xfd010102
321 #define OID_PNP_ADD_WAKE_UP_PATTERN 0xfd010103
322 #define OID_PNP_REMOVE_WAKE_UP_PATTERN 0xfd010104
323 #define OID_PNP_ENABLE_WAKE_UP 0xfd010106
325 typedef uint32_t le32
;
327 typedef struct rndis_init_msg_type
{
333 le32 MaxTransferSize
;
334 } rndis_init_msg_type
;
336 typedef struct rndis_init_cmplt_type
{
345 le32 MaxPacketsPerTransfer
;
346 le32 MaxTransferSize
;
347 le32 PacketAlignmentFactor
;
350 } rndis_init_cmplt_type
;
352 typedef struct rndis_halt_msg_type
{
356 } rndis_halt_msg_type
;
358 typedef struct rndis_query_msg_type
{
363 le32 InformationBufferLength
;
364 le32 InformationBufferOffset
;
366 } rndis_query_msg_type
;
368 typedef struct rndis_query_cmplt_type
{
373 le32 InformationBufferLength
;
374 le32 InformationBufferOffset
;
375 } rndis_query_cmplt_type
;
377 typedef struct rndis_set_msg_type
{
382 le32 InformationBufferLength
;
383 le32 InformationBufferOffset
;
385 } rndis_set_msg_type
;
387 typedef struct rndis_set_cmplt_type
{
392 } rndis_set_cmplt_type
;
394 typedef struct rndis_reset_msg_type
{
398 } rndis_reset_msg_type
;
400 typedef struct rndis_reset_cmplt_type
{
404 le32 AddressingReset
;
405 } rndis_reset_cmplt_type
;
407 typedef struct rndis_indicate_status_msg_type
{
411 le32 StatusBufferLength
;
412 le32 StatusBufferOffset
;
413 } rndis_indicate_status_msg_type
;
415 typedef struct rndis_keepalive_msg_type
{
419 } rndis_keepalive_msg_type
;
421 typedef struct rndis_keepalive_cmplt_type
{
426 } rndis_keepalive_cmplt_type
;
428 struct rndis_packet_msg_type
{
435 le32 NumOOBDataElements
;
436 le32 PerPacketInfoOffset
;
437 le32 PerPacketInfoLength
;
442 struct rndis_config_parameter
{
443 le32 ParameterNameOffset
;
444 le32 ParameterNameLength
;
446 le32 ParameterValueOffset
;
447 le32 ParameterValueLength
;
450 /* implementation specific */
455 RNDIS_DATA_INITIALIZED
,
460 /* Required Object IDs (OIDs) */
461 OID_GEN_SUPPORTED_LIST
= 0x00010101,
462 OID_GEN_HARDWARE_STATUS
= 0x00010102,
463 OID_GEN_MEDIA_SUPPORTED
= 0x00010103,
464 OID_GEN_MEDIA_IN_USE
= 0x00010104,
465 OID_GEN_MAXIMUM_LOOKAHEAD
= 0x00010105,
466 OID_GEN_MAXIMUM_FRAME_SIZE
= 0x00010106,
467 OID_GEN_LINK_SPEED
= 0x00010107,
468 OID_GEN_TRANSMIT_BUFFER_SPACE
= 0x00010108,
469 OID_GEN_RECEIVE_BUFFER_SPACE
= 0x00010109,
470 OID_GEN_TRANSMIT_BLOCK_SIZE
= 0x0001010a,
471 OID_GEN_RECEIVE_BLOCK_SIZE
= 0x0001010b,
472 OID_GEN_VENDOR_ID
= 0x0001010c,
473 OID_GEN_VENDOR_DESCRIPTION
= 0x0001010d,
474 OID_GEN_CURRENT_PACKET_FILTER
= 0x0001010e,
475 OID_GEN_CURRENT_LOOKAHEAD
= 0x0001010f,
476 OID_GEN_DRIVER_VERSION
= 0x00010110,
477 OID_GEN_MAXIMUM_TOTAL_SIZE
= 0x00010111,
478 OID_GEN_PROTOCOL_OPTIONS
= 0x00010112,
479 OID_GEN_MAC_OPTIONS
= 0x00010113,
480 OID_GEN_MEDIA_CONNECT_STATUS
= 0x00010114,
481 OID_GEN_MAXIMUM_SEND_PACKETS
= 0x00010115,
482 OID_GEN_VENDOR_DRIVER_VERSION
= 0x00010116,
483 OID_GEN_SUPPORTED_GUIDS
= 0x00010117,
484 OID_GEN_NETWORK_LAYER_ADDRESSES
= 0x00010118,
485 OID_GEN_TRANSPORT_HEADER_OFFSET
= 0x00010119,
486 OID_GEN_MACHINE_NAME
= 0x0001021a,
487 OID_GEN_RNDIS_CONFIG_PARAMETER
= 0x0001021b,
488 OID_GEN_VLAN_ID
= 0x0001021c,
491 OID_GEN_MEDIA_CAPABILITIES
= 0x00010201,
492 OID_GEN_PHYSICAL_MEDIUM
= 0x00010202,
494 /* Required statistics OIDs */
495 OID_GEN_XMIT_OK
= 0x00020101,
496 OID_GEN_RCV_OK
= 0x00020102,
497 OID_GEN_XMIT_ERROR
= 0x00020103,
498 OID_GEN_RCV_ERROR
= 0x00020104,
499 OID_GEN_RCV_NO_BUFFER
= 0x00020105,
501 /* Optional statistics OIDs */
502 OID_GEN_DIRECTED_BYTES_XMIT
= 0x00020201,
503 OID_GEN_DIRECTED_FRAMES_XMIT
= 0x00020202,
504 OID_GEN_MULTICAST_BYTES_XMIT
= 0x00020203,
505 OID_GEN_MULTICAST_FRAMES_XMIT
= 0x00020204,
506 OID_GEN_BROADCAST_BYTES_XMIT
= 0x00020205,
507 OID_GEN_BROADCAST_FRAMES_XMIT
= 0x00020206,
508 OID_GEN_DIRECTED_BYTES_RCV
= 0x00020207,
509 OID_GEN_DIRECTED_FRAMES_RCV
= 0x00020208,
510 OID_GEN_MULTICAST_BYTES_RCV
= 0x00020209,
511 OID_GEN_MULTICAST_FRAMES_RCV
= 0x0002020a,
512 OID_GEN_BROADCAST_BYTES_RCV
= 0x0002020b,
513 OID_GEN_BROADCAST_FRAMES_RCV
= 0x0002020c,
514 OID_GEN_RCV_CRC_ERROR
= 0x0002020d,
515 OID_GEN_TRANSMIT_QUEUE_LENGTH
= 0x0002020e,
516 OID_GEN_GET_TIME_CAPS
= 0x0002020f,
517 OID_GEN_GET_NETCARD_TIME
= 0x00020210,
518 OID_GEN_NETCARD_LOAD
= 0x00020211,
519 OID_GEN_DEVICE_PROFILE
= 0x00020212,
520 OID_GEN_INIT_TIME_MS
= 0x00020213,
521 OID_GEN_RESET_COUNTS
= 0x00020214,
522 OID_GEN_MEDIA_SENSE_COUNTS
= 0x00020215,
523 OID_GEN_FRIENDLY_NAME
= 0x00020216,
524 OID_GEN_MINIPORT_INFO
= 0x00020217,
525 OID_GEN_RESET_VERIFY_PARAMETERS
= 0x00020218,
527 /* IEEE 802.3 (Ethernet) OIDs */
528 OID_802_3_PERMANENT_ADDRESS
= 0x01010101,
529 OID_802_3_CURRENT_ADDRESS
= 0x01010102,
530 OID_802_3_MULTICAST_LIST
= 0x01010103,
531 OID_802_3_MAXIMUM_LIST_SIZE
= 0x01010104,
532 OID_802_3_MAC_OPTIONS
= 0x01010105,
533 OID_802_3_RCV_ERROR_ALIGNMENT
= 0x01020101,
534 OID_802_3_XMIT_ONE_COLLISION
= 0x01020102,
535 OID_802_3_XMIT_MORE_COLLISIONS
= 0x01020103,
536 OID_802_3_XMIT_DEFERRED
= 0x01020201,
537 OID_802_3_XMIT_MAX_COLLISIONS
= 0x01020202,
538 OID_802_3_RCV_OVERRUN
= 0x01020203,
539 OID_802_3_XMIT_UNDERRUN
= 0x01020204,
540 OID_802_3_XMIT_HEARTBEAT_FAILURE
= 0x01020205,
541 OID_802_3_XMIT_TIMES_CRS_LOST
= 0x01020206,
542 OID_802_3_XMIT_LATE_COLLISIONS
= 0x01020207,
545 static const uint32_t oid_supported_list
[] =
547 /* the general stuff */
548 OID_GEN_SUPPORTED_LIST
,
549 OID_GEN_HARDWARE_STATUS
,
550 OID_GEN_MEDIA_SUPPORTED
,
551 OID_GEN_MEDIA_IN_USE
,
552 OID_GEN_MAXIMUM_FRAME_SIZE
,
554 OID_GEN_TRANSMIT_BLOCK_SIZE
,
555 OID_GEN_RECEIVE_BLOCK_SIZE
,
557 OID_GEN_VENDOR_DESCRIPTION
,
558 OID_GEN_VENDOR_DRIVER_VERSION
,
559 OID_GEN_CURRENT_PACKET_FILTER
,
560 OID_GEN_MAXIMUM_TOTAL_SIZE
,
561 OID_GEN_MEDIA_CONNECT_STATUS
,
562 OID_GEN_PHYSICAL_MEDIUM
,
564 /* the statistical stuff */
569 OID_GEN_RCV_NO_BUFFER
,
572 /* the general stuff */
573 OID_802_3_PERMANENT_ADDRESS
,
574 OID_802_3_CURRENT_ADDRESS
,
575 OID_802_3_MULTICAST_LIST
,
576 OID_802_3_MAC_OPTIONS
,
577 OID_802_3_MAXIMUM_LIST_SIZE
,
579 /* the statistical stuff */
580 OID_802_3_RCV_ERROR_ALIGNMENT
,
581 OID_802_3_XMIT_ONE_COLLISION
,
582 OID_802_3_XMIT_MORE_COLLISIONS
,
585 #define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA (1 << 0)
586 #define NDIS_MAC_OPTION_RECEIVE_SERIALIZED (1 << 1)
587 #define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND (1 << 2)
588 #define NDIS_MAC_OPTION_NO_LOOPBACK (1 << 3)
589 #define NDIS_MAC_OPTION_FULL_DUPLEX (1 << 4)
590 #define NDIS_MAC_OPTION_EOTX_INDICATION (1 << 5)
591 #define NDIS_MAC_OPTION_8021P_PRIORITY (1 << 6)
593 struct rndis_response
{
594 QTAILQ_ENTRY(rndis_response
) entries
;
599 typedef struct USBNetState
{
603 enum rndis_state rndis_state
;
606 uint32_t media_state
;
610 unsigned int out_ptr
;
611 uint8_t out_buf
[2048];
614 unsigned int in_ptr
, in_len
;
615 uint8_t in_buf
[2048];
617 char usbstring_mac
[13];
620 QTAILQ_HEAD(rndis_resp_head
, rndis_response
) rndis_resp
;
623 static int ndis_query(USBNetState
*s
, uint32_t oid
,
624 uint8_t *inbuf
, unsigned int inlen
, uint8_t *outbuf
,
630 /* general oids (table 4-1) */
632 case OID_GEN_SUPPORTED_LIST
:
633 for (i
= 0; i
< ARRAY_SIZE(oid_supported_list
); i
++)
634 ((le32
*) outbuf
)[i
] = cpu_to_le32(oid_supported_list
[i
]);
635 return sizeof(oid_supported_list
);
638 case OID_GEN_HARDWARE_STATUS
:
639 *((le32
*) outbuf
) = cpu_to_le32(0);
643 case OID_GEN_MEDIA_SUPPORTED
:
644 *((le32
*) outbuf
) = cpu_to_le32(s
->medium
);
648 case OID_GEN_MEDIA_IN_USE
:
649 *((le32
*) outbuf
) = cpu_to_le32(s
->medium
);
653 case OID_GEN_MAXIMUM_FRAME_SIZE
:
654 *((le32
*) outbuf
) = cpu_to_le32(ETH_FRAME_LEN
);
658 case OID_GEN_LINK_SPEED
:
659 *((le32
*) outbuf
) = cpu_to_le32(s
->speed
);
663 case OID_GEN_TRANSMIT_BLOCK_SIZE
:
664 *((le32
*) outbuf
) = cpu_to_le32(ETH_FRAME_LEN
);
668 case OID_GEN_RECEIVE_BLOCK_SIZE
:
669 *((le32
*) outbuf
) = cpu_to_le32(ETH_FRAME_LEN
);
673 case OID_GEN_VENDOR_ID
:
674 *((le32
*) outbuf
) = cpu_to_le32(s
->vendorid
);
678 case OID_GEN_VENDOR_DESCRIPTION
:
679 pstrcpy((char *)outbuf
, outlen
, "QEMU USB RNDIS Net");
680 return strlen((char *)outbuf
) + 1;
682 case OID_GEN_VENDOR_DRIVER_VERSION
:
683 *((le32
*) outbuf
) = cpu_to_le32(1);
687 case OID_GEN_CURRENT_PACKET_FILTER
:
688 *((le32
*) outbuf
) = cpu_to_le32(s
->filter
);
692 case OID_GEN_MAXIMUM_TOTAL_SIZE
:
693 *((le32
*) outbuf
) = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE
);
697 case OID_GEN_MEDIA_CONNECT_STATUS
:
698 *((le32
*) outbuf
) = cpu_to_le32(s
->media_state
);
701 case OID_GEN_PHYSICAL_MEDIUM
:
702 *((le32
*) outbuf
) = cpu_to_le32(0);
705 case OID_GEN_MAC_OPTIONS
:
706 *((le32
*) outbuf
) = cpu_to_le32(
707 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
|
708 NDIS_MAC_OPTION_FULL_DUPLEX
);
711 /* statistics OIDs (table 4-2) */
713 case OID_GEN_XMIT_OK
:
714 *((le32
*) outbuf
) = cpu_to_le32(0);
719 *((le32
*) outbuf
) = cpu_to_le32(0);
723 case OID_GEN_XMIT_ERROR
:
724 *((le32
*) outbuf
) = cpu_to_le32(0);
728 case OID_GEN_RCV_ERROR
:
729 *((le32
*) outbuf
) = cpu_to_le32(0);
733 case OID_GEN_RCV_NO_BUFFER
:
734 *((le32
*) outbuf
) = cpu_to_le32(0);
737 /* ieee802.3 OIDs (table 4-3) */
739 case OID_802_3_PERMANENT_ADDRESS
:
740 memcpy(outbuf
, s
->conf
.macaddr
.a
, 6);
744 case OID_802_3_CURRENT_ADDRESS
:
745 memcpy(outbuf
, s
->conf
.macaddr
.a
, 6);
749 case OID_802_3_MULTICAST_LIST
:
750 *((le32
*) outbuf
) = cpu_to_le32(0xe0000000);
754 case OID_802_3_MAXIMUM_LIST_SIZE
:
755 *((le32
*) outbuf
) = cpu_to_le32(1);
758 case OID_802_3_MAC_OPTIONS
:
761 /* ieee802.3 statistics OIDs (table 4-4) */
763 case OID_802_3_RCV_ERROR_ALIGNMENT
:
764 *((le32
*) outbuf
) = cpu_to_le32(0);
768 case OID_802_3_XMIT_ONE_COLLISION
:
769 *((le32
*) outbuf
) = cpu_to_le32(0);
773 case OID_802_3_XMIT_MORE_COLLISIONS
:
774 *((le32
*) outbuf
) = cpu_to_le32(0);
778 fprintf(stderr
, "usbnet: unknown OID 0x%08x\n", oid
);
784 static int ndis_set(USBNetState
*s
, uint32_t oid
,
785 uint8_t *inbuf
, unsigned int inlen
)
788 case OID_GEN_CURRENT_PACKET_FILTER
:
789 s
->filter
= le32_to_cpup((le32
*) inbuf
);
791 s
->rndis_state
= RNDIS_DATA_INITIALIZED
;
793 s
->rndis_state
= RNDIS_INITIALIZED
;
797 case OID_802_3_MULTICAST_LIST
:
803 static int rndis_get_response(USBNetState
*s
, uint8_t *buf
)
806 struct rndis_response
*r
= s
->rndis_resp
.tqh_first
;
811 QTAILQ_REMOVE(&s
->rndis_resp
, r
, entries
);
813 memcpy(buf
, r
->buf
, r
->length
);
819 static void *rndis_queue_response(USBNetState
*s
, unsigned int length
)
821 struct rndis_response
*r
=
822 qemu_mallocz(sizeof(struct rndis_response
) + length
);
824 QTAILQ_INSERT_TAIL(&s
->rndis_resp
, r
, entries
);
830 static void rndis_clear_responsequeue(USBNetState
*s
)
832 struct rndis_response
*r
;
834 while ((r
= s
->rndis_resp
.tqh_first
)) {
835 QTAILQ_REMOVE(&s
->rndis_resp
, r
, entries
);
840 static int rndis_init_response(USBNetState
*s
, rndis_init_msg_type
*buf
)
842 rndis_init_cmplt_type
*resp
=
843 rndis_queue_response(s
, sizeof(rndis_init_cmplt_type
));
846 return USB_RET_STALL
;
848 resp
->MessageType
= cpu_to_le32(RNDIS_INITIALIZE_CMPLT
);
849 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_init_cmplt_type
));
850 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
851 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
852 resp
->MajorVersion
= cpu_to_le32(RNDIS_MAJOR_VERSION
);
853 resp
->MinorVersion
= cpu_to_le32(RNDIS_MINOR_VERSION
);
854 resp
->DeviceFlags
= cpu_to_le32(RNDIS_DF_CONNECTIONLESS
);
855 resp
->Medium
= cpu_to_le32(RNDIS_MEDIUM_802_3
);
856 resp
->MaxPacketsPerTransfer
= cpu_to_le32(1);
857 resp
->MaxTransferSize
= cpu_to_le32(ETH_FRAME_LEN
+
858 sizeof(struct rndis_packet_msg_type
) + 22);
859 resp
->PacketAlignmentFactor
= cpu_to_le32(0);
860 resp
->AFListOffset
= cpu_to_le32(0);
861 resp
->AFListSize
= cpu_to_le32(0);
865 static int rndis_query_response(USBNetState
*s
,
866 rndis_query_msg_type
*buf
, unsigned int length
)
868 rndis_query_cmplt_type
*resp
;
869 /* oid_supported_list is the largest data reply */
870 uint8_t infobuf
[sizeof(oid_supported_list
)];
871 uint32_t bufoffs
, buflen
;
873 unsigned int resplen
;
875 bufoffs
= le32_to_cpu(buf
->InformationBufferOffset
) + 8;
876 buflen
= le32_to_cpu(buf
->InformationBufferLength
);
877 if (bufoffs
+ buflen
> length
)
878 return USB_RET_STALL
;
880 infobuflen
= ndis_query(s
, le32_to_cpu(buf
->OID
),
881 bufoffs
+ (uint8_t *) buf
, buflen
, infobuf
,
883 resplen
= sizeof(rndis_query_cmplt_type
) +
884 ((infobuflen
< 0) ? 0 : infobuflen
);
885 resp
= rndis_queue_response(s
, resplen
);
887 return USB_RET_STALL
;
889 resp
->MessageType
= cpu_to_le32(RNDIS_QUERY_CMPLT
);
890 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
891 resp
->MessageLength
= cpu_to_le32(resplen
);
893 if (infobuflen
< 0) {
894 /* OID not supported */
895 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
896 resp
->InformationBufferLength
= cpu_to_le32(0);
897 resp
->InformationBufferOffset
= cpu_to_le32(0);
901 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
902 resp
->InformationBufferOffset
=
903 cpu_to_le32(infobuflen
? sizeof(rndis_query_cmplt_type
) - 8 : 0);
904 resp
->InformationBufferLength
= cpu_to_le32(infobuflen
);
905 memcpy(resp
+ 1, infobuf
, infobuflen
);
910 static int rndis_set_response(USBNetState
*s
,
911 rndis_set_msg_type
*buf
, unsigned int length
)
913 rndis_set_cmplt_type
*resp
=
914 rndis_queue_response(s
, sizeof(rndis_set_cmplt_type
));
915 uint32_t bufoffs
, buflen
;
919 return USB_RET_STALL
;
921 bufoffs
= le32_to_cpu(buf
->InformationBufferOffset
) + 8;
922 buflen
= le32_to_cpu(buf
->InformationBufferLength
);
923 if (bufoffs
+ buflen
> length
)
924 return USB_RET_STALL
;
926 ret
= ndis_set(s
, le32_to_cpu(buf
->OID
),
927 bufoffs
+ (uint8_t *) buf
, buflen
);
928 resp
->MessageType
= cpu_to_le32(RNDIS_SET_CMPLT
);
929 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
930 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_set_cmplt_type
));
932 /* OID not supported */
933 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
936 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
941 static int rndis_reset_response(USBNetState
*s
, rndis_reset_msg_type
*buf
)
943 rndis_reset_cmplt_type
*resp
=
944 rndis_queue_response(s
, sizeof(rndis_reset_cmplt_type
));
947 return USB_RET_STALL
;
949 resp
->MessageType
= cpu_to_le32(RNDIS_RESET_CMPLT
);
950 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_reset_cmplt_type
));
951 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
952 resp
->AddressingReset
= cpu_to_le32(1); /* reset information */
957 static int rndis_keepalive_response(USBNetState
*s
,
958 rndis_keepalive_msg_type
*buf
)
960 rndis_keepalive_cmplt_type
*resp
=
961 rndis_queue_response(s
, sizeof(rndis_keepalive_cmplt_type
));
964 return USB_RET_STALL
;
966 resp
->MessageType
= cpu_to_le32(RNDIS_KEEPALIVE_CMPLT
);
967 resp
->MessageLength
= cpu_to_le32(sizeof(rndis_keepalive_cmplt_type
));
968 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
969 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
974 static int rndis_parse(USBNetState
*s
, uint8_t *data
, int length
)
977 le32
*tmp
= (le32
*) data
;
979 msg_type
= le32_to_cpup(tmp
);
982 case RNDIS_INITIALIZE_MSG
:
983 s
->rndis_state
= RNDIS_INITIALIZED
;
984 return rndis_init_response(s
, (rndis_init_msg_type
*) data
);
987 s
->rndis_state
= RNDIS_UNINITIALIZED
;
990 case RNDIS_QUERY_MSG
:
991 return rndis_query_response(s
, (rndis_query_msg_type
*) data
, length
);
994 return rndis_set_response(s
, (rndis_set_msg_type
*) data
, length
);
996 case RNDIS_RESET_MSG
:
997 rndis_clear_responsequeue(s
);
998 s
->out_ptr
= s
->in_ptr
= s
->in_len
= 0;
999 return rndis_reset_response(s
, (rndis_reset_msg_type
*) data
);
1001 case RNDIS_KEEPALIVE_MSG
:
1002 /* For USB: host does this every 5 seconds */
1003 return rndis_keepalive_response(s
, (rndis_keepalive_msg_type
*) data
);
1006 return USB_RET_STALL
;
1009 static void usb_net_handle_reset(USBDevice
*dev
)
1013 static const char * const usb_net_stringtable
[] = {
1014 [STRING_MANUFACTURER
] = "QEMU",
1015 [STRING_PRODUCT
] = "RNDIS/QEMU USB Network Device",
1016 [STRING_ETHADDR
] = "400102030405",
1017 [STRING_DATA
] = "QEMU USB Net Data Interface",
1018 [STRING_CONTROL
] = "QEMU USB Net Control Interface",
1019 [STRING_RNDIS_CONTROL
] = "QEMU USB Net RNDIS Control Interface",
1020 [STRING_CDC
] = "QEMU USB Net CDC",
1021 [STRING_SUBSET
] = "QEMU USB Net Subset",
1022 [STRING_RNDIS
] = "QEMU USB Net RNDIS",
1023 [STRING_SERIALNUMBER
] = "1",
1026 static int usb_net_handle_control(USBDevice
*dev
, int request
, int value
,
1027 int index
, int length
, uint8_t *data
)
1029 USBNetState
*s
= (USBNetState
*) dev
;
1033 case DeviceRequest
| USB_REQ_GET_STATUS
:
1034 data
[0] = (1 << USB_DEVICE_SELF_POWERED
) |
1035 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
1040 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
1041 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
1042 dev
->remote_wakeup
= 0;
1049 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
1050 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
1051 dev
->remote_wakeup
= 1;
1058 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1063 case ClassInterfaceOutRequest
| USB_CDC_SEND_ENCAPSULATED_COMMAND
:
1064 if (!s
->rndis
|| value
|| index
!= 0)
1066 #ifdef TRAFFIC_DEBUG
1069 fprintf(stderr
, "SEND_ENCAPSULATED_COMMAND:");
1070 for (i
= 0; i
< length
; i
++) {
1072 fprintf(stderr
, "\n%04x:", i
);
1073 fprintf(stderr
, " %02x", data
[i
]);
1075 fprintf(stderr
, "\n\n");
1078 ret
= rndis_parse(s
, data
, length
);
1081 case ClassInterfaceRequest
| USB_CDC_GET_ENCAPSULATED_RESPONSE
:
1082 if (!s
->rndis
|| value
|| index
!= 0)
1084 ret
= rndis_get_response(s
, data
);
1089 #ifdef TRAFFIC_DEBUG
1092 fprintf(stderr
, "GET_ENCAPSULATED_RESPONSE:");
1093 for (i
= 0; i
< ret
; i
++) {
1095 fprintf(stderr
, "\n%04x:", i
);
1096 fprintf(stderr
, " %02x", data
[i
]);
1098 fprintf(stderr
, "\n\n");
1103 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
1104 switch(value
>> 8) {
1106 ret
= sizeof(qemu_net_dev_descriptor
);
1107 memcpy(data
, qemu_net_dev_descriptor
, ret
);
1111 switch (value
& 0xff) {
1113 ret
= sizeof(qemu_net_rndis_config_descriptor
);
1114 memcpy(data
, qemu_net_rndis_config_descriptor
, ret
);
1118 ret
= sizeof(qemu_net_cdc_config_descriptor
);
1119 memcpy(data
, qemu_net_cdc_config_descriptor
, ret
);
1126 data
[2] = ret
& 0xff;
1131 switch (value
& 0xff) {
1141 case STRING_ETHADDR
:
1142 ret
= set_usb_string(data
, s
->usbstring_mac
);
1146 if (ARRAY_SIZE(usb_net_stringtable
) > (value
& 0xff)) {
1147 ret
= set_usb_string(data
,
1148 usb_net_stringtable
[value
& 0xff]);
1161 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
1162 data
[0] = s
->rndis
? DEV_RNDIS_CONFIG_VALUE
: DEV_CONFIG_VALUE
;
1166 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1167 switch (value
& 0xff) {
1168 case DEV_CONFIG_VALUE
:
1172 case DEV_RNDIS_CONFIG_VALUE
:
1182 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
1183 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
1188 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
1189 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1195 fprintf(stderr
, "usbnet: failed control transaction: "
1196 "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1197 request
, value
, index
, length
);
1198 ret
= USB_RET_STALL
;
1204 static int usb_net_handle_statusin(USBNetState
*s
, USBPacket
*p
)
1209 return USB_RET_STALL
;
1211 ((le32
*) p
->data
)[0] = cpu_to_le32(1);
1212 ((le32
*) p
->data
)[1] = cpu_to_le32(0);
1213 if (!s
->rndis_resp
.tqh_first
)
1216 #ifdef TRAFFIC_DEBUG
1217 fprintf(stderr
, "usbnet: interrupt poll len %u return %d", p
->len
, ret
);
1220 fprintf(stderr
, ":");
1221 for (i
= 0; i
< ret
; i
++) {
1223 fprintf(stderr
, "\n%04x:", i
);
1224 fprintf(stderr
, " %02x", p
->data
[i
]);
1226 fprintf(stderr
, "\n\n");
1233 static int usb_net_handle_datain(USBNetState
*s
, USBPacket
*p
)
1235 int ret
= USB_RET_NAK
;
1237 if (s
->in_ptr
> s
->in_len
) {
1238 s
->in_ptr
= s
->in_len
= 0;
1246 ret
= s
->in_len
- s
->in_ptr
;
1249 memcpy(p
->data
, &s
->in_buf
[s
->in_ptr
], ret
);
1251 if (s
->in_ptr
>= s
->in_len
&&
1252 (s
->rndis
|| (s
->in_len
& (64 - 1)) || !ret
)) {
1253 /* no short packet necessary */
1254 s
->in_ptr
= s
->in_len
= 0;
1257 #ifdef TRAFFIC_DEBUG
1258 fprintf(stderr
, "usbnet: data in len %u return %d", p
->len
, ret
);
1261 fprintf(stderr
, ":");
1262 for (i
= 0; i
< ret
; i
++) {
1264 fprintf(stderr
, "\n%04x:", i
);
1265 fprintf(stderr
, " %02x", p
->data
[i
]);
1267 fprintf(stderr
, "\n\n");
1274 static int usb_net_handle_dataout(USBNetState
*s
, USBPacket
*p
)
1277 int sz
= sizeof(s
->out_buf
) - s
->out_ptr
;
1278 struct rndis_packet_msg_type
*msg
=
1279 (struct rndis_packet_msg_type
*) s
->out_buf
;
1282 #ifdef TRAFFIC_DEBUG
1283 fprintf(stderr
, "usbnet: data out len %u\n", p
->len
);
1286 fprintf(stderr
, ":");
1287 for (i
= 0; i
< p
->len
; i
++) {
1289 fprintf(stderr
, "\n%04x:", i
);
1290 fprintf(stderr
, " %02x", p
->data
[i
]);
1292 fprintf(stderr
, "\n\n");
1298 memcpy(&s
->out_buf
[s
->out_ptr
], p
->data
, sz
);
1303 qemu_send_packet(&s
->nic
->nc
, s
->out_buf
, s
->out_ptr
);
1308 len
= le32_to_cpu(msg
->MessageLength
);
1309 if (s
->out_ptr
< 8 || s
->out_ptr
< len
)
1311 if (le32_to_cpu(msg
->MessageType
) == RNDIS_PACKET_MSG
) {
1312 uint32_t offs
= 8 + le32_to_cpu(msg
->DataOffset
);
1313 uint32_t size
= le32_to_cpu(msg
->DataLength
);
1314 if (offs
+ size
<= len
)
1315 qemu_send_packet(&s
->nic
->nc
, s
->out_buf
+ offs
, size
);
1318 memmove(s
->out_buf
, &s
->out_buf
[len
], s
->out_ptr
);
1323 static int usb_net_handle_data(USBDevice
*dev
, USBPacket
*p
)
1325 USBNetState
*s
= (USBNetState
*) dev
;
1332 ret
= usb_net_handle_statusin(s
, p
);
1336 ret
= usb_net_handle_datain(s
, p
);
1347 ret
= usb_net_handle_dataout(s
, p
);
1357 ret
= USB_RET_STALL
;
1360 if (ret
== USB_RET_STALL
)
1361 fprintf(stderr
, "usbnet: failed data transaction: "
1362 "pid 0x%x ep 0x%x len 0x%x\n",
1363 p
->pid
, p
->devep
, p
->len
);
1367 static ssize_t
usbnet_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
1369 USBNetState
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
1370 struct rndis_packet_msg_type
*msg
;
1373 msg
= (struct rndis_packet_msg_type
*) s
->in_buf
;
1374 if (!s
->rndis_state
== RNDIS_DATA_INITIALIZED
)
1376 if (size
+ sizeof(struct rndis_packet_msg_type
) > sizeof(s
->in_buf
))
1379 memset(msg
, 0, sizeof(struct rndis_packet_msg_type
));
1380 msg
->MessageType
= cpu_to_le32(RNDIS_PACKET_MSG
);
1381 msg
->MessageLength
= cpu_to_le32(size
+ sizeof(struct rndis_packet_msg_type
));
1382 msg
->DataOffset
= cpu_to_le32(sizeof(struct rndis_packet_msg_type
) - 8);
1383 msg
->DataLength
= cpu_to_le32(size
);
1384 /* msg->OOBDataOffset;
1385 * msg->OOBDataLength;
1386 * msg->NumOOBDataElements;
1387 * msg->PerPacketInfoOffset;
1388 * msg->PerPacketInfoLength;
1392 memcpy(msg
+ 1, buf
, size
);
1393 s
->in_len
= size
+ sizeof(struct rndis_packet_msg_type
);
1395 if (size
> sizeof(s
->in_buf
))
1397 memcpy(s
->in_buf
, buf
, size
);
1404 static int usbnet_can_receive(VLANClientState
*nc
)
1406 USBNetState
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
1408 if (s
->rndis
&& !s
->rndis_state
== RNDIS_DATA_INITIALIZED
)
1414 static void usbnet_cleanup(VLANClientState
*nc
)
1416 USBNetState
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
1421 static void usb_net_handle_destroy(USBDevice
*dev
)
1423 USBNetState
*s
= (USBNetState
*) dev
;
1425 /* TODO: remove the nd_table[] entry */
1426 rndis_clear_responsequeue(s
);
1427 qemu_del_vlan_client(&s
->nic
->nc
);
1430 static NetClientInfo net_usbnet_info
= {
1431 .type
= NET_CLIENT_TYPE_NIC
,
1432 .size
= sizeof(NICState
),
1433 .can_receive
= usbnet_can_receive
,
1434 .receive
= usbnet_receive
,
1435 .cleanup
= usbnet_cleanup
,
1438 static int usb_net_initfn(USBDevice
*dev
)
1440 USBNetState
*s
= DO_UPCAST(USBNetState
, dev
, dev
);
1442 s
->dev
.speed
= USB_SPEED_FULL
;
1445 s
->rndis_state
= RNDIS_UNINITIALIZED
;
1446 QTAILQ_INIT(&s
->rndis_resp
);
1448 s
->medium
= 0; /* NDIS_MEDIUM_802_3 */
1449 s
->speed
= 1000000; /* 100MBps, in 100Bps units */
1450 s
->media_state
= 0; /* NDIS_MEDIA_STATE_CONNECTED */;
1452 s
->vendorid
= 0x1234;
1454 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
1455 s
->nic
= qemu_new_nic(&net_usbnet_info
, &s
->conf
,
1456 s
->dev
.qdev
.info
->name
, s
->dev
.qdev
.id
, s
);
1457 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
1458 snprintf(s
->usbstring_mac
, sizeof(s
->usbstring_mac
),
1459 "%02x%02x%02x%02x%02x%02x",
1461 s
->conf
.macaddr
.a
[1],
1462 s
->conf
.macaddr
.a
[2],
1463 s
->conf
.macaddr
.a
[3],
1464 s
->conf
.macaddr
.a
[4],
1465 s
->conf
.macaddr
.a
[5]);
1467 add_boot_device_path(s
->conf
.bootindex
, &dev
->qdev
, "/ethernet@0");
1471 static USBDevice
*usb_net_init(const char *cmdline
)
1477 opts
= qemu_opts_parse(qemu_find_opts("net"), cmdline
, 0);
1481 qemu_opt_set(opts
, "type", "nic");
1482 qemu_opt_set(opts
, "model", "usb");
1484 idx
= net_client_init(NULL
, opts
, 0);
1489 dev
= usb_create(NULL
/* FIXME */, "usb-net");
1493 qdev_set_nic_properties(&dev
->qdev
, &nd_table
[idx
]);
1494 qdev_init_nofail(&dev
->qdev
);
1498 static struct USBDeviceInfo net_info
= {
1499 .product_desc
= "QEMU USB Network Interface",
1500 .qdev
.name
= "usb-net",
1501 .qdev
.fw_name
= "network",
1502 .qdev
.size
= sizeof(USBNetState
),
1503 .init
= usb_net_initfn
,
1504 .handle_packet
= usb_generic_handle_packet
,
1505 .handle_reset
= usb_net_handle_reset
,
1506 .handle_control
= usb_net_handle_control
,
1507 .handle_data
= usb_net_handle_data
,
1508 .handle_destroy
= usb_net_handle_destroy
,
1509 .usbdevice_name
= "net",
1510 .usbdevice_init
= usb_net_init
,
1511 .qdev
.props
= (Property
[]) {
1512 DEFINE_NIC_PROPERTIES(USBNetState
, conf
),
1513 DEFINE_PROP_END_OF_LIST(),
1517 static void usb_net_register_devices(void)
1519 usb_qdev_register(&net_info
);
1521 device_init(usb_net_register_devices
)