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