blob: 98a29ae0c877ba8d20b846836c316a9af2065ee1 [file] [log] [blame]
Ofir Cohen7b155422012-07-31 13:02:49 +03001/*
2 * f_qc_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 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 and
10 * only version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/* #define VERBOSE_DEBUG */
23
24#include <linux/slab.h>
25#include <linux/kernel.h>
26#include <linux/device.h>
27#include <linux/etherdevice.h>
28
29#include "u_ether.h"
30#include "u_qc_ether.h"
31
32
33/*
34 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
35 * Ethernet link. The data transfer model is simple (packets sent and
36 * received over bulk endpoints using normal short packet termination),
37 * and the control model exposes various data and optional notifications.
38 *
39 * ECM is well standardized and (except for Microsoft) supported by most
40 * operating systems with USB host support. It's the preferred interop
41 * solution for Ethernet over USB, at least for firmware based solutions.
42 * (Hardware solutions tend to be more minimalist.) A newer and simpler
43 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
44 *
45 * Note that ECM requires the use of "alternate settings" for its data
46 * interface. This means that the set_alt() method has real work to do,
47 * and also means that a get_alt() method is required.
48 *
49 * This function is based on USB CDC Ethernet link function driver and
50 * contains MSM specific implementation.
51 */
52
53
54enum ecm_qc_notify_state {
55 ECM_QC_NOTIFY_NONE, /* don't notify */
56 ECM_QC_NOTIFY_CONNECT, /* issue CONNECT next */
57 ECM_QC_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
58};
59
60struct f_ecm_qc {
61 struct qc_gether port;
62 u8 ctrl_id, data_id;
63
64 char ethaddr[14];
65
66 struct usb_ep *notify;
67 struct usb_request *notify_req;
68 u8 notify_state;
69 bool is_open;
70};
71
72static inline struct f_ecm_qc *func_to_ecm_qc(struct usb_function *f)
73{
74 return container_of(f, struct f_ecm_qc, port.func);
75}
76
77/* peak (theoretical) bulk transfer rate in bits-per-second */
78static inline unsigned ecm_qc_bitrate(struct usb_gadget *g)
79{
80 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
81 return 13 * 512 * 8 * 1000 * 8;
82 else
83 return 19 * 64 * 1 * 1000 * 8;
84}
85
86/*-------------------------------------------------------------------------*/
87
88/*
89 * Include the status endpoint if we can, even though it's optional.
90 *
91 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
92 * packet, to simplify cancellation; and a big transfer interval, to
93 * waste less bandwidth.
94 *
95 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
96 * if they ignore the connect/disconnect notifications that real aether
97 * can provide. More advanced cdc configurations might want to support
98 * encapsulated commands (vendor-specific, using control-OUT).
99 */
100
101#define ECM_QC_LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
102#define ECM_QC_STATUS_BYTECOUNT 16 /* 8 byte header + data */
103
104/* currently only one std ecm instance is supported */
105#define ECM_QC_NO_PORTS 1
106
107/* interface descriptor: */
108
109static struct usb_interface_descriptor ecm_qc_control_intf = {
110 .bLength = sizeof ecm_qc_control_intf,
111 .bDescriptorType = USB_DT_INTERFACE,
112
113 /* .bInterfaceNumber = DYNAMIC */
114 /* status endpoint is optional; this could be patched later */
115 .bNumEndpoints = 1,
116 .bInterfaceClass = USB_CLASS_COMM,
117 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
118 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
119 /* .iInterface = DYNAMIC */
120};
121
122static struct usb_cdc_header_desc ecm_qc_header_desc = {
123 .bLength = sizeof ecm_qc_header_desc,
124 .bDescriptorType = USB_DT_CS_INTERFACE,
125 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
126
127 .bcdCDC = cpu_to_le16(0x0110),
128};
129
130static struct usb_cdc_union_desc ecm_qc_union_desc = {
131 .bLength = sizeof(ecm_qc_union_desc),
132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_UNION_TYPE,
134 /* .bMasterInterface0 = DYNAMIC */
135 /* .bSlaveInterface0 = DYNAMIC */
136};
137
138static struct usb_cdc_ether_desc ecm_qc_desc = {
139 .bLength = sizeof ecm_qc_desc,
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
142
143 /* this descriptor actually adds value, surprise! */
144 /* .iMACAddress = DYNAMIC */
145 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
146 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
147 .wNumberMCFilters = cpu_to_le16(0),
148 .bNumberPowerFilters = 0,
149};
150
151/* the default data interface has no endpoints ... */
152
153static struct usb_interface_descriptor ecm_qc_data_nop_intf = {
154 .bLength = sizeof ecm_qc_data_nop_intf,
155 .bDescriptorType = USB_DT_INTERFACE,
156
157 .bInterfaceNumber = 1,
158 .bAlternateSetting = 0,
159 .bNumEndpoints = 0,
160 .bInterfaceClass = USB_CLASS_CDC_DATA,
161 .bInterfaceSubClass = 0,
162 .bInterfaceProtocol = 0,
163 /* .iInterface = DYNAMIC */
164};
165
166/* ... but the "real" data interface has two bulk endpoints */
167
168static struct usb_interface_descriptor ecm_qc_data_intf = {
169 .bLength = sizeof ecm_qc_data_intf,
170 .bDescriptorType = USB_DT_INTERFACE,
171
172 .bInterfaceNumber = 1,
173 .bAlternateSetting = 1,
174 .bNumEndpoints = 2,
175 .bInterfaceClass = USB_CLASS_CDC_DATA,
176 .bInterfaceSubClass = 0,
177 .bInterfaceProtocol = 0,
178 /* .iInterface = DYNAMIC */
179};
180
181/* full speed support: */
182
183static struct usb_endpoint_descriptor ecm_qc_fs_notify_desc = {
184 .bLength = USB_DT_ENDPOINT_SIZE,
185 .bDescriptorType = USB_DT_ENDPOINT,
186
187 .bEndpointAddress = USB_DIR_IN,
188 .bmAttributes = USB_ENDPOINT_XFER_INT,
189 .wMaxPacketSize = cpu_to_le16(ECM_QC_STATUS_BYTECOUNT),
190 .bInterval = 1 << ECM_QC_LOG2_STATUS_INTERVAL_MSEC,
191};
192
193static struct usb_endpoint_descriptor ecm_qc_fs_in_desc = {
194 .bLength = USB_DT_ENDPOINT_SIZE,
195 .bDescriptorType = USB_DT_ENDPOINT,
196
197 .bEndpointAddress = USB_DIR_IN,
198 .bmAttributes = USB_ENDPOINT_XFER_BULK,
199};
200
201static struct usb_endpoint_descriptor ecm_qc_fs_out_desc = {
202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
204
205 .bEndpointAddress = USB_DIR_OUT,
206 .bmAttributes = USB_ENDPOINT_XFER_BULK,
207};
208
209static struct usb_descriptor_header *ecm_qc_fs_function[] = {
210 /* CDC ECM control descriptors */
211 (struct usb_descriptor_header *) &ecm_qc_control_intf,
212 (struct usb_descriptor_header *) &ecm_qc_header_desc,
213 (struct usb_descriptor_header *) &ecm_qc_union_desc,
214 (struct usb_descriptor_header *) &ecm_qc_desc,
215 /* NOTE: status endpoint might need to be removed */
216 (struct usb_descriptor_header *) &ecm_qc_fs_notify_desc,
217 /* data interface, altsettings 0 and 1 */
218 (struct usb_descriptor_header *) &ecm_qc_data_nop_intf,
219 (struct usb_descriptor_header *) &ecm_qc_data_intf,
220 (struct usb_descriptor_header *) &ecm_qc_fs_in_desc,
221 (struct usb_descriptor_header *) &ecm_qc_fs_out_desc,
222 NULL,
223};
224
225/* high speed support: */
226
227static struct usb_endpoint_descriptor ecm_qc_hs_notify_desc = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
230
231 .bEndpointAddress = USB_DIR_IN,
232 .bmAttributes = USB_ENDPOINT_XFER_INT,
233 .wMaxPacketSize = cpu_to_le16(ECM_QC_STATUS_BYTECOUNT),
234 .bInterval = ECM_QC_LOG2_STATUS_INTERVAL_MSEC + 4,
235};
236static struct usb_endpoint_descriptor ecm_qc_hs_in_desc = {
237 .bLength = USB_DT_ENDPOINT_SIZE,
238 .bDescriptorType = USB_DT_ENDPOINT,
239
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_XFER_BULK,
242 .wMaxPacketSize = cpu_to_le16(512),
243};
244
245static struct usb_endpoint_descriptor ecm_qc_hs_out_desc = {
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_OUT,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
251 .wMaxPacketSize = cpu_to_le16(512),
252};
253
254static struct usb_descriptor_header *ecm_qc_hs_function[] = {
255 /* CDC ECM control descriptors */
256 (struct usb_descriptor_header *) &ecm_qc_control_intf,
257 (struct usb_descriptor_header *) &ecm_qc_header_desc,
258 (struct usb_descriptor_header *) &ecm_qc_union_desc,
259 (struct usb_descriptor_header *) &ecm_qc_desc,
260 /* NOTE: status endpoint might need to be removed */
261 (struct usb_descriptor_header *) &ecm_qc_hs_notify_desc,
262 /* data interface, altsettings 0 and 1 */
263 (struct usb_descriptor_header *) &ecm_qc_data_nop_intf,
264 (struct usb_descriptor_header *) &ecm_qc_data_intf,
265 (struct usb_descriptor_header *) &ecm_qc_hs_in_desc,
266 (struct usb_descriptor_header *) &ecm_qc_hs_out_desc,
267 NULL,
268};
269
270/* string descriptors: */
271
272static struct usb_string ecm_qc_string_defs[] = {
273 [0].s = "CDC Ethernet Control Model (ECM)",
274 [1].s = NULL /* DYNAMIC */,
275 [2].s = "CDC Ethernet Data",
276 { } /* end of list */
277};
278
279static struct usb_gadget_strings ecm_qc_string_table = {
280 .language = 0x0409, /* en-us */
281 .strings = ecm_qc_string_defs,
282};
283
284static struct usb_gadget_strings *ecm_qc_strings[] = {
285 &ecm_qc_string_table,
286 NULL,
287};
288
289static struct data_port ecm_qc_bam_port;
290
291static int ecm_qc_bam_setup(void)
292{
293 int ret;
294
295 ret = bam_data_setup(ECM_QC_NO_PORTS);
296 if (ret) {
297 pr_err("bam_data_setup failed err: %d\n", ret);
298 return ret;
299 }
300
301 return 0;
302}
303
304static int ecm_qc_bam_connect(struct f_ecm_qc *dev)
305{
306 int ret;
307
308 ecm_qc_bam_port.func = dev->port.func;
309 ecm_qc_bam_port.in = dev->port.in_ep;
310 ecm_qc_bam_port.out = dev->port.out_ep;
311
312 /* currently we use the first connection */
313 ret = bam_data_connect(&ecm_qc_bam_port, 0, 0);
314 if (ret) {
315 pr_err("bam_data_connect failed: err:%d\n",
316 ret);
317 return ret;
318 } else {
319 pr_info("ecm bam connected\n");
320 }
321
322 return 0;
323}
324
325static int ecm_qc_bam_disconnect(struct f_ecm_qc *dev)
326{
327 pr_debug("dev:%p. %s Do nothing.\n",
328 dev, __func__);
329
330 return 0;
331}
332
333/*-------------------------------------------------------------------------*/
334
335static void ecm_qc_do_notify(struct f_ecm_qc *ecm)
336{
337 struct usb_request *req = ecm->notify_req;
338 struct usb_cdc_notification *event;
339 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
340 __le32 *data;
341 int status;
342
343 /* notification already in flight? */
344 if (!req)
345 return;
346
347 event = req->buf;
348 switch (ecm->notify_state) {
349 case ECM_QC_NOTIFY_NONE:
350 return;
351
352 case ECM_QC_NOTIFY_CONNECT:
353 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
354 if (ecm->is_open)
355 event->wValue = cpu_to_le16(1);
356 else
357 event->wValue = cpu_to_le16(0);
358 event->wLength = 0;
359 req->length = sizeof *event;
360
361 DBG(cdev, "notify connect %s\n",
362 ecm->is_open ? "true" : "false");
363 ecm->notify_state = ECM_QC_NOTIFY_SPEED;
364 break;
365
366 case ECM_QC_NOTIFY_SPEED:
367 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
368 event->wValue = cpu_to_le16(0);
369 event->wLength = cpu_to_le16(8);
370 req->length = ECM_QC_STATUS_BYTECOUNT;
371
372 /* SPEED_CHANGE data is up/down speeds in bits/sec */
373 data = req->buf + sizeof *event;
374 data[0] = cpu_to_le32(ecm_qc_bitrate(cdev->gadget));
375 data[1] = data[0];
376
377 DBG(cdev, "notify speed %d\n", ecm_qc_bitrate(cdev->gadget));
378 ecm->notify_state = ECM_QC_NOTIFY_NONE;
379 break;
380 }
381 event->bmRequestType = 0xA1;
382 event->wIndex = cpu_to_le16(ecm->ctrl_id);
383
384 ecm->notify_req = NULL;
385 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
386 if (status < 0) {
387 ecm->notify_req = req;
388 DBG(cdev, "notify --> %d\n", status);
389 }
390}
391
392static void ecm_qc_notify(struct f_ecm_qc *ecm)
393{
394 /* NOTE on most versions of Linux, host side cdc-ethernet
395 * won't listen for notifications until its netdevice opens.
396 * The first notification then sits in the FIFO for a long
397 * time, and the second one is queued.
398 */
399 ecm->notify_state = ECM_QC_NOTIFY_CONNECT;
400 ecm_qc_do_notify(ecm);
401}
402
403static void ecm_qc_notify_complete(struct usb_ep *ep, struct usb_request *req)
404{
405 struct f_ecm_qc *ecm = req->context;
406
407 switch (req->status) {
408 case 0:
409 /* no fault */
410 break;
411 case -ECONNRESET:
412 case -ESHUTDOWN:
413 ecm->notify_state = ECM_QC_NOTIFY_NONE;
414 break;
415 default:
416 DBG(cdev, "event %02x --> %d\n",
417 event->bNotificationType, req->status);
418 break;
419 }
420 ecm->notify_req = req;
421 ecm_qc_do_notify(ecm);
422}
423
424static int ecm_qc_setup(struct usb_function *f,
425 const struct usb_ctrlrequest *ctrl)
426{
427 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
428 struct usb_composite_dev *cdev = f->config->cdev;
429 struct usb_request *req = cdev->req;
430 int value = -EOPNOTSUPP;
431 u16 w_index = le16_to_cpu(ctrl->wIndex);
432 u16 w_value = le16_to_cpu(ctrl->wValue);
433 u16 w_length = le16_to_cpu(ctrl->wLength);
434
435 /* composite driver infrastructure handles everything except
436 * CDC class messages; interface activation uses set_alt().
437 */
438 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
439 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
440 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
441 /* see 6.2.30: no data, wIndex = interface,
442 * wValue = packet filter bitmap
443 */
444 if (w_length != 0 || w_index != ecm->ctrl_id)
445 goto invalid;
446 DBG(cdev, "packet filter %02x\n", w_value);
447 /* REVISIT locking of cdc_filter. This assumes the UDC
448 * driver won't have a concurrent packet TX irq running on
449 * another CPU; or that if it does, this write is atomic...
450 */
451 ecm->port.cdc_filter = w_value;
452 value = 0;
453 break;
454
455 /* and optionally:
456 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
457 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
458 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
459 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
460 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
461 * case USB_CDC_GET_ETHERNET_STATISTIC:
462 */
463
464 default:
465invalid:
466 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
467 ctrl->bRequestType, ctrl->bRequest,
468 w_value, w_index, w_length);
469 }
470
471 /* respond with data transfer or status phase? */
472 if (value >= 0) {
473 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
474 ctrl->bRequestType, ctrl->bRequest,
475 w_value, w_index, w_length);
476 req->zero = 0;
477 req->length = value;
478 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
479 if (value < 0)
480 pr_err("ecm req %02x.%02x response err %d\n",
481 ctrl->bRequestType, ctrl->bRequest,
482 value);
483 }
484
485 /* device either stalls (value < 0) or reports success */
486 return value;
487}
488
489
490static int ecm_qc_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
491{
492 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
493 struct usb_composite_dev *cdev = f->config->cdev;
494
495 /* Control interface has only altsetting 0 */
496 if (intf == ecm->ctrl_id) {
497 if (alt != 0)
498 goto fail;
499
500 if (ecm->notify->driver_data) {
501 VDBG(cdev, "reset ecm control %d\n", intf);
502 usb_ep_disable(ecm->notify);
503 }
504 if (!(ecm->notify->desc)) {
505 VDBG(cdev, "init ecm ctrl %d\n", intf);
506 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
507 goto fail;
508 }
509 usb_ep_enable(ecm->notify);
510 ecm->notify->driver_data = ecm;
511
512 /* Data interface has two altsettings, 0 and 1 */
513 } else if (intf == ecm->data_id) {
514 if (alt > 1)
515 goto fail;
516
517 if (ecm->port.in_ep->driver_data) {
518 DBG(cdev, "reset ecm\n");
519 gether_qc_disconnect(&ecm->port);
520 ecm_qc_bam_disconnect(ecm);
521 }
522
523 if (!ecm->port.in_ep->desc ||
524 !ecm->port.out_ep->desc) {
525 DBG(cdev, "init ecm\n");
526 if (config_ep_by_speed(cdev->gadget, f,
527 ecm->port.in_ep) ||
528 config_ep_by_speed(cdev->gadget, f,
529 ecm->port.out_ep)) {
530 ecm->port.in_ep->desc = NULL;
531 ecm->port.out_ep->desc = NULL;
532 goto fail;
533 }
534 }
535
536 /* CDC Ethernet only sends data in non-default altsettings.
537 * Changing altsettings resets filters, statistics, etc.
538 */
539 if (alt == 1) {
540 struct net_device *net;
541
542 /* Enable zlps by default for ECM conformance;
543 * override for musb_hdrc (avoids txdma ovhead).
544 */
545 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
546 );
547 ecm->port.cdc_filter = DEFAULT_FILTER;
548 DBG(cdev, "activate ecm\n");
549 net = gether_qc_connect(&ecm->port);
550 if (IS_ERR(net))
551 return PTR_ERR(net);
552
553 if (ecm_qc_bam_connect(ecm))
554 goto fail;
555 }
556
557 /* NOTE this can be a minor disagreement with the ECM spec,
558 * which says speed notifications will "always" follow
559 * connection notifications. But we allow one connect to
560 * follow another (if the first is in flight), and instead
561 * just guarantee that a speed notification is always sent.
562 */
563 ecm_qc_notify(ecm);
564 } else
565 goto fail;
566
567 return 0;
568fail:
569 return -EINVAL;
570}
571
572/* Because the data interface supports multiple altsettings,
573 * this ECM function *MUST* implement a get_alt() method.
574 */
575static int ecm_qc_get_alt(struct usb_function *f, unsigned intf)
576{
577 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
578
579 if (intf == ecm->ctrl_id)
580 return 0;
581 return ecm->port.in_ep->driver_data ? 1 : 0;
582}
583
584static void ecm_qc_disable(struct usb_function *f)
585{
586 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
587
588 DBG(cdev, "ecm deactivated\n");
589
590 if (ecm->port.in_ep->driver_data) {
591 gether_qc_disconnect(&ecm->port);
592 ecm_qc_bam_disconnect(ecm);
593 }
594
595 if (ecm->notify->driver_data) {
596 usb_ep_disable(ecm->notify);
597 ecm->notify->driver_data = NULL;
598 ecm->notify->desc = NULL;
599 }
600}
601
602/*-------------------------------------------------------------------------*/
603
604/*
605 * Callbacks let us notify the host about connect/disconnect when the
606 * net device is opened or closed.
607 *
608 * For testing, note that link states on this side include both opened
609 * and closed variants of:
610 *
611 * - disconnected/unconfigured
612 * - configured but inactive (data alt 0)
613 * - configured and active (data alt 1)
614 *
615 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
616 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
617 * imply the host is actually polling the notification endpoint, and
618 * likewise that "active" doesn't imply it's actually using the data
619 * endpoints for traffic.
620 */
621
622static void ecm_qc_open(struct qc_gether *geth)
623{
624 struct f_ecm_qc *ecm = func_to_ecm_qc(&geth->func);
625 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
626
627 ecm->is_open = true;
628 ecm_qc_notify(ecm);
629}
630
631static void ecm_qc_close(struct qc_gether *geth)
632{
633 struct f_ecm_qc *ecm = func_to_ecm_qc(&geth->func);
634
635 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
636
637 ecm->is_open = false;
638 ecm_qc_notify(ecm);
639}
640
641/*-------------------------------------------------------------------------*/
642
643/* ethernet function driver setup/binding */
644
645static int
646ecm_qc_bind(struct usb_configuration *c, struct usb_function *f)
647{
648 struct usb_composite_dev *cdev = c->cdev;
649 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
650 int status;
651 struct usb_ep *ep;
652
653 /* allocate instance-specific interface IDs */
654 status = usb_interface_id(c, f);
655 if (status < 0)
656 goto fail;
657 ecm->ctrl_id = status;
658
659 ecm_qc_control_intf.bInterfaceNumber = status;
660 ecm_qc_union_desc.bMasterInterface0 = status;
661
662 status = usb_interface_id(c, f);
663 if (status < 0)
664 goto fail;
665 ecm->data_id = status;
666
667 ecm_qc_data_nop_intf.bInterfaceNumber = status;
668 ecm_qc_data_intf.bInterfaceNumber = status;
669 ecm_qc_union_desc.bSlaveInterface0 = status;
670
671 status = -ENODEV;
672
673 /* allocate instance-specific endpoints */
674 ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_in_desc);
675 if (!ep)
676 goto fail;
677
678 ecm->port.in_ep = ep;
679 ep->driver_data = cdev; /* claim */
680
681 ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_out_desc);
682 if (!ep)
683 goto fail;
684
685 ecm->port.out_ep = ep;
686 ep->driver_data = cdev; /* claim */
687
688 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
689 * don't treat it that way. It's simpler, and some newer CDC
690 * profiles (wireless handsets) no longer treat it as optional.
691 */
692 ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_notify_desc);
693 if (!ep)
694 goto fail;
695 ecm->notify = ep;
696 ep->driver_data = cdev; /* claim */
697
698 status = -ENOMEM;
699
700 /* allocate notification request and buffer */
701 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
702 if (!ecm->notify_req)
703 goto fail;
704 ecm->notify_req->buf = kmalloc(ECM_QC_STATUS_BYTECOUNT, GFP_KERNEL);
705 if (!ecm->notify_req->buf)
706 goto fail;
707 ecm->notify_req->context = ecm;
708 ecm->notify_req->complete = ecm_qc_notify_complete;
709
710 /* copy descriptors, and track endpoint copies */
711 f->descriptors = usb_copy_descriptors(ecm_qc_fs_function);
712 if (!f->descriptors)
713 goto fail;
714
715 /* support all relevant hardware speeds... we expect that when
716 * hardware is dual speed, all bulk-capable endpoints work at
717 * both speeds
718 */
719 if (gadget_is_dualspeed(c->cdev->gadget)) {
720 ecm_qc_hs_in_desc.bEndpointAddress =
721 ecm_qc_fs_in_desc.bEndpointAddress;
722 ecm_qc_hs_out_desc.bEndpointAddress =
723 ecm_qc_fs_out_desc.bEndpointAddress;
724 ecm_qc_hs_notify_desc.bEndpointAddress =
725 ecm_qc_fs_notify_desc.bEndpointAddress;
726
727 /* copy descriptors, and track endpoint copies */
728 f->hs_descriptors = usb_copy_descriptors(ecm_qc_hs_function);
729 if (!f->hs_descriptors)
730 goto fail;
731 }
732
733 /* NOTE: all that is done without knowing or caring about
734 * the network link ... which is unavailable to this code
735 * until we're activated via set_alt().
736 */
737
738 ecm->port.open = ecm_qc_open;
739 ecm->port.close = ecm_qc_close;
740
741 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
742 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
743 ecm->port.in_ep->name, ecm->port.out_ep->name,
744 ecm->notify->name);
745 return 0;
746
747fail:
748 if (f->descriptors)
749 usb_free_descriptors(f->descriptors);
750
751 if (ecm->notify_req) {
752 kfree(ecm->notify_req->buf);
753 usb_ep_free_request(ecm->notify, ecm->notify_req);
754 }
755
756 /* we might as well release our claims on endpoints */
757 if (ecm->notify)
758 ecm->notify->driver_data = NULL;
759 if (ecm->port.out_ep->desc)
760 ecm->port.out_ep->driver_data = NULL;
761 if (ecm->port.in_ep->desc)
762 ecm->port.in_ep->driver_data = NULL;
763
764 pr_err("%s: can't bind, err %d\n", f->name, status);
765
766 return status;
767}
768
769static void
770ecm_qc_unbind(struct usb_configuration *c, struct usb_function *f)
771{
772 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
773
774 DBG(c->cdev, "ecm unbind\n");
775
776 if (gadget_is_dualspeed(c->cdev->gadget))
777 usb_free_descriptors(f->hs_descriptors);
778 usb_free_descriptors(f->descriptors);
779
780 kfree(ecm->notify_req->buf);
781 usb_ep_free_request(ecm->notify, ecm->notify_req);
782
783 ecm_qc_string_defs[1].s = NULL;
784 kfree(ecm);
785}
786
787/**
788 * ecm_qc_bind_config - add CDC Ethernet network link to a configuration
789 * @c: the configuration to support the network link
790 * @ethaddr: a buffer in which the ethernet address of the host side
791 * side of the link was recorded
792 * Context: single threaded during gadget setup
793 *
794 * Returns zero on success, else negative errno.
795 *
796 * Caller must have called @gether_qc_setup(). Caller is also responsible
797 * for calling @gether_cleanup() before module unload.
798 */
799int
800ecm_qc_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
801{
802 struct f_ecm_qc *ecm;
803 int status;
804
805 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
806 return -EINVAL;
807
808 status = ecm_qc_bam_setup();
809 if (status) {
810 pr_err("bam setup failed");
811 return status;
812 }
813
814 /* maybe allocate device-global string IDs */
815 if (ecm_qc_string_defs[0].id == 0) {
816
817 /* control interface label */
818 status = usb_string_id(c->cdev);
819 if (status < 0)
820 return status;
821 ecm_qc_string_defs[0].id = status;
822 ecm_qc_control_intf.iInterface = status;
823
824 /* data interface label */
825 status = usb_string_id(c->cdev);
826 if (status < 0)
827 return status;
828 ecm_qc_string_defs[2].id = status;
829 ecm_qc_data_intf.iInterface = status;
830
831 /* MAC address */
832 status = usb_string_id(c->cdev);
833 if (status < 0)
834 return status;
835 ecm_qc_string_defs[1].id = status;
836 ecm_qc_desc.iMACAddress = status;
837 }
838
839 /* allocate and initialize one new instance */
840 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
841 if (!ecm)
842 return -ENOMEM;
843
844 /* export host's Ethernet address in CDC format */
845 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
846 "%02X%02X%02X%02X%02X%02X",
847 ethaddr[0], ethaddr[1], ethaddr[2],
848 ethaddr[3], ethaddr[4], ethaddr[5]);
849 ecm_qc_string_defs[1].s = ecm->ethaddr;
850
851 ecm->port.cdc_filter = DEFAULT_FILTER;
852
853 ecm->port.func.name = "cdc_ethernet";
854 ecm->port.func.strings = ecm_qc_strings;
855 /* descriptors are per-instance copies */
856 ecm->port.func.bind = ecm_qc_bind;
857 ecm->port.func.unbind = ecm_qc_unbind;
858 ecm->port.func.set_alt = ecm_qc_set_alt;
859 ecm->port.func.get_alt = ecm_qc_get_alt;
860 ecm->port.func.setup = ecm_qc_setup;
861 ecm->port.func.disable = ecm_qc_disable;
862
863 status = usb_add_function(c, &ecm->port.func);
864 if (status) {
865 ecm_qc_string_defs[1].s = NULL;
866 kfree(ecm);
867 }
868 return status;
869}