blob: 44752f531e85e7d75ccf1698a3dd0bb482dfcbfc [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 */
David Brownell4e9ba512008-08-18 17:41:02 -070039#include "f_acm.c"
Felipe Balbi30867752008-08-18 17:39:30 -070040#include "f_obex.c"
David Brownell4e9ba512008-08-18 17:41:02 -070041#include "f_serial.c"
42#include "u_serial.c"
43
44/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020045USB_GADGET_COMPOSITE_OPTIONS();
David Brownell4e9ba512008-08-18 17:41:02 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* Thanks to NetChip Technologies for donating this product ID.
David Brownell7bb5ea52008-06-19 18:19:03 -070048*
49* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50* Instead: allocate your own, using normal USB-IF procedures.
51*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define GS_VENDOR_ID 0x0525 /* NetChip */
53#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
54#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
Felipe Balbi30867752008-08-18 17:39:30 -070055#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David Brownell7bb5ea52008-06-19 18:19:03 -070057/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020059#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
David Brownella7707ad2008-06-19 17:52:07 -070060
David Brownell7bb5ea52008-06-19 18:19:03 -070061static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020062 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020063 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
64 [USB_GADGET_SERIAL_IDX].s = "",
David Brownell7bb5ea52008-06-19 18:19:03 -070065 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 { } /* end of list */
67};
68
David Brownell7bb5ea52008-06-19 18:19:03 -070069static struct usb_gadget_strings stringtab_dev = {
70 .language = 0x0409, /* en-us */
71 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
David Brownell7bb5ea52008-06-19 18:19:03 -070074static struct usb_gadget_strings *dev_strings[] = {
75 &stringtab_dev,
76 NULL,
77};
78
79static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .bLength = USB_DT_DEVICE_SIZE,
81 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -080082 .bcdUSB = cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070083 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .bDeviceSubClass = 0,
85 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070086 /* .bMaxPacketSize0 = f(hardware) */
Harvey Harrison551509d2009-02-11 14:11:36 -080087 .idVendor = cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070088 /* .idProduct = f(use_acm) */
Sebastian Andrzej Siewior5c4d46e2012-09-10 19:06:44 +020089 .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
David Brownell7bb5ea52008-06-19 18:19:03 -070090 /* .iManufacturer = DYNAMIC */
91 /* .iProduct = DYNAMIC */
92 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
David Brownell7bb5ea52008-06-19 18:19:03 -070095static struct usb_otg_descriptor otg_descriptor = {
96 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -070098
99 /* REVISIT SRP-only hardware is possible, although
100 * it would not be called "OTG" ...
101 */
102 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
David Brownell7bb5ea52008-06-19 18:19:03 -0700105static const struct usb_descriptor_header *otg_desc[] = {
106 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 NULL,
108};
109
David Brownell9079e912008-05-07 16:00:36 -0700110/*-------------------------------------------------------------------------*/
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700113MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700115MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116MODULE_LICENSE("GPL");
117
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030118static bool use_acm = true;
David Brownell7bb5ea52008-06-19 18:19:03 -0700119module_param(use_acm, bool, 0);
120MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
121
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030122static bool use_obex = false;
Felipe Balbi30867752008-08-18 17:39:30 -0700123module_param(use_obex, bool, 0);
124MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
125
David Brownell7bb5ea52008-06-19 18:19:03 -0700126static unsigned n_ports = 1;
127module_param(n_ports, uint, 0);
128MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David Brownell9079e912008-05-07 16:00:36 -0700130/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200132static int __init serial_bind_config(struct usb_configuration *c)
David Brownell9079e912008-05-07 16:00:36 -0700133{
David Brownell7bb5ea52008-06-19 18:19:03 -0700134 unsigned i;
135 int status = 0;
David Brownell9079e912008-05-07 16:00:36 -0700136
David Brownell7bb5ea52008-06-19 18:19:03 -0700137 for (i = 0; i < n_ports && status == 0; i++) {
138 if (use_acm)
139 status = acm_bind_config(c, i);
Felipe Balbi30867752008-08-18 17:39:30 -0700140 else if (use_obex)
141 status = obex_bind_config(c, i);
David Brownell7bb5ea52008-06-19 18:19:03 -0700142 else
143 status = gser_bind_config(c, i);
David Brownell9079e912008-05-07 16:00:36 -0700144 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700145 return status;
David Brownell9079e912008-05-07 16:00:36 -0700146}
147
David Brownell7bb5ea52008-06-19 18:19:03 -0700148static struct usb_configuration serial_config_driver = {
149 /* .label = f(use_acm) */
David Brownell7bb5ea52008-06-19 18:19:03 -0700150 /* .bConfigurationValue = f(use_acm) */
151 /* .iConfiguration = DYNAMIC */
152 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell9079e912008-05-07 16:00:36 -0700153};
154
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200155static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
David Brownell7bb5ea52008-06-19 18:19:03 -0700157 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
David Brownell7bb5ea52008-06-19 18:19:03 -0700159 status = gserial_setup(cdev->gadget, n_ports);
160 if (status < 0)
161 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David Brownell7bb5ea52008-06-19 18:19:03 -0700163 /* Allocate string descriptor numbers ... note that string
164 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700165 */
166
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200167 status = usb_string_ids_tab(cdev, strings_dev);
David Brownell7bb5ea52008-06-19 18:19:03 -0700168 if (status < 0)
169 goto fail;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200170 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
171 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200172 status = strings_dev[STRING_DESCRIPTION_IDX].id;
David Brownell7bb5ea52008-06-19 18:19:03 -0700173 serial_config_driver.iConfiguration = status;
174
David Brownell7bb5ea52008-06-19 18:19:03 -0700175 if (gadget_is_otg(cdev->gadget)) {
176 serial_config_driver.descriptors = otg_desc;
177 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
178 }
179
180 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200181 status = usb_add_config(cdev, &serial_config_driver,
182 serial_bind_config);
David Brownell7bb5ea52008-06-19 18:19:03 -0700183 if (status < 0)
184 goto fail;
185
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200186 usb_composite_overwrite_options(cdev, &coverwrite);
David Brownell7bb5ea52008-06-19 18:19:03 -0700187 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700190
191fail:
192 gserial_cleanup();
193 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200196static __refdata struct usb_composite_driver gserial_driver = {
David Brownell7bb5ea52008-06-19 18:19:03 -0700197 .name = "g_serial",
198 .dev = &device_desc,
199 .strings = dev_strings,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100200 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200201 .bind = gs_bind,
David Brownell7bb5ea52008-06-19 18:19:03 -0700202};
203
204static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
David Brownell7bb5ea52008-06-19 18:19:03 -0700206 /* We *could* export two configs; that'd be much cleaner...
207 * but neither of these product IDs was defined that way.
208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700210 serial_config_driver.label = "CDC ACM config";
211 serial_config_driver.bConfigurationValue = 2;
212 device_desc.bDeviceClass = USB_CLASS_COMM;
213 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800214 cpu_to_le16(GS_CDC_PRODUCT_ID);
Felipe Balbi30867752008-08-18 17:39:30 -0700215 } else if (use_obex) {
216 serial_config_driver.label = "CDC OBEX config";
217 serial_config_driver.bConfigurationValue = 3;
218 device_desc.bDeviceClass = USB_CLASS_COMM;
219 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800220 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700222 serial_config_driver.label = "Generic Serial config";
223 serial_config_driver.bConfigurationValue = 1;
224 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
225 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800226 cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700228 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200230 return usb_composite_probe(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
David Brownell7bb5ea52008-06-19 18:19:03 -0700232module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
David Brownell7bb5ea52008-06-19 18:19:03 -0700234static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
David Brownell7bb5ea52008-06-19 18:19:03 -0700236 usb_composite_unregister(&gserial_driver);
237 gserial_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
David Brownell7bb5ea52008-06-19 18:19:03 -0700239module_exit(cleanup);