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/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
19 #include "gadget_chips.h"
23 * This function packages a simple "generic serial" port with no real
24 * control mechanisms, just raw data transfer over two bulk endpoints.
26 * Because it's not standardized, this isn't as interoperable as the
27 * CDC ACM driver. However, for many purposes it's just as functional
28 * if you can arrange appropriate host side drivers.
37 static inline struct f_gser
*func_to_gser(struct usb_function
*f
)
39 return container_of(f
, struct f_gser
, port
.func
);
42 /*-------------------------------------------------------------------------*/
44 /* interface descriptor: */
46 static struct usb_interface_descriptor gser_interface_desc
= {
47 .bLength
= USB_DT_INTERFACE_SIZE
,
48 .bDescriptorType
= USB_DT_INTERFACE
,
49 /* .bInterfaceNumber = DYNAMIC */
51 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
52 .bInterfaceSubClass
= 0,
53 .bInterfaceProtocol
= 0,
54 /* .iInterface = DYNAMIC */
57 /* full speed support: */
59 static struct usb_endpoint_descriptor gser_fs_in_desc
= {
60 .bLength
= USB_DT_ENDPOINT_SIZE
,
61 .bDescriptorType
= USB_DT_ENDPOINT
,
62 .bEndpointAddress
= USB_DIR_IN
,
63 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
66 static struct usb_endpoint_descriptor gser_fs_out_desc
= {
67 .bLength
= USB_DT_ENDPOINT_SIZE
,
68 .bDescriptorType
= USB_DT_ENDPOINT
,
69 .bEndpointAddress
= USB_DIR_OUT
,
70 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
73 static struct usb_descriptor_header
*gser_fs_function
[] = {
74 (struct usb_descriptor_header
*) &gser_interface_desc
,
75 (struct usb_descriptor_header
*) &gser_fs_in_desc
,
76 (struct usb_descriptor_header
*) &gser_fs_out_desc
,
80 /* high speed support: */
82 static struct usb_endpoint_descriptor gser_hs_in_desc
= {
83 .bLength
= USB_DT_ENDPOINT_SIZE
,
84 .bDescriptorType
= USB_DT_ENDPOINT
,
85 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
86 .wMaxPacketSize
= cpu_to_le16(512),
89 static struct usb_endpoint_descriptor gser_hs_out_desc
= {
90 .bLength
= USB_DT_ENDPOINT_SIZE
,
91 .bDescriptorType
= USB_DT_ENDPOINT
,
92 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
93 .wMaxPacketSize
= cpu_to_le16(512),
96 static struct usb_descriptor_header
*gser_hs_function
[] = {
97 (struct usb_descriptor_header
*) &gser_interface_desc
,
98 (struct usb_descriptor_header
*) &gser_hs_in_desc
,
99 (struct usb_descriptor_header
*) &gser_hs_out_desc
,
103 static struct usb_endpoint_descriptor gser_ss_in_desc
= {
104 .bLength
= USB_DT_ENDPOINT_SIZE
,
105 .bDescriptorType
= USB_DT_ENDPOINT
,
106 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
107 .wMaxPacketSize
= cpu_to_le16(1024),
110 static struct usb_endpoint_descriptor gser_ss_out_desc
= {
111 .bLength
= USB_DT_ENDPOINT_SIZE
,
112 .bDescriptorType
= USB_DT_ENDPOINT
,
113 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
114 .wMaxPacketSize
= cpu_to_le16(1024),
117 static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc
= {
118 .bLength
= sizeof gser_ss_bulk_comp_desc
,
119 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
122 static struct usb_descriptor_header
*gser_ss_function
[] = {
123 (struct usb_descriptor_header
*) &gser_interface_desc
,
124 (struct usb_descriptor_header
*) &gser_ss_in_desc
,
125 (struct usb_descriptor_header
*) &gser_ss_bulk_comp_desc
,
126 (struct usb_descriptor_header
*) &gser_ss_out_desc
,
127 (struct usb_descriptor_header
*) &gser_ss_bulk_comp_desc
,
131 /* string descriptors: */
133 static struct usb_string gser_string_defs
[] = {
134 [0].s
= "Generic Serial",
135 { } /* end of list */
138 static struct usb_gadget_strings gser_string_table
= {
139 .language
= 0x0409, /* en-us */
140 .strings
= gser_string_defs
,
143 static struct usb_gadget_strings
*gser_strings
[] = {
148 /*-------------------------------------------------------------------------*/
150 static int gser_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
152 struct f_gser
*gser
= func_to_gser(f
);
153 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
155 /* we know alt == 0, so this is an activation or a reset */
157 if (gser
->port
.in
->driver_data
) {
158 DBG(cdev
, "reset generic ttyGS%d\n", gser
->port_num
);
159 gserial_disconnect(&gser
->port
);
161 if (!gser
->port
.in
->desc
|| !gser
->port
.out
->desc
) {
162 DBG(cdev
, "activate generic ttyGS%d\n", gser
->port_num
);
163 if (config_ep_by_speed(cdev
->gadget
, f
, gser
->port
.in
) ||
164 config_ep_by_speed(cdev
->gadget
, f
, gser
->port
.out
)) {
165 gser
->port
.in
->desc
= NULL
;
166 gser
->port
.out
->desc
= NULL
;
170 gserial_connect(&gser
->port
, gser
->port_num
);
174 static void gser_disable(struct usb_function
*f
)
176 struct f_gser
*gser
= func_to_gser(f
);
177 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
179 DBG(cdev
, "generic ttyGS%d deactivated\n", gser
->port_num
);
180 gserial_disconnect(&gser
->port
);
183 /*-------------------------------------------------------------------------*/
185 /* serial function driver setup/binding */
187 static int gser_bind(struct usb_configuration
*c
, struct usb_function
*f
)
189 struct usb_composite_dev
*cdev
= c
->cdev
;
190 struct f_gser
*gser
= func_to_gser(f
);
194 /* REVISIT might want instance-specific strings to help
195 * distinguish instances ...
198 /* maybe allocate device-global string ID */
199 if (gser_string_defs
[0].id
== 0) {
200 status
= usb_string_id(c
->cdev
);
203 gser_string_defs
[0].id
= status
;
206 /* allocate instance-specific interface IDs */
207 status
= usb_interface_id(c
, f
);
210 gser
->data_id
= status
;
211 gser_interface_desc
.bInterfaceNumber
= status
;
215 /* allocate instance-specific endpoints */
216 ep
= usb_ep_autoconfig(cdev
->gadget
, &gser_fs_in_desc
);
220 ep
->driver_data
= cdev
; /* claim */
222 ep
= usb_ep_autoconfig(cdev
->gadget
, &gser_fs_out_desc
);
226 ep
->driver_data
= cdev
; /* claim */
228 /* support all relevant hardware speeds... we expect that when
229 * hardware is dual speed, all bulk-capable endpoints work at
232 gser_hs_in_desc
.bEndpointAddress
= gser_fs_in_desc
.bEndpointAddress
;
233 gser_hs_out_desc
.bEndpointAddress
= gser_fs_out_desc
.bEndpointAddress
;
235 gser_ss_in_desc
.bEndpointAddress
= gser_fs_in_desc
.bEndpointAddress
;
236 gser_ss_out_desc
.bEndpointAddress
= gser_fs_out_desc
.bEndpointAddress
;
238 status
= usb_assign_descriptors(f
, gser_fs_function
, gser_hs_function
,
242 DBG(cdev
, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
244 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
245 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
246 gser
->port
.in
->name
, gser
->port
.out
->name
);
250 /* we might as well release our claims on endpoints */
252 gser
->port
.out
->driver_data
= NULL
;
254 gser
->port
.in
->driver_data
= NULL
;
256 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
261 static inline struct f_serial_opts
*to_f_serial_opts(struct config_item
*item
)
263 return container_of(to_config_group(item
), struct f_serial_opts
,
267 CONFIGFS_ATTR_STRUCT(f_serial_opts
);
268 static ssize_t
f_serial_attr_show(struct config_item
*item
,
269 struct configfs_attribute
*attr
,
272 struct f_serial_opts
*opts
= to_f_serial_opts(item
);
273 struct f_serial_opts_attribute
*f_serial_opts_attr
=
274 container_of(attr
, struct f_serial_opts_attribute
, attr
);
277 if (f_serial_opts_attr
->show
)
278 ret
= f_serial_opts_attr
->show(opts
, page
);
283 static void serial_attr_release(struct config_item
*item
)
285 struct f_serial_opts
*opts
= to_f_serial_opts(item
);
287 usb_put_function_instance(&opts
->func_inst
);
290 static struct configfs_item_operations serial_item_ops
= {
291 .release
= serial_attr_release
,
292 .show_attribute
= f_serial_attr_show
,
295 static ssize_t
f_serial_port_num_show(struct f_serial_opts
*opts
, char *page
)
297 return sprintf(page
, "%u\n", opts
->port_num
);
300 static struct f_serial_opts_attribute f_serial_port_num
=
301 __CONFIGFS_ATTR_RO(port_num
, f_serial_port_num_show
);
303 static struct configfs_attribute
*acm_attrs
[] = {
304 &f_serial_port_num
.attr
,
308 static struct config_item_type serial_func_type
= {
309 .ct_item_ops
= &serial_item_ops
,
310 .ct_attrs
= acm_attrs
,
311 .ct_owner
= THIS_MODULE
,
314 static void gser_free_inst(struct usb_function_instance
*f
)
316 struct f_serial_opts
*opts
;
318 opts
= container_of(f
, struct f_serial_opts
, func_inst
);
319 gserial_free_line(opts
->port_num
);
323 static struct usb_function_instance
*gser_alloc_inst(void)
325 struct f_serial_opts
*opts
;
328 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
330 return ERR_PTR(-ENOMEM
);
332 opts
->func_inst
.free_func_inst
= gser_free_inst
;
333 ret
= gserial_alloc_line(&opts
->port_num
);
338 config_group_init_type_name(&opts
->func_inst
.group
, "",
341 return &opts
->func_inst
;
344 static void gser_free(struct usb_function
*f
)
346 struct f_gser
*serial
;
348 serial
= func_to_gser(f
);
352 static void gser_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
354 usb_free_all_descriptors(f
);
357 static struct usb_function
*gser_alloc(struct usb_function_instance
*fi
)
360 struct f_serial_opts
*opts
;
362 /* allocate and initialize one new instance */
363 gser
= kzalloc(sizeof(*gser
), GFP_KERNEL
);
365 return ERR_PTR(-ENOMEM
);
367 opts
= container_of(fi
, struct f_serial_opts
, func_inst
);
369 gser
->port_num
= opts
->port_num
;
371 gser
->port
.func
.name
= "gser";
372 gser
->port
.func
.strings
= gser_strings
;
373 gser
->port
.func
.bind
= gser_bind
;
374 gser
->port
.func
.unbind
= gser_unbind
;
375 gser
->port
.func
.set_alt
= gser_set_alt
;
376 gser
->port
.func
.disable
= gser_disable
;
377 gser
->port
.func
.free_func
= gser_free
;
379 return &gser
->port
.func
;
382 DECLARE_USB_FUNCTION_INIT(gser
, gser_alloc_inst
, gser_alloc
);
383 MODULE_LICENSE("GPL");
384 MODULE_AUTHOR("Al Borchers");
385 MODULE_AUTHOR("David Brownell");