blob: 1f5f978d35d5318be56d1e069750d42e18b6b3c5 [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>
Andrzej Pietrasiewiczd1412792013-03-21 09:22:30 +010015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020032USB_GADGET_COMPOSITE_OPTIONS();
David Brownell4e9ba512008-08-18 17:41:02 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Thanks to NetChip Technologies for donating this product ID.
David Brownell7bb5ea52008-06-19 18:19:03 -070035*
36* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
37* Instead: allocate your own, using normal USB-IF procedures.
38*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define GS_VENDOR_ID 0x0525 /* NetChip */
40#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
41#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
Felipe Balbi30867752008-08-18 17:39:30 -070042#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Brownell7bb5ea52008-06-19 18:19:03 -070044/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020046#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
David Brownella7707ad2008-06-19 17:52:07 -070047
David Brownell7bb5ea52008-06-19 18:19:03 -070048static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020049 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020050 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
51 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell7bb5ea52008-06-19 18:19:03 -070052 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 { } /* end of list */
54};
55
David Brownell7bb5ea52008-06-19 18:19:03 -070056static struct usb_gadget_strings stringtab_dev = {
57 .language = 0x0409, /* en-us */
58 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
David Brownell7bb5ea52008-06-19 18:19:03 -070061static struct usb_gadget_strings *dev_strings[] = {
62 &stringtab_dev,
63 NULL,
64};
65
66static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .bLength = USB_DT_DEVICE_SIZE,
68 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -080069 .bcdUSB = cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070070 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .bDeviceSubClass = 0,
72 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070073 /* .bMaxPacketSize0 = f(hardware) */
Harvey Harrison551509d2009-02-11 14:11:36 -080074 .idVendor = cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070075 /* .idProduct = f(use_acm) */
Sebastian Andrzej Siewior5c4d46e2012-09-10 19:06:44 +020076 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
David Brownell7bb5ea52008-06-19 18:19:03 -070077 /* .iManufacturer = DYNAMIC */
78 /* .iProduct = DYNAMIC */
79 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
David Brownell7bb5ea52008-06-19 18:19:03 -070082static struct usb_otg_descriptor otg_descriptor = {
83 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -070085
86 /* REVISIT SRP-only hardware is possible, although
87 * it would not be called "OTG" ...
88 */
89 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
David Brownell7bb5ea52008-06-19 18:19:03 -070092static const struct usb_descriptor_header *otg_desc[] = {
93 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 NULL,
95};
96
David Brownell9079e912008-05-07 16:00:36 -070097/*-------------------------------------------------------------------------*/
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700100MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700102MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103MODULE_LICENSE("GPL");
104
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030105static bool use_acm = true;
David Brownell7bb5ea52008-06-19 18:19:03 -0700106module_param(use_acm, bool, 0);
107MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
108
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030109static bool use_obex = false;
Felipe Balbi30867752008-08-18 17:39:30 -0700110module_param(use_obex, bool, 0);
111MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
112
David Brownell7bb5ea52008-06-19 18:19:03 -0700113static unsigned n_ports = 1;
114module_param(n_ports, uint, 0);
115MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
David Brownell9079e912008-05-07 16:00:36 -0700117/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior48177cd2012-12-23 21:10:03 +0100118
David Brownell7bb5ea52008-06-19 18:19:03 -0700119static struct usb_configuration serial_config_driver = {
120 /* .label = f(use_acm) */
David Brownell7bb5ea52008-06-19 18:19:03 -0700121 /* .bConfigurationValue = f(use_acm) */
122 /* .iConfiguration = DYNAMIC */
123 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell9079e912008-05-07 16:00:36 -0700124};
125
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100126static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
127static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
128
129static int serial_register_ports(struct usb_composite_dev *cdev,
130 struct usb_configuration *c, const char *f_name)
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100131{
132 int i;
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100133 int ret;
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100134
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100135 ret = usb_add_config_only(cdev, c);
136 if (ret)
137 goto out;
138
139 for (i = 0; i < n_ports; i++) {
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100140
141 fi_serial[i] = usb_get_function_instance(f_name);
142 if (IS_ERR(fi_serial[i])) {
143 ret = PTR_ERR(fi_serial[i]);
144 goto fail;
145 }
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100146
147 f_serial[i] = usb_get_function(fi_serial[i]);
148 if (IS_ERR(f_serial[i])) {
149 ret = PTR_ERR(f_serial[i]);
150 goto err_get_func;
151 }
152
153 ret = usb_add_function(c, f_serial[i]);
154 if (ret)
155 goto err_add_func;
156 }
157
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100158 return 0;
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100159
160err_add_func:
161 usb_put_function(f_serial[i]);
162err_get_func:
163 usb_put_function_instance(fi_serial[i]);
164
165fail:
166 i--;
167 while (i >= 0) {
168 usb_remove_function(c, f_serial[i]);
169 usb_put_function(f_serial[i]);
170 usb_put_function_instance(fi_serial[i]);
171 i--;
172 }
173out:
174 return ret;
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100175}
176
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200177static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
David Brownell7bb5ea52008-06-19 18:19:03 -0700179 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
David Brownell7bb5ea52008-06-19 18:19:03 -0700181 /* Allocate string descriptor numbers ... note that string
182 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700183 */
184
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200185 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell7bb5ea52008-06-19 18:19:03 -0700186 if (status < 0)
187 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200188 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
189 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200190 status = strings_dev[STRING_DESCRIPTION_IDX].id;
David Brownell7bb5ea52008-06-19 18:19:03 -0700191 serial_config_driver.iConfiguration = status;
192
David Brownell7bb5ea52008-06-19 18:19:03 -0700193 if (gadget_is_otg(cdev->gadget)) {
194 serial_config_driver.descriptors = otg_desc;
195 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
196 }
197
198 /* register our configuration */
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100199 if (use_acm) {
200 status = serial_register_ports(cdev, &serial_config_driver,
201 "acm");
202 usb_ep_autoconfig_reset(cdev->gadget);
203 } else if (use_obex)
Andrzej Pietrasiewiczd1412792013-03-21 09:22:30 +0100204 status = serial_register_ports(cdev, &serial_config_driver,
205 "obex");
Andrzej Pietrasiewicz70cc3c02013-03-14 16:02:12 +0100206 else {
207 status = serial_register_ports(cdev, &serial_config_driver,
208 "gser");
209 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700210 if (status < 0)
211 goto fail;
212
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200213 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell7bb5ea52008-06-19 18:19:03 -0700214 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700217
218fail:
David Brownell7bb5ea52008-06-19 18:19:03 -0700219 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100222static int gs_unbind(struct usb_composite_dev *cdev)
223{
224 int i;
225
226 for (i = 0; i < n_ports; i++) {
227 usb_put_function(f_serial[i]);
228 usb_put_function_instance(fi_serial[i]);
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100229 }
230 return 0;
231}
232
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200233static __refdata struct usb_composite_driver gserial_driver = {
David Brownell7bb5ea52008-06-19 18:19:03 -0700234 .name = "g_serial",
235 .dev = &device_desc,
236 .strings = dev_strings,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100237 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200238 .bind = gs_bind,
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100239 .unbind = gs_unbind,
David Brownell7bb5ea52008-06-19 18:19:03 -0700240};
241
242static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
David Brownell7bb5ea52008-06-19 18:19:03 -0700244 /* We *could* export two configs; that'd be much cleaner...
245 * but neither of these product IDs was defined that way.
246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700248 serial_config_driver.label = "CDC ACM config";
249 serial_config_driver.bConfigurationValue = 2;
250 device_desc.bDeviceClass = USB_CLASS_COMM;
251 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800252 cpu_to_le16(GS_CDC_PRODUCT_ID);
Felipe Balbi30867752008-08-18 17:39:30 -0700253 } else if (use_obex) {
254 serial_config_driver.label = "CDC OBEX config";
255 serial_config_driver.bConfigurationValue = 3;
256 device_desc.bDeviceClass = USB_CLASS_COMM;
257 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800258 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700260 serial_config_driver.label = "Generic Serial config";
261 serial_config_driver.bConfigurationValue = 1;
262 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
263 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800264 cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700266 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200268 return usb_composite_probe(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
David Brownell7bb5ea52008-06-19 18:19:03 -0700270module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David Brownell7bb5ea52008-06-19 18:19:03 -0700272static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
David Brownell7bb5ea52008-06-19 18:19:03 -0700274 usb_composite_unregister(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
David Brownell7bb5ea52008-06-19 18:19:03 -0700276module_exit(cleanup);