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