blob: 62cf798155068107f61b8edd23640a7c369c573d [file] [log] [blame]
Fabien Chouteau71adf112010-04-08 09:31:15 +02001/*
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.
Fabien Chouteau71adf112010-04-08 09:31:15 +020010 */
11
12#include <linux/kernel.h>
Fabien Chouteau71adf112010-04-08 09:31:15 +020013#include <linux/module.h>
14#include <linux/hid.h>
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +010015#include <linux/idr.h>
Fabien Chouteau71adf112010-04-08 09:31:15 +020016#include <linux/cdev.h>
17#include <linux/mutex.h>
18#include <linux/poll.h>
Fabien Chouteau71adf112010-04-08 09:31:15 +020019#include <linux/uaccess.h>
20#include <linux/wait.h>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020021#include <linux/sched.h>
Fabien Chouteau71adf112010-04-08 09:31:15 +020022#include <linux/usb/g_hid.h>
23
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010024#include "u_f.h"
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +010025#include "u_hid.h"
26
27#define HIDG_MINORS 4
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010028
Fabien Chouteau71adf112010-04-08 09:31:15 +020029static int major, minors;
30static struct class *hidg_class;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +010031static DEFINE_IDA(hidg_ida);
32static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
Fabien Chouteau71adf112010-04-08 09:31:15 +020033
34/*-------------------------------------------------------------------------*/
35/* HID gadget struct */
36
Daniel Mack99c5150052012-06-13 12:54:45 +020037struct f_hidg_req_list {
38 struct usb_request *req;
39 unsigned int pos;
40 struct list_head list;
41};
42
Fabien Chouteau71adf112010-04-08 09:31:15 +020043struct 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 */
Daniel Mack99c5150052012-06-13 12:54:45 +020052 struct list_head completed_out_req;
Fabien Chouteau71adf112010-04-08 09:31:15 +020053 spinlock_t spinlock;
54 wait_queue_head_t read_queue;
Daniel Mack99c5150052012-06-13 12:54:45 +020055 unsigned int qlen;
Fabien Chouteau71adf112010-04-08 09:31:15 +020056
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;
Daniel Mack99c5150052012-06-13 12:54:45 +020066
Fabien Chouteau71adf112010-04-08 09:31:15 +020067 struct usb_ep *in_ep;
Daniel Mack99c5150052012-06-13 12:54:45 +020068 struct usb_ep *out_ep;
Fabien Chouteau71adf112010-04-08 09:31:15 +020069};
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,
Daniel Mack99c5150052012-06-13 12:54:45 +020084 .bNumEndpoints = 2,
Fabien Chouteau71adf112010-04-08 09:31:15 +020085 .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
Janusz Dziedzicdbf499c2016-11-03 10:27:15 +0100101/* Super-Speed Support */
102
103static struct usb_endpoint_descriptor hidg_ss_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
115static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
116 .bLength = sizeof(hidg_ss_in_comp_desc),
117 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
118
119 /* .bMaxBurst = 0, */
120 /* .bmAttributes = 0, */
121 /* .wBytesPerInterval = DYNAMIC */
122};
123
124static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
127 .bEndpointAddress = USB_DIR_OUT,
128 .bmAttributes = USB_ENDPOINT_XFER_INT,
129 /*.wMaxPacketSize = DYNAMIC */
130 .bInterval = 4, /* FIXME: Add this field in the
131 * HID gadget configuration?
132 * (struct hidg_func_descriptor)
133 */
134};
135
136static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
137 .bLength = sizeof(hidg_ss_out_comp_desc),
138 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
139
140 /* .bMaxBurst = 0, */
141 /* .bmAttributes = 0, */
142 /* .wBytesPerInterval = DYNAMIC */
143};
144
145static struct usb_descriptor_header *hidg_ss_descriptors[] = {
146 (struct usb_descriptor_header *)&hidg_interface_desc,
147 (struct usb_descriptor_header *)&hidg_desc,
148 (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
149 (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
150 (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
151 (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
152 NULL,
153};
154
Fabien Chouteau71adf112010-04-08 09:31:15 +0200155/* High-Speed Support */
156
157static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
158 .bLength = USB_DT_ENDPOINT_SIZE,
159 .bDescriptorType = USB_DT_ENDPOINT,
160 .bEndpointAddress = USB_DIR_IN,
161 .bmAttributes = USB_ENDPOINT_XFER_INT,
162 /*.wMaxPacketSize = DYNAMIC */
163 .bInterval = 4, /* FIXME: Add this field in the
164 * HID gadget configuration?
165 * (struct hidg_func_descriptor)
166 */
167};
168
Daniel Mack99c5150052012-06-13 12:54:45 +0200169static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
170 .bLength = USB_DT_ENDPOINT_SIZE,
171 .bDescriptorType = USB_DT_ENDPOINT,
172 .bEndpointAddress = USB_DIR_OUT,
173 .bmAttributes = USB_ENDPOINT_XFER_INT,
174 /*.wMaxPacketSize = DYNAMIC */
175 .bInterval = 4, /* FIXME: Add this field in the
176 * HID gadget configuration?
177 * (struct hidg_func_descriptor)
178 */
179};
180
Fabien Chouteau71adf112010-04-08 09:31:15 +0200181static struct usb_descriptor_header *hidg_hs_descriptors[] = {
182 (struct usb_descriptor_header *)&hidg_interface_desc,
183 (struct usb_descriptor_header *)&hidg_desc,
184 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
Daniel Mack99c5150052012-06-13 12:54:45 +0200185 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200186 NULL,
187};
188
189/* Full-Speed Support */
190
191static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
194 .bEndpointAddress = USB_DIR_IN,
195 .bmAttributes = USB_ENDPOINT_XFER_INT,
196 /*.wMaxPacketSize = DYNAMIC */
197 .bInterval = 10, /* FIXME: Add this field in the
198 * HID gadget configuration?
199 * (struct hidg_func_descriptor)
200 */
201};
202
Daniel Mack99c5150052012-06-13 12:54:45 +0200203static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
204 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = USB_DIR_OUT,
207 .bmAttributes = USB_ENDPOINT_XFER_INT,
208 /*.wMaxPacketSize = DYNAMIC */
209 .bInterval = 10, /* FIXME: Add this field in the
210 * HID gadget configuration?
211 * (struct hidg_func_descriptor)
212 */
213};
214
Fabien Chouteau71adf112010-04-08 09:31:15 +0200215static struct usb_descriptor_header *hidg_fs_descriptors[] = {
216 (struct usb_descriptor_header *)&hidg_interface_desc,
217 (struct usb_descriptor_header *)&hidg_desc,
218 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
Daniel Mack99c5150052012-06-13 12:54:45 +0200219 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200220 NULL,
221};
222
223/*-------------------------------------------------------------------------*/
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100224/* Strings */
225
226#define CT_FUNC_HID_IDX 0
227
228static struct usb_string ct_func_string_defs[] = {
229 [CT_FUNC_HID_IDX].s = "HID Interface",
230 {}, /* end of list */
231};
232
233static struct usb_gadget_strings ct_func_string_table = {
234 .language = 0x0409, /* en-US */
235 .strings = ct_func_string_defs,
236};
237
238static struct usb_gadget_strings *ct_func_strings[] = {
239 &ct_func_string_table,
240 NULL,
241};
242
243/*-------------------------------------------------------------------------*/
Fabien Chouteau71adf112010-04-08 09:31:15 +0200244/* Char Device */
245
246static ssize_t f_hidg_read(struct file *file, char __user *buffer,
247 size_t count, loff_t *ptr)
248{
Daniel Mack99c5150052012-06-13 12:54:45 +0200249 struct f_hidg *hidg = file->private_data;
250 struct f_hidg_req_list *list;
251 struct usb_request *req;
252 unsigned long flags;
253 int ret;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200254
255 if (!count)
256 return 0;
257
258 if (!access_ok(VERIFY_WRITE, buffer, count))
259 return -EFAULT;
260
261 spin_lock_irqsave(&hidg->spinlock, flags);
262
Daniel Mack99c5150052012-06-13 12:54:45 +0200263#define READ_COND (!list_empty(&hidg->completed_out_req))
Fabien Chouteau71adf112010-04-08 09:31:15 +0200264
Daniel Mack99c5150052012-06-13 12:54:45 +0200265 /* wait for at least one buffer to complete */
Fabien Chouteau71adf112010-04-08 09:31:15 +0200266 while (!READ_COND) {
267 spin_unlock_irqrestore(&hidg->spinlock, flags);
268 if (file->f_flags & O_NONBLOCK)
269 return -EAGAIN;
270
271 if (wait_event_interruptible(hidg->read_queue, READ_COND))
272 return -ERESTARTSYS;
273
274 spin_lock_irqsave(&hidg->spinlock, flags);
275 }
276
Daniel Mack99c5150052012-06-13 12:54:45 +0200277 /* pick the first one */
278 list = list_first_entry(&hidg->completed_out_req,
279 struct f_hidg_req_list, list);
280 req = list->req;
281 count = min_t(unsigned int, count, req->actual - list->pos);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200282 spin_unlock_irqrestore(&hidg->spinlock, flags);
283
Daniel Mack99c5150052012-06-13 12:54:45 +0200284 /* copy to user outside spinlock */
285 count -= copy_to_user(buffer, req->buf + list->pos, count);
286 list->pos += count;
287
288 /*
289 * if this request is completely handled and transfered to
290 * userspace, remove its entry from the list and requeue it
291 * again. Otherwise, we will revisit it again upon the next
292 * call, taking into account its current read position.
293 */
294 if (list->pos == req->actual) {
295 spin_lock_irqsave(&hidg->spinlock, flags);
296 list_del(&list->list);
297 kfree(list);
298 spin_unlock_irqrestore(&hidg->spinlock, flags);
299
300 req->length = hidg->report_length;
301 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
302 if (ret < 0)
303 return ret;
304 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200305
306 return count;
307}
308
309static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
310{
311 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
312
313 if (req->status != 0) {
314 ERROR(hidg->func.config->cdev,
315 "End Point Request ERROR: %d\n", req->status);
316 }
317
318 hidg->write_pending = 0;
319 wake_up(&hidg->write_queue);
320}
321
322static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
323 size_t count, loff_t *offp)
324{
Joe Perchesfd63b102010-07-12 13:50:11 -0700325 struct f_hidg *hidg = file->private_data;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200326 ssize_t status = -ENOMEM;
327
328 if (!access_ok(VERIFY_READ, buffer, count))
329 return -EFAULT;
330
331 mutex_lock(&hidg->lock);
332
333#define WRITE_COND (!hidg->write_pending)
334
335 /* write queue */
336 while (!WRITE_COND) {
337 mutex_unlock(&hidg->lock);
338 if (file->f_flags & O_NONBLOCK)
339 return -EAGAIN;
340
341 if (wait_event_interruptible_exclusive(
342 hidg->write_queue, WRITE_COND))
343 return -ERESTARTSYS;
344
345 mutex_lock(&hidg->lock);
346 }
347
348 count = min_t(unsigned, count, hidg->report_length);
349 status = copy_from_user(hidg->req->buf, buffer, count);
350
351 if (status != 0) {
352 ERROR(hidg->func.config->cdev,
353 "copy_from_user error\n");
354 mutex_unlock(&hidg->lock);
355 return -EINVAL;
356 }
357
358 hidg->req->status = 0;
359 hidg->req->zero = 0;
360 hidg->req->length = count;
361 hidg->req->complete = f_hidg_req_complete;
362 hidg->req->context = hidg;
363 hidg->write_pending = 1;
364
365 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
366 if (status < 0) {
367 ERROR(hidg->func.config->cdev,
368 "usb_ep_queue error on int endpoint %zd\n", status);
369 hidg->write_pending = 0;
370 wake_up(&hidg->write_queue);
371 } else {
372 status = count;
373 }
374
375 mutex_unlock(&hidg->lock);
376
377 return status;
378}
379
380static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
381{
Joe Perchesfd63b102010-07-12 13:50:11 -0700382 struct f_hidg *hidg = file->private_data;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200383 unsigned int ret = 0;
384
385 poll_wait(file, &hidg->read_queue, wait);
386 poll_wait(file, &hidg->write_queue, wait);
387
388 if (WRITE_COND)
389 ret |= POLLOUT | POLLWRNORM;
390
391 if (READ_COND)
392 ret |= POLLIN | POLLRDNORM;
393
394 return ret;
395}
396
397#undef WRITE_COND
398#undef READ_COND
399
400static int f_hidg_release(struct inode *inode, struct file *fd)
401{
402 fd->private_data = NULL;
403 return 0;
404}
405
406static int f_hidg_open(struct inode *inode, struct file *fd)
407{
408 struct f_hidg *hidg =
409 container_of(inode->i_cdev, struct f_hidg, cdev);
410
411 fd->private_data = hidg;
412
413 return 0;
414}
415
416/*-------------------------------------------------------------------------*/
417/* usb_function */
418
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100419static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
420 unsigned length)
Daniel Mack99c5150052012-06-13 12:54:45 +0200421{
Felipe F. Tonelloaadbe812016-08-23 18:24:49 +0100422 return alloc_ep_req(ep, length);
Daniel Mack99c5150052012-06-13 12:54:45 +0200423}
424
Fabien Chouteau71adf112010-04-08 09:31:15 +0200425static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
426{
Daniel Mack99c5150052012-06-13 12:54:45 +0200427 struct f_hidg *hidg = (struct f_hidg *) req->context;
Krzysztof Opasiak20d2ca92017-01-19 18:55:27 +0100428 struct usb_composite_dev *cdev = hidg->func.config->cdev;
Daniel Mack99c5150052012-06-13 12:54:45 +0200429 struct f_hidg_req_list *req_list;
430 unsigned long flags;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200431
Krzysztof Opasiak20d2ca92017-01-19 18:55:27 +0100432 switch (req->status) {
433 case 0:
434 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
435 if (!req_list) {
436 ERROR(cdev, "Unable to allocate mem for req_list\n");
437 goto free_req;
438 }
439
440 req_list->req = req;
441
442 spin_lock_irqsave(&hidg->spinlock, flags);
443 list_add_tail(&req_list->list, &hidg->completed_out_req);
444 spin_unlock_irqrestore(&hidg->spinlock, flags);
445
446 wake_up(&hidg->read_queue);
447 break;
448 default:
449 ERROR(cdev, "Set report failed %d\n", req->status);
450 /* FALLTHROUGH */
451 case -ECONNABORTED: /* hardware forced ep reset */
452 case -ECONNRESET: /* request dequeued */
453 case -ESHUTDOWN: /* disconnect from host */
454free_req:
455 free_ep_req(ep, req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200456 return;
Krzysztof Opasiak20d2ca92017-01-19 18:55:27 +0100457 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200458}
459
460static int hidg_setup(struct usb_function *f,
461 const struct usb_ctrlrequest *ctrl)
462{
463 struct f_hidg *hidg = func_to_hidg(f);
464 struct usb_composite_dev *cdev = f->config->cdev;
465 struct usb_request *req = cdev->req;
466 int status = 0;
467 __u16 value, length;
468
469 value = __le16_to_cpu(ctrl->wValue);
470 length = __le16_to_cpu(ctrl->wLength);
471
Julia Lawallc9b3bde2014-12-07 20:21:00 +0100472 VDBG(cdev,
473 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
474 __func__, ctrl->bRequestType, ctrl->bRequest, value);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200475
476 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
477 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
478 | HID_REQ_GET_REPORT):
479 VDBG(cdev, "get_report\n");
480
481 /* send an empty report */
482 length = min_t(unsigned, length, hidg->report_length);
483 memset(req->buf, 0x0, length);
484
485 goto respond;
486 break;
487
488 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
489 | HID_REQ_GET_PROTOCOL):
490 VDBG(cdev, "get_protocol\n");
491 goto stall;
492 break;
493
494 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
495 | HID_REQ_SET_REPORT):
Masanari Iida6774def2014-11-05 22:26:48 +0900496 VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
Daniel Mack99c5150052012-06-13 12:54:45 +0200497 goto stall;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200498 break;
499
500 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
501 | HID_REQ_SET_PROTOCOL):
502 VDBG(cdev, "set_protocol\n");
503 goto stall;
504 break;
505
506 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
507 | USB_REQ_GET_DESCRIPTOR):
508 switch (value >> 8) {
Sebastian Bauerc240d782011-07-21 15:40:07 +0200509 case HID_DT_HID:
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100510 {
511 struct hid_descriptor hidg_desc_copy = hidg_desc;
512
Sebastian Bauerc240d782011-07-21 15:40:07 +0200513 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100514 hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
515 hidg_desc_copy.desc[0].wDescriptorLength =
516 cpu_to_le16(hidg->report_desc_length);
517
Sebastian Bauerc240d782011-07-21 15:40:07 +0200518 length = min_t(unsigned short, length,
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100519 hidg_desc_copy.bLength);
520 memcpy(req->buf, &hidg_desc_copy, length);
Sebastian Bauerc240d782011-07-21 15:40:07 +0200521 goto respond;
522 break;
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100523 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200524 case HID_DT_REPORT:
525 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
526 length = min_t(unsigned short, length,
527 hidg->report_desc_length);
528 memcpy(req->buf, hidg->report_desc, length);
529 goto respond;
530 break;
531
532 default:
Masanari Iida1c1301d2012-04-19 00:04:46 +0900533 VDBG(cdev, "Unknown descriptor request 0x%x\n",
Fabien Chouteau71adf112010-04-08 09:31:15 +0200534 value >> 8);
535 goto stall;
536 break;
537 }
538 break;
539
540 default:
541 VDBG(cdev, "Unknown request 0x%x\n",
542 ctrl->bRequest);
543 goto stall;
544 break;
545 }
546
547stall:
548 return -EOPNOTSUPP;
549
550respond:
551 req->zero = 0;
552 req->length = length;
553 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
554 if (status < 0)
555 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
556 return status;
557}
558
559static void hidg_disable(struct usb_function *f)
560{
561 struct f_hidg *hidg = func_to_hidg(f);
Daniel Mack99c5150052012-06-13 12:54:45 +0200562 struct f_hidg_req_list *list, *next;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200563
564 usb_ep_disable(hidg->in_ep);
Daniel Mack99c5150052012-06-13 12:54:45 +0200565 usb_ep_disable(hidg->out_ep);
Daniel Mack99c5150052012-06-13 12:54:45 +0200566
567 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
568 list_del(&list->list);
569 kfree(list);
570 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200571}
572
573static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
574{
575 struct usb_composite_dev *cdev = f->config->cdev;
576 struct f_hidg *hidg = func_to_hidg(f);
Daniel Mack99c5150052012-06-13 12:54:45 +0200577 int i, status = 0;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200578
579 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
580
581 if (hidg->in_ep != NULL) {
582 /* restart endpoint */
Robert Baldyga2516a682015-09-16 12:10:46 +0200583 usb_ep_disable(hidg->in_ep);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200584
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300585 status = config_ep_by_speed(f->config->cdev->gadget, f,
586 hidg->in_ep);
587 if (status) {
588 ERROR(cdev, "config_ep_by_speed FAILED!\n");
589 goto fail;
590 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300591 status = usb_ep_enable(hidg->in_ep);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200592 if (status < 0) {
Daniel Mack99c5150052012-06-13 12:54:45 +0200593 ERROR(cdev, "Enable IN endpoint FAILED!\n");
Fabien Chouteau71adf112010-04-08 09:31:15 +0200594 goto fail;
595 }
596 hidg->in_ep->driver_data = hidg;
597 }
Daniel Mack99c5150052012-06-13 12:54:45 +0200598
599
600 if (hidg->out_ep != NULL) {
601 /* restart endpoint */
Robert Baldyga2516a682015-09-16 12:10:46 +0200602 usb_ep_disable(hidg->out_ep);
Daniel Mack99c5150052012-06-13 12:54:45 +0200603
604 status = config_ep_by_speed(f->config->cdev->gadget, f,
605 hidg->out_ep);
606 if (status) {
607 ERROR(cdev, "config_ep_by_speed FAILED!\n");
608 goto fail;
609 }
610 status = usb_ep_enable(hidg->out_ep);
611 if (status < 0) {
David Lechner43aef5c2017-01-02 17:28:39 -0600612 ERROR(cdev, "Enable OUT endpoint FAILED!\n");
Daniel Mack99c5150052012-06-13 12:54:45 +0200613 goto fail;
614 }
615 hidg->out_ep->driver_data = hidg;
616
617 /*
618 * allocate a bunch of read buffers and queue them all at once.
619 */
620 for (i = 0; i < hidg->qlen && status == 0; i++) {
621 struct usb_request *req =
622 hidg_alloc_ep_req(hidg->out_ep,
623 hidg->report_length);
624 if (req) {
625 req->complete = hidg_set_report_complete;
626 req->context = hidg;
627 status = usb_ep_queue(hidg->out_ep, req,
628 GFP_ATOMIC);
629 if (status)
630 ERROR(cdev, "%s queue req --> %d\n",
631 hidg->out_ep->name, status);
632 } else {
633 usb_ep_disable(hidg->out_ep);
Daniel Mack99c5150052012-06-13 12:54:45 +0200634 status = -ENOMEM;
635 goto fail;
636 }
637 }
638 }
639
Fabien Chouteau71adf112010-04-08 09:31:15 +0200640fail:
641 return status;
642}
643
Lad, Prabhakar7a3cc462015-02-04 17:49:36 +0000644static const struct file_operations f_hidg_fops = {
Fabien Chouteau71adf112010-04-08 09:31:15 +0200645 .owner = THIS_MODULE,
646 .open = f_hidg_open,
647 .release = f_hidg_release,
648 .write = f_hidg_write,
649 .read = f_hidg_read,
650 .poll = f_hidg_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200651 .llseek = noop_llseek,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200652};
653
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100654static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200655{
656 struct usb_ep *ep;
657 struct f_hidg *hidg = func_to_hidg(f);
Andrzej Pietrasiewicz5ca8d3ec2014-11-06 11:12:02 +0100658 struct usb_string *us;
Andrzej Pietrasiewicz63406082014-11-06 11:11:57 +0100659 struct device *device;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200660 int status;
661 dev_t dev;
662
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100663 /* maybe allocate device-global string IDs, and patch descriptors */
Andrzej Pietrasiewicz5ca8d3ec2014-11-06 11:12:02 +0100664 us = usb_gstrings_attach(c->cdev, ct_func_strings,
665 ARRAY_SIZE(ct_func_string_defs));
666 if (IS_ERR(us))
667 return PTR_ERR(us);
668 hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100669
Fabien Chouteau71adf112010-04-08 09:31:15 +0200670 /* allocate instance-specific interface IDs, and patch descriptors */
671 status = usb_interface_id(c, f);
672 if (status < 0)
673 goto fail;
674 hidg_interface_desc.bInterfaceNumber = status;
675
Fabien Chouteau71adf112010-04-08 09:31:15 +0200676 /* allocate instance-specific endpoints */
677 status = -ENODEV;
678 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
679 if (!ep)
680 goto fail;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200681 hidg->in_ep = ep;
682
Daniel Mack99c5150052012-06-13 12:54:45 +0200683 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
684 if (!ep)
685 goto fail;
Daniel Mack99c5150052012-06-13 12:54:45 +0200686 hidg->out_ep = ep;
687
Fabien Chouteau71adf112010-04-08 09:31:15 +0200688 /* preallocate request and buffer */
689 status = -ENOMEM;
Felipe F. Tonelloba1582f2016-08-23 18:24:51 +0100690 hidg->req = alloc_ep_req(hidg->in_ep, hidg->report_length);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200691 if (!hidg->req)
692 goto fail;
693
Fabien Chouteau71adf112010-04-08 09:31:15 +0200694 /* set descriptor dynamic values */
695 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
696 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
Janusz Dziedzicdbf499c2016-11-03 10:27:15 +0100697 hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
698 hidg_ss_in_comp_desc.wBytesPerInterval =
699 cpu_to_le16(hidg->report_length);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200700 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
701 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
Janusz Dziedzicdbf499c2016-11-03 10:27:15 +0100702 hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
703 hidg_ss_out_comp_desc.wBytesPerInterval =
704 cpu_to_le16(hidg->report_length);
Daniel Mack99c5150052012-06-13 12:54:45 +0200705 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
706 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100707 /*
708 * We can use hidg_desc struct here but we should not relay
709 * that its content won't change after returning from this function.
710 */
Fabien Chouteau71adf112010-04-08 09:31:15 +0200711 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
712 hidg_desc.desc[0].wDescriptorLength =
713 cpu_to_le16(hidg->report_desc_length);
714
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200715 hidg_hs_in_ep_desc.bEndpointAddress =
716 hidg_fs_in_ep_desc.bEndpointAddress;
717 hidg_hs_out_ep_desc.bEndpointAddress =
718 hidg_fs_out_ep_desc.bEndpointAddress;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200719
Janusz Dziedzicdbf499c2016-11-03 10:27:15 +0100720 hidg_ss_in_ep_desc.bEndpointAddress =
721 hidg_fs_in_ep_desc.bEndpointAddress;
722 hidg_ss_out_ep_desc.bEndpointAddress =
723 hidg_fs_out_ep_desc.bEndpointAddress;
724
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200725 status = usb_assign_descriptors(f, hidg_fs_descriptors,
Janusz Dziedzicdbf499c2016-11-03 10:27:15 +0100726 hidg_hs_descriptors, hidg_ss_descriptors, NULL);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200727 if (status)
728 goto fail;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200729
730 mutex_init(&hidg->lock);
731 spin_lock_init(&hidg->spinlock);
732 init_waitqueue_head(&hidg->write_queue);
733 init_waitqueue_head(&hidg->read_queue);
Daniel Mack99c5150052012-06-13 12:54:45 +0200734 INIT_LIST_HEAD(&hidg->completed_out_req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200735
736 /* create char device */
737 cdev_init(&hidg->cdev, &f_hidg_fops);
738 dev = MKDEV(major, hidg->minor);
739 status = cdev_add(&hidg->cdev, dev, 1);
740 if (status)
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +0530741 goto fail_free_descs;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200742
Andrzej Pietrasiewicz63406082014-11-06 11:11:57 +0100743 device = device_create(hidg_class, NULL, dev, NULL,
744 "%s%d", "hidg", hidg->minor);
745 if (IS_ERR(device)) {
746 status = PTR_ERR(device);
747 goto del;
748 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200749
750 return 0;
Andrzej Pietrasiewicz63406082014-11-06 11:11:57 +0100751del:
752 cdev_del(&hidg->cdev);
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +0530753fail_free_descs:
754 usb_free_all_descriptors(f);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200755fail:
756 ERROR(f->config->cdev, "hidg_bind FAILED\n");
Felipe F. Tonello14794d72016-08-23 18:24:50 +0100757 if (hidg->req != NULL)
758 free_ep_req(hidg->in_ep, hidg->req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200759
Fabien Chouteau71adf112010-04-08 09:31:15 +0200760 return status;
761}
762
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100763static inline int hidg_get_minor(void)
764{
765 int ret;
766
767 ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
Andrzej Pietrasiewicz774cf722015-07-24 09:48:40 +0200768 if (ret >= HIDG_MINORS) {
769 ida_simple_remove(&hidg_ida, ret);
770 ret = -ENODEV;
771 }
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100772
773 return ret;
774}
775
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100776static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
777{
778 return container_of(to_config_group(item), struct f_hid_opts,
779 func_inst.group);
780}
781
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100782static void hid_attr_release(struct config_item *item)
783{
784 struct f_hid_opts *opts = to_f_hid_opts(item);
785
786 usb_put_function_instance(&opts->func_inst);
787}
788
789static struct configfs_item_operations hidg_item_ops = {
790 .release = hid_attr_release,
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100791};
792
793#define F_HID_OPT(name, prec, limit) \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200794static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100795{ \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200796 struct f_hid_opts *opts = to_f_hid_opts(item); \
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100797 int result; \
798 \
799 mutex_lock(&opts->lock); \
800 result = sprintf(page, "%d\n", opts->name); \
801 mutex_unlock(&opts->lock); \
802 \
803 return result; \
804} \
805 \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200806static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100807 const char *page, size_t len) \
808{ \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200809 struct f_hid_opts *opts = to_f_hid_opts(item); \
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100810 int ret; \
811 u##prec num; \
812 \
813 mutex_lock(&opts->lock); \
814 if (opts->refcnt) { \
815 ret = -EBUSY; \
816 goto end; \
817 } \
818 \
819 ret = kstrtou##prec(page, 0, &num); \
820 if (ret) \
821 goto end; \
822 \
823 if (num > limit) { \
824 ret = -EINVAL; \
825 goto end; \
826 } \
827 opts->name = num; \
828 ret = len; \
829 \
830end: \
831 mutex_unlock(&opts->lock); \
832 return ret; \
833} \
834 \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200835CONFIGFS_ATTR(f_hid_opts_, name)
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100836
837F_HID_OPT(subclass, 8, 255);
838F_HID_OPT(protocol, 8, 255);
Andrzej Pietrasiewicz39a2ac272014-12-15 13:50:05 +0100839F_HID_OPT(report_length, 16, 65535);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100840
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200841static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100842{
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200843 struct f_hid_opts *opts = to_f_hid_opts(item);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100844 int result;
845
846 mutex_lock(&opts->lock);
847 result = opts->report_desc_length;
848 memcpy(page, opts->report_desc, opts->report_desc_length);
849 mutex_unlock(&opts->lock);
850
851 return result;
852}
853
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200854static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100855 const char *page, size_t len)
856{
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200857 struct f_hid_opts *opts = to_f_hid_opts(item);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100858 int ret = -EBUSY;
859 char *d;
860
861 mutex_lock(&opts->lock);
862
863 if (opts->refcnt)
864 goto end;
865 if (len > PAGE_SIZE) {
866 ret = -ENOSPC;
867 goto end;
868 }
869 d = kmemdup(page, len, GFP_KERNEL);
870 if (!d) {
871 ret = -ENOMEM;
872 goto end;
873 }
874 kfree(opts->report_desc);
875 opts->report_desc = d;
876 opts->report_desc_length = len;
877 opts->report_desc_alloc = true;
878 ret = len;
879end:
880 mutex_unlock(&opts->lock);
881 return ret;
882}
883
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200884CONFIGFS_ATTR(f_hid_opts_, report_desc);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100885
Johannes Berged6fe1f2016-06-23 22:28:54 +0200886static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
887{
888 struct f_hid_opts *opts = to_f_hid_opts(item);
889
890 return sprintf(page, "%d:%d\n", major, opts->minor);
891}
892
893CONFIGFS_ATTR_RO(f_hid_opts_, dev);
894
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100895static struct configfs_attribute *hid_attrs[] = {
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200896 &f_hid_opts_attr_subclass,
897 &f_hid_opts_attr_protocol,
898 &f_hid_opts_attr_report_length,
899 &f_hid_opts_attr_report_desc,
Johannes Berged6fe1f2016-06-23 22:28:54 +0200900 &f_hid_opts_attr_dev,
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100901 NULL,
902};
903
904static struct config_item_type hid_func_type = {
905 .ct_item_ops = &hidg_item_ops,
906 .ct_attrs = hid_attrs,
907 .ct_owner = THIS_MODULE,
908};
909
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100910static inline void hidg_put_minor(int minor)
911{
912 ida_simple_remove(&hidg_ida, minor);
913}
914
915static void hidg_free_inst(struct usb_function_instance *f)
916{
917 struct f_hid_opts *opts;
918
919 opts = container_of(f, struct f_hid_opts, func_inst);
920
921 mutex_lock(&hidg_ida_lock);
922
923 hidg_put_minor(opts->minor);
Matthew Wilcox99c49402016-12-14 15:09:13 -0800924 if (ida_is_empty(&hidg_ida))
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100925 ghid_cleanup();
926
927 mutex_unlock(&hidg_ida_lock);
928
929 if (opts->report_desc_alloc)
930 kfree(opts->report_desc);
931
932 kfree(opts);
933}
934
935static struct usb_function_instance *hidg_alloc_inst(void)
936{
937 struct f_hid_opts *opts;
938 struct usb_function_instance *ret;
939 int status = 0;
940
941 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
942 if (!opts)
943 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100944 mutex_init(&opts->lock);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100945 opts->func_inst.free_func_inst = hidg_free_inst;
946 ret = &opts->func_inst;
947
948 mutex_lock(&hidg_ida_lock);
949
Matthew Wilcox99c49402016-12-14 15:09:13 -0800950 if (ida_is_empty(&hidg_ida)) {
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100951 status = ghid_setup(NULL, HIDG_MINORS);
952 if (status) {
953 ret = ERR_PTR(status);
954 kfree(opts);
955 goto unlock;
956 }
957 }
958
959 opts->minor = hidg_get_minor();
960 if (opts->minor < 0) {
961 ret = ERR_PTR(opts->minor);
962 kfree(opts);
Matthew Wilcox99c49402016-12-14 15:09:13 -0800963 if (ida_is_empty(&hidg_ida))
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100964 ghid_cleanup();
Dan Carpenter828f6142014-11-13 09:19:47 +0300965 goto unlock;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100966 }
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100967 config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100968
969unlock:
970 mutex_unlock(&hidg_ida_lock);
971 return ret;
972}
973
974static void hidg_free(struct usb_function *f)
975{
976 struct f_hidg *hidg;
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100977 struct f_hid_opts *opts;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100978
979 hidg = func_to_hidg(f);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100980 opts = container_of(f->fi, struct f_hid_opts, func_inst);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100981 kfree(hidg->report_desc);
982 kfree(hidg);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100983 mutex_lock(&opts->lock);
984 --opts->refcnt;
985 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100986}
987
Fabien Chouteau71adf112010-04-08 09:31:15 +0200988static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
989{
990 struct f_hidg *hidg = func_to_hidg(f);
991
992 device_destroy(hidg_class, MKDEV(major, hidg->minor));
993 cdev_del(&hidg->cdev);
994
995 /* disable/free request and end point */
996 usb_ep_disable(hidg->in_ep);
Felipe F. Tonello14794d72016-08-23 18:24:50 +0100997 free_ep_req(hidg->in_ep, hidg->req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200998
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200999 usb_free_all_descriptors(f);
Fabien Chouteau71adf112010-04-08 09:31:15 +02001000}
1001
Fengguang Wu0fc57ea2014-11-12 22:44:05 +08001002static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
Fabien Chouteau71adf112010-04-08 09:31:15 +02001003{
1004 struct f_hidg *hidg;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001005 struct f_hid_opts *opts;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001006
1007 /* allocate and initialize one new instance */
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001008 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
Fabien Chouteau71adf112010-04-08 09:31:15 +02001009 if (!hidg)
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001010 return ERR_PTR(-ENOMEM);
Fabien Chouteau71adf112010-04-08 09:31:15 +02001011
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001012 opts = container_of(fi, struct f_hid_opts, func_inst);
1013
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +01001014 mutex_lock(&opts->lock);
1015 ++opts->refcnt;
1016
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001017 hidg->minor = opts->minor;
1018 hidg->bInterfaceSubClass = opts->subclass;
1019 hidg->bInterfaceProtocol = opts->protocol;
1020 hidg->report_length = opts->report_length;
1021 hidg->report_desc_length = opts->report_desc_length;
1022 if (opts->report_desc) {
1023 hidg->report_desc = kmemdup(opts->report_desc,
1024 opts->report_desc_length,
1025 GFP_KERNEL);
1026 if (!hidg->report_desc) {
1027 kfree(hidg);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +01001028 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001029 return ERR_PTR(-ENOMEM);
1030 }
Fabien Chouteau71adf112010-04-08 09:31:15 +02001031 }
1032
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +01001033 mutex_unlock(&opts->lock);
1034
Fabien Chouteau71adf112010-04-08 09:31:15 +02001035 hidg->func.name = "hid";
Fabien Chouteau71adf112010-04-08 09:31:15 +02001036 hidg->func.bind = hidg_bind;
1037 hidg->func.unbind = hidg_unbind;
1038 hidg->func.set_alt = hidg_set_alt;
1039 hidg->func.disable = hidg_disable;
1040 hidg->func.setup = hidg_setup;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001041 hidg->func.free_func = hidg_free;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001042
Daniel Mack99c5150052012-06-13 12:54:45 +02001043 /* this could me made configurable at some point */
1044 hidg->qlen = 4;
1045
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001046 return &hidg->func;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001047}
1048
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001049DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1050MODULE_LICENSE("GPL");
1051MODULE_AUTHOR("Fabien Chouteau");
1052
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001053int ghid_setup(struct usb_gadget *g, int count)
Fabien Chouteau71adf112010-04-08 09:31:15 +02001054{
1055 int status;
1056 dev_t dev;
1057
1058 hidg_class = class_create(THIS_MODULE, "hidg");
Andrzej Pietrasiewicz06529402014-11-06 11:11:56 +01001059 if (IS_ERR(hidg_class)) {
Dan Carpenter0448d382014-11-13 09:20:59 +03001060 status = PTR_ERR(hidg_class);
Andrzej Pietrasiewicz06529402014-11-06 11:11:56 +01001061 hidg_class = NULL;
Dan Carpenter0448d382014-11-13 09:20:59 +03001062 return status;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001063 }
1064
Fabien Chouteau71adf112010-04-08 09:31:15 +02001065 status = alloc_chrdev_region(&dev, 0, count, "hidg");
Dan Carpenter0448d382014-11-13 09:20:59 +03001066 if (status) {
1067 class_destroy(hidg_class);
1068 hidg_class = NULL;
1069 return status;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001070 }
1071
Dan Carpenter0448d382014-11-13 09:20:59 +03001072 major = MAJOR(dev);
1073 minors = count;
1074
1075 return 0;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001076}
1077
1078void ghid_cleanup(void)
1079{
1080 if (major) {
1081 unregister_chrdev_region(MKDEV(major, 0), minors);
1082 major = minors = 0;
1083 }
1084
1085 class_destroy(hidg_class);
1086 hidg_class = NULL;
1087}