Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / usb / gadget / ncm.c
blob89530034dff18bbfcf0223b200c0f75b43d0cfb7
1 /*
2 * ncm.c -- NCM gadget driver
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
7 * The driver borrows from ether.c which is:
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
11 * Copyright (C) 2008 Nokia Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
19 /* #define DEBUG */
20 /* #define VERBOSE_DEBUG */
22 #include <linux/kernel.h>
23 #include <linux/utsname.h>
26 #include "u_ether.h"
28 #define DRIVER_DESC "NCM Gadget"
30 /*-------------------------------------------------------------------------*/
33 * Kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
39 #include "composite.c"
40 #include "usbstring.c"
41 #include "config.c"
42 #include "epautoconf.c"
44 #include "f_ncm.c"
45 #include "u_ether.c"
47 /*-------------------------------------------------------------------------*/
49 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50 * Instead: allocate your own, using normal USB-IF procedures.
53 /* Thanks to NetChip Technologies for donating this product ID.
54 * It's for devices with only CDC Ethernet configurations.
56 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
57 #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
59 /*-------------------------------------------------------------------------*/
61 static struct usb_device_descriptor device_desc = {
62 .bLength = sizeof device_desc,
63 .bDescriptorType = USB_DT_DEVICE,
65 .bcdUSB = cpu_to_le16 (0x0200),
67 .bDeviceClass = USB_CLASS_COMM,
68 .bDeviceSubClass = 0,
69 .bDeviceProtocol = 0,
70 /* .bMaxPacketSize0 = f(hardware) */
72 /* Vendor and product id defaults change according to what configs
73 * we support. (As does bNumConfigurations.) These values can
74 * also be overridden by module parameters.
76 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
77 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
78 /* .bcdDevice = f(hardware) */
79 /* .iManufacturer = DYNAMIC */
80 /* .iProduct = DYNAMIC */
81 /* NO SERIAL NUMBER */
82 .bNumConfigurations = 1,
85 static struct usb_otg_descriptor otg_descriptor = {
86 .bLength = sizeof otg_descriptor,
87 .bDescriptorType = USB_DT_OTG,
89 /* REVISIT SRP-only hardware is possible, although
90 * it would not be called "OTG" ...
92 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
95 static const struct usb_descriptor_header *otg_desc[] = {
96 (struct usb_descriptor_header *) &otg_descriptor,
97 NULL,
101 /* string IDs are assigned dynamically */
103 #define STRING_MANUFACTURER_IDX 0
104 #define STRING_PRODUCT_IDX 1
106 static char manufacturer[50];
108 static struct usb_string strings_dev[] = {
109 [STRING_MANUFACTURER_IDX].s = manufacturer,
110 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
111 { } /* end of list */
114 static struct usb_gadget_strings stringtab_dev = {
115 .language = 0x0409, /* en-us */
116 .strings = strings_dev,
119 static struct usb_gadget_strings *dev_strings[] = {
120 &stringtab_dev,
121 NULL,
124 static u8 hostaddr[ETH_ALEN];
126 /*-------------------------------------------------------------------------*/
128 static int __init ncm_do_config(struct usb_configuration *c)
130 /* FIXME alloc iConfiguration string, set it in c->strings */
132 if (gadget_is_otg(c->cdev->gadget)) {
133 c->descriptors = otg_desc;
134 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
137 return ncm_bind_config(c, hostaddr);
140 static struct usb_configuration ncm_config_driver = {
141 /* .label = f(hardware) */
142 .label = "CDC Ethernet (NCM)",
143 .bConfigurationValue = 1,
144 /* .iConfiguration = DYNAMIC */
145 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
148 /*-------------------------------------------------------------------------*/
150 static int __init gncm_bind(struct usb_composite_dev *cdev)
152 int gcnum;
153 struct usb_gadget *gadget = cdev->gadget;
154 int status;
156 /* set up network link layer */
157 status = gether_setup(cdev->gadget, hostaddr);
158 if (status < 0)
159 return status;
161 gcnum = usb_gadget_controller_number(gadget);
162 if (gcnum >= 0)
163 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
164 else {
165 /* We assume that can_support_ecm() tells the truth;
166 * but if the controller isn't recognized at all then
167 * that assumption is a bit more likely to be wrong.
169 dev_warn(&gadget->dev,
170 "controller '%s' not recognized; trying %s\n",
171 gadget->name,
172 ncm_config_driver.label);
173 device_desc.bcdDevice =
174 cpu_to_le16(0x0300 | 0x0099);
178 /* Allocate string descriptor numbers ... note that string
179 * contents can be overridden by the composite_dev glue.
182 /* device descriptor strings: manufacturer, product */
183 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
184 init_utsname()->sysname, init_utsname()->release,
185 gadget->name);
186 status = usb_string_id(cdev);
187 if (status < 0)
188 goto fail;
189 strings_dev[STRING_MANUFACTURER_IDX].id = status;
190 device_desc.iManufacturer = status;
192 status = usb_string_id(cdev);
193 if (status < 0)
194 goto fail;
195 strings_dev[STRING_PRODUCT_IDX].id = status;
196 device_desc.iProduct = status;
198 status = usb_add_config(cdev, &ncm_config_driver,
199 ncm_do_config);
200 if (status < 0)
201 goto fail;
203 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
205 return 0;
207 fail:
208 gether_cleanup();
209 return status;
212 static int __exit gncm_unbind(struct usb_composite_dev *cdev)
214 gether_cleanup();
215 return 0;
218 static struct usb_composite_driver ncm_driver = {
219 .name = "g_ncm",
220 .dev = &device_desc,
221 .strings = dev_strings,
222 .max_speed = USB_SPEED_HIGH,
223 .unbind = __exit_p(gncm_unbind),
226 MODULE_DESCRIPTION(DRIVER_DESC);
227 MODULE_AUTHOR("Yauheni Kaliuta");
228 MODULE_LICENSE("GPL");
230 static int __init init(void)
232 return usb_composite_probe(&ncm_driver, gncm_bind);
234 module_init(init);
236 static void __exit cleanup(void)
238 usb_composite_unregister(&ncm_driver);
240 module_exit(cleanup);