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