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