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