blob: 1003b1df3b06005f797d63c73ef2af3b76f3a1d9 [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.
David Brownellda741b82008-06-19 18:19:46 -070011 */
12
13/* #define VERBOSE_DEBUG */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Brownellda741b82008-06-19 18:19:46 -070016#include <linux/kernel.h>
17#include <linux/device.h>
18#include <linux/etherdevice.h>
19
20#include "u_ether.h"
21
22
23/*
24 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
25 * Ethernet link. The data transfer model is simple (packets sent and
26 * received over bulk endpoints using normal short packet termination),
27 * and the control model exposes various data and optional notifications.
28 *
29 * ECM is well standardized and (except for Microsoft) supported by most
30 * operating systems with USB host support. It's the preferred interop
31 * solution for Ethernet over USB, at least for firmware based solutions.
32 * (Hardware solutions tend to be more minimalist.) A newer and simpler
33 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
34 *
35 * Note that ECM requires the use of "alternate settings" for its data
36 * interface. This means that the set_alt() method has real work to do,
37 * and also means that a get_alt() method is required.
38 */
39
David Brownellda741b82008-06-19 18:19:46 -070040
41enum ecm_notify_state {
42 ECM_NOTIFY_NONE, /* don't notify */
43 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
44 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
45};
46
47struct f_ecm {
48 struct gether port;
49 u8 ctrl_id, data_id;
50
51 char ethaddr[14];
52
David Brownellda741b82008-06-19 18:19:46 -070053 struct usb_ep *notify;
David Brownellda741b82008-06-19 18:19:46 -070054 struct usb_request *notify_req;
55 u8 notify_state;
56 bool is_open;
57
58 /* FIXME is_open needs some irq-ish locking
59 * ... possibly the same as port.ioport
60 */
61};
62
63static inline struct f_ecm *func_to_ecm(struct usb_function *f)
64{
65 return container_of(f, struct f_ecm, port.func);
66}
67
68/* peak (theoretical) bulk transfer rate in bits-per-second */
David Brownell33376c12008-08-18 17:45:07 -070069static inline unsigned ecm_bitrate(struct usb_gadget *g)
David Brownellda741b82008-06-19 18:19:46 -070070{
Paul Zimmerman04617db2011-06-27 14:13:18 -070071 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
72 return 13 * 1024 * 8 * 1000 * 8;
73 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
David Brownellda741b82008-06-19 18:19:46 -070074 return 13 * 512 * 8 * 1000 * 8;
75 else
Paul Zimmerman04617db2011-06-27 14:13:18 -070076 return 19 * 64 * 1 * 1000 * 8;
David Brownellda741b82008-06-19 18:19:46 -070077}
78
79/*-------------------------------------------------------------------------*/
80
81/*
82 * Include the status endpoint if we can, even though it's optional.
83 *
84 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
85 * packet, to simplify cancellation; and a big transfer interval, to
86 * waste less bandwidth.
87 *
88 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
89 * if they ignore the connect/disconnect notifications that real aether
90 * can provide. More advanced cdc configurations might want to support
91 * encapsulated commands (vendor-specific, using control-OUT).
92 */
93
94#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
Devin Kim0c5b9ce2012-06-19 21:34:44 -070095#ifdef CONFIG_USB_ANDROID_CDC_ECM
96#define ECM_STATUS_BYTECOUNT 64
97#else
David Brownell33376c12008-08-18 17:45:07 -070098#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
Devin Kim0c5b9ce2012-06-19 21:34:44 -070099#endif
David Brownellda741b82008-06-19 18:19:46 -0700100
101
102/* interface descriptor: */
103
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100104static struct usb_interface_assoc_descriptor
105ecm_iad_descriptor = {
106 .bLength = sizeof ecm_iad_descriptor,
107 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
108
109 /* .bFirstInterface = DYNAMIC, */
110 .bInterfaceCount = 2, /* control + data */
111 .bFunctionClass = USB_CLASS_COMM,
112 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
113 .bFunctionProtocol = USB_CDC_PROTO_NONE,
114 /* .iFunction = DYNAMIC */
115};
116
117
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200118static struct usb_interface_descriptor ecm_control_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700119 .bLength = sizeof ecm_control_intf,
120 .bDescriptorType = USB_DT_INTERFACE,
121
122 /* .bInterfaceNumber = DYNAMIC */
123 /* status endpoint is optional; this could be patched later */
124 .bNumEndpoints = 1,
125 .bInterfaceClass = USB_CLASS_COMM,
126 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
127 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
128 /* .iInterface = DYNAMIC */
129};
130
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200131static struct usb_cdc_header_desc ecm_header_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700132 .bLength = sizeof ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700133 .bDescriptorType = USB_DT_CS_INTERFACE,
134 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
135
Harvey Harrison551509d2009-02-11 14:11:36 -0800136 .bcdCDC = cpu_to_le16(0x0110),
David Brownellda741b82008-06-19 18:19:46 -0700137};
138
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200139static struct usb_cdc_union_desc ecm_union_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700140 .bLength = sizeof(ecm_union_desc),
141 .bDescriptorType = USB_DT_CS_INTERFACE,
142 .bDescriptorSubType = USB_CDC_UNION_TYPE,
143 /* .bMasterInterface0 = DYNAMIC */
144 /* .bSlaveInterface0 = DYNAMIC */
145};
146
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200147static struct usb_cdc_ether_desc ecm_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700148 .bLength = sizeof ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
151
152 /* this descriptor actually adds value, surprise! */
153 /* .iMACAddress = DYNAMIC */
Harvey Harrison551509d2009-02-11 14:11:36 -0800154 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
155 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
156 .wNumberMCFilters = cpu_to_le16(0),
David Brownellda741b82008-06-19 18:19:46 -0700157 .bNumberPowerFilters = 0,
158};
159
160/* the default data interface has no endpoints ... */
161
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200162static struct usb_interface_descriptor ecm_data_nop_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700163 .bLength = sizeof ecm_data_nop_intf,
164 .bDescriptorType = USB_DT_INTERFACE,
165
166 .bInterfaceNumber = 1,
167 .bAlternateSetting = 0,
168 .bNumEndpoints = 0,
169 .bInterfaceClass = USB_CLASS_CDC_DATA,
170 .bInterfaceSubClass = 0,
171 .bInterfaceProtocol = 0,
172 /* .iInterface = DYNAMIC */
173};
174
175/* ... but the "real" data interface has two bulk endpoints */
176
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200177static struct usb_interface_descriptor ecm_data_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700178 .bLength = sizeof ecm_data_intf,
179 .bDescriptorType = USB_DT_INTERFACE,
180
181 .bInterfaceNumber = 1,
182 .bAlternateSetting = 1,
183 .bNumEndpoints = 2,
184 .bInterfaceClass = USB_CLASS_CDC_DATA,
185 .bInterfaceSubClass = 0,
186 .bInterfaceProtocol = 0,
187 /* .iInterface = DYNAMIC */
188};
189
190/* full speed support: */
191
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200192static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
195
196 .bEndpointAddress = USB_DIR_IN,
197 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800198 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700199#ifdef CONFIG_USB_CDC_ECM
200 .bInterval = 4,
201#else
David Brownellda741b82008-06-19 18:19:46 -0700202 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700203#endif
David Brownellda741b82008-06-19 18:19:46 -0700204};
205
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200206static struct usb_endpoint_descriptor fs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700207 .bLength = USB_DT_ENDPOINT_SIZE,
208 .bDescriptorType = USB_DT_ENDPOINT,
209
210 .bEndpointAddress = USB_DIR_IN,
211 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700212#ifdef CONFIG_USB_CDC_ECM
213 .wMaxPacketSize = cpu_to_le16(64),
214#endif
David Brownellda741b82008-06-19 18:19:46 -0700215};
216
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200217static struct usb_endpoint_descriptor fs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700218 .bLength = USB_DT_ENDPOINT_SIZE,
219 .bDescriptorType = USB_DT_ENDPOINT,
220
221 .bEndpointAddress = USB_DIR_OUT,
222 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700223#ifdef CONFIG_USB_CDC_ECM
224 .wMaxPacketSize = cpu_to_le16(64),
225#endif
David Brownellda741b82008-06-19 18:19:46 -0700226};
227
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200228static struct usb_descriptor_header *ecm_fs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700229 /* CDC ECM control descriptors */
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100230 (struct usb_descriptor_header *) &ecm_iad_descriptor,
David Brownellda741b82008-06-19 18:19:46 -0700231 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700232 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700233 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700234 (struct usb_descriptor_header *) &ecm_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700235
David Brownellda741b82008-06-19 18:19:46 -0700236 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700237 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700238
David Brownellda741b82008-06-19 18:19:46 -0700239 /* data interface, altsettings 0 and 1 */
240 (struct usb_descriptor_header *) &ecm_data_nop_intf,
241 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700242 (struct usb_descriptor_header *) &fs_ecm_in_desc,
243 (struct usb_descriptor_header *) &fs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700244 NULL,
245};
246
247/* high speed support: */
248
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200249static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
252
253 .bEndpointAddress = USB_DIR_IN,
254 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800255 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700256#ifdef CONFIG_USB_CDC_ECM
257 .bInterval = 4,
258#else
David Brownellda741b82008-06-19 18:19:46 -0700259 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700260#endif
David Brownellda741b82008-06-19 18:19:46 -0700261};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700262
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200263static struct usb_endpoint_descriptor hs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700264 .bLength = USB_DT_ENDPOINT_SIZE,
265 .bDescriptorType = USB_DT_ENDPOINT,
266
267 .bEndpointAddress = USB_DIR_IN,
268 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800269 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700270};
271
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200272static struct usb_endpoint_descriptor hs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700273 .bLength = USB_DT_ENDPOINT_SIZE,
274 .bDescriptorType = USB_DT_ENDPOINT,
275
276 .bEndpointAddress = USB_DIR_OUT,
277 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800278 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700279};
280
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200281static struct usb_descriptor_header *ecm_hs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700282 /* CDC ECM control descriptors */
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100283 (struct usb_descriptor_header *) &ecm_iad_descriptor,
David Brownellda741b82008-06-19 18:19:46 -0700284 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700285 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700286 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700287 (struct usb_descriptor_header *) &ecm_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700288
David Brownellda741b82008-06-19 18:19:46 -0700289 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700290 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700291
David Brownellda741b82008-06-19 18:19:46 -0700292 /* data interface, altsettings 0 and 1 */
293 (struct usb_descriptor_header *) &ecm_data_nop_intf,
294 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700295 (struct usb_descriptor_header *) &hs_ecm_in_desc,
296 (struct usb_descriptor_header *) &hs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700297 NULL,
298};
299
Paul Zimmerman04617db2011-06-27 14:13:18 -0700300/* super speed support: */
301
302static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
303 .bLength = USB_DT_ENDPOINT_SIZE,
304 .bDescriptorType = USB_DT_ENDPOINT,
305
306 .bEndpointAddress = USB_DIR_IN,
307 .bmAttributes = USB_ENDPOINT_XFER_INT,
308 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
309 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
310};
311
312static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
313 .bLength = sizeof ss_ecm_intr_comp_desc,
314 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
315
316 /* the following 3 values can be tweaked if necessary */
317 /* .bMaxBurst = 0, */
318 /* .bmAttributes = 0, */
319 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
320};
321
322static struct usb_endpoint_descriptor ss_ecm_in_desc = {
323 .bLength = USB_DT_ENDPOINT_SIZE,
324 .bDescriptorType = USB_DT_ENDPOINT,
325
326 .bEndpointAddress = USB_DIR_IN,
327 .bmAttributes = USB_ENDPOINT_XFER_BULK,
328 .wMaxPacketSize = cpu_to_le16(1024),
329};
330
331static struct usb_endpoint_descriptor ss_ecm_out_desc = {
332 .bLength = USB_DT_ENDPOINT_SIZE,
333 .bDescriptorType = USB_DT_ENDPOINT,
334
335 .bEndpointAddress = USB_DIR_OUT,
336 .bmAttributes = USB_ENDPOINT_XFER_BULK,
337 .wMaxPacketSize = cpu_to_le16(1024),
338};
339
340static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
341 .bLength = sizeof ss_ecm_bulk_comp_desc,
342 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
343
344 /* the following 2 values can be tweaked if necessary */
345 /* .bMaxBurst = 0, */
346 /* .bmAttributes = 0, */
347};
348
349static struct usb_descriptor_header *ecm_ss_function[] = {
350 /* CDC ECM control descriptors */
351 (struct usb_descriptor_header *) &ecm_control_intf,
352 (struct usb_descriptor_header *) &ecm_header_desc,
353 (struct usb_descriptor_header *) &ecm_union_desc,
354 (struct usb_descriptor_header *) &ecm_desc,
355
356 /* NOTE: status endpoint might need to be removed */
357 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
358 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
359
360 /* data interface, altsettings 0 and 1 */
361 (struct usb_descriptor_header *) &ecm_data_nop_intf,
362 (struct usb_descriptor_header *) &ecm_data_intf,
363 (struct usb_descriptor_header *) &ss_ecm_in_desc,
364 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
365 (struct usb_descriptor_header *) &ss_ecm_out_desc,
366 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
367 NULL,
368};
369
David Brownellda741b82008-06-19 18:19:46 -0700370/* string descriptors: */
371
372static struct usb_string ecm_string_defs[] = {
373 [0].s = "CDC Ethernet Control Model (ECM)",
374 [1].s = NULL /* DYNAMIC */,
375 [2].s = "CDC Ethernet Data",
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100376 [3].s = "CDC ECM",
David Brownellda741b82008-06-19 18:19:46 -0700377 { } /* end of list */
378};
379
380static struct usb_gadget_strings ecm_string_table = {
381 .language = 0x0409, /* en-us */
382 .strings = ecm_string_defs,
383};
384
385static struct usb_gadget_strings *ecm_strings[] = {
386 &ecm_string_table,
387 NULL,
388};
389
390/*-------------------------------------------------------------------------*/
391
392static void ecm_do_notify(struct f_ecm *ecm)
393{
394 struct usb_request *req = ecm->notify_req;
395 struct usb_cdc_notification *event;
396 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
397 __le32 *data;
398 int status;
399
400 /* notification already in flight? */
401 if (!req)
402 return;
403
404 event = req->buf;
405 switch (ecm->notify_state) {
406 case ECM_NOTIFY_NONE:
407 return;
408
409 case ECM_NOTIFY_CONNECT:
410 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
411 if (ecm->is_open)
412 event->wValue = cpu_to_le16(1);
413 else
414 event->wValue = cpu_to_le16(0);
415 event->wLength = 0;
416 req->length = sizeof *event;
417
418 DBG(cdev, "notify connect %s\n",
419 ecm->is_open ? "true" : "false");
420 ecm->notify_state = ECM_NOTIFY_SPEED;
421 break;
422
423 case ECM_NOTIFY_SPEED:
424 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
425 event->wValue = cpu_to_le16(0);
426 event->wLength = cpu_to_le16(8);
David Brownell33376c12008-08-18 17:45:07 -0700427 req->length = ECM_STATUS_BYTECOUNT;
David Brownellda741b82008-06-19 18:19:46 -0700428
429 /* SPEED_CHANGE data is up/down speeds in bits/sec */
430 data = req->buf + sizeof *event;
David Brownell33376c12008-08-18 17:45:07 -0700431 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700432 data[1] = data[0];
433
David Brownell33376c12008-08-18 17:45:07 -0700434 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700435 ecm->notify_state = ECM_NOTIFY_NONE;
436 break;
437 }
438 event->bmRequestType = 0xA1;
439 event->wIndex = cpu_to_le16(ecm->ctrl_id);
440
441 ecm->notify_req = NULL;
442 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
443 if (status < 0) {
444 ecm->notify_req = req;
445 DBG(cdev, "notify --> %d\n", status);
446 }
447}
448
449static void ecm_notify(struct f_ecm *ecm)
450{
451 /* NOTE on most versions of Linux, host side cdc-ethernet
452 * won't listen for notifications until its netdevice opens.
453 * The first notification then sits in the FIFO for a long
454 * time, and the second one is queued.
455 */
456 ecm->notify_state = ECM_NOTIFY_CONNECT;
457 ecm_do_notify(ecm);
458}
459
460static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
461{
462 struct f_ecm *ecm = req->context;
463 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
464 struct usb_cdc_notification *event = req->buf;
465
466 switch (req->status) {
467 case 0:
468 /* no fault */
469 break;
470 case -ECONNRESET:
471 case -ESHUTDOWN:
472 ecm->notify_state = ECM_NOTIFY_NONE;
473 break;
474 default:
475 DBG(cdev, "event %02x --> %d\n",
476 event->bNotificationType, req->status);
477 break;
478 }
479 ecm->notify_req = req;
480 ecm_do_notify(ecm);
481}
482
483static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
484{
485 struct f_ecm *ecm = func_to_ecm(f);
486 struct usb_composite_dev *cdev = f->config->cdev;
487 struct usb_request *req = cdev->req;
488 int value = -EOPNOTSUPP;
489 u16 w_index = le16_to_cpu(ctrl->wIndex);
490 u16 w_value = le16_to_cpu(ctrl->wValue);
491 u16 w_length = le16_to_cpu(ctrl->wLength);
492
493 /* composite driver infrastructure handles everything except
494 * CDC class messages; interface activation uses set_alt().
495 */
496 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
497 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
498 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
499 /* see 6.2.30: no data, wIndex = interface,
500 * wValue = packet filter bitmap
501 */
502 if (w_length != 0 || w_index != ecm->ctrl_id)
503 goto invalid;
504 DBG(cdev, "packet filter %02x\n", w_value);
505 /* REVISIT locking of cdc_filter. This assumes the UDC
506 * driver won't have a concurrent packet TX irq running on
507 * another CPU; or that if it does, this write is atomic...
508 */
509 ecm->port.cdc_filter = w_value;
510 value = 0;
511 break;
512
513 /* and optionally:
514 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
515 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
516 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
517 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
518 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
519 * case USB_CDC_GET_ETHERNET_STATISTIC:
520 */
521
522 default:
523invalid:
524 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
525 ctrl->bRequestType, ctrl->bRequest,
526 w_value, w_index, w_length);
527 }
528
529 /* respond with data transfer or status phase? */
530 if (value >= 0) {
531 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
532 ctrl->bRequestType, ctrl->bRequest,
533 w_value, w_index, w_length);
534 req->zero = 0;
535 req->length = value;
536 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
537 if (value < 0)
538 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
539 ctrl->bRequestType, ctrl->bRequest,
540 value);
541 }
542
543 /* device either stalls (value < 0) or reports success */
544 return value;
545}
546
547
548static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
549{
550 struct f_ecm *ecm = func_to_ecm(f);
551 struct usb_composite_dev *cdev = f->config->cdev;
552
553 /* Control interface has only altsetting 0 */
554 if (intf == ecm->ctrl_id) {
555 if (alt != 0)
556 goto fail;
557
558 if (ecm->notify->driver_data) {
559 VDBG(cdev, "reset ecm control %d\n", intf);
560 usb_ep_disable(ecm->notify);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300561 }
562 if (!(ecm->notify->desc)) {
David Brownellda741b82008-06-19 18:19:46 -0700563 VDBG(cdev, "init ecm ctrl %d\n", intf);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300564 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
565 goto fail;
David Brownellda741b82008-06-19 18:19:46 -0700566 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300567 usb_ep_enable(ecm->notify);
David Brownellda741b82008-06-19 18:19:46 -0700568 ecm->notify->driver_data = ecm;
569
570 /* Data interface has two altsettings, 0 and 1 */
571 } else if (intf == ecm->data_id) {
572 if (alt > 1)
573 goto fail;
574
575 if (ecm->port.in_ep->driver_data) {
576 DBG(cdev, "reset ecm\n");
577 gether_disconnect(&ecm->port);
578 }
579
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300580 if (!ecm->port.in_ep->desc ||
581 !ecm->port.out_ep->desc) {
David Brownellda741b82008-06-19 18:19:46 -0700582 DBG(cdev, "init ecm\n");
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300583 if (config_ep_by_speed(cdev->gadget, f,
584 ecm->port.in_ep) ||
585 config_ep_by_speed(cdev->gadget, f,
586 ecm->port.out_ep)) {
587 ecm->port.in_ep->desc = NULL;
588 ecm->port.out_ep->desc = NULL;
589 goto fail;
590 }
David Brownellda741b82008-06-19 18:19:46 -0700591 }
592
593 /* CDC Ethernet only sends data in non-default altsettings.
594 * Changing altsettings resets filters, statistics, etc.
595 */
596 if (alt == 1) {
597 struct net_device *net;
598
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700599#ifdef CONFIG_USB_CDC_ECM
600 /* Disable zlps in case of LG android USB and qct's udc.
601 * By this, host driver can handle null packet properly.
602 */
603 ecm->port.is_zlp_ok = !(
604 gadget_is_musbhdrc(cdev->gadget) ||
605 gadget_is_ci13xxx_msm(cdev->gadget));
606#else
David Brownellda741b82008-06-19 18:19:46 -0700607 /* Enable zlps by default for ECM conformance;
Christoph Egger90f79762010-02-05 13:24:12 +0100608 * override for musb_hdrc (avoids txdma ovhead).
David Brownellda741b82008-06-19 18:19:46 -0700609 */
Christoph Egger90f79762010-02-05 13:24:12 +0100610 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
David Brownellda741b82008-06-19 18:19:46 -0700611 );
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700612#endif
David Brownellda741b82008-06-19 18:19:46 -0700613 ecm->port.cdc_filter = DEFAULT_FILTER;
614 DBG(cdev, "activate ecm\n");
615 net = gether_connect(&ecm->port);
616 if (IS_ERR(net))
617 return PTR_ERR(net);
618 }
619
620 /* NOTE this can be a minor disagreement with the ECM spec,
621 * which says speed notifications will "always" follow
622 * connection notifications. But we allow one connect to
623 * follow another (if the first is in flight), and instead
624 * just guarantee that a speed notification is always sent.
625 */
626 ecm_notify(ecm);
627 } else
628 goto fail;
629
630 return 0;
631fail:
632 return -EINVAL;
633}
634
635/* Because the data interface supports multiple altsettings,
636 * this ECM function *MUST* implement a get_alt() method.
637 */
638static int ecm_get_alt(struct usb_function *f, unsigned intf)
639{
640 struct f_ecm *ecm = func_to_ecm(f);
641
642 if (intf == ecm->ctrl_id)
643 return 0;
644 return ecm->port.in_ep->driver_data ? 1 : 0;
645}
646
647static void ecm_disable(struct usb_function *f)
648{
649 struct f_ecm *ecm = func_to_ecm(f);
650 struct usb_composite_dev *cdev = f->config->cdev;
651
652 DBG(cdev, "ecm deactivated\n");
653
654 if (ecm->port.in_ep->driver_data)
655 gether_disconnect(&ecm->port);
656
657 if (ecm->notify->driver_data) {
658 usb_ep_disable(ecm->notify);
659 ecm->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300660 ecm->notify->desc = NULL;
David Brownellda741b82008-06-19 18:19:46 -0700661 }
662}
663
664/*-------------------------------------------------------------------------*/
665
666/*
667 * Callbacks let us notify the host about connect/disconnect when the
668 * net device is opened or closed.
669 *
670 * For testing, note that link states on this side include both opened
671 * and closed variants of:
672 *
673 * - disconnected/unconfigured
674 * - configured but inactive (data alt 0)
675 * - configured and active (data alt 1)
676 *
677 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
678 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
679 * imply the host is actually polling the notification endpoint, and
680 * likewise that "active" doesn't imply it's actually using the data
681 * endpoints for traffic.
682 */
683
684static void ecm_open(struct gether *geth)
685{
686 struct f_ecm *ecm = func_to_ecm(&geth->func);
687
688 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
689
690 ecm->is_open = true;
691 ecm_notify(ecm);
692}
693
694static void ecm_close(struct gether *geth)
695{
696 struct f_ecm *ecm = func_to_ecm(&geth->func);
697
698 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
699
700 ecm->is_open = false;
701 ecm_notify(ecm);
702}
703
704/*-------------------------------------------------------------------------*/
705
706/* ethernet function driver setup/binding */
707
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200708static int
David Brownellda741b82008-06-19 18:19:46 -0700709ecm_bind(struct usb_configuration *c, struct usb_function *f)
710{
711 struct usb_composite_dev *cdev = c->cdev;
712 struct f_ecm *ecm = func_to_ecm(f);
713 int status;
714 struct usb_ep *ep;
715
716 /* allocate instance-specific interface IDs */
717 status = usb_interface_id(c, f);
718 if (status < 0)
719 goto fail;
720 ecm->ctrl_id = status;
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100721 ecm_iad_descriptor.bFirstInterface = status;
David Brownellda741b82008-06-19 18:19:46 -0700722
723 ecm_control_intf.bInterfaceNumber = status;
724 ecm_union_desc.bMasterInterface0 = status;
725
726 status = usb_interface_id(c, f);
727 if (status < 0)
728 goto fail;
729 ecm->data_id = status;
730
731 ecm_data_nop_intf.bInterfaceNumber = status;
732 ecm_data_intf.bInterfaceNumber = status;
733 ecm_union_desc.bSlaveInterface0 = status;
734
735 status = -ENODEV;
736
737 /* allocate instance-specific endpoints */
David Brownell33376c12008-08-18 17:45:07 -0700738 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
David Brownellda741b82008-06-19 18:19:46 -0700739 if (!ep)
740 goto fail;
741 ecm->port.in_ep = ep;
742 ep->driver_data = cdev; /* claim */
743
David Brownell33376c12008-08-18 17:45:07 -0700744 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
David Brownellda741b82008-06-19 18:19:46 -0700745 if (!ep)
746 goto fail;
747 ecm->port.out_ep = ep;
748 ep->driver_data = cdev; /* claim */
749
750 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
751 * don't treat it that way. It's simpler, and some newer CDC
752 * profiles (wireless handsets) no longer treat it as optional.
753 */
David Brownell33376c12008-08-18 17:45:07 -0700754 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
David Brownellda741b82008-06-19 18:19:46 -0700755 if (!ep)
756 goto fail;
757 ecm->notify = ep;
758 ep->driver_data = cdev; /* claim */
759
760 status = -ENOMEM;
761
762 /* allocate notification request and buffer */
763 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
764 if (!ecm->notify_req)
765 goto fail;
David Brownell33376c12008-08-18 17:45:07 -0700766 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
David Brownellda741b82008-06-19 18:19:46 -0700767 if (!ecm->notify_req->buf)
768 goto fail;
769 ecm->notify_req->context = ecm;
770 ecm->notify_req->complete = ecm_notify_complete;
771
772 /* copy descriptors, and track endpoint copies */
David Brownell33376c12008-08-18 17:45:07 -0700773 f->descriptors = usb_copy_descriptors(ecm_fs_function);
David Brownellda741b82008-06-19 18:19:46 -0700774 if (!f->descriptors)
775 goto fail;
776
David Brownellda741b82008-06-19 18:19:46 -0700777 /* support all relevant hardware speeds... we expect that when
778 * hardware is dual speed, all bulk-capable endpoints work at
779 * both speeds
780 */
781 if (gadget_is_dualspeed(c->cdev->gadget)) {
David Brownell33376c12008-08-18 17:45:07 -0700782 hs_ecm_in_desc.bEndpointAddress =
783 fs_ecm_in_desc.bEndpointAddress;
784 hs_ecm_out_desc.bEndpointAddress =
785 fs_ecm_out_desc.bEndpointAddress;
786 hs_ecm_notify_desc.bEndpointAddress =
787 fs_ecm_notify_desc.bEndpointAddress;
David Brownellda741b82008-06-19 18:19:46 -0700788
789 /* copy descriptors, and track endpoint copies */
David Brownell33376c12008-08-18 17:45:07 -0700790 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
David Brownellda741b82008-06-19 18:19:46 -0700791 if (!f->hs_descriptors)
792 goto fail;
David Brownellda741b82008-06-19 18:19:46 -0700793 }
794
Paul Zimmerman04617db2011-06-27 14:13:18 -0700795 if (gadget_is_superspeed(c->cdev->gadget)) {
796 ss_ecm_in_desc.bEndpointAddress =
797 fs_ecm_in_desc.bEndpointAddress;
798 ss_ecm_out_desc.bEndpointAddress =
799 fs_ecm_out_desc.bEndpointAddress;
800 ss_ecm_notify_desc.bEndpointAddress =
801 fs_ecm_notify_desc.bEndpointAddress;
802
803 /* copy descriptors, and track endpoint copies */
804 f->ss_descriptors = usb_copy_descriptors(ecm_ss_function);
805 if (!f->ss_descriptors)
806 goto fail;
807 }
808
David Brownellda741b82008-06-19 18:19:46 -0700809 /* NOTE: all that is done without knowing or caring about
810 * the network link ... which is unavailable to this code
811 * until we're activated via set_alt().
812 */
813
814 ecm->port.open = ecm_open;
815 ecm->port.close = ecm_close;
816
817 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700818 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownellda741b82008-06-19 18:19:46 -0700819 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
820 ecm->port.in_ep->name, ecm->port.out_ep->name,
821 ecm->notify->name);
822 return 0;
823
824fail:
825 if (f->descriptors)
826 usb_free_descriptors(f->descriptors);
Paul Zimmerman04617db2011-06-27 14:13:18 -0700827 if (f->hs_descriptors)
828 usb_free_descriptors(f->hs_descriptors);
David Brownellda741b82008-06-19 18:19:46 -0700829
830 if (ecm->notify_req) {
831 kfree(ecm->notify_req->buf);
832 usb_ep_free_request(ecm->notify, ecm->notify_req);
833 }
834
835 /* we might as well release our claims on endpoints */
836 if (ecm->notify)
837 ecm->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300838 if (ecm->port.out_ep->desc)
David Brownellda741b82008-06-19 18:19:46 -0700839 ecm->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300840 if (ecm->port.in_ep->desc)
David Brownellda741b82008-06-19 18:19:46 -0700841 ecm->port.in_ep->driver_data = NULL;
842
843 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
844
845 return status;
846}
847
848static void
849ecm_unbind(struct usb_configuration *c, struct usb_function *f)
850{
851 struct f_ecm *ecm = func_to_ecm(f);
852
853 DBG(c->cdev, "ecm unbind\n");
854
Paul Zimmerman04617db2011-06-27 14:13:18 -0700855 if (gadget_is_superspeed(c->cdev->gadget))
856 usb_free_descriptors(f->ss_descriptors);
David Brownellda741b82008-06-19 18:19:46 -0700857 if (gadget_is_dualspeed(c->cdev->gadget))
858 usb_free_descriptors(f->hs_descriptors);
859 usb_free_descriptors(f->descriptors);
860
861 kfree(ecm->notify_req->buf);
862 usb_ep_free_request(ecm->notify, ecm->notify_req);
863
864 ecm_string_defs[1].s = NULL;
865 kfree(ecm);
866}
867
868/**
869 * ecm_bind_config - add CDC Ethernet network link to a configuration
870 * @c: the configuration to support the network link
871 * @ethaddr: a buffer in which the ethernet address of the host side
872 * side of the link was recorded
873 * Context: single threaded during gadget setup
874 *
875 * Returns zero on success, else negative errno.
876 *
877 * Caller must have called @gether_setup(). Caller is also responsible
878 * for calling @gether_cleanup() before module unload.
879 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200880int
881ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownellda741b82008-06-19 18:19:46 -0700882{
883 struct f_ecm *ecm;
884 int status;
885
886 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
887 return -EINVAL;
888
889 /* maybe allocate device-global string IDs */
890 if (ecm_string_defs[0].id == 0) {
891
892 /* control interface label */
893 status = usb_string_id(c->cdev);
894 if (status < 0)
895 return status;
896 ecm_string_defs[0].id = status;
897 ecm_control_intf.iInterface = status;
898
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700899#ifdef CONFIG_USB_CDC_ECM
900 /* MAC address */
901 status = usb_string_id(c->cdev);
902 if (status < 0)
903 return status;
904 ecm_string_defs[1].id = status;
905 pr_info("%s: iMACAddress = %d\n", __func__, status);
906 ecm_desc.iMACAddress = status;
907#endif
908
David Brownellda741b82008-06-19 18:19:46 -0700909 /* data interface label */
910 status = usb_string_id(c->cdev);
911 if (status < 0)
912 return status;
913 ecm_string_defs[2].id = status;
914 ecm_data_intf.iInterface = status;
915
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700916#ifndef CONFIG_USB_CDC_ECM
David Brownellda741b82008-06-19 18:19:46 -0700917 /* MAC address */
918 status = usb_string_id(c->cdev);
919 if (status < 0)
920 return status;
921 ecm_string_defs[1].id = status;
David Brownell33376c12008-08-18 17:45:07 -0700922 ecm_desc.iMACAddress = status;
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700923#endif
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100924
925 /* IAD label */
926 status = usb_string_id(c->cdev);
927 if (status < 0)
928 return status;
929 ecm_string_defs[3].id = status;
930 ecm_iad_descriptor.iFunction = status;
David Brownellda741b82008-06-19 18:19:46 -0700931 }
932
933 /* allocate and initialize one new instance */
934 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
935 if (!ecm)
936 return -ENOMEM;
937
938 /* export host's Ethernet address in CDC format */
939 snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
940 "%02X%02X%02X%02X%02X%02X",
941 ethaddr[0], ethaddr[1], ethaddr[2],
942 ethaddr[3], ethaddr[4], ethaddr[5]);
943 ecm_string_defs[1].s = ecm->ethaddr;
944
945 ecm->port.cdc_filter = DEFAULT_FILTER;
946
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700947#ifdef CONFIG_USB_CDC_ECM
948 ecm->port.func.name = "ecm";
949#else
David Brownellda741b82008-06-19 18:19:46 -0700950 ecm->port.func.name = "cdc_ethernet";
Devin Kim0c5b9ce2012-06-19 21:34:44 -0700951#endif
David Brownellda741b82008-06-19 18:19:46 -0700952 ecm->port.func.strings = ecm_strings;
953 /* descriptors are per-instance copies */
954 ecm->port.func.bind = ecm_bind;
955 ecm->port.func.unbind = ecm_unbind;
956 ecm->port.func.set_alt = ecm_set_alt;
957 ecm->port.func.get_alt = ecm_get_alt;
958 ecm->port.func.setup = ecm_setup;
959 ecm->port.func.disable = ecm_disable;
960
961 status = usb_add_function(c, &ecm->port.func);
962 if (status) {
963 ecm_string_defs[1].s = NULL;
964 kfree(ecm);
965 }
966 return status;
967}