]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/usb/gadget/f_hid.c
usb: gadget: add usb_endpoint_descriptor to struct usb_ep
[mirror_ubuntu-hirsute-kernel.git] / drivers / usb / gadget / f_hid.c
CommitLineData
71adf118
FC
1/*
2 * f_hid.c -- USB HID function driver
3 *
4 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
22#include <linux/utsname.h>
23#include <linux/module.h>
24#include <linux/hid.h>
25#include <linux/cdev.h>
26#include <linux/mutex.h>
27#include <linux/poll.h>
71adf118
FC
28#include <linux/uaccess.h>
29#include <linux/wait.h>
30#include <linux/usb/g_hid.h>
31
32static int major, minors;
33static struct class *hidg_class;
34
35/*-------------------------------------------------------------------------*/
36/* HID gadget struct */
37
38struct f_hidg {
39 /* configuration */
40 unsigned char bInterfaceSubClass;
41 unsigned char bInterfaceProtocol;
42 unsigned short report_desc_length;
43 char *report_desc;
44 unsigned short report_length;
45
46 /* recv report */
47 char *set_report_buff;
48 unsigned short set_report_length;
49 spinlock_t spinlock;
50 wait_queue_head_t read_queue;
51
52 /* send report */
53 struct mutex lock;
54 bool write_pending;
55 wait_queue_head_t write_queue;
56 struct usb_request *req;
57
58 int minor;
59 struct cdev cdev;
60 struct usb_function func;
61 struct usb_ep *in_ep;
62 struct usb_endpoint_descriptor *fs_in_ep_desc;
63 struct usb_endpoint_descriptor *hs_in_ep_desc;
64};
65
66static inline struct f_hidg *func_to_hidg(struct usb_function *f)
67{
68 return container_of(f, struct f_hidg, func);
69}
70
71/*-------------------------------------------------------------------------*/
72/* Static descriptors */
73
74static struct usb_interface_descriptor hidg_interface_desc = {
75 .bLength = sizeof hidg_interface_desc,
76 .bDescriptorType = USB_DT_INTERFACE,
77 /* .bInterfaceNumber = DYNAMIC */
78 .bAlternateSetting = 0,
79 .bNumEndpoints = 1,
80 .bInterfaceClass = USB_CLASS_HID,
81 /* .bInterfaceSubClass = DYNAMIC */
82 /* .bInterfaceProtocol = DYNAMIC */
83 /* .iInterface = DYNAMIC */
84};
85
86static struct hid_descriptor hidg_desc = {
87 .bLength = sizeof hidg_desc,
88 .bDescriptorType = HID_DT_HID,
89 .bcdHID = 0x0101,
90 .bCountryCode = 0x00,
91 .bNumDescriptors = 0x1,
92 /*.desc[0].bDescriptorType = DYNAMIC */
93 /*.desc[0].wDescriptorLenght = DYNAMIC */
94};
95
96/* High-Speed Support */
97
98static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
99 .bLength = USB_DT_ENDPOINT_SIZE,
100 .bDescriptorType = USB_DT_ENDPOINT,
101 .bEndpointAddress = USB_DIR_IN,
102 .bmAttributes = USB_ENDPOINT_XFER_INT,
103 /*.wMaxPacketSize = DYNAMIC */
104 .bInterval = 4, /* FIXME: Add this field in the
105 * HID gadget configuration?
106 * (struct hidg_func_descriptor)
107 */
108};
109
110static struct usb_descriptor_header *hidg_hs_descriptors[] = {
111 (struct usb_descriptor_header *)&hidg_interface_desc,
112 (struct usb_descriptor_header *)&hidg_desc,
113 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
114 NULL,
115};
116
117/* Full-Speed Support */
118
119static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
120 .bLength = USB_DT_ENDPOINT_SIZE,
121 .bDescriptorType = USB_DT_ENDPOINT,
122 .bEndpointAddress = USB_DIR_IN,
123 .bmAttributes = USB_ENDPOINT_XFER_INT,
124 /*.wMaxPacketSize = DYNAMIC */
125 .bInterval = 10, /* FIXME: Add this field in the
126 * HID gadget configuration?
127 * (struct hidg_func_descriptor)
128 */
129};
130
131static struct usb_descriptor_header *hidg_fs_descriptors[] = {
132 (struct usb_descriptor_header *)&hidg_interface_desc,
133 (struct usb_descriptor_header *)&hidg_desc,
134 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
135 NULL,
136};
137
138/*-------------------------------------------------------------------------*/
139/* Char Device */
140
141static ssize_t f_hidg_read(struct file *file, char __user *buffer,
142 size_t count, loff_t *ptr)
143{
fd63b10b 144 struct f_hidg *hidg = file->private_data;
71adf118
FC
145 char *tmp_buff = NULL;
146 unsigned long flags;
147
148 if (!count)
149 return 0;
150
151 if (!access_ok(VERIFY_WRITE, buffer, count))
152 return -EFAULT;
153
154 spin_lock_irqsave(&hidg->spinlock, flags);
155
156#define READ_COND (hidg->set_report_buff != NULL)
157
158 while (!READ_COND) {
159 spin_unlock_irqrestore(&hidg->spinlock, flags);
160 if (file->f_flags & O_NONBLOCK)
161 return -EAGAIN;
162
163 if (wait_event_interruptible(hidg->read_queue, READ_COND))
164 return -ERESTARTSYS;
165
166 spin_lock_irqsave(&hidg->spinlock, flags);
167 }
168
169
170 count = min_t(unsigned, count, hidg->set_report_length);
171 tmp_buff = hidg->set_report_buff;
172 hidg->set_report_buff = NULL;
173
174 spin_unlock_irqrestore(&hidg->spinlock, flags);
175
176 if (tmp_buff != NULL) {
177 /* copy to user outside spinlock */
178 count -= copy_to_user(buffer, tmp_buff, count);
179 kfree(tmp_buff);
180 } else
181 count = -ENOMEM;
182
183 return count;
184}
185
186static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
187{
188 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
189
190 if (req->status != 0) {
191 ERROR(hidg->func.config->cdev,
192 "End Point Request ERROR: %d\n", req->status);
193 }
194
195 hidg->write_pending = 0;
196 wake_up(&hidg->write_queue);
197}
198
199static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
200 size_t count, loff_t *offp)
201{
fd63b10b 202 struct f_hidg *hidg = file->private_data;
71adf118
FC
203 ssize_t status = -ENOMEM;
204
205 if (!access_ok(VERIFY_READ, buffer, count))
206 return -EFAULT;
207
208 mutex_lock(&hidg->lock);
209
210#define WRITE_COND (!hidg->write_pending)
211
212 /* write queue */
213 while (!WRITE_COND) {
214 mutex_unlock(&hidg->lock);
215 if (file->f_flags & O_NONBLOCK)
216 return -EAGAIN;
217
218 if (wait_event_interruptible_exclusive(
219 hidg->write_queue, WRITE_COND))
220 return -ERESTARTSYS;
221
222 mutex_lock(&hidg->lock);
223 }
224
225 count = min_t(unsigned, count, hidg->report_length);
226 status = copy_from_user(hidg->req->buf, buffer, count);
227
228 if (status != 0) {
229 ERROR(hidg->func.config->cdev,
230 "copy_from_user error\n");
231 mutex_unlock(&hidg->lock);
232 return -EINVAL;
233 }
234
235 hidg->req->status = 0;
236 hidg->req->zero = 0;
237 hidg->req->length = count;
238 hidg->req->complete = f_hidg_req_complete;
239 hidg->req->context = hidg;
240 hidg->write_pending = 1;
241
242 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
243 if (status < 0) {
244 ERROR(hidg->func.config->cdev,
245 "usb_ep_queue error on int endpoint %zd\n", status);
246 hidg->write_pending = 0;
247 wake_up(&hidg->write_queue);
248 } else {
249 status = count;
250 }
251
252 mutex_unlock(&hidg->lock);
253
254 return status;
255}
256
257static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
258{
fd63b10b 259 struct f_hidg *hidg = file->private_data;
71adf118
FC
260 unsigned int ret = 0;
261
262 poll_wait(file, &hidg->read_queue, wait);
263 poll_wait(file, &hidg->write_queue, wait);
264
265 if (WRITE_COND)
266 ret |= POLLOUT | POLLWRNORM;
267
268 if (READ_COND)
269 ret |= POLLIN | POLLRDNORM;
270
271 return ret;
272}
273
274#undef WRITE_COND
275#undef READ_COND
276
277static int f_hidg_release(struct inode *inode, struct file *fd)
278{
279 fd->private_data = NULL;
280 return 0;
281}
282
283static int f_hidg_open(struct inode *inode, struct file *fd)
284{
285 struct f_hidg *hidg =
286 container_of(inode->i_cdev, struct f_hidg, cdev);
287
288 fd->private_data = hidg;
289
290 return 0;
291}
292
293/*-------------------------------------------------------------------------*/
294/* usb_function */
295
296static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
297{
298 struct f_hidg *hidg = (struct f_hidg *)req->context;
299
300 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
301 ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__);
302 return;
303 }
304
305 spin_lock(&hidg->spinlock);
306
307 hidg->set_report_buff = krealloc(hidg->set_report_buff,
308 req->actual, GFP_ATOMIC);
309
310 if (hidg->set_report_buff == NULL) {
311 spin_unlock(&hidg->spinlock);
312 return;
313 }
314 hidg->set_report_length = req->actual;
315 memcpy(hidg->set_report_buff, req->buf, req->actual);
316
317 spin_unlock(&hidg->spinlock);
318
319 wake_up(&hidg->read_queue);
71adf118
FC
320}
321
322static int hidg_setup(struct usb_function *f,
323 const struct usb_ctrlrequest *ctrl)
324{
325 struct f_hidg *hidg = func_to_hidg(f);
326 struct usb_composite_dev *cdev = f->config->cdev;
327 struct usb_request *req = cdev->req;
328 int status = 0;
329 __u16 value, length;
330
331 value = __le16_to_cpu(ctrl->wValue);
332 length = __le16_to_cpu(ctrl->wLength);
333
334 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
335 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
336
337 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
338 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
339 | HID_REQ_GET_REPORT):
340 VDBG(cdev, "get_report\n");
341
342 /* send an empty report */
343 length = min_t(unsigned, length, hidg->report_length);
344 memset(req->buf, 0x0, length);
345
346 goto respond;
347 break;
348
349 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
350 | HID_REQ_GET_PROTOCOL):
351 VDBG(cdev, "get_protocol\n");
352 goto stall;
353 break;
354
355 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
356 | HID_REQ_SET_REPORT):
357 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
358 req->context = hidg;
359 req->complete = hidg_set_report_complete;
360 goto respond;
361 break;
362
363 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
364 | HID_REQ_SET_PROTOCOL):
365 VDBG(cdev, "set_protocol\n");
366 goto stall;
367 break;
368
369 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
370 | USB_REQ_GET_DESCRIPTOR):
371 switch (value >> 8) {
372 case HID_DT_REPORT:
373 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
374 length = min_t(unsigned short, length,
375 hidg->report_desc_length);
376 memcpy(req->buf, hidg->report_desc, length);
377 goto respond;
378 break;
379
380 default:
381 VDBG(cdev, "Unknown decriptor request 0x%x\n",
382 value >> 8);
383 goto stall;
384 break;
385 }
386 break;
387
388 default:
389 VDBG(cdev, "Unknown request 0x%x\n",
390 ctrl->bRequest);
391 goto stall;
392 break;
393 }
394
395stall:
396 return -EOPNOTSUPP;
397
398respond:
399 req->zero = 0;
400 req->length = length;
401 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
402 if (status < 0)
403 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
404 return status;
405}
406
407static void hidg_disable(struct usb_function *f)
408{
409 struct f_hidg *hidg = func_to_hidg(f);
410
411 usb_ep_disable(hidg->in_ep);
412 hidg->in_ep->driver_data = NULL;
71adf118
FC
413}
414
415static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
416{
417 struct usb_composite_dev *cdev = f->config->cdev;
418 struct f_hidg *hidg = func_to_hidg(f);
71adf118
FC
419 int status = 0;
420
421 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
422
423 if (hidg->in_ep != NULL) {
424 /* restart endpoint */
425 if (hidg->in_ep->driver_data != NULL)
426 usb_ep_disable(hidg->in_ep);
427
72c973dd 428 hidg->in_ep->desc = ep_choose(f->config->cdev->gadget,
71adf118 429 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
72c973dd 430 status = usb_ep_enable(hidg->in_ep);
71adf118
FC
431 if (status < 0) {
432 ERROR(cdev, "Enable endpoint FAILED!\n");
433 goto fail;
434 }
435 hidg->in_ep->driver_data = hidg;
436 }
437fail:
438 return status;
439}
440
441const struct file_operations f_hidg_fops = {
442 .owner = THIS_MODULE,
443 .open = f_hidg_open,
444 .release = f_hidg_release,
445 .write = f_hidg_write,
446 .read = f_hidg_read,
447 .poll = f_hidg_poll,
6038f373 448 .llseek = noop_llseek,
71adf118
FC
449};
450
451static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
452{
453 struct usb_ep *ep;
454 struct f_hidg *hidg = func_to_hidg(f);
455 int status;
456 dev_t dev;
457
458 /* allocate instance-specific interface IDs, and patch descriptors */
459 status = usb_interface_id(c, f);
460 if (status < 0)
461 goto fail;
462 hidg_interface_desc.bInterfaceNumber = status;
463
464
465 /* allocate instance-specific endpoints */
466 status = -ENODEV;
467 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
468 if (!ep)
469 goto fail;
470 ep->driver_data = c->cdev; /* claim */
471 hidg->in_ep = ep;
472
473 /* preallocate request and buffer */
474 status = -ENOMEM;
475 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
476 if (!hidg->req)
477 goto fail;
478
479
480 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
481 if (!hidg->req->buf)
482 goto fail;
483
484 /* set descriptor dynamic values */
485 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
486 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
487 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
488 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
489 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
490 hidg_desc.desc[0].wDescriptorLength =
491 cpu_to_le16(hidg->report_desc_length);
492
493 hidg->set_report_buff = NULL;
494
495 /* copy descriptors */
496 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
497 if (!f->descriptors)
498 goto fail;
499
500 hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
501 f->descriptors,
502 &hidg_fs_in_ep_desc);
503
504 if (gadget_is_dualspeed(c->cdev->gadget)) {
505 hidg_hs_in_ep_desc.bEndpointAddress =
506 hidg_fs_in_ep_desc.bEndpointAddress;
507 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
508 if (!f->hs_descriptors)
509 goto fail;
510 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
511 f->hs_descriptors,
512 &hidg_hs_in_ep_desc);
513 } else {
514 hidg->hs_in_ep_desc = NULL;
515 }
516
517 mutex_init(&hidg->lock);
518 spin_lock_init(&hidg->spinlock);
519 init_waitqueue_head(&hidg->write_queue);
520 init_waitqueue_head(&hidg->read_queue);
521
522 /* create char device */
523 cdev_init(&hidg->cdev, &f_hidg_fops);
524 dev = MKDEV(major, hidg->minor);
525 status = cdev_add(&hidg->cdev, dev, 1);
526 if (status)
527 goto fail;
528
529 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
530
531 return 0;
532
533fail:
534 ERROR(f->config->cdev, "hidg_bind FAILED\n");
535 if (hidg->req != NULL) {
536 kfree(hidg->req->buf);
537 if (hidg->in_ep != NULL)
538 usb_ep_free_request(hidg->in_ep, hidg->req);
539 }
540
541 usb_free_descriptors(f->hs_descriptors);
542 usb_free_descriptors(f->descriptors);
543
544 return status;
545}
546
547static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
548{
549 struct f_hidg *hidg = func_to_hidg(f);
550
551 device_destroy(hidg_class, MKDEV(major, hidg->minor));
552 cdev_del(&hidg->cdev);
553
554 /* disable/free request and end point */
555 usb_ep_disable(hidg->in_ep);
556 usb_ep_dequeue(hidg->in_ep, hidg->req);
557 kfree(hidg->req->buf);
558 usb_ep_free_request(hidg->in_ep, hidg->req);
559
560 /* free descriptors copies */
561 usb_free_descriptors(f->hs_descriptors);
562 usb_free_descriptors(f->descriptors);
563
564 kfree(hidg->report_desc);
565 kfree(hidg->set_report_buff);
566 kfree(hidg);
567}
568
569/*-------------------------------------------------------------------------*/
570/* Strings */
571
572#define CT_FUNC_HID_IDX 0
573
574static struct usb_string ct_func_string_defs[] = {
575 [CT_FUNC_HID_IDX].s = "HID Interface",
576 {}, /* end of list */
577};
578
579static struct usb_gadget_strings ct_func_string_table = {
580 .language = 0x0409, /* en-US */
581 .strings = ct_func_string_defs,
582};
583
584static struct usb_gadget_strings *ct_func_strings[] = {
585 &ct_func_string_table,
586 NULL,
587};
588
589/*-------------------------------------------------------------------------*/
590/* usb_configuration */
591
592int __init hidg_bind_config(struct usb_configuration *c,
593 struct hidg_func_descriptor *fdesc, int index)
594{
595 struct f_hidg *hidg;
596 int status;
597
598 if (index >= minors)
599 return -ENOENT;
600
601 /* maybe allocate device-global string IDs, and patch descriptors */
602 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
603 status = usb_string_id(c->cdev);
604 if (status < 0)
605 return status;
606 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
607 hidg_interface_desc.iInterface = status;
608 }
609
610 /* allocate and initialize one new instance */
611 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
612 if (!hidg)
613 return -ENOMEM;
614
615 hidg->minor = index;
616 hidg->bInterfaceSubClass = fdesc->subclass;
617 hidg->bInterfaceProtocol = fdesc->protocol;
618 hidg->report_length = fdesc->report_length;
619 hidg->report_desc_length = fdesc->report_desc_length;
620 hidg->report_desc = kmemdup(fdesc->report_desc,
621 fdesc->report_desc_length,
622 GFP_KERNEL);
623 if (!hidg->report_desc) {
624 kfree(hidg);
625 return -ENOMEM;
626 }
627
628 hidg->func.name = "hid";
629 hidg->func.strings = ct_func_strings;
630 hidg->func.bind = hidg_bind;
631 hidg->func.unbind = hidg_unbind;
632 hidg->func.set_alt = hidg_set_alt;
633 hidg->func.disable = hidg_disable;
634 hidg->func.setup = hidg_setup;
635
636 status = usb_add_function(c, &hidg->func);
637 if (status)
638 kfree(hidg);
639
640 return status;
641}
642
643int __init ghid_setup(struct usb_gadget *g, int count)
644{
645 int status;
646 dev_t dev;
647
648 hidg_class = class_create(THIS_MODULE, "hidg");
649
650 status = alloc_chrdev_region(&dev, 0, count, "hidg");
651 if (!status) {
652 major = MAJOR(dev);
653 minors = count;
654 }
655
656 return status;
657}
658
659void ghid_cleanup(void)
660{
661 if (major) {
662 unregister_chrdev_region(MKDEV(major, 0), minors);
663 major = minors = 0;
664 }
665
666 class_destroy(hidg_class);
667 hidg_class = NULL;
668}