blob: 7800ceea5b454b20d9cb3fa6bf2a709b3ddbd3a7 [file] [log] [blame]
balrog6c9f8862008-07-17 20:47:13 +00001/*
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"
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +010027#include "hw/usb.h"
28#include "hw/usb/desc.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020029#include "net/net.h"
Gongleid73ad352014-09-19 14:48:25 +080030#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/queue.h"
32#include "qemu/config-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/iov.h"
balrog6c9f8862008-07-17 20:47:13 +000035
36/*#define TRAFFIC_DEBUG*/
37/* Thanks to NetChip Technologies for donating this product ID.
38 * It's for devices with only CDC Ethernet configurations.
39 */
40#define CDC_VENDOR_NUM 0x0525 /* NetChip */
41#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
42/* For hardware that can talk RNDIS and either of the above protocols,
43 * use this ID ... the windows INF files will know it.
44 */
45#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
46#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
47
48enum usbstring_idx {
49 STRING_MANUFACTURER = 1,
50 STRING_PRODUCT,
51 STRING_ETHADDR,
52 STRING_DATA,
53 STRING_CONTROL,
54 STRING_RNDIS_CONTROL,
55 STRING_CDC,
56 STRING_SUBSET,
57 STRING_RNDIS,
58 STRING_SERIALNUMBER,
59};
60
61#define DEV_CONFIG_VALUE 1 /* CDC or a subset */
62#define DEV_RNDIS_CONFIG_VALUE 2 /* RNDIS; optional */
63
64#define USB_CDC_SUBCLASS_ACM 0x02
65#define USB_CDC_SUBCLASS_ETHERNET 0x06
66
67#define USB_CDC_PROTO_NONE 0
68#define USB_CDC_ACM_PROTO_VENDOR 0xff
69
70#define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
71#define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
72#define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
73#define USB_CDC_UNION_TYPE 0x06 /* union_desc */
74#define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
75
balrog6c9f8862008-07-17 20:47:13 +000076#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
Gerd Hoffmann30c7d322010-11-26 10:25:06 +010093static 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",
balrog6c9f8862008-07-17 20:47:13 +0000104};
105
Gerd Hoffmann30c7d322010-11-26 10:25:06 +0100106static 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 }
balrog6c9f8862008-07-17 20:47:13 +0000179};
180
Gerd Hoffmann30c7d322010-11-26 10:25:06 +0100181static 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,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200269 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
Gerd Hoffmann30c7d322010-11-26 10:25:06 +0100270 .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,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200277 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
Gerd Hoffmann30c7d322010-11-26 10:25:06 +0100278 .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,
balrog6c9f8862008-07-17 20:47:13 +0000296};
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 {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000622 QTAILQ_ENTRY(rndis_response) entries;
balrog6c9f8862008-07-17 20:47:13 +0000623 uint32_t length;
624 uint8_t buf[0];
625};
626
627typedef struct USBNetState {
628 USBDevice dev;
629
balrog6c9f8862008-07-17 20:47:13 +0000630 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;
balrog6c9f8862008-07-17 20:47:13 +0000636
637 unsigned int out_ptr;
638 uint8_t out_buf[2048];
639
balrog6c9f8862008-07-17 20:47:13 +0000640 unsigned int in_ptr, in_len;
641 uint8_t in_buf[2048];
642
Hans de Goede8beba932012-11-17 12:47:14 +0100643 USBEndpoint *intr;
644
balrog6c9f8862008-07-17 20:47:13 +0000645 char usbstring_mac[13];
Mark McLoughline0394b8b52009-11-25 18:49:25 +0000646 NICState *nic;
647 NICConf conf;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000648 QTAILQ_HEAD(rndis_resp_head, rndis_response) rndis_resp;
balrog6c9f8862008-07-17 20:47:13 +0000649} USBNetState;
650
Gongleife47db72015-05-06 20:55:30 +0800651#define TYPE_USB_NET "usb-net"
652#define USB_NET(obj) OBJECT_CHECK(USBNetState, (obj), TYPE_USB_NET)
653
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100654static int is_rndis(USBNetState *s)
655{
656 return s->dev.config->bConfigurationValue == DEV_RNDIS_CONFIG_VALUE;
657}
658
balrog6c9f8862008-07-17 20:47:13 +0000659static int ndis_query(USBNetState *s, uint32_t oid,
blueswir1363a37d2008-08-21 17:58:08 +0000660 uint8_t *inbuf, unsigned int inlen, uint8_t *outbuf,
661 size_t outlen)
balrog6c9f8862008-07-17 20:47:13 +0000662{
malcb1503cd2008-12-22 20:33:55 +0000663 unsigned int i;
balrog6c9f8862008-07-17 20:47:13 +0000664
665 switch (oid) {
666 /* general oids (table 4-1) */
667 /* mandatory */
668 case OID_GEN_SUPPORTED_LIST:
malcb1503cd2008-12-22 20:33:55 +0000669 for (i = 0; i < ARRAY_SIZE(oid_supported_list); i++)
balrog6c9f8862008-07-17 20:47:13 +0000670 ((le32 *) outbuf)[i] = cpu_to_le32(oid_supported_list[i]);
671 return sizeof(oid_supported_list);
672
673 /* mandatory */
674 case OID_GEN_HARDWARE_STATUS:
675 *((le32 *) outbuf) = cpu_to_le32(0);
676 return sizeof(le32);
677
678 /* mandatory */
679 case OID_GEN_MEDIA_SUPPORTED:
680 *((le32 *) outbuf) = cpu_to_le32(s->medium);
681 return sizeof(le32);
682
683 /* mandatory */
684 case OID_GEN_MEDIA_IN_USE:
685 *((le32 *) outbuf) = cpu_to_le32(s->medium);
686 return sizeof(le32);
687
688 /* mandatory */
689 case OID_GEN_MAXIMUM_FRAME_SIZE:
690 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
691 return sizeof(le32);
692
693 /* mandatory */
694 case OID_GEN_LINK_SPEED:
695 *((le32 *) outbuf) = cpu_to_le32(s->speed);
696 return sizeof(le32);
697
698 /* mandatory */
699 case OID_GEN_TRANSMIT_BLOCK_SIZE:
700 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
701 return sizeof(le32);
702
703 /* mandatory */
704 case OID_GEN_RECEIVE_BLOCK_SIZE:
705 *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
706 return sizeof(le32);
707
708 /* mandatory */
709 case OID_GEN_VENDOR_ID:
710 *((le32 *) outbuf) = cpu_to_le32(s->vendorid);
711 return sizeof(le32);
712
713 /* mandatory */
714 case OID_GEN_VENDOR_DESCRIPTION:
blueswir1b55266b2008-09-20 08:07:15 +0000715 pstrcpy((char *)outbuf, outlen, "QEMU USB RNDIS Net");
716 return strlen((char *)outbuf) + 1;
balrog6c9f8862008-07-17 20:47:13 +0000717
718 case OID_GEN_VENDOR_DRIVER_VERSION:
719 *((le32 *) outbuf) = cpu_to_le32(1);
720 return sizeof(le32);
721
722 /* mandatory */
723 case OID_GEN_CURRENT_PACKET_FILTER:
724 *((le32 *) outbuf) = cpu_to_le32(s->filter);
725 return sizeof(le32);
726
727 /* mandatory */
728 case OID_GEN_MAXIMUM_TOTAL_SIZE:
729 *((le32 *) outbuf) = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
730 return sizeof(le32);
731
732 /* mandatory */
733 case OID_GEN_MEDIA_CONNECT_STATUS:
734 *((le32 *) outbuf) = cpu_to_le32(s->media_state);
735 return sizeof(le32);
736
737 case OID_GEN_PHYSICAL_MEDIUM:
738 *((le32 *) outbuf) = cpu_to_le32(0);
739 return sizeof(le32);
740
741 case OID_GEN_MAC_OPTIONS:
742 *((le32 *) outbuf) = cpu_to_le32(
743 NDIS_MAC_OPTION_RECEIVE_SERIALIZED |
744 NDIS_MAC_OPTION_FULL_DUPLEX);
745 return sizeof(le32);
746
747 /* statistics OIDs (table 4-2) */
748 /* mandatory */
749 case OID_GEN_XMIT_OK:
750 *((le32 *) outbuf) = cpu_to_le32(0);
751 return sizeof(le32);
752
753 /* mandatory */
754 case OID_GEN_RCV_OK:
755 *((le32 *) outbuf) = cpu_to_le32(0);
756 return sizeof(le32);
757
758 /* mandatory */
759 case OID_GEN_XMIT_ERROR:
760 *((le32 *) outbuf) = cpu_to_le32(0);
761 return sizeof(le32);
762
763 /* mandatory */
764 case OID_GEN_RCV_ERROR:
765 *((le32 *) outbuf) = cpu_to_le32(0);
766 return sizeof(le32);
767
768 /* mandatory */
769 case OID_GEN_RCV_NO_BUFFER:
770 *((le32 *) outbuf) = cpu_to_le32(0);
771 return sizeof(le32);
772
773 /* ieee802.3 OIDs (table 4-3) */
774 /* mandatory */
775 case OID_802_3_PERMANENT_ADDRESS:
Mark McLoughline0394b8b52009-11-25 18:49:25 +0000776 memcpy(outbuf, s->conf.macaddr.a, 6);
balrog6c9f8862008-07-17 20:47:13 +0000777 return 6;
778
779 /* mandatory */
780 case OID_802_3_CURRENT_ADDRESS:
Mark McLoughline0394b8b52009-11-25 18:49:25 +0000781 memcpy(outbuf, s->conf.macaddr.a, 6);
balrog6c9f8862008-07-17 20:47:13 +0000782 return 6;
783
784 /* mandatory */
785 case OID_802_3_MULTICAST_LIST:
786 *((le32 *) outbuf) = cpu_to_le32(0xe0000000);
787 return sizeof(le32);
788
789 /* mandatory */
790 case OID_802_3_MAXIMUM_LIST_SIZE:
791 *((le32 *) outbuf) = cpu_to_le32(1);
792 return sizeof(le32);
793
794 case OID_802_3_MAC_OPTIONS:
795 return 0;
796
797 /* ieee802.3 statistics OIDs (table 4-4) */
798 /* mandatory */
799 case OID_802_3_RCV_ERROR_ALIGNMENT:
800 *((le32 *) outbuf) = cpu_to_le32(0);
801 return sizeof(le32);
802
803 /* mandatory */
804 case OID_802_3_XMIT_ONE_COLLISION:
805 *((le32 *) outbuf) = cpu_to_le32(0);
806 return sizeof(le32);
807
808 /* mandatory */
809 case OID_802_3_XMIT_MORE_COLLISIONS:
810 *((le32 *) outbuf) = cpu_to_le32(0);
811 return sizeof(le32);
812
813 default:
814 fprintf(stderr, "usbnet: unknown OID 0x%08x\n", oid);
815 return 0;
816 }
817 return -1;
818}
819
820static int ndis_set(USBNetState *s, uint32_t oid,
821 uint8_t *inbuf, unsigned int inlen)
822{
823 switch (oid) {
824 case OID_GEN_CURRENT_PACKET_FILTER:
825 s->filter = le32_to_cpup((le32 *) inbuf);
826 if (s->filter) {
827 s->rndis_state = RNDIS_DATA_INITIALIZED;
828 } else {
829 s->rndis_state = RNDIS_INITIALIZED;
830 }
831 return 0;
832
833 case OID_802_3_MULTICAST_LIST:
834 return 0;
835 }
836 return -1;
837}
838
839static int rndis_get_response(USBNetState *s, uint8_t *buf)
840{
841 int ret = 0;
842 struct rndis_response *r = s->rndis_resp.tqh_first;
843
844 if (!r)
845 return ret;
846
Blue Swirl72cf2d42009-09-12 07:36:22 +0000847 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
balrog6c9f8862008-07-17 20:47:13 +0000848 ret = r->length;
849 memcpy(buf, r->buf, r->length);
Anthony Liguori7267c092011-08-20 22:09:37 -0500850 g_free(r);
balrog6c9f8862008-07-17 20:47:13 +0000851
852 return ret;
853}
854
855static void *rndis_queue_response(USBNetState *s, unsigned int length)
856{
857 struct rndis_response *r =
Anthony Liguori7267c092011-08-20 22:09:37 -0500858 g_malloc0(sizeof(struct rndis_response) + length);
balrog6c9f8862008-07-17 20:47:13 +0000859
Hans de Goede8beba932012-11-17 12:47:14 +0100860 if (QTAILQ_EMPTY(&s->rndis_resp)) {
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100861 usb_wakeup(s->intr, 0);
Hans de Goede8beba932012-11-17 12:47:14 +0100862 }
863
Blue Swirl72cf2d42009-09-12 07:36:22 +0000864 QTAILQ_INSERT_TAIL(&s->rndis_resp, r, entries);
balrog6c9f8862008-07-17 20:47:13 +0000865 r->length = length;
866
867 return &r->buf[0];
868}
869
870static void rndis_clear_responsequeue(USBNetState *s)
871{
872 struct rndis_response *r;
873
874 while ((r = s->rndis_resp.tqh_first)) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000875 QTAILQ_REMOVE(&s->rndis_resp, r, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500876 g_free(r);
balrog6c9f8862008-07-17 20:47:13 +0000877 }
878}
879
880static int rndis_init_response(USBNetState *s, rndis_init_msg_type *buf)
881{
882 rndis_init_cmplt_type *resp =
883 rndis_queue_response(s, sizeof(rndis_init_cmplt_type));
884
885 if (!resp)
886 return USB_RET_STALL;
887
888 resp->MessageType = cpu_to_le32(RNDIS_INITIALIZE_CMPLT);
889 resp->MessageLength = cpu_to_le32(sizeof(rndis_init_cmplt_type));
890 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
891 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
892 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
893 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
894 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
895 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
896 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
897 resp->MaxTransferSize = cpu_to_le32(ETH_FRAME_LEN +
898 sizeof(struct rndis_packet_msg_type) + 22);
899 resp->PacketAlignmentFactor = cpu_to_le32(0);
900 resp->AFListOffset = cpu_to_le32(0);
901 resp->AFListSize = cpu_to_le32(0);
902 return 0;
903}
904
905static int rndis_query_response(USBNetState *s,
906 rndis_query_msg_type *buf, unsigned int length)
907{
908 rndis_query_cmplt_type *resp;
909 /* oid_supported_list is the largest data reply */
910 uint8_t infobuf[sizeof(oid_supported_list)];
911 uint32_t bufoffs, buflen;
912 int infobuflen;
913 unsigned int resplen;
914
915 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
916 buflen = le32_to_cpu(buf->InformationBufferLength);
917 if (bufoffs + buflen > length)
918 return USB_RET_STALL;
919
920 infobuflen = ndis_query(s, le32_to_cpu(buf->OID),
blueswir1363a37d2008-08-21 17:58:08 +0000921 bufoffs + (uint8_t *) buf, buflen, infobuf,
922 sizeof(infobuf));
balrog6c9f8862008-07-17 20:47:13 +0000923 resplen = sizeof(rndis_query_cmplt_type) +
924 ((infobuflen < 0) ? 0 : infobuflen);
925 resp = rndis_queue_response(s, resplen);
926 if (!resp)
927 return USB_RET_STALL;
928
929 resp->MessageType = cpu_to_le32(RNDIS_QUERY_CMPLT);
930 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
931 resp->MessageLength = cpu_to_le32(resplen);
932
933 if (infobuflen < 0) {
934 /* OID not supported */
935 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
936 resp->InformationBufferLength = cpu_to_le32(0);
937 resp->InformationBufferOffset = cpu_to_le32(0);
938 return 0;
939 }
940
941 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
942 resp->InformationBufferOffset =
943 cpu_to_le32(infobuflen ? sizeof(rndis_query_cmplt_type) - 8 : 0);
944 resp->InformationBufferLength = cpu_to_le32(infobuflen);
945 memcpy(resp + 1, infobuf, infobuflen);
946
947 return 0;
948}
949
950static int rndis_set_response(USBNetState *s,
951 rndis_set_msg_type *buf, unsigned int length)
952{
953 rndis_set_cmplt_type *resp =
954 rndis_queue_response(s, sizeof(rndis_set_cmplt_type));
955 uint32_t bufoffs, buflen;
956 int ret;
957
958 if (!resp)
959 return USB_RET_STALL;
960
961 bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
962 buflen = le32_to_cpu(buf->InformationBufferLength);
963 if (bufoffs + buflen > length)
964 return USB_RET_STALL;
965
966 ret = ndis_set(s, le32_to_cpu(buf->OID),
967 bufoffs + (uint8_t *) buf, buflen);
968 resp->MessageType = cpu_to_le32(RNDIS_SET_CMPLT);
969 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
970 resp->MessageLength = cpu_to_le32(sizeof(rndis_set_cmplt_type));
971 if (ret < 0) {
972 /* OID not supported */
973 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
974 return 0;
975 }
976 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
977
978 return 0;
979}
980
981static int rndis_reset_response(USBNetState *s, rndis_reset_msg_type *buf)
982{
983 rndis_reset_cmplt_type *resp =
984 rndis_queue_response(s, sizeof(rndis_reset_cmplt_type));
985
986 if (!resp)
987 return USB_RET_STALL;
988
989 resp->MessageType = cpu_to_le32(RNDIS_RESET_CMPLT);
990 resp->MessageLength = cpu_to_le32(sizeof(rndis_reset_cmplt_type));
991 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
992 resp->AddressingReset = cpu_to_le32(1); /* reset information */
993
994 return 0;
995}
996
997static int rndis_keepalive_response(USBNetState *s,
998 rndis_keepalive_msg_type *buf)
999{
1000 rndis_keepalive_cmplt_type *resp =
1001 rndis_queue_response(s, sizeof(rndis_keepalive_cmplt_type));
1002
1003 if (!resp)
1004 return USB_RET_STALL;
1005
1006 resp->MessageType = cpu_to_le32(RNDIS_KEEPALIVE_CMPLT);
1007 resp->MessageLength = cpu_to_le32(sizeof(rndis_keepalive_cmplt_type));
1008 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
1009 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
1010
1011 return 0;
1012}
1013
Stefan Hajnoczi190563f2012-08-24 13:37:29 +01001014/* Prepare to receive the next packet */
1015static void usb_net_reset_in_buf(USBNetState *s)
1016{
1017 s->in_ptr = s->in_len = 0;
Jason Wangb356f762013-01-30 19:12:22 +08001018 qemu_flush_queued_packets(qemu_get_queue(s->nic));
Stefan Hajnoczi190563f2012-08-24 13:37:29 +01001019}
1020
balrog6c9f8862008-07-17 20:47:13 +00001021static int rndis_parse(USBNetState *s, uint8_t *data, int length)
1022{
Blue Swirld4c4e6f2010-04-25 18:23:04 +00001023 uint32_t msg_type;
balrog6c9f8862008-07-17 20:47:13 +00001024 le32 *tmp = (le32 *) data;
1025
Blue Swirld4c4e6f2010-04-25 18:23:04 +00001026 msg_type = le32_to_cpup(tmp);
balrog6c9f8862008-07-17 20:47:13 +00001027
1028 switch (msg_type) {
1029 case RNDIS_INITIALIZE_MSG:
1030 s->rndis_state = RNDIS_INITIALIZED;
1031 return rndis_init_response(s, (rndis_init_msg_type *) data);
1032
1033 case RNDIS_HALT_MSG:
1034 s->rndis_state = RNDIS_UNINITIALIZED;
1035 return 0;
1036
1037 case RNDIS_QUERY_MSG:
1038 return rndis_query_response(s, (rndis_query_msg_type *) data, length);
1039
1040 case RNDIS_SET_MSG:
1041 return rndis_set_response(s, (rndis_set_msg_type *) data, length);
1042
1043 case RNDIS_RESET_MSG:
1044 rndis_clear_responsequeue(s);
Stefan Hajnoczi190563f2012-08-24 13:37:29 +01001045 s->out_ptr = 0;
1046 usb_net_reset_in_buf(s);
balrog6c9f8862008-07-17 20:47:13 +00001047 return rndis_reset_response(s, (rndis_reset_msg_type *) data);
1048
1049 case RNDIS_KEEPALIVE_MSG:
1050 /* For USB: host does this every 5 seconds */
1051 return rndis_keepalive_response(s, (rndis_keepalive_msg_type *) data);
1052 }
1053
1054 return USB_RET_STALL;
1055}
1056
1057static void usb_net_handle_reset(USBDevice *dev)
1058{
1059}
1060
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001061static void usb_net_handle_control(USBDevice *dev, USBPacket *p,
Hans de Goede007fd622011-02-02 16:33:13 +01001062 int request, int value, int index, int length, uint8_t *data)
balrog6c9f8862008-07-17 20:47:13 +00001063{
1064 USBNetState *s = (USBNetState *) dev;
Gerd Hoffmann30c7d322010-11-26 10:25:06 +01001065 int ret;
balrog6c9f8862008-07-17 20:47:13 +00001066
Hans de Goede007fd622011-02-02 16:33:13 +01001067 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
Gerd Hoffmann30c7d322010-11-26 10:25:06 +01001068 if (ret >= 0) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001069 return;
Gerd Hoffmann30c7d322010-11-26 10:25:06 +01001070 }
1071
balrog6c9f8862008-07-17 20:47:13 +00001072 switch(request) {
balrog6c9f8862008-07-17 20:47:13 +00001073 case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Gerd Hoffmanna980a062010-11-26 20:20:41 +01001074 if (!is_rndis(s) || value || index != 0) {
balrog6c9f8862008-07-17 20:47:13 +00001075 goto fail;
Gerd Hoffmanna980a062010-11-26 20:20:41 +01001076 }
blueswir1eb38c522008-09-06 17:47:39 +00001077#ifdef TRAFFIC_DEBUG
balrog6c9f8862008-07-17 20:47:13 +00001078 {
1079 unsigned int i;
1080 fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");
1081 for (i = 0; i < length; i++) {
1082 if (!(i & 15))
1083 fprintf(stderr, "\n%04x:", i);
1084 fprintf(stderr, " %02x", data[i]);
1085 }
1086 fprintf(stderr, "\n\n");
1087 }
1088#endif
1089 ret = rndis_parse(s, data, length);
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001090 if (ret < 0) {
1091 p->status = ret;
1092 }
balrog6c9f8862008-07-17 20:47:13 +00001093 break;
1094
1095 case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
Gerd Hoffmanna980a062010-11-26 20:20:41 +01001096 if (!is_rndis(s) || value || index != 0) {
balrog6c9f8862008-07-17 20:47:13 +00001097 goto fail;
Gerd Hoffmanna980a062010-11-26 20:20:41 +01001098 }
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001099 p->actual_length = rndis_get_response(s, data);
1100 if (p->actual_length == 0) {
balrog6c9f8862008-07-17 20:47:13 +00001101 data[0] = 0;
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001102 p->actual_length = 1;
balrog6c9f8862008-07-17 20:47:13 +00001103 }
blueswir1eb38c522008-09-06 17:47:39 +00001104#ifdef TRAFFIC_DEBUG
balrog6c9f8862008-07-17 20:47:13 +00001105 {
1106 unsigned int i;
1107 fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001108 for (i = 0; i < p->actual_length; i++) {
balrog6c9f8862008-07-17 20:47:13 +00001109 if (!(i & 15))
1110 fprintf(stderr, "\n%04x:", i);
1111 fprintf(stderr, " %02x", data[i]);
1112 }
1113 fprintf(stderr, "\n\n");
1114 }
1115#endif
1116 break;
1117
balrog6c9f8862008-07-17 20:47:13 +00001118 default:
1119 fail:
1120 fprintf(stderr, "usbnet: failed control transaction: "
1121 "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1122 request, value, index, length);
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001123 p->status = USB_RET_STALL;
balrog6c9f8862008-07-17 20:47:13 +00001124 break;
1125 }
balrog6c9f8862008-07-17 20:47:13 +00001126}
1127
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001128static void usb_net_handle_statusin(USBNetState *s, USBPacket *p)
balrog6c9f8862008-07-17 20:47:13 +00001129{
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001130 le32 buf[2];
balrog6c9f8862008-07-17 20:47:13 +00001131
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001132 if (p->iov.size < 8) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001133 p->status = USB_RET_STALL;
1134 return;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001135 }
balrog6c9f8862008-07-17 20:47:13 +00001136
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001137 buf[0] = cpu_to_le32(1);
1138 buf[1] = cpu_to_le32(0);
1139 usb_packet_copy(p, buf, 8);
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001140 if (!s->rndis_resp.tqh_first) {
1141 p->status = USB_RET_NAK;
1142 }
balrog6c9f8862008-07-17 20:47:13 +00001143
blueswir1eb38c522008-09-06 17:47:39 +00001144#ifdef TRAFFIC_DEBUG
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001145 fprintf(stderr, "usbnet: interrupt poll len %zu return %d",
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001146 p->iov.size, p->status);
1147 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->status);
balrog6c9f8862008-07-17 20:47:13 +00001148#endif
balrog6c9f8862008-07-17 20:47:13 +00001149}
1150
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001151static void usb_net_handle_datain(USBNetState *s, USBPacket *p)
balrog6c9f8862008-07-17 20:47:13 +00001152{
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001153 int len;
balrog6c9f8862008-07-17 20:47:13 +00001154
1155 if (s->in_ptr > s->in_len) {
Stefan Hajnoczi190563f2012-08-24 13:37:29 +01001156 usb_net_reset_in_buf(s);
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001157 p->status = USB_RET_NAK;
1158 return;
balrog6c9f8862008-07-17 20:47:13 +00001159 }
1160 if (!s->in_len) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001161 p->status = USB_RET_NAK;
1162 return;
balrog6c9f8862008-07-17 20:47:13 +00001163 }
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001164 len = s->in_len - s->in_ptr;
1165 if (len > p->iov.size) {
1166 len = p->iov.size;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001167 }
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001168 usb_packet_copy(p, &s->in_buf[s->in_ptr], len);
1169 s->in_ptr += len;
balrog6c9f8862008-07-17 20:47:13 +00001170 if (s->in_ptr >= s->in_len &&
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001171 (is_rndis(s) || (s->in_len & (64 - 1)) || !len)) {
balrog6c9f8862008-07-17 20:47:13 +00001172 /* no short packet necessary */
Stefan Hajnoczi190563f2012-08-24 13:37:29 +01001173 usb_net_reset_in_buf(s);
balrog6c9f8862008-07-17 20:47:13 +00001174 }
1175
blueswir1eb38c522008-09-06 17:47:39 +00001176#ifdef TRAFFIC_DEBUG
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001177 fprintf(stderr, "usbnet: data in len %zu return %d", p->iov.size, len);
1178 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", len);
balrog6c9f8862008-07-17 20:47:13 +00001179#endif
balrog6c9f8862008-07-17 20:47:13 +00001180}
1181
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001182static void usb_net_handle_dataout(USBNetState *s, USBPacket *p)
balrog6c9f8862008-07-17 20:47:13 +00001183{
balrog6c9f8862008-07-17 20:47:13 +00001184 int sz = sizeof(s->out_buf) - s->out_ptr;
1185 struct rndis_packet_msg_type *msg =
1186 (struct rndis_packet_msg_type *) s->out_buf;
1187 uint32_t len;
1188
blueswir1eb38c522008-09-06 17:47:39 +00001189#ifdef TRAFFIC_DEBUG
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001190 fprintf(stderr, "usbnet: data out len %zu\n", p->iov.size);
1191 iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->iov.size);
balrog6c9f8862008-07-17 20:47:13 +00001192#endif
1193
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001194 if (sz > p->iov.size) {
1195 sz = p->iov.size;
1196 }
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001197 usb_packet_copy(p, &s->out_buf[s->out_ptr], sz);
balrog6c9f8862008-07-17 20:47:13 +00001198 s->out_ptr += sz;
1199
Gerd Hoffmanna980a062010-11-26 20:20:41 +01001200 if (!is_rndis(s)) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001201 if (p->iov.size < 64) {
Jason Wangb356f762013-01-30 19:12:22 +08001202 qemu_send_packet(qemu_get_queue(s->nic), s->out_buf, s->out_ptr);
balrog6c9f8862008-07-17 20:47:13 +00001203 s->out_ptr = 0;
1204 }
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001205 return;
balrog6c9f8862008-07-17 20:47:13 +00001206 }
1207 len = le32_to_cpu(msg->MessageLength);
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001208 if (s->out_ptr < 8 || s->out_ptr < len) {
1209 return;
1210 }
balrog6c9f8862008-07-17 20:47:13 +00001211 if (le32_to_cpu(msg->MessageType) == RNDIS_PACKET_MSG) {
1212 uint32_t offs = 8 + le32_to_cpu(msg->DataOffset);
1213 uint32_t size = le32_to_cpu(msg->DataLength);
1214 if (offs + size <= len)
Jason Wangb356f762013-01-30 19:12:22 +08001215 qemu_send_packet(qemu_get_queue(s->nic), s->out_buf + offs, size);
balrog6c9f8862008-07-17 20:47:13 +00001216 }
1217 s->out_ptr -= len;
1218 memmove(s->out_buf, &s->out_buf[len], s->out_ptr);
balrog6c9f8862008-07-17 20:47:13 +00001219}
1220
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001221static void usb_net_handle_data(USBDevice *dev, USBPacket *p)
balrog6c9f8862008-07-17 20:47:13 +00001222{
1223 USBNetState *s = (USBNetState *) dev;
balrog6c9f8862008-07-17 20:47:13 +00001224
1225 switch(p->pid) {
1226 case USB_TOKEN_IN:
Gerd Hoffmann079d0b72012-01-12 13:23:01 +01001227 switch (p->ep->nr) {
balrog6c9f8862008-07-17 20:47:13 +00001228 case 1:
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001229 usb_net_handle_statusin(s, p);
balrog6c9f8862008-07-17 20:47:13 +00001230 break;
1231
1232 case 2:
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001233 usb_net_handle_datain(s, p);
balrog6c9f8862008-07-17 20:47:13 +00001234 break;
1235
1236 default:
1237 goto fail;
1238 }
1239 break;
1240
1241 case USB_TOKEN_OUT:
Gerd Hoffmann079d0b72012-01-12 13:23:01 +01001242 switch (p->ep->nr) {
balrog6c9f8862008-07-17 20:47:13 +00001243 case 2:
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001244 usb_net_handle_dataout(s, p);
balrog6c9f8862008-07-17 20:47:13 +00001245 break;
1246
1247 default:
1248 goto fail;
1249 }
1250 break;
1251
1252 default:
1253 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001254 p->status = USB_RET_STALL;
balrog6c9f8862008-07-17 20:47:13 +00001255 break;
1256 }
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001257
1258 if (p->status == USB_RET_STALL) {
balrog6c9f8862008-07-17 20:47:13 +00001259 fprintf(stderr, "usbnet: failed data transaction: "
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +02001260 "pid 0x%x ep 0x%x len 0x%zx\n",
Gerd Hoffmann079d0b72012-01-12 13:23:01 +01001261 p->pid, p->ep->nr, p->iov.size);
Hans de Goede9a77a0f2012-11-01 17:15:01 +01001262 }
balrog6c9f8862008-07-17 20:47:13 +00001263}
1264
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +01001265static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t size)
balrog6c9f8862008-07-17 20:47:13 +00001266{
Jason Wangcc1f0f42013-01-30 19:12:23 +08001267 USBNetState *s = qemu_get_nic_opaque(nc);
Stefan Hajnoczif237ddb2012-08-24 13:32:16 +01001268 uint8_t *in_buf = s->in_buf;
1269 size_t total_size = size;
balrog6c9f8862008-07-17 20:47:13 +00001270
Fam Zheng91344022015-07-15 18:19:05 +08001271 if (!s->dev.config) {
1272 return -1;
1273 }
1274
Gerd Hoffmanna980a062010-11-26 20:20:41 +01001275 if (is_rndis(s)) {
Peter Maydell98d23702011-11-09 21:09:23 +00001276 if (s->rndis_state != RNDIS_DATA_INITIALIZED) {
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001277 return -1;
Peter Maydell98d23702011-11-09 21:09:23 +00001278 }
Stefan Hajnoczif237ddb2012-08-24 13:32:16 +01001279 total_size += sizeof(struct rndis_packet_msg_type);
1280 }
1281 if (total_size > sizeof(s->in_buf)) {
1282 return -1;
1283 }
balrog6c9f8862008-07-17 20:47:13 +00001284
Stefan Hajnoczi190563f2012-08-24 13:37:29 +01001285 /* Only accept packet if input buffer is empty */
1286 if (s->in_len > 0) {
1287 return 0;
1288 }
1289
Stefan Hajnoczif237ddb2012-08-24 13:32:16 +01001290 if (is_rndis(s)) {
1291 struct rndis_packet_msg_type *msg;
1292
1293 msg = (struct rndis_packet_msg_type *)in_buf;
balrog6c9f8862008-07-17 20:47:13 +00001294 memset(msg, 0, sizeof(struct rndis_packet_msg_type));
1295 msg->MessageType = cpu_to_le32(RNDIS_PACKET_MSG);
Stefan Hajnoczif237ddb2012-08-24 13:32:16 +01001296 msg->MessageLength = cpu_to_le32(size + sizeof(*msg));
1297 msg->DataOffset = cpu_to_le32(sizeof(*msg) - 8);
balrog6c9f8862008-07-17 20:47:13 +00001298 msg->DataLength = cpu_to_le32(size);
1299 /* msg->OOBDataOffset;
1300 * msg->OOBDataLength;
1301 * msg->NumOOBDataElements;
1302 * msg->PerPacketInfoOffset;
1303 * msg->PerPacketInfoLength;
1304 * msg->VcHandle;
1305 * msg->Reserved;
1306 */
Stefan Hajnoczif237ddb2012-08-24 13:32:16 +01001307 in_buf += sizeof(*msg);
balrog6c9f8862008-07-17 20:47:13 +00001308 }
Stefan Hajnoczif237ddb2012-08-24 13:32:16 +01001309
1310 memcpy(in_buf, buf, size);
1311 s->in_len = total_size;
balrog6c9f8862008-07-17 20:47:13 +00001312 s->in_ptr = 0;
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001313 return size;
balrog6c9f8862008-07-17 20:47:13 +00001314}
1315
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +01001316static void usbnet_cleanup(NetClientState *nc)
aliguorib946a152009-04-17 17:11:08 +00001317{
Jason Wangcc1f0f42013-01-30 19:12:23 +08001318 USBNetState *s = qemu_get_nic_opaque(nc);
aliguorib946a152009-04-17 17:11:08 +00001319
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001320 s->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +00001321}
1322
balrog6c9f8862008-07-17 20:47:13 +00001323static void usb_net_handle_destroy(USBDevice *dev)
1324{
1325 USBNetState *s = (USBNetState *) dev;
1326
balrogdcf414d2008-07-17 21:00:05 +00001327 /* TODO: remove the nd_table[] entry */
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001328 rndis_clear_responsequeue(s);
Jason Wang948ecf22013-01-30 19:12:24 +08001329 qemu_del_nic(s->nic);
balrog6c9f8862008-07-17 20:47:13 +00001330}
1331
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001332static NetClientInfo net_usbnet_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001333 .type = NET_CLIENT_OPTIONS_KIND_NIC,
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001334 .size = sizeof(NICState),
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001335 .receive = usbnet_receive,
1336 .cleanup = usbnet_cleanup,
1337};
1338
Gongleid73ad352014-09-19 14:48:25 +08001339static void usb_net_realize(USBDevice *dev, Error **errrp)
balrog6c9f8862008-07-17 20:47:13 +00001340{
Gongleife47db72015-05-06 20:55:30 +08001341 USBNetState *s = USB_NET(dev);
balrog6c9f8862008-07-17 20:47:13 +00001342
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +02001343 usb_desc_create_serial(dev);
Gerd Hoffmanna980a062010-11-26 20:20:41 +01001344 usb_desc_init(dev);
balrog6c9f8862008-07-17 20:47:13 +00001345
balrog6c9f8862008-07-17 20:47:13 +00001346 s->rndis_state = RNDIS_UNINITIALIZED;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001347 QTAILQ_INIT(&s->rndis_resp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001348
balrog6c9f8862008-07-17 20:47:13 +00001349 s->medium = 0; /* NDIS_MEDIUM_802_3 */
1350 s->speed = 1000000; /* 100MBps, in 100Bps units */
1351 s->media_state = 0; /* NDIS_MEDIA_STATE_CONNECTED */;
1352 s->filter = 0;
1353 s->vendorid = 0x1234;
Hans de Goede8beba932012-11-17 12:47:14 +01001354 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001355
1356 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1357 s->nic = qemu_new_nic(&net_usbnet_info, &s->conf,
Anthony Liguorif79f2bf2011-12-04 11:17:51 -06001358 object_get_typename(OBJECT(s)), s->dev.qdev.id, s);
Jason Wangb356f762013-01-30 19:12:22 +08001359 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001360 snprintf(s->usbstring_mac, sizeof(s->usbstring_mac),
1361 "%02x%02x%02x%02x%02x%02x",
1362 0x40,
1363 s->conf.macaddr.a[1],
1364 s->conf.macaddr.a[2],
1365 s->conf.macaddr.a[3],
1366 s->conf.macaddr.a[4],
1367 s->conf.macaddr.a[5]);
Gerd Hoffmann30c7d322010-11-26 10:25:06 +01001368 usb_desc_set_string(dev, STRING_ETHADDR, s->usbstring_mac);
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001369}
1370
Gongleic11f4bc2014-10-07 16:00:20 +08001371static void usb_net_instance_init(Object *obj)
1372{
1373 USBDevice *dev = USB_DEVICE(obj);
Gongleife47db72015-05-06 20:55:30 +08001374 USBNetState *s = USB_NET(dev);
Gongleic11f4bc2014-10-07 16:00:20 +08001375
1376 device_add_bootindex_property(obj, &s->conf.bootindex,
1377 "bootindex", "/ethernet-phy@0",
1378 &dev->qdev, NULL);
1379}
1380
Jan Kiszka37417152012-02-27 15:18:47 +01001381static USBDevice *usb_net_init(USBBus *bus, const char *cmdline)
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001382{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001383 Error *local_err = NULL;
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001384 USBDevice *dev;
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001385 QemuOpts *opts;
1386 int idx;
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001387
Markus Armbruster70b94332015-02-13 12:50:26 +01001388 opts = qemu_opts_parse_noisily(qemu_find_opts("net"), cmdline, false);
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001389 if (!opts) {
1390 return NULL;
1391 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001392 qemu_opt_set(opts, "type", "nic", &error_abort);
1393 qemu_opt_set(opts, "model", "usb", &error_abort);
balrog6c9f8862008-07-17 20:47:13 +00001394
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001395 idx = net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001396 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001397 error_report_err(local_err);
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001398 return NULL;
1399 }
balrog6c9f8862008-07-17 20:47:13 +00001400
Jan Kiszka37417152012-02-27 15:18:47 +01001401 dev = usb_create(bus, "usb-net");
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001402 qdev_set_nic_properties(&dev->qdev, &nd_table[idx]);
Gerd Hoffmann42be86c2009-12-15 10:20:57 +01001403 return dev;
balrog6c9f8862008-07-17 20:47:13 +00001404}
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001405
Gerd Hoffmann4ab0ba92011-07-20 10:06:07 +02001406static const VMStateDescription vmstate_usb_net = {
1407 .name = "usb-net",
1408 .unmigratable = 1,
1409};
1410
Anthony Liguori39bffca2011-12-07 21:34:16 -06001411static Property net_properties[] = {
1412 DEFINE_NIC_PROPERTIES(USBNetState, conf),
1413 DEFINE_PROP_END_OF_LIST(),
1414};
1415
Anthony Liguori62aed762011-12-15 14:53:10 -06001416static void usb_net_class_initfn(ObjectClass *klass, void *data)
1417{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001418 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -06001419 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1420
Gongleid73ad352014-09-19 14:48:25 +08001421 uc->realize = usb_net_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -06001422 uc->product_desc = "QEMU USB Network Interface";
1423 uc->usb_desc = &desc_net;
Anthony Liguori62aed762011-12-15 14:53:10 -06001424 uc->handle_reset = usb_net_handle_reset;
1425 uc->handle_control = usb_net_handle_control;
1426 uc->handle_data = usb_net_handle_data;
1427 uc->handle_destroy = usb_net_handle_destroy;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +03001428 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -06001429 dc->fw_name = "network";
1430 dc->vmsd = &vmstate_usb_net;
1431 dc->props = net_properties;
Anthony Liguori62aed762011-12-15 14:53:10 -06001432}
1433
Andreas Färber8c43a6f2013-01-10 16:19:07 +01001434static const TypeInfo net_info = {
Gongleife47db72015-05-06 20:55:30 +08001435 .name = TYPE_USB_NET,
Anthony Liguori39bffca2011-12-07 21:34:16 -06001436 .parent = TYPE_USB_DEVICE,
1437 .instance_size = sizeof(USBNetState),
1438 .class_init = usb_net_class_initfn,
Gongleic11f4bc2014-10-07 16:00:20 +08001439 .instance_init = usb_net_instance_init,
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001440};
1441
Andreas Färber83f7d432012-02-09 15:20:55 +01001442static void usb_net_register_types(void)
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001443{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001444 type_register_static(&net_info);
Gongleife47db72015-05-06 20:55:30 +08001445 usb_legacy_register(TYPE_USB_NET, "net", usb_net_init);
Gerd Hoffmann806b6022009-08-31 14:23:59 +02001446}
Andreas Färber83f7d432012-02-09 15:20:55 +01001447
1448type_init(usb_net_register_types)