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