blob: bf12d55cd07b4492ee0c299093c73d15301f6ff7 [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/device.h>
15#include <linux/tty.h>
16#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
David Brownella7707ad2008-06-19 17:52:07 -070018#include "u_serial.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "gadget_chips.h"
20
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/* Defines */
23
David Brownell7bb5ea52008-06-19 18:19:03 -070024#define GS_VERSION_STR "v2.4"
25#define GS_VERSION_NUM 0x2400
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define GS_LONG_NAME "Gadget Serial"
David Brownella7707ad2008-06-19 17:52:07 -070028#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
29
David Brownell7bb5ea52008-06-19 18:19:03 -070030/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
David Brownell4e9ba512008-08-18 17:41:02 -070032/*
33 * Kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38 */
39#include "composite.c"
David Brownell4e9ba512008-08-18 17:41:02 -070040
41#include "f_acm.c"
Felipe Balbi30867752008-08-18 17:39:30 -070042#include "f_obex.c"
David Brownell4e9ba512008-08-18 17:41:02 -070043#include "f_serial.c"
44#include "u_serial.c"
45
46/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020047USB_GADGET_COMPOSITE_OPTIONS();
David Brownell4e9ba512008-08-18 17:41:02 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Thanks to NetChip Technologies for donating this product ID.
David Brownell7bb5ea52008-06-19 18:19:03 -070050*
51* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
52* Instead: allocate your own, using normal USB-IF procedures.
53*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define GS_VENDOR_ID 0x0525 /* NetChip */
55#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
56#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
Felipe Balbi30867752008-08-18 17:39:30 -070057#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Brownell7bb5ea52008-06-19 18:19:03 -070059/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020061#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
David Brownella7707ad2008-06-19 17:52:07 -070062
David Brownell7bb5ea52008-06-19 18:19:03 -070063static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020064 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020065 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
66 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell7bb5ea52008-06-19 18:19:03 -070067 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 { } /* end of list */
69};
70
David Brownell7bb5ea52008-06-19 18:19:03 -070071static struct usb_gadget_strings stringtab_dev = {
72 .language = 0x0409, /* en-us */
73 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
David Brownell7bb5ea52008-06-19 18:19:03 -070076static struct usb_gadget_strings *dev_strings[] = {
77 &stringtab_dev,
78 NULL,
79};
80
81static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .bLength = USB_DT_DEVICE_SIZE,
83 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -080084 .bcdUSB = cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070085 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .bDeviceSubClass = 0,
87 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070088 /* .bMaxPacketSize0 = f(hardware) */
Harvey Harrison551509d2009-02-11 14:11:36 -080089 .idVendor = cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070090 /* .idProduct = f(use_acm) */
91 /* .bcdDevice = f(hardware) */
92 /* .iManufacturer = DYNAMIC */
93 /* .iProduct = DYNAMIC */
94 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
David Brownell7bb5ea52008-06-19 18:19:03 -070097static struct usb_otg_descriptor otg_descriptor = {
98 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -0700100
101 /* REVISIT SRP-only hardware is possible, although
102 * it would not be called "OTG" ...
103 */
104 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
David Brownell7bb5ea52008-06-19 18:19:03 -0700107static const struct usb_descriptor_header *otg_desc[] = {
108 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 NULL,
110};
111
David Brownell9079e912008-05-07 16:00:36 -0700112/*-------------------------------------------------------------------------*/
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700115MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700117MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118MODULE_LICENSE("GPL");
119
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030120static bool use_acm = true;
David Brownell7bb5ea52008-06-19 18:19:03 -0700121module_param(use_acm, bool, 0);
122MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
123
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030124static bool use_obex = false;
Felipe Balbi30867752008-08-18 17:39:30 -0700125module_param(use_obex, bool, 0);
126MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
127
David Brownell7bb5ea52008-06-19 18:19:03 -0700128static unsigned n_ports = 1;
129module_param(n_ports, uint, 0);
130MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Brownell9079e912008-05-07 16:00:36 -0700132/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200134static int __init serial_bind_config(struct usb_configuration *c)
David Brownell9079e912008-05-07 16:00:36 -0700135{
David Brownell7bb5ea52008-06-19 18:19:03 -0700136 unsigned i;
137 int status = 0;
David Brownell9079e912008-05-07 16:00:36 -0700138
David Brownell7bb5ea52008-06-19 18:19:03 -0700139 for (i = 0; i < n_ports && status == 0; i++) {
140 if (use_acm)
141 status = acm_bind_config(c, i);
Felipe Balbi30867752008-08-18 17:39:30 -0700142 else if (use_obex)
143 status = obex_bind_config(c, i);
David Brownell7bb5ea52008-06-19 18:19:03 -0700144 else
145 status = gser_bind_config(c, i);
David Brownell9079e912008-05-07 16:00:36 -0700146 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700147 return status;
David Brownell9079e912008-05-07 16:00:36 -0700148}
149
David Brownell7bb5ea52008-06-19 18:19:03 -0700150static struct usb_configuration serial_config_driver = {
151 /* .label = f(use_acm) */
David Brownell7bb5ea52008-06-19 18:19:03 -0700152 /* .bConfigurationValue = f(use_acm) */
153 /* .iConfiguration = DYNAMIC */
154 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell9079e912008-05-07 16:00:36 -0700155};
156
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200157static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
David Brownell7bb5ea52008-06-19 18:19:03 -0700159 int gcnum;
160 struct usb_gadget *gadget = cdev->gadget;
161 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David Brownell7bb5ea52008-06-19 18:19:03 -0700163 status = gserial_setup(cdev->gadget, n_ports);
164 if (status < 0)
165 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
David Brownell7bb5ea52008-06-19 18:19:03 -0700167 /* Allocate string descriptor numbers ... note that string
168 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700169 */
170
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200171 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell7bb5ea52008-06-19 18:19:03 -0700172 if (status < 0)
173 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200174 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
175 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200176 status = strings_dev[STRING_DESCRIPTION_IDX].id;
David Brownell7bb5ea52008-06-19 18:19:03 -0700177 serial_config_driver.iConfiguration = status;
178
179 /* set up other descriptors */
180 gcnum = usb_gadget_controller_number(gadget);
181 if (gcnum >= 0)
182 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
183 else {
184 /* this is so simple (for now, no altsettings) that it
185 * SHOULD NOT have problems with bulk-capable hardware.
186 * so warn about unrcognized controllers -- don't panic.
187 *
188 * things like configuration and altsetting numbering
189 * can need hardware-specific attention though.
190 */
191 pr_warning("gs_bind: controller '%s' not recognized\n",
192 gadget->name);
193 device_desc.bcdDevice =
Harvey Harrison551509d2009-02-11 14:11:36 -0800194 cpu_to_le16(GS_VERSION_NUM | 0x0099);
David Brownell7bb5ea52008-06-19 18:19:03 -0700195 }
196
197 if (gadget_is_otg(cdev->gadget)) {
198 serial_config_driver.descriptors = otg_desc;
199 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
200 }
201
202 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200203 status = usb_add_config(cdev, &serial_config_driver,
204 serial_bind_config);
David Brownell7bb5ea52008-06-19 18:19:03 -0700205 if (status < 0)
206 goto fail;
207
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200208 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell7bb5ea52008-06-19 18:19:03 -0700209 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700212
213fail:
214 gserial_cleanup();
215 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200218static __refdata struct usb_composite_driver gserial_driver = {
David Brownell7bb5ea52008-06-19 18:19:03 -0700219 .name = "g_serial",
220 .dev = &device_desc,
221 .strings = dev_strings,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100222 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200223 .bind = gs_bind,
David Brownell7bb5ea52008-06-19 18:19:03 -0700224};
225
226static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
David Brownell7bb5ea52008-06-19 18:19:03 -0700228 /* We *could* export two configs; that'd be much cleaner...
229 * but neither of these product IDs was defined that way.
230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700232 serial_config_driver.label = "CDC ACM config";
233 serial_config_driver.bConfigurationValue = 2;
234 device_desc.bDeviceClass = USB_CLASS_COMM;
235 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800236 cpu_to_le16(GS_CDC_PRODUCT_ID);
Felipe Balbi30867752008-08-18 17:39:30 -0700237 } else if (use_obex) {
238 serial_config_driver.label = "CDC OBEX config";
239 serial_config_driver.bConfigurationValue = 3;
240 device_desc.bDeviceClass = USB_CLASS_COMM;
241 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800242 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700244 serial_config_driver.label = "Generic Serial config";
245 serial_config_driver.bConfigurationValue = 1;
246 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
247 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800248 cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700250 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200252 return usb_composite_probe(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
David Brownell7bb5ea52008-06-19 18:19:03 -0700254module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David Brownell7bb5ea52008-06-19 18:19:03 -0700256static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
David Brownell7bb5ea52008-06-19 18:19:03 -0700258 usb_composite_unregister(&gserial_driver);
259 gserial_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
David Brownell7bb5ea52008-06-19 18:19:03 -0700261module_exit(cleanup);