usb: gadget: r8a66597-udc: fix cannot connect after rmmod gadget driver
[linux/fpc-iii.git] / drivers / usb / gadget / f_ecm.c
blobddedbc83bc375ac12710e4eecd27406602e8482d
1 /*
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/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/device.h>
27 #include <linux/etherdevice.h>
29 #include "u_ether.h"
33 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
34 * Ethernet link. The data transfer model is simple (packets sent and
35 * received over bulk endpoints using normal short packet termination),
36 * and the control model exposes various data and optional notifications.
38 * ECM is well standardized and (except for Microsoft) supported by most
39 * operating systems with USB host support. It's the preferred interop
40 * solution for Ethernet over USB, at least for firmware based solutions.
41 * (Hardware solutions tend to be more minimalist.) A newer and simpler
42 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
44 * Note that ECM requires the use of "alternate settings" for its data
45 * interface. This means that the set_alt() method has real work to do,
46 * and also means that a get_alt() method is required.
50 enum ecm_notify_state {
51 ECM_NOTIFY_NONE, /* don't notify */
52 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
53 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
56 struct f_ecm {
57 struct gether port;
58 u8 ctrl_id, data_id;
60 char ethaddr[14];
62 struct usb_ep *notify;
63 struct usb_request *notify_req;
64 u8 notify_state;
65 bool is_open;
67 /* FIXME is_open needs some irq-ish locking
68 * ... possibly the same as port.ioport
72 static inline struct f_ecm *func_to_ecm(struct usb_function *f)
74 return container_of(f, struct f_ecm, port.func);
77 /* peak (theoretical) bulk transfer rate in bits-per-second */
78 static inline unsigned ecm_bitrate(struct usb_gadget *g)
80 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
81 return 13 * 512 * 8 * 1000 * 8;
82 else
83 return 19 * 64 * 1 * 1000 * 8;
86 /*-------------------------------------------------------------------------*/
89 * Include the status endpoint if we can, even though it's optional.
91 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
92 * packet, to simplify cancellation; and a big transfer interval, to
93 * waste less bandwidth.
95 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
96 * if they ignore the connect/disconnect notifications that real aether
97 * can provide. More advanced cdc configurations might want to support
98 * encapsulated commands (vendor-specific, using control-OUT).
101 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
102 #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
105 /* interface descriptor: */
107 static struct usb_interface_descriptor ecm_control_intf = {
108 .bLength = sizeof ecm_control_intf,
109 .bDescriptorType = USB_DT_INTERFACE,
111 /* .bInterfaceNumber = DYNAMIC */
112 /* status endpoint is optional; this could be patched later */
113 .bNumEndpoints = 1,
114 .bInterfaceClass = USB_CLASS_COMM,
115 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
116 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
117 /* .iInterface = DYNAMIC */
120 static struct usb_cdc_header_desc ecm_header_desc = {
121 .bLength = sizeof ecm_header_desc,
122 .bDescriptorType = USB_DT_CS_INTERFACE,
123 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
125 .bcdCDC = cpu_to_le16(0x0110),
128 static struct usb_cdc_union_desc ecm_union_desc = {
129 .bLength = sizeof(ecm_union_desc),
130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubType = USB_CDC_UNION_TYPE,
132 /* .bMasterInterface0 = DYNAMIC */
133 /* .bSlaveInterface0 = DYNAMIC */
136 static struct usb_cdc_ether_desc ecm_desc = {
137 .bLength = sizeof ecm_desc,
138 .bDescriptorType = USB_DT_CS_INTERFACE,
139 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
141 /* this descriptor actually adds value, surprise! */
142 /* .iMACAddress = DYNAMIC */
143 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
144 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
145 .wNumberMCFilters = cpu_to_le16(0),
146 .bNumberPowerFilters = 0,
149 /* the default data interface has no endpoints ... */
151 static struct usb_interface_descriptor ecm_data_nop_intf = {
152 .bLength = sizeof ecm_data_nop_intf,
153 .bDescriptorType = USB_DT_INTERFACE,
155 .bInterfaceNumber = 1,
156 .bAlternateSetting = 0,
157 .bNumEndpoints = 0,
158 .bInterfaceClass = USB_CLASS_CDC_DATA,
159 .bInterfaceSubClass = 0,
160 .bInterfaceProtocol = 0,
161 /* .iInterface = DYNAMIC */
164 /* ... but the "real" data interface has two bulk endpoints */
166 static struct usb_interface_descriptor ecm_data_intf = {
167 .bLength = sizeof ecm_data_intf,
168 .bDescriptorType = USB_DT_INTERFACE,
170 .bInterfaceNumber = 1,
171 .bAlternateSetting = 1,
172 .bNumEndpoints = 2,
173 .bInterfaceClass = USB_CLASS_CDC_DATA,
174 .bInterfaceSubClass = 0,
175 .bInterfaceProtocol = 0,
176 /* .iInterface = DYNAMIC */
179 /* full speed support: */
181 static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
182 .bLength = USB_DT_ENDPOINT_SIZE,
183 .bDescriptorType = USB_DT_ENDPOINT,
185 .bEndpointAddress = USB_DIR_IN,
186 .bmAttributes = USB_ENDPOINT_XFER_INT,
187 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
188 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
191 static struct usb_endpoint_descriptor fs_ecm_in_desc = {
192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
195 .bEndpointAddress = USB_DIR_IN,
196 .bmAttributes = USB_ENDPOINT_XFER_BULK,
199 static struct usb_endpoint_descriptor fs_ecm_out_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
203 .bEndpointAddress = USB_DIR_OUT,
204 .bmAttributes = USB_ENDPOINT_XFER_BULK,
207 static struct usb_descriptor_header *ecm_fs_function[] = {
208 /* CDC ECM control descriptors */
209 (struct usb_descriptor_header *) &ecm_control_intf,
210 (struct usb_descriptor_header *) &ecm_header_desc,
211 (struct usb_descriptor_header *) &ecm_union_desc,
212 (struct usb_descriptor_header *) &ecm_desc,
213 /* NOTE: status endpoint might need to be removed */
214 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
215 /* data interface, altsettings 0 and 1 */
216 (struct usb_descriptor_header *) &ecm_data_nop_intf,
217 (struct usb_descriptor_header *) &ecm_data_intf,
218 (struct usb_descriptor_header *) &fs_ecm_in_desc,
219 (struct usb_descriptor_header *) &fs_ecm_out_desc,
220 NULL,
223 /* high speed support: */
225 static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
226 .bLength = USB_DT_ENDPOINT_SIZE,
227 .bDescriptorType = USB_DT_ENDPOINT,
229 .bEndpointAddress = USB_DIR_IN,
230 .bmAttributes = USB_ENDPOINT_XFER_INT,
231 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
232 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
234 static struct usb_endpoint_descriptor hs_ecm_in_desc = {
235 .bLength = USB_DT_ENDPOINT_SIZE,
236 .bDescriptorType = USB_DT_ENDPOINT,
238 .bEndpointAddress = USB_DIR_IN,
239 .bmAttributes = USB_ENDPOINT_XFER_BULK,
240 .wMaxPacketSize = cpu_to_le16(512),
243 static struct usb_endpoint_descriptor hs_ecm_out_desc = {
244 .bLength = USB_DT_ENDPOINT_SIZE,
245 .bDescriptorType = USB_DT_ENDPOINT,
247 .bEndpointAddress = USB_DIR_OUT,
248 .bmAttributes = USB_ENDPOINT_XFER_BULK,
249 .wMaxPacketSize = cpu_to_le16(512),
252 static struct usb_descriptor_header *ecm_hs_function[] = {
253 /* CDC ECM control descriptors */
254 (struct usb_descriptor_header *) &ecm_control_intf,
255 (struct usb_descriptor_header *) &ecm_header_desc,
256 (struct usb_descriptor_header *) &ecm_union_desc,
257 (struct usb_descriptor_header *) &ecm_desc,
258 /* NOTE: status endpoint might need to be removed */
259 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
260 /* data interface, altsettings 0 and 1 */
261 (struct usb_descriptor_header *) &ecm_data_nop_intf,
262 (struct usb_descriptor_header *) &ecm_data_intf,
263 (struct usb_descriptor_header *) &hs_ecm_in_desc,
264 (struct usb_descriptor_header *) &hs_ecm_out_desc,
265 NULL,
268 /* string descriptors: */
270 static struct usb_string ecm_string_defs[] = {
271 [0].s = "CDC Ethernet Control Model (ECM)",
272 [1].s = NULL /* DYNAMIC */,
273 [2].s = "CDC Ethernet Data",
274 { } /* end of list */
277 static struct usb_gadget_strings ecm_string_table = {
278 .language = 0x0409, /* en-us */
279 .strings = ecm_string_defs,
282 static struct usb_gadget_strings *ecm_strings[] = {
283 &ecm_string_table,
284 NULL,
287 /*-------------------------------------------------------------------------*/
289 static void ecm_do_notify(struct f_ecm *ecm)
291 struct usb_request *req = ecm->notify_req;
292 struct usb_cdc_notification *event;
293 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
294 __le32 *data;
295 int status;
297 /* notification already in flight? */
298 if (!req)
299 return;
301 event = req->buf;
302 switch (ecm->notify_state) {
303 case ECM_NOTIFY_NONE:
304 return;
306 case ECM_NOTIFY_CONNECT:
307 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
308 if (ecm->is_open)
309 event->wValue = cpu_to_le16(1);
310 else
311 event->wValue = cpu_to_le16(0);
312 event->wLength = 0;
313 req->length = sizeof *event;
315 DBG(cdev, "notify connect %s\n",
316 ecm->is_open ? "true" : "false");
317 ecm->notify_state = ECM_NOTIFY_SPEED;
318 break;
320 case ECM_NOTIFY_SPEED:
321 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
322 event->wValue = cpu_to_le16(0);
323 event->wLength = cpu_to_le16(8);
324 req->length = ECM_STATUS_BYTECOUNT;
326 /* SPEED_CHANGE data is up/down speeds in bits/sec */
327 data = req->buf + sizeof *event;
328 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
329 data[1] = data[0];
331 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
332 ecm->notify_state = ECM_NOTIFY_NONE;
333 break;
335 event->bmRequestType = 0xA1;
336 event->wIndex = cpu_to_le16(ecm->ctrl_id);
338 ecm->notify_req = NULL;
339 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
340 if (status < 0) {
341 ecm->notify_req = req;
342 DBG(cdev, "notify --> %d\n", status);
346 static void ecm_notify(struct f_ecm *ecm)
348 /* NOTE on most versions of Linux, host side cdc-ethernet
349 * won't listen for notifications until its netdevice opens.
350 * The first notification then sits in the FIFO for a long
351 * time, and the second one is queued.
353 ecm->notify_state = ECM_NOTIFY_CONNECT;
354 ecm_do_notify(ecm);
357 static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
359 struct f_ecm *ecm = req->context;
360 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
361 struct usb_cdc_notification *event = req->buf;
363 switch (req->status) {
364 case 0:
365 /* no fault */
366 break;
367 case -ECONNRESET:
368 case -ESHUTDOWN:
369 ecm->notify_state = ECM_NOTIFY_NONE;
370 break;
371 default:
372 DBG(cdev, "event %02x --> %d\n",
373 event->bNotificationType, req->status);
374 break;
376 ecm->notify_req = req;
377 ecm_do_notify(ecm);
380 static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
382 struct f_ecm *ecm = func_to_ecm(f);
383 struct usb_composite_dev *cdev = f->config->cdev;
384 struct usb_request *req = cdev->req;
385 int value = -EOPNOTSUPP;
386 u16 w_index = le16_to_cpu(ctrl->wIndex);
387 u16 w_value = le16_to_cpu(ctrl->wValue);
388 u16 w_length = le16_to_cpu(ctrl->wLength);
390 /* composite driver infrastructure handles everything except
391 * CDC class messages; interface activation uses set_alt().
393 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
394 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
395 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
396 /* see 6.2.30: no data, wIndex = interface,
397 * wValue = packet filter bitmap
399 if (w_length != 0 || w_index != ecm->ctrl_id)
400 goto invalid;
401 DBG(cdev, "packet filter %02x\n", w_value);
402 /* REVISIT locking of cdc_filter. This assumes the UDC
403 * driver won't have a concurrent packet TX irq running on
404 * another CPU; or that if it does, this write is atomic...
406 ecm->port.cdc_filter = w_value;
407 value = 0;
408 break;
410 /* and optionally:
411 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
412 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
413 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
414 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
415 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
416 * case USB_CDC_GET_ETHERNET_STATISTIC:
419 default:
420 invalid:
421 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
422 ctrl->bRequestType, ctrl->bRequest,
423 w_value, w_index, w_length);
426 /* respond with data transfer or status phase? */
427 if (value >= 0) {
428 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
429 ctrl->bRequestType, ctrl->bRequest,
430 w_value, w_index, w_length);
431 req->zero = 0;
432 req->length = value;
433 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
434 if (value < 0)
435 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
436 ctrl->bRequestType, ctrl->bRequest,
437 value);
440 /* device either stalls (value < 0) or reports success */
441 return value;
445 static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
447 struct f_ecm *ecm = func_to_ecm(f);
448 struct usb_composite_dev *cdev = f->config->cdev;
450 /* Control interface has only altsetting 0 */
451 if (intf == ecm->ctrl_id) {
452 if (alt != 0)
453 goto fail;
455 if (ecm->notify->driver_data) {
456 VDBG(cdev, "reset ecm control %d\n", intf);
457 usb_ep_disable(ecm->notify);
459 if (!(ecm->notify->desc)) {
460 VDBG(cdev, "init ecm ctrl %d\n", intf);
461 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
462 goto fail;
464 usb_ep_enable(ecm->notify);
465 ecm->notify->driver_data = ecm;
467 /* Data interface has two altsettings, 0 and 1 */
468 } else if (intf == ecm->data_id) {
469 if (alt > 1)
470 goto fail;
472 if (ecm->port.in_ep->driver_data) {
473 DBG(cdev, "reset ecm\n");
474 gether_disconnect(&ecm->port);
477 if (!ecm->port.in_ep->desc ||
478 !ecm->port.out_ep->desc) {
479 DBG(cdev, "init ecm\n");
480 if (config_ep_by_speed(cdev->gadget, f,
481 ecm->port.in_ep) ||
482 config_ep_by_speed(cdev->gadget, f,
483 ecm->port.out_ep)) {
484 ecm->port.in_ep->desc = NULL;
485 ecm->port.out_ep->desc = NULL;
486 goto fail;
490 /* CDC Ethernet only sends data in non-default altsettings.
491 * Changing altsettings resets filters, statistics, etc.
493 if (alt == 1) {
494 struct net_device *net;
496 /* Enable zlps by default for ECM conformance;
497 * override for musb_hdrc (avoids txdma ovhead).
499 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
501 ecm->port.cdc_filter = DEFAULT_FILTER;
502 DBG(cdev, "activate ecm\n");
503 net = gether_connect(&ecm->port);
504 if (IS_ERR(net))
505 return PTR_ERR(net);
508 /* NOTE this can be a minor disagreement with the ECM spec,
509 * which says speed notifications will "always" follow
510 * connection notifications. But we allow one connect to
511 * follow another (if the first is in flight), and instead
512 * just guarantee that a speed notification is always sent.
514 ecm_notify(ecm);
515 } else
516 goto fail;
518 return 0;
519 fail:
520 return -EINVAL;
523 /* Because the data interface supports multiple altsettings,
524 * this ECM function *MUST* implement a get_alt() method.
526 static int ecm_get_alt(struct usb_function *f, unsigned intf)
528 struct f_ecm *ecm = func_to_ecm(f);
530 if (intf == ecm->ctrl_id)
531 return 0;
532 return ecm->port.in_ep->driver_data ? 1 : 0;
535 static void ecm_disable(struct usb_function *f)
537 struct f_ecm *ecm = func_to_ecm(f);
538 struct usb_composite_dev *cdev = f->config->cdev;
540 DBG(cdev, "ecm deactivated\n");
542 if (ecm->port.in_ep->driver_data)
543 gether_disconnect(&ecm->port);
545 if (ecm->notify->driver_data) {
546 usb_ep_disable(ecm->notify);
547 ecm->notify->driver_data = NULL;
548 ecm->notify->desc = NULL;
552 /*-------------------------------------------------------------------------*/
555 * Callbacks let us notify the host about connect/disconnect when the
556 * net device is opened or closed.
558 * For testing, note that link states on this side include both opened
559 * and closed variants of:
561 * - disconnected/unconfigured
562 * - configured but inactive (data alt 0)
563 * - configured and active (data alt 1)
565 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
566 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
567 * imply the host is actually polling the notification endpoint, and
568 * likewise that "active" doesn't imply it's actually using the data
569 * endpoints for traffic.
572 static void ecm_open(struct gether *geth)
574 struct f_ecm *ecm = func_to_ecm(&geth->func);
576 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
578 ecm->is_open = true;
579 ecm_notify(ecm);
582 static void ecm_close(struct gether *geth)
584 struct f_ecm *ecm = func_to_ecm(&geth->func);
586 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
588 ecm->is_open = false;
589 ecm_notify(ecm);
592 /*-------------------------------------------------------------------------*/
594 /* ethernet function driver setup/binding */
596 static int
597 ecm_bind(struct usb_configuration *c, struct usb_function *f)
599 struct usb_composite_dev *cdev = c->cdev;
600 struct f_ecm *ecm = func_to_ecm(f);
601 int status;
602 struct usb_ep *ep;
604 /* allocate instance-specific interface IDs */
605 status = usb_interface_id(c, f);
606 if (status < 0)
607 goto fail;
608 ecm->ctrl_id = status;
610 ecm_control_intf.bInterfaceNumber = status;
611 ecm_union_desc.bMasterInterface0 = status;
613 status = usb_interface_id(c, f);
614 if (status < 0)
615 goto fail;
616 ecm->data_id = status;
618 ecm_data_nop_intf.bInterfaceNumber = status;
619 ecm_data_intf.bInterfaceNumber = status;
620 ecm_union_desc.bSlaveInterface0 = status;
622 status = -ENODEV;
624 /* allocate instance-specific endpoints */
625 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
626 if (!ep)
627 goto fail;
628 ecm->port.in_ep = ep;
629 ep->driver_data = cdev; /* claim */
631 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
632 if (!ep)
633 goto fail;
634 ecm->port.out_ep = ep;
635 ep->driver_data = cdev; /* claim */
637 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
638 * don't treat it that way. It's simpler, and some newer CDC
639 * profiles (wireless handsets) no longer treat it as optional.
641 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
642 if (!ep)
643 goto fail;
644 ecm->notify = ep;
645 ep->driver_data = cdev; /* claim */
647 status = -ENOMEM;
649 /* allocate notification request and buffer */
650 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
651 if (!ecm->notify_req)
652 goto fail;
653 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
654 if (!ecm->notify_req->buf)
655 goto fail;
656 ecm->notify_req->context = ecm;
657 ecm->notify_req->complete = ecm_notify_complete;
659 /* copy descriptors, and track endpoint copies */
660 f->descriptors = usb_copy_descriptors(ecm_fs_function);
661 if (!f->descriptors)
662 goto fail;
664 /* support all relevant hardware speeds... we expect that when
665 * hardware is dual speed, all bulk-capable endpoints work at
666 * both speeds
668 if (gadget_is_dualspeed(c->cdev->gadget)) {
669 hs_ecm_in_desc.bEndpointAddress =
670 fs_ecm_in_desc.bEndpointAddress;
671 hs_ecm_out_desc.bEndpointAddress =
672 fs_ecm_out_desc.bEndpointAddress;
673 hs_ecm_notify_desc.bEndpointAddress =
674 fs_ecm_notify_desc.bEndpointAddress;
676 /* copy descriptors, and track endpoint copies */
677 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
678 if (!f->hs_descriptors)
679 goto fail;
682 /* NOTE: all that is done without knowing or caring about
683 * the network link ... which is unavailable to this code
684 * until we're activated via set_alt().
687 ecm->port.open = ecm_open;
688 ecm->port.close = ecm_close;
690 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
691 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
692 ecm->port.in_ep->name, ecm->port.out_ep->name,
693 ecm->notify->name);
694 return 0;
696 fail:
697 if (f->descriptors)
698 usb_free_descriptors(f->descriptors);
700 if (ecm->notify_req) {
701 kfree(ecm->notify_req->buf);
702 usb_ep_free_request(ecm->notify, ecm->notify_req);
705 /* we might as well release our claims on endpoints */
706 if (ecm->notify)
707 ecm->notify->driver_data = NULL;
708 if (ecm->port.out_ep->desc)
709 ecm->port.out_ep->driver_data = NULL;
710 if (ecm->port.in_ep->desc)
711 ecm->port.in_ep->driver_data = NULL;
713 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
715 return status;
718 static void
719 ecm_unbind(struct usb_configuration *c, struct usb_function *f)
721 struct f_ecm *ecm = func_to_ecm(f);
723 DBG(c->cdev, "ecm unbind\n");
725 if (gadget_is_dualspeed(c->cdev->gadget))
726 usb_free_descriptors(f->hs_descriptors);
727 usb_free_descriptors(f->descriptors);
729 kfree(ecm->notify_req->buf);
730 usb_ep_free_request(ecm->notify, ecm->notify_req);
732 ecm_string_defs[1].s = NULL;
733 kfree(ecm);
737 * ecm_bind_config - add CDC Ethernet network link to a configuration
738 * @c: the configuration to support the network link
739 * @ethaddr: a buffer in which the ethernet address of the host side
740 * side of the link was recorded
741 * Context: single threaded during gadget setup
743 * Returns zero on success, else negative errno.
745 * Caller must have called @gether_setup(). Caller is also responsible
746 * for calling @gether_cleanup() before module unload.
749 ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
751 struct f_ecm *ecm;
752 int status;
754 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
755 return -EINVAL;
757 /* maybe allocate device-global string IDs */
758 if (ecm_string_defs[0].id == 0) {
760 /* control interface label */
761 status = usb_string_id(c->cdev);
762 if (status < 0)
763 return status;
764 ecm_string_defs[0].id = status;
765 ecm_control_intf.iInterface = status;
767 /* data interface label */
768 status = usb_string_id(c->cdev);
769 if (status < 0)
770 return status;
771 ecm_string_defs[2].id = status;
772 ecm_data_intf.iInterface = status;
774 /* MAC address */
775 status = usb_string_id(c->cdev);
776 if (status < 0)
777 return status;
778 ecm_string_defs[1].id = status;
779 ecm_desc.iMACAddress = status;
782 /* allocate and initialize one new instance */
783 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
784 if (!ecm)
785 return -ENOMEM;
787 /* export host's Ethernet address in CDC format */
788 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
789 "%02X%02X%02X%02X%02X%02X",
790 ethaddr[0], ethaddr[1], ethaddr[2],
791 ethaddr[3], ethaddr[4], ethaddr[5]);
792 ecm_string_defs[1].s = ecm->ethaddr;
794 ecm->port.cdc_filter = DEFAULT_FILTER;
796 ecm->port.func.name = "cdc_ethernet";
797 ecm->port.func.strings = ecm_strings;
798 /* descriptors are per-instance copies */
799 ecm->port.func.bind = ecm_bind;
800 ecm->port.func.unbind = ecm_unbind;
801 ecm->port.func.set_alt = ecm_set_alt;
802 ecm->port.func.get_alt = ecm_get_alt;
803 ecm->port.func.setup = ecm_setup;
804 ecm->port.func.disable = ecm_disable;
806 status = usb_add_function(c, &ecm->port.func);
807 if (status) {
808 ecm_string_defs[1].s = NULL;
809 kfree(ecm);
811 return status;