usb: gadget: f_hid: use usb_gstrings_attach
[deliverable/linux.git] / drivers / usb / gadget / function / f_hid.c
CommitLineData
71adf118
FC
1/*
2 * f_hid.c -- USB HID function driver
3 *
4 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
71adf118
FC
10 */
11
12#include <linux/kernel.h>
71adf118
FC
13#include <linux/module.h>
14#include <linux/hid.h>
cb382536 15#include <linux/idr.h>
71adf118
FC
16#include <linux/cdev.h>
17#include <linux/mutex.h>
18#include <linux/poll.h>
71adf118
FC
19#include <linux/uaccess.h>
20#include <linux/wait.h>
721e2e91 21#include <linux/sched.h>
71adf118
FC
22#include <linux/usb/g_hid.h>
23
1efd54ea 24#include "u_f.h"
cb382536
AP
25#include "u_hid.h"
26
27#define HIDG_MINORS 4
1efd54ea 28
71adf118
FC
29static int major, minors;
30static struct class *hidg_class;
cb382536
AP
31static DEFINE_IDA(hidg_ida);
32static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
71adf118
FC
33
34/*-------------------------------------------------------------------------*/
35/* HID gadget struct */
36
99c51500
DM
37struct f_hidg_req_list {
38 struct usb_request *req;
39 unsigned int pos;
40 struct list_head list;
41};
42
71adf118
FC
43struct 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;
50
51 /* recv report */
99c51500 52 struct list_head completed_out_req;
71adf118
FC
53 spinlock_t spinlock;
54 wait_queue_head_t read_queue;
99c51500 55 unsigned int qlen;
71adf118
FC
56
57 /* send report */
58 struct mutex lock;
59 bool write_pending;
60 wait_queue_head_t write_queue;
61 struct usb_request *req;
62
63 int minor;
64 struct cdev cdev;
65 struct usb_function func;
99c51500 66
71adf118 67 struct usb_ep *in_ep;
99c51500 68 struct usb_ep *out_ep;
71adf118
FC
69};
70
71static inline struct f_hidg *func_to_hidg(struct usb_function *f)
72{
73 return container_of(f, struct f_hidg, func);
74}
75
76/*-------------------------------------------------------------------------*/
77/* Static descriptors */
78
79static struct usb_interface_descriptor hidg_interface_desc = {
80 .bLength = sizeof hidg_interface_desc,
81 .bDescriptorType = USB_DT_INTERFACE,
82 /* .bInterfaceNumber = DYNAMIC */
83 .bAlternateSetting = 0,
99c51500 84 .bNumEndpoints = 2,
71adf118
FC
85 .bInterfaceClass = USB_CLASS_HID,
86 /* .bInterfaceSubClass = DYNAMIC */
87 /* .bInterfaceProtocol = DYNAMIC */
88 /* .iInterface = DYNAMIC */
89};
90
91static 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 */
99};
100
101/* High-Speed Support */
102
103static 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)
112 */
113};
114
99c51500
DM
115static 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)
124 */
125};
126
71adf118
FC
127static 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,
99c51500 131 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
71adf118
FC
132 NULL,
133};
134
135/* Full-Speed Support */
136
137static 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)
146 */
147};
148
99c51500
DM
149static 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)
158 */
159};
160
71adf118
FC
161static 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,
99c51500 165 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
71adf118
FC
166 NULL,
167};
168
cb382536
AP
169/*-------------------------------------------------------------------------*/
170/* Strings */
171
172#define CT_FUNC_HID_IDX 0
173
174static struct usb_string ct_func_string_defs[] = {
175 [CT_FUNC_HID_IDX].s = "HID Interface",
176 {}, /* end of list */
177};
178
179static struct usb_gadget_strings ct_func_string_table = {
180 .language = 0x0409, /* en-US */
181 .strings = ct_func_string_defs,
182};
183
184static struct usb_gadget_strings *ct_func_strings[] = {
185 &ct_func_string_table,
186 NULL,
187};
188
71adf118
FC
189/*-------------------------------------------------------------------------*/
190/* Char Device */
191
192static ssize_t f_hidg_read(struct file *file, char __user *buffer,
193 size_t count, loff_t *ptr)
194{
99c51500
DM
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;
71adf118
FC
200
201 if (!count)
202 return 0;
203
204 if (!access_ok(VERIFY_WRITE, buffer, count))
205 return -EFAULT;
206
207 spin_lock_irqsave(&hidg->spinlock, flags);
208
99c51500 209#define READ_COND (!list_empty(&hidg->completed_out_req))
71adf118 210
99c51500 211 /* wait for at least one buffer to complete */
71adf118
FC
212 while (!READ_COND) {
213 spin_unlock_irqrestore(&hidg->spinlock, flags);
214 if (file->f_flags & O_NONBLOCK)
215 return -EAGAIN;
216
217 if (wait_event_interruptible(hidg->read_queue, READ_COND))
218 return -ERESTARTSYS;
219
220 spin_lock_irqsave(&hidg->spinlock, flags);
221 }
222
99c51500
DM
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);
71adf118
FC
228 spin_unlock_irqrestore(&hidg->spinlock, flags);
229
99c51500
DM
230 /* copy to user outside spinlock */
231 count -= copy_to_user(buffer, req->buf + list->pos, count);
232 list->pos += count;
233
234 /*
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.
239 */
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);
245
246 req->length = hidg->report_length;
247 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
248 if (ret < 0)
249 return ret;
250 }
71adf118
FC
251
252 return count;
253}
254
255static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
256{
257 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
258
259 if (req->status != 0) {
260 ERROR(hidg->func.config->cdev,
261 "End Point Request ERROR: %d\n", req->status);
262 }
263
264 hidg->write_pending = 0;
265 wake_up(&hidg->write_queue);
266}
267
268static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
269 size_t count, loff_t *offp)
270{
fd63b10b 271 struct f_hidg *hidg = file->private_data;
71adf118
FC
272 ssize_t status = -ENOMEM;
273
274 if (!access_ok(VERIFY_READ, buffer, count))
275 return -EFAULT;
276
277 mutex_lock(&hidg->lock);
278
279#define WRITE_COND (!hidg->write_pending)
280
281 /* write queue */
282 while (!WRITE_COND) {
283 mutex_unlock(&hidg->lock);
284 if (file->f_flags & O_NONBLOCK)
285 return -EAGAIN;
286
287 if (wait_event_interruptible_exclusive(
288 hidg->write_queue, WRITE_COND))
289 return -ERESTARTSYS;
290
291 mutex_lock(&hidg->lock);
292 }
293
294 count = min_t(unsigned, count, hidg->report_length);
295 status = copy_from_user(hidg->req->buf, buffer, count);
296
297 if (status != 0) {
298 ERROR(hidg->func.config->cdev,
299 "copy_from_user error\n");
300 mutex_unlock(&hidg->lock);
301 return -EINVAL;
302 }
303
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;
310
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;
319 }
320
321 mutex_unlock(&hidg->lock);
322
323 return status;
324}
325
326static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
327{
fd63b10b 328 struct f_hidg *hidg = file->private_data;
71adf118
FC
329 unsigned int ret = 0;
330
331 poll_wait(file, &hidg->read_queue, wait);
332 poll_wait(file, &hidg->write_queue, wait);
333
334 if (WRITE_COND)
335 ret |= POLLOUT | POLLWRNORM;
336
337 if (READ_COND)
338 ret |= POLLIN | POLLRDNORM;
339
340 return ret;
341}
342
343#undef WRITE_COND
344#undef READ_COND
345
346static int f_hidg_release(struct inode *inode, struct file *fd)
347{
348 fd->private_data = NULL;
349 return 0;
350}
351
352static int f_hidg_open(struct inode *inode, struct file *fd)
353{
354 struct f_hidg *hidg =
355 container_of(inode->i_cdev, struct f_hidg, cdev);
356
357 fd->private_data = hidg;
358
359 return 0;
360}
361
362/*-------------------------------------------------------------------------*/
363/* usb_function */
364
1efd54ea
AP
365static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
366 unsigned length)
71adf118 367{
1efd54ea 368 return alloc_ep_req(ep, length, length);
99c51500 369}
71adf118 370
99c51500
DM
371static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
372{
373 struct f_hidg *hidg = (struct f_hidg *) req->context;
374 struct f_hidg_req_list *req_list;
375 unsigned long flags;
71adf118 376
99c51500
DM
377 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
378 if (!req_list)
71adf118 379 return;
71adf118 380
99c51500
DM
381 req_list->req = req;
382
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);
71adf118
FC
386
387 wake_up(&hidg->read_queue);
71adf118
FC
388}
389
390static int hidg_setup(struct usb_function *f,
391 const struct usb_ctrlrequest *ctrl)
392{
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;
398
399 value = __le16_to_cpu(ctrl->wValue);
400 length = __le16_to_cpu(ctrl->wLength);
401
402 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
403 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
404
405 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
406 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
407 | HID_REQ_GET_REPORT):
408 VDBG(cdev, "get_report\n");
409
410 /* send an empty report */
411 length = min_t(unsigned, length, hidg->report_length);
412 memset(req->buf, 0x0, length);
413
414 goto respond;
415 break;
416
417 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
418 | HID_REQ_GET_PROTOCOL):
419 VDBG(cdev, "get_protocol\n");
420 goto stall;
421 break;
422
423 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
424 | HID_REQ_SET_REPORT):
425 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
99c51500 426 goto stall;
71adf118
FC
427 break;
428
429 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
430 | HID_REQ_SET_PROTOCOL):
431 VDBG(cdev, "set_protocol\n");
432 goto stall;
433 break;
434
435 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
436 | USB_REQ_GET_DESCRIPTOR):
437 switch (value >> 8) {
c240d78a
SB
438 case HID_DT_HID:
439 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
440 length = min_t(unsigned short, length,
441 hidg_desc.bLength);
442 memcpy(req->buf, &hidg_desc, length);
443 goto respond;
444 break;
71adf118
FC
445 case HID_DT_REPORT:
446 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
447 length = min_t(unsigned short, length,
448 hidg->report_desc_length);
449 memcpy(req->buf, hidg->report_desc, length);
450 goto respond;
451 break;
452
453 default:
1c1301dd 454 VDBG(cdev, "Unknown descriptor request 0x%x\n",
71adf118
FC
455 value >> 8);
456 goto stall;
457 break;
458 }
459 break;
460
461 default:
462 VDBG(cdev, "Unknown request 0x%x\n",
463 ctrl->bRequest);
464 goto stall;
465 break;
466 }
467
468stall:
469 return -EOPNOTSUPP;
470
471respond:
472 req->zero = 0;
473 req->length = length;
474 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
475 if (status < 0)
476 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
477 return status;
478}
479
480static void hidg_disable(struct usb_function *f)
481{
482 struct f_hidg *hidg = func_to_hidg(f);
99c51500 483 struct f_hidg_req_list *list, *next;
71adf118
FC
484
485 usb_ep_disable(hidg->in_ep);
486 hidg->in_ep->driver_data = NULL;
99c51500
DM
487
488 usb_ep_disable(hidg->out_ep);
489 hidg->out_ep->driver_data = NULL;
490
491 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
492 list_del(&list->list);
493 kfree(list);
494 }
71adf118
FC
495}
496
497static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
498{
499 struct usb_composite_dev *cdev = f->config->cdev;
500 struct f_hidg *hidg = func_to_hidg(f);
99c51500 501 int i, status = 0;
71adf118
FC
502
503 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
504
505 if (hidg->in_ep != NULL) {
506 /* restart endpoint */
507 if (hidg->in_ep->driver_data != NULL)
508 usb_ep_disable(hidg->in_ep);
509
ea2a1df7
TB
510 status = config_ep_by_speed(f->config->cdev->gadget, f,
511 hidg->in_ep);
512 if (status) {
513 ERROR(cdev, "config_ep_by_speed FAILED!\n");
514 goto fail;
515 }
72c973dd 516 status = usb_ep_enable(hidg->in_ep);
71adf118 517 if (status < 0) {
99c51500 518 ERROR(cdev, "Enable IN endpoint FAILED!\n");
71adf118
FC
519 goto fail;
520 }
521 hidg->in_ep->driver_data = hidg;
522 }
99c51500
DM
523
524
525 if (hidg->out_ep != NULL) {
526 /* restart endpoint */
527 if (hidg->out_ep->driver_data != NULL)
528 usb_ep_disable(hidg->out_ep);
529
530 status = config_ep_by_speed(f->config->cdev->gadget, f,
531 hidg->out_ep);
532 if (status) {
533 ERROR(cdev, "config_ep_by_speed FAILED!\n");
534 goto fail;
535 }
536 status = usb_ep_enable(hidg->out_ep);
537 if (status < 0) {
538 ERROR(cdev, "Enable IN endpoint FAILED!\n");
539 goto fail;
540 }
541 hidg->out_ep->driver_data = hidg;
542
543 /*
544 * allocate a bunch of read buffers and queue them all at once.
545 */
546 for (i = 0; i < hidg->qlen && status == 0; i++) {
547 struct usb_request *req =
548 hidg_alloc_ep_req(hidg->out_ep,
549 hidg->report_length);
550 if (req) {
551 req->complete = hidg_set_report_complete;
552 req->context = hidg;
553 status = usb_ep_queue(hidg->out_ep, req,
554 GFP_ATOMIC);
555 if (status)
556 ERROR(cdev, "%s queue req --> %d\n",
557 hidg->out_ep->name, status);
558 } else {
559 usb_ep_disable(hidg->out_ep);
560 hidg->out_ep->driver_data = NULL;
561 status = -ENOMEM;
562 goto fail;
563 }
564 }
565 }
566
71adf118
FC
567fail:
568 return status;
569}
570
571const struct file_operations f_hidg_fops = {
572 .owner = THIS_MODULE,
573 .open = f_hidg_open,
574 .release = f_hidg_release,
575 .write = f_hidg_write,
576 .read = f_hidg_read,
577 .poll = f_hidg_poll,
6038f373 578 .llseek = noop_llseek,
71adf118
FC
579};
580
cb382536 581static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
71adf118
FC
582{
583 struct usb_ep *ep;
584 struct f_hidg *hidg = func_to_hidg(f);
5ca8d3ec 585 struct usb_string *us;
63406087 586 struct device *device;
71adf118
FC
587 int status;
588 dev_t dev;
589
cb382536 590 /* maybe allocate device-global string IDs, and patch descriptors */
5ca8d3ec
AP
591 us = usb_gstrings_attach(c->cdev, ct_func_strings,
592 ARRAY_SIZE(ct_func_string_defs));
593 if (IS_ERR(us))
594 return PTR_ERR(us);
595 hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
cb382536 596
71adf118
FC
597 /* allocate instance-specific interface IDs, and patch descriptors */
598 status = usb_interface_id(c, f);
599 if (status < 0)
600 goto fail;
601 hidg_interface_desc.bInterfaceNumber = status;
602
71adf118
FC
603 /* allocate instance-specific endpoints */
604 status = -ENODEV;
605 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
606 if (!ep)
607 goto fail;
608 ep->driver_data = c->cdev; /* claim */
609 hidg->in_ep = ep;
610
99c51500
DM
611 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
612 if (!ep)
613 goto fail;
614 ep->driver_data = c->cdev; /* claim */
615 hidg->out_ep = ep;
616
71adf118
FC
617 /* preallocate request and buffer */
618 status = -ENOMEM;
619 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
620 if (!hidg->req)
621 goto fail;
622
71adf118
FC
623 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
624 if (!hidg->req->buf)
625 goto fail;
626
627 /* set descriptor dynamic values */
628 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
629 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
630 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
631 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
99c51500
DM
632 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
633 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
71adf118
FC
634 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
635 hidg_desc.desc[0].wDescriptorLength =
636 cpu_to_le16(hidg->report_desc_length);
637
10287bae
SAS
638 hidg_hs_in_ep_desc.bEndpointAddress =
639 hidg_fs_in_ep_desc.bEndpointAddress;
640 hidg_hs_out_ep_desc.bEndpointAddress =
641 hidg_fs_out_ep_desc.bEndpointAddress;
71adf118 642
10287bae
SAS
643 status = usb_assign_descriptors(f, hidg_fs_descriptors,
644 hidg_hs_descriptors, NULL);
645 if (status)
646 goto fail;
71adf118
FC
647
648 mutex_init(&hidg->lock);
649 spin_lock_init(&hidg->spinlock);
650 init_waitqueue_head(&hidg->write_queue);
651 init_waitqueue_head(&hidg->read_queue);
99c51500 652 INIT_LIST_HEAD(&hidg->completed_out_req);
71adf118
FC
653
654 /* create char device */
655 cdev_init(&hidg->cdev, &f_hidg_fops);
656 dev = MKDEV(major, hidg->minor);
657 status = cdev_add(&hidg->cdev, dev, 1);
658 if (status)
d12a8727 659 goto fail_free_descs;
71adf118 660
63406087
AP
661 device = device_create(hidg_class, NULL, dev, NULL,
662 "%s%d", "hidg", hidg->minor);
663 if (IS_ERR(device)) {
664 status = PTR_ERR(device);
665 goto del;
666 }
71adf118
FC
667
668 return 0;
63406087
AP
669del:
670 cdev_del(&hidg->cdev);
d12a8727
PM
671fail_free_descs:
672 usb_free_all_descriptors(f);
71adf118
FC
673fail:
674 ERROR(f->config->cdev, "hidg_bind FAILED\n");
675 if (hidg->req != NULL) {
676 kfree(hidg->req->buf);
677 if (hidg->in_ep != NULL)
678 usb_ep_free_request(hidg->in_ep, hidg->req);
679 }
680
71adf118
FC
681 return status;
682}
683
cb382536
AP
684static inline int hidg_get_minor(void)
685{
686 int ret;
687
688 ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
689
690 return ret;
691}
692
693static inline void hidg_put_minor(int minor)
694{
695 ida_simple_remove(&hidg_ida, minor);
696}
697
698static void hidg_free_inst(struct usb_function_instance *f)
699{
700 struct f_hid_opts *opts;
701
702 opts = container_of(f, struct f_hid_opts, func_inst);
703
704 mutex_lock(&hidg_ida_lock);
705
706 hidg_put_minor(opts->minor);
707 if (idr_is_empty(&hidg_ida.idr))
708 ghid_cleanup();
709
710 mutex_unlock(&hidg_ida_lock);
711
712 if (opts->report_desc_alloc)
713 kfree(opts->report_desc);
714
715 kfree(opts);
716}
717
718static struct usb_function_instance *hidg_alloc_inst(void)
719{
720 struct f_hid_opts *opts;
721 struct usb_function_instance *ret;
722 int status = 0;
723
724 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
725 if (!opts)
726 return ERR_PTR(-ENOMEM);
727
728 opts->func_inst.free_func_inst = hidg_free_inst;
729 ret = &opts->func_inst;
730
731 mutex_lock(&hidg_ida_lock);
732
733 if (idr_is_empty(&hidg_ida.idr)) {
734 status = ghid_setup(NULL, HIDG_MINORS);
735 if (status) {
736 ret = ERR_PTR(status);
737 kfree(opts);
738 goto unlock;
739 }
740 }
741
742 opts->minor = hidg_get_minor();
743 if (opts->minor < 0) {
744 ret = ERR_PTR(opts->minor);
745 kfree(opts);
746 if (idr_is_empty(&hidg_ida.idr))
747 ghid_cleanup();
748 }
749
750unlock:
751 mutex_unlock(&hidg_ida_lock);
752 return ret;
753}
754
755static void hidg_free(struct usb_function *f)
756{
757 struct f_hidg *hidg;
758
759 hidg = func_to_hidg(f);
760 kfree(hidg->report_desc);
761 kfree(hidg);
762}
763
764static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
765{
766 struct f_hidg *hidg = func_to_hidg(f);
767
768 device_destroy(hidg_class, MKDEV(major, hidg->minor));
769 cdev_del(&hidg->cdev);
770
771 /* disable/free request and end point */
772 usb_ep_disable(hidg->in_ep);
773 usb_ep_dequeue(hidg->in_ep, hidg->req);
774 kfree(hidg->req->buf);
775 usb_ep_free_request(hidg->in_ep, hidg->req);
776
777 usb_free_all_descriptors(f);
778}
779
780struct usb_function *hidg_alloc(struct usb_function_instance *fi)
781{
782 struct f_hidg *hidg;
783 struct f_hid_opts *opts;
784
785 /* allocate and initialize one new instance */
786 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
787 if (!hidg)
788 return ERR_PTR(-ENOMEM);
789
790 opts = container_of(fi, struct f_hid_opts, func_inst);
791
792 hidg->minor = opts->minor;
793 hidg->bInterfaceSubClass = opts->subclass;
794 hidg->bInterfaceProtocol = opts->protocol;
795 hidg->report_length = opts->report_length;
796 hidg->report_desc_length = opts->report_desc_length;
797 if (opts->report_desc) {
798 hidg->report_desc = kmemdup(opts->report_desc,
799 opts->report_desc_length,
800 GFP_KERNEL);
801 if (!hidg->report_desc) {
802 kfree(hidg);
803 return ERR_PTR(-ENOMEM);
804 }
805 }
806
807 hidg->func.name = "hid";
cb382536
AP
808 hidg->func.bind = hidg_bind;
809 hidg->func.unbind = hidg_unbind;
810 hidg->func.set_alt = hidg_set_alt;
811 hidg->func.disable = hidg_disable;
812 hidg->func.setup = hidg_setup;
813 hidg->func.free_func = hidg_free;
814
815 /* this could me made configurable at some point */
816 hidg->qlen = 4;
817
818 return &hidg->func;
819}
820
821DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
822MODULE_LICENSE("GPL");
823MODULE_AUTHOR("Fabien Chouteau");
824
cb382536 825int ghid_setup(struct usb_gadget *g, int count)
71adf118
FC
826{
827 int status;
828 dev_t dev;
829
830 hidg_class = class_create(THIS_MODULE, "hidg");
06529407
AP
831 if (IS_ERR(hidg_class)) {
832 hidg_class = NULL;
833 return PTR_ERR(hidg_class);
834 }
71adf118
FC
835
836 status = alloc_chrdev_region(&dev, 0, count, "hidg");
837 if (!status) {
838 major = MAJOR(dev);
839 minors = count;
840 }
841
842 return status;
843}
844
845void ghid_cleanup(void)
846{
847 if (major) {
848 unregister_chrdev_region(MKDEV(major, 0), minors);
849 major = minors = 0;
850 }
851
852 class_destroy(hidg_class);
853 hidg_class = NULL;
854}
This page took 0.362329 seconds and 5 git commands to generate.