blob: b6d4b484c51aeefb154a653897391fecdca19ae6 [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 Mack99c51502012-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 Mack99c51502012-06-13 12:54:45 +020052 struct list_head completed_out_req;
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +010053 spinlock_t read_spinlock;
Fabien Chouteau71adf112010-04-08 09:31:15 +020054 wait_queue_head_t read_queue;
Daniel Mack99c51502012-06-13 12:54:45 +020055 unsigned int qlen;
Fabien Chouteau71adf112010-04-08 09:31:15 +020056
57 /* send report */
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +010058 spinlock_t write_spinlock;
Fabien Chouteau71adf112010-04-08 09:31:15 +020059 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 Mack99c51502012-06-13 12:54:45 +020066
Fabien Chouteau71adf112010-04-08 09:31:15 +020067 struct usb_ep *in_ep;
Daniel Mack99c51502012-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 Mack99c51502012-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
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
Daniel Mack99c51502012-06-13 12:54:45 +0200115static 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
Fabien Chouteau71adf112010-04-08 09:31:15 +0200127static 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,
Daniel Mack99c51502012-06-13 12:54:45 +0200131 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200132 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
Daniel Mack99c51502012-06-13 12:54:45 +0200149static 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
Fabien Chouteau71adf112010-04-08 09:31:15 +0200161static 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,
Daniel Mack99c51502012-06-13 12:54:45 +0200165 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200166 NULL,
167};
168
169/*-------------------------------------------------------------------------*/
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100170/* 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
189/*-------------------------------------------------------------------------*/
Fabien Chouteau71adf112010-04-08 09:31:15 +0200190/* Char Device */
191
192static ssize_t f_hidg_read(struct file *file, char __user *buffer,
193 size_t count, loff_t *ptr)
194{
Daniel Mack99c51502012-06-13 12:54:45 +0200195 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;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200200
201 if (!count)
202 return 0;
203
204 if (!access_ok(VERIFY_WRITE, buffer, count))
205 return -EFAULT;
206
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100207 spin_lock_irqsave(&hidg->read_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200208
Daniel Mack99c51502012-06-13 12:54:45 +0200209#define READ_COND (!list_empty(&hidg->completed_out_req))
Fabien Chouteau71adf112010-04-08 09:31:15 +0200210
Daniel Mack99c51502012-06-13 12:54:45 +0200211 /* wait for at least one buffer to complete */
Fabien Chouteau71adf112010-04-08 09:31:15 +0200212 while (!READ_COND) {
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100213 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200214 if (file->f_flags & O_NONBLOCK)
215 return -EAGAIN;
216
217 if (wait_event_interruptible(hidg->read_queue, READ_COND))
218 return -ERESTARTSYS;
219
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100220 spin_lock_irqsave(&hidg->read_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200221 }
222
Daniel Mack99c51502012-06-13 12:54:45 +0200223 /* pick the first one */
224 list = list_first_entry(&hidg->completed_out_req,
225 struct f_hidg_req_list, list);
Krzysztof Opasiakd3acd942017-01-19 18:55:28 +0100226
227 /*
228 * Remove this from list to protect it from beign free()
229 * while host disables our function
230 */
231 list_del(&list->list);
232
Daniel Mack99c51502012-06-13 12:54:45 +0200233 req = list->req;
234 count = min_t(unsigned int, count, req->actual - list->pos);
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100235 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200236
Daniel Mack99c51502012-06-13 12:54:45 +0200237 /* copy to user outside spinlock */
238 count -= copy_to_user(buffer, req->buf + list->pos, count);
239 list->pos += count;
240
241 /*
242 * if this request is completely handled and transfered to
243 * userspace, remove its entry from the list and requeue it
244 * again. Otherwise, we will revisit it again upon the next
245 * call, taking into account its current read position.
246 */
247 if (list->pos == req->actual) {
Daniel Mack99c51502012-06-13 12:54:45 +0200248 kfree(list);
Daniel Mack99c51502012-06-13 12:54:45 +0200249
250 req->length = hidg->report_length;
251 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
Krzysztof Opasiakd3acd942017-01-19 18:55:28 +0100252 if (ret < 0) {
253 free_ep_req(hidg->out_ep, req);
Daniel Mack99c51502012-06-13 12:54:45 +0200254 return ret;
Krzysztof Opasiakd3acd942017-01-19 18:55:28 +0100255 }
256 } else {
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100257 spin_lock_irqsave(&hidg->read_spinlock, flags);
Krzysztof Opasiakd3acd942017-01-19 18:55:28 +0100258 list_add(&list->list, &hidg->completed_out_req);
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100259 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
Krzysztof Opasiakd3acd942017-01-19 18:55:28 +0100260
261 wake_up(&hidg->read_queue);
Daniel Mack99c51502012-06-13 12:54:45 +0200262 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200263
264 return count;
265}
266
267static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
268{
269 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100270 unsigned long flags;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200271
272 if (req->status != 0) {
273 ERROR(hidg->func.config->cdev,
274 "End Point Request ERROR: %d\n", req->status);
275 }
276
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100277 spin_lock_irqsave(&hidg->write_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200278 hidg->write_pending = 0;
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100279 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200280 wake_up(&hidg->write_queue);
281}
282
283static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
284 size_t count, loff_t *offp)
285{
Joe Perchesfd63b102010-07-12 13:50:11 -0700286 struct f_hidg *hidg = file->private_data;
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100287 unsigned long flags;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200288 ssize_t status = -ENOMEM;
289
290 if (!access_ok(VERIFY_READ, buffer, count))
291 return -EFAULT;
292
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100293 spin_lock_irqsave(&hidg->write_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200294
295#define WRITE_COND (!hidg->write_pending)
296
297 /* write queue */
298 while (!WRITE_COND) {
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100299 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200300 if (file->f_flags & O_NONBLOCK)
301 return -EAGAIN;
302
303 if (wait_event_interruptible_exclusive(
304 hidg->write_queue, WRITE_COND))
305 return -ERESTARTSYS;
306
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100307 spin_lock_irqsave(&hidg->write_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200308 }
309
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100310 hidg->write_pending = 1;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200311 count = min_t(unsigned, count, hidg->report_length);
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100312
313 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200314 status = copy_from_user(hidg->req->buf, buffer, count);
315
316 if (status != 0) {
317 ERROR(hidg->func.config->cdev,
318 "copy_from_user error\n");
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100319 status = -EINVAL;
320 goto release_write_pending;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200321 }
322
323 hidg->req->status = 0;
324 hidg->req->zero = 0;
325 hidg->req->length = count;
326 hidg->req->complete = f_hidg_req_complete;
327 hidg->req->context = hidg;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200328
329 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
330 if (status < 0) {
331 ERROR(hidg->func.config->cdev,
332 "usb_ep_queue error on int endpoint %zd\n", status);
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100333 goto release_write_pending;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200334 } else {
335 status = count;
336 }
337
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100338 return status;
339release_write_pending:
340 spin_lock_irqsave(&hidg->write_spinlock, flags);
341 hidg->write_pending = 0;
342 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
343
344 wake_up(&hidg->write_queue);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200345
346 return status;
347}
348
349static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
350{
Joe Perchesfd63b102010-07-12 13:50:11 -0700351 struct f_hidg *hidg = file->private_data;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200352 unsigned int ret = 0;
353
354 poll_wait(file, &hidg->read_queue, wait);
355 poll_wait(file, &hidg->write_queue, wait);
356
357 if (WRITE_COND)
358 ret |= POLLOUT | POLLWRNORM;
359
360 if (READ_COND)
361 ret |= POLLIN | POLLRDNORM;
362
363 return ret;
364}
365
366#undef WRITE_COND
367#undef READ_COND
368
369static int f_hidg_release(struct inode *inode, struct file *fd)
370{
371 fd->private_data = NULL;
372 return 0;
373}
374
375static int f_hidg_open(struct inode *inode, struct file *fd)
376{
377 struct f_hidg *hidg =
378 container_of(inode->i_cdev, struct f_hidg, cdev);
379
380 fd->private_data = hidg;
381
382 return 0;
383}
384
385/*-------------------------------------------------------------------------*/
386/* usb_function */
387
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100388static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
389 unsigned length)
Daniel Mack99c51502012-06-13 12:54:45 +0200390{
Felipe F. Tonelloaadbe812016-08-23 18:24:49 +0100391 return alloc_ep_req(ep, length);
Daniel Mack99c51502012-06-13 12:54:45 +0200392}
393
Fabien Chouteau71adf112010-04-08 09:31:15 +0200394static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
395{
Daniel Mack99c51502012-06-13 12:54:45 +0200396 struct f_hidg *hidg = (struct f_hidg *) req->context;
Krzysztof Opasiakb6092a52017-01-19 18:55:27 +0100397 struct usb_composite_dev *cdev = hidg->func.config->cdev;
Daniel Mack99c51502012-06-13 12:54:45 +0200398 struct f_hidg_req_list *req_list;
399 unsigned long flags;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200400
Krzysztof Opasiakb6092a52017-01-19 18:55:27 +0100401 switch (req->status) {
402 case 0:
403 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
404 if (!req_list) {
405 ERROR(cdev, "Unable to allocate mem for req_list\n");
406 goto free_req;
407 }
408
409 req_list->req = req;
410
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100411 spin_lock_irqsave(&hidg->read_spinlock, flags);
Krzysztof Opasiakb6092a52017-01-19 18:55:27 +0100412 list_add_tail(&req_list->list, &hidg->completed_out_req);
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100413 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
Krzysztof Opasiakb6092a52017-01-19 18:55:27 +0100414
415 wake_up(&hidg->read_queue);
416 break;
417 default:
418 ERROR(cdev, "Set report failed %d\n", req->status);
419 /* FALLTHROUGH */
420 case -ECONNABORTED: /* hardware forced ep reset */
421 case -ECONNRESET: /* request dequeued */
422 case -ESHUTDOWN: /* disconnect from host */
423free_req:
424 free_ep_req(ep, req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200425 return;
Krzysztof Opasiakb6092a52017-01-19 18:55:27 +0100426 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200427}
428
429static int hidg_setup(struct usb_function *f,
430 const struct usb_ctrlrequest *ctrl)
431{
432 struct f_hidg *hidg = func_to_hidg(f);
433 struct usb_composite_dev *cdev = f->config->cdev;
434 struct usb_request *req = cdev->req;
435 int status = 0;
436 __u16 value, length;
437
438 value = __le16_to_cpu(ctrl->wValue);
439 length = __le16_to_cpu(ctrl->wLength);
440
Julia Lawallc9b3bde2014-12-07 20:21:00 +0100441 VDBG(cdev,
442 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
443 __func__, ctrl->bRequestType, ctrl->bRequest, value);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200444
445 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
446 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
447 | HID_REQ_GET_REPORT):
448 VDBG(cdev, "get_report\n");
449
450 /* send an empty report */
451 length = min_t(unsigned, length, hidg->report_length);
452 memset(req->buf, 0x0, length);
453
454 goto respond;
455 break;
456
457 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
458 | HID_REQ_GET_PROTOCOL):
459 VDBG(cdev, "get_protocol\n");
460 goto stall;
461 break;
462
463 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
464 | HID_REQ_SET_REPORT):
Masanari Iida6774def2014-11-05 22:26:48 +0900465 VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
Daniel Mack99c51502012-06-13 12:54:45 +0200466 goto stall;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200467 break;
468
469 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
470 | HID_REQ_SET_PROTOCOL):
471 VDBG(cdev, "set_protocol\n");
472 goto stall;
473 break;
474
475 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
476 | USB_REQ_GET_DESCRIPTOR):
477 switch (value >> 8) {
Sebastian Bauerc240d782011-07-21 15:40:07 +0200478 case HID_DT_HID:
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100479 {
480 struct hid_descriptor hidg_desc_copy = hidg_desc;
481
Sebastian Bauerc240d782011-07-21 15:40:07 +0200482 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100483 hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
484 hidg_desc_copy.desc[0].wDescriptorLength =
485 cpu_to_le16(hidg->report_desc_length);
486
Sebastian Bauerc240d782011-07-21 15:40:07 +0200487 length = min_t(unsigned short, length,
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100488 hidg_desc_copy.bLength);
489 memcpy(req->buf, &hidg_desc_copy, length);
Sebastian Bauerc240d782011-07-21 15:40:07 +0200490 goto respond;
491 break;
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100492 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200493 case HID_DT_REPORT:
494 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
495 length = min_t(unsigned short, length,
496 hidg->report_desc_length);
497 memcpy(req->buf, hidg->report_desc, length);
498 goto respond;
499 break;
500
501 default:
Masanari Iida1c1301d2012-04-19 00:04:46 +0900502 VDBG(cdev, "Unknown descriptor request 0x%x\n",
Fabien Chouteau71adf112010-04-08 09:31:15 +0200503 value >> 8);
504 goto stall;
505 break;
506 }
507 break;
508
509 default:
510 VDBG(cdev, "Unknown request 0x%x\n",
511 ctrl->bRequest);
512 goto stall;
513 break;
514 }
515
516stall:
517 return -EOPNOTSUPP;
518
519respond:
520 req->zero = 0;
521 req->length = length;
522 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
523 if (status < 0)
524 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
525 return status;
526}
527
528static void hidg_disable(struct usb_function *f)
529{
530 struct f_hidg *hidg = func_to_hidg(f);
Daniel Mack99c51502012-06-13 12:54:45 +0200531 struct f_hidg_req_list *list, *next;
Krzysztof Opasiakd3acd942017-01-19 18:55:28 +0100532 unsigned long flags;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200533
534 usb_ep_disable(hidg->in_ep);
Daniel Mack99c51502012-06-13 12:54:45 +0200535 usb_ep_disable(hidg->out_ep);
Daniel Mack99c51502012-06-13 12:54:45 +0200536
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100537 spin_lock_irqsave(&hidg->read_spinlock, flags);
Daniel Mack99c51502012-06-13 12:54:45 +0200538 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
Krzysztof Opasiakd3acd942017-01-19 18:55:28 +0100539 free_ep_req(hidg->out_ep, list->req);
Daniel Mack99c51502012-06-13 12:54:45 +0200540 list_del(&list->list);
541 kfree(list);
542 }
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100543 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200544}
545
546static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
547{
548 struct usb_composite_dev *cdev = f->config->cdev;
549 struct f_hidg *hidg = func_to_hidg(f);
Daniel Mack99c51502012-06-13 12:54:45 +0200550 int i, status = 0;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200551
552 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
553
554 if (hidg->in_ep != NULL) {
555 /* restart endpoint */
Robert Baldyga2516a682015-09-16 12:10:46 +0200556 usb_ep_disable(hidg->in_ep);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200557
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300558 status = config_ep_by_speed(f->config->cdev->gadget, f,
559 hidg->in_ep);
560 if (status) {
561 ERROR(cdev, "config_ep_by_speed FAILED!\n");
562 goto fail;
563 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300564 status = usb_ep_enable(hidg->in_ep);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200565 if (status < 0) {
Daniel Mack99c51502012-06-13 12:54:45 +0200566 ERROR(cdev, "Enable IN endpoint FAILED!\n");
Fabien Chouteau71adf112010-04-08 09:31:15 +0200567 goto fail;
568 }
569 hidg->in_ep->driver_data = hidg;
570 }
Daniel Mack99c51502012-06-13 12:54:45 +0200571
572
573 if (hidg->out_ep != NULL) {
574 /* restart endpoint */
Robert Baldyga2516a682015-09-16 12:10:46 +0200575 usb_ep_disable(hidg->out_ep);
Daniel Mack99c51502012-06-13 12:54:45 +0200576
577 status = config_ep_by_speed(f->config->cdev->gadget, f,
578 hidg->out_ep);
579 if (status) {
580 ERROR(cdev, "config_ep_by_speed FAILED!\n");
581 goto fail;
582 }
583 status = usb_ep_enable(hidg->out_ep);
584 if (status < 0) {
David Lechner92d6a812017-01-02 17:28:39 -0600585 ERROR(cdev, "Enable OUT endpoint FAILED!\n");
Daniel Mack99c51502012-06-13 12:54:45 +0200586 goto fail;
587 }
588 hidg->out_ep->driver_data = hidg;
589
590 /*
591 * allocate a bunch of read buffers and queue them all at once.
592 */
593 for (i = 0; i < hidg->qlen && status == 0; i++) {
594 struct usb_request *req =
595 hidg_alloc_ep_req(hidg->out_ep,
596 hidg->report_length);
597 if (req) {
598 req->complete = hidg_set_report_complete;
599 req->context = hidg;
600 status = usb_ep_queue(hidg->out_ep, req,
601 GFP_ATOMIC);
602 if (status)
603 ERROR(cdev, "%s queue req --> %d\n",
604 hidg->out_ep->name, status);
605 } else {
606 usb_ep_disable(hidg->out_ep);
Daniel Mack99c51502012-06-13 12:54:45 +0200607 status = -ENOMEM;
608 goto fail;
609 }
610 }
611 }
612
Fabien Chouteau71adf112010-04-08 09:31:15 +0200613fail:
614 return status;
615}
616
Lad, Prabhakar7a3cc462015-02-04 17:49:36 +0000617static const struct file_operations f_hidg_fops = {
Fabien Chouteau71adf112010-04-08 09:31:15 +0200618 .owner = THIS_MODULE,
619 .open = f_hidg_open,
620 .release = f_hidg_release,
621 .write = f_hidg_write,
622 .read = f_hidg_read,
623 .poll = f_hidg_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200624 .llseek = noop_llseek,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200625};
626
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100627static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200628{
629 struct usb_ep *ep;
630 struct f_hidg *hidg = func_to_hidg(f);
Andrzej Pietrasiewicz5ca8d3ec2014-11-06 11:12:02 +0100631 struct usb_string *us;
Andrzej Pietrasiewicz63406082014-11-06 11:11:57 +0100632 struct device *device;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200633 int status;
634 dev_t dev;
635
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100636 /* maybe allocate device-global string IDs, and patch descriptors */
Andrzej Pietrasiewicz5ca8d3ec2014-11-06 11:12:02 +0100637 us = usb_gstrings_attach(c->cdev, ct_func_strings,
638 ARRAY_SIZE(ct_func_string_defs));
639 if (IS_ERR(us))
640 return PTR_ERR(us);
641 hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100642
Fabien Chouteau71adf112010-04-08 09:31:15 +0200643 /* allocate instance-specific interface IDs, and patch descriptors */
644 status = usb_interface_id(c, f);
645 if (status < 0)
646 goto fail;
647 hidg_interface_desc.bInterfaceNumber = status;
648
Fabien Chouteau71adf112010-04-08 09:31:15 +0200649 /* allocate instance-specific endpoints */
650 status = -ENODEV;
651 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
652 if (!ep)
653 goto fail;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200654 hidg->in_ep = ep;
655
Daniel Mack99c51502012-06-13 12:54:45 +0200656 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
657 if (!ep)
658 goto fail;
Daniel Mack99c51502012-06-13 12:54:45 +0200659 hidg->out_ep = ep;
660
Fabien Chouteau71adf112010-04-08 09:31:15 +0200661 /* preallocate request and buffer */
662 status = -ENOMEM;
Felipe F. Tonelloba1582f2016-08-23 18:24:51 +0100663 hidg->req = alloc_ep_req(hidg->in_ep, hidg->report_length);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200664 if (!hidg->req)
665 goto fail;
666
Fabien Chouteau71adf112010-04-08 09:31:15 +0200667 /* set descriptor dynamic values */
668 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
669 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
670 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
671 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
Daniel Mack99c51502012-06-13 12:54:45 +0200672 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
673 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
Krzysztof Opasiakf286d482015-03-27 09:35:44 +0100674 /*
675 * We can use hidg_desc struct here but we should not relay
676 * that its content won't change after returning from this function.
677 */
Fabien Chouteau71adf112010-04-08 09:31:15 +0200678 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
679 hidg_desc.desc[0].wDescriptorLength =
680 cpu_to_le16(hidg->report_desc_length);
681
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200682 hidg_hs_in_ep_desc.bEndpointAddress =
683 hidg_fs_in_ep_desc.bEndpointAddress;
684 hidg_hs_out_ep_desc.bEndpointAddress =
685 hidg_fs_out_ep_desc.bEndpointAddress;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200686
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200687 status = usb_assign_descriptors(f, hidg_fs_descriptors,
John Youneaef50c2016-02-05 17:06:07 -0800688 hidg_hs_descriptors, NULL, NULL);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200689 if (status)
690 goto fail;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200691
Krzysztof Opasiak6d0511e2017-01-19 18:55:29 +0100692 spin_lock_init(&hidg->write_spinlock);
693 spin_lock_init(&hidg->read_spinlock);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200694 init_waitqueue_head(&hidg->write_queue);
695 init_waitqueue_head(&hidg->read_queue);
Daniel Mack99c51502012-06-13 12:54:45 +0200696 INIT_LIST_HEAD(&hidg->completed_out_req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200697
698 /* create char device */
699 cdev_init(&hidg->cdev, &f_hidg_fops);
700 dev = MKDEV(major, hidg->minor);
701 status = cdev_add(&hidg->cdev, dev, 1);
702 if (status)
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +0530703 goto fail_free_descs;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200704
Andrzej Pietrasiewicz63406082014-11-06 11:11:57 +0100705 device = device_create(hidg_class, NULL, dev, NULL,
706 "%s%d", "hidg", hidg->minor);
707 if (IS_ERR(device)) {
708 status = PTR_ERR(device);
709 goto del;
710 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200711
712 return 0;
Andrzej Pietrasiewicz63406082014-11-06 11:11:57 +0100713del:
714 cdev_del(&hidg->cdev);
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +0530715fail_free_descs:
716 usb_free_all_descriptors(f);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200717fail:
718 ERROR(f->config->cdev, "hidg_bind FAILED\n");
Felipe F. Tonello14794d72016-08-23 18:24:50 +0100719 if (hidg->req != NULL)
720 free_ep_req(hidg->in_ep, hidg->req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200721
Fabien Chouteau71adf112010-04-08 09:31:15 +0200722 return status;
723}
724
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100725static inline int hidg_get_minor(void)
726{
727 int ret;
728
729 ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
Andrzej Pietrasiewicz774cf722015-07-24 09:48:40 +0200730 if (ret >= HIDG_MINORS) {
731 ida_simple_remove(&hidg_ida, ret);
732 ret = -ENODEV;
733 }
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100734
735 return ret;
736}
737
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100738static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
739{
740 return container_of(to_config_group(item), struct f_hid_opts,
741 func_inst.group);
742}
743
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100744static void hid_attr_release(struct config_item *item)
745{
746 struct f_hid_opts *opts = to_f_hid_opts(item);
747
748 usb_put_function_instance(&opts->func_inst);
749}
750
751static struct configfs_item_operations hidg_item_ops = {
752 .release = hid_attr_release,
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100753};
754
755#define F_HID_OPT(name, prec, limit) \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200756static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100757{ \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200758 struct f_hid_opts *opts = to_f_hid_opts(item); \
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100759 int result; \
760 \
761 mutex_lock(&opts->lock); \
762 result = sprintf(page, "%d\n", opts->name); \
763 mutex_unlock(&opts->lock); \
764 \
765 return result; \
766} \
767 \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200768static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100769 const char *page, size_t len) \
770{ \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200771 struct f_hid_opts *opts = to_f_hid_opts(item); \
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100772 int ret; \
773 u##prec num; \
774 \
775 mutex_lock(&opts->lock); \
776 if (opts->refcnt) { \
777 ret = -EBUSY; \
778 goto end; \
779 } \
780 \
781 ret = kstrtou##prec(page, 0, &num); \
782 if (ret) \
783 goto end; \
784 \
785 if (num > limit) { \
786 ret = -EINVAL; \
787 goto end; \
788 } \
789 opts->name = num; \
790 ret = len; \
791 \
792end: \
793 mutex_unlock(&opts->lock); \
794 return ret; \
795} \
796 \
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200797CONFIGFS_ATTR(f_hid_opts_, name)
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100798
799F_HID_OPT(subclass, 8, 255);
800F_HID_OPT(protocol, 8, 255);
Andrzej Pietrasiewicz39a2ac272014-12-15 13:50:05 +0100801F_HID_OPT(report_length, 16, 65535);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100802
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200803static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100804{
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200805 struct f_hid_opts *opts = to_f_hid_opts(item);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100806 int result;
807
808 mutex_lock(&opts->lock);
809 result = opts->report_desc_length;
810 memcpy(page, opts->report_desc, opts->report_desc_length);
811 mutex_unlock(&opts->lock);
812
813 return result;
814}
815
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200816static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100817 const char *page, size_t len)
818{
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200819 struct f_hid_opts *opts = to_f_hid_opts(item);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100820 int ret = -EBUSY;
821 char *d;
822
823 mutex_lock(&opts->lock);
824
825 if (opts->refcnt)
826 goto end;
827 if (len > PAGE_SIZE) {
828 ret = -ENOSPC;
829 goto end;
830 }
831 d = kmemdup(page, len, GFP_KERNEL);
832 if (!d) {
833 ret = -ENOMEM;
834 goto end;
835 }
836 kfree(opts->report_desc);
837 opts->report_desc = d;
838 opts->report_desc_length = len;
839 opts->report_desc_alloc = true;
840 ret = len;
841end:
842 mutex_unlock(&opts->lock);
843 return ret;
844}
845
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200846CONFIGFS_ATTR(f_hid_opts_, report_desc);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100847
Johannes Berged6fe1f2016-06-23 22:28:54 +0200848static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
849{
850 struct f_hid_opts *opts = to_f_hid_opts(item);
851
852 return sprintf(page, "%d:%d\n", major, opts->minor);
853}
854
855CONFIGFS_ATTR_RO(f_hid_opts_, dev);
856
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100857static struct configfs_attribute *hid_attrs[] = {
Christoph Hellwigda4e5272015-10-03 15:32:40 +0200858 &f_hid_opts_attr_subclass,
859 &f_hid_opts_attr_protocol,
860 &f_hid_opts_attr_report_length,
861 &f_hid_opts_attr_report_desc,
Johannes Berged6fe1f2016-06-23 22:28:54 +0200862 &f_hid_opts_attr_dev,
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100863 NULL,
864};
865
866static struct config_item_type hid_func_type = {
867 .ct_item_ops = &hidg_item_ops,
868 .ct_attrs = hid_attrs,
869 .ct_owner = THIS_MODULE,
870};
871
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100872static inline void hidg_put_minor(int minor)
873{
874 ida_simple_remove(&hidg_ida, minor);
875}
876
877static void hidg_free_inst(struct usb_function_instance *f)
878{
879 struct f_hid_opts *opts;
880
881 opts = container_of(f, struct f_hid_opts, func_inst);
882
883 mutex_lock(&hidg_ida_lock);
884
885 hidg_put_minor(opts->minor);
886 if (idr_is_empty(&hidg_ida.idr))
887 ghid_cleanup();
888
889 mutex_unlock(&hidg_ida_lock);
890
891 if (opts->report_desc_alloc)
892 kfree(opts->report_desc);
893
894 kfree(opts);
895}
896
897static struct usb_function_instance *hidg_alloc_inst(void)
898{
899 struct f_hid_opts *opts;
900 struct usb_function_instance *ret;
901 int status = 0;
902
903 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
904 if (!opts)
905 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100906 mutex_init(&opts->lock);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100907 opts->func_inst.free_func_inst = hidg_free_inst;
908 ret = &opts->func_inst;
909
910 mutex_lock(&hidg_ida_lock);
911
912 if (idr_is_empty(&hidg_ida.idr)) {
913 status = ghid_setup(NULL, HIDG_MINORS);
914 if (status) {
915 ret = ERR_PTR(status);
916 kfree(opts);
917 goto unlock;
918 }
919 }
920
921 opts->minor = hidg_get_minor();
922 if (opts->minor < 0) {
923 ret = ERR_PTR(opts->minor);
924 kfree(opts);
925 if (idr_is_empty(&hidg_ida.idr))
926 ghid_cleanup();
Dan Carpenter828f6142014-11-13 09:19:47 +0300927 goto unlock;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100928 }
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100929 config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100930
931unlock:
932 mutex_unlock(&hidg_ida_lock);
933 return ret;
934}
935
936static void hidg_free(struct usb_function *f)
937{
938 struct f_hidg *hidg;
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100939 struct f_hid_opts *opts;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100940
941 hidg = func_to_hidg(f);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100942 opts = container_of(f->fi, struct f_hid_opts, func_inst);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100943 kfree(hidg->report_desc);
944 kfree(hidg);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100945 mutex_lock(&opts->lock);
946 --opts->refcnt;
947 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100948}
949
Fabien Chouteau71adf112010-04-08 09:31:15 +0200950static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
951{
952 struct f_hidg *hidg = func_to_hidg(f);
953
954 device_destroy(hidg_class, MKDEV(major, hidg->minor));
955 cdev_del(&hidg->cdev);
956
957 /* disable/free request and end point */
958 usb_ep_disable(hidg->in_ep);
Felipe F. Tonello14794d72016-08-23 18:24:50 +0100959 free_ep_req(hidg->in_ep, hidg->req);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200960
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200961 usb_free_all_descriptors(f);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200962}
963
Fengguang Wu0fc57ea2014-11-12 22:44:05 +0800964static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200965{
966 struct f_hidg *hidg;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100967 struct f_hid_opts *opts;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200968
969 /* allocate and initialize one new instance */
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100970 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200971 if (!hidg)
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100972 return ERR_PTR(-ENOMEM);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200973
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100974 opts = container_of(fi, struct f_hid_opts, func_inst);
975
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100976 mutex_lock(&opts->lock);
977 ++opts->refcnt;
978
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100979 hidg->minor = opts->minor;
980 hidg->bInterfaceSubClass = opts->subclass;
981 hidg->bInterfaceProtocol = opts->protocol;
982 hidg->report_length = opts->report_length;
983 hidg->report_desc_length = opts->report_desc_length;
984 if (opts->report_desc) {
985 hidg->report_desc = kmemdup(opts->report_desc,
986 opts->report_desc_length,
987 GFP_KERNEL);
988 if (!hidg->report_desc) {
989 kfree(hidg);
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100990 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +0100991 return ERR_PTR(-ENOMEM);
992 }
Fabien Chouteau71adf112010-04-08 09:31:15 +0200993 }
994
Andrzej Pietrasiewicz21a94762014-11-06 11:12:03 +0100995 mutex_unlock(&opts->lock);
996
Fabien Chouteau71adf112010-04-08 09:31:15 +0200997 hidg->func.name = "hid";
Fabien Chouteau71adf112010-04-08 09:31:15 +0200998 hidg->func.bind = hidg_bind;
999 hidg->func.unbind = hidg_unbind;
1000 hidg->func.set_alt = hidg_set_alt;
1001 hidg->func.disable = hidg_disable;
1002 hidg->func.setup = hidg_setup;
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001003 hidg->func.free_func = hidg_free;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001004
Daniel Mack99c51502012-06-13 12:54:45 +02001005 /* this could me made configurable at some point */
1006 hidg->qlen = 4;
1007
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001008 return &hidg->func;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001009}
1010
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001011DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1012MODULE_LICENSE("GPL");
1013MODULE_AUTHOR("Fabien Chouteau");
1014
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +01001015int ghid_setup(struct usb_gadget *g, int count)
Fabien Chouteau71adf112010-04-08 09:31:15 +02001016{
1017 int status;
1018 dev_t dev;
1019
1020 hidg_class = class_create(THIS_MODULE, "hidg");
Andrzej Pietrasiewicz06529402014-11-06 11:11:56 +01001021 if (IS_ERR(hidg_class)) {
Dan Carpenter0448d382014-11-13 09:20:59 +03001022 status = PTR_ERR(hidg_class);
Andrzej Pietrasiewicz06529402014-11-06 11:11:56 +01001023 hidg_class = NULL;
Dan Carpenter0448d382014-11-13 09:20:59 +03001024 return status;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001025 }
1026
Fabien Chouteau71adf112010-04-08 09:31:15 +02001027 status = alloc_chrdev_region(&dev, 0, count, "hidg");
Dan Carpenter0448d382014-11-13 09:20:59 +03001028 if (status) {
1029 class_destroy(hidg_class);
1030 hidg_class = NULL;
1031 return status;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001032 }
1033
Dan Carpenter0448d382014-11-13 09:20:59 +03001034 major = MAJOR(dev);
1035 minors = count;
1036
1037 return 0;
Fabien Chouteau71adf112010-04-08 09:31:15 +02001038}
1039
1040void ghid_cleanup(void)
1041{
1042 if (major) {
1043 unregister_chrdev_region(MKDEV(major, 0), minors);
1044 major = minors = 0;
1045 }
1046
1047 class_destroy(hidg_class);
1048 hidg_class = NULL;
1049}