e7c6dd6c48e40b63b2fb1a9b1d2fb12d12b95111
[profile/mobile/platform/kernel/u-boot-tm1.git] / drivers / usb / gadget / f_serial.c
1 /*
2  * f_serial.c - generic USB serial function 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
13 //#include <linux/slab.h>
14 //#include <linux/kernel.h>
15 //#include <linux/device.h>
16 //#include <linux/usb/android_composite.h>
17 #include <common.h>
18 #include <ubi_uboot.h>
19 #include <linux/mtd/compat.h>
20
21 #include "u_serial.h"
22 #include "gadget_chips.h"
23
24
25 /*
26  * This function packages a simple "generic serial" port with no real
27  * control mechanisms, just raw data transfer over two bulk endpoints.
28  *
29  * Because it's not standardized, this isn't as interoperable as the
30  * CDC ACM driver.  However, for many purposes it's just as functional
31  * if you can arrange appropriate host side drivers.
32  */
33
34 struct gser_descs {
35         struct usb_endpoint_descriptor  *in;
36         struct usb_endpoint_descriptor  *out;
37 };
38
39 struct f_gser {
40         struct gserial                  port;
41         u8                              data_id;
42         u8                              port_num;
43
44         struct gser_descs               fs;
45         struct gser_descs               hs;
46 };
47
48 static inline struct f_gser *func_to_gser(struct usb_function *f)
49 {
50         return container_of(f, struct f_gser, port.func);
51 }
52
53 /*-------------------------------------------------------------------------*/
54
55 /* interface descriptor: */
56
57 static struct usb_interface_descriptor gser_interface_desc __initdata = {
58         .bLength =              USB_DT_INTERFACE_SIZE,
59         .bDescriptorType =      USB_DT_INTERFACE,
60         /* .bInterfaceNumber = DYNAMIC */
61         .bNumEndpoints =        2,
62         .bInterfaceClass =      USB_CLASS_VENDOR_SPEC,
63         .bInterfaceSubClass =   0,
64         .bInterfaceProtocol =   0,
65         /* .iInterface = DYNAMIC */
66 };
67
68 /* full speed support: */
69
70 static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = {
71         .bLength =              USB_DT_ENDPOINT_SIZE,
72         .bDescriptorType =      USB_DT_ENDPOINT,
73         .bEndpointAddress =     USB_DIR_IN,
74         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
75 };
76
77 static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = {
78         .bLength =              USB_DT_ENDPOINT_SIZE,
79         .bDescriptorType =      USB_DT_ENDPOINT,
80         .bEndpointAddress =     USB_DIR_OUT,
81         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
82 };
83
84 static struct usb_descriptor_header *gser_fs_function[] __initdata = {
85         (struct usb_descriptor_header *) &gser_interface_desc,
86         (struct usb_descriptor_header *) &gser_fs_in_desc,
87         (struct usb_descriptor_header *) &gser_fs_out_desc,
88         NULL,
89 };
90
91 /* high speed support: */
92
93 static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = {
94         .bLength =              USB_DT_ENDPOINT_SIZE,
95         .bDescriptorType =      USB_DT_ENDPOINT,
96         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
97         .wMaxPacketSize =       cpu_to_le16(512),
98 };
99
100 static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = {
101         .bLength =              USB_DT_ENDPOINT_SIZE,
102         .bDescriptorType =      USB_DT_ENDPOINT,
103         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
104         .wMaxPacketSize =       cpu_to_le16(512),
105 };
106
107 static struct usb_descriptor_header *gser_hs_function[] __initdata = {
108         (struct usb_descriptor_header *) &gser_interface_desc,
109         (struct usb_descriptor_header *) &gser_hs_in_desc,
110         (struct usb_descriptor_header *) &gser_hs_out_desc,
111         NULL,
112 };
113
114 /* string descriptors: */
115
116 static struct usb_string gser_string_defs[] = {
117         [0].s = "Generic Serial",
118         {  } /* end of list */
119 };
120
121 static struct usb_gadget_strings gser_string_table = {
122         .language =             0x0409, /* en-us */
123         .strings =              gser_string_defs,
124 };
125
126 static struct usb_gadget_strings *gser_strings[] = {
127         &gser_string_table,
128         NULL,
129 };
130
131 int usb_port_open = false;
132
133 /*-------------------------------------------------------------------------*/
134 static int gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
135 {
136         struct usb_composite_dev *cdev = f->config->cdev;
137         u16                     w_length = le16_to_cpu(ctrl->wLength);
138         u16         w_value = le16_to_cpu(ctrl->wValue);
139         int                     value = -EOPNOTSUPP;
140
141         DBG(cdev, "%s\n", __func__);
142         /* Handle Bulk-only class-specific requests */
143         if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
144                 switch (ctrl->bRequest) {
145                         case 0x22:
146                                 value = 0;
147                                 if ((w_value & 0xff) == 1)
148                                         usb_port_open = true;
149                                 else
150                                         usb_port_open = false;
151                                 break;
152                 }
153         }
154
155                 /* respond with data transfer or status phase? */
156         if (value >= 0) {
157                 int rc;
158                 cdev->req->zero = value < w_length;
159                 cdev->req->length = value;
160                 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
161                 if (rc < 0)
162                         printk("%s setup response queue error\n", __func__);
163         }
164
165         if (value == -EOPNOTSUPP)
166                 VDBG(cdev,
167                         "unknown class-specific control req "
168                         "%02x.%02x v%04x i%04x l%u\n",
169                         ctrl->bRequestType, ctrl->bRequest,
170                         le16_to_cpu(ctrl->wValue), le16_to_cpu(ctrl->wIndex), 
171             le16_to_cpu(ctrl->wLength));
172         return value;
173 }
174
175 static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
176 {
177         struct f_gser           *gser = func_to_gser(f);
178         struct usb_composite_dev *cdev = f->config->cdev;
179
180         /* we know alt == 0, so this is an activation or a reset */
181
182         if (gser->port.in->driver_data) {
183                 DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
184                 gserial_disconnect(&gser->port);
185         } else {
186                 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
187         }
188         gser->port.in_desc = ep_choose(cdev->gadget,
189                         gser->hs.in, gser->fs.in);
190         gser->port.out_desc = ep_choose(cdev->gadget,
191                         gser->hs.out, gser->fs.out);
192         gserial_connect(&gser->port, gser->port_num);
193         return 0;
194 }
195
196 static void gser_disable(struct usb_function *f)
197 {
198         struct f_gser   *gser = func_to_gser(f);
199         struct usb_composite_dev *cdev = f->config->cdev;
200
201         DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
202         usb_port_open = false;
203         gserial_disconnect(&gser->port);
204 }
205
206 /*-------------------------------------------------------------------------*/
207
208 /* serial function driver setup/binding */
209
210 static int __init
211 gser_bind(struct usb_configuration *c, struct usb_function *f)
212 {
213         struct usb_composite_dev *cdev = c->cdev;
214         struct f_gser           *gser = func_to_gser(f);
215         int                     status;
216         struct usb_ep           *ep;
217
218         /* allocate instance-specific interface IDs */
219         status = usb_interface_id(c, f);
220         if (status < 0)
221                 goto fail;
222         gser->data_id = status;
223         gser_interface_desc.bInterfaceNumber = status;
224
225         status = -ENODEV;
226
227         /* allocate instance-specific endpoints */
228         ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
229         if (!ep)
230                 goto fail;
231         gser->port.in = ep;
232         ep->driver_data = cdev; /* claim */
233
234         ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
235         if (!ep)
236                 goto fail;
237         gser->port.out = ep;
238         ep->driver_data = cdev; /* claim */
239
240         /* copy descriptors, and track endpoint copies */
241         f->descriptors = usb_copy_descriptors(gser_fs_function);
242
243         gser->fs.in = usb_find_endpoint(gser_fs_function,
244                         f->descriptors, &gser_fs_in_desc);
245         gser->fs.out = usb_find_endpoint(gser_fs_function,
246                         f->descriptors, &gser_fs_out_desc);
247
248
249         /* support all relevant hardware speeds... we expect that when
250          * hardware is dual speed, all bulk-capable endpoints work at
251          * both speeds
252          */
253         if (gadget_is_dualspeed(c->cdev->gadget)) {
254                 gser_hs_in_desc.bEndpointAddress =
255                                 gser_fs_in_desc.bEndpointAddress;
256                 gser_hs_out_desc.bEndpointAddress =
257                                 gser_fs_out_desc.bEndpointAddress;
258
259                 /* copy descriptors, and track endpoint copies */
260                 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
261
262                 gser->hs.in = usb_find_endpoint(gser_hs_function,
263                                 f->hs_descriptors, &gser_hs_in_desc);
264                 gser->hs.out = usb_find_endpoint(gser_hs_function,
265                                 f->hs_descriptors, &gser_hs_out_desc);
266         }
267
268         DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
269                         gser->port_num,
270                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
271                         gser->port.in->name, gser->port.out->name);
272         return 0;
273
274 fail:
275         /* we might as well release our claims on endpoints */
276         if (gser->port.out)
277                 gser->port.out->driver_data = NULL;
278         if (gser->port.in)
279                 gser->port.in->driver_data = NULL;
280
281         ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
282
283         return status;
284 }
285
286 static void
287 gser_unbind(struct usb_configuration *c, struct usb_function *f)
288 {
289         if (gadget_is_dualspeed(c->cdev->gadget))
290                 usb_free_descriptors(f->hs_descriptors);
291         usb_free_descriptors(f->descriptors);
292         kfree(func_to_gser(f));
293 }
294
295 /**
296  * gser_bind_config - add a generic serial function to a configuration
297  * @c: the configuration to support the serial instance
298  * @port_num: /dev/ttyGS* port this interface will use
299  * Context: single threaded during gadget setup
300  *
301  * Returns zero on success, else negative errno.
302  *
303  * Caller must have called @gserial_setup() with enough ports to
304  * handle all the ones it binds.  Caller is also responsible
305  * for calling @gserial_cleanup() before module unload.
306  */
307 int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
308 {
309         struct f_gser   *gser;
310         int             status;
311         /* REVISIT might want instance-specific strings to help
312          * distinguish instances ...
313          */
314
315         /* maybe allocate device-global string ID */
316         if (gser_string_defs[0].id == 0) {
317                 status = usb_string_id(c->cdev);
318                 if (status < 0)
319                         return status;
320                 gser_string_defs[0].id = status;
321         }
322
323         /* allocate and initialize one new instance */
324         gser = kzalloc(sizeof *gser, GFP_KERNEL);
325         if (!gser)
326                 return -ENOMEM;
327
328         gser->port_num = port_num;
329
330 //      sprintf(buf, "gser%d", port_num);
331 //      gser->port.func.name = buf;
332         gser->port.func.name = "gser";
333         gser->port.func.strings = gser_strings;
334         gser->port.func.bind = gser_bind;
335         gser->port.func.unbind = gser_unbind;
336         gser->port.func.set_alt = gser_set_alt;
337         gser->port.func.disable = gser_disable;
338         gser->port.func.setup = gser_setup;
339
340         status = usb_add_function(c, &gser->port.func);
341         if (status)
342                 kfree(gser);
343         return status;
344 }
345
346 #ifdef CONFIG_USB_ANDROID_GSERIAL
347
348 int gserial_function_bind_config(struct usb_configuration *c)
349 {
350         int ret = gser_bind_config(c, 0);
351         if (ret == 0)
352                 gserial_setup(c->cdev->gadget, 1);
353         return ret;
354 }
355
356 static struct android_usb_function gserial_function = {
357         .name = "gser",
358         .bind_config = gserial_function_bind_config,
359 };
360
361 static int __init init(void)
362 {
363         printk(KERN_INFO "f_serial init\n");
364         android_register_function(&gserial_function);
365         return 0;
366 }
367 module_init(init);
368
369 #endif /* CONFIG_USB_ANDROID_ACM */
370