]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/gadget/function/f_ecm.c
usb: gadget: f_ecm: Use atomic_t to track in-flight request
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / gadget / function / f_ecm.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
da741b8c
DB
2/*
3 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
4 *
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2008 Nokia Corporation
da741b8c
DB
7 */
8
9/* #define VERBOSE_DEBUG */
10
5a0e3ad6 11#include <linux/slab.h>
da741b8c 12#include <linux/kernel.h>
fee562a6 13#include <linux/module.h>
da741b8c
DB
14#include <linux/device.h>
15#include <linux/etherdevice.h>
16
17#include "u_ether.h"
da92801c 18#include "u_ether_configfs.h"
fee562a6 19#include "u_ecm.h"
da741b8c
DB
20
21
22/*
23 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
24 * Ethernet link. The data transfer model is simple (packets sent and
25 * received over bulk endpoints using normal short packet termination),
26 * and the control model exposes various data and optional notifications.
27 *
28 * ECM is well standardized and (except for Microsoft) supported by most
29 * operating systems with USB host support. It's the preferred interop
30 * solution for Ethernet over USB, at least for firmware based solutions.
31 * (Hardware solutions tend to be more minimalist.) A newer and simpler
32 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
33 *
34 * Note that ECM requires the use of "alternate settings" for its data
35 * interface. This means that the set_alt() method has real work to do,
36 * and also means that a get_alt() method is required.
37 */
38
da741b8c
DB
39
40enum ecm_notify_state {
41 ECM_NOTIFY_NONE, /* don't notify */
42 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
43 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
44};
45
46struct f_ecm {
47 struct gether port;
48 u8 ctrl_id, data_id;
49
50 char ethaddr[14];
51
da741b8c 52 struct usb_ep *notify;
da741b8c
DB
53 struct usb_request *notify_req;
54 u8 notify_state;
c14827fe 55 atomic_t notify_count;
da741b8c
DB
56 bool is_open;
57
58 /* FIXME is_open needs some irq-ish locking
59 * ... possibly the same as port.ioport
60 */
61};
62
63static inline struct f_ecm *func_to_ecm(struct usb_function *f)
64{
65 return container_of(f, struct f_ecm, port.func);
66}
67
68/* peak (theoretical) bulk transfer rate in bits-per-second */
33376c1c 69static inline unsigned ecm_bitrate(struct usb_gadget *g)
da741b8c 70{
04617db7
PZ
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)
da741b8c
DB
74 return 13 * 512 * 8 * 1000 * 8;
75 else
04617db7 76 return 19 * 64 * 1 * 1000 * 8;
da741b8c
DB
77}
78
79/*-------------------------------------------------------------------------*/
80
81/*
82 * Include the status endpoint if we can, even though it's optional.
83 *
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.
87 *
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).
92 */
93
bcb2f99c 94#define ECM_STATUS_INTERVAL_MS 32
33376c1c 95#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
da741b8c
DB
96
97
98/* interface descriptor: */
99
d11519ad
PN
100static struct usb_interface_assoc_descriptor
101ecm_iad_descriptor = {
102 .bLength = sizeof ecm_iad_descriptor,
103 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
104
105 /* .bFirstInterface = DYNAMIC, */
106 .bInterfaceCount = 2, /* control + data */
107 .bFunctionClass = USB_CLASS_COMM,
108 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
109 .bFunctionProtocol = USB_CDC_PROTO_NONE,
110 /* .iFunction = DYNAMIC */
111};
112
113
28824b18 114static struct usb_interface_descriptor ecm_control_intf = {
da741b8c
DB
115 .bLength = sizeof ecm_control_intf,
116 .bDescriptorType = USB_DT_INTERFACE,
117
118 /* .bInterfaceNumber = DYNAMIC */
119 /* status endpoint is optional; this could be patched later */
120 .bNumEndpoints = 1,
121 .bInterfaceClass = USB_CLASS_COMM,
122 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
123 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
124 /* .iInterface = DYNAMIC */
125};
126
28824b18 127static struct usb_cdc_header_desc ecm_header_desc = {
33376c1c 128 .bLength = sizeof ecm_header_desc,
da741b8c
DB
129 .bDescriptorType = USB_DT_CS_INTERFACE,
130 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
131
551509d2 132 .bcdCDC = cpu_to_le16(0x0110),
da741b8c
DB
133};
134
28824b18 135static struct usb_cdc_union_desc ecm_union_desc = {
da741b8c
DB
136 .bLength = sizeof(ecm_union_desc),
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubType = USB_CDC_UNION_TYPE,
139 /* .bMasterInterface0 = DYNAMIC */
140 /* .bSlaveInterface0 = DYNAMIC */
141};
142
28824b18 143static struct usb_cdc_ether_desc ecm_desc = {
33376c1c 144 .bLength = sizeof ecm_desc,
da741b8c
DB
145 .bDescriptorType = USB_DT_CS_INTERFACE,
146 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
147
148 /* this descriptor actually adds value, surprise! */
149 /* .iMACAddress = DYNAMIC */
551509d2
HH
150 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
151 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
152 .wNumberMCFilters = cpu_to_le16(0),
da741b8c
DB
153 .bNumberPowerFilters = 0,
154};
155
156/* the default data interface has no endpoints ... */
157
28824b18 158static struct usb_interface_descriptor ecm_data_nop_intf = {
da741b8c
DB
159 .bLength = sizeof ecm_data_nop_intf,
160 .bDescriptorType = USB_DT_INTERFACE,
161
162 .bInterfaceNumber = 1,
163 .bAlternateSetting = 0,
164 .bNumEndpoints = 0,
165 .bInterfaceClass = USB_CLASS_CDC_DATA,
166 .bInterfaceSubClass = 0,
167 .bInterfaceProtocol = 0,
168 /* .iInterface = DYNAMIC */
169};
170
171/* ... but the "real" data interface has two bulk endpoints */
172
28824b18 173static struct usb_interface_descriptor ecm_data_intf = {
da741b8c
DB
174 .bLength = sizeof ecm_data_intf,
175 .bDescriptorType = USB_DT_INTERFACE,
176
177 .bInterfaceNumber = 1,
178 .bAlternateSetting = 1,
179 .bNumEndpoints = 2,
180 .bInterfaceClass = USB_CLASS_CDC_DATA,
181 .bInterfaceSubClass = 0,
182 .bInterfaceProtocol = 0,
183 /* .iInterface = DYNAMIC */
184};
185
186/* full speed support: */
187
28824b18 188static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
da741b8c
DB
189 .bLength = USB_DT_ENDPOINT_SIZE,
190 .bDescriptorType = USB_DT_ENDPOINT,
191
192 .bEndpointAddress = USB_DIR_IN,
193 .bmAttributes = USB_ENDPOINT_XFER_INT,
551509d2 194 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
bcb2f99c 195 .bInterval = ECM_STATUS_INTERVAL_MS,
da741b8c
DB
196};
197
28824b18 198static struct usb_endpoint_descriptor fs_ecm_in_desc = {
da741b8c
DB
199 .bLength = USB_DT_ENDPOINT_SIZE,
200 .bDescriptorType = USB_DT_ENDPOINT,
201
202 .bEndpointAddress = USB_DIR_IN,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204};
205
28824b18 206static struct usb_endpoint_descriptor fs_ecm_out_desc = {
da741b8c
DB
207 .bLength = USB_DT_ENDPOINT_SIZE,
208 .bDescriptorType = USB_DT_ENDPOINT,
209
210 .bEndpointAddress = USB_DIR_OUT,
211 .bmAttributes = USB_ENDPOINT_XFER_BULK,
212};
213
28824b18 214static struct usb_descriptor_header *ecm_fs_function[] = {
da741b8c 215 /* CDC ECM control descriptors */
d11519ad 216 (struct usb_descriptor_header *) &ecm_iad_descriptor,
da741b8c 217 (struct usb_descriptor_header *) &ecm_control_intf,
33376c1c 218 (struct usb_descriptor_header *) &ecm_header_desc,
da741b8c 219 (struct usb_descriptor_header *) &ecm_union_desc,
33376c1c 220 (struct usb_descriptor_header *) &ecm_desc,
04617db7 221
da741b8c 222 /* NOTE: status endpoint might need to be removed */
33376c1c 223 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
04617db7 224
da741b8c
DB
225 /* data interface, altsettings 0 and 1 */
226 (struct usb_descriptor_header *) &ecm_data_nop_intf,
227 (struct usb_descriptor_header *) &ecm_data_intf,
33376c1c
DB
228 (struct usb_descriptor_header *) &fs_ecm_in_desc,
229 (struct usb_descriptor_header *) &fs_ecm_out_desc,
da741b8c
DB
230 NULL,
231};
232
233/* high speed support: */
234
28824b18 235static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
da741b8c
DB
236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
238
239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT,
551509d2 241 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
bcb2f99c 242 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
da741b8c 243};
04617db7 244
28824b18 245static struct usb_endpoint_descriptor hs_ecm_in_desc = {
da741b8c
DB
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_IN,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 251 .wMaxPacketSize = cpu_to_le16(512),
da741b8c
DB
252};
253
28824b18 254static struct usb_endpoint_descriptor hs_ecm_out_desc = {
da741b8c
DB
255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT,
257
258 .bEndpointAddress = USB_DIR_OUT,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 260 .wMaxPacketSize = cpu_to_le16(512),
da741b8c
DB
261};
262
28824b18 263static struct usb_descriptor_header *ecm_hs_function[] = {
da741b8c 264 /* CDC ECM control descriptors */
d11519ad 265 (struct usb_descriptor_header *) &ecm_iad_descriptor,
da741b8c 266 (struct usb_descriptor_header *) &ecm_control_intf,
33376c1c 267 (struct usb_descriptor_header *) &ecm_header_desc,
da741b8c 268 (struct usb_descriptor_header *) &ecm_union_desc,
33376c1c 269 (struct usb_descriptor_header *) &ecm_desc,
04617db7 270
da741b8c 271 /* NOTE: status endpoint might need to be removed */
33376c1c 272 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
04617db7 273
da741b8c
DB
274 /* data interface, altsettings 0 and 1 */
275 (struct usb_descriptor_header *) &ecm_data_nop_intf,
276 (struct usb_descriptor_header *) &ecm_data_intf,
33376c1c
DB
277 (struct usb_descriptor_header *) &hs_ecm_in_desc,
278 (struct usb_descriptor_header *) &hs_ecm_out_desc,
da741b8c
DB
279 NULL,
280};
281
04617db7
PZ
282/* super speed support: */
283
284static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
285 .bLength = USB_DT_ENDPOINT_SIZE,
286 .bDescriptorType = USB_DT_ENDPOINT,
287
288 .bEndpointAddress = USB_DIR_IN,
289 .bmAttributes = USB_ENDPOINT_XFER_INT,
290 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
bcb2f99c 291 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
04617db7
PZ
292};
293
294static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
295 .bLength = sizeof ss_ecm_intr_comp_desc,
296 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
297
298 /* the following 3 values can be tweaked if necessary */
299 /* .bMaxBurst = 0, */
300 /* .bmAttributes = 0, */
301 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
302};
303
304static struct usb_endpoint_descriptor ss_ecm_in_desc = {
305 .bLength = USB_DT_ENDPOINT_SIZE,
306 .bDescriptorType = USB_DT_ENDPOINT,
307
308 .bEndpointAddress = USB_DIR_IN,
309 .bmAttributes = USB_ENDPOINT_XFER_BULK,
310 .wMaxPacketSize = cpu_to_le16(1024),
311};
312
313static struct usb_endpoint_descriptor ss_ecm_out_desc = {
314 .bLength = USB_DT_ENDPOINT_SIZE,
315 .bDescriptorType = USB_DT_ENDPOINT,
316
317 .bEndpointAddress = USB_DIR_OUT,
318 .bmAttributes = USB_ENDPOINT_XFER_BULK,
319 .wMaxPacketSize = cpu_to_le16(1024),
320};
321
322static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
323 .bLength = sizeof ss_ecm_bulk_comp_desc,
324 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
325
326 /* the following 2 values can be tweaked if necessary */
327 /* .bMaxBurst = 0, */
328 /* .bmAttributes = 0, */
329};
330
331static struct usb_descriptor_header *ecm_ss_function[] = {
332 /* CDC ECM control descriptors */
585e3931 333 (struct usb_descriptor_header *) &ecm_iad_descriptor,
04617db7
PZ
334 (struct usb_descriptor_header *) &ecm_control_intf,
335 (struct usb_descriptor_header *) &ecm_header_desc,
336 (struct usb_descriptor_header *) &ecm_union_desc,
337 (struct usb_descriptor_header *) &ecm_desc,
338
339 /* NOTE: status endpoint might need to be removed */
340 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
341 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
342
343 /* data interface, altsettings 0 and 1 */
344 (struct usb_descriptor_header *) &ecm_data_nop_intf,
345 (struct usb_descriptor_header *) &ecm_data_intf,
346 (struct usb_descriptor_header *) &ss_ecm_in_desc,
347 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
348 (struct usb_descriptor_header *) &ss_ecm_out_desc,
349 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
350 NULL,
351};
352
da741b8c
DB
353/* string descriptors: */
354
355static struct usb_string ecm_string_defs[] = {
356 [0].s = "CDC Ethernet Control Model (ECM)",
1616e99d 357 [1].s = "",
da741b8c 358 [2].s = "CDC Ethernet Data",
d11519ad 359 [3].s = "CDC ECM",
da741b8c
DB
360 { } /* end of list */
361};
362
363static struct usb_gadget_strings ecm_string_table = {
364 .language = 0x0409, /* en-us */
365 .strings = ecm_string_defs,
366};
367
368static struct usb_gadget_strings *ecm_strings[] = {
369 &ecm_string_table,
370 NULL,
371};
372
373/*-------------------------------------------------------------------------*/
374
375static void ecm_do_notify(struct f_ecm *ecm)
376{
377 struct usb_request *req = ecm->notify_req;
378 struct usb_cdc_notification *event;
379 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
380 __le32 *data;
381 int status;
382
383 /* notification already in flight? */
c14827fe 384 if (atomic_read(&ecm->notify_count))
da741b8c
DB
385 return;
386
387 event = req->buf;
388 switch (ecm->notify_state) {
389 case ECM_NOTIFY_NONE:
390 return;
391
392 case ECM_NOTIFY_CONNECT:
393 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
394 if (ecm->is_open)
395 event->wValue = cpu_to_le16(1);
396 else
397 event->wValue = cpu_to_le16(0);
398 event->wLength = 0;
399 req->length = sizeof *event;
400
401 DBG(cdev, "notify connect %s\n",
402 ecm->is_open ? "true" : "false");
403 ecm->notify_state = ECM_NOTIFY_SPEED;
404 break;
405
406 case ECM_NOTIFY_SPEED:
407 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
408 event->wValue = cpu_to_le16(0);
409 event->wLength = cpu_to_le16(8);
33376c1c 410 req->length = ECM_STATUS_BYTECOUNT;
da741b8c
DB
411
412 /* SPEED_CHANGE data is up/down speeds in bits/sec */
413 data = req->buf + sizeof *event;
33376c1c 414 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
da741b8c
DB
415 data[1] = data[0];
416
33376c1c 417 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
da741b8c
DB
418 ecm->notify_state = ECM_NOTIFY_NONE;
419 break;
420 }
421 event->bmRequestType = 0xA1;
422 event->wIndex = cpu_to_le16(ecm->ctrl_id);
423
c14827fe 424 atomic_inc(&ecm->notify_count);
da741b8c
DB
425 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
426 if (status < 0) {
c14827fe 427 atomic_dec(&ecm->notify_count);
da741b8c
DB
428 DBG(cdev, "notify --> %d\n", status);
429 }
430}
431
432static void ecm_notify(struct f_ecm *ecm)
433{
434 /* NOTE on most versions of Linux, host side cdc-ethernet
435 * won't listen for notifications until its netdevice opens.
436 * The first notification then sits in the FIFO for a long
437 * time, and the second one is queued.
438 */
439 ecm->notify_state = ECM_NOTIFY_CONNECT;
440 ecm_do_notify(ecm);
441}
442
443static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
444{
445 struct f_ecm *ecm = req->context;
446 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
447 struct usb_cdc_notification *event = req->buf;
448
449 switch (req->status) {
450 case 0:
451 /* no fault */
c14827fe 452 atomic_dec(&ecm->notify_count);
da741b8c
DB
453 break;
454 case -ECONNRESET:
455 case -ESHUTDOWN:
c14827fe 456 atomic_set(&ecm->notify_count, 0);
da741b8c
DB
457 ecm->notify_state = ECM_NOTIFY_NONE;
458 break;
459 default:
460 DBG(cdev, "event %02x --> %d\n",
461 event->bNotificationType, req->status);
c14827fe 462 atomic_dec(&ecm->notify_count);
da741b8c
DB
463 break;
464 }
da741b8c
DB
465 ecm_do_notify(ecm);
466}
467
468static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
469{
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);
477
478 /* composite driver infrastructure handles everything except
479 * CDC class messages; interface activation uses set_alt().
480 */
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
486 */
487 if (w_length != 0 || w_index != ecm->ctrl_id)
488 goto invalid;
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...
493 */
494 ecm->port.cdc_filter = w_value;
495 value = 0;
496 break;
497
498 /* and optionally:
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:
505 */
506
507 default:
508invalid:
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);
512 }
513
514 /* respond with data transfer or status phase? */
515 if (value >= 0) {
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);
519 req->zero = 0;
520 req->length = value;
521 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
522 if (value < 0)
523 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
524 ctrl->bRequestType, ctrl->bRequest,
525 value);
526 }
527
528 /* device either stalls (value < 0) or reports success */
529 return value;
530}
531
532
533static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
534{
535 struct f_ecm *ecm = func_to_ecm(f);
536 struct usb_composite_dev *cdev = f->config->cdev;
537
538 /* Control interface has only altsetting 0 */
539 if (intf == ecm->ctrl_id) {
540 if (alt != 0)
541 goto fail;
542
92fbfc38
RB
543 VDBG(cdev, "reset ecm control %d\n", intf);
544 usb_ep_disable(ecm->notify);
ea2a1df7 545 if (!(ecm->notify->desc)) {
da741b8c 546 VDBG(cdev, "init ecm ctrl %d\n", intf);
ea2a1df7
TB
547 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
548 goto fail;
da741b8c 549 }
72c973dd 550 usb_ep_enable(ecm->notify);
da741b8c
DB
551
552 /* Data interface has two altsettings, 0 and 1 */
553 } else if (intf == ecm->data_id) {
554 if (alt > 1)
555 goto fail;
556
92fbfc38 557 if (ecm->port.in_ep->enabled) {
da741b8c
DB
558 DBG(cdev, "reset ecm\n");
559 gether_disconnect(&ecm->port);
560 }
561
ea2a1df7
TB
562 if (!ecm->port.in_ep->desc ||
563 !ecm->port.out_ep->desc) {
da741b8c 564 DBG(cdev, "init ecm\n");
ea2a1df7
TB
565 if (config_ep_by_speed(cdev->gadget, f,
566 ecm->port.in_ep) ||
567 config_ep_by_speed(cdev->gadget, f,
568 ecm->port.out_ep)) {
569 ecm->port.in_ep->desc = NULL;
570 ecm->port.out_ep->desc = NULL;
571 goto fail;
572 }
da741b8c
DB
573 }
574
575 /* CDC Ethernet only sends data in non-default altsettings.
576 * Changing altsettings resets filters, statistics, etc.
577 */
578 if (alt == 1) {
579 struct net_device *net;
580
581 /* Enable zlps by default for ECM conformance;
90f79768 582 * override for musb_hdrc (avoids txdma ovhead).
da741b8c 583 */
7a896d40
RB
584 ecm->port.is_zlp_ok =
585 gadget_is_zlp_supported(cdev->gadget);
da741b8c
DB
586 ecm->port.cdc_filter = DEFAULT_FILTER;
587 DBG(cdev, "activate ecm\n");
588 net = gether_connect(&ecm->port);
589 if (IS_ERR(net))
590 return PTR_ERR(net);
591 }
592
593 /* NOTE this can be a minor disagreement with the ECM spec,
594 * which says speed notifications will "always" follow
595 * connection notifications. But we allow one connect to
596 * follow another (if the first is in flight), and instead
597 * just guarantee that a speed notification is always sent.
598 */
599 ecm_notify(ecm);
600 } else
601 goto fail;
602
603 return 0;
604fail:
605 return -EINVAL;
606}
607
608/* Because the data interface supports multiple altsettings,
609 * this ECM function *MUST* implement a get_alt() method.
610 */
611static int ecm_get_alt(struct usb_function *f, unsigned intf)
612{
613 struct f_ecm *ecm = func_to_ecm(f);
614
615 if (intf == ecm->ctrl_id)
616 return 0;
92fbfc38 617 return ecm->port.in_ep->enabled ? 1 : 0;
da741b8c
DB
618}
619
620static void ecm_disable(struct usb_function *f)
621{
622 struct f_ecm *ecm = func_to_ecm(f);
623 struct usb_composite_dev *cdev = f->config->cdev;
624
625 DBG(cdev, "ecm deactivated\n");
626
3dd46bdc 627 if (ecm->port.in_ep->enabled) {
da741b8c 628 gether_disconnect(&ecm->port);
3dd46bdc
EH
629 } else {
630 ecm->port.in_ep->desc = NULL;
631 ecm->port.out_ep->desc = NULL;
632 }
da741b8c 633
92fbfc38
RB
634 usb_ep_disable(ecm->notify);
635 ecm->notify->desc = NULL;
da741b8c
DB
636}
637
638/*-------------------------------------------------------------------------*/
639
640/*
641 * Callbacks let us notify the host about connect/disconnect when the
642 * net device is opened or closed.
643 *
644 * For testing, note that link states on this side include both opened
645 * and closed variants of:
646 *
647 * - disconnected/unconfigured
648 * - configured but inactive (data alt 0)
649 * - configured and active (data alt 1)
650 *
651 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
652 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
653 * imply the host is actually polling the notification endpoint, and
654 * likewise that "active" doesn't imply it's actually using the data
655 * endpoints for traffic.
656 */
657
658static void ecm_open(struct gether *geth)
659{
660 struct f_ecm *ecm = func_to_ecm(&geth->func);
661
662 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
663
664 ecm->is_open = true;
665 ecm_notify(ecm);
666}
667
668static void ecm_close(struct gether *geth)
669{
670 struct f_ecm *ecm = func_to_ecm(&geth->func);
671
672 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
673
674 ecm->is_open = false;
675 ecm_notify(ecm);
676}
677
678/*-------------------------------------------------------------------------*/
679
680/* ethernet function driver setup/binding */
681
28824b18 682static int
da741b8c
DB
683ecm_bind(struct usb_configuration *c, struct usb_function *f)
684{
685 struct usb_composite_dev *cdev = c->cdev;
686 struct f_ecm *ecm = func_to_ecm(f);
06b72598 687 struct usb_string *us;
da741b8c
DB
688 int status;
689 struct usb_ep *ep;
690
fee562a6
AP
691 struct f_ecm_opts *ecm_opts;
692
693 if (!can_support_ecm(cdev->gadget))
694 return -EINVAL;
695
696 ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
697
698 /*
699 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
700 * configurations are bound in sequence with list_for_each_entry,
701 * in each configuration its functions are bound in sequence
702 * with list_for_each_entry, so we assume no race condition
703 * with regard to ecm_opts->bound access
704 */
705 if (!ecm_opts->bound) {
da92801c 706 mutex_lock(&ecm_opts->lock);
fee562a6
AP
707 gether_set_gadget(ecm_opts->net, cdev->gadget);
708 status = gether_register_netdev(ecm_opts->net);
da92801c 709 mutex_unlock(&ecm_opts->lock);
fee562a6
AP
710 if (status)
711 return status;
712 ecm_opts->bound = true;
713 }
f1a2ca2e 714
06b72598
AP
715 us = usb_gstrings_attach(cdev, ecm_strings,
716 ARRAY_SIZE(ecm_string_defs));
717 if (IS_ERR(us))
718 return PTR_ERR(us);
719 ecm_control_intf.iInterface = us[0].id;
720 ecm_data_intf.iInterface = us[2].id;
721 ecm_desc.iMACAddress = us[1].id;
722 ecm_iad_descriptor.iFunction = us[3].id;
fee562a6 723
da741b8c
DB
724 /* allocate instance-specific interface IDs */
725 status = usb_interface_id(c, f);
726 if (status < 0)
727 goto fail;
728 ecm->ctrl_id = status;
d11519ad 729 ecm_iad_descriptor.bFirstInterface = status;
da741b8c
DB
730
731 ecm_control_intf.bInterfaceNumber = status;
732 ecm_union_desc.bMasterInterface0 = status;
733
734 status = usb_interface_id(c, f);
735 if (status < 0)
736 goto fail;
737 ecm->data_id = status;
738
739 ecm_data_nop_intf.bInterfaceNumber = status;
740 ecm_data_intf.bInterfaceNumber = status;
741 ecm_union_desc.bSlaveInterface0 = status;
742
743 status = -ENODEV;
744
745 /* allocate instance-specific endpoints */
33376c1c 746 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
da741b8c
DB
747 if (!ep)
748 goto fail;
749 ecm->port.in_ep = ep;
da741b8c 750
33376c1c 751 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
da741b8c
DB
752 if (!ep)
753 goto fail;
754 ecm->port.out_ep = ep;
da741b8c
DB
755
756 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
757 * don't treat it that way. It's simpler, and some newer CDC
758 * profiles (wireless handsets) no longer treat it as optional.
759 */
33376c1c 760 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
da741b8c
DB
761 if (!ep)
762 goto fail;
763 ecm->notify = ep;
da741b8c
DB
764
765 status = -ENOMEM;
766
767 /* allocate notification request and buffer */
768 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
769 if (!ecm->notify_req)
770 goto fail;
33376c1c 771 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
da741b8c
DB
772 if (!ecm->notify_req->buf)
773 goto fail;
774 ecm->notify_req->context = ecm;
775 ecm->notify_req->complete = ecm_notify_complete;
776
da741b8c
DB
777 /* support all relevant hardware speeds... we expect that when
778 * hardware is dual speed, all bulk-capable endpoints work at
779 * both speeds
780 */
10287bae
SAS
781 hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
782 hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
783 hs_ecm_notify_desc.bEndpointAddress =
784 fs_ecm_notify_desc.bEndpointAddress;
785
786 ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
787 ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
788 ss_ecm_notify_desc.bEndpointAddress =
789 fs_ecm_notify_desc.bEndpointAddress;
790
791 status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
eaef50c7 792 ecm_ss_function, NULL);
10287bae
SAS
793 if (status)
794 goto fail;
04617db7 795
da741b8c
DB
796 /* NOTE: all that is done without knowing or caring about
797 * the network link ... which is unavailable to this code
798 * until we're activated via set_alt().
799 */
800
801 ecm->port.open = ecm_open;
802 ecm->port.close = ecm_close;
803
804 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
04617db7 805 gadget_is_superspeed(c->cdev->gadget) ? "super" :
da741b8c
DB
806 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
807 ecm->port.in_ep->name, ecm->port.out_ep->name,
808 ecm->notify->name);
809 return 0;
810
811fail:
da741b8c
DB
812 if (ecm->notify_req) {
813 kfree(ecm->notify_req->buf);
814 usb_ep_free_request(ecm->notify, ecm->notify_req);
815 }
816
da741b8c
DB
817 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
818
819 return status;
820}
821
da92801c
AP
822static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
823{
824 return container_of(to_config_group(item), struct f_ecm_opts,
825 func_inst.group);
826}
827
828/* f_ecm_item_ops */
829USB_ETHERNET_CONFIGFS_ITEM(ecm);
830
831/* f_ecm_opts_dev_addr */
832USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);
833
834/* f_ecm_opts_host_addr */
835USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);
836
837/* f_ecm_opts_qmult */
838USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);
839
840/* f_ecm_opts_ifname */
841USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);
842
843static struct configfs_attribute *ecm_attrs[] = {
f9a63da3
CH
844 &ecm_opts_attr_dev_addr,
845 &ecm_opts_attr_host_addr,
846 &ecm_opts_attr_qmult,
847 &ecm_opts_attr_ifname,
da92801c
AP
848 NULL,
849};
850
97363902 851static const struct config_item_type ecm_func_type = {
da92801c
AP
852 .ct_item_ops = &ecm_item_ops,
853 .ct_attrs = ecm_attrs,
854 .ct_owner = THIS_MODULE,
855};
856
fee562a6
AP
857static void ecm_free_inst(struct usb_function_instance *f)
858{
859 struct f_ecm_opts *opts;
860
861 opts = container_of(f, struct f_ecm_opts, func_inst);
862 if (opts->bound)
863 gether_cleanup(netdev_priv(opts->net));
864 else
865 free_netdev(opts->net);
866 kfree(opts);
867}
868
869static struct usb_function_instance *ecm_alloc_inst(void)
870{
871 struct f_ecm_opts *opts;
872
873 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
874 if (!opts)
875 return ERR_PTR(-ENOMEM);
da92801c 876 mutex_init(&opts->lock);
fee562a6
AP
877 opts->func_inst.free_func_inst = ecm_free_inst;
878 opts->net = gether_setup_default();
172d934c
AP
879 if (IS_ERR(opts->net)) {
880 struct net_device *net = opts->net;
881 kfree(opts);
882 return ERR_CAST(net);
883 }
fee562a6 884
da92801c
AP
885 config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);
886
fee562a6
AP
887 return &opts->func_inst;
888}
889
890static void ecm_free(struct usb_function *f)
891{
892 struct f_ecm *ecm;
da92801c 893 struct f_ecm_opts *opts;
fee562a6
AP
894
895 ecm = func_to_ecm(f);
da92801c 896 opts = container_of(f->fi, struct f_ecm_opts, func_inst);
fee562a6 897 kfree(ecm);
da92801c
AP
898 mutex_lock(&opts->lock);
899 opts->refcnt--;
900 mutex_unlock(&opts->lock);
fee562a6
AP
901}
902
903static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
904{
905 struct f_ecm *ecm = func_to_ecm(f);
906
907 DBG(c->cdev, "ecm unbind\n");
908
fee562a6
AP
909 usb_free_all_descriptors(f);
910
c14827fe
BD
911 if (atomic_read(&ecm->notify_count)) {
912 usb_ep_dequeue(ecm->notify, ecm->notify_req);
913 atomic_set(&ecm->notify_count, 0);
914 }
915
fee562a6
AP
916 kfree(ecm->notify_req->buf);
917 usb_ep_free_request(ecm->notify, ecm->notify_req);
918}
919
0f0392f2 920static struct usb_function *ecm_alloc(struct usb_function_instance *fi)
fee562a6
AP
921{
922 struct f_ecm *ecm;
923 struct f_ecm_opts *opts;
924 int status;
925
926 /* allocate and initialize one new instance */
927 ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
928 if (!ecm)
929 return ERR_PTR(-ENOMEM);
930
931 opts = container_of(fi, struct f_ecm_opts, func_inst);
da92801c
AP
932 mutex_lock(&opts->lock);
933 opts->refcnt++;
fee562a6
AP
934
935 /* export host's Ethernet address in CDC format */
936 status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
937 sizeof(ecm->ethaddr));
938 if (status < 12) {
939 kfree(ecm);
2a98dbbc 940 mutex_unlock(&opts->lock);
fee562a6
AP
941 return ERR_PTR(-EINVAL);
942 }
943 ecm_string_defs[1].s = ecm->ethaddr;
944
945 ecm->port.ioport = netdev_priv(opts->net);
da92801c 946 mutex_unlock(&opts->lock);
fee562a6
AP
947 ecm->port.cdc_filter = DEFAULT_FILTER;
948
949 ecm->port.func.name = "cdc_ethernet";
fee562a6
AP
950 /* descriptors are per-instance copies */
951 ecm->port.func.bind = ecm_bind;
952 ecm->port.func.unbind = ecm_unbind;
953 ecm->port.func.set_alt = ecm_set_alt;
954 ecm->port.func.get_alt = ecm_get_alt;
955 ecm->port.func.setup = ecm_setup;
956 ecm->port.func.disable = ecm_disable;
957 ecm->port.func.free_func = ecm_free;
958
959 return &ecm->port.func;
960}
961
962DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
963MODULE_LICENSE("GPL");
964MODULE_AUTHOR("David Brownell");