usb: gadget: f_obex: convert to new function interface with backward compatibility
[deliverable/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/tty.h>
16 #include <linux/tty_flip.h>
17
18 #include "u_serial.h"
19 #include "gadget_chips.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
32 /*
33 * Kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38 */
39 #define USBF_OBEX_INCLUDED
40 #include "f_obex.c"
41
42 /*-------------------------------------------------------------------------*/
43 USB_GADGET_COMPOSITE_OPTIONS();
44
45 /* Thanks to NetChip Technologies for donating this product ID.
46 *
47 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
48 * Instead: allocate your own, using normal USB-IF procedures.
49 */
50 #define GS_VENDOR_ID 0x0525 /* NetChip */
51 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
52 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
53 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
54
55 /* string IDs are assigned dynamically */
56
57 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
58
59 static struct usb_string strings_dev[] = {
60 [USB_GADGET_MANUFACTURER_IDX].s = "",
61 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
62 [USB_GADGET_SERIAL_IDX].s = "",
63 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
64 { } /* end of list */
65 };
66
67 static struct usb_gadget_strings stringtab_dev = {
68 .language = 0x0409, /* en-us */
69 .strings = strings_dev,
70 };
71
72 static struct usb_gadget_strings *dev_strings[] = {
73 &stringtab_dev,
74 NULL,
75 };
76
77 static struct usb_device_descriptor device_desc = {
78 .bLength = USB_DT_DEVICE_SIZE,
79 .bDescriptorType = USB_DT_DEVICE,
80 .bcdUSB = cpu_to_le16(0x0200),
81 /* .bDeviceClass = f(use_acm) */
82 .bDeviceSubClass = 0,
83 .bDeviceProtocol = 0,
84 /* .bMaxPacketSize0 = f(hardware) */
85 .idVendor = cpu_to_le16(GS_VENDOR_ID),
86 /* .idProduct = f(use_acm) */
87 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
88 /* .iManufacturer = DYNAMIC */
89 /* .iProduct = DYNAMIC */
90 .bNumConfigurations = 1,
91 };
92
93 static struct usb_otg_descriptor otg_descriptor = {
94 .bLength = sizeof otg_descriptor,
95 .bDescriptorType = USB_DT_OTG,
96
97 /* REVISIT SRP-only hardware is possible, although
98 * it would not be called "OTG" ...
99 */
100 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
101 };
102
103 static const struct usb_descriptor_header *otg_desc[] = {
104 (struct usb_descriptor_header *) &otg_descriptor,
105 NULL,
106 };
107
108 /*-------------------------------------------------------------------------*/
109
110 /* Module */
111 MODULE_DESCRIPTION(GS_VERSION_NAME);
112 MODULE_AUTHOR("Al Borchers");
113 MODULE_AUTHOR("David Brownell");
114 MODULE_LICENSE("GPL");
115
116 static bool use_acm = true;
117 module_param(use_acm, bool, 0);
118 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
119
120 static bool use_obex = false;
121 module_param(use_obex, bool, 0);
122 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
123
124 static unsigned n_ports = 1;
125 module_param(n_ports, uint, 0);
126 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
127
128 /*-------------------------------------------------------------------------*/
129 static unsigned char tty_lines[MAX_U_SERIAL_PORTS];
130
131 static int __init serial_bind_obex_config(struct usb_configuration *c)
132 {
133 unsigned i;
134 int status = 0;
135
136 for (i = 0; i < n_ports && status == 0; i++)
137 status = obex_bind_config(c, tty_lines[i]);
138 return status;
139 }
140
141 static struct usb_configuration serial_config_driver = {
142 /* .label = f(use_acm) */
143 /* .bConfigurationValue = f(use_acm) */
144 /* .iConfiguration = DYNAMIC */
145 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
146 };
147
148 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
149 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
150
151 static int serial_register_ports(struct usb_composite_dev *cdev,
152 struct usb_configuration *c, const char *f_name)
153 {
154 int i;
155 int ret;
156
157 ret = usb_add_config_only(cdev, c);
158 if (ret)
159 goto out;
160
161 for (i = 0; i < n_ports; i++) {
162
163 fi_serial[i] = usb_get_function_instance(f_name);
164 if (IS_ERR(fi_serial[i])) {
165 ret = PTR_ERR(fi_serial[i]);
166 goto fail;
167 }
168
169 f_serial[i] = usb_get_function(fi_serial[i]);
170 if (IS_ERR(f_serial[i])) {
171 ret = PTR_ERR(f_serial[i]);
172 goto err_get_func;
173 }
174
175 ret = usb_add_function(c, f_serial[i]);
176 if (ret)
177 goto err_add_func;
178 }
179
180 return 0;
181
182 err_add_func:
183 usb_put_function(f_serial[i]);
184 err_get_func:
185 usb_put_function_instance(fi_serial[i]);
186
187 fail:
188 i--;
189 while (i >= 0) {
190 usb_remove_function(c, f_serial[i]);
191 usb_put_function(f_serial[i]);
192 usb_put_function_instance(fi_serial[i]);
193 i--;
194 }
195 out:
196 return ret;
197 }
198
199 static int __init gs_bind(struct usb_composite_dev *cdev)
200 {
201 int status;
202 int cur_line = 0;
203
204 if (use_obex) {
205 for (cur_line = 0; cur_line < n_ports; cur_line++) {
206 status = gserial_alloc_line(&tty_lines[cur_line]);
207 if (status)
208 goto fail;
209 }
210 }
211
212 /* Allocate string descriptor numbers ... note that string
213 * contents can be overridden by the composite_dev glue.
214 */
215
216 status = usb_string_ids_tab(cdev, strings_dev);
217 if (status < 0)
218 goto fail;
219 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
220 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
221 status = strings_dev[STRING_DESCRIPTION_IDX].id;
222 serial_config_driver.iConfiguration = status;
223
224 if (gadget_is_otg(cdev->gadget)) {
225 serial_config_driver.descriptors = otg_desc;
226 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
227 }
228
229 /* register our configuration */
230 if (use_acm) {
231 status = serial_register_ports(cdev, &serial_config_driver,
232 "acm");
233 usb_ep_autoconfig_reset(cdev->gadget);
234 } else if (use_obex)
235 status = usb_add_config(cdev, &serial_config_driver,
236 serial_bind_obex_config);
237 else {
238 status = serial_register_ports(cdev, &serial_config_driver,
239 "gser");
240 }
241 if (status < 0)
242 goto fail;
243
244 usb_composite_overwrite_options(cdev, &coverwrite);
245 INFO(cdev, "%s\n", GS_VERSION_NAME);
246
247 return 0;
248
249 fail:
250 cur_line--;
251 while (cur_line >= 0 && use_obex)
252 gserial_free_line(tty_lines[cur_line--]);
253 return status;
254 }
255
256 static int gs_unbind(struct usb_composite_dev *cdev)
257 {
258 int i;
259
260 for (i = 0; i < n_ports; i++) {
261 usb_put_function(f_serial[i]);
262 usb_put_function_instance(fi_serial[i]);
263 if (use_obex)
264 gserial_free_line(tty_lines[i]);
265 }
266 return 0;
267 }
268
269 static __refdata struct usb_composite_driver gserial_driver = {
270 .name = "g_serial",
271 .dev = &device_desc,
272 .strings = dev_strings,
273 .max_speed = USB_SPEED_SUPER,
274 .bind = gs_bind,
275 .unbind = gs_unbind,
276 };
277
278 static int __init init(void)
279 {
280 /* We *could* export two configs; that'd be much cleaner...
281 * but neither of these product IDs was defined that way.
282 */
283 if (use_acm) {
284 serial_config_driver.label = "CDC ACM config";
285 serial_config_driver.bConfigurationValue = 2;
286 device_desc.bDeviceClass = USB_CLASS_COMM;
287 device_desc.idProduct =
288 cpu_to_le16(GS_CDC_PRODUCT_ID);
289 } else if (use_obex) {
290 serial_config_driver.label = "CDC OBEX config";
291 serial_config_driver.bConfigurationValue = 3;
292 device_desc.bDeviceClass = USB_CLASS_COMM;
293 device_desc.idProduct =
294 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
295 } else {
296 serial_config_driver.label = "Generic Serial config";
297 serial_config_driver.bConfigurationValue = 1;
298 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
299 device_desc.idProduct =
300 cpu_to_le16(GS_PRODUCT_ID);
301 }
302 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
303
304 return usb_composite_probe(&gserial_driver);
305 }
306 module_init(init);
307
308 static void __exit cleanup(void)
309 {
310 usb_composite_unregister(&gserial_driver);
311 }
312 module_exit(cleanup);
This page took 0.038743 seconds and 6 git commands to generate.