blob: edab45da37417e16960b75033a46a8453475b77b [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>
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +020017#include <linux/module.h>
David Brownellda741b82008-06-19 18:19:46 -070018#include <linux/device.h>
19#include <linux/etherdevice.h>
20
21#include "u_ether.h"
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +020022#include "u_ether_configfs.h"
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +020023#include "u_ecm.h"
David Brownellda741b82008-06-19 18:19:46 -070024
25
26/*
27 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
28 * Ethernet link. The data transfer model is simple (packets sent and
29 * received over bulk endpoints using normal short packet termination),
30 * and the control model exposes various data and optional notifications.
31 *
32 * ECM is well standardized and (except for Microsoft) supported by most
33 * operating systems with USB host support. It's the preferred interop
34 * solution for Ethernet over USB, at least for firmware based solutions.
35 * (Hardware solutions tend to be more minimalist.) A newer and simpler
36 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
37 *
38 * Note that ECM requires the use of "alternate settings" for its data
39 * interface. This means that the set_alt() method has real work to do,
40 * and also means that a get_alt() method is required.
41 */
42
David Brownellda741b82008-06-19 18:19:46 -070043
44enum ecm_notify_state {
45 ECM_NOTIFY_NONE, /* don't notify */
46 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
47 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
48};
49
50struct f_ecm {
51 struct gether port;
52 u8 ctrl_id, data_id;
53
54 char ethaddr[14];
55
David Brownellda741b82008-06-19 18:19:46 -070056 struct usb_ep *notify;
David Brownellda741b82008-06-19 18:19:46 -070057 struct usb_request *notify_req;
58 u8 notify_state;
59 bool is_open;
60
61 /* FIXME is_open needs some irq-ish locking
62 * ... possibly the same as port.ioport
63 */
64};
65
66static inline struct f_ecm *func_to_ecm(struct usb_function *f)
67{
68 return container_of(f, struct f_ecm, port.func);
69}
70
71/* peak (theoretical) bulk transfer rate in bits-per-second */
David Brownell33376c12008-08-18 17:45:07 -070072static inline unsigned ecm_bitrate(struct usb_gadget *g)
David Brownellda741b82008-06-19 18:19:46 -070073{
Paul Zimmerman04617db2011-06-27 14:13:18 -070074 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
75 return 13 * 1024 * 8 * 1000 * 8;
76 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
David Brownellda741b82008-06-19 18:19:46 -070077 return 13 * 512 * 8 * 1000 * 8;
78 else
Paul Zimmerman04617db2011-06-27 14:13:18 -070079 return 19 * 64 * 1 * 1000 * 8;
David Brownellda741b82008-06-19 18:19:46 -070080}
81
82/*-------------------------------------------------------------------------*/
83
84/*
85 * Include the status endpoint if we can, even though it's optional.
86 *
87 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
88 * packet, to simplify cancellation; and a big transfer interval, to
89 * waste less bandwidth.
90 *
91 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
92 * if they ignore the connect/disconnect notifications that real aether
93 * can provide. More advanced cdc configurations might want to support
94 * encapsulated commands (vendor-specific, using control-OUT).
95 */
96
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +020097#define ECM_STATUS_INTERVAL_MS 32
David Brownell33376c12008-08-18 17:45:07 -070098#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
David Brownellda741b82008-06-19 18:19:46 -070099
100
101/* interface descriptor: */
102
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100103static struct usb_interface_assoc_descriptor
104ecm_iad_descriptor = {
105 .bLength = sizeof ecm_iad_descriptor,
106 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
107
108 /* .bFirstInterface = DYNAMIC, */
109 .bInterfaceCount = 2, /* control + data */
110 .bFunctionClass = USB_CLASS_COMM,
111 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
112 .bFunctionProtocol = USB_CDC_PROTO_NONE,
113 /* .iFunction = DYNAMIC */
114};
115
116
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200117static struct usb_interface_descriptor ecm_control_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700118 .bLength = sizeof ecm_control_intf,
119 .bDescriptorType = USB_DT_INTERFACE,
120
121 /* .bInterfaceNumber = DYNAMIC */
122 /* status endpoint is optional; this could be patched later */
123 .bNumEndpoints = 1,
124 .bInterfaceClass = USB_CLASS_COMM,
125 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
126 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
127 /* .iInterface = DYNAMIC */
128};
129
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200130static struct usb_cdc_header_desc ecm_header_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700131 .bLength = sizeof ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
134
Harvey Harrison551509d2009-02-11 14:11:36 -0800135 .bcdCDC = cpu_to_le16(0x0110),
David Brownellda741b82008-06-19 18:19:46 -0700136};
137
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200138static struct usb_cdc_union_desc ecm_union_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700139 .bLength = sizeof(ecm_union_desc),
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_UNION_TYPE,
142 /* .bMasterInterface0 = DYNAMIC */
143 /* .bSlaveInterface0 = DYNAMIC */
144};
145
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200146static struct usb_cdc_ether_desc ecm_desc = {
David Brownell33376c12008-08-18 17:45:07 -0700147 .bLength = sizeof ecm_desc,
David Brownellda741b82008-06-19 18:19:46 -0700148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
150
151 /* this descriptor actually adds value, surprise! */
152 /* .iMACAddress = DYNAMIC */
Harvey Harrison551509d2009-02-11 14:11:36 -0800153 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
154 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
155 .wNumberMCFilters = cpu_to_le16(0),
David Brownellda741b82008-06-19 18:19:46 -0700156 .bNumberPowerFilters = 0,
157};
158
159/* the default data interface has no endpoints ... */
160
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200161static struct usb_interface_descriptor ecm_data_nop_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700162 .bLength = sizeof ecm_data_nop_intf,
163 .bDescriptorType = USB_DT_INTERFACE,
164
165 .bInterfaceNumber = 1,
166 .bAlternateSetting = 0,
167 .bNumEndpoints = 0,
168 .bInterfaceClass = USB_CLASS_CDC_DATA,
169 .bInterfaceSubClass = 0,
170 .bInterfaceProtocol = 0,
171 /* .iInterface = DYNAMIC */
172};
173
174/* ... but the "real" data interface has two bulk endpoints */
175
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200176static struct usb_interface_descriptor ecm_data_intf = {
David Brownellda741b82008-06-19 18:19:46 -0700177 .bLength = sizeof ecm_data_intf,
178 .bDescriptorType = USB_DT_INTERFACE,
179
180 .bInterfaceNumber = 1,
181 .bAlternateSetting = 1,
182 .bNumEndpoints = 2,
183 .bInterfaceClass = USB_CLASS_CDC_DATA,
184 .bInterfaceSubClass = 0,
185 .bInterfaceProtocol = 0,
186 /* .iInterface = DYNAMIC */
187};
188
189/* full speed support: */
190
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200191static struct usb_endpoint_descriptor fs_ecm_notify_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_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800197 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200198 .bInterval = ECM_STATUS_INTERVAL_MS,
David Brownellda741b82008-06-19 18:19:46 -0700199};
200
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200201static struct usb_endpoint_descriptor fs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
204
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_BULK,
207};
208
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200209static struct usb_endpoint_descriptor fs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212
213 .bEndpointAddress = USB_DIR_OUT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
215};
216
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200217static struct usb_descriptor_header *ecm_fs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700218 /* CDC ECM control descriptors */
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100219 (struct usb_descriptor_header *) &ecm_iad_descriptor,
David Brownellda741b82008-06-19 18:19:46 -0700220 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700221 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700222 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700223 (struct usb_descriptor_header *) &ecm_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700224
David Brownellda741b82008-06-19 18:19:46 -0700225 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700226 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700227
David Brownellda741b82008-06-19 18:19:46 -0700228 /* data interface, altsettings 0 and 1 */
229 (struct usb_descriptor_header *) &ecm_data_nop_intf,
230 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700231 (struct usb_descriptor_header *) &fs_ecm_in_desc,
232 (struct usb_descriptor_header *) &fs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700233 NULL,
234};
235
236/* high speed support: */
237
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200238static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700239 .bLength = USB_DT_ENDPOINT_SIZE,
240 .bDescriptorType = USB_DT_ENDPOINT,
241
242 .bEndpointAddress = USB_DIR_IN,
243 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800244 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200245 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
David Brownellda741b82008-06-19 18:19:46 -0700246};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700247
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200248static struct usb_endpoint_descriptor hs_ecm_in_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251
252 .bEndpointAddress = USB_DIR_IN,
253 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800254 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700255};
256
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200257static struct usb_endpoint_descriptor hs_ecm_out_desc = {
David Brownellda741b82008-06-19 18:19:46 -0700258 .bLength = USB_DT_ENDPOINT_SIZE,
259 .bDescriptorType = USB_DT_ENDPOINT,
260
261 .bEndpointAddress = USB_DIR_OUT,
262 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800263 .wMaxPacketSize = cpu_to_le16(512),
David Brownellda741b82008-06-19 18:19:46 -0700264};
265
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200266static struct usb_descriptor_header *ecm_hs_function[] = {
David Brownellda741b82008-06-19 18:19:46 -0700267 /* CDC ECM control descriptors */
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100268 (struct usb_descriptor_header *) &ecm_iad_descriptor,
David Brownellda741b82008-06-19 18:19:46 -0700269 (struct usb_descriptor_header *) &ecm_control_intf,
David Brownell33376c12008-08-18 17:45:07 -0700270 (struct usb_descriptor_header *) &ecm_header_desc,
David Brownellda741b82008-06-19 18:19:46 -0700271 (struct usb_descriptor_header *) &ecm_union_desc,
David Brownell33376c12008-08-18 17:45:07 -0700272 (struct usb_descriptor_header *) &ecm_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700273
David Brownellda741b82008-06-19 18:19:46 -0700274 /* NOTE: status endpoint might need to be removed */
David Brownell33376c12008-08-18 17:45:07 -0700275 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700276
David Brownellda741b82008-06-19 18:19:46 -0700277 /* data interface, altsettings 0 and 1 */
278 (struct usb_descriptor_header *) &ecm_data_nop_intf,
279 (struct usb_descriptor_header *) &ecm_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700280 (struct usb_descriptor_header *) &hs_ecm_in_desc,
281 (struct usb_descriptor_header *) &hs_ecm_out_desc,
David Brownellda741b82008-06-19 18:19:46 -0700282 NULL,
283};
284
Paul Zimmerman04617db2011-06-27 14:13:18 -0700285/* super speed support: */
286
287static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
288 .bLength = USB_DT_ENDPOINT_SIZE,
289 .bDescriptorType = USB_DT_ENDPOINT,
290
291 .bEndpointAddress = USB_DIR_IN,
292 .bmAttributes = USB_ENDPOINT_XFER_INT,
293 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200294 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
Paul Zimmerman04617db2011-06-27 14:13:18 -0700295};
296
297static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
298 .bLength = sizeof ss_ecm_intr_comp_desc,
299 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
300
301 /* the following 3 values can be tweaked if necessary */
302 /* .bMaxBurst = 0, */
303 /* .bmAttributes = 0, */
304 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
305};
306
307static struct usb_endpoint_descriptor ss_ecm_in_desc = {
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
310
311 .bEndpointAddress = USB_DIR_IN,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 .wMaxPacketSize = cpu_to_le16(1024),
314};
315
316static struct usb_endpoint_descriptor ss_ecm_out_desc = {
317 .bLength = USB_DT_ENDPOINT_SIZE,
318 .bDescriptorType = USB_DT_ENDPOINT,
319
320 .bEndpointAddress = USB_DIR_OUT,
321 .bmAttributes = USB_ENDPOINT_XFER_BULK,
322 .wMaxPacketSize = cpu_to_le16(1024),
323};
324
325static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
326 .bLength = sizeof ss_ecm_bulk_comp_desc,
327 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
328
329 /* the following 2 values can be tweaked if necessary */
330 /* .bMaxBurst = 0, */
331 /* .bmAttributes = 0, */
332};
333
334static struct usb_descriptor_header *ecm_ss_function[] = {
335 /* CDC ECM control descriptors */
Sebastian Andrzej Siewior585e3932012-10-22 22:14:55 +0200336 (struct usb_descriptor_header *) &ecm_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700337 (struct usb_descriptor_header *) &ecm_control_intf,
338 (struct usb_descriptor_header *) &ecm_header_desc,
339 (struct usb_descriptor_header *) &ecm_union_desc,
340 (struct usb_descriptor_header *) &ecm_desc,
341
342 /* NOTE: status endpoint might need to be removed */
343 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
344 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
345
346 /* data interface, altsettings 0 and 1 */
347 (struct usb_descriptor_header *) &ecm_data_nop_intf,
348 (struct usb_descriptor_header *) &ecm_data_intf,
349 (struct usb_descriptor_header *) &ss_ecm_in_desc,
350 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
351 (struct usb_descriptor_header *) &ss_ecm_out_desc,
352 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
353 NULL,
354};
355
David Brownellda741b82008-06-19 18:19:46 -0700356/* string descriptors: */
357
358static struct usb_string ecm_string_defs[] = {
359 [0].s = "CDC Ethernet Control Model (ECM)",
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200360 [1].s = "",
David Brownellda741b82008-06-19 18:19:46 -0700361 [2].s = "CDC Ethernet Data",
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100362 [3].s = "CDC ECM",
David Brownellda741b82008-06-19 18:19:46 -0700363 { } /* end of list */
364};
365
366static struct usb_gadget_strings ecm_string_table = {
367 .language = 0x0409, /* en-us */
368 .strings = ecm_string_defs,
369};
370
371static struct usb_gadget_strings *ecm_strings[] = {
372 &ecm_string_table,
373 NULL,
374};
375
376/*-------------------------------------------------------------------------*/
377
378static void ecm_do_notify(struct f_ecm *ecm)
379{
380 struct usb_request *req = ecm->notify_req;
381 struct usb_cdc_notification *event;
382 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
383 __le32 *data;
384 int status;
385
386 /* notification already in flight? */
387 if (!req)
388 return;
389
390 event = req->buf;
391 switch (ecm->notify_state) {
392 case ECM_NOTIFY_NONE:
393 return;
394
395 case ECM_NOTIFY_CONNECT:
396 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
397 if (ecm->is_open)
398 event->wValue = cpu_to_le16(1);
399 else
400 event->wValue = cpu_to_le16(0);
401 event->wLength = 0;
402 req->length = sizeof *event;
403
404 DBG(cdev, "notify connect %s\n",
405 ecm->is_open ? "true" : "false");
406 ecm->notify_state = ECM_NOTIFY_SPEED;
407 break;
408
409 case ECM_NOTIFY_SPEED:
410 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
411 event->wValue = cpu_to_le16(0);
412 event->wLength = cpu_to_le16(8);
David Brownell33376c12008-08-18 17:45:07 -0700413 req->length = ECM_STATUS_BYTECOUNT;
David Brownellda741b82008-06-19 18:19:46 -0700414
415 /* SPEED_CHANGE data is up/down speeds in bits/sec */
416 data = req->buf + sizeof *event;
David Brownell33376c12008-08-18 17:45:07 -0700417 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700418 data[1] = data[0];
419
David Brownell33376c12008-08-18 17:45:07 -0700420 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
David Brownellda741b82008-06-19 18:19:46 -0700421 ecm->notify_state = ECM_NOTIFY_NONE;
422 break;
423 }
424 event->bmRequestType = 0xA1;
425 event->wIndex = cpu_to_le16(ecm->ctrl_id);
426
427 ecm->notify_req = NULL;
428 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
429 if (status < 0) {
430 ecm->notify_req = req;
431 DBG(cdev, "notify --> %d\n", status);
432 }
433}
434
435static void ecm_notify(struct f_ecm *ecm)
436{
437 /* NOTE on most versions of Linux, host side cdc-ethernet
438 * won't listen for notifications until its netdevice opens.
439 * The first notification then sits in the FIFO for a long
440 * time, and the second one is queued.
441 */
442 ecm->notify_state = ECM_NOTIFY_CONNECT;
443 ecm_do_notify(ecm);
444}
445
446static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
447{
448 struct f_ecm *ecm = req->context;
449 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
450 struct usb_cdc_notification *event = req->buf;
451
452 switch (req->status) {
453 case 0:
454 /* no fault */
455 break;
456 case -ECONNRESET:
457 case -ESHUTDOWN:
458 ecm->notify_state = ECM_NOTIFY_NONE;
459 break;
460 default:
461 DBG(cdev, "event %02x --> %d\n",
462 event->bNotificationType, req->status);
463 break;
464 }
465 ecm->notify_req = req;
466 ecm_do_notify(ecm);
467}
468
469static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
470{
471 struct f_ecm *ecm = func_to_ecm(f);
472 struct usb_composite_dev *cdev = f->config->cdev;
473 struct usb_request *req = cdev->req;
474 int value = -EOPNOTSUPP;
475 u16 w_index = le16_to_cpu(ctrl->wIndex);
476 u16 w_value = le16_to_cpu(ctrl->wValue);
477 u16 w_length = le16_to_cpu(ctrl->wLength);
478
479 /* composite driver infrastructure handles everything except
480 * CDC class messages; interface activation uses set_alt().
481 */
482 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
483 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
484 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
485 /* see 6.2.30: no data, wIndex = interface,
486 * wValue = packet filter bitmap
487 */
488 if (w_length != 0 || w_index != ecm->ctrl_id)
489 goto invalid;
490 DBG(cdev, "packet filter %02x\n", w_value);
491 /* REVISIT locking of cdc_filter. This assumes the UDC
492 * driver won't have a concurrent packet TX irq running on
493 * another CPU; or that if it does, this write is atomic...
494 */
495 ecm->port.cdc_filter = w_value;
496 value = 0;
497 break;
498
499 /* and optionally:
500 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
501 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
502 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
503 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
504 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
505 * case USB_CDC_GET_ETHERNET_STATISTIC:
506 */
507
508 default:
509invalid:
510 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
511 ctrl->bRequestType, ctrl->bRequest,
512 w_value, w_index, w_length);
513 }
514
515 /* respond with data transfer or status phase? */
516 if (value >= 0) {
517 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
518 ctrl->bRequestType, ctrl->bRequest,
519 w_value, w_index, w_length);
520 req->zero = 0;
521 req->length = value;
522 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
523 if (value < 0)
524 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
525 ctrl->bRequestType, ctrl->bRequest,
526 value);
527 }
528
529 /* device either stalls (value < 0) or reports success */
530 return value;
531}
532
533
534static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
535{
536 struct f_ecm *ecm = func_to_ecm(f);
537 struct usb_composite_dev *cdev = f->config->cdev;
538
539 /* Control interface has only altsetting 0 */
540 if (intf == ecm->ctrl_id) {
541 if (alt != 0)
542 goto fail;
543
544 if (ecm->notify->driver_data) {
545 VDBG(cdev, "reset ecm control %d\n", intf);
546 usb_ep_disable(ecm->notify);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300547 }
548 if (!(ecm->notify->desc)) {
David Brownellda741b82008-06-19 18:19:46 -0700549 VDBG(cdev, "init ecm ctrl %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300550 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
551 goto fail;
David Brownellda741b82008-06-19 18:19:46 -0700552 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300553 usb_ep_enable(ecm->notify);
David Brownellda741b82008-06-19 18:19:46 -0700554 ecm->notify->driver_data = ecm;
555
556 /* Data interface has two altsettings, 0 and 1 */
557 } else if (intf == ecm->data_id) {
558 if (alt > 1)
559 goto fail;
560
561 if (ecm->port.in_ep->driver_data) {
562 DBG(cdev, "reset ecm\n");
563 gether_disconnect(&ecm->port);
564 }
565
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300566 if (!ecm->port.in_ep->desc ||
567 !ecm->port.out_ep->desc) {
David Brownellda741b82008-06-19 18:19:46 -0700568 DBG(cdev, "init ecm\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300569 if (config_ep_by_speed(cdev->gadget, f,
570 ecm->port.in_ep) ||
571 config_ep_by_speed(cdev->gadget, f,
572 ecm->port.out_ep)) {
573 ecm->port.in_ep->desc = NULL;
574 ecm->port.out_ep->desc = NULL;
575 goto fail;
576 }
David Brownellda741b82008-06-19 18:19:46 -0700577 }
578
579 /* CDC Ethernet only sends data in non-default altsettings.
580 * Changing altsettings resets filters, statistics, etc.
581 */
582 if (alt == 1) {
583 struct net_device *net;
584
585 /* Enable zlps by default for ECM conformance;
Christoph Egger90f79762010-02-05 13:24:12 +0100586 * override for musb_hdrc (avoids txdma ovhead).
David Brownellda741b82008-06-19 18:19:46 -0700587 */
Christoph Egger90f79762010-02-05 13:24:12 +0100588 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
David Brownellda741b82008-06-19 18:19:46 -0700589 );
590 ecm->port.cdc_filter = DEFAULT_FILTER;
591 DBG(cdev, "activate ecm\n");
592 net = gether_connect(&ecm->port);
593 if (IS_ERR(net))
594 return PTR_ERR(net);
595 }
596
597 /* NOTE this can be a minor disagreement with the ECM spec,
598 * which says speed notifications will "always" follow
599 * connection notifications. But we allow one connect to
600 * follow another (if the first is in flight), and instead
601 * just guarantee that a speed notification is always sent.
602 */
603 ecm_notify(ecm);
604 } else
605 goto fail;
606
607 return 0;
608fail:
609 return -EINVAL;
610}
611
612/* Because the data interface supports multiple altsettings,
613 * this ECM function *MUST* implement a get_alt() method.
614 */
615static int ecm_get_alt(struct usb_function *f, unsigned intf)
616{
617 struct f_ecm *ecm = func_to_ecm(f);
618
619 if (intf == ecm->ctrl_id)
620 return 0;
621 return ecm->port.in_ep->driver_data ? 1 : 0;
622}
623
624static void ecm_disable(struct usb_function *f)
625{
626 struct f_ecm *ecm = func_to_ecm(f);
627 struct usb_composite_dev *cdev = f->config->cdev;
628
629 DBG(cdev, "ecm deactivated\n");
630
631 if (ecm->port.in_ep->driver_data)
632 gether_disconnect(&ecm->port);
633
634 if (ecm->notify->driver_data) {
635 usb_ep_disable(ecm->notify);
636 ecm->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300637 ecm->notify->desc = NULL;
David Brownellda741b82008-06-19 18:19:46 -0700638 }
639}
640
641/*-------------------------------------------------------------------------*/
642
643/*
644 * Callbacks let us notify the host about connect/disconnect when the
645 * net device is opened or closed.
646 *
647 * For testing, note that link states on this side include both opened
648 * and closed variants of:
649 *
650 * - disconnected/unconfigured
651 * - configured but inactive (data alt 0)
652 * - configured and active (data alt 1)
653 *
654 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
655 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
656 * imply the host is actually polling the notification endpoint, and
657 * likewise that "active" doesn't imply it's actually using the data
658 * endpoints for traffic.
659 */
660
661static void ecm_open(struct gether *geth)
662{
663 struct f_ecm *ecm = func_to_ecm(&geth->func);
664
665 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
666
667 ecm->is_open = true;
668 ecm_notify(ecm);
669}
670
671static void ecm_close(struct gether *geth)
672{
673 struct f_ecm *ecm = func_to_ecm(&geth->func);
674
675 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
676
677 ecm->is_open = false;
678 ecm_notify(ecm);
679}
680
681/*-------------------------------------------------------------------------*/
682
683/* ethernet function driver setup/binding */
684
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200685static int
David Brownellda741b82008-06-19 18:19:46 -0700686ecm_bind(struct usb_configuration *c, struct usb_function *f)
687{
688 struct usb_composite_dev *cdev = c->cdev;
689 struct f_ecm *ecm = func_to_ecm(f);
Andrzej Pietrasiewicz06b72592013-05-23 10:32:05 +0200690 struct usb_string *us;
David Brownellda741b82008-06-19 18:19:46 -0700691 int status;
692 struct usb_ep *ep;
693
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200694#ifndef USBF_ECM_INCLUDED
695 struct f_ecm_opts *ecm_opts;
696
697 if (!can_support_ecm(cdev->gadget))
698 return -EINVAL;
699
700 ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
701
702 /*
703 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
704 * configurations are bound in sequence with list_for_each_entry,
705 * in each configuration its functions are bound in sequence
706 * with list_for_each_entry, so we assume no race condition
707 * with regard to ecm_opts->bound access
708 */
709 if (!ecm_opts->bound) {
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200710 mutex_lock(&ecm_opts->lock);
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200711 gether_set_gadget(ecm_opts->net, cdev->gadget);
712 status = gether_register_netdev(ecm_opts->net);
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200713 mutex_unlock(&ecm_opts->lock);
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200714 if (status)
715 return status;
716 ecm_opts->bound = true;
717 }
718#endif
Andrzej Pietrasiewicz06b72592013-05-23 10:32:05 +0200719 us = usb_gstrings_attach(cdev, ecm_strings,
720 ARRAY_SIZE(ecm_string_defs));
721 if (IS_ERR(us))
722 return PTR_ERR(us);
723 ecm_control_intf.iInterface = us[0].id;
724 ecm_data_intf.iInterface = us[2].id;
725 ecm_desc.iMACAddress = us[1].id;
726 ecm_iad_descriptor.iFunction = us[3].id;
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200727
David Brownellda741b82008-06-19 18:19:46 -0700728 /* allocate instance-specific interface IDs */
729 status = usb_interface_id(c, f);
730 if (status < 0)
731 goto fail;
732 ecm->ctrl_id = status;
Praveena Nadahallyd11519a2012-01-25 11:02:03 +0100733 ecm_iad_descriptor.bFirstInterface = status;
David Brownellda741b82008-06-19 18:19:46 -0700734
735 ecm_control_intf.bInterfaceNumber = status;
736 ecm_union_desc.bMasterInterface0 = status;
737
738 status = usb_interface_id(c, f);
739 if (status < 0)
740 goto fail;
741 ecm->data_id = status;
742
743 ecm_data_nop_intf.bInterfaceNumber = status;
744 ecm_data_intf.bInterfaceNumber = status;
745 ecm_union_desc.bSlaveInterface0 = status;
746
747 status = -ENODEV;
748
749 /* allocate instance-specific endpoints */
David Brownell33376c12008-08-18 17:45:07 -0700750 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
David Brownellda741b82008-06-19 18:19:46 -0700751 if (!ep)
752 goto fail;
753 ecm->port.in_ep = ep;
754 ep->driver_data = cdev; /* claim */
755
David Brownell33376c12008-08-18 17:45:07 -0700756 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
David Brownellda741b82008-06-19 18:19:46 -0700757 if (!ep)
758 goto fail;
759 ecm->port.out_ep = ep;
760 ep->driver_data = cdev; /* claim */
761
762 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
763 * don't treat it that way. It's simpler, and some newer CDC
764 * profiles (wireless handsets) no longer treat it as optional.
765 */
David Brownell33376c12008-08-18 17:45:07 -0700766 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
David Brownellda741b82008-06-19 18:19:46 -0700767 if (!ep)
768 goto fail;
769 ecm->notify = ep;
770 ep->driver_data = cdev; /* claim */
771
772 status = -ENOMEM;
773
774 /* allocate notification request and buffer */
775 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
776 if (!ecm->notify_req)
777 goto fail;
David Brownell33376c12008-08-18 17:45:07 -0700778 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
David Brownellda741b82008-06-19 18:19:46 -0700779 if (!ecm->notify_req->buf)
780 goto fail;
781 ecm->notify_req->context = ecm;
782 ecm->notify_req->complete = ecm_notify_complete;
783
David Brownellda741b82008-06-19 18:19:46 -0700784 /* support all relevant hardware speeds... we expect that when
785 * hardware is dual speed, all bulk-capable endpoints work at
786 * both speeds
787 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200788 hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
789 hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
790 hs_ecm_notify_desc.bEndpointAddress =
791 fs_ecm_notify_desc.bEndpointAddress;
David Brownellda741b82008-06-19 18:19:46 -0700792
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200793 ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
794 ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
795 ss_ecm_notify_desc.bEndpointAddress =
796 fs_ecm_notify_desc.bEndpointAddress;
David Brownellda741b82008-06-19 18:19:46 -0700797
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200798 status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
799 ecm_ss_function);
800 if (status)
801 goto fail;
Paul Zimmerman04617db2011-06-27 14:13:18 -0700802
David Brownellda741b82008-06-19 18:19:46 -0700803 /* NOTE: all that is done without knowing or caring about
804 * the network link ... which is unavailable to this code
805 * until we're activated via set_alt().
806 */
807
808 ecm->port.open = ecm_open;
809 ecm->port.close = ecm_close;
810
811 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700812 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownellda741b82008-06-19 18:19:46 -0700813 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
814 ecm->port.in_ep->name, ecm->port.out_ep->name,
815 ecm->notify->name);
816 return 0;
817
818fail:
David Brownellda741b82008-06-19 18:19:46 -0700819 if (ecm->notify_req) {
820 kfree(ecm->notify_req->buf);
821 usb_ep_free_request(ecm->notify, ecm->notify_req);
822 }
823
824 /* we might as well release our claims on endpoints */
825 if (ecm->notify)
826 ecm->notify->driver_data = NULL;
Sebastian Andrzej Siewiore79cc612012-10-22 22:15:00 +0200827 if (ecm->port.out_ep)
David Brownellda741b82008-06-19 18:19:46 -0700828 ecm->port.out_ep->driver_data = NULL;
Sebastian Andrzej Siewiore79cc612012-10-22 22:15:00 +0200829 if (ecm->port.in_ep)
David Brownellda741b82008-06-19 18:19:46 -0700830 ecm->port.in_ep->driver_data = NULL;
831
832 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
833
834 return status;
835}
836
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200837#ifdef USBF_ECM_INCLUDED
838
David Brownellda741b82008-06-19 18:19:46 -0700839static void
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200840ecm_old_unbind(struct usb_configuration *c, struct usb_function *f)
David Brownellda741b82008-06-19 18:19:46 -0700841{
842 struct f_ecm *ecm = func_to_ecm(f);
843
844 DBG(c->cdev, "ecm unbind\n");
845
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200846 usb_free_all_descriptors(f);
David Brownellda741b82008-06-19 18:19:46 -0700847
848 kfree(ecm->notify_req->buf);
849 usb_ep_free_request(ecm->notify, ecm->notify_req);
David Brownellda741b82008-06-19 18:19:46 -0700850 kfree(ecm);
851}
852
853/**
854 * ecm_bind_config - add CDC Ethernet network link to a configuration
855 * @c: the configuration to support the network link
856 * @ethaddr: a buffer in which the ethernet address of the host side
857 * side of the link was recorded
Robert P. J. Day21c7dee2013-05-02 10:28:33 -0400858 * @dev: eth_dev structure
David Brownellda741b82008-06-19 18:19:46 -0700859 * Context: single threaded during gadget setup
860 *
861 * Returns zero on success, else negative errno.
862 *
863 * Caller must have called @gether_setup(). Caller is also responsible
864 * for calling @gether_cleanup() before module unload.
865 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200866int
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100867ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
868 struct eth_dev *dev)
David Brownellda741b82008-06-19 18:19:46 -0700869{
870 struct f_ecm *ecm;
871 int status;
872
873 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
874 return -EINVAL;
875
David Brownellda741b82008-06-19 18:19:46 -0700876 /* allocate and initialize one new instance */
877 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
878 if (!ecm)
879 return -ENOMEM;
880
881 /* export host's Ethernet address in CDC format */
Andy Shevchenko8c7ca992012-07-06 18:30:21 +0300882 snprintf(ecm->ethaddr, sizeof ecm->ethaddr, "%pm", ethaddr);
David Brownellda741b82008-06-19 18:19:46 -0700883 ecm_string_defs[1].s = ecm->ethaddr;
884
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100885 ecm->port.ioport = dev;
David Brownellda741b82008-06-19 18:19:46 -0700886 ecm->port.cdc_filter = DEFAULT_FILTER;
887
888 ecm->port.func.name = "cdc_ethernet";
David Brownellda741b82008-06-19 18:19:46 -0700889 /* descriptors are per-instance copies */
890 ecm->port.func.bind = ecm_bind;
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200891 ecm->port.func.unbind = ecm_old_unbind;
David Brownellda741b82008-06-19 18:19:46 -0700892 ecm->port.func.set_alt = ecm_set_alt;
893 ecm->port.func.get_alt = ecm_get_alt;
894 ecm->port.func.setup = ecm_setup;
895 ecm->port.func.disable = ecm_disable;
896
897 status = usb_add_function(c, &ecm->port.func);
Sebastian Andrzej Siewior1616e992012-10-22 22:15:10 +0200898 if (status)
David Brownellda741b82008-06-19 18:19:46 -0700899 kfree(ecm);
David Brownellda741b82008-06-19 18:19:46 -0700900 return status;
901}
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200902
903#else
904
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200905static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
906{
907 return container_of(to_config_group(item), struct f_ecm_opts,
908 func_inst.group);
909}
910
911/* f_ecm_item_ops */
912USB_ETHERNET_CONFIGFS_ITEM(ecm);
913
914/* f_ecm_opts_dev_addr */
915USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);
916
917/* f_ecm_opts_host_addr */
918USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);
919
920/* f_ecm_opts_qmult */
921USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);
922
923/* f_ecm_opts_ifname */
924USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);
925
926static struct configfs_attribute *ecm_attrs[] = {
927 &f_ecm_opts_dev_addr.attr,
928 &f_ecm_opts_host_addr.attr,
929 &f_ecm_opts_qmult.attr,
930 &f_ecm_opts_ifname.attr,
931 NULL,
932};
933
934static struct config_item_type ecm_func_type = {
935 .ct_item_ops = &ecm_item_ops,
936 .ct_attrs = ecm_attrs,
937 .ct_owner = THIS_MODULE,
938};
939
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200940static void ecm_free_inst(struct usb_function_instance *f)
941{
942 struct f_ecm_opts *opts;
943
944 opts = container_of(f, struct f_ecm_opts, func_inst);
945 if (opts->bound)
946 gether_cleanup(netdev_priv(opts->net));
947 else
948 free_netdev(opts->net);
949 kfree(opts);
950}
951
952static struct usb_function_instance *ecm_alloc_inst(void)
953{
954 struct f_ecm_opts *opts;
955
956 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
957 if (!opts)
958 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200959 mutex_init(&opts->lock);
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200960 opts->func_inst.free_func_inst = ecm_free_inst;
961 opts->net = gether_setup_default();
Andrzej Pietrasiewicz172d9342013-07-25 09:13:18 +0200962 if (IS_ERR(opts->net)) {
963 struct net_device *net = opts->net;
964 kfree(opts);
965 return ERR_CAST(net);
966 }
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200967
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200968 config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);
969
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200970 return &opts->func_inst;
971}
972
973static void ecm_free(struct usb_function *f)
974{
975 struct f_ecm *ecm;
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200976 struct f_ecm_opts *opts;
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200977
978 ecm = func_to_ecm(f);
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200979 opts = container_of(f->fi, struct f_ecm_opts, func_inst);
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200980 kfree(ecm);
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +0200981 mutex_lock(&opts->lock);
982 opts->refcnt--;
983 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200984}
985
986static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
987{
988 struct f_ecm *ecm = func_to_ecm(f);
989
990 DBG(c->cdev, "ecm unbind\n");
991
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +0200992 usb_free_all_descriptors(f);
993
994 kfree(ecm->notify_req->buf);
995 usb_ep_free_request(ecm->notify, ecm->notify_req);
996}
997
998struct usb_function *ecm_alloc(struct usb_function_instance *fi)
999{
1000 struct f_ecm *ecm;
1001 struct f_ecm_opts *opts;
1002 int status;
1003
1004 /* allocate and initialize one new instance */
1005 ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
1006 if (!ecm)
1007 return ERR_PTR(-ENOMEM);
1008
1009 opts = container_of(fi, struct f_ecm_opts, func_inst);
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +02001010 mutex_lock(&opts->lock);
1011 opts->refcnt++;
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +02001012
1013 /* export host's Ethernet address in CDC format */
1014 status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
1015 sizeof(ecm->ethaddr));
1016 if (status < 12) {
1017 kfree(ecm);
Wei Yongjun2a98dbb2013-06-18 12:21:21 +08001018 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +02001019 return ERR_PTR(-EINVAL);
1020 }
1021 ecm_string_defs[1].s = ecm->ethaddr;
1022
1023 ecm->port.ioport = netdev_priv(opts->net);
Andrzej Pietrasiewiczda928012013-05-23 10:32:06 +02001024 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +02001025 ecm->port.cdc_filter = DEFAULT_FILTER;
1026
1027 ecm->port.func.name = "cdc_ethernet";
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +02001028 /* descriptors are per-instance copies */
1029 ecm->port.func.bind = ecm_bind;
1030 ecm->port.func.unbind = ecm_unbind;
1031 ecm->port.func.set_alt = ecm_set_alt;
1032 ecm->port.func.get_alt = ecm_get_alt;
1033 ecm->port.func.setup = ecm_setup;
1034 ecm->port.func.disable = ecm_disable;
1035 ecm->port.func.free_func = ecm_free;
1036
1037 return &ecm->port.func;
1038}
1039
1040DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
1041MODULE_LICENSE("GPL");
1042MODULE_AUTHOR("David Brownell");
1043
1044#endif