]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/gadget/serial.c
usb: gadget: push VID/PID/USB BCD module option into gadgets
[mirror_ubuntu-artful-kernel.git] / drivers / usb / gadget / serial.c
CommitLineData
1da177e4 1/*
a7707adf 2 * serial.c -- USB gadget serial driver
1da177e4 3 *
a7707adf
DB
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
7bb5ea54 6 * Copyright (C) 2008 by Nokia Corporation
1da177e4
LT
7 *
8 * This software is distributed under the terms of the GNU General
9 * Public License ("GPL") as published by the Free Software Foundation,
10 * either version 2 of that License or (at your option) any later version.
1da177e4
LT
11 */
12
1da177e4 13#include <linux/kernel.h>
1da177e4 14#include <linux/utsname.h>
1da177e4
LT
15#include <linux/device.h>
16#include <linux/tty.h>
17#include <linux/tty_flip.h>
1da177e4 18
a7707adf 19#include "u_serial.h"
1da177e4
LT
20#include "gadget_chips.h"
21
22
1da177e4
LT
23/* Defines */
24
7bb5ea54
DB
25#define GS_VERSION_STR "v2.4"
26#define GS_VERSION_NUM 0x2400
1da177e4
LT
27
28#define GS_LONG_NAME "Gadget Serial"
a7707adf
DB
29#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
30
7bb5ea54 31/*-------------------------------------------------------------------------*/
1da177e4 32
4e9ba518
DB
33/*
34 * Kbuild is not very cooperative with respect to linking separately
35 * compiled library objects into one module. So for now we won't use
36 * separate compilation ... ensuring init/exit sections work to shrink
37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
39 */
40#include "composite.c"
4e9ba518
DB
41
42#include "f_acm.c"
3086775a 43#include "f_obex.c"
4e9ba518
DB
44#include "f_serial.c"
45#include "u_serial.c"
46
47/*-------------------------------------------------------------------------*/
7d16e8d3 48USB_GADGET_COMPOSITE_OPTIONS();
4e9ba518 49
1da177e4 50/* Thanks to NetChip Technologies for donating this product ID.
7bb5ea54
DB
51*
52* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
53* Instead: allocate your own, using normal USB-IF procedures.
54*/
1da177e4
LT
55#define GS_VENDOR_ID 0x0525 /* NetChip */
56#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
57#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
3086775a 58#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
1da177e4 59
7bb5ea54 60/* string IDs are assigned dynamically */
1da177e4 61
7bb5ea54
DB
62#define STRING_MANUFACTURER_IDX 0
63#define STRING_PRODUCT_IDX 1
64#define STRING_DESCRIPTION_IDX 2
a7707adf 65
1da177e4 66static char manufacturer[50];
7bb5ea54
DB
67
68static struct usb_string strings_dev[] = {
69 [STRING_MANUFACTURER_IDX].s = manufacturer,
70 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
71 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
1da177e4
LT
72 { } /* end of list */
73};
74
7bb5ea54
DB
75static struct usb_gadget_strings stringtab_dev = {
76 .language = 0x0409, /* en-us */
77 .strings = strings_dev,
1da177e4
LT
78};
79
7bb5ea54
DB
80static struct usb_gadget_strings *dev_strings[] = {
81 &stringtab_dev,
82 NULL,
83};
84
85static struct usb_device_descriptor device_desc = {
1da177e4
LT
86 .bLength = USB_DT_DEVICE_SIZE,
87 .bDescriptorType = USB_DT_DEVICE,
551509d2 88 .bcdUSB = cpu_to_le16(0x0200),
7bb5ea54 89 /* .bDeviceClass = f(use_acm) */
1da177e4
LT
90 .bDeviceSubClass = 0,
91 .bDeviceProtocol = 0,
7bb5ea54 92 /* .bMaxPacketSize0 = f(hardware) */
551509d2 93 .idVendor = cpu_to_le16(GS_VENDOR_ID),
7bb5ea54
DB
94 /* .idProduct = f(use_acm) */
95 /* .bcdDevice = f(hardware) */
96 /* .iManufacturer = DYNAMIC */
97 /* .iProduct = DYNAMIC */
98 .bNumConfigurations = 1,
1da177e4
LT
99};
100
7bb5ea54
DB
101static struct usb_otg_descriptor otg_descriptor = {
102 .bLength = sizeof otg_descriptor,
1da177e4 103 .bDescriptorType = USB_DT_OTG,
1da177e4 104
7bb5ea54
DB
105 /* REVISIT SRP-only hardware is possible, although
106 * it would not be called "OTG" ...
107 */
108 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
a7707adf
DB
109};
110
7bb5ea54
DB
111static const struct usb_descriptor_header *otg_desc[] = {
112 (struct usb_descriptor_header *) &otg_descriptor,
a7707adf
DB
113 NULL,
114};
1da177e4 115
a7707adf 116/*-------------------------------------------------------------------------*/
1da177e4 117
a7707adf
DB
118/* Module */
119MODULE_DESCRIPTION(GS_VERSION_NAME);
120MODULE_AUTHOR("Al Borchers");
121MODULE_AUTHOR("David Brownell");
122MODULE_LICENSE("GPL");
1da177e4 123
90ab5ee9 124static bool use_acm = true;
7bb5ea54
DB
125module_param(use_acm, bool, 0);
126MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
1da177e4 127
90ab5ee9 128static bool use_obex = false;
3086775a
FB
129module_param(use_obex, bool, 0);
130MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
131
7bb5ea54
DB
132static unsigned n_ports = 1;
133module_param(n_ports, uint, 0);
134MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
9079e91b 135
7bb5ea54 136/*-------------------------------------------------------------------------*/
1da177e4 137
e12995ec 138static int __init serial_bind_config(struct usb_configuration *c)
9079e91b 139{
7bb5ea54
DB
140 unsigned i;
141 int status = 0;
9079e91b 142
7bb5ea54
DB
143 for (i = 0; i < n_ports && status == 0; i++) {
144 if (use_acm)
145 status = acm_bind_config(c, i);
3086775a
FB
146 else if (use_obex)
147 status = obex_bind_config(c, i);
7bb5ea54
DB
148 else
149 status = gser_bind_config(c, i);
9079e91b 150 }
7bb5ea54 151 return status;
9079e91b
DB
152}
153
7bb5ea54
DB
154static struct usb_configuration serial_config_driver = {
155 /* .label = f(use_acm) */
7bb5ea54
DB
156 /* .bConfigurationValue = f(use_acm) */
157 /* .iConfiguration = DYNAMIC */
158 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
7bb5ea54
DB
159};
160
e12995ec 161static int __init gs_bind(struct usb_composite_dev *cdev)
1da177e4 162{
7bb5ea54
DB
163 int gcnum;
164 struct usb_gadget *gadget = cdev->gadget;
165 int status;
1da177e4 166
7bb5ea54
DB
167 status = gserial_setup(cdev->gadget, n_ports);
168 if (status < 0)
169 return status;
a7707adf 170
7bb5ea54
DB
171 /* Allocate string descriptor numbers ... note that string
172 * contents can be overridden by the composite_dev glue.
91e79c91 173 */
1da177e4 174
7bb5ea54
DB
175 /* device description: manufacturer, product */
176 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
96b644bd 177 init_utsname()->sysname, init_utsname()->release,
1da177e4 178 gadget->name);
e1f15ccb 179 status = usb_string_ids_tab(cdev, strings_dev);
7bb5ea54
DB
180 if (status < 0)
181 goto fail;
e1f15ccb
SAS
182 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
183 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
184 status = strings_dev[STRING_DESCRIPTION_IDX].id;
7bb5ea54 185 serial_config_driver.iConfiguration = status;
1da177e4 186
7bb5ea54
DB
187 /* set up other descriptors */
188 gcnum = usb_gadget_controller_number(gadget);
189 if (gcnum >= 0)
190 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
191 else {
192 /* this is so simple (for now, no altsettings) that it
193 * SHOULD NOT have problems with bulk-capable hardware.
194 * so warn about unrcognized controllers -- don't panic.
195 *
196 * things like configuration and altsetting numbering
197 * can need hardware-specific attention though.
f371e750 198 */
7bb5ea54
DB
199 pr_warning("gs_bind: controller '%s' not recognized\n",
200 gadget->name);
201 device_desc.bcdDevice =
551509d2 202 cpu_to_le16(GS_VERSION_NUM | 0x0099);
f371e750 203 }
f371e750 204
7bb5ea54
DB
205 if (gadget_is_otg(cdev->gadget)) {
206 serial_config_driver.descriptors = otg_desc;
207 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1da177e4 208 }
9079e91b 209
7bb5ea54 210 /* register our configuration */
c9bfff9c
UKK
211 status = usb_add_config(cdev, &serial_config_driver,
212 serial_bind_config);
7bb5ea54
DB
213 if (status < 0)
214 goto fail;
1da177e4 215
7d16e8d3 216 usb_composite_overwrite_options(cdev, &coverwrite);
7bb5ea54 217 INFO(cdev, "%s\n", GS_VERSION_NAME);
1da177e4 218
7bb5ea54 219 return 0;
1da177e4 220
7bb5ea54
DB
221fail:
222 gserial_cleanup();
223 return status;
1da177e4
LT
224}
225
c2ec75c2 226static __refdata struct usb_composite_driver gserial_driver = {
7bb5ea54
DB
227 .name = "g_serial",
228 .dev = &device_desc,
229 .strings = dev_strings,
6fecfb05 230 .max_speed = USB_SPEED_SUPER,
03e42bd5 231 .bind = gs_bind,
9079e91b
DB
232};
233
7bb5ea54 234static int __init init(void)
1da177e4 235{
7bb5ea54
DB
236 /* We *could* export two configs; that'd be much cleaner...
237 * but neither of these product IDs was defined that way.
f371e750 238 */
1da177e4 239 if (use_acm) {
7bb5ea54
DB
240 serial_config_driver.label = "CDC ACM config";
241 serial_config_driver.bConfigurationValue = 2;
242 device_desc.bDeviceClass = USB_CLASS_COMM;
243 device_desc.idProduct =
551509d2 244 cpu_to_le16(GS_CDC_PRODUCT_ID);
3086775a
FB
245 } else if (use_obex) {
246 serial_config_driver.label = "CDC OBEX config";
247 serial_config_driver.bConfigurationValue = 3;
248 device_desc.bDeviceClass = USB_CLASS_COMM;
249 device_desc.idProduct =
551509d2 250 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
1da177e4 251 } else {
7bb5ea54
DB
252 serial_config_driver.label = "Generic Serial config";
253 serial_config_driver.bConfigurationValue = 1;
254 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
255 device_desc.idProduct =
551509d2 256 cpu_to_le16(GS_PRODUCT_ID);
1da177e4 257 }
7bb5ea54 258 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
9079e91b 259
03e42bd5 260 return usb_composite_probe(&gserial_driver);
9079e91b 261}
7bb5ea54 262module_init(init);
9079e91b 263
7bb5ea54 264static void __exit cleanup(void)
9079e91b 265{
7bb5ea54
DB
266 usb_composite_unregister(&gserial_driver);
267 gserial_cleanup();
9079e91b 268}
7bb5ea54 269module_exit(cleanup);