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