fix spelling of $pkg_config, move default together with other cross tools
[qemu.git] / hw / usb-net.c
blob84924550fd0c66519949f12c6ebffe214d2638e0
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 "usb.h"
28 #include "net.h"
29 #include "qemu-queue.h"
30 #include "sysemu.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 */
44 enum usbstring_idx {
45 STRING_MANUFACTURER = 1,
46 STRING_PRODUCT,
47 STRING_ETHADDR,
48 STRING_DATA,
49 STRING_CONTROL,
50 STRING_RNDIS_CONTROL,
51 STRING_CDC,
52 STRING_SUBSET,
53 STRING_RNDIS,
54 STRING_SERIALNUMBER,
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 */
143 /* ACM Descriptor */
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 */
178 /* Sink Endpoint */
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 */
261 /* Sink Endpoint */
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
280 /* Status Values */
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 */
289 enum {
290 RNDIS_PACKET_MSG = 1,
291 RNDIS_INITIALIZE_MSG = 2, /* Initialize device */
292 RNDIS_HALT_MSG = 3,
293 RNDIS_QUERY_MSG = 4,
294 RNDIS_SET_MSG = 5,
295 RNDIS_RESET_MSG = 6,
296 RNDIS_INDICATE_STATUS_MSG = 7,
297 RNDIS_KEEPALIVE_MSG = 8,
300 /* Message completion */
301 enum {
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,
309 /* Device Flags */
310 enum {
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 {
328 le32 MessageType;
329 le32 MessageLength;
330 le32 RequestID;
331 le32 MajorVersion;
332 le32 MinorVersion;
333 le32 MaxTransferSize;
334 } rndis_init_msg_type;
336 typedef struct rndis_init_cmplt_type {
337 le32 MessageType;
338 le32 MessageLength;
339 le32 RequestID;
340 le32 Status;
341 le32 MajorVersion;
342 le32 MinorVersion;
343 le32 DeviceFlags;
344 le32 Medium;
345 le32 MaxPacketsPerTransfer;
346 le32 MaxTransferSize;
347 le32 PacketAlignmentFactor;
348 le32 AFListOffset;
349 le32 AFListSize;
350 } rndis_init_cmplt_type;
352 typedef struct rndis_halt_msg_type {
353 le32 MessageType;
354 le32 MessageLength;
355 le32 RequestID;
356 } rndis_halt_msg_type;
358 typedef struct rndis_query_msg_type {
359 le32 MessageType;
360 le32 MessageLength;
361 le32 RequestID;
362 le32 OID;
363 le32 InformationBufferLength;
364 le32 InformationBufferOffset;
365 le32 DeviceVcHandle;
366 } rndis_query_msg_type;
368 typedef struct rndis_query_cmplt_type {
369 le32 MessageType;
370 le32 MessageLength;
371 le32 RequestID;
372 le32 Status;
373 le32 InformationBufferLength;
374 le32 InformationBufferOffset;
375 } rndis_query_cmplt_type;
377 typedef struct rndis_set_msg_type {
378 le32 MessageType;
379 le32 MessageLength;
380 le32 RequestID;
381 le32 OID;
382 le32 InformationBufferLength;
383 le32 InformationBufferOffset;
384 le32 DeviceVcHandle;
385 } rndis_set_msg_type;
387 typedef struct rndis_set_cmplt_type {
388 le32 MessageType;
389 le32 MessageLength;
390 le32 RequestID;
391 le32 Status;
392 } rndis_set_cmplt_type;
394 typedef struct rndis_reset_msg_type {
395 le32 MessageType;
396 le32 MessageLength;
397 le32 Reserved;
398 } rndis_reset_msg_type;
400 typedef struct rndis_reset_cmplt_type {
401 le32 MessageType;
402 le32 MessageLength;
403 le32 Status;
404 le32 AddressingReset;
405 } rndis_reset_cmplt_type;
407 typedef struct rndis_indicate_status_msg_type {
408 le32 MessageType;
409 le32 MessageLength;
410 le32 Status;
411 le32 StatusBufferLength;
412 le32 StatusBufferOffset;
413 } rndis_indicate_status_msg_type;
415 typedef struct rndis_keepalive_msg_type {
416 le32 MessageType;
417 le32 MessageLength;
418 le32 RequestID;
419 } rndis_keepalive_msg_type;
421 typedef struct rndis_keepalive_cmplt_type {
422 le32 MessageType;
423 le32 MessageLength;
424 le32 RequestID;
425 le32 Status;
426 } rndis_keepalive_cmplt_type;
428 struct rndis_packet_msg_type {
429 le32 MessageType;
430 le32 MessageLength;
431 le32 DataOffset;
432 le32 DataLength;
433 le32 OOBDataOffset;
434 le32 OOBDataLength;
435 le32 NumOOBDataElements;
436 le32 PerPacketInfoOffset;
437 le32 PerPacketInfoLength;
438 le32 VcHandle;
439 le32 Reserved;
442 struct rndis_config_parameter {
443 le32 ParameterNameOffset;
444 le32 ParameterNameLength;
445 le32 ParameterType;
446 le32 ParameterValueOffset;
447 le32 ParameterValueLength;
450 /* implementation specific */
451 enum rndis_state
453 RNDIS_UNINITIALIZED,
454 RNDIS_INITIALIZED,
455 RNDIS_DATA_INITIALIZED,
458 /* from ndis.h */
459 enum ndis_oid {
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,
490 /* Optional OIDs */
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,
553 OID_GEN_LINK_SPEED,
554 OID_GEN_TRANSMIT_BLOCK_SIZE,
555 OID_GEN_RECEIVE_BLOCK_SIZE,
556 OID_GEN_VENDOR_ID,
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 */
565 OID_GEN_XMIT_OK,
566 OID_GEN_RCV_OK,
567 OID_GEN_XMIT_ERROR,
568 OID_GEN_RCV_ERROR,
569 OID_GEN_RCV_NO_BUFFER,
571 /* IEEE 802.3 */
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;
595 uint32_t length;
596 uint8_t buf[0];
599 typedef struct USBNetState {
600 USBDevice dev;
602 unsigned int rndis;
603 enum rndis_state rndis_state;
604 uint32_t medium;
605 uint32_t speed;
606 uint32_t media_state;
607 uint16_t filter;
608 uint32_t vendorid;
610 unsigned int out_ptr;
611 uint8_t out_buf[2048];
613 USBPacket *inpkt;
614 unsigned int in_ptr, in_len;
615 uint8_t in_buf[2048];
617 char usbstring_mac[13];
618 NICState *nic;
619 NICConf conf;
620 QTAILQ_HEAD(rndis_resp_head, rndis_response) rndis_resp;
621 } USBNetState;
623 static int ndis_query(USBNetState *s, uint32_t oid,
624 uint8_t *inbuf, unsigned int inlen, uint8_t *outbuf,
625 size_t outlen)
627 unsigned int i;
629 switch (oid) {
630 /* general oids (table 4-1) */
631 /* mandatory */
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);
637 /* mandatory */
638 case OID_GEN_HARDWARE_STATUS:
639 *((le32 *) outbuf) = cpu_to_le32(0);
640 return sizeof(le32);
642 /* mandatory */
643 case OID_GEN_MEDIA_SUPPORTED:
644 *((le32 *) outbuf) = cpu_to_le32(s->medium);
645 return sizeof(le32);
647 /* mandatory */
648 case OID_GEN_MEDIA_IN_USE:
649 *((le32 *) outbuf) = cpu_to_le32(s->medium);
650 return sizeof(le32);
652 /* mandatory */
653 case OID_GEN_MAXIMUM_FRAME_SIZE:
654 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
655 return sizeof(le32);
657 /* mandatory */
658 case OID_GEN_LINK_SPEED:
659 *((le32 *) outbuf) = cpu_to_le32(s->speed);
660 return sizeof(le32);
662 /* mandatory */
663 case OID_GEN_TRANSMIT_BLOCK_SIZE:
664 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
665 return sizeof(le32);
667 /* mandatory */
668 case OID_GEN_RECEIVE_BLOCK_SIZE:
669 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
670 return sizeof(le32);
672 /* mandatory */
673 case OID_GEN_VENDOR_ID:
674 *((le32 *) outbuf) = cpu_to_le32(s->vendorid);
675 return sizeof(le32);
677 /* mandatory */
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);
684 return sizeof(le32);
686 /* mandatory */
687 case OID_GEN_CURRENT_PACKET_FILTER:
688 *((le32 *) outbuf) = cpu_to_le32(s->filter);
689 return sizeof(le32);
691 /* mandatory */
692 case OID_GEN_MAXIMUM_TOTAL_SIZE:
693 *((le32 *) outbuf) = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
694 return sizeof(le32);
696 /* mandatory */
697 case OID_GEN_MEDIA_CONNECT_STATUS:
698 *((le32 *) outbuf) = cpu_to_le32(s->media_state);
699 return sizeof(le32);
701 case OID_GEN_PHYSICAL_MEDIUM:
702 *((le32 *) outbuf) = cpu_to_le32(0);
703 return sizeof(le32);
705 case OID_GEN_MAC_OPTIONS:
706 *((le32 *) outbuf) = cpu_to_le32(
707 NDIS_MAC_OPTION_RECEIVE_SERIALIZED |
708 NDIS_MAC_OPTION_FULL_DUPLEX);
709 return sizeof(le32);
711 /* statistics OIDs (table 4-2) */
712 /* mandatory */
713 case OID_GEN_XMIT_OK:
714 *((le32 *) outbuf) = cpu_to_le32(0);
715 return sizeof(le32);
717 /* mandatory */
718 case OID_GEN_RCV_OK:
719 *((le32 *) outbuf) = cpu_to_le32(0);
720 return sizeof(le32);
722 /* mandatory */
723 case OID_GEN_XMIT_ERROR:
724 *((le32 *) outbuf) = cpu_to_le32(0);
725 return sizeof(le32);
727 /* mandatory */
728 case OID_GEN_RCV_ERROR:
729 *((le32 *) outbuf) = cpu_to_le32(0);
730 return sizeof(le32);
732 /* mandatory */
733 case OID_GEN_RCV_NO_BUFFER:
734 *((le32 *) outbuf) = cpu_to_le32(0);
735 return sizeof(le32);
737 /* ieee802.3 OIDs (table 4-3) */
738 /* mandatory */
739 case OID_802_3_PERMANENT_ADDRESS:
740 memcpy(outbuf, s->conf.macaddr.a, 6);
741 return 6;
743 /* mandatory */
744 case OID_802_3_CURRENT_ADDRESS:
745 memcpy(outbuf, s->conf.macaddr.a, 6);
746 return 6;
748 /* mandatory */
749 case OID_802_3_MULTICAST_LIST:
750 *((le32 *) outbuf) = cpu_to_le32(0xe0000000);
751 return sizeof(le32);
753 /* mandatory */
754 case OID_802_3_MAXIMUM_LIST_SIZE:
755 *((le32 *) outbuf) = cpu_to_le32(1);
756 return sizeof(le32);
758 case OID_802_3_MAC_OPTIONS:
759 return 0;
761 /* ieee802.3 statistics OIDs (table 4-4) */
762 /* mandatory */
763 case OID_802_3_RCV_ERROR_ALIGNMENT:
764 *((le32 *) outbuf) = cpu_to_le32(0);
765 return sizeof(le32);
767 /* mandatory */
768 case OID_802_3_XMIT_ONE_COLLISION:
769 *((le32 *) outbuf) = cpu_to_le32(0);
770 return sizeof(le32);
772 /* mandatory */
773 case OID_802_3_XMIT_MORE_COLLISIONS:
774 *((le32 *) outbuf) = cpu_to_le32(0);
775 return sizeof(le32);
777 default:
778 fprintf(stderr, "usbnet: unknown OID 0x%08x\n", oid);
779 return 0;
781 return -1;
784 static int ndis_set(USBNetState *s, uint32_t oid,
785 uint8_t *inbuf, unsigned int inlen)
787 switch (oid) {
788 case OID_GEN_CURRENT_PACKET_FILTER:
789 s->filter = le32_to_cpup((le32 *) inbuf);
790 if (s->filter) {
791 s->rndis_state = RNDIS_DATA_INITIALIZED;
792 } else {
793 s->rndis_state = RNDIS_INITIALIZED;
795 return 0;
797 case OID_802_3_MULTICAST_LIST:
798 return 0;
800 return -1;
803 static int rndis_get_response(USBNetState *s, uint8_t *buf)
805 int ret = 0;
806 struct rndis_response *r = s->rndis_resp.tqh_first;
808 if (!r)
809 return ret;
811 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
812 ret = r->length;
813 memcpy(buf, r->buf, r->length);
814 qemu_free(r);
816 return ret;
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);
825 r->length = length;
827 return &r->buf[0];
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);
836 qemu_free(r);
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));
845 if (!resp)
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);
862 return 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;
872 int infobuflen;
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,
882 sizeof(infobuf));
883 resplen = sizeof(rndis_query_cmplt_type) +
884 ((infobuflen < 0) ? 0 : infobuflen);
885 resp = rndis_queue_response(s, resplen);
886 if (!resp)
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);
898 return 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);
907 return 0;
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;
916 int ret;
918 if (!resp)
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));
931 if (ret < 0) {
932 /* OID not supported */
933 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
934 return 0;
936 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
938 return 0;
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));
946 if (!resp)
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 */
954 return 0;
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));
963 if (!resp)
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);
971 return 0;
974 static int rndis_parse(USBNetState *s, uint8_t *data, int length)
976 uint32_t msg_type;
977 le32 *tmp = (le32 *) data;
979 msg_type = le32_to_cpup(tmp);
981 switch (msg_type) {
982 case RNDIS_INITIALIZE_MSG:
983 s->rndis_state = RNDIS_INITIALIZED;
984 return rndis_init_response(s, (rndis_init_msg_type *) data);
986 case RNDIS_HALT_MSG:
987 s->rndis_state = RNDIS_UNINITIALIZED;
988 return 0;
990 case RNDIS_QUERY_MSG:
991 return rndis_query_response(s, (rndis_query_msg_type *) data, length);
993 case RNDIS_SET_MSG:
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;
1030 int ret = 0;
1032 switch(request) {
1033 case DeviceRequest | USB_REQ_GET_STATUS:
1034 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
1035 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
1036 data[1] = 0x00;
1037 ret = 2;
1038 break;
1040 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
1041 if (value == USB_DEVICE_REMOTE_WAKEUP) {
1042 dev->remote_wakeup = 0;
1043 } else {
1044 goto fail;
1046 ret = 0;
1047 break;
1049 case DeviceOutRequest | USB_REQ_SET_FEATURE:
1050 if (value == USB_DEVICE_REMOTE_WAKEUP) {
1051 dev->remote_wakeup = 1;
1052 } else {
1053 goto fail;
1055 ret = 0;
1056 break;
1058 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1059 dev->addr = value;
1060 ret = 0;
1061 break;
1063 case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
1064 if (!s->rndis || value || index != 0)
1065 goto fail;
1066 #ifdef TRAFFIC_DEBUG
1068 unsigned int i;
1069 fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");
1070 for (i = 0; i < length; i++) {
1071 if (!(i & 15))
1072 fprintf(stderr, "\n%04x:", i);
1073 fprintf(stderr, " %02x", data[i]);
1075 fprintf(stderr, "\n\n");
1077 #endif
1078 ret = rndis_parse(s, data, length);
1079 break;
1081 case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
1082 if (!s->rndis || value || index != 0)
1083 goto fail;
1084 ret = rndis_get_response(s, data);
1085 if (!ret) {
1086 data[0] = 0;
1087 ret = 1;
1089 #ifdef TRAFFIC_DEBUG
1091 unsigned int i;
1092 fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");
1093 for (i = 0; i < ret; i++) {
1094 if (!(i & 15))
1095 fprintf(stderr, "\n%04x:", i);
1096 fprintf(stderr, " %02x", data[i]);
1098 fprintf(stderr, "\n\n");
1100 #endif
1101 break;
1103 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1104 switch(value >> 8) {
1105 case USB_DT_DEVICE:
1106 ret = sizeof(qemu_net_dev_descriptor);
1107 memcpy(data, qemu_net_dev_descriptor, ret);
1108 break;
1110 case USB_DT_CONFIG:
1111 switch (value & 0xff) {
1112 case 0:
1113 ret = sizeof(qemu_net_rndis_config_descriptor);
1114 memcpy(data, qemu_net_rndis_config_descriptor, ret);
1115 break;
1117 case 1:
1118 ret = sizeof(qemu_net_cdc_config_descriptor);
1119 memcpy(data, qemu_net_cdc_config_descriptor, ret);
1120 break;
1122 default:
1123 goto fail;
1126 data[2] = ret & 0xff;
1127 data[3] = ret >> 8;
1128 break;
1130 case USB_DT_STRING:
1131 switch (value & 0xff) {
1132 case 0:
1133 /* language ids */
1134 data[0] = 4;
1135 data[1] = 3;
1136 data[2] = 0x09;
1137 data[3] = 0x04;
1138 ret = 4;
1139 break;
1141 case STRING_ETHADDR:
1142 ret = set_usb_string(data, s->usbstring_mac);
1143 break;
1145 default:
1146 if (ARRAY_SIZE(usb_net_stringtable) > (value & 0xff)) {
1147 ret = set_usb_string(data,
1148 usb_net_stringtable[value & 0xff]);
1149 break;
1152 goto fail;
1154 break;
1156 default:
1157 goto fail;
1159 break;
1161 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
1162 data[0] = s->rndis ? DEV_RNDIS_CONFIG_VALUE : DEV_CONFIG_VALUE;
1163 ret = 1;
1164 break;
1166 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1167 switch (value & 0xff) {
1168 case DEV_CONFIG_VALUE:
1169 s->rndis = 0;
1170 break;
1172 case DEV_RNDIS_CONFIG_VALUE:
1173 s->rndis = 1;
1174 break;
1176 default:
1177 goto fail;
1179 ret = 0;
1180 break;
1182 case DeviceRequest | USB_REQ_GET_INTERFACE:
1183 case InterfaceRequest | USB_REQ_GET_INTERFACE:
1184 data[0] = 0;
1185 ret = 1;
1186 break;
1188 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
1189 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1190 ret = 0;
1191 break;
1193 default:
1194 fail:
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;
1199 break;
1201 return ret;
1204 static int usb_net_handle_statusin(USBNetState *s, USBPacket *p)
1206 int ret = 8;
1208 if (p->len < 8)
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)
1214 ret = USB_RET_NAK;
1216 #ifdef TRAFFIC_DEBUG
1217 fprintf(stderr, "usbnet: interrupt poll len %u return %d", p->len, ret);
1219 int i;
1220 fprintf(stderr, ":");
1221 for (i = 0; i < ret; i++) {
1222 if (!(i & 15))
1223 fprintf(stderr, "\n%04x:", i);
1224 fprintf(stderr, " %02x", p->data[i]);
1226 fprintf(stderr, "\n\n");
1228 #endif
1230 return ret;
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;
1239 ret = USB_RET_NAK;
1240 return ret;
1242 if (!s->in_len) {
1243 ret = USB_RET_NAK;
1244 return ret;
1246 ret = s->in_len - s->in_ptr;
1247 if (ret > p->len)
1248 ret = p->len;
1249 memcpy(p->data, &s->in_buf[s->in_ptr], ret);
1250 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);
1260 int i;
1261 fprintf(stderr, ":");
1262 for (i = 0; i < ret; i++) {
1263 if (!(i & 15))
1264 fprintf(stderr, "\n%04x:", i);
1265 fprintf(stderr, " %02x", p->data[i]);
1267 fprintf(stderr, "\n\n");
1269 #endif
1271 return ret;
1274 static int usb_net_handle_dataout(USBNetState *s, USBPacket *p)
1276 int ret = p->len;
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;
1280 uint32_t len;
1282 #ifdef TRAFFIC_DEBUG
1283 fprintf(stderr, "usbnet: data out len %u\n", p->len);
1285 int i;
1286 fprintf(stderr, ":");
1287 for (i = 0; i < p->len; i++) {
1288 if (!(i & 15))
1289 fprintf(stderr, "\n%04x:", i);
1290 fprintf(stderr, " %02x", p->data[i]);
1292 fprintf(stderr, "\n\n");
1294 #endif
1296 if (sz > ret)
1297 sz = ret;
1298 memcpy(&s->out_buf[s->out_ptr], p->data, sz);
1299 s->out_ptr += sz;
1301 if (!s->rndis) {
1302 if (ret < 64) {
1303 qemu_send_packet(&s->nic->nc, s->out_buf, s->out_ptr);
1304 s->out_ptr = 0;
1306 return ret;
1308 len = le32_to_cpu(msg->MessageLength);
1309 if (s->out_ptr < 8 || s->out_ptr < len)
1310 return ret;
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);
1317 s->out_ptr -= len;
1318 memmove(s->out_buf, &s->out_buf[len], s->out_ptr);
1320 return ret;
1323 static int usb_net_handle_data(USBDevice *dev, USBPacket *p)
1325 USBNetState *s = (USBNetState *) dev;
1326 int ret = 0;
1328 switch(p->pid) {
1329 case USB_TOKEN_IN:
1330 switch (p->devep) {
1331 case 1:
1332 ret = usb_net_handle_statusin(s, p);
1333 break;
1335 case 2:
1336 ret = usb_net_handle_datain(s, p);
1337 break;
1339 default:
1340 goto fail;
1342 break;
1344 case USB_TOKEN_OUT:
1345 switch (p->devep) {
1346 case 2:
1347 ret = usb_net_handle_dataout(s, p);
1348 break;
1350 default:
1351 goto fail;
1353 break;
1355 default:
1356 fail:
1357 ret = USB_RET_STALL;
1358 break;
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);
1364 return ret;
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;
1372 if (s->rndis) {
1373 msg = (struct rndis_packet_msg_type *) s->in_buf;
1374 if (!s->rndis_state == RNDIS_DATA_INITIALIZED)
1375 return -1;
1376 if (size + sizeof(struct rndis_packet_msg_type) > sizeof(s->in_buf))
1377 return -1;
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;
1389 * msg->VcHandle;
1390 * msg->Reserved;
1392 memcpy(msg + 1, buf, size);
1393 s->in_len = size + sizeof(struct rndis_packet_msg_type);
1394 } else {
1395 if (size > sizeof(s->in_buf))
1396 return -1;
1397 memcpy(s->in_buf, buf, size);
1398 s->in_len = size;
1400 s->in_ptr = 0;
1401 return 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)
1409 return 1;
1411 return !s->in_len;
1414 static void usbnet_cleanup(VLANClientState *nc)
1416 USBNetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
1418 s->nic = NULL;
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;
1444 s->rndis = 1;
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 */;
1451 s->filter = 0;
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",
1460 0x40,
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");
1468 return 0;
1471 static USBDevice *usb_net_init(const char *cmdline)
1473 USBDevice *dev;
1474 QemuOpts *opts;
1475 int idx;
1477 opts = qemu_opts_parse(qemu_find_opts("net"), cmdline, 0);
1478 if (!opts) {
1479 return NULL;
1481 qemu_opt_set(opts, "type", "nic");
1482 qemu_opt_set(opts, "model", "usb");
1484 idx = net_client_init(NULL, opts, 0);
1485 if (idx == -1) {
1486 return NULL;
1489 dev = usb_create(NULL /* FIXME */, "usb-net");
1490 if (!dev) {
1491 return NULL;
1493 qdev_set_nic_properties(&dev->qdev, &nd_table[idx]);
1494 qdev_init_nofail(&dev->qdev);
1495 return dev;
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)