9836d164469ab55a7b873ef8a0457081c5287db5
[deliverable/linux.git] / drivers / usb / gadget / legacy / 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 const struct usb_descriptor_header *otg_desc[2];
83
84 /*-------------------------------------------------------------------------*/
85
86 /* Module */
87 MODULE_DESCRIPTION(GS_VERSION_NAME);
88 MODULE_AUTHOR("Al Borchers");
89 MODULE_AUTHOR("David Brownell");
90 MODULE_LICENSE("GPL");
91
92 static bool use_acm = true;
93 module_param(use_acm, bool, 0);
94 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
95
96 static bool use_obex = false;
97 module_param(use_obex, bool, 0);
98 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
99
100 static unsigned n_ports = 1;
101 module_param(n_ports, uint, 0);
102 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
103
104 /*-------------------------------------------------------------------------*/
105
106 static struct usb_configuration serial_config_driver = {
107 /* .label = f(use_acm) */
108 /* .bConfigurationValue = f(use_acm) */
109 /* .iConfiguration = DYNAMIC */
110 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
111 };
112
113 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
114 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
115
116 static int serial_register_ports(struct usb_composite_dev *cdev,
117 struct usb_configuration *c, const char *f_name)
118 {
119 int i;
120 int ret;
121
122 ret = usb_add_config_only(cdev, c);
123 if (ret)
124 goto out;
125
126 for (i = 0; i < n_ports; i++) {
127
128 fi_serial[i] = usb_get_function_instance(f_name);
129 if (IS_ERR(fi_serial[i])) {
130 ret = PTR_ERR(fi_serial[i]);
131 goto fail;
132 }
133
134 f_serial[i] = usb_get_function(fi_serial[i]);
135 if (IS_ERR(f_serial[i])) {
136 ret = PTR_ERR(f_serial[i]);
137 goto err_get_func;
138 }
139
140 ret = usb_add_function(c, f_serial[i]);
141 if (ret)
142 goto err_add_func;
143 }
144
145 return 0;
146
147 err_add_func:
148 usb_put_function(f_serial[i]);
149 err_get_func:
150 usb_put_function_instance(fi_serial[i]);
151
152 fail:
153 i--;
154 while (i >= 0) {
155 usb_remove_function(c, f_serial[i]);
156 usb_put_function(f_serial[i]);
157 usb_put_function_instance(fi_serial[i]);
158 i--;
159 }
160 out:
161 return ret;
162 }
163
164 static int gs_bind(struct usb_composite_dev *cdev)
165 {
166 int status;
167
168 /* Allocate string descriptor numbers ... note that string
169 * contents can be overridden by the composite_dev glue.
170 */
171
172 status = usb_string_ids_tab(cdev, strings_dev);
173 if (status < 0)
174 goto fail;
175 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
176 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
177 status = strings_dev[STRING_DESCRIPTION_IDX].id;
178 serial_config_driver.iConfiguration = status;
179
180 if (gadget_is_otg(cdev->gadget)) {
181 if (!otg_desc[0]) {
182 struct usb_descriptor_header *usb_desc;
183
184 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
185 if (!usb_desc) {
186 status = -ENOMEM;
187 goto fail;
188 }
189 usb_otg_descriptor_init(cdev->gadget, usb_desc);
190 otg_desc[0] = usb_desc;
191 otg_desc[1] = NULL;
192 }
193 serial_config_driver.descriptors = otg_desc;
194 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
195 }
196
197 /* register our configuration */
198 if (use_acm) {
199 status = serial_register_ports(cdev, &serial_config_driver,
200 "acm");
201 usb_ep_autoconfig_reset(cdev->gadget);
202 } else if (use_obex)
203 status = serial_register_ports(cdev, &serial_config_driver,
204 "obex");
205 else {
206 status = serial_register_ports(cdev, &serial_config_driver,
207 "gser");
208 }
209 if (status < 0)
210 goto fail1;
211
212 usb_composite_overwrite_options(cdev, &coverwrite);
213 INFO(cdev, "%s\n", GS_VERSION_NAME);
214
215 return 0;
216 fail1:
217 kfree(otg_desc[0]);
218 otg_desc[0] = NULL;
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
232 kfree(otg_desc[0]);
233 otg_desc[0] = NULL;
234
235 return 0;
236 }
237
238 static struct usb_composite_driver gserial_driver = {
239 .name = "g_serial",
240 .dev = &device_desc,
241 .strings = dev_strings,
242 .max_speed = USB_SPEED_SUPER,
243 .bind = gs_bind,
244 .unbind = gs_unbind,
245 };
246
247 static int __init init(void)
248 {
249 /* We *could* export two configs; that'd be much cleaner...
250 * but neither of these product IDs was defined that way.
251 */
252 if (use_acm) {
253 serial_config_driver.label = "CDC ACM config";
254 serial_config_driver.bConfigurationValue = 2;
255 device_desc.bDeviceClass = USB_CLASS_COMM;
256 device_desc.idProduct =
257 cpu_to_le16(GS_CDC_PRODUCT_ID);
258 } else if (use_obex) {
259 serial_config_driver.label = "CDC OBEX config";
260 serial_config_driver.bConfigurationValue = 3;
261 device_desc.bDeviceClass = USB_CLASS_COMM;
262 device_desc.idProduct =
263 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
264 } else {
265 serial_config_driver.label = "Generic Serial config";
266 serial_config_driver.bConfigurationValue = 1;
267 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
268 device_desc.idProduct =
269 cpu_to_le16(GS_PRODUCT_ID);
270 }
271 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
272
273 return usb_composite_probe(&gserial_driver);
274 }
275 module_init(init);
276
277 static void __exit cleanup(void)
278 {
279 usb_composite_unregister(&gserial_driver);
280 }
281 module_exit(cleanup);
This page took 0.100307 seconds and 4 git commands to generate.