blob: 768a38e896f7678885e5ed18ffffec94338a203f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownella7707ad2008-06-19 17:52:07 -07002 * serial.c -- USB gadget serial driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Brownella7707ad2008-06-19 17:52:07 -07004 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
David Brownell7bb5ea52008-06-19 18:19:03 -07006 * Copyright (C) 2008 by Nokia Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/device.h>
16#include <linux/tty.h>
17#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
David Brownella7707ad2008-06-19 17:52:07 -070019#include "u_serial.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "gadget_chips.h"
21
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* Defines */
24
David Brownell7bb5ea52008-06-19 18:19:03 -070025#define GS_VERSION_STR "v2.4"
26#define GS_VERSION_NUM 0x2400
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#define GS_LONG_NAME "Gadget Serial"
David Brownella7707ad2008-06-19 17:52:07 -070029#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
30
David Brownell7bb5ea52008-06-19 18:19:03 -070031/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Brownell4e9ba512008-08-18 17:41:02 -070033/*
34 * Kbuild is not very cooperative with respect to linking separately
35 * compiled library objects into one module. So for now we won't use
36 * separate compilation ... ensuring init/exit sections work to shrink
37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
39 */
40#include "composite.c"
David Brownell4e9ba512008-08-18 17:41:02 -070041
42#include "f_acm.c"
Felipe Balbi30867752008-08-18 17:39:30 -070043#include "f_obex.c"
David Brownell4e9ba512008-08-18 17:41:02 -070044#include "f_serial.c"
45#include "u_serial.c"
46
47/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020048USB_GADGET_COMPOSITE_OPTIONS();
David Brownell4e9ba512008-08-18 17:41:02 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* Thanks to NetChip Technologies for donating this product ID.
David Brownell7bb5ea52008-06-19 18:19:03 -070051*
52* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
53* Instead: allocate your own, using normal USB-IF procedures.
54*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define GS_VENDOR_ID 0x0525 /* NetChip */
56#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
57#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
Felipe Balbi30867752008-08-18 17:39:30 -070058#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David Brownell7bb5ea52008-06-19 18:19:03 -070060/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020062#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
David Brownella7707ad2008-06-19 17:52:07 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static char manufacturer[50];
David Brownell7bb5ea52008-06-19 18:19:03 -070065
66static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020067 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer,
68 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
69 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell7bb5ea52008-06-19 18:19:03 -070070 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 { } /* end of list */
72};
73
David Brownell7bb5ea52008-06-19 18:19:03 -070074static struct usb_gadget_strings stringtab_dev = {
75 .language = 0x0409, /* en-us */
76 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
David Brownell7bb5ea52008-06-19 18:19:03 -070079static struct usb_gadget_strings *dev_strings[] = {
80 &stringtab_dev,
81 NULL,
82};
83
84static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .bLength = USB_DT_DEVICE_SIZE,
86 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -080087 .bcdUSB = cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070088 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .bDeviceSubClass = 0,
90 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070091 /* .bMaxPacketSize0 = f(hardware) */
Harvey Harrison551509d2009-02-11 14:11:36 -080092 .idVendor = cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070093 /* .idProduct = f(use_acm) */
94 /* .bcdDevice = f(hardware) */
95 /* .iManufacturer = DYNAMIC */
96 /* .iProduct = DYNAMIC */
97 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
David Brownell7bb5ea52008-06-19 18:19:03 -0700100static struct usb_otg_descriptor otg_descriptor = {
101 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -0700103
104 /* REVISIT SRP-only hardware is possible, although
105 * it would not be called "OTG" ...
106 */
107 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
David Brownell7bb5ea52008-06-19 18:19:03 -0700110static const struct usb_descriptor_header *otg_desc[] = {
111 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 NULL,
113};
114
David Brownell9079e912008-05-07 16:00:36 -0700115/*-------------------------------------------------------------------------*/
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700118MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700120MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121MODULE_LICENSE("GPL");
122
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030123static bool use_acm = true;
David Brownell7bb5ea52008-06-19 18:19:03 -0700124module_param(use_acm, bool, 0);
125MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
126
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030127static bool use_obex = false;
Felipe Balbi30867752008-08-18 17:39:30 -0700128module_param(use_obex, bool, 0);
129MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
130
David Brownell7bb5ea52008-06-19 18:19:03 -0700131static unsigned n_ports = 1;
132module_param(n_ports, uint, 0);
133MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
David Brownell9079e912008-05-07 16:00:36 -0700135/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200137static int __init serial_bind_config(struct usb_configuration *c)
David Brownell9079e912008-05-07 16:00:36 -0700138{
David Brownell7bb5ea52008-06-19 18:19:03 -0700139 unsigned i;
140 int status = 0;
David Brownell9079e912008-05-07 16:00:36 -0700141
David Brownell7bb5ea52008-06-19 18:19:03 -0700142 for (i = 0; i < n_ports && status == 0; i++) {
143 if (use_acm)
144 status = acm_bind_config(c, i);
Felipe Balbi30867752008-08-18 17:39:30 -0700145 else if (use_obex)
146 status = obex_bind_config(c, i);
David Brownell7bb5ea52008-06-19 18:19:03 -0700147 else
148 status = gser_bind_config(c, i);
David Brownell9079e912008-05-07 16:00:36 -0700149 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700150 return status;
David Brownell9079e912008-05-07 16:00:36 -0700151}
152
David Brownell7bb5ea52008-06-19 18:19:03 -0700153static struct usb_configuration serial_config_driver = {
154 /* .label = f(use_acm) */
David Brownell7bb5ea52008-06-19 18:19:03 -0700155 /* .bConfigurationValue = f(use_acm) */
156 /* .iConfiguration = DYNAMIC */
157 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell9079e912008-05-07 16:00:36 -0700158};
159
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200160static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
David Brownell7bb5ea52008-06-19 18:19:03 -0700162 int gcnum;
163 struct usb_gadget *gadget = cdev->gadget;
164 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
David Brownell7bb5ea52008-06-19 18:19:03 -0700166 status = gserial_setup(cdev->gadget, n_ports);
167 if (status < 0)
168 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
David Brownell7bb5ea52008-06-19 18:19:03 -0700170 /* Allocate string descriptor numbers ... note that string
171 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700172 */
173
David Brownell7bb5ea52008-06-19 18:19:03 -0700174 /* device description: manufacturer, product */
175 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
176 init_utsname()->sysname, init_utsname()->release,
177 gadget->name);
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200178 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell7bb5ea52008-06-19 18:19:03 -0700179 if (status < 0)
180 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200181 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
182 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200183 status = strings_dev[STRING_DESCRIPTION_IDX].id;
David Brownell7bb5ea52008-06-19 18:19:03 -0700184 serial_config_driver.iConfiguration = status;
185
186 /* set up other descriptors */
187 gcnum = usb_gadget_controller_number(gadget);
188 if (gcnum >= 0)
189 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
190 else {
191 /* this is so simple (for now, no altsettings) that it
192 * SHOULD NOT have problems with bulk-capable hardware.
193 * so warn about unrcognized controllers -- don't panic.
194 *
195 * things like configuration and altsetting numbering
196 * can need hardware-specific attention though.
197 */
198 pr_warning("gs_bind: controller '%s' not recognized\n",
199 gadget->name);
200 device_desc.bcdDevice =
Harvey Harrison551509d2009-02-11 14:11:36 -0800201 cpu_to_le16(GS_VERSION_NUM | 0x0099);
David Brownell7bb5ea52008-06-19 18:19:03 -0700202 }
203
204 if (gadget_is_otg(cdev->gadget)) {
205 serial_config_driver.descriptors = otg_desc;
206 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
207 }
208
209 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200210 status = usb_add_config(cdev, &serial_config_driver,
211 serial_bind_config);
David Brownell7bb5ea52008-06-19 18:19:03 -0700212 if (status < 0)
213 goto fail;
214
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200215 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell7bb5ea52008-06-19 18:19:03 -0700216 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700219
220fail:
221 gserial_cleanup();
222 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200225static __refdata struct usb_composite_driver gserial_driver = {
David Brownell7bb5ea52008-06-19 18:19:03 -0700226 .name = "g_serial",
227 .dev = &device_desc,
228 .strings = dev_strings,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100229 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200230 .bind = gs_bind,
David Brownell7bb5ea52008-06-19 18:19:03 -0700231};
232
233static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
David Brownell7bb5ea52008-06-19 18:19:03 -0700235 /* We *could* export two configs; that'd be much cleaner...
236 * but neither of these product IDs was defined that way.
237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700239 serial_config_driver.label = "CDC ACM config";
240 serial_config_driver.bConfigurationValue = 2;
241 device_desc.bDeviceClass = USB_CLASS_COMM;
242 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800243 cpu_to_le16(GS_CDC_PRODUCT_ID);
Felipe Balbi30867752008-08-18 17:39:30 -0700244 } else if (use_obex) {
245 serial_config_driver.label = "CDC OBEX config";
246 serial_config_driver.bConfigurationValue = 3;
247 device_desc.bDeviceClass = USB_CLASS_COMM;
248 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800249 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700251 serial_config_driver.label = "Generic Serial config";
252 serial_config_driver.bConfigurationValue = 1;
253 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
254 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800255 cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700257 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200259 return usb_composite_probe(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
David Brownell7bb5ea52008-06-19 18:19:03 -0700261module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Brownell7bb5ea52008-06-19 18:19:03 -0700263static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
David Brownell7bb5ea52008-06-19 18:19:03 -0700265 usb_composite_unregister(&gserial_driver);
266 gserial_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
David Brownell7bb5ea52008-06-19 18:19:03 -0700268module_exit(cleanup);