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>
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 /*-------------------------------------------------------------------------*/
31 USB_GADGET_COMPOSITE_OPTIONS();
33 /* Thanks to NetChip Technologies for donating this product ID.
35 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
36 * Instead: allocate your own, using normal USB-IF procedures.
38 #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 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
43 /* string IDs are assigned dynamically */
45 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
47 static struct usb_string strings_dev
[] = {
48 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
49 [USB_GADGET_PRODUCT_IDX
].s
= GS_VERSION_NAME
,
50 [USB_GADGET_SERIAL_IDX
].s
= "",
51 [STRING_DESCRIPTION_IDX
].s
= NULL
/* updated; f(use_acm) */,
55 static struct usb_gadget_strings stringtab_dev
= {
56 .language
= 0x0409, /* en-us */
57 .strings
= strings_dev
,
60 static struct usb_gadget_strings
*dev_strings
[] = {
65 static struct usb_device_descriptor device_desc
= {
66 .bLength
= USB_DT_DEVICE_SIZE
,
67 .bDescriptorType
= USB_DT_DEVICE
,
68 .bcdUSB
= cpu_to_le16(0x0200),
69 /* .bDeviceClass = f(use_acm) */
72 /* .bMaxPacketSize0 = f(hardware) */
73 .idVendor
= cpu_to_le16(GS_VENDOR_ID
),
74 /* .idProduct = f(use_acm) */
75 .bcdDevice
= cpu_to_le16(GS_VERSION_NUM
),
76 /* .iManufacturer = DYNAMIC */
77 /* .iProduct = DYNAMIC */
78 .bNumConfigurations
= 1,
81 static const struct usb_descriptor_header
*otg_desc
[2];
83 /*-------------------------------------------------------------------------*/
86 MODULE_DESCRIPTION(GS_VERSION_NAME
);
87 MODULE_AUTHOR("Al Borchers");
88 MODULE_AUTHOR("David Brownell");
89 MODULE_LICENSE("GPL");
91 static bool use_acm
= true;
92 module_param(use_acm
, bool, 0);
93 MODULE_PARM_DESC(use_acm
, "Use CDC ACM, default=yes");
95 static bool use_obex
= false;
96 module_param(use_obex
, bool, 0);
97 MODULE_PARM_DESC(use_obex
, "Use CDC OBEX, default=no");
99 static unsigned n_ports
= 1;
100 module_param(n_ports
, uint
, 0);
101 MODULE_PARM_DESC(n_ports
, "number of ports to create, default=1");
103 /*-------------------------------------------------------------------------*/
105 static struct usb_configuration serial_config_driver
= {
106 /* .label = f(use_acm) */
107 /* .bConfigurationValue = f(use_acm) */
108 /* .iConfiguration = DYNAMIC */
109 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
112 static struct usb_function_instance
*fi_serial
[MAX_U_SERIAL_PORTS
];
113 static struct usb_function
*f_serial
[MAX_U_SERIAL_PORTS
];
115 static int serial_register_ports(struct usb_composite_dev
*cdev
,
116 struct usb_configuration
*c
, const char *f_name
)
121 ret
= usb_add_config_only(cdev
, c
);
125 for (i
= 0; i
< n_ports
; i
++) {
127 fi_serial
[i
] = usb_get_function_instance(f_name
);
128 if (IS_ERR(fi_serial
[i
])) {
129 ret
= PTR_ERR(fi_serial
[i
]);
133 f_serial
[i
] = usb_get_function(fi_serial
[i
]);
134 if (IS_ERR(f_serial
[i
])) {
135 ret
= PTR_ERR(f_serial
[i
]);
139 ret
= usb_add_function(c
, f_serial
[i
]);
147 usb_put_function(f_serial
[i
]);
149 usb_put_function_instance(fi_serial
[i
]);
154 usb_remove_function(c
, f_serial
[i
]);
155 usb_put_function(f_serial
[i
]);
156 usb_put_function_instance(fi_serial
[i
]);
163 static int gs_bind(struct usb_composite_dev
*cdev
)
167 /* Allocate string descriptor numbers ... note that string
168 * contents can be overridden by the composite_dev glue.
171 status
= usb_string_ids_tab(cdev
, strings_dev
);
174 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
175 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
176 status
= strings_dev
[STRING_DESCRIPTION_IDX
].id
;
177 serial_config_driver
.iConfiguration
= status
;
179 if (gadget_is_otg(cdev
->gadget
)) {
181 struct usb_descriptor_header
*usb_desc
;
183 usb_desc
= usb_otg_descriptor_alloc(cdev
->gadget
);
188 usb_otg_descriptor_init(cdev
->gadget
, usb_desc
);
189 otg_desc
[0] = usb_desc
;
192 serial_config_driver
.descriptors
= otg_desc
;
193 serial_config_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
196 /* register our configuration */
198 status
= serial_register_ports(cdev
, &serial_config_driver
,
200 usb_ep_autoconfig_reset(cdev
->gadget
);
202 status
= serial_register_ports(cdev
, &serial_config_driver
,
205 status
= serial_register_ports(cdev
, &serial_config_driver
,
211 usb_composite_overwrite_options(cdev
, &coverwrite
);
212 INFO(cdev
, "%s\n", GS_VERSION_NAME
);
222 static int gs_unbind(struct usb_composite_dev
*cdev
)
226 for (i
= 0; i
< n_ports
; i
++) {
227 usb_put_function(f_serial
[i
]);
228 usb_put_function_instance(fi_serial
[i
]);
237 static struct usb_composite_driver gserial_driver
= {
240 .strings
= dev_strings
,
241 .max_speed
= USB_SPEED_SUPER
,
246 static int __init
init(void)
248 /* We *could* export two configs; that'd be much cleaner...
249 * but neither of these product IDs was defined that way.
252 serial_config_driver
.label
= "CDC ACM config";
253 serial_config_driver
.bConfigurationValue
= 2;
254 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
255 device_desc
.idProduct
=
256 cpu_to_le16(GS_CDC_PRODUCT_ID
);
257 } else if (use_obex
) {
258 serial_config_driver
.label
= "CDC OBEX config";
259 serial_config_driver
.bConfigurationValue
= 3;
260 device_desc
.bDeviceClass
= USB_CLASS_COMM
;
261 device_desc
.idProduct
=
262 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID
);
264 serial_config_driver
.label
= "Generic Serial config";
265 serial_config_driver
.bConfigurationValue
= 1;
266 device_desc
.bDeviceClass
= USB_CLASS_VENDOR_SPEC
;
267 device_desc
.idProduct
=
268 cpu_to_le16(GS_PRODUCT_ID
);
270 strings_dev
[STRING_DESCRIPTION_IDX
].s
= serial_config_driver
.label
;
272 return usb_composite_probe(&gserial_driver
);
276 static void __exit
cleanup(void)
278 usb_composite_unregister(&gserial_driver
);
280 module_exit(cleanup
);