2 * serial.c -- USB gadget serial driver
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
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.
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
19 #include "gadget_chips.h"
24 #define GS_VERSION_STR "v2.4"
25 #define GS_VERSION_NUM 0x2400
27 #define GS_LONG_NAME "Gadget Serial"
28 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
30 /*-------------------------------------------------------------------------*/
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.
42 /*-------------------------------------------------------------------------*/
43 USB_GADGET_COMPOSITE_OPTIONS();
45 /* Thanks to NetChip Technologies for donating this product ID.
47 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
48 * Instead: allocate your own, using normal USB-IF procedures.
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 */
55 /* string IDs are assigned dynamically */
57 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
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) */,
67 static struct usb_gadget_strings stringtab_dev = {
68 .language = 0x0409, /* en-us */
69 .strings = strings_dev,
72 static struct usb_gadget_strings *dev_strings[] = {
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) */
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,
93 static struct usb_otg_descriptor otg_descriptor = {
94 .bLength = sizeof otg_descriptor,
95 .bDescriptorType = USB_DT_OTG,
97 /* REVISIT SRP-only hardware is possible, although
98 * it would not be called "OTG" ...
100 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
103 static const struct usb_descriptor_header *otg_desc[] = {
104 (struct usb_descriptor_header *) &otg_descriptor,
108 /*-------------------------------------------------------------------------*/
111 MODULE_DESCRIPTION(GS_VERSION_NAME);
112 MODULE_AUTHOR("Al Borchers");
113 MODULE_AUTHOR("David Brownell");
114 MODULE_LICENSE("GPL");
116 static bool use_acm = true;
117 module_param(use_acm, bool, 0);
118 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
120 static bool use_obex = false;
121 module_param(use_obex, bool, 0);
122 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
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");
128 /*-------------------------------------------------------------------------*/
129 static unsigned char tty_lines[MAX_U_SERIAL_PORTS];
131 static int __init serial_bind_obex_config(struct usb_configuration *c)
136 for (i = 0; i < n_ports && status == 0; i++)
137 status = obex_bind_config(c, tty_lines[i]);
141 static int __init serial_bind_gser_config(struct usb_configuration *c)
146 for (i = 0; i < n_ports && status == 0; i++)
147 status = gser_bind_config(c, tty_lines[i]);
151 static struct usb_configuration serial_config_driver = {
152 /* .label = f(use_acm) */
153 /* .bConfigurationValue = f(use_acm) */
154 /* .iConfiguration = DYNAMIC */
155 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
158 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
159 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
161 static int serial_register_ports(struct usb_composite_dev *cdev,
162 struct usb_configuration *c, const char *f_name)
167 ret = usb_add_config_only(cdev, c);
171 for (i = 0; i < n_ports; i++) {
172 struct f_serial_opts *opts;
174 fi_serial[i] = usb_get_function_instance(f_name);
175 if (IS_ERR(fi_serial[i])) {
176 ret = PTR_ERR(fi_serial[i]);
179 opts = container_of(fi_serial[i], struct f_serial_opts, func_inst);
180 opts->port_num = tty_lines[i];
182 f_serial[i] = usb_get_function(fi_serial[i]);
183 if (IS_ERR(f_serial[i])) {
184 ret = PTR_ERR(f_serial[i]);
188 ret = usb_add_function(c, f_serial[i]);
196 usb_put_function(f_serial[i]);
198 usb_put_function_instance(fi_serial[i]);
203 usb_remove_function(c, f_serial[i]);
204 usb_put_function(f_serial[i]);
205 usb_put_function_instance(fi_serial[i]);
212 static int __init gs_bind(struct usb_composite_dev *cdev)
217 for (cur_line = 0; cur_line < n_ports; cur_line++) {
218 status = gserial_alloc_line(&tty_lines[cur_line]);
223 /* Allocate string descriptor numbers ... note that string
224 * contents can be overridden by the composite_dev glue.
227 status = usb_string_ids_tab(cdev, strings_dev);
230 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
231 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
232 status = strings_dev[STRING_DESCRIPTION_IDX].id;
233 serial_config_driver.iConfiguration = status;
235 if (gadget_is_otg(cdev->gadget)) {
236 serial_config_driver.descriptors = otg_desc;
237 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
240 /* register our configuration */
242 status = serial_register_ports(cdev, &serial_config_driver,
244 usb_ep_autoconfig_reset(cdev->gadget);
246 status = usb_add_config(cdev, &serial_config_driver,
247 serial_bind_obex_config);
249 status = usb_add_config(cdev, &serial_config_driver,
250 serial_bind_gser_config);
254 usb_composite_overwrite_options(cdev, &coverwrite);
255 INFO(cdev, "%s\n", GS_VERSION_NAME);
261 while (cur_line >= 0)
262 gserial_free_line(tty_lines[cur_line--]);
266 static int gs_unbind(struct usb_composite_dev *cdev)
270 for (i = 0; i < n_ports; i++) {
271 usb_put_function(f_serial[i]);
272 usb_put_function_instance(fi_serial[i]);
273 gserial_free_line(tty_lines[i]);
278 static __refdata struct usb_composite_driver gserial_driver = {
281 .strings = dev_strings,
282 .max_speed = USB_SPEED_SUPER,
287 static int __init init(void)
289 /* We *could* export two configs; that'd be much cleaner...
290 * but neither of these product IDs was defined that way.
293 serial_config_driver.label = "CDC ACM config";
294 serial_config_driver.bConfigurationValue = 2;
295 device_desc.bDeviceClass = USB_CLASS_COMM;
296 device_desc.idProduct =
297 cpu_to_le16(GS_CDC_PRODUCT_ID);
298 } else if (use_obex) {
299 serial_config_driver.label = "CDC OBEX config";
300 serial_config_driver.bConfigurationValue = 3;
301 device_desc.bDeviceClass = USB_CLASS_COMM;
302 device_desc.idProduct =
303 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
305 serial_config_driver.label = "Generic Serial config";
306 serial_config_driver.bConfigurationValue = 1;
307 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
308 device_desc.idProduct =
309 cpu_to_le16(GS_PRODUCT_ID);
311 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
313 return usb_composite_probe(&gserial_driver);
317 static void __exit cleanup(void)
319 usb_composite_unregister(&gserial_driver);
321 module_exit(cleanup);