2 * serial.c -- USB gadget serial driver
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
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.
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
19 #include "gadget_chips.h"
24 #define GS_VERSION_STR "v2.4"
25 #define GS_VERSION_NUM 0x2400
27 #define GS_LONG_NAME "Gadget Serial"
28 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
30 /*-------------------------------------------------------------------------*/
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.
44 /*-------------------------------------------------------------------------*/
45 USB_GADGET_COMPOSITE_OPTIONS();
47 /* Thanks to NetChip Technologies for donating this product ID.
49 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50 * Instead: allocate your own, using normal USB-IF procedures.
52 #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 */
55 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
57 /* string IDs are assigned dynamically */
59 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
61 static struct usb_string strings_dev
[] = {
62 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
63 [USB_GADGET_PRODUCT_IDX
].s
= GS_VERSION_NAME
,
64 [USB_GADGET_SERIAL_IDX
].s
= "",
65 [STRING_DESCRIPTION_IDX
].s
= NULL
/* updated; f(use_acm) */,
69 static struct usb_gadget_strings stringtab_dev
= {
70 .language
= 0x0409, /* en-us */
71 .strings
= strings_dev
,
74 static struct usb_gadget_strings
*dev_strings
[] = {
79 static struct usb_device_descriptor device_desc
= {
80 .bLength
= USB_DT_DEVICE_SIZE
,
81 .bDescriptorType
= USB_DT_DEVICE
,
82 .bcdUSB
= cpu_to_le16(0x0200),
83 /* .bDeviceClass = f(use_acm) */
86 /* .bMaxPacketSize0 = f(hardware) */
87 .idVendor
= cpu_to_le16(GS_VENDOR_ID
),
88 /* .idProduct = f(use_acm) */
89 /* .bcdDevice = f(hardware) */
90 /* .iManufacturer = DYNAMIC */
91 /* .iProduct = DYNAMIC */
92 .bNumConfigurations
= 1,
95 static struct usb_otg_descriptor otg_descriptor
= {
96 .bLength
= sizeof otg_descriptor
,
97 .bDescriptorType
= USB_DT_OTG
,
99 /* REVISIT SRP-only hardware is possible, although
100 * it would not be called "OTG" ...
102 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
105 static const struct usb_descriptor_header
*otg_desc
[] = {
106 (struct usb_descriptor_header
*) &otg_descriptor
,
110 /*-------------------------------------------------------------------------*/
113 MODULE_DESCRIPTION(GS_VERSION_NAME
);
114 MODULE_AUTHOR("Al Borchers");
115 MODULE_AUTHOR("David Brownell");
116 MODULE_LICENSE("GPL");
118 static bool use_acm
= true;
119 module_param(use_acm
, bool, 0);
120 MODULE_PARM_DESC(use_acm
, "Use CDC ACM, default=yes");
122 static bool use_obex
= false;
123 module_param(use_obex
, bool, 0);
124 MODULE_PARM_DESC(use_obex
, "Use CDC OBEX, default=no");
126 static unsigned n_ports
= 1;
127 module_param(n_ports
, uint
, 0);
128 MODULE_PARM_DESC(n_ports
, "number of ports to create, default=1");
130 /*-------------------------------------------------------------------------*/
132 static int __init
serial_bind_config(struct usb_configuration
*c
)
137 for (i
= 0; i
< n_ports
&& status
== 0; i
++) {
139 status
= acm_bind_config(c
, i
);
141 status
= obex_bind_config(c
, i
);
143 status
= gser_bind_config(c
, i
);
148 static struct usb_configuration serial_config_driver
= {
149 /* .label = f(use_acm) */
150 /* .bConfigurationValue = f(use_acm) */
151 /* .iConfiguration = DYNAMIC */
152 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
155 static int __init
gs_bind(struct usb_composite_dev
*cdev
)
158 struct usb_gadget
*gadget
= cdev
->gadget
;
161 status
= gserial_setup(cdev
->gadget
, n_ports
);
165 /* Allocate string descriptor numbers ... note that string
166 * contents can be overridden by the composite_dev glue.
169 status
= usb_string_ids_tab(cdev
, strings_dev
);
172 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
173 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
174 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
175 serial_config_driver
.iConfiguration
= status
;
177 /* set up other descriptors */
178 gcnum
= usb_gadget_controller_number(gadget
);
180 device_desc
.bcdDevice
= cpu_to_le16(GS_VERSION_NUM
| gcnum
);
182 /* this is so simple (for now, no altsettings) that it
183 * SHOULD NOT have problems with bulk-capable hardware.
184 * so warn about unrcognized controllers -- don't panic.
186 * things like configuration and altsetting numbering
187 * can need hardware-specific attention though.
189 pr_warning("gs_bind: controller '%s' not recognized\n",
191 device_desc
.bcdDevice
=
192 cpu_to_le16(GS_VERSION_NUM
| 0x0099);
195 if (gadget_is_otg(cdev
->gadget
)) {
196 serial_config_driver
.descriptors
= otg_desc
;
197 serial_config_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
200 /* register our configuration */
201 status
= usb_add_config(cdev
, &serial_config_driver
,
206 usb_composite_overwrite_options(cdev
, &coverwrite
);
207 INFO(cdev
, "%s\n", GS_VERSION_NAME
);
216 static __refdata
struct usb_composite_driver gserial_driver
= {
219 .strings
= dev_strings
,
220 .max_speed
= USB_SPEED_SUPER
,
224 static int __init
init(void)
226 /* We *could* export two configs; that'd be much cleaner...
227 * but neither of these product IDs was defined that way.
230 serial_config_driver
.label
= "CDC ACM config";
231 serial_config_driver
.bConfigurationValue
= 2;
232 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
233 device_desc
.idProduct
=
234 cpu_to_le16(GS_CDC_PRODUCT_ID
);
235 } else if (use_obex
) {
236 serial_config_driver
.label
= "CDC OBEX config";
237 serial_config_driver
.bConfigurationValue
= 3;
238 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
239 device_desc
.idProduct
=
240 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID
);
242 serial_config_driver
.label
= "Generic Serial config";
243 serial_config_driver
.bConfigurationValue
= 1;
244 device_desc
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
245 device_desc
.idProduct
=
246 cpu_to_le16(GS_PRODUCT_ID
);
248 strings_dev
[STRING_DESCRIPTION_IDX
].s
= serial_config_driver
.label
;
250 return usb_composite_probe(&gserial_driver
);
254 static void __exit
cleanup(void)
256 usb_composite_unregister(&gserial_driver
);
259 module_exit(cleanup
);