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