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.
41 /*-------------------------------------------------------------------------*/
42 USB_GADGET_COMPOSITE_OPTIONS();
44 /* Thanks to NetChip Technologies for donating this product ID.
46 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
47 * Instead: allocate your own, using normal USB-IF procedures.
49 #define GS_VENDOR_ID 0x0525 /* NetChip */
50 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
51 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
52 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
54 /* string IDs are assigned dynamically */
56 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
58 static struct usb_string strings_dev
[] = {
59 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
60 [USB_GADGET_PRODUCT_IDX
].s
= GS_VERSION_NAME
,
61 [USB_GADGET_SERIAL_IDX
].s
= "",
62 [STRING_DESCRIPTION_IDX
].s
= NULL
/* updated; f(use_acm) */,
66 static struct usb_gadget_strings stringtab_dev
= {
67 .language
= 0x0409, /* en-us */
68 .strings
= strings_dev
,
71 static struct usb_gadget_strings
*dev_strings
[] = {
76 static struct usb_device_descriptor device_desc
= {
77 .bLength
= USB_DT_DEVICE_SIZE
,
78 .bDescriptorType
= USB_DT_DEVICE
,
79 .bcdUSB
= cpu_to_le16(0x0200),
80 /* .bDeviceClass = f(use_acm) */
83 /* .bMaxPacketSize0 = f(hardware) */
84 .idVendor
= cpu_to_le16(GS_VENDOR_ID
),
85 /* .idProduct = f(use_acm) */
86 .bcdDevice
= cpu_to_le16(GS_VERSION_NUM
),
87 /* .iManufacturer = DYNAMIC */
88 /* .iProduct = DYNAMIC */
89 .bNumConfigurations
= 1,
92 static struct usb_otg_descriptor otg_descriptor
= {
93 .bLength
= sizeof otg_descriptor
,
94 .bDescriptorType
= USB_DT_OTG
,
96 /* REVISIT SRP-only hardware is possible, although
97 * it would not be called "OTG" ...
99 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
102 static const struct usb_descriptor_header
*otg_desc
[] = {
103 (struct usb_descriptor_header
*) &otg_descriptor
,
107 /*-------------------------------------------------------------------------*/
110 MODULE_DESCRIPTION(GS_VERSION_NAME
);
111 MODULE_AUTHOR("Al Borchers");
112 MODULE_AUTHOR("David Brownell");
113 MODULE_LICENSE("GPL");
115 static bool use_acm
= true;
116 module_param(use_acm
, bool, 0);
117 MODULE_PARM_DESC(use_acm
, "Use CDC ACM, default=yes");
119 static bool use_obex
= false;
120 module_param(use_obex
, bool, 0);
121 MODULE_PARM_DESC(use_obex
, "Use CDC OBEX, default=no");
123 static unsigned n_ports
= 1;
124 module_param(n_ports
, uint
, 0);
125 MODULE_PARM_DESC(n_ports
, "number of ports to create, default=1");
127 /*-------------------------------------------------------------------------*/
128 static unsigned char tty_lines
[MAX_U_SERIAL_PORTS
];
130 static int __init
serial_bind_obex_config(struct usb_configuration
*c
)
135 for (i
= 0; i
< n_ports
&& status
== 0; i
++)
136 status
= obex_bind_config(c
, tty_lines
[i
]);
140 static struct usb_configuration serial_config_driver
= {
141 /* .label = f(use_acm) */
142 /* .bConfigurationValue = f(use_acm) */
143 /* .iConfiguration = DYNAMIC */
144 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
147 static struct usb_function_instance
*fi_serial
[MAX_U_SERIAL_PORTS
];
148 static struct usb_function
*f_serial
[MAX_U_SERIAL_PORTS
];
150 static int serial_register_ports(struct usb_composite_dev
*cdev
,
151 struct usb_configuration
*c
, const char *f_name
)
156 ret
= usb_add_config_only(cdev
, c
);
160 for (i
= 0; i
< n_ports
; i
++) {
162 fi_serial
[i
] = usb_get_function_instance(f_name
);
163 if (IS_ERR(fi_serial
[i
])) {
164 ret
= PTR_ERR(fi_serial
[i
]);
168 f_serial
[i
] = usb_get_function(fi_serial
[i
]);
169 if (IS_ERR(f_serial
[i
])) {
170 ret
= PTR_ERR(f_serial
[i
]);
174 ret
= usb_add_function(c
, f_serial
[i
]);
182 usb_put_function(f_serial
[i
]);
184 usb_put_function_instance(fi_serial
[i
]);
189 usb_remove_function(c
, f_serial
[i
]);
190 usb_put_function(f_serial
[i
]);
191 usb_put_function_instance(fi_serial
[i
]);
198 static int __init
gs_bind(struct usb_composite_dev
*cdev
)
204 for (cur_line
= 0; cur_line
< n_ports
; cur_line
++) {
205 status
= gserial_alloc_line(&tty_lines
[cur_line
]);
211 /* Allocate string descriptor numbers ... note that string
212 * contents can be overridden by the composite_dev glue.
215 status
= usb_string_ids_tab(cdev
, strings_dev
);
218 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
219 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
220 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
221 serial_config_driver
.iConfiguration
= status
;
223 if (gadget_is_otg(cdev
->gadget
)) {
224 serial_config_driver
.descriptors
= otg_desc
;
225 serial_config_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
228 /* register our configuration */
230 status
= serial_register_ports(cdev
, &serial_config_driver
,
232 usb_ep_autoconfig_reset(cdev
->gadget
);
234 status
= usb_add_config(cdev
, &serial_config_driver
,
235 serial_bind_obex_config
);
237 status
= serial_register_ports(cdev
, &serial_config_driver
,
243 usb_composite_overwrite_options(cdev
, &coverwrite
);
244 INFO(cdev
, "%s\n", GS_VERSION_NAME
);
250 while (cur_line
>= 0 && use_obex
)
251 gserial_free_line(tty_lines
[cur_line
--]);
255 static int gs_unbind(struct usb_composite_dev
*cdev
)
259 for (i
= 0; i
< n_ports
; i
++) {
260 usb_put_function(f_serial
[i
]);
261 usb_put_function_instance(fi_serial
[i
]);
263 gserial_free_line(tty_lines
[i
]);
268 static __refdata
struct usb_composite_driver gserial_driver
= {
271 .strings
= dev_strings
,
272 .max_speed
= USB_SPEED_SUPER
,
277 static int __init
init(void)
279 /* We *could* export two configs; that'd be much cleaner...
280 * but neither of these product IDs was defined that way.
283 serial_config_driver
.label
= "CDC ACM config";
284 serial_config_driver
.bConfigurationValue
= 2;
285 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
286 device_desc
.idProduct
=
287 cpu_to_le16(GS_CDC_PRODUCT_ID
);
288 } else if (use_obex
) {
289 serial_config_driver
.label
= "CDC OBEX config";
290 serial_config_driver
.bConfigurationValue
= 3;
291 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
292 device_desc
.idProduct
=
293 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID
);
295 serial_config_driver
.label
= "Generic Serial config";
296 serial_config_driver
.bConfigurationValue
= 1;
297 device_desc
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
298 device_desc
.idProduct
=
299 cpu_to_le16(GS_PRODUCT_ID
);
301 strings_dev
[STRING_DESCRIPTION_IDX
].s
= serial_config_driver
.label
;
303 return usb_composite_probe(&gserial_driver
);
307 static void __exit
cleanup(void)
309 usb_composite_unregister(&gserial_driver
);
311 module_exit(cleanup
);