usb: dwc3: keystone: drop dma_mask configuration
[linux/fpc-iii.git] / drivers / usb / gadget / f_subset.c
blob1ea8baf33333be75990c6c1b7d2d6208192f7375
1 /*
2 * f_subset.c -- "CDC Subset" Ethernet link function driver
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
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.
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/etherdevice.h>
19 #include "u_ether.h"
20 #include "u_ether_configfs.h"
21 #include "u_gether.h"
24 * This function packages a simple "CDC Subset" Ethernet port with no real
25 * control mechanisms; just raw data transfer over two bulk endpoints.
26 * The data transfer model is exactly that of CDC Ethernet, which is
27 * why we call it the "CDC Subset".
29 * Because it's not standardized, this has some interoperability issues.
30 * They mostly relate to driver binding, since the data transfer model is
31 * so simple (CDC Ethernet). The original versions of this protocol used
32 * specific product/vendor IDs: byteswapped IDs for Digital Equipment's
33 * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
34 * daughtercards with USB peripheral connectors. (It was used more often
35 * with other boards, using the Itsy identifiers.) Linux hosts recognized
36 * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
37 * and one interface.
39 * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
40 * "SAFE", which happens to have a mode which is identical to the "CDC
41 * Subset" in terms of data transfer and lack of control model. This was
42 * adopted by later Sharp Zaurus models, and by some other software which
43 * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
45 * Because Microsoft's RNDIS drivers are far from robust, we added a few
46 * descriptors to the CDC Subset code, making this code look like a SAFE
47 * implementation. This lets you use MCCI's host side MS-Windows drivers
48 * if you get fed up with RNDIS. It also makes it easier for composite
49 * drivers to work, since they can use class based binding instead of
50 * caring about specific product and vendor IDs.
53 struct f_gether {
54 struct gether port;
56 char ethaddr[14];
59 static inline struct f_gether *func_to_geth(struct usb_function *f)
61 return container_of(f, struct f_gether, port.func);
64 /*-------------------------------------------------------------------------*/
67 * "Simple" CDC-subset option is a simple vendor-neutral model that most
68 * full speed controllers can handle: one interface, two bulk endpoints.
69 * To assist host side drivers, we fancy it up a bit, and add descriptors so
70 * some host side drivers will understand it as a "SAFE" variant.
72 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
73 * Data endpoints live in the control interface, there's no data interface.
74 * And it's not used to talk to a cell phone radio.
77 /* interface descriptor: */
79 static struct usb_interface_descriptor subset_data_intf = {
80 .bLength = sizeof subset_data_intf,
81 .bDescriptorType = USB_DT_INTERFACE,
83 /* .bInterfaceNumber = DYNAMIC */
84 .bAlternateSetting = 0,
85 .bNumEndpoints = 2,
86 .bInterfaceClass = USB_CLASS_COMM,
87 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
88 .bInterfaceProtocol = 0,
89 /* .iInterface = DYNAMIC */
92 static struct usb_cdc_header_desc mdlm_header_desc = {
93 .bLength = sizeof mdlm_header_desc,
94 .bDescriptorType = USB_DT_CS_INTERFACE,
95 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
97 .bcdCDC = cpu_to_le16(0x0110),
100 static struct usb_cdc_mdlm_desc mdlm_desc = {
101 .bLength = sizeof mdlm_desc,
102 .bDescriptorType = USB_DT_CS_INTERFACE,
103 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
105 .bcdVersion = cpu_to_le16(0x0100),
106 .bGUID = {
107 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
108 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
112 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
113 * can't really use its struct. All we do here is say that we're using
114 * the submode of "SAFE" which directly matches the CDC Subset.
116 static u8 mdlm_detail_desc[] = {
118 USB_DT_CS_INTERFACE,
119 USB_CDC_MDLM_DETAIL_TYPE,
121 0, /* "SAFE" */
122 0, /* network control capabilities (none) */
123 0, /* network data capabilities ("raw" encapsulation) */
126 static struct usb_cdc_ether_desc ether_desc = {
127 .bLength = sizeof ether_desc,
128 .bDescriptorType = USB_DT_CS_INTERFACE,
129 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
131 /* this descriptor actually adds value, surprise! */
132 /* .iMACAddress = DYNAMIC */
133 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
134 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
135 .wNumberMCFilters = cpu_to_le16(0),
136 .bNumberPowerFilters = 0,
139 /* full speed support: */
141 static struct usb_endpoint_descriptor fs_subset_in_desc = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
145 .bEndpointAddress = USB_DIR_IN,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
149 static struct usb_endpoint_descriptor fs_subset_out_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
153 .bEndpointAddress = USB_DIR_OUT,
154 .bmAttributes = USB_ENDPOINT_XFER_BULK,
157 static struct usb_descriptor_header *fs_eth_function[] = {
158 (struct usb_descriptor_header *) &subset_data_intf,
159 (struct usb_descriptor_header *) &mdlm_header_desc,
160 (struct usb_descriptor_header *) &mdlm_desc,
161 (struct usb_descriptor_header *) &mdlm_detail_desc,
162 (struct usb_descriptor_header *) &ether_desc,
163 (struct usb_descriptor_header *) &fs_subset_in_desc,
164 (struct usb_descriptor_header *) &fs_subset_out_desc,
165 NULL,
168 /* high speed support: */
170 static struct usb_endpoint_descriptor hs_subset_in_desc = {
171 .bLength = USB_DT_ENDPOINT_SIZE,
172 .bDescriptorType = USB_DT_ENDPOINT,
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 .wMaxPacketSize = cpu_to_le16(512),
178 static struct usb_endpoint_descriptor hs_subset_out_desc = {
179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
182 .bmAttributes = USB_ENDPOINT_XFER_BULK,
183 .wMaxPacketSize = cpu_to_le16(512),
186 static struct usb_descriptor_header *hs_eth_function[] = {
187 (struct usb_descriptor_header *) &subset_data_intf,
188 (struct usb_descriptor_header *) &mdlm_header_desc,
189 (struct usb_descriptor_header *) &mdlm_desc,
190 (struct usb_descriptor_header *) &mdlm_detail_desc,
191 (struct usb_descriptor_header *) &ether_desc,
192 (struct usb_descriptor_header *) &hs_subset_in_desc,
193 (struct usb_descriptor_header *) &hs_subset_out_desc,
194 NULL,
197 /* super speed support: */
199 static struct usb_endpoint_descriptor ss_subset_in_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204 .wMaxPacketSize = cpu_to_le16(1024),
207 static struct usb_endpoint_descriptor ss_subset_out_desc = {
208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
211 .bmAttributes = USB_ENDPOINT_XFER_BULK,
212 .wMaxPacketSize = cpu_to_le16(1024),
215 static struct usb_ss_ep_comp_descriptor ss_subset_bulk_comp_desc = {
216 .bLength = sizeof ss_subset_bulk_comp_desc,
217 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
219 /* the following 2 values can be tweaked if necessary */
220 /* .bMaxBurst = 0, */
221 /* .bmAttributes = 0, */
224 static struct usb_descriptor_header *ss_eth_function[] = {
225 (struct usb_descriptor_header *) &subset_data_intf,
226 (struct usb_descriptor_header *) &mdlm_header_desc,
227 (struct usb_descriptor_header *) &mdlm_desc,
228 (struct usb_descriptor_header *) &mdlm_detail_desc,
229 (struct usb_descriptor_header *) &ether_desc,
230 (struct usb_descriptor_header *) &ss_subset_in_desc,
231 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
232 (struct usb_descriptor_header *) &ss_subset_out_desc,
233 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
234 NULL,
237 /* string descriptors: */
239 static struct usb_string geth_string_defs[] = {
240 [0].s = "CDC Ethernet Subset/SAFE",
241 [1].s = "",
242 { } /* end of list */
245 static struct usb_gadget_strings geth_string_table = {
246 .language = 0x0409, /* en-us */
247 .strings = geth_string_defs,
250 static struct usb_gadget_strings *geth_strings[] = {
251 &geth_string_table,
252 NULL,
255 /*-------------------------------------------------------------------------*/
257 static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
259 struct f_gether *geth = func_to_geth(f);
260 struct usb_composite_dev *cdev = f->config->cdev;
261 struct net_device *net;
263 /* we know alt == 0, so this is an activation or a reset */
265 if (geth->port.in_ep->driver_data) {
266 DBG(cdev, "reset cdc subset\n");
267 gether_disconnect(&geth->port);
270 DBG(cdev, "init + activate cdc subset\n");
271 if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) ||
272 config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) {
273 geth->port.in_ep->desc = NULL;
274 geth->port.out_ep->desc = NULL;
275 return -EINVAL;
278 net = gether_connect(&geth->port);
279 return PTR_ERR_OR_ZERO(net);
282 static void geth_disable(struct usb_function *f)
284 struct f_gether *geth = func_to_geth(f);
285 struct usb_composite_dev *cdev = f->config->cdev;
287 DBG(cdev, "net deactivated\n");
288 gether_disconnect(&geth->port);
291 /*-------------------------------------------------------------------------*/
293 /* serial function driver setup/binding */
295 static int
296 geth_bind(struct usb_configuration *c, struct usb_function *f)
298 struct usb_composite_dev *cdev = c->cdev;
299 struct f_gether *geth = func_to_geth(f);
300 struct usb_string *us;
301 int status;
302 struct usb_ep *ep;
304 struct f_gether_opts *gether_opts;
306 gether_opts = container_of(f->fi, struct f_gether_opts, func_inst);
309 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
310 * configurations are bound in sequence with list_for_each_entry,
311 * in each configuration its functions are bound in sequence
312 * with list_for_each_entry, so we assume no race condition
313 * with regard to gether_opts->bound access
315 if (!gether_opts->bound) {
316 mutex_lock(&gether_opts->lock);
317 gether_set_gadget(gether_opts->net, cdev->gadget);
318 status = gether_register_netdev(gether_opts->net);
319 mutex_unlock(&gether_opts->lock);
320 if (status)
321 return status;
322 gether_opts->bound = true;
325 us = usb_gstrings_attach(cdev, geth_strings,
326 ARRAY_SIZE(geth_string_defs));
327 if (IS_ERR(us))
328 return PTR_ERR(us);
330 subset_data_intf.iInterface = us[0].id;
331 ether_desc.iMACAddress = us[1].id;
333 /* allocate instance-specific interface IDs */
334 status = usb_interface_id(c, f);
335 if (status < 0)
336 goto fail;
337 subset_data_intf.bInterfaceNumber = status;
339 status = -ENODEV;
341 /* allocate instance-specific endpoints */
342 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
343 if (!ep)
344 goto fail;
345 geth->port.in_ep = ep;
346 ep->driver_data = cdev; /* claim */
348 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
349 if (!ep)
350 goto fail;
351 geth->port.out_ep = ep;
352 ep->driver_data = cdev; /* claim */
354 /* support all relevant hardware speeds... we expect that when
355 * hardware is dual speed, all bulk-capable endpoints work at
356 * both speeds
358 hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
359 hs_subset_out_desc.bEndpointAddress =
360 fs_subset_out_desc.bEndpointAddress;
362 ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
363 ss_subset_out_desc.bEndpointAddress =
364 fs_subset_out_desc.bEndpointAddress;
366 status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function,
367 ss_eth_function);
368 if (status)
369 goto fail;
371 /* NOTE: all that is done without knowing or caring about
372 * the network link ... which is unavailable to this code
373 * until we're activated via set_alt().
376 DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
377 gadget_is_superspeed(c->cdev->gadget) ? "super" :
378 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
379 geth->port.in_ep->name, geth->port.out_ep->name);
380 return 0;
382 fail:
383 usb_free_all_descriptors(f);
384 /* we might as well release our claims on endpoints */
385 if (geth->port.out_ep)
386 geth->port.out_ep->driver_data = NULL;
387 if (geth->port.in_ep)
388 geth->port.in_ep->driver_data = NULL;
390 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
392 return status;
395 static inline struct f_gether_opts *to_f_gether_opts(struct config_item *item)
397 return container_of(to_config_group(item), struct f_gether_opts,
398 func_inst.group);
401 /* f_gether_item_ops */
402 USB_ETHERNET_CONFIGFS_ITEM(gether);
404 /* f_gether_opts_dev_addr */
405 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(gether);
407 /* f_gether_opts_host_addr */
408 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(gether);
410 /* f_gether_opts_qmult */
411 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(gether);
413 /* f_gether_opts_ifname */
414 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(gether);
416 static struct configfs_attribute *gether_attrs[] = {
417 &f_gether_opts_dev_addr.attr,
418 &f_gether_opts_host_addr.attr,
419 &f_gether_opts_qmult.attr,
420 &f_gether_opts_ifname.attr,
421 NULL,
424 static struct config_item_type gether_func_type = {
425 .ct_item_ops = &gether_item_ops,
426 .ct_attrs = gether_attrs,
427 .ct_owner = THIS_MODULE,
430 static void geth_free_inst(struct usb_function_instance *f)
432 struct f_gether_opts *opts;
434 opts = container_of(f, struct f_gether_opts, func_inst);
435 if (opts->bound)
436 gether_cleanup(netdev_priv(opts->net));
437 else
438 free_netdev(opts->net);
439 kfree(opts);
442 static struct usb_function_instance *geth_alloc_inst(void)
444 struct f_gether_opts *opts;
446 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
447 if (!opts)
448 return ERR_PTR(-ENOMEM);
449 mutex_init(&opts->lock);
450 opts->func_inst.free_func_inst = geth_free_inst;
451 opts->net = gether_setup_default();
452 if (IS_ERR(opts->net)) {
453 struct net_device *net = opts->net;
454 kfree(opts);
455 return ERR_CAST(net);
458 config_group_init_type_name(&opts->func_inst.group, "",
459 &gether_func_type);
461 return &opts->func_inst;
464 static void geth_free(struct usb_function *f)
466 struct f_gether *eth;
468 eth = func_to_geth(f);
469 kfree(eth);
472 static void geth_unbind(struct usb_configuration *c, struct usb_function *f)
474 geth_string_defs[0].id = 0;
475 usb_free_all_descriptors(f);
478 static struct usb_function *geth_alloc(struct usb_function_instance *fi)
480 struct f_gether *geth;
481 struct f_gether_opts *opts;
482 int status;
484 /* allocate and initialize one new instance */
485 geth = kzalloc(sizeof(*geth), GFP_KERNEL);
486 if (!geth)
487 return ERR_PTR(-ENOMEM);
489 opts = container_of(fi, struct f_gether_opts, func_inst);
491 mutex_lock(&opts->lock);
492 opts->refcnt++;
493 /* export host's Ethernet address in CDC format */
494 status = gether_get_host_addr_cdc(opts->net, geth->ethaddr,
495 sizeof(geth->ethaddr));
496 if (status < 12) {
497 kfree(geth);
498 mutex_unlock(&opts->lock);
499 return ERR_PTR(-EINVAL);
501 geth_string_defs[1].s = geth->ethaddr;
503 geth->port.ioport = netdev_priv(opts->net);
504 mutex_unlock(&opts->lock);
505 geth->port.cdc_filter = DEFAULT_FILTER;
507 geth->port.func.name = "cdc_subset";
508 geth->port.func.bind = geth_bind;
509 geth->port.func.unbind = geth_unbind;
510 geth->port.func.set_alt = geth_set_alt;
511 geth->port.func.disable = geth_disable;
512 geth->port.func.free_func = geth_free;
514 return &geth->port.func;
517 DECLARE_USB_FUNCTION_INIT(geth, geth_alloc_inst, geth_alloc);
518 MODULE_LICENSE("GPL");
519 MODULE_AUTHOR("David Brownell");