]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/function/f_hid.c
usb: gadget: f_hid: convert to new function interface with backward compatibility
[karo-tx-linux.git] / drivers / usb / gadget / function / f_hid.c
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
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>
23
24 #include "u_f.h"
25 #include "u_hid.h"
26
27 #define HIDG_MINORS     4
28
29 static int major, minors;
30 static struct class *hidg_class;
31 #ifndef USBF_HID_INCLUDED
32 static DEFINE_IDA(hidg_ida);
33 static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
34 #endif
35
36 /*-------------------------------------------------------------------------*/
37 /*                            HID gadget struct                            */
38
39 struct f_hidg_req_list {
40         struct usb_request      *req;
41         unsigned int            pos;
42         struct list_head        list;
43 };
44
45 struct f_hidg {
46         /* configuration */
47         unsigned char                   bInterfaceSubClass;
48         unsigned char                   bInterfaceProtocol;
49         unsigned short                  report_desc_length;
50         char                            *report_desc;
51         unsigned short                  report_length;
52
53         /* recv report */
54         struct list_head                completed_out_req;
55         spinlock_t                      spinlock;
56         wait_queue_head_t               read_queue;
57         unsigned int                    qlen;
58
59         /* send report */
60         struct mutex                    lock;
61         bool                            write_pending;
62         wait_queue_head_t               write_queue;
63         struct usb_request              *req;
64
65         int                             minor;
66         struct cdev                     cdev;
67         struct usb_function             func;
68
69         struct usb_ep                   *in_ep;
70         struct usb_ep                   *out_ep;
71 };
72
73 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
74 {
75         return container_of(f, struct f_hidg, func);
76 }
77
78 /*-------------------------------------------------------------------------*/
79 /*                           Static descriptors                            */
80
81 static struct usb_interface_descriptor hidg_interface_desc = {
82         .bLength                = sizeof hidg_interface_desc,
83         .bDescriptorType        = USB_DT_INTERFACE,
84         /* .bInterfaceNumber    = DYNAMIC */
85         .bAlternateSetting      = 0,
86         .bNumEndpoints          = 2,
87         .bInterfaceClass        = USB_CLASS_HID,
88         /* .bInterfaceSubClass  = DYNAMIC */
89         /* .bInterfaceProtocol  = DYNAMIC */
90         /* .iInterface          = DYNAMIC */
91 };
92
93 static struct hid_descriptor hidg_desc = {
94         .bLength                        = sizeof hidg_desc,
95         .bDescriptorType                = HID_DT_HID,
96         .bcdHID                         = 0x0101,
97         .bCountryCode                   = 0x00,
98         .bNumDescriptors                = 0x1,
99         /*.desc[0].bDescriptorType      = DYNAMIC */
100         /*.desc[0].wDescriptorLenght    = DYNAMIC */
101 };
102
103 /* High-Speed Support */
104
105 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
106         .bLength                = USB_DT_ENDPOINT_SIZE,
107         .bDescriptorType        = USB_DT_ENDPOINT,
108         .bEndpointAddress       = USB_DIR_IN,
109         .bmAttributes           = USB_ENDPOINT_XFER_INT,
110         /*.wMaxPacketSize       = DYNAMIC */
111         .bInterval              = 4, /* FIXME: Add this field in the
112                                       * HID gadget configuration?
113                                       * (struct hidg_func_descriptor)
114                                       */
115 };
116
117 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
118         .bLength                = USB_DT_ENDPOINT_SIZE,
119         .bDescriptorType        = USB_DT_ENDPOINT,
120         .bEndpointAddress       = USB_DIR_OUT,
121         .bmAttributes           = USB_ENDPOINT_XFER_INT,
122         /*.wMaxPacketSize       = DYNAMIC */
123         .bInterval              = 4, /* FIXME: Add this field in the
124                                       * HID gadget configuration?
125                                       * (struct hidg_func_descriptor)
126                                       */
127 };
128
129 static struct usb_descriptor_header *hidg_hs_descriptors[] = {
130         (struct usb_descriptor_header *)&hidg_interface_desc,
131         (struct usb_descriptor_header *)&hidg_desc,
132         (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
133         (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
134         NULL,
135 };
136
137 /* Full-Speed Support */
138
139 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
140         .bLength                = USB_DT_ENDPOINT_SIZE,
141         .bDescriptorType        = USB_DT_ENDPOINT,
142         .bEndpointAddress       = USB_DIR_IN,
143         .bmAttributes           = USB_ENDPOINT_XFER_INT,
144         /*.wMaxPacketSize       = DYNAMIC */
145         .bInterval              = 10, /* FIXME: Add this field in the
146                                        * HID gadget configuration?
147                                        * (struct hidg_func_descriptor)
148                                        */
149 };
150
151 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
152         .bLength                = USB_DT_ENDPOINT_SIZE,
153         .bDescriptorType        = USB_DT_ENDPOINT,
154         .bEndpointAddress       = USB_DIR_OUT,
155         .bmAttributes           = USB_ENDPOINT_XFER_INT,
156         /*.wMaxPacketSize       = DYNAMIC */
157         .bInterval              = 10, /* FIXME: Add this field in the
158                                        * HID gadget configuration?
159                                        * (struct hidg_func_descriptor)
160                                        */
161 };
162
163 static struct usb_descriptor_header *hidg_fs_descriptors[] = {
164         (struct usb_descriptor_header *)&hidg_interface_desc,
165         (struct usb_descriptor_header *)&hidg_desc,
166         (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
167         (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
168         NULL,
169 };
170
171 /*-------------------------------------------------------------------------*/
172 /*                                 Strings                                 */
173
174 #define CT_FUNC_HID_IDX 0
175
176 static struct usb_string ct_func_string_defs[] = {
177         [CT_FUNC_HID_IDX].s     = "HID Interface",
178         {},                     /* end of list */
179 };
180
181 static struct usb_gadget_strings ct_func_string_table = {
182         .language       = 0x0409,       /* en-US */
183         .strings        = ct_func_string_defs,
184 };
185
186 static struct usb_gadget_strings *ct_func_strings[] = {
187         &ct_func_string_table,
188         NULL,
189 };
190
191 /*-------------------------------------------------------------------------*/
192 /*                              Char Device                                */
193
194 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
195                         size_t count, loff_t *ptr)
196 {
197         struct f_hidg *hidg = file->private_data;
198         struct f_hidg_req_list *list;
199         struct usb_request *req;
200         unsigned long flags;
201         int ret;
202
203         if (!count)
204                 return 0;
205
206         if (!access_ok(VERIFY_WRITE, buffer, count))
207                 return -EFAULT;
208
209         spin_lock_irqsave(&hidg->spinlock, flags);
210
211 #define READ_COND (!list_empty(&hidg->completed_out_req))
212
213         /* wait for at least one buffer to complete */
214         while (!READ_COND) {
215                 spin_unlock_irqrestore(&hidg->spinlock, flags);
216                 if (file->f_flags & O_NONBLOCK)
217                         return -EAGAIN;
218
219                 if (wait_event_interruptible(hidg->read_queue, READ_COND))
220                         return -ERESTARTSYS;
221
222                 spin_lock_irqsave(&hidg->spinlock, flags);
223         }
224
225         /* pick the first one */
226         list = list_first_entry(&hidg->completed_out_req,
227                                 struct f_hidg_req_list, list);
228         req = list->req;
229         count = min_t(unsigned int, count, req->actual - list->pos);
230         spin_unlock_irqrestore(&hidg->spinlock, flags);
231
232         /* copy to user outside spinlock */
233         count -= copy_to_user(buffer, req->buf + list->pos, count);
234         list->pos += count;
235
236         /*
237          * if this request is completely handled and transfered to
238          * userspace, remove its entry from the list and requeue it
239          * again. Otherwise, we will revisit it again upon the next
240          * call, taking into account its current read position.
241          */
242         if (list->pos == req->actual) {
243                 spin_lock_irqsave(&hidg->spinlock, flags);
244                 list_del(&list->list);
245                 kfree(list);
246                 spin_unlock_irqrestore(&hidg->spinlock, flags);
247
248                 req->length = hidg->report_length;
249                 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
250                 if (ret < 0)
251                         return ret;
252         }
253
254         return count;
255 }
256
257 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
258 {
259         struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
260
261         if (req->status != 0) {
262                 ERROR(hidg->func.config->cdev,
263                         "End Point Request ERROR: %d\n", req->status);
264         }
265
266         hidg->write_pending = 0;
267         wake_up(&hidg->write_queue);
268 }
269
270 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
271                             size_t count, loff_t *offp)
272 {
273         struct f_hidg *hidg  = file->private_data;
274         ssize_t status = -ENOMEM;
275
276         if (!access_ok(VERIFY_READ, buffer, count))
277                 return -EFAULT;
278
279         mutex_lock(&hidg->lock);
280
281 #define WRITE_COND (!hidg->write_pending)
282
283         /* write queue */
284         while (!WRITE_COND) {
285                 mutex_unlock(&hidg->lock);
286                 if (file->f_flags & O_NONBLOCK)
287                         return -EAGAIN;
288
289                 if (wait_event_interruptible_exclusive(
290                                 hidg->write_queue, WRITE_COND))
291                         return -ERESTARTSYS;
292
293                 mutex_lock(&hidg->lock);
294         }
295
296         count  = min_t(unsigned, count, hidg->report_length);
297         status = copy_from_user(hidg->req->buf, buffer, count);
298
299         if (status != 0) {
300                 ERROR(hidg->func.config->cdev,
301                         "copy_from_user error\n");
302                 mutex_unlock(&hidg->lock);
303                 return -EINVAL;
304         }
305
306         hidg->req->status   = 0;
307         hidg->req->zero     = 0;
308         hidg->req->length   = count;
309         hidg->req->complete = f_hidg_req_complete;
310         hidg->req->context  = hidg;
311         hidg->write_pending = 1;
312
313         status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
314         if (status < 0) {
315                 ERROR(hidg->func.config->cdev,
316                         "usb_ep_queue error on int endpoint %zd\n", status);
317                 hidg->write_pending = 0;
318                 wake_up(&hidg->write_queue);
319         } else {
320                 status = count;
321         }
322
323         mutex_unlock(&hidg->lock);
324
325         return status;
326 }
327
328 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
329 {
330         struct f_hidg   *hidg  = file->private_data;
331         unsigned int    ret = 0;
332
333         poll_wait(file, &hidg->read_queue, wait);
334         poll_wait(file, &hidg->write_queue, wait);
335
336         if (WRITE_COND)
337                 ret |= POLLOUT | POLLWRNORM;
338
339         if (READ_COND)
340                 ret |= POLLIN | POLLRDNORM;
341
342         return ret;
343 }
344
345 #undef WRITE_COND
346 #undef READ_COND
347
348 static int f_hidg_release(struct inode *inode, struct file *fd)
349 {
350         fd->private_data = NULL;
351         return 0;
352 }
353
354 static int f_hidg_open(struct inode *inode, struct file *fd)
355 {
356         struct f_hidg *hidg =
357                 container_of(inode->i_cdev, struct f_hidg, cdev);
358
359         fd->private_data = hidg;
360
361         return 0;
362 }
363
364 /*-------------------------------------------------------------------------*/
365 /*                                usb_function                             */
366
367 static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
368                                                     unsigned length)
369 {
370         return alloc_ep_req(ep, length, length);
371 }
372
373 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
374 {
375         struct f_hidg *hidg = (struct f_hidg *) req->context;
376         struct f_hidg_req_list *req_list;
377         unsigned long flags;
378
379         req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
380         if (!req_list)
381                 return;
382
383         req_list->req = req;
384
385         spin_lock_irqsave(&hidg->spinlock, flags);
386         list_add_tail(&req_list->list, &hidg->completed_out_req);
387         spin_unlock_irqrestore(&hidg->spinlock, flags);
388
389         wake_up(&hidg->read_queue);
390 }
391
392 static int hidg_setup(struct usb_function *f,
393                 const struct usb_ctrlrequest *ctrl)
394 {
395         struct f_hidg                   *hidg = func_to_hidg(f);
396         struct usb_composite_dev        *cdev = f->config->cdev;
397         struct usb_request              *req  = cdev->req;
398         int status = 0;
399         __u16 value, length;
400
401         value   = __le16_to_cpu(ctrl->wValue);
402         length  = __le16_to_cpu(ctrl->wLength);
403
404         VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
405                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
406
407         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
408         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
409                   | HID_REQ_GET_REPORT):
410                 VDBG(cdev, "get_report\n");
411
412                 /* send an empty report */
413                 length = min_t(unsigned, length, hidg->report_length);
414                 memset(req->buf, 0x0, length);
415
416                 goto respond;
417                 break;
418
419         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
420                   | HID_REQ_GET_PROTOCOL):
421                 VDBG(cdev, "get_protocol\n");
422                 goto stall;
423                 break;
424
425         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
426                   | HID_REQ_SET_REPORT):
427                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
428                 goto stall;
429                 break;
430
431         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
432                   | HID_REQ_SET_PROTOCOL):
433                 VDBG(cdev, "set_protocol\n");
434                 goto stall;
435                 break;
436
437         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
438                   | USB_REQ_GET_DESCRIPTOR):
439                 switch (value >> 8) {
440                 case HID_DT_HID:
441                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
442                         length = min_t(unsigned short, length,
443                                                    hidg_desc.bLength);
444                         memcpy(req->buf, &hidg_desc, length);
445                         goto respond;
446                         break;
447                 case HID_DT_REPORT:
448                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
449                         length = min_t(unsigned short, length,
450                                                    hidg->report_desc_length);
451                         memcpy(req->buf, hidg->report_desc, length);
452                         goto respond;
453                         break;
454
455                 default:
456                         VDBG(cdev, "Unknown descriptor request 0x%x\n",
457                                  value >> 8);
458                         goto stall;
459                         break;
460                 }
461                 break;
462
463         default:
464                 VDBG(cdev, "Unknown request 0x%x\n",
465                          ctrl->bRequest);
466                 goto stall;
467                 break;
468         }
469
470 stall:
471         return -EOPNOTSUPP;
472
473 respond:
474         req->zero = 0;
475         req->length = length;
476         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
477         if (status < 0)
478                 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
479         return status;
480 }
481
482 static void hidg_disable(struct usb_function *f)
483 {
484         struct f_hidg *hidg = func_to_hidg(f);
485         struct f_hidg_req_list *list, *next;
486
487         usb_ep_disable(hidg->in_ep);
488         hidg->in_ep->driver_data = NULL;
489
490         usb_ep_disable(hidg->out_ep);
491         hidg->out_ep->driver_data = NULL;
492
493         list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
494                 list_del(&list->list);
495                 kfree(list);
496         }
497 }
498
499 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
500 {
501         struct usb_composite_dev                *cdev = f->config->cdev;
502         struct f_hidg                           *hidg = func_to_hidg(f);
503         int i, status = 0;
504
505         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
506
507         if (hidg->in_ep != NULL) {
508                 /* restart endpoint */
509                 if (hidg->in_ep->driver_data != NULL)
510                         usb_ep_disable(hidg->in_ep);
511
512                 status = config_ep_by_speed(f->config->cdev->gadget, f,
513                                             hidg->in_ep);
514                 if (status) {
515                         ERROR(cdev, "config_ep_by_speed FAILED!\n");
516                         goto fail;
517                 }
518                 status = usb_ep_enable(hidg->in_ep);
519                 if (status < 0) {
520                         ERROR(cdev, "Enable IN endpoint FAILED!\n");
521                         goto fail;
522                 }
523                 hidg->in_ep->driver_data = hidg;
524         }
525
526
527         if (hidg->out_ep != NULL) {
528                 /* restart endpoint */
529                 if (hidg->out_ep->driver_data != NULL)
530                         usb_ep_disable(hidg->out_ep);
531
532                 status = config_ep_by_speed(f->config->cdev->gadget, f,
533                                             hidg->out_ep);
534                 if (status) {
535                         ERROR(cdev, "config_ep_by_speed FAILED!\n");
536                         goto fail;
537                 }
538                 status = usb_ep_enable(hidg->out_ep);
539                 if (status < 0) {
540                         ERROR(cdev, "Enable IN endpoint FAILED!\n");
541                         goto fail;
542                 }
543                 hidg->out_ep->driver_data = hidg;
544
545                 /*
546                  * allocate a bunch of read buffers and queue them all at once.
547                  */
548                 for (i = 0; i < hidg->qlen && status == 0; i++) {
549                         struct usb_request *req =
550                                         hidg_alloc_ep_req(hidg->out_ep,
551                                                           hidg->report_length);
552                         if (req) {
553                                 req->complete = hidg_set_report_complete;
554                                 req->context  = hidg;
555                                 status = usb_ep_queue(hidg->out_ep, req,
556                                                       GFP_ATOMIC);
557                                 if (status)
558                                         ERROR(cdev, "%s queue req --> %d\n",
559                                                 hidg->out_ep->name, status);
560                         } else {
561                                 usb_ep_disable(hidg->out_ep);
562                                 hidg->out_ep->driver_data = NULL;
563                                 status = -ENOMEM;
564                                 goto fail;
565                         }
566                 }
567         }
568
569 fail:
570         return status;
571 }
572
573 const struct file_operations f_hidg_fops = {
574         .owner          = THIS_MODULE,
575         .open           = f_hidg_open,
576         .release        = f_hidg_release,
577         .write          = f_hidg_write,
578         .read           = f_hidg_read,
579         .poll           = f_hidg_poll,
580         .llseek         = noop_llseek,
581 };
582
583 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
584 {
585         struct usb_ep           *ep;
586         struct f_hidg           *hidg = func_to_hidg(f);
587         struct device           *device;
588         int                     status;
589         dev_t                   dev;
590
591         /* maybe allocate device-global string IDs, and patch descriptors */
592         if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
593                 status = usb_string_id(c->cdev);
594                 if (status < 0)
595                         return status;
596                 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
597                 hidg_interface_desc.iInterface = status;
598         }
599
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;
605
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         ep->driver_data = c->cdev;      /* claim */
612         hidg->in_ep = ep;
613
614         ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
615         if (!ep)
616                 goto fail;
617         ep->driver_data = c->cdev;      /* claim */
618         hidg->out_ep = ep;
619
620         /* preallocate request and buffer */
621         status = -ENOMEM;
622         hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
623         if (!hidg->req)
624                 goto fail;
625
626         hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
627         if (!hidg->req->buf)
628                 goto fail;
629
630         /* set descriptor dynamic values */
631         hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
632         hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
633         hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
634         hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
635         hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
636         hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
637         hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
638         hidg_desc.desc[0].wDescriptorLength =
639                 cpu_to_le16(hidg->report_desc_length);
640
641         hidg_hs_in_ep_desc.bEndpointAddress =
642                 hidg_fs_in_ep_desc.bEndpointAddress;
643         hidg_hs_out_ep_desc.bEndpointAddress =
644                 hidg_fs_out_ep_desc.bEndpointAddress;
645
646         status = usb_assign_descriptors(f, hidg_fs_descriptors,
647                         hidg_hs_descriptors, NULL);
648         if (status)
649                 goto fail;
650
651         mutex_init(&hidg->lock);
652         spin_lock_init(&hidg->spinlock);
653         init_waitqueue_head(&hidg->write_queue);
654         init_waitqueue_head(&hidg->read_queue);
655         INIT_LIST_HEAD(&hidg->completed_out_req);
656
657         /* create char device */
658         cdev_init(&hidg->cdev, &f_hidg_fops);
659         dev = MKDEV(major, hidg->minor);
660         status = cdev_add(&hidg->cdev, dev, 1);
661         if (status)
662                 goto fail_free_descs;
663
664         device = device_create(hidg_class, NULL, dev, NULL,
665                                "%s%d", "hidg", hidg->minor);
666         if (IS_ERR(device)) {
667                 status = PTR_ERR(device);
668                 goto del;
669         }
670
671         return 0;
672 del:
673         cdev_del(&hidg->cdev);
674 fail_free_descs:
675         usb_free_all_descriptors(f);
676 fail:
677         ERROR(f->config->cdev, "hidg_bind FAILED\n");
678         if (hidg->req != NULL) {
679                 kfree(hidg->req->buf);
680                 if (hidg->in_ep != NULL)
681                         usb_ep_free_request(hidg->in_ep, hidg->req);
682         }
683
684         return status;
685 }
686
687 #ifdef USBF_HID_INCLUDED
688 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
689 {
690         struct f_hidg *hidg = func_to_hidg(f);
691
692         device_destroy(hidg_class, MKDEV(major, hidg->minor));
693         cdev_del(&hidg->cdev);
694
695         /* disable/free request and end point */
696         usb_ep_disable(hidg->in_ep);
697         usb_ep_dequeue(hidg->in_ep, hidg->req);
698         kfree(hidg->req->buf);
699         usb_ep_free_request(hidg->in_ep, hidg->req);
700
701         usb_free_all_descriptors(f);
702
703         kfree(hidg->report_desc);
704         kfree(hidg);
705 }
706
707 /*-------------------------------------------------------------------------*/
708 /*                             usb_configuration                           */
709 int __init hidg_bind_config(struct usb_configuration *c,
710                             struct hidg_func_descriptor *fdesc, int index)
711 {
712         struct f_hidg *hidg;
713         int status;
714
715         if (index >= minors)
716                 return -ENOENT;
717
718         /* allocate and initialize one new instance */
719         hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
720         if (!hidg)
721                 return -ENOMEM;
722
723         hidg->minor = index;
724         hidg->bInterfaceSubClass = fdesc->subclass;
725         hidg->bInterfaceProtocol = fdesc->protocol;
726         hidg->report_length = fdesc->report_length;
727         hidg->report_desc_length = fdesc->report_desc_length;
728         hidg->report_desc = kmemdup(fdesc->report_desc,
729                                     fdesc->report_desc_length,
730                                     GFP_KERNEL);
731         if (!hidg->report_desc) {
732                 kfree(hidg);
733                 return -ENOMEM;
734         }
735
736         hidg->func.name    = "hid";
737         hidg->func.strings = ct_func_strings;
738         hidg->func.bind    = hidg_bind;
739         hidg->func.unbind  = hidg_unbind;
740         hidg->func.set_alt = hidg_set_alt;
741         hidg->func.disable = hidg_disable;
742         hidg->func.setup   = hidg_setup;
743
744         /* this could me made configurable at some point */
745         hidg->qlen         = 4;
746
747         status = usb_add_function(c, &hidg->func);
748         if (status)
749                 kfree(hidg);
750
751         return status;
752 }
753
754 #else
755
756 static inline int hidg_get_minor(void)
757 {
758         int ret;
759
760         ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
761
762         return ret;
763 }
764
765 static inline void hidg_put_minor(int minor)
766 {
767         ida_simple_remove(&hidg_ida, minor);
768 }
769
770 static void hidg_free_inst(struct usb_function_instance *f)
771 {
772         struct f_hid_opts *opts;
773
774         opts = container_of(f, struct f_hid_opts, func_inst);
775
776         mutex_lock(&hidg_ida_lock);
777
778         hidg_put_minor(opts->minor);
779         if (idr_is_empty(&hidg_ida.idr))
780                 ghid_cleanup();
781
782         mutex_unlock(&hidg_ida_lock);
783
784         if (opts->report_desc_alloc)
785                 kfree(opts->report_desc);
786
787         kfree(opts);
788 }
789
790 static struct usb_function_instance *hidg_alloc_inst(void)
791 {
792         struct f_hid_opts *opts;
793         struct usb_function_instance *ret;
794         int status = 0;
795
796         opts = kzalloc(sizeof(*opts), GFP_KERNEL);
797         if (!opts)
798                 return ERR_PTR(-ENOMEM);
799
800         opts->func_inst.free_func_inst = hidg_free_inst;
801         ret = &opts->func_inst;
802
803         mutex_lock(&hidg_ida_lock);
804
805         if (idr_is_empty(&hidg_ida.idr)) {
806                 status = ghid_setup(NULL, HIDG_MINORS);
807                 if (status)  {
808                         ret = ERR_PTR(status);
809                         kfree(opts);
810                         goto unlock;
811                 }
812         }
813
814         opts->minor = hidg_get_minor();
815         if (opts->minor < 0) {
816                 ret = ERR_PTR(opts->minor);
817                 kfree(opts);
818                 if (idr_is_empty(&hidg_ida.idr))
819                         ghid_cleanup();
820         }
821
822 unlock:
823         mutex_unlock(&hidg_ida_lock);
824         return ret;
825 }
826
827 static void hidg_free(struct usb_function *f)
828 {
829         struct f_hidg *hidg;
830
831         hidg = func_to_hidg(f);
832         kfree(hidg->report_desc);
833         kfree(hidg);
834 }
835
836 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
837 {
838         struct f_hidg *hidg = func_to_hidg(f);
839
840         device_destroy(hidg_class, MKDEV(major, hidg->minor));
841         cdev_del(&hidg->cdev);
842
843         /* disable/free request and end point */
844         usb_ep_disable(hidg->in_ep);
845         usb_ep_dequeue(hidg->in_ep, hidg->req);
846         kfree(hidg->req->buf);
847         usb_ep_free_request(hidg->in_ep, hidg->req);
848
849         usb_free_all_descriptors(f);
850 }
851
852 struct usb_function *hidg_alloc(struct usb_function_instance *fi)
853 {
854         struct f_hidg *hidg;
855         struct f_hid_opts *opts;
856
857         /* allocate and initialize one new instance */
858         hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
859         if (!hidg)
860                 return ERR_PTR(-ENOMEM);
861
862         opts = container_of(fi, struct f_hid_opts, func_inst);
863
864         hidg->minor = opts->minor;
865         hidg->bInterfaceSubClass = opts->subclass;
866         hidg->bInterfaceProtocol = opts->protocol;
867         hidg->report_length = opts->report_length;
868         hidg->report_desc_length = opts->report_desc_length;
869         if (opts->report_desc) {
870                 hidg->report_desc = kmemdup(opts->report_desc,
871                                             opts->report_desc_length,
872                                             GFP_KERNEL);
873                 if (!hidg->report_desc) {
874                         kfree(hidg);
875                         return ERR_PTR(-ENOMEM);
876                 }
877         }
878
879         hidg->func.name    = "hid";
880         hidg->func.strings = ct_func_strings;
881         hidg->func.bind    = hidg_bind;
882         hidg->func.unbind  = hidg_unbind;
883         hidg->func.set_alt = hidg_set_alt;
884         hidg->func.disable = hidg_disable;
885         hidg->func.setup   = hidg_setup;
886         hidg->func.free_func = hidg_free;
887
888         /* this could me made configurable at some point */
889         hidg->qlen         = 4;
890
891         return &hidg->func;
892 }
893
894 DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
895 MODULE_LICENSE("GPL");
896 MODULE_AUTHOR("Fabien Chouteau");
897
898 #endif
899
900 int ghid_setup(struct usb_gadget *g, int count)
901 {
902         int status;
903         dev_t dev;
904
905         hidg_class = class_create(THIS_MODULE, "hidg");
906         if (IS_ERR(hidg_class)) {
907                 hidg_class = NULL;
908                 return PTR_ERR(hidg_class);
909         }
910
911         status = alloc_chrdev_region(&dev, 0, count, "hidg");
912         if (!status) {
913                 major = MAJOR(dev);
914                 minors = count;
915         }
916
917         return status;
918 }
919
920 void ghid_cleanup(void)
921 {
922         if (major) {
923                 unregister_chrdev_region(MKDEV(major, 0), minors);
924                 major = minors = 0;
925         }
926
927         class_destroy(hidg_class);
928         hidg_class = NULL;
929 }