2 * f_serial.c - generic USB serial function 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>
17 #include "gadget_chips.h"
21 * This function packages a simple "generic serial" port with no real
22 * control mechanisms, just raw data transfer over two bulk endpoints.
24 * Because it's not standardized, this isn't as interoperable as the
25 * CDC ACM driver. However, for many purposes it's just as functional
26 * if you can arrange appropriate host side drivers.
30 struct usb_endpoint_descriptor
*in
;
31 struct usb_endpoint_descriptor
*out
;
43 static inline struct f_gser
*func_to_gser(struct usb_function
*f
)
45 return container_of(f
, struct f_gser
, port
.func
);
48 /*-------------------------------------------------------------------------*/
50 /* interface descriptor: */
52 static struct usb_interface_descriptor gser_interface_desc __initdata
= {
53 .bLength
= USB_DT_INTERFACE_SIZE
,
54 .bDescriptorType
= USB_DT_INTERFACE
,
55 /* .bInterfaceNumber = DYNAMIC */
57 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
58 .bInterfaceSubClass
= 0,
59 .bInterfaceProtocol
= 0,
60 /* .iInterface = DYNAMIC */
63 /* full speed support: */
65 static struct usb_endpoint_descriptor gser_fs_in_desc __initdata
= {
66 .bLength
= USB_DT_ENDPOINT_SIZE
,
67 .bDescriptorType
= USB_DT_ENDPOINT
,
68 .bEndpointAddress
= USB_DIR_IN
,
69 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
72 static struct usb_endpoint_descriptor gser_fs_out_desc __initdata
= {
73 .bLength
= USB_DT_ENDPOINT_SIZE
,
74 .bDescriptorType
= USB_DT_ENDPOINT
,
75 .bEndpointAddress
= USB_DIR_OUT
,
76 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
79 static struct usb_descriptor_header
*gser_fs_function
[] __initdata
= {
80 (struct usb_descriptor_header
*) &gser_interface_desc
,
81 (struct usb_descriptor_header
*) &gser_fs_in_desc
,
82 (struct usb_descriptor_header
*) &gser_fs_out_desc
,
86 /* high speed support: */
88 static struct usb_endpoint_descriptor gser_hs_in_desc __initdata
= {
89 .bLength
= USB_DT_ENDPOINT_SIZE
,
90 .bDescriptorType
= USB_DT_ENDPOINT
,
91 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
92 .wMaxPacketSize
= cpu_to_le16(512),
95 static struct usb_endpoint_descriptor gser_hs_out_desc __initdata
= {
96 .bLength
= USB_DT_ENDPOINT_SIZE
,
97 .bDescriptorType
= USB_DT_ENDPOINT
,
98 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
99 .wMaxPacketSize
= cpu_to_le16(512),
102 static struct usb_descriptor_header
*gser_hs_function
[] __initdata
= {
103 (struct usb_descriptor_header
*) &gser_interface_desc
,
104 (struct usb_descriptor_header
*) &gser_hs_in_desc
,
105 (struct usb_descriptor_header
*) &gser_hs_out_desc
,
109 /* string descriptors: */
111 static struct usb_string gser_string_defs
[] = {
112 [0].s
= "Generic Serial",
113 { } /* end of list */
116 static struct usb_gadget_strings gser_string_table
= {
117 .language
= 0x0409, /* en-us */
118 .strings
= gser_string_defs
,
121 static struct usb_gadget_strings
*gser_strings
[] = {
126 /*-------------------------------------------------------------------------*/
128 static int gser_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
130 struct f_gser
*gser
= func_to_gser(f
);
131 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
133 /* we know alt == 0, so this is an activation or a reset */
135 if (gser
->port
.in
->driver_data
) {
136 DBG(cdev
, "reset generic ttyGS%d\n", gser
->port_num
);
137 gserial_disconnect(&gser
->port
);
139 DBG(cdev
, "activate generic ttyGS%d\n", gser
->port_num
);
140 gser
->port
.in_desc
= ep_choose(cdev
->gadget
,
141 gser
->hs
.in
, gser
->fs
.in
);
142 gser
->port
.out_desc
= ep_choose(cdev
->gadget
,
143 gser
->hs
.out
, gser
->fs
.out
);
145 gserial_connect(&gser
->port
, gser
->port_num
);
149 static void gser_disable(struct usb_function
*f
)
151 struct f_gser
*gser
= func_to_gser(f
);
152 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
154 DBG(cdev
, "generic ttyGS%d deactivated\n", gser
->port_num
);
155 gserial_disconnect(&gser
->port
);
158 /*-------------------------------------------------------------------------*/
160 /* serial function driver setup/binding */
163 gser_bind(struct usb_configuration
*c
, struct usb_function
*f
)
165 struct usb_composite_dev
*cdev
= c
->cdev
;
166 struct f_gser
*gser
= func_to_gser(f
);
170 /* allocate instance-specific interface IDs */
171 status
= usb_interface_id(c
, f
);
174 gser
->data_id
= status
;
175 gser_interface_desc
.bInterfaceNumber
= status
;
179 /* allocate instance-specific endpoints */
180 ep
= usb_ep_autoconfig(cdev
->gadget
, &gser_fs_in_desc
);
184 ep
->driver_data
= cdev
; /* claim */
186 ep
= usb_ep_autoconfig(cdev
->gadget
, &gser_fs_out_desc
);
190 ep
->driver_data
= cdev
; /* claim */
192 /* copy descriptors, and track endpoint copies */
193 f
->descriptors
= usb_copy_descriptors(gser_fs_function
);
195 gser
->fs
.in
= usb_find_endpoint(gser_fs_function
,
196 f
->descriptors
, &gser_fs_in_desc
);
197 gser
->fs
.out
= usb_find_endpoint(gser_fs_function
,
198 f
->descriptors
, &gser_fs_out_desc
);
201 /* support all relevant hardware speeds... we expect that when
202 * hardware is dual speed, all bulk-capable endpoints work at
205 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
206 gser_hs_in_desc
.bEndpointAddress
=
207 gser_fs_in_desc
.bEndpointAddress
;
208 gser_hs_out_desc
.bEndpointAddress
=
209 gser_fs_out_desc
.bEndpointAddress
;
211 /* copy descriptors, and track endpoint copies */
212 f
->hs_descriptors
= usb_copy_descriptors(gser_hs_function
);
214 gser
->hs
.in
= usb_find_endpoint(gser_hs_function
,
215 f
->hs_descriptors
, &gser_hs_in_desc
);
216 gser
->hs
.out
= usb_find_endpoint(gser_hs_function
,
217 f
->hs_descriptors
, &gser_hs_out_desc
);
220 DBG(cdev
, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
222 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
223 gser
->port
.in
->name
, gser
->port
.out
->name
);
227 /* we might as well release our claims on endpoints */
229 gser
->port
.out
->driver_data
= NULL
;
231 gser
->port
.in
->driver_data
= NULL
;
233 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
239 gser_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
241 if (gadget_is_dualspeed(c
->cdev
->gadget
))
242 usb_free_descriptors(f
->hs_descriptors
);
243 usb_free_descriptors(f
->descriptors
);
244 kfree(func_to_gser(f
));
248 * gser_bind_config - add a generic serial function to a configuration
249 * @c: the configuration to support the serial instance
250 * @port_num: /dev/ttyGS* port this interface will use
251 * Context: single threaded during gadget setup
253 * Returns zero on success, else negative errno.
255 * Caller must have called @gserial_setup() with enough ports to
256 * handle all the ones it binds. Caller is also responsible
257 * for calling @gserial_cleanup() before module unload.
259 int __init
gser_bind_config(struct usb_configuration
*c
, u8 port_num
)
264 /* REVISIT might want instance-specific strings to help
265 * distinguish instances ...
268 /* maybe allocate device-global string ID */
269 if (gser_string_defs
[0].id
== 0) {
270 status
= usb_string_id(c
->cdev
);
273 gser_string_defs
[0].id
= status
;
276 /* allocate and initialize one new instance */
277 gser
= kzalloc(sizeof *gser
, GFP_KERNEL
);
281 gser
->port_num
= port_num
;
283 gser
->port
.func
.name
= "gser";
284 gser
->port
.func
.strings
= gser_strings
;
285 gser
->port
.func
.bind
= gser_bind
;
286 gser
->port
.func
.unbind
= gser_unbind
;
287 gser
->port
.func
.set_alt
= gser_set_alt
;
288 gser
->port
.func
.disable
= gser_disable
;
290 status
= usb_add_function(c
, &gser
->port
.func
);