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 <ubi_uboot.h>
14 #include <asm/errno.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/gadget.h>
17 #include <linux/ctype.h>
19 //#include <linux/kernel.h>
20 //#include <linux/utsname.h>
21 //#include <linux/device.h>
22 //#include <linux/tty.h>
23 //#include <linux/tty_flip.h>
26 #include "gadget_chips.h"
31 #define GS_VERSION_STR "v2.4"
32 #define GS_VERSION_NUM 0x2400
34 #define GS_LONG_NAME "Gadget Serial"
35 //#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
36 #define GS_VERSION_NAME GS_LONG_NAME
38 /*-------------------------------------------------------------------------*/
41 * Kbuild is not very cooperative with respect to linking separately
42 * compiled library objects into one module. So for now we won't use
43 * separate compilation ... ensuring init/exit sections work to shrink
44 * the runtime footprint, and giving us at least some parts of what
45 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
47 #include "composite.c"
48 //#include "usbstring.c"
50 //#include "epautoconf.c"
57 /*-------------------------------------------------------------------------*/
59 /* Thanks to NetChip Technologies for donating this product ID.
61 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
62 * Instead: allocate your own, using normal USB-IF procedures.
64 //#define GS_VENDOR_ID 0x0525 /* NetChip */
65 #define GS_VENDOR_ID 0x1782/* SPREADTRUM*/
66 //#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
67 #define GS_PRODUCT_ID 0x4d00/* Linux-USB Serial Gadget */
68 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
69 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
71 /* string IDs are assigned dynamically */
73 #define STRING_MANUFACTURER_IDX 0
74 #define STRING_PRODUCT_IDX 1
75 #define STRING_DESCRIPTION_IDX 2
77 static char manufacturer[50];
79 static struct usb_string strings_dev[] = {
80 [STRING_MANUFACTURER_IDX].s = manufacturer,
81 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
82 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
86 static struct usb_gadget_strings stringtab_dev = {
87 .language = 0x0409, /* en-us */
88 .strings = strings_dev,
91 static struct usb_gadget_strings *dev_strings[] = {
96 static struct usb_device_descriptor device_desc = {
97 .bLength = USB_DT_DEVICE_SIZE,
98 .bDescriptorType = USB_DT_DEVICE,
99 .bcdUSB = cpu_to_le16(0x0200),
100 /* .bDeviceClass = f(use_acm) */
101 .bDeviceSubClass = 0,
102 .bDeviceProtocol = 0,
103 /* .bMaxPacketSize0 = f(hardware) */
104 .idVendor = cpu_to_le16(GS_VENDOR_ID),
105 /* .idProduct = f(use_acm) */
106 /* .bcdDevice = f(hardware) */
107 /* .iManufacturer = DYNAMIC */
108 /* .iProduct = DYNAMIC */
109 .bNumConfigurations = 1,
112 static struct usb_otg_descriptor otg_descriptor = {
113 .bLength = sizeof otg_descriptor,
114 .bDescriptorType = USB_DT_OTG,
116 /* REVISIT SRP-only hardware is possible, although
117 * it would not be called "OTG" ...
119 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
122 static const struct usb_descriptor_header *otg_desc[] = {
123 (struct usb_descriptor_header *) &otg_descriptor,
127 /*-------------------------------------------------------------------------*/
130 MODULE_DESCRIPTION(GS_VERSION_NAME);
131 MODULE_AUTHOR("Al Borchers");
132 MODULE_AUTHOR("David Brownell");
133 MODULE_LICENSE("GPL");
135 static int use_acm = false;
136 module_param(use_acm, bool, 0);
137 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
139 static int use_obex = false;
140 module_param(use_obex, bool, 0);
141 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
143 static unsigned n_ports = 1;
144 module_param(n_ports, uint, 0);
145 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
147 /*-------------------------------------------------------------------------*/
149 static int __init serial_bind_config(struct usb_configuration *c)
154 for (i = 0; i < n_ports && status == 0; i++) {
156 status = acm_bind_config(c, i);
158 status = obex_bind_config(c, i);
160 status = gser_bind_config(c, i);
165 static struct usb_configuration serial_config_driver = {
166 /* .label = f(use_acm) */
167 .bind = serial_bind_config,
168 /* .bConfigurationValue = f(use_acm) */
169 /* .iConfiguration = DYNAMIC */
170 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
173 static int __init gs_bind(struct usb_composite_dev *cdev)
176 struct usb_gadget *gadget = cdev->gadget;
178 status = gserial_setup(cdev->gadget, n_ports);
182 /* Allocate string descriptor numbers ... note that string
183 * contents can be overridden by the composite_dev glue.
186 /* device description: manufacturer, product */
188 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
189 init_utsname()->sysname, init_utsname()->release,
192 sprintf(manufacturer, "%s with %s", "spreadtrum", gadget->name);
194 status = usb_string_id(cdev);
197 strings_dev[STRING_MANUFACTURER_IDX].id = status;
199 device_desc.iManufacturer = status;
201 status = usb_string_id(cdev);
204 strings_dev[STRING_PRODUCT_IDX].id = status;
206 device_desc.iProduct = status;
208 /* config description */
209 status = usb_string_id(cdev);
212 strings_dev[STRING_DESCRIPTION_IDX].id = status;
214 serial_config_driver.iConfiguration = status;
216 /* set up other descriptors */
217 gcnum = usb_gadget_controller_number(gadget);
219 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
221 /* this is so simple (for now, no altsettings) that it
222 * SHOULD NOT have problems with bulk-capable hardware.
223 * so warn about unrcognized controllers -- don't panic.
225 * things like configuration and altsetting numbering
226 * can need hardware-specific attention though.
228 printf("gs_bind: controller '%s' not recognized\n",
230 device_desc.bcdDevice =
231 cpu_to_le16(GS_VERSION_NUM | 0x0099);
234 if (gadget_is_otg(cdev->gadget)) {
235 serial_config_driver.descriptors = otg_desc;
236 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
239 /* register our configuration */
240 status = usb_add_config(cdev, &serial_config_driver);
244 INFO(cdev, "%s\n", GS_VERSION_NAME);
253 static struct usb_composite_driver gserial_driver = {
256 .strings = dev_strings,
260 int __init usb_serial_init(void)
262 /* We *could* export two configs; that'd be much cleaner...
263 * but neither of these product IDs was defined that way.
267 serial_config_driver.label = "CDC ACM config";
268 serial_config_driver.bConfigurationValue = 1;//2;
269 device_desc.bDeviceClass = USB_CLASS_COMM;
270 device_desc.idProduct =
271 cpu_to_le16(GS_CDC_PRODUCT_ID);
272 } else if (use_obex) {
273 serial_config_driver.label = "CDC OBEX config";
274 serial_config_driver.bConfigurationValue = 3;
275 device_desc.bDeviceClass = USB_CLASS_COMM;
276 device_desc.idProduct =
277 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
279 serial_config_driver.label = "Generic Serial config";
280 serial_config_driver.bConfigurationValue = 1;
281 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
282 device_desc.idProduct =
283 cpu_to_le16(GS_PRODUCT_ID);
285 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
287 return usb_composite_register(&gserial_driver);
289 module_init(usb_serial_init);
291 void usb_serial_cleanup(void)
293 usb_composite_unregister(&gserial_driver);
296 module_exit(cleanup);