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/module.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
20 #include "gadget_chips.h"
25 #define GS_VERSION_STR "v2.4"
26 #define GS_VERSION_NUM 0x2400
28 #define GS_LONG_NAME "Gadget Serial"
29 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
31 /*-------------------------------------------------------------------------*/
32 USB_GADGET_COMPOSITE_OPTIONS();
34 /* Thanks to NetChip Technologies for donating this product ID.
36 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
37 * Instead: allocate your own, using normal USB-IF procedures.
39 #define GS_VENDOR_ID 0x0525 /* NetChip */
40 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
41 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
42 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
44 /* string IDs are assigned dynamically */
46 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
48 static struct usb_string strings_dev
[] = {
49 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
50 [USB_GADGET_PRODUCT_IDX
].s
= GS_VERSION_NAME
,
51 [USB_GADGET_SERIAL_IDX
].s
= "",
52 [STRING_DESCRIPTION_IDX
].s
= NULL
/* updated; f(use_acm) */,
56 static struct usb_gadget_strings stringtab_dev
= {
57 .language
= 0x0409, /* en-us */
58 .strings
= strings_dev
,
61 static struct usb_gadget_strings
*dev_strings
[] = {
66 static struct usb_device_descriptor device_desc
= {
67 .bLength
= USB_DT_DEVICE_SIZE
,
68 .bDescriptorType
= USB_DT_DEVICE
,
69 .bcdUSB
= cpu_to_le16(0x0200),
70 /* .bDeviceClass = f(use_acm) */
73 /* .bMaxPacketSize0 = f(hardware) */
74 .idVendor
= cpu_to_le16(GS_VENDOR_ID
),
75 /* .idProduct = f(use_acm) */
76 .bcdDevice
= cpu_to_le16(GS_VERSION_NUM
),
77 /* .iManufacturer = DYNAMIC */
78 /* .iProduct = DYNAMIC */
79 .bNumConfigurations
= 1,
82 static const struct usb_descriptor_header
*otg_desc
[2];
84 /*-------------------------------------------------------------------------*/
87 MODULE_DESCRIPTION(GS_VERSION_NAME
);
88 MODULE_AUTHOR("Al Borchers");
89 MODULE_AUTHOR("David Brownell");
90 MODULE_LICENSE("GPL");
92 static bool use_acm
= true;
93 module_param(use_acm
, bool, 0);
94 MODULE_PARM_DESC(use_acm
, "Use CDC ACM, default=yes");
96 static bool use_obex
= false;
97 module_param(use_obex
, bool, 0);
98 MODULE_PARM_DESC(use_obex
, "Use CDC OBEX, default=no");
100 static unsigned n_ports
= 1;
101 module_param(n_ports
, uint
, 0);
102 MODULE_PARM_DESC(n_ports
, "number of ports to create, default=1");
104 /*-------------------------------------------------------------------------*/
106 static struct usb_configuration serial_config_driver
= {
107 /* .label = f(use_acm) */
108 /* .bConfigurationValue = f(use_acm) */
109 /* .iConfiguration = DYNAMIC */
110 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
113 static struct usb_function_instance
*fi_serial
[MAX_U_SERIAL_PORTS
];
114 static struct usb_function
*f_serial
[MAX_U_SERIAL_PORTS
];
116 static int serial_register_ports(struct usb_composite_dev
*cdev
,
117 struct usb_configuration
*c
, const char *f_name
)
122 ret
= usb_add_config_only(cdev
, c
);
126 for (i
= 0; i
< n_ports
; i
++) {
128 fi_serial
[i
] = usb_get_function_instance(f_name
);
129 if (IS_ERR(fi_serial
[i
])) {
130 ret
= PTR_ERR(fi_serial
[i
]);
134 f_serial
[i
] = usb_get_function(fi_serial
[i
]);
135 if (IS_ERR(f_serial
[i
])) {
136 ret
= PTR_ERR(f_serial
[i
]);
140 ret
= usb_add_function(c
, f_serial
[i
]);
148 usb_put_function(f_serial
[i
]);
150 usb_put_function_instance(fi_serial
[i
]);
155 usb_remove_function(c
, f_serial
[i
]);
156 usb_put_function(f_serial
[i
]);
157 usb_put_function_instance(fi_serial
[i
]);
164 static int gs_bind(struct usb_composite_dev
*cdev
)
168 /* Allocate string descriptor numbers ... note that string
169 * contents can be overridden by the composite_dev glue.
172 status
= usb_string_ids_tab(cdev
, strings_dev
);
175 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
176 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
177 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
178 serial_config_driver
.iConfiguration
= status
;
180 if (gadget_is_otg(cdev
->gadget
)) {
182 struct usb_descriptor_header
*usb_desc
;
184 usb_desc
= usb_otg_descriptor_alloc(cdev
->gadget
);
189 usb_otg_descriptor_init(cdev
->gadget
, usb_desc
);
190 otg_desc
[0] = usb_desc
;
193 serial_config_driver
.descriptors
= otg_desc
;
194 serial_config_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
197 /* register our configuration */
199 status
= serial_register_ports(cdev
, &serial_config_driver
,
201 usb_ep_autoconfig_reset(cdev
->gadget
);
203 status
= serial_register_ports(cdev
, &serial_config_driver
,
206 status
= serial_register_ports(cdev
, &serial_config_driver
,
212 usb_composite_overwrite_options(cdev
, &coverwrite
);
213 INFO(cdev
, "%s\n", GS_VERSION_NAME
);
223 static int gs_unbind(struct usb_composite_dev
*cdev
)
227 for (i
= 0; i
< n_ports
; i
++) {
228 usb_put_function(f_serial
[i
]);
229 usb_put_function_instance(fi_serial
[i
]);
238 static struct usb_composite_driver gserial_driver
= {
241 .strings
= dev_strings
,
242 .max_speed
= USB_SPEED_SUPER
,
247 static int __init
init(void)
249 /* We *could* export two configs; that'd be much cleaner...
250 * but neither of these product IDs was defined that way.
253 serial_config_driver
.label
= "CDC ACM config";
254 serial_config_driver
.bConfigurationValue
= 2;
255 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
256 device_desc
.idProduct
=
257 cpu_to_le16(GS_CDC_PRODUCT_ID
);
258 } else if (use_obex
) {
259 serial_config_driver
.label
= "CDC OBEX config";
260 serial_config_driver
.bConfigurationValue
= 3;
261 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
262 device_desc
.idProduct
=
263 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID
);
265 serial_config_driver
.label
= "Generic Serial config";
266 serial_config_driver
.bConfigurationValue
= 1;
267 device_desc
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
268 device_desc
.idProduct
=
269 cpu_to_le16(GS_PRODUCT_ID
);
271 strings_dev
[STRING_DESCRIPTION_IDX
].s
= serial_config_driver
.label
;
273 return usb_composite_probe(&gserial_driver
);
277 static void __exit
cleanup(void)
279 usb_composite_unregister(&gserial_driver
);
281 module_exit(cleanup
);