Merge branch 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6] / drivers / usb / gadget / serial.c
1 /*
2  * serial.c -- USB gadget serial driver
3  *
4  * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5  * Copyright (C) 2008 by David Brownell
6  * Copyright (C) 2008 by Nokia Corporation
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.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/utsname.h>
15 #include <linux/device.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18
19 #include "u_serial.h"
20 #include "gadget_chips.h"
21
22
23 /* Defines */
24
25 #define GS_VERSION_STR                  "v2.4"
26 #define GS_VERSION_NUM                  0x2400
27
28 #define GS_LONG_NAME                    "Gadget Serial"
29 #define GS_VERSION_NAME                 GS_LONG_NAME " " GS_VERSION_STR
30
31 /*-------------------------------------------------------------------------*/
32
33 /* Thanks to NetChip Technologies for donating this product ID.
34 *
35 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
36 * Instead:  allocate your own, using normal USB-IF procedures.
37 */
38 #define GS_VENDOR_ID                    0x0525  /* NetChip */
39 #define GS_PRODUCT_ID                   0xa4a6  /* Linux-USB Serial Gadget */
40 #define GS_CDC_PRODUCT_ID               0xa4a7  /* ... as CDC-ACM */
41
42 /* string IDs are assigned dynamically */
43
44 #define STRING_MANUFACTURER_IDX         0
45 #define STRING_PRODUCT_IDX              1
46 #define STRING_DESCRIPTION_IDX          2
47
48 static char manufacturer[50];
49
50 static struct usb_string strings_dev[] = {
51         [STRING_MANUFACTURER_IDX].s = manufacturer,
52         [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
53         [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
54         {  } /* end of list */
55 };
56
57 static struct usb_gadget_strings stringtab_dev = {
58         .language       = 0x0409,       /* en-us */
59         .strings        = strings_dev,
60 };
61
62 static struct usb_gadget_strings *dev_strings[] = {
63         &stringtab_dev,
64         NULL,
65 };
66
67 static struct usb_device_descriptor device_desc = {
68         .bLength =              USB_DT_DEVICE_SIZE,
69         .bDescriptorType =      USB_DT_DEVICE,
70         .bcdUSB =               __constant_cpu_to_le16(0x0200),
71         /* .bDeviceClass = f(use_acm) */
72         .bDeviceSubClass =      0,
73         .bDeviceProtocol =      0,
74         /* .bMaxPacketSize0 = f(hardware) */
75         .idVendor =             __constant_cpu_to_le16(GS_VENDOR_ID),
76         /* .idProduct = f(use_acm) */
77         /* .bcdDevice = f(hardware) */
78         /* .iManufacturer = DYNAMIC */
79         /* .iProduct = DYNAMIC */
80         .bNumConfigurations =   1,
81 };
82
83 static struct usb_otg_descriptor otg_descriptor = {
84         .bLength =              sizeof otg_descriptor,
85         .bDescriptorType =      USB_DT_OTG,
86
87         /* REVISIT SRP-only hardware is possible, although
88          * it would not be called "OTG" ...
89          */
90         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
91 };
92
93 static const struct usb_descriptor_header *otg_desc[] = {
94         (struct usb_descriptor_header *) &otg_descriptor,
95         NULL,
96 };
97
98 /*-------------------------------------------------------------------------*/
99
100 /* Module */
101 MODULE_DESCRIPTION(GS_VERSION_NAME);
102 MODULE_AUTHOR("Al Borchers");
103 MODULE_AUTHOR("David Brownell");
104 MODULE_LICENSE("GPL");
105
106 static int use_acm = true;
107 module_param(use_acm, bool, 0);
108 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
109
110 static unsigned n_ports = 1;
111 module_param(n_ports, uint, 0);
112 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
113
114 /*-------------------------------------------------------------------------*/
115
116 static int __init serial_bind_config(struct usb_configuration *c)
117 {
118         unsigned i;
119         int status = 0;
120
121         for (i = 0; i < n_ports && status == 0; i++) {
122                 if (use_acm)
123                         status = acm_bind_config(c, i);
124                 else
125                         status = gser_bind_config(c, i);
126         }
127         return status;
128 }
129
130 static struct usb_configuration serial_config_driver = {
131         /* .label = f(use_acm) */
132         .bind           = serial_bind_config,
133         /* .bConfigurationValue = f(use_acm) */
134         /* .iConfiguration = DYNAMIC */
135         .bmAttributes   = USB_CONFIG_ATT_SELFPOWER,
136         .bMaxPower      = 1,    /* 2 mA, minimal */
137 };
138
139 static int __init gs_bind(struct usb_composite_dev *cdev)
140 {
141         int                     gcnum;
142         struct usb_gadget       *gadget = cdev->gadget;
143         int                     status;
144
145         status = gserial_setup(cdev->gadget, n_ports);
146         if (status < 0)
147                 return status;
148
149         /* Allocate string descriptor numbers ... note that string
150          * contents can be overridden by the composite_dev glue.
151          */
152
153         /* device description: manufacturer, product */
154         snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
155                 init_utsname()->sysname, init_utsname()->release,
156                 gadget->name);
157         status = usb_string_id(cdev);
158         if (status < 0)
159                 goto fail;
160         strings_dev[STRING_MANUFACTURER_IDX].id = status;
161
162         device_desc.iManufacturer = status;
163
164         status = usb_string_id(cdev);
165         if (status < 0)
166                 goto fail;
167         strings_dev[STRING_PRODUCT_IDX].id = status;
168
169         device_desc.iProduct = status;
170
171         /* config description */
172         status = usb_string_id(cdev);
173         if (status < 0)
174                 goto fail;
175         strings_dev[STRING_DESCRIPTION_IDX].id = status;
176
177         serial_config_driver.iConfiguration = status;
178
179         /* set up other descriptors */
180         gcnum = usb_gadget_controller_number(gadget);
181         if (gcnum >= 0)
182                 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
183         else {
184                 /* this is so simple (for now, no altsettings) that it
185                  * SHOULD NOT have problems with bulk-capable hardware.
186                  * so warn about unrcognized controllers -- don't panic.
187                  *
188                  * things like configuration and altsetting numbering
189                  * can need hardware-specific attention though.
190                  */
191                 pr_warning("gs_bind: controller '%s' not recognized\n",
192                         gadget->name);
193                 device_desc.bcdDevice =
194                         __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099);
195         }
196
197         if (gadget_is_otg(cdev->gadget)) {
198                 serial_config_driver.descriptors = otg_desc;
199                 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
200         }
201
202         /* register our configuration */
203         status = usb_add_config(cdev, &serial_config_driver);
204         if (status < 0)
205                 goto fail;
206
207         INFO(cdev, "%s\n", GS_VERSION_NAME);
208
209         return 0;
210
211 fail:
212         gserial_cleanup();
213         return status;
214 }
215
216 static struct usb_composite_driver gserial_driver = {
217         .name           = "g_serial",
218         .dev            = &device_desc,
219         .strings        = dev_strings,
220         .bind           = gs_bind,
221 };
222
223 static int __init init(void)
224 {
225         /* We *could* export two configs; that'd be much cleaner...
226          * but neither of these product IDs was defined that way.
227          */
228         if (use_acm) {
229                 serial_config_driver.label = "CDC ACM config";
230                 serial_config_driver.bConfigurationValue = 2;
231                 device_desc.bDeviceClass = USB_CLASS_COMM;
232                 device_desc.idProduct =
233                                 __constant_cpu_to_le16(GS_CDC_PRODUCT_ID);
234         } else {
235                 serial_config_driver.label = "Generic Serial config";
236                 serial_config_driver.bConfigurationValue = 1;
237                 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
238                 device_desc.idProduct =
239                                 __constant_cpu_to_le16(GS_PRODUCT_ID);
240         }
241         strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
242
243         return usb_composite_register(&gserial_driver);
244 }
245 module_init(init);
246
247 static void __exit cleanup(void)
248 {
249         usb_composite_unregister(&gserial_driver);
250         gserial_cleanup();
251 }
252 module_exit(cleanup);