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