2 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* #define VERBOSE_DEBUG */
24 #include <linux/kernel.h>
25 #include <linux/device.h>
26 #include <linux/etherdevice.h>
32 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
33 * Ethernet link. The data transfer model is simple (packets sent and
34 * received over bulk endpoints using normal short packet termination),
35 * and the control model exposes various data and optional notifications.
37 * ECM is well standardized and (except for Microsoft) supported by most
38 * operating systems with USB host support. It's the preferred interop
39 * solution for Ethernet over USB, at least for firmware based solutions.
40 * (Hardware solutions tend to be more minimalist.) A newer and simpler
41 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
43 * Note that ECM requires the use of "alternate settings" for its data
44 * interface. This means that the set_alt() method has real work to do,
45 * and also means that a get_alt() method is required.
49 struct usb_endpoint_descriptor *in;
50 struct usb_endpoint_descriptor *out;
51 struct usb_endpoint_descriptor *notify;
54 enum ecm_notify_state {
55 ECM_NOTIFY_NONE, /* don't notify */
56 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
57 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
66 struct usb_descriptor_header **fs_function;
67 struct ecm_ep_descs fs;
68 struct usb_descriptor_header **hs_function;
69 struct ecm_ep_descs hs;
71 struct usb_ep *notify;
72 struct usb_endpoint_descriptor *notify_desc;
73 struct usb_request *notify_req;
77 /* FIXME is_open needs some irq-ish locking
78 * ... possibly the same as port.ioport
82 static inline struct f_ecm *func_to_ecm(struct usb_function *f)
84 return container_of(f, struct f_ecm, port.func);
87 /* peak (theoretical) bulk transfer rate in bits-per-second */
88 static inline unsigned bitrate(struct usb_gadget *g)
90 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
91 return 13 * 512 * 8 * 1000 * 8;
93 return 19 * 64 * 1 * 1000 * 8;
96 /*-------------------------------------------------------------------------*/
99 * Include the status endpoint if we can, even though it's optional.
101 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
102 * packet, to simplify cancellation; and a big transfer interval, to
103 * waste less bandwidth.
105 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
106 * if they ignore the connect/disconnect notifications that real aether
107 * can provide. More advanced cdc configurations might want to support
108 * encapsulated commands (vendor-specific, using control-OUT).
111 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
112 #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
115 /* interface descriptor: */
117 static struct usb_interface_descriptor ecm_control_intf __initdata = {
118 .bLength = sizeof ecm_control_intf,
119 .bDescriptorType = USB_DT_INTERFACE,
121 /* .bInterfaceNumber = DYNAMIC */
122 /* status endpoint is optional; this could be patched later */
124 .bInterfaceClass = USB_CLASS_COMM,
125 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
126 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
127 /* .iInterface = DYNAMIC */
130 static struct usb_cdc_header_desc header_desc __initdata = {
131 .bLength = sizeof header_desc,
132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
135 .bcdCDC = __constant_cpu_to_le16(0x0110),
138 static struct usb_cdc_union_desc ecm_union_desc __initdata = {
139 .bLength = sizeof(ecm_union_desc),
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_UNION_TYPE,
142 /* .bMasterInterface0 = DYNAMIC */
143 /* .bSlaveInterface0 = DYNAMIC */
146 static struct usb_cdc_ether_desc ether_desc __initdata = {
147 .bLength = sizeof ether_desc,
148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
151 /* this descriptor actually adds value, surprise! */
152 /* .iMACAddress = DYNAMIC */
153 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
154 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
155 .wNumberMCFilters = __constant_cpu_to_le16(0),
156 .bNumberPowerFilters = 0,
159 /* the default data interface has no endpoints ... */
161 static struct usb_interface_descriptor ecm_data_nop_intf __initdata = {
162 .bLength = sizeof ecm_data_nop_intf,
163 .bDescriptorType = USB_DT_INTERFACE,
165 .bInterfaceNumber = 1,
166 .bAlternateSetting = 0,
168 .bInterfaceClass = USB_CLASS_CDC_DATA,
169 .bInterfaceSubClass = 0,
170 .bInterfaceProtocol = 0,
171 /* .iInterface = DYNAMIC */
174 /* ... but the "real" data interface has two bulk endpoints */
176 static struct usb_interface_descriptor ecm_data_intf __initdata = {
177 .bLength = sizeof ecm_data_intf,
178 .bDescriptorType = USB_DT_INTERFACE,
180 .bInterfaceNumber = 1,
181 .bAlternateSetting = 1,
183 .bInterfaceClass = USB_CLASS_CDC_DATA,
184 .bInterfaceSubClass = 0,
185 .bInterfaceProtocol = 0,
186 /* .iInterface = DYNAMIC */
189 /* full speed support: */
191 static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
195 .bEndpointAddress = USB_DIR_IN,
196 .bmAttributes = USB_ENDPOINT_XFER_INT,
197 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
198 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
201 static struct usb_endpoint_descriptor fs_in_desc __initdata = {
202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_BULK,
209 static struct usb_endpoint_descriptor fs_out_desc __initdata = {
210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
213 .bEndpointAddress = USB_DIR_OUT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
217 static struct usb_descriptor_header *eth_fs_function[] __initdata = {
218 /* CDC ECM control descriptors */
219 (struct usb_descriptor_header *) &ecm_control_intf,
220 (struct usb_descriptor_header *) &header_desc,
221 (struct usb_descriptor_header *) &ecm_union_desc,
222 (struct usb_descriptor_header *) ðer_desc,
223 /* NOTE: status endpoint might need to be removed */
224 (struct usb_descriptor_header *) &fs_notify_desc,
225 /* data interface, altsettings 0 and 1 */
226 (struct usb_descriptor_header *) &ecm_data_nop_intf,
227 (struct usb_descriptor_header *) &ecm_data_intf,
228 (struct usb_descriptor_header *) &fs_in_desc,
229 (struct usb_descriptor_header *) &fs_out_desc,
233 /* high speed support: */
235 static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT,
241 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
242 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
244 static struct usb_endpoint_descriptor hs_in_desc __initdata = {
245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
248 .bEndpointAddress = USB_DIR_IN,
249 .bmAttributes = USB_ENDPOINT_XFER_BULK,
250 .wMaxPacketSize = __constant_cpu_to_le16(512),
253 static struct usb_endpoint_descriptor hs_out_desc __initdata = {
254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
257 .bEndpointAddress = USB_DIR_OUT,
258 .bmAttributes = USB_ENDPOINT_XFER_BULK,
259 .wMaxPacketSize = __constant_cpu_to_le16(512),
262 static struct usb_descriptor_header *eth_hs_function[] __initdata = {
263 /* CDC ECM control descriptors */
264 (struct usb_descriptor_header *) &ecm_control_intf,
265 (struct usb_descriptor_header *) &header_desc,
266 (struct usb_descriptor_header *) &ecm_union_desc,
267 (struct usb_descriptor_header *) ðer_desc,
268 /* NOTE: status endpoint might need to be removed */
269 (struct usb_descriptor_header *) &hs_notify_desc,
270 /* data interface, altsettings 0 and 1 */
271 (struct usb_descriptor_header *) &ecm_data_nop_intf,
272 (struct usb_descriptor_header *) &ecm_data_intf,
273 (struct usb_descriptor_header *) &hs_in_desc,
274 (struct usb_descriptor_header *) &hs_out_desc,
278 /* string descriptors: */
280 static struct usb_string ecm_string_defs[] = {
281 [0].s = "CDC Ethernet Control Model (ECM)",
282 [1].s = NULL /* DYNAMIC */,
283 [2].s = "CDC Ethernet Data",
284 { } /* end of list */
287 static struct usb_gadget_strings ecm_string_table = {
288 .language = 0x0409, /* en-us */
289 .strings = ecm_string_defs,
292 static struct usb_gadget_strings *ecm_strings[] = {
297 /*-------------------------------------------------------------------------*/
299 static void ecm_do_notify(struct f_ecm *ecm)
301 struct usb_request *req = ecm->notify_req;
302 struct usb_cdc_notification *event;
303 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
307 /* notification already in flight? */
312 switch (ecm->notify_state) {
313 case ECM_NOTIFY_NONE:
316 case ECM_NOTIFY_CONNECT:
317 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
319 event->wValue = cpu_to_le16(1);
321 event->wValue = cpu_to_le16(0);
323 req->length = sizeof *event;
325 DBG(cdev, "notify connect %s\n",
326 ecm->is_open ? "true" : "false");
327 ecm->notify_state = ECM_NOTIFY_SPEED;
330 case ECM_NOTIFY_SPEED:
331 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
332 event->wValue = cpu_to_le16(0);
333 event->wLength = cpu_to_le16(8);
334 req->length = STATUS_BYTECOUNT;
336 /* SPEED_CHANGE data is up/down speeds in bits/sec */
337 data = req->buf + sizeof *event;
338 data[0] = cpu_to_le32(bitrate(cdev->gadget));
341 DBG(cdev, "notify speed %d\n", bitrate(cdev->gadget));
342 ecm->notify_state = ECM_NOTIFY_NONE;
345 event->bmRequestType = 0xA1;
346 event->wIndex = cpu_to_le16(ecm->ctrl_id);
348 ecm->notify_req = NULL;
349 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
351 ecm->notify_req = req;
352 DBG(cdev, "notify --> %d\n", status);
356 static void ecm_notify(struct f_ecm *ecm)
358 /* NOTE on most versions of Linux, host side cdc-ethernet
359 * won't listen for notifications until its netdevice opens.
360 * The first notification then sits in the FIFO for a long
361 * time, and the second one is queued.
363 ecm->notify_state = ECM_NOTIFY_CONNECT;
367 static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
369 struct f_ecm *ecm = req->context;
370 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
371 struct usb_cdc_notification *event = req->buf;
373 switch (req->status) {
379 ecm->notify_state = ECM_NOTIFY_NONE;
382 DBG(cdev, "event %02x --> %d\n",
383 event->bNotificationType, req->status);
386 ecm->notify_req = req;
390 static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
392 struct f_ecm *ecm = func_to_ecm(f);
393 struct usb_composite_dev *cdev = f->config->cdev;
394 struct usb_request *req = cdev->req;
395 int value = -EOPNOTSUPP;
396 u16 w_index = le16_to_cpu(ctrl->wIndex);
397 u16 w_value = le16_to_cpu(ctrl->wValue);
398 u16 w_length = le16_to_cpu(ctrl->wLength);
400 /* composite driver infrastructure handles everything except
401 * CDC class messages; interface activation uses set_alt().
403 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
404 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
405 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
406 /* see 6.2.30: no data, wIndex = interface,
407 * wValue = packet filter bitmap
409 if (w_length != 0 || w_index != ecm->ctrl_id)
411 DBG(cdev, "packet filter %02x\n", w_value);
412 /* REVISIT locking of cdc_filter. This assumes the UDC
413 * driver won't have a concurrent packet TX irq running on
414 * another CPU; or that if it does, this write is atomic...
416 ecm->port.cdc_filter = w_value;
421 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
422 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
423 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
424 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
425 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
426 * case USB_CDC_GET_ETHERNET_STATISTIC:
431 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
432 ctrl->bRequestType, ctrl->bRequest,
433 w_value, w_index, w_length);
436 /* respond with data transfer or status phase? */
438 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
439 ctrl->bRequestType, ctrl->bRequest,
440 w_value, w_index, w_length);
443 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
445 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
446 ctrl->bRequestType, ctrl->bRequest,
450 /* device either stalls (value < 0) or reports success */
455 static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
457 struct f_ecm *ecm = func_to_ecm(f);
458 struct usb_composite_dev *cdev = f->config->cdev;
460 /* Control interface has only altsetting 0 */
461 if (intf == ecm->ctrl_id) {
465 if (ecm->notify->driver_data) {
466 VDBG(cdev, "reset ecm control %d\n", intf);
467 usb_ep_disable(ecm->notify);
469 VDBG(cdev, "init ecm ctrl %d\n", intf);
470 ecm->notify_desc = ep_choose(cdev->gadget,
474 usb_ep_enable(ecm->notify, ecm->notify_desc);
475 ecm->notify->driver_data = ecm;
477 /* Data interface has two altsettings, 0 and 1 */
478 } else if (intf == ecm->data_id) {
482 if (ecm->port.in_ep->driver_data) {
483 DBG(cdev, "reset ecm\n");
484 gether_disconnect(&ecm->port);
488 DBG(cdev, "init ecm\n");
489 ecm->port.in = ep_choose(cdev->gadget,
490 ecm->hs.in, ecm->fs.in);
491 ecm->port.out = ep_choose(cdev->gadget,
492 ecm->hs.out, ecm->fs.out);
495 /* CDC Ethernet only sends data in non-default altsettings.
496 * Changing altsettings resets filters, statistics, etc.
499 struct net_device *net;
501 /* Enable zlps by default for ECM conformance;
502 * override for musb_hdrc (avoids txdma ovhead)
503 * and sa1100 (can't).
505 ecm->port.is_zlp_ok = !(
506 gadget_is_sa1100(cdev->gadget)
507 || gadget_is_musbhdrc(cdev->gadget)
509 ecm->port.cdc_filter = DEFAULT_FILTER;
510 DBG(cdev, "activate ecm\n");
511 net = gether_connect(&ecm->port);
516 /* NOTE this can be a minor disagreement with the ECM spec,
517 * which says speed notifications will "always" follow
518 * connection notifications. But we allow one connect to
519 * follow another (if the first is in flight), and instead
520 * just guarantee that a speed notification is always sent.
531 /* Because the data interface supports multiple altsettings,
532 * this ECM function *MUST* implement a get_alt() method.
534 static int ecm_get_alt(struct usb_function *f, unsigned intf)
536 struct f_ecm *ecm = func_to_ecm(f);
538 if (intf == ecm->ctrl_id)
540 return ecm->port.in_ep->driver_data ? 1 : 0;
543 static void ecm_disable(struct usb_function *f)
545 struct f_ecm *ecm = func_to_ecm(f);
546 struct usb_composite_dev *cdev = f->config->cdev;
548 DBG(cdev, "ecm deactivated\n");
550 if (ecm->port.in_ep->driver_data)
551 gether_disconnect(&ecm->port);
553 if (ecm->notify->driver_data) {
554 usb_ep_disable(ecm->notify);
555 ecm->notify->driver_data = NULL;
556 ecm->notify_desc = NULL;
560 /*-------------------------------------------------------------------------*/
563 * Callbacks let us notify the host about connect/disconnect when the
564 * net device is opened or closed.
566 * For testing, note that link states on this side include both opened
567 * and closed variants of:
569 * - disconnected/unconfigured
570 * - configured but inactive (data alt 0)
571 * - configured and active (data alt 1)
573 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
574 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
575 * imply the host is actually polling the notification endpoint, and
576 * likewise that "active" doesn't imply it's actually using the data
577 * endpoints for traffic.
580 static void ecm_open(struct gether *geth)
582 struct f_ecm *ecm = func_to_ecm(&geth->func);
584 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
590 static void ecm_close(struct gether *geth)
592 struct f_ecm *ecm = func_to_ecm(&geth->func);
594 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
596 ecm->is_open = false;
600 /*-------------------------------------------------------------------------*/
602 /* ethernet function driver setup/binding */
605 ecm_bind(struct usb_configuration *c, struct usb_function *f)
607 struct usb_composite_dev *cdev = c->cdev;
608 struct f_ecm *ecm = func_to_ecm(f);
612 /* allocate instance-specific interface IDs */
613 status = usb_interface_id(c, f);
616 ecm->ctrl_id = status;
618 ecm_control_intf.bInterfaceNumber = status;
619 ecm_union_desc.bMasterInterface0 = status;
621 status = usb_interface_id(c, f);
624 ecm->data_id = status;
626 ecm_data_nop_intf.bInterfaceNumber = status;
627 ecm_data_intf.bInterfaceNumber = status;
628 ecm_union_desc.bSlaveInterface0 = status;
632 /* allocate instance-specific endpoints */
633 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
636 ecm->port.in_ep = ep;
637 ep->driver_data = cdev; /* claim */
639 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
642 ecm->port.out_ep = ep;
643 ep->driver_data = cdev; /* claim */
645 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
646 * don't treat it that way. It's simpler, and some newer CDC
647 * profiles (wireless handsets) no longer treat it as optional.
649 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
653 ep->driver_data = cdev; /* claim */
657 /* allocate notification request and buffer */
658 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
659 if (!ecm->notify_req)
661 ecm->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
662 if (!ecm->notify_req->buf)
664 ecm->notify_req->context = ecm;
665 ecm->notify_req->complete = ecm_notify_complete;
667 /* copy descriptors, and track endpoint copies */
668 f->descriptors = usb_copy_descriptors(eth_fs_function);
672 ecm->fs.in = usb_find_endpoint(eth_fs_function,
673 f->descriptors, &fs_in_desc);
674 ecm->fs.out = usb_find_endpoint(eth_fs_function,
675 f->descriptors, &fs_out_desc);
676 ecm->fs.notify = usb_find_endpoint(eth_fs_function,
677 f->descriptors, &fs_notify_desc);
679 /* support all relevant hardware speeds... we expect that when
680 * hardware is dual speed, all bulk-capable endpoints work at
683 if (gadget_is_dualspeed(c->cdev->gadget)) {
684 hs_in_desc.bEndpointAddress =
685 fs_in_desc.bEndpointAddress;
686 hs_out_desc.bEndpointAddress =
687 fs_out_desc.bEndpointAddress;
688 hs_notify_desc.bEndpointAddress =
689 fs_notify_desc.bEndpointAddress;
691 /* copy descriptors, and track endpoint copies */
692 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
693 if (!f->hs_descriptors)
696 ecm->hs.in = usb_find_endpoint(eth_hs_function,
697 f->hs_descriptors, &hs_in_desc);
698 ecm->hs.out = usb_find_endpoint(eth_hs_function,
699 f->hs_descriptors, &hs_out_desc);
700 ecm->hs.notify = usb_find_endpoint(eth_hs_function,
701 f->hs_descriptors, &hs_notify_desc);
704 /* NOTE: all that is done without knowing or caring about
705 * the network link ... which is unavailable to this code
706 * until we're activated via set_alt().
709 ecm->port.open = ecm_open;
710 ecm->port.close = ecm_close;
712 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
713 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
714 ecm->port.in_ep->name, ecm->port.out_ep->name,
720 usb_free_descriptors(f->descriptors);
722 if (ecm->notify_req) {
723 kfree(ecm->notify_req->buf);
724 usb_ep_free_request(ecm->notify, ecm->notify_req);
727 /* we might as well release our claims on endpoints */
729 ecm->notify->driver_data = NULL;
731 ecm->port.out_ep->driver_data = NULL;
733 ecm->port.in_ep->driver_data = NULL;
735 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
741 ecm_unbind(struct usb_configuration *c, struct usb_function *f)
743 struct f_ecm *ecm = func_to_ecm(f);
745 DBG(c->cdev, "ecm unbind\n");
747 if (gadget_is_dualspeed(c->cdev->gadget))
748 usb_free_descriptors(f->hs_descriptors);
749 usb_free_descriptors(f->descriptors);
751 kfree(ecm->notify_req->buf);
752 usb_ep_free_request(ecm->notify, ecm->notify_req);
754 ecm_string_defs[1].s = NULL;
759 * ecm_bind_config - add CDC Ethernet network link to a configuration
760 * @c: the configuration to support the network link
761 * @ethaddr: a buffer in which the ethernet address of the host side
762 * side of the link was recorded
763 * Context: single threaded during gadget setup
765 * Returns zero on success, else negative errno.
767 * Caller must have called @gether_setup(). Caller is also responsible
768 * for calling @gether_cleanup() before module unload.
770 int __init ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
775 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
778 /* maybe allocate device-global string IDs */
779 if (ecm_string_defs[0].id == 0) {
781 /* control interface label */
782 status = usb_string_id(c->cdev);
785 ecm_string_defs[0].id = status;
786 ecm_control_intf.iInterface = status;
788 /* data interface label */
789 status = usb_string_id(c->cdev);
792 ecm_string_defs[2].id = status;
793 ecm_data_intf.iInterface = status;
796 status = usb_string_id(c->cdev);
799 ecm_string_defs[1].id = status;
800 ether_desc.iMACAddress = status;
803 /* allocate and initialize one new instance */
804 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
808 /* export host's Ethernet address in CDC format */
809 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
810 "%02X%02X%02X%02X%02X%02X",
811 ethaddr[0], ethaddr[1], ethaddr[2],
812 ethaddr[3], ethaddr[4], ethaddr[5]);
813 ecm_string_defs[1].s = ecm->ethaddr;
815 ecm->port.cdc_filter = DEFAULT_FILTER;
817 ecm->port.func.name = "cdc_ethernet";
818 ecm->port.func.strings = ecm_strings;
819 /* descriptors are per-instance copies */
820 ecm->port.func.bind = ecm_bind;
821 ecm->port.func.unbind = ecm_unbind;
822 ecm->port.func.set_alt = ecm_set_alt;
823 ecm->port.func.get_alt = ecm_get_alt;
824 ecm->port.func.setup = ecm_setup;
825 ecm->port.func.disable = ecm_disable;
827 status = usb_add_function(c, &ecm->port.func);
829 ecm_string_defs[1].s = NULL;