GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / usb / gadget / function / f_hid.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * f_hid.c -- USB HID function driver
4  *
5  * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/hid.h>
11 #include <linux/idr.h>
12 #include <linux/cdev.h>
13 #include <linux/mutex.h>
14 #include <linux/poll.h>
15 #include <linux/uaccess.h>
16 #include <linux/wait.h>
17 #include <linux/sched.h>
18 #include <linux/usb/g_hid.h>
19
20 #include "u_f.h"
21 #include "u_hid.h"
22
23 #define HIDG_MINORS     4
24
25 static int major, minors;
26 static struct class *hidg_class;
27 static DEFINE_IDA(hidg_ida);
28 static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
29
30 /*-------------------------------------------------------------------------*/
31 /*                            HID gadget struct                            */
32
33 struct f_hidg_req_list {
34         struct usb_request      *req;
35         unsigned int            pos;
36         struct list_head        list;
37 };
38
39 struct f_hidg {
40         /* configuration */
41         unsigned char                   bInterfaceSubClass;
42         unsigned char                   bInterfaceProtocol;
43         unsigned char                   protocol;
44         unsigned char                   idle;
45         unsigned short                  report_desc_length;
46         char                            *report_desc;
47         unsigned short                  report_length;
48         /*
49          * use_out_ep - if true, the OUT Endpoint (interrupt out method)
50          *              will be used to receive reports from the host
51          *              using functions with the "intout" suffix.
52          *              Otherwise, the OUT Endpoint will not be configured
53          *              and the SETUP/SET_REPORT method ("ssreport" suffix)
54          *              will be used to receive reports.
55          */
56         bool                            use_out_ep;
57
58         /* recv report */
59         spinlock_t                      read_spinlock;
60         wait_queue_head_t               read_queue;
61         /* recv report - interrupt out only (use_out_ep == 1) */
62         struct list_head                completed_out_req;
63         unsigned int                    qlen;
64         /* recv report - setup set_report only (use_out_ep == 0) */
65         char                            *set_report_buf;
66         unsigned int                    set_report_length;
67
68         /* send report */
69         spinlock_t                      write_spinlock;
70         bool                            write_pending;
71         wait_queue_head_t               write_queue;
72         struct usb_request              *req;
73
74         struct device                   dev;
75         struct cdev                     cdev;
76         struct usb_function             func;
77
78         struct usb_ep                   *in_ep;
79         struct usb_ep                   *out_ep;
80 };
81
82 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
83 {
84         return container_of(f, struct f_hidg, func);
85 }
86
87 static void hidg_release(struct device *dev)
88 {
89         struct f_hidg *hidg = container_of(dev, struct f_hidg, dev);
90
91         kfree(hidg->set_report_buf);
92         kfree(hidg);
93 }
94
95 /*-------------------------------------------------------------------------*/
96 /*                           Static descriptors                            */
97
98 static struct usb_interface_descriptor hidg_interface_desc = {
99         .bLength                = sizeof hidg_interface_desc,
100         .bDescriptorType        = USB_DT_INTERFACE,
101         /* .bInterfaceNumber    = DYNAMIC */
102         .bAlternateSetting      = 0,
103         /* .bNumEndpoints       = DYNAMIC (depends on use_out_ep) */
104         .bInterfaceClass        = USB_CLASS_HID,
105         /* .bInterfaceSubClass  = DYNAMIC */
106         /* .bInterfaceProtocol  = DYNAMIC */
107         /* .iInterface          = DYNAMIC */
108 };
109
110 static struct hid_descriptor hidg_desc = {
111         .bLength                        = sizeof hidg_desc,
112         .bDescriptorType                = HID_DT_HID,
113         .bcdHID                         = cpu_to_le16(0x0101),
114         .bCountryCode                   = 0x00,
115         .bNumDescriptors                = 0x1,
116         /*.desc[0].bDescriptorType      = DYNAMIC */
117         /*.desc[0].wDescriptorLenght    = DYNAMIC */
118 };
119
120 /* Super-Speed Support */
121
122 static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
123         .bLength                = USB_DT_ENDPOINT_SIZE,
124         .bDescriptorType        = USB_DT_ENDPOINT,
125         .bEndpointAddress       = USB_DIR_IN,
126         .bmAttributes           = USB_ENDPOINT_XFER_INT,
127         /*.wMaxPacketSize       = DYNAMIC */
128         .bInterval              = 4, /* FIXME: Add this field in the
129                                       * HID gadget configuration?
130                                       * (struct hidg_func_descriptor)
131                                       */
132 };
133
134 static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
135         .bLength                = sizeof(hidg_ss_in_comp_desc),
136         .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
137
138         /* .bMaxBurst           = 0, */
139         /* .bmAttributes        = 0, */
140         /* .wBytesPerInterval   = DYNAMIC */
141 };
142
143 static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
144         .bLength                = USB_DT_ENDPOINT_SIZE,
145         .bDescriptorType        = USB_DT_ENDPOINT,
146         .bEndpointAddress       = USB_DIR_OUT,
147         .bmAttributes           = USB_ENDPOINT_XFER_INT,
148         /*.wMaxPacketSize       = DYNAMIC */
149         .bInterval              = 4, /* FIXME: Add this field in the
150                                       * HID gadget configuration?
151                                       * (struct hidg_func_descriptor)
152                                       */
153 };
154
155 static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
156         .bLength                = sizeof(hidg_ss_out_comp_desc),
157         .bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
158
159         /* .bMaxBurst           = 0, */
160         /* .bmAttributes        = 0, */
161         /* .wBytesPerInterval   = DYNAMIC */
162 };
163
164 static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = {
165         (struct usb_descriptor_header *)&hidg_interface_desc,
166         (struct usb_descriptor_header *)&hidg_desc,
167         (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
168         (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
169         (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
170         (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
171         NULL,
172 };
173
174 static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = {
175         (struct usb_descriptor_header *)&hidg_interface_desc,
176         (struct usb_descriptor_header *)&hidg_desc,
177         (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
178         (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
179         NULL,
180 };
181
182 /* High-Speed Support */
183
184 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
185         .bLength                = USB_DT_ENDPOINT_SIZE,
186         .bDescriptorType        = USB_DT_ENDPOINT,
187         .bEndpointAddress       = USB_DIR_IN,
188         .bmAttributes           = USB_ENDPOINT_XFER_INT,
189         /*.wMaxPacketSize       = DYNAMIC */
190         .bInterval              = 4, /* FIXME: Add this field in the
191                                       * HID gadget configuration?
192                                       * (struct hidg_func_descriptor)
193                                       */
194 };
195
196 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
197         .bLength                = USB_DT_ENDPOINT_SIZE,
198         .bDescriptorType        = USB_DT_ENDPOINT,
199         .bEndpointAddress       = USB_DIR_OUT,
200         .bmAttributes           = USB_ENDPOINT_XFER_INT,
201         /*.wMaxPacketSize       = DYNAMIC */
202         .bInterval              = 4, /* FIXME: Add this field in the
203                                       * HID gadget configuration?
204                                       * (struct hidg_func_descriptor)
205                                       */
206 };
207
208 static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = {
209         (struct usb_descriptor_header *)&hidg_interface_desc,
210         (struct usb_descriptor_header *)&hidg_desc,
211         (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
212         (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
213         NULL,
214 };
215
216 static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = {
217         (struct usb_descriptor_header *)&hidg_interface_desc,
218         (struct usb_descriptor_header *)&hidg_desc,
219         (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
220         NULL,
221 };
222
223 /* Full-Speed Support */
224
225 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
226         .bLength                = USB_DT_ENDPOINT_SIZE,
227         .bDescriptorType        = USB_DT_ENDPOINT,
228         .bEndpointAddress       = USB_DIR_IN,
229         .bmAttributes           = USB_ENDPOINT_XFER_INT,
230         /*.wMaxPacketSize       = DYNAMIC */
231         .bInterval              = 10, /* FIXME: Add this field in the
232                                        * HID gadget configuration?
233                                        * (struct hidg_func_descriptor)
234                                        */
235 };
236
237 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
238         .bLength                = USB_DT_ENDPOINT_SIZE,
239         .bDescriptorType        = USB_DT_ENDPOINT,
240         .bEndpointAddress       = USB_DIR_OUT,
241         .bmAttributes           = USB_ENDPOINT_XFER_INT,
242         /*.wMaxPacketSize       = DYNAMIC */
243         .bInterval              = 10, /* FIXME: Add this field in the
244                                        * HID gadget configuration?
245                                        * (struct hidg_func_descriptor)
246                                        */
247 };
248
249 static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = {
250         (struct usb_descriptor_header *)&hidg_interface_desc,
251         (struct usb_descriptor_header *)&hidg_desc,
252         (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
253         (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
254         NULL,
255 };
256
257 static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = {
258         (struct usb_descriptor_header *)&hidg_interface_desc,
259         (struct usb_descriptor_header *)&hidg_desc,
260         (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
261         NULL,
262 };
263
264 /*-------------------------------------------------------------------------*/
265 /*                                 Strings                                 */
266
267 #define CT_FUNC_HID_IDX 0
268
269 static struct usb_string ct_func_string_defs[] = {
270         [CT_FUNC_HID_IDX].s     = "HID Interface",
271         {},                     /* end of list */
272 };
273
274 static struct usb_gadget_strings ct_func_string_table = {
275         .language       = 0x0409,       /* en-US */
276         .strings        = ct_func_string_defs,
277 };
278
279 static struct usb_gadget_strings *ct_func_strings[] = {
280         &ct_func_string_table,
281         NULL,
282 };
283
284 /*-------------------------------------------------------------------------*/
285 /*                              Char Device                                */
286
287 static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer,
288                                   size_t count, loff_t *ptr)
289 {
290         struct f_hidg *hidg = file->private_data;
291         struct f_hidg_req_list *list;
292         struct usb_request *req;
293         unsigned long flags;
294         int ret;
295
296         if (!count)
297                 return 0;
298
299         if (!access_ok(VERIFY_WRITE, buffer, count))
300                 return -EFAULT;
301
302         spin_lock_irqsave(&hidg->read_spinlock, flags);
303
304 #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req))
305
306         /* wait for at least one buffer to complete */
307         while (!READ_COND_INTOUT) {
308                 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
309                 if (file->f_flags & O_NONBLOCK)
310                         return -EAGAIN;
311
312                 if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT))
313                         return -ERESTARTSYS;
314
315                 spin_lock_irqsave(&hidg->read_spinlock, flags);
316         }
317
318         /* pick the first one */
319         list = list_first_entry(&hidg->completed_out_req,
320                                 struct f_hidg_req_list, list);
321
322         /*
323          * Remove this from list to protect it from beign free()
324          * while host disables our function
325          */
326         list_del(&list->list);
327
328         req = list->req;
329         count = min_t(unsigned int, count, req->actual - list->pos);
330         spin_unlock_irqrestore(&hidg->read_spinlock, flags);
331
332         /* copy to user outside spinlock */
333         count -= copy_to_user(buffer, req->buf + list->pos, count);
334         list->pos += count;
335
336         /*
337          * if this request is completely handled and transfered to
338          * userspace, remove its entry from the list and requeue it
339          * again. Otherwise, we will revisit it again upon the next
340          * call, taking into account its current read position.
341          */
342         if (list->pos == req->actual) {
343                 kfree(list);
344
345                 req->length = hidg->report_length;
346                 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
347                 if (ret < 0) {
348                         free_ep_req(hidg->out_ep, req);
349                         return ret;
350                 }
351         } else {
352                 spin_lock_irqsave(&hidg->read_spinlock, flags);
353                 list_add(&list->list, &hidg->completed_out_req);
354                 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
355
356                 wake_up(&hidg->read_queue);
357         }
358
359         return count;
360 }
361
362 #define READ_COND_SSREPORT (hidg->set_report_buf != NULL)
363
364 static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer,
365                                     size_t count, loff_t *ptr)
366 {
367         struct f_hidg *hidg = file->private_data;
368         char *tmp_buf = NULL;
369         unsigned long flags;
370
371         if (!count)
372                 return 0;
373
374         spin_lock_irqsave(&hidg->read_spinlock, flags);
375
376         while (!READ_COND_SSREPORT) {
377                 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
378                 if (file->f_flags & O_NONBLOCK)
379                         return -EAGAIN;
380
381                 if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT))
382                         return -ERESTARTSYS;
383
384                 spin_lock_irqsave(&hidg->read_spinlock, flags);
385         }
386
387         count = min_t(unsigned int, count, hidg->set_report_length);
388         tmp_buf = hidg->set_report_buf;
389         hidg->set_report_buf = NULL;
390
391         spin_unlock_irqrestore(&hidg->read_spinlock, flags);
392
393         if (tmp_buf != NULL) {
394                 count -= copy_to_user(buffer, tmp_buf, count);
395                 kfree(tmp_buf);
396         } else {
397                 count = -ENOMEM;
398         }
399
400         wake_up(&hidg->read_queue);
401
402         return count;
403 }
404
405 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
406                            size_t count, loff_t *ptr)
407 {
408         struct f_hidg *hidg = file->private_data;
409
410         if (hidg->use_out_ep)
411                 return f_hidg_intout_read(file, buffer, count, ptr);
412         else
413                 return f_hidg_ssreport_read(file, buffer, count, ptr);
414 }
415
416 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
417 {
418         struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
419         unsigned long flags;
420
421         if (req->status != 0) {
422                 ERROR(hidg->func.config->cdev,
423                         "End Point Request ERROR: %d\n", req->status);
424         }
425
426         spin_lock_irqsave(&hidg->write_spinlock, flags);
427         hidg->write_pending = 0;
428         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
429         wake_up(&hidg->write_queue);
430 }
431
432 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
433                             size_t count, loff_t *offp)
434 {
435         struct f_hidg *hidg  = file->private_data;
436         struct usb_request *req;
437         unsigned long flags;
438         ssize_t status = -ENOMEM;
439
440         if (!access_ok(VERIFY_READ, buffer, count))
441                 return -EFAULT;
442
443         spin_lock_irqsave(&hidg->write_spinlock, flags);
444
445         if (!hidg->req) {
446                 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
447                 return -ESHUTDOWN;
448         }
449
450 #define WRITE_COND (!hidg->write_pending)
451 try_again:
452         /* write queue */
453         while (!WRITE_COND) {
454                 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
455                 if (file->f_flags & O_NONBLOCK)
456                         return -EAGAIN;
457
458                 if (wait_event_interruptible_exclusive(
459                                 hidg->write_queue, WRITE_COND))
460                         return -ERESTARTSYS;
461
462                 spin_lock_irqsave(&hidg->write_spinlock, flags);
463         }
464
465         hidg->write_pending = 1;
466         req = hidg->req;
467         count  = min_t(unsigned, count, hidg->report_length);
468
469         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
470
471         if (!req) {
472                 ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
473                 status = -ESHUTDOWN;
474                 goto release_write_pending;
475         }
476
477         status = copy_from_user(req->buf, buffer, count);
478         if (status != 0) {
479                 ERROR(hidg->func.config->cdev,
480                         "copy_from_user error\n");
481                 status = -EINVAL;
482                 goto release_write_pending;
483         }
484
485         spin_lock_irqsave(&hidg->write_spinlock, flags);
486
487         /* when our function has been disabled by host */
488         if (!hidg->req) {
489                 free_ep_req(hidg->in_ep, req);
490                 /*
491                  * TODO
492                  * Should we fail with error here?
493                  */
494                 goto try_again;
495         }
496
497         req->status   = 0;
498         req->zero     = 0;
499         req->length   = count;
500         req->complete = f_hidg_req_complete;
501         req->context  = hidg;
502
503         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
504
505         if (!hidg->in_ep->enabled) {
506                 ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
507                 status = -ESHUTDOWN;
508                 goto release_write_pending;
509         }
510
511         status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
512         if (status < 0)
513                 goto release_write_pending;
514         else
515                 status = count;
516
517         return status;
518 release_write_pending:
519         spin_lock_irqsave(&hidg->write_spinlock, flags);
520         hidg->write_pending = 0;
521         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
522
523         wake_up(&hidg->write_queue);
524
525         return status;
526 }
527
528 static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
529 {
530         struct f_hidg   *hidg  = file->private_data;
531         __poll_t        ret = 0;
532
533         poll_wait(file, &hidg->read_queue, wait);
534         poll_wait(file, &hidg->write_queue, wait);
535
536         if (WRITE_COND)
537                 ret |= EPOLLOUT | EPOLLWRNORM;
538
539         if (hidg->use_out_ep) {
540                 if (READ_COND_INTOUT)
541                         ret |= EPOLLIN | EPOLLRDNORM;
542         } else {
543                 if (READ_COND_SSREPORT)
544                         ret |= EPOLLIN | EPOLLRDNORM;
545         }
546
547         return ret;
548 }
549
550 #undef WRITE_COND
551 #undef READ_COND_SSREPORT
552 #undef READ_COND_INTOUT
553
554 static int f_hidg_release(struct inode *inode, struct file *fd)
555 {
556         fd->private_data = NULL;
557         return 0;
558 }
559
560 static int f_hidg_open(struct inode *inode, struct file *fd)
561 {
562         struct f_hidg *hidg =
563                 container_of(inode->i_cdev, struct f_hidg, cdev);
564
565         fd->private_data = hidg;
566
567         return 0;
568 }
569
570 /*-------------------------------------------------------------------------*/
571 /*                                usb_function                             */
572
573 static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
574                                                     unsigned length)
575 {
576         return alloc_ep_req(ep, length);
577 }
578
579 static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req)
580 {
581         struct f_hidg *hidg = (struct f_hidg *) req->context;
582         struct usb_composite_dev *cdev = hidg->func.config->cdev;
583         struct f_hidg_req_list *req_list;
584         unsigned long flags;
585
586         switch (req->status) {
587         case 0:
588                 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
589                 if (!req_list) {
590                         ERROR(cdev, "Unable to allocate mem for req_list\n");
591                         goto free_req;
592                 }
593
594                 req_list->req = req;
595
596                 spin_lock_irqsave(&hidg->read_spinlock, flags);
597                 list_add_tail(&req_list->list, &hidg->completed_out_req);
598                 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
599
600                 wake_up(&hidg->read_queue);
601                 break;
602         default:
603                 ERROR(cdev, "Set report failed %d\n", req->status);
604                 /* FALLTHROUGH */
605         case -ECONNABORTED:             /* hardware forced ep reset */
606         case -ECONNRESET:               /* request dequeued */
607         case -ESHUTDOWN:                /* disconnect from host */
608 free_req:
609                 free_ep_req(ep, req);
610                 return;
611         }
612 }
613
614 static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req)
615 {
616         struct f_hidg *hidg = (struct f_hidg *)req->context;
617         struct usb_composite_dev *cdev = hidg->func.config->cdev;
618         char *new_buf = NULL;
619         unsigned long flags;
620
621         if (req->status != 0 || req->buf == NULL || req->actual == 0) {
622                 ERROR(cdev,
623                       "%s FAILED: status=%d, buf=%p, actual=%d\n",
624                       __func__, req->status, req->buf, req->actual);
625                 return;
626         }
627
628         spin_lock_irqsave(&hidg->read_spinlock, flags);
629
630         new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC);
631         if (new_buf == NULL) {
632                 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
633                 return;
634         }
635         hidg->set_report_buf = new_buf;
636
637         hidg->set_report_length = req->actual;
638         memcpy(hidg->set_report_buf, req->buf, req->actual);
639
640         spin_unlock_irqrestore(&hidg->read_spinlock, flags);
641
642         wake_up(&hidg->read_queue);
643 }
644
645 static int hidg_setup(struct usb_function *f,
646                 const struct usb_ctrlrequest *ctrl)
647 {
648         struct f_hidg                   *hidg = func_to_hidg(f);
649         struct usb_composite_dev        *cdev = f->config->cdev;
650         struct usb_request              *req  = cdev->req;
651         int status = 0;
652         __u16 value, length;
653
654         value   = __le16_to_cpu(ctrl->wValue);
655         length  = __le16_to_cpu(ctrl->wLength);
656
657         VDBG(cdev,
658              "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
659              __func__, ctrl->bRequestType, ctrl->bRequest, value);
660
661         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
662         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
663                   | HID_REQ_GET_REPORT):
664                 VDBG(cdev, "get_report\n");
665
666                 /* send an empty report */
667                 length = min_t(unsigned, length, hidg->report_length);
668                 memset(req->buf, 0x0, length);
669
670                 goto respond;
671                 break;
672
673         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
674                   | HID_REQ_GET_PROTOCOL):
675                 VDBG(cdev, "get_protocol\n");
676                 length = min_t(unsigned int, length, 1);
677                 ((u8 *) req->buf)[0] = hidg->protocol;
678                 goto respond;
679                 break;
680
681         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
682                   | HID_REQ_GET_IDLE):
683                 VDBG(cdev, "get_idle\n");
684                 length = min_t(unsigned int, length, 1);
685                 ((u8 *) req->buf)[0] = hidg->idle;
686                 goto respond;
687                 break;
688
689         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
690                   | HID_REQ_SET_REPORT):
691                 VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
692                 if (hidg->use_out_ep)
693                         goto stall;
694                 req->complete = hidg_ssreport_complete;
695                 req->context  = hidg;
696                 goto respond;
697                 break;
698
699         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
700                   | HID_REQ_SET_PROTOCOL):
701                 VDBG(cdev, "set_protocol\n");
702                 if (value > HID_REPORT_PROTOCOL)
703                         goto stall;
704                 length = 0;
705                 /*
706                  * We assume that programs implementing the Boot protocol
707                  * are also compatible with the Report Protocol
708                  */
709                 if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
710                         hidg->protocol = value;
711                         goto respond;
712                 }
713                 goto stall;
714                 break;
715
716         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
717                   | HID_REQ_SET_IDLE):
718                 VDBG(cdev, "set_idle\n");
719                 length = 0;
720                 hidg->idle = value >> 8;
721                 goto respond;
722                 break;
723
724         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
725                   | USB_REQ_GET_DESCRIPTOR):
726                 switch (value >> 8) {
727                 case HID_DT_HID:
728                 {
729                         struct hid_descriptor hidg_desc_copy = hidg_desc;
730
731                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
732                         hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
733                         hidg_desc_copy.desc[0].wDescriptorLength =
734                                 cpu_to_le16(hidg->report_desc_length);
735
736                         length = min_t(unsigned short, length,
737                                                    hidg_desc_copy.bLength);
738                         memcpy(req->buf, &hidg_desc_copy, length);
739                         goto respond;
740                         break;
741                 }
742                 case HID_DT_REPORT:
743                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
744                         length = min_t(unsigned short, length,
745                                                    hidg->report_desc_length);
746                         memcpy(req->buf, hidg->report_desc, length);
747                         goto respond;
748                         break;
749
750                 default:
751                         VDBG(cdev, "Unknown descriptor request 0x%x\n",
752                                  value >> 8);
753                         goto stall;
754                         break;
755                 }
756                 break;
757
758         default:
759                 VDBG(cdev, "Unknown request 0x%x\n",
760                          ctrl->bRequest);
761                 goto stall;
762                 break;
763         }
764
765 stall:
766         return -EOPNOTSUPP;
767
768 respond:
769         req->zero = 0;
770         req->length = length;
771         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
772         if (status < 0)
773                 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
774         return status;
775 }
776
777 static void hidg_disable(struct usb_function *f)
778 {
779         struct f_hidg *hidg = func_to_hidg(f);
780         struct f_hidg_req_list *list, *next;
781         unsigned long flags;
782
783         usb_ep_disable(hidg->in_ep);
784
785         if (hidg->out_ep) {
786                 usb_ep_disable(hidg->out_ep);
787
788                 spin_lock_irqsave(&hidg->read_spinlock, flags);
789                 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
790                         free_ep_req(hidg->out_ep, list->req);
791                         list_del(&list->list);
792                         kfree(list);
793                 }
794                 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
795         }
796
797         spin_lock_irqsave(&hidg->write_spinlock, flags);
798         if (!hidg->write_pending) {
799                 free_ep_req(hidg->in_ep, hidg->req);
800                 hidg->write_pending = 1;
801         }
802
803         hidg->req = NULL;
804         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
805 }
806
807 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
808 {
809         struct usb_composite_dev                *cdev = f->config->cdev;
810         struct f_hidg                           *hidg = func_to_hidg(f);
811         struct usb_request                      *req_in = NULL;
812         unsigned long                           flags;
813         int i, status = 0;
814
815         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
816
817         if (hidg->in_ep != NULL) {
818                 /* restart endpoint */
819                 usb_ep_disable(hidg->in_ep);
820
821                 status = config_ep_by_speed(f->config->cdev->gadget, f,
822                                             hidg->in_ep);
823                 if (status) {
824                         ERROR(cdev, "config_ep_by_speed FAILED!\n");
825                         goto fail;
826                 }
827                 status = usb_ep_enable(hidg->in_ep);
828                 if (status < 0) {
829                         ERROR(cdev, "Enable IN endpoint FAILED!\n");
830                         goto fail;
831                 }
832                 hidg->in_ep->driver_data = hidg;
833
834                 req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
835                 if (!req_in) {
836                         status = -ENOMEM;
837                         goto disable_ep_in;
838                 }
839         }
840
841         if (hidg->use_out_ep && hidg->out_ep != NULL) {
842                 /* restart endpoint */
843                 usb_ep_disable(hidg->out_ep);
844
845                 status = config_ep_by_speed(f->config->cdev->gadget, f,
846                                             hidg->out_ep);
847                 if (status) {
848                         ERROR(cdev, "config_ep_by_speed FAILED!\n");
849                         goto free_req_in;
850                 }
851                 status = usb_ep_enable(hidg->out_ep);
852                 if (status < 0) {
853                         ERROR(cdev, "Enable OUT endpoint FAILED!\n");
854                         goto free_req_in;
855                 }
856                 hidg->out_ep->driver_data = hidg;
857
858                 /*
859                  * allocate a bunch of read buffers and queue them all at once.
860                  */
861                 for (i = 0; i < hidg->qlen && status == 0; i++) {
862                         struct usb_request *req =
863                                         hidg_alloc_ep_req(hidg->out_ep,
864                                                           hidg->report_length);
865                         if (req) {
866                                 req->complete = hidg_intout_complete;
867                                 req->context  = hidg;
868                                 status = usb_ep_queue(hidg->out_ep, req,
869                                                       GFP_ATOMIC);
870                                 if (status) {
871                                         ERROR(cdev, "%s queue req --> %d\n",
872                                                 hidg->out_ep->name, status);
873                                         free_ep_req(hidg->out_ep, req);
874                                 }
875                         } else {
876                                 status = -ENOMEM;
877                                 goto disable_out_ep;
878                         }
879                 }
880         }
881
882         if (hidg->in_ep != NULL) {
883                 spin_lock_irqsave(&hidg->write_spinlock, flags);
884                 hidg->req = req_in;
885                 hidg->write_pending = 0;
886                 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
887
888                 wake_up(&hidg->write_queue);
889         }
890         return 0;
891 disable_out_ep:
892         if (hidg->out_ep)
893                 usb_ep_disable(hidg->out_ep);
894 free_req_in:
895         if (req_in)
896                 free_ep_req(hidg->in_ep, req_in);
897
898 disable_ep_in:
899         if (hidg->in_ep)
900                 usb_ep_disable(hidg->in_ep);
901
902 fail:
903         return status;
904 }
905
906 static const struct file_operations f_hidg_fops = {
907         .owner          = THIS_MODULE,
908         .open           = f_hidg_open,
909         .release        = f_hidg_release,
910         .write          = f_hidg_write,
911         .read           = f_hidg_read,
912         .poll           = f_hidg_poll,
913         .llseek         = noop_llseek,
914 };
915
916 static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
917 {
918         struct usb_ep           *ep;
919         struct f_hidg           *hidg = func_to_hidg(f);
920         struct usb_string       *us;
921         int                     status;
922
923         /* maybe allocate device-global string IDs, and patch descriptors */
924         us = usb_gstrings_attach(c->cdev, ct_func_strings,
925                                  ARRAY_SIZE(ct_func_string_defs));
926         if (IS_ERR(us))
927                 return PTR_ERR(us);
928         hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
929
930         /* allocate instance-specific interface IDs, and patch descriptors */
931         status = usb_interface_id(c, f);
932         if (status < 0)
933                 goto fail;
934         hidg_interface_desc.bInterfaceNumber = status;
935
936         /* allocate instance-specific endpoints */
937         status = -ENODEV;
938         ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
939         if (!ep)
940                 goto fail;
941         hidg->in_ep = ep;
942
943         hidg->out_ep = NULL;
944         if (hidg->use_out_ep) {
945                 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
946                 if (!ep)
947                         goto fail;
948                 hidg->out_ep = ep;
949         }
950
951         /* used only if use_out_ep == 1 */
952         hidg->set_report_buf = NULL;
953
954         /* set descriptor dynamic values */
955         hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
956         hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
957         hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1;
958         hidg->protocol = HID_REPORT_PROTOCOL;
959         hidg->idle = 1;
960         hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
961         hidg_ss_in_comp_desc.wBytesPerInterval =
962                                 cpu_to_le16(hidg->report_length);
963         hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
964         hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
965         hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
966         hidg_ss_out_comp_desc.wBytesPerInterval =
967                                 cpu_to_le16(hidg->report_length);
968         hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
969         hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
970         /*
971          * We can use hidg_desc struct here but we should not relay
972          * that its content won't change after returning from this function.
973          */
974         hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
975         hidg_desc.desc[0].wDescriptorLength =
976                 cpu_to_le16(hidg->report_desc_length);
977
978         hidg_hs_in_ep_desc.bEndpointAddress =
979                 hidg_fs_in_ep_desc.bEndpointAddress;
980         hidg_hs_out_ep_desc.bEndpointAddress =
981                 hidg_fs_out_ep_desc.bEndpointAddress;
982
983         hidg_ss_in_ep_desc.bEndpointAddress =
984                 hidg_fs_in_ep_desc.bEndpointAddress;
985         hidg_ss_out_ep_desc.bEndpointAddress =
986                 hidg_fs_out_ep_desc.bEndpointAddress;
987
988         if (hidg->use_out_ep)
989                 status = usb_assign_descriptors(f,
990                         hidg_fs_descriptors_intout,
991                         hidg_hs_descriptors_intout,
992                         hidg_ss_descriptors_intout,
993                         hidg_ss_descriptors_intout);
994         else
995                 status = usb_assign_descriptors(f,
996                         hidg_fs_descriptors_ssreport,
997                         hidg_hs_descriptors_ssreport,
998                         hidg_ss_descriptors_ssreport,
999                         hidg_ss_descriptors_ssreport);
1000
1001         if (status)
1002                 goto fail;
1003
1004         spin_lock_init(&hidg->write_spinlock);
1005         hidg->write_pending = 1;
1006         hidg->req = NULL;
1007         spin_lock_init(&hidg->read_spinlock);
1008         init_waitqueue_head(&hidg->write_queue);
1009         init_waitqueue_head(&hidg->read_queue);
1010         INIT_LIST_HEAD(&hidg->completed_out_req);
1011
1012         /* create char device */
1013         cdev_init(&hidg->cdev, &f_hidg_fops);
1014         status = cdev_device_add(&hidg->cdev, &hidg->dev);
1015         if (status)
1016                 goto fail_free_descs;
1017
1018         return 0;
1019 fail_free_descs:
1020         usb_free_all_descriptors(f);
1021 fail:
1022         ERROR(f->config->cdev, "hidg_bind FAILED\n");
1023         if (hidg->req != NULL)
1024                 free_ep_req(hidg->in_ep, hidg->req);
1025
1026         return status;
1027 }
1028
1029 static inline int hidg_get_minor(void)
1030 {
1031         int ret;
1032
1033         ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
1034         if (ret >= HIDG_MINORS) {
1035                 ida_simple_remove(&hidg_ida, ret);
1036                 ret = -ENODEV;
1037         }
1038
1039         return ret;
1040 }
1041
1042 static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
1043 {
1044         return container_of(to_config_group(item), struct f_hid_opts,
1045                             func_inst.group);
1046 }
1047
1048 static void hid_attr_release(struct config_item *item)
1049 {
1050         struct f_hid_opts *opts = to_f_hid_opts(item);
1051
1052         usb_put_function_instance(&opts->func_inst);
1053 }
1054
1055 static struct configfs_item_operations hidg_item_ops = {
1056         .release        = hid_attr_release,
1057 };
1058
1059 #define F_HID_OPT(name, prec, limit)                                    \
1060 static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
1061 {                                                                       \
1062         struct f_hid_opts *opts = to_f_hid_opts(item);                  \
1063         int result;                                                     \
1064                                                                         \
1065         mutex_lock(&opts->lock);                                        \
1066         result = sprintf(page, "%d\n", opts->name);                     \
1067         mutex_unlock(&opts->lock);                                      \
1068                                                                         \
1069         return result;                                                  \
1070 }                                                                       \
1071                                                                         \
1072 static ssize_t f_hid_opts_##name##_store(struct config_item *item,      \
1073                                          const char *page, size_t len)  \
1074 {                                                                       \
1075         struct f_hid_opts *opts = to_f_hid_opts(item);                  \
1076         int ret;                                                        \
1077         u##prec num;                                                    \
1078                                                                         \
1079         mutex_lock(&opts->lock);                                        \
1080         if (opts->refcnt) {                                             \
1081                 ret = -EBUSY;                                           \
1082                 goto end;                                               \
1083         }                                                               \
1084                                                                         \
1085         ret = kstrtou##prec(page, 0, &num);                             \
1086         if (ret)                                                        \
1087                 goto end;                                               \
1088                                                                         \
1089         if (num > limit) {                                              \
1090                 ret = -EINVAL;                                          \
1091                 goto end;                                               \
1092         }                                                               \
1093         opts->name = num;                                               \
1094         ret = len;                                                      \
1095                                                                         \
1096 end:                                                                    \
1097         mutex_unlock(&opts->lock);                                      \
1098         return ret;                                                     \
1099 }                                                                       \
1100                                                                         \
1101 CONFIGFS_ATTR(f_hid_opts_, name)
1102
1103 F_HID_OPT(subclass, 8, 255);
1104 F_HID_OPT(protocol, 8, 255);
1105 F_HID_OPT(no_out_endpoint, 8, 1);
1106 F_HID_OPT(report_length, 16, 65535);
1107
1108 static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
1109 {
1110         struct f_hid_opts *opts = to_f_hid_opts(item);
1111         int result;
1112
1113         mutex_lock(&opts->lock);
1114         result = opts->report_desc_length;
1115         memcpy(page, opts->report_desc, opts->report_desc_length);
1116         mutex_unlock(&opts->lock);
1117
1118         return result;
1119 }
1120
1121 static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
1122                                             const char *page, size_t len)
1123 {
1124         struct f_hid_opts *opts = to_f_hid_opts(item);
1125         int ret = -EBUSY;
1126         char *d;
1127
1128         mutex_lock(&opts->lock);
1129
1130         if (opts->refcnt)
1131                 goto end;
1132         if (len > PAGE_SIZE) {
1133                 ret = -ENOSPC;
1134                 goto end;
1135         }
1136         d = kmemdup(page, len, GFP_KERNEL);
1137         if (!d) {
1138                 ret = -ENOMEM;
1139                 goto end;
1140         }
1141         kfree(opts->report_desc);
1142         opts->report_desc = d;
1143         opts->report_desc_length = len;
1144         opts->report_desc_alloc = true;
1145         ret = len;
1146 end:
1147         mutex_unlock(&opts->lock);
1148         return ret;
1149 }
1150
1151 CONFIGFS_ATTR(f_hid_opts_, report_desc);
1152
1153 static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
1154 {
1155         struct f_hid_opts *opts = to_f_hid_opts(item);
1156
1157         return sprintf(page, "%d:%d\n", major, opts->minor);
1158 }
1159
1160 CONFIGFS_ATTR_RO(f_hid_opts_, dev);
1161
1162 static struct configfs_attribute *hid_attrs[] = {
1163         &f_hid_opts_attr_subclass,
1164         &f_hid_opts_attr_protocol,
1165         &f_hid_opts_attr_no_out_endpoint,
1166         &f_hid_opts_attr_report_length,
1167         &f_hid_opts_attr_report_desc,
1168         &f_hid_opts_attr_dev,
1169         NULL,
1170 };
1171
1172 static const struct config_item_type hid_func_type = {
1173         .ct_item_ops    = &hidg_item_ops,
1174         .ct_attrs       = hid_attrs,
1175         .ct_owner       = THIS_MODULE,
1176 };
1177
1178 static inline void hidg_put_minor(int minor)
1179 {
1180         ida_simple_remove(&hidg_ida, minor);
1181 }
1182
1183 static void hidg_free_inst(struct usb_function_instance *f)
1184 {
1185         struct f_hid_opts *opts;
1186
1187         opts = container_of(f, struct f_hid_opts, func_inst);
1188
1189         mutex_lock(&hidg_ida_lock);
1190
1191         hidg_put_minor(opts->minor);
1192         if (ida_is_empty(&hidg_ida))
1193                 ghid_cleanup();
1194
1195         mutex_unlock(&hidg_ida_lock);
1196
1197         if (opts->report_desc_alloc)
1198                 kfree(opts->report_desc);
1199
1200         kfree(opts);
1201 }
1202
1203 static struct usb_function_instance *hidg_alloc_inst(void)
1204 {
1205         struct f_hid_opts *opts;
1206         struct usb_function_instance *ret;
1207         int status = 0;
1208
1209         opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1210         if (!opts)
1211                 return ERR_PTR(-ENOMEM);
1212         mutex_init(&opts->lock);
1213         opts->func_inst.free_func_inst = hidg_free_inst;
1214         ret = &opts->func_inst;
1215
1216         mutex_lock(&hidg_ida_lock);
1217
1218         if (ida_is_empty(&hidg_ida)) {
1219                 status = ghid_setup(NULL, HIDG_MINORS);
1220                 if (status)  {
1221                         ret = ERR_PTR(status);
1222                         kfree(opts);
1223                         goto unlock;
1224                 }
1225         }
1226
1227         opts->minor = hidg_get_minor();
1228         if (opts->minor < 0) {
1229                 ret = ERR_PTR(opts->minor);
1230                 kfree(opts);
1231                 if (ida_is_empty(&hidg_ida))
1232                         ghid_cleanup();
1233                 goto unlock;
1234         }
1235         config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
1236
1237 unlock:
1238         mutex_unlock(&hidg_ida_lock);
1239         return ret;
1240 }
1241
1242 static void hidg_free(struct usb_function *f)
1243 {
1244         struct f_hidg *hidg;
1245         struct f_hid_opts *opts;
1246
1247         hidg = func_to_hidg(f);
1248         opts = container_of(f->fi, struct f_hid_opts, func_inst);
1249         put_device(&hidg->dev);
1250         mutex_lock(&opts->lock);
1251         --opts->refcnt;
1252         mutex_unlock(&opts->lock);
1253 }
1254
1255 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
1256 {
1257         struct f_hidg *hidg = func_to_hidg(f);
1258
1259         cdev_device_del(&hidg->cdev, &hidg->dev);
1260
1261         usb_free_all_descriptors(f);
1262 }
1263
1264 static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
1265 {
1266         struct f_hidg *hidg;
1267         struct f_hid_opts *opts;
1268         int ret;
1269
1270         /* allocate and initialize one new instance */
1271         hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
1272         if (!hidg)
1273                 return ERR_PTR(-ENOMEM);
1274
1275         opts = container_of(fi, struct f_hid_opts, func_inst);
1276
1277         mutex_lock(&opts->lock);
1278         ++opts->refcnt;
1279
1280         device_initialize(&hidg->dev);
1281         hidg->dev.release = hidg_release;
1282         hidg->dev.class = hidg_class;
1283         hidg->dev.devt = MKDEV(major, opts->minor);
1284         ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
1285         if (ret) {
1286                 --opts->refcnt;
1287                 mutex_unlock(&opts->lock);
1288                 return ERR_PTR(ret);
1289         }
1290
1291         hidg->bInterfaceSubClass = opts->subclass;
1292         hidg->bInterfaceProtocol = opts->protocol;
1293         hidg->report_length = opts->report_length;
1294         hidg->report_desc_length = opts->report_desc_length;
1295         if (opts->report_desc) {
1296                 hidg->report_desc = devm_kmemdup(&hidg->dev, opts->report_desc,
1297                                                  opts->report_desc_length,
1298                                                  GFP_KERNEL);
1299                 if (!hidg->report_desc) {
1300                         put_device(&hidg->dev);
1301                         --opts->refcnt;
1302                         mutex_unlock(&opts->lock);
1303                         return ERR_PTR(-ENOMEM);
1304                 }
1305         }
1306         hidg->use_out_ep = !opts->no_out_endpoint;
1307
1308         mutex_unlock(&opts->lock);
1309
1310         hidg->func.name    = "hid";
1311         hidg->func.bind    = hidg_bind;
1312         hidg->func.unbind  = hidg_unbind;
1313         hidg->func.set_alt = hidg_set_alt;
1314         hidg->func.disable = hidg_disable;
1315         hidg->func.setup   = hidg_setup;
1316         hidg->func.free_func = hidg_free;
1317
1318         /* this could me made configurable at some point */
1319         hidg->qlen         = 4;
1320
1321         return &hidg->func;
1322 }
1323
1324 DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1325 MODULE_LICENSE("GPL");
1326 MODULE_AUTHOR("Fabien Chouteau");
1327
1328 int ghid_setup(struct usb_gadget *g, int count)
1329 {
1330         int status;
1331         dev_t dev;
1332
1333         hidg_class = class_create(THIS_MODULE, "hidg");
1334         if (IS_ERR(hidg_class)) {
1335                 status = PTR_ERR(hidg_class);
1336                 hidg_class = NULL;
1337                 return status;
1338         }
1339
1340         status = alloc_chrdev_region(&dev, 0, count, "hidg");
1341         if (status) {
1342                 class_destroy(hidg_class);
1343                 hidg_class = NULL;
1344                 return status;
1345         }
1346
1347         major = MAJOR(dev);
1348         minors = count;
1349
1350         return 0;
1351 }
1352
1353 void ghid_cleanup(void)
1354 {
1355         if (major) {
1356                 unregister_chrdev_region(MKDEV(major, 0), minors);
1357                 major = minors = 0;
1358         }
1359
1360         class_destroy(hidg_class);
1361         hidg_class = NULL;
1362 }