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.
44 /*-------------------------------------------------------------------------*/
45 USB_GADGET_COMPOSITE_OPTIONS();
47 /* Thanks to NetChip Technologies for donating this product ID.
49 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50 * Instead: allocate your own, using normal USB-IF procedures.
52 #define GS_VENDOR_ID 0x0525 /* NetChip */
53 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
54 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
55 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
57 /* string IDs are assigned dynamically */
59 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
61 static struct usb_string strings_dev[] = {
62 [USB_GADGET_MANUFACTURER_IDX].s = "",
63 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
64 [USB_GADGET_SERIAL_IDX].s = "",
65 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
69 static struct usb_gadget_strings stringtab_dev = {
70 .language = 0x0409, /* en-us */
71 .strings = strings_dev,
74 static struct usb_gadget_strings *dev_strings[] = {
79 static struct usb_device_descriptor device_desc = {
80 .bLength = USB_DT_DEVICE_SIZE,
81 .bDescriptorType = USB_DT_DEVICE,
82 .bcdUSB = cpu_to_le16(0x0200),
83 /* .bDeviceClass = f(use_acm) */
86 /* .bMaxPacketSize0 = f(hardware) */
87 .idVendor = cpu_to_le16(GS_VENDOR_ID),
88 /* .idProduct = f(use_acm) */
89 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
90 /* .iManufacturer = DYNAMIC */
91 /* .iProduct = DYNAMIC */
92 .bNumConfigurations = 1,
95 static struct usb_otg_descriptor otg_descriptor = {
96 .bLength = sizeof otg_descriptor,
97 .bDescriptorType = USB_DT_OTG,
99 /* REVISIT SRP-only hardware is possible, although
100 * it would not be called "OTG" ...
102 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
105 static const struct usb_descriptor_header *otg_desc[] = {
106 (struct usb_descriptor_header *) &otg_descriptor,
110 /*-------------------------------------------------------------------------*/
113 MODULE_DESCRIPTION(GS_VERSION_NAME);
114 MODULE_AUTHOR("Al Borchers");
115 MODULE_AUTHOR("David Brownell");
116 MODULE_LICENSE("GPL");
118 static bool use_acm = true;
119 module_param(use_acm, bool, 0);
120 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
122 static bool use_obex = false;
123 module_param(use_obex, bool, 0);
124 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
126 static unsigned n_ports = 1;
127 module_param(n_ports, uint, 0);
128 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
130 /*-------------------------------------------------------------------------*/
132 static int __init serial_bind_config(struct usb_configuration *c)
137 for (i = 0; i < n_ports && status == 0; i++) {
139 status = acm_bind_config(c, i);
141 status = obex_bind_config(c, i);
143 status = gser_bind_config(c, i);
148 static struct usb_configuration serial_config_driver = {
149 /* .label = f(use_acm) */
150 /* .bConfigurationValue = f(use_acm) */
151 /* .iConfiguration = DYNAMIC */
152 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
155 static int __init gs_bind(struct usb_composite_dev *cdev)
159 status = gserial_setup(cdev->gadget, n_ports);
163 /* Allocate string descriptor numbers ... note that string
164 * contents can be overridden by the composite_dev glue.
167 status = usb_string_ids_tab(cdev, strings_dev);
170 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
171 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
172 status = strings_dev[STRING_DESCRIPTION_IDX].id;
173 serial_config_driver.iConfiguration = status;
175 if (gadget_is_otg(cdev->gadget)) {
176 serial_config_driver.descriptors = otg_desc;
177 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
180 /* register our configuration */
181 status = usb_add_config(cdev, &serial_config_driver,
186 usb_composite_overwrite_options(cdev, &coverwrite);
187 INFO(cdev, "%s\n", GS_VERSION_NAME);
196 static __refdata struct usb_composite_driver gserial_driver = {
199 .strings = dev_strings,
200 .max_speed = USB_SPEED_SUPER,
204 static int __init init(void)
206 /* We *could* export two configs; that'd be much cleaner...
207 * but neither of these product IDs was defined that way.
210 serial_config_driver.label = "CDC ACM config";
211 serial_config_driver.bConfigurationValue = 2;
212 device_desc.bDeviceClass = USB_CLASS_COMM;
213 device_desc.idProduct =
214 cpu_to_le16(GS_CDC_PRODUCT_ID);
215 } else if (use_obex) {
216 serial_config_driver.label = "CDC OBEX config";
217 serial_config_driver.bConfigurationValue = 3;
218 device_desc.bDeviceClass = USB_CLASS_COMM;
219 device_desc.idProduct =
220 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
222 serial_config_driver.label = "Generic Serial config";
223 serial_config_driver.bConfigurationValue = 1;
224 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
225 device_desc.idProduct =
226 cpu_to_le16(GS_PRODUCT_ID);
228 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
230 return usb_composite_probe(&gserial_driver);
234 static void __exit cleanup(void)
236 usb_composite_unregister(&gserial_driver);
239 module_exit(cleanup);