blob: 4ae579948e54dc67288499cea56929b220e98ebe [file] [log] [blame]
David Brownellda741b82008-06-19 18:19:46 -07001/*
2 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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/kernel.h>
25#include <linux/device.h>
26#include <linux/etherdevice.h>
27
28#include "u_ether.h"
29
30
31/*
32 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
33 * Ethernet link. The data transfer model is simple (packets sent and
34 * received over bulk endpoints using normal short packet termination),
35 * and the control model exposes various data and optional notifications.
36 *
37 * ECM is well standardized and (except for Microsoft) supported by most
38 * operating systems with USB host support. It's the preferred interop
39 * solution for Ethernet over USB, at least for firmware based solutions.
40 * (Hardware solutions tend to be more minimalist.) A newer and simpler
41 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
42 *
43 * Note that ECM requires the use of "alternate settings" for its data
44 * interface. This means that the set_alt() method has real work to do,
45 * and also means that a get_alt() method is required.
46 */
47
48struct ecm_ep_descs {
49 struct usb_endpoint_descriptor *in;
50 struct usb_endpoint_descriptor *out;
51 struct usb_endpoint_descriptor *notify;
52};
53
54enum ecm_notify_state {
55 ECM_NOTIFY_NONE, /* don't notify */
56 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
57 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
58};
59
60struct f_ecm {
61 struct gether port;
62 u8 ctrl_id, data_id;
63
64 char ethaddr[14];
65
David Brownellda741b82008-06-19 18:19:46 -070066 struct ecm_ep_descs fs;
David Brownellda741b82008-06-19 18:19:46 -070067 struct ecm_ep_descs hs;
68
69 struct usb_ep *notify;
70 struct usb_endpoint_descriptor *notify_desc;
71 struct usb_request *notify_req;
72 u8 notify_state;
73 bool is_open;
74
75 /* FIXME is_open needs some irq-ish locking
76 * ... possibly the same as port.ioport
77 */
78};
79
80static inline struct f_ecm *func_to_ecm(struct usb_function *f)
81{
82 return container_of(f, struct f_ecm, port.func);
83}
84
85/* peak (theoretical) bulk transfer rate in bits-per-second */
David Brownell33376c12008-08-18 17:45:07 -070086static inline unsigned ecm_bitrate(struct usb_gadget *g)
David Brownellda741b82008-06-19 18:19:46 -070087{
88 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
89 return 13 * 512 * 8 * 1000 * 8;
90 else
91 return 19 * 64 * 1 * 1000 * 8;
92}
93
94/*-------------------------------------------------------------------------*/
95
96/*
97 * Include the status endpoint if we can, even though it's optional.
98 *
99 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
100 * packet, to simplify cancellation; and a big transfer interval, to
101 * waste less bandwidth.
102 *
103 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
104 * if they ignore the connect/disconnect notifications that real aether
105 * can provide. More advanced cdc configurations might want to support
106 * encapsulated commands (vendor-specific, using control-OUT).
107 */
108
109#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
David Brownell33376c12008-08-18 17:45:07 -0700110#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
David Brownellda741b82008-06-19 18:19:46 -0700111
112
113/* interface descriptor: */
114
115static struct usb_interface_descriptor ecm_control_intf __initdata = {
116 .bLength = sizeof ecm_control_intf,
117 .bDescriptorType = USB_DT_INTERFACE,
118
119 /* .bInterfaceNumber = DYNAMIC */
120 /* status endpoint is optional; this could be patched later */
121 .bNumEndpoints = 1,
122 .bInterfaceClass = USB_CLASS_COMM,
123 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
124 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
125 /* .iInterface = DYNAMIC */
126};
127
David Brownell33376c12008-08-18 17:45:07 -0700128static struct usb_cdc_header_desc ecm_header_desc __initdata = {
129 .bLength = sizeof ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
132
133 .bcdCDC = __constant_cpu_to_le16(0x0110),
134};
135
136static struct usb_cdc_union_desc ecm_union_desc __initdata = {
137 .bLength = sizeof(ecm_union_desc),
138 .bDescriptorType = USB_DT_CS_INTERFACE,
139 .bDescriptorSubType = USB_CDC_UNION_TYPE,
140 /* .bMasterInterface0 = DYNAMIC */
141 /* .bSlaveInterface0 = DYNAMIC */
142};
143
David Brownell33376c12008-08-18 17:45:07 -0700144static struct usb_cdc_ether_desc ecm_desc __initdata = {
145 .bLength = sizeof ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700146 .bDescriptorType = USB_DT_CS_INTERFACE,
147 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
148
149 /* this descriptor actually adds value, surprise! */
150 /* .iMACAddress = DYNAMIC */
151 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
152 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
153 .wNumberMCFilters = __constant_cpu_to_le16(0),
154 .bNumberPowerFilters = 0,
155};
156
157/* the default data interface has no endpoints ... */
158
159static struct usb_interface_descriptor ecm_data_nop_intf __initdata = {
160 .bLength = sizeof ecm_data_nop_intf,
161 .bDescriptorType = USB_DT_INTERFACE,
162
163 .bInterfaceNumber = 1,
164 .bAlternateSetting = 0,
165 .bNumEndpoints = 0,
166 .bInterfaceClass = USB_CLASS_CDC_DATA,
167 .bInterfaceSubClass = 0,
168 .bInterfaceProtocol = 0,
169 /* .iInterface = DYNAMIC */
170};
171
172/* ... but the "real" data interface has two bulk endpoints */
173
174static struct usb_interface_descriptor ecm_data_intf __initdata = {
175 .bLength = sizeof ecm_data_intf,
176 .bDescriptorType = USB_DT_INTERFACE,
177
178 .bInterfaceNumber = 1,
179 .bAlternateSetting = 1,
180 .bNumEndpoints = 2,
181 .bInterfaceClass = USB_CLASS_CDC_DATA,
182 .bInterfaceSubClass = 0,
183 .bInterfaceProtocol = 0,
184 /* .iInterface = DYNAMIC */
185};
186
187/* full speed support: */
188
David Brownell33376c12008-08-18 17:45:07 -0700189static struct usb_endpoint_descriptor fs_ecm_notify_desc __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700190 .bLength = USB_DT_ENDPOINT_SIZE,
191 .bDescriptorType = USB_DT_ENDPOINT,
192
193 .bEndpointAddress = USB_DIR_IN,
194 .bmAttributes = USB_ENDPOINT_XFER_INT,
David Brownell33376c12008-08-18 17:45:07 -0700195 .wMaxPacketSize = __constant_cpu_to_le16(ECM_STATUS_BYTECOUNT),
David Brownellda741b82008-06-19 18:19:46 -0700196 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
197};
198
David Brownell33376c12008-08-18 17:45:07 -0700199static struct usb_endpoint_descriptor fs_ecm_in_desc __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
202
203 .bEndpointAddress = USB_DIR_IN,
204 .bmAttributes = USB_ENDPOINT_XFER_BULK,
205};
206
David Brownell33376c12008-08-18 17:45:07 -0700207static struct usb_endpoint_descriptor fs_ecm_out_desc __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210
211 .bEndpointAddress = USB_DIR_OUT,
212 .bmAttributes = USB_ENDPOINT_XFER_BULK,
213};
214
David Brownell33376c12008-08-18 17:45:07 -0700215static struct usb_descriptor_header *ecm_fs_function[] __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700216 /* CDC ECM control descriptors */
217 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700218 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700219 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700220 (struct usb_descriptor_header *) &ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700221 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700222 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
David Brownellda741b82008-06-19 18:19:46 -0700223 /* data interface, altsettings 0 and 1 */
224 (struct usb_descriptor_header *) &ecm_data_nop_intf,
225 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700226 (struct usb_descriptor_header *) &fs_ecm_in_desc,
227 (struct usb_descriptor_header *) &fs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700228 NULL,
229};
230
231/* high speed support: */
232
David Brownell33376c12008-08-18 17:45:07 -0700233static struct usb_endpoint_descriptor hs_ecm_notify_desc __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700234 .bLength = USB_DT_ENDPOINT_SIZE,
235 .bDescriptorType = USB_DT_ENDPOINT,
236
237 .bEndpointAddress = USB_DIR_IN,
238 .bmAttributes = USB_ENDPOINT_XFER_INT,
David Brownell33376c12008-08-18 17:45:07 -0700239 .wMaxPacketSize = __constant_cpu_to_le16(ECM_STATUS_BYTECOUNT),
David Brownellda741b82008-06-19 18:19:46 -0700240 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
241};
David Brownell33376c12008-08-18 17:45:07 -0700242static struct usb_endpoint_descriptor hs_ecm_in_desc __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700243 .bLength = USB_DT_ENDPOINT_SIZE,
244 .bDescriptorType = USB_DT_ENDPOINT,
245
246 .bEndpointAddress = USB_DIR_IN,
247 .bmAttributes = USB_ENDPOINT_XFER_BULK,
248 .wMaxPacketSize = __constant_cpu_to_le16(512),
249};
250
David Brownell33376c12008-08-18 17:45:07 -0700251static struct usb_endpoint_descriptor hs_ecm_out_desc __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700252 .bLength = USB_DT_ENDPOINT_SIZE,
253 .bDescriptorType = USB_DT_ENDPOINT,
254
255 .bEndpointAddress = USB_DIR_OUT,
256 .bmAttributes = USB_ENDPOINT_XFER_BULK,
257 .wMaxPacketSize = __constant_cpu_to_le16(512),
258};
259
David Brownell33376c12008-08-18 17:45:07 -0700260static struct usb_descriptor_header *ecm_hs_function[] __initdata = {
David Brownellda741b82008-06-19 18:19:46 -0700261 /* CDC ECM control descriptors */
262 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700263 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700264 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700265 (struct usb_descriptor_header *) &ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700266 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700267 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
David Brownellda741b82008-06-19 18:19:46 -0700268 /* data interface, altsettings 0 and 1 */
269 (struct usb_descriptor_header *) &ecm_data_nop_intf,
270 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700271 (struct usb_descriptor_header *) &hs_ecm_in_desc,
272 (struct usb_descriptor_header *) &hs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700273 NULL,
274};
275
276/* string descriptors: */
277
278static struct usb_string ecm_string_defs[] = {
279 [0].s = "CDC Ethernet Control Model (ECM)",
280 [1].s = NULL /* DYNAMIC */,
281 [2].s = "CDC Ethernet Data",
282 { } /* end of list */
283};
284
285static struct usb_gadget_strings ecm_string_table = {
286 .language = 0x0409, /* en-us */
287 .strings = ecm_string_defs,
288};
289
290static struct usb_gadget_strings *ecm_strings[] = {
291 &ecm_string_table,
292 NULL,
293};
294
295/*-------------------------------------------------------------------------*/
296
297static void ecm_do_notify(struct f_ecm *ecm)
298{
299 struct usb_request *req = ecm->notify_req;
300 struct usb_cdc_notification *event;
301 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
302 __le32 *data;
303 int status;
304
305 /* notification already in flight? */
306 if (!req)
307 return;
308
309 event = req->buf;
310 switch (ecm->notify_state) {
311 case ECM_NOTIFY_NONE:
312 return;
313
314 case ECM_NOTIFY_CONNECT:
315 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
316 if (ecm->is_open)
317 event->wValue = cpu_to_le16(1);
318 else
319 event->wValue = cpu_to_le16(0);
320 event->wLength = 0;
321 req->length = sizeof *event;
322
323 DBG(cdev, "notify connect %s\n",
324 ecm->is_open ? "true" : "false");
325 ecm->notify_state = ECM_NOTIFY_SPEED;
326 break;
327
328 case ECM_NOTIFY_SPEED:
329 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
330 event->wValue = cpu_to_le16(0);
331 event->wLength = cpu_to_le16(8);
David Brownell33376c12008-08-18 17:45:07 -0700332 req->length = ECM_STATUS_BYTECOUNT;
David Brownellda741b82008-06-19 18:19:46 -0700333
334 /* SPEED_CHANGE data is up/down speeds in bits/sec */
335 data = req->buf + sizeof *event;
David Brownell33376c12008-08-18 17:45:07 -0700336 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700337 data[1] = data[0];
338
David Brownell33376c12008-08-18 17:45:07 -0700339 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700340 ecm->notify_state = ECM_NOTIFY_NONE;
341 break;
342 }
343 event->bmRequestType = 0xA1;
344 event->wIndex = cpu_to_le16(ecm->ctrl_id);
345
346 ecm->notify_req = NULL;
347 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
348 if (status < 0) {
349 ecm->notify_req = req;
350 DBG(cdev, "notify --> %d\n", status);
351 }
352}
353
354static void ecm_notify(struct f_ecm *ecm)
355{
356 /* NOTE on most versions of Linux, host side cdc-ethernet
357 * won't listen for notifications until its netdevice opens.
358 * The first notification then sits in the FIFO for a long
359 * time, and the second one is queued.
360 */
361 ecm->notify_state = ECM_NOTIFY_CONNECT;
362 ecm_do_notify(ecm);
363}
364
365static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
366{
367 struct f_ecm *ecm = req->context;
368 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
369 struct usb_cdc_notification *event = req->buf;
370
371 switch (req->status) {
372 case 0:
373 /* no fault */
374 break;
375 case -ECONNRESET:
376 case -ESHUTDOWN:
377 ecm->notify_state = ECM_NOTIFY_NONE;
378 break;
379 default:
380 DBG(cdev, "event %02x --> %d\n",
381 event->bNotificationType, req->status);
382 break;
383 }
384 ecm->notify_req = req;
385 ecm_do_notify(ecm);
386}
387
388static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
389{
390 struct f_ecm *ecm = func_to_ecm(f);
391 struct usb_composite_dev *cdev = f->config->cdev;
392 struct usb_request *req = cdev->req;
393 int value = -EOPNOTSUPP;
394 u16 w_index = le16_to_cpu(ctrl->wIndex);
395 u16 w_value = le16_to_cpu(ctrl->wValue);
396 u16 w_length = le16_to_cpu(ctrl->wLength);
397
398 /* composite driver infrastructure handles everything except
399 * CDC class messages; interface activation uses set_alt().
400 */
401 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
402 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
403 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
404 /* see 6.2.30: no data, wIndex = interface,
405 * wValue = packet filter bitmap
406 */
407 if (w_length != 0 || w_index != ecm->ctrl_id)
408 goto invalid;
409 DBG(cdev, "packet filter %02x\n", w_value);
410 /* REVISIT locking of cdc_filter. This assumes the UDC
411 * driver won't have a concurrent packet TX irq running on
412 * another CPU; or that if it does, this write is atomic...
413 */
414 ecm->port.cdc_filter = w_value;
415 value = 0;
416 break;
417
418 /* and optionally:
419 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
420 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
421 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
422 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
423 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
424 * case USB_CDC_GET_ETHERNET_STATISTIC:
425 */
426
427 default:
428invalid:
429 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
430 ctrl->bRequestType, ctrl->bRequest,
431 w_value, w_index, w_length);
432 }
433
434 /* respond with data transfer or status phase? */
435 if (value >= 0) {
436 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
437 ctrl->bRequestType, ctrl->bRequest,
438 w_value, w_index, w_length);
439 req->zero = 0;
440 req->length = value;
441 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
442 if (value < 0)
443 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
444 ctrl->bRequestType, ctrl->bRequest,
445 value);
446 }
447
448 /* device either stalls (value < 0) or reports success */
449 return value;
450}
451
452
453static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
454{
455 struct f_ecm *ecm = func_to_ecm(f);
456 struct usb_composite_dev *cdev = f->config->cdev;
457
458 /* Control interface has only altsetting 0 */
459 if (intf == ecm->ctrl_id) {
460 if (alt != 0)
461 goto fail;
462
463 if (ecm->notify->driver_data) {
464 VDBG(cdev, "reset ecm control %d\n", intf);
465 usb_ep_disable(ecm->notify);
466 } else {
467 VDBG(cdev, "init ecm ctrl %d\n", intf);
468 ecm->notify_desc = ep_choose(cdev->gadget,
469 ecm->hs.notify,
470 ecm->fs.notify);
471 }
472 usb_ep_enable(ecm->notify, ecm->notify_desc);
473 ecm->notify->driver_data = ecm;
474
475 /* Data interface has two altsettings, 0 and 1 */
476 } else if (intf == ecm->data_id) {
477 if (alt > 1)
478 goto fail;
479
480 if (ecm->port.in_ep->driver_data) {
481 DBG(cdev, "reset ecm\n");
482 gether_disconnect(&ecm->port);
483 }
484
485 if (!ecm->port.in) {
486 DBG(cdev, "init ecm\n");
487 ecm->port.in = ep_choose(cdev->gadget,
488 ecm->hs.in, ecm->fs.in);
489 ecm->port.out = ep_choose(cdev->gadget,
490 ecm->hs.out, ecm->fs.out);
491 }
492
493 /* CDC Ethernet only sends data in non-default altsettings.
494 * Changing altsettings resets filters, statistics, etc.
495 */
496 if (alt == 1) {
497 struct net_device *net;
498
499 /* Enable zlps by default for ECM conformance;
500 * override for musb_hdrc (avoids txdma ovhead)
501 * and sa1100 (can't).
502 */
503 ecm->port.is_zlp_ok = !(
504 gadget_is_sa1100(cdev->gadget)
505 || gadget_is_musbhdrc(cdev->gadget)
506 );
507 ecm->port.cdc_filter = DEFAULT_FILTER;
508 DBG(cdev, "activate ecm\n");
509 net = gether_connect(&ecm->port);
510 if (IS_ERR(net))
511 return PTR_ERR(net);
512 }
513
514 /* NOTE this can be a minor disagreement with the ECM spec,
515 * which says speed notifications will "always" follow
516 * connection notifications. But we allow one connect to
517 * follow another (if the first is in flight), and instead
518 * just guarantee that a speed notification is always sent.
519 */
520 ecm_notify(ecm);
521 } else
522 goto fail;
523
524 return 0;
525fail:
526 return -EINVAL;
527}
528
529/* Because the data interface supports multiple altsettings,
530 * this ECM function *MUST* implement a get_alt() method.
531 */
532static int ecm_get_alt(struct usb_function *f, unsigned intf)
533{
534 struct f_ecm *ecm = func_to_ecm(f);
535
536 if (intf == ecm->ctrl_id)
537 return 0;
538 return ecm->port.in_ep->driver_data ? 1 : 0;
539}
540
541static void ecm_disable(struct usb_function *f)
542{
543 struct f_ecm *ecm = func_to_ecm(f);
544 struct usb_composite_dev *cdev = f->config->cdev;
545
546 DBG(cdev, "ecm deactivated\n");
547
548 if (ecm->port.in_ep->driver_data)
549 gether_disconnect(&ecm->port);
550
551 if (ecm->notify->driver_data) {
552 usb_ep_disable(ecm->notify);
553 ecm->notify->driver_data = NULL;
554 ecm->notify_desc = NULL;
555 }
556}
557
558/*-------------------------------------------------------------------------*/
559
560/*
561 * Callbacks let us notify the host about connect/disconnect when the
562 * net device is opened or closed.
563 *
564 * For testing, note that link states on this side include both opened
565 * and closed variants of:
566 *
567 * - disconnected/unconfigured
568 * - configured but inactive (data alt 0)
569 * - configured and active (data alt 1)
570 *
571 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
572 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
573 * imply the host is actually polling the notification endpoint, and
574 * likewise that "active" doesn't imply it's actually using the data
575 * endpoints for traffic.
576 */
577
578static void ecm_open(struct gether *geth)
579{
580 struct f_ecm *ecm = func_to_ecm(&geth->func);
581
582 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
583
584 ecm->is_open = true;
585 ecm_notify(ecm);
586}
587
588static void ecm_close(struct gether *geth)
589{
590 struct f_ecm *ecm = func_to_ecm(&geth->func);
591
592 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
593
594 ecm->is_open = false;
595 ecm_notify(ecm);
596}
597
598/*-------------------------------------------------------------------------*/
599
600/* ethernet function driver setup/binding */
601
602static int __init
603ecm_bind(struct usb_configuration *c, struct usb_function *f)
604{
605 struct usb_composite_dev *cdev = c->cdev;
606 struct f_ecm *ecm = func_to_ecm(f);
607 int status;
608 struct usb_ep *ep;
609
610 /* allocate instance-specific interface IDs */
611 status = usb_interface_id(c, f);
612 if (status < 0)
613 goto fail;
614 ecm->ctrl_id = status;
615
616 ecm_control_intf.bInterfaceNumber = status;
617 ecm_union_desc.bMasterInterface0 = status;
618
619 status = usb_interface_id(c, f);
620 if (status < 0)
621 goto fail;
622 ecm->data_id = status;
623
624 ecm_data_nop_intf.bInterfaceNumber = status;
625 ecm_data_intf.bInterfaceNumber = status;
626 ecm_union_desc.bSlaveInterface0 = status;
627
628 status = -ENODEV;
629
630 /* allocate instance-specific endpoints */
David Brownell33376c12008-08-18 17:45:07 -0700631 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
David Brownellda741b82008-06-19 18:19:46 -0700632 if (!ep)
633 goto fail;
634 ecm->port.in_ep = ep;
635 ep->driver_data = cdev; /* claim */
636
David Brownell33376c12008-08-18 17:45:07 -0700637 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
David Brownellda741b82008-06-19 18:19:46 -0700638 if (!ep)
639 goto fail;
640 ecm->port.out_ep = ep;
641 ep->driver_data = cdev; /* claim */
642
643 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
644 * don't treat it that way. It's simpler, and some newer CDC
645 * profiles (wireless handsets) no longer treat it as optional.
646 */
David Brownell33376c12008-08-18 17:45:07 -0700647 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
David Brownellda741b82008-06-19 18:19:46 -0700648 if (!ep)
649 goto fail;
650 ecm->notify = ep;
651 ep->driver_data = cdev; /* claim */
652
653 status = -ENOMEM;
654
655 /* allocate notification request and buffer */
656 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
657 if (!ecm->notify_req)
658 goto fail;
David Brownell33376c12008-08-18 17:45:07 -0700659 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
David Brownellda741b82008-06-19 18:19:46 -0700660 if (!ecm->notify_req->buf)
661 goto fail;
662 ecm->notify_req->context = ecm;
663 ecm->notify_req->complete = ecm_notify_complete;
664
665 /* copy descriptors, and track endpoint copies */
David Brownell33376c12008-08-18 17:45:07 -0700666 f->descriptors = usb_copy_descriptors(ecm_fs_function);
David Brownellda741b82008-06-19 18:19:46 -0700667 if (!f->descriptors)
668 goto fail;
669
David Brownell33376c12008-08-18 17:45:07 -0700670 ecm->fs.in = usb_find_endpoint(ecm_fs_function,
671 f->descriptors, &fs_ecm_in_desc);
672 ecm->fs.out = usb_find_endpoint(ecm_fs_function,
673 f->descriptors, &fs_ecm_out_desc);
674 ecm->fs.notify = usb_find_endpoint(ecm_fs_function,
675 f->descriptors, &fs_ecm_notify_desc);
David Brownellda741b82008-06-19 18:19:46 -0700676
677 /* support all relevant hardware speeds... we expect that when
678 * hardware is dual speed, all bulk-capable endpoints work at
679 * both speeds
680 */
681 if (gadget_is_dualspeed(c->cdev->gadget)) {
David Brownell33376c12008-08-18 17:45:07 -0700682 hs_ecm_in_desc.bEndpointAddress =
683 fs_ecm_in_desc.bEndpointAddress;
684 hs_ecm_out_desc.bEndpointAddress =
685 fs_ecm_out_desc.bEndpointAddress;
686 hs_ecm_notify_desc.bEndpointAddress =
687 fs_ecm_notify_desc.bEndpointAddress;
David Brownellda741b82008-06-19 18:19:46 -0700688
689 /* copy descriptors, and track endpoint copies */
David Brownell33376c12008-08-18 17:45:07 -0700690 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
David Brownellda741b82008-06-19 18:19:46 -0700691 if (!f->hs_descriptors)
692 goto fail;
693
David Brownell33376c12008-08-18 17:45:07 -0700694 ecm->hs.in = usb_find_endpoint(ecm_hs_function,
695 f->hs_descriptors, &hs_ecm_in_desc);
696 ecm->hs.out = usb_find_endpoint(ecm_hs_function,
697 f->hs_descriptors, &hs_ecm_out_desc);
698 ecm->hs.notify = usb_find_endpoint(ecm_hs_function,
699 f->hs_descriptors, &hs_ecm_notify_desc);
David Brownellda741b82008-06-19 18:19:46 -0700700 }
701
702 /* NOTE: all that is done without knowing or caring about
703 * the network link ... which is unavailable to this code
704 * until we're activated via set_alt().
705 */
706
707 ecm->port.open = ecm_open;
708 ecm->port.close = ecm_close;
709
710 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
711 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
712 ecm->port.in_ep->name, ecm->port.out_ep->name,
713 ecm->notify->name);
714 return 0;
715
716fail:
717 if (f->descriptors)
718 usb_free_descriptors(f->descriptors);
719
720 if (ecm->notify_req) {
721 kfree(ecm->notify_req->buf);
722 usb_ep_free_request(ecm->notify, ecm->notify_req);
723 }
724
725 /* we might as well release our claims on endpoints */
726 if (ecm->notify)
727 ecm->notify->driver_data = NULL;
728 if (ecm->port.out)
729 ecm->port.out_ep->driver_data = NULL;
730 if (ecm->port.in)
731 ecm->port.in_ep->driver_data = NULL;
732
733 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
734
735 return status;
736}
737
738static void
739ecm_unbind(struct usb_configuration *c, struct usb_function *f)
740{
741 struct f_ecm *ecm = func_to_ecm(f);
742
743 DBG(c->cdev, "ecm unbind\n");
744
745 if (gadget_is_dualspeed(c->cdev->gadget))
746 usb_free_descriptors(f->hs_descriptors);
747 usb_free_descriptors(f->descriptors);
748
749 kfree(ecm->notify_req->buf);
750 usb_ep_free_request(ecm->notify, ecm->notify_req);
751
752 ecm_string_defs[1].s = NULL;
753 kfree(ecm);
754}
755
756/**
757 * ecm_bind_config - add CDC Ethernet network link to a configuration
758 * @c: the configuration to support the network link
759 * @ethaddr: a buffer in which the ethernet address of the host side
760 * side of the link was recorded
761 * Context: single threaded during gadget setup
762 *
763 * Returns zero on success, else negative errno.
764 *
765 * Caller must have called @gether_setup(). Caller is also responsible
766 * for calling @gether_cleanup() before module unload.
767 */
768int __init ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
769{
770 struct f_ecm *ecm;
771 int status;
772
773 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
774 return -EINVAL;
775
776 /* maybe allocate device-global string IDs */
777 if (ecm_string_defs[0].id == 0) {
778
779 /* control interface label */
780 status = usb_string_id(c->cdev);
781 if (status < 0)
782 return status;
783 ecm_string_defs[0].id = status;
784 ecm_control_intf.iInterface = status;
785
786 /* data interface label */
787 status = usb_string_id(c->cdev);
788 if (status < 0)
789 return status;
790 ecm_string_defs[2].id = status;
791 ecm_data_intf.iInterface = status;
792
793 /* MAC address */
794 status = usb_string_id(c->cdev);
795 if (status < 0)
796 return status;
797 ecm_string_defs[1].id = status;
David Brownell33376c12008-08-18 17:45:07 -0700798 ecm_desc.iMACAddress = status;
David Brownellda741b82008-06-19 18:19:46 -0700799 }
800
801 /* allocate and initialize one new instance */
802 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
803 if (!ecm)
804 return -ENOMEM;
805
806 /* export host's Ethernet address in CDC format */
807 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
808 "%02X%02X%02X%02X%02X%02X",
809 ethaddr[0], ethaddr[1], ethaddr[2],
810 ethaddr[3], ethaddr[4], ethaddr[5]);
811 ecm_string_defs[1].s = ecm->ethaddr;
812
813 ecm->port.cdc_filter = DEFAULT_FILTER;
814
815 ecm->port.func.name = "cdc_ethernet";
816 ecm->port.func.strings = ecm_strings;
817 /* descriptors are per-instance copies */
818 ecm->port.func.bind = ecm_bind;
819 ecm->port.func.unbind = ecm_unbind;
820 ecm->port.func.set_alt = ecm_set_alt;
821 ecm->port.func.get_alt = ecm_get_alt;
822 ecm->port.func.setup = ecm_setup;
823 ecm->port.func.disable = ecm_disable;
824
825 status = usb_add_function(c, &ecm->port.func);
826 if (status) {
827 ecm_string_defs[1].s = NULL;
828 kfree(ecm);
829 }
830 return status;
831}