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