15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/device.h>
69 static inline unsigned ecm_bitrate(
struct usb_gadget *g)
72 return 13 * 1024 * 8 * 1000 * 8;
74 return 13 * 512 * 8 * 1000 * 8;
76 return 19 * 64 * 1 * 1000 * 8;
94 #define LOG2_STATUS_INTERVAL_MSEC 5
95 #define ECM_STATUS_BYTECOUNT 16
101 ecm_iad_descriptor = {
102 .bLength =
sizeof ecm_iad_descriptor,
106 .bInterfaceCount = 2,
115 .bLength =
sizeof ecm_control_intf,
128 .bLength =
sizeof ecm_header_desc,
136 .bLength =
sizeof(ecm_union_desc),
144 .bLength =
sizeof ecm_desc,
153 .bNumberPowerFilters = 0,
159 .bLength =
sizeof ecm_data_nop_intf,
162 .bInterfaceNumber = 1,
163 .bAlternateSetting = 0,
166 .bInterfaceSubClass = 0,
167 .bInterfaceProtocol = 0,
174 .bLength =
sizeof ecm_data_intf,
177 .bInterfaceNumber = 1,
178 .bAlternateSetting = 1,
181 .bInterfaceSubClass = 0,
182 .bInterfaceProtocol = 0,
295 .bLength =
sizeof ss_ecm_intr_comp_desc,
323 .bLength =
sizeof ss_ecm_bulk_comp_desc,
354 static struct usb_string ecm_string_defs[] = {
355 [0].s =
"CDC Ethernet Control Model (ECM)",
357 [2].s =
"CDC Ethernet Data",
364 .strings = ecm_string_defs,
374 static void ecm_do_notify(
struct f_ecm *
ecm)
400 DBG(cdev,
"notify connect %s\n",
401 ecm->
is_open ?
"true" :
"false");
416 DBG(cdev,
"notify speed %d\n", ecm_bitrate(cdev->
gadget));
420 event->bmRequestType = 0xA1;
427 DBG(cdev,
"notify --> %d\n", status);
431 static void ecm_notify(
struct f_ecm *ecm)
457 DBG(cdev,
"event %02x --> %d\n",
467 struct f_ecm *ecm = func_to_ecm(f);
484 if (w_length != 0 || w_index != ecm->
ctrl_id)
486 DBG(cdev,
"packet filter %02x\n", w_value);
491 ecm->
port.cdc_filter = w_value;
506 DBG(cdev,
"invalid control req%02x.%02x v%04x i%04x l%d\n",
508 w_value, w_index, w_length);
513 DBG(cdev,
"ecm req%02x.%02x v%04x i%04x l%d\n",
515 w_value, w_index, w_length);
520 ERROR(cdev,
"ecm req %02x.%02x response err %d\n",
532 struct f_ecm *ecm = func_to_ecm(f);
540 if (ecm->
notify->driver_data) {
541 VDBG(cdev,
"reset ecm control %d\n", intf);
542 usb_ep_disable(ecm->
notify);
544 if (!(ecm->
notify->desc)) {
545 VDBG(cdev,
"init ecm ctrl %d\n", intf);
549 usb_ep_enable(ecm->
notify);
553 }
else if (intf == ecm->
data_id) {
557 if (ecm->
port.in_ep->driver_data) {
558 DBG(cdev,
"reset ecm\n");
562 if (!ecm->
port.in_ep->desc ||
563 !ecm->
port.out_ep->desc) {
564 DBG(cdev,
"init ecm\n");
587 DBG(cdev,
"activate ecm\n");
611 static int ecm_get_alt(
struct usb_function *f,
unsigned intf)
613 struct f_ecm *ecm = func_to_ecm(f);
617 return ecm->
port.in_ep->driver_data ? 1 : 0;
622 struct f_ecm *ecm = func_to_ecm(f);
625 DBG(cdev,
"ecm deactivated\n");
627 if (ecm->
port.in_ep->driver_data)
630 if (ecm->
notify->driver_data) {
631 usb_ep_disable(ecm->
notify);
657 static void ecm_open(
struct gether *geth)
659 struct f_ecm *ecm = func_to_ecm(&geth->
func);
661 DBG(ecm->
port.func.config->cdev,
"%s\n", __func__);
667 static void ecm_close(
struct gether *geth)
669 struct f_ecm *ecm = func_to_ecm(&geth->
func);
671 DBG(ecm->
port.func.config->cdev,
"%s\n", __func__);
685 struct f_ecm *ecm = func_to_ecm(f);
714 ecm->
port.in_ep = ep;
720 ecm->
port.out_ep = ep;
743 ecm->
notify_req->complete = ecm_notify_complete;
754 if (gadget_is_dualspeed(c->
cdev->gadget)) {
768 if (gadget_is_superspeed(c->
cdev->gadget)) {
787 ecm->
port.open = ecm_open;
788 ecm->
port.close = ecm_close;
790 DBG(cdev,
"CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
791 gadget_is_superspeed(c->
cdev->gadget) ?
"super" :
792 gadget_is_dualspeed(c->
cdev->gadget) ?
"dual" :
"full",
793 ecm->
port.in_ep->name, ecm->
port.out_ep->name,
811 if (ecm->
port.out_ep->desc)
812 ecm->
port.out_ep->driver_data =
NULL;
813 if (ecm->
port.in_ep->desc)
814 ecm->
port.in_ep->driver_data =
NULL;
816 ERROR(cdev,
"%s: can't bind, err %d\n", f->
name, status);
824 struct f_ecm *ecm = func_to_ecm(f);
828 if (gadget_is_superspeed(c->
cdev->gadget))
830 if (gadget_is_dualspeed(c->
cdev->gadget))
837 ecm_string_defs[1].
s =
NULL;
863 if (ecm_string_defs[0].
id == 0) {
901 ecm_string_defs[1].
s = ecm->
ethaddr;
905 ecm->
port.func.name =
"cdc_ethernet";
906 ecm->
port.func.strings = ecm_strings;
908 ecm->
port.func.bind = ecm_bind;
909 ecm->
port.func.unbind = ecm_unbind;
910 ecm->
port.func.set_alt = ecm_set_alt;
911 ecm->
port.func.get_alt = ecm_get_alt;
912 ecm->
port.func.setup = ecm_setup;
913 ecm->
port.func.disable = ecm_disable;
917 ecm_string_defs[1].
s =
NULL;