blob: 51f0e503d2950a887a987ea475b0effaeacd991a [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;
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200393 u8 src_connection_idx, dst_connection_idx;
394 struct usb_composite_dev *cdev = dev->port.func.config->cdev;
395 struct usb_gadget *gadget = cdev->gadget;
396 enum peer_bam peer_bam = (dev->xport == USB_GADGET_XPORT_BAM2BAM_IPA) ?
397 IPA_P_BAM : A2_P_BAM;
Amit Blayf9b352b2013-03-04 15:01:40 +0200398
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200399 ecm_qc_bam_port.cdev = cdev;
Amit Blayf9b352b2013-03-04 15:01:40 +0200400 ecm_qc_bam_port.in = dev->port.in_ep;
401 ecm_qc_bam_port.out = dev->port.out_ep;
402
403 /* currently we use the first connection */
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200404 src_connection_idx = usb_bam_get_connection_idx(gadget->name, peer_bam,
405 USB_TO_PEER_PERIPHERAL, 0);
406 dst_connection_idx = usb_bam_get_connection_idx(gadget->name, peer_bam,
407 PEER_PERIPHERAL_TO_USB, 0);
408 if (src_connection_idx < 0 || dst_connection_idx < 0) {
409 pr_err("%s: usb_bam_get_connection_idx failed\n", __func__);
410 return ret;
411 }
Amit Blayf9b352b2013-03-04 15:01:40 +0200412 ret = bam_data_connect(&ecm_qc_bam_port, 0, dev->xport,
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200413 src_connection_idx, dst_connection_idx, USB_FUNC_ECM);
Amit Blayf9b352b2013-03-04 15:01:40 +0200414 if (ret) {
415 pr_err("bam_data_connect failed: err:%d\n", ret);
416 return ret;
417 } else {
418 pr_debug("ecm bam connected\n");
419 }
420
421 dev->is_open = true;
422 ecm_qc_notify(dev);
423
424 return 0;
425}
426
427static int ecm_qc_bam_disconnect(struct f_ecm_qc *dev)
428{
429 pr_debug("dev:%p. Disconnect BAM.\n", dev);
430
431 bam_data_disconnect(&ecm_qc_bam_port, 0);
432
433 ecm_ipa_cleanup(ipa_params.ipa_priv);
434
435 return 0;
436}
437
438void *ecm_qc_get_ipa_rx_cb(void)
439{
440 return ipa_params.ipa_rx_cb;
441}
442
443void *ecm_qc_get_ipa_tx_cb(void)
444{
445 return ipa_params.ipa_tx_cb;
446}
447
448void *ecm_qc_get_ipa_priv(void)
449{
450 return ipa_params.ipa_priv;
451}
452
453/*-------------------------------------------------------------------------*/
454
455
456
Ofir Cohen7b155422012-07-31 13:02:49 +0300457static void ecm_qc_notify_complete(struct usb_ep *ep, struct usb_request *req)
458{
459 struct f_ecm_qc *ecm = req->context;
Jack Pham0ad82e62012-09-27 17:31:08 -0700460 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
461 struct usb_cdc_notification *event = req->buf;
Ofir Cohen7b155422012-07-31 13:02:49 +0300462
463 switch (req->status) {
464 case 0:
465 /* no fault */
466 break;
467 case -ECONNRESET:
468 case -ESHUTDOWN:
469 ecm->notify_state = ECM_QC_NOTIFY_NONE;
470 break;
471 default:
472 DBG(cdev, "event %02x --> %d\n",
473 event->bNotificationType, req->status);
474 break;
475 }
476 ecm->notify_req = req;
477 ecm_qc_do_notify(ecm);
478}
479
480static int ecm_qc_setup(struct usb_function *f,
481 const struct usb_ctrlrequest *ctrl)
482{
483 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
484 struct usb_composite_dev *cdev = f->config->cdev;
485 struct usb_request *req = cdev->req;
486 int value = -EOPNOTSUPP;
487 u16 w_index = le16_to_cpu(ctrl->wIndex);
488 u16 w_value = le16_to_cpu(ctrl->wValue);
489 u16 w_length = le16_to_cpu(ctrl->wLength);
490
491 /* composite driver infrastructure handles everything except
492 * CDC class messages; interface activation uses set_alt().
493 */
494 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
495 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
496 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
497 /* see 6.2.30: no data, wIndex = interface,
498 * wValue = packet filter bitmap
499 */
500 if (w_length != 0 || w_index != ecm->ctrl_id)
501 goto invalid;
502 DBG(cdev, "packet filter %02x\n", w_value);
503 /* REVISIT locking of cdc_filter. This assumes the UDC
504 * driver won't have a concurrent packet TX irq running on
505 * another CPU; or that if it does, this write is atomic...
506 */
507 ecm->port.cdc_filter = w_value;
508 value = 0;
509 break;
510
511 /* and optionally:
512 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
513 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
514 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
515 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
516 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
517 * case USB_CDC_GET_ETHERNET_STATISTIC:
518 */
519
520 default:
521invalid:
522 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
523 ctrl->bRequestType, ctrl->bRequest,
524 w_value, w_index, w_length);
525 }
526
527 /* respond with data transfer or status phase? */
528 if (value >= 0) {
529 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
530 ctrl->bRequestType, ctrl->bRequest,
531 w_value, w_index, w_length);
532 req->zero = 0;
533 req->length = value;
534 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
535 if (value < 0)
536 pr_err("ecm req %02x.%02x response err %d\n",
537 ctrl->bRequestType, ctrl->bRequest,
538 value);
539 }
540
541 /* device either stalls (value < 0) or reports success */
542 return value;
543}
544
545
546static int ecm_qc_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
547{
548 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
549 struct usb_composite_dev *cdev = f->config->cdev;
550
551 /* Control interface has only altsetting 0 */
552 if (intf == ecm->ctrl_id) {
553 if (alt != 0)
554 goto fail;
555
556 if (ecm->notify->driver_data) {
557 VDBG(cdev, "reset ecm control %d\n", intf);
558 usb_ep_disable(ecm->notify);
559 }
560 if (!(ecm->notify->desc)) {
561 VDBG(cdev, "init ecm ctrl %d\n", intf);
562 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
563 goto fail;
564 }
565 usb_ep_enable(ecm->notify);
566 ecm->notify->driver_data = ecm;
567
568 /* Data interface has two altsettings, 0 and 1 */
569 } else if (intf == ecm->data_id) {
570 if (alt > 1)
571 goto fail;
572
573 if (ecm->port.in_ep->driver_data) {
574 DBG(cdev, "reset ecm\n");
Amit Blay51bebe92012-12-25 18:48:10 +0200575 /* ecm->port is needed for disconnecting the BAM data
576 * path. Only after the BAM data path is disconnected,
577 * we can disconnect the port from the network layer.
578 */
Ofir Cohen7b155422012-07-31 13:02:49 +0300579 ecm_qc_bam_disconnect(ecm);
Amit Blayf9b352b2013-03-04 15:01:40 +0200580 if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA)
581 gether_qc_disconnect_name(&ecm->port, "ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300582 }
583
584 if (!ecm->port.in_ep->desc ||
585 !ecm->port.out_ep->desc) {
586 DBG(cdev, "init ecm\n");
587 if (config_ep_by_speed(cdev->gadget, f,
588 ecm->port.in_ep) ||
589 config_ep_by_speed(cdev->gadget, f,
590 ecm->port.out_ep)) {
591 ecm->port.in_ep->desc = NULL;
592 ecm->port.out_ep->desc = NULL;
593 goto fail;
594 }
595 }
596
597 /* CDC Ethernet only sends data in non-default altsettings.
598 * Changing altsettings resets filters, statistics, etc.
599 */
600 if (alt == 1) {
601 struct net_device *net;
602
603 /* Enable zlps by default for ECM conformance;
604 * override for musb_hdrc (avoids txdma ovhead).
605 */
606 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
607 );
608 ecm->port.cdc_filter = DEFAULT_FILTER;
609 DBG(cdev, "activate ecm\n");
Amit Blayf9b352b2013-03-04 15:01:40 +0200610 if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA) {
611 net = gether_qc_connect_name(&ecm->port,
612 "ecm0");
613 if (IS_ERR(net))
614 return PTR_ERR(net);
615 }
Ofir Cohen7b155422012-07-31 13:02:49 +0300616
617 if (ecm_qc_bam_connect(ecm))
618 goto fail;
619 }
620
621 /* NOTE this can be a minor disagreement with the ECM spec,
622 * which says speed notifications will "always" follow
623 * connection notifications. But we allow one connect to
624 * follow another (if the first is in flight), and instead
625 * just guarantee that a speed notification is always sent.
626 */
627 ecm_qc_notify(ecm);
628 } else
629 goto fail;
630
631 return 0;
632fail:
633 return -EINVAL;
634}
635
636/* Because the data interface supports multiple altsettings,
637 * this ECM function *MUST* implement a get_alt() method.
638 */
639static int ecm_qc_get_alt(struct usb_function *f, unsigned intf)
640{
641 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
642
643 if (intf == ecm->ctrl_id)
644 return 0;
645 return ecm->port.in_ep->driver_data ? 1 : 0;
646}
647
648static void ecm_qc_disable(struct usb_function *f)
649{
650 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
Jack Pham0ad82e62012-09-27 17:31:08 -0700651 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
Ofir Cohen7b155422012-07-31 13:02:49 +0300652
653 DBG(cdev, "ecm deactivated\n");
654
655 if (ecm->port.in_ep->driver_data) {
Ofir Cohen7b155422012-07-31 13:02:49 +0300656 ecm_qc_bam_disconnect(ecm);
Amit Blayf9b352b2013-03-04 15:01:40 +0200657 if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA)
658 gether_qc_disconnect_name(&ecm->port, "ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300659 }
660
661 if (ecm->notify->driver_data) {
662 usb_ep_disable(ecm->notify);
663 ecm->notify->driver_data = NULL;
664 ecm->notify->desc = NULL;
665 }
666}
667
668/*-------------------------------------------------------------------------*/
669
670/*
671 * Callbacks let us notify the host about connect/disconnect when the
672 * net device is opened or closed.
673 *
674 * For testing, note that link states on this side include both opened
675 * and closed variants of:
676 *
677 * - disconnected/unconfigured
678 * - configured but inactive (data alt 0)
679 * - configured and active (data alt 1)
680 *
681 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
682 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
683 * imply the host is actually polling the notification endpoint, and
684 * likewise that "active" doesn't imply it's actually using the data
685 * endpoints for traffic.
686 */
687
688static void ecm_qc_open(struct qc_gether *geth)
689{
690 struct f_ecm_qc *ecm = func_to_ecm_qc(&geth->func);
691 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
692
693 ecm->is_open = true;
694 ecm_qc_notify(ecm);
695}
696
697static void ecm_qc_close(struct qc_gether *geth)
698{
699 struct f_ecm_qc *ecm = func_to_ecm_qc(&geth->func);
700
701 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
702
703 ecm->is_open = false;
704 ecm_qc_notify(ecm);
705}
706
707/*-------------------------------------------------------------------------*/
708
709/* ethernet function driver setup/binding */
710
711static int
712ecm_qc_bind(struct usb_configuration *c, struct usb_function *f)
713{
714 struct usb_composite_dev *cdev = c->cdev;
715 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
716 int status;
717 struct usb_ep *ep;
718
719 /* allocate instance-specific interface IDs */
720 status = usb_interface_id(c, f);
721 if (status < 0)
722 goto fail;
Amit Blayf9b352b2013-03-04 15:01:40 +0200723
Ofir Cohen7b155422012-07-31 13:02:49 +0300724 ecm->ctrl_id = status;
725
726 ecm_qc_control_intf.bInterfaceNumber = status;
727 ecm_qc_union_desc.bMasterInterface0 = status;
728
729 status = usb_interface_id(c, f);
730 if (status < 0)
731 goto fail;
Amit Blayf9b352b2013-03-04 15:01:40 +0200732
Ofir Cohen7b155422012-07-31 13:02:49 +0300733 ecm->data_id = status;
734
735 ecm_qc_data_nop_intf.bInterfaceNumber = status;
736 ecm_qc_data_intf.bInterfaceNumber = status;
737 ecm_qc_union_desc.bSlaveInterface0 = status;
738
739 status = -ENODEV;
740
741 /* allocate instance-specific endpoints */
742 ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_in_desc);
743 if (!ep)
744 goto fail;
745
746 ecm->port.in_ep = ep;
747 ep->driver_data = cdev; /* claim */
748
749 ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_out_desc);
750 if (!ep)
751 goto fail;
752
753 ecm->port.out_ep = ep;
754 ep->driver_data = cdev; /* claim */
755
756 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
757 * don't treat it that way. It's simpler, and some newer CDC
758 * profiles (wireless handsets) no longer treat it as optional.
759 */
760 ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_notify_desc);
761 if (!ep)
762 goto fail;
763 ecm->notify = ep;
764 ep->driver_data = cdev; /* claim */
765
766 status = -ENOMEM;
767
768 /* allocate notification request and buffer */
769 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
770 if (!ecm->notify_req)
771 goto fail;
772 ecm->notify_req->buf = kmalloc(ECM_QC_STATUS_BYTECOUNT, GFP_KERNEL);
773 if (!ecm->notify_req->buf)
774 goto fail;
775 ecm->notify_req->context = ecm;
776 ecm->notify_req->complete = ecm_qc_notify_complete;
777
778 /* copy descriptors, and track endpoint copies */
779 f->descriptors = usb_copy_descriptors(ecm_qc_fs_function);
780 if (!f->descriptors)
781 goto fail;
782
783 /* support all relevant hardware speeds... we expect that when
784 * hardware is dual speed, all bulk-capable endpoints work at
785 * both speeds
786 */
787 if (gadget_is_dualspeed(c->cdev->gadget)) {
788 ecm_qc_hs_in_desc.bEndpointAddress =
789 ecm_qc_fs_in_desc.bEndpointAddress;
790 ecm_qc_hs_out_desc.bEndpointAddress =
791 ecm_qc_fs_out_desc.bEndpointAddress;
792 ecm_qc_hs_notify_desc.bEndpointAddress =
793 ecm_qc_fs_notify_desc.bEndpointAddress;
794
795 /* copy descriptors, and track endpoint copies */
796 f->hs_descriptors = usb_copy_descriptors(ecm_qc_hs_function);
797 if (!f->hs_descriptors)
798 goto fail;
799 }
800
801 /* NOTE: all that is done without knowing or caring about
802 * the network link ... which is unavailable to this code
803 * until we're activated via set_alt().
804 */
805
806 ecm->port.open = ecm_qc_open;
807 ecm->port.close = ecm_qc_close;
808
809 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
810 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
811 ecm->port.in_ep->name, ecm->port.out_ep->name,
812 ecm->notify->name);
813 return 0;
814
815fail:
816 if (f->descriptors)
817 usb_free_descriptors(f->descriptors);
818
819 if (ecm->notify_req) {
820 kfree(ecm->notify_req->buf);
821 usb_ep_free_request(ecm->notify, ecm->notify_req);
822 }
823
824 /* we might as well release our claims on endpoints */
825 if (ecm->notify)
826 ecm->notify->driver_data = NULL;
827 if (ecm->port.out_ep->desc)
828 ecm->port.out_ep->driver_data = NULL;
829 if (ecm->port.in_ep->desc)
830 ecm->port.in_ep->driver_data = NULL;
831
832 pr_err("%s: can't bind, err %d\n", f->name, status);
833
834 return status;
835}
836
837static void
838ecm_qc_unbind(struct usb_configuration *c, struct usb_function *f)
839{
840 struct f_ecm_qc *ecm = func_to_ecm_qc(f);
841
842 DBG(c->cdev, "ecm unbind\n");
843
844 if (gadget_is_dualspeed(c->cdev->gadget))
845 usb_free_descriptors(f->hs_descriptors);
846 usb_free_descriptors(f->descriptors);
847
848 kfree(ecm->notify_req->buf);
849 usb_ep_free_request(ecm->notify, ecm->notify_req);
850
851 ecm_qc_string_defs[1].s = NULL;
852 kfree(ecm);
853}
854
855/**
856 * ecm_qc_bind_config - add CDC Ethernet network link to a configuration
857 * @c: the configuration to support the network link
858 * @ethaddr: a buffer in which the ethernet address of the host side
859 * side of the link was recorded
Amit Blayf9b352b2013-03-04 15:01:40 +0200860 * @xport_name: data path transport type name ("BAM2BAM" or "BAM2BAM_IPA")
Ofir Cohen7b155422012-07-31 13:02:49 +0300861 * Context: single threaded during gadget setup
862 *
863 * Returns zero on success, else negative errno.
864 *
865 * Caller must have called @gether_qc_setup(). Caller is also responsible
866 * for calling @gether_cleanup() before module unload.
867 */
868int
Amit Blayf9b352b2013-03-04 15:01:40 +0200869ecm_qc_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
870 char *xport_name)
Ofir Cohen7b155422012-07-31 13:02:49 +0300871{
872 struct f_ecm_qc *ecm;
873 int status;
874
875 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
876 return -EINVAL;
877
878 status = ecm_qc_bam_setup();
879 if (status) {
880 pr_err("bam setup failed");
881 return status;
882 }
883
Amit Blayf9b352b2013-03-04 15:01:40 +0200884 pr_debug("data transport type is %s", xport_name);
885
Ofir Cohen7b155422012-07-31 13:02:49 +0300886 /* maybe allocate device-global string IDs */
887 if (ecm_qc_string_defs[0].id == 0) {
888
889 /* control interface label */
890 status = usb_string_id(c->cdev);
891 if (status < 0)
892 return status;
893 ecm_qc_string_defs[0].id = status;
894 ecm_qc_control_intf.iInterface = status;
895
896 /* data interface label */
897 status = usb_string_id(c->cdev);
898 if (status < 0)
899 return status;
900 ecm_qc_string_defs[2].id = status;
901 ecm_qc_data_intf.iInterface = status;
902
903 /* MAC address */
904 status = usb_string_id(c->cdev);
905 if (status < 0)
906 return status;
907 ecm_qc_string_defs[1].id = status;
908 ecm_qc_desc.iMACAddress = status;
909 }
910
911 /* allocate and initialize one new instance */
912 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
913 if (!ecm)
914 return -ENOMEM;
915
Amit Blayf9b352b2013-03-04 15:01:40 +0200916 ecm->xport = str_to_xport(xport_name);
917 pr_debug("set xport = %d", ecm->xport);
918
Ofir Cohen7b155422012-07-31 13:02:49 +0300919 /* export host's Ethernet address in CDC format */
Amit Blayf9b352b2013-03-04 15:01:40 +0200920 if (ecm->xport == USB_GADGET_XPORT_BAM2BAM_IPA) {
921 gether_qc_get_macs(ipa_params.dev_mac, ipa_params.host_mac);
922 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
923 "%02X%02X%02X%02X%02X%02X",
924 ipa_params.host_mac[0], ipa_params.host_mac[1],
925 ipa_params.host_mac[2], ipa_params.host_mac[3],
926 ipa_params.host_mac[4], ipa_params.host_mac[5]);
927 } else
928 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
Ofir Cohen7b155422012-07-31 13:02:49 +0300929 "%02X%02X%02X%02X%02X%02X",
930 ethaddr[0], ethaddr[1], ethaddr[2],
931 ethaddr[3], ethaddr[4], ethaddr[5]);
Amit Blayf9b352b2013-03-04 15:01:40 +0200932
Ofir Cohen7b155422012-07-31 13:02:49 +0300933 ecm_qc_string_defs[1].s = ecm->ethaddr;
934
935 ecm->port.cdc_filter = DEFAULT_FILTER;
936
937 ecm->port.func.name = "cdc_ethernet";
938 ecm->port.func.strings = ecm_qc_strings;
939 /* descriptors are per-instance copies */
940 ecm->port.func.bind = ecm_qc_bind;
941 ecm->port.func.unbind = ecm_qc_unbind;
942 ecm->port.func.set_alt = ecm_qc_set_alt;
943 ecm->port.func.get_alt = ecm_qc_get_alt;
944 ecm->port.func.setup = ecm_qc_setup;
945 ecm->port.func.disable = ecm_qc_disable;
946
947 status = usb_add_function(c, &ecm->port.func);
948 if (status) {
Amit Blayf9b352b2013-03-04 15:01:40 +0200949 pr_err("failed to add function");
950 ecm_qc_string_defs[1].s = NULL;
951 kfree(ecm);
952 return status;
953 }
954
955 if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA)
956 return status;
957
958 status = ecm_ipa_init(&ipa_params.ipa_rx_cb, &ipa_params.ipa_tx_cb,
959 &ipa_params.ipa_priv);
960 if (status) {
961 pr_err("failed to initialize ECM IPA Driver");
962 ecm_qc_string_defs[1].s = NULL;
963 kfree(ecm);
964 return status;
965 }
966
967 status = ecm_ipa_configure(ipa_params.host_mac, ipa_params.dev_mac,
968 ipa_params.ipa_priv);
969 if (status) {
970 pr_err("failed to configure ECM IPA Driver");
Ofir Cohen7b155422012-07-31 13:02:49 +0300971 ecm_qc_string_defs[1].s = NULL;
972 kfree(ecm);
973 }
Amit Blayf9b352b2013-03-04 15:01:40 +0200974
Ofir Cohen7b155422012-07-31 13:02:49 +0300975 return status;
976}