blob: ddedbc83bc375ac12710e4eecd27406602e8482d [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
David Brownellda741b82008-06-19 18:19:46 -070025#include <linux/kernel.h>
26#include <linux/device.h>
27#include <linux/etherdevice.h>
28
29#include "u_ether.h"
30
31
32/*
33 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
34 * Ethernet link. The data transfer model is simple (packets sent and
35 * received over bulk endpoints using normal short packet termination),
36 * and the control model exposes various data and optional notifications.
37 *
38 * ECM is well standardized and (except for Microsoft) supported by most
39 * operating systems with USB host support. It's the preferred interop
40 * solution for Ethernet over USB, at least for firmware based solutions.
41 * (Hardware solutions tend to be more minimalist.) A newer and simpler
42 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
43 *
44 * Note that ECM requires the use of "alternate settings" for its data
45 * interface. This means that the set_alt() method has real work to do,
46 * and also means that a get_alt() method is required.
47 */
48
David Brownellda741b82008-06-19 18:19:46 -070049
50enum ecm_notify_state {
51 ECM_NOTIFY_NONE, /* don't notify */
52 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
53 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
54};
55
56struct f_ecm {
57 struct gether port;
58 u8 ctrl_id, data_id;
59
60 char ethaddr[14];
61
David Brownellda741b82008-06-19 18:19:46 -070062 struct usb_ep *notify;
David Brownellda741b82008-06-19 18:19:46 -070063 struct usb_request *notify_req;
64 u8 notify_state;
65 bool is_open;
66
67 /* FIXME is_open needs some irq-ish locking
68 * ... possibly the same as port.ioport
69 */
70};
71
72static inline struct f_ecm *func_to_ecm(struct usb_function *f)
73{
74 return container_of(f, struct f_ecm, port.func);
75}
76
77/* peak (theoretical) bulk transfer rate in bits-per-second */
David Brownell33376c12008-08-18 17:45:07 -070078static inline unsigned ecm_bitrate(struct usb_gadget *g)
David Brownellda741b82008-06-19 18:19:46 -070079{
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 LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
David Brownell33376c12008-08-18 17:45:07 -0700102#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
David Brownellda741b82008-06-19 18:19:46 -0700103
104
105/* interface descriptor: */
106
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200107static struct usb_interface_descriptor ecm_control_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700108 .bLength = sizeof ecm_control_intf,
109 .bDescriptorType = USB_DT_INTERFACE,
110
111 /* .bInterfaceNumber = DYNAMIC */
112 /* status endpoint is optional; this could be patched later */
113 .bNumEndpoints = 1,
114 .bInterfaceClass = USB_CLASS_COMM,
115 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
116 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
117 /* .iInterface = DYNAMIC */
118};
119
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200120static struct usb_cdc_header_desc ecm_header_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700121 .bLength = sizeof ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700122 .bDescriptorType = USB_DT_CS_INTERFACE,
123 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
124
Harvey Harrison551509d2009-02-11 14:11:36 -0800125 .bcdCDC = cpu_to_le16(0x0110),
David Brownellda741b82008-06-19 18:19:46 -0700126};
127
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200128static struct usb_cdc_union_desc ecm_union_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700129 .bLength = sizeof(ecm_union_desc),
130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubType = USB_CDC_UNION_TYPE,
132 /* .bMasterInterface0 = DYNAMIC */
133 /* .bSlaveInterface0 = DYNAMIC */
134};
135
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200136static struct usb_cdc_ether_desc ecm_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700137 .bLength = sizeof ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700138 .bDescriptorType = USB_DT_CS_INTERFACE,
139 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
140
141 /* this descriptor actually adds value, surprise! */
142 /* .iMACAddress = DYNAMIC */
Harvey Harrison551509d2009-02-11 14:11:36 -0800143 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
144 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
145 .wNumberMCFilters = cpu_to_le16(0),
David Brownellda741b82008-06-19 18:19:46 -0700146 .bNumberPowerFilters = 0,
147};
148
149/* the default data interface has no endpoints ... */
150
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200151static struct usb_interface_descriptor ecm_data_nop_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700152 .bLength = sizeof ecm_data_nop_intf,
153 .bDescriptorType = USB_DT_INTERFACE,
154
155 .bInterfaceNumber = 1,
156 .bAlternateSetting = 0,
157 .bNumEndpoints = 0,
158 .bInterfaceClass = USB_CLASS_CDC_DATA,
159 .bInterfaceSubClass = 0,
160 .bInterfaceProtocol = 0,
161 /* .iInterface = DYNAMIC */
162};
163
164/* ... but the "real" data interface has two bulk endpoints */
165
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200166static struct usb_interface_descriptor ecm_data_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700167 .bLength = sizeof ecm_data_intf,
168 .bDescriptorType = USB_DT_INTERFACE,
169
170 .bInterfaceNumber = 1,
171 .bAlternateSetting = 1,
172 .bNumEndpoints = 2,
173 .bInterfaceClass = USB_CLASS_CDC_DATA,
174 .bInterfaceSubClass = 0,
175 .bInterfaceProtocol = 0,
176 /* .iInterface = DYNAMIC */
177};
178
179/* full speed support: */
180
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200181static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700182 .bLength = USB_DT_ENDPOINT_SIZE,
183 .bDescriptorType = USB_DT_ENDPOINT,
184
185 .bEndpointAddress = USB_DIR_IN,
186 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800187 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
David Brownellda741b82008-06-19 18:19:46 -0700188 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
189};
190
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200191static struct usb_endpoint_descriptor fs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
194
195 .bEndpointAddress = USB_DIR_IN,
196 .bmAttributes = USB_ENDPOINT_XFER_BULK,
197};
198
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200199static struct usb_endpoint_descriptor fs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
202
203 .bEndpointAddress = USB_DIR_OUT,
204 .bmAttributes = USB_ENDPOINT_XFER_BULK,
205};
206
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200207static struct usb_descriptor_header *ecm_fs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700208 /* CDC ECM control descriptors */
209 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700210 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700211 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700212 (struct usb_descriptor_header *) &ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700213 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700214 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
David Brownellda741b82008-06-19 18:19:46 -0700215 /* data interface, altsettings 0 and 1 */
216 (struct usb_descriptor_header *) &ecm_data_nop_intf,
217 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700218 (struct usb_descriptor_header *) &fs_ecm_in_desc,
219 (struct usb_descriptor_header *) &fs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700220 NULL,
221};
222
223/* high speed support: */
224
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200225static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700226 .bLength = USB_DT_ENDPOINT_SIZE,
227 .bDescriptorType = USB_DT_ENDPOINT,
228
229 .bEndpointAddress = USB_DIR_IN,
230 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800231 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
David Brownellda741b82008-06-19 18:19:46 -0700232 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
233};
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200234static struct usb_endpoint_descriptor hs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700235 .bLength = USB_DT_ENDPOINT_SIZE,
236 .bDescriptorType = USB_DT_ENDPOINT,
237
238 .bEndpointAddress = USB_DIR_IN,
239 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800240 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700241};
242
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200243static struct usb_endpoint_descriptor hs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700244 .bLength = USB_DT_ENDPOINT_SIZE,
245 .bDescriptorType = USB_DT_ENDPOINT,
246
247 .bEndpointAddress = USB_DIR_OUT,
248 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800249 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700250};
251
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200252static struct usb_descriptor_header *ecm_hs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700253 /* CDC ECM control descriptors */
254 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700255 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700256 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700257 (struct usb_descriptor_header *) &ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700258 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700259 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
David Brownellda741b82008-06-19 18:19:46 -0700260 /* data interface, altsettings 0 and 1 */
261 (struct usb_descriptor_header *) &ecm_data_nop_intf,
262 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700263 (struct usb_descriptor_header *) &hs_ecm_in_desc,
264 (struct usb_descriptor_header *) &hs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700265 NULL,
266};
267
268/* string descriptors: */
269
270static struct usb_string ecm_string_defs[] = {
271 [0].s = "CDC Ethernet Control Model (ECM)",
272 [1].s = NULL /* DYNAMIC */,
273 [2].s = "CDC Ethernet Data",
274 { } /* end of list */
275};
276
277static struct usb_gadget_strings ecm_string_table = {
278 .language = 0x0409, /* en-us */
279 .strings = ecm_string_defs,
280};
281
282static struct usb_gadget_strings *ecm_strings[] = {
283 &ecm_string_table,
284 NULL,
285};
286
287/*-------------------------------------------------------------------------*/
288
289static void ecm_do_notify(struct f_ecm *ecm)
290{
291 struct usb_request *req = ecm->notify_req;
292 struct usb_cdc_notification *event;
293 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
294 __le32 *data;
295 int status;
296
297 /* notification already in flight? */
298 if (!req)
299 return;
300
301 event = req->buf;
302 switch (ecm->notify_state) {
303 case ECM_NOTIFY_NONE:
304 return;
305
306 case ECM_NOTIFY_CONNECT:
307 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
308 if (ecm->is_open)
309 event->wValue = cpu_to_le16(1);
310 else
311 event->wValue = cpu_to_le16(0);
312 event->wLength = 0;
313 req->length = sizeof *event;
314
315 DBG(cdev, "notify connect %s\n",
316 ecm->is_open ? "true" : "false");
317 ecm->notify_state = ECM_NOTIFY_SPEED;
318 break;
319
320 case ECM_NOTIFY_SPEED:
321 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
322 event->wValue = cpu_to_le16(0);
323 event->wLength = cpu_to_le16(8);
David Brownell33376c12008-08-18 17:45:07 -0700324 req->length = ECM_STATUS_BYTECOUNT;
David Brownellda741b82008-06-19 18:19:46 -0700325
326 /* SPEED_CHANGE data is up/down speeds in bits/sec */
327 data = req->buf + sizeof *event;
David Brownell33376c12008-08-18 17:45:07 -0700328 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700329 data[1] = data[0];
330
David Brownell33376c12008-08-18 17:45:07 -0700331 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700332 ecm->notify_state = ECM_NOTIFY_NONE;
333 break;
334 }
335 event->bmRequestType = 0xA1;
336 event->wIndex = cpu_to_le16(ecm->ctrl_id);
337
338 ecm->notify_req = NULL;
339 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
340 if (status < 0) {
341 ecm->notify_req = req;
342 DBG(cdev, "notify --> %d\n", status);
343 }
344}
345
346static void ecm_notify(struct f_ecm *ecm)
347{
348 /* NOTE on most versions of Linux, host side cdc-ethernet
349 * won't listen for notifications until its netdevice opens.
350 * The first notification then sits in the FIFO for a long
351 * time, and the second one is queued.
352 */
353 ecm->notify_state = ECM_NOTIFY_CONNECT;
354 ecm_do_notify(ecm);
355}
356
357static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
358{
359 struct f_ecm *ecm = req->context;
360 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
361 struct usb_cdc_notification *event = req->buf;
362
363 switch (req->status) {
364 case 0:
365 /* no fault */
366 break;
367 case -ECONNRESET:
368 case -ESHUTDOWN:
369 ecm->notify_state = ECM_NOTIFY_NONE;
370 break;
371 default:
372 DBG(cdev, "event %02x --> %d\n",
373 event->bNotificationType, req->status);
374 break;
375 }
376 ecm->notify_req = req;
377 ecm_do_notify(ecm);
378}
379
380static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
381{
382 struct f_ecm *ecm = func_to_ecm(f);
383 struct usb_composite_dev *cdev = f->config->cdev;
384 struct usb_request *req = cdev->req;
385 int value = -EOPNOTSUPP;
386 u16 w_index = le16_to_cpu(ctrl->wIndex);
387 u16 w_value = le16_to_cpu(ctrl->wValue);
388 u16 w_length = le16_to_cpu(ctrl->wLength);
389
390 /* composite driver infrastructure handles everything except
391 * CDC class messages; interface activation uses set_alt().
392 */
393 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
394 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
395 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
396 /* see 6.2.30: no data, wIndex = interface,
397 * wValue = packet filter bitmap
398 */
399 if (w_length != 0 || w_index != ecm->ctrl_id)
400 goto invalid;
401 DBG(cdev, "packet filter %02x\n", w_value);
402 /* REVISIT locking of cdc_filter. This assumes the UDC
403 * driver won't have a concurrent packet TX irq running on
404 * another CPU; or that if it does, this write is atomic...
405 */
406 ecm->port.cdc_filter = w_value;
407 value = 0;
408 break;
409
410 /* and optionally:
411 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
412 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
413 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
414 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
415 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
416 * case USB_CDC_GET_ETHERNET_STATISTIC:
417 */
418
419 default:
420invalid:
421 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
422 ctrl->bRequestType, ctrl->bRequest,
423 w_value, w_index, w_length);
424 }
425
426 /* respond with data transfer or status phase? */
427 if (value >= 0) {
428 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
429 ctrl->bRequestType, ctrl->bRequest,
430 w_value, w_index, w_length);
431 req->zero = 0;
432 req->length = value;
433 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
434 if (value < 0)
435 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
436 ctrl->bRequestType, ctrl->bRequest,
437 value);
438 }
439
440 /* device either stalls (value < 0) or reports success */
441 return value;
442}
443
444
445static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
446{
447 struct f_ecm *ecm = func_to_ecm(f);
448 struct usb_composite_dev *cdev = f->config->cdev;
449
450 /* Control interface has only altsetting 0 */
451 if (intf == ecm->ctrl_id) {
452 if (alt != 0)
453 goto fail;
454
455 if (ecm->notify->driver_data) {
456 VDBG(cdev, "reset ecm control %d\n", intf);
457 usb_ep_disable(ecm->notify);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300458 }
459 if (!(ecm->notify->desc)) {
David Brownellda741b82008-06-19 18:19:46 -0700460 VDBG(cdev, "init ecm ctrl %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300461 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
462 goto fail;
David Brownellda741b82008-06-19 18:19:46 -0700463 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300464 usb_ep_enable(ecm->notify);
David Brownellda741b82008-06-19 18:19:46 -0700465 ecm->notify->driver_data = ecm;
466
467 /* Data interface has two altsettings, 0 and 1 */
468 } else if (intf == ecm->data_id) {
469 if (alt > 1)
470 goto fail;
471
472 if (ecm->port.in_ep->driver_data) {
473 DBG(cdev, "reset ecm\n");
474 gether_disconnect(&ecm->port);
475 }
476
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300477 if (!ecm->port.in_ep->desc ||
478 !ecm->port.out_ep->desc) {
David Brownellda741b82008-06-19 18:19:46 -0700479 DBG(cdev, "init ecm\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300480 if (config_ep_by_speed(cdev->gadget, f,
481 ecm->port.in_ep) ||
482 config_ep_by_speed(cdev->gadget, f,
483 ecm->port.out_ep)) {
484 ecm->port.in_ep->desc = NULL;
485 ecm->port.out_ep->desc = NULL;
486 goto fail;
487 }
David Brownellda741b82008-06-19 18:19:46 -0700488 }
489
490 /* CDC Ethernet only sends data in non-default altsettings.
491 * Changing altsettings resets filters, statistics, etc.
492 */
493 if (alt == 1) {
494 struct net_device *net;
495
496 /* Enable zlps by default for ECM conformance;
Christoph Egger90f79762010-02-05 13:24:12 +0100497 * override for musb_hdrc (avoids txdma ovhead).
David Brownellda741b82008-06-19 18:19:46 -0700498 */
Christoph Egger90f79762010-02-05 13:24:12 +0100499 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
David Brownellda741b82008-06-19 18:19:46 -0700500 );
501 ecm->port.cdc_filter = DEFAULT_FILTER;
502 DBG(cdev, "activate ecm\n");
503 net = gether_connect(&ecm->port);
504 if (IS_ERR(net))
505 return PTR_ERR(net);
506 }
507
508 /* NOTE this can be a minor disagreement with the ECM spec,
509 * which says speed notifications will "always" follow
510 * connection notifications. But we allow one connect to
511 * follow another (if the first is in flight), and instead
512 * just guarantee that a speed notification is always sent.
513 */
514 ecm_notify(ecm);
515 } else
516 goto fail;
517
518 return 0;
519fail:
520 return -EINVAL;
521}
522
523/* Because the data interface supports multiple altsettings,
524 * this ECM function *MUST* implement a get_alt() method.
525 */
526static int ecm_get_alt(struct usb_function *f, unsigned intf)
527{
528 struct f_ecm *ecm = func_to_ecm(f);
529
530 if (intf == ecm->ctrl_id)
531 return 0;
532 return ecm->port.in_ep->driver_data ? 1 : 0;
533}
534
535static void ecm_disable(struct usb_function *f)
536{
537 struct f_ecm *ecm = func_to_ecm(f);
538 struct usb_composite_dev *cdev = f->config->cdev;
539
540 DBG(cdev, "ecm deactivated\n");
541
542 if (ecm->port.in_ep->driver_data)
543 gether_disconnect(&ecm->port);
544
545 if (ecm->notify->driver_data) {
546 usb_ep_disable(ecm->notify);
547 ecm->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300548 ecm->notify->desc = NULL;
David Brownellda741b82008-06-19 18:19:46 -0700549 }
550}
551
552/*-------------------------------------------------------------------------*/
553
554/*
555 * Callbacks let us notify the host about connect/disconnect when the
556 * net device is opened or closed.
557 *
558 * For testing, note that link states on this side include both opened
559 * and closed variants of:
560 *
561 * - disconnected/unconfigured
562 * - configured but inactive (data alt 0)
563 * - configured and active (data alt 1)
564 *
565 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
566 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
567 * imply the host is actually polling the notification endpoint, and
568 * likewise that "active" doesn't imply it's actually using the data
569 * endpoints for traffic.
570 */
571
572static void ecm_open(struct gether *geth)
573{
574 struct f_ecm *ecm = func_to_ecm(&geth->func);
575
576 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
577
578 ecm->is_open = true;
579 ecm_notify(ecm);
580}
581
582static void ecm_close(struct gether *geth)
583{
584 struct f_ecm *ecm = func_to_ecm(&geth->func);
585
586 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
587
588 ecm->is_open = false;
589 ecm_notify(ecm);
590}
591
592/*-------------------------------------------------------------------------*/
593
594/* ethernet function driver setup/binding */
595
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200596static int
David Brownellda741b82008-06-19 18:19:46 -0700597ecm_bind(struct usb_configuration *c, struct usb_function *f)
598{
599 struct usb_composite_dev *cdev = c->cdev;
600 struct f_ecm *ecm = func_to_ecm(f);
601 int status;
602 struct usb_ep *ep;
603
604 /* allocate instance-specific interface IDs */
605 status = usb_interface_id(c, f);
606 if (status < 0)
607 goto fail;
608 ecm->ctrl_id = status;
609
610 ecm_control_intf.bInterfaceNumber = status;
611 ecm_union_desc.bMasterInterface0 = status;
612
613 status = usb_interface_id(c, f);
614 if (status < 0)
615 goto fail;
616 ecm->data_id = status;
617
618 ecm_data_nop_intf.bInterfaceNumber = status;
619 ecm_data_intf.bInterfaceNumber = status;
620 ecm_union_desc.bSlaveInterface0 = status;
621
622 status = -ENODEV;
623
624 /* allocate instance-specific endpoints */
David Brownell33376c12008-08-18 17:45:07 -0700625 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
David Brownellda741b82008-06-19 18:19:46 -0700626 if (!ep)
627 goto fail;
628 ecm->port.in_ep = ep;
629 ep->driver_data = cdev; /* claim */
630
David Brownell33376c12008-08-18 17:45:07 -0700631 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
David Brownellda741b82008-06-19 18:19:46 -0700632 if (!ep)
633 goto fail;
634 ecm->port.out_ep = ep;
635 ep->driver_data = cdev; /* claim */
636
637 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
638 * don't treat it that way. It's simpler, and some newer CDC
639 * profiles (wireless handsets) no longer treat it as optional.
640 */
David Brownell33376c12008-08-18 17:45:07 -0700641 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
David Brownellda741b82008-06-19 18:19:46 -0700642 if (!ep)
643 goto fail;
644 ecm->notify = ep;
645 ep->driver_data = cdev; /* claim */
646
647 status = -ENOMEM;
648
649 /* allocate notification request and buffer */
650 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
651 if (!ecm->notify_req)
652 goto fail;
David Brownell33376c12008-08-18 17:45:07 -0700653 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
David Brownellda741b82008-06-19 18:19:46 -0700654 if (!ecm->notify_req->buf)
655 goto fail;
656 ecm->notify_req->context = ecm;
657 ecm->notify_req->complete = ecm_notify_complete;
658
659 /* copy descriptors, and track endpoint copies */
David Brownell33376c12008-08-18 17:45:07 -0700660 f->descriptors = usb_copy_descriptors(ecm_fs_function);
David Brownellda741b82008-06-19 18:19:46 -0700661 if (!f->descriptors)
662 goto fail;
663
David Brownellda741b82008-06-19 18:19:46 -0700664 /* support all relevant hardware speeds... we expect that when
665 * hardware is dual speed, all bulk-capable endpoints work at
666 * both speeds
667 */
668 if (gadget_is_dualspeed(c->cdev->gadget)) {
David Brownell33376c12008-08-18 17:45:07 -0700669 hs_ecm_in_desc.bEndpointAddress =
670 fs_ecm_in_desc.bEndpointAddress;
671 hs_ecm_out_desc.bEndpointAddress =
672 fs_ecm_out_desc.bEndpointAddress;
673 hs_ecm_notify_desc.bEndpointAddress =
674 fs_ecm_notify_desc.bEndpointAddress;
David Brownellda741b82008-06-19 18:19:46 -0700675
676 /* copy descriptors, and track endpoint copies */
David Brownell33376c12008-08-18 17:45:07 -0700677 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
David Brownellda741b82008-06-19 18:19:46 -0700678 if (!f->hs_descriptors)
679 goto fail;
David Brownellda741b82008-06-19 18:19:46 -0700680 }
681
682 /* NOTE: all that is done without knowing or caring about
683 * the network link ... which is unavailable to this code
684 * until we're activated via set_alt().
685 */
686
687 ecm->port.open = ecm_open;
688 ecm->port.close = ecm_close;
689
690 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
691 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
692 ecm->port.in_ep->name, ecm->port.out_ep->name,
693 ecm->notify->name);
694 return 0;
695
696fail:
697 if (f->descriptors)
698 usb_free_descriptors(f->descriptors);
699
700 if (ecm->notify_req) {
701 kfree(ecm->notify_req->buf);
702 usb_ep_free_request(ecm->notify, ecm->notify_req);
703 }
704
705 /* we might as well release our claims on endpoints */
706 if (ecm->notify)
707 ecm->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300708 if (ecm->port.out_ep->desc)
David Brownellda741b82008-06-19 18:19:46 -0700709 ecm->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300710 if (ecm->port.in_ep->desc)
David Brownellda741b82008-06-19 18:19:46 -0700711 ecm->port.in_ep->driver_data = NULL;
712
713 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
714
715 return status;
716}
717
718static void
719ecm_unbind(struct usb_configuration *c, struct usb_function *f)
720{
721 struct f_ecm *ecm = func_to_ecm(f);
722
723 DBG(c->cdev, "ecm unbind\n");
724
725 if (gadget_is_dualspeed(c->cdev->gadget))
726 usb_free_descriptors(f->hs_descriptors);
727 usb_free_descriptors(f->descriptors);
728
729 kfree(ecm->notify_req->buf);
730 usb_ep_free_request(ecm->notify, ecm->notify_req);
731
732 ecm_string_defs[1].s = NULL;
733 kfree(ecm);
734}
735
736/**
737 * ecm_bind_config - add CDC Ethernet network link to a configuration
738 * @c: the configuration to support the network link
739 * @ethaddr: a buffer in which the ethernet address of the host side
740 * side of the link was recorded
741 * Context: single threaded during gadget setup
742 *
743 * Returns zero on success, else negative errno.
744 *
745 * Caller must have called @gether_setup(). Caller is also responsible
746 * for calling @gether_cleanup() before module unload.
747 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200748int
749ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownellda741b82008-06-19 18:19:46 -0700750{
751 struct f_ecm *ecm;
752 int status;
753
754 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
755 return -EINVAL;
756
757 /* maybe allocate device-global string IDs */
758 if (ecm_string_defs[0].id == 0) {
759
760 /* control interface label */
761 status = usb_string_id(c->cdev);
762 if (status < 0)
763 return status;
764 ecm_string_defs[0].id = status;
765 ecm_control_intf.iInterface = status;
766
767 /* data interface label */
768 status = usb_string_id(c->cdev);
769 if (status < 0)
770 return status;
771 ecm_string_defs[2].id = status;
772 ecm_data_intf.iInterface = status;
773
774 /* MAC address */
775 status = usb_string_id(c->cdev);
776 if (status < 0)
777 return status;
778 ecm_string_defs[1].id = status;
David Brownell33376c12008-08-18 17:45:07 -0700779 ecm_desc.iMACAddress = status;
David Brownellda741b82008-06-19 18:19:46 -0700780 }
781
782 /* allocate and initialize one new instance */
783 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
784 if (!ecm)
785 return -ENOMEM;
786
787 /* export host's Ethernet address in CDC format */
788 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
789 "%02X%02X%02X%02X%02X%02X",
790 ethaddr[0], ethaddr[1], ethaddr[2],
791 ethaddr[3], ethaddr[4], ethaddr[5]);
792 ecm_string_defs[1].s = ecm->ethaddr;
793
794 ecm->port.cdc_filter = DEFAULT_FILTER;
795
796 ecm->port.func.name = "cdc_ethernet";
797 ecm->port.func.strings = ecm_strings;
798 /* descriptors are per-instance copies */
799 ecm->port.func.bind = ecm_bind;
800 ecm->port.func.unbind = ecm_unbind;
801 ecm->port.func.set_alt = ecm_set_alt;
802 ecm->port.func.get_alt = ecm_get_alt;
803 ecm->port.func.setup = ecm_setup;
804 ecm->port.func.disable = ecm_disable;
805
806 status = usb_add_function(c, &ecm->port.func);
807 if (status) {
808 ecm_string_defs[1].s = NULL;
809 kfree(ecm);
810 }
811 return status;
812}