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