tizen 2.4 release
[kernel/u-boot-tm1.git] / drivers / usb / gadget / serial.c
1 /*
2  * serial.c -- USB gadget serial driver
3  *
4  * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5  * Copyright (C) 2008 by David Brownell
6  * Copyright (C) 2008 by Nokia Corporation
7  *
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.
11  */
12 #include <common.h>
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>
18
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>
24
25 #include "u_serial.h"
26 #include "gadget_chips.h"
27
28
29 /* Defines */
30
31 #define GS_VERSION_STR                  "v2.4"
32 #define GS_VERSION_NUM                  0x2400
33
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
37
38 /*-------------------------------------------------------------------------*/
39
40 /*
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.
46  */
47 #include "composite.c"
48 //#include "usbstring.c"
49 //#include "config.c"
50 //#include "epautoconf.c"
51
52 #include "f_acm.c"
53 #include "f_obex.c"
54 #include "f_serial.c"
55 #include "u_serial.c"
56
57 #define pr_warning(args...) printf(##args)
58
59 /*-------------------------------------------------------------------------*/
60
61 /* Thanks to NetChip Technologies for donating this product ID.
62 *
63 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
64 * Instead:  allocate your own, using normal USB-IF procedures.
65 */
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 */
72
73 /* string IDs are assigned dynamically */
74
75 #define STRING_MANUFACTURER_IDX         0
76 #define STRING_PRODUCT_IDX              1
77 #define STRING_DESCRIPTION_IDX          2
78
79 static char manufacturer[50];
80
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) */,
85         {  } /* end of list */
86 };
87
88 static struct usb_gadget_strings stringtab_dev = {
89         .language       = 0x0409,       /* en-us */
90         .strings        = strings_dev,
91 };
92
93 static struct usb_gadget_strings *dev_strings[] = {
94         &stringtab_dev,
95         NULL,
96 };
97
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,
112 };
113
114 static struct usb_otg_descriptor otg_descriptor = {
115         .bLength =              sizeof otg_descriptor,
116         .bDescriptorType =      USB_DT_OTG,
117
118         /* REVISIT SRP-only hardware is possible, although
119          * it would not be called "OTG" ...
120          */
121         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
122 };
123
124 static const struct usb_descriptor_header *otg_desc[] = {
125         (struct usb_descriptor_header *) &otg_descriptor,
126         NULL,
127 };
128
129 /*-------------------------------------------------------------------------*/
130
131 /* Module */
132 MODULE_DESCRIPTION(GS_VERSION_NAME);
133 MODULE_AUTHOR("Al Borchers");
134 MODULE_AUTHOR("David Brownell");
135 MODULE_LICENSE("GPL");
136
137 static int use_acm = false;
138 module_param(use_acm, bool, 0);
139 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
140
141 static int use_obex = false;
142 module_param(use_obex, bool, 0);
143 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
144
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");
148
149 /*-------------------------------------------------------------------------*/
150
151 static int __init serial_bind_config(struct usb_configuration *c)
152 {
153         unsigned i;
154         int status = 0;
155
156         for (i = 0; i < n_ports && status == 0; i++) {
157                 if (use_acm)
158                         status = acm_bind_config(c, i);
159                 else if (use_obex)
160                         status = obex_bind_config(c, i);
161                 else
162                         status = gser_bind_config(c, i);
163         }
164         return status;
165 }
166
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,
173 };
174
175 static int __init gs_bind(struct usb_composite_dev *cdev)
176 {
177         int                     gcnum;
178         struct usb_gadget       *gadget = cdev->gadget;
179         int                     status;
180         status = gserial_setup(cdev->gadget, n_ports);
181         if (status < 0)
182                 return status;
183
184         /* Allocate string descriptor numbers ... note that string
185          * contents can be overridden by the composite_dev glue.
186          */
187
188         /* device description: manufacturer, product */
189         #if 0 
190         snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
191                 init_utsname()->sysname, init_utsname()->release,
192                 gadget->name);
193         #else
194         sprintf(manufacturer, "%s with %s", "spreadtrum", gadget->name);
195         #endif
196         status = usb_string_id(cdev);
197         if (status < 0)
198                 goto fail;
199         strings_dev[STRING_MANUFACTURER_IDX].id = status;
200
201         device_desc.iManufacturer = status;
202
203         status = usb_string_id(cdev);
204         if (status < 0)
205                 goto fail;
206         strings_dev[STRING_PRODUCT_IDX].id = status;
207
208         device_desc.iProduct = status;
209
210         /* config description */
211         status = usb_string_id(cdev);
212         if (status < 0)
213                 goto fail;
214         strings_dev[STRING_DESCRIPTION_IDX].id = status;
215
216         serial_config_driver.iConfiguration = status;
217
218         /* set up other descriptors */
219         gcnum = usb_gadget_controller_number(gadget);
220         if (gcnum >= 0)
221                 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
222         else {
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.
226                  *
227                  * things like configuration and altsetting numbering
228                  * can need hardware-specific attention though.
229                  */
230                 printf("gs_bind: controller '%s' not recognized\n",
231                         gadget->name);
232                 device_desc.bcdDevice =
233                         cpu_to_le16(GS_VERSION_NUM | 0x0099);
234         }
235
236         if (gadget_is_otg(cdev->gadget)) {
237                 serial_config_driver.descriptors = otg_desc;
238                 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
239         }
240
241         /* register our configuration */
242         status = usb_add_config(cdev, &serial_config_driver);
243         if (status < 0)
244                 goto fail;
245
246         INFO(cdev, "%s\n", GS_VERSION_NAME);
247
248         return 0;
249
250 fail:
251         gserial_cleanup();
252         return status;
253 }
254
255 static struct usb_composite_driver gserial_driver = {
256         .name           = "g_serial",
257         .dev            = &device_desc,
258         .strings        = dev_strings,
259         .bind           = gs_bind,
260 };
261
262 int __init usb_serial_init(void)
263 {
264         /* We *could* export two configs; that'd be much cleaner...
265          * but neither of these product IDs was defined that way.
266          */
267          n_ports = 1;
268         if (use_acm) {
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);
280         } else {
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);
286         }
287         strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
288
289         return usb_composite_register(&gserial_driver);
290 }
291 module_init(usb_serial_init);
292
293 void  usb_serial_cleanup(void)
294 {
295         usb_composite_unregister(&gserial_driver);
296         gserial_cleanup();
297 }
298 module_exit(cleanup);