]>
Commit | Line | Data |
---|---|---|
61d8baea DB |
1 | /* |
2 | * f_serial.c - generic USB serial function driver | |
3 | * | |
4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) | |
5 | * Copyright (C) 2008 by David Brownell | |
6 | * Copyright (C) 2008 by Nokia Corporation | |
7 | * | |
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. | |
11 | */ | |
12 | ||
5a0e3ad6 | 13 | #include <linux/slab.h> |
61d8baea DB |
14 | #include <linux/kernel.h> |
15 | #include <linux/device.h> | |
16 | ||
17 | #include "u_serial.h" | |
18 | #include "gadget_chips.h" | |
19 | ||
20 | ||
21 | /* | |
22 | * This function packages a simple "generic serial" port with no real | |
23 | * control mechanisms, just raw data transfer over two bulk endpoints. | |
24 | * | |
25 | * Because it's not standardized, this isn't as interoperable as the | |
26 | * CDC ACM driver. However, for many purposes it's just as functional | |
27 | * if you can arrange appropriate host side drivers. | |
28 | */ | |
29 | ||
61d8baea DB |
30 | struct f_gser { |
31 | struct gserial port; | |
32 | u8 data_id; | |
33 | u8 port_num; | |
61d8baea DB |
34 | }; |
35 | ||
36 | static inline struct f_gser *func_to_gser(struct usb_function *f) | |
37 | { | |
38 | return container_of(f, struct f_gser, port.func); | |
39 | } | |
40 | ||
41 | /*-------------------------------------------------------------------------*/ | |
42 | ||
43 | /* interface descriptor: */ | |
44 | ||
45 | static struct usb_interface_descriptor gser_interface_desc __initdata = { | |
46 | .bLength = USB_DT_INTERFACE_SIZE, | |
47 | .bDescriptorType = USB_DT_INTERFACE, | |
48 | /* .bInterfaceNumber = DYNAMIC */ | |
49 | .bNumEndpoints = 2, | |
50 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
51 | .bInterfaceSubClass = 0, | |
52 | .bInterfaceProtocol = 0, | |
53 | /* .iInterface = DYNAMIC */ | |
54 | }; | |
55 | ||
56 | /* full speed support: */ | |
57 | ||
58 | static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = { | |
59 | .bLength = USB_DT_ENDPOINT_SIZE, | |
60 | .bDescriptorType = USB_DT_ENDPOINT, | |
61 | .bEndpointAddress = USB_DIR_IN, | |
62 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
63 | }; | |
64 | ||
65 | static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = { | |
66 | .bLength = USB_DT_ENDPOINT_SIZE, | |
67 | .bDescriptorType = USB_DT_ENDPOINT, | |
68 | .bEndpointAddress = USB_DIR_OUT, | |
69 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
70 | }; | |
71 | ||
72 | static struct usb_descriptor_header *gser_fs_function[] __initdata = { | |
73 | (struct usb_descriptor_header *) &gser_interface_desc, | |
74 | (struct usb_descriptor_header *) &gser_fs_in_desc, | |
75 | (struct usb_descriptor_header *) &gser_fs_out_desc, | |
76 | NULL, | |
77 | }; | |
78 | ||
79 | /* high speed support: */ | |
80 | ||
81 | static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = { | |
82 | .bLength = USB_DT_ENDPOINT_SIZE, | |
83 | .bDescriptorType = USB_DT_ENDPOINT, | |
84 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 85 | .wMaxPacketSize = cpu_to_le16(512), |
61d8baea DB |
86 | }; |
87 | ||
88 | static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = { | |
89 | .bLength = USB_DT_ENDPOINT_SIZE, | |
90 | .bDescriptorType = USB_DT_ENDPOINT, | |
91 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 92 | .wMaxPacketSize = cpu_to_le16(512), |
61d8baea DB |
93 | }; |
94 | ||
95 | static struct usb_descriptor_header *gser_hs_function[] __initdata = { | |
96 | (struct usb_descriptor_header *) &gser_interface_desc, | |
97 | (struct usb_descriptor_header *) &gser_hs_in_desc, | |
98 | (struct usb_descriptor_header *) &gser_hs_out_desc, | |
99 | NULL, | |
100 | }; | |
101 | ||
6fecfb05 SAS |
102 | static struct usb_endpoint_descriptor gser_ss_in_desc __initdata = { |
103 | .bLength = USB_DT_ENDPOINT_SIZE, | |
104 | .bDescriptorType = USB_DT_ENDPOINT, | |
105 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
106 | .wMaxPacketSize = cpu_to_le16(1024), | |
107 | }; | |
108 | ||
109 | static struct usb_endpoint_descriptor gser_ss_out_desc __initdata = { | |
110 | .bLength = USB_DT_ENDPOINT_SIZE, | |
111 | .bDescriptorType = USB_DT_ENDPOINT, | |
112 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
113 | .wMaxPacketSize = cpu_to_le16(1024), | |
114 | }; | |
115 | ||
116 | static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc __initdata = { | |
117 | .bLength = sizeof gser_ss_bulk_comp_desc, | |
118 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
119 | }; | |
120 | ||
121 | static struct usb_descriptor_header *gser_ss_function[] __initdata = { | |
122 | (struct usb_descriptor_header *) &gser_interface_desc, | |
123 | (struct usb_descriptor_header *) &gser_ss_in_desc, | |
124 | (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc, | |
125 | (struct usb_descriptor_header *) &gser_ss_out_desc, | |
126 | (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc, | |
127 | NULL, | |
128 | }; | |
129 | ||
61d8baea DB |
130 | /* string descriptors: */ |
131 | ||
132 | static struct usb_string gser_string_defs[] = { | |
133 | [0].s = "Generic Serial", | |
134 | { } /* end of list */ | |
135 | }; | |
136 | ||
137 | static struct usb_gadget_strings gser_string_table = { | |
138 | .language = 0x0409, /* en-us */ | |
139 | .strings = gser_string_defs, | |
140 | }; | |
141 | ||
142 | static struct usb_gadget_strings *gser_strings[] = { | |
143 | &gser_string_table, | |
144 | NULL, | |
145 | }; | |
146 | ||
147 | /*-------------------------------------------------------------------------*/ | |
148 | ||
149 | static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | |
150 | { | |
151 | struct f_gser *gser = func_to_gser(f); | |
152 | struct usb_composite_dev *cdev = f->config->cdev; | |
153 | ||
154 | /* we know alt == 0, so this is an activation or a reset */ | |
155 | ||
156 | if (gser->port.in->driver_data) { | |
157 | DBG(cdev, "reset generic ttyGS%d\n", gser->port_num); | |
158 | gserial_disconnect(&gser->port); | |
ea2a1df7 TB |
159 | } |
160 | if (!gser->port.in->desc || !gser->port.out->desc) { | |
61d8baea | 161 | DBG(cdev, "activate generic ttyGS%d\n", gser->port_num); |
fef69644 RJ |
162 | if (config_ep_by_speed(cdev->gadget, f, gser->port.in) || |
163 | config_ep_by_speed(cdev->gadget, f, gser->port.out)) { | |
ea2a1df7 TB |
164 | gser->port.in->desc = NULL; |
165 | gser->port.out->desc = NULL; | |
166 | return -EINVAL; | |
167 | } | |
61d8baea DB |
168 | } |
169 | gserial_connect(&gser->port, gser->port_num); | |
170 | return 0; | |
171 | } | |
172 | ||
173 | static void gser_disable(struct usb_function *f) | |
174 | { | |
175 | struct f_gser *gser = func_to_gser(f); | |
176 | struct usb_composite_dev *cdev = f->config->cdev; | |
177 | ||
178 | DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num); | |
179 | gserial_disconnect(&gser->port); | |
180 | } | |
181 | ||
182 | /*-------------------------------------------------------------------------*/ | |
183 | ||
184 | /* serial function driver setup/binding */ | |
185 | ||
186 | static int __init | |
187 | gser_bind(struct usb_configuration *c, struct usb_function *f) | |
188 | { | |
189 | struct usb_composite_dev *cdev = c->cdev; | |
190 | struct f_gser *gser = func_to_gser(f); | |
191 | int status; | |
192 | struct usb_ep *ep; | |
193 | ||
194 | /* allocate instance-specific interface IDs */ | |
195 | status = usb_interface_id(c, f); | |
196 | if (status < 0) | |
197 | goto fail; | |
198 | gser->data_id = status; | |
199 | gser_interface_desc.bInterfaceNumber = status; | |
200 | ||
201 | status = -ENODEV; | |
202 | ||
203 | /* allocate instance-specific endpoints */ | |
204 | ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc); | |
205 | if (!ep) | |
206 | goto fail; | |
207 | gser->port.in = ep; | |
208 | ep->driver_data = cdev; /* claim */ | |
209 | ||
210 | ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc); | |
211 | if (!ep) | |
212 | goto fail; | |
213 | gser->port.out = ep; | |
214 | ep->driver_data = cdev; /* claim */ | |
215 | ||
61d8baea DB |
216 | /* support all relevant hardware speeds... we expect that when |
217 | * hardware is dual speed, all bulk-capable endpoints work at | |
218 | * both speeds | |
219 | */ | |
10287bae SAS |
220 | gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress; |
221 | gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress; | |
61d8baea | 222 | |
10287bae SAS |
223 | gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress; |
224 | gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress; | |
225 | ||
226 | status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function, | |
227 | gser_ss_function); | |
228 | if (status) | |
229 | goto fail; | |
61d8baea DB |
230 | DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n", |
231 | gser->port_num, | |
6fecfb05 | 232 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
61d8baea DB |
233 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
234 | gser->port.in->name, gser->port.out->name); | |
235 | return 0; | |
236 | ||
237 | fail: | |
238 | /* we might as well release our claims on endpoints */ | |
239 | if (gser->port.out) | |
240 | gser->port.out->driver_data = NULL; | |
241 | if (gser->port.in) | |
242 | gser->port.in->driver_data = NULL; | |
243 | ||
244 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | |
245 | ||
246 | return status; | |
247 | } | |
248 | ||
249 | static void | |
250 | gser_unbind(struct usb_configuration *c, struct usb_function *f) | |
251 | { | |
10287bae | 252 | usb_free_all_descriptors(f); |
61d8baea DB |
253 | kfree(func_to_gser(f)); |
254 | } | |
255 | ||
256 | /** | |
257 | * gser_bind_config - add a generic serial function to a configuration | |
258 | * @c: the configuration to support the serial instance | |
259 | * @port_num: /dev/ttyGS* port this interface will use | |
260 | * Context: single threaded during gadget setup | |
261 | * | |
262 | * Returns zero on success, else negative errno. | |
263 | * | |
264 | * Caller must have called @gserial_setup() with enough ports to | |
265 | * handle all the ones it binds. Caller is also responsible | |
266 | * for calling @gserial_cleanup() before module unload. | |
267 | */ | |
268 | int __init gser_bind_config(struct usb_configuration *c, u8 port_num) | |
269 | { | |
270 | struct f_gser *gser; | |
271 | int status; | |
272 | ||
273 | /* REVISIT might want instance-specific strings to help | |
274 | * distinguish instances ... | |
275 | */ | |
276 | ||
277 | /* maybe allocate device-global string ID */ | |
278 | if (gser_string_defs[0].id == 0) { | |
279 | status = usb_string_id(c->cdev); | |
280 | if (status < 0) | |
281 | return status; | |
282 | gser_string_defs[0].id = status; | |
283 | } | |
284 | ||
285 | /* allocate and initialize one new instance */ | |
286 | gser = kzalloc(sizeof *gser, GFP_KERNEL); | |
287 | if (!gser) | |
288 | return -ENOMEM; | |
289 | ||
290 | gser->port_num = port_num; | |
291 | ||
292 | gser->port.func.name = "gser"; | |
293 | gser->port.func.strings = gser_strings; | |
294 | gser->port.func.bind = gser_bind; | |
295 | gser->port.func.unbind = gser_unbind; | |
296 | gser->port.func.set_alt = gser_set_alt; | |
297 | gser->port.func.disable = gser_disable; | |
298 | ||
299 | status = usb_add_function(c, &gser->port.func); | |
300 | if (status) | |
301 | kfree(gser); | |
302 | return status; | |
303 | } |