blob: 27b5ce939bdb7b131a0d9c19e53d87a65d85d5aa [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
David Brownell7bb5ea52008-06-19 18:19:03 -070062#define STRING_MANUFACTURER_IDX 0
63#define STRING_PRODUCT_IDX 1
64#define STRING_DESCRIPTION_IDX 2
David Brownella7707ad2008-06-19 17:52:07 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static char manufacturer[50];
David Brownell7bb5ea52008-06-19 18:19:03 -070067
68static struct usb_string strings_dev[] = {
69 [STRING_MANUFACTURER_IDX].s = manufacturer,
70 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
71 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 { } /* end of list */
73};
74
David Brownell7bb5ea52008-06-19 18:19:03 -070075static struct usb_gadget_strings stringtab_dev = {
76 .language = 0x0409, /* en-us */
77 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
David Brownell7bb5ea52008-06-19 18:19:03 -070080static struct usb_gadget_strings *dev_strings[] = {
81 &stringtab_dev,
82 NULL,
83};
84
85static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .bLength = USB_DT_DEVICE_SIZE,
87 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -080088 .bcdUSB = cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070089 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .bDeviceSubClass = 0,
91 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070092 /* .bMaxPacketSize0 = f(hardware) */
Harvey Harrison551509d2009-02-11 14:11:36 -080093 .idVendor = cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070094 /* .idProduct = f(use_acm) */
95 /* .bcdDevice = f(hardware) */
96 /* .iManufacturer = DYNAMIC */
97 /* .iProduct = DYNAMIC */
98 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
David Brownell7bb5ea52008-06-19 18:19:03 -0700101static struct usb_otg_descriptor otg_descriptor = {
102 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -0700104
105 /* REVISIT SRP-only hardware is possible, although
106 * it would not be called "OTG" ...
107 */
108 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
David Brownell7bb5ea52008-06-19 18:19:03 -0700111static const struct usb_descriptor_header *otg_desc[] = {
112 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 NULL,
114};
115
David Brownell9079e912008-05-07 16:00:36 -0700116/*-------------------------------------------------------------------------*/
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700119MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700121MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122MODULE_LICENSE("GPL");
123
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030124static bool use_acm = true;
David Brownell7bb5ea52008-06-19 18:19:03 -0700125module_param(use_acm, bool, 0);
126MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
127
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030128static bool use_obex = false;
Felipe Balbi30867752008-08-18 17:39:30 -0700129module_param(use_obex, bool, 0);
130MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
131
David Brownell7bb5ea52008-06-19 18:19:03 -0700132static unsigned n_ports = 1;
133module_param(n_ports, uint, 0);
134MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
David Brownell9079e912008-05-07 16:00:36 -0700136/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200138static int __init serial_bind_config(struct usb_configuration *c)
David Brownell9079e912008-05-07 16:00:36 -0700139{
David Brownell7bb5ea52008-06-19 18:19:03 -0700140 unsigned i;
141 int status = 0;
David Brownell9079e912008-05-07 16:00:36 -0700142
David Brownell7bb5ea52008-06-19 18:19:03 -0700143 for (i = 0; i < n_ports && status == 0; i++) {
144 if (use_acm)
145 status = acm_bind_config(c, i);
Felipe Balbi30867752008-08-18 17:39:30 -0700146 else if (use_obex)
147 status = obex_bind_config(c, i);
David Brownell7bb5ea52008-06-19 18:19:03 -0700148 else
149 status = gser_bind_config(c, i);
David Brownell9079e912008-05-07 16:00:36 -0700150 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700151 return status;
David Brownell9079e912008-05-07 16:00:36 -0700152}
153
David Brownell7bb5ea52008-06-19 18:19:03 -0700154static struct usb_configuration serial_config_driver = {
155 /* .label = f(use_acm) */
David Brownell7bb5ea52008-06-19 18:19:03 -0700156 /* .bConfigurationValue = f(use_acm) */
157 /* .iConfiguration = DYNAMIC */
158 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell9079e912008-05-07 16:00:36 -0700159};
160
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200161static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
David Brownell7bb5ea52008-06-19 18:19:03 -0700163 int gcnum;
164 struct usb_gadget *gadget = cdev->gadget;
165 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
David Brownell7bb5ea52008-06-19 18:19:03 -0700167 status = gserial_setup(cdev->gadget, n_ports);
168 if (status < 0)
169 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
David Brownell7bb5ea52008-06-19 18:19:03 -0700171 /* Allocate string descriptor numbers ... note that string
172 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700173 */
174
David Brownell7bb5ea52008-06-19 18:19:03 -0700175 /* device description: manufacturer, product */
176 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
177 init_utsname()->sysname, init_utsname()->release,
178 gadget->name);
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200179 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell7bb5ea52008-06-19 18:19:03 -0700180 if (status < 0)
181 goto fail;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200182 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
183 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
184 status = strings_dev[STRING_DESCRIPTION_IDX].id;
David Brownell7bb5ea52008-06-19 18:19:03 -0700185 serial_config_driver.iConfiguration = status;
186
187 /* set up other descriptors */
188 gcnum = usb_gadget_controller_number(gadget);
189 if (gcnum >= 0)
190 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
191 else {
192 /* this is so simple (for now, no altsettings) that it
193 * SHOULD NOT have problems with bulk-capable hardware.
194 * so warn about unrcognized controllers -- don't panic.
195 *
196 * things like configuration and altsetting numbering
197 * can need hardware-specific attention though.
198 */
199 pr_warning("gs_bind: controller '%s' not recognized\n",
200 gadget->name);
201 device_desc.bcdDevice =
Harvey Harrison551509d2009-02-11 14:11:36 -0800202 cpu_to_le16(GS_VERSION_NUM | 0x0099);
David Brownell7bb5ea52008-06-19 18:19:03 -0700203 }
204
205 if (gadget_is_otg(cdev->gadget)) {
206 serial_config_driver.descriptors = otg_desc;
207 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
208 }
209
210 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200211 status = usb_add_config(cdev, &serial_config_driver,
212 serial_bind_config);
David Brownell7bb5ea52008-06-19 18:19:03 -0700213 if (status < 0)
214 goto fail;
215
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200216 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell7bb5ea52008-06-19 18:19:03 -0700217 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700220
221fail:
222 gserial_cleanup();
223 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200226static __refdata struct usb_composite_driver gserial_driver = {
David Brownell7bb5ea52008-06-19 18:19:03 -0700227 .name = "g_serial",
228 .dev = &device_desc,
229 .strings = dev_strings,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100230 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200231 .bind = gs_bind,
David Brownell7bb5ea52008-06-19 18:19:03 -0700232};
233
234static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
David Brownell7bb5ea52008-06-19 18:19:03 -0700236 /* We *could* export two configs; that'd be much cleaner...
237 * but neither of these product IDs was defined that way.
238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700240 serial_config_driver.label = "CDC ACM config";
241 serial_config_driver.bConfigurationValue = 2;
242 device_desc.bDeviceClass = USB_CLASS_COMM;
243 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800244 cpu_to_le16(GS_CDC_PRODUCT_ID);
Felipe Balbi30867752008-08-18 17:39:30 -0700245 } else if (use_obex) {
246 serial_config_driver.label = "CDC OBEX config";
247 serial_config_driver.bConfigurationValue = 3;
248 device_desc.bDeviceClass = USB_CLASS_COMM;
249 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800250 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700252 serial_config_driver.label = "Generic Serial config";
253 serial_config_driver.bConfigurationValue = 1;
254 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
255 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800256 cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700258 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200260 return usb_composite_probe(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
David Brownell7bb5ea52008-06-19 18:19:03 -0700262module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Brownell7bb5ea52008-06-19 18:19:03 -0700264static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
David Brownell7bb5ea52008-06-19 18:19:03 -0700266 usb_composite_unregister(&gserial_driver);
267 gserial_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
David Brownell7bb5ea52008-06-19 18:19:03 -0700269module_exit(cleanup);