]>
Commit | Line | Data |
---|---|---|
23cd1385 RB |
1 | /* |
2 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options | |
3 | * | |
4 | * Copyright (C) 2003-2005,2008 David Brownell | |
5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | |
6 | * Copyright (C) 2008 Nokia Corporation | |
7 | * | |
1a459660 | 8 | * SPDX-License-Identifier: GPL-2.0+ |
23cd1385 RB |
9 | */ |
10 | ||
11 | #include <common.h> | |
24b852a7 | 12 | #include <console.h> |
1221ce45 | 13 | #include <linux/errno.h> |
c85d70ef | 14 | #include <linux/netdevice.h> |
23cd1385 RB |
15 | #include <linux/usb/ch9.h> |
16 | #include <linux/usb/cdc.h> | |
17 | #include <linux/usb/gadget.h> | |
18 | #include <net.h> | |
8bfc288c | 19 | #include <usb.h> |
7612a43d | 20 | #include <malloc.h> |
cf92e05c | 21 | #include <memalign.h> |
23cd1385 RB |
22 | #include <linux/ctype.h> |
23 | ||
24 | #include "gadget_chips.h" | |
7612a43d | 25 | #include "rndis.h" |
23cd1385 | 26 | |
d4345aee M |
27 | #include <dm.h> |
28 | #include <dm/uclass-internal.h> | |
29 | #include <dm/device-internal.h> | |
30 | ||
8f7aa831 | 31 | #define USB_NET_NAME "usb_ether" |
7de73185 | 32 | |
23cd1385 RB |
33 | #define atomic_read |
34 | extern struct platform_data brd; | |
23cd1385 RB |
35 | |
36 | ||
37 | unsigned packet_received, packet_sent; | |
38 | ||
23cd1385 RB |
39 | /* |
40 | * Ethernet gadget driver -- with CDC and non-CDC options | |
41 | * Builds on hardware support for a full duplex link. | |
42 | * | |
43 | * CDC Ethernet is the standard USB solution for sending Ethernet frames | |
44 | * using USB. Real hardware tends to use the same framing protocol but look | |
45 | * different for control features. This driver strongly prefers to use | |
46 | * this USB-IF standard as its open-systems interoperability solution; | |
47 | * most host side USB stacks (except from Microsoft) support it. | |
48 | * | |
49 | * This is sometimes called "CDC ECM" (Ethernet Control Model) to support | |
50 | * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new | |
51 | * "CDC EEM" (Ethernet Emulation Model) is starting to spread. | |
52 | * | |
53 | * There's some hardware that can't talk CDC ECM. We make that hardware | |
54 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but | |
55 | * link-level setup only requires activating the configuration. Only the | |
56 | * endpoint descriptors, and product/vendor IDs, are relevant; no control | |
57 | * operations are available. Linux supports it, but other host operating | |
58 | * systems may not. (This is a subset of CDC Ethernet.) | |
59 | * | |
60 | * It turns out that if you add a few descriptors to that "CDC Subset", | |
61 | * (Windows) host side drivers from MCCI can treat it as one submode of | |
62 | * a proprietary scheme called "SAFE" ... without needing to know about | |
63 | * specific product/vendor IDs. So we do that, making it easier to use | |
64 | * those MS-Windows drivers. Those added descriptors make it resemble a | |
65 | * CDC MDLM device, but they don't change device behavior at all. (See | |
66 | * MCCI Engineering report 950198 "SAFE Networking Functions".) | |
67 | * | |
68 | * A third option is also in use. Rather than CDC Ethernet, or something | |
69 | * simpler, Microsoft pushes their own approach: RNDIS. The published | |
70 | * RNDIS specs are ambiguous and appear to be incomplete, and are also | |
71 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. | |
72 | */ | |
73 | #define ETH_ALEN 6 /* Octets in one ethernet addr */ | |
74 | #define ETH_HLEN 14 /* Total octets in header. */ | |
75 | #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */ | |
76 | #define ETH_DATA_LEN 1500 /* Max. octets in payload */ | |
77 | #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */ | |
23cd1385 RB |
78 | |
79 | #define DRIVER_DESC "Ethernet Gadget" | |
80 | /* Based on linux 2.6.27 version */ | |
81 | #define DRIVER_VERSION "May Day 2005" | |
82 | ||
6142e0ae VK |
83 | static const char shortname[] = "ether"; |
84 | static const char driver_desc[] = DRIVER_DESC; | |
23cd1385 RB |
85 | |
86 | #define RX_EXTRA 20 /* guard against rx overflows */ | |
87 | ||
7612a43d VK |
88 | #ifndef CONFIG_USB_ETH_RNDIS |
89 | #define rndis_uninit(x) do {} while (0) | |
90 | #define rndis_deregister(c) do {} while (0) | |
91 | #define rndis_exit() do {} while (0) | |
92 | #endif | |
93 | ||
94 | /* CDC and RNDIS support the same host-chosen outgoing packet filters. */ | |
23cd1385 RB |
95 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ |
96 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ | |
97 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ | |
98 | |USB_CDC_PACKET_TYPE_DIRECTED) | |
99 | ||
100 | #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) | |
101 | ||
8b6b66b4 VK |
102 | /*-------------------------------------------------------------------------*/ |
103 | ||
104 | struct eth_dev { | |
105 | struct usb_gadget *gadget; | |
106 | struct usb_request *req; /* for control responses */ | |
7612a43d | 107 | struct usb_request *stat_req; /* for cdc & rndis status */ |
d4345aee M |
108 | #ifdef CONFIG_DM_USB |
109 | struct udevice *usb_udev; | |
110 | #endif | |
8b6b66b4 VK |
111 | |
112 | u8 config; | |
113 | struct usb_ep *in_ep, *out_ep, *status_ep; | |
114 | const struct usb_endpoint_descriptor | |
115 | *in, *out, *status; | |
116 | ||
117 | struct usb_request *tx_req, *rx_req; | |
118 | ||
119 | struct eth_device *net; | |
120 | struct net_device_stats stats; | |
121 | unsigned int tx_qlen; | |
122 | ||
123 | unsigned zlp:1; | |
124 | unsigned cdc:1; | |
7612a43d | 125 | unsigned rndis:1; |
8b6b66b4 VK |
126 | unsigned suspended:1; |
127 | unsigned network_started:1; | |
128 | u16 cdc_filter; | |
129 | unsigned long todo; | |
130 | int mtu; | |
131 | #define WORK_RX_MEMORY 0 | |
7612a43d | 132 | int rndis_config; |
8b6b66b4 VK |
133 | u8 host_mac[ETH_ALEN]; |
134 | }; | |
135 | ||
136 | /* | |
137 | * This version autoconfigures as much as possible at run-time. | |
138 | * | |
139 | * It also ASSUMES a self-powered device, without remote wakeup, | |
140 | * although remote wakeup support would make sense. | |
141 | */ | |
142 | ||
23cd1385 | 143 | /*-------------------------------------------------------------------------*/ |
5cb3b9d7 M |
144 | struct ether_priv { |
145 | struct eth_dev ethdev; | |
146 | struct eth_device netdev; | |
147 | struct usb_gadget_driver eth_driver; | |
148 | }; | |
149 | ||
150 | struct ether_priv eth_priv; | |
151 | struct ether_priv *l_priv = ð_priv; | |
23cd1385 RB |
152 | |
153 | /*-------------------------------------------------------------------------*/ | |
154 | ||
155 | /* "main" config is either CDC, or its simple subset */ | |
156 | static inline int is_cdc(struct eth_dev *dev) | |
157 | { | |
2bb37884 | 158 | #if !defined(CONFIG_USB_ETH_SUBSET) |
23cd1385 | 159 | return 1; /* only cdc possible */ |
2bb37884 | 160 | #elif !defined(CONFIG_USB_ETH_CDC) |
23cd1385 RB |
161 | return 0; /* only subset possible */ |
162 | #else | |
163 | return dev->cdc; /* depends on what hardware we found */ | |
164 | #endif | |
165 | } | |
166 | ||
7612a43d VK |
167 | /* "secondary" RNDIS config may sometimes be activated */ |
168 | static inline int rndis_active(struct eth_dev *dev) | |
169 | { | |
170 | #ifdef CONFIG_USB_ETH_RNDIS | |
171 | return dev->rndis; | |
172 | #else | |
173 | return 0; | |
174 | #endif | |
175 | } | |
176 | ||
177 | #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev)) | |
178 | #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev)) | |
23cd1385 RB |
179 | |
180 | #define DEFAULT_QLEN 2 /* double buffering by default */ | |
181 | ||
182 | /* peak bulk transfer bits-per-second */ | |
183 | #define HS_BPS (13 * 512 * 8 * 1000 * 8) | |
184 | #define FS_BPS (19 * 64 * 1 * 1000 * 8) | |
185 | ||
186 | #ifdef CONFIG_USB_GADGET_DUALSPEED | |
187 | #define DEVSPEED USB_SPEED_HIGH | |
188 | ||
2721dbf1 VK |
189 | #ifdef CONFIG_USB_ETH_QMULT |
190 | #define qmult CONFIG_USB_ETH_QMULT | |
191 | #else | |
192 | #define qmult 5 | |
193 | #endif | |
194 | ||
23cd1385 RB |
195 | /* for dual-speed hardware, use deeper queues at highspeed */ |
196 | #define qlen(gadget) \ | |
197 | (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) | |
198 | ||
199 | static inline int BITRATE(struct usb_gadget *g) | |
200 | { | |
201 | return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; | |
202 | } | |
203 | ||
204 | #else /* full speed (low speed doesn't do bulk) */ | |
205 | ||
206 | #define qmult 1 | |
207 | ||
208 | #define DEVSPEED USB_SPEED_FULL | |
209 | ||
210 | #define qlen(gadget) DEFAULT_QLEN | |
211 | ||
212 | static inline int BITRATE(struct usb_gadget *g) | |
213 | { | |
214 | return FS_BPS; | |
215 | } | |
216 | #endif | |
217 | ||
23cd1385 RB |
218 | /*-------------------------------------------------------------------------*/ |
219 | ||
6142e0ae VK |
220 | /* |
221 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
23cd1385 RB |
222 | * Instead: allocate your own, using normal USB-IF procedures. |
223 | */ | |
224 | ||
6142e0ae VK |
225 | /* |
226 | * Thanks to NetChip Technologies for donating this product ID. | |
23cd1385 RB |
227 | * It's for devices with only CDC Ethernet configurations. |
228 | */ | |
229 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ | |
230 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ | |
231 | ||
6142e0ae VK |
232 | /* |
233 | * For hardware that can't talk CDC, we use the same vendor ID that | |
23cd1385 RB |
234 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and |
235 | * with pxa250. We're protocol-compatible, if the host-side drivers | |
236 | * use the endpoint descriptors. bcdDevice (version) is nonzero, so | |
237 | * drivers that need to hard-wire endpoint numbers have a hook. | |
238 | * | |
239 | * The protocol is a minimal subset of CDC Ether, which works on any bulk | |
240 | * hardware that's not deeply broken ... even on hardware that can't talk | |
241 | * RNDIS (like SA-1100, with no interrupt endpoint, or anything that | |
242 | * doesn't handle control-OUT). | |
243 | */ | |
7612a43d VK |
244 | #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */ |
245 | #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */ | |
246 | ||
247 | /* | |
248 | * For hardware that can talk RNDIS and either of the above protocols, | |
249 | * use this ID ... the windows INF files will know it. Unless it's | |
250 | * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose | |
251 | * the non-RNDIS configuration. | |
252 | */ | |
253 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ | |
254 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ | |
23cd1385 | 255 | |
6142e0ae VK |
256 | /* |
257 | * Some systems will want different product identifers published in the | |
23cd1385 RB |
258 | * device descriptor, either numbers or strings or both. These string |
259 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). | |
260 | */ | |
261 | ||
7612a43d VK |
262 | /* |
263 | * Emulating them in eth_bind: | |
264 | * static ushort idVendor; | |
265 | * static ushort idProduct; | |
266 | */ | |
267 | ||
23cd1385 RB |
268 | #if defined(CONFIG_USBNET_MANUFACTURER) |
269 | static char *iManufacturer = CONFIG_USBNET_MANUFACTURER; | |
270 | #else | |
a187559e | 271 | static char *iManufacturer = "U-Boot"; |
23cd1385 | 272 | #endif |
7612a43d VK |
273 | |
274 | /* These probably need to be configurable. */ | |
275 | static ushort bcdDevice; | |
23cd1385 RB |
276 | static char *iProduct; |
277 | static char *iSerialNumber; | |
7612a43d | 278 | |
23cd1385 | 279 | static char dev_addr[18]; |
7612a43d | 280 | |
23cd1385 RB |
281 | static char host_addr[18]; |
282 | ||
7612a43d | 283 | |
23cd1385 RB |
284 | /*-------------------------------------------------------------------------*/ |
285 | ||
6142e0ae VK |
286 | /* |
287 | * USB DRIVER HOOKUP (to the hardware driver, below us), mostly | |
23cd1385 RB |
288 | * ep0 implementation: descriptors, config management, setup(). |
289 | * also optional class-specific notification interrupt transfer. | |
290 | */ | |
291 | ||
292 | /* | |
293 | * DESCRIPTORS ... most are static, but strings and (full) configuration | |
294 | * descriptors are built on demand. For now we do either full CDC, or | |
7612a43d VK |
295 | * our simple subset, with RNDIS as an optional second configuration. |
296 | * | |
297 | * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But | |
298 | * the class descriptors match a modem (they're ignored; it's really just | |
299 | * Ethernet functionality), they don't need the NOP altsetting, and the | |
300 | * status transfer endpoint isn't optional. | |
23cd1385 RB |
301 | */ |
302 | ||
303 | #define STRING_MANUFACTURER 1 | |
304 | #define STRING_PRODUCT 2 | |
305 | #define STRING_ETHADDR 3 | |
306 | #define STRING_DATA 4 | |
307 | #define STRING_CONTROL 5 | |
7612a43d | 308 | #define STRING_RNDIS_CONTROL 6 |
23cd1385 RB |
309 | #define STRING_CDC 7 |
310 | #define STRING_SUBSET 8 | |
7612a43d | 311 | #define STRING_RNDIS 9 |
23cd1385 RB |
312 | #define STRING_SERIALNUMBER 10 |
313 | ||
7612a43d | 314 | /* holds our biggest descriptor (or RNDIS response) */ |
23cd1385 RB |
315 | #define USB_BUFSIZ 256 |
316 | ||
317 | /* | |
7612a43d VK |
318 | * This device advertises one configuration, eth_config, unless RNDIS |
319 | * is enabled (rndis_config) on hardware supporting at least two configs. | |
320 | * | |
321 | * NOTE: Controllers like superh_udc should probably be able to use | |
322 | * an RNDIS-only configuration. | |
23cd1385 RB |
323 | * |
324 | * FIXME define some higher-powered configurations to make it easier | |
325 | * to recharge batteries ... | |
326 | */ | |
327 | ||
328 | #define DEV_CONFIG_VALUE 1 /* cdc or subset */ | |
7612a43d | 329 | #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */ |
23cd1385 RB |
330 | |
331 | static struct usb_device_descriptor | |
332 | device_desc = { | |
333 | .bLength = sizeof device_desc, | |
334 | .bDescriptorType = USB_DT_DEVICE, | |
335 | ||
6142e0ae | 336 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
23cd1385 RB |
337 | |
338 | .bDeviceClass = USB_CLASS_COMM, | |
339 | .bDeviceSubClass = 0, | |
340 | .bDeviceProtocol = 0, | |
341 | ||
6142e0ae VK |
342 | .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), |
343 | .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), | |
23cd1385 RB |
344 | .iManufacturer = STRING_MANUFACTURER, |
345 | .iProduct = STRING_PRODUCT, | |
346 | .bNumConfigurations = 1, | |
347 | }; | |
348 | ||
349 | static struct usb_otg_descriptor | |
350 | otg_descriptor = { | |
351 | .bLength = sizeof otg_descriptor, | |
352 | .bDescriptorType = USB_DT_OTG, | |
353 | ||
354 | .bmAttributes = USB_OTG_SRP, | |
355 | }; | |
356 | ||
357 | static struct usb_config_descriptor | |
358 | eth_config = { | |
359 | .bLength = sizeof eth_config, | |
360 | .bDescriptorType = USB_DT_CONFIG, | |
361 | ||
362 | /* compute wTotalLength on the fly */ | |
363 | .bNumInterfaces = 2, | |
364 | .bConfigurationValue = DEV_CONFIG_VALUE, | |
365 | .iConfiguration = STRING_CDC, | |
366 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | |
367 | .bMaxPower = 1, | |
368 | }; | |
369 | ||
7612a43d VK |
370 | #ifdef CONFIG_USB_ETH_RNDIS |
371 | static struct usb_config_descriptor | |
372 | rndis_config = { | |
373 | .bLength = sizeof rndis_config, | |
374 | .bDescriptorType = USB_DT_CONFIG, | |
375 | ||
376 | /* compute wTotalLength on the fly */ | |
377 | .bNumInterfaces = 2, | |
378 | .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE, | |
379 | .iConfiguration = STRING_RNDIS, | |
380 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | |
381 | .bMaxPower = 1, | |
382 | }; | |
383 | #endif | |
384 | ||
23cd1385 RB |
385 | /* |
386 | * Compared to the simple CDC subset, the full CDC Ethernet model adds | |
387 | * three class descriptors, two interface descriptors, optional status | |
388 | * endpoint. Both have a "data" interface and two bulk endpoints. | |
389 | * There are also differences in how control requests are handled. | |
7612a43d VK |
390 | * |
391 | * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the | |
392 | * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it | |
393 | * may hang or oops. Since bugfixes (or accurate specs, letting Linux | |
394 | * work around those bugs) are unlikely to ever come from MSFT, you may | |
395 | * wish to avoid using RNDIS. | |
396 | * | |
397 | * MCCI offers an alternative to RNDIS if you need to connect to Windows | |
398 | * but have hardware that can't support CDC Ethernet. We add descriptors | |
399 | * to present the CDC Subset as a (nonconformant) CDC MDLM variant called | |
400 | * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can | |
401 | * get those drivers from MCCI, or bundled with various products. | |
23cd1385 RB |
402 | */ |
403 | ||
2bb37884 | 404 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 RB |
405 | static struct usb_interface_descriptor |
406 | control_intf = { | |
407 | .bLength = sizeof control_intf, | |
408 | .bDescriptorType = USB_DT_INTERFACE, | |
409 | ||
410 | .bInterfaceNumber = 0, | |
411 | /* status endpoint is optional; this may be patched later */ | |
412 | .bNumEndpoints = 1, | |
413 | .bInterfaceClass = USB_CLASS_COMM, | |
414 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, | |
415 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, | |
416 | .iInterface = STRING_CONTROL, | |
417 | }; | |
418 | #endif | |
419 | ||
7612a43d VK |
420 | #ifdef CONFIG_USB_ETH_RNDIS |
421 | static const struct usb_interface_descriptor | |
422 | rndis_control_intf = { | |
423 | .bLength = sizeof rndis_control_intf, | |
424 | .bDescriptorType = USB_DT_INTERFACE, | |
425 | ||
426 | .bInterfaceNumber = 0, | |
427 | .bNumEndpoints = 1, | |
428 | .bInterfaceClass = USB_CLASS_COMM, | |
429 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, | |
430 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, | |
431 | .iInterface = STRING_RNDIS_CONTROL, | |
432 | }; | |
433 | #endif | |
434 | ||
23cd1385 RB |
435 | static const struct usb_cdc_header_desc header_desc = { |
436 | .bLength = sizeof header_desc, | |
437 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
438 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | |
439 | ||
6142e0ae | 440 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
23cd1385 RB |
441 | }; |
442 | ||
2bb37884 | 443 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
23cd1385 RB |
444 | |
445 | static const struct usb_cdc_union_desc union_desc = { | |
446 | .bLength = sizeof union_desc, | |
447 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
448 | .bDescriptorSubType = USB_CDC_UNION_TYPE, | |
449 | ||
450 | .bMasterInterface0 = 0, /* index of control interface */ | |
451 | .bSlaveInterface0 = 1, /* index of DATA interface */ | |
452 | }; | |
453 | ||
7612a43d VK |
454 | #endif /* CDC || RNDIS */ |
455 | ||
456 | #ifdef CONFIG_USB_ETH_RNDIS | |
457 | ||
458 | static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { | |
459 | .bLength = sizeof call_mgmt_descriptor, | |
460 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
461 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, | |
462 | ||
463 | .bmCapabilities = 0x00, | |
464 | .bDataInterface = 0x01, | |
465 | }; | |
466 | ||
467 | static const struct usb_cdc_acm_descriptor acm_descriptor = { | |
468 | .bLength = sizeof acm_descriptor, | |
469 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
470 | .bDescriptorSubType = USB_CDC_ACM_TYPE, | |
471 | ||
472 | .bmCapabilities = 0x00, | |
473 | }; | |
474 | ||
475 | #endif | |
23cd1385 | 476 | |
2bb37884 | 477 | #ifndef CONFIG_USB_ETH_CDC |
23cd1385 | 478 | |
6142e0ae VK |
479 | /* |
480 | * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various | |
23cd1385 RB |
481 | * ways: data endpoints live in the control interface, there's no data |
482 | * interface, and it's not used to talk to a cell phone radio. | |
483 | */ | |
484 | ||
485 | static const struct usb_cdc_mdlm_desc mdlm_desc = { | |
486 | .bLength = sizeof mdlm_desc, | |
487 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
488 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, | |
489 | ||
490 | .bcdVersion = __constant_cpu_to_le16(0x0100), | |
491 | .bGUID = { | |
492 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, | |
493 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, | |
494 | }, | |
495 | }; | |
496 | ||
6142e0ae VK |
497 | /* |
498 | * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we | |
23cd1385 RB |
499 | * can't really use its struct. All we do here is say that we're using |
500 | * the submode of "SAFE" which directly matches the CDC Subset. | |
501 | */ | |
502 | static const u8 mdlm_detail_desc[] = { | |
503 | 6, | |
504 | USB_DT_CS_INTERFACE, | |
505 | USB_CDC_MDLM_DETAIL_TYPE, | |
506 | ||
507 | 0, /* "SAFE" */ | |
508 | 0, /* network control capabilities (none) */ | |
509 | 0, /* network data capabilities ("raw" encapsulation) */ | |
510 | }; | |
511 | ||
512 | #endif | |
513 | ||
23cd1385 | 514 | static const struct usb_cdc_ether_desc ether_desc = { |
6142e0ae | 515 | .bLength = sizeof(ether_desc), |
23cd1385 RB |
516 | .bDescriptorType = USB_DT_CS_INTERFACE, |
517 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, | |
518 | ||
519 | /* this descriptor actually adds value, surprise! */ | |
520 | .iMACAddress = STRING_ETHADDR, | |
6142e0ae VK |
521 | .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ |
522 | .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), | |
523 | .wNumberMCFilters = __constant_cpu_to_le16(0), | |
23cd1385 RB |
524 | .bNumberPowerFilters = 0, |
525 | }; | |
526 | ||
2bb37884 | 527 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
23cd1385 | 528 | |
6142e0ae VK |
529 | /* |
530 | * include the status endpoint if we can, even where it's optional. | |
23cd1385 RB |
531 | * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one |
532 | * packet, to simplify cancellation; and a big transfer interval, to | |
533 | * waste less bandwidth. | |
534 | * | |
535 | * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even | |
536 | * if they ignore the connect/disconnect notifications that real aether | |
537 | * can provide. more advanced cdc configurations might want to support | |
538 | * encapsulated commands (vendor-specific, using control-OUT). | |
7612a43d VK |
539 | * |
540 | * RNDIS requires the status endpoint, since it uses that encapsulation | |
541 | * mechanism for its funky RPC scheme. | |
23cd1385 RB |
542 | */ |
543 | ||
544 | #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ | |
545 | #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ | |
546 | ||
547 | static struct usb_endpoint_descriptor | |
548 | fs_status_desc = { | |
549 | .bLength = USB_DT_ENDPOINT_SIZE, | |
550 | .bDescriptorType = USB_DT_ENDPOINT, | |
551 | ||
552 | .bEndpointAddress = USB_DIR_IN, | |
553 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
6142e0ae | 554 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
23cd1385 RB |
555 | .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, |
556 | }; | |
557 | #endif | |
558 | ||
2bb37884 | 559 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 RB |
560 | |
561 | /* the default data interface has no endpoints ... */ | |
562 | ||
563 | static const struct usb_interface_descriptor | |
564 | data_nop_intf = { | |
565 | .bLength = sizeof data_nop_intf, | |
566 | .bDescriptorType = USB_DT_INTERFACE, | |
567 | ||
568 | .bInterfaceNumber = 1, | |
569 | .bAlternateSetting = 0, | |
570 | .bNumEndpoints = 0, | |
571 | .bInterfaceClass = USB_CLASS_CDC_DATA, | |
572 | .bInterfaceSubClass = 0, | |
573 | .bInterfaceProtocol = 0, | |
574 | }; | |
575 | ||
576 | /* ... but the "real" data interface has two bulk endpoints */ | |
577 | ||
578 | static const struct usb_interface_descriptor | |
579 | data_intf = { | |
580 | .bLength = sizeof data_intf, | |
581 | .bDescriptorType = USB_DT_INTERFACE, | |
582 | ||
583 | .bInterfaceNumber = 1, | |
584 | .bAlternateSetting = 1, | |
585 | .bNumEndpoints = 2, | |
586 | .bInterfaceClass = USB_CLASS_CDC_DATA, | |
587 | .bInterfaceSubClass = 0, | |
588 | .bInterfaceProtocol = 0, | |
589 | .iInterface = STRING_DATA, | |
590 | }; | |
591 | ||
592 | #endif | |
593 | ||
7612a43d VK |
594 | #ifdef CONFIG_USB_ETH_RNDIS |
595 | ||
596 | /* RNDIS doesn't activate by changing to the "real" altsetting */ | |
597 | ||
598 | static const struct usb_interface_descriptor | |
599 | rndis_data_intf = { | |
600 | .bLength = sizeof rndis_data_intf, | |
601 | .bDescriptorType = USB_DT_INTERFACE, | |
602 | ||
603 | .bInterfaceNumber = 1, | |
604 | .bAlternateSetting = 0, | |
605 | .bNumEndpoints = 2, | |
606 | .bInterfaceClass = USB_CLASS_CDC_DATA, | |
607 | .bInterfaceSubClass = 0, | |
608 | .bInterfaceProtocol = 0, | |
609 | .iInterface = STRING_DATA, | |
610 | }; | |
611 | ||
612 | #endif | |
613 | ||
2bb37884 | 614 | #ifdef CONFIG_USB_ETH_SUBSET |
23cd1385 RB |
615 | |
616 | /* | |
617 | * "Simple" CDC-subset option is a simple vendor-neutral model that most | |
618 | * full speed controllers can handle: one interface, two bulk endpoints. | |
619 | * | |
620 | * To assist host side drivers, we fancy it up a bit, and add descriptors | |
621 | * so some host side drivers will understand it as a "SAFE" variant. | |
622 | */ | |
623 | ||
624 | static const struct usb_interface_descriptor | |
625 | subset_data_intf = { | |
626 | .bLength = sizeof subset_data_intf, | |
627 | .bDescriptorType = USB_DT_INTERFACE, | |
628 | ||
629 | .bInterfaceNumber = 0, | |
630 | .bAlternateSetting = 0, | |
631 | .bNumEndpoints = 2, | |
632 | .bInterfaceClass = USB_CLASS_COMM, | |
633 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, | |
634 | .bInterfaceProtocol = 0, | |
635 | .iInterface = STRING_DATA, | |
636 | }; | |
637 | ||
638 | #endif /* SUBSET */ | |
639 | ||
23cd1385 RB |
640 | static struct usb_endpoint_descriptor |
641 | fs_source_desc = { | |
642 | .bLength = USB_DT_ENDPOINT_SIZE, | |
643 | .bDescriptorType = USB_DT_ENDPOINT, | |
644 | ||
645 | .bEndpointAddress = USB_DIR_IN, | |
646 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
43880ce5 | 647 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
23cd1385 RB |
648 | }; |
649 | ||
650 | static struct usb_endpoint_descriptor | |
651 | fs_sink_desc = { | |
652 | .bLength = USB_DT_ENDPOINT_SIZE, | |
653 | .bDescriptorType = USB_DT_ENDPOINT, | |
654 | ||
655 | .bEndpointAddress = USB_DIR_OUT, | |
656 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
43880ce5 | 657 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
23cd1385 RB |
658 | }; |
659 | ||
6142e0ae | 660 | static const struct usb_descriptor_header *fs_eth_function[11] = { |
23cd1385 | 661 | (struct usb_descriptor_header *) &otg_descriptor, |
2bb37884 | 662 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 RB |
663 | /* "cdc" mode descriptors */ |
664 | (struct usb_descriptor_header *) &control_intf, | |
665 | (struct usb_descriptor_header *) &header_desc, | |
666 | (struct usb_descriptor_header *) &union_desc, | |
667 | (struct usb_descriptor_header *) ðer_desc, | |
668 | /* NOTE: status endpoint may need to be removed */ | |
669 | (struct usb_descriptor_header *) &fs_status_desc, | |
670 | /* data interface, with altsetting */ | |
671 | (struct usb_descriptor_header *) &data_nop_intf, | |
672 | (struct usb_descriptor_header *) &data_intf, | |
673 | (struct usb_descriptor_header *) &fs_source_desc, | |
674 | (struct usb_descriptor_header *) &fs_sink_desc, | |
675 | NULL, | |
2bb37884 | 676 | #endif /* CONFIG_USB_ETH_CDC */ |
23cd1385 RB |
677 | }; |
678 | ||
679 | static inline void fs_subset_descriptors(void) | |
680 | { | |
2bb37884 | 681 | #ifdef CONFIG_USB_ETH_SUBSET |
23cd1385 RB |
682 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
683 | fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; | |
684 | fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; | |
685 | fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; | |
686 | fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; | |
687 | fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; | |
688 | fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; | |
689 | fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; | |
690 | fs_eth_function[8] = NULL; | |
691 | #else | |
692 | fs_eth_function[1] = NULL; | |
693 | #endif | |
694 | } | |
695 | ||
7612a43d VK |
696 | #ifdef CONFIG_USB_ETH_RNDIS |
697 | static const struct usb_descriptor_header *fs_rndis_function[] = { | |
698 | (struct usb_descriptor_header *) &otg_descriptor, | |
699 | /* control interface matches ACM, not Ethernet */ | |
700 | (struct usb_descriptor_header *) &rndis_control_intf, | |
701 | (struct usb_descriptor_header *) &header_desc, | |
702 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | |
703 | (struct usb_descriptor_header *) &acm_descriptor, | |
704 | (struct usb_descriptor_header *) &union_desc, | |
705 | (struct usb_descriptor_header *) &fs_status_desc, | |
706 | /* data interface has no altsetting */ | |
707 | (struct usb_descriptor_header *) &rndis_data_intf, | |
708 | (struct usb_descriptor_header *) &fs_source_desc, | |
709 | (struct usb_descriptor_header *) &fs_sink_desc, | |
710 | NULL, | |
711 | }; | |
712 | #endif | |
713 | ||
23cd1385 RB |
714 | /* |
715 | * usb 2.0 devices need to expose both high speed and full speed | |
716 | * descriptors, unless they only run at full speed. | |
717 | */ | |
718 | ||
2bb37884 | 719 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
23cd1385 RB |
720 | static struct usb_endpoint_descriptor |
721 | hs_status_desc = { | |
722 | .bLength = USB_DT_ENDPOINT_SIZE, | |
723 | .bDescriptorType = USB_DT_ENDPOINT, | |
724 | ||
725 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
6142e0ae | 726 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
23cd1385 RB |
727 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, |
728 | }; | |
2bb37884 | 729 | #endif /* CONFIG_USB_ETH_CDC */ |
23cd1385 RB |
730 | |
731 | static struct usb_endpoint_descriptor | |
732 | hs_source_desc = { | |
733 | .bLength = USB_DT_ENDPOINT_SIZE, | |
734 | .bDescriptorType = USB_DT_ENDPOINT, | |
735 | ||
736 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
6142e0ae | 737 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
23cd1385 RB |
738 | }; |
739 | ||
740 | static struct usb_endpoint_descriptor | |
741 | hs_sink_desc = { | |
742 | .bLength = USB_DT_ENDPOINT_SIZE, | |
743 | .bDescriptorType = USB_DT_ENDPOINT, | |
744 | ||
745 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
6142e0ae | 746 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
23cd1385 RB |
747 | }; |
748 | ||
749 | static struct usb_qualifier_descriptor | |
750 | dev_qualifier = { | |
751 | .bLength = sizeof dev_qualifier, | |
752 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | |
753 | ||
6142e0ae | 754 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
23cd1385 RB |
755 | .bDeviceClass = USB_CLASS_COMM, |
756 | ||
757 | .bNumConfigurations = 1, | |
758 | }; | |
759 | ||
6142e0ae | 760 | static const struct usb_descriptor_header *hs_eth_function[11] = { |
23cd1385 | 761 | (struct usb_descriptor_header *) &otg_descriptor, |
2bb37884 | 762 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 RB |
763 | /* "cdc" mode descriptors */ |
764 | (struct usb_descriptor_header *) &control_intf, | |
765 | (struct usb_descriptor_header *) &header_desc, | |
766 | (struct usb_descriptor_header *) &union_desc, | |
767 | (struct usb_descriptor_header *) ðer_desc, | |
768 | /* NOTE: status endpoint may need to be removed */ | |
769 | (struct usb_descriptor_header *) &hs_status_desc, | |
770 | /* data interface, with altsetting */ | |
771 | (struct usb_descriptor_header *) &data_nop_intf, | |
772 | (struct usb_descriptor_header *) &data_intf, | |
773 | (struct usb_descriptor_header *) &hs_source_desc, | |
774 | (struct usb_descriptor_header *) &hs_sink_desc, | |
775 | NULL, | |
2bb37884 | 776 | #endif /* CONFIG_USB_ETH_CDC */ |
23cd1385 RB |
777 | }; |
778 | ||
779 | static inline void hs_subset_descriptors(void) | |
780 | { | |
2bb37884 | 781 | #ifdef CONFIG_USB_ETH_SUBSET |
23cd1385 RB |
782 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
783 | hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; | |
784 | hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; | |
785 | hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; | |
786 | hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; | |
787 | hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; | |
788 | hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; | |
789 | hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; | |
790 | hs_eth_function[8] = NULL; | |
791 | #else | |
792 | hs_eth_function[1] = NULL; | |
793 | #endif | |
794 | } | |
795 | ||
7612a43d VK |
796 | #ifdef CONFIG_USB_ETH_RNDIS |
797 | static const struct usb_descriptor_header *hs_rndis_function[] = { | |
798 | (struct usb_descriptor_header *) &otg_descriptor, | |
799 | /* control interface matches ACM, not Ethernet */ | |
800 | (struct usb_descriptor_header *) &rndis_control_intf, | |
801 | (struct usb_descriptor_header *) &header_desc, | |
802 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | |
803 | (struct usb_descriptor_header *) &acm_descriptor, | |
804 | (struct usb_descriptor_header *) &union_desc, | |
805 | (struct usb_descriptor_header *) &hs_status_desc, | |
806 | /* data interface has no altsetting */ | |
807 | (struct usb_descriptor_header *) &rndis_data_intf, | |
808 | (struct usb_descriptor_header *) &hs_source_desc, | |
809 | (struct usb_descriptor_header *) &hs_sink_desc, | |
810 | NULL, | |
811 | }; | |
812 | #endif | |
813 | ||
814 | ||
23cd1385 RB |
815 | /* maxpacket and other transfer characteristics vary by speed. */ |
816 | static inline struct usb_endpoint_descriptor * | |
817 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, | |
818 | struct usb_endpoint_descriptor *fs) | |
819 | { | |
820 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | |
821 | return hs; | |
822 | return fs; | |
823 | } | |
824 | ||
23cd1385 RB |
825 | /*-------------------------------------------------------------------------*/ |
826 | ||
827 | /* descriptors that are built on-demand */ | |
828 | ||
6142e0ae VK |
829 | static char manufacturer[50]; |
830 | static char product_desc[40] = DRIVER_DESC; | |
831 | static char serial_number[20]; | |
23cd1385 RB |
832 | |
833 | /* address that the host will use ... usually assigned at random */ | |
6142e0ae | 834 | static char ethaddr[2 * ETH_ALEN + 1]; |
23cd1385 RB |
835 | |
836 | /* static strings, in UTF-8 */ | |
6142e0ae | 837 | static struct usb_string strings[] = { |
23cd1385 RB |
838 | { STRING_MANUFACTURER, manufacturer, }, |
839 | { STRING_PRODUCT, product_desc, }, | |
840 | { STRING_SERIALNUMBER, serial_number, }, | |
841 | { STRING_DATA, "Ethernet Data", }, | |
842 | { STRING_ETHADDR, ethaddr, }, | |
2bb37884 | 843 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 RB |
844 | { STRING_CDC, "CDC Ethernet", }, |
845 | { STRING_CONTROL, "CDC Communications Control", }, | |
846 | #endif | |
2bb37884 | 847 | #ifdef CONFIG_USB_ETH_SUBSET |
23cd1385 | 848 | { STRING_SUBSET, "CDC Ethernet Subset", }, |
7612a43d VK |
849 | #endif |
850 | #ifdef CONFIG_USB_ETH_RNDIS | |
851 | { STRING_RNDIS, "RNDIS", }, | |
852 | { STRING_RNDIS_CONTROL, "RNDIS Communications Control", }, | |
23cd1385 RB |
853 | #endif |
854 | { } /* end of list */ | |
855 | }; | |
856 | ||
857 | static struct usb_gadget_strings stringtab = { | |
858 | .language = 0x0409, /* en-us */ | |
859 | .strings = strings, | |
860 | }; | |
861 | ||
23cd1385 | 862 | /*============================================================================*/ |
5290759c JF |
863 | DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ); |
864 | ||
2bb37884 | 865 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
5290759c | 866 | DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT); |
563aed25 | 867 | #endif |
23cd1385 | 868 | |
23cd1385 RB |
869 | /*============================================================================*/ |
870 | ||
871 | /* | |
872 | * one config, two interfaces: control, data. | |
873 | * complications: class descriptors, and an altsetting. | |
874 | */ | |
875 | static int | |
876 | config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) | |
877 | { | |
878 | int len; | |
879 | const struct usb_config_descriptor *config; | |
880 | const struct usb_descriptor_header **function; | |
881 | int hs = 0; | |
882 | ||
883 | if (gadget_is_dualspeed(g)) { | |
884 | hs = (g->speed == USB_SPEED_HIGH); | |
885 | if (type == USB_DT_OTHER_SPEED_CONFIG) | |
886 | hs = !hs; | |
887 | } | |
888 | #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) | |
889 | ||
890 | if (index >= device_desc.bNumConfigurations) | |
891 | return -EINVAL; | |
892 | ||
7612a43d VK |
893 | #ifdef CONFIG_USB_ETH_RNDIS |
894 | /* | |
895 | * list the RNDIS config first, to make Microsoft's drivers | |
896 | * happy. DOCSIS 1.0 needs this too. | |
897 | */ | |
898 | if (device_desc.bNumConfigurations == 2 && index == 0) { | |
899 | config = &rndis_config; | |
900 | function = which_fn(rndis); | |
901 | } else | |
902 | #endif | |
903 | { | |
904 | config = ð_config; | |
905 | function = which_fn(eth); | |
906 | } | |
23cd1385 RB |
907 | |
908 | /* for now, don't advertise srp-only devices */ | |
909 | if (!is_otg) | |
910 | function++; | |
911 | ||
6142e0ae | 912 | len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); |
23cd1385 RB |
913 | if (len < 0) |
914 | return len; | |
915 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; | |
916 | return len; | |
917 | } | |
918 | ||
919 | /*-------------------------------------------------------------------------*/ | |
920 | ||
7612a43d | 921 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags); |
6142e0ae | 922 | static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); |
23cd1385 RB |
923 | |
924 | static int | |
6142e0ae | 925 | set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) |
23cd1385 RB |
926 | { |
927 | int result = 0; | |
928 | struct usb_gadget *gadget = dev->gadget; | |
929 | ||
2bb37884 | 930 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
7612a43d | 931 | /* status endpoint used for RNDIS and (optionally) CDC */ |
23cd1385 | 932 | if (!subset_active(dev) && dev->status_ep) { |
6142e0ae | 933 | dev->status = ep_desc(gadget, &hs_status_desc, |
23cd1385 RB |
934 | &fs_status_desc); |
935 | dev->status_ep->driver_data = dev; | |
936 | ||
6142e0ae | 937 | result = usb_ep_enable(dev->status_ep, dev->status); |
23cd1385 | 938 | if (result != 0) { |
7de73185 | 939 | debug("enable %s --> %d\n", |
23cd1385 RB |
940 | dev->status_ep->name, result); |
941 | goto done; | |
942 | } | |
943 | } | |
944 | #endif | |
945 | ||
946 | dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); | |
947 | dev->in_ep->driver_data = dev; | |
948 | ||
949 | dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); | |
950 | dev->out_ep->driver_data = dev; | |
951 | ||
6142e0ae VK |
952 | /* |
953 | * With CDC, the host isn't allowed to use these two data | |
23cd1385 RB |
954 | * endpoints in the default altsetting for the interface. |
955 | * so we don't activate them yet. Reset from SET_INTERFACE. | |
7612a43d VK |
956 | * |
957 | * Strictly speaking RNDIS should work the same: activation is | |
958 | * a side effect of setting a packet filter. Deactivation is | |
959 | * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG. | |
23cd1385 RB |
960 | */ |
961 | if (!cdc_active(dev)) { | |
6142e0ae | 962 | result = usb_ep_enable(dev->in_ep, dev->in); |
23cd1385 | 963 | if (result != 0) { |
7de73185 | 964 | debug("enable %s --> %d\n", |
23cd1385 RB |
965 | dev->in_ep->name, result); |
966 | goto done; | |
967 | } | |
968 | ||
6142e0ae | 969 | result = usb_ep_enable(dev->out_ep, dev->out); |
23cd1385 | 970 | if (result != 0) { |
7de73185 | 971 | debug("enable %s --> %d\n", |
23cd1385 RB |
972 | dev->out_ep->name, result); |
973 | goto done; | |
974 | } | |
975 | } | |
976 | ||
977 | done: | |
978 | if (result == 0) | |
6142e0ae | 979 | result = alloc_requests(dev, qlen(gadget), gfp_flags); |
23cd1385 RB |
980 | |
981 | /* on error, disable any endpoints */ | |
982 | if (result < 0) { | |
d5292c16 | 983 | if (!subset_active(dev) && dev->status_ep) |
6142e0ae | 984 | (void) usb_ep_disable(dev->status_ep); |
23cd1385 | 985 | dev->status = NULL; |
6142e0ae VK |
986 | (void) usb_ep_disable(dev->in_ep); |
987 | (void) usb_ep_disable(dev->out_ep); | |
23cd1385 RB |
988 | dev->in = NULL; |
989 | dev->out = NULL; | |
7612a43d VK |
990 | } else if (!cdc_active(dev)) { |
991 | /* | |
992 | * activate non-CDC configs right away | |
993 | * this isn't strictly according to the RNDIS spec | |
994 | */ | |
995 | eth_start(dev, GFP_ATOMIC); | |
23cd1385 RB |
996 | } |
997 | ||
998 | /* caller is responsible for cleanup on error */ | |
999 | return result; | |
1000 | } | |
1001 | ||
6142e0ae | 1002 | static void eth_reset_config(struct eth_dev *dev) |
23cd1385 RB |
1003 | { |
1004 | if (dev->config == 0) | |
1005 | return; | |
1006 | ||
7de73185 VK |
1007 | debug("%s\n", __func__); |
1008 | ||
7612a43d VK |
1009 | rndis_uninit(dev->rndis_config); |
1010 | ||
6142e0ae VK |
1011 | /* |
1012 | * disable endpoints, forcing (synchronous) completion of | |
23cd1385 RB |
1013 | * pending i/o. then free the requests. |
1014 | */ | |
1015 | ||
1016 | if (dev->in) { | |
6142e0ae | 1017 | usb_ep_disable(dev->in_ep); |
23cd1385 | 1018 | if (dev->tx_req) { |
6142e0ae VK |
1019 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
1020 | dev->tx_req = NULL; | |
23cd1385 RB |
1021 | } |
1022 | } | |
1023 | if (dev->out) { | |
6142e0ae | 1024 | usb_ep_disable(dev->out_ep); |
23cd1385 | 1025 | if (dev->rx_req) { |
6142e0ae VK |
1026 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
1027 | dev->rx_req = NULL; | |
23cd1385 RB |
1028 | } |
1029 | } | |
6142e0ae VK |
1030 | if (dev->status) |
1031 | usb_ep_disable(dev->status_ep); | |
1032 | ||
7612a43d | 1033 | dev->rndis = 0; |
23cd1385 RB |
1034 | dev->cdc_filter = 0; |
1035 | dev->config = 0; | |
1036 | } | |
1037 | ||
6142e0ae VK |
1038 | /* |
1039 | * change our operational config. must agree with the code | |
23cd1385 RB |
1040 | * that returns config descriptors, and altsetting code. |
1041 | */ | |
6142e0ae VK |
1042 | static int eth_set_config(struct eth_dev *dev, unsigned number, |
1043 | gfp_t gfp_flags) | |
23cd1385 RB |
1044 | { |
1045 | int result = 0; | |
1046 | struct usb_gadget *gadget = dev->gadget; | |
1047 | ||
6142e0ae | 1048 | if (gadget_is_sa1100(gadget) |
23cd1385 RB |
1049 | && dev->config |
1050 | && dev->tx_qlen != 0) { | |
1051 | /* tx fifo is full, but we can't clear it...*/ | |
7de73185 | 1052 | error("can't change configurations"); |
23cd1385 RB |
1053 | return -ESPIPE; |
1054 | } | |
6142e0ae | 1055 | eth_reset_config(dev); |
23cd1385 RB |
1056 | |
1057 | switch (number) { | |
1058 | case DEV_CONFIG_VALUE: | |
6142e0ae | 1059 | result = set_ether_config(dev, gfp_flags); |
23cd1385 | 1060 | break; |
7612a43d VK |
1061 | #ifdef CONFIG_USB_ETH_RNDIS |
1062 | case DEV_RNDIS_CONFIG_VALUE: | |
1063 | dev->rndis = 1; | |
1064 | result = set_ether_config(dev, gfp_flags); | |
1065 | break; | |
1066 | #endif | |
23cd1385 RB |
1067 | default: |
1068 | result = -EINVAL; | |
1069 | /* FALL THROUGH */ | |
1070 | case 0: | |
1071 | break; | |
1072 | } | |
1073 | ||
1074 | if (result) { | |
1075 | if (number) | |
6142e0ae | 1076 | eth_reset_config(dev); |
23cd1385 RB |
1077 | usb_gadget_vbus_draw(dev->gadget, |
1078 | gadget_is_otg(dev->gadget) ? 8 : 100); | |
1079 | } else { | |
1080 | char *speed; | |
1081 | unsigned power; | |
1082 | ||
1083 | power = 2 * eth_config.bMaxPower; | |
1084 | usb_gadget_vbus_draw(dev->gadget, power); | |
1085 | ||
1086 | switch (gadget->speed) { | |
6142e0ae VK |
1087 | case USB_SPEED_FULL: |
1088 | speed = "full"; break; | |
23cd1385 | 1089 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
6142e0ae VK |
1090 | case USB_SPEED_HIGH: |
1091 | speed = "high"; break; | |
23cd1385 | 1092 | #endif |
6142e0ae VK |
1093 | default: |
1094 | speed = "?"; break; | |
23cd1385 RB |
1095 | } |
1096 | ||
1097 | dev->config = number; | |
7de73185 | 1098 | printf("%s speed config #%d: %d mA, %s, using %s\n", |
23cd1385 | 1099 | speed, number, power, driver_desc, |
7612a43d VK |
1100 | rndis_active(dev) |
1101 | ? "RNDIS" | |
1102 | : (cdc_active(dev) | |
1103 | ? "CDC Ethernet" | |
23cd1385 RB |
1104 | : "CDC Ethernet Subset")); |
1105 | } | |
1106 | return result; | |
1107 | } | |
1108 | ||
1109 | /*-------------------------------------------------------------------------*/ | |
1110 | ||
2bb37884 | 1111 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 | 1112 | |
6142e0ae VK |
1113 | /* |
1114 | * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) | |
23cd1385 | 1115 | * only to notify the host about link status changes (which we support) or |
7612a43d | 1116 | * report completion of some encapsulated command (as used in RNDIS). Since |
23cd1385 RB |
1117 | * we want this CDC Ethernet code to be vendor-neutral, we don't use that |
1118 | * command mechanism; and only one status request is ever queued. | |
1119 | */ | |
6142e0ae | 1120 | static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) |
23cd1385 RB |
1121 | { |
1122 | struct usb_cdc_notification *event = req->buf; | |
1123 | int value = req->status; | |
1124 | struct eth_dev *dev = ep->driver_data; | |
1125 | ||
1126 | /* issue the second notification if host reads the first */ | |
1127 | if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION | |
1128 | && value == 0) { | |
1129 | __le32 *data = req->buf + sizeof *event; | |
1130 | ||
1131 | event->bmRequestType = 0xA1; | |
1132 | event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; | |
6142e0ae VK |
1133 | event->wValue = __constant_cpu_to_le16(0); |
1134 | event->wIndex = __constant_cpu_to_le16(1); | |
1135 | event->wLength = __constant_cpu_to_le16(8); | |
23cd1385 RB |
1136 | |
1137 | /* SPEED_CHANGE data is up/down speeds in bits/sec */ | |
6142e0ae | 1138 | data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); |
23cd1385 RB |
1139 | |
1140 | req->length = STATUS_BYTECOUNT; | |
6142e0ae | 1141 | value = usb_ep_queue(ep, req, GFP_ATOMIC); |
7de73185 | 1142 | debug("send SPEED_CHANGE --> %d\n", value); |
23cd1385 RB |
1143 | if (value == 0) |
1144 | return; | |
1145 | } else if (value != -ECONNRESET) { | |
7de73185 | 1146 | debug("event %02x --> %d\n", |
23cd1385 | 1147 | event->bNotificationType, value); |
6142e0ae VK |
1148 | if (event->bNotificationType == |
1149 | USB_CDC_NOTIFY_SPEED_CHANGE) { | |
17b4f308 | 1150 | dev->network_started = 1; |
23cd1385 RB |
1151 | printf("USB network up!\n"); |
1152 | } | |
1153 | } | |
1154 | req->context = NULL; | |
1155 | } | |
1156 | ||
6142e0ae | 1157 | static void issue_start_status(struct eth_dev *dev) |
23cd1385 RB |
1158 | { |
1159 | struct usb_request *req = dev->stat_req; | |
1160 | struct usb_cdc_notification *event; | |
1161 | int value; | |
1162 | ||
6142e0ae VK |
1163 | /* |
1164 | * flush old status | |
23cd1385 RB |
1165 | * |
1166 | * FIXME ugly idiom, maybe we'd be better with just | |
1167 | * a "cancel the whole queue" primitive since any | |
1168 | * unlink-one primitive has way too many error modes. | |
1169 | * here, we "know" toggle is already clear... | |
1170 | * | |
1171 | * FIXME iff req->context != null just dequeue it | |
1172 | */ | |
6142e0ae VK |
1173 | usb_ep_disable(dev->status_ep); |
1174 | usb_ep_enable(dev->status_ep, dev->status); | |
23cd1385 | 1175 | |
6142e0ae VK |
1176 | /* |
1177 | * 3.8.1 says to issue first NETWORK_CONNECTION, then | |
23cd1385 RB |
1178 | * a SPEED_CHANGE. could be useful in some configs. |
1179 | */ | |
1180 | event = req->buf; | |
1181 | event->bmRequestType = 0xA1; | |
1182 | event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; | |
6142e0ae VK |
1183 | event->wValue = __constant_cpu_to_le16(1); /* connected */ |
1184 | event->wIndex = __constant_cpu_to_le16(1); | |
23cd1385 RB |
1185 | event->wLength = 0; |
1186 | ||
1187 | req->length = sizeof *event; | |
1188 | req->complete = eth_status_complete; | |
1189 | req->context = dev; | |
1190 | ||
6142e0ae | 1191 | value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); |
23cd1385 | 1192 | if (value < 0) |
7de73185 | 1193 | debug("status buf queue --> %d\n", value); |
23cd1385 RB |
1194 | } |
1195 | ||
1196 | #endif | |
1197 | ||
1198 | /*-------------------------------------------------------------------------*/ | |
1199 | ||
6142e0ae | 1200 | static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) |
23cd1385 RB |
1201 | { |
1202 | if (req->status || req->actual != req->length) | |
7de73185 | 1203 | debug("setup complete --> %d, %d/%d\n", |
23cd1385 RB |
1204 | req->status, req->actual, req->length); |
1205 | } | |
1206 | ||
7612a43d VK |
1207 | #ifdef CONFIG_USB_ETH_RNDIS |
1208 | ||
1209 | static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) | |
1210 | { | |
1211 | if (req->status || req->actual != req->length) | |
1212 | debug("rndis response complete --> %d, %d/%d\n", | |
1213 | req->status, req->actual, req->length); | |
1214 | ||
1215 | /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */ | |
1216 | } | |
1217 | ||
1218 | static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) | |
1219 | { | |
1220 | struct eth_dev *dev = ep->driver_data; | |
1221 | int status; | |
1222 | ||
1223 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ | |
1224 | status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf); | |
1225 | if (status < 0) | |
1226 | error("%s: rndis parse error %d", __func__, status); | |
1227 | } | |
1228 | ||
1229 | #endif /* RNDIS */ | |
1230 | ||
23cd1385 RB |
1231 | /* |
1232 | * The setup() callback implements all the ep0 functionality that's not | |
1233 | * handled lower down. CDC has a number of less-common features: | |
1234 | * | |
1235 | * - two interfaces: control, and ethernet data | |
1236 | * - Ethernet data interface has two altsettings: default, and active | |
1237 | * - class-specific descriptors for the control interface | |
1238 | * - class-specific control requests | |
1239 | */ | |
1240 | static int | |
6142e0ae | 1241 | eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
23cd1385 | 1242 | { |
6142e0ae | 1243 | struct eth_dev *dev = get_gadget_data(gadget); |
23cd1385 RB |
1244 | struct usb_request *req = dev->req; |
1245 | int value = -EOPNOTSUPP; | |
1246 | u16 wIndex = le16_to_cpu(ctrl->wIndex); | |
1247 | u16 wValue = le16_to_cpu(ctrl->wValue); | |
1248 | u16 wLength = le16_to_cpu(ctrl->wLength); | |
1249 | ||
6142e0ae VK |
1250 | /* |
1251 | * descriptors just go into the pre-allocated ep0 buffer, | |
23cd1385 RB |
1252 | * while config change events may enable network traffic. |
1253 | */ | |
1254 | ||
7de73185 | 1255 | debug("%s\n", __func__); |
23cd1385 RB |
1256 | |
1257 | req->complete = eth_setup_complete; | |
1258 | switch (ctrl->bRequest) { | |
1259 | ||
1260 | case USB_REQ_GET_DESCRIPTOR: | |
1261 | if (ctrl->bRequestType != USB_DIR_IN) | |
1262 | break; | |
1263 | switch (wValue >> 8) { | |
1264 | ||
1265 | case USB_DT_DEVICE: | |
04afd5b5 | 1266 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
6142e0ae VK |
1267 | value = min(wLength, (u16) sizeof device_desc); |
1268 | memcpy(req->buf, &device_desc, value); | |
23cd1385 RB |
1269 | break; |
1270 | case USB_DT_DEVICE_QUALIFIER: | |
1271 | if (!gadget_is_dualspeed(gadget)) | |
1272 | break; | |
6142e0ae VK |
1273 | value = min(wLength, (u16) sizeof dev_qualifier); |
1274 | memcpy(req->buf, &dev_qualifier, value); | |
23cd1385 RB |
1275 | break; |
1276 | ||
1277 | case USB_DT_OTHER_SPEED_CONFIG: | |
1278 | if (!gadget_is_dualspeed(gadget)) | |
1279 | break; | |
1280 | /* FALLTHROUGH */ | |
1281 | case USB_DT_CONFIG: | |
1282 | value = config_buf(gadget, req->buf, | |
1283 | wValue >> 8, | |
1284 | wValue & 0xff, | |
1285 | gadget_is_otg(gadget)); | |
1286 | if (value >= 0) | |
6142e0ae | 1287 | value = min(wLength, (u16) value); |
23cd1385 RB |
1288 | break; |
1289 | ||
1290 | case USB_DT_STRING: | |
6142e0ae | 1291 | value = usb_gadget_get_string(&stringtab, |
23cd1385 RB |
1292 | wValue & 0xff, req->buf); |
1293 | ||
1294 | if (value >= 0) | |
6142e0ae | 1295 | value = min(wLength, (u16) value); |
23cd1385 RB |
1296 | |
1297 | break; | |
1298 | } | |
1299 | break; | |
1300 | ||
1301 | case USB_REQ_SET_CONFIGURATION: | |
1302 | if (ctrl->bRequestType != 0) | |
1303 | break; | |
1304 | if (gadget->a_hnp_support) | |
7de73185 | 1305 | debug("HNP available\n"); |
23cd1385 | 1306 | else if (gadget->a_alt_hnp_support) |
7de73185 | 1307 | debug("HNP needs a different root port\n"); |
6142e0ae | 1308 | value = eth_set_config(dev, wValue, GFP_ATOMIC); |
23cd1385 RB |
1309 | break; |
1310 | case USB_REQ_GET_CONFIGURATION: | |
1311 | if (ctrl->bRequestType != USB_DIR_IN) | |
1312 | break; | |
1313 | *(u8 *)req->buf = dev->config; | |
6142e0ae | 1314 | value = min(wLength, (u16) 1); |
23cd1385 RB |
1315 | break; |
1316 | ||
1317 | case USB_REQ_SET_INTERFACE: | |
1318 | if (ctrl->bRequestType != USB_RECIP_INTERFACE | |
1319 | || !dev->config | |
1320 | || wIndex > 1) | |
1321 | break; | |
1322 | if (!cdc_active(dev) && wIndex != 0) | |
1323 | break; | |
1324 | ||
6142e0ae VK |
1325 | /* |
1326 | * PXA hardware partially handles SET_INTERFACE; | |
23cd1385 RB |
1327 | * we need to kluge around that interference. |
1328 | */ | |
6142e0ae VK |
1329 | if (gadget_is_pxa(gadget)) { |
1330 | value = eth_set_config(dev, DEV_CONFIG_VALUE, | |
23cd1385 | 1331 | GFP_ATOMIC); |
563aed25 LD |
1332 | /* |
1333 | * PXA25x driver use non-CDC ethernet gadget. | |
1334 | * But only _CDC and _RNDIS code can signalize | |
1335 | * that network is working. So we signalize it | |
1336 | * here. | |
1337 | */ | |
17b4f308 | 1338 | dev->network_started = 1; |
563aed25 | 1339 | debug("USB network up!\n"); |
23cd1385 RB |
1340 | goto done_set_intf; |
1341 | } | |
1342 | ||
2bb37884 | 1343 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 RB |
1344 | switch (wIndex) { |
1345 | case 0: /* control/master intf */ | |
1346 | if (wValue != 0) | |
1347 | break; | |
1348 | if (dev->status) { | |
6142e0ae VK |
1349 | usb_ep_disable(dev->status_ep); |
1350 | usb_ep_enable(dev->status_ep, dev->status); | |
23cd1385 | 1351 | } |
7612a43d | 1352 | |
23cd1385 RB |
1353 | value = 0; |
1354 | break; | |
1355 | case 1: /* data intf */ | |
1356 | if (wValue > 1) | |
1357 | break; | |
6142e0ae VK |
1358 | usb_ep_disable(dev->in_ep); |
1359 | usb_ep_disable(dev->out_ep); | |
23cd1385 | 1360 | |
6142e0ae VK |
1361 | /* |
1362 | * CDC requires the data transfers not be done from | |
23cd1385 RB |
1363 | * the default interface setting ... also, setting |
1364 | * the non-default interface resets filters etc. | |
1365 | */ | |
1366 | if (wValue == 1) { | |
6142e0ae | 1367 | if (!cdc_active(dev)) |
23cd1385 | 1368 | break; |
6142e0ae VK |
1369 | usb_ep_enable(dev->in_ep, dev->in); |
1370 | usb_ep_enable(dev->out_ep, dev->out); | |
23cd1385 RB |
1371 | dev->cdc_filter = DEFAULT_FILTER; |
1372 | if (dev->status) | |
6142e0ae | 1373 | issue_start_status(dev); |
7612a43d | 1374 | eth_start(dev, GFP_ATOMIC); |
23cd1385 | 1375 | } |
23cd1385 RB |
1376 | value = 0; |
1377 | break; | |
1378 | } | |
1379 | #else | |
6142e0ae VK |
1380 | /* |
1381 | * FIXME this is wrong, as is the assumption that | |
23cd1385 RB |
1382 | * all non-PXA hardware talks real CDC ... |
1383 | */ | |
7de73185 | 1384 | debug("set_interface ignored!\n"); |
2bb37884 | 1385 | #endif /* CONFIG_USB_ETH_CDC */ |
23cd1385 RB |
1386 | |
1387 | done_set_intf: | |
1388 | break; | |
1389 | case USB_REQ_GET_INTERFACE: | |
1390 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) | |
1391 | || !dev->config | |
1392 | || wIndex > 1) | |
1393 | break; | |
7612a43d | 1394 | if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0) |
23cd1385 RB |
1395 | break; |
1396 | ||
1397 | /* for CDC, iff carrier is on, data interface is active. */ | |
7612a43d | 1398 | if (rndis_active(dev) || wIndex != 1) |
23cd1385 RB |
1399 | *(u8 *)req->buf = 0; |
1400 | else { | |
1401 | /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ | |
1402 | /* carrier always ok ...*/ | |
1403 | *(u8 *)req->buf = 1 ; | |
1404 | } | |
6142e0ae | 1405 | value = min(wLength, (u16) 1); |
23cd1385 RB |
1406 | break; |
1407 | ||
2bb37884 | 1408 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 | 1409 | case USB_CDC_SET_ETHERNET_PACKET_FILTER: |
6142e0ae VK |
1410 | /* |
1411 | * see 6.2.30: no data, wIndex = interface, | |
23cd1385 RB |
1412 | * wValue = packet filter bitmap |
1413 | */ | |
1414 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) | |
1415 | || !cdc_active(dev) | |
1416 | || wLength != 0 | |
1417 | || wIndex > 1) | |
1418 | break; | |
7de73185 | 1419 | debug("packet filter %02x\n", wValue); |
23cd1385 RB |
1420 | dev->cdc_filter = wValue; |
1421 | value = 0; | |
1422 | break; | |
1423 | ||
6142e0ae VK |
1424 | /* |
1425 | * and potentially: | |
23cd1385 RB |
1426 | * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: |
1427 | * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: | |
1428 | * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: | |
1429 | * case USB_CDC_GET_ETHERNET_STATISTIC: | |
1430 | */ | |
1431 | ||
2bb37884 | 1432 | #endif /* CONFIG_USB_ETH_CDC */ |
23cd1385 | 1433 | |
7612a43d VK |
1434 | #ifdef CONFIG_USB_ETH_RNDIS |
1435 | /* | |
1436 | * RNDIS uses the CDC command encapsulation mechanism to implement | |
1437 | * an RPC scheme, with much getting/setting of attributes by OID. | |
1438 | */ | |
1439 | case USB_CDC_SEND_ENCAPSULATED_COMMAND: | |
1440 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) | |
1441 | || !rndis_active(dev) | |
1442 | || wLength > USB_BUFSIZ | |
1443 | || wValue | |
1444 | || rndis_control_intf.bInterfaceNumber | |
1445 | != wIndex) | |
1446 | break; | |
1447 | /* read the request, then process it */ | |
1448 | value = wLength; | |
1449 | req->complete = rndis_command_complete; | |
1450 | /* later, rndis_control_ack () sends a notification */ | |
1451 | break; | |
1452 | ||
1453 | case USB_CDC_GET_ENCAPSULATED_RESPONSE: | |
1454 | if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE) | |
1455 | == ctrl->bRequestType | |
1456 | && rndis_active(dev) | |
1457 | /* && wLength >= 0x0400 */ | |
1458 | && !wValue | |
1459 | && rndis_control_intf.bInterfaceNumber | |
1460 | == wIndex) { | |
1461 | u8 *buf; | |
1462 | u32 n; | |
1463 | ||
1464 | /* return the result */ | |
1465 | buf = rndis_get_next_response(dev->rndis_config, &n); | |
1466 | if (buf) { | |
1467 | memcpy(req->buf, buf, n); | |
1468 | req->complete = rndis_response_complete; | |
1469 | rndis_free_response(dev->rndis_config, buf); | |
1470 | value = n; | |
1471 | } | |
1472 | /* else stalls ... spec says to avoid that */ | |
1473 | } | |
1474 | break; | |
1475 | #endif /* RNDIS */ | |
1476 | ||
23cd1385 | 1477 | default: |
7de73185 | 1478 | debug("unknown control req%02x.%02x v%04x i%04x l%d\n", |
23cd1385 RB |
1479 | ctrl->bRequestType, ctrl->bRequest, |
1480 | wValue, wIndex, wLength); | |
1481 | } | |
1482 | ||
1483 | /* respond with data transfer before status phase? */ | |
1484 | if (value >= 0) { | |
7de73185 | 1485 | debug("respond with data transfer before status phase\n"); |
23cd1385 RB |
1486 | req->length = value; |
1487 | req->zero = value < wLength | |
1488 | && (value % gadget->ep0->maxpacket) == 0; | |
6142e0ae | 1489 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
23cd1385 | 1490 | if (value < 0) { |
7de73185 | 1491 | debug("ep_queue --> %d\n", value); |
23cd1385 | 1492 | req->status = 0; |
6142e0ae | 1493 | eth_setup_complete(gadget->ep0, req); |
23cd1385 RB |
1494 | } |
1495 | } | |
1496 | ||
1497 | /* host either stalls (value < 0) or reports success */ | |
1498 | return value; | |
1499 | } | |
1500 | ||
23cd1385 RB |
1501 | /*-------------------------------------------------------------------------*/ |
1502 | ||
6142e0ae | 1503 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); |
23cd1385 | 1504 | |
6142e0ae | 1505 | static int rx_submit(struct eth_dev *dev, struct usb_request *req, |
23cd1385 RB |
1506 | gfp_t gfp_flags) |
1507 | { | |
1508 | int retval = -ENOMEM; | |
1509 | size_t size; | |
1510 | ||
6142e0ae VK |
1511 | /* |
1512 | * Padding up to RX_EXTRA handles minor disagreements with host. | |
23cd1385 RB |
1513 | * Normally we use the USB "terminate on short read" convention; |
1514 | * so allow up to (N*maxpacket), since that memory is normally | |
1515 | * already allocated. Some hardware doesn't deal well with short | |
1516 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a | |
1517 | * byte off the end (to force hardware errors on overflow). | |
7612a43d VK |
1518 | * |
1519 | * RNDIS uses internal framing, and explicitly allows senders to | |
1520 | * pad to end-of-packet. That's potentially nice for speed, | |
1521 | * but means receivers can't recover synch on their own. | |
23cd1385 RB |
1522 | */ |
1523 | ||
7de73185 | 1524 | debug("%s\n", __func__); |
71fc5f91 TK |
1525 | if (!req) |
1526 | return -EINVAL; | |
23cd1385 RB |
1527 | |
1528 | size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); | |
1529 | size += dev->out_ep->maxpacket - 1; | |
7612a43d VK |
1530 | if (rndis_active(dev)) |
1531 | size += sizeof(struct rndis_packet_msg_type); | |
23cd1385 RB |
1532 | size -= size % dev->out_ep->maxpacket; |
1533 | ||
6142e0ae VK |
1534 | /* |
1535 | * Some platforms perform better when IP packets are aligned, | |
7612a43d VK |
1536 | * but on at least one, checksumming fails otherwise. Note: |
1537 | * RNDIS headers involve variable numbers of LE32 values. | |
23cd1385 RB |
1538 | */ |
1539 | ||
1fd92db8 | 1540 | req->buf = (u8 *)net_rx_packets[0]; |
23cd1385 RB |
1541 | req->length = size; |
1542 | req->complete = rx_complete; | |
1543 | ||
6142e0ae | 1544 | retval = usb_ep_queue(dev->out_ep, req, gfp_flags); |
23cd1385 | 1545 | |
6142e0ae | 1546 | if (retval) |
7de73185 | 1547 | error("rx submit --> %d", retval); |
6142e0ae | 1548 | |
23cd1385 RB |
1549 | return retval; |
1550 | } | |
1551 | ||
6142e0ae | 1552 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) |
23cd1385 RB |
1553 | { |
1554 | struct eth_dev *dev = ep->driver_data; | |
1555 | ||
7de73185 | 1556 | debug("%s: status %d\n", __func__, req->status); |
c85d70ef VK |
1557 | switch (req->status) { |
1558 | /* normal completion */ | |
1559 | case 0: | |
7612a43d VK |
1560 | if (rndis_active(dev)) { |
1561 | /* we know MaxPacketsPerTransfer == 1 here */ | |
1562 | int length = rndis_rm_hdr(req->buf, req->actual); | |
1563 | if (length < 0) | |
1564 | goto length_err; | |
1565 | req->length -= length; | |
1566 | req->actual -= length; | |
1567 | } | |
1568 | if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) { | |
1569 | length_err: | |
1570 | dev->stats.rx_errors++; | |
1571 | dev->stats.rx_length_errors++; | |
1572 | debug("rx length %d\n", req->length); | |
1573 | break; | |
1574 | } | |
1575 | ||
c85d70ef VK |
1576 | dev->stats.rx_packets++; |
1577 | dev->stats.rx_bytes += req->length; | |
1578 | break; | |
1579 | ||
1580 | /* software-driven interface shutdown */ | |
1581 | case -ECONNRESET: /* unlink */ | |
1582 | case -ESHUTDOWN: /* disconnect etc */ | |
1583 | /* for hardware automagic (such as pxa) */ | |
1584 | case -ECONNABORTED: /* endpoint reset */ | |
1585 | break; | |
1586 | ||
1587 | /* data overrun */ | |
1588 | case -EOVERFLOW: | |
1589 | dev->stats.rx_over_errors++; | |
1590 | /* FALLTHROUGH */ | |
1591 | default: | |
1592 | dev->stats.rx_errors++; | |
1593 | break; | |
1594 | } | |
23cd1385 | 1595 | |
6142e0ae | 1596 | packet_received = 1; |
23cd1385 RB |
1597 | } |
1598 | ||
6142e0ae | 1599 | static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) |
23cd1385 RB |
1600 | { |
1601 | ||
6142e0ae | 1602 | dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); |
23cd1385 RB |
1603 | |
1604 | if (!dev->tx_req) | |
ac5d32d1 | 1605 | goto fail1; |
23cd1385 | 1606 | |
6142e0ae | 1607 | dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); |
23cd1385 RB |
1608 | |
1609 | if (!dev->rx_req) | |
ac5d32d1 | 1610 | goto fail2; |
23cd1385 RB |
1611 | |
1612 | return 0; | |
1613 | ||
ac5d32d1 VK |
1614 | fail2: |
1615 | usb_ep_free_request(dev->in_ep, dev->tx_req); | |
1616 | fail1: | |
7de73185 | 1617 | error("can't alloc requests"); |
23cd1385 RB |
1618 | return -1; |
1619 | } | |
1620 | ||
6142e0ae | 1621 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) |
23cd1385 | 1622 | { |
c85d70ef VK |
1623 | struct eth_dev *dev = ep->driver_data; |
1624 | ||
6142e0ae | 1625 | debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); |
c85d70ef VK |
1626 | switch (req->status) { |
1627 | default: | |
1628 | dev->stats.tx_errors++; | |
1629 | debug("tx err %d\n", req->status); | |
1630 | /* FALLTHROUGH */ | |
1631 | case -ECONNRESET: /* unlink */ | |
1632 | case -ESHUTDOWN: /* disconnect etc */ | |
1633 | break; | |
1634 | case 0: | |
1635 | dev->stats.tx_bytes += req->length; | |
1636 | } | |
1637 | dev->stats.tx_packets++; | |
1638 | ||
6142e0ae | 1639 | packet_sent = 1; |
23cd1385 RB |
1640 | } |
1641 | ||
6142e0ae | 1642 | static inline int eth_is_promisc(struct eth_dev *dev) |
23cd1385 RB |
1643 | { |
1644 | /* no filters for the CDC subset; always promisc */ | |
6142e0ae | 1645 | if (subset_active(dev)) |
23cd1385 RB |
1646 | return 1; |
1647 | return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; | |
1648 | } | |
1649 | ||
1650 | #if 0 | |
1651 | static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) | |
1652 | { | |
1653 | struct eth_dev *dev = netdev_priv(net); | |
1654 | int length = skb->len; | |
1655 | int retval; | |
1656 | struct usb_request *req = NULL; | |
1657 | unsigned long flags; | |
1658 | ||
1659 | /* apply outgoing CDC or RNDIS filters */ | |
1660 | if (!eth_is_promisc (dev)) { | |
1661 | u8 *dest = skb->data; | |
1662 | ||
0adb5b76 | 1663 | if (is_multicast_ethaddr(dest)) { |
23cd1385 RB |
1664 | u16 type; |
1665 | ||
1666 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host | |
1667 | * SET_ETHERNET_MULTICAST_FILTERS requests | |
1668 | */ | |
0adb5b76 | 1669 | if (is_broadcast_ethaddr(dest)) |
23cd1385 RB |
1670 | type = USB_CDC_PACKET_TYPE_BROADCAST; |
1671 | else | |
1672 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; | |
1673 | if (!(dev->cdc_filter & type)) { | |
1674 | dev_kfree_skb_any (skb); | |
1675 | return 0; | |
1676 | } | |
1677 | } | |
1678 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ | |
1679 | } | |
1680 | ||
1681 | spin_lock_irqsave(&dev->req_lock, flags); | |
1682 | /* | |
1683 | * this freelist can be empty if an interrupt triggered disconnect() | |
1684 | * and reconfigured the gadget (shutting down this queue) after the | |
1685 | * network stack decided to xmit but before we got the spinlock. | |
1686 | */ | |
1687 | if (list_empty(&dev->tx_reqs)) { | |
1688 | spin_unlock_irqrestore(&dev->req_lock, flags); | |
1689 | return 1; | |
1690 | } | |
1691 | ||
1692 | req = container_of (dev->tx_reqs.next, struct usb_request, list); | |
1693 | list_del (&req->list); | |
1694 | ||
1695 | /* temporarily stop TX queue when the freelist empties */ | |
1696 | if (list_empty (&dev->tx_reqs)) | |
1697 | netif_stop_queue (net); | |
1698 | spin_unlock_irqrestore(&dev->req_lock, flags); | |
1699 | ||
1700 | /* no buffer copies needed, unless the network stack did it | |
1701 | * or the hardware can't use skb buffers. | |
1702 | * or there's not enough space for any RNDIS headers we need | |
1703 | */ | |
1704 | if (rndis_active(dev)) { | |
1705 | struct sk_buff *skb_rndis; | |
1706 | ||
1707 | skb_rndis = skb_realloc_headroom (skb, | |
1708 | sizeof (struct rndis_packet_msg_type)); | |
1709 | if (!skb_rndis) | |
1710 | goto drop; | |
1711 | ||
1712 | dev_kfree_skb_any (skb); | |
1713 | skb = skb_rndis; | |
1714 | rndis_add_hdr (skb); | |
1715 | length = skb->len; | |
1716 | } | |
1717 | req->buf = skb->data; | |
1718 | req->context = skb; | |
1719 | req->complete = tx_complete; | |
1720 | ||
1721 | /* use zlp framing on tx for strict CDC-Ether conformance, | |
1722 | * though any robust network rx path ignores extra padding. | |
1723 | * and some hardware doesn't like to write zlps. | |
1724 | */ | |
1725 | req->zero = 1; | |
1726 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) | |
1727 | length++; | |
1728 | ||
1729 | req->length = length; | |
1730 | ||
1731 | /* throttle highspeed IRQ rate back slightly */ | |
1732 | if (gadget_is_dualspeed(dev->gadget)) | |
1733 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) | |
1734 | ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) | |
1735 | : 0; | |
1736 | ||
1737 | retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); | |
1738 | switch (retval) { | |
1739 | default: | |
1740 | DEBUG (dev, "tx queue err %d\n", retval); | |
1741 | break; | |
1742 | case 0: | |
1743 | net->trans_start = jiffies; | |
1744 | atomic_inc (&dev->tx_qlen); | |
1745 | } | |
1746 | ||
1747 | if (retval) { | |
1748 | drop: | |
1749 | dev->stats.tx_dropped++; | |
1750 | dev_kfree_skb_any (skb); | |
1751 | spin_lock_irqsave(&dev->req_lock, flags); | |
1752 | if (list_empty (&dev->tx_reqs)) | |
1753 | netif_start_queue (net); | |
1754 | list_add (&req->list, &dev->tx_reqs); | |
1755 | spin_unlock_irqrestore(&dev->req_lock, flags); | |
1756 | } | |
1757 | return 0; | |
1758 | } | |
1759 | ||
1760 | /*-------------------------------------------------------------------------*/ | |
1761 | #endif | |
1762 | ||
6142e0ae | 1763 | static void eth_unbind(struct usb_gadget *gadget) |
23cd1385 | 1764 | { |
7612a43d | 1765 | struct eth_dev *dev = get_gadget_data(gadget); |
23cd1385 | 1766 | |
7de73185 | 1767 | debug("%s...\n", __func__); |
7612a43d VK |
1768 | rndis_deregister(dev->rndis_config); |
1769 | rndis_exit(); | |
23cd1385 | 1770 | |
0129e327 VK |
1771 | /* we've already been disconnected ... no i/o is active */ |
1772 | if (dev->req) { | |
6142e0ae | 1773 | usb_ep_free_request(gadget->ep0, dev->req); |
0129e327 VK |
1774 | dev->req = NULL; |
1775 | } | |
23cd1385 | 1776 | if (dev->stat_req) { |
6142e0ae | 1777 | usb_ep_free_request(dev->status_ep, dev->stat_req); |
23cd1385 RB |
1778 | dev->stat_req = NULL; |
1779 | } | |
1780 | ||
1781 | if (dev->tx_req) { | |
6142e0ae VK |
1782 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
1783 | dev->tx_req = NULL; | |
23cd1385 RB |
1784 | } |
1785 | ||
1786 | if (dev->rx_req) { | |
6142e0ae VK |
1787 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
1788 | dev->rx_req = NULL; | |
23cd1385 RB |
1789 | } |
1790 | ||
1791 | /* unregister_netdev (dev->net);*/ | |
1792 | /* free_netdev(dev->net);*/ | |
1793 | ||
988ee3e3 | 1794 | dev->gadget = NULL; |
6142e0ae | 1795 | set_gadget_data(gadget, NULL); |
23cd1385 RB |
1796 | } |
1797 | ||
6142e0ae | 1798 | static void eth_disconnect(struct usb_gadget *gadget) |
23cd1385 | 1799 | { |
6142e0ae | 1800 | eth_reset_config(get_gadget_data(gadget)); |
7612a43d | 1801 | /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */ |
23cd1385 RB |
1802 | } |
1803 | ||
6142e0ae | 1804 | static void eth_suspend(struct usb_gadget *gadget) |
23cd1385 RB |
1805 | { |
1806 | /* Not used */ | |
1807 | } | |
1808 | ||
6142e0ae | 1809 | static void eth_resume(struct usb_gadget *gadget) |
23cd1385 RB |
1810 | { |
1811 | /* Not used */ | |
1812 | } | |
1813 | ||
1814 | /*-------------------------------------------------------------------------*/ | |
1815 | ||
7612a43d VK |
1816 | #ifdef CONFIG_USB_ETH_RNDIS |
1817 | ||
1818 | /* | |
1819 | * The interrupt endpoint is used in RNDIS to notify the host when messages | |
1820 | * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT | |
1821 | * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even | |
1822 | * REMOTE_NDIS_KEEPALIVE_MSG. | |
1823 | * | |
1824 | * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and | |
1825 | * normally just one notification will be queued. | |
1826 | */ | |
1827 | ||
1828 | static void rndis_control_ack_complete(struct usb_ep *ep, | |
1829 | struct usb_request *req) | |
1830 | { | |
1831 | struct eth_dev *dev = ep->driver_data; | |
1832 | ||
1833 | debug("%s...\n", __func__); | |
1834 | if (req->status || req->actual != req->length) | |
1835 | debug("rndis control ack complete --> %d, %d/%d\n", | |
1836 | req->status, req->actual, req->length); | |
1837 | ||
17b4f308 | 1838 | if (!dev->network_started) { |
7612a43d VK |
1839 | if (rndis_get_state(dev->rndis_config) |
1840 | == RNDIS_DATA_INITIALIZED) { | |
17b4f308 | 1841 | dev->network_started = 1; |
7612a43d VK |
1842 | printf("USB RNDIS network up!\n"); |
1843 | } | |
1844 | } | |
1845 | ||
1846 | req->context = NULL; | |
1847 | ||
1848 | if (req != dev->stat_req) | |
1849 | usb_ep_free_request(ep, req); | |
1850 | } | |
1851 | ||
1852 | static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32)))); | |
1853 | ||
1854 | static int rndis_control_ack(struct eth_device *net) | |
1855 | { | |
5cb3b9d7 | 1856 | struct eth_dev *dev = &l_priv->ethdev; |
7612a43d VK |
1857 | int length; |
1858 | struct usb_request *resp = dev->stat_req; | |
1859 | ||
1860 | /* in case RNDIS calls this after disconnect */ | |
1861 | if (!dev->status) { | |
1862 | debug("status ENODEV\n"); | |
1863 | return -ENODEV; | |
1864 | } | |
1865 | ||
1866 | /* in case queue length > 1 */ | |
1867 | if (resp->context) { | |
1868 | resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC); | |
1869 | if (!resp) | |
1870 | return -ENOMEM; | |
1871 | resp->buf = rndis_resp_buf; | |
1872 | } | |
1873 | ||
1874 | /* | |
1875 | * Send RNDIS RESPONSE_AVAILABLE notification; | |
1876 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too | |
1877 | */ | |
1878 | resp->length = 8; | |
1879 | resp->complete = rndis_control_ack_complete; | |
1880 | resp->context = dev; | |
1881 | ||
1882 | *((__le32 *) resp->buf) = __constant_cpu_to_le32(1); | |
1883 | *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0); | |
1884 | ||
1885 | length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC); | |
1886 | if (length < 0) { | |
1887 | resp->status = 0; | |
1888 | rndis_control_ack_complete(dev->status_ep, resp); | |
1889 | } | |
1890 | ||
1891 | return 0; | |
1892 | } | |
1893 | ||
1894 | #else | |
1895 | ||
1896 | #define rndis_control_ack NULL | |
1897 | ||
1898 | #endif /* RNDIS */ | |
1899 | ||
1900 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) | |
1901 | { | |
1902 | if (rndis_active(dev)) { | |
1903 | rndis_set_param_medium(dev->rndis_config, | |
1904 | NDIS_MEDIUM_802_3, | |
1905 | BITRATE(dev->gadget)/100); | |
1906 | rndis_signal_connect(dev->rndis_config); | |
1907 | } | |
1908 | } | |
1909 | ||
1910 | static int eth_stop(struct eth_dev *dev) | |
1911 | { | |
e4ae6660 VK |
1912 | #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT |
1913 | unsigned long ts; | |
1914 | unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */ | |
1915 | #endif | |
1916 | ||
7612a43d VK |
1917 | if (rndis_active(dev)) { |
1918 | rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0); | |
1919 | rndis_signal_disconnect(dev->rndis_config); | |
1920 | ||
e4ae6660 VK |
1921 | #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT |
1922 | /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ | |
1923 | ts = get_timer(0); | |
1924 | while (get_timer(ts) < timeout) | |
2d48aa69 | 1925 | usb_gadget_handle_interrupts(0); |
e4ae6660 VK |
1926 | #endif |
1927 | ||
7612a43d VK |
1928 | rndis_uninit(dev->rndis_config); |
1929 | dev->rndis = 0; | |
1930 | } | |
1931 | ||
1932 | return 0; | |
1933 | } | |
1934 | ||
1935 | /*-------------------------------------------------------------------------*/ | |
1936 | ||
23cd1385 RB |
1937 | static int is_eth_addr_valid(char *str) |
1938 | { | |
1939 | if (strlen(str) == 17) { | |
1940 | int i; | |
1941 | char *p, *q; | |
1942 | uchar ea[6]; | |
1943 | ||
1944 | /* see if it looks like an ethernet address */ | |
1945 | ||
1946 | p = str; | |
1947 | ||
1948 | for (i = 0; i < 6; i++) { | |
1949 | char term = (i == 5 ? '\0' : ':'); | |
1950 | ||
1951 | ea[i] = simple_strtol(p, &q, 16); | |
1952 | ||
1953 | if ((q - p) != 2 || *q++ != term) | |
1954 | break; | |
1955 | ||
1956 | p = q; | |
1957 | } | |
1958 | ||
57a87a25 | 1959 | /* Now check the contents. */ |
0adb5b76 | 1960 | return is_valid_ethaddr(ea); |
23cd1385 RB |
1961 | } |
1962 | return 0; | |
1963 | } | |
1964 | ||
6142e0ae | 1965 | static u8 nibble(unsigned char c) |
23cd1385 | 1966 | { |
6142e0ae | 1967 | if (likely(isdigit(c))) |
23cd1385 | 1968 | return c - '0'; |
6142e0ae VK |
1969 | c = toupper(c); |
1970 | if (likely(isxdigit(c))) | |
23cd1385 RB |
1971 | return 10 + c - 'A'; |
1972 | return 0; | |
1973 | } | |
1974 | ||
1975 | static int get_ether_addr(const char *str, u8 *dev_addr) | |
1976 | { | |
1977 | if (str) { | |
1978 | unsigned i; | |
1979 | ||
1980 | for (i = 0; i < 6; i++) { | |
1981 | unsigned char num; | |
1982 | ||
6142e0ae | 1983 | if ((*str == '.') || (*str == ':')) |
23cd1385 RB |
1984 | str++; |
1985 | num = nibble(*str++) << 4; | |
1986 | num |= (nibble(*str++)); | |
6142e0ae | 1987 | dev_addr[i] = num; |
23cd1385 | 1988 | } |
0adb5b76 | 1989 | if (is_valid_ethaddr(dev_addr)) |
23cd1385 RB |
1990 | return 0; |
1991 | } | |
1992 | return 1; | |
1993 | } | |
1994 | ||
1995 | static int eth_bind(struct usb_gadget *gadget) | |
1996 | { | |
5cb3b9d7 | 1997 | struct eth_dev *dev = &l_priv->ethdev; |
7612a43d | 1998 | u8 cdc = 1, zlp = 1, rndis = 1; |
23cd1385 | 1999 | struct usb_ep *in_ep, *out_ep, *status_ep = NULL; |
7612a43d | 2000 | int status = -ENOMEM; |
23cd1385 | 2001 | int gcnum; |
6142e0ae | 2002 | u8 tmp[7]; |
23cd1385 RB |
2003 | |
2004 | /* these flags are only ever cleared; compiler take note */ | |
2bb37884 | 2005 | #ifndef CONFIG_USB_ETH_CDC |
23cd1385 | 2006 | cdc = 0; |
7612a43d VK |
2007 | #endif |
2008 | #ifndef CONFIG_USB_ETH_RNDIS | |
2009 | rndis = 0; | |
23cd1385 | 2010 | #endif |
6142e0ae VK |
2011 | /* |
2012 | * Because most host side USB stacks handle CDC Ethernet, that | |
23cd1385 RB |
2013 | * standard protocol is _strongly_ preferred for interop purposes. |
2014 | * (By everyone except Microsoft.) | |
2015 | */ | |
6142e0ae | 2016 | if (gadget_is_pxa(gadget)) { |
23cd1385 RB |
2017 | /* pxa doesn't support altsettings */ |
2018 | cdc = 0; | |
2019 | } else if (gadget_is_musbhdrc(gadget)) { | |
2020 | /* reduce tx dma overhead by avoiding special cases */ | |
2021 | zlp = 0; | |
2022 | } else if (gadget_is_sh(gadget)) { | |
2023 | /* sh doesn't support multiple interfaces or configs */ | |
2024 | cdc = 0; | |
7612a43d | 2025 | rndis = 0; |
6142e0ae | 2026 | } else if (gadget_is_sa1100(gadget)) { |
23cd1385 RB |
2027 | /* hardware can't write zlps */ |
2028 | zlp = 0; | |
6142e0ae VK |
2029 | /* |
2030 | * sa1100 CAN do CDC, without status endpoint ... we use | |
23cd1385 RB |
2031 | * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". |
2032 | */ | |
2033 | cdc = 0; | |
2034 | } | |
2035 | ||
6142e0ae | 2036 | gcnum = usb_gadget_controller_number(gadget); |
23cd1385 | 2037 | if (gcnum >= 0) |
6142e0ae | 2038 | device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); |
23cd1385 | 2039 | else { |
6142e0ae VK |
2040 | /* |
2041 | * can't assume CDC works. don't want to default to | |
23cd1385 RB |
2042 | * anything less functional on CDC-capable hardware, |
2043 | * so we fail in this case. | |
2044 | */ | |
7de73185 | 2045 | error("controller '%s' not recognized", |
23cd1385 RB |
2046 | gadget->name); |
2047 | return -ENODEV; | |
2048 | } | |
2049 | ||
6142e0ae | 2050 | /* |
7612a43d VK |
2051 | * If there's an RNDIS configuration, that's what Windows wants to |
2052 | * be using ... so use these product IDs here and in the "linux.inf" | |
2053 | * needed to install MSFT drivers. Current Linux kernels will use | |
2054 | * the second configuration if it's CDC Ethernet, and need some help | |
2055 | * to choose the right configuration otherwise. | |
23cd1385 | 2056 | */ |
7612a43d VK |
2057 | if (rndis) { |
2058 | #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID) | |
23cd1385 | 2059 | device_desc.idVendor = |
7612a43d | 2060 | __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID); |
23cd1385 | 2061 | device_desc.idProduct = |
7612a43d VK |
2062 | __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID); |
2063 | #else | |
2064 | device_desc.idVendor = | |
2065 | __constant_cpu_to_le16(RNDIS_VENDOR_NUM); | |
2066 | device_desc.idProduct = | |
2067 | __constant_cpu_to_le16(RNDIS_PRODUCT_NUM); | |
2068 | #endif | |
2069 | sprintf(product_desc, "RNDIS/%s", driver_desc); | |
23cd1385 | 2070 | |
7612a43d VK |
2071 | /* |
2072 | * CDC subset ... recognized by Linux since 2.4.10, but Windows | |
2073 | * drivers aren't widely available. (That may be improved by | |
2074 | * supporting one submode of the "SAFE" variant of MDLM.) | |
2075 | */ | |
2076 | } else { | |
23cd1385 | 2077 | #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID) |
7612a43d VK |
2078 | device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID); |
2079 | device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID); | |
2080 | #else | |
2081 | if (!cdc) { | |
2082 | device_desc.idVendor = | |
2083 | __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); | |
2084 | device_desc.idProduct = | |
2085 | __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); | |
2086 | } | |
23cd1385 | 2087 | #endif |
7612a43d VK |
2088 | } |
2089 | /* support optional vendor/distro customization */ | |
23cd1385 RB |
2090 | if (bcdDevice) |
2091 | device_desc.bcdDevice = cpu_to_le16(bcdDevice); | |
2092 | if (iManufacturer) | |
6142e0ae | 2093 | strlcpy(manufacturer, iManufacturer, sizeof manufacturer); |
23cd1385 | 2094 | if (iProduct) |
6142e0ae | 2095 | strlcpy(product_desc, iProduct, sizeof product_desc); |
23cd1385 RB |
2096 | if (iSerialNumber) { |
2097 | device_desc.iSerialNumber = STRING_SERIALNUMBER, | |
2e12abe6 | 2098 | strlcpy(serial_number, iSerialNumber, sizeof serial_number); |
23cd1385 RB |
2099 | } |
2100 | ||
2101 | /* all we really need is bulk IN/OUT */ | |
6142e0ae VK |
2102 | usb_ep_autoconfig_reset(gadget); |
2103 | in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); | |
23cd1385 RB |
2104 | if (!in_ep) { |
2105 | autoconf_fail: | |
7de73185 | 2106 | error("can't autoconfigure on %s\n", |
23cd1385 RB |
2107 | gadget->name); |
2108 | return -ENODEV; | |
2109 | } | |
2110 | in_ep->driver_data = in_ep; /* claim */ | |
2111 | ||
6142e0ae | 2112 | out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); |
23cd1385 RB |
2113 | if (!out_ep) |
2114 | goto autoconf_fail; | |
2115 | out_ep->driver_data = out_ep; /* claim */ | |
2116 | ||
2bb37884 | 2117 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
6142e0ae VK |
2118 | /* |
2119 | * CDC Ethernet control interface doesn't require a status endpoint. | |
23cd1385 RB |
2120 | * Since some hosts expect one, try to allocate one anyway. |
2121 | */ | |
7612a43d | 2122 | if (cdc || rndis) { |
6142e0ae | 2123 | status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); |
23cd1385 RB |
2124 | if (status_ep) { |
2125 | status_ep->driver_data = status_ep; /* claim */ | |
7612a43d VK |
2126 | } else if (rndis) { |
2127 | error("can't run RNDIS on %s", gadget->name); | |
2128 | return -ENODEV; | |
2bb37884 | 2129 | #ifdef CONFIG_USB_ETH_CDC |
23cd1385 RB |
2130 | } else if (cdc) { |
2131 | control_intf.bNumEndpoints = 0; | |
2132 | /* FIXME remove endpoint from descriptor list */ | |
7612a43d | 2133 | #endif |
23cd1385 RB |
2134 | } |
2135 | } | |
2136 | #endif | |
2137 | ||
2138 | /* one config: cdc, else minimal subset */ | |
2139 | if (!cdc) { | |
2140 | eth_config.bNumInterfaces = 1; | |
2141 | eth_config.iConfiguration = STRING_SUBSET; | |
2142 | ||
6142e0ae VK |
2143 | /* |
2144 | * use functions to set these up, in case we're built to work | |
23cd1385 RB |
2145 | * with multiple controllers and must override CDC Ethernet. |
2146 | */ | |
2147 | fs_subset_descriptors(); | |
2148 | hs_subset_descriptors(); | |
2149 | } | |
2150 | ||
6142e0ae | 2151 | usb_gadget_set_selfpowered(gadget); |
23cd1385 | 2152 | |
7612a43d VK |
2153 | /* For now RNDIS is always a second config */ |
2154 | if (rndis) | |
2155 | device_desc.bNumConfigurations = 2; | |
2156 | ||
23cd1385 | 2157 | if (gadget_is_dualspeed(gadget)) { |
7612a43d VK |
2158 | if (rndis) |
2159 | dev_qualifier.bNumConfigurations = 2; | |
2160 | else if (!cdc) | |
23cd1385 RB |
2161 | dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
2162 | ||
2163 | /* assumes ep0 uses the same value for both speeds ... */ | |
2164 | dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; | |
2165 | ||
2166 | /* and that all endpoints are dual-speed */ | |
2167 | hs_source_desc.bEndpointAddress = | |
2168 | fs_source_desc.bEndpointAddress; | |
2169 | hs_sink_desc.bEndpointAddress = | |
2170 | fs_sink_desc.bEndpointAddress; | |
2bb37884 | 2171 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
23cd1385 RB |
2172 | if (status_ep) |
2173 | hs_status_desc.bEndpointAddress = | |
2174 | fs_status_desc.bEndpointAddress; | |
2175 | #endif | |
2176 | } | |
2177 | ||
2178 | if (gadget_is_otg(gadget)) { | |
2179 | otg_descriptor.bmAttributes |= USB_OTG_HNP, | |
2180 | eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
2181 | eth_config.bMaxPower = 4; | |
7612a43d VK |
2182 | #ifdef CONFIG_USB_ETH_RNDIS |
2183 | rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
2184 | rndis_config.bMaxPower = 4; | |
2185 | #endif | |
23cd1385 RB |
2186 | } |
2187 | ||
7612a43d VK |
2188 | |
2189 | /* network device setup */ | |
5cb3b9d7 | 2190 | dev->net = &l_priv->netdev; |
23cd1385 RB |
2191 | |
2192 | dev->cdc = cdc; | |
2193 | dev->zlp = zlp; | |
2194 | ||
2195 | dev->in_ep = in_ep; | |
2196 | dev->out_ep = out_ep; | |
2197 | dev->status_ep = status_ep; | |
2198 | ||
6142e0ae VK |
2199 | /* |
2200 | * Module params for these addresses should come from ID proms. | |
7612a43d | 2201 | * The host side address is used with CDC and RNDIS, and commonly |
23cd1385 RB |
2202 | * ends up in a persistent config database. It's not clear if |
2203 | * host side code for the SAFE thing cares -- its original BLAN | |
2204 | * thing didn't, Sharp never assigned those addresses on Zaurii. | |
2205 | */ | |
2206 | get_ether_addr(dev_addr, dev->net->enetaddr); | |
2207 | ||
2208 | memset(tmp, 0, sizeof(tmp)); | |
2209 | memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); | |
2210 | ||
2211 | get_ether_addr(host_addr, dev->host_mac); | |
2212 | ||
6142e0ae VK |
2213 | sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", |
2214 | dev->host_mac[0], dev->host_mac[1], | |
2215 | dev->host_mac[2], dev->host_mac[3], | |
2216 | dev->host_mac[4], dev->host_mac[5]); | |
23cd1385 | 2217 | |
7612a43d VK |
2218 | if (rndis) { |
2219 | status = rndis_init(); | |
2220 | if (status < 0) { | |
2221 | error("can't init RNDIS, %d", status); | |
2222 | goto fail; | |
2223 | } | |
23cd1385 RB |
2224 | } |
2225 | ||
6142e0ae VK |
2226 | /* |
2227 | * use PKTSIZE (or aligned... from u-boot) and set | |
2228 | * wMaxSegmentSize accordingly | |
2229 | */ | |
23cd1385 RB |
2230 | dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ |
2231 | ||
2232 | /* preallocate control message data and buffer */ | |
6142e0ae | 2233 | dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
23cd1385 RB |
2234 | if (!dev->req) |
2235 | goto fail; | |
2236 | dev->req->buf = control_req; | |
2237 | dev->req->complete = eth_setup_complete; | |
2238 | ||
2239 | /* ... and maybe likewise for status transfer */ | |
2bb37884 | 2240 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
23cd1385 | 2241 | if (dev->status_ep) { |
6142e0ae VK |
2242 | dev->stat_req = usb_ep_alloc_request(dev->status_ep, |
2243 | GFP_KERNEL); | |
23cd1385 | 2244 | if (!dev->stat_req) { |
6142e0ae | 2245 | usb_ep_free_request(dev->status_ep, dev->req); |
23cd1385 RB |
2246 | |
2247 | goto fail; | |
2248 | } | |
df559c1d | 2249 | dev->stat_req->buf = status_req; |
23cd1385 RB |
2250 | dev->stat_req->context = NULL; |
2251 | } | |
2252 | #endif | |
2253 | ||
2254 | /* finish hookup to lower layer ... */ | |
2255 | dev->gadget = gadget; | |
6142e0ae | 2256 | set_gadget_data(gadget, dev); |
23cd1385 RB |
2257 | gadget->ep0->driver_data = dev; |
2258 | ||
6142e0ae VK |
2259 | /* |
2260 | * two kinds of host-initiated state changes: | |
23cd1385 RB |
2261 | * - iff DATA transfer is active, carrier is "on" |
2262 | * - tx queueing enabled if open *and* carrier is "on" | |
2263 | */ | |
7612a43d VK |
2264 | |
2265 | printf("using %s, OUT %s IN %s%s%s\n", gadget->name, | |
2266 | out_ep->name, in_ep->name, | |
2267 | status_ep ? " STATUS " : "", | |
2268 | status_ep ? status_ep->name : "" | |
2269 | ); | |
2270 | printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", | |
2271 | dev->net->enetaddr[0], dev->net->enetaddr[1], | |
2272 | dev->net->enetaddr[2], dev->net->enetaddr[3], | |
2273 | dev->net->enetaddr[4], dev->net->enetaddr[5]); | |
2274 | ||
2275 | if (cdc || rndis) | |
2276 | printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", | |
2277 | dev->host_mac[0], dev->host_mac[1], | |
2278 | dev->host_mac[2], dev->host_mac[3], | |
2279 | dev->host_mac[4], dev->host_mac[5]); | |
2280 | ||
2281 | if (rndis) { | |
2282 | u32 vendorID = 0; | |
2283 | ||
2284 | /* FIXME RNDIS vendor id == "vendor NIC code" == ? */ | |
2285 | ||
2286 | dev->rndis_config = rndis_register(rndis_control_ack); | |
2287 | if (dev->rndis_config < 0) { | |
2288 | fail0: | |
2289 | eth_unbind(gadget); | |
2290 | debug("RNDIS setup failed\n"); | |
2291 | status = -ENODEV; | |
2292 | goto fail; | |
2293 | } | |
2294 | ||
2295 | /* these set up a lot of the OIDs that RNDIS needs */ | |
2296 | rndis_set_host_mac(dev->rndis_config, dev->host_mac); | |
2297 | if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu, | |
2298 | &dev->stats, &dev->cdc_filter)) | |
2299 | goto fail0; | |
2300 | if (rndis_set_param_vendor(dev->rndis_config, vendorID, | |
2301 | manufacturer)) | |
2302 | goto fail0; | |
2303 | if (rndis_set_param_medium(dev->rndis_config, | |
2304 | NDIS_MEDIUM_802_3, 0)) | |
2305 | goto fail0; | |
2306 | printf("RNDIS ready\n"); | |
2307 | } | |
23cd1385 RB |
2308 | return 0; |
2309 | ||
2310 | fail: | |
7612a43d | 2311 | error("%s failed, status = %d", __func__, status); |
6142e0ae | 2312 | eth_unbind(gadget); |
7612a43d | 2313 | return status; |
23cd1385 RB |
2314 | } |
2315 | ||
7612a43d VK |
2316 | /*-------------------------------------------------------------------------*/ |
2317 | ||
d4345aee M |
2318 | #ifdef CONFIG_DM_USB |
2319 | int dm_usb_init(struct eth_dev *e_dev) | |
2320 | { | |
2321 | struct udevice *dev = NULL; | |
2322 | int ret; | |
2323 | ||
2324 | ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev); | |
2325 | if (!dev || ret) { | |
2326 | error("No USB device found\n"); | |
2327 | return -ENODEV; | |
2328 | } | |
2329 | ||
2330 | e_dev->usb_udev = dev; | |
2331 | ||
2332 | return ret; | |
2333 | } | |
2334 | #endif | |
2335 | ||
6142e0ae | 2336 | static int usb_eth_init(struct eth_device *netdev, bd_t *bd) |
23cd1385 | 2337 | { |
5cb3b9d7 | 2338 | struct eth_dev *dev = &l_priv->ethdev; |
23cd1385 RB |
2339 | struct usb_gadget *gadget; |
2340 | unsigned long ts; | |
2341 | unsigned long timeout = USB_CONNECT_TIMEOUT; | |
2342 | ||
2343 | if (!netdev) { | |
7de73185 | 2344 | error("received NULL ptr"); |
23cd1385 RB |
2345 | goto fail; |
2346 | } | |
58939fcc | 2347 | |
d4345aee M |
2348 | #ifdef CONFIG_DM_USB |
2349 | if (dm_usb_init(dev)) { | |
2350 | error("USB ether not found\n"); | |
2351 | return -ENODEV; | |
2352 | } | |
2353 | #else | |
8bfc288c | 2354 | board_usb_init(0, USB_INIT_DEVICE); |
d4345aee | 2355 | #endif |
8bfc288c | 2356 | |
58939fcc VK |
2357 | /* Configure default mac-addresses for the USB ethernet device */ |
2358 | #ifdef CONFIG_USBNET_DEV_ADDR | |
2359 | strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); | |
2360 | #endif | |
2361 | #ifdef CONFIG_USBNET_HOST_ADDR | |
2362 | strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); | |
2363 | #endif | |
2364 | /* Check if the user overruled the MAC addresses */ | |
2365 | if (getenv("usbnet_devaddr")) | |
2366 | strlcpy(dev_addr, getenv("usbnet_devaddr"), | |
2367 | sizeof(dev_addr)); | |
2368 | ||
2369 | if (getenv("usbnet_hostaddr")) | |
2370 | strlcpy(host_addr, getenv("usbnet_hostaddr"), | |
2371 | sizeof(host_addr)); | |
2372 | ||
2373 | if (!is_eth_addr_valid(dev_addr)) { | |
2374 | error("Need valid 'usbnet_devaddr' to be set"); | |
2375 | goto fail; | |
2376 | } | |
2377 | if (!is_eth_addr_valid(host_addr)) { | |
2378 | error("Need valid 'usbnet_hostaddr' to be set"); | |
2379 | goto fail; | |
2380 | } | |
2381 | ||
5cb3b9d7 M |
2382 | l_priv->eth_driver.speed = DEVSPEED; |
2383 | l_priv->eth_driver.bind = eth_bind; | |
2384 | l_priv->eth_driver.unbind = eth_unbind; | |
2385 | l_priv->eth_driver.setup = eth_setup; | |
2386 | l_priv->eth_driver.reset = eth_disconnect; | |
2387 | l_priv->eth_driver.disconnect = eth_disconnect; | |
2388 | l_priv->eth_driver.suspend = eth_suspend; | |
2389 | l_priv->eth_driver.resume = eth_resume; | |
2390 | if (usb_gadget_register_driver(&l_priv->eth_driver) < 0) | |
988ee3e3 | 2391 | goto fail; |
23cd1385 RB |
2392 | |
2393 | dev->network_started = 0; | |
23cd1385 RB |
2394 | |
2395 | packet_received = 0; | |
2396 | packet_sent = 0; | |
2397 | ||
2398 | gadget = dev->gadget; | |
2399 | usb_gadget_connect(gadget); | |
2400 | ||
2401 | if (getenv("cdc_connect_timeout")) | |
2402 | timeout = simple_strtoul(getenv("cdc_connect_timeout"), | |
2403 | NULL, 10) * CONFIG_SYS_HZ; | |
2404 | ts = get_timer(0); | |
17b4f308 | 2405 | while (!dev->network_started) { |
23cd1385 RB |
2406 | /* Handle control-c and timeouts */ |
2407 | if (ctrlc() || (get_timer(ts) > timeout)) { | |
7de73185 | 2408 | error("The remote end did not respond in time."); |
23cd1385 RB |
2409 | goto fail; |
2410 | } | |
2d48aa69 | 2411 | usb_gadget_handle_interrupts(0); |
23cd1385 RB |
2412 | } |
2413 | ||
98fae970 | 2414 | packet_received = 0; |
6142e0ae | 2415 | rx_submit(dev, dev->rx_req, 0); |
23cd1385 RB |
2416 | return 0; |
2417 | fail: | |
2418 | return -1; | |
2419 | } | |
2420 | ||
10cbe3b6 | 2421 | static int usb_eth_send(struct eth_device *netdev, void *packet, int length) |
23cd1385 RB |
2422 | { |
2423 | int retval; | |
7612a43d | 2424 | void *rndis_pkt = NULL; |
5cb3b9d7 | 2425 | struct eth_dev *dev = &l_priv->ethdev; |
ac5d32d1 | 2426 | struct usb_request *req = dev->tx_req; |
6142e0ae VK |
2427 | unsigned long ts; |
2428 | unsigned long timeout = USB_CONNECT_TIMEOUT; | |
23cd1385 | 2429 | |
7de73185 | 2430 | debug("%s:...\n", __func__); |
23cd1385 | 2431 | |
7612a43d VK |
2432 | /* new buffer is needed to include RNDIS header */ |
2433 | if (rndis_active(dev)) { | |
2434 | rndis_pkt = malloc(length + | |
2435 | sizeof(struct rndis_packet_msg_type)); | |
2436 | if (!rndis_pkt) { | |
2437 | error("No memory to alloc RNDIS packet"); | |
2438 | goto drop; | |
2439 | } | |
2440 | rndis_add_hdr(rndis_pkt, length); | |
2441 | memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type), | |
10cbe3b6 | 2442 | packet, length); |
7612a43d VK |
2443 | packet = rndis_pkt; |
2444 | length += sizeof(struct rndis_packet_msg_type); | |
2445 | } | |
10cbe3b6 | 2446 | req->buf = packet; |
23cd1385 RB |
2447 | req->context = NULL; |
2448 | req->complete = tx_complete; | |
2449 | ||
6142e0ae VK |
2450 | /* |
2451 | * use zlp framing on tx for strict CDC-Ether conformance, | |
23cd1385 RB |
2452 | * though any robust network rx path ignores extra padding. |
2453 | * and some hardware doesn't like to write zlps. | |
2454 | */ | |
2455 | req->zero = 1; | |
2456 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) | |
2457 | length++; | |
2458 | ||
2459 | req->length = length; | |
2460 | #if 0 | |
2461 | /* throttle highspeed IRQ rate back slightly */ | |
2462 | if (gadget_is_dualspeed(dev->gadget)) | |
2463 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) | |
2464 | ? ((dev->tx_qlen % qmult) != 0) : 0; | |
2465 | #endif | |
6142e0ae VK |
2466 | dev->tx_qlen = 1; |
2467 | ts = get_timer(0); | |
2468 | packet_sent = 0; | |
23cd1385 | 2469 | |
6142e0ae | 2470 | retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); |
23cd1385 RB |
2471 | |
2472 | if (!retval) | |
7de73185 | 2473 | debug("%s: packet queued\n", __func__); |
6142e0ae VK |
2474 | while (!packet_sent) { |
2475 | if (get_timer(ts) > timeout) { | |
2476 | printf("timeout sending packets to usb ethernet\n"); | |
2477 | return -1; | |
2478 | } | |
2d48aa69 | 2479 | usb_gadget_handle_interrupts(0); |
23cd1385 | 2480 | } |
7612a43d VK |
2481 | if (rndis_pkt) |
2482 | free(rndis_pkt); | |
23cd1385 RB |
2483 | |
2484 | return 0; | |
7612a43d VK |
2485 | drop: |
2486 | dev->stats.tx_dropped++; | |
2487 | return -ENOMEM; | |
23cd1385 RB |
2488 | } |
2489 | ||
6142e0ae | 2490 | static int usb_eth_recv(struct eth_device *netdev) |
23cd1385 | 2491 | { |
5cb3b9d7 | 2492 | struct eth_dev *dev = &l_priv->ethdev; |
23cd1385 | 2493 | |
2d48aa69 | 2494 | usb_gadget_handle_interrupts(0); |
23cd1385 | 2495 | |
6142e0ae VK |
2496 | if (packet_received) { |
2497 | debug("%s: packet received\n", __func__); | |
2498 | if (dev->rx_req) { | |
1fd92db8 JH |
2499 | net_process_received_packet(net_rx_packets[0], |
2500 | dev->rx_req->length); | |
6142e0ae | 2501 | packet_received = 0; |
23cd1385 | 2502 | |
ac5d32d1 | 2503 | rx_submit(dev, dev->rx_req, 0); |
6142e0ae VK |
2504 | } else |
2505 | error("dev->rx_req invalid"); | |
23cd1385 RB |
2506 | } |
2507 | return 0; | |
2508 | } | |
2509 | ||
6142e0ae | 2510 | void usb_eth_halt(struct eth_device *netdev) |
23cd1385 | 2511 | { |
5cb3b9d7 | 2512 | struct eth_dev *dev = &l_priv->ethdev; |
23cd1385 | 2513 | |
6142e0ae | 2514 | if (!netdev) { |
7de73185 | 2515 | error("received NULL ptr"); |
23cd1385 RB |
2516 | return; |
2517 | } | |
2518 | ||
988ee3e3 LW |
2519 | /* If the gadget not registered, simple return */ |
2520 | if (!dev->gadget) | |
2521 | return; | |
2522 | ||
e4ae6660 VK |
2523 | /* |
2524 | * Some USB controllers may need additional deinitialization here | |
2525 | * before dropping pull-up (also due to hardware issues). | |
2526 | * For example: unhandled interrupt with status stage started may | |
2527 | * bring the controller to fully broken state (until board reset). | |
2528 | * There are some variants to debug and fix such cases: | |
2529 | * 1) In the case of RNDIS connection eth_stop can perform additional | |
2530 | * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition. | |
2531 | * 2) 'pullup' callback in your UDC driver can be improved to perform | |
2532 | * this deinitialization. | |
2533 | */ | |
7612a43d VK |
2534 | eth_stop(dev); |
2535 | ||
23cd1385 | 2536 | usb_gadget_disconnect(dev->gadget); |
b3649f3b VK |
2537 | |
2538 | /* Clear pending interrupt */ | |
2539 | if (dev->network_started) { | |
2d48aa69 | 2540 | usb_gadget_handle_interrupts(0); |
b3649f3b VK |
2541 | dev->network_started = 0; |
2542 | } | |
2543 | ||
5cb3b9d7 | 2544 | usb_gadget_unregister_driver(&l_priv->eth_driver); |
d4345aee M |
2545 | #ifdef CONFIG_DM_USB |
2546 | device_remove(dev->usb_udev); | |
2547 | #else | |
8bfc288c | 2548 | board_usb_cleanup(0, USB_INIT_DEVICE); |
d4345aee | 2549 | #endif |
23cd1385 RB |
2550 | } |
2551 | ||
23cd1385 RB |
2552 | int usb_eth_initialize(bd_t *bi) |
2553 | { | |
5cb3b9d7 | 2554 | struct eth_device *netdev = &l_priv->netdev; |
23cd1385 | 2555 | |
8f7aa831 | 2556 | strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name)); |
23cd1385 RB |
2557 | |
2558 | netdev->init = usb_eth_init; | |
2559 | netdev->send = usb_eth_send; | |
2560 | netdev->recv = usb_eth_recv; | |
2561 | netdev->halt = usb_eth_halt; | |
2562 | ||
2563 | #ifdef CONFIG_MCAST_TFTP | |
2564 | #error not supported | |
2565 | #endif | |
23cd1385 RB |
2566 | eth_register(netdev); |
2567 | return 0; | |
23cd1385 | 2568 | } |