i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / usb / gadget / f_ecm.c
blob11c07cb7d3372b967725dbaf1c3d236193603433
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.
13 /* #define VERBOSE_DEBUG */
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/etherdevice.h>
20 #include "u_ether.h"
24 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
25 * Ethernet link. The data transfer model is simple (packets sent and
26 * received over bulk endpoints using normal short packet termination),
27 * and the control model exposes various data and optional notifications.
29 * ECM is well standardized and (except for Microsoft) supported by most
30 * operating systems with USB host support. It's the preferred interop
31 * solution for Ethernet over USB, at least for firmware based solutions.
32 * (Hardware solutions tend to be more minimalist.) A newer and simpler
33 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
35 * Note that ECM requires the use of "alternate settings" for its data
36 * interface. This means that the set_alt() method has real work to do,
37 * and also means that a get_alt() method is required.
41 enum ecm_notify_state {
42 ECM_NOTIFY_NONE, /* don't notify */
43 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
44 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
47 struct f_ecm {
48 struct gether port;
49 u8 ctrl_id, data_id;
51 char ethaddr[14];
53 struct usb_ep *notify;
54 struct usb_request *notify_req;
55 u8 notify_state;
56 bool is_open;
58 /* FIXME is_open needs some irq-ish locking
59 * ... possibly the same as port.ioport
63 static inline struct f_ecm *func_to_ecm(struct usb_function *f)
65 return container_of(f, struct f_ecm, port.func);
68 /* peak (theoretical) bulk transfer rate in bits-per-second */
69 static inline unsigned ecm_bitrate(struct usb_gadget *g)
71 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
72 return 13 * 1024 * 8 * 1000 * 8;
73 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
74 return 13 * 512 * 8 * 1000 * 8;
75 else
76 return 19 * 64 * 1 * 1000 * 8;
79 /*-------------------------------------------------------------------------*/
82 * Include the status endpoint if we can, even though it's optional.
84 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
85 * packet, to simplify cancellation; and a big transfer interval, to
86 * waste less bandwidth.
88 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
89 * if they ignore the connect/disconnect notifications that real aether
90 * can provide. More advanced cdc configurations might want to support
91 * encapsulated commands (vendor-specific, using control-OUT).
94 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
95 #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
98 /* interface descriptor: */
100 static struct usb_interface_descriptor ecm_control_intf = {
101 .bLength = sizeof ecm_control_intf,
102 .bDescriptorType = USB_DT_INTERFACE,
104 /* .bInterfaceNumber = DYNAMIC */
105 /* status endpoint is optional; this could be patched later */
106 .bNumEndpoints = 1,
107 .bInterfaceClass = USB_CLASS_COMM,
108 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
109 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
110 /* .iInterface = DYNAMIC */
113 static struct usb_cdc_header_desc ecm_header_desc = {
114 .bLength = sizeof ecm_header_desc,
115 .bDescriptorType = USB_DT_CS_INTERFACE,
116 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
118 .bcdCDC = cpu_to_le16(0x0110),
121 static struct usb_cdc_union_desc ecm_union_desc = {
122 .bLength = sizeof(ecm_union_desc),
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_UNION_TYPE,
125 /* .bMasterInterface0 = DYNAMIC */
126 /* .bSlaveInterface0 = DYNAMIC */
129 static struct usb_cdc_ether_desc ecm_desc = {
130 .bLength = sizeof ecm_desc,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
134 /* this descriptor actually adds value, surprise! */
135 /* .iMACAddress = DYNAMIC */
136 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
137 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
138 .wNumberMCFilters = cpu_to_le16(0),
139 .bNumberPowerFilters = 0,
142 /* the default data interface has no endpoints ... */
144 static struct usb_interface_descriptor ecm_data_nop_intf = {
145 .bLength = sizeof ecm_data_nop_intf,
146 .bDescriptorType = USB_DT_INTERFACE,
148 .bInterfaceNumber = 1,
149 .bAlternateSetting = 0,
150 .bNumEndpoints = 0,
151 .bInterfaceClass = USB_CLASS_CDC_DATA,
152 .bInterfaceSubClass = 0,
153 .bInterfaceProtocol = 0,
154 /* .iInterface = DYNAMIC */
157 /* ... but the "real" data interface has two bulk endpoints */
159 static struct usb_interface_descriptor ecm_data_intf = {
160 .bLength = sizeof ecm_data_intf,
161 .bDescriptorType = USB_DT_INTERFACE,
163 .bInterfaceNumber = 1,
164 .bAlternateSetting = 1,
165 .bNumEndpoints = 2,
166 .bInterfaceClass = USB_CLASS_CDC_DATA,
167 .bInterfaceSubClass = 0,
168 .bInterfaceProtocol = 0,
169 /* .iInterface = DYNAMIC */
172 /* full speed support: */
174 static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
178 .bEndpointAddress = USB_DIR_IN,
179 .bmAttributes = USB_ENDPOINT_XFER_INT,
180 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
181 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
184 static struct usb_endpoint_descriptor fs_ecm_in_desc = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_BULK,
192 static struct usb_endpoint_descriptor fs_ecm_out_desc = {
193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
196 .bEndpointAddress = USB_DIR_OUT,
197 .bmAttributes = USB_ENDPOINT_XFER_BULK,
200 static struct usb_descriptor_header *ecm_fs_function[] = {
201 /* CDC ECM control descriptors */
202 (struct usb_descriptor_header *) &ecm_control_intf,
203 (struct usb_descriptor_header *) &ecm_header_desc,
204 (struct usb_descriptor_header *) &ecm_union_desc,
205 (struct usb_descriptor_header *) &ecm_desc,
207 /* NOTE: status endpoint might need to be removed */
208 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
210 /* data interface, altsettings 0 and 1 */
211 (struct usb_descriptor_header *) &ecm_data_nop_intf,
212 (struct usb_descriptor_header *) &ecm_data_intf,
213 (struct usb_descriptor_header *) &fs_ecm_in_desc,
214 (struct usb_descriptor_header *) &fs_ecm_out_desc,
215 NULL,
218 /* high speed support: */
220 static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
221 .bLength = USB_DT_ENDPOINT_SIZE,
222 .bDescriptorType = USB_DT_ENDPOINT,
224 .bEndpointAddress = USB_DIR_IN,
225 .bmAttributes = USB_ENDPOINT_XFER_INT,
226 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
227 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
230 static struct usb_endpoint_descriptor hs_ecm_in_desc = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
234 .bEndpointAddress = USB_DIR_IN,
235 .bmAttributes = USB_ENDPOINT_XFER_BULK,
236 .wMaxPacketSize = cpu_to_le16(512),
239 static struct usb_endpoint_descriptor hs_ecm_out_desc = {
240 .bLength = USB_DT_ENDPOINT_SIZE,
241 .bDescriptorType = USB_DT_ENDPOINT,
243 .bEndpointAddress = USB_DIR_OUT,
244 .bmAttributes = USB_ENDPOINT_XFER_BULK,
245 .wMaxPacketSize = cpu_to_le16(512),
248 static struct usb_descriptor_header *ecm_hs_function[] = {
249 /* CDC ECM control descriptors */
250 (struct usb_descriptor_header *) &ecm_control_intf,
251 (struct usb_descriptor_header *) &ecm_header_desc,
252 (struct usb_descriptor_header *) &ecm_union_desc,
253 (struct usb_descriptor_header *) &ecm_desc,
255 /* NOTE: status endpoint might need to be removed */
256 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
258 /* data interface, altsettings 0 and 1 */
259 (struct usb_descriptor_header *) &ecm_data_nop_intf,
260 (struct usb_descriptor_header *) &ecm_data_intf,
261 (struct usb_descriptor_header *) &hs_ecm_in_desc,
262 (struct usb_descriptor_header *) &hs_ecm_out_desc,
263 NULL,
266 /* super speed support: */
268 static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
269 .bLength = USB_DT_ENDPOINT_SIZE,
270 .bDescriptorType = USB_DT_ENDPOINT,
272 .bEndpointAddress = USB_DIR_IN,
273 .bmAttributes = USB_ENDPOINT_XFER_INT,
274 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
275 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
278 static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
279 .bLength = sizeof ss_ecm_intr_comp_desc,
280 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
282 /* the following 3 values can be tweaked if necessary */
283 /* .bMaxBurst = 0, */
284 /* .bmAttributes = 0, */
285 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
288 static struct usb_endpoint_descriptor ss_ecm_in_desc = {
289 .bLength = USB_DT_ENDPOINT_SIZE,
290 .bDescriptorType = USB_DT_ENDPOINT,
292 .bEndpointAddress = USB_DIR_IN,
293 .bmAttributes = USB_ENDPOINT_XFER_BULK,
294 .wMaxPacketSize = cpu_to_le16(1024),
297 static struct usb_endpoint_descriptor ss_ecm_out_desc = {
298 .bLength = USB_DT_ENDPOINT_SIZE,
299 .bDescriptorType = USB_DT_ENDPOINT,
301 .bEndpointAddress = USB_DIR_OUT,
302 .bmAttributes = USB_ENDPOINT_XFER_BULK,
303 .wMaxPacketSize = cpu_to_le16(1024),
306 static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
307 .bLength = sizeof ss_ecm_bulk_comp_desc,
308 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
310 /* the following 2 values can be tweaked if necessary */
311 /* .bMaxBurst = 0, */
312 /* .bmAttributes = 0, */
315 static struct usb_descriptor_header *ecm_ss_function[] = {
316 /* CDC ECM control descriptors */
317 (struct usb_descriptor_header *) &ecm_control_intf,
318 (struct usb_descriptor_header *) &ecm_header_desc,
319 (struct usb_descriptor_header *) &ecm_union_desc,
320 (struct usb_descriptor_header *) &ecm_desc,
322 /* NOTE: status endpoint might need to be removed */
323 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
324 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
326 /* data interface, altsettings 0 and 1 */
327 (struct usb_descriptor_header *) &ecm_data_nop_intf,
328 (struct usb_descriptor_header *) &ecm_data_intf,
329 (struct usb_descriptor_header *) &ss_ecm_in_desc,
330 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
331 (struct usb_descriptor_header *) &ss_ecm_out_desc,
332 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
333 NULL,
336 /* string descriptors: */
338 static struct usb_string ecm_string_defs[] = {
339 [0].s = "CDC Ethernet Control Model (ECM)",
340 [1].s = NULL /* DYNAMIC */,
341 [2].s = "CDC Ethernet Data",
342 { } /* end of list */
345 static struct usb_gadget_strings ecm_string_table = {
346 .language = 0x0409, /* en-us */
347 .strings = ecm_string_defs,
350 static struct usb_gadget_strings *ecm_strings[] = {
351 &ecm_string_table,
352 NULL,
355 /*-------------------------------------------------------------------------*/
357 static void ecm_do_notify(struct f_ecm *ecm)
359 struct usb_request *req = ecm->notify_req;
360 struct usb_cdc_notification *event;
361 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
362 __le32 *data;
363 int status;
365 /* notification already in flight? */
366 if (!req)
367 return;
369 event = req->buf;
370 switch (ecm->notify_state) {
371 case ECM_NOTIFY_NONE:
372 return;
374 case ECM_NOTIFY_CONNECT:
375 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
376 if (ecm->is_open)
377 event->wValue = cpu_to_le16(1);
378 else
379 event->wValue = cpu_to_le16(0);
380 event->wLength = 0;
381 req->length = sizeof *event;
383 DBG(cdev, "notify connect %s\n",
384 ecm->is_open ? "true" : "false");
385 ecm->notify_state = ECM_NOTIFY_SPEED;
386 break;
388 case ECM_NOTIFY_SPEED:
389 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
390 event->wValue = cpu_to_le16(0);
391 event->wLength = cpu_to_le16(8);
392 req->length = ECM_STATUS_BYTECOUNT;
394 /* SPEED_CHANGE data is up/down speeds in bits/sec */
395 data = req->buf + sizeof *event;
396 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
397 data[1] = data[0];
399 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
400 ecm->notify_state = ECM_NOTIFY_NONE;
401 break;
403 event->bmRequestType = 0xA1;
404 event->wIndex = cpu_to_le16(ecm->ctrl_id);
406 ecm->notify_req = NULL;
407 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
408 if (status < 0) {
409 ecm->notify_req = req;
410 DBG(cdev, "notify --> %d\n", status);
414 static void ecm_notify(struct f_ecm *ecm)
416 /* NOTE on most versions of Linux, host side cdc-ethernet
417 * won't listen for notifications until its netdevice opens.
418 * The first notification then sits in the FIFO for a long
419 * time, and the second one is queued.
421 ecm->notify_state = ECM_NOTIFY_CONNECT;
422 ecm_do_notify(ecm);
425 static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
427 struct f_ecm *ecm = req->context;
428 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
429 struct usb_cdc_notification *event = req->buf;
431 switch (req->status) {
432 case 0:
433 /* no fault */
434 break;
435 case -ECONNRESET:
436 case -ESHUTDOWN:
437 ecm->notify_state = ECM_NOTIFY_NONE;
438 break;
439 default:
440 DBG(cdev, "event %02x --> %d\n",
441 event->bNotificationType, req->status);
442 break;
444 ecm->notify_req = req;
445 ecm_do_notify(ecm);
448 static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
450 struct f_ecm *ecm = func_to_ecm(f);
451 struct usb_composite_dev *cdev = f->config->cdev;
452 struct usb_request *req = cdev->req;
453 int value = -EOPNOTSUPP;
454 u16 w_index = le16_to_cpu(ctrl->wIndex);
455 u16 w_value = le16_to_cpu(ctrl->wValue);
456 u16 w_length = le16_to_cpu(ctrl->wLength);
458 /* composite driver infrastructure handles everything except
459 * CDC class messages; interface activation uses set_alt().
461 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
462 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
463 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
464 /* see 6.2.30: no data, wIndex = interface,
465 * wValue = packet filter bitmap
467 if (w_length != 0 || w_index != ecm->ctrl_id)
468 goto invalid;
469 DBG(cdev, "packet filter %02x\n", w_value);
470 /* REVISIT locking of cdc_filter. This assumes the UDC
471 * driver won't have a concurrent packet TX irq running on
472 * another CPU; or that if it does, this write is atomic...
474 ecm->port.cdc_filter = w_value;
475 value = 0;
476 break;
478 /* and optionally:
479 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
480 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
481 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
482 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
483 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
484 * case USB_CDC_GET_ETHERNET_STATISTIC:
487 default:
488 invalid:
489 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
490 ctrl->bRequestType, ctrl->bRequest,
491 w_value, w_index, w_length);
494 /* respond with data transfer or status phase? */
495 if (value >= 0) {
496 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
497 ctrl->bRequestType, ctrl->bRequest,
498 w_value, w_index, w_length);
499 req->zero = 0;
500 req->length = value;
501 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
502 if (value < 0)
503 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
504 ctrl->bRequestType, ctrl->bRequest,
505 value);
508 /* device either stalls (value < 0) or reports success */
509 return value;
513 static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
515 struct f_ecm *ecm = func_to_ecm(f);
516 struct usb_composite_dev *cdev = f->config->cdev;
518 /* Control interface has only altsetting 0 */
519 if (intf == ecm->ctrl_id) {
520 if (alt != 0)
521 goto fail;
523 if (ecm->notify->driver_data) {
524 VDBG(cdev, "reset ecm control %d\n", intf);
525 usb_ep_disable(ecm->notify);
527 if (!(ecm->notify->desc)) {
528 VDBG(cdev, "init ecm ctrl %d\n", intf);
529 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
530 goto fail;
532 usb_ep_enable(ecm->notify);
533 ecm->notify->driver_data = ecm;
535 /* Data interface has two altsettings, 0 and 1 */
536 } else if (intf == ecm->data_id) {
537 if (alt > 1)
538 goto fail;
540 if (ecm->port.in_ep->driver_data) {
541 DBG(cdev, "reset ecm\n");
542 gether_disconnect(&ecm->port);
545 if (!ecm->port.in_ep->desc ||
546 !ecm->port.out_ep->desc) {
547 DBG(cdev, "init ecm\n");
548 if (config_ep_by_speed(cdev->gadget, f,
549 ecm->port.in_ep) ||
550 config_ep_by_speed(cdev->gadget, f,
551 ecm->port.out_ep)) {
552 ecm->port.in_ep->desc = NULL;
553 ecm->port.out_ep->desc = NULL;
554 goto fail;
558 /* CDC Ethernet only sends data in non-default altsettings.
559 * Changing altsettings resets filters, statistics, etc.
561 if (alt == 1) {
562 struct net_device *net;
564 /* Enable zlps by default for ECM conformance;
565 * override for musb_hdrc (avoids txdma ovhead).
567 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
569 ecm->port.cdc_filter = DEFAULT_FILTER;
570 DBG(cdev, "activate ecm\n");
571 net = gether_connect(&ecm->port);
572 if (IS_ERR(net))
573 return PTR_ERR(net);
576 /* NOTE this can be a minor disagreement with the ECM spec,
577 * which says speed notifications will "always" follow
578 * connection notifications. But we allow one connect to
579 * follow another (if the first is in flight), and instead
580 * just guarantee that a speed notification is always sent.
582 ecm_notify(ecm);
583 } else
584 goto fail;
586 return 0;
587 fail:
588 return -EINVAL;
591 /* Because the data interface supports multiple altsettings,
592 * this ECM function *MUST* implement a get_alt() method.
594 static int ecm_get_alt(struct usb_function *f, unsigned intf)
596 struct f_ecm *ecm = func_to_ecm(f);
598 if (intf == ecm->ctrl_id)
599 return 0;
600 return ecm->port.in_ep->driver_data ? 1 : 0;
603 static void ecm_disable(struct usb_function *f)
605 struct f_ecm *ecm = func_to_ecm(f);
606 struct usb_composite_dev *cdev = f->config->cdev;
608 DBG(cdev, "ecm deactivated\n");
610 if (ecm->port.in_ep->driver_data)
611 gether_disconnect(&ecm->port);
613 if (ecm->notify->driver_data) {
614 usb_ep_disable(ecm->notify);
615 ecm->notify->driver_data = NULL;
616 ecm->notify->desc = NULL;
620 /*-------------------------------------------------------------------------*/
623 * Callbacks let us notify the host about connect/disconnect when the
624 * net device is opened or closed.
626 * For testing, note that link states on this side include both opened
627 * and closed variants of:
629 * - disconnected/unconfigured
630 * - configured but inactive (data alt 0)
631 * - configured and active (data alt 1)
633 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
634 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
635 * imply the host is actually polling the notification endpoint, and
636 * likewise that "active" doesn't imply it's actually using the data
637 * endpoints for traffic.
640 static void ecm_open(struct gether *geth)
642 struct f_ecm *ecm = func_to_ecm(&geth->func);
644 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
646 ecm->is_open = true;
647 ecm_notify(ecm);
650 static void ecm_close(struct gether *geth)
652 struct f_ecm *ecm = func_to_ecm(&geth->func);
654 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
656 ecm->is_open = false;
657 ecm_notify(ecm);
660 /*-------------------------------------------------------------------------*/
662 /* ethernet function driver setup/binding */
664 static int
665 ecm_bind(struct usb_configuration *c, struct usb_function *f)
667 struct usb_composite_dev *cdev = c->cdev;
668 struct f_ecm *ecm = func_to_ecm(f);
669 int status;
670 struct usb_ep *ep;
672 /* allocate instance-specific interface IDs */
673 status = usb_interface_id(c, f);
674 if (status < 0)
675 goto fail;
676 ecm->ctrl_id = status;
678 ecm_control_intf.bInterfaceNumber = status;
679 ecm_union_desc.bMasterInterface0 = status;
681 status = usb_interface_id(c, f);
682 if (status < 0)
683 goto fail;
684 ecm->data_id = status;
686 ecm_data_nop_intf.bInterfaceNumber = status;
687 ecm_data_intf.bInterfaceNumber = status;
688 ecm_union_desc.bSlaveInterface0 = status;
690 status = -ENODEV;
692 /* allocate instance-specific endpoints */
693 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
694 if (!ep)
695 goto fail;
696 ecm->port.in_ep = ep;
697 ep->driver_data = cdev; /* claim */
699 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
700 if (!ep)
701 goto fail;
702 ecm->port.out_ep = ep;
703 ep->driver_data = cdev; /* claim */
705 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
706 * don't treat it that way. It's simpler, and some newer CDC
707 * profiles (wireless handsets) no longer treat it as optional.
709 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
710 if (!ep)
711 goto fail;
712 ecm->notify = ep;
713 ep->driver_data = cdev; /* claim */
715 status = -ENOMEM;
717 /* allocate notification request and buffer */
718 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
719 if (!ecm->notify_req)
720 goto fail;
721 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
722 if (!ecm->notify_req->buf)
723 goto fail;
724 ecm->notify_req->context = ecm;
725 ecm->notify_req->complete = ecm_notify_complete;
727 /* copy descriptors, and track endpoint copies */
728 f->descriptors = usb_copy_descriptors(ecm_fs_function);
729 if (!f->descriptors)
730 goto fail;
732 /* support all relevant hardware speeds... we expect that when
733 * hardware is dual speed, all bulk-capable endpoints work at
734 * both speeds
736 if (gadget_is_dualspeed(c->cdev->gadget)) {
737 hs_ecm_in_desc.bEndpointAddress =
738 fs_ecm_in_desc.bEndpointAddress;
739 hs_ecm_out_desc.bEndpointAddress =
740 fs_ecm_out_desc.bEndpointAddress;
741 hs_ecm_notify_desc.bEndpointAddress =
742 fs_ecm_notify_desc.bEndpointAddress;
744 /* copy descriptors, and track endpoint copies */
745 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
746 if (!f->hs_descriptors)
747 goto fail;
750 if (gadget_is_superspeed(c->cdev->gadget)) {
751 ss_ecm_in_desc.bEndpointAddress =
752 fs_ecm_in_desc.bEndpointAddress;
753 ss_ecm_out_desc.bEndpointAddress =
754 fs_ecm_out_desc.bEndpointAddress;
755 ss_ecm_notify_desc.bEndpointAddress =
756 fs_ecm_notify_desc.bEndpointAddress;
758 /* copy descriptors, and track endpoint copies */
759 f->ss_descriptors = usb_copy_descriptors(ecm_ss_function);
760 if (!f->ss_descriptors)
761 goto fail;
764 /* NOTE: all that is done without knowing or caring about
765 * the network link ... which is unavailable to this code
766 * until we're activated via set_alt().
769 ecm->port.open = ecm_open;
770 ecm->port.close = ecm_close;
772 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
773 gadget_is_superspeed(c->cdev->gadget) ? "super" :
774 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
775 ecm->port.in_ep->name, ecm->port.out_ep->name,
776 ecm->notify->name);
777 return 0;
779 fail:
780 if (f->descriptors)
781 usb_free_descriptors(f->descriptors);
782 if (f->hs_descriptors)
783 usb_free_descriptors(f->hs_descriptors);
785 if (ecm->notify_req) {
786 kfree(ecm->notify_req->buf);
787 usb_ep_free_request(ecm->notify, ecm->notify_req);
790 /* we might as well release our claims on endpoints */
791 if (ecm->notify)
792 ecm->notify->driver_data = NULL;
793 if (ecm->port.out_ep->desc)
794 ecm->port.out_ep->driver_data = NULL;
795 if (ecm->port.in_ep->desc)
796 ecm->port.in_ep->driver_data = NULL;
798 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
800 return status;
803 static void
804 ecm_unbind(struct usb_configuration *c, struct usb_function *f)
806 struct f_ecm *ecm = func_to_ecm(f);
808 DBG(c->cdev, "ecm unbind\n");
810 if (gadget_is_superspeed(c->cdev->gadget))
811 usb_free_descriptors(f->ss_descriptors);
812 if (gadget_is_dualspeed(c->cdev->gadget))
813 usb_free_descriptors(f->hs_descriptors);
814 usb_free_descriptors(f->descriptors);
816 kfree(ecm->notify_req->buf);
817 usb_ep_free_request(ecm->notify, ecm->notify_req);
819 ecm_string_defs[1].s = NULL;
820 kfree(ecm);
824 * ecm_bind_config - add CDC Ethernet network link to a configuration
825 * @c: the configuration to support the network link
826 * @ethaddr: a buffer in which the ethernet address of the host side
827 * side of the link was recorded
828 * Context: single threaded during gadget setup
830 * Returns zero on success, else negative errno.
832 * Caller must have called @gether_setup(). Caller is also responsible
833 * for calling @gether_cleanup() before module unload.
836 ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
838 struct f_ecm *ecm;
839 int status;
841 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
842 return -EINVAL;
844 /* maybe allocate device-global string IDs */
845 if (ecm_string_defs[0].id == 0) {
847 /* control interface label */
848 status = usb_string_id(c->cdev);
849 if (status < 0)
850 return status;
851 ecm_string_defs[0].id = status;
852 ecm_control_intf.iInterface = status;
854 /* data interface label */
855 status = usb_string_id(c->cdev);
856 if (status < 0)
857 return status;
858 ecm_string_defs[2].id = status;
859 ecm_data_intf.iInterface = status;
861 /* MAC address */
862 status = usb_string_id(c->cdev);
863 if (status < 0)
864 return status;
865 ecm_string_defs[1].id = status;
866 ecm_desc.iMACAddress = status;
869 /* allocate and initialize one new instance */
870 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
871 if (!ecm)
872 return -ENOMEM;
874 /* export host's Ethernet address in CDC format */
875 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
876 "%02X%02X%02X%02X%02X%02X",
877 ethaddr[0], ethaddr[1], ethaddr[2],
878 ethaddr[3], ethaddr[4], ethaddr[5]);
879 ecm_string_defs[1].s = ecm->ethaddr;
881 ecm->port.cdc_filter = DEFAULT_FILTER;
883 ecm->port.func.name = "cdc_ethernet";
884 ecm->port.func.strings = ecm_strings;
885 /* descriptors are per-instance copies */
886 ecm->port.func.bind = ecm_bind;
887 ecm->port.func.unbind = ecm_unbind;
888 ecm->port.func.set_alt = ecm_set_alt;
889 ecm->port.func.get_alt = ecm_get_alt;
890 ecm->port.func.setup = ecm_setup;
891 ecm->port.func.disable = ecm_disable;
893 status = usb_add_function(c, &ecm->port.func);
894 if (status) {
895 ecm_string_defs[1].s = NULL;
896 kfree(ecm);
898 return status;