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.
43 /*-------------------------------------------------------------------------*/
44 USB_GADGET_COMPOSITE_OPTIONS();
46 /* Thanks to NetChip Technologies for donating this product ID.
48 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
49 * Instead: allocate your own, using normal USB-IF procedures.
51 #define GS_VENDOR_ID 0x0525 /* NetChip */
52 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
53 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
54 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
56 /* string IDs are assigned dynamically */
58 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
60 static struct usb_string strings_dev
[] = {
61 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
62 [USB_GADGET_PRODUCT_IDX
].s
= GS_VERSION_NAME
,
63 [USB_GADGET_SERIAL_IDX
].s
= "",
64 [STRING_DESCRIPTION_IDX
].s
= NULL
/* updated; f(use_acm) */,
68 static struct usb_gadget_strings stringtab_dev
= {
69 .language
= 0x0409, /* en-us */
70 .strings
= strings_dev
,
73 static struct usb_gadget_strings
*dev_strings
[] = {
78 static struct usb_device_descriptor device_desc
= {
79 .bLength
= USB_DT_DEVICE_SIZE
,
80 .bDescriptorType
= USB_DT_DEVICE
,
81 .bcdUSB
= cpu_to_le16(0x0200),
82 /* .bDeviceClass = f(use_acm) */
85 /* .bMaxPacketSize0 = f(hardware) */
86 .idVendor
= cpu_to_le16(GS_VENDOR_ID
),
87 /* .idProduct = f(use_acm) */
88 .bcdDevice
= cpu_to_le16(GS_VERSION_NUM
),
89 /* .iManufacturer = DYNAMIC */
90 /* .iProduct = DYNAMIC */
91 .bNumConfigurations
= 1,
94 static struct usb_otg_descriptor otg_descriptor
= {
95 .bLength
= sizeof otg_descriptor
,
96 .bDescriptorType
= USB_DT_OTG
,
98 /* REVISIT SRP-only hardware is possible, although
99 * it would not be called "OTG" ...
101 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
104 static const struct usb_descriptor_header
*otg_desc
[] = {
105 (struct usb_descriptor_header
*) &otg_descriptor
,
109 /*-------------------------------------------------------------------------*/
112 MODULE_DESCRIPTION(GS_VERSION_NAME
);
113 MODULE_AUTHOR("Al Borchers");
114 MODULE_AUTHOR("David Brownell");
115 MODULE_LICENSE("GPL");
117 static bool use_acm
= true;
118 module_param(use_acm
, bool, 0);
119 MODULE_PARM_DESC(use_acm
, "Use CDC ACM, default=yes");
121 static bool use_obex
= false;
122 module_param(use_obex
, bool, 0);
123 MODULE_PARM_DESC(use_obex
, "Use CDC OBEX, default=no");
125 static unsigned n_ports
= 1;
126 module_param(n_ports
, uint
, 0);
127 MODULE_PARM_DESC(n_ports
, "number of ports to create, default=1");
129 /*-------------------------------------------------------------------------*/
131 static int __init
serial_bind_acm_config(struct usb_configuration
*c
)
136 for (i
= 0; i
< n_ports
&& status
== 0; i
++)
137 status
= acm_bind_config(c
, i
);
141 static int __init
serial_bind_obex_config(struct usb_configuration
*c
)
146 for (i
= 0; i
< n_ports
&& status
== 0; i
++)
147 status
= obex_bind_config(c
, i
);
151 static int __init
serial_bind_gser_config(struct usb_configuration
*c
)
156 for (i
= 0; i
< n_ports
&& status
== 0; i
++)
157 status
= gser_bind_config(c
, i
);
161 static struct usb_configuration serial_config_driver
= {
162 /* .label = f(use_acm) */
163 /* .bConfigurationValue = f(use_acm) */
164 /* .iConfiguration = DYNAMIC */
165 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
168 static int __init
gs_bind(struct usb_composite_dev
*cdev
)
172 status
= gserial_setup(cdev
->gadget
, n_ports
);
176 /* Allocate string descriptor numbers ... note that string
177 * contents can be overridden by the composite_dev glue.
180 status
= usb_string_ids_tab(cdev
, strings_dev
);
183 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
184 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
185 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
186 serial_config_driver
.iConfiguration
= status
;
188 if (gadget_is_otg(cdev
->gadget
)) {
189 serial_config_driver
.descriptors
= otg_desc
;
190 serial_config_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
193 /* register our configuration */
195 status
= usb_add_config(cdev
, &serial_config_driver
,
196 serial_bind_acm_config
);
198 status
= usb_add_config(cdev
, &serial_config_driver
,
199 serial_bind_obex_config
);
201 status
= usb_add_config(cdev
, &serial_config_driver
,
202 serial_bind_gser_config
);
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
);