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