aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
authorDavid Brownell <dbrownell@users.sourceforge.net>2008-06-19 21:19:46 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2008-07-21 18:16:13 -0400
commitda741b8c56d612b5dd26ffa31341911a5fea23ee (patch)
tree201018abff24086e048a7dbf96ae07e60a82433e /drivers/usb/gadget
parent8a40819e97368f2b6e67fea103348f9fc2f68ceb (diff)
usb ethernet gadget: split CDC Ethernet function
This is a "CDC Ethernet" (ECM) function driver, extracted from the all-in-one Ethernet gadget driver. This is a good example of how to implement interface altsettings. In fact it's currently the only such example in the gadget stack, pending addition of OBEX support. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Makefile2
-rw-r--r--drivers/usb/gadget/f_ecm.c833
-rw-r--r--drivers/usb/gadget/gadget_chips.h23
-rw-r--r--drivers/usb/gadget/u_ether.h24
4 files changed, 881 insertions, 1 deletions
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index e9e0e61eb0ec..564065253643 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -25,7 +25,7 @@ obj-$(CONFIG_USB_M66592) += m66592-udc.o
25C_UTILS = composite.o usbstring.o config.o epautoconf.o 25C_UTILS = composite.o usbstring.o config.o epautoconf.o
26 26
27g_zero-objs := zero.o f_sourcesink.o f_loopback.o $(C_UTILS) 27g_zero-objs := zero.o f_sourcesink.o f_loopback.o $(C_UTILS)
28g_ether-objs := ether.o u_ether.o f_subset.o $(C_UTILS) 28g_ether-objs := ether.o u_ether.o f_subset.o f_ecm.o $(C_UTILS)
29g_serial-objs := serial.o u_serial.o f_acm.o f_serial.o $(C_UTILS) 29g_serial-objs := serial.o u_serial.o f_acm.o f_serial.o $(C_UTILS)
30g_midi-objs := gmidi.o usbstring.o config.o epautoconf.o 30g_midi-objs := gmidi.o usbstring.o config.o epautoconf.o
31gadgetfs-objs := inode.o 31gadgetfs-objs := inode.o
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c
new file mode 100644
index 000000000000..0822e9d7693a
--- /dev/null
+++ b/drivers/usb/gadget/f_ecm.c
@@ -0,0 +1,833 @@
1/*
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
66 struct usb_descriptor_header **fs_function;
67 struct ecm_ep_descs fs;
68 struct usb_descriptor_header **hs_function;
69 struct ecm_ep_descs hs;
70
71 struct usb_ep *notify;
72 struct usb_endpoint_descriptor *notify_desc;
73 struct usb_request *notify_req;
74 u8 notify_state;
75 bool is_open;
76
77 /* FIXME is_open needs some irq-ish locking
78 * ... possibly the same as port.ioport
79 */
80};
81
82static inline struct f_ecm *func_to_ecm(struct usb_function *f)
83{
84 return container_of(f, struct f_ecm, port.func);
85}
86
87/* peak (theoretical) bulk transfer rate in bits-per-second */
88static inline unsigned bitrate(struct usb_gadget *g)
89{
90 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
91 return 13 * 512 * 8 * 1000 * 8;
92 else
93 return 19 * 64 * 1 * 1000 * 8;
94}
95
96/*-------------------------------------------------------------------------*/
97
98/*
99 * Include the status endpoint if we can, even though it's optional.
100 *
101 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
102 * packet, to simplify cancellation; and a big transfer interval, to
103 * waste less bandwidth.
104 *
105 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
106 * if they ignore the connect/disconnect notifications that real aether
107 * can provide. More advanced cdc configurations might want to support
108 * encapsulated commands (vendor-specific, using control-OUT).
109 */
110
111#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
112#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
113
114
115/* interface descriptor: */
116
117static struct usb_interface_descriptor ecm_control_intf __initdata = {
118 .bLength = sizeof ecm_control_intf,
119 .bDescriptorType = USB_DT_INTERFACE,
120
121 /* .bInterfaceNumber = DYNAMIC */
122 /* status endpoint is optional; this could be patched later */
123 .bNumEndpoints = 1,
124 .bInterfaceClass = USB_CLASS_COMM,
125 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
126 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
127 /* .iInterface = DYNAMIC */
128};
129
130static struct usb_cdc_header_desc header_desc __initdata = {
131 .bLength = sizeof header_desc,
132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
134
135 .bcdCDC = __constant_cpu_to_le16(0x0110),
136};
137
138static struct usb_cdc_union_desc ecm_union_desc __initdata = {
139 .bLength = sizeof(ecm_union_desc),
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_UNION_TYPE,
142 /* .bMasterInterface0 = DYNAMIC */
143 /* .bSlaveInterface0 = DYNAMIC */
144};
145
146static struct usb_cdc_ether_desc ether_desc __initdata = {
147 .bLength = sizeof ether_desc,
148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
150
151 /* this descriptor actually adds value, surprise! */
152 /* .iMACAddress = DYNAMIC */
153 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
154 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
155 .wNumberMCFilters = __constant_cpu_to_le16(0),
156 .bNumberPowerFilters = 0,
157};
158
159/* the default data interface has no endpoints ... */
160
161static struct usb_interface_descriptor ecm_data_nop_intf __initdata = {
162 .bLength = sizeof ecm_data_nop_intf,
163 .bDescriptorType = USB_DT_INTERFACE,
164
165 .bInterfaceNumber = 1,
166 .bAlternateSetting = 0,
167 .bNumEndpoints = 0,
168 .bInterfaceClass = USB_CLASS_CDC_DATA,
169 .bInterfaceSubClass = 0,
170 .bInterfaceProtocol = 0,
171 /* .iInterface = DYNAMIC */
172};
173
174/* ... but the "real" data interface has two bulk endpoints */
175
176static struct usb_interface_descriptor ecm_data_intf __initdata = {
177 .bLength = sizeof ecm_data_intf,
178 .bDescriptorType = USB_DT_INTERFACE,
179
180 .bInterfaceNumber = 1,
181 .bAlternateSetting = 1,
182 .bNumEndpoints = 2,
183 .bInterfaceClass = USB_CLASS_CDC_DATA,
184 .bInterfaceSubClass = 0,
185 .bInterfaceProtocol = 0,
186 /* .iInterface = DYNAMIC */
187};
188
189/* full speed support: */
190
191static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
194
195 .bEndpointAddress = USB_DIR_IN,
196 .bmAttributes = USB_ENDPOINT_XFER_INT,
197 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
198 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
199};
200
201static struct usb_endpoint_descriptor fs_in_desc __initdata = {
202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
204
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_BULK,
207};
208
209static struct usb_endpoint_descriptor fs_out_desc __initdata = {
210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212
213 .bEndpointAddress = USB_DIR_OUT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
215};
216
217static struct usb_descriptor_header *eth_fs_function[] __initdata = {
218 /* CDC ECM control descriptors */
219 (struct usb_descriptor_header *) &ecm_control_intf,
220 (struct usb_descriptor_header *) &header_desc,
221 (struct usb_descriptor_header *) &ecm_union_desc,
222 (struct usb_descriptor_header *) &ether_desc,
223 /* NOTE: status endpoint might need to be removed */
224 (struct usb_descriptor_header *) &fs_notify_desc,
225 /* data interface, altsettings 0 and 1 */
226 (struct usb_descriptor_header *) &ecm_data_nop_intf,
227 (struct usb_descriptor_header *) &ecm_data_intf,
228 (struct usb_descriptor_header *) &fs_in_desc,
229 (struct usb_descriptor_header *) &fs_out_desc,
230 NULL,
231};
232
233/* high speed support: */
234
235static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
238
239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT,
241 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
242 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
243};
244static struct usb_endpoint_descriptor hs_in_desc __initdata = {
245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
247
248 .bEndpointAddress = USB_DIR_IN,
249 .bmAttributes = USB_ENDPOINT_XFER_BULK,
250 .wMaxPacketSize = __constant_cpu_to_le16(512),
251};
252
253static struct usb_endpoint_descriptor hs_out_desc __initdata = {
254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
256
257 .bEndpointAddress = USB_DIR_OUT,
258 .bmAttributes = USB_ENDPOINT_XFER_BULK,
259 .wMaxPacketSize = __constant_cpu_to_le16(512),
260};
261
262static struct usb_descriptor_header *eth_hs_function[] __initdata = {
263 /* CDC ECM control descriptors */
264 (struct usb_descriptor_header *) &ecm_control_intf,
265 (struct usb_descriptor_header *) &header_desc,
266 (struct usb_descriptor_header *) &ecm_union_desc,
267 (struct usb_descriptor_header *) &ether_desc,
268 /* NOTE: status endpoint might need to be removed */
269 (struct usb_descriptor_header *) &hs_notify_desc,
270 /* data interface, altsettings 0 and 1 */
271 (struct usb_descriptor_header *) &ecm_data_nop_intf,
272 (struct usb_descriptor_header *) &ecm_data_intf,
273 (struct usb_descriptor_header *) &hs_in_desc,
274 (struct usb_descriptor_header *) &hs_out_desc,
275 NULL,
276};
277
278/* string descriptors: */
279
280static struct usb_string ecm_string_defs[] = {
281 [0].s = "CDC Ethernet Control Model (ECM)",
282 [1].s = NULL /* DYNAMIC */,
283 [2].s = "CDC Ethernet Data",
284 { } /* end of list */
285};
286
287static struct usb_gadget_strings ecm_string_table = {
288 .language = 0x0409, /* en-us */
289 .strings = ecm_string_defs,
290};
291
292static struct usb_gadget_strings *ecm_strings[] = {
293 &ecm_string_table,
294 NULL,
295};
296
297/*-------------------------------------------------------------------------*/
298
299static void ecm_do_notify(struct f_ecm *ecm)
300{
301 struct usb_request *req = ecm->notify_req;
302 struct usb_cdc_notification *event;
303 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
304 __le32 *data;
305 int status;
306
307 /* notification already in flight? */
308 if (!req)
309 return;
310
311 event = req->buf;
312 switch (ecm->notify_state) {
313 case ECM_NOTIFY_NONE:
314 return;
315
316 case ECM_NOTIFY_CONNECT:
317 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
318 if (ecm->is_open)
319 event->wValue = cpu_to_le16(1);
320 else
321 event->wValue = cpu_to_le16(0);
322 event->wLength = 0;
323 req->length = sizeof *event;
324
325 DBG(cdev, "notify connect %s\n",
326 ecm->is_open ? "true" : "false");
327 ecm->notify_state = ECM_NOTIFY_SPEED;
328 break;
329
330 case ECM_NOTIFY_SPEED:
331 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
332 event->wValue = cpu_to_le16(0);
333 event->wLength = cpu_to_le16(8);
334 req->length = STATUS_BYTECOUNT;
335
336 /* SPEED_CHANGE data is up/down speeds in bits/sec */
337 data = req->buf + sizeof *event;
338 data[0] = cpu_to_le32(bitrate(cdev->gadget));
339 data[1] = data[0];
340
341 DBG(cdev, "notify speed %d\n", bitrate(cdev->gadget));
342 ecm->notify_state = ECM_NOTIFY_NONE;
343 break;
344 }
345 event->bmRequestType = 0xA1;
346 event->wIndex = cpu_to_le16(ecm->ctrl_id);
347
348 ecm->notify_req = NULL;
349 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
350 if (status < 0) {
351 ecm->notify_req = req;
352 DBG(cdev, "notify --> %d\n", status);
353 }
354}
355
356static void ecm_notify(struct f_ecm *ecm)
357{
358 /* NOTE on most versions of Linux, host side cdc-ethernet
359 * won't listen for notifications until its netdevice opens.
360 * The first notification then sits in the FIFO for a long
361 * time, and the second one is queued.
362 */
363 ecm->notify_state = ECM_NOTIFY_CONNECT;
364 ecm_do_notify(ecm);
365}
366
367static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
368{
369 struct f_ecm *ecm = req->context;
370 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
371 struct usb_cdc_notification *event = req->buf;
372
373 switch (req->status) {
374 case 0:
375 /* no fault */
376 break;
377 case -ECONNRESET:
378 case -ESHUTDOWN:
379 ecm->notify_state = ECM_NOTIFY_NONE;
380 break;
381 default:
382 DBG(cdev, "event %02x --> %d\n",
383 event->bNotificationType, req->status);
384 break;
385 }
386 ecm->notify_req = req;
387 ecm_do_notify(ecm);
388}
389
390static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
391{
392 struct f_ecm *ecm = func_to_ecm(f);
393 struct usb_composite_dev *cdev = f->config->cdev;
394 struct usb_request *req = cdev->req;
395 int value = -EOPNOTSUPP;
396 u16 w_index = le16_to_cpu(ctrl->wIndex);
397 u16 w_value = le16_to_cpu(ctrl->wValue);
398 u16 w_length = le16_to_cpu(ctrl->wLength);
399
400 /* composite driver infrastructure handles everything except
401 * CDC class messages; interface activation uses set_alt().
402 */
403 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
404 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
405 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
406 /* see 6.2.30: no data, wIndex = interface,
407 * wValue = packet filter bitmap
408 */
409 if (w_length != 0 || w_index != ecm->ctrl_id)
410 goto invalid;
411 DBG(cdev, "packet filter %02x\n", w_value);
412 /* REVISIT locking of cdc_filter. This assumes the UDC
413 * driver won't have a concurrent packet TX irq running on
414 * another CPU; or that if it does, this write is atomic...
415 */
416 ecm->port.cdc_filter = w_value;
417 value = 0;
418 break;
419
420 /* and optionally:
421 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
422 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
423 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
424 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
425 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
426 * case USB_CDC_GET_ETHERNET_STATISTIC:
427 */
428
429 default:
430invalid:
431 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
432 ctrl->bRequestType, ctrl->bRequest,
433 w_value, w_index, w_length);
434 }
435
436 /* respond with data transfer or status phase? */
437 if (value >= 0) {
438 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
439 ctrl->bRequestType, ctrl->bRequest,
440 w_value, w_index, w_length);
441 req->zero = 0;
442 req->length = value;
443 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
444 if (value < 0)
445 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
446 ctrl->bRequestType, ctrl->bRequest,
447 value);
448 }
449
450 /* device either stalls (value < 0) or reports success */
451 return value;
452}
453
454
455static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
456{
457 struct f_ecm *ecm = func_to_ecm(f);
458 struct usb_composite_dev *cdev = f->config->cdev;
459
460 /* Control interface has only altsetting 0 */
461 if (intf == ecm->ctrl_id) {
462 if (alt != 0)
463 goto fail;
464
465 if (ecm->notify->driver_data) {
466 VDBG(cdev, "reset ecm control %d\n", intf);
467 usb_ep_disable(ecm->notify);
468 } else {
469 VDBG(cdev, "init ecm ctrl %d\n", intf);
470 ecm->notify_desc = ep_choose(cdev->gadget,
471 ecm->hs.notify,
472 ecm->fs.notify);
473 }
474 usb_ep_enable(ecm->notify, ecm->notify_desc);
475 ecm->notify->driver_data = ecm;
476
477 /* Data interface has two altsettings, 0 and 1 */
478 } else if (intf == ecm->data_id) {
479 if (alt > 1)
480 goto fail;
481
482 if (ecm->port.in_ep->driver_data) {
483 DBG(cdev, "reset ecm\n");
484 gether_disconnect(&ecm->port);
485 }
486
487 if (!ecm->port.in) {
488 DBG(cdev, "init ecm\n");
489 ecm->port.in = ep_choose(cdev->gadget,
490 ecm->hs.in, ecm->fs.in);
491 ecm->port.out = ep_choose(cdev->gadget,
492 ecm->hs.out, ecm->fs.out);
493 }
494
495 /* CDC Ethernet only sends data in non-default altsettings.
496 * Changing altsettings resets filters, statistics, etc.
497 */
498 if (alt == 1) {
499 struct net_device *net;
500
501 /* Enable zlps by default for ECM conformance;
502 * override for musb_hdrc (avoids txdma ovhead)
503 * and sa1100 (can't).
504 */
505 ecm->port.is_zlp_ok = !(
506 gadget_is_sa1100(cdev->gadget)
507 || gadget_is_musbhdrc(cdev->gadget)
508 );
509 ecm->port.cdc_filter = DEFAULT_FILTER;
510 DBG(cdev, "activate ecm\n");
511 net = gether_connect(&ecm->port);
512 if (IS_ERR(net))
513 return PTR_ERR(net);
514 }
515
516 /* NOTE this can be a minor disagreement with the ECM spec,
517 * which says speed notifications will "always" follow
518 * connection notifications. But we allow one connect to
519 * follow another (if the first is in flight), and instead
520 * just guarantee that a speed notification is always sent.
521 */
522 ecm_notify(ecm);
523 } else
524 goto fail;
525
526 return 0;
527fail:
528 return -EINVAL;
529}
530
531/* Because the data interface supports multiple altsettings,
532 * this ECM function *MUST* implement a get_alt() method.
533 */
534static int ecm_get_alt(struct usb_function *f, unsigned intf)
535{
536 struct f_ecm *ecm = func_to_ecm(f);
537
538 if (intf == ecm->ctrl_id)
539 return 0;
540 return ecm->port.in_ep->driver_data ? 1 : 0;
541}
542
543static void ecm_disable(struct usb_function *f)
544{
545 struct f_ecm *ecm = func_to_ecm(f);
546 struct usb_composite_dev *cdev = f->config->cdev;
547
548 DBG(cdev, "ecm deactivated\n");
549
550 if (ecm->port.in_ep->driver_data)
551 gether_disconnect(&ecm->port);
552
553 if (ecm->notify->driver_data) {
554 usb_ep_disable(ecm->notify);
555 ecm->notify->driver_data = NULL;
556 ecm->notify_desc = NULL;
557 }
558}
559
560/*-------------------------------------------------------------------------*/
561
562/*
563 * Callbacks let us notify the host about connect/disconnect when the
564 * net device is opened or closed.
565 *
566 * For testing, note that link states on this side include both opened
567 * and closed variants of:
568 *
569 * - disconnected/unconfigured
570 * - configured but inactive (data alt 0)
571 * - configured and active (data alt 1)
572 *
573 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
574 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
575 * imply the host is actually polling the notification endpoint, and
576 * likewise that "active" doesn't imply it's actually using the data
577 * endpoints for traffic.
578 */
579
580static void ecm_open(struct gether *geth)
581{
582 struct f_ecm *ecm = func_to_ecm(&geth->func);
583
584 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
585
586 ecm->is_open = true;
587 ecm_notify(ecm);
588}
589
590static void ecm_close(struct gether *geth)
591{
592 struct f_ecm *ecm = func_to_ecm(&geth->func);
593
594 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
595
596 ecm->is_open = false;
597 ecm_notify(ecm);
598}
599
600/*-------------------------------------------------------------------------*/
601
602/* ethernet function driver setup/binding */
603
604static int __init
605ecm_bind(struct usb_configuration *c, struct usb_function *f)
606{
607 struct usb_composite_dev *cdev = c->cdev;
608 struct f_ecm *ecm = func_to_ecm(f);
609 int status;
610 struct usb_ep *ep;
611
612 /* allocate instance-specific interface IDs */
613 status = usb_interface_id(c, f);
614 if (status < 0)
615 goto fail;
616 ecm->ctrl_id = status;
617
618 ecm_control_intf.bInterfaceNumber = status;
619 ecm_union_desc.bMasterInterface0 = status;
620
621 status = usb_interface_id(c, f);
622 if (status < 0)
623 goto fail;
624 ecm->data_id = status;
625
626 ecm_data_nop_intf.bInterfaceNumber = status;
627 ecm_data_intf.bInterfaceNumber = status;
628 ecm_union_desc.bSlaveInterface0 = status;
629
630 status = -ENODEV;
631
632 /* allocate instance-specific endpoints */
633 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
634 if (!ep)
635 goto fail;
636 ecm->port.in_ep = ep;
637 ep->driver_data = cdev; /* claim */
638
639 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
640 if (!ep)
641 goto fail;
642 ecm->port.out_ep = ep;
643 ep->driver_data = cdev; /* claim */
644
645 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
646 * don't treat it that way. It's simpler, and some newer CDC
647 * profiles (wireless handsets) no longer treat it as optional.
648 */
649 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
650 if (!ep)
651 goto fail;
652 ecm->notify = ep;
653 ep->driver_data = cdev; /* claim */
654
655 status = -ENOMEM;
656
657 /* allocate notification request and buffer */
658 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
659 if (!ecm->notify_req)
660 goto fail;
661 ecm->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
662 if (!ecm->notify_req->buf)
663 goto fail;
664 ecm->notify_req->context = ecm;
665 ecm->notify_req->complete = ecm_notify_complete;
666
667 /* copy descriptors, and track endpoint copies */
668 f->descriptors = usb_copy_descriptors(eth_fs_function);
669 if (!f->descriptors)
670 goto fail;
671
672 ecm->fs.in = usb_find_endpoint(eth_fs_function,
673 f->descriptors, &fs_in_desc);
674 ecm->fs.out = usb_find_endpoint(eth_fs_function,
675 f->descriptors, &fs_out_desc);
676 ecm->fs.notify = usb_find_endpoint(eth_fs_function,
677 f->descriptors, &fs_notify_desc);
678
679 /* support all relevant hardware speeds... we expect that when
680 * hardware is dual speed, all bulk-capable endpoints work at
681 * both speeds
682 */
683 if (gadget_is_dualspeed(c->cdev->gadget)) {
684 hs_in_desc.bEndpointAddress =
685 fs_in_desc.bEndpointAddress;
686 hs_out_desc.bEndpointAddress =
687 fs_out_desc.bEndpointAddress;
688 hs_notify_desc.bEndpointAddress =
689 fs_notify_desc.bEndpointAddress;
690
691 /* copy descriptors, and track endpoint copies */
692 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
693 if (!f->hs_descriptors)
694 goto fail;
695
696 ecm->hs.in = usb_find_endpoint(eth_hs_function,
697 f->hs_descriptors, &hs_in_desc);
698 ecm->hs.out = usb_find_endpoint(eth_hs_function,
699 f->hs_descriptors, &hs_out_desc);
700 ecm->hs.notify = usb_find_endpoint(eth_hs_function,
701 f->hs_descriptors, &hs_notify_desc);
702 }
703
704 /* NOTE: all that is done without knowing or caring about
705 * the network link ... which is unavailable to this code
706 * until we're activated via set_alt().
707 */
708
709 ecm->port.open = ecm_open;
710 ecm->port.close = ecm_close;
711
712 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
713 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
714 ecm->port.in_ep->name, ecm->port.out_ep->name,
715 ecm->notify->name);
716 return 0;
717
718fail:
719 if (f->descriptors)
720 usb_free_descriptors(f->descriptors);
721
722 if (ecm->notify_req) {
723 kfree(ecm->notify_req->buf);
724 usb_ep_free_request(ecm->notify, ecm->notify_req);
725 }
726
727 /* we might as well release our claims on endpoints */
728 if (ecm->notify)
729 ecm->notify->driver_data = NULL;
730 if (ecm->port.out)
731 ecm->port.out_ep->driver_data = NULL;
732 if (ecm->port.in)
733 ecm->port.in_ep->driver_data = NULL;
734
735 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
736
737 return status;
738}
739
740static void
741ecm_unbind(struct usb_configuration *c, struct usb_function *f)
742{
743 struct f_ecm *ecm = func_to_ecm(f);
744
745 DBG(c->cdev, "ecm unbind\n");
746
747 if (gadget_is_dualspeed(c->cdev->gadget))
748 usb_free_descriptors(f->hs_descriptors);
749 usb_free_descriptors(f->descriptors);
750
751 kfree(ecm->notify_req->buf);
752 usb_ep_free_request(ecm->notify, ecm->notify_req);
753
754 ecm_string_defs[1].s = NULL;
755 kfree(ecm);
756}
757
758/**
759 * ecm_bind_config - add CDC Ethernet network link to a configuration
760 * @c: the configuration to support the network link
761 * @ethaddr: a buffer in which the ethernet address of the host side
762 * side of the link was recorded
763 * Context: single threaded during gadget setup
764 *
765 * Returns zero on success, else negative errno.
766 *
767 * Caller must have called @gether_setup(). Caller is also responsible
768 * for calling @gether_cleanup() before module unload.
769 */
770int __init ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
771{
772 struct f_ecm *ecm;
773 int status;
774
775 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
776 return -EINVAL;
777
778 /* maybe allocate device-global string IDs */
779 if (ecm_string_defs[0].id == 0) {
780
781 /* control interface label */
782 status = usb_string_id(c->cdev);
783 if (status < 0)
784 return status;
785 ecm_string_defs[0].id = status;
786 ecm_control_intf.iInterface = status;
787
788 /* data interface label */
789 status = usb_string_id(c->cdev);
790 if (status < 0)
791 return status;
792 ecm_string_defs[2].id = status;
793 ecm_data_intf.iInterface = status;
794
795 /* MAC address */
796 status = usb_string_id(c->cdev);
797 if (status < 0)
798 return status;
799 ecm_string_defs[1].id = status;
800 ether_desc.iMACAddress = status;
801 }
802
803 /* allocate and initialize one new instance */
804 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
805 if (!ecm)
806 return -ENOMEM;
807
808 /* export host's Ethernet address in CDC format */
809 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
810 "%02X%02X%02X%02X%02X%02X",
811 ethaddr[0], ethaddr[1], ethaddr[2],
812 ethaddr[3], ethaddr[4], ethaddr[5]);
813 ecm_string_defs[1].s = ecm->ethaddr;
814
815 ecm->port.cdc_filter = DEFAULT_FILTER;
816
817 ecm->port.func.name = "cdc_ethernet";
818 ecm->port.func.strings = ecm_strings;
819 /* descriptors are per-instance copies */
820 ecm->port.func.bind = ecm_bind;
821 ecm->port.func.unbind = ecm_unbind;
822 ecm->port.func.set_alt = ecm_set_alt;
823 ecm->port.func.get_alt = ecm_get_alt;
824 ecm->port.func.setup = ecm_setup;
825 ecm->port.func.disable = ecm_disable;
826
827 status = usb_add_function(c, &ecm->port.func);
828 if (status) {
829 ecm_string_defs[1].s = NULL;
830 kfree(ecm);
831 }
832 return status;
833}
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index ca5149ea7312..5246e8fef2b2 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -214,3 +214,26 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
214 return 0x21; 214 return 0x21;
215 return -ENOENT; 215 return -ENOENT;
216} 216}
217
218
219/**
220 * gadget_supports_altsettings - return true if altsettings work
221 * @gadget: the gadget in question
222 */
223static inline bool gadget_supports_altsettings(struct usb_gadget *gadget)
224{
225 /* PXA 21x/25x/26x has no altsettings at all */
226 if (gadget_is_pxa(gadget))
227 return false;
228
229 /* PXA 27x and 3xx have *broken* altsetting support */
230 if (gadget_is_pxa27x(gadget))
231 return false;
232
233 /* SH3 hardware just doesn't do altsettings */
234 if (gadget_is_sh(gadget))
235 return false;
236
237 /* Everything else is *presumably* fine ... */
238 return true;
239}
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index 0ef823447f28..8abf197ab402 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -28,6 +28,9 @@
28#include <linux/usb/composite.h> 28#include <linux/usb/composite.h>
29#include <linux/usb/cdc.h> 29#include <linux/usb/cdc.h>
30 30
31#include "gadget_chips.h"
32
33
31/* 34/*
32 * This represents the USB side of an "ethernet" link, managed by a USB 35 * This represents the USB side of an "ethernet" link, managed by a USB
33 * function which provides control and (maybe) framing. Two functions 36 * function which provides control and (maybe) framing. Two functions
@@ -80,7 +83,28 @@ void gether_cleanup(void);
80struct net_device *gether_connect(struct gether *); 83struct net_device *gether_connect(struct gether *);
81void gether_disconnect(struct gether *); 84void gether_disconnect(struct gether *);
82 85
86/* Some controllers can't support CDC Ethernet (ECM) ... */
87static inline bool can_support_ecm(struct usb_gadget *gadget)
88{
89 if (!gadget_supports_altsettings(gadget))
90 return false;
91
92 /* SA1100 can do ECM, *without* status endpoint ... but we'll
93 * only use it in non-ECM mode for backwards compatibility
94 * (and since we currently require a status endpoint)
95 */
96 if (gadget_is_sa1100(gadget))
97 return false;
98
99 /* Everything else is *presumably* fine ... but this is a bit
100 * chancy, so be **CERTAIN** there are no hardware issues with
101 * your controller. Add it above if it can't handle CDC.
102 */
103 return true;
104}
105
83/* each configuration may bind one instance of an ethernet link */ 106/* each configuration may bind one instance of an ethernet link */
84int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 107int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
108int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
85 109
86#endif /* __U_ETHER_H */ 110#endif /* __U_ETHER_H */