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 #define pr_warning(args...) printf(##args)
59 /*-------------------------------------------------------------------------*/
61 /* Thanks to NetChip Technologies for donating this product ID.
63 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
64 * Instead: allocate your own, using normal USB-IF procedures.
66 //#define GS_VENDOR_ID 0x0525 /* NetChip */
67 #define GS_VENDOR_ID 0x1782/* SPREADTRUM*/
68 //#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
69 #define GS_PRODUCT_ID 0x4d00/* Linux-USB Serial Gadget */
70 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
71 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
73 /* string IDs are assigned dynamically */
75 #define STRING_MANUFACTURER_IDX 0
76 #define STRING_PRODUCT_IDX 1
77 #define STRING_DESCRIPTION_IDX 2
79 static char manufacturer[50];
81 static struct usb_string strings_dev[] = {
82 [STRING_MANUFACTURER_IDX].s = manufacturer,
83 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
84 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
88 static struct usb_gadget_strings stringtab_dev = {
89 .language = 0x0409, /* en-us */
90 .strings = strings_dev,
93 static struct usb_gadget_strings *dev_strings[] = {
98 static struct usb_device_descriptor device_desc = {
99 .bLength = USB_DT_DEVICE_SIZE,
100 .bDescriptorType = USB_DT_DEVICE,
101 .bcdUSB = cpu_to_le16(0x0200),
102 /* .bDeviceClass = f(use_acm) */
103 .bDeviceSubClass = 0,
104 .bDeviceProtocol = 0,
105 /* .bMaxPacketSize0 = f(hardware) */
106 .idVendor = cpu_to_le16(GS_VENDOR_ID),
107 /* .idProduct = f(use_acm) */
108 /* .bcdDevice = f(hardware) */
109 /* .iManufacturer = DYNAMIC */
110 /* .iProduct = DYNAMIC */
111 .bNumConfigurations = 1,
114 static struct usb_otg_descriptor otg_descriptor = {
115 .bLength = sizeof otg_descriptor,
116 .bDescriptorType = USB_DT_OTG,
118 /* REVISIT SRP-only hardware is possible, although
119 * it would not be called "OTG" ...
121 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
124 static const struct usb_descriptor_header *otg_desc[] = {
125 (struct usb_descriptor_header *) &otg_descriptor,
129 /*-------------------------------------------------------------------------*/
132 MODULE_DESCRIPTION(GS_VERSION_NAME);
133 MODULE_AUTHOR("Al Borchers");
134 MODULE_AUTHOR("David Brownell");
135 MODULE_LICENSE("GPL");
137 static int use_acm = false;
138 module_param(use_acm, bool, 0);
139 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
141 static int use_obex = false;
142 module_param(use_obex, bool, 0);
143 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
145 static unsigned n_ports = 1;
146 module_param(n_ports, uint, 0);
147 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
149 /*-------------------------------------------------------------------------*/
151 static int __init serial_bind_config(struct usb_configuration *c)
156 for (i = 0; i < n_ports && status == 0; i++) {
158 status = acm_bind_config(c, i);
160 status = obex_bind_config(c, i);
162 status = gser_bind_config(c, i);
167 static struct usb_configuration serial_config_driver = {
168 /* .label = f(use_acm) */
169 .bind = serial_bind_config,
170 /* .bConfigurationValue = f(use_acm) */
171 /* .iConfiguration = DYNAMIC */
172 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
175 static int __init gs_bind(struct usb_composite_dev *cdev)
178 struct usb_gadget *gadget = cdev->gadget;
180 status = gserial_setup(cdev->gadget, n_ports);
184 /* Allocate string descriptor numbers ... note that string
185 * contents can be overridden by the composite_dev glue.
188 /* device description: manufacturer, product */
190 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
191 init_utsname()->sysname, init_utsname()->release,
194 sprintf(manufacturer, "%s with %s", "spreadtrum", gadget->name);
196 status = usb_string_id(cdev);
199 strings_dev[STRING_MANUFACTURER_IDX].id = status;
201 device_desc.iManufacturer = status;
203 status = usb_string_id(cdev);
206 strings_dev[STRING_PRODUCT_IDX].id = status;
208 device_desc.iProduct = status;
210 /* config description */
211 status = usb_string_id(cdev);
214 strings_dev[STRING_DESCRIPTION_IDX].id = status;
216 serial_config_driver.iConfiguration = status;
218 /* set up other descriptors */
219 gcnum = usb_gadget_controller_number(gadget);
221 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
223 /* this is so simple (for now, no altsettings) that it
224 * SHOULD NOT have problems with bulk-capable hardware.
225 * so warn about unrcognized controllers -- don't panic.
227 * things like configuration and altsetting numbering
228 * can need hardware-specific attention though.
230 printf("gs_bind: controller '%s' not recognized\n",
232 device_desc.bcdDevice =
233 cpu_to_le16(GS_VERSION_NUM | 0x0099);
236 if (gadget_is_otg(cdev->gadget)) {
237 serial_config_driver.descriptors = otg_desc;
238 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
241 /* register our configuration */
242 status = usb_add_config(cdev, &serial_config_driver);
246 INFO(cdev, "%s\n", GS_VERSION_NAME);
255 static struct usb_composite_driver gserial_driver = {
258 .strings = dev_strings,
262 int __init usb_serial_init(void)
264 /* We *could* export two configs; that'd be much cleaner...
265 * but neither of these product IDs was defined that way.
269 serial_config_driver.label = "CDC ACM config";
270 serial_config_driver.bConfigurationValue = 1;//2;
271 device_desc.bDeviceClass = USB_CLASS_COMM;
272 device_desc.idProduct =
273 cpu_to_le16(GS_CDC_PRODUCT_ID);
274 } else if (use_obex) {
275 serial_config_driver.label = "CDC OBEX config";
276 serial_config_driver.bConfigurationValue = 3;
277 device_desc.bDeviceClass = USB_CLASS_COMM;
278 device_desc.idProduct =
279 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
281 serial_config_driver.label = "Generic Serial config";
282 serial_config_driver.bConfigurationValue = 1;
283 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
284 device_desc.idProduct =
285 cpu_to_le16(GS_PRODUCT_ID);
287 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
289 return usb_composite_register(&gserial_driver);
291 module_init(usb_serial_init);
293 void usb_serial_cleanup(void)
295 usb_composite_unregister(&gserial_driver);
298 module_exit(cleanup);