mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / usb / gadget / function / f_hid.c
blob51980c50546d5b1061eb289b8825604cf8b031bc
1 /*
2 * f_hid.c -- USB HID function driver
4 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/hid.h>
15 #include <linux/idr.h>
16 #include <linux/cdev.h>
17 #include <linux/mutex.h>
18 #include <linux/poll.h>
19 #include <linux/uaccess.h>
20 #include <linux/wait.h>
21 #include <linux/sched.h>
22 #include <linux/usb/g_hid.h>
24 #include "u_f.h"
25 #include "u_hid.h"
27 #define HIDG_MINORS 4
29 static int major, minors;
30 static struct class *hidg_class;
31 static DEFINE_IDA(hidg_ida);
32 static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
34 /*-------------------------------------------------------------------------*/
35 /* HID gadget struct */
37 struct f_hidg_req_list {
38 struct usb_request *req;
39 unsigned int pos;
40 struct list_head list;
43 struct f_hidg {
44 /* configuration */
45 unsigned char bInterfaceSubClass;
46 unsigned char bInterfaceProtocol;
47 unsigned short report_desc_length;
48 char *report_desc;
49 unsigned short report_length;
51 /* recv report */
52 struct list_head completed_out_req;
53 spinlock_t spinlock;
54 wait_queue_head_t read_queue;
55 unsigned int qlen;
57 /* send report */
58 struct mutex lock;
59 bool write_pending;
60 wait_queue_head_t write_queue;
61 struct usb_request *req;
63 int minor;
64 struct cdev cdev;
65 struct usb_function func;
67 struct usb_ep *in_ep;
68 struct usb_ep *out_ep;
71 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
73 return container_of(f, struct f_hidg, func);
76 /*-------------------------------------------------------------------------*/
77 /* Static descriptors */
79 static struct usb_interface_descriptor hidg_interface_desc = {
80 .bLength = sizeof hidg_interface_desc,
81 .bDescriptorType = USB_DT_INTERFACE,
82 /* .bInterfaceNumber = DYNAMIC */
83 .bAlternateSetting = 0,
84 .bNumEndpoints = 2,
85 .bInterfaceClass = USB_CLASS_HID,
86 /* .bInterfaceSubClass = DYNAMIC */
87 /* .bInterfaceProtocol = DYNAMIC */
88 /* .iInterface = DYNAMIC */
91 static struct hid_descriptor hidg_desc = {
92 .bLength = sizeof hidg_desc,
93 .bDescriptorType = HID_DT_HID,
94 .bcdHID = 0x0101,
95 .bCountryCode = 0x00,
96 .bNumDescriptors = 0x1,
97 /*.desc[0].bDescriptorType = DYNAMIC */
98 /*.desc[0].wDescriptorLenght = DYNAMIC */
101 /* High-Speed Support */
103 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
104 .bLength = USB_DT_ENDPOINT_SIZE,
105 .bDescriptorType = USB_DT_ENDPOINT,
106 .bEndpointAddress = USB_DIR_IN,
107 .bmAttributes = USB_ENDPOINT_XFER_INT,
108 /*.wMaxPacketSize = DYNAMIC */
109 .bInterval = 4, /* FIXME: Add this field in the
110 * HID gadget configuration?
111 * (struct hidg_func_descriptor)
115 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
116 .bLength = USB_DT_ENDPOINT_SIZE,
117 .bDescriptorType = USB_DT_ENDPOINT,
118 .bEndpointAddress = USB_DIR_OUT,
119 .bmAttributes = USB_ENDPOINT_XFER_INT,
120 /*.wMaxPacketSize = DYNAMIC */
121 .bInterval = 4, /* FIXME: Add this field in the
122 * HID gadget configuration?
123 * (struct hidg_func_descriptor)
127 static struct usb_descriptor_header *hidg_hs_descriptors[] = {
128 (struct usb_descriptor_header *)&hidg_interface_desc,
129 (struct usb_descriptor_header *)&hidg_desc,
130 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
131 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
132 NULL,
135 /* Full-Speed Support */
137 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
138 .bLength = USB_DT_ENDPOINT_SIZE,
139 .bDescriptorType = USB_DT_ENDPOINT,
140 .bEndpointAddress = USB_DIR_IN,
141 .bmAttributes = USB_ENDPOINT_XFER_INT,
142 /*.wMaxPacketSize = DYNAMIC */
143 .bInterval = 10, /* FIXME: Add this field in the
144 * HID gadget configuration?
145 * (struct hidg_func_descriptor)
149 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
152 .bEndpointAddress = USB_DIR_OUT,
153 .bmAttributes = USB_ENDPOINT_XFER_INT,
154 /*.wMaxPacketSize = DYNAMIC */
155 .bInterval = 10, /* FIXME: Add this field in the
156 * HID gadget configuration?
157 * (struct hidg_func_descriptor)
161 static struct usb_descriptor_header *hidg_fs_descriptors[] = {
162 (struct usb_descriptor_header *)&hidg_interface_desc,
163 (struct usb_descriptor_header *)&hidg_desc,
164 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
165 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
166 NULL,
169 /*-------------------------------------------------------------------------*/
170 /* Strings */
172 #define CT_FUNC_HID_IDX 0
174 static struct usb_string ct_func_string_defs[] = {
175 [CT_FUNC_HID_IDX].s = "HID Interface",
176 {}, /* end of list */
179 static struct usb_gadget_strings ct_func_string_table = {
180 .language = 0x0409, /* en-US */
181 .strings = ct_func_string_defs,
184 static struct usb_gadget_strings *ct_func_strings[] = {
185 &ct_func_string_table,
186 NULL,
189 /*-------------------------------------------------------------------------*/
190 /* Char Device */
192 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
193 size_t count, loff_t *ptr)
195 struct f_hidg *hidg = file->private_data;
196 struct f_hidg_req_list *list;
197 struct usb_request *req;
198 unsigned long flags;
199 int ret;
201 if (!count)
202 return 0;
204 if (!access_ok(VERIFY_WRITE, buffer, count))
205 return -EFAULT;
207 spin_lock_irqsave(&hidg->spinlock, flags);
209 #define READ_COND (!list_empty(&hidg->completed_out_req))
211 /* wait for at least one buffer to complete */
212 while (!READ_COND) {
213 spin_unlock_irqrestore(&hidg->spinlock, flags);
214 if (file->f_flags & O_NONBLOCK)
215 return -EAGAIN;
217 if (wait_event_interruptible(hidg->read_queue, READ_COND))
218 return -ERESTARTSYS;
220 spin_lock_irqsave(&hidg->spinlock, flags);
223 /* pick the first one */
224 list = list_first_entry(&hidg->completed_out_req,
225 struct f_hidg_req_list, list);
226 req = list->req;
227 count = min_t(unsigned int, count, req->actual - list->pos);
228 spin_unlock_irqrestore(&hidg->spinlock, flags);
230 /* copy to user outside spinlock */
231 count -= copy_to_user(buffer, req->buf + list->pos, count);
232 list->pos += count;
235 * if this request is completely handled and transfered to
236 * userspace, remove its entry from the list and requeue it
237 * again. Otherwise, we will revisit it again upon the next
238 * call, taking into account its current read position.
240 if (list->pos == req->actual) {
241 spin_lock_irqsave(&hidg->spinlock, flags);
242 list_del(&list->list);
243 kfree(list);
244 spin_unlock_irqrestore(&hidg->spinlock, flags);
246 req->length = hidg->report_length;
247 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
248 if (ret < 0)
249 return ret;
252 return count;
255 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
257 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
259 if (req->status != 0) {
260 ERROR(hidg->func.config->cdev,
261 "End Point Request ERROR: %d\n", req->status);
264 hidg->write_pending = 0;
265 wake_up(&hidg->write_queue);
268 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
269 size_t count, loff_t *offp)
271 struct f_hidg *hidg = file->private_data;
272 ssize_t status = -ENOMEM;
274 if (!access_ok(VERIFY_READ, buffer, count))
275 return -EFAULT;
277 mutex_lock(&hidg->lock);
279 #define WRITE_COND (!hidg->write_pending)
281 /* write queue */
282 while (!WRITE_COND) {
283 mutex_unlock(&hidg->lock);
284 if (file->f_flags & O_NONBLOCK)
285 return -EAGAIN;
287 if (wait_event_interruptible_exclusive(
288 hidg->write_queue, WRITE_COND))
289 return -ERESTARTSYS;
291 mutex_lock(&hidg->lock);
294 count = min_t(unsigned, count, hidg->report_length);
295 status = copy_from_user(hidg->req->buf, buffer, count);
297 if (status != 0) {
298 ERROR(hidg->func.config->cdev,
299 "copy_from_user error\n");
300 mutex_unlock(&hidg->lock);
301 return -EINVAL;
304 hidg->req->status = 0;
305 hidg->req->zero = 0;
306 hidg->req->length = count;
307 hidg->req->complete = f_hidg_req_complete;
308 hidg->req->context = hidg;
309 hidg->write_pending = 1;
311 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
312 if (status < 0) {
313 ERROR(hidg->func.config->cdev,
314 "usb_ep_queue error on int endpoint %zd\n", status);
315 hidg->write_pending = 0;
316 wake_up(&hidg->write_queue);
317 } else {
318 status = count;
321 mutex_unlock(&hidg->lock);
323 return status;
326 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
328 struct f_hidg *hidg = file->private_data;
329 unsigned int ret = 0;
331 poll_wait(file, &hidg->read_queue, wait);
332 poll_wait(file, &hidg->write_queue, wait);
334 if (WRITE_COND)
335 ret |= POLLOUT | POLLWRNORM;
337 if (READ_COND)
338 ret |= POLLIN | POLLRDNORM;
340 return ret;
343 #undef WRITE_COND
344 #undef READ_COND
346 static int f_hidg_release(struct inode *inode, struct file *fd)
348 fd->private_data = NULL;
349 return 0;
352 static int f_hidg_open(struct inode *inode, struct file *fd)
354 struct f_hidg *hidg =
355 container_of(inode->i_cdev, struct f_hidg, cdev);
357 fd->private_data = hidg;
359 return 0;
362 /*-------------------------------------------------------------------------*/
363 /* usb_function */
365 static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
366 unsigned length)
368 return alloc_ep_req(ep, length, length);
371 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
373 struct f_hidg *hidg = (struct f_hidg *) req->context;
374 struct f_hidg_req_list *req_list;
375 unsigned long flags;
377 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
378 if (!req_list)
379 return;
381 req_list->req = req;
383 spin_lock_irqsave(&hidg->spinlock, flags);
384 list_add_tail(&req_list->list, &hidg->completed_out_req);
385 spin_unlock_irqrestore(&hidg->spinlock, flags);
387 wake_up(&hidg->read_queue);
390 static int hidg_setup(struct usb_function *f,
391 const struct usb_ctrlrequest *ctrl)
393 struct f_hidg *hidg = func_to_hidg(f);
394 struct usb_composite_dev *cdev = f->config->cdev;
395 struct usb_request *req = cdev->req;
396 int status = 0;
397 __u16 value, length;
399 value = __le16_to_cpu(ctrl->wValue);
400 length = __le16_to_cpu(ctrl->wLength);
402 VDBG(cdev,
403 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
404 __func__, ctrl->bRequestType, ctrl->bRequest, value);
406 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
407 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
408 | HID_REQ_GET_REPORT):
409 VDBG(cdev, "get_report\n");
411 /* send an empty report */
412 length = min_t(unsigned, length, hidg->report_length);
413 memset(req->buf, 0x0, length);
415 goto respond;
416 break;
418 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
419 | HID_REQ_GET_PROTOCOL):
420 VDBG(cdev, "get_protocol\n");
421 goto stall;
422 break;
424 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
425 | HID_REQ_SET_REPORT):
426 VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
427 goto stall;
428 break;
430 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
431 | HID_REQ_SET_PROTOCOL):
432 VDBG(cdev, "set_protocol\n");
433 goto stall;
434 break;
436 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
437 | USB_REQ_GET_DESCRIPTOR):
438 switch (value >> 8) {
439 case HID_DT_HID:
441 struct hid_descriptor hidg_desc_copy = hidg_desc;
443 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
444 hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
445 hidg_desc_copy.desc[0].wDescriptorLength =
446 cpu_to_le16(hidg->report_desc_length);
448 length = min_t(unsigned short, length,
449 hidg_desc_copy.bLength);
450 memcpy(req->buf, &hidg_desc_copy, length);
451 goto respond;
452 break;
454 case HID_DT_REPORT:
455 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
456 length = min_t(unsigned short, length,
457 hidg->report_desc_length);
458 memcpy(req->buf, hidg->report_desc, length);
459 goto respond;
460 break;
462 default:
463 VDBG(cdev, "Unknown descriptor request 0x%x\n",
464 value >> 8);
465 goto stall;
466 break;
468 break;
470 default:
471 VDBG(cdev, "Unknown request 0x%x\n",
472 ctrl->bRequest);
473 goto stall;
474 break;
477 stall:
478 return -EOPNOTSUPP;
480 respond:
481 req->zero = 0;
482 req->length = length;
483 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
484 if (status < 0)
485 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
486 return status;
489 static void hidg_disable(struct usb_function *f)
491 struct f_hidg *hidg = func_to_hidg(f);
492 struct f_hidg_req_list *list, *next;
494 usb_ep_disable(hidg->in_ep);
495 usb_ep_disable(hidg->out_ep);
497 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
498 list_del(&list->list);
499 kfree(list);
503 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
505 struct usb_composite_dev *cdev = f->config->cdev;
506 struct f_hidg *hidg = func_to_hidg(f);
507 int i, status = 0;
509 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
511 if (hidg->in_ep != NULL) {
512 /* restart endpoint */
513 usb_ep_disable(hidg->in_ep);
515 status = config_ep_by_speed(f->config->cdev->gadget, f,
516 hidg->in_ep);
517 if (status) {
518 ERROR(cdev, "config_ep_by_speed FAILED!\n");
519 goto fail;
521 status = usb_ep_enable(hidg->in_ep);
522 if (status < 0) {
523 ERROR(cdev, "Enable IN endpoint FAILED!\n");
524 goto fail;
526 hidg->in_ep->driver_data = hidg;
530 if (hidg->out_ep != NULL) {
531 /* restart endpoint */
532 usb_ep_disable(hidg->out_ep);
534 status = config_ep_by_speed(f->config->cdev->gadget, f,
535 hidg->out_ep);
536 if (status) {
537 ERROR(cdev, "config_ep_by_speed FAILED!\n");
538 goto fail;
540 status = usb_ep_enable(hidg->out_ep);
541 if (status < 0) {
542 ERROR(cdev, "Enable IN endpoint FAILED!\n");
543 goto fail;
545 hidg->out_ep->driver_data = hidg;
548 * allocate a bunch of read buffers and queue them all at once.
550 for (i = 0; i < hidg->qlen && status == 0; i++) {
551 struct usb_request *req =
552 hidg_alloc_ep_req(hidg->out_ep,
553 hidg->report_length);
554 if (req) {
555 req->complete = hidg_set_report_complete;
556 req->context = hidg;
557 status = usb_ep_queue(hidg->out_ep, req,
558 GFP_ATOMIC);
559 if (status)
560 ERROR(cdev, "%s queue req --> %d\n",
561 hidg->out_ep->name, status);
562 } else {
563 usb_ep_disable(hidg->out_ep);
564 status = -ENOMEM;
565 goto fail;
570 fail:
571 return status;
574 static const struct file_operations f_hidg_fops = {
575 .owner = THIS_MODULE,
576 .open = f_hidg_open,
577 .release = f_hidg_release,
578 .write = f_hidg_write,
579 .read = f_hidg_read,
580 .poll = f_hidg_poll,
581 .llseek = noop_llseek,
584 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
586 struct usb_ep *ep;
587 struct f_hidg *hidg = func_to_hidg(f);
588 struct usb_string *us;
589 struct device *device;
590 int status;
591 dev_t dev;
593 /* maybe allocate device-global string IDs, and patch descriptors */
594 us = usb_gstrings_attach(c->cdev, ct_func_strings,
595 ARRAY_SIZE(ct_func_string_defs));
596 if (IS_ERR(us))
597 return PTR_ERR(us);
598 hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
600 /* allocate instance-specific interface IDs, and patch descriptors */
601 status = usb_interface_id(c, f);
602 if (status < 0)
603 goto fail;
604 hidg_interface_desc.bInterfaceNumber = status;
606 /* allocate instance-specific endpoints */
607 status = -ENODEV;
608 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
609 if (!ep)
610 goto fail;
611 hidg->in_ep = ep;
613 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
614 if (!ep)
615 goto fail;
616 hidg->out_ep = ep;
618 /* preallocate request and buffer */
619 status = -ENOMEM;
620 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
621 if (!hidg->req)
622 goto fail;
624 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
625 if (!hidg->req->buf)
626 goto fail;
628 /* set descriptor dynamic values */
629 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
630 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
631 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
632 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
633 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
634 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
636 * We can use hidg_desc struct here but we should not relay
637 * that its content won't change after returning from this function.
639 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
640 hidg_desc.desc[0].wDescriptorLength =
641 cpu_to_le16(hidg->report_desc_length);
643 hidg_hs_in_ep_desc.bEndpointAddress =
644 hidg_fs_in_ep_desc.bEndpointAddress;
645 hidg_hs_out_ep_desc.bEndpointAddress =
646 hidg_fs_out_ep_desc.bEndpointAddress;
648 status = usb_assign_descriptors(f, hidg_fs_descriptors,
649 hidg_hs_descriptors, NULL, NULL);
650 if (status)
651 goto fail;
653 mutex_init(&hidg->lock);
654 spin_lock_init(&hidg->spinlock);
655 init_waitqueue_head(&hidg->write_queue);
656 init_waitqueue_head(&hidg->read_queue);
657 INIT_LIST_HEAD(&hidg->completed_out_req);
659 /* create char device */
660 cdev_init(&hidg->cdev, &f_hidg_fops);
661 dev = MKDEV(major, hidg->minor);
662 status = cdev_add(&hidg->cdev, dev, 1);
663 if (status)
664 goto fail_free_descs;
666 device = device_create(hidg_class, NULL, dev, NULL,
667 "%s%d", "hidg", hidg->minor);
668 if (IS_ERR(device)) {
669 status = PTR_ERR(device);
670 goto del;
673 return 0;
674 del:
675 cdev_del(&hidg->cdev);
676 fail_free_descs:
677 usb_free_all_descriptors(f);
678 fail:
679 ERROR(f->config->cdev, "hidg_bind FAILED\n");
680 if (hidg->req != NULL) {
681 kfree(hidg->req->buf);
682 if (hidg->in_ep != NULL)
683 usb_ep_free_request(hidg->in_ep, hidg->req);
686 return status;
689 static inline int hidg_get_minor(void)
691 int ret;
693 ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
694 if (ret >= HIDG_MINORS) {
695 ida_simple_remove(&hidg_ida, ret);
696 ret = -ENODEV;
699 return ret;
702 static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
704 return container_of(to_config_group(item), struct f_hid_opts,
705 func_inst.group);
708 static void hid_attr_release(struct config_item *item)
710 struct f_hid_opts *opts = to_f_hid_opts(item);
712 usb_put_function_instance(&opts->func_inst);
715 static struct configfs_item_operations hidg_item_ops = {
716 .release = hid_attr_release,
719 #define F_HID_OPT(name, prec, limit) \
720 static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
722 struct f_hid_opts *opts = to_f_hid_opts(item); \
723 int result; \
725 mutex_lock(&opts->lock); \
726 result = sprintf(page, "%d\n", opts->name); \
727 mutex_unlock(&opts->lock); \
729 return result; \
732 static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
733 const char *page, size_t len) \
735 struct f_hid_opts *opts = to_f_hid_opts(item); \
736 int ret; \
737 u##prec num; \
739 mutex_lock(&opts->lock); \
740 if (opts->refcnt) { \
741 ret = -EBUSY; \
742 goto end; \
745 ret = kstrtou##prec(page, 0, &num); \
746 if (ret) \
747 goto end; \
749 if (num > limit) { \
750 ret = -EINVAL; \
751 goto end; \
753 opts->name = num; \
754 ret = len; \
756 end: \
757 mutex_unlock(&opts->lock); \
758 return ret; \
761 CONFIGFS_ATTR(f_hid_opts_, name)
763 F_HID_OPT(subclass, 8, 255);
764 F_HID_OPT(protocol, 8, 255);
765 F_HID_OPT(report_length, 16, 65535);
767 static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
769 struct f_hid_opts *opts = to_f_hid_opts(item);
770 int result;
772 mutex_lock(&opts->lock);
773 result = opts->report_desc_length;
774 memcpy(page, opts->report_desc, opts->report_desc_length);
775 mutex_unlock(&opts->lock);
777 return result;
780 static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
781 const char *page, size_t len)
783 struct f_hid_opts *opts = to_f_hid_opts(item);
784 int ret = -EBUSY;
785 char *d;
787 mutex_lock(&opts->lock);
789 if (opts->refcnt)
790 goto end;
791 if (len > PAGE_SIZE) {
792 ret = -ENOSPC;
793 goto end;
795 d = kmemdup(page, len, GFP_KERNEL);
796 if (!d) {
797 ret = -ENOMEM;
798 goto end;
800 kfree(opts->report_desc);
801 opts->report_desc = d;
802 opts->report_desc_length = len;
803 opts->report_desc_alloc = true;
804 ret = len;
805 end:
806 mutex_unlock(&opts->lock);
807 return ret;
810 CONFIGFS_ATTR(f_hid_opts_, report_desc);
812 static struct configfs_attribute *hid_attrs[] = {
813 &f_hid_opts_attr_subclass,
814 &f_hid_opts_attr_protocol,
815 &f_hid_opts_attr_report_length,
816 &f_hid_opts_attr_report_desc,
817 NULL,
820 static struct config_item_type hid_func_type = {
821 .ct_item_ops = &hidg_item_ops,
822 .ct_attrs = hid_attrs,
823 .ct_owner = THIS_MODULE,
826 static inline void hidg_put_minor(int minor)
828 ida_simple_remove(&hidg_ida, minor);
831 static void hidg_free_inst(struct usb_function_instance *f)
833 struct f_hid_opts *opts;
835 opts = container_of(f, struct f_hid_opts, func_inst);
837 mutex_lock(&hidg_ida_lock);
839 hidg_put_minor(opts->minor);
840 if (idr_is_empty(&hidg_ida.idr))
841 ghid_cleanup();
843 mutex_unlock(&hidg_ida_lock);
845 if (opts->report_desc_alloc)
846 kfree(opts->report_desc);
848 kfree(opts);
851 static struct usb_function_instance *hidg_alloc_inst(void)
853 struct f_hid_opts *opts;
854 struct usb_function_instance *ret;
855 int status = 0;
857 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
858 if (!opts)
859 return ERR_PTR(-ENOMEM);
860 mutex_init(&opts->lock);
861 opts->func_inst.free_func_inst = hidg_free_inst;
862 ret = &opts->func_inst;
864 mutex_lock(&hidg_ida_lock);
866 if (idr_is_empty(&hidg_ida.idr)) {
867 status = ghid_setup(NULL, HIDG_MINORS);
868 if (status) {
869 ret = ERR_PTR(status);
870 kfree(opts);
871 goto unlock;
875 opts->minor = hidg_get_minor();
876 if (opts->minor < 0) {
877 ret = ERR_PTR(opts->minor);
878 kfree(opts);
879 if (idr_is_empty(&hidg_ida.idr))
880 ghid_cleanup();
881 goto unlock;
883 config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
885 unlock:
886 mutex_unlock(&hidg_ida_lock);
887 return ret;
890 static void hidg_free(struct usb_function *f)
892 struct f_hidg *hidg;
893 struct f_hid_opts *opts;
895 hidg = func_to_hidg(f);
896 opts = container_of(f->fi, struct f_hid_opts, func_inst);
897 kfree(hidg->report_desc);
898 kfree(hidg);
899 mutex_lock(&opts->lock);
900 --opts->refcnt;
901 mutex_unlock(&opts->lock);
904 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
906 struct f_hidg *hidg = func_to_hidg(f);
908 device_destroy(hidg_class, MKDEV(major, hidg->minor));
909 cdev_del(&hidg->cdev);
911 /* disable/free request and end point */
912 usb_ep_disable(hidg->in_ep);
913 kfree(hidg->req->buf);
914 usb_ep_free_request(hidg->in_ep, hidg->req);
916 usb_free_all_descriptors(f);
919 static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
921 struct f_hidg *hidg;
922 struct f_hid_opts *opts;
924 /* allocate and initialize one new instance */
925 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
926 if (!hidg)
927 return ERR_PTR(-ENOMEM);
929 opts = container_of(fi, struct f_hid_opts, func_inst);
931 mutex_lock(&opts->lock);
932 ++opts->refcnt;
934 hidg->minor = opts->minor;
935 hidg->bInterfaceSubClass = opts->subclass;
936 hidg->bInterfaceProtocol = opts->protocol;
937 hidg->report_length = opts->report_length;
938 hidg->report_desc_length = opts->report_desc_length;
939 if (opts->report_desc) {
940 hidg->report_desc = kmemdup(opts->report_desc,
941 opts->report_desc_length,
942 GFP_KERNEL);
943 if (!hidg->report_desc) {
944 kfree(hidg);
945 mutex_unlock(&opts->lock);
946 return ERR_PTR(-ENOMEM);
950 mutex_unlock(&opts->lock);
952 hidg->func.name = "hid";
953 hidg->func.bind = hidg_bind;
954 hidg->func.unbind = hidg_unbind;
955 hidg->func.set_alt = hidg_set_alt;
956 hidg->func.disable = hidg_disable;
957 hidg->func.setup = hidg_setup;
958 hidg->func.free_func = hidg_free;
960 /* this could me made configurable at some point */
961 hidg->qlen = 4;
963 return &hidg->func;
966 DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
967 MODULE_LICENSE("GPL");
968 MODULE_AUTHOR("Fabien Chouteau");
970 int ghid_setup(struct usb_gadget *g, int count)
972 int status;
973 dev_t dev;
975 hidg_class = class_create(THIS_MODULE, "hidg");
976 if (IS_ERR(hidg_class)) {
977 status = PTR_ERR(hidg_class);
978 hidg_class = NULL;
979 return status;
982 status = alloc_chrdev_region(&dev, 0, count, "hidg");
983 if (status) {
984 class_destroy(hidg_class);
985 hidg_class = NULL;
986 return status;
989 major = MAJOR(dev);
990 minors = count;
992 return 0;
995 void ghid_cleanup(void)
997 if (major) {
998 unregister_chrdev_region(MKDEV(major, 0), minors);
999 major = minors = 0;
1002 class_destroy(hidg_class);
1003 hidg_class = NULL;