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.
13 /* #define VERBOSE_DEBUG */
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>
22 #include "u_ether_configfs.h"
27 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
28 * Ethernet link. The data transfer model is simple (packets sent and
29 * received over bulk endpoints using normal short packet termination),
30 * and the control model exposes various data and optional notifications.
32 * ECM is well standardized and (except for Microsoft) supported by most
33 * operating systems with USB host support. It's the preferred interop
34 * solution for Ethernet over USB, at least for firmware based solutions.
35 * (Hardware solutions tend to be more minimalist.) A newer and simpler
36 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
38 * Note that ECM requires the use of "alternate settings" for its data
39 * interface. This means that the set_alt() method has real work to do,
40 * and also means that a get_alt() method is required.
44 enum ecm_notify_state {
45 ECM_NOTIFY_NONE, /* don't notify */
46 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
47 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
56 struct usb_ep *notify;
57 struct usb_request *notify_req;
59 atomic_t notify_count;
62 /* FIXME is_open needs some irq-ish locking
63 * ... possibly the same as port.ioport
67 static inline struct f_ecm *func_to_ecm(struct usb_function *f)
69 return container_of(f, struct f_ecm, port.func);
72 /* peak (theoretical) bulk transfer rate in bits-per-second */
73 static inline unsigned ecm_bitrate(struct usb_gadget *g)
75 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
76 return 13 * 1024 * 8 * 1000 * 8;
77 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
78 return 13 * 512 * 8 * 1000 * 8;
80 return 19 * 64 * 1 * 1000 * 8;
83 /*-------------------------------------------------------------------------*/
86 * Include the status endpoint if we can, even though it's optional.
88 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
89 * packet, to simplify cancellation; and a big transfer interval, to
90 * waste less bandwidth.
92 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
93 * if they ignore the connect/disconnect notifications that real aether
94 * can provide. More advanced cdc configurations might want to support
95 * encapsulated commands (vendor-specific, using control-OUT).
98 #define ECM_STATUS_INTERVAL_MS 32
99 #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
102 /* interface descriptor: */
104 static struct usb_interface_assoc_descriptor
105 ecm_iad_descriptor = {
106 .bLength = sizeof ecm_iad_descriptor,
107 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
109 /* .bFirstInterface = DYNAMIC, */
110 .bInterfaceCount = 2, /* control + data */
111 .bFunctionClass = USB_CLASS_COMM,
112 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
113 .bFunctionProtocol = USB_CDC_PROTO_NONE,
114 /* .iFunction = DYNAMIC */
118 static struct usb_interface_descriptor ecm_control_intf = {
119 .bLength = sizeof ecm_control_intf,
120 .bDescriptorType = USB_DT_INTERFACE,
122 /* .bInterfaceNumber = DYNAMIC */
123 /* status endpoint is optional; this could be patched later */
125 .bInterfaceClass = USB_CLASS_COMM,
126 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
127 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
128 /* .iInterface = DYNAMIC */
131 static struct usb_cdc_header_desc ecm_header_desc = {
132 .bLength = sizeof ecm_header_desc,
133 .bDescriptorType = USB_DT_CS_INTERFACE,
134 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
136 .bcdCDC = cpu_to_le16(0x0110),
139 static struct usb_cdc_union_desc ecm_union_desc = {
140 .bLength = sizeof(ecm_union_desc),
141 .bDescriptorType = USB_DT_CS_INTERFACE,
142 .bDescriptorSubType = USB_CDC_UNION_TYPE,
143 /* .bMasterInterface0 = DYNAMIC */
144 /* .bSlaveInterface0 = DYNAMIC */
147 static struct usb_cdc_ether_desc ecm_desc = {
148 .bLength = sizeof ecm_desc,
149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
152 /* this descriptor actually adds value, surprise! */
153 /* .iMACAddress = DYNAMIC */
154 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
155 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
156 .wNumberMCFilters = cpu_to_le16(0),
157 .bNumberPowerFilters = 0,
160 /* the default data interface has no endpoints ... */
162 static struct usb_interface_descriptor ecm_data_nop_intf = {
163 .bLength = sizeof ecm_data_nop_intf,
164 .bDescriptorType = USB_DT_INTERFACE,
166 .bInterfaceNumber = 1,
167 .bAlternateSetting = 0,
169 .bInterfaceClass = USB_CLASS_CDC_DATA,
170 .bInterfaceSubClass = 0,
171 .bInterfaceProtocol = 0,
172 /* .iInterface = DYNAMIC */
175 /* ... but the "real" data interface has two bulk endpoints */
177 static struct usb_interface_descriptor ecm_data_intf = {
178 .bLength = sizeof ecm_data_intf,
179 .bDescriptorType = USB_DT_INTERFACE,
181 .bInterfaceNumber = 1,
182 .bAlternateSetting = 1,
184 .bInterfaceClass = USB_CLASS_CDC_DATA,
185 .bInterfaceSubClass = 0,
186 .bInterfaceProtocol = 0,
187 /* .iInterface = DYNAMIC */
190 /* full speed support: */
192 static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
196 .bEndpointAddress = USB_DIR_IN,
197 .bmAttributes = USB_ENDPOINT_XFER_INT,
198 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
199 .bInterval = ECM_STATUS_INTERVAL_MS,
202 static struct usb_endpoint_descriptor fs_ecm_in_desc = {
203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
210 static struct usb_endpoint_descriptor fs_ecm_out_desc = {
211 .bLength = USB_DT_ENDPOINT_SIZE,
212 .bDescriptorType = USB_DT_ENDPOINT,
214 .bEndpointAddress = USB_DIR_OUT,
215 .bmAttributes = USB_ENDPOINT_XFER_BULK,
218 static struct usb_descriptor_header *ecm_fs_function[] = {
219 /* CDC ECM control descriptors */
220 (struct usb_descriptor_header *) &ecm_iad_descriptor,
221 (struct usb_descriptor_header *) &ecm_control_intf,
222 (struct usb_descriptor_header *) &ecm_header_desc,
223 (struct usb_descriptor_header *) &ecm_union_desc,
224 (struct usb_descriptor_header *) &ecm_desc,
226 /* NOTE: status endpoint might need to be removed */
227 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
229 /* data interface, altsettings 0 and 1 */
230 (struct usb_descriptor_header *) &ecm_data_nop_intf,
231 (struct usb_descriptor_header *) &ecm_data_intf,
232 (struct usb_descriptor_header *) &fs_ecm_in_desc,
233 (struct usb_descriptor_header *) &fs_ecm_out_desc,
237 /* high speed support: */
239 static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
240 .bLength = USB_DT_ENDPOINT_SIZE,
241 .bDescriptorType = USB_DT_ENDPOINT,
243 .bEndpointAddress = USB_DIR_IN,
244 .bmAttributes = USB_ENDPOINT_XFER_INT,
245 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
246 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
249 static struct usb_endpoint_descriptor hs_ecm_in_desc = {
250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
253 .bEndpointAddress = USB_DIR_IN,
254 .bmAttributes = USB_ENDPOINT_XFER_BULK,
255 .wMaxPacketSize = cpu_to_le16(512),
258 static struct usb_endpoint_descriptor hs_ecm_out_desc = {
259 .bLength = USB_DT_ENDPOINT_SIZE,
260 .bDescriptorType = USB_DT_ENDPOINT,
262 .bEndpointAddress = USB_DIR_OUT,
263 .bmAttributes = USB_ENDPOINT_XFER_BULK,
264 .wMaxPacketSize = cpu_to_le16(512),
267 static struct usb_descriptor_header *ecm_hs_function[] = {
268 /* CDC ECM control descriptors */
269 (struct usb_descriptor_header *) &ecm_iad_descriptor,
270 (struct usb_descriptor_header *) &ecm_control_intf,
271 (struct usb_descriptor_header *) &ecm_header_desc,
272 (struct usb_descriptor_header *) &ecm_union_desc,
273 (struct usb_descriptor_header *) &ecm_desc,
275 /* NOTE: status endpoint might need to be removed */
276 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
278 /* data interface, altsettings 0 and 1 */
279 (struct usb_descriptor_header *) &ecm_data_nop_intf,
280 (struct usb_descriptor_header *) &ecm_data_intf,
281 (struct usb_descriptor_header *) &hs_ecm_in_desc,
282 (struct usb_descriptor_header *) &hs_ecm_out_desc,
286 /* super speed support: */
288 static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
289 .bLength = USB_DT_ENDPOINT_SIZE,
290 .bDescriptorType = USB_DT_ENDPOINT,
292 .bEndpointAddress = USB_DIR_IN,
293 .bmAttributes = USB_ENDPOINT_XFER_INT,
294 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
295 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
298 static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
299 .bLength = sizeof ss_ecm_intr_comp_desc,
300 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
302 /* the following 3 values can be tweaked if necessary */
303 /* .bMaxBurst = 0, */
304 /* .bmAttributes = 0, */
305 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
308 static struct usb_endpoint_descriptor ss_ecm_in_desc = {
309 .bLength = USB_DT_ENDPOINT_SIZE,
310 .bDescriptorType = USB_DT_ENDPOINT,
312 .bEndpointAddress = USB_DIR_IN,
313 .bmAttributes = USB_ENDPOINT_XFER_BULK,
314 .wMaxPacketSize = cpu_to_le16(1024),
317 static struct usb_endpoint_descriptor ss_ecm_out_desc = {
318 .bLength = USB_DT_ENDPOINT_SIZE,
319 .bDescriptorType = USB_DT_ENDPOINT,
321 .bEndpointAddress = USB_DIR_OUT,
322 .bmAttributes = USB_ENDPOINT_XFER_BULK,
323 .wMaxPacketSize = cpu_to_le16(1024),
326 static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
327 .bLength = sizeof ss_ecm_bulk_comp_desc,
328 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
330 /* the following 2 values can be tweaked if necessary */
331 /* .bMaxBurst = 0, */
332 /* .bmAttributes = 0, */
335 static struct usb_descriptor_header *ecm_ss_function[] = {
336 /* CDC ECM control descriptors */
337 (struct usb_descriptor_header *) &ecm_iad_descriptor,
338 (struct usb_descriptor_header *) &ecm_control_intf,
339 (struct usb_descriptor_header *) &ecm_header_desc,
340 (struct usb_descriptor_header *) &ecm_union_desc,
341 (struct usb_descriptor_header *) &ecm_desc,
343 /* NOTE: status endpoint might need to be removed */
344 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
345 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
347 /* data interface, altsettings 0 and 1 */
348 (struct usb_descriptor_header *) &ecm_data_nop_intf,
349 (struct usb_descriptor_header *) &ecm_data_intf,
350 (struct usb_descriptor_header *) &ss_ecm_in_desc,
351 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
352 (struct usb_descriptor_header *) &ss_ecm_out_desc,
353 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
357 /* string descriptors: */
359 static struct usb_string ecm_string_defs[] = {
360 [0].s = "CDC Ethernet Control Model (ECM)",
362 [2].s = "CDC Ethernet Data",
364 { } /* end of list */
367 static struct usb_gadget_strings ecm_string_table = {
368 .language = 0x0409, /* en-us */
369 .strings = ecm_string_defs,
372 static struct usb_gadget_strings *ecm_strings[] = {
377 /*-------------------------------------------------------------------------*/
379 static void ecm_do_notify(struct f_ecm *ecm)
381 struct usb_request *req = ecm->notify_req;
382 struct usb_cdc_notification *event;
383 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
387 /* notification already in flight? */
388 if (atomic_read(&ecm->notify_count))
392 switch (ecm->notify_state) {
393 case ECM_NOTIFY_NONE:
396 case ECM_NOTIFY_CONNECT:
397 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
399 event->wValue = cpu_to_le16(1);
401 event->wValue = cpu_to_le16(0);
403 req->length = sizeof *event;
405 DBG(cdev, "notify connect %s\n",
406 ecm->is_open ? "true" : "false");
407 ecm->notify_state = ECM_NOTIFY_SPEED;
410 case ECM_NOTIFY_SPEED:
411 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
412 event->wValue = cpu_to_le16(0);
413 event->wLength = cpu_to_le16(8);
414 req->length = ECM_STATUS_BYTECOUNT;
416 /* SPEED_CHANGE data is up/down speeds in bits/sec */
417 data = req->buf + sizeof *event;
418 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
421 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
422 ecm->notify_state = ECM_NOTIFY_NONE;
425 event->bmRequestType = 0xA1;
426 event->wIndex = cpu_to_le16(ecm->ctrl_id);
428 atomic_inc(&ecm->notify_count);
429 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
431 atomic_dec(&ecm->notify_count);
432 DBG(cdev, "notify --> %d\n", status);
436 static void ecm_notify(struct f_ecm *ecm)
438 /* NOTE on most versions of Linux, host side cdc-ethernet
439 * won't listen for notifications until its netdevice opens.
440 * The first notification then sits in the FIFO for a long
441 * time, and the second one is queued.
443 ecm->notify_state = ECM_NOTIFY_CONNECT;
447 static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
449 struct f_ecm *ecm = req->context;
450 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
451 struct usb_cdc_notification *event = req->buf;
453 switch (req->status) {
456 atomic_dec(&ecm->notify_count);
460 atomic_set(&ecm->notify_count, 0);
461 ecm->notify_state = ECM_NOTIFY_NONE;
464 DBG(cdev, "event %02x --> %d\n",
465 event->bNotificationType, req->status);
466 atomic_dec(&ecm->notify_count);
472 static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
474 struct f_ecm *ecm = func_to_ecm(f);
475 struct usb_composite_dev *cdev = f->config->cdev;
476 struct usb_request *req = cdev->req;
477 int value = -EOPNOTSUPP;
478 u16 w_index = le16_to_cpu(ctrl->wIndex);
479 u16 w_value = le16_to_cpu(ctrl->wValue);
480 u16 w_length = le16_to_cpu(ctrl->wLength);
482 /* composite driver infrastructure handles everything except
483 * CDC class messages; interface activation uses set_alt().
485 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
486 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
487 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
488 /* see 6.2.30: no data, wIndex = interface,
489 * wValue = packet filter bitmap
491 if (w_length != 0 || w_index != ecm->ctrl_id)
493 DBG(cdev, "packet filter %02x\n", w_value);
494 /* REVISIT locking of cdc_filter. This assumes the UDC
495 * driver won't have a concurrent packet TX irq running on
496 * another CPU; or that if it does, this write is atomic...
498 ecm->port.cdc_filter = w_value;
503 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
504 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
505 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
506 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
507 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
508 * case USB_CDC_GET_ETHERNET_STATISTIC:
513 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
514 ctrl->bRequestType, ctrl->bRequest,
515 w_value, w_index, w_length);
518 /* respond with data transfer or status phase? */
520 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
521 ctrl->bRequestType, ctrl->bRequest,
522 w_value, w_index, w_length);
525 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
527 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
528 ctrl->bRequestType, ctrl->bRequest,
532 /* device either stalls (value < 0) or reports success */
537 static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
539 struct f_ecm *ecm = func_to_ecm(f);
540 struct usb_composite_dev *cdev = f->config->cdev;
542 /* Control interface has only altsetting 0 */
543 if (intf == ecm->ctrl_id) {
547 VDBG(cdev, "reset ecm control %d\n", intf);
548 usb_ep_disable(ecm->notify);
549 if (!(ecm->notify->desc)) {
550 VDBG(cdev, "init ecm ctrl %d\n", intf);
551 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
554 usb_ep_enable(ecm->notify);
556 /* Data interface has two altsettings, 0 and 1 */
557 } else if (intf == ecm->data_id) {
561 if (ecm->port.in_ep->enabled) {
562 DBG(cdev, "reset ecm\n");
563 gether_disconnect(&ecm->port);
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,
571 config_ep_by_speed(cdev->gadget, f,
573 ecm->port.in_ep->desc = NULL;
574 ecm->port.out_ep->desc = NULL;
579 /* CDC Ethernet only sends data in non-default altsettings.
580 * Changing altsettings resets filters, statistics, etc.
583 struct net_device *net;
585 /* Enable zlps by default for ECM conformance;
586 * override for musb_hdrc (avoids txdma ovhead).
588 ecm->port.is_zlp_ok =
589 gadget_is_zlp_supported(cdev->gadget);
590 ecm->port.cdc_filter = DEFAULT_FILTER;
591 DBG(cdev, "activate ecm\n");
592 net = gether_connect(&ecm->port);
597 /* NOTE this can be a minor disagreement with the ECM spec,
598 * which says speed notifications will "always" follow
599 * connection notifications. But we allow one connect to
600 * follow another (if the first is in flight), and instead
601 * just guarantee that a speed notification is always sent.
612 /* Because the data interface supports multiple altsettings,
613 * this ECM function *MUST* implement a get_alt() method.
615 static int ecm_get_alt(struct usb_function *f, unsigned intf)
617 struct f_ecm *ecm = func_to_ecm(f);
619 if (intf == ecm->ctrl_id)
621 return ecm->port.in_ep->enabled ? 1 : 0;
624 static void ecm_disable(struct usb_function *f)
626 struct f_ecm *ecm = func_to_ecm(f);
627 struct usb_composite_dev *cdev = f->config->cdev;
629 DBG(cdev, "ecm deactivated\n");
631 if (ecm->port.in_ep->enabled) {
632 gether_disconnect(&ecm->port);
634 ecm->port.in_ep->desc = NULL;
635 ecm->port.out_ep->desc = NULL;
638 usb_ep_disable(ecm->notify);
639 ecm->notify->desc = NULL;
642 /*-------------------------------------------------------------------------*/
645 * Callbacks let us notify the host about connect/disconnect when the
646 * net device is opened or closed.
648 * For testing, note that link states on this side include both opened
649 * and closed variants of:
651 * - disconnected/unconfigured
652 * - configured but inactive (data alt 0)
653 * - configured and active (data alt 1)
655 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
656 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
657 * imply the host is actually polling the notification endpoint, and
658 * likewise that "active" doesn't imply it's actually using the data
659 * endpoints for traffic.
662 static void ecm_open(struct gether *geth)
664 struct f_ecm *ecm = func_to_ecm(&geth->func);
666 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
672 static void ecm_close(struct gether *geth)
674 struct f_ecm *ecm = func_to_ecm(&geth->func);
676 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
678 ecm->is_open = false;
682 /*-------------------------------------------------------------------------*/
684 /* ethernet function driver setup/binding */
687 ecm_bind(struct usb_configuration *c, struct usb_function *f)
689 struct usb_composite_dev *cdev = c->cdev;
690 struct f_ecm *ecm = func_to_ecm(f);
691 struct usb_string *us;
695 struct f_ecm_opts *ecm_opts;
697 if (!can_support_ecm(cdev->gadget))
700 ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
703 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
704 * configurations are bound in sequence with list_for_each_entry,
705 * in each configuration its functions are bound in sequence
706 * with list_for_each_entry, so we assume no race condition
707 * with regard to ecm_opts->bound access
709 if (!ecm_opts->bound) {
710 mutex_lock(&ecm_opts->lock);
711 gether_set_gadget(ecm_opts->net, cdev->gadget);
712 status = gether_register_netdev(ecm_opts->net);
713 mutex_unlock(&ecm_opts->lock);
716 ecm_opts->bound = true;
719 us = usb_gstrings_attach(cdev, ecm_strings,
720 ARRAY_SIZE(ecm_string_defs));
723 ecm_control_intf.iInterface = us[0].id;
724 ecm_data_intf.iInterface = us[2].id;
725 ecm_desc.iMACAddress = us[1].id;
726 ecm_iad_descriptor.iFunction = us[3].id;
728 /* allocate instance-specific interface IDs */
729 status = usb_interface_id(c, f);
732 ecm->ctrl_id = status;
733 ecm_iad_descriptor.bFirstInterface = status;
735 ecm_control_intf.bInterfaceNumber = status;
736 ecm_union_desc.bMasterInterface0 = status;
738 status = usb_interface_id(c, f);
741 ecm->data_id = status;
743 ecm_data_nop_intf.bInterfaceNumber = status;
744 ecm_data_intf.bInterfaceNumber = status;
745 ecm_union_desc.bSlaveInterface0 = status;
749 /* allocate instance-specific endpoints */
750 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
753 ecm->port.in_ep = ep;
755 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
758 ecm->port.out_ep = ep;
760 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
761 * don't treat it that way. It's simpler, and some newer CDC
762 * profiles (wireless handsets) no longer treat it as optional.
764 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
771 /* allocate notification request and buffer */
772 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
773 if (!ecm->notify_req)
775 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
776 if (!ecm->notify_req->buf)
778 ecm->notify_req->context = ecm;
779 ecm->notify_req->complete = ecm_notify_complete;
781 /* support all relevant hardware speeds... we expect that when
782 * hardware is dual speed, all bulk-capable endpoints work at
785 hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
786 hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
787 hs_ecm_notify_desc.bEndpointAddress =
788 fs_ecm_notify_desc.bEndpointAddress;
790 ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
791 ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
792 ss_ecm_notify_desc.bEndpointAddress =
793 fs_ecm_notify_desc.bEndpointAddress;
795 status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
800 /* NOTE: all that is done without knowing or caring about
801 * the network link ... which is unavailable to this code
802 * until we're activated via set_alt().
805 ecm->port.open = ecm_open;
806 ecm->port.close = ecm_close;
808 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
809 gadget_is_superspeed(c->cdev->gadget) ? "super" :
810 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
811 ecm->port.in_ep->name, ecm->port.out_ep->name,
816 if (ecm->notify_req) {
817 kfree(ecm->notify_req->buf);
818 usb_ep_free_request(ecm->notify, ecm->notify_req);
821 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
826 static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
828 return container_of(to_config_group(item), struct f_ecm_opts,
833 USB_ETHERNET_CONFIGFS_ITEM(ecm);
835 /* f_ecm_opts_dev_addr */
836 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);
838 /* f_ecm_opts_host_addr */
839 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);
841 /* f_ecm_opts_qmult */
842 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);
844 /* f_ecm_opts_ifname */
845 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);
847 static struct configfs_attribute *ecm_attrs[] = {
848 &ecm_opts_attr_dev_addr,
849 &ecm_opts_attr_host_addr,
850 &ecm_opts_attr_qmult,
851 &ecm_opts_attr_ifname,
855 static struct config_item_type ecm_func_type = {
856 .ct_item_ops = &ecm_item_ops,
857 .ct_attrs = ecm_attrs,
858 .ct_owner = THIS_MODULE,
861 static void ecm_free_inst(struct usb_function_instance *f)
863 struct f_ecm_opts *opts;
865 opts = container_of(f, struct f_ecm_opts, func_inst);
867 gether_cleanup(netdev_priv(opts->net));
869 free_netdev(opts->net);
873 static struct usb_function_instance *ecm_alloc_inst(void)
875 struct f_ecm_opts *opts;
877 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
879 return ERR_PTR(-ENOMEM);
880 mutex_init(&opts->lock);
881 opts->func_inst.free_func_inst = ecm_free_inst;
882 opts->net = gether_setup_default();
883 if (IS_ERR(opts->net)) {
884 struct net_device *net = opts->net;
886 return ERR_CAST(net);
889 config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);
891 return &opts->func_inst;
894 static void ecm_free(struct usb_function *f)
897 struct f_ecm_opts *opts;
899 ecm = func_to_ecm(f);
900 opts = container_of(f->fi, struct f_ecm_opts, func_inst);
902 mutex_lock(&opts->lock);
904 mutex_unlock(&opts->lock);
907 static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
909 struct f_ecm *ecm = func_to_ecm(f);
911 DBG(c->cdev, "ecm unbind\n");
913 usb_free_all_descriptors(f);
915 if (atomic_read(&ecm->notify_count)) {
916 usb_ep_dequeue(ecm->notify, ecm->notify_req);
917 atomic_set(&ecm->notify_count, 0);
920 kfree(ecm->notify_req->buf);
921 usb_ep_free_request(ecm->notify, ecm->notify_req);
924 static struct usb_function *ecm_alloc(struct usb_function_instance *fi)
927 struct f_ecm_opts *opts;
930 /* allocate and initialize one new instance */
931 ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
933 return ERR_PTR(-ENOMEM);
935 opts = container_of(fi, struct f_ecm_opts, func_inst);
936 mutex_lock(&opts->lock);
939 /* export host's Ethernet address in CDC format */
940 status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
941 sizeof(ecm->ethaddr));
944 mutex_unlock(&opts->lock);
945 return ERR_PTR(-EINVAL);
947 ecm_string_defs[1].s = ecm->ethaddr;
949 ecm->port.ioport = netdev_priv(opts->net);
950 mutex_unlock(&opts->lock);
951 ecm->port.cdc_filter = DEFAULT_FILTER;
953 ecm->port.func.name = "cdc_ethernet";
954 /* descriptors are per-instance copies */
955 ecm->port.func.bind = ecm_bind;
956 ecm->port.func.unbind = ecm_unbind;
957 ecm->port.func.set_alt = ecm_set_alt;
958 ecm->port.func.get_alt = ecm_get_alt;
959 ecm->port.func.setup = ecm_setup;
960 ecm->port.func.disable = ecm_disable;
961 ecm->port.func.free_func = ecm_free;
963 return &ecm->port.func;
966 DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
967 MODULE_LICENSE("GPL");
968 MODULE_AUTHOR("David Brownell");