2 * f_serial.c - generic USB serial function 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 <linux/slab.h>
14 //#include <linux/kernel.h>
15 //#include <linux/device.h>
16 //#include <linux/usb/android_composite.h>
18 #include <ubi_uboot.h>
19 #include <linux/mtd/compat.h>
22 #include "gadget_chips.h"
26 * This function packages a simple "generic serial" port with no real
27 * control mechanisms, just raw data transfer over two bulk endpoints.
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.
35 struct usb_endpoint_descriptor *in;
36 struct usb_endpoint_descriptor *out;
48 static inline struct f_gser *func_to_gser(struct usb_function *f)
50 return container_of(f, struct f_gser, port.func);
53 /*-------------------------------------------------------------------------*/
55 /* interface descriptor: */
57 static struct usb_interface_descriptor gser_interface_desc __initdata = {
58 .bLength = USB_DT_INTERFACE_SIZE,
59 .bDescriptorType = USB_DT_INTERFACE,
60 /* .bInterfaceNumber = DYNAMIC */
62 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
63 .bInterfaceSubClass = 0,
64 .bInterfaceProtocol = 0,
65 /* .iInterface = DYNAMIC */
68 /* full speed support: */
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,
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,
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,
91 /* high speed support: */
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),
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),
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,
114 /* string descriptors: */
116 static struct usb_string gser_string_defs[] = {
117 [0].s = "Generic Serial",
118 { } /* end of list */
121 static struct usb_gadget_strings gser_string_table = {
122 .language = 0x0409, /* en-us */
123 .strings = gser_string_defs,
126 static struct usb_gadget_strings *gser_strings[] = {
131 int usb_port_open = false;
133 /*-------------------------------------------------------------------------*/
134 static int gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
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;
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) {
147 if ((w_value & 0xff) == 1)
148 usb_port_open = true;
150 usb_port_open = false;
155 /* respond with data transfer or status phase? */
158 cdev->req->zero = value < w_length;
159 cdev->req->length = value;
160 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
162 printk("%s setup response queue error\n", __func__);
165 if (value == -EOPNOTSUPP)
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));
175 static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
177 struct f_gser *gser = func_to_gser(f);
178 struct usb_composite_dev *cdev = f->config->cdev;
180 /* we know alt == 0, so this is an activation or a reset */
182 if (gser->port.in->driver_data) {
183 DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
184 gserial_disconnect(&gser->port);
186 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
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);
196 static void gser_disable(struct usb_function *f)
198 struct f_gser *gser = func_to_gser(f);
199 struct usb_composite_dev *cdev = f->config->cdev;
201 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
202 usb_port_open = false;
203 gserial_disconnect(&gser->port);
206 /*-------------------------------------------------------------------------*/
208 /* serial function driver setup/binding */
211 gser_bind(struct usb_configuration *c, struct usb_function *f)
213 struct usb_composite_dev *cdev = c->cdev;
214 struct f_gser *gser = func_to_gser(f);
218 /* allocate instance-specific interface IDs */
219 status = usb_interface_id(c, f);
222 gser->data_id = status;
223 gser_interface_desc.bInterfaceNumber = status;
227 /* allocate instance-specific endpoints */
228 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
232 ep->driver_data = cdev; /* claim */
234 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
238 ep->driver_data = cdev; /* claim */
240 /* copy descriptors, and track endpoint copies */
241 f->descriptors = usb_copy_descriptors(gser_fs_function);
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);
249 /* support all relevant hardware speeds... we expect that when
250 * hardware is dual speed, all bulk-capable endpoints work at
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;
259 /* copy descriptors, and track endpoint copies */
260 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
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);
268 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
270 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
271 gser->port.in->name, gser->port.out->name);
275 /* we might as well release our claims on endpoints */
277 gser->port.out->driver_data = NULL;
279 gser->port.in->driver_data = NULL;
281 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
287 gser_unbind(struct usb_configuration *c, struct usb_function *f)
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));
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
301 * Returns zero on success, else negative errno.
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.
307 int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
311 /* REVISIT might want instance-specific strings to help
312 * distinguish instances ...
315 /* maybe allocate device-global string ID */
316 if (gser_string_defs[0].id == 0) {
317 status = usb_string_id(c->cdev);
320 gser_string_defs[0].id = status;
323 /* allocate and initialize one new instance */
324 gser = kzalloc(sizeof *gser, GFP_KERNEL);
328 gser->port_num = port_num;
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;
340 status = usb_add_function(c, &gser->port.func);
346 #ifdef CONFIG_USB_ANDROID_GSERIAL
348 int gserial_function_bind_config(struct usb_configuration *c)
350 int ret = gser_bind_config(c, 0);
352 gserial_setup(c->cdev->gadget, 1);
356 static struct android_usb_function gserial_function = {
358 .bind_config = gserial_function_bind_config,
361 static int __init init(void)
363 printk(KERN_INFO "f_serial init\n");
364 android_register_function(&gserial_function);
369 #endif /* CONFIG_USB_ANDROID_ACM */