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/utsname.h>
15 #include <linux/device.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
20 #include "gadget_chips.h"
25 #define GS_VERSION_STR "v2.4"
26 #define GS_VERSION_NUM 0x2400
28 #define GS_LONG_NAME "Gadget Serial"
29 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
31 /*-------------------------------------------------------------------------*/
34 * Kbuild is not very cooperative with respect to linking separately
35 * compiled library objects into one module. So for now we won't use
36 * separate compilation ... ensuring init/exit sections work to shrink
37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
40 #include "composite.c"
42 #include "epautoconf.c"
49 /*-------------------------------------------------------------------------*/
51 /* Thanks to NetChip Technologies for donating this product ID.
53 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
54 * Instead: allocate your own, using normal USB-IF procedures.
56 #define GS_VENDOR_ID 0x0525 /* NetChip */
57 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
58 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
59 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
61 /* string IDs are assigned dynamically */
63 #define STRING_MANUFACTURER_IDX 0
64 #define STRING_PRODUCT_IDX 1
65 #define STRING_DESCRIPTION_IDX 2
67 static char manufacturer[50];
69 static struct usb_string strings_dev[] = {
70 [STRING_MANUFACTURER_IDX].s = manufacturer,
71 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
72 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
76 static struct usb_gadget_strings stringtab_dev = {
77 .language = 0x0409, /* en-us */
78 .strings = strings_dev,
81 static struct usb_gadget_strings *dev_strings[] = {
86 static struct usb_device_descriptor device_desc = {
87 .bLength = USB_DT_DEVICE_SIZE,
88 .bDescriptorType = USB_DT_DEVICE,
89 .bcdUSB = cpu_to_le16(0x0200),
90 /* .bDeviceClass = f(use_acm) */
93 /* .bMaxPacketSize0 = f(hardware) */
94 .idVendor = cpu_to_le16(GS_VENDOR_ID),
95 /* .idProduct = f(use_acm) */
96 /* .bcdDevice = f(hardware) */
97 /* .iManufacturer = DYNAMIC */
98 /* .iProduct = DYNAMIC */
99 .bNumConfigurations = 1,
102 static struct usb_otg_descriptor otg_descriptor = {
103 .bLength = sizeof otg_descriptor,
104 .bDescriptorType = USB_DT_OTG,
106 /* REVISIT SRP-only hardware is possible, although
107 * it would not be called "OTG" ...
109 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
112 static const struct usb_descriptor_header *otg_desc[] = {
113 (struct usb_descriptor_header *) &otg_descriptor,
117 /*-------------------------------------------------------------------------*/
120 MODULE_DESCRIPTION(GS_VERSION_NAME);
121 MODULE_AUTHOR("Al Borchers");
122 MODULE_AUTHOR("David Brownell");
123 MODULE_LICENSE("GPL");
125 static bool use_acm = true;
126 module_param(use_acm, bool, 0);
127 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
129 static bool use_obex = false;
130 module_param(use_obex, bool, 0);
131 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
133 static unsigned n_ports = 1;
134 module_param(n_ports, uint, 0);
135 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
137 /*-------------------------------------------------------------------------*/
139 static int __init serial_bind_config(struct usb_configuration *c)
144 for (i = 0; i < n_ports && status == 0; i++) {
146 status = acm_bind_config(c, i);
148 status = obex_bind_config(c, i);
150 status = gser_bind_config(c, i);
155 static struct usb_configuration serial_config_driver = {
156 /* .label = f(use_acm) */
157 /* .bConfigurationValue = f(use_acm) */
158 /* .iConfiguration = DYNAMIC */
159 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
162 static int __init gs_bind(struct usb_composite_dev *cdev)
165 struct usb_gadget *gadget = cdev->gadget;
168 status = gserial_setup(cdev->gadget, n_ports);
172 /* Allocate string descriptor numbers ... note that string
173 * contents can be overridden by the composite_dev glue.
176 /* device description: manufacturer, product */
177 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
178 init_utsname()->sysname, init_utsname()->release,
180 status = usb_string_id(cdev);
183 strings_dev[STRING_MANUFACTURER_IDX].id = status;
185 device_desc.iManufacturer = status;
187 status = usb_string_id(cdev);
190 strings_dev[STRING_PRODUCT_IDX].id = status;
192 device_desc.iProduct = status;
194 /* config description */
195 status = usb_string_id(cdev);
198 strings_dev[STRING_DESCRIPTION_IDX].id = status;
200 serial_config_driver.iConfiguration = status;
202 /* set up other descriptors */
203 gcnum = usb_gadget_controller_number(gadget);
205 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
207 /* this is so simple (for now, no altsettings) that it
208 * SHOULD NOT have problems with bulk-capable hardware.
209 * so warn about unrcognized controllers -- don't panic.
211 * things like configuration and altsetting numbering
212 * can need hardware-specific attention though.
214 pr_warning("gs_bind: controller '%s' not recognized\n",
216 device_desc.bcdDevice =
217 cpu_to_le16(GS_VERSION_NUM | 0x0099);
220 if (gadget_is_otg(cdev->gadget)) {
221 serial_config_driver.descriptors = otg_desc;
222 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
225 /* register our configuration */
226 status = usb_add_config(cdev, &serial_config_driver,
231 INFO(cdev, "%s\n", GS_VERSION_NAME);
240 static __refdata struct usb_composite_driver gserial_driver = {
243 .strings = dev_strings,
244 .max_speed = USB_SPEED_SUPER,
248 static int __init init(void)
250 /* We *could* export two configs; that'd be much cleaner...
251 * but neither of these product IDs was defined that way.
254 serial_config_driver.label = "CDC ACM config";
255 serial_config_driver.bConfigurationValue = 2;
256 device_desc.bDeviceClass = USB_CLASS_COMM;
257 device_desc.idProduct =
258 cpu_to_le16(GS_CDC_PRODUCT_ID);
259 } else if (use_obex) {
260 serial_config_driver.label = "CDC OBEX config";
261 serial_config_driver.bConfigurationValue = 3;
262 device_desc.bDeviceClass = USB_CLASS_COMM;
263 device_desc.idProduct =
264 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
266 serial_config_driver.label = "Generic Serial config";
267 serial_config_driver.bConfigurationValue = 1;
268 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
269 device_desc.idProduct =
270 cpu_to_le16(GS_PRODUCT_ID);
272 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
274 return usb_composite_probe(&gserial_driver);
278 static void __exit cleanup(void)
280 usb_composite_unregister(&gserial_driver);
283 module_exit(cleanup);