1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/slab.h>
16#include <linux/kernel.h>
17#include <linux/device.h>
18#include <linux/etherdevice.h>
19
20#include "u_ether.h"
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41enum ecm_notify_state {
42 ECM_NOTIFY_NONE,
43 ECM_NOTIFY_CONNECT,
44 ECM_NOTIFY_SPEED,
45};
46
47struct f_ecm {
48 struct gether port;
49 u8 ctrl_id, data_id;
50
51 char ethaddr[14];
52
53 struct usb_ep *notify;
54 struct usb_request *notify_req;
55 u8 notify_state;
56 bool is_open;
57
58
59
60
61};
62
63static inline struct f_ecm *func_to_ecm(struct usb_function *f)
64{
65 return container_of(f, struct f_ecm, port.func);
66}
67
68
69static inline unsigned ecm_bitrate(struct usb_gadget *g)
70{
71 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
72 return 13 * 1024 * 8 * 1000 * 8;
73 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
74 return 13 * 512 * 8 * 1000 * 8;
75 else
76 return 19 * 64 * 1 * 1000 * 8;
77}
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94#define LOG2_STATUS_INTERVAL_MSEC 5
95#define ECM_STATUS_BYTECOUNT 16
96
97
98
99
100static struct usb_interface_descriptor ecm_control_intf = {
101 .bLength = sizeof ecm_control_intf,
102 .bDescriptorType = USB_DT_INTERFACE,
103
104
105
106 .bNumEndpoints = 1,
107 .bInterfaceClass = USB_CLASS_COMM,
108 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
109 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
110
111};
112
113static struct usb_cdc_header_desc ecm_header_desc = {
114 .bLength = sizeof ecm_header_desc,
115 .bDescriptorType = USB_DT_CS_INTERFACE,
116 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
117
118 .bcdCDC = cpu_to_le16(0x0110),
119};
120
121static struct usb_cdc_union_desc ecm_union_desc = {
122 .bLength = sizeof(ecm_union_desc),
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_UNION_TYPE,
125
126
127};
128
129static struct usb_cdc_ether_desc ecm_desc = {
130 .bLength = sizeof ecm_desc,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
133
134
135
136 .bmEthernetStatistics = cpu_to_le32(0),
137 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
138 .wNumberMCFilters = cpu_to_le16(0),
139 .bNumberPowerFilters = 0,
140};
141
142
143
144static struct usb_interface_descriptor ecm_data_nop_intf = {
145 .bLength = sizeof ecm_data_nop_intf,
146 .bDescriptorType = USB_DT_INTERFACE,
147
148 .bInterfaceNumber = 1,
149 .bAlternateSetting = 0,
150 .bNumEndpoints = 0,
151 .bInterfaceClass = USB_CLASS_CDC_DATA,
152 .bInterfaceSubClass = 0,
153 .bInterfaceProtocol = 0,
154
155};
156
157
158
159static struct usb_interface_descriptor ecm_data_intf = {
160 .bLength = sizeof ecm_data_intf,
161 .bDescriptorType = USB_DT_INTERFACE,
162
163 .bInterfaceNumber = 1,
164 .bAlternateSetting = 1,
165 .bNumEndpoints = 2,
166 .bInterfaceClass = USB_CLASS_CDC_DATA,
167 .bInterfaceSubClass = 0,
168 .bInterfaceProtocol = 0,
169
170};
171
172
173
174static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177
178 .bEndpointAddress = USB_DIR_IN,
179 .bmAttributes = USB_ENDPOINT_XFER_INT,
180 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
181 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
182};
183
184static struct usb_endpoint_descriptor fs_ecm_in_desc = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_BULK,
190};
191
192static struct usb_endpoint_descriptor fs_ecm_out_desc = {
193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
195
196 .bEndpointAddress = USB_DIR_OUT,
197 .bmAttributes = USB_ENDPOINT_XFER_BULK,
198};
199
200static struct usb_descriptor_header *ecm_fs_function[] = {
201
202 (struct usb_descriptor_header *) &ecm_control_intf,
203 (struct usb_descriptor_header *) &ecm_header_desc,
204 (struct usb_descriptor_header *) &ecm_union_desc,
205 (struct usb_descriptor_header *) &ecm_desc,
206
207
208 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
209
210
211 (struct usb_descriptor_header *) &ecm_data_nop_intf,
212 (struct usb_descriptor_header *) &ecm_data_intf,
213 (struct usb_descriptor_header *) &fs_ecm_in_desc,
214 (struct usb_descriptor_header *) &fs_ecm_out_desc,
215 NULL,
216};
217
218
219
220static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
221 .bLength = USB_DT_ENDPOINT_SIZE,
222 .bDescriptorType = USB_DT_ENDPOINT,
223
224 .bEndpointAddress = USB_DIR_IN,
225 .bmAttributes = USB_ENDPOINT_XFER_INT,
226 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
227 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
228};
229
230static struct usb_endpoint_descriptor hs_ecm_in_desc = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233
234 .bEndpointAddress = USB_DIR_IN,
235 .bmAttributes = USB_ENDPOINT_XFER_BULK,
236 .wMaxPacketSize = cpu_to_le16(512),
237};
238
239static struct usb_endpoint_descriptor hs_ecm_out_desc = {
240 .bLength = USB_DT_ENDPOINT_SIZE,
241 .bDescriptorType = USB_DT_ENDPOINT,
242
243 .bEndpointAddress = USB_DIR_OUT,
244 .bmAttributes = USB_ENDPOINT_XFER_BULK,
245 .wMaxPacketSize = cpu_to_le16(512),
246};
247
248static struct usb_descriptor_header *ecm_hs_function[] = {
249
250 (struct usb_descriptor_header *) &ecm_control_intf,
251 (struct usb_descriptor_header *) &ecm_header_desc,
252 (struct usb_descriptor_header *) &ecm_union_desc,
253 (struct usb_descriptor_header *) &ecm_desc,
254
255
256 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
257
258
259 (struct usb_descriptor_header *) &ecm_data_nop_intf,
260 (struct usb_descriptor_header *) &ecm_data_intf,
261 (struct usb_descriptor_header *) &hs_ecm_in_desc,
262 (struct usb_descriptor_header *) &hs_ecm_out_desc,
263 NULL,
264};
265
266
267
268static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
269 .bLength = USB_DT_ENDPOINT_SIZE,
270 .bDescriptorType = USB_DT_ENDPOINT,
271
272 .bEndpointAddress = USB_DIR_IN,
273 .bmAttributes = USB_ENDPOINT_XFER_INT,
274 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
275 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
276};
277
278static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
279 .bLength = sizeof ss_ecm_intr_comp_desc,
280 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
281
282
283
284
285 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
286};
287
288static struct usb_endpoint_descriptor ss_ecm_in_desc = {
289 .bLength = USB_DT_ENDPOINT_SIZE,
290 .bDescriptorType = USB_DT_ENDPOINT,
291
292 .bEndpointAddress = USB_DIR_IN,
293 .bmAttributes = USB_ENDPOINT_XFER_BULK,
294 .wMaxPacketSize = cpu_to_le16(1024),
295};
296
297static struct usb_endpoint_descriptor ss_ecm_out_desc = {
298 .bLength = USB_DT_ENDPOINT_SIZE,
299 .bDescriptorType = USB_DT_ENDPOINT,
300
301 .bEndpointAddress = USB_DIR_OUT,
302 .bmAttributes = USB_ENDPOINT_XFER_BULK,
303 .wMaxPacketSize = cpu_to_le16(1024),
304};
305
306static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
307 .bLength = sizeof ss_ecm_bulk_comp_desc,
308 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
309
310
311
312
313};
314
315static struct usb_descriptor_header *ecm_ss_function[] = {
316
317 (struct usb_descriptor_header *) &ecm_control_intf,
318 (struct usb_descriptor_header *) &ecm_header_desc,
319 (struct usb_descriptor_header *) &ecm_union_desc,
320 (struct usb_descriptor_header *) &ecm_desc,
321
322
323 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
324 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
325
326
327 (struct usb_descriptor_header *) &ecm_data_nop_intf,
328 (struct usb_descriptor_header *) &ecm_data_intf,
329 (struct usb_descriptor_header *) &ss_ecm_in_desc,
330 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
331 (struct usb_descriptor_header *) &ss_ecm_out_desc,
332 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
333 NULL,
334};
335
336
337
338static struct usb_string ecm_string_defs[] = {
339 [0].s = "CDC Ethernet Control Model (ECM)",
340 [1].s = NULL ,
341 [2].s = "CDC Ethernet Data",
342 { }
343};
344
345static struct usb_gadget_strings ecm_string_table = {
346 .language = 0x0409,
347 .strings = ecm_string_defs,
348};
349
350static struct usb_gadget_strings *ecm_strings[] = {
351 &ecm_string_table,
352 NULL,
353};
354
355
356
357static void ecm_do_notify(struct f_ecm *ecm)
358{
359 struct usb_request *req = ecm->notify_req;
360 struct usb_cdc_notification *event;
361 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
362 __le32 *data;
363 int status;
364
365
366 if (!req)
367 return;
368
369 event = req->buf;
370 switch (ecm->notify_state) {
371 case ECM_NOTIFY_NONE:
372 return;
373
374 case ECM_NOTIFY_CONNECT:
375 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
376 if (ecm->is_open)
377 event->wValue = cpu_to_le16(1);
378 else
379 event->wValue = cpu_to_le16(0);
380 event->wLength = 0;
381 req->length = sizeof *event;
382
383 DBG(cdev, "notify connect %s\n",
384 ecm->is_open ? "true" : "false");
385 ecm->notify_state = ECM_NOTIFY_SPEED;
386 break;
387
388 case ECM_NOTIFY_SPEED:
389 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
390 event->wValue = cpu_to_le16(0);
391 event->wLength = cpu_to_le16(8);
392 req->length = ECM_STATUS_BYTECOUNT;
393
394
395 data = req->buf + sizeof *event;
396 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
397 data[1] = data[0];
398
399 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
400 ecm->notify_state = ECM_NOTIFY_NONE;
401 break;
402 }
403 event->bmRequestType = 0xA1;
404 event->wIndex = cpu_to_le16(ecm->ctrl_id);
405
406 ecm->notify_req = NULL;
407 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
408 if (status < 0) {
409 ecm->notify_req = req;
410 DBG(cdev, "notify --> %d\n", status);
411 }
412}
413
414static void ecm_notify(struct f_ecm *ecm)
415{
416
417
418
419
420
421 ecm->notify_state = ECM_NOTIFY_CONNECT;
422 ecm_do_notify(ecm);
423}
424
425static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
426{
427 struct f_ecm *ecm = req->context;
428 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
429 struct usb_cdc_notification *event = req->buf;
430
431 switch (req->status) {
432 case 0:
433
434 break;
435 case -ECONNRESET:
436 case -ESHUTDOWN:
437 ecm->notify_state = ECM_NOTIFY_NONE;
438 break;
439 default:
440 DBG(cdev, "event %02x --> %d\n",
441 event->bNotificationType, req->status);
442 break;
443 }
444 ecm->notify_req = req;
445 ecm_do_notify(ecm);
446}
447
448static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
449{
450 struct f_ecm *ecm = func_to_ecm(f);
451 struct usb_composite_dev *cdev = f->config->cdev;
452 struct usb_request *req = cdev->req;
453 int value = -EOPNOTSUPP;
454 u16 w_index = le16_to_cpu(ctrl->wIndex);
455 u16 w_value = le16_to_cpu(ctrl->wValue);
456 u16 w_length = le16_to_cpu(ctrl->wLength);
457
458
459
460
461 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
462 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
463 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
464
465
466
467 if (w_length != 0 || w_index != ecm->ctrl_id)
468 goto invalid;
469 DBG(cdev, "packet filter %02x\n", w_value);
470
471
472
473
474 ecm->port.cdc_filter = w_value;
475 value = 0;
476 break;
477
478
479
480
481
482
483
484
485
486
487 default:
488invalid:
489 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
490 ctrl->bRequestType, ctrl->bRequest,
491 w_value, w_index, w_length);
492 }
493
494
495 if (value >= 0) {
496 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
497 ctrl->bRequestType, ctrl->bRequest,
498 w_value, w_index, w_length);
499 req->zero = 0;
500 req->length = value;
501 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
502 if (value < 0)
503 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
504 ctrl->bRequestType, ctrl->bRequest,
505 value);
506 }
507
508
509 return value;
510}
511
512
513static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
514{
515 struct f_ecm *ecm = func_to_ecm(f);
516 struct usb_composite_dev *cdev = f->config->cdev;
517
518
519 if (intf == ecm->ctrl_id) {
520 if (alt != 0)
521 goto fail;
522
523 if (ecm->notify->driver_data) {
524 VDBG(cdev, "reset ecm control %d\n", intf);
525 usb_ep_disable(ecm->notify);
526 }
527 if (!(ecm->notify->desc)) {
528 VDBG(cdev, "init ecm ctrl %d\n", intf);
529 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
530 goto fail;
531 }
532 usb_ep_enable(ecm->notify);
533 ecm->notify->driver_data = ecm;
534
535
536 } else if (intf == ecm->data_id) {
537 if (alt > 1)
538 goto fail;
539
540 if (ecm->port.in_ep->driver_data) {
541 DBG(cdev, "reset ecm\n");
542 gether_disconnect(&ecm->port);
543 }
544
545 if (!ecm->port.in_ep->desc ||
546 !ecm->port.out_ep->desc) {
547 DBG(cdev, "init ecm\n");
548 if (config_ep_by_speed(cdev->gadget, f,
549 ecm->port.in_ep) ||
550 config_ep_by_speed(cdev->gadget, f,
551 ecm->port.out_ep)) {
552 ecm->port.in_ep->desc = NULL;
553 ecm->port.out_ep->desc = NULL;
554 goto fail;
555 }
556 }
557
558
559
560
561 if (alt == 1) {
562 struct net_device *net;
563
564
565
566
567 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
568 );
569 ecm->port.cdc_filter = DEFAULT_FILTER;
570 DBG(cdev, "activate ecm\n");
571 net = gether_connect(&ecm->port);
572 if (IS_ERR(net))
573 return PTR_ERR(net);
574 }
575
576
577
578
579
580
581
582 ecm_notify(ecm);
583 } else
584 goto fail;
585
586 return 0;
587fail:
588 return -EINVAL;
589}
590
591
592
593
594static int ecm_get_alt(struct usb_function *f, unsigned intf)
595{
596 struct f_ecm *ecm = func_to_ecm(f);
597
598 if (intf == ecm->ctrl_id)
599 return 0;
600 return ecm->port.in_ep->driver_data ? 1 : 0;
601}
602
603static void ecm_disable(struct usb_function *f)
604{
605 struct f_ecm *ecm = func_to_ecm(f);
606 struct usb_composite_dev *cdev = f->config->cdev;
607
608 DBG(cdev, "ecm deactivated\n");
609
610 if (ecm->port.in_ep->driver_data)
611 gether_disconnect(&ecm->port);
612
613 if (ecm->notify->driver_data) {
614 usb_ep_disable(ecm->notify);
615 ecm->notify->driver_data = NULL;
616 ecm->notify->desc = NULL;
617 }
618}
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640static void ecm_open(struct gether *geth)
641{
642 struct f_ecm *ecm = func_to_ecm(&geth->func);
643
644 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
645
646 ecm->is_open = true;
647 ecm_notify(ecm);
648}
649
650static void ecm_close(struct gether *geth)
651{
652 struct f_ecm *ecm = func_to_ecm(&geth->func);
653
654 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
655
656 ecm->is_open = false;
657 ecm_notify(ecm);
658}
659
660
661
662
663
664static int
665ecm_bind(struct usb_configuration *c, struct usb_function *f)
666{
667 struct usb_composite_dev *cdev = c->cdev;
668 struct f_ecm *ecm = func_to_ecm(f);
669 int status;
670 struct usb_ep *ep;
671
672
673 status = usb_interface_id(c, f);
674 if (status < 0)
675 goto fail;
676 ecm->ctrl_id = status;
677
678 ecm_control_intf.bInterfaceNumber = status;
679 ecm_union_desc.bMasterInterface0 = status;
680
681 status = usb_interface_id(c, f);
682 if (status < 0)
683 goto fail;
684 ecm->data_id = status;
685
686 ecm_data_nop_intf.bInterfaceNumber = status;
687 ecm_data_intf.bInterfaceNumber = status;
688 ecm_union_desc.bSlaveInterface0 = status;
689
690 status = -ENODEV;
691
692
693 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
694 if (!ep)
695 goto fail;
696 ecm->port.in_ep = ep;
697 ep->driver_data = cdev;
698
699 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
700 if (!ep)
701 goto fail;
702 ecm->port.out_ep = ep;
703 ep->driver_data = cdev;
704
705
706
707
708
709 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
710 if (!ep)
711 goto fail;
712 ecm->notify = ep;
713 ep->driver_data = cdev;
714
715 status = -ENOMEM;
716
717
718 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
719 if (!ecm->notify_req)
720 goto fail;
721 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
722 if (!ecm->notify_req->buf)
723 goto fail;
724 ecm->notify_req->context = ecm;
725 ecm->notify_req->complete = ecm_notify_complete;
726
727
728 f->descriptors = usb_copy_descriptors(ecm_fs_function);
729 if (!f->descriptors)
730 goto fail;
731
732
733
734
735
736 if (gadget_is_dualspeed(c->cdev->gadget)) {
737 hs_ecm_in_desc.bEndpointAddress =
738 fs_ecm_in_desc.bEndpointAddress;
739 hs_ecm_out_desc.bEndpointAddress =
740 fs_ecm_out_desc.bEndpointAddress;
741 hs_ecm_notify_desc.bEndpointAddress =
742 fs_ecm_notify_desc.bEndpointAddress;
743
744
745 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
746 if (!f->hs_descriptors)
747 goto fail;
748 }
749
750 if (gadget_is_superspeed(c->cdev->gadget)) {
751 ss_ecm_in_desc.bEndpointAddress =
752 fs_ecm_in_desc.bEndpointAddress;
753 ss_ecm_out_desc.bEndpointAddress =
754 fs_ecm_out_desc.bEndpointAddress;
755 ss_ecm_notify_desc.bEndpointAddress =
756 fs_ecm_notify_desc.bEndpointAddress;
757
758
759 f->ss_descriptors = usb_copy_descriptors(ecm_ss_function);
760 if (!f->ss_descriptors)
761 goto fail;
762 }
763
764
765
766
767
768
769 ecm->port.open = ecm_open;
770 ecm->port.close = ecm_close;
771
772 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
773 gadget_is_superspeed(c->cdev->gadget) ? "super" :
774 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
775 ecm->port.in_ep->name, ecm->port.out_ep->name,
776 ecm->notify->name);
777 return 0;
778
779fail:
780 if (f->descriptors)
781 usb_free_descriptors(f->descriptors);
782 if (f->hs_descriptors)
783 usb_free_descriptors(f->hs_descriptors);
784
785 if (ecm->notify_req) {
786 kfree(ecm->notify_req->buf);
787 usb_ep_free_request(ecm->notify, ecm->notify_req);
788 }
789
790
791 if (ecm->notify)
792 ecm->notify->driver_data = NULL;
793 if (ecm->port.out_ep->desc)
794 ecm->port.out_ep->driver_data = NULL;
795 if (ecm->port.in_ep->desc)
796 ecm->port.in_ep->driver_data = NULL;
797
798 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
799
800 return status;
801}
802
803static void
804ecm_unbind(struct usb_configuration *c, struct usb_function *f)
805{
806 struct f_ecm *ecm = func_to_ecm(f);
807
808 DBG(c->cdev, "ecm unbind\n");
809
810 if (gadget_is_superspeed(c->cdev->gadget))
811 usb_free_descriptors(f->ss_descriptors);
812 if (gadget_is_dualspeed(c->cdev->gadget))
813 usb_free_descriptors(f->hs_descriptors);
814 usb_free_descriptors(f->descriptors);
815
816 kfree(ecm->notify_req->buf);
817 usb_ep_free_request(ecm->notify, ecm->notify_req);
818
819 ecm_string_defs[1].s = NULL;
820 kfree(ecm);
821}
822
823
824
825
826
827
828
829
830
831
832
833
834
835int
836ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
837{
838 struct f_ecm *ecm;
839 int status;
840
841 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
842 return -EINVAL;
843
844
845 if (ecm_string_defs[0].id == 0) {
846
847
848 status = usb_string_id(c->cdev);
849 if (status < 0)
850 return status;
851 ecm_string_defs[0].id = status;
852 ecm_control_intf.iInterface = status;
853
854
855 status = usb_string_id(c->cdev);
856 if (status < 0)
857 return status;
858 ecm_string_defs[2].id = status;
859 ecm_data_intf.iInterface = status;
860
861
862 status = usb_string_id(c->cdev);
863 if (status < 0)
864 return status;
865 ecm_string_defs[1].id = status;
866 ecm_desc.iMACAddress = status;
867 }
868
869
870 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
871 if (!ecm)
872 return -ENOMEM;
873
874
875 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
876 "%02X%02X%02X%02X%02X%02X",
877 ethaddr[0], ethaddr[1], ethaddr[2],
878 ethaddr[3], ethaddr[4], ethaddr[5]);
879 ecm_string_defs[1].s = ecm->ethaddr;
880
881 ecm->port.cdc_filter = DEFAULT_FILTER;
882
883 ecm->port.func.name = "cdc_ethernet";
884 ecm->port.func.strings = ecm_strings;
885
886 ecm->port.func.bind = ecm_bind;
887 ecm->port.func.unbind = ecm_unbind;
888 ecm->port.func.set_alt = ecm_set_alt;
889 ecm->port.func.get_alt = ecm_get_alt;
890 ecm->port.func.setup = ecm_setup;
891 ecm->port.func.disable = ecm_disable;
892
893 status = usb_add_function(c, &ecm->port.func);
894 if (status) {
895 ecm_string_defs[1].s = NULL;
896 kfree(ecm);
897 }
898 return status;
899}
900