1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/kernel.h>
25#include <linux/utsname.h>
26#include <linux/device.h>
27#include <linux/ctype.h>
28#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30
31#include <linux/usb/ch9.h>
32#include <linux/usb/cdc.h>
33#include <linux/usb/gadget.h>
34
35#include "gadget_chips.h"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#define DRIVER_DESC "Ethernet Gadget"
71#define DRIVER_VERSION "May Day 2005"
72
73static const char shortname [] = "ether";
74static const char driver_desc [] = DRIVER_DESC;
75
76#define RX_EXTRA 20
77
78#include "rndis.h"
79
80#ifndef CONFIG_USB_ETH_RNDIS
81#define rndis_uninit(x) do{}while(0)
82#define rndis_deregister(c) do{}while(0)
83#define rndis_exit() do{}while(0)
84#endif
85
86
87#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
88 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
89 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
90 |USB_CDC_PACKET_TYPE_DIRECTED)
91
92
93
94
95struct eth_dev {
96 spinlock_t lock;
97 struct usb_gadget *gadget;
98 struct usb_request *req;
99 struct usb_request *stat_req;
100
101 u8 config;
102 struct usb_ep *in_ep, *out_ep, *status_ep;
103 const struct usb_endpoint_descriptor
104 *in, *out, *status;
105
106 spinlock_t req_lock;
107 struct list_head tx_reqs, rx_reqs;
108
109 struct net_device *net;
110 struct net_device_stats stats;
111 atomic_t tx_qlen;
112
113 struct work_struct work;
114 unsigned zlp:1;
115 unsigned cdc:1;
116 unsigned rndis:1;
117 unsigned suspended:1;
118 u16 cdc_filter;
119 unsigned long todo;
120#define WORK_RX_MEMORY 0
121 int rndis_config;
122 u8 host_mac [ETH_ALEN];
123};
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140#define CDC_VENDOR_NUM 0x0525
141#define CDC_PRODUCT_NUM 0xa4a1
142
143
144
145
146
147
148
149
150
151
152
153
154#define SIMPLE_VENDOR_NUM 0x049f
155#define SIMPLE_PRODUCT_NUM 0x505a
156
157
158
159
160
161
162#define RNDIS_VENDOR_NUM 0x0525
163#define RNDIS_PRODUCT_NUM 0xa4a2
164
165
166
167
168
169
170
171static ushort idVendor;
172module_param(idVendor, ushort, S_IRUGO);
173MODULE_PARM_DESC(idVendor, "USB Vendor ID");
174
175static ushort idProduct;
176module_param(idProduct, ushort, S_IRUGO);
177MODULE_PARM_DESC(idProduct, "USB Product ID");
178
179static ushort bcdDevice;
180module_param(bcdDevice, ushort, S_IRUGO);
181MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
182
183static char *iManufacturer;
184module_param(iManufacturer, charp, S_IRUGO);
185MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
186
187static char *iProduct;
188module_param(iProduct, charp, S_IRUGO);
189MODULE_PARM_DESC(iProduct, "USB Product string");
190
191static char *iSerialNumber;
192module_param(iSerialNumber, charp, S_IRUGO);
193MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
194
195
196static char *dev_addr;
197module_param(dev_addr, charp, S_IRUGO);
198MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
199
200
201static char *host_addr;
202module_param(host_addr, charp, S_IRUGO);
203MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
204
205
206
207
208
209
210#ifdef CONFIG_USB_GADGET_NET2280
211#define DEV_CONFIG_CDC
212#endif
213
214#ifdef CONFIG_USB_GADGET_DUMMY_HCD
215#define DEV_CONFIG_CDC
216#endif
217
218#ifdef CONFIG_USB_GADGET_GOKU
219#define DEV_CONFIG_CDC
220#endif
221
222#ifdef CONFIG_USB_GADGET_LH7A40X
223#define DEV_CONFIG_CDC
224#endif
225
226#ifdef CONFIG_USB_GADGET_MQ11XX
227#define DEV_CONFIG_CDC
228#endif
229
230#ifdef CONFIG_USB_GADGET_OMAP
231#define DEV_CONFIG_CDC
232#endif
233
234#ifdef CONFIG_USB_GADGET_N9604
235#define DEV_CONFIG_CDC
236#endif
237
238#ifdef CONFIG_USB_GADGET_PXA27X
239#define DEV_CONFIG_CDC
240#endif
241
242#ifdef CONFIG_USB_GADGET_S3C2410
243#define DEV_CONFIG_CDC
244#endif
245
246#ifdef CONFIG_USB_GADGET_AT91
247#define DEV_CONFIG_CDC
248#endif
249
250#ifdef CONFIG_USB_GADGET_MUSBHSFC
251#define DEV_CONFIG_CDC
252#endif
253
254#ifdef CONFIG_USB_GADGET_MUSB_HDRC
255#define DEV_CONFIG_CDC
256#endif
257
258#ifdef CONFIG_USB_GADGET_ATMEL_USBA
259#define DEV_CONFIG_CDC
260#endif
261
262#ifdef CONFIG_USB_GADGET_FSL_USB2
263#define DEV_CONFIG_CDC
264#endif
265
266
267
268
269#ifdef CONFIG_USB_GADGET_PXA2XX
270#define DEV_CONFIG_SUBSET
271#endif
272
273#ifdef CONFIG_USB_GADGET_SUPERH
274#define DEV_CONFIG_SUBSET
275#endif
276
277#ifdef CONFIG_USB_GADGET_SA1100
278
279#define DEV_CONFIG_SUBSET
280#endif
281
282#ifdef CONFIG_USB_GADGET_M66592
283#define DEV_CONFIG_CDC
284#endif
285
286#ifdef CONFIG_USB_GADGET_AMD5536UDC
287#define DEV_CONFIG_CDC
288#endif
289
290
291
292
293
294static inline int is_cdc(struct eth_dev *dev)
295{
296#if !defined(DEV_CONFIG_SUBSET)
297 return 1;
298#elif !defined (DEV_CONFIG_CDC)
299 return 0;
300#else
301 return dev->cdc;
302#endif
303}
304
305
306static inline int rndis_active(struct eth_dev *dev)
307{
308#ifdef CONFIG_USB_ETH_RNDIS
309 return dev->rndis;
310#else
311 return 0;
312#endif
313}
314
315#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
316#define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
317
318
319
320#define DEFAULT_QLEN 2
321
322
323#define HS_BPS (13 * 512 * 8 * 1000 * 8)
324#define FS_BPS (19 * 64 * 1 * 1000 * 8)
325
326#ifdef CONFIG_USB_GADGET_DUALSPEED
327#define DEVSPEED USB_SPEED_HIGH
328
329static unsigned qmult = 5;
330module_param (qmult, uint, S_IRUGO|S_IWUSR);
331
332
333
334#define qlen(gadget) \
335 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
336
337static inline int BITRATE(struct usb_gadget *g)
338{
339 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
340}
341
342#else
343
344#define qmult 1
345
346#define DEVSPEED USB_SPEED_FULL
347
348#define qlen(gadget) DEFAULT_QLEN
349
350static inline int BITRATE(struct usb_gadget *g)
351{
352 return FS_BPS;
353}
354#endif
355
356
357
358
359#define xprintk(d,level,fmt,args...) \
360 printk(level "%s: " fmt , (d)->net->name , ## args)
361
362#ifdef DEBUG
363#undef DEBUG
364#define DEBUG(dev,fmt,args...) \
365 xprintk(dev , KERN_DEBUG , fmt , ## args)
366#else
367#define DEBUG(dev,fmt,args...) \
368 do { } while (0)
369#endif
370
371#ifdef VERBOSE_DEBUG
372#define VDEBUG DEBUG
373#else
374#define VDEBUG(dev,fmt,args...) \
375 do { } while (0)
376#endif
377
378#define ERROR(dev,fmt,args...) \
379 xprintk(dev , KERN_ERR , fmt , ## args)
380#define WARN(dev,fmt,args...) \
381 xprintk(dev , KERN_WARNING , fmt , ## args)
382#define INFO(dev,fmt,args...) \
383 xprintk(dev , KERN_INFO , fmt , ## args)
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403#define STRING_MANUFACTURER 1
404#define STRING_PRODUCT 2
405#define STRING_ETHADDR 3
406#define STRING_DATA 4
407#define STRING_CONTROL 5
408#define STRING_RNDIS_CONTROL 6
409#define STRING_CDC 7
410#define STRING_SUBSET 8
411#define STRING_RNDIS 9
412#define STRING_SERIALNUMBER 10
413
414
415#define USB_BUFSIZ 256
416
417
418
419
420
421
422
423
424
425
426
427
428#define DEV_CONFIG_VALUE 1
429#define DEV_RNDIS_CONFIG_VALUE 2
430
431static struct usb_device_descriptor
432device_desc = {
433 .bLength = sizeof device_desc,
434 .bDescriptorType = USB_DT_DEVICE,
435
436 .bcdUSB = __constant_cpu_to_le16 (0x0200),
437
438 .bDeviceClass = USB_CLASS_COMM,
439 .bDeviceSubClass = 0,
440 .bDeviceProtocol = 0,
441
442 .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
443 .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
444 .iManufacturer = STRING_MANUFACTURER,
445 .iProduct = STRING_PRODUCT,
446 .bNumConfigurations = 1,
447};
448
449static struct usb_otg_descriptor
450otg_descriptor = {
451 .bLength = sizeof otg_descriptor,
452 .bDescriptorType = USB_DT_OTG,
453
454 .bmAttributes = USB_OTG_SRP,
455};
456
457static struct usb_config_descriptor
458eth_config = {
459 .bLength = sizeof eth_config,
460 .bDescriptorType = USB_DT_CONFIG,
461
462
463 .bNumInterfaces = 2,
464 .bConfigurationValue = DEV_CONFIG_VALUE,
465 .iConfiguration = STRING_CDC,
466 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
467 .bMaxPower = 50,
468};
469
470#ifdef CONFIG_USB_ETH_RNDIS
471static struct usb_config_descriptor
472rndis_config = {
473 .bLength = sizeof rndis_config,
474 .bDescriptorType = USB_DT_CONFIG,
475
476
477 .bNumInterfaces = 2,
478 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
479 .iConfiguration = STRING_RNDIS,
480 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
481 .bMaxPower = 50,
482};
483#endif
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504#ifdef DEV_CONFIG_CDC
505static struct usb_interface_descriptor
506control_intf = {
507 .bLength = sizeof control_intf,
508 .bDescriptorType = USB_DT_INTERFACE,
509
510 .bInterfaceNumber = 0,
511
512 .bNumEndpoints = 1,
513 .bInterfaceClass = USB_CLASS_COMM,
514 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
515 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
516 .iInterface = STRING_CONTROL,
517};
518#endif
519
520#ifdef CONFIG_USB_ETH_RNDIS
521static const struct usb_interface_descriptor
522rndis_control_intf = {
523 .bLength = sizeof rndis_control_intf,
524 .bDescriptorType = USB_DT_INTERFACE,
525
526 .bInterfaceNumber = 0,
527 .bNumEndpoints = 1,
528 .bInterfaceClass = USB_CLASS_COMM,
529 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
530 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
531 .iInterface = STRING_RNDIS_CONTROL,
532};
533#endif
534
535static const struct usb_cdc_header_desc header_desc = {
536 .bLength = sizeof header_desc,
537 .bDescriptorType = USB_DT_CS_INTERFACE,
538 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
539
540 .bcdCDC = __constant_cpu_to_le16 (0x0110),
541};
542
543#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
544
545static const struct usb_cdc_union_desc union_desc = {
546 .bLength = sizeof union_desc,
547 .bDescriptorType = USB_DT_CS_INTERFACE,
548 .bDescriptorSubType = USB_CDC_UNION_TYPE,
549
550 .bMasterInterface0 = 0,
551 .bSlaveInterface0 = 1,
552};
553
554#endif
555
556#ifdef CONFIG_USB_ETH_RNDIS
557
558static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
559 .bLength = sizeof call_mgmt_descriptor,
560 .bDescriptorType = USB_DT_CS_INTERFACE,
561 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
562
563 .bmCapabilities = 0x00,
564 .bDataInterface = 0x01,
565};
566
567static const struct usb_cdc_acm_descriptor acm_descriptor = {
568 .bLength = sizeof acm_descriptor,
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_ACM_TYPE,
571
572 .bmCapabilities = 0x00,
573};
574
575#endif
576
577#ifndef DEV_CONFIG_CDC
578
579
580
581
582
583
584static const struct usb_cdc_mdlm_desc mdlm_desc = {
585 .bLength = sizeof mdlm_desc,
586 .bDescriptorType = USB_DT_CS_INTERFACE,
587 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
588
589 .bcdVersion = __constant_cpu_to_le16(0x0100),
590 .bGUID = {
591 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
592 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
593 },
594};
595
596
597
598
599
600static const u8 mdlm_detail_desc[] = {
601 6,
602 USB_DT_CS_INTERFACE,
603 USB_CDC_MDLM_DETAIL_TYPE,
604
605 0,
606 0,
607 0,
608};
609
610#endif
611
612static const struct usb_cdc_ether_desc ether_desc = {
613 .bLength = sizeof ether_desc,
614 .bDescriptorType = USB_DT_CS_INTERFACE,
615 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
616
617
618 .iMACAddress = STRING_ETHADDR,
619 .bmEthernetStatistics = __constant_cpu_to_le32 (0),
620 .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
621 .wNumberMCFilters = __constant_cpu_to_le16 (0),
622 .bNumberPowerFilters = 0,
623};
624
625
626#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642#define LOG2_STATUS_INTERVAL_MSEC 5
643#define STATUS_BYTECOUNT 16
644
645static struct usb_endpoint_descriptor
646fs_status_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
649
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_INT,
652 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
653 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
654};
655#endif
656
657#ifdef DEV_CONFIG_CDC
658
659
660
661static const struct usb_interface_descriptor
662data_nop_intf = {
663 .bLength = sizeof data_nop_intf,
664 .bDescriptorType = USB_DT_INTERFACE,
665
666 .bInterfaceNumber = 1,
667 .bAlternateSetting = 0,
668 .bNumEndpoints = 0,
669 .bInterfaceClass = USB_CLASS_CDC_DATA,
670 .bInterfaceSubClass = 0,
671 .bInterfaceProtocol = 0,
672};
673
674
675
676static const struct usb_interface_descriptor
677data_intf = {
678 .bLength = sizeof data_intf,
679 .bDescriptorType = USB_DT_INTERFACE,
680
681 .bInterfaceNumber = 1,
682 .bAlternateSetting = 1,
683 .bNumEndpoints = 2,
684 .bInterfaceClass = USB_CLASS_CDC_DATA,
685 .bInterfaceSubClass = 0,
686 .bInterfaceProtocol = 0,
687 .iInterface = STRING_DATA,
688};
689
690#endif
691
692#ifdef CONFIG_USB_ETH_RNDIS
693
694
695
696static const struct usb_interface_descriptor
697rndis_data_intf = {
698 .bLength = sizeof rndis_data_intf,
699 .bDescriptorType = USB_DT_INTERFACE,
700
701 .bInterfaceNumber = 1,
702 .bAlternateSetting = 0,
703 .bNumEndpoints = 2,
704 .bInterfaceClass = USB_CLASS_CDC_DATA,
705 .bInterfaceSubClass = 0,
706 .bInterfaceProtocol = 0,
707 .iInterface = STRING_DATA,
708};
709
710#endif
711
712#ifdef DEV_CONFIG_SUBSET
713
714
715
716
717
718
719
720
721
722static const struct usb_interface_descriptor
723subset_data_intf = {
724 .bLength = sizeof subset_data_intf,
725 .bDescriptorType = USB_DT_INTERFACE,
726
727 .bInterfaceNumber = 0,
728 .bAlternateSetting = 0,
729 .bNumEndpoints = 2,
730 .bInterfaceClass = USB_CLASS_COMM,
731 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
732 .bInterfaceProtocol = 0,
733 .iInterface = STRING_DATA,
734};
735
736#endif
737
738
739static struct usb_endpoint_descriptor
740fs_source_desc = {
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
743
744 .bEndpointAddress = USB_DIR_IN,
745 .bmAttributes = USB_ENDPOINT_XFER_BULK,
746};
747
748static struct usb_endpoint_descriptor
749fs_sink_desc = {
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
752
753 .bEndpointAddress = USB_DIR_OUT,
754 .bmAttributes = USB_ENDPOINT_XFER_BULK,
755};
756
757static const struct usb_descriptor_header *fs_eth_function [11] = {
758 (struct usb_descriptor_header *) &otg_descriptor,
759#ifdef DEV_CONFIG_CDC
760
761 (struct usb_descriptor_header *) &control_intf,
762 (struct usb_descriptor_header *) &header_desc,
763 (struct usb_descriptor_header *) &union_desc,
764 (struct usb_descriptor_header *) ðer_desc,
765
766 (struct usb_descriptor_header *) &fs_status_desc,
767
768 (struct usb_descriptor_header *) &data_nop_intf,
769 (struct usb_descriptor_header *) &data_intf,
770 (struct usb_descriptor_header *) &fs_source_desc,
771 (struct usb_descriptor_header *) &fs_sink_desc,
772 NULL,
773#endif
774};
775
776static inline void __init fs_subset_descriptors(void)
777{
778#ifdef DEV_CONFIG_SUBSET
779
780 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
781 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
782 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
783 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
784 fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
785 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
786 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
787 fs_eth_function[8] = NULL;
788#else
789 fs_eth_function[1] = NULL;
790#endif
791}
792
793#ifdef CONFIG_USB_ETH_RNDIS
794static const struct usb_descriptor_header *fs_rndis_function [] = {
795 (struct usb_descriptor_header *) &otg_descriptor,
796
797 (struct usb_descriptor_header *) &rndis_control_intf,
798 (struct usb_descriptor_header *) &header_desc,
799 (struct usb_descriptor_header *) &call_mgmt_descriptor,
800 (struct usb_descriptor_header *) &acm_descriptor,
801 (struct usb_descriptor_header *) &union_desc,
802 (struct usb_descriptor_header *) &fs_status_desc,
803
804 (struct usb_descriptor_header *) &rndis_data_intf,
805 (struct usb_descriptor_header *) &fs_source_desc,
806 (struct usb_descriptor_header *) &fs_sink_desc,
807 NULL,
808};
809#endif
810
811
812
813
814
815
816#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
817static struct usb_endpoint_descriptor
818hs_status_desc = {
819 .bLength = USB_DT_ENDPOINT_SIZE,
820 .bDescriptorType = USB_DT_ENDPOINT,
821
822 .bmAttributes = USB_ENDPOINT_XFER_INT,
823 .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
824 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
825};
826#endif
827
828static struct usb_endpoint_descriptor
829hs_source_desc = {
830 .bLength = USB_DT_ENDPOINT_SIZE,
831 .bDescriptorType = USB_DT_ENDPOINT,
832
833 .bmAttributes = USB_ENDPOINT_XFER_BULK,
834 .wMaxPacketSize = __constant_cpu_to_le16 (512),
835};
836
837static struct usb_endpoint_descriptor
838hs_sink_desc = {
839 .bLength = USB_DT_ENDPOINT_SIZE,
840 .bDescriptorType = USB_DT_ENDPOINT,
841
842 .bmAttributes = USB_ENDPOINT_XFER_BULK,
843 .wMaxPacketSize = __constant_cpu_to_le16 (512),
844};
845
846static struct usb_qualifier_descriptor
847dev_qualifier = {
848 .bLength = sizeof dev_qualifier,
849 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
850
851 .bcdUSB = __constant_cpu_to_le16 (0x0200),
852 .bDeviceClass = USB_CLASS_COMM,
853
854 .bNumConfigurations = 1,
855};
856
857static const struct usb_descriptor_header *hs_eth_function [11] = {
858 (struct usb_descriptor_header *) &otg_descriptor,
859#ifdef DEV_CONFIG_CDC
860
861 (struct usb_descriptor_header *) &control_intf,
862 (struct usb_descriptor_header *) &header_desc,
863 (struct usb_descriptor_header *) &union_desc,
864 (struct usb_descriptor_header *) ðer_desc,
865
866 (struct usb_descriptor_header *) &hs_status_desc,
867
868 (struct usb_descriptor_header *) &data_nop_intf,
869 (struct usb_descriptor_header *) &data_intf,
870 (struct usb_descriptor_header *) &hs_source_desc,
871 (struct usb_descriptor_header *) &hs_sink_desc,
872 NULL,
873#endif
874};
875
876static inline void __init hs_subset_descriptors(void)
877{
878#ifdef DEV_CONFIG_SUBSET
879
880 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
881 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
882 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
883 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
884 hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
885 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
886 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
887 hs_eth_function[8] = NULL;
888#else
889 hs_eth_function[1] = NULL;
890#endif
891}
892
893#ifdef CONFIG_USB_ETH_RNDIS
894static const struct usb_descriptor_header *hs_rndis_function [] = {
895 (struct usb_descriptor_header *) &otg_descriptor,
896
897 (struct usb_descriptor_header *) &rndis_control_intf,
898 (struct usb_descriptor_header *) &header_desc,
899 (struct usb_descriptor_header *) &call_mgmt_descriptor,
900 (struct usb_descriptor_header *) &acm_descriptor,
901 (struct usb_descriptor_header *) &union_desc,
902 (struct usb_descriptor_header *) &hs_status_desc,
903
904 (struct usb_descriptor_header *) &rndis_data_intf,
905 (struct usb_descriptor_header *) &hs_source_desc,
906 (struct usb_descriptor_header *) &hs_sink_desc,
907 NULL,
908};
909#endif
910
911
912
913static inline struct usb_endpoint_descriptor *
914ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
915 struct usb_endpoint_descriptor *fs)
916{
917 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
918 return hs;
919 return fs;
920}
921
922
923
924
925
926
927static char manufacturer [50];
928static char product_desc [40] = DRIVER_DESC;
929static char serial_number [20];
930
931
932static char ethaddr [2 * ETH_ALEN + 1];
933
934
935static struct usb_string strings [] = {
936 { STRING_MANUFACTURER, manufacturer, },
937 { STRING_PRODUCT, product_desc, },
938 { STRING_SERIALNUMBER, serial_number, },
939 { STRING_DATA, "Ethernet Data", },
940 { STRING_ETHADDR, ethaddr, },
941#ifdef DEV_CONFIG_CDC
942 { STRING_CDC, "CDC Ethernet", },
943 { STRING_CONTROL, "CDC Communications Control", },
944#endif
945#ifdef DEV_CONFIG_SUBSET
946 { STRING_SUBSET, "CDC Ethernet Subset", },
947#endif
948#ifdef CONFIG_USB_ETH_RNDIS
949 { STRING_RNDIS, "RNDIS", },
950 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
951#endif
952 { }
953};
954
955static struct usb_gadget_strings stringtab = {
956 .language = 0x0409,
957 .strings = strings,
958};
959
960
961
962
963
964static int
965config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
966{
967 int len;
968 const struct usb_config_descriptor *config;
969 const struct usb_descriptor_header **function;
970 int hs = 0;
971
972 if (gadget_is_dualspeed(g)) {
973 hs = (g->speed == USB_SPEED_HIGH);
974 if (type == USB_DT_OTHER_SPEED_CONFIG)
975 hs = !hs;
976 }
977#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
978
979 if (index >= device_desc.bNumConfigurations)
980 return -EINVAL;
981
982#ifdef CONFIG_USB_ETH_RNDIS
983
984
985
986 if (device_desc.bNumConfigurations == 2 && index == 0) {
987 config = &rndis_config;
988 function = which_fn (rndis);
989 } else
990#endif
991 {
992 config = ð_config;
993 function = which_fn (eth);
994 }
995
996
997 if (!is_otg)
998 function++;
999
1000 len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1001 if (len < 0)
1002 return len;
1003 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1004 return len;
1005}
1006
1007
1008
1009static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
1010static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
1011
1012static int
1013set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
1014{
1015 int result = 0;
1016 struct usb_gadget *gadget = dev->gadget;
1017
1018#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1019
1020 if (!subset_active(dev) && dev->status_ep) {
1021 dev->status = ep_desc (gadget, &hs_status_desc,
1022 &fs_status_desc);
1023 dev->status_ep->driver_data = dev;
1024
1025 result = usb_ep_enable (dev->status_ep, dev->status);
1026 if (result != 0) {
1027 DEBUG (dev, "enable %s --> %d\n",
1028 dev->status_ep->name, result);
1029 goto done;
1030 }
1031 }
1032#endif
1033
1034 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
1035 dev->in_ep->driver_data = dev;
1036
1037 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
1038 dev->out_ep->driver_data = dev;
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048 if (!cdc_active(dev)) {
1049 result = usb_ep_enable (dev->in_ep, dev->in);
1050 if (result != 0) {
1051 DEBUG(dev, "enable %s --> %d\n",
1052 dev->in_ep->name, result);
1053 goto done;
1054 }
1055
1056 result = usb_ep_enable (dev->out_ep, dev->out);
1057 if (result != 0) {
1058 DEBUG (dev, "enable %s --> %d\n",
1059 dev->out_ep->name, result);
1060 goto done;
1061 }
1062 }
1063
1064done:
1065 if (result == 0)
1066 result = alloc_requests (dev, qlen (gadget), gfp_flags);
1067
1068
1069 if (result < 0) {
1070 if (!subset_active(dev))
1071 (void) usb_ep_disable (dev->status_ep);
1072 dev->status = NULL;
1073 (void) usb_ep_disable (dev->in_ep);
1074 (void) usb_ep_disable (dev->out_ep);
1075 dev->in = NULL;
1076 dev->out = NULL;
1077 } else
1078
1079
1080
1081
1082 if (!cdc_active (dev)) {
1083 netif_carrier_on (dev->net);
1084 if (netif_running (dev->net)) {
1085 spin_unlock (&dev->lock);
1086 eth_start (dev, GFP_ATOMIC);
1087 spin_lock (&dev->lock);
1088 }
1089 }
1090
1091 if (result == 0)
1092 DEBUG (dev, "qlen %d\n", qlen (gadget));
1093
1094
1095 return result;
1096}
1097
1098static void eth_reset_config (struct eth_dev *dev)
1099{
1100 struct usb_request *req;
1101
1102 if (dev->config == 0)
1103 return;
1104
1105 DEBUG (dev, "%s\n", __FUNCTION__);
1106
1107 netif_stop_queue (dev->net);
1108 netif_carrier_off (dev->net);
1109 rndis_uninit(dev->rndis_config);
1110
1111
1112
1113
1114 if (dev->in) {
1115 usb_ep_disable (dev->in_ep);
1116 spin_lock(&dev->req_lock);
1117 while (likely (!list_empty (&dev->tx_reqs))) {
1118 req = container_of (dev->tx_reqs.next,
1119 struct usb_request, list);
1120 list_del (&req->list);
1121
1122 spin_unlock(&dev->req_lock);
1123 usb_ep_free_request (dev->in_ep, req);
1124 spin_lock(&dev->req_lock);
1125 }
1126 spin_unlock(&dev->req_lock);
1127 }
1128 if (dev->out) {
1129 usb_ep_disable (dev->out_ep);
1130 spin_lock(&dev->req_lock);
1131 while (likely (!list_empty (&dev->rx_reqs))) {
1132 req = container_of (dev->rx_reqs.next,
1133 struct usb_request, list);
1134 list_del (&req->list);
1135
1136 spin_unlock(&dev->req_lock);
1137 usb_ep_free_request (dev->out_ep, req);
1138 spin_lock(&dev->req_lock);
1139 }
1140 spin_unlock(&dev->req_lock);
1141 }
1142
1143 if (dev->status) {
1144 usb_ep_disable (dev->status_ep);
1145 }
1146 dev->rndis = 0;
1147 dev->cdc_filter = 0;
1148 dev->config = 0;
1149}
1150
1151
1152
1153
1154static int
1155eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
1156{
1157 int result = 0;
1158 struct usb_gadget *gadget = dev->gadget;
1159
1160 if (gadget_is_sa1100 (gadget)
1161 && dev->config
1162 && atomic_read (&dev->tx_qlen) != 0) {
1163
1164 INFO (dev, "can't change configurations\n");
1165 return -ESPIPE;
1166 }
1167 eth_reset_config (dev);
1168
1169 switch (number) {
1170 case DEV_CONFIG_VALUE:
1171 result = set_ether_config (dev, gfp_flags);
1172 break;
1173#ifdef CONFIG_USB_ETH_RNDIS
1174 case DEV_RNDIS_CONFIG_VALUE:
1175 dev->rndis = 1;
1176 result = set_ether_config (dev, gfp_flags);
1177 break;
1178#endif
1179 default:
1180 result = -EINVAL;
1181
1182 case 0:
1183 break;
1184 }
1185
1186 if (result) {
1187 if (number)
1188 eth_reset_config (dev);
1189 usb_gadget_vbus_draw(dev->gadget,
1190 gadget_is_otg(dev->gadget) ? 8 : 100);
1191 } else {
1192 char *speed;
1193 unsigned power;
1194
1195 power = 2 * eth_config.bMaxPower;
1196 usb_gadget_vbus_draw(dev->gadget, power);
1197
1198 switch (gadget->speed) {
1199 case USB_SPEED_FULL: speed = "full"; break;
1200#ifdef CONFIG_USB_GADGET_DUALSPEED
1201 case USB_SPEED_HIGH: speed = "high"; break;
1202#endif
1203 default: speed = "?"; break;
1204 }
1205
1206 dev->config = number;
1207 INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
1208 speed, number, power, driver_desc,
1209 rndis_active(dev)
1210 ? "RNDIS"
1211 : (cdc_active(dev)
1212 ? "CDC Ethernet"
1213 : "CDC Ethernet Subset"));
1214 }
1215 return result;
1216}
1217
1218
1219
1220#ifdef DEV_CONFIG_CDC
1221
1222
1223
1224
1225
1226
1227
1228
1229static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
1230{
1231 struct usb_cdc_notification *event = req->buf;
1232 int value = req->status;
1233 struct eth_dev *dev = ep->driver_data;
1234
1235
1236 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1237 && value == 0) {
1238 __le32 *data = req->buf + sizeof *event;
1239
1240 event->bmRequestType = 0xA1;
1241 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
1242 event->wValue = __constant_cpu_to_le16 (0);
1243 event->wIndex = __constant_cpu_to_le16 (1);
1244 event->wLength = __constant_cpu_to_le16 (8);
1245
1246
1247 data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
1248
1249 req->length = STATUS_BYTECOUNT;
1250 value = usb_ep_queue (ep, req, GFP_ATOMIC);
1251 DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
1252 if (value == 0)
1253 return;
1254 } else if (value != -ECONNRESET)
1255 DEBUG (dev, "event %02x --> %d\n",
1256 event->bNotificationType, value);
1257 req->context = NULL;
1258}
1259
1260static void issue_start_status (struct eth_dev *dev)
1261{
1262 struct usb_request *req = dev->stat_req;
1263 struct usb_cdc_notification *event;
1264 int value;
1265
1266 DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277 usb_ep_disable (dev->status_ep);
1278 usb_ep_enable (dev->status_ep, dev->status);
1279
1280
1281
1282
1283 event = req->buf;
1284 event->bmRequestType = 0xA1;
1285 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
1286 event->wValue = __constant_cpu_to_le16 (1);
1287 event->wIndex = __constant_cpu_to_le16 (1);
1288 event->wLength = 0;
1289
1290 req->length = sizeof *event;
1291 req->complete = eth_status_complete;
1292 req->context = dev;
1293
1294 value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
1295 if (value < 0)
1296 DEBUG (dev, "status buf queue --> %d\n", value);
1297}
1298
1299#endif
1300
1301
1302
1303static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
1304{
1305 if (req->status || req->actual != req->length)
1306 DEBUG ((struct eth_dev *) ep->driver_data,
1307 "setup complete --> %d, %d/%d\n",
1308 req->status, req->actual, req->length);
1309}
1310
1311#ifdef CONFIG_USB_ETH_RNDIS
1312
1313static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
1314{
1315 if (req->status || req->actual != req->length)
1316 DEBUG ((struct eth_dev *) ep->driver_data,
1317 "rndis response complete --> %d, %d/%d\n",
1318 req->status, req->actual, req->length);
1319
1320
1321}
1322
1323static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
1324{
1325 struct eth_dev *dev = ep->driver_data;
1326 int status;
1327
1328
1329 spin_lock(&dev->lock);
1330 status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
1331 if (status < 0)
1332 ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
1333 spin_unlock(&dev->lock);
1334}
1335
1336#endif
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347static int
1348eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1349{
1350 struct eth_dev *dev = get_gadget_data (gadget);
1351 struct usb_request *req = dev->req;
1352 int value = -EOPNOTSUPP;
1353 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1354 u16 wValue = le16_to_cpu(ctrl->wValue);
1355 u16 wLength = le16_to_cpu(ctrl->wLength);
1356
1357
1358
1359
1360 req->complete = eth_setup_complete;
1361 switch (ctrl->bRequest) {
1362
1363 case USB_REQ_GET_DESCRIPTOR:
1364 if (ctrl->bRequestType != USB_DIR_IN)
1365 break;
1366 switch (wValue >> 8) {
1367
1368 case USB_DT_DEVICE:
1369 value = min (wLength, (u16) sizeof device_desc);
1370 memcpy (req->buf, &device_desc, value);
1371 break;
1372 case USB_DT_DEVICE_QUALIFIER:
1373 if (!gadget_is_dualspeed(gadget))
1374 break;
1375 value = min (wLength, (u16) sizeof dev_qualifier);
1376 memcpy (req->buf, &dev_qualifier, value);
1377 break;
1378
1379 case USB_DT_OTHER_SPEED_CONFIG:
1380 if (!gadget_is_dualspeed(gadget))
1381 break;
1382
1383 case USB_DT_CONFIG:
1384 value = config_buf(gadget, req->buf,
1385 wValue >> 8,
1386 wValue & 0xff,
1387 gadget_is_otg(gadget));
1388 if (value >= 0)
1389 value = min (wLength, (u16) value);
1390 break;
1391
1392 case USB_DT_STRING:
1393 value = usb_gadget_get_string (&stringtab,
1394 wValue & 0xff, req->buf);
1395 if (value >= 0)
1396 value = min (wLength, (u16) value);
1397 break;
1398 }
1399 break;
1400
1401 case USB_REQ_SET_CONFIGURATION:
1402 if (ctrl->bRequestType != 0)
1403 break;
1404 if (gadget->a_hnp_support)
1405 DEBUG (dev, "HNP available\n");
1406 else if (gadget->a_alt_hnp_support)
1407 DEBUG (dev, "HNP needs a different root port\n");
1408 spin_lock (&dev->lock);
1409 value = eth_set_config (dev, wValue, GFP_ATOMIC);
1410 spin_unlock (&dev->lock);
1411 break;
1412 case USB_REQ_GET_CONFIGURATION:
1413 if (ctrl->bRequestType != USB_DIR_IN)
1414 break;
1415 *(u8 *)req->buf = dev->config;
1416 value = min (wLength, (u16) 1);
1417 break;
1418
1419 case USB_REQ_SET_INTERFACE:
1420 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1421 || !dev->config
1422 || wIndex > 1)
1423 break;
1424 if (!cdc_active(dev) && wIndex != 0)
1425 break;
1426 spin_lock (&dev->lock);
1427
1428
1429
1430
1431 if (gadget_is_pxa (gadget)) {
1432 value = eth_set_config (dev, DEV_CONFIG_VALUE,
1433 GFP_ATOMIC);
1434 goto done_set_intf;
1435 }
1436
1437#ifdef DEV_CONFIG_CDC
1438 switch (wIndex) {
1439 case 0:
1440 if (wValue != 0)
1441 break;
1442 if (dev->status) {
1443 usb_ep_disable (dev->status_ep);
1444 usb_ep_enable (dev->status_ep, dev->status);
1445 }
1446 value = 0;
1447 break;
1448 case 1:
1449 if (wValue > 1)
1450 break;
1451 usb_ep_disable (dev->in_ep);
1452 usb_ep_disable (dev->out_ep);
1453
1454
1455
1456
1457
1458 if (wValue == 1) {
1459 if (!cdc_active (dev))
1460 break;
1461 usb_ep_enable (dev->in_ep, dev->in);
1462 usb_ep_enable (dev->out_ep, dev->out);
1463 dev->cdc_filter = DEFAULT_FILTER;
1464 netif_carrier_on (dev->net);
1465 if (dev->status)
1466 issue_start_status (dev);
1467 if (netif_running (dev->net)) {
1468 spin_unlock (&dev->lock);
1469 eth_start (dev, GFP_ATOMIC);
1470 spin_lock (&dev->lock);
1471 }
1472 } else {
1473 netif_stop_queue (dev->net);
1474 netif_carrier_off (dev->net);
1475 }
1476 value = 0;
1477 break;
1478 }
1479#else
1480
1481
1482
1483 dev_warn (&gadget->dev, "set_interface ignored!\n");
1484#endif
1485
1486done_set_intf:
1487 spin_unlock (&dev->lock);
1488 break;
1489 case USB_REQ_GET_INTERFACE:
1490 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1491 || !dev->config
1492 || wIndex > 1)
1493 break;
1494 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
1495 break;
1496
1497
1498 if (rndis_active(dev) || wIndex != 1)
1499 *(u8 *)req->buf = 0;
1500 else
1501 *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
1502 value = min (wLength, (u16) 1);
1503 break;
1504
1505#ifdef DEV_CONFIG_CDC
1506 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
1507
1508
1509
1510 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1511 || !cdc_active(dev)
1512 || wLength != 0
1513 || wIndex > 1)
1514 break;
1515 DEBUG (dev, "packet filter %02x\n", wValue);
1516 dev->cdc_filter = wValue;
1517 value = 0;
1518 break;
1519
1520
1521
1522
1523
1524
1525
1526
1527#endif
1528
1529#ifdef CONFIG_USB_ETH_RNDIS
1530
1531
1532
1533 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1534 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1535 || !rndis_active(dev)
1536 || wLength > USB_BUFSIZ
1537 || wValue
1538 || rndis_control_intf.bInterfaceNumber
1539 != wIndex)
1540 break;
1541
1542 value = wLength;
1543 req->complete = rndis_command_complete;
1544
1545 break;
1546
1547 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1548 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1549 == ctrl->bRequestType
1550 && rndis_active(dev)
1551
1552 && !wValue
1553 && rndis_control_intf.bInterfaceNumber
1554 == wIndex) {
1555 u8 *buf;
1556 u32 n;
1557
1558
1559 buf = rndis_get_next_response(dev->rndis_config, &n);
1560 if (buf) {
1561 memcpy(req->buf, buf, n);
1562 req->complete = rndis_response_complete;
1563 rndis_free_response(dev->rndis_config, buf);
1564 }
1565
1566 }
1567 break;
1568#endif
1569
1570 default:
1571 VDEBUG (dev,
1572 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1573 ctrl->bRequestType, ctrl->bRequest,
1574 wValue, wIndex, wLength);
1575 }
1576
1577
1578 if (value >= 0) {
1579 req->length = value;
1580 req->zero = value < wLength
1581 && (value % gadget->ep0->maxpacket) == 0;
1582 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1583 if (value < 0) {
1584 DEBUG (dev, "ep_queue --> %d\n", value);
1585 req->status = 0;
1586 eth_setup_complete (gadget->ep0, req);
1587 }
1588 }
1589
1590
1591 return value;
1592}
1593
1594static void
1595eth_disconnect (struct usb_gadget *gadget)
1596{
1597 struct eth_dev *dev = get_gadget_data (gadget);
1598 unsigned long flags;
1599
1600 spin_lock_irqsave (&dev->lock, flags);
1601 netif_stop_queue (dev->net);
1602 netif_carrier_off (dev->net);
1603 eth_reset_config (dev);
1604 spin_unlock_irqrestore (&dev->lock, flags);
1605
1606
1607
1608
1609
1610
1611}
1612
1613
1614
1615
1616
1617static int eth_change_mtu (struct net_device *net, int new_mtu)
1618{
1619 struct eth_dev *dev = netdev_priv(net);
1620
1621 if (dev->rndis)
1622 return -EBUSY;
1623
1624 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1625 return -ERANGE;
1626
1627 if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
1628 return -EDOM;
1629 net->mtu = new_mtu;
1630 return 0;
1631}
1632
1633static struct net_device_stats *eth_get_stats (struct net_device *net)
1634{
1635 return &((struct eth_dev *)netdev_priv(net))->stats;
1636}
1637
1638static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
1639{
1640 struct eth_dev *dev = netdev_priv(net);
1641 strlcpy(p->driver, shortname, sizeof p->driver);
1642 strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
1643 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
1644 strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
1645}
1646
1647static u32 eth_get_link(struct net_device *net)
1648{
1649 struct eth_dev *dev = netdev_priv(net);
1650 return dev->gadget->speed != USB_SPEED_UNKNOWN;
1651}
1652
1653static struct ethtool_ops ops = {
1654 .get_drvinfo = eth_get_drvinfo,
1655 .get_link = eth_get_link
1656};
1657
1658static void defer_kevent (struct eth_dev *dev, int flag)
1659{
1660 if (test_and_set_bit (flag, &dev->todo))
1661 return;
1662 if (!schedule_work (&dev->work))
1663 ERROR (dev, "kevent %d may have been dropped\n", flag);
1664 else
1665 DEBUG (dev, "kevent %d scheduled\n", flag);
1666}
1667
1668static void rx_complete (struct usb_ep *ep, struct usb_request *req);
1669
1670static int
1671rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
1672{
1673 struct sk_buff *skb;
1674 int retval = -ENOMEM;
1675 size_t size;
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688 size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
1689 size += dev->out_ep->maxpacket - 1;
1690 if (rndis_active(dev))
1691 size += sizeof (struct rndis_packet_msg_type);
1692 size -= size % dev->out_ep->maxpacket;
1693
1694 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
1695 if (skb == NULL) {
1696 DEBUG (dev, "no rx skb\n");
1697 goto enomem;
1698 }
1699
1700
1701
1702
1703
1704 skb_reserve(skb, NET_IP_ALIGN);
1705
1706 req->buf = skb->data;
1707 req->length = size;
1708 req->complete = rx_complete;
1709 req->context = skb;
1710
1711 retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
1712 if (retval == -ENOMEM)
1713enomem:
1714 defer_kevent (dev, WORK_RX_MEMORY);
1715 if (retval) {
1716 DEBUG (dev, "rx submit --> %d\n", retval);
1717 if (skb)
1718 dev_kfree_skb_any(skb);
1719 spin_lock(&dev->req_lock);
1720 list_add (&req->list, &dev->rx_reqs);
1721 spin_unlock(&dev->req_lock);
1722 }
1723 return retval;
1724}
1725
1726static void rx_complete (struct usb_ep *ep, struct usb_request *req)
1727{
1728 struct sk_buff *skb = req->context;
1729 struct eth_dev *dev = ep->driver_data;
1730 int status = req->status;
1731
1732 switch (status) {
1733
1734
1735 case 0:
1736 skb_put (skb, req->actual);
1737
1738 if (rndis_active(dev))
1739 status = rndis_rm_hdr (skb);
1740 if (status < 0
1741 || ETH_HLEN > skb->len
1742 || skb->len > ETH_FRAME_LEN) {
1743 dev->stats.rx_errors++;
1744 dev->stats.rx_length_errors++;
1745 DEBUG (dev, "rx length %d\n", skb->len);
1746 break;
1747 }
1748
1749 skb->protocol = eth_type_trans (skb, dev->net);
1750 dev->stats.rx_packets++;
1751 dev->stats.rx_bytes += skb->len;
1752
1753
1754
1755
1756 status = netif_rx (skb);
1757 skb = NULL;
1758 break;
1759
1760
1761 case -ECONNRESET:
1762 case -ESHUTDOWN:
1763 VDEBUG (dev, "rx shutdown, code %d\n", status);
1764 goto quiesce;
1765
1766
1767 case -ECONNABORTED:
1768 DEBUG (dev, "rx %s reset\n", ep->name);
1769 defer_kevent (dev, WORK_RX_MEMORY);
1770quiesce:
1771 dev_kfree_skb_any (skb);
1772 goto clean;
1773
1774
1775 case -EOVERFLOW:
1776 dev->stats.rx_over_errors++;
1777
1778
1779 default:
1780 dev->stats.rx_errors++;
1781 DEBUG (dev, "rx status %d\n", status);
1782 break;
1783 }
1784
1785 if (skb)
1786 dev_kfree_skb_any (skb);
1787 if (!netif_running (dev->net)) {
1788clean:
1789 spin_lock(&dev->req_lock);
1790 list_add (&req->list, &dev->rx_reqs);
1791 spin_unlock(&dev->req_lock);
1792 req = NULL;
1793 }
1794 if (req)
1795 rx_submit (dev, req, GFP_ATOMIC);
1796}
1797
1798static int prealloc (struct list_head *list, struct usb_ep *ep,
1799 unsigned n, gfp_t gfp_flags)
1800{
1801 unsigned i;
1802 struct usb_request *req;
1803
1804 if (!n)
1805 return -ENOMEM;
1806
1807
1808 i = n;
1809 list_for_each_entry (req, list, list) {
1810 if (i-- == 0)
1811 goto extra;
1812 }
1813 while (i--) {
1814 req = usb_ep_alloc_request (ep, gfp_flags);
1815 if (!req)
1816 return list_empty (list) ? -ENOMEM : 0;
1817 list_add (&req->list, list);
1818 }
1819 return 0;
1820
1821extra:
1822
1823 for (;;) {
1824 struct list_head *next;
1825
1826 next = req->list.next;
1827 list_del (&req->list);
1828 usb_ep_free_request (ep, req);
1829
1830 if (next == list)
1831 break;
1832
1833 req = container_of (next, struct usb_request, list);
1834 }
1835 return 0;
1836}
1837
1838static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
1839{
1840 int status;
1841
1842 spin_lock(&dev->req_lock);
1843 status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
1844 if (status < 0)
1845 goto fail;
1846 status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
1847 if (status < 0)
1848 goto fail;
1849 goto done;
1850fail:
1851 DEBUG (dev, "can't alloc requests\n");
1852done:
1853 spin_unlock(&dev->req_lock);
1854 return status;
1855}
1856
1857static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
1858{
1859 struct usb_request *req;
1860 unsigned long flags;
1861
1862
1863 spin_lock_irqsave(&dev->req_lock, flags);
1864 while (!list_empty (&dev->rx_reqs)) {
1865 req = container_of (dev->rx_reqs.next,
1866 struct usb_request, list);
1867 list_del_init (&req->list);
1868 spin_unlock_irqrestore(&dev->req_lock, flags);
1869
1870 if (rx_submit (dev, req, gfp_flags) < 0) {
1871 defer_kevent (dev, WORK_RX_MEMORY);
1872 return;
1873 }
1874
1875 spin_lock_irqsave(&dev->req_lock, flags);
1876 }
1877 spin_unlock_irqrestore(&dev->req_lock, flags);
1878}
1879
1880static void eth_work (struct work_struct *work)
1881{
1882 struct eth_dev *dev = container_of(work, struct eth_dev, work);
1883
1884 if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
1885 if (netif_running (dev->net))
1886 rx_fill (dev, GFP_KERNEL);
1887 }
1888
1889 if (dev->todo)
1890 DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
1891}
1892
1893static void tx_complete (struct usb_ep *ep, struct usb_request *req)
1894{
1895 struct sk_buff *skb = req->context;
1896 struct eth_dev *dev = ep->driver_data;
1897
1898 switch (req->status) {
1899 default:
1900 dev->stats.tx_errors++;
1901 VDEBUG (dev, "tx err %d\n", req->status);
1902
1903 case -ECONNRESET:
1904 case -ESHUTDOWN:
1905 break;
1906 case 0:
1907 dev->stats.tx_bytes += skb->len;
1908 }
1909 dev->stats.tx_packets++;
1910
1911 spin_lock(&dev->req_lock);
1912 list_add (&req->list, &dev->tx_reqs);
1913 spin_unlock(&dev->req_lock);
1914 dev_kfree_skb_any (skb);
1915
1916 atomic_dec (&dev->tx_qlen);
1917 if (netif_carrier_ok (dev->net))
1918 netif_wake_queue (dev->net);
1919}
1920
1921static inline int eth_is_promisc (struct eth_dev *dev)
1922{
1923
1924 if (subset_active (dev))
1925 return 1;
1926 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1927}
1928
1929static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1930{
1931 struct eth_dev *dev = netdev_priv(net);
1932 int length = skb->len;
1933 int retval;
1934 struct usb_request *req = NULL;
1935 unsigned long flags;
1936
1937
1938 if (!eth_is_promisc (dev)) {
1939 u8 *dest = skb->data;
1940
1941 if (is_multicast_ether_addr(dest)) {
1942 u16 type;
1943
1944
1945
1946
1947 if (is_broadcast_ether_addr(dest))
1948 type = USB_CDC_PACKET_TYPE_BROADCAST;
1949 else
1950 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1951 if (!(dev->cdc_filter & type)) {
1952 dev_kfree_skb_any (skb);
1953 return 0;
1954 }
1955 }
1956
1957 }
1958
1959 spin_lock_irqsave(&dev->req_lock, flags);
1960
1961
1962
1963
1964
1965 if (list_empty(&dev->tx_reqs)) {
1966 spin_unlock_irqrestore(&dev->req_lock, flags);
1967 return 1;
1968 }
1969
1970 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1971 list_del (&req->list);
1972
1973
1974 if (list_empty (&dev->tx_reqs))
1975 netif_stop_queue (net);
1976 spin_unlock_irqrestore(&dev->req_lock, flags);
1977
1978
1979
1980
1981
1982 if (rndis_active(dev)) {
1983 struct sk_buff *skb_rndis;
1984
1985 skb_rndis = skb_realloc_headroom (skb,
1986 sizeof (struct rndis_packet_msg_type));
1987 if (!skb_rndis)
1988 goto drop;
1989
1990 dev_kfree_skb_any (skb);
1991 skb = skb_rndis;
1992 rndis_add_hdr (skb);
1993 length = skb->len;
1994 }
1995 req->buf = skb->data;
1996 req->context = skb;
1997 req->complete = tx_complete;
1998
1999
2000
2001
2002
2003 req->zero = 1;
2004 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2005 length++;
2006
2007 req->length = length;
2008
2009
2010 if (gadget_is_dualspeed(dev->gadget))
2011 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2012 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
2013 : 0;
2014
2015 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2016 switch (retval) {
2017 default:
2018 DEBUG (dev, "tx queue err %d\n", retval);
2019 break;
2020 case 0:
2021 net->trans_start = jiffies;
2022 atomic_inc (&dev->tx_qlen);
2023 }
2024
2025 if (retval) {
2026drop:
2027 dev->stats.tx_dropped++;
2028 dev_kfree_skb_any (skb);
2029 spin_lock_irqsave(&dev->req_lock, flags);
2030 if (list_empty (&dev->tx_reqs))
2031 netif_start_queue (net);
2032 list_add (&req->list, &dev->tx_reqs);
2033 spin_unlock_irqrestore(&dev->req_lock, flags);
2034 }
2035 return 0;
2036}
2037
2038
2039
2040#ifdef CONFIG_USB_ETH_RNDIS
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
2052static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
2053
2054static void
2055rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2056{
2057 struct eth_dev *dev = ep->driver_data;
2058
2059 if (req->status || req->actual != req->length)
2060 DEBUG (dev,
2061 "rndis control ack complete --> %d, %d/%d\n",
2062 req->status, req->actual, req->length);
2063 req->context = NULL;
2064
2065 if (req != dev->stat_req)
2066 eth_req_free(ep, req);
2067}
2068
2069static int rndis_control_ack (struct net_device *net)
2070{
2071 struct eth_dev *dev = netdev_priv(net);
2072 int length;
2073 struct usb_request *resp = dev->stat_req;
2074
2075
2076 if (!dev->status) {
2077 DEBUG (dev, "status ENODEV\n");
2078 return -ENODEV;
2079 }
2080
2081
2082 if (resp->context) {
2083 resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
2084 if (!resp)
2085 return -ENOMEM;
2086 }
2087
2088
2089
2090
2091 resp->length = 8;
2092 resp->complete = rndis_control_ack_complete;
2093 resp->context = dev;
2094
2095 *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2096 *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2097
2098 length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2099 if (length < 0) {
2100 resp->status = 0;
2101 rndis_control_ack_complete (dev->status_ep, resp);
2102 }
2103
2104 return 0;
2105}
2106
2107#else
2108
2109#define rndis_control_ack NULL
2110
2111#endif
2112
2113static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
2114{
2115 DEBUG (dev, "%s\n", __FUNCTION__);
2116
2117
2118 rx_fill (dev, gfp_flags);
2119
2120
2121 atomic_set (&dev->tx_qlen, 0);
2122 netif_wake_queue (dev->net);
2123 if (rndis_active(dev)) {
2124 rndis_set_param_medium (dev->rndis_config,
2125 NDIS_MEDIUM_802_3,
2126 BITRATE(dev->gadget)/100);
2127 (void) rndis_signal_connect (dev->rndis_config);
2128 }
2129}
2130
2131static int eth_open (struct net_device *net)
2132{
2133 struct eth_dev *dev = netdev_priv(net);
2134
2135 DEBUG (dev, "%s\n", __FUNCTION__);
2136 if (netif_carrier_ok (dev->net))
2137 eth_start (dev, GFP_KERNEL);
2138 return 0;
2139}
2140
2141static int eth_stop (struct net_device *net)
2142{
2143 struct eth_dev *dev = netdev_priv(net);
2144
2145 VDEBUG (dev, "%s\n", __FUNCTION__);
2146 netif_stop_queue (net);
2147
2148 DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
2149 dev->stats.rx_packets, dev->stats.tx_packets,
2150 dev->stats.rx_errors, dev->stats.tx_errors
2151 );
2152
2153
2154 if (dev->config) {
2155 usb_ep_disable (dev->in_ep);
2156 usb_ep_disable (dev->out_ep);
2157 if (netif_carrier_ok (dev->net)) {
2158 DEBUG (dev, "host still using in/out endpoints\n");
2159
2160 usb_ep_enable (dev->in_ep, dev->in);
2161 usb_ep_enable (dev->out_ep, dev->out);
2162 }
2163 if (dev->status_ep) {
2164 usb_ep_disable (dev->status_ep);
2165 usb_ep_enable (dev->status_ep, dev->status);
2166 }
2167 }
2168
2169 if (rndis_active(dev)) {
2170 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
2171 (void) rndis_signal_disconnect (dev->rndis_config);
2172 }
2173
2174 return 0;
2175}
2176
2177
2178
2179static struct usb_request *
2180eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
2181{
2182 struct usb_request *req;
2183
2184 req = usb_ep_alloc_request (ep, gfp_flags);
2185 if (!req)
2186 return NULL;
2187
2188 req->buf = kmalloc (size, gfp_flags);
2189 if (!req->buf) {
2190 usb_ep_free_request (ep, req);
2191 req = NULL;
2192 }
2193 return req;
2194}
2195
2196static void
2197eth_req_free (struct usb_ep *ep, struct usb_request *req)
2198{
2199 kfree (req->buf);
2200 usb_ep_free_request (ep, req);
2201}
2202
2203
2204static void
2205eth_unbind (struct usb_gadget *gadget)
2206{
2207 struct eth_dev *dev = get_gadget_data (gadget);
2208
2209 DEBUG (dev, "unbind\n");
2210 rndis_deregister (dev->rndis_config);
2211 rndis_exit ();
2212
2213
2214 if (dev->req) {
2215 eth_req_free (gadget->ep0, dev->req);
2216 dev->req = NULL;
2217 }
2218 if (dev->stat_req) {
2219 eth_req_free (dev->status_ep, dev->stat_req);
2220 dev->stat_req = NULL;
2221 }
2222
2223 unregister_netdev (dev->net);
2224 free_netdev(dev->net);
2225
2226
2227 flush_scheduled_work ();
2228 set_gadget_data (gadget, NULL);
2229}
2230
2231static u8 __devinit nibble (unsigned char c)
2232{
2233 if (likely (isdigit (c)))
2234 return c - '0';
2235 c = toupper (c);
2236 if (likely (isxdigit (c)))
2237 return 10 + c - 'A';
2238 return 0;
2239}
2240
2241static int __devinit get_ether_addr(const char *str, u8 *dev_addr)
2242{
2243 if (str) {
2244 unsigned i;
2245
2246 for (i = 0; i < 6; i++) {
2247 unsigned char num;
2248
2249 if((*str == '.') || (*str == ':'))
2250 str++;
2251 num = nibble(*str++) << 4;
2252 num |= (nibble(*str++));
2253 dev_addr [i] = num;
2254 }
2255 if (is_valid_ether_addr (dev_addr))
2256 return 0;
2257 }
2258 random_ether_addr(dev_addr);
2259 return 1;
2260}
2261
2262static int __devinit
2263eth_bind (struct usb_gadget *gadget)
2264{
2265 struct eth_dev *dev;
2266 struct net_device *net;
2267 u8 cdc = 1, zlp = 1, rndis = 1;
2268 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
2269 int status = -ENOMEM;
2270 int gcnum;
2271
2272
2273#ifndef DEV_CONFIG_CDC
2274 cdc = 0;
2275#endif
2276#ifndef CONFIG_USB_ETH_RNDIS
2277 rndis = 0;
2278#endif
2279
2280
2281
2282
2283
2284 if (gadget_is_pxa (gadget)) {
2285
2286 cdc = 0;
2287 } else if (gadget_is_musbhdrc(gadget)) {
2288
2289 zlp = 0;
2290 } else if (gadget_is_sh(gadget)) {
2291
2292 cdc = 0;
2293 rndis = 0;
2294 } else if (gadget_is_sa1100 (gadget)) {
2295
2296 zlp = 0;
2297
2298
2299
2300 cdc = 0;
2301 }
2302
2303 gcnum = usb_gadget_controller_number (gadget);
2304 if (gcnum >= 0)
2305 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
2306 else {
2307
2308
2309
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 init_utsname()->sysname, init_utsname()->release,
2318 gadget->name);
2319
2320
2321
2322
2323
2324
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
2335
2336
2337
2338 } else if (!cdc) {
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
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 if (iSerialNumber) {
2361 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2362 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2363 }
2364
2365
2366 usb_ep_autoconfig_reset (gadget);
2367 in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
2368 if (!in_ep) {
2369autoconf_fail:
2370 dev_err (&gadget->dev,
2371 "can't autoconfigure on %s\n",
2372 gadget->name);
2373 return -ENODEV;
2374 }
2375 in_ep->driver_data = in_ep;
2376
2377 out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
2378 if (!out_ep)
2379 goto autoconf_fail;
2380 out_ep->driver_data = out_ep;
2381
2382#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2383
2384
2385
2386 if (cdc || rndis) {
2387 status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
2388 if (status_ep) {
2389 status_ep->driver_data = status_ep;
2390 } else if (rndis) {
2391 dev_err (&gadget->dev,
2392 "can't run RNDIS on %s\n",
2393 gadget->name);
2394 return -ENODEV;
2395#ifdef DEV_CONFIG_CDC
2396
2397 } else if (cdc) {
2398 control_intf.bNumEndpoints = 0;
2399
2400#endif
2401 }
2402 }
2403#endif
2404
2405
2406 if (!cdc) {
2407 eth_config.bNumInterfaces = 1;
2408 eth_config.iConfiguration = STRING_SUBSET;
2409
2410
2411
2412
2413 fs_subset_descriptors();
2414 hs_subset_descriptors();
2415 }
2416
2417 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
2418 usb_gadget_set_selfpowered (gadget);
2419
2420
2421 if (rndis)
2422 device_desc.bNumConfigurations = 2;
2423
2424 if (gadget_is_dualspeed(gadget)) {
2425 if (rndis)
2426 dev_qualifier.bNumConfigurations = 2;
2427 else if (!cdc)
2428 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2429
2430
2431 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2432
2433
2434 hs_source_desc.bEndpointAddress =
2435 fs_source_desc.bEndpointAddress;
2436 hs_sink_desc.bEndpointAddress =
2437 fs_sink_desc.bEndpointAddress;
2438#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2439 if (status_ep)
2440 hs_status_desc.bEndpointAddress =
2441 fs_status_desc.bEndpointAddress;
2442#endif
2443 }
2444
2445 if (gadget_is_otg(gadget)) {
2446 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2447 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2448 eth_config.bMaxPower = 4;
2449#ifdef CONFIG_USB_ETH_RNDIS
2450 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2451 rndis_config.bMaxPower = 4;
2452#endif
2453 }
2454
2455 net = alloc_etherdev (sizeof *dev);
2456 if (!net)
2457 return status;
2458 dev = netdev_priv(net);
2459 spin_lock_init (&dev->lock);
2460 spin_lock_init (&dev->req_lock);
2461 INIT_WORK (&dev->work, eth_work);
2462 INIT_LIST_HEAD (&dev->tx_reqs);
2463 INIT_LIST_HEAD (&dev->rx_reqs);
2464
2465
2466 dev->net = net;
2467 strcpy (net->name, "usb%d");
2468 dev->cdc = cdc;
2469 dev->zlp = zlp;
2470
2471 dev->in_ep = in_ep;
2472 dev->out_ep = out_ep;
2473 dev->status_ep = status_ep;
2474
2475
2476
2477
2478
2479
2480
2481 if (get_ether_addr(dev_addr, net->dev_addr))
2482 dev_warn(&gadget->dev,
2483 "using random %s ethernet address\n", "self");
2484 if (get_ether_addr(host_addr, dev->host_mac))
2485 dev_warn(&gadget->dev,
2486 "using random %s ethernet address\n", "host");
2487 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2488 dev->host_mac [0], dev->host_mac [1],
2489 dev->host_mac [2], dev->host_mac [3],
2490 dev->host_mac [4], dev->host_mac [5]);
2491
2492 if (rndis) {
2493 status = rndis_init();
2494 if (status < 0) {
2495 dev_err (&gadget->dev, "can't init RNDIS, %d\n",
2496 status);
2497 goto fail;
2498 }
2499 }
2500
2501 net->change_mtu = eth_change_mtu;
2502 net->get_stats = eth_get_stats;
2503 net->hard_start_xmit = eth_start_xmit;
2504 net->open = eth_open;
2505 net->stop = eth_stop;
2506
2507
2508 SET_ETHTOOL_OPS(net, &ops);
2509
2510
2511 dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
2512 if (!dev->req)
2513 goto fail;
2514 dev->req->complete = eth_setup_complete;
2515
2516
2517#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
2518 if (dev->status_ep) {
2519 dev->stat_req = eth_req_alloc (dev->status_ep,
2520 STATUS_BYTECOUNT, GFP_KERNEL);
2521 if (!dev->stat_req) {
2522 eth_req_free (gadget->ep0, dev->req);
2523 goto fail;
2524 }
2525 dev->stat_req->context = NULL;
2526 }
2527#endif
2528
2529
2530 dev->gadget = gadget;
2531 set_gadget_data (gadget, dev);
2532 gadget->ep0->driver_data = dev;
2533
2534
2535
2536
2537
2538 netif_stop_queue (dev->net);
2539 netif_carrier_off (dev->net);
2540
2541 SET_NETDEV_DEV (dev->net, &gadget->dev);
2542 status = register_netdev (dev->net);
2543 if (status < 0)
2544 goto fail1;
2545
2546 INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
2547 INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
2548 out_ep->name, in_ep->name,
2549 status_ep ? " STATUS " : "",
2550 status_ep ? status_ep->name : ""
2551 );
2552 INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2553 net->dev_addr [0], net->dev_addr [1],
2554 net->dev_addr [2], net->dev_addr [3],
2555 net->dev_addr [4], net->dev_addr [5]);
2556
2557 if (cdc || rndis)
2558 INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2559 dev->host_mac [0], dev->host_mac [1],
2560 dev->host_mac [2], dev->host_mac [3],
2561 dev->host_mac [4], dev->host_mac [5]);
2562
2563 if (rndis) {
2564 u32 vendorID = 0;
2565
2566
2567
2568 dev->rndis_config = rndis_register (rndis_control_ack);
2569 if (dev->rndis_config < 0) {
2570fail0:
2571 unregister_netdev (dev->net);
2572 status = -ENODEV;
2573 goto fail;
2574 }
2575
2576
2577 rndis_set_host_mac (dev->rndis_config, dev->host_mac);
2578 if (rndis_set_param_dev (dev->rndis_config, dev->net,
2579 &dev->stats, &dev->cdc_filter))
2580 goto fail0;
2581 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2582 manufacturer))
2583 goto fail0;
2584 if (rndis_set_param_medium(dev->rndis_config,
2585 NDIS_MEDIUM_802_3, 0))
2586 goto fail0;
2587 INFO (dev, "RNDIS ready\n");
2588 }
2589
2590 return status;
2591
2592fail1:
2593 dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
2594fail:
2595 eth_unbind (gadget);
2596 return status;
2597}
2598
2599
2600
2601static void
2602eth_suspend (struct usb_gadget *gadget)
2603{
2604 struct eth_dev *dev = get_gadget_data (gadget);
2605
2606 DEBUG (dev, "suspend\n");
2607 dev->suspended = 1;
2608}
2609
2610static void
2611eth_resume (struct usb_gadget *gadget)
2612{
2613 struct eth_dev *dev = get_gadget_data (gadget);
2614
2615 DEBUG (dev, "resume\n");
2616 dev->suspended = 0;
2617}
2618
2619
2620
2621static struct usb_gadget_driver eth_driver = {
2622 .speed = DEVSPEED,
2623
2624 .function = (char *) driver_desc,
2625 .bind = eth_bind,
2626 .unbind = eth_unbind,
2627
2628 .setup = eth_setup,
2629 .disconnect = eth_disconnect,
2630
2631 .suspend = eth_suspend,
2632 .resume = eth_resume,
2633
2634 .driver = {
2635 .name = (char *) shortname,
2636 .owner = THIS_MODULE,
2637 },
2638};
2639
2640MODULE_DESCRIPTION (DRIVER_DESC);
2641MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
2642MODULE_LICENSE ("GPL");
2643
2644
2645static int __init init (void)
2646{
2647 return usb_gadget_register_driver (ð_driver);
2648}
2649module_init (init);
2650
2651static void __exit cleanup (void)
2652{
2653 usb_gadget_unregister_driver (ð_driver);
2654}
2655module_exit (cleanup);
2656
2657