blob: 9d215c4c22756dc6dde648f1b3761f7257d75b2a [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 */
Felipe Balbi30867752008-08-18 17:39:30 -070039#include "f_obex.c"
David Brownell4e9ba512008-08-18 17:41:02 -070040
41/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020042USB_GADGET_COMPOSITE_OPTIONS();
David Brownell4e9ba512008-08-18 17:41:02 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* Thanks to NetChip Technologies for donating this product ID.
David Brownell7bb5ea52008-06-19 18:19:03 -070045*
46* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
47* Instead: allocate your own, using normal USB-IF procedures.
48*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define GS_VENDOR_ID 0x0525 /* NetChip */
50#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
51#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
Felipe Balbi30867752008-08-18 17:39:30 -070052#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David Brownell7bb5ea52008-06-19 18:19:03 -070054/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020056#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
David Brownella7707ad2008-06-19 17:52:07 -070057
David Brownell7bb5ea52008-06-19 18:19:03 -070058static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020059 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020060 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
61 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell7bb5ea52008-06-19 18:19:03 -070062 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { } /* end of list */
64};
65
David Brownell7bb5ea52008-06-19 18:19:03 -070066static struct usb_gadget_strings stringtab_dev = {
67 .language = 0x0409, /* en-us */
68 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
David Brownell7bb5ea52008-06-19 18:19:03 -070071static struct usb_gadget_strings *dev_strings[] = {
72 &stringtab_dev,
73 NULL,
74};
75
76static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .bLength = USB_DT_DEVICE_SIZE,
78 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -080079 .bcdUSB = cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070080 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 .bDeviceSubClass = 0,
82 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070083 /* .bMaxPacketSize0 = f(hardware) */
Harvey Harrison551509d2009-02-11 14:11:36 -080084 .idVendor = cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070085 /* .idProduct = f(use_acm) */
Sebastian Andrzej Siewior5c4d46e2012-09-10 19:06:44 +020086 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
David Brownell7bb5ea52008-06-19 18:19:03 -070087 /* .iManufacturer = DYNAMIC */
88 /* .iProduct = DYNAMIC */
89 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
David Brownell7bb5ea52008-06-19 18:19:03 -070092static struct usb_otg_descriptor otg_descriptor = {
93 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -070095
96 /* REVISIT SRP-only hardware is possible, although
97 * it would not be called "OTG" ...
98 */
99 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
David Brownell7bb5ea52008-06-19 18:19:03 -0700102static const struct usb_descriptor_header *otg_desc[] = {
103 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 NULL,
105};
106
David Brownell9079e912008-05-07 16:00:36 -0700107/*-------------------------------------------------------------------------*/
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700110MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700112MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113MODULE_LICENSE("GPL");
114
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030115static bool use_acm = true;
David Brownell7bb5ea52008-06-19 18:19:03 -0700116module_param(use_acm, bool, 0);
117MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
118
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030119static bool use_obex = false;
Felipe Balbi30867752008-08-18 17:39:30 -0700120module_param(use_obex, bool, 0);
121MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
122
David Brownell7bb5ea52008-06-19 18:19:03 -0700123static unsigned n_ports = 1;
124module_param(n_ports, uint, 0);
125MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
David Brownell9079e912008-05-07 16:00:36 -0700127/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100128static unsigned char tty_lines[MAX_U_SERIAL_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Sebastian Andrzej Siewior48177cd2012-12-23 21:10:03 +0100130static int __init serial_bind_obex_config(struct usb_configuration *c)
131{
132 unsigned i;
133 int status = 0;
134
135 for (i = 0; i < n_ports && status == 0; i++)
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100136 status = obex_bind_config(c, tty_lines[i]);
Sebastian Andrzej Siewior48177cd2012-12-23 21:10:03 +0100137 return status;
138}
139
David Brownell7bb5ea52008-06-19 18:19:03 -0700140static struct usb_configuration serial_config_driver = {
141 /* .label = f(use_acm) */
David Brownell7bb5ea52008-06-19 18:19:03 -0700142 /* .bConfigurationValue = f(use_acm) */
143 /* .iConfiguration = DYNAMIC */
144 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell9079e912008-05-07 16:00:36 -0700145};
146
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100147static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
148static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
149
150static int serial_register_ports(struct usb_composite_dev *cdev,
151 struct usb_configuration *c, const char *f_name)
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100152{
153 int i;
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100154 int ret;
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100155
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100156 ret = usb_add_config_only(cdev, c);
157 if (ret)
158 goto out;
159
160 for (i = 0; i < n_ports; i++) {
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100161
162 fi_serial[i] = usb_get_function_instance(f_name);
163 if (IS_ERR(fi_serial[i])) {
164 ret = PTR_ERR(fi_serial[i]);
165 goto fail;
166 }
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100167
168 f_serial[i] = usb_get_function(fi_serial[i]);
169 if (IS_ERR(f_serial[i])) {
170 ret = PTR_ERR(f_serial[i]);
171 goto err_get_func;
172 }
173
174 ret = usb_add_function(c, f_serial[i]);
175 if (ret)
176 goto err_add_func;
177 }
178
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100179 return 0;
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100180
181err_add_func:
182 usb_put_function(f_serial[i]);
183err_get_func:
184 usb_put_function_instance(fi_serial[i]);
185
186fail:
187 i--;
188 while (i >= 0) {
189 usb_remove_function(c, f_serial[i]);
190 usb_put_function(f_serial[i]);
191 usb_put_function_instance(fi_serial[i]);
192 i--;
193 }
194out:
195 return ret;
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100196}
197
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200198static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Brownell7bb5ea52008-06-19 18:19:03 -0700200 int status;
Sebastian Andrzej Siewiorc4ed4ac2012-12-23 21:10:18 +0100201 int cur_line = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Andrzej Pietrasiewicz70cc3c02013-03-14 16:02:12 +0100203 if (use_obex) {
Sebastian Andrzej Siewiorc4ed4ac2012-12-23 21:10:18 +0100204 for (cur_line = 0; cur_line < n_ports; cur_line++) {
205 status = gserial_alloc_line(&tty_lines[cur_line]);
206 if (status)
207 goto fail;
208 }
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
David Brownell7bb5ea52008-06-19 18:19:03 -0700211 /* Allocate string descriptor numbers ... note that string
212 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700213 */
214
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200215 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell7bb5ea52008-06-19 18:19:03 -0700216 if (status < 0)
217 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200218 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
219 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200220 status = strings_dev[STRING_DESCRIPTION_IDX].id;
David Brownell7bb5ea52008-06-19 18:19:03 -0700221 serial_config_driver.iConfiguration = status;
222
David Brownell7bb5ea52008-06-19 18:19:03 -0700223 if (gadget_is_otg(cdev->gadget)) {
224 serial_config_driver.descriptors = otg_desc;
225 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
226 }
227
228 /* register our configuration */
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100229 if (use_acm) {
230 status = serial_register_ports(cdev, &serial_config_driver,
231 "acm");
232 usb_ep_autoconfig_reset(cdev->gadget);
233 } else if (use_obex)
Sebastian Andrzej Siewior48177cd2012-12-23 21:10:03 +0100234 status = usb_add_config(cdev, &serial_config_driver,
235 serial_bind_obex_config);
Andrzej Pietrasiewicz70cc3c02013-03-14 16:02:12 +0100236 else {
237 status = serial_register_ports(cdev, &serial_config_driver,
238 "gser");
239 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700240 if (status < 0)
241 goto fail;
242
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200243 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell7bb5ea52008-06-19 18:19:03 -0700244 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700247
248fail:
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100249 cur_line--;
Andrzej Pietrasiewicz70cc3c02013-03-14 16:02:12 +0100250 while (cur_line >= 0 && use_obex)
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100251 gserial_free_line(tty_lines[cur_line--]);
David Brownell7bb5ea52008-06-19 18:19:03 -0700252 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100255static int gs_unbind(struct usb_composite_dev *cdev)
256{
257 int i;
258
259 for (i = 0; i < n_ports; i++) {
260 usb_put_function(f_serial[i]);
261 usb_put_function_instance(fi_serial[i]);
Andrzej Pietrasiewicz70cc3c02013-03-14 16:02:12 +0100262 if (use_obex)
Sebastian Andrzej Siewiorc4ed4ac2012-12-23 21:10:18 +0100263 gserial_free_line(tty_lines[i]);
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100264 }
265 return 0;
266}
267
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200268static __refdata struct usb_composite_driver gserial_driver = {
David Brownell7bb5ea52008-06-19 18:19:03 -0700269 .name = "g_serial",
270 .dev = &device_desc,
271 .strings = dev_strings,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100272 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200273 .bind = gs_bind,
Sebastian Andrzej Siewior19b10a82012-12-23 21:10:06 +0100274 .unbind = gs_unbind,
David Brownell7bb5ea52008-06-19 18:19:03 -0700275};
276
277static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
David Brownell7bb5ea52008-06-19 18:19:03 -0700279 /* We *could* export two configs; that'd be much cleaner...
280 * but neither of these product IDs was defined that way.
281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700283 serial_config_driver.label = "CDC ACM config";
284 serial_config_driver.bConfigurationValue = 2;
285 device_desc.bDeviceClass = USB_CLASS_COMM;
286 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800287 cpu_to_le16(GS_CDC_PRODUCT_ID);
Felipe Balbi30867752008-08-18 17:39:30 -0700288 } else if (use_obex) {
289 serial_config_driver.label = "CDC OBEX config";
290 serial_config_driver.bConfigurationValue = 3;
291 device_desc.bDeviceClass = USB_CLASS_COMM;
292 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800293 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700295 serial_config_driver.label = "Generic Serial config";
296 serial_config_driver.bConfigurationValue = 1;
297 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
298 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800299 cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700301 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200303 return usb_composite_probe(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
David Brownell7bb5ea52008-06-19 18:19:03 -0700305module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Brownell7bb5ea52008-06-19 18:19:03 -0700307static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
David Brownell7bb5ea52008-06-19 18:19:03 -0700309 usb_composite_unregister(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
David Brownell7bb5ea52008-06-19 18:19:03 -0700311module_exit(cleanup);