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>
26 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
27 * Ethernet link. The data transfer model is simple (packets sent and
28 * received over bulk endpoints using normal short packet termination),
29 * and the control model exposes various data and optional notifications.
31 * ECM is well standardized and (except for Microsoft) supported by most
32 * operating systems with USB host support. It's the preferred interop
33 * solution for Ethernet over USB, at least for firmware based solutions.
34 * (Hardware solutions tend to be more minimalist.) A newer and simpler
35 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
37 * Note that ECM requires the use of "alternate settings" for its data
38 * interface. This means that the set_alt() method has real work to do,
39 * and also means that a get_alt() method is required.
43 enum ecm_notify_state
{
44 ECM_NOTIFY_NONE
, /* don't notify */
45 ECM_NOTIFY_CONNECT
, /* issue CONNECT next */
46 ECM_NOTIFY_SPEED
, /* issue SPEED_CHANGE next */
55 struct usb_ep
*notify
;
56 struct usb_request
*notify_req
;
60 /* FIXME is_open needs some irq-ish locking
61 * ... possibly the same as port.ioport
65 static inline struct f_ecm
*func_to_ecm(struct usb_function
*f
)
67 return container_of(f
, struct f_ecm
, port
.func
);
70 /* peak (theoretical) bulk transfer rate in bits-per-second */
71 static inline unsigned ecm_bitrate(struct usb_gadget
*g
)
73 if (gadget_is_superspeed(g
) && g
->speed
== USB_SPEED_SUPER
)
74 return 13 * 1024 * 8 * 1000 * 8;
75 else if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
76 return 13 * 512 * 8 * 1000 * 8;
78 return 19 * 64 * 1 * 1000 * 8;
81 /*-------------------------------------------------------------------------*/
84 * Include the status endpoint if we can, even though it's optional.
86 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
87 * packet, to simplify cancellation; and a big transfer interval, to
88 * waste less bandwidth.
90 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
91 * if they ignore the connect/disconnect notifications that real aether
92 * can provide. More advanced cdc configurations might want to support
93 * encapsulated commands (vendor-specific, using control-OUT).
96 #define ECM_STATUS_INTERVAL_MS 32
97 #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
100 /* interface descriptor: */
102 static struct usb_interface_assoc_descriptor
103 ecm_iad_descriptor
= {
104 .bLength
= sizeof ecm_iad_descriptor
,
105 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
107 /* .bFirstInterface = DYNAMIC, */
108 .bInterfaceCount
= 2, /* control + data */
109 .bFunctionClass
= USB_CLASS_COMM
,
110 .bFunctionSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
111 .bFunctionProtocol
= USB_CDC_PROTO_NONE
,
112 /* .iFunction = DYNAMIC */
116 static struct usb_interface_descriptor ecm_control_intf
= {
117 .bLength
= sizeof ecm_control_intf
,
118 .bDescriptorType
= USB_DT_INTERFACE
,
120 /* .bInterfaceNumber = DYNAMIC */
121 /* status endpoint is optional; this could be patched later */
123 .bInterfaceClass
= USB_CLASS_COMM
,
124 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ETHERNET
,
125 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
126 /* .iInterface = DYNAMIC */
129 static struct usb_cdc_header_desc ecm_header_desc
= {
130 .bLength
= sizeof ecm_header_desc
,
131 .bDescriptorType
= USB_DT_CS_INTERFACE
,
132 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
134 .bcdCDC
= cpu_to_le16(0x0110),
137 static struct usb_cdc_union_desc ecm_union_desc
= {
138 .bLength
= sizeof(ecm_union_desc
),
139 .bDescriptorType
= USB_DT_CS_INTERFACE
,
140 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
141 /* .bMasterInterface0 = DYNAMIC */
142 /* .bSlaveInterface0 = DYNAMIC */
145 static struct usb_cdc_ether_desc ecm_desc
= {
146 .bLength
= sizeof ecm_desc
,
147 .bDescriptorType
= USB_DT_CS_INTERFACE
,
148 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
150 /* this descriptor actually adds value, surprise! */
151 /* .iMACAddress = DYNAMIC */
152 .bmEthernetStatistics
= cpu_to_le32(0), /* no statistics */
153 .wMaxSegmentSize
= cpu_to_le16(ETH_FRAME_LEN
),
154 .wNumberMCFilters
= cpu_to_le16(0),
155 .bNumberPowerFilters
= 0,
158 /* the default data interface has no endpoints ... */
160 static struct usb_interface_descriptor ecm_data_nop_intf
= {
161 .bLength
= sizeof ecm_data_nop_intf
,
162 .bDescriptorType
= USB_DT_INTERFACE
,
164 .bInterfaceNumber
= 1,
165 .bAlternateSetting
= 0,
167 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
168 .bInterfaceSubClass
= 0,
169 .bInterfaceProtocol
= 0,
170 /* .iInterface = DYNAMIC */
173 /* ... but the "real" data interface has two bulk endpoints */
175 static struct usb_interface_descriptor ecm_data_intf
= {
176 .bLength
= sizeof ecm_data_intf
,
177 .bDescriptorType
= USB_DT_INTERFACE
,
179 .bInterfaceNumber
= 1,
180 .bAlternateSetting
= 1,
182 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
183 .bInterfaceSubClass
= 0,
184 .bInterfaceProtocol
= 0,
185 /* .iInterface = DYNAMIC */
188 /* full speed support: */
190 static struct usb_endpoint_descriptor fs_ecm_notify_desc
= {
191 .bLength
= USB_DT_ENDPOINT_SIZE
,
192 .bDescriptorType
= USB_DT_ENDPOINT
,
194 .bEndpointAddress
= USB_DIR_IN
,
195 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
196 .wMaxPacketSize
= cpu_to_le16(ECM_STATUS_BYTECOUNT
),
197 .bInterval
= ECM_STATUS_INTERVAL_MS
,
200 static struct usb_endpoint_descriptor fs_ecm_in_desc
= {
201 .bLength
= USB_DT_ENDPOINT_SIZE
,
202 .bDescriptorType
= USB_DT_ENDPOINT
,
204 .bEndpointAddress
= USB_DIR_IN
,
205 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
208 static struct usb_endpoint_descriptor fs_ecm_out_desc
= {
209 .bLength
= USB_DT_ENDPOINT_SIZE
,
210 .bDescriptorType
= USB_DT_ENDPOINT
,
212 .bEndpointAddress
= USB_DIR_OUT
,
213 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
216 static struct usb_descriptor_header
*ecm_fs_function
[] = {
217 /* CDC ECM control descriptors */
218 (struct usb_descriptor_header
*) &ecm_iad_descriptor
,
219 (struct usb_descriptor_header
*) &ecm_control_intf
,
220 (struct usb_descriptor_header
*) &ecm_header_desc
,
221 (struct usb_descriptor_header
*) &ecm_union_desc
,
222 (struct usb_descriptor_header
*) &ecm_desc
,
224 /* NOTE: status endpoint might need to be removed */
225 (struct usb_descriptor_header
*) &fs_ecm_notify_desc
,
227 /* data interface, altsettings 0 and 1 */
228 (struct usb_descriptor_header
*) &ecm_data_nop_intf
,
229 (struct usb_descriptor_header
*) &ecm_data_intf
,
230 (struct usb_descriptor_header
*) &fs_ecm_in_desc
,
231 (struct usb_descriptor_header
*) &fs_ecm_out_desc
,
235 /* high speed support: */
237 static struct usb_endpoint_descriptor hs_ecm_notify_desc
= {
238 .bLength
= USB_DT_ENDPOINT_SIZE
,
239 .bDescriptorType
= USB_DT_ENDPOINT
,
241 .bEndpointAddress
= USB_DIR_IN
,
242 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
243 .wMaxPacketSize
= cpu_to_le16(ECM_STATUS_BYTECOUNT
),
244 .bInterval
= USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS
),
247 static struct usb_endpoint_descriptor hs_ecm_in_desc
= {
248 .bLength
= USB_DT_ENDPOINT_SIZE
,
249 .bDescriptorType
= USB_DT_ENDPOINT
,
251 .bEndpointAddress
= USB_DIR_IN
,
252 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
253 .wMaxPacketSize
= cpu_to_le16(512),
256 static struct usb_endpoint_descriptor hs_ecm_out_desc
= {
257 .bLength
= USB_DT_ENDPOINT_SIZE
,
258 .bDescriptorType
= USB_DT_ENDPOINT
,
260 .bEndpointAddress
= USB_DIR_OUT
,
261 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
262 .wMaxPacketSize
= cpu_to_le16(512),
265 static struct usb_descriptor_header
*ecm_hs_function
[] = {
266 /* CDC ECM control descriptors */
267 (struct usb_descriptor_header
*) &ecm_iad_descriptor
,
268 (struct usb_descriptor_header
*) &ecm_control_intf
,
269 (struct usb_descriptor_header
*) &ecm_header_desc
,
270 (struct usb_descriptor_header
*) &ecm_union_desc
,
271 (struct usb_descriptor_header
*) &ecm_desc
,
273 /* NOTE: status endpoint might need to be removed */
274 (struct usb_descriptor_header
*) &hs_ecm_notify_desc
,
276 /* data interface, altsettings 0 and 1 */
277 (struct usb_descriptor_header
*) &ecm_data_nop_intf
,
278 (struct usb_descriptor_header
*) &ecm_data_intf
,
279 (struct usb_descriptor_header
*) &hs_ecm_in_desc
,
280 (struct usb_descriptor_header
*) &hs_ecm_out_desc
,
284 /* super speed support: */
286 static struct usb_endpoint_descriptor ss_ecm_notify_desc
= {
287 .bLength
= USB_DT_ENDPOINT_SIZE
,
288 .bDescriptorType
= USB_DT_ENDPOINT
,
290 .bEndpointAddress
= USB_DIR_IN
,
291 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
292 .wMaxPacketSize
= cpu_to_le16(ECM_STATUS_BYTECOUNT
),
293 .bInterval
= USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS
),
296 static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc
= {
297 .bLength
= sizeof ss_ecm_intr_comp_desc
,
298 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
300 /* the following 3 values can be tweaked if necessary */
301 /* .bMaxBurst = 0, */
302 /* .bmAttributes = 0, */
303 .wBytesPerInterval
= cpu_to_le16(ECM_STATUS_BYTECOUNT
),
306 static struct usb_endpoint_descriptor ss_ecm_in_desc
= {
307 .bLength
= USB_DT_ENDPOINT_SIZE
,
308 .bDescriptorType
= USB_DT_ENDPOINT
,
310 .bEndpointAddress
= USB_DIR_IN
,
311 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
312 .wMaxPacketSize
= cpu_to_le16(1024),
315 static struct usb_endpoint_descriptor ss_ecm_out_desc
= {
316 .bLength
= USB_DT_ENDPOINT_SIZE
,
317 .bDescriptorType
= USB_DT_ENDPOINT
,
319 .bEndpointAddress
= USB_DIR_OUT
,
320 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
321 .wMaxPacketSize
= cpu_to_le16(1024),
324 static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc
= {
325 .bLength
= sizeof ss_ecm_bulk_comp_desc
,
326 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
328 /* the following 2 values can be tweaked if necessary */
329 /* .bMaxBurst = 0, */
330 /* .bmAttributes = 0, */
333 static struct usb_descriptor_header
*ecm_ss_function
[] = {
334 /* CDC ECM control descriptors */
335 (struct usb_descriptor_header
*) &ecm_iad_descriptor
,
336 (struct usb_descriptor_header
*) &ecm_control_intf
,
337 (struct usb_descriptor_header
*) &ecm_header_desc
,
338 (struct usb_descriptor_header
*) &ecm_union_desc
,
339 (struct usb_descriptor_header
*) &ecm_desc
,
341 /* NOTE: status endpoint might need to be removed */
342 (struct usb_descriptor_header
*) &ss_ecm_notify_desc
,
343 (struct usb_descriptor_header
*) &ss_ecm_intr_comp_desc
,
345 /* data interface, altsettings 0 and 1 */
346 (struct usb_descriptor_header
*) &ecm_data_nop_intf
,
347 (struct usb_descriptor_header
*) &ecm_data_intf
,
348 (struct usb_descriptor_header
*) &ss_ecm_in_desc
,
349 (struct usb_descriptor_header
*) &ss_ecm_bulk_comp_desc
,
350 (struct usb_descriptor_header
*) &ss_ecm_out_desc
,
351 (struct usb_descriptor_header
*) &ss_ecm_bulk_comp_desc
,
355 /* string descriptors: */
357 static struct usb_string ecm_string_defs
[] = {
358 [0].s
= "CDC Ethernet Control Model (ECM)",
360 [2].s
= "CDC Ethernet Data",
362 { } /* end of list */
365 static struct usb_gadget_strings ecm_string_table
= {
366 .language
= 0x0409, /* en-us */
367 .strings
= ecm_string_defs
,
370 static struct usb_gadget_strings
*ecm_strings
[] = {
375 /*-------------------------------------------------------------------------*/
377 static void ecm_do_notify(struct f_ecm
*ecm
)
379 struct usb_request
*req
= ecm
->notify_req
;
380 struct usb_cdc_notification
*event
;
381 struct usb_composite_dev
*cdev
= ecm
->port
.func
.config
->cdev
;
385 /* notification already in flight? */
390 switch (ecm
->notify_state
) {
391 case ECM_NOTIFY_NONE
:
394 case ECM_NOTIFY_CONNECT
:
395 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
397 event
->wValue
= cpu_to_le16(1);
399 event
->wValue
= cpu_to_le16(0);
401 req
->length
= sizeof *event
;
403 DBG(cdev
, "notify connect %s\n",
404 ecm
->is_open
? "true" : "false");
405 ecm
->notify_state
= ECM_NOTIFY_SPEED
;
408 case ECM_NOTIFY_SPEED
:
409 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
410 event
->wValue
= cpu_to_le16(0);
411 event
->wLength
= cpu_to_le16(8);
412 req
->length
= ECM_STATUS_BYTECOUNT
;
414 /* SPEED_CHANGE data is up/down speeds in bits/sec */
415 data
= req
->buf
+ sizeof *event
;
416 data
[0] = cpu_to_le32(ecm_bitrate(cdev
->gadget
));
419 DBG(cdev
, "notify speed %d\n", ecm_bitrate(cdev
->gadget
));
420 ecm
->notify_state
= ECM_NOTIFY_NONE
;
423 event
->bmRequestType
= 0xA1;
424 event
->wIndex
= cpu_to_le16(ecm
->ctrl_id
);
426 ecm
->notify_req
= NULL
;
427 status
= usb_ep_queue(ecm
->notify
, req
, GFP_ATOMIC
);
429 ecm
->notify_req
= req
;
430 DBG(cdev
, "notify --> %d\n", status
);
434 static void ecm_notify(struct f_ecm
*ecm
)
436 /* NOTE on most versions of Linux, host side cdc-ethernet
437 * won't listen for notifications until its netdevice opens.
438 * The first notification then sits in the FIFO for a long
439 * time, and the second one is queued.
441 ecm
->notify_state
= ECM_NOTIFY_CONNECT
;
445 static void ecm_notify_complete(struct usb_ep
*ep
, struct usb_request
*req
)
447 struct f_ecm
*ecm
= req
->context
;
448 struct usb_composite_dev
*cdev
= ecm
->port
.func
.config
->cdev
;
449 struct usb_cdc_notification
*event
= req
->buf
;
451 switch (req
->status
) {
457 ecm
->notify_state
= ECM_NOTIFY_NONE
;
460 DBG(cdev
, "event %02x --> %d\n",
461 event
->bNotificationType
, req
->status
);
464 ecm
->notify_req
= req
;
468 static int ecm_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
470 struct f_ecm
*ecm
= func_to_ecm(f
);
471 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
472 struct usb_request
*req
= cdev
->req
;
473 int value
= -EOPNOTSUPP
;
474 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
475 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
476 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
478 /* composite driver infrastructure handles everything except
479 * CDC class messages; interface activation uses set_alt().
481 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
482 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
483 | USB_CDC_SET_ETHERNET_PACKET_FILTER
:
484 /* see 6.2.30: no data, wIndex = interface,
485 * wValue = packet filter bitmap
487 if (w_length
!= 0 || w_index
!= ecm
->ctrl_id
)
489 DBG(cdev
, "packet filter %02x\n", w_value
);
490 /* REVISIT locking of cdc_filter. This assumes the UDC
491 * driver won't have a concurrent packet TX irq running on
492 * another CPU; or that if it does, this write is atomic...
494 ecm
->port
.cdc_filter
= w_value
;
499 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
500 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
501 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
502 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
503 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
504 * case USB_CDC_GET_ETHERNET_STATISTIC:
509 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
510 ctrl
->bRequestType
, ctrl
->bRequest
,
511 w_value
, w_index
, w_length
);
514 /* respond with data transfer or status phase? */
516 DBG(cdev
, "ecm req%02x.%02x v%04x i%04x l%d\n",
517 ctrl
->bRequestType
, ctrl
->bRequest
,
518 w_value
, w_index
, w_length
);
521 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
523 ERROR(cdev
, "ecm req %02x.%02x response err %d\n",
524 ctrl
->bRequestType
, ctrl
->bRequest
,
528 /* device either stalls (value < 0) or reports success */
533 static int ecm_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
535 struct f_ecm
*ecm
= func_to_ecm(f
);
536 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
538 /* Control interface has only altsetting 0 */
539 if (intf
== ecm
->ctrl_id
) {
543 if (ecm
->notify
->driver_data
) {
544 VDBG(cdev
, "reset ecm control %d\n", intf
);
545 usb_ep_disable(ecm
->notify
);
547 if (!(ecm
->notify
->desc
)) {
548 VDBG(cdev
, "init ecm ctrl %d\n", intf
);
549 if (config_ep_by_speed(cdev
->gadget
, f
, ecm
->notify
))
552 usb_ep_enable(ecm
->notify
);
553 ecm
->notify
->driver_data
= ecm
;
555 /* Data interface has two altsettings, 0 and 1 */
556 } else if (intf
== ecm
->data_id
) {
560 if (ecm
->port
.in_ep
->driver_data
) {
561 DBG(cdev
, "reset ecm\n");
562 gether_disconnect(&ecm
->port
);
565 if (!ecm
->port
.in_ep
->desc
||
566 !ecm
->port
.out_ep
->desc
) {
567 DBG(cdev
, "init ecm\n");
568 if (config_ep_by_speed(cdev
->gadget
, f
,
570 config_ep_by_speed(cdev
->gadget
, f
,
572 ecm
->port
.in_ep
->desc
= NULL
;
573 ecm
->port
.out_ep
->desc
= NULL
;
578 /* CDC Ethernet only sends data in non-default altsettings.
579 * Changing altsettings resets filters, statistics, etc.
582 struct net_device
*net
;
584 /* Enable zlps by default for ECM conformance;
585 * override for musb_hdrc (avoids txdma ovhead).
587 ecm
->port
.is_zlp_ok
= !(gadget_is_musbhdrc(cdev
->gadget
)
589 ecm
->port
.cdc_filter
= DEFAULT_FILTER
;
590 DBG(cdev
, "activate ecm\n");
591 net
= gether_connect(&ecm
->port
);
596 /* NOTE this can be a minor disagreement with the ECM spec,
597 * which says speed notifications will "always" follow
598 * connection notifications. But we allow one connect to
599 * follow another (if the first is in flight), and instead
600 * just guarantee that a speed notification is always sent.
611 /* Because the data interface supports multiple altsettings,
612 * this ECM function *MUST* implement a get_alt() method.
614 static int ecm_get_alt(struct usb_function
*f
, unsigned intf
)
616 struct f_ecm
*ecm
= func_to_ecm(f
);
618 if (intf
== ecm
->ctrl_id
)
620 return ecm
->port
.in_ep
->driver_data
? 1 : 0;
623 static void ecm_disable(struct usb_function
*f
)
625 struct f_ecm
*ecm
= func_to_ecm(f
);
626 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
628 DBG(cdev
, "ecm deactivated\n");
630 if (ecm
->port
.in_ep
->driver_data
)
631 gether_disconnect(&ecm
->port
);
633 if (ecm
->notify
->driver_data
) {
634 usb_ep_disable(ecm
->notify
);
635 ecm
->notify
->driver_data
= NULL
;
636 ecm
->notify
->desc
= NULL
;
640 /*-------------------------------------------------------------------------*/
643 * Callbacks let us notify the host about connect/disconnect when the
644 * net device is opened or closed.
646 * For testing, note that link states on this side include both opened
647 * and closed variants of:
649 * - disconnected/unconfigured
650 * - configured but inactive (data alt 0)
651 * - configured and active (data alt 1)
653 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
654 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
655 * imply the host is actually polling the notification endpoint, and
656 * likewise that "active" doesn't imply it's actually using the data
657 * endpoints for traffic.
660 static void ecm_open(struct gether
*geth
)
662 struct f_ecm
*ecm
= func_to_ecm(&geth
->func
);
664 DBG(ecm
->port
.func
.config
->cdev
, "%s\n", __func__
);
670 static void ecm_close(struct gether
*geth
)
672 struct f_ecm
*ecm
= func_to_ecm(&geth
->func
);
674 DBG(ecm
->port
.func
.config
->cdev
, "%s\n", __func__
);
676 ecm
->is_open
= false;
680 /*-------------------------------------------------------------------------*/
682 /* ethernet function driver setup/binding */
685 ecm_bind(struct usb_configuration
*c
, struct usb_function
*f
)
687 struct usb_composite_dev
*cdev
= c
->cdev
;
688 struct f_ecm
*ecm
= func_to_ecm(f
);
689 struct usb_string
*us
;
693 #ifndef USBF_ECM_INCLUDED
694 struct f_ecm_opts
*ecm_opts
;
696 if (!can_support_ecm(cdev
->gadget
))
699 ecm_opts
= container_of(f
->fi
, struct f_ecm_opts
, func_inst
);
702 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
703 * configurations are bound in sequence with list_for_each_entry,
704 * in each configuration its functions are bound in sequence
705 * with list_for_each_entry, so we assume no race condition
706 * with regard to ecm_opts->bound access
708 if (!ecm_opts
->bound
) {
709 gether_set_gadget(ecm_opts
->net
, cdev
->gadget
);
710 status
= gether_register_netdev(ecm_opts
->net
);
713 ecm_opts
->bound
= true;
716 us
= usb_gstrings_attach(cdev
, ecm_strings
,
717 ARRAY_SIZE(ecm_string_defs
));
720 ecm_control_intf
.iInterface
= us
[0].id
;
721 ecm_data_intf
.iInterface
= us
[2].id
;
722 ecm_desc
.iMACAddress
= us
[1].id
;
723 ecm_iad_descriptor
.iFunction
= us
[3].id
;
725 /* allocate instance-specific interface IDs */
726 status
= usb_interface_id(c
, f
);
729 ecm
->ctrl_id
= status
;
730 ecm_iad_descriptor
.bFirstInterface
= status
;
732 ecm_control_intf
.bInterfaceNumber
= status
;
733 ecm_union_desc
.bMasterInterface0
= status
;
735 status
= usb_interface_id(c
, f
);
738 ecm
->data_id
= status
;
740 ecm_data_nop_intf
.bInterfaceNumber
= status
;
741 ecm_data_intf
.bInterfaceNumber
= status
;
742 ecm_union_desc
.bSlaveInterface0
= status
;
746 /* allocate instance-specific endpoints */
747 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ecm_in_desc
);
750 ecm
->port
.in_ep
= ep
;
751 ep
->driver_data
= cdev
; /* claim */
753 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ecm_out_desc
);
756 ecm
->port
.out_ep
= ep
;
757 ep
->driver_data
= cdev
; /* claim */
759 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
760 * don't treat it that way. It's simpler, and some newer CDC
761 * profiles (wireless handsets) no longer treat it as optional.
763 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ecm_notify_desc
);
767 ep
->driver_data
= cdev
; /* claim */
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 /* we might as well release our claims on endpoints */
823 ecm
->notify
->driver_data
= NULL
;
824 if (ecm
->port
.out_ep
)
825 ecm
->port
.out_ep
->driver_data
= NULL
;
827 ecm
->port
.in_ep
->driver_data
= NULL
;
829 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
834 #ifdef USBF_ECM_INCLUDED
837 ecm_old_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
839 struct f_ecm
*ecm
= func_to_ecm(f
);
841 DBG(c
->cdev
, "ecm unbind\n");
843 usb_free_all_descriptors(f
);
845 kfree(ecm
->notify_req
->buf
);
846 usb_ep_free_request(ecm
->notify
, ecm
->notify_req
);
851 * ecm_bind_config - add CDC Ethernet network link to a configuration
852 * @c: the configuration to support the network link
853 * @ethaddr: a buffer in which the ethernet address of the host side
854 * side of the link was recorded
855 * @dev: eth_dev structure
856 * Context: single threaded during gadget setup
858 * Returns zero on success, else negative errno.
860 * Caller must have called @gether_setup(). Caller is also responsible
861 * for calling @gether_cleanup() before module unload.
864 ecm_bind_config(struct usb_configuration
*c
, u8 ethaddr
[ETH_ALEN
],
870 if (!can_support_ecm(c
->cdev
->gadget
) || !ethaddr
)
873 /* allocate and initialize one new instance */
874 ecm
= kzalloc(sizeof *ecm
, GFP_KERNEL
);
878 /* export host's Ethernet address in CDC format */
879 snprintf(ecm
->ethaddr
, sizeof ecm
->ethaddr
, "%pm", ethaddr
);
880 ecm_string_defs
[1].s
= ecm
->ethaddr
;
882 ecm
->port
.ioport
= dev
;
883 ecm
->port
.cdc_filter
= DEFAULT_FILTER
;
885 ecm
->port
.func
.name
= "cdc_ethernet";
886 /* descriptors are per-instance copies */
887 ecm
->port
.func
.bind
= ecm_bind
;
888 ecm
->port
.func
.unbind
= ecm_old_unbind
;
889 ecm
->port
.func
.set_alt
= ecm_set_alt
;
890 ecm
->port
.func
.get_alt
= ecm_get_alt
;
891 ecm
->port
.func
.setup
= ecm_setup
;
892 ecm
->port
.func
.disable
= ecm_disable
;
894 status
= usb_add_function(c
, &ecm
->port
.func
);
902 static void ecm_free_inst(struct usb_function_instance
*f
)
904 struct f_ecm_opts
*opts
;
906 opts
= container_of(f
, struct f_ecm_opts
, func_inst
);
908 gether_cleanup(netdev_priv(opts
->net
));
910 free_netdev(opts
->net
);
914 static struct usb_function_instance
*ecm_alloc_inst(void)
916 struct f_ecm_opts
*opts
;
918 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
920 return ERR_PTR(-ENOMEM
);
922 opts
->func_inst
.free_func_inst
= ecm_free_inst
;
923 opts
->net
= gether_setup_default();
924 if (IS_ERR(opts
->net
))
925 return ERR_PTR(PTR_ERR(opts
->net
));
927 return &opts
->func_inst
;
930 static void ecm_free(struct usb_function
*f
)
934 ecm
= func_to_ecm(f
);
938 static void ecm_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
940 struct f_ecm
*ecm
= func_to_ecm(f
);
942 DBG(c
->cdev
, "ecm unbind\n");
944 usb_free_all_descriptors(f
);
946 kfree(ecm
->notify_req
->buf
);
947 usb_ep_free_request(ecm
->notify
, ecm
->notify_req
);
950 struct usb_function
*ecm_alloc(struct usb_function_instance
*fi
)
953 struct f_ecm_opts
*opts
;
956 /* allocate and initialize one new instance */
957 ecm
= kzalloc(sizeof(*ecm
), GFP_KERNEL
);
959 return ERR_PTR(-ENOMEM
);
961 opts
= container_of(fi
, struct f_ecm_opts
, func_inst
);
963 /* export host's Ethernet address in CDC format */
964 status
= gether_get_host_addr_cdc(opts
->net
, ecm
->ethaddr
,
965 sizeof(ecm
->ethaddr
));
968 return ERR_PTR(-EINVAL
);
970 ecm_string_defs
[1].s
= ecm
->ethaddr
;
972 ecm
->port
.ioport
= netdev_priv(opts
->net
);
973 ecm
->port
.cdc_filter
= DEFAULT_FILTER
;
975 ecm
->port
.func
.name
= "cdc_ethernet";
976 /* descriptors are per-instance copies */
977 ecm
->port
.func
.bind
= ecm_bind
;
978 ecm
->port
.func
.unbind
= ecm_unbind
;
979 ecm
->port
.func
.set_alt
= ecm_set_alt
;
980 ecm
->port
.func
.get_alt
= ecm_get_alt
;
981 ecm
->port
.func
.setup
= ecm_setup
;
982 ecm
->port
.func
.disable
= ecm_disable
;
983 ecm
->port
.func
.free_func
= ecm_free
;
985 return &ecm
->port
.func
;
988 DECLARE_USB_FUNCTION_INIT(ecm
, ecm_alloc_inst
, ecm_alloc
);
989 MODULE_LICENSE("GPL");
990 MODULE_AUTHOR("David Brownell");