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