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