]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/gadget/function/f_serial.c
HID: sony: Remove the size check for the Dualshock 4 HID Descriptor
[mirror_ubuntu-artful-kernel.git] / drivers / usb / gadget / function / f_serial.c
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
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17
18 #include "u_serial.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
30 struct f_gser {
31 struct gserial port;
32 u8 data_id;
33 u8 port_num;
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 = {
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 = {
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 = {
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[] = {
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 = {
82 .bLength = USB_DT_ENDPOINT_SIZE,
83 .bDescriptorType = USB_DT_ENDPOINT,
84 .bmAttributes = USB_ENDPOINT_XFER_BULK,
85 .wMaxPacketSize = cpu_to_le16(512),
86 };
87
88 static struct usb_endpoint_descriptor gser_hs_out_desc = {
89 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92 .wMaxPacketSize = cpu_to_le16(512),
93 };
94
95 static struct usb_descriptor_header *gser_hs_function[] = {
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
102 static struct usb_endpoint_descriptor gser_ss_in_desc = {
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 = {
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 = {
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[] = {
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
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->enabled) {
157 dev_dbg(&cdev->gadget->dev,
158 "reset generic ttyGS%d\n", gser->port_num);
159 gserial_disconnect(&gser->port);
160 }
161 if (!gser->port.in->desc || !gser->port.out->desc) {
162 dev_dbg(&cdev->gadget->dev,
163 "activate generic ttyGS%d\n", gser->port_num);
164 if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
165 config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
166 gser->port.in->desc = NULL;
167 gser->port.out->desc = NULL;
168 return -EINVAL;
169 }
170 }
171 gserial_connect(&gser->port, gser->port_num);
172 return 0;
173 }
174
175 static void gser_disable(struct usb_function *f)
176 {
177 struct f_gser *gser = func_to_gser(f);
178 struct usb_composite_dev *cdev = f->config->cdev;
179
180 dev_dbg(&cdev->gadget->dev,
181 "generic ttyGS%d deactivated\n", gser->port_num);
182 gserial_disconnect(&gser->port);
183 }
184
185 /*-------------------------------------------------------------------------*/
186
187 /* serial function driver setup/binding */
188
189 static int gser_bind(struct usb_configuration *c, struct usb_function *f)
190 {
191 struct usb_composite_dev *cdev = c->cdev;
192 struct f_gser *gser = func_to_gser(f);
193 int status;
194 struct usb_ep *ep;
195
196 /* REVISIT might want instance-specific strings to help
197 * distinguish instances ...
198 */
199
200 /* maybe allocate device-global string ID */
201 if (gser_string_defs[0].id == 0) {
202 status = usb_string_id(c->cdev);
203 if (status < 0)
204 return status;
205 gser_string_defs[0].id = status;
206 }
207
208 /* allocate instance-specific interface IDs */
209 status = usb_interface_id(c, f);
210 if (status < 0)
211 goto fail;
212 gser->data_id = status;
213 gser_interface_desc.bInterfaceNumber = status;
214
215 status = -ENODEV;
216
217 /* allocate instance-specific endpoints */
218 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
219 if (!ep)
220 goto fail;
221 gser->port.in = ep;
222
223 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
224 if (!ep)
225 goto fail;
226 gser->port.out = ep;
227
228 /* support all relevant hardware speeds... we expect that when
229 * hardware is dual speed, all bulk-capable endpoints work at
230 * both speeds
231 */
232 gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
233 gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
234
235 gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
236 gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
237
238 status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
239 gser_ss_function);
240 if (status)
241 goto fail;
242 dev_dbg(&cdev->gadget->dev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
243 gser->port_num,
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);
247 return 0;
248
249 fail:
250 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
251
252 return status;
253 }
254
255 static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
256 {
257 return container_of(to_config_group(item), struct f_serial_opts,
258 func_inst.group);
259 }
260
261 CONFIGFS_ATTR_STRUCT(f_serial_opts);
262 static ssize_t f_serial_attr_show(struct config_item *item,
263 struct configfs_attribute *attr,
264 char *page)
265 {
266 struct f_serial_opts *opts = to_f_serial_opts(item);
267 struct f_serial_opts_attribute *f_serial_opts_attr =
268 container_of(attr, struct f_serial_opts_attribute, attr);
269 ssize_t ret = 0;
270
271 if (f_serial_opts_attr->show)
272 ret = f_serial_opts_attr->show(opts, page);
273
274 return ret;
275 }
276
277 static void serial_attr_release(struct config_item *item)
278 {
279 struct f_serial_opts *opts = to_f_serial_opts(item);
280
281 usb_put_function_instance(&opts->func_inst);
282 }
283
284 static struct configfs_item_operations serial_item_ops = {
285 .release = serial_attr_release,
286 .show_attribute = f_serial_attr_show,
287 };
288
289 static ssize_t f_serial_port_num_show(struct f_serial_opts *opts, char *page)
290 {
291 return sprintf(page, "%u\n", opts->port_num);
292 }
293
294 static struct f_serial_opts_attribute f_serial_port_num =
295 __CONFIGFS_ATTR_RO(port_num, f_serial_port_num_show);
296
297 static struct configfs_attribute *acm_attrs[] = {
298 &f_serial_port_num.attr,
299 NULL,
300 };
301
302 static struct config_item_type serial_func_type = {
303 .ct_item_ops = &serial_item_ops,
304 .ct_attrs = acm_attrs,
305 .ct_owner = THIS_MODULE,
306 };
307
308 static void gser_free_inst(struct usb_function_instance *f)
309 {
310 struct f_serial_opts *opts;
311
312 opts = container_of(f, struct f_serial_opts, func_inst);
313 gserial_free_line(opts->port_num);
314 kfree(opts);
315 }
316
317 static struct usb_function_instance *gser_alloc_inst(void)
318 {
319 struct f_serial_opts *opts;
320 int ret;
321
322 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
323 if (!opts)
324 return ERR_PTR(-ENOMEM);
325
326 opts->func_inst.free_func_inst = gser_free_inst;
327 ret = gserial_alloc_line(&opts->port_num);
328 if (ret) {
329 kfree(opts);
330 return ERR_PTR(ret);
331 }
332 config_group_init_type_name(&opts->func_inst.group, "",
333 &serial_func_type);
334
335 return &opts->func_inst;
336 }
337
338 static void gser_free(struct usb_function *f)
339 {
340 struct f_gser *serial;
341
342 serial = func_to_gser(f);
343 kfree(serial);
344 }
345
346 static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
347 {
348 usb_free_all_descriptors(f);
349 }
350
351 static struct usb_function *gser_alloc(struct usb_function_instance *fi)
352 {
353 struct f_gser *gser;
354 struct f_serial_opts *opts;
355
356 /* allocate and initialize one new instance */
357 gser = kzalloc(sizeof(*gser), GFP_KERNEL);
358 if (!gser)
359 return ERR_PTR(-ENOMEM);
360
361 opts = container_of(fi, struct f_serial_opts, func_inst);
362
363 gser->port_num = opts->port_num;
364
365 gser->port.func.name = "gser";
366 gser->port.func.strings = gser_strings;
367 gser->port.func.bind = gser_bind;
368 gser->port.func.unbind = gser_unbind;
369 gser->port.func.set_alt = gser_set_alt;
370 gser->port.func.disable = gser_disable;
371 gser->port.func.free_func = gser_free;
372
373 return &gser->port.func;
374 }
375
376 DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
377 MODULE_LICENSE("GPL");
378 MODULE_AUTHOR("Al Borchers");
379 MODULE_AUTHOR("David Brownell");