usb: gadget: remove duplicated print macro
[profile/mobile/platform/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 /*-------------------------------------------------------------------------*/
58
59 /* Thanks to NetChip Technologies for donating this product ID.
60 *
61 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
62 * Instead:  allocate your own, using normal USB-IF procedures.
63 */
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 */
70
71 /* string IDs are assigned dynamically */
72
73 #define STRING_MANUFACTURER_IDX         0
74 #define STRING_PRODUCT_IDX              1
75 #define STRING_DESCRIPTION_IDX          2
76
77 static char manufacturer[50];
78
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) */,
83         {  } /* end of list */
84 };
85
86 static struct usb_gadget_strings stringtab_dev = {
87         .language       = 0x0409,       /* en-us */
88         .strings        = strings_dev,
89 };
90
91 static struct usb_gadget_strings *dev_strings[] = {
92         &stringtab_dev,
93         NULL,
94 };
95
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,
110 };
111
112 static struct usb_otg_descriptor otg_descriptor = {
113         .bLength =              sizeof otg_descriptor,
114         .bDescriptorType =      USB_DT_OTG,
115
116         /* REVISIT SRP-only hardware is possible, although
117          * it would not be called "OTG" ...
118          */
119         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
120 };
121
122 static const struct usb_descriptor_header *otg_desc[] = {
123         (struct usb_descriptor_header *) &otg_descriptor,
124         NULL,
125 };
126
127 /*-------------------------------------------------------------------------*/
128
129 /* Module */
130 MODULE_DESCRIPTION(GS_VERSION_NAME);
131 MODULE_AUTHOR("Al Borchers");
132 MODULE_AUTHOR("David Brownell");
133 MODULE_LICENSE("GPL");
134
135 static int use_acm = false;
136 module_param(use_acm, bool, 0);
137 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
138
139 static int use_obex = false;
140 module_param(use_obex, bool, 0);
141 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
142
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");
146
147 /*-------------------------------------------------------------------------*/
148
149 static int __init serial_bind_config(struct usb_configuration *c)
150 {
151         unsigned i;
152         int status = 0;
153
154         for (i = 0; i < n_ports && status == 0; i++) {
155                 if (use_acm)
156                         status = acm_bind_config(c, i);
157                 else if (use_obex)
158                         status = obex_bind_config(c, i);
159                 else
160                         status = gser_bind_config(c, i);
161         }
162         return status;
163 }
164
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,
171 };
172
173 static int __init gs_bind(struct usb_composite_dev *cdev)
174 {
175         int                     gcnum;
176         struct usb_gadget       *gadget = cdev->gadget;
177         int                     status;
178         status = gserial_setup(cdev->gadget, n_ports);
179         if (status < 0)
180                 return status;
181
182         /* Allocate string descriptor numbers ... note that string
183          * contents can be overridden by the composite_dev glue.
184          */
185
186         /* device description: manufacturer, product */
187         #if 0 
188         snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
189                 init_utsname()->sysname, init_utsname()->release,
190                 gadget->name);
191         #else
192         sprintf(manufacturer, "%s with %s", "spreadtrum", gadget->name);
193         #endif
194         status = usb_string_id(cdev);
195         if (status < 0)
196                 goto fail;
197         strings_dev[STRING_MANUFACTURER_IDX].id = status;
198
199         device_desc.iManufacturer = status;
200
201         status = usb_string_id(cdev);
202         if (status < 0)
203                 goto fail;
204         strings_dev[STRING_PRODUCT_IDX].id = status;
205
206         device_desc.iProduct = status;
207
208         /* config description */
209         status = usb_string_id(cdev);
210         if (status < 0)
211                 goto fail;
212         strings_dev[STRING_DESCRIPTION_IDX].id = status;
213
214         serial_config_driver.iConfiguration = status;
215
216         /* set up other descriptors */
217         gcnum = usb_gadget_controller_number(gadget);
218         if (gcnum >= 0)
219                 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
220         else {
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.
224                  *
225                  * things like configuration and altsetting numbering
226                  * can need hardware-specific attention though.
227                  */
228                 printf("gs_bind: controller '%s' not recognized\n",
229                         gadget->name);
230                 device_desc.bcdDevice =
231                         cpu_to_le16(GS_VERSION_NUM | 0x0099);
232         }
233
234         if (gadget_is_otg(cdev->gadget)) {
235                 serial_config_driver.descriptors = otg_desc;
236                 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
237         }
238
239         /* register our configuration */
240         status = usb_add_config(cdev, &serial_config_driver);
241         if (status < 0)
242                 goto fail;
243
244         INFO(cdev, "%s\n", GS_VERSION_NAME);
245
246         return 0;
247
248 fail:
249         gserial_cleanup();
250         return status;
251 }
252
253 static struct usb_composite_driver gserial_driver = {
254         .name           = "g_serial",
255         .dev            = &device_desc,
256         .strings        = dev_strings,
257         .bind           = gs_bind,
258 };
259
260 int __init usb_serial_init(void)
261 {
262         /* We *could* export two configs; that'd be much cleaner...
263          * but neither of these product IDs was defined that way.
264          */
265          n_ports = 1;
266         if (use_acm) {
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);
278         } else {
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);
284         }
285         strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
286
287         return usb_composite_register(&gserial_driver);
288 }
289 module_init(usb_serial_init);
290
291 void  usb_serial_cleanup(void)
292 {
293         usb_composite_unregister(&gserial_driver);
294         gserial_cleanup();
295 }
296 module_exit(cleanup);