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