blob: b3699afff002f4115b4c3ffaf1b975638aa88f1f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* Thanks to NetChip Technologies for donating this product ID.
David Brownell7bb5ea52008-06-19 18:19:03 -070034*
35* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
36* Instead: allocate your own, using normal USB-IF procedures.
37*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define GS_VENDOR_ID 0x0525 /* NetChip */
39#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
40#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
41
David Brownell7bb5ea52008-06-19 18:19:03 -070042/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Brownell7bb5ea52008-06-19 18:19:03 -070044#define STRING_MANUFACTURER_IDX 0
45#define STRING_PRODUCT_IDX 1
46#define STRING_DESCRIPTION_IDX 2
David Brownella7707ad2008-06-19 17:52:07 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static char manufacturer[50];
David Brownell7bb5ea52008-06-19 18:19:03 -070049
50static struct usb_string strings_dev[] = {
51 [STRING_MANUFACTURER_IDX].s = manufacturer,
52 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
53 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 { } /* end of list */
55};
56
David Brownell7bb5ea52008-06-19 18:19:03 -070057static struct usb_gadget_strings stringtab_dev = {
58 .language = 0x0409, /* en-us */
59 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
David Brownell7bb5ea52008-06-19 18:19:03 -070062static struct usb_gadget_strings *dev_strings[] = {
63 &stringtab_dev,
64 NULL,
65};
66
67static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .bLength = USB_DT_DEVICE_SIZE,
69 .bDescriptorType = USB_DT_DEVICE,
70 .bcdUSB = __constant_cpu_to_le16(0x0200),
David Brownell7bb5ea52008-06-19 18:19:03 -070071 /* .bDeviceClass = f(use_acm) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .bDeviceSubClass = 0,
73 .bDeviceProtocol = 0,
David Brownell7bb5ea52008-06-19 18:19:03 -070074 /* .bMaxPacketSize0 = f(hardware) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
David Brownell7bb5ea52008-06-19 18:19:03 -070076 /* .idProduct = f(use_acm) */
77 /* .bcdDevice = f(hardware) */
78 /* .iManufacturer = DYNAMIC */
79 /* .iProduct = DYNAMIC */
80 .bNumConfigurations = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
David Brownell7bb5ea52008-06-19 18:19:03 -070083static struct usb_otg_descriptor otg_descriptor = {
84 .bLength = sizeof otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .bDescriptorType = USB_DT_OTG,
David Brownell7bb5ea52008-06-19 18:19:03 -070086
87 /* REVISIT SRP-only hardware is possible, although
88 * it would not be called "OTG" ...
89 */
90 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
David Brownell7bb5ea52008-06-19 18:19:03 -070093static const struct usb_descriptor_header *otg_desc[] = {
94 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 NULL,
96};
97
David Brownell9079e912008-05-07 16:00:36 -070098/*-------------------------------------------------------------------------*/
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700101MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700103MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104MODULE_LICENSE("GPL");
105
David Brownell7bb5ea52008-06-19 18:19:03 -0700106static int use_acm = true;
107module_param(use_acm, bool, 0);
108MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
109
110static unsigned n_ports = 1;
111module_param(n_ports, uint, 0);
112MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Brownell9079e912008-05-07 16:00:36 -0700114/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
David Brownell7bb5ea52008-06-19 18:19:03 -0700116static int __init serial_bind_config(struct usb_configuration *c)
David Brownell9079e912008-05-07 16:00:36 -0700117{
David Brownell7bb5ea52008-06-19 18:19:03 -0700118 unsigned i;
119 int status = 0;
David Brownell9079e912008-05-07 16:00:36 -0700120
David Brownell7bb5ea52008-06-19 18:19:03 -0700121 for (i = 0; i < n_ports && status == 0; i++) {
122 if (use_acm)
123 status = acm_bind_config(c, i);
124 else
125 status = gser_bind_config(c, i);
David Brownell9079e912008-05-07 16:00:36 -0700126 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700127 return status;
David Brownell9079e912008-05-07 16:00:36 -0700128}
129
David Brownell7bb5ea52008-06-19 18:19:03 -0700130static struct usb_configuration serial_config_driver = {
131 /* .label = f(use_acm) */
132 .bind = serial_bind_config,
133 /* .bConfigurationValue = f(use_acm) */
134 /* .iConfiguration = DYNAMIC */
135 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
136 .bMaxPower = 1, /* 2 mA, minimal */
David Brownell9079e912008-05-07 16:00:36 -0700137};
138
David Brownell7bb5ea52008-06-19 18:19:03 -0700139static int __init gs_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
David Brownell7bb5ea52008-06-19 18:19:03 -0700141 int gcnum;
142 struct usb_gadget *gadget = cdev->gadget;
143 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Brownell7bb5ea52008-06-19 18:19:03 -0700145 status = gserial_setup(cdev->gadget, n_ports);
146 if (status < 0)
147 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David Brownell7bb5ea52008-06-19 18:19:03 -0700149 /* Allocate string descriptor numbers ... note that string
150 * contents can be overridden by the composite_dev glue.
David Brownellf371e752008-04-18 17:37:49 -0700151 */
152
David Brownell7bb5ea52008-06-19 18:19:03 -0700153 /* device description: manufacturer, product */
154 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
155 init_utsname()->sysname, init_utsname()->release,
156 gadget->name);
157 status = usb_string_id(cdev);
158 if (status < 0)
159 goto fail;
160 strings_dev[STRING_MANUFACTURER_IDX].id = status;
161
162 device_desc.iManufacturer = status;
163
164 status = usb_string_id(cdev);
165 if (status < 0)
166 goto fail;
167 strings_dev[STRING_PRODUCT_IDX].id = status;
168
169 device_desc.iProduct = status;
170
171 /* config description */
172 status = usb_string_id(cdev);
173 if (status < 0)
174 goto fail;
175 strings_dev[STRING_DESCRIPTION_IDX].id = status;
176
177 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 =
194 __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099);
195 }
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 */
203 status = usb_add_config(cdev, &serial_config_driver);
204 if (status < 0)
205 goto fail;
206
207 INFO(cdev, "%s\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 return 0;
David Brownell7bb5ea52008-06-19 18:19:03 -0700210
211fail:
212 gserial_cleanup();
213 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
David Brownell7bb5ea52008-06-19 18:19:03 -0700216static struct usb_composite_driver gserial_driver = {
217 .name = "g_serial",
218 .dev = &device_desc,
219 .strings = dev_strings,
220 .bind = gs_bind,
221};
222
223static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
David Brownell7bb5ea52008-06-19 18:19:03 -0700225 /* We *could* export two configs; that'd be much cleaner...
226 * but neither of these product IDs was defined that way.
227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (use_acm) {
David Brownell7bb5ea52008-06-19 18:19:03 -0700229 serial_config_driver.label = "CDC ACM config";
230 serial_config_driver.bConfigurationValue = 2;
231 device_desc.bDeviceClass = USB_CLASS_COMM;
232 device_desc.idProduct =
233 __constant_cpu_to_le16(GS_CDC_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 } else {
David Brownell7bb5ea52008-06-19 18:19:03 -0700235 serial_config_driver.label = "Generic Serial config";
236 serial_config_driver.bConfigurationValue = 1;
237 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
238 device_desc.idProduct =
239 __constant_cpu_to_le16(GS_PRODUCT_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
David Brownell7bb5ea52008-06-19 18:19:03 -0700241 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
David Brownell7bb5ea52008-06-19 18:19:03 -0700243 return usb_composite_register(&gserial_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
David Brownell7bb5ea52008-06-19 18:19:03 -0700245module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Brownell7bb5ea52008-06-19 18:19:03 -0700247static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
David Brownell7bb5ea52008-06-19 18:19:03 -0700249 usb_composite_unregister(&gserial_driver);
250 gserial_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
David Brownell7bb5ea52008-06-19 18:19:03 -0700252module_exit(cleanup);