blob: 83420a310fb71784db5b269bf39e7939ef9af1b3 [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
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +020094#define ECM_STATUS_INTERVAL_MS 32
David Brownell33376c12008-08-18 17:45:07 -070095#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
David Brownellda741b82008-06-19 18:19:46 -070096
97
98/* interface descriptor: */
99
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100100static struct usb_interface_assoc_descriptor
101ecm_iad_descriptor = {
102 .bLength = sizeof ecm_iad_descriptor,
103 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
104
105 /* .bFirstInterface = DYNAMIC, */
106 .bInterfaceCount = 2, /* control + data */
107 .bFunctionClass = USB_CLASS_COMM,
108 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
109 .bFunctionProtocol = USB_CDC_PROTO_NONE,
110 /* .iFunction = DYNAMIC */
111};
112
113
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200114static struct usb_interface_descriptor ecm_control_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700115 .bLength = sizeof ecm_control_intf,
116 .bDescriptorType = USB_DT_INTERFACE,
117
118 /* .bInterfaceNumber = DYNAMIC */
119 /* status endpoint is optional; this could be patched later */
120 .bNumEndpoints = 1,
121 .bInterfaceClass = USB_CLASS_COMM,
122 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
123 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
124 /* .iInterface = DYNAMIC */
125};
126
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200127static struct usb_cdc_header_desc ecm_header_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700128 .bLength = sizeof ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700129 .bDescriptorType = USB_DT_CS_INTERFACE,
130 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
131
Harvey Harrison551509d2009-02-11 14:11:36 -0800132 .bcdCDC = cpu_to_le16(0x0110),
David Brownellda741b82008-06-19 18:19:46 -0700133};
134
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200135static struct usb_cdc_union_desc ecm_union_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700136 .bLength = sizeof(ecm_union_desc),
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubType = USB_CDC_UNION_TYPE,
139 /* .bMasterInterface0 = DYNAMIC */
140 /* .bSlaveInterface0 = DYNAMIC */
141};
142
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200143static struct usb_cdc_ether_desc ecm_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700144 .bLength = sizeof ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700145 .bDescriptorType = USB_DT_CS_INTERFACE,
146 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
147
148 /* this descriptor actually adds value, surprise! */
149 /* .iMACAddress = DYNAMIC */
Harvey Harrison551509d2009-02-11 14:11:36 -0800150 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
151 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
152 .wNumberMCFilters = cpu_to_le16(0),
David Brownellda741b82008-06-19 18:19:46 -0700153 .bNumberPowerFilters = 0,
154};
155
156/* the default data interface has no endpoints ... */
157
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200158static struct usb_interface_descriptor ecm_data_nop_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700159 .bLength = sizeof ecm_data_nop_intf,
160 .bDescriptorType = USB_DT_INTERFACE,
161
162 .bInterfaceNumber = 1,
163 .bAlternateSetting = 0,
164 .bNumEndpoints = 0,
165 .bInterfaceClass = USB_CLASS_CDC_DATA,
166 .bInterfaceSubClass = 0,
167 .bInterfaceProtocol = 0,
168 /* .iInterface = DYNAMIC */
169};
170
171/* ... but the "real" data interface has two bulk endpoints */
172
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200173static struct usb_interface_descriptor ecm_data_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700174 .bLength = sizeof ecm_data_intf,
175 .bDescriptorType = USB_DT_INTERFACE,
176
177 .bInterfaceNumber = 1,
178 .bAlternateSetting = 1,
179 .bNumEndpoints = 2,
180 .bInterfaceClass = USB_CLASS_CDC_DATA,
181 .bInterfaceSubClass = 0,
182 .bInterfaceProtocol = 0,
183 /* .iInterface = DYNAMIC */
184};
185
186/* full speed support: */
187
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200188static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700189 .bLength = USB_DT_ENDPOINT_SIZE,
190 .bDescriptorType = USB_DT_ENDPOINT,
191
192 .bEndpointAddress = USB_DIR_IN,
193 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800194 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200195 .bInterval = ECM_STATUS_INTERVAL_MS,
David Brownellda741b82008-06-19 18:19:46 -0700196};
197
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200198static struct usb_endpoint_descriptor fs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700199 .bLength = USB_DT_ENDPOINT_SIZE,
200 .bDescriptorType = USB_DT_ENDPOINT,
201
202 .bEndpointAddress = USB_DIR_IN,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204};
205
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200206static struct usb_endpoint_descriptor fs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700207 .bLength = USB_DT_ENDPOINT_SIZE,
208 .bDescriptorType = USB_DT_ENDPOINT,
209
210 .bEndpointAddress = USB_DIR_OUT,
211 .bmAttributes = USB_ENDPOINT_XFER_BULK,
212};
213
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200214static struct usb_descriptor_header *ecm_fs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700215 /* CDC ECM control descriptors */
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100216 (struct usb_descriptor_header *) &ecm_iad_descriptor,
David Brownellda741b82008-06-19 18:19:46 -0700217 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700218 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700219 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700220 (struct usb_descriptor_header *) &ecm_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700221
David Brownellda741b82008-06-19 18:19:46 -0700222 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700223 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700224
David Brownellda741b82008-06-19 18:19:46 -0700225 /* data interface, altsettings 0 and 1 */
226 (struct usb_descriptor_header *) &ecm_data_nop_intf,
227 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700228 (struct usb_descriptor_header *) &fs_ecm_in_desc,
229 (struct usb_descriptor_header *) &fs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700230 NULL,
231};
232
233/* high speed support: */
234
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200235static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
238
239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800241 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200242 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
David Brownellda741b82008-06-19 18:19:46 -0700243};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700244
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200245static struct usb_endpoint_descriptor hs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_IN,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800251 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700252};
253
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200254static struct usb_endpoint_descriptor hs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT,
257
258 .bEndpointAddress = USB_DIR_OUT,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800260 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700261};
262
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200263static struct usb_descriptor_header *ecm_hs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700264 /* CDC ECM control descriptors */
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100265 (struct usb_descriptor_header *) &ecm_iad_descriptor,
David Brownellda741b82008-06-19 18:19:46 -0700266 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700267 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700268 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700269 (struct usb_descriptor_header *) &ecm_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700270
David Brownellda741b82008-06-19 18:19:46 -0700271 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700272 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700273
David Brownellda741b82008-06-19 18:19:46 -0700274 /* data interface, altsettings 0 and 1 */
275 (struct usb_descriptor_header *) &ecm_data_nop_intf,
276 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700277 (struct usb_descriptor_header *) &hs_ecm_in_desc,
278 (struct usb_descriptor_header *) &hs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700279 NULL,
280};
281
Paul Zimmerman04617db2011-06-27 14:13:18 -0700282/* super speed support: */
283
284static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
285 .bLength = USB_DT_ENDPOINT_SIZE,
286 .bDescriptorType = USB_DT_ENDPOINT,
287
288 .bEndpointAddress = USB_DIR_IN,
289 .bmAttributes = USB_ENDPOINT_XFER_INT,
290 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200291 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
Paul Zimmerman04617db2011-06-27 14:13:18 -0700292};
293
294static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
295 .bLength = sizeof ss_ecm_intr_comp_desc,
296 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
297
298 /* the following 3 values can be tweaked if necessary */
299 /* .bMaxBurst = 0, */
300 /* .bmAttributes = 0, */
301 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
302};
303
304static struct usb_endpoint_descriptor ss_ecm_in_desc = {
305 .bLength = USB_DT_ENDPOINT_SIZE,
306 .bDescriptorType = USB_DT_ENDPOINT,
307
308 .bEndpointAddress = USB_DIR_IN,
309 .bmAttributes = USB_ENDPOINT_XFER_BULK,
310 .wMaxPacketSize = cpu_to_le16(1024),
311};
312
313static struct usb_endpoint_descriptor ss_ecm_out_desc = {
314 .bLength = USB_DT_ENDPOINT_SIZE,
315 .bDescriptorType = USB_DT_ENDPOINT,
316
317 .bEndpointAddress = USB_DIR_OUT,
318 .bmAttributes = USB_ENDPOINT_XFER_BULK,
319 .wMaxPacketSize = cpu_to_le16(1024),
320};
321
322static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
323 .bLength = sizeof ss_ecm_bulk_comp_desc,
324 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
325
326 /* the following 2 values can be tweaked if necessary */
327 /* .bMaxBurst = 0, */
328 /* .bmAttributes = 0, */
329};
330
331static struct usb_descriptor_header *ecm_ss_function[] = {
332 /* CDC ECM control descriptors */
Sebastian Andrzej Siewior585e3932012-10-22 22:14:55 +0200333 (struct usb_descriptor_header *) &ecm_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700334 (struct usb_descriptor_header *) &ecm_control_intf,
335 (struct usb_descriptor_header *) &ecm_header_desc,
336 (struct usb_descriptor_header *) &ecm_union_desc,
337 (struct usb_descriptor_header *) &ecm_desc,
338
339 /* NOTE: status endpoint might need to be removed */
340 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
341 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
342
343 /* data interface, altsettings 0 and 1 */
344 (struct usb_descriptor_header *) &ecm_data_nop_intf,
345 (struct usb_descriptor_header *) &ecm_data_intf,
346 (struct usb_descriptor_header *) &ss_ecm_in_desc,
347 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
348 (struct usb_descriptor_header *) &ss_ecm_out_desc,
349 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
350 NULL,
351};
352
David Brownellda741b82008-06-19 18:19:46 -0700353/* string descriptors: */
354
355static struct usb_string ecm_string_defs[] = {
356 [0].s = "CDC Ethernet Control Model (ECM)",
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200357 [1].s = "",
David Brownellda741b82008-06-19 18:19:46 -0700358 [2].s = "CDC Ethernet Data",
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100359 [3].s = "CDC ECM",
David Brownellda741b82008-06-19 18:19:46 -0700360 { } /* end of list */
361};
362
363static struct usb_gadget_strings ecm_string_table = {
364 .language = 0x0409, /* en-us */
365 .strings = ecm_string_defs,
366};
367
368static struct usb_gadget_strings *ecm_strings[] = {
369 &ecm_string_table,
370 NULL,
371};
372
373/*-------------------------------------------------------------------------*/
374
375static void ecm_do_notify(struct f_ecm *ecm)
376{
377 struct usb_request *req = ecm->notify_req;
378 struct usb_cdc_notification *event;
379 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
380 __le32 *data;
381 int status;
382
383 /* notification already in flight? */
384 if (!req)
385 return;
386
387 event = req->buf;
388 switch (ecm->notify_state) {
389 case ECM_NOTIFY_NONE:
390 return;
391
392 case ECM_NOTIFY_CONNECT:
393 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
394 if (ecm->is_open)
395 event->wValue = cpu_to_le16(1);
396 else
397 event->wValue = cpu_to_le16(0);
398 event->wLength = 0;
399 req->length = sizeof *event;
400
401 DBG(cdev, "notify connect %s\n",
402 ecm->is_open ? "true" : "false");
403 ecm->notify_state = ECM_NOTIFY_SPEED;
404 break;
405
406 case ECM_NOTIFY_SPEED:
407 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
408 event->wValue = cpu_to_le16(0);
409 event->wLength = cpu_to_le16(8);
David Brownell33376c12008-08-18 17:45:07 -0700410 req->length = ECM_STATUS_BYTECOUNT;
David Brownellda741b82008-06-19 18:19:46 -0700411
412 /* SPEED_CHANGE data is up/down speeds in bits/sec */
413 data = req->buf + sizeof *event;
David Brownell33376c12008-08-18 17:45:07 -0700414 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700415 data[1] = data[0];
416
David Brownell33376c12008-08-18 17:45:07 -0700417 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700418 ecm->notify_state = ECM_NOTIFY_NONE;
419 break;
420 }
421 event->bmRequestType = 0xA1;
422 event->wIndex = cpu_to_le16(ecm->ctrl_id);
423
424 ecm->notify_req = NULL;
425 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
426 if (status < 0) {
427 ecm->notify_req = req;
428 DBG(cdev, "notify --> %d\n", status);
429 }
430}
431
432static void ecm_notify(struct f_ecm *ecm)
433{
434 /* NOTE on most versions of Linux, host side cdc-ethernet
435 * won't listen for notifications until its netdevice opens.
436 * The first notification then sits in the FIFO for a long
437 * time, and the second one is queued.
438 */
439 ecm->notify_state = ECM_NOTIFY_CONNECT;
440 ecm_do_notify(ecm);
441}
442
443static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
444{
445 struct f_ecm *ecm = req->context;
446 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
447 struct usb_cdc_notification *event = req->buf;
448
449 switch (req->status) {
450 case 0:
451 /* no fault */
452 break;
453 case -ECONNRESET:
454 case -ESHUTDOWN:
455 ecm->notify_state = ECM_NOTIFY_NONE;
456 break;
457 default:
458 DBG(cdev, "event %02x --> %d\n",
459 event->bNotificationType, req->status);
460 break;
461 }
462 ecm->notify_req = req;
463 ecm_do_notify(ecm);
464}
465
466static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
467{
468 struct f_ecm *ecm = func_to_ecm(f);
469 struct usb_composite_dev *cdev = f->config->cdev;
470 struct usb_request *req = cdev->req;
471 int value = -EOPNOTSUPP;
472 u16 w_index = le16_to_cpu(ctrl->wIndex);
473 u16 w_value = le16_to_cpu(ctrl->wValue);
474 u16 w_length = le16_to_cpu(ctrl->wLength);
475
476 /* composite driver infrastructure handles everything except
477 * CDC class messages; interface activation uses set_alt().
478 */
479 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
480 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
481 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
482 /* see 6.2.30: no data, wIndex = interface,
483 * wValue = packet filter bitmap
484 */
485 if (w_length != 0 || w_index != ecm->ctrl_id)
486 goto invalid;
487 DBG(cdev, "packet filter %02x\n", w_value);
488 /* REVISIT locking of cdc_filter. This assumes the UDC
489 * driver won't have a concurrent packet TX irq running on
490 * another CPU; or that if it does, this write is atomic...
491 */
492 ecm->port.cdc_filter = w_value;
493 value = 0;
494 break;
495
496 /* and optionally:
497 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
498 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
499 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
500 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
501 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
502 * case USB_CDC_GET_ETHERNET_STATISTIC:
503 */
504
505 default:
506invalid:
507 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
508 ctrl->bRequestType, ctrl->bRequest,
509 w_value, w_index, w_length);
510 }
511
512 /* respond with data transfer or status phase? */
513 if (value >= 0) {
514 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
515 ctrl->bRequestType, ctrl->bRequest,
516 w_value, w_index, w_length);
517 req->zero = 0;
518 req->length = value;
519 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
520 if (value < 0)
521 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
522 ctrl->bRequestType, ctrl->bRequest,
523 value);
524 }
525
526 /* device either stalls (value < 0) or reports success */
527 return value;
528}
529
530
531static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
532{
533 struct f_ecm *ecm = func_to_ecm(f);
534 struct usb_composite_dev *cdev = f->config->cdev;
535
536 /* Control interface has only altsetting 0 */
537 if (intf == ecm->ctrl_id) {
538 if (alt != 0)
539 goto fail;
540
541 if (ecm->notify->driver_data) {
542 VDBG(cdev, "reset ecm control %d\n", intf);
543 usb_ep_disable(ecm->notify);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300544 }
545 if (!(ecm->notify->desc)) {
David Brownellda741b82008-06-19 18:19:46 -0700546 VDBG(cdev, "init ecm ctrl %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300547 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
548 goto fail;
David Brownellda741b82008-06-19 18:19:46 -0700549 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300550 usb_ep_enable(ecm->notify);
David Brownellda741b82008-06-19 18:19:46 -0700551 ecm->notify->driver_data = ecm;
552
553 /* Data interface has two altsettings, 0 and 1 */
554 } else if (intf == ecm->data_id) {
555 if (alt > 1)
556 goto fail;
557
558 if (ecm->port.in_ep->driver_data) {
559 DBG(cdev, "reset ecm\n");
560 gether_disconnect(&ecm->port);
561 }
562
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300563 if (!ecm->port.in_ep->desc ||
564 !ecm->port.out_ep->desc) {
David Brownellda741b82008-06-19 18:19:46 -0700565 DBG(cdev, "init ecm\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300566 if (config_ep_by_speed(cdev->gadget, f,
567 ecm->port.in_ep) ||
568 config_ep_by_speed(cdev->gadget, f,
569 ecm->port.out_ep)) {
570 ecm->port.in_ep->desc = NULL;
571 ecm->port.out_ep->desc = NULL;
572 goto fail;
573 }
David Brownellda741b82008-06-19 18:19:46 -0700574 }
575
576 /* CDC Ethernet only sends data in non-default altsettings.
577 * Changing altsettings resets filters, statistics, etc.
578 */
579 if (alt == 1) {
580 struct net_device *net;
581
582 /* Enable zlps by default for ECM conformance;
Christoph Egger90f79762010-02-05 13:24:12 +0100583 * override for musb_hdrc (avoids txdma ovhead).
David Brownellda741b82008-06-19 18:19:46 -0700584 */
Christoph Egger90f79762010-02-05 13:24:12 +0100585 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
David Brownellda741b82008-06-19 18:19:46 -0700586 );
587 ecm->port.cdc_filter = DEFAULT_FILTER;
588 DBG(cdev, "activate ecm\n");
589 net = gether_connect(&ecm->port);
590 if (IS_ERR(net))
591 return PTR_ERR(net);
592 }
593
594 /* NOTE this can be a minor disagreement with the ECM spec,
595 * which says speed notifications will "always" follow
596 * connection notifications. But we allow one connect to
597 * follow another (if the first is in flight), and instead
598 * just guarantee that a speed notification is always sent.
599 */
600 ecm_notify(ecm);
601 } else
602 goto fail;
603
604 return 0;
605fail:
606 return -EINVAL;
607}
608
609/* Because the data interface supports multiple altsettings,
610 * this ECM function *MUST* implement a get_alt() method.
611 */
612static int ecm_get_alt(struct usb_function *f, unsigned intf)
613{
614 struct f_ecm *ecm = func_to_ecm(f);
615
616 if (intf == ecm->ctrl_id)
617 return 0;
618 return ecm->port.in_ep->driver_data ? 1 : 0;
619}
620
621static void ecm_disable(struct usb_function *f)
622{
623 struct f_ecm *ecm = func_to_ecm(f);
624 struct usb_composite_dev *cdev = f->config->cdev;
625
626 DBG(cdev, "ecm deactivated\n");
627
628 if (ecm->port.in_ep->driver_data)
629 gether_disconnect(&ecm->port);
630
631 if (ecm->notify->driver_data) {
632 usb_ep_disable(ecm->notify);
633 ecm->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300634 ecm->notify->desc = NULL;
David Brownellda741b82008-06-19 18:19:46 -0700635 }
636}
637
638/*-------------------------------------------------------------------------*/
639
640/*
641 * Callbacks let us notify the host about connect/disconnect when the
642 * net device is opened or closed.
643 *
644 * For testing, note that link states on this side include both opened
645 * and closed variants of:
646 *
647 * - disconnected/unconfigured
648 * - configured but inactive (data alt 0)
649 * - configured and active (data alt 1)
650 *
651 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
652 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
653 * imply the host is actually polling the notification endpoint, and
654 * likewise that "active" doesn't imply it's actually using the data
655 * endpoints for traffic.
656 */
657
658static void ecm_open(struct gether *geth)
659{
660 struct f_ecm *ecm = func_to_ecm(&geth->func);
661
662 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
663
664 ecm->is_open = true;
665 ecm_notify(ecm);
666}
667
668static void ecm_close(struct gether *geth)
669{
670 struct f_ecm *ecm = func_to_ecm(&geth->func);
671
672 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
673
674 ecm->is_open = false;
675 ecm_notify(ecm);
676}
677
678/*-------------------------------------------------------------------------*/
679
680/* ethernet function driver setup/binding */
681
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200682static int
David Brownellda741b82008-06-19 18:19:46 -0700683ecm_bind(struct usb_configuration *c, struct usb_function *f)
684{
685 struct usb_composite_dev *cdev = c->cdev;
686 struct f_ecm *ecm = func_to_ecm(f);
687 int status;
688 struct usb_ep *ep;
689
690 /* allocate instance-specific interface IDs */
691 status = usb_interface_id(c, f);
692 if (status < 0)
693 goto fail;
694 ecm->ctrl_id = status;
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100695 ecm_iad_descriptor.bFirstInterface = status;
David Brownellda741b82008-06-19 18:19:46 -0700696
697 ecm_control_intf.bInterfaceNumber = status;
698 ecm_union_desc.bMasterInterface0 = status;
699
700 status = usb_interface_id(c, f);
701 if (status < 0)
702 goto fail;
703 ecm->data_id = status;
704
705 ecm_data_nop_intf.bInterfaceNumber = status;
706 ecm_data_intf.bInterfaceNumber = status;
707 ecm_union_desc.bSlaveInterface0 = status;
708
709 status = -ENODEV;
710
711 /* allocate instance-specific endpoints */
David Brownell33376c12008-08-18 17:45:07 -0700712 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
David Brownellda741b82008-06-19 18:19:46 -0700713 if (!ep)
714 goto fail;
715 ecm->port.in_ep = ep;
716 ep->driver_data = cdev; /* claim */
717
David Brownell33376c12008-08-18 17:45:07 -0700718 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
David Brownellda741b82008-06-19 18:19:46 -0700719 if (!ep)
720 goto fail;
721 ecm->port.out_ep = ep;
722 ep->driver_data = cdev; /* claim */
723
724 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
725 * don't treat it that way. It's simpler, and some newer CDC
726 * profiles (wireless handsets) no longer treat it as optional.
727 */
David Brownell33376c12008-08-18 17:45:07 -0700728 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
David Brownellda741b82008-06-19 18:19:46 -0700729 if (!ep)
730 goto fail;
731 ecm->notify = ep;
732 ep->driver_data = cdev; /* claim */
733
734 status = -ENOMEM;
735
736 /* allocate notification request and buffer */
737 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
738 if (!ecm->notify_req)
739 goto fail;
David Brownell33376c12008-08-18 17:45:07 -0700740 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
David Brownellda741b82008-06-19 18:19:46 -0700741 if (!ecm->notify_req->buf)
742 goto fail;
743 ecm->notify_req->context = ecm;
744 ecm->notify_req->complete = ecm_notify_complete;
745
David Brownellda741b82008-06-19 18:19:46 -0700746 /* support all relevant hardware speeds... we expect that when
747 * hardware is dual speed, all bulk-capable endpoints work at
748 * both speeds
749 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200750 hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
751 hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
752 hs_ecm_notify_desc.bEndpointAddress =
753 fs_ecm_notify_desc.bEndpointAddress;
David Brownellda741b82008-06-19 18:19:46 -0700754
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200755 ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
756 ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
757 ss_ecm_notify_desc.bEndpointAddress =
758 fs_ecm_notify_desc.bEndpointAddress;
David Brownellda741b82008-06-19 18:19:46 -0700759
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200760 status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
761 ecm_ss_function);
762 if (status)
763 goto fail;
Paul Zimmerman04617db2011-06-27 14:13:18 -0700764
David Brownellda741b82008-06-19 18:19:46 -0700765 /* NOTE: all that is done without knowing or caring about
766 * the network link ... which is unavailable to this code
767 * until we're activated via set_alt().
768 */
769
770 ecm->port.open = ecm_open;
771 ecm->port.close = ecm_close;
772
773 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700774 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownellda741b82008-06-19 18:19:46 -0700775 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
776 ecm->port.in_ep->name, ecm->port.out_ep->name,
777 ecm->notify->name);
778 return 0;
779
780fail:
David Brownellda741b82008-06-19 18:19:46 -0700781 if (ecm->notify_req) {
782 kfree(ecm->notify_req->buf);
783 usb_ep_free_request(ecm->notify, ecm->notify_req);
784 }
785
786 /* we might as well release our claims on endpoints */
787 if (ecm->notify)
788 ecm->notify->driver_data = NULL;
Sebastian Andrzej Siewiore79cc612012-10-22 22:15:00 +0200789 if (ecm->port.out_ep)
David Brownellda741b82008-06-19 18:19:46 -0700790 ecm->port.out_ep->driver_data = NULL;
Sebastian Andrzej Siewiore79cc612012-10-22 22:15:00 +0200791 if (ecm->port.in_ep)
David Brownellda741b82008-06-19 18:19:46 -0700792 ecm->port.in_ep->driver_data = NULL;
793
794 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
795
796 return status;
797}
798
799static void
800ecm_unbind(struct usb_configuration *c, struct usb_function *f)
801{
802 struct f_ecm *ecm = func_to_ecm(f);
803
804 DBG(c->cdev, "ecm unbind\n");
805
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200806 ecm_string_defs[0].id = 0;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200807 usb_free_all_descriptors(f);
David Brownellda741b82008-06-19 18:19:46 -0700808
809 kfree(ecm->notify_req->buf);
810 usb_ep_free_request(ecm->notify, ecm->notify_req);
David Brownellda741b82008-06-19 18:19:46 -0700811 kfree(ecm);
812}
813
814/**
815 * ecm_bind_config - add CDC Ethernet network link to a configuration
816 * @c: the configuration to support the network link
817 * @ethaddr: a buffer in which the ethernet address of the host side
818 * side of the link was recorded
819 * Context: single threaded during gadget setup
820 *
821 * Returns zero on success, else negative errno.
822 *
823 * Caller must have called @gether_setup(). Caller is also responsible
824 * for calling @gether_cleanup() before module unload.
825 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200826int
827ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownellda741b82008-06-19 18:19:46 -0700828{
829 struct f_ecm *ecm;
830 int status;
831
832 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
833 return -EINVAL;
834
David Brownellda741b82008-06-19 18:19:46 -0700835 if (ecm_string_defs[0].id == 0) {
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200836 status = usb_string_ids_tab(c->cdev, ecm_string_defs);
837 if (status)
David Brownellda741b82008-06-19 18:19:46 -0700838 return status;
David Brownellda741b82008-06-19 18:19:46 -0700839
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200840 ecm_control_intf.iInterface = ecm_string_defs[0].id;
841 ecm_data_intf.iInterface = ecm_string_defs[2].id;
842 ecm_desc.iMACAddress = ecm_string_defs[1].id;
843 ecm_iad_descriptor.iFunction = ecm_string_defs[3].id;
David Brownellda741b82008-06-19 18:19:46 -0700844 }
845
846 /* allocate and initialize one new instance */
847 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
848 if (!ecm)
849 return -ENOMEM;
850
851 /* export host's Ethernet address in CDC format */
Andy Shevchenko8c7ca992012-07-06 18:30:21 +0300852 snprintf(ecm->ethaddr, sizeof ecm->ethaddr, "%pm", ethaddr);
David Brownellda741b82008-06-19 18:19:46 -0700853 ecm_string_defs[1].s = ecm->ethaddr;
854
855 ecm->port.cdc_filter = DEFAULT_FILTER;
856
857 ecm->port.func.name = "cdc_ethernet";
858 ecm->port.func.strings = ecm_strings;
859 /* descriptors are per-instance copies */
860 ecm->port.func.bind = ecm_bind;
861 ecm->port.func.unbind = ecm_unbind;
862 ecm->port.func.set_alt = ecm_set_alt;
863 ecm->port.func.get_alt = ecm_get_alt;
864 ecm->port.func.setup = ecm_setup;
865 ecm->port.func.disable = ecm_disable;
866
867 status = usb_add_function(c, &ecm->port.func);
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200868 if (status)
David Brownellda741b82008-06-19 18:19:46 -0700869 kfree(ecm);
David Brownellda741b82008-06-19 18:19:46 -0700870 return status;
871}