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