]> git.proxmox.com Git - qemu.git/blame - hw/usb-net.c
usb: move USB_REQ_{GET,SET}_CONFIGURATION handling to common code
[qemu.git] / hw / usb-net.c
CommitLineData
6c9f886c
AZ
1/*
2 * QEMU USB Net devices
3 *
4 * Copyright (c) 2006 Thomas Sailer
5 * Copyright (c) 2008 Andrzej Zaborowski
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "qemu-common.h"
27#include "usb.h"
30c7d32a 28#include "usb-desc.h"
6c9f886c 29#include "net.h"
72cf2d4f 30#include "qemu-queue.h"
1ca4d09a 31#include "sysemu.h"
6c9f886c
AZ
32
33/*#define TRAFFIC_DEBUG*/
34/* Thanks to NetChip Technologies for donating this product ID.
35 * It's for devices with only CDC Ethernet configurations.
36 */
37#define CDC_VENDOR_NUM 0x0525 /* NetChip */
38#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
39/* For hardware that can talk RNDIS and either of the above protocols,
40 * use this ID ... the windows INF files will know it.
41 */
42#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
43#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
44
45enum usbstring_idx {
46 STRING_MANUFACTURER = 1,
47 STRING_PRODUCT,
48 STRING_ETHADDR,
49 STRING_DATA,
50 STRING_CONTROL,
51 STRING_RNDIS_CONTROL,
52 STRING_CDC,
53 STRING_SUBSET,
54 STRING_RNDIS,
55 STRING_SERIALNUMBER,
56};
57
58#define DEV_CONFIG_VALUE 1 /* CDC or a subset */
59#define DEV_RNDIS_CONFIG_VALUE 2 /* RNDIS; optional */
60
61#define USB_CDC_SUBCLASS_ACM 0x02
62#define USB_CDC_SUBCLASS_ETHERNET 0x06
63
64#define USB_CDC_PROTO_NONE 0
65#define USB_CDC_ACM_PROTO_VENDOR 0xff
66
67#define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
68#define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
69#define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
70#define USB_CDC_UNION_TYPE 0x06 /* union_desc */
71#define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
72
73#define USB_DT_CS_INTERFACE 0x24
74#define USB_DT_CS_ENDPOINT 0x25
75
6c9f886c
AZ
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
87
88#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
89#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
90
91#define ETH_FRAME_LEN 1514 /* Max. octets in frame sans FCS */
92
30c7d32a
GH
93static 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",
6c9f886c
AZ
104};
105
30c7d32a
GH
106static const USBDescIface desc_iface_rndis[] = {
107 {
108 /* RNDIS Control Interface */
109 .bInterfaceNumber = 0,
110 .bNumEndpoints = 1,
111 .bInterfaceClass = USB_CLASS_COMM,
112 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
113 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
114 .iInterface = STRING_RNDIS_CONTROL,
115 .ndesc = 4,
116 .descs = (USBDescOther[]) {
117 {
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 */
124 },
125 },{
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 */
133 },
134 },{
135 /* ACM Descriptor */
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 */
141 },
142 },{
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 */
150 },
151 },
152 },
153 .eps = (USBDescEndpoint[]) {
154 {
155 .bEndpointAddress = USB_DIR_IN | 0x01,
156 .bmAttributes = USB_ENDPOINT_XFER_INT,
157 .wMaxPacketSize = STATUS_BYTECOUNT,
158 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
159 },
160 }
161 },{
162 /* RNDIS Data Interface */
163 .bInterfaceNumber = 1,
164 .bNumEndpoints = 2,
165 .bInterfaceClass = USB_CLASS_CDC_DATA,
166 .iInterface = STRING_DATA,
167 .eps = (USBDescEndpoint[]) {
168 {
169 .bEndpointAddress = USB_DIR_IN | 0x02,
170 .bmAttributes = USB_ENDPOINT_XFER_BULK,
171 .wMaxPacketSize = 0x40,
172 },{
173 .bEndpointAddress = USB_DIR_OUT | 0x02,
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 .wMaxPacketSize = 0x40,
176 }
177 }
178 }
6c9f886c
AZ
179};
180
30c7d32a
GH
181static const USBDescIface desc_iface_cdc[] = {
182 {
183 /* CDC Control Interface */
184 .bInterfaceNumber = 0,
185 .bNumEndpoints = 1,
186 .bInterfaceClass = USB_CLASS_COMM,
187 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
188 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
189 .iInterface = STRING_CONTROL,
190 .ndesc = 3,
191 .descs = (USBDescOther[]) {
192 {
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 */
199 },
200 },{
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 */
208 },
209 },{
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 */
221 },
222 },
223 },
224 .eps = (USBDescEndpoint[]) {
225 {
226 .bEndpointAddress = USB_DIR_IN | 0x01,
227 .bmAttributes = USB_ENDPOINT_XFER_INT,
228 .wMaxPacketSize = STATUS_BYTECOUNT,
229 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
230 },
231 }
232 },{
233 /* CDC Data Interface (off) */
234 .bInterfaceNumber = 1,
235 .bAlternateSetting = 0,
236 .bNumEndpoints = 0,
237 .bInterfaceClass = USB_CLASS_CDC_DATA,
238 },{
239 /* CDC Data Interface */
240 .bInterfaceNumber = 1,
241 .bAlternateSetting = 1,
242 .bNumEndpoints = 2,
243 .bInterfaceClass = USB_CLASS_CDC_DATA,
244 .iInterface = STRING_DATA,
245 .eps = (USBDescEndpoint[]) {
246 {
247 .bEndpointAddress = USB_DIR_IN | 0x02,
248 .bmAttributes = USB_ENDPOINT_XFER_BULK,
249 .wMaxPacketSize = 0x40,
250 },{
251 .bEndpointAddress = USB_DIR_OUT | 0x02,
252 .bmAttributes = USB_ENDPOINT_XFER_BULK,
253 .wMaxPacketSize = 0x40,
254 }
255 }
256 }
257};
258
259static const USBDescDevice desc_device_net = {
260 .bcdUSB = 0x0200,
261 .bDeviceClass = USB_CLASS_COMM,
262 .bMaxPacketSize0 = 0x40,
263 .bNumConfigurations = 2,
264 .confs = (USBDescConfig[]) {
265 {
266 .bNumInterfaces = 2,
267 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
268 .iConfiguration = STRING_RNDIS,
269 .bmAttributes = 0xc0,
270 .bMaxPower = 0x32,
271 .nif = ARRAY_SIZE(desc_iface_rndis),
272 .ifs = desc_iface_rndis,
273 },{
274 .bNumInterfaces = 2,
275 .bConfigurationValue = DEV_CONFIG_VALUE,
276 .iConfiguration = STRING_CDC,
277 .bmAttributes = 0xc0,
278 .bMaxPower = 0x32,
279 .nif = ARRAY_SIZE(desc_iface_cdc),
280 .ifs = desc_iface_cdc,
281 }
282 },
283};
284
285static const USBDesc desc_net = {
286 .id = {
287 .idVendor = RNDIS_VENDOR_NUM,
288 .idProduct = RNDIS_PRODUCT_NUM,
289 .bcdDevice = 0,
290 .iManufacturer = STRING_MANUFACTURER,
291 .iProduct = STRING_PRODUCT,
292 .iSerialNumber = STRING_SERIALNUMBER,
293 },
294 .full = &desc_device_net,
295 .str = usb_net_stringtable,
6c9f886c
AZ
296};
297
298/*
299 * RNDIS Definitions - in theory not specific to USB.
300 */
301#define RNDIS_MAXIMUM_FRAME_SIZE 1518
302#define RNDIS_MAX_TOTAL_SIZE 1558
303
304/* Remote NDIS Versions */
305#define RNDIS_MAJOR_VERSION 1
306#define RNDIS_MINOR_VERSION 0
307
308/* Status Values */
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 */
315
316/* Message Set for Connectionless (802.3) Devices */
317enum {
318 RNDIS_PACKET_MSG = 1,
319 RNDIS_INITIALIZE_MSG = 2, /* Initialize device */
320 RNDIS_HALT_MSG = 3,
321 RNDIS_QUERY_MSG = 4,
322 RNDIS_SET_MSG = 5,
323 RNDIS_RESET_MSG = 6,
324 RNDIS_INDICATE_STATUS_MSG = 7,
325 RNDIS_KEEPALIVE_MSG = 8,
326};
327
328/* Message completion */
329enum {
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,
335};
336
337/* Device Flags */
338enum {
339 RNDIS_DF_CONNECTIONLESS = 1,
340 RNDIS_DF_CONNECTIONORIENTED = 2,
341};
342
343#define RNDIS_MEDIUM_802_3 0x00000000U
344
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
352
353typedef uint32_t le32;
354
355typedef struct rndis_init_msg_type {
356 le32 MessageType;
357 le32 MessageLength;
358 le32 RequestID;
359 le32 MajorVersion;
360 le32 MinorVersion;
361 le32 MaxTransferSize;
362} rndis_init_msg_type;
363
364typedef struct rndis_init_cmplt_type {
365 le32 MessageType;
366 le32 MessageLength;
367 le32 RequestID;
368 le32 Status;
369 le32 MajorVersion;
370 le32 MinorVersion;
371 le32 DeviceFlags;
372 le32 Medium;
373 le32 MaxPacketsPerTransfer;
374 le32 MaxTransferSize;
375 le32 PacketAlignmentFactor;
376 le32 AFListOffset;
377 le32 AFListSize;
378} rndis_init_cmplt_type;
379
380typedef struct rndis_halt_msg_type {
381 le32 MessageType;
382 le32 MessageLength;
383 le32 RequestID;
384} rndis_halt_msg_type;
385
386typedef struct rndis_query_msg_type {
387 le32 MessageType;
388 le32 MessageLength;
389 le32 RequestID;
390 le32 OID;
391 le32 InformationBufferLength;
392 le32 InformationBufferOffset;
393 le32 DeviceVcHandle;
394} rndis_query_msg_type;
395
396typedef struct rndis_query_cmplt_type {
397 le32 MessageType;
398 le32 MessageLength;
399 le32 RequestID;
400 le32 Status;
401 le32 InformationBufferLength;
402 le32 InformationBufferOffset;
403} rndis_query_cmplt_type;
404
405typedef struct rndis_set_msg_type {
406 le32 MessageType;
407 le32 MessageLength;
408 le32 RequestID;
409 le32 OID;
410 le32 InformationBufferLength;
411 le32 InformationBufferOffset;
412 le32 DeviceVcHandle;
413} rndis_set_msg_type;
414
415typedef struct rndis_set_cmplt_type {
416 le32 MessageType;
417 le32 MessageLength;
418 le32 RequestID;
419 le32 Status;
420} rndis_set_cmplt_type;
421
422typedef struct rndis_reset_msg_type {
423 le32 MessageType;
424 le32 MessageLength;
425 le32 Reserved;
426} rndis_reset_msg_type;
427
428typedef struct rndis_reset_cmplt_type {
429 le32 MessageType;
430 le32 MessageLength;
431 le32 Status;
432 le32 AddressingReset;
433} rndis_reset_cmplt_type;
434
435typedef struct rndis_indicate_status_msg_type {
436 le32 MessageType;
437 le32 MessageLength;
438 le32 Status;
439 le32 StatusBufferLength;
440 le32 StatusBufferOffset;
441} rndis_indicate_status_msg_type;
442
443typedef struct rndis_keepalive_msg_type {
444 le32 MessageType;
445 le32 MessageLength;
446 le32 RequestID;
447} rndis_keepalive_msg_type;
448
449typedef struct rndis_keepalive_cmplt_type {
450 le32 MessageType;
451 le32 MessageLength;
452 le32 RequestID;
453 le32 Status;
454} rndis_keepalive_cmplt_type;
455
456struct rndis_packet_msg_type {
457 le32 MessageType;
458 le32 MessageLength;
459 le32 DataOffset;
460 le32 DataLength;
461 le32 OOBDataOffset;
462 le32 OOBDataLength;
463 le32 NumOOBDataElements;
464 le32 PerPacketInfoOffset;
465 le32 PerPacketInfoLength;
466 le32 VcHandle;
467 le32 Reserved;
468};
469
470struct rndis_config_parameter {
471 le32 ParameterNameOffset;
472 le32 ParameterNameLength;
473 le32 ParameterType;
474 le32 ParameterValueOffset;
475 le32 ParameterValueLength;
476};
477
478/* implementation specific */
479enum rndis_state
480{
481 RNDIS_UNINITIALIZED,
482 RNDIS_INITIALIZED,
483 RNDIS_DATA_INITIALIZED,
484};
485
486/* from ndis.h */
487enum ndis_oid {
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,
517
518 /* Optional OIDs */
519 OID_GEN_MEDIA_CAPABILITIES = 0x00010201,
520 OID_GEN_PHYSICAL_MEDIUM = 0x00010202,
521
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,
528
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,
554
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,
571};
572
573static const uint32_t oid_supported_list[] =
574{
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,
581 OID_GEN_LINK_SPEED,
582 OID_GEN_TRANSMIT_BLOCK_SIZE,
583 OID_GEN_RECEIVE_BLOCK_SIZE,
584 OID_GEN_VENDOR_ID,
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,
591
592 /* the statistical stuff */
593 OID_GEN_XMIT_OK,
594 OID_GEN_RCV_OK,
595 OID_GEN_XMIT_ERROR,
596 OID_GEN_RCV_ERROR,
597 OID_GEN_RCV_NO_BUFFER,
598
599 /* IEEE 802.3 */
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,
606
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,
611};
612
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)
620
621struct rndis_response {
72cf2d4f 622 QTAILQ_ENTRY(rndis_response) entries;
6c9f886c
AZ
623 uint32_t length;
624 uint8_t buf[0];
625};
626
627typedef struct USBNetState {
628 USBDevice dev;
629
6c9f886c
AZ
630 enum rndis_state rndis_state;
631 uint32_t medium;
632 uint32_t speed;
633 uint32_t media_state;
634 uint16_t filter;
635 uint32_t vendorid;
6c9f886c
AZ
636
637 unsigned int out_ptr;
638 uint8_t out_buf[2048];
639
640 USBPacket *inpkt;
641 unsigned int in_ptr, in_len;
642 uint8_t in_buf[2048];
643
644 char usbstring_mac[13];
e0394b8b
MM
645 NICState *nic;
646 NICConf conf;
72cf2d4f 647 QTAILQ_HEAD(rndis_resp_head, rndis_response) rndis_resp;
6c9f886c
AZ
648} USBNetState;
649
a980a065
GH
650static int is_rndis(USBNetState *s)
651{
652 return s->dev.config->bConfigurationValue == DEV_RNDIS_CONFIG_VALUE;
653}
654
6c9f886c 655static int ndis_query(USBNetState *s, uint32_t oid,
363a37d5
BS
656 uint8_t *inbuf, unsigned int inlen, uint8_t *outbuf,
657 size_t outlen)
6c9f886c 658{
b1503cda 659 unsigned int i;
6c9f886c
AZ
660
661 switch (oid) {
662 /* general oids (table 4-1) */
663 /* mandatory */
664 case OID_GEN_SUPPORTED_LIST:
b1503cda 665 for (i = 0; i < ARRAY_SIZE(oid_supported_list); i++)
6c9f886c
AZ
666 ((le32 *) outbuf)[i] = cpu_to_le32(oid_supported_list[i]);
667 return sizeof(oid_supported_list);
668
669 /* mandatory */
670 case OID_GEN_HARDWARE_STATUS:
671 *((le32 *) outbuf) = cpu_to_le32(0);
672 return sizeof(le32);
673
674 /* mandatory */
675 case OID_GEN_MEDIA_SUPPORTED:
676 *((le32 *) outbuf) = cpu_to_le32(s->medium);
677 return sizeof(le32);
678
679 /* mandatory */
680 case OID_GEN_MEDIA_IN_USE:
681 *((le32 *) outbuf) = cpu_to_le32(s->medium);
682 return sizeof(le32);
683
684 /* mandatory */
685 case OID_GEN_MAXIMUM_FRAME_SIZE:
686 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
687 return sizeof(le32);
688
689 /* mandatory */
690 case OID_GEN_LINK_SPEED:
691 *((le32 *) outbuf) = cpu_to_le32(s->speed);
692 return sizeof(le32);
693
694 /* mandatory */
695 case OID_GEN_TRANSMIT_BLOCK_SIZE:
696 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
697 return sizeof(le32);
698
699 /* mandatory */
700 case OID_GEN_RECEIVE_BLOCK_SIZE:
701 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
702 return sizeof(le32);
703
704 /* mandatory */
705 case OID_GEN_VENDOR_ID:
706 *((le32 *) outbuf) = cpu_to_le32(s->vendorid);
707 return sizeof(le32);
708
709 /* mandatory */
710 case OID_GEN_VENDOR_DESCRIPTION:
b55266b5
BS
711 pstrcpy((char *)outbuf, outlen, "QEMU USB RNDIS Net");
712 return strlen((char *)outbuf) + 1;
6c9f886c
AZ
713
714 case OID_GEN_VENDOR_DRIVER_VERSION:
715 *((le32 *) outbuf) = cpu_to_le32(1);
716 return sizeof(le32);
717
718 /* mandatory */
719 case OID_GEN_CURRENT_PACKET_FILTER:
720 *((le32 *) outbuf) = cpu_to_le32(s->filter);
721 return sizeof(le32);
722
723 /* mandatory */
724 case OID_GEN_MAXIMUM_TOTAL_SIZE:
725 *((le32 *) outbuf) = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
726 return sizeof(le32);
727
728 /* mandatory */
729 case OID_GEN_MEDIA_CONNECT_STATUS:
730 *((le32 *) outbuf) = cpu_to_le32(s->media_state);
731 return sizeof(le32);
732
733 case OID_GEN_PHYSICAL_MEDIUM:
734 *((le32 *) outbuf) = cpu_to_le32(0);
735 return sizeof(le32);
736
737 case OID_GEN_MAC_OPTIONS:
738 *((le32 *) outbuf) = cpu_to_le32(
739 NDIS_MAC_OPTION_RECEIVE_SERIALIZED |
740 NDIS_MAC_OPTION_FULL_DUPLEX);
741 return sizeof(le32);
742
743 /* statistics OIDs (table 4-2) */
744 /* mandatory */
745 case OID_GEN_XMIT_OK:
746 *((le32 *) outbuf) = cpu_to_le32(0);
747 return sizeof(le32);
748
749 /* mandatory */
750 case OID_GEN_RCV_OK:
751 *((le32 *) outbuf) = cpu_to_le32(0);
752 return sizeof(le32);
753
754 /* mandatory */
755 case OID_GEN_XMIT_ERROR:
756 *((le32 *) outbuf) = cpu_to_le32(0);
757 return sizeof(le32);
758
759 /* mandatory */
760 case OID_GEN_RCV_ERROR:
761 *((le32 *) outbuf) = cpu_to_le32(0);
762 return sizeof(le32);
763
764 /* mandatory */
765 case OID_GEN_RCV_NO_BUFFER:
766 *((le32 *) outbuf) = cpu_to_le32(0);
767 return sizeof(le32);
768
769 /* ieee802.3 OIDs (table 4-3) */
770 /* mandatory */
771 case OID_802_3_PERMANENT_ADDRESS:
e0394b8b 772 memcpy(outbuf, s->conf.macaddr.a, 6);
6c9f886c
AZ
773 return 6;
774
775 /* mandatory */
776 case OID_802_3_CURRENT_ADDRESS:
e0394b8b 777 memcpy(outbuf, s->conf.macaddr.a, 6);
6c9f886c
AZ
778 return 6;
779
780 /* mandatory */
781 case OID_802_3_MULTICAST_LIST:
782 *((le32 *) outbuf) = cpu_to_le32(0xe0000000);
783 return sizeof(le32);
784
785 /* mandatory */
786 case OID_802_3_MAXIMUM_LIST_SIZE:
787 *((le32 *) outbuf) = cpu_to_le32(1);
788 return sizeof(le32);
789
790 case OID_802_3_MAC_OPTIONS:
791 return 0;
792
793 /* ieee802.3 statistics OIDs (table 4-4) */
794 /* mandatory */
795 case OID_802_3_RCV_ERROR_ALIGNMENT:
796 *((le32 *) outbuf) = cpu_to_le32(0);
797 return sizeof(le32);
798
799 /* mandatory */
800 case OID_802_3_XMIT_ONE_COLLISION:
801 *((le32 *) outbuf) = cpu_to_le32(0);
802 return sizeof(le32);
803
804 /* mandatory */
805 case OID_802_3_XMIT_MORE_COLLISIONS:
806 *((le32 *) outbuf) = cpu_to_le32(0);
807 return sizeof(le32);
808
809 default:
810 fprintf(stderr, "usbnet: unknown OID 0x%08x\n", oid);
811 return 0;
812 }
813 return -1;
814}
815
816static int ndis_set(USBNetState *s, uint32_t oid,
817 uint8_t *inbuf, unsigned int inlen)
818{
819 switch (oid) {
820 case OID_GEN_CURRENT_PACKET_FILTER:
821 s->filter = le32_to_cpup((le32 *) inbuf);
822 if (s->filter) {
823 s->rndis_state = RNDIS_DATA_INITIALIZED;
824 } else {
825 s->rndis_state = RNDIS_INITIALIZED;
826 }
827 return 0;
828
829 case OID_802_3_MULTICAST_LIST:
830 return 0;
831 }
832 return -1;
833}
834
835static int rndis_get_response(USBNetState *s, uint8_t *buf)
836{
837 int ret = 0;
838 struct rndis_response *r = s->rndis_resp.tqh_first;
839
840 if (!r)
841 return ret;
842
72cf2d4f 843 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
6c9f886c
AZ
844 ret = r->length;
845 memcpy(buf, r->buf, r->length);
846 qemu_free(r);
847
848 return ret;
849}
850
851static void *rndis_queue_response(USBNetState *s, unsigned int length)
852{
853 struct rndis_response *r =
854 qemu_mallocz(sizeof(struct rndis_response) + length);
855
72cf2d4f 856 QTAILQ_INSERT_TAIL(&s->rndis_resp, r, entries);
6c9f886c
AZ
857 r->length = length;
858
859 return &r->buf[0];
860}
861
862static void rndis_clear_responsequeue(USBNetState *s)
863{
864 struct rndis_response *r;
865
866 while ((r = s->rndis_resp.tqh_first)) {
72cf2d4f 867 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
6c9f886c
AZ
868 qemu_free(r);
869 }
870}
871
872static int rndis_init_response(USBNetState *s, rndis_init_msg_type *buf)
873{
874 rndis_init_cmplt_type *resp =
875 rndis_queue_response(s, sizeof(rndis_init_cmplt_type));
876
877 if (!resp)
878 return USB_RET_STALL;
879
880 resp->MessageType = cpu_to_le32(RNDIS_INITIALIZE_CMPLT);
881 resp->MessageLength = cpu_to_le32(sizeof(rndis_init_cmplt_type));
882 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
883 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
884 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
885 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
886 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
887 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
888 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
889 resp->MaxTransferSize = cpu_to_le32(ETH_FRAME_LEN +
890 sizeof(struct rndis_packet_msg_type) + 22);
891 resp->PacketAlignmentFactor = cpu_to_le32(0);
892 resp->AFListOffset = cpu_to_le32(0);
893 resp->AFListSize = cpu_to_le32(0);
894 return 0;
895}
896
897static int rndis_query_response(USBNetState *s,
898 rndis_query_msg_type *buf, unsigned int length)
899{
900 rndis_query_cmplt_type *resp;
901 /* oid_supported_list is the largest data reply */
902 uint8_t infobuf[sizeof(oid_supported_list)];
903 uint32_t bufoffs, buflen;
904 int infobuflen;
905 unsigned int resplen;
906
907 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
908 buflen = le32_to_cpu(buf->InformationBufferLength);
909 if (bufoffs + buflen > length)
910 return USB_RET_STALL;
911
912 infobuflen = ndis_query(s, le32_to_cpu(buf->OID),
363a37d5
BS
913 bufoffs + (uint8_t *) buf, buflen, infobuf,
914 sizeof(infobuf));
6c9f886c
AZ
915 resplen = sizeof(rndis_query_cmplt_type) +
916 ((infobuflen < 0) ? 0 : infobuflen);
917 resp = rndis_queue_response(s, resplen);
918 if (!resp)
919 return USB_RET_STALL;
920
921 resp->MessageType = cpu_to_le32(RNDIS_QUERY_CMPLT);
922 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
923 resp->MessageLength = cpu_to_le32(resplen);
924
925 if (infobuflen < 0) {
926 /* OID not supported */
927 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
928 resp->InformationBufferLength = cpu_to_le32(0);
929 resp->InformationBufferOffset = cpu_to_le32(0);
930 return 0;
931 }
932
933 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
934 resp->InformationBufferOffset =
935 cpu_to_le32(infobuflen ? sizeof(rndis_query_cmplt_type) - 8 : 0);
936 resp->InformationBufferLength = cpu_to_le32(infobuflen);
937 memcpy(resp + 1, infobuf, infobuflen);
938
939 return 0;
940}
941
942static int rndis_set_response(USBNetState *s,
943 rndis_set_msg_type *buf, unsigned int length)
944{
945 rndis_set_cmplt_type *resp =
946 rndis_queue_response(s, sizeof(rndis_set_cmplt_type));
947 uint32_t bufoffs, buflen;
948 int ret;
949
950 if (!resp)
951 return USB_RET_STALL;
952
953 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
954 buflen = le32_to_cpu(buf->InformationBufferLength);
955 if (bufoffs + buflen > length)
956 return USB_RET_STALL;
957
958 ret = ndis_set(s, le32_to_cpu(buf->OID),
959 bufoffs + (uint8_t *) buf, buflen);
960 resp->MessageType = cpu_to_le32(RNDIS_SET_CMPLT);
961 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
962 resp->MessageLength = cpu_to_le32(sizeof(rndis_set_cmplt_type));
963 if (ret < 0) {
964 /* OID not supported */
965 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
966 return 0;
967 }
968 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
969
970 return 0;
971}
972
973static int rndis_reset_response(USBNetState *s, rndis_reset_msg_type *buf)
974{
975 rndis_reset_cmplt_type *resp =
976 rndis_queue_response(s, sizeof(rndis_reset_cmplt_type));
977
978 if (!resp)
979 return USB_RET_STALL;
980
981 resp->MessageType = cpu_to_le32(RNDIS_RESET_CMPLT);
982 resp->MessageLength = cpu_to_le32(sizeof(rndis_reset_cmplt_type));
983 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
984 resp->AddressingReset = cpu_to_le32(1); /* reset information */
985
986 return 0;
987}
988
989static int rndis_keepalive_response(USBNetState *s,
990 rndis_keepalive_msg_type *buf)
991{
992 rndis_keepalive_cmplt_type *resp =
993 rndis_queue_response(s, sizeof(rndis_keepalive_cmplt_type));
994
995 if (!resp)
996 return USB_RET_STALL;
997
998 resp->MessageType = cpu_to_le32(RNDIS_KEEPALIVE_CMPLT);
999 resp->MessageLength = cpu_to_le32(sizeof(rndis_keepalive_cmplt_type));
1000 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
1001 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
1002
1003 return 0;
1004}
1005
1006static int rndis_parse(USBNetState *s, uint8_t *data, int length)
1007{
d4c4e6fd 1008 uint32_t msg_type;
6c9f886c
AZ
1009 le32 *tmp = (le32 *) data;
1010
d4c4e6fd 1011 msg_type = le32_to_cpup(tmp);
6c9f886c
AZ
1012
1013 switch (msg_type) {
1014 case RNDIS_INITIALIZE_MSG:
1015 s->rndis_state = RNDIS_INITIALIZED;
1016 return rndis_init_response(s, (rndis_init_msg_type *) data);
1017
1018 case RNDIS_HALT_MSG:
1019 s->rndis_state = RNDIS_UNINITIALIZED;
1020 return 0;
1021
1022 case RNDIS_QUERY_MSG:
1023 return rndis_query_response(s, (rndis_query_msg_type *) data, length);
1024
1025 case RNDIS_SET_MSG:
1026 return rndis_set_response(s, (rndis_set_msg_type *) data, length);
1027
1028 case RNDIS_RESET_MSG:
1029 rndis_clear_responsequeue(s);
1030 s->out_ptr = s->in_ptr = s->in_len = 0;
1031 return rndis_reset_response(s, (rndis_reset_msg_type *) data);
1032
1033 case RNDIS_KEEPALIVE_MSG:
1034 /* For USB: host does this every 5 seconds */
1035 return rndis_keepalive_response(s, (rndis_keepalive_msg_type *) data);
1036 }
1037
1038 return USB_RET_STALL;
1039}
1040
1041static void usb_net_handle_reset(USBDevice *dev)
1042{
1043}
1044
6c9f886c
AZ
1045static int usb_net_handle_control(USBDevice *dev, int request, int value,
1046 int index, int length, uint8_t *data)
1047{
1048 USBNetState *s = (USBNetState *) dev;
30c7d32a 1049 int ret;
6c9f886c 1050
30c7d32a
GH
1051 ret = usb_desc_handle_control(dev, request, value, index, length, data);
1052 if (ret >= 0) {
1053 return ret;
1054 }
1055
1056 ret = 0;
6c9f886c
AZ
1057 switch(request) {
1058 case DeviceRequest | USB_REQ_GET_STATUS:
1059 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
1060 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
1061 data[1] = 0x00;
1062 ret = 2;
1063 break;
1064
1065 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
1066 if (value == USB_DEVICE_REMOTE_WAKEUP) {
1067 dev->remote_wakeup = 0;
1068 } else {
1069 goto fail;
1070 }
1071 ret = 0;
1072 break;
1073
1074 case DeviceOutRequest | USB_REQ_SET_FEATURE:
1075 if (value == USB_DEVICE_REMOTE_WAKEUP) {
1076 dev->remote_wakeup = 1;
1077 } else {
1078 goto fail;
1079 }
1080 ret = 0;
1081 break;
1082
6c9f886c 1083 case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
a980a065 1084 if (!is_rndis(s) || value || index != 0) {
6c9f886c 1085 goto fail;
a980a065 1086 }
eb38c52c 1087#ifdef TRAFFIC_DEBUG
6c9f886c
AZ
1088 {
1089 unsigned int i;
1090 fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");
1091 for (i = 0; i < length; i++) {
1092 if (!(i & 15))
1093 fprintf(stderr, "\n%04x:", i);
1094 fprintf(stderr, " %02x", data[i]);
1095 }
1096 fprintf(stderr, "\n\n");
1097 }
1098#endif
1099 ret = rndis_parse(s, data, length);
1100 break;
1101
1102 case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
a980a065 1103 if (!is_rndis(s) || value || index != 0) {
6c9f886c 1104 goto fail;
a980a065 1105 }
6c9f886c
AZ
1106 ret = rndis_get_response(s, data);
1107 if (!ret) {
1108 data[0] = 0;
1109 ret = 1;
1110 }
eb38c52c 1111#ifdef TRAFFIC_DEBUG
6c9f886c
AZ
1112 {
1113 unsigned int i;
1114 fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");
1115 for (i = 0; i < ret; i++) {
1116 if (!(i & 15))
1117 fprintf(stderr, "\n%04x:", i);
1118 fprintf(stderr, " %02x", data[i]);
1119 }
1120 fprintf(stderr, "\n\n");
1121 }
1122#endif
1123 break;
1124
6c9f886c
AZ
1125 case DeviceRequest | USB_REQ_GET_INTERFACE:
1126 case InterfaceRequest | USB_REQ_GET_INTERFACE:
1127 data[0] = 0;
1128 ret = 1;
1129 break;
1130
1131 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
1132 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1133 ret = 0;
1134 break;
1135
1136 default:
1137 fail:
1138 fprintf(stderr, "usbnet: failed control transaction: "
1139 "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1140 request, value, index, length);
1141 ret = USB_RET_STALL;
1142 break;
1143 }
1144 return ret;
1145}
1146
1147static int usb_net_handle_statusin(USBNetState *s, USBPacket *p)
1148{
1149 int ret = 8;
1150
1151 if (p->len < 8)
1152 return USB_RET_STALL;
1153
1154 ((le32 *) p->data)[0] = cpu_to_le32(1);
1155 ((le32 *) p->data)[1] = cpu_to_le32(0);
1156 if (!s->rndis_resp.tqh_first)
1157 ret = USB_RET_NAK;
1158
eb38c52c 1159#ifdef TRAFFIC_DEBUG
6c9f886c
AZ
1160 fprintf(stderr, "usbnet: interrupt poll len %u return %d", p->len, ret);
1161 {
1162 int i;
1163 fprintf(stderr, ":");
1164 for (i = 0; i < ret; i++) {
1165 if (!(i & 15))
1166 fprintf(stderr, "\n%04x:", i);
1167 fprintf(stderr, " %02x", p->data[i]);
1168 }
1169 fprintf(stderr, "\n\n");
1170 }
1171#endif
1172
1173 return ret;
1174}
1175
1176static int usb_net_handle_datain(USBNetState *s, USBPacket *p)
1177{
1178 int ret = USB_RET_NAK;
1179
1180 if (s->in_ptr > s->in_len) {
1181 s->in_ptr = s->in_len = 0;
1182 ret = USB_RET_NAK;
1183 return ret;
1184 }
1185 if (!s->in_len) {
1186 ret = USB_RET_NAK;
1187 return ret;
1188 }
1189 ret = s->in_len - s->in_ptr;
1190 if (ret > p->len)
1191 ret = p->len;
1192 memcpy(p->data, &s->in_buf[s->in_ptr], ret);
1193 s->in_ptr += ret;
1194 if (s->in_ptr >= s->in_len &&
a980a065 1195 (is_rndis(s) || (s->in_len & (64 - 1)) || !ret)) {
6c9f886c
AZ
1196 /* no short packet necessary */
1197 s->in_ptr = s->in_len = 0;
1198 }
1199
eb38c52c 1200#ifdef TRAFFIC_DEBUG
6c9f886c
AZ
1201 fprintf(stderr, "usbnet: data in len %u return %d", p->len, ret);
1202 {
1203 int i;
1204 fprintf(stderr, ":");
1205 for (i = 0; i < ret; i++) {
1206 if (!(i & 15))
1207 fprintf(stderr, "\n%04x:", i);
1208 fprintf(stderr, " %02x", p->data[i]);
1209 }
1210 fprintf(stderr, "\n\n");
1211 }
1212#endif
1213
1214 return ret;
1215}
1216
1217static int usb_net_handle_dataout(USBNetState *s, USBPacket *p)
1218{
1219 int ret = p->len;
1220 int sz = sizeof(s->out_buf) - s->out_ptr;
1221 struct rndis_packet_msg_type *msg =
1222 (struct rndis_packet_msg_type *) s->out_buf;
1223 uint32_t len;
1224
eb38c52c 1225#ifdef TRAFFIC_DEBUG
6c9f886c
AZ
1226 fprintf(stderr, "usbnet: data out len %u\n", p->len);
1227 {
1228 int i;
1229 fprintf(stderr, ":");
1230 for (i = 0; i < p->len; i++) {
1231 if (!(i & 15))
1232 fprintf(stderr, "\n%04x:", i);
1233 fprintf(stderr, " %02x", p->data[i]);
1234 }
1235 fprintf(stderr, "\n\n");
1236 }
1237#endif
1238
1239 if (sz > ret)
1240 sz = ret;
1241 memcpy(&s->out_buf[s->out_ptr], p->data, sz);
1242 s->out_ptr += sz;
1243
a980a065 1244 if (!is_rndis(s)) {
6c9f886c 1245 if (ret < 64) {
e0394b8b 1246 qemu_send_packet(&s->nic->nc, s->out_buf, s->out_ptr);
6c9f886c
AZ
1247 s->out_ptr = 0;
1248 }
1249 return ret;
1250 }
1251 len = le32_to_cpu(msg->MessageLength);
1252 if (s->out_ptr < 8 || s->out_ptr < len)
1253 return ret;
1254 if (le32_to_cpu(msg->MessageType) == RNDIS_PACKET_MSG) {
1255 uint32_t offs = 8 + le32_to_cpu(msg->DataOffset);
1256 uint32_t size = le32_to_cpu(msg->DataLength);
1257 if (offs + size <= len)
e0394b8b 1258 qemu_send_packet(&s->nic->nc, s->out_buf + offs, size);
6c9f886c
AZ
1259 }
1260 s->out_ptr -= len;
1261 memmove(s->out_buf, &s->out_buf[len], s->out_ptr);
1262
1263 return ret;
1264}
1265
1266static int usb_net_handle_data(USBDevice *dev, USBPacket *p)
1267{
1268 USBNetState *s = (USBNetState *) dev;
1269 int ret = 0;
1270
1271 switch(p->pid) {
1272 case USB_TOKEN_IN:
1273 switch (p->devep) {
1274 case 1:
1275 ret = usb_net_handle_statusin(s, p);
1276 break;
1277
1278 case 2:
1279 ret = usb_net_handle_datain(s, p);
1280 break;
1281
1282 default:
1283 goto fail;
1284 }
1285 break;
1286
1287 case USB_TOKEN_OUT:
1288 switch (p->devep) {
1289 case 2:
1290 ret = usb_net_handle_dataout(s, p);
1291 break;
1292
1293 default:
1294 goto fail;
1295 }
1296 break;
1297
1298 default:
1299 fail:
1300 ret = USB_RET_STALL;
1301 break;
1302 }
1303 if (ret == USB_RET_STALL)
1304 fprintf(stderr, "usbnet: failed data transaction: "
1305 "pid 0x%x ep 0x%x len 0x%x\n",
1306 p->pid, p->devep, p->len);
1307 return ret;
1308}
1309
e0394b8b 1310static ssize_t usbnet_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
6c9f886c 1311{
e0394b8b 1312 USBNetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
6c9f886c
AZ
1313 struct rndis_packet_msg_type *msg;
1314
a980a065 1315 if (is_rndis(s)) {
6c9f886c
AZ
1316 msg = (struct rndis_packet_msg_type *) s->in_buf;
1317 if (!s->rndis_state == RNDIS_DATA_INITIALIZED)
4f1c942b 1318 return -1;
6c9f886c 1319 if (size + sizeof(struct rndis_packet_msg_type) > sizeof(s->in_buf))
4f1c942b 1320 return -1;
6c9f886c
AZ
1321
1322 memset(msg, 0, sizeof(struct rndis_packet_msg_type));
1323 msg->MessageType = cpu_to_le32(RNDIS_PACKET_MSG);
1324 msg->MessageLength = cpu_to_le32(size + sizeof(struct rndis_packet_msg_type));
1325 msg->DataOffset = cpu_to_le32(sizeof(struct rndis_packet_msg_type) - 8);
1326 msg->DataLength = cpu_to_le32(size);
1327 /* msg->OOBDataOffset;
1328 * msg->OOBDataLength;
1329 * msg->NumOOBDataElements;
1330 * msg->PerPacketInfoOffset;
1331 * msg->PerPacketInfoLength;
1332 * msg->VcHandle;
1333 * msg->Reserved;
1334 */
1335 memcpy(msg + 1, buf, size);
1336 s->in_len = size + sizeof(struct rndis_packet_msg_type);
1337 } else {
1338 if (size > sizeof(s->in_buf))
4f1c942b 1339 return -1;
6c9f886c
AZ
1340 memcpy(s->in_buf, buf, size);
1341 s->in_len = size;
1342 }
1343 s->in_ptr = 0;
4f1c942b 1344 return size;
6c9f886c
AZ
1345}
1346
e0394b8b 1347static int usbnet_can_receive(VLANClientState *nc)
6c9f886c 1348{
e0394b8b 1349 USBNetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
6c9f886c 1350
a980a065 1351 if (is_rndis(s) && !s->rndis_state == RNDIS_DATA_INITIALIZED) {
6c9f886c 1352 return 1;
a980a065 1353 }
6c9f886c
AZ
1354
1355 return !s->in_len;
1356}
1357
e0394b8b 1358static void usbnet_cleanup(VLANClientState *nc)
b946a153 1359{
e0394b8b 1360 USBNetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
b946a153 1361
42be86ce 1362 s->nic = NULL;
b946a153
AL
1363}
1364
6c9f886c
AZ
1365static void usb_net_handle_destroy(USBDevice *dev)
1366{
1367 USBNetState *s = (USBNetState *) dev;
1368
dcf414d6 1369 /* TODO: remove the nd_table[] entry */
42be86ce 1370 rndis_clear_responsequeue(s);
e0394b8b 1371 qemu_del_vlan_client(&s->nic->nc);
6c9f886c
AZ
1372}
1373
42be86ce
GH
1374static NetClientInfo net_usbnet_info = {
1375 .type = NET_CLIENT_TYPE_NIC,
1376 .size = sizeof(NICState),
1377 .can_receive = usbnet_can_receive,
1378 .receive = usbnet_receive,
1379 .cleanup = usbnet_cleanup,
1380};
1381
806b6024 1382static int usb_net_initfn(USBDevice *dev)
6c9f886c 1383{
806b6024 1384 USBNetState *s = DO_UPCAST(USBNetState, dev, dev);
6c9f886c 1385
a980a065 1386 usb_desc_init(dev);
6c9f886c 1387
6c9f886c 1388 s->rndis_state = RNDIS_UNINITIALIZED;
72cf2d4f 1389 QTAILQ_INIT(&s->rndis_resp);
806b6024 1390
6c9f886c
AZ
1391 s->medium = 0; /* NDIS_MEDIUM_802_3 */
1392 s->speed = 1000000; /* 100MBps, in 100Bps units */
1393 s->media_state = 0; /* NDIS_MEDIA_STATE_CONNECTED */;
1394 s->filter = 0;
1395 s->vendorid = 0x1234;
42be86ce
GH
1396
1397 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1398 s->nic = qemu_new_nic(&net_usbnet_info, &s->conf,
1399 s->dev.qdev.info->name, s->dev.qdev.id, s);
1400 qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
1401 snprintf(s->usbstring_mac, sizeof(s->usbstring_mac),
1402 "%02x%02x%02x%02x%02x%02x",
1403 0x40,
1404 s->conf.macaddr.a[1],
1405 s->conf.macaddr.a[2],
1406 s->conf.macaddr.a[3],
1407 s->conf.macaddr.a[4],
1408 s->conf.macaddr.a[5]);
30c7d32a 1409 usb_desc_set_string(dev, STRING_ETHADDR, s->usbstring_mac);
42be86ce 1410
1ca4d09a 1411 add_boot_device_path(s->conf.bootindex, &dev->qdev, "/ethernet@0");
806b6024
GH
1412 return 0;
1413}
1414
42be86ce 1415static USBDevice *usb_net_init(const char *cmdline)
806b6024
GH
1416{
1417 USBDevice *dev;
42be86ce
GH
1418 QemuOpts *opts;
1419 int idx;
6c9f886c 1420
3329f07b 1421 opts = qemu_opts_parse(qemu_find_opts("net"), cmdline, 0);
42be86ce
GH
1422 if (!opts) {
1423 return NULL;
1424 }
1425 qemu_opt_set(opts, "type", "nic");
1426 qemu_opt_set(opts, "model", "usb");
6c9f886c 1427
42be86ce
GH
1428 idx = net_client_init(NULL, opts, 0);
1429 if (idx == -1) {
1430 return NULL;
1431 }
7cb7434b 1432
42be86ce 1433 dev = usb_create(NULL /* FIXME */, "usb-net");
d44168ff
PB
1434 if (!dev) {
1435 return NULL;
1436 }
42be86ce 1437 qdev_set_nic_properties(&dev->qdev, &nd_table[idx]);
beb6f0de 1438 qdev_init_nofail(&dev->qdev);
42be86ce 1439 return dev;
6c9f886c 1440}
806b6024
GH
1441
1442static struct USBDeviceInfo net_info = {
06384698 1443 .product_desc = "QEMU USB Network Interface",
556cd098 1444 .qdev.name = "usb-net",
779206de 1445 .qdev.fw_name = "network",
806b6024 1446 .qdev.size = sizeof(USBNetState),
30c7d32a 1447 .usb_desc = &desc_net,
806b6024
GH
1448 .init = usb_net_initfn,
1449 .handle_packet = usb_generic_handle_packet,
1450 .handle_reset = usb_net_handle_reset,
1451 .handle_control = usb_net_handle_control,
1452 .handle_data = usb_net_handle_data,
1453 .handle_destroy = usb_net_handle_destroy,
42be86ce
GH
1454 .usbdevice_name = "net",
1455 .usbdevice_init = usb_net_init,
1456 .qdev.props = (Property[]) {
1457 DEFINE_NIC_PROPERTIES(USBNetState, conf),
1458 DEFINE_PROP_END_OF_LIST(),
1459 }
806b6024
GH
1460};
1461
1462static void usb_net_register_devices(void)
1463{
1464 usb_qdev_register(&net_info);
1465}
1466device_init(usb_net_register_devices)