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