blob: ed1b816e58d86291ecd81e6163f9c7bf5a3409d0 [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"
41#include "usbstring.c"
42#include "config.c"
43#include "epautoconf.c"
44
45#include "f_acm.c"
Felipe Balbi30867752008-08-18 17:39:30 -070046#include "f_obex.c"
David Brownell4e9ba512008-08-18 17:41:02 -070047#include "f_serial.c"
48#include "u_serial.c"
49
50/*-------------------------------------------------------------------------*/
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* Thanks to NetChip Technologies for donating this product ID.
David Brownell7bb5ea52008-06-19 18:19:03 -070053*
54* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
55* Instead: allocate your own, using normal USB-IF procedures.
56*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define GS_VENDOR_ID 0x0525 /* NetChip */
58#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
59#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
Felipe Balbi30867752008-08-18 17:39:30 -070060#define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
David Brownell7bb5ea52008-06-19 18:19:03 -070062/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David Brownell7bb5ea52008-06-19 18:19:03 -070064#define STRING_MANUFACTURER_IDX 0
65#define STRING_PRODUCT_IDX 1
66#define STRING_DESCRIPTION_IDX 2
David Brownella7707ad2008-06-19 17:52:07 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static char manufacturer[50];
David Brownell7bb5ea52008-06-19 18:19:03 -070069
70static struct usb_string strings_dev[] = {
71 [STRING_MANUFACTURER_IDX].s = manufacturer,
72 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
73 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 { } /* end of list */
75};
76
David Brownell7bb5ea52008-06-19 18:19:03 -070077static struct usb_gadget_strings stringtab_dev = {
78 .language = 0x0409, /* en-us */
79 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
David Brownell7bb5ea52008-06-19 18:19:03 -070082static struct usb_gadget_strings *dev_strings[] = {
83 &stringtab_dev,
84 NULL,
85};
86
87static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 .bLength = USB_DT_DEVICE_SIZE,
89 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -080090 .bcdUSB = cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070091 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .bDeviceSubClass = 0,
93 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070094 /* .bMaxPacketSize0 = f(hardware) */
Harvey Harrison551509d2009-02-11 14:11:36 -080095 .idVendor = cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070096 /* .idProduct = f(use_acm) */
97 /* .bcdDevice = f(hardware) */
98 /* .iManufacturer = DYNAMIC */
99 /* .iProduct = DYNAMIC */
100 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
David Brownell7bb5ea52008-06-19 18:19:03 -0700103static struct usb_otg_descriptor otg_descriptor = {
104 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -0700106
107 /* REVISIT SRP-only hardware is possible, although
108 * it would not be called "OTG" ...
109 */
110 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
David Brownell7bb5ea52008-06-19 18:19:03 -0700113static const struct usb_descriptor_header *otg_desc[] = {
114 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 NULL,
116};
117
David Brownell9079e912008-05-07 16:00:36 -0700118/*-------------------------------------------------------------------------*/
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700121MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700123MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124MODULE_LICENSE("GPL");
125
David Brownell7bb5ea52008-06-19 18:19:03 -0700126static int use_acm = true;
127module_param(use_acm, bool, 0);
128MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
129
Felipe Balbi30867752008-08-18 17:39:30 -0700130static int use_obex = false;
131module_param(use_obex, bool, 0);
132MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
133
David Brownell7bb5ea52008-06-19 18:19:03 -0700134static unsigned n_ports = 1;
135module_param(n_ports, uint, 0);
136MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David Brownell9079e912008-05-07 16:00:36 -0700138/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200140static int __init serial_bind_config(struct usb_configuration *c)
David Brownell9079e912008-05-07 16:00:36 -0700141{
David Brownell7bb5ea52008-06-19 18:19:03 -0700142 unsigned i;
143 int status = 0;
David Brownell9079e912008-05-07 16:00:36 -0700144
David Brownell7bb5ea52008-06-19 18:19:03 -0700145 for (i = 0; i < n_ports && status == 0; i++) {
146 if (use_acm)
147 status = acm_bind_config(c, i);
Felipe Balbi30867752008-08-18 17:39:30 -0700148 else if (use_obex)
149 status = obex_bind_config(c, i);
David Brownell7bb5ea52008-06-19 18:19:03 -0700150 else
151 status = gser_bind_config(c, i);
David Brownell9079e912008-05-07 16:00:36 -0700152 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700153 return status;
David Brownell9079e912008-05-07 16:00:36 -0700154}
155
David Brownell7bb5ea52008-06-19 18:19:03 -0700156static struct usb_configuration serial_config_driver = {
157 /* .label = f(use_acm) */
David Brownell7bb5ea52008-06-19 18:19:03 -0700158 /* .bConfigurationValue = f(use_acm) */
159 /* .iConfiguration = DYNAMIC */
160 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
David Brownell9079e912008-05-07 16:00:36 -0700161};
162
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200163static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
David Brownell7bb5ea52008-06-19 18:19:03 -0700165 int gcnum;
166 struct usb_gadget *gadget = cdev->gadget;
167 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
David Brownell7bb5ea52008-06-19 18:19:03 -0700169 status = gserial_setup(cdev->gadget, n_ports);
170 if (status < 0)
171 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
David Brownell7bb5ea52008-06-19 18:19:03 -0700173 /* Allocate string descriptor numbers ... note that string
174 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700175 */
176
David Brownell7bb5ea52008-06-19 18:19:03 -0700177 /* device description: manufacturer, product */
178 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
179 init_utsname()->sysname, init_utsname()->release,
180 gadget->name);
181 status = usb_string_id(cdev);
182 if (status < 0)
183 goto fail;
184 strings_dev[STRING_MANUFACTURER_IDX].id = status;
185
186 device_desc.iManufacturer = status;
187
188 status = usb_string_id(cdev);
189 if (status < 0)
190 goto fail;
191 strings_dev[STRING_PRODUCT_IDX].id = status;
192
193 device_desc.iProduct = status;
194
195 /* config description */
196 status = usb_string_id(cdev);
197 if (status < 0)
198 goto fail;
199 strings_dev[STRING_DESCRIPTION_IDX].id = status;
200
201 serial_config_driver.iConfiguration = status;
202
203 /* set up other descriptors */
204 gcnum = usb_gadget_controller_number(gadget);
205 if (gcnum >= 0)
206 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
207 else {
208 /* this is so simple (for now, no altsettings) that it
209 * SHOULD NOT have problems with bulk-capable hardware.
210 * so warn about unrcognized controllers -- don't panic.
211 *
212 * things like configuration and altsetting numbering
213 * can need hardware-specific attention though.
214 */
215 pr_warning("gs_bind: controller '%s' not recognized\n",
216 gadget->name);
217 device_desc.bcdDevice =
Harvey Harrison551509d2009-02-11 14:11:36 -0800218 cpu_to_le16(GS_VERSION_NUM | 0x0099);
David Brownell7bb5ea52008-06-19 18:19:03 -0700219 }
220
221 if (gadget_is_otg(cdev->gadget)) {
222 serial_config_driver.descriptors = otg_desc;
223 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
224 }
225
226 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200227 status = usb_add_config(cdev, &serial_config_driver,
228 serial_bind_config);
David Brownell7bb5ea52008-06-19 18:19:03 -0700229 if (status < 0)
230 goto fail;
231
232 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700235
236fail:
237 gserial_cleanup();
238 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
David Brownell7bb5ea52008-06-19 18:19:03 -0700241static struct usb_composite_driver gserial_driver = {
242 .name = "g_serial",
243 .dev = &device_desc,
244 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300245 .max_speed = USB_SPEED_HIGH,
David Brownell7bb5ea52008-06-19 18:19:03 -0700246};
247
248static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
David Brownell7bb5ea52008-06-19 18:19:03 -0700250 /* We *could* export two configs; that'd be much cleaner...
251 * but neither of these product IDs was defined that way.
252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700254 serial_config_driver.label = "CDC ACM config";
255 serial_config_driver.bConfigurationValue = 2;
256 device_desc.bDeviceClass = USB_CLASS_COMM;
257 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800258 cpu_to_le16(GS_CDC_PRODUCT_ID);
Felipe Balbi30867752008-08-18 17:39:30 -0700259 } else if (use_obex) {
260 serial_config_driver.label = "CDC OBEX config";
261 serial_config_driver.bConfigurationValue = 3;
262 device_desc.bDeviceClass = USB_CLASS_COMM;
263 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800264 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700266 serial_config_driver.label = "Generic Serial config";
267 serial_config_driver.bConfigurationValue = 1;
268 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
269 device_desc.idProduct =
Harvey Harrison551509d2009-02-11 14:11:36 -0800270 cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700272 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200274 return usb_composite_probe(&gserial_driver, gs_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
David Brownell7bb5ea52008-06-19 18:19:03 -0700276module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David Brownell7bb5ea52008-06-19 18:19:03 -0700278static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
David Brownell7bb5ea52008-06-19 18:19:03 -0700280 usb_composite_unregister(&gserial_driver);
281 gserial_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
David Brownell7bb5ea52008-06-19 18:19:03 -0700283module_exit(cleanup);