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