]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/serial.c
MLK-9617-2 usb: gadget: set bcdOTG of OTG descriptor for gadget drivers
[karo-tx-linux.git] / 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/device.h>
15 #include <linux/module.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 USB_GADGET_COMPOSITE_OPTIONS();
33
34 /* Thanks to NetChip Technologies for donating this product ID.
35 *
36 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
37 * Instead:  allocate your own, using normal USB-IF procedures.
38 */
39 #define GS_VENDOR_ID                    0x0525  /* NetChip */
40 #define GS_PRODUCT_ID                   0xa4a6  /* Linux-USB Serial Gadget */
41 #define GS_CDC_PRODUCT_ID               0xa4a7  /* ... as CDC-ACM */
42 #define GS_CDC_OBEX_PRODUCT_ID          0xa4a9  /* ... as CDC-OBEX */
43
44 /* string IDs are assigned dynamically */
45
46 #define STRING_DESCRIPTION_IDX          USB_GADGET_FIRST_AVAIL_IDX
47
48 static struct usb_string strings_dev[] = {
49         [USB_GADGET_MANUFACTURER_IDX].s = "",
50         [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
51         [USB_GADGET_SERIAL_IDX].s = "",
52         [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
53         {  } /* end of list */
54 };
55
56 static struct usb_gadget_strings stringtab_dev = {
57         .language       = 0x0409,       /* en-us */
58         .strings        = strings_dev,
59 };
60
61 static struct usb_gadget_strings *dev_strings[] = {
62         &stringtab_dev,
63         NULL,
64 };
65
66 static struct usb_device_descriptor device_desc = {
67         .bLength =              USB_DT_DEVICE_SIZE,
68         .bDescriptorType =      USB_DT_DEVICE,
69         .bcdUSB =               cpu_to_le16(0x0200),
70         /* .bDeviceClass = f(use_acm) */
71         .bDeviceSubClass =      0,
72         .bDeviceProtocol =      0,
73         /* .bMaxPacketSize0 = f(hardware) */
74         .idVendor =             cpu_to_le16(GS_VENDOR_ID),
75         /* .idProduct = f(use_acm) */
76         .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
77         /* .iManufacturer = DYNAMIC */
78         /* .iProduct = DYNAMIC */
79         .bNumConfigurations =   1,
80 };
81
82 static struct usb_otg_descriptor otg_descriptor = {
83         .bLength =              sizeof otg_descriptor,
84         .bDescriptorType =      USB_DT_OTG,
85
86         /* REVISIT SRP-only hardware is possible, although
87          * it would not be called "OTG" ...
88          */
89         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
90         .bcdOTG =               cpu_to_le16(0x0200),
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 bool use_acm = true;
107 module_param(use_acm, bool, 0);
108 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
109
110 static bool use_obex = false;
111 module_param(use_obex, bool, 0);
112 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
113
114 static unsigned n_ports = 1;
115 module_param(n_ports, uint, 0);
116 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
117
118 /*-------------------------------------------------------------------------*/
119
120 static struct usb_configuration serial_config_driver = {
121         /* .label = f(use_acm) */
122         /* .bConfigurationValue = f(use_acm) */
123         /* .iConfiguration = DYNAMIC */
124         .bmAttributes   = USB_CONFIG_ATT_SELFPOWER,
125 };
126
127 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
128 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
129
130 static int serial_register_ports(struct usb_composite_dev *cdev,
131                 struct usb_configuration *c, const char *f_name)
132 {
133         int i;
134         int ret;
135
136         ret = usb_add_config_only(cdev, c);
137         if (ret)
138                 goto out;
139
140         for (i = 0; i < n_ports; i++) {
141
142                 fi_serial[i] = usb_get_function_instance(f_name);
143                 if (IS_ERR(fi_serial[i])) {
144                         ret = PTR_ERR(fi_serial[i]);
145                         goto fail;
146                 }
147
148                 f_serial[i] = usb_get_function(fi_serial[i]);
149                 if (IS_ERR(f_serial[i])) {
150                         ret = PTR_ERR(f_serial[i]);
151                         goto err_get_func;
152                 }
153
154                 ret = usb_add_function(c, f_serial[i]);
155                 if (ret)
156                         goto err_add_func;
157         }
158
159         return 0;
160
161 err_add_func:
162         usb_put_function(f_serial[i]);
163 err_get_func:
164         usb_put_function_instance(fi_serial[i]);
165
166 fail:
167         i--;
168         while (i >= 0) {
169                 usb_remove_function(c, f_serial[i]);
170                 usb_put_function(f_serial[i]);
171                 usb_put_function_instance(fi_serial[i]);
172                 i--;
173         }
174 out:
175         return ret;
176 }
177
178 static int __init gs_bind(struct usb_composite_dev *cdev)
179 {
180         int                     status;
181
182         /* Allocate string descriptor numbers ... note that string
183          * contents can be overridden by the composite_dev glue.
184          */
185
186         status = usb_string_ids_tab(cdev, strings_dev);
187         if (status < 0)
188                 goto fail;
189         device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
190         device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
191         status = strings_dev[STRING_DESCRIPTION_IDX].id;
192         serial_config_driver.iConfiguration = status;
193
194         if (gadget_is_otg(cdev->gadget)) {
195                 serial_config_driver.descriptors = otg_desc;
196                 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
197         }
198
199         /* register our configuration */
200         if (use_acm) {
201                 status  = serial_register_ports(cdev, &serial_config_driver,
202                                 "acm");
203                 usb_ep_autoconfig_reset(cdev->gadget);
204         } else if (use_obex)
205                 status = serial_register_ports(cdev, &serial_config_driver,
206                                 "obex");
207         else {
208                 status = serial_register_ports(cdev, &serial_config_driver,
209                                 "gser");
210         }
211         if (status < 0)
212                 goto fail;
213
214         usb_composite_overwrite_options(cdev, &coverwrite);
215         INFO(cdev, "%s\n", GS_VERSION_NAME);
216
217         return 0;
218
219 fail:
220         return status;
221 }
222
223 static int gs_unbind(struct usb_composite_dev *cdev)
224 {
225         int i;
226
227         for (i = 0; i < n_ports; i++) {
228                 usb_put_function(f_serial[i]);
229                 usb_put_function_instance(fi_serial[i]);
230         }
231         return 0;
232 }
233
234 static __refdata struct usb_composite_driver gserial_driver = {
235         .name           = "g_serial",
236         .dev            = &device_desc,
237         .strings        = dev_strings,
238         .max_speed      = USB_SPEED_SUPER,
239         .bind           = gs_bind,
240         .unbind         = gs_unbind,
241 };
242
243 static int __init init(void)
244 {
245         /* We *could* export two configs; that'd be much cleaner...
246          * but neither of these product IDs was defined that way.
247          */
248         if (use_acm) {
249                 serial_config_driver.label = "CDC ACM config";
250                 serial_config_driver.bConfigurationValue = 2;
251                 device_desc.bDeviceClass = USB_CLASS_COMM;
252                 device_desc.idProduct =
253                                 cpu_to_le16(GS_CDC_PRODUCT_ID);
254         } else if (use_obex) {
255                 serial_config_driver.label = "CDC OBEX config";
256                 serial_config_driver.bConfigurationValue = 3;
257                 device_desc.bDeviceClass = USB_CLASS_COMM;
258                 device_desc.idProduct =
259                         cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
260         } else {
261                 serial_config_driver.label = "Generic Serial config";
262                 serial_config_driver.bConfigurationValue = 1;
263                 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
264                 device_desc.idProduct =
265                                 cpu_to_le16(GS_PRODUCT_ID);
266         }
267         strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
268
269         return usb_composite_probe(&gserial_driver);
270 }
271 module_init(init);
272
273 static void __exit cleanup(void)
274 {
275         usb_composite_unregister(&gserial_driver);
276 }
277 module_exit(cleanup);