blob: 93bf676ef50733328ef36203e7c3f2f3ce351c1a [file] [log] [blame]
David Brownell8a408192008-06-19 18:19:32 -07001/*
2 * f_subset.c -- "CDC Subset" Ethernet link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Brownell8a408192008-06-19 18:19:32 -070023#include <linux/kernel.h>
24#include <linux/device.h>
25#include <linux/etherdevice.h>
26
27#include "u_ether.h"
28
29
30/*
31 * This function packages a simple "CDC Subset" Ethernet port with no real
32 * control mechanisms; just raw data transfer over two bulk endpoints.
33 * The data transfer model is exactly that of CDC Ethernet, which is
34 * why we call it the "CDC Subset".
35 *
36 * Because it's not standardized, this has some interoperability issues.
37 * They mostly relate to driver binding, since the data transfer model is
38 * so simple (CDC Ethernet). The original versions of this protocol used
39 * specific product/vendor IDs: byteswapped IDs for Digital Equipment's
40 * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
41 * daughtercards with USB peripheral connectors. (It was used more often
42 * with other boards, using the Itsy identifiers.) Linux hosts recognized
43 * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
44 * and one interface.
45 *
46 * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
47 * "SAFE", which happens to have a mode which is identical to the "CDC
48 * Subset" in terms of data transfer and lack of control model. This was
49 * adopted by later Sharp Zaurus models, and by some other software which
50 * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
51 *
52 * Because Microsoft's RNDIS drivers are far from robust, we added a few
53 * descriptors to the CDC Subset code, making this code look like a SAFE
54 * implementation. This lets you use MCCI's host side MS-Windows drivers
55 * if you get fed up with RNDIS. It also makes it easier for composite
56 * drivers to work, since they can use class based binding instead of
57 * caring about specific product and vendor IDs.
58 */
59
David Brownell8a408192008-06-19 18:19:32 -070060struct f_gether {
61 struct gether port;
62
63 char ethaddr[14];
David Brownell8a408192008-06-19 18:19:32 -070064};
65
66static inline struct f_gether *func_to_geth(struct usb_function *f)
67{
68 return container_of(f, struct f_gether, port.func);
69}
70
71/*-------------------------------------------------------------------------*/
72
73/*
74 * "Simple" CDC-subset option is a simple vendor-neutral model that most
75 * full speed controllers can handle: one interface, two bulk endpoints.
76 * To assist host side drivers, we fancy it up a bit, and add descriptors so
77 * some host side drivers will understand it as a "SAFE" variant.
78 *
79 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
80 * Data endpoints live in the control interface, there's no data interface.
81 * And it's not used to talk to a cell phone radio.
82 */
83
84/* interface descriptor: */
85
86static struct usb_interface_descriptor subset_data_intf __initdata = {
87 .bLength = sizeof subset_data_intf,
88 .bDescriptorType = USB_DT_INTERFACE,
89
90 /* .bInterfaceNumber = DYNAMIC */
91 .bAlternateSetting = 0,
92 .bNumEndpoints = 2,
93 .bInterfaceClass = USB_CLASS_COMM,
94 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
95 .bInterfaceProtocol = 0,
96 /* .iInterface = DYNAMIC */
97};
98
David Brownell33376c12008-08-18 17:45:07 -070099static struct usb_cdc_header_desc mdlm_header_desc __initdata = {
100 .bLength = sizeof mdlm_header_desc,
David Brownell8a408192008-06-19 18:19:32 -0700101 .bDescriptorType = USB_DT_CS_INTERFACE,
102 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
103
Harvey Harrison551509d2009-02-11 14:11:36 -0800104 .bcdCDC = cpu_to_le16(0x0110),
David Brownell8a408192008-06-19 18:19:32 -0700105};
106
107static struct usb_cdc_mdlm_desc mdlm_desc __initdata = {
108 .bLength = sizeof mdlm_desc,
109 .bDescriptorType = USB_DT_CS_INTERFACE,
110 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
111
Harvey Harrison551509d2009-02-11 14:11:36 -0800112 .bcdVersion = cpu_to_le16(0x0100),
David Brownell8a408192008-06-19 18:19:32 -0700113 .bGUID = {
114 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
115 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
116 },
117};
118
119/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
120 * can't really use its struct. All we do here is say that we're using
121 * the submode of "SAFE" which directly matches the CDC Subset.
122 */
123static u8 mdlm_detail_desc[] __initdata = {
124 6,
125 USB_DT_CS_INTERFACE,
126 USB_CDC_MDLM_DETAIL_TYPE,
127
128 0, /* "SAFE" */
129 0, /* network control capabilities (none) */
130 0, /* network data capabilities ("raw" encapsulation) */
131};
132
133static struct usb_cdc_ether_desc ether_desc __initdata = {
134 .bLength = sizeof ether_desc,
135 .bDescriptorType = USB_DT_CS_INTERFACE,
136 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
137
138 /* this descriptor actually adds value, surprise! */
139 /* .iMACAddress = DYNAMIC */
Harvey Harrison551509d2009-02-11 14:11:36 -0800140 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
141 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
142 .wNumberMCFilters = cpu_to_le16(0),
David Brownell8a408192008-06-19 18:19:32 -0700143 .bNumberPowerFilters = 0,
144};
145
146/* full speed support: */
147
David Brownell33376c12008-08-18 17:45:07 -0700148static struct usb_endpoint_descriptor fs_subset_in_desc __initdata = {
David Brownell8a408192008-06-19 18:19:32 -0700149 .bLength = USB_DT_ENDPOINT_SIZE,
150 .bDescriptorType = USB_DT_ENDPOINT,
151
152 .bEndpointAddress = USB_DIR_IN,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154};
155
David Brownell33376c12008-08-18 17:45:07 -0700156static struct usb_endpoint_descriptor fs_subset_out_desc __initdata = {
David Brownell8a408192008-06-19 18:19:32 -0700157 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT,
159
160 .bEndpointAddress = USB_DIR_OUT,
161 .bmAttributes = USB_ENDPOINT_XFER_BULK,
162};
163
164static struct usb_descriptor_header *fs_eth_function[] __initdata = {
165 (struct usb_descriptor_header *) &subset_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700166 (struct usb_descriptor_header *) &mdlm_header_desc,
David Brownell8a408192008-06-19 18:19:32 -0700167 (struct usb_descriptor_header *) &mdlm_desc,
168 (struct usb_descriptor_header *) &mdlm_detail_desc,
169 (struct usb_descriptor_header *) &ether_desc,
David Brownell33376c12008-08-18 17:45:07 -0700170 (struct usb_descriptor_header *) &fs_subset_in_desc,
171 (struct usb_descriptor_header *) &fs_subset_out_desc,
David Brownell8a408192008-06-19 18:19:32 -0700172 NULL,
173};
174
175/* high speed support: */
176
David Brownell33376c12008-08-18 17:45:07 -0700177static struct usb_endpoint_descriptor hs_subset_in_desc __initdata = {
David Brownell8a408192008-06-19 18:19:32 -0700178 .bLength = USB_DT_ENDPOINT_SIZE,
179 .bDescriptorType = USB_DT_ENDPOINT,
180
181 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800182 .wMaxPacketSize = cpu_to_le16(512),
David Brownell8a408192008-06-19 18:19:32 -0700183};
184
David Brownell33376c12008-08-18 17:45:07 -0700185static struct usb_endpoint_descriptor hs_subset_out_desc __initdata = {
David Brownell8a408192008-06-19 18:19:32 -0700186 .bLength = USB_DT_ENDPOINT_SIZE,
187 .bDescriptorType = USB_DT_ENDPOINT,
188
189 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800190 .wMaxPacketSize = cpu_to_le16(512),
David Brownell8a408192008-06-19 18:19:32 -0700191};
192
193static struct usb_descriptor_header *hs_eth_function[] __initdata = {
194 (struct usb_descriptor_header *) &subset_data_intf,
David Brownell33376c12008-08-18 17:45:07 -0700195 (struct usb_descriptor_header *) &mdlm_header_desc,
David Brownell8a408192008-06-19 18:19:32 -0700196 (struct usb_descriptor_header *) &mdlm_desc,
197 (struct usb_descriptor_header *) &mdlm_detail_desc,
198 (struct usb_descriptor_header *) &ether_desc,
David Brownell33376c12008-08-18 17:45:07 -0700199 (struct usb_descriptor_header *) &hs_subset_in_desc,
200 (struct usb_descriptor_header *) &hs_subset_out_desc,
David Brownell8a408192008-06-19 18:19:32 -0700201 NULL,
202};
203
204/* string descriptors: */
205
206static struct usb_string geth_string_defs[] = {
207 [0].s = "CDC Ethernet Subset/SAFE",
208 [1].s = NULL /* DYNAMIC */,
209 { } /* end of list */
210};
211
212static struct usb_gadget_strings geth_string_table = {
213 .language = 0x0409, /* en-us */
214 .strings = geth_string_defs,
215};
216
217static struct usb_gadget_strings *geth_strings[] = {
218 &geth_string_table,
219 NULL,
220};
221
222/*-------------------------------------------------------------------------*/
223
224static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
225{
226 struct f_gether *geth = func_to_geth(f);
227 struct usb_composite_dev *cdev = f->config->cdev;
228 struct net_device *net;
229
230 /* we know alt == 0, so this is an activation or a reset */
231
232 if (geth->port.in_ep->driver_data) {
233 DBG(cdev, "reset cdc subset\n");
234 gether_disconnect(&geth->port);
235 }
236
237 DBG(cdev, "init + activate cdc subset\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300238 if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) ||
239 config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) {
240 geth->port.in_ep->desc = NULL;
241 geth->port.out_ep->desc = NULL;
242 return -EINVAL;
243 }
David Brownell8a408192008-06-19 18:19:32 -0700244
245 net = gether_connect(&geth->port);
246 return IS_ERR(net) ? PTR_ERR(net) : 0;
247}
248
249static void geth_disable(struct usb_function *f)
250{
251 struct f_gether *geth = func_to_geth(f);
252 struct usb_composite_dev *cdev = f->config->cdev;
253
254 DBG(cdev, "net deactivated\n");
255 gether_disconnect(&geth->port);
256}
257
258/*-------------------------------------------------------------------------*/
259
260/* serial function driver setup/binding */
261
262static int __init
263geth_bind(struct usb_configuration *c, struct usb_function *f)
264{
265 struct usb_composite_dev *cdev = c->cdev;
266 struct f_gether *geth = func_to_geth(f);
267 int status;
268 struct usb_ep *ep;
269
270 /* allocate instance-specific interface IDs */
271 status = usb_interface_id(c, f);
272 if (status < 0)
273 goto fail;
274 subset_data_intf.bInterfaceNumber = status;
275
276 status = -ENODEV;
277
278 /* allocate instance-specific endpoints */
David Brownell33376c12008-08-18 17:45:07 -0700279 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
David Brownell8a408192008-06-19 18:19:32 -0700280 if (!ep)
281 goto fail;
282 geth->port.in_ep = ep;
283 ep->driver_data = cdev; /* claim */
284
David Brownell33376c12008-08-18 17:45:07 -0700285 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
David Brownell8a408192008-06-19 18:19:32 -0700286 if (!ep)
287 goto fail;
288 geth->port.out_ep = ep;
289 ep->driver_data = cdev; /* claim */
290
291 /* copy descriptors, and track endpoint copies */
292 f->descriptors = usb_copy_descriptors(fs_eth_function);
293
David Brownell8a408192008-06-19 18:19:32 -0700294 /* support all relevant hardware speeds... we expect that when
295 * hardware is dual speed, all bulk-capable endpoints work at
296 * both speeds
297 */
298 if (gadget_is_dualspeed(c->cdev->gadget)) {
David Brownell33376c12008-08-18 17:45:07 -0700299 hs_subset_in_desc.bEndpointAddress =
300 fs_subset_in_desc.bEndpointAddress;
301 hs_subset_out_desc.bEndpointAddress =
302 fs_subset_out_desc.bEndpointAddress;
David Brownell8a408192008-06-19 18:19:32 -0700303
304 /* copy descriptors, and track endpoint copies */
305 f->hs_descriptors = usb_copy_descriptors(hs_eth_function);
David Brownell8a408192008-06-19 18:19:32 -0700306 }
307
308 /* NOTE: all that is done without knowing or caring about
309 * the network link ... which is unavailable to this code
310 * until we're activated via set_alt().
311 */
312
313 DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
314 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
315 geth->port.in_ep->name, geth->port.out_ep->name);
316 return 0;
317
318fail:
319 /* we might as well release our claims on endpoints */
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300320 if (geth->port.out_ep->desc)
David Brownell8a408192008-06-19 18:19:32 -0700321 geth->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300322 if (geth->port.in_ep->desc)
David Brownell8a408192008-06-19 18:19:32 -0700323 geth->port.in_ep->driver_data = NULL;
324
325 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
326
327 return status;
328}
329
330static void
331geth_unbind(struct usb_configuration *c, struct usb_function *f)
332{
333 if (gadget_is_dualspeed(c->cdev->gadget))
334 usb_free_descriptors(f->hs_descriptors);
335 usb_free_descriptors(f->descriptors);
336 geth_string_defs[1].s = NULL;
337 kfree(func_to_geth(f));
338}
339
340/**
341 * geth_bind_config - add CDC Subset network link to a configuration
342 * @c: the configuration to support the network link
343 * @ethaddr: a buffer in which the ethernet address of the host side
344 * side of the link was recorded
345 * Context: single threaded during gadget setup
346 *
347 * Returns zero on success, else negative errno.
348 *
349 * Caller must have called @gether_setup(). Caller is also responsible
350 * for calling @gether_cleanup() before module unload.
351 */
352int __init geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
353{
354 struct f_gether *geth;
355 int status;
356
357 if (!ethaddr)
358 return -EINVAL;
359
360 /* maybe allocate device-global string IDs */
361 if (geth_string_defs[0].id == 0) {
362
363 /* interface label */
364 status = usb_string_id(c->cdev);
365 if (status < 0)
366 return status;
367 geth_string_defs[0].id = status;
368 subset_data_intf.iInterface = status;
369
370 /* MAC address */
371 status = usb_string_id(c->cdev);
372 if (status < 0)
373 return status;
374 geth_string_defs[1].id = status;
375 ether_desc.iMACAddress = status;
376 }
377
378 /* allocate and initialize one new instance */
379 geth = kzalloc(sizeof *geth, GFP_KERNEL);
380 if (!geth)
381 return -ENOMEM;
382
383 /* export host's Ethernet address in CDC format */
384 snprintf(geth->ethaddr, sizeof geth->ethaddr,
385 "%02X%02X%02X%02X%02X%02X",
386 ethaddr[0], ethaddr[1], ethaddr[2],
387 ethaddr[3], ethaddr[4], ethaddr[5]);
388 geth_string_defs[1].s = geth->ethaddr;
389
390 geth->port.cdc_filter = DEFAULT_FILTER;
391
392 geth->port.func.name = "cdc_subset";
393 geth->port.func.strings = geth_strings;
394 geth->port.func.bind = geth_bind;
395 geth->port.func.unbind = geth_unbind;
396 geth->port.func.set_alt = geth_set_alt;
397 geth->port.func.disable = geth_disable;
398
399 status = usb_add_function(c, &geth->port.func);
400 if (status) {
401 geth_string_defs[1].s = NULL;
402 kfree(geth);
403 }
404 return status;
405}