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