Merge remote-tracking branch 'qcom/for-next'
[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
21
22 /* Defines */
23
24 #define GS_VERSION_STR "v2.4"
25 #define GS_VERSION_NUM 0x2400
26
27 #define GS_LONG_NAME "Gadget Serial"
28 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
29
30 /*-------------------------------------------------------------------------*/
31 USB_GADGET_COMPOSITE_OPTIONS();
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 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
42
43 /* string IDs are assigned dynamically */
44
45 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
46
47 static struct usb_string strings_dev[] = {
48 [USB_GADGET_MANUFACTURER_IDX].s = "",
49 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
50 [USB_GADGET_SERIAL_IDX].s = "",
51 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
52 { } /* end of list */
53 };
54
55 static struct usb_gadget_strings stringtab_dev = {
56 .language = 0x0409, /* en-us */
57 .strings = strings_dev,
58 };
59
60 static struct usb_gadget_strings *dev_strings[] = {
61 &stringtab_dev,
62 NULL,
63 };
64
65 static struct usb_device_descriptor device_desc = {
66 .bLength = USB_DT_DEVICE_SIZE,
67 .bDescriptorType = USB_DT_DEVICE,
68 /* .bcdUSB = DYNAMIC */
69 /* .bDeviceClass = f(use_acm) */
70 .bDeviceSubClass = 0,
71 .bDeviceProtocol = 0,
72 /* .bMaxPacketSize0 = f(hardware) */
73 .idVendor = cpu_to_le16(GS_VENDOR_ID),
74 /* .idProduct = f(use_acm) */
75 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
76 /* .iManufacturer = DYNAMIC */
77 /* .iProduct = DYNAMIC */
78 .bNumConfigurations = 1,
79 };
80
81 static const struct usb_descriptor_header *otg_desc[2];
82
83 /*-------------------------------------------------------------------------*/
84
85 /* Module */
86 MODULE_DESCRIPTION(GS_VERSION_NAME);
87 MODULE_AUTHOR("Al Borchers");
88 MODULE_AUTHOR("David Brownell");
89 MODULE_LICENSE("GPL");
90
91 static bool use_acm = true;
92 module_param(use_acm, bool, 0);
93 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
94
95 static bool use_obex = false;
96 module_param(use_obex, bool, 0);
97 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
98
99 static unsigned n_ports = 1;
100 module_param(n_ports, uint, 0);
101 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
102
103 /*-------------------------------------------------------------------------*/
104
105 static struct usb_configuration serial_config_driver = {
106 /* .label = f(use_acm) */
107 /* .bConfigurationValue = f(use_acm) */
108 /* .iConfiguration = DYNAMIC */
109 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
110 };
111
112 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
113 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
114
115 static int serial_register_ports(struct usb_composite_dev *cdev,
116 struct usb_configuration *c, const char *f_name)
117 {
118 int i;
119 int ret;
120
121 ret = usb_add_config_only(cdev, c);
122 if (ret)
123 goto out;
124
125 for (i = 0; i < n_ports; i++) {
126
127 fi_serial[i] = usb_get_function_instance(f_name);
128 if (IS_ERR(fi_serial[i])) {
129 ret = PTR_ERR(fi_serial[i]);
130 goto fail;
131 }
132
133 f_serial[i] = usb_get_function(fi_serial[i]);
134 if (IS_ERR(f_serial[i])) {
135 ret = PTR_ERR(f_serial[i]);
136 goto err_get_func;
137 }
138
139 ret = usb_add_function(c, f_serial[i]);
140 if (ret)
141 goto err_add_func;
142 }
143
144 return 0;
145
146 err_add_func:
147 usb_put_function(f_serial[i]);
148 err_get_func:
149 usb_put_function_instance(fi_serial[i]);
150
151 fail:
152 i--;
153 while (i >= 0) {
154 usb_remove_function(c, f_serial[i]);
155 usb_put_function(f_serial[i]);
156 usb_put_function_instance(fi_serial[i]);
157 i--;
158 }
159 out:
160 return ret;
161 }
162
163 static int gs_bind(struct usb_composite_dev *cdev)
164 {
165 int status;
166
167 /* Allocate string descriptor numbers ... note that string
168 * contents can be overridden by the composite_dev glue.
169 */
170
171 status = usb_string_ids_tab(cdev, strings_dev);
172 if (status < 0)
173 goto fail;
174 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
175 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
176 status = strings_dev[STRING_DESCRIPTION_IDX].id;
177 serial_config_driver.iConfiguration = status;
178
179 if (gadget_is_otg(cdev->gadget)) {
180 if (!otg_desc[0]) {
181 struct usb_descriptor_header *usb_desc;
182
183 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
184 if (!usb_desc) {
185 status = -ENOMEM;
186 goto fail;
187 }
188 usb_otg_descriptor_init(cdev->gadget, usb_desc);
189 otg_desc[0] = usb_desc;
190 otg_desc[1] = NULL;
191 }
192 serial_config_driver.descriptors = otg_desc;
193 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
194 }
195
196 /* register our configuration */
197 if (use_acm) {
198 status = serial_register_ports(cdev, &serial_config_driver,
199 "acm");
200 usb_ep_autoconfig_reset(cdev->gadget);
201 } else if (use_obex)
202 status = serial_register_ports(cdev, &serial_config_driver,
203 "obex");
204 else {
205 status = serial_register_ports(cdev, &serial_config_driver,
206 "gser");
207 }
208 if (status < 0)
209 goto fail1;
210
211 usb_composite_overwrite_options(cdev, &coverwrite);
212 INFO(cdev, "%s\n", GS_VERSION_NAME);
213
214 return 0;
215 fail1:
216 kfree(otg_desc[0]);
217 otg_desc[0] = NULL;
218 fail:
219 return status;
220 }
221
222 static int gs_unbind(struct usb_composite_dev *cdev)
223 {
224 int i;
225
226 for (i = 0; i < n_ports; i++) {
227 usb_put_function(f_serial[i]);
228 usb_put_function_instance(fi_serial[i]);
229 }
230
231 kfree(otg_desc[0]);
232 otg_desc[0] = NULL;
233
234 return 0;
235 }
236
237 static struct usb_composite_driver gserial_driver = {
238 .name = "g_serial",
239 .dev = &device_desc,
240 .strings = dev_strings,
241 .max_speed = USB_SPEED_SUPER,
242 .bind = gs_bind,
243 .unbind = gs_unbind,
244 };
245
246 static int __init init(void)
247 {
248 /* We *could* export two configs; that'd be much cleaner...
249 * but neither of these product IDs was defined that way.
250 */
251 if (use_acm) {
252 serial_config_driver.label = "CDC ACM config";
253 serial_config_driver.bConfigurationValue = 2;
254 device_desc.bDeviceClass = USB_CLASS_COMM;
255 device_desc.idProduct =
256 cpu_to_le16(GS_CDC_PRODUCT_ID);
257 } else if (use_obex) {
258 serial_config_driver.label = "CDC OBEX config";
259 serial_config_driver.bConfigurationValue = 3;
260 device_desc.bDeviceClass = USB_CLASS_COMM;
261 device_desc.idProduct =
262 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
263 } else {
264 serial_config_driver.label = "Generic Serial config";
265 serial_config_driver.bConfigurationValue = 1;
266 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
267 device_desc.idProduct =
268 cpu_to_le16(GS_PRODUCT_ID);
269 }
270 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
271
272 return usb_composite_probe(&gserial_driver);
273 }
274 module_init(init);
275
276 static void __exit cleanup(void)
277 {
278 usb_composite_unregister(&gserial_driver);
279 }
280 module_exit(cleanup);
This page took 0.037923 seconds and 5 git commands to generate.