GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / usb / gadget / f_ecm.c
blobc9cbd9eecef856d9b7b4acc04ba30a09117f1249
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.
49 struct ecm_ep_descs {
50 struct usb_endpoint_descriptor *in;
51 struct usb_endpoint_descriptor *out;
52 struct usb_endpoint_descriptor *notify;
55 enum ecm_notify_state {
56 ECM_NOTIFY_NONE, /* don't notify */
57 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
58 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
61 struct f_ecm {
62 struct gether port;
63 u8 ctrl_id, data_id;
65 char ethaddr[14];
67 struct ecm_ep_descs fs;
68 struct ecm_ep_descs hs;
70 struct usb_ep *notify;
71 struct usb_endpoint_descriptor *notify_desc;
72 struct usb_request *notify_req;
73 u8 notify_state;
74 bool is_open;
78 static inline struct f_ecm *func_to_ecm(struct usb_function *f)
80 return container_of(f, struct f_ecm, port.func);
83 /* peak (theoretical) bulk transfer rate in bits-per-second */
84 static inline unsigned ecm_bitrate(struct usb_gadget *g)
86 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
87 return 13 * 512 * 8 * 1000 * 8;
88 else
89 return 19 * 64 * 1 * 1000 * 8;
92 /*-------------------------------------------------------------------------*/
95 * Include the status endpoint if we can, even though it's optional.
97 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
98 * packet, to simplify cancellation; and a big transfer interval, to
99 * waste less bandwidth.
101 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
102 * if they ignore the connect/disconnect notifications that real aether
103 * can provide. More advanced cdc configurations might want to support
104 * encapsulated commands (vendor-specific, using control-OUT).
107 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
108 #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
111 /* interface descriptor: */
113 static struct usb_interface_descriptor ecm_control_intf = {
114 .bLength = sizeof ecm_control_intf,
115 .bDescriptorType = USB_DT_INTERFACE,
117 /* .bInterfaceNumber = DYNAMIC */
118 /* status endpoint is optional; this could be patched later */
119 .bNumEndpoints = 1,
120 .bInterfaceClass = USB_CLASS_COMM,
121 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
122 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
123 /* .iInterface = DYNAMIC */
126 static struct usb_cdc_header_desc ecm_header_desc = {
127 .bLength = sizeof ecm_header_desc,
128 .bDescriptorType = USB_DT_CS_INTERFACE,
129 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
131 .bcdCDC = cpu_to_le16(0x0110),
134 static struct usb_cdc_union_desc ecm_union_desc = {
135 .bLength = sizeof(ecm_union_desc),
136 .bDescriptorType = USB_DT_CS_INTERFACE,
137 .bDescriptorSubType = USB_CDC_UNION_TYPE,
138 /* .bMasterInterface0 = DYNAMIC */
139 /* .bSlaveInterface0 = DYNAMIC */
142 static struct usb_cdc_ether_desc ecm_desc = {
143 .bLength = sizeof ecm_desc,
144 .bDescriptorType = USB_DT_CS_INTERFACE,
145 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
147 /* this descriptor actually adds value, surprise! */
148 /* .iMACAddress = DYNAMIC */
149 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
150 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
151 .wNumberMCFilters = cpu_to_le16(0),
152 .bNumberPowerFilters = 0,
155 /* the default data interface has no endpoints ... */
157 static struct usb_interface_descriptor ecm_data_nop_intf = {
158 .bLength = sizeof ecm_data_nop_intf,
159 .bDescriptorType = USB_DT_INTERFACE,
161 .bInterfaceNumber = 1,
162 .bAlternateSetting = 0,
163 .bNumEndpoints = 0,
164 .bInterfaceClass = USB_CLASS_CDC_DATA,
165 .bInterfaceSubClass = 0,
166 .bInterfaceProtocol = 0,
167 /* .iInterface = DYNAMIC */
170 /* ... but the "real" data interface has two bulk endpoints */
172 static struct usb_interface_descriptor ecm_data_intf = {
173 .bLength = sizeof ecm_data_intf,
174 .bDescriptorType = USB_DT_INTERFACE,
176 .bInterfaceNumber = 1,
177 .bAlternateSetting = 1,
178 .bNumEndpoints = 2,
179 .bInterfaceClass = USB_CLASS_CDC_DATA,
180 .bInterfaceSubClass = 0,
181 .bInterfaceProtocol = 0,
182 /* .iInterface = DYNAMIC */
185 /* full speed support: */
187 static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
188 .bLength = USB_DT_ENDPOINT_SIZE,
189 .bDescriptorType = USB_DT_ENDPOINT,
191 .bEndpointAddress = USB_DIR_IN,
192 .bmAttributes = USB_ENDPOINT_XFER_INT,
193 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
194 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
197 static struct usb_endpoint_descriptor fs_ecm_in_desc = {
198 .bLength = USB_DT_ENDPOINT_SIZE,
199 .bDescriptorType = USB_DT_ENDPOINT,
201 .bEndpointAddress = USB_DIR_IN,
202 .bmAttributes = USB_ENDPOINT_XFER_BULK,
205 static struct usb_endpoint_descriptor fs_ecm_out_desc = {
206 .bLength = USB_DT_ENDPOINT_SIZE,
207 .bDescriptorType = USB_DT_ENDPOINT,
209 .bEndpointAddress = USB_DIR_OUT,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
213 static struct usb_descriptor_header *ecm_fs_function[] = {
214 /* CDC ECM control descriptors */
215 (struct usb_descriptor_header *) &ecm_control_intf,
216 (struct usb_descriptor_header *) &ecm_header_desc,
217 (struct usb_descriptor_header *) &ecm_union_desc,
218 (struct usb_descriptor_header *) &ecm_desc,
219 /* NOTE: status endpoint might need to be removed */
220 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
221 /* data interface, altsettings 0 and 1 */
222 (struct usb_descriptor_header *) &ecm_data_nop_intf,
223 (struct usb_descriptor_header *) &ecm_data_intf,
224 (struct usb_descriptor_header *) &fs_ecm_in_desc,
225 (struct usb_descriptor_header *) &fs_ecm_out_desc,
226 NULL,
229 /* high speed support: */
231 static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
232 .bLength = USB_DT_ENDPOINT_SIZE,
233 .bDescriptorType = USB_DT_ENDPOINT,
235 .bEndpointAddress = USB_DIR_IN,
236 .bmAttributes = USB_ENDPOINT_XFER_INT,
237 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
238 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
240 static struct usb_endpoint_descriptor hs_ecm_in_desc = {
241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
244 .bEndpointAddress = USB_DIR_IN,
245 .bmAttributes = USB_ENDPOINT_XFER_BULK,
246 .wMaxPacketSize = cpu_to_le16(512),
249 static struct usb_endpoint_descriptor hs_ecm_out_desc = {
250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
253 .bEndpointAddress = USB_DIR_OUT,
254 .bmAttributes = USB_ENDPOINT_XFER_BULK,
255 .wMaxPacketSize = cpu_to_le16(512),
258 static struct usb_descriptor_header *ecm_hs_function[] = {
259 /* CDC ECM control descriptors */
260 (struct usb_descriptor_header *) &ecm_control_intf,
261 (struct usb_descriptor_header *) &ecm_header_desc,
262 (struct usb_descriptor_header *) &ecm_union_desc,
263 (struct usb_descriptor_header *) &ecm_desc,
264 /* NOTE: status endpoint might need to be removed */
265 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
266 /* data interface, altsettings 0 and 1 */
267 (struct usb_descriptor_header *) &ecm_data_nop_intf,
268 (struct usb_descriptor_header *) &ecm_data_intf,
269 (struct usb_descriptor_header *) &hs_ecm_in_desc,
270 (struct usb_descriptor_header *) &hs_ecm_out_desc,
271 NULL,
274 /* string descriptors: */
276 static struct usb_string ecm_string_defs[] = {
277 [0].s = "CDC Ethernet Control Model (ECM)",
278 [1].s = NULL /* DYNAMIC */,
279 [2].s = "CDC Ethernet Data",
280 { } /* end of list */
283 static struct usb_gadget_strings ecm_string_table = {
284 .language = 0x0409, /* en-us */
285 .strings = ecm_string_defs,
288 static struct usb_gadget_strings *ecm_strings[] = {
289 &ecm_string_table,
290 NULL,
293 /*-------------------------------------------------------------------------*/
295 static void ecm_do_notify(struct f_ecm *ecm)
297 struct usb_request *req = ecm->notify_req;
298 struct usb_cdc_notification *event;
299 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
300 __le32 *data;
301 int status;
303 /* notification already in flight? */
304 if (!req)
305 return;
307 event = req->buf;
308 switch (ecm->notify_state) {
309 case ECM_NOTIFY_NONE:
310 return;
312 case ECM_NOTIFY_CONNECT:
313 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
314 if (ecm->is_open)
315 event->wValue = cpu_to_le16(1);
316 else
317 event->wValue = cpu_to_le16(0);
318 event->wLength = 0;
319 req->length = sizeof *event;
321 DBG(cdev, "notify connect %s\n",
322 ecm->is_open ? "true" : "false");
323 ecm->notify_state = ECM_NOTIFY_SPEED;
324 break;
326 case ECM_NOTIFY_SPEED:
327 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
328 event->wValue = cpu_to_le16(0);
329 event->wLength = cpu_to_le16(8);
330 req->length = ECM_STATUS_BYTECOUNT;
332 /* SPEED_CHANGE data is up/down speeds in bits/sec */
333 data = req->buf + sizeof *event;
334 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
335 data[1] = data[0];
337 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
338 ecm->notify_state = ECM_NOTIFY_NONE;
339 break;
341 event->bmRequestType = 0xA1;
342 event->wIndex = cpu_to_le16(ecm->ctrl_id);
344 ecm->notify_req = NULL;
345 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
346 if (status < 0) {
347 ecm->notify_req = req;
348 DBG(cdev, "notify --> %d\n", status);
352 static void ecm_notify(struct f_ecm *ecm)
354 /* NOTE on most versions of Linux, host side cdc-ethernet
355 * won't listen for notifications until its netdevice opens.
356 * The first notification then sits in the FIFO for a long
357 * time, and the second one is queued.
359 ecm->notify_state = ECM_NOTIFY_CONNECT;
360 ecm_do_notify(ecm);
363 static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
365 struct f_ecm *ecm = req->context;
366 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
367 struct usb_cdc_notification *event = req->buf;
369 switch (req->status) {
370 case 0:
371 /* no fault */
372 break;
373 case -ECONNRESET:
374 case -ESHUTDOWN:
375 ecm->notify_state = ECM_NOTIFY_NONE;
376 break;
377 default:
378 DBG(cdev, "event %02x --> %d\n",
379 event->bNotificationType, req->status);
380 break;
382 ecm->notify_req = req;
383 ecm_do_notify(ecm);
386 static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
388 struct f_ecm *ecm = func_to_ecm(f);
389 struct usb_composite_dev *cdev = f->config->cdev;
390 struct usb_request *req = cdev->req;
391 int value = -EOPNOTSUPP;
392 u16 w_index = le16_to_cpu(ctrl->wIndex);
393 u16 w_value = le16_to_cpu(ctrl->wValue);
394 u16 w_length = le16_to_cpu(ctrl->wLength);
396 /* composite driver infrastructure handles everything except
397 * CDC class messages; interface activation uses set_alt().
399 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
400 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
401 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
402 /* see 6.2.30: no data, wIndex = interface,
403 * wValue = packet filter bitmap
405 if (w_length != 0 || w_index != ecm->ctrl_id)
406 goto invalid;
407 DBG(cdev, "packet filter %02x\n", w_value);
408 /* REVISIT locking of cdc_filter. This assumes the UDC
409 * driver won't have a concurrent packet TX irq running on
410 * another CPU; or that if it does, this write is atomic...
412 ecm->port.cdc_filter = w_value;
413 value = 0;
414 break;
416 /* and optionally:
417 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
418 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
419 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
420 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
421 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
422 * case USB_CDC_GET_ETHERNET_STATISTIC:
425 default:
426 invalid:
427 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
428 ctrl->bRequestType, ctrl->bRequest,
429 w_value, w_index, w_length);
432 /* respond with data transfer or status phase? */
433 if (value >= 0) {
434 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
435 ctrl->bRequestType, ctrl->bRequest,
436 w_value, w_index, w_length);
437 req->zero = 0;
438 req->length = value;
439 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
440 if (value < 0)
441 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
442 ctrl->bRequestType, ctrl->bRequest,
443 value);
446 /* device either stalls (value < 0) or reports success */
447 return value;
451 static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
453 struct f_ecm *ecm = func_to_ecm(f);
454 struct usb_composite_dev *cdev = f->config->cdev;
456 /* Control interface has only altsetting 0 */
457 if (intf == ecm->ctrl_id) {
458 if (alt != 0)
459 goto fail;
461 if (ecm->notify->driver_data) {
462 VDBG(cdev, "reset ecm control %d\n", intf);
463 usb_ep_disable(ecm->notify);
464 } else {
465 VDBG(cdev, "init ecm ctrl %d\n", intf);
466 ecm->notify_desc = ep_choose(cdev->gadget,
467 ecm->hs.notify,
468 ecm->fs.notify);
470 usb_ep_enable(ecm->notify, ecm->notify_desc);
471 ecm->notify->driver_data = ecm;
473 /* Data interface has two altsettings, 0 and 1 */
474 } else if (intf == ecm->data_id) {
475 if (alt > 1)
476 goto fail;
478 if (ecm->port.in_ep->driver_data) {
479 DBG(cdev, "reset ecm\n");
480 gether_disconnect(&ecm->port);
483 if (!ecm->port.in) {
484 DBG(cdev, "init ecm\n");
485 ecm->port.in = ep_choose(cdev->gadget,
486 ecm->hs.in, ecm->fs.in);
487 ecm->port.out = ep_choose(cdev->gadget,
488 ecm->hs.out, ecm->fs.out);
491 /* CDC Ethernet only sends data in non-default altsettings.
492 * Changing altsettings resets filters, statistics, etc.
494 if (alt == 1) {
495 struct net_device *net;
497 /* Enable zlps by default for ECM conformance;
498 * override for musb_hdrc (avoids txdma ovhead).
500 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
502 ecm->port.cdc_filter = DEFAULT_FILTER;
503 DBG(cdev, "activate ecm\n");
504 net = gether_connect(&ecm->port);
505 if (IS_ERR(net))
506 return PTR_ERR(net);
509 /* NOTE this can be a minor disagreement with the ECM spec,
510 * which says speed notifications will "always" follow
511 * connection notifications. But we allow one connect to
512 * follow another (if the first is in flight), and instead
513 * just guarantee that a speed notification is always sent.
515 ecm_notify(ecm);
516 } else
517 goto fail;
519 return 0;
520 fail:
521 return -EINVAL;
524 /* Because the data interface supports multiple altsettings,
525 * this ECM function *MUST* implement a get_alt() method.
527 static int ecm_get_alt(struct usb_function *f, unsigned intf)
529 struct f_ecm *ecm = func_to_ecm(f);
531 if (intf == ecm->ctrl_id)
532 return 0;
533 return ecm->port.in_ep->driver_data ? 1 : 0;
536 static void ecm_disable(struct usb_function *f)
538 struct f_ecm *ecm = func_to_ecm(f);
539 struct usb_composite_dev *cdev = f->config->cdev;
541 DBG(cdev, "ecm deactivated\n");
543 if (ecm->port.in_ep->driver_data)
544 gether_disconnect(&ecm->port);
546 if (ecm->notify->driver_data) {
547 usb_ep_disable(ecm->notify);
548 ecm->notify->driver_data = NULL;
549 ecm->notify_desc = NULL;
553 /*-------------------------------------------------------------------------*/
556 * Callbacks let us notify the host about connect/disconnect when the
557 * net device is opened or closed.
559 * For testing, note that link states on this side include both opened
560 * and closed variants of:
562 * - disconnected/unconfigured
563 * - configured but inactive (data alt 0)
564 * - configured and active (data alt 1)
566 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
567 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
568 * imply the host is actually polling the notification endpoint, and
569 * likewise that "active" doesn't imply it's actually using the data
570 * endpoints for traffic.
573 static void ecm_open(struct gether *geth)
575 struct f_ecm *ecm = func_to_ecm(&geth->func);
577 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
579 ecm->is_open = true;
580 ecm_notify(ecm);
583 static void ecm_close(struct gether *geth)
585 struct f_ecm *ecm = func_to_ecm(&geth->func);
587 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
589 ecm->is_open = false;
590 ecm_notify(ecm);
593 /*-------------------------------------------------------------------------*/
595 /* ethernet function driver setup/binding */
597 static int
598 ecm_bind(struct usb_configuration *c, struct usb_function *f)
600 struct usb_composite_dev *cdev = c->cdev;
601 struct f_ecm *ecm = func_to_ecm(f);
602 int status;
603 struct usb_ep *ep;
605 /* allocate instance-specific interface IDs */
606 status = usb_interface_id(c, f);
607 if (status < 0)
608 goto fail;
609 ecm->ctrl_id = status;
611 ecm_control_intf.bInterfaceNumber = status;
612 ecm_union_desc.bMasterInterface0 = status;
614 status = usb_interface_id(c, f);
615 if (status < 0)
616 goto fail;
617 ecm->data_id = status;
619 ecm_data_nop_intf.bInterfaceNumber = status;
620 ecm_data_intf.bInterfaceNumber = status;
621 ecm_union_desc.bSlaveInterface0 = status;
623 status = -ENODEV;
625 /* allocate instance-specific endpoints */
626 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
627 if (!ep)
628 goto fail;
629 ecm->port.in_ep = ep;
630 ep->driver_data = cdev; /* claim */
632 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
633 if (!ep)
634 goto fail;
635 ecm->port.out_ep = ep;
636 ep->driver_data = cdev; /* claim */
638 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
639 * don't treat it that way. It's simpler, and some newer CDC
640 * profiles (wireless handsets) no longer treat it as optional.
642 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
643 if (!ep)
644 goto fail;
645 ecm->notify = ep;
646 ep->driver_data = cdev; /* claim */
648 status = -ENOMEM;
650 /* allocate notification request and buffer */
651 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
652 if (!ecm->notify_req)
653 goto fail;
654 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
655 if (!ecm->notify_req->buf)
656 goto fail;
657 ecm->notify_req->context = ecm;
658 ecm->notify_req->complete = ecm_notify_complete;
660 /* copy descriptors, and track endpoint copies */
661 f->descriptors = usb_copy_descriptors(ecm_fs_function);
662 if (!f->descriptors)
663 goto fail;
665 ecm->fs.in = usb_find_endpoint(ecm_fs_function,
666 f->descriptors, &fs_ecm_in_desc);
667 ecm->fs.out = usb_find_endpoint(ecm_fs_function,
668 f->descriptors, &fs_ecm_out_desc);
669 ecm->fs.notify = usb_find_endpoint(ecm_fs_function,
670 f->descriptors, &fs_ecm_notify_desc);
672 /* support all relevant hardware speeds... we expect that when
673 * hardware is dual speed, all bulk-capable endpoints work at
674 * both speeds
676 if (gadget_is_dualspeed(c->cdev->gadget)) {
677 hs_ecm_in_desc.bEndpointAddress =
678 fs_ecm_in_desc.bEndpointAddress;
679 hs_ecm_out_desc.bEndpointAddress =
680 fs_ecm_out_desc.bEndpointAddress;
681 hs_ecm_notify_desc.bEndpointAddress =
682 fs_ecm_notify_desc.bEndpointAddress;
684 /* copy descriptors, and track endpoint copies */
685 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
686 if (!f->hs_descriptors)
687 goto fail;
689 ecm->hs.in = usb_find_endpoint(ecm_hs_function,
690 f->hs_descriptors, &hs_ecm_in_desc);
691 ecm->hs.out = usb_find_endpoint(ecm_hs_function,
692 f->hs_descriptors, &hs_ecm_out_desc);
693 ecm->hs.notify = usb_find_endpoint(ecm_hs_function,
694 f->hs_descriptors, &hs_ecm_notify_desc);
697 /* NOTE: all that is done without knowing or caring about
698 * the network link ... which is unavailable to this code
699 * until we're activated via set_alt().
702 ecm->port.open = ecm_open;
703 ecm->port.close = ecm_close;
705 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
706 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
707 ecm->port.in_ep->name, ecm->port.out_ep->name,
708 ecm->notify->name);
709 return 0;
711 fail:
712 if (f->descriptors)
713 usb_free_descriptors(f->descriptors);
715 if (ecm->notify_req) {
716 kfree(ecm->notify_req->buf);
717 usb_ep_free_request(ecm->notify, ecm->notify_req);
720 /* we might as well release our claims on endpoints */
721 if (ecm->notify)
722 ecm->notify->driver_data = NULL;
723 if (ecm->port.out)
724 ecm->port.out_ep->driver_data = NULL;
725 if (ecm->port.in)
726 ecm->port.in_ep->driver_data = NULL;
728 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
730 return status;
733 static void
734 ecm_unbind(struct usb_configuration *c, struct usb_function *f)
736 struct f_ecm *ecm = func_to_ecm(f);
738 DBG(c->cdev, "ecm unbind\n");
740 if (gadget_is_dualspeed(c->cdev->gadget))
741 usb_free_descriptors(f->hs_descriptors);
742 usb_free_descriptors(f->descriptors);
744 kfree(ecm->notify_req->buf);
745 usb_ep_free_request(ecm->notify, ecm->notify_req);
747 ecm_string_defs[1].s = NULL;
748 kfree(ecm);
752 * ecm_bind_config - add CDC Ethernet network link to a configuration
753 * @c: the configuration to support the network link
754 * @ethaddr: a buffer in which the ethernet address of the host side
755 * side of the link was recorded
756 * Context: single threaded during gadget setup
758 * Returns zero on success, else negative errno.
760 * Caller must have called @gether_setup(). Caller is also responsible
761 * for calling @gether_cleanup() before module unload.
764 ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
766 struct f_ecm *ecm;
767 int status;
769 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
770 return -EINVAL;
772 /* maybe allocate device-global string IDs */
773 if (ecm_string_defs[0].id == 0) {
775 /* control interface label */
776 status = usb_string_id(c->cdev);
777 if (status < 0)
778 return status;
779 ecm_string_defs[0].id = status;
780 ecm_control_intf.iInterface = status;
782 /* data interface label */
783 status = usb_string_id(c->cdev);
784 if (status < 0)
785 return status;
786 ecm_string_defs[2].id = status;
787 ecm_data_intf.iInterface = status;
789 /* MAC address */
790 status = usb_string_id(c->cdev);
791 if (status < 0)
792 return status;
793 ecm_string_defs[1].id = status;
794 ecm_desc.iMACAddress = status;
797 /* allocate and initialize one new instance */
798 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
799 if (!ecm)
800 return -ENOMEM;
802 /* export host's Ethernet address in CDC format */
803 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
804 "%02X%02X%02X%02X%02X%02X",
805 ethaddr[0], ethaddr[1], ethaddr[2],
806 ethaddr[3], ethaddr[4], ethaddr[5]);
807 ecm_string_defs[1].s = ecm->ethaddr;
809 ecm->port.cdc_filter = DEFAULT_FILTER;
811 ecm->port.func.name = "cdc_ethernet";
812 ecm->port.func.strings = ecm_strings;
813 /* descriptors are per-instance copies */
814 ecm->port.func.bind = ecm_bind;
815 ecm->port.func.unbind = ecm_unbind;
816 ecm->port.func.set_alt = ecm_set_alt;
817 ecm->port.func.get_alt = ecm_get_alt;
818 ecm->port.func.setup = ecm_setup;
819 ecm->port.func.disable = ecm_disable;
821 status = usb_add_function(c, &ecm->port.func);
822 if (status) {
823 ecm_string_defs[1].s = NULL;
824 kfree(ecm);
826 return status;