]>
Commit | Line | Data |
---|---|---|
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 |
29 | static int major, minors; |
30 | static struct class *hidg_class; | |
cb382536 AP |
31 | static DEFINE_IDA(hidg_ida); |
32 | static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ | |
71adf118 FC |
33 | |
34 | /*-------------------------------------------------------------------------*/ | |
35 | /* HID gadget struct */ | |
36 | ||
99c51500 DM |
37 | struct f_hidg_req_list { |
38 | struct usb_request *req; | |
39 | unsigned int pos; | |
40 | struct list_head list; | |
41 | }; | |
42 | ||
71adf118 FC |
43 | struct 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 | ||
71 | static 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 | ||
79 | static 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 | ||
91 | static 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 | ||
103 | static 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 |
115 | static 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 |
127 | static 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 | ||
137 | static 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 |
149 | static 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 |
161 | static 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 | ||
174 | static struct usb_string ct_func_string_defs[] = { | |
175 | [CT_FUNC_HID_IDX].s = "HID Interface", | |
176 | {}, /* end of list */ | |
177 | }; | |
178 | ||
179 | static struct usb_gadget_strings ct_func_string_table = { | |
180 | .language = 0x0409, /* en-US */ | |
181 | .strings = ct_func_string_defs, | |
182 | }; | |
183 | ||
184 | static struct usb_gadget_strings *ct_func_strings[] = { | |
185 | &ct_func_string_table, | |
186 | NULL, | |
187 | }; | |
188 | ||
71adf118 FC |
189 | /*-------------------------------------------------------------------------*/ |
190 | /* Char Device */ | |
191 | ||
192 | static 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 | ||
255 | static 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 | ||
268 | static 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 | ||
326 | static 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 | ||
346 | static int f_hidg_release(struct inode *inode, struct file *fd) | |
347 | { | |
348 | fd->private_data = NULL; | |
349 | return 0; | |
350 | } | |
351 | ||
352 | static 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 |
365 | static 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 |
371 | static 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 | ||
390 | static 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 | ||
c9b3bde0 JL |
402 | VDBG(cdev, |
403 | "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", | |
404 | __func__, ctrl->bRequestType, ctrl->bRequest, value); | |
71adf118 FC |
405 | |
406 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { | |
407 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
408 | | HID_REQ_GET_REPORT): | |
409 | VDBG(cdev, "get_report\n"); | |
410 | ||
411 | /* send an empty report */ | |
412 | length = min_t(unsigned, length, hidg->report_length); | |
413 | memset(req->buf, 0x0, length); | |
414 | ||
415 | goto respond; | |
416 | break; | |
417 | ||
418 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
419 | | HID_REQ_GET_PROTOCOL): | |
420 | VDBG(cdev, "get_protocol\n"); | |
421 | goto stall; | |
422 | break; | |
423 | ||
424 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
425 | | HID_REQ_SET_REPORT): | |
6774def6 | 426 | VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); |
99c51500 | 427 | goto stall; |
71adf118 FC |
428 | break; |
429 | ||
430 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
431 | | HID_REQ_SET_PROTOCOL): | |
432 | VDBG(cdev, "set_protocol\n"); | |
433 | goto stall; | |
434 | break; | |
435 | ||
436 | case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 | |
437 | | USB_REQ_GET_DESCRIPTOR): | |
438 | switch (value >> 8) { | |
c240d78a | 439 | case HID_DT_HID: |
f286d487 KO |
440 | { |
441 | struct hid_descriptor hidg_desc_copy = hidg_desc; | |
442 | ||
c240d78a | 443 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); |
f286d487 KO |
444 | hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; |
445 | hidg_desc_copy.desc[0].wDescriptorLength = | |
446 | cpu_to_le16(hidg->report_desc_length); | |
447 | ||
c240d78a | 448 | length = min_t(unsigned short, length, |
f286d487 KO |
449 | hidg_desc_copy.bLength); |
450 | memcpy(req->buf, &hidg_desc_copy, length); | |
c240d78a SB |
451 | goto respond; |
452 | break; | |
f286d487 | 453 | } |
71adf118 FC |
454 | case HID_DT_REPORT: |
455 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); | |
456 | length = min_t(unsigned short, length, | |
457 | hidg->report_desc_length); | |
458 | memcpy(req->buf, hidg->report_desc, length); | |
459 | goto respond; | |
460 | break; | |
461 | ||
462 | default: | |
1c1301dd | 463 | VDBG(cdev, "Unknown descriptor request 0x%x\n", |
71adf118 FC |
464 | value >> 8); |
465 | goto stall; | |
466 | break; | |
467 | } | |
468 | break; | |
469 | ||
470 | default: | |
471 | VDBG(cdev, "Unknown request 0x%x\n", | |
472 | ctrl->bRequest); | |
473 | goto stall; | |
474 | break; | |
475 | } | |
476 | ||
477 | stall: | |
478 | return -EOPNOTSUPP; | |
479 | ||
480 | respond: | |
481 | req->zero = 0; | |
482 | req->length = length; | |
483 | status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | |
484 | if (status < 0) | |
485 | ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); | |
486 | return status; | |
487 | } | |
488 | ||
489 | static void hidg_disable(struct usb_function *f) | |
490 | { | |
491 | struct f_hidg *hidg = func_to_hidg(f); | |
99c51500 | 492 | struct f_hidg_req_list *list, *next; |
71adf118 FC |
493 | |
494 | usb_ep_disable(hidg->in_ep); | |
495 | hidg->in_ep->driver_data = NULL; | |
99c51500 DM |
496 | |
497 | usb_ep_disable(hidg->out_ep); | |
498 | hidg->out_ep->driver_data = NULL; | |
499 | ||
500 | list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { | |
501 | list_del(&list->list); | |
502 | kfree(list); | |
503 | } | |
71adf118 FC |
504 | } |
505 | ||
506 | static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | |
507 | { | |
508 | struct usb_composite_dev *cdev = f->config->cdev; | |
509 | struct f_hidg *hidg = func_to_hidg(f); | |
99c51500 | 510 | int i, status = 0; |
71adf118 FC |
511 | |
512 | VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); | |
513 | ||
514 | if (hidg->in_ep != NULL) { | |
515 | /* restart endpoint */ | |
516 | if (hidg->in_ep->driver_data != NULL) | |
517 | usb_ep_disable(hidg->in_ep); | |
518 | ||
ea2a1df7 TB |
519 | status = config_ep_by_speed(f->config->cdev->gadget, f, |
520 | hidg->in_ep); | |
521 | if (status) { | |
522 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); | |
523 | goto fail; | |
524 | } | |
72c973dd | 525 | status = usb_ep_enable(hidg->in_ep); |
71adf118 | 526 | if (status < 0) { |
99c51500 | 527 | ERROR(cdev, "Enable IN endpoint FAILED!\n"); |
71adf118 FC |
528 | goto fail; |
529 | } | |
530 | hidg->in_ep->driver_data = hidg; | |
531 | } | |
99c51500 DM |
532 | |
533 | ||
534 | if (hidg->out_ep != NULL) { | |
535 | /* restart endpoint */ | |
536 | if (hidg->out_ep->driver_data != NULL) | |
537 | usb_ep_disable(hidg->out_ep); | |
538 | ||
539 | status = config_ep_by_speed(f->config->cdev->gadget, f, | |
540 | hidg->out_ep); | |
541 | if (status) { | |
542 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); | |
543 | goto fail; | |
544 | } | |
545 | status = usb_ep_enable(hidg->out_ep); | |
546 | if (status < 0) { | |
547 | ERROR(cdev, "Enable IN endpoint FAILED!\n"); | |
548 | goto fail; | |
549 | } | |
550 | hidg->out_ep->driver_data = hidg; | |
551 | ||
552 | /* | |
553 | * allocate a bunch of read buffers and queue them all at once. | |
554 | */ | |
555 | for (i = 0; i < hidg->qlen && status == 0; i++) { | |
556 | struct usb_request *req = | |
557 | hidg_alloc_ep_req(hidg->out_ep, | |
558 | hidg->report_length); | |
559 | if (req) { | |
560 | req->complete = hidg_set_report_complete; | |
561 | req->context = hidg; | |
562 | status = usb_ep_queue(hidg->out_ep, req, | |
563 | GFP_ATOMIC); | |
564 | if (status) | |
565 | ERROR(cdev, "%s queue req --> %d\n", | |
566 | hidg->out_ep->name, status); | |
567 | } else { | |
568 | usb_ep_disable(hidg->out_ep); | |
569 | hidg->out_ep->driver_data = NULL; | |
570 | status = -ENOMEM; | |
571 | goto fail; | |
572 | } | |
573 | } | |
574 | } | |
575 | ||
71adf118 FC |
576 | fail: |
577 | return status; | |
578 | } | |
579 | ||
7a3cc461 | 580 | static const struct file_operations f_hidg_fops = { |
71adf118 FC |
581 | .owner = THIS_MODULE, |
582 | .open = f_hidg_open, | |
583 | .release = f_hidg_release, | |
584 | .write = f_hidg_write, | |
585 | .read = f_hidg_read, | |
586 | .poll = f_hidg_poll, | |
6038f373 | 587 | .llseek = noop_llseek, |
71adf118 FC |
588 | }; |
589 | ||
cb382536 | 590 | static int hidg_bind(struct usb_configuration *c, struct usb_function *f) |
71adf118 FC |
591 | { |
592 | struct usb_ep *ep; | |
593 | struct f_hidg *hidg = func_to_hidg(f); | |
5ca8d3ec | 594 | struct usb_string *us; |
63406087 | 595 | struct device *device; |
71adf118 FC |
596 | int status; |
597 | dev_t dev; | |
598 | ||
cb382536 | 599 | /* maybe allocate device-global string IDs, and patch descriptors */ |
5ca8d3ec AP |
600 | us = usb_gstrings_attach(c->cdev, ct_func_strings, |
601 | ARRAY_SIZE(ct_func_string_defs)); | |
602 | if (IS_ERR(us)) | |
603 | return PTR_ERR(us); | |
604 | hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; | |
cb382536 | 605 | |
71adf118 FC |
606 | /* allocate instance-specific interface IDs, and patch descriptors */ |
607 | status = usb_interface_id(c, f); | |
608 | if (status < 0) | |
609 | goto fail; | |
610 | hidg_interface_desc.bInterfaceNumber = status; | |
611 | ||
71adf118 FC |
612 | /* allocate instance-specific endpoints */ |
613 | status = -ENODEV; | |
614 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); | |
615 | if (!ep) | |
616 | goto fail; | |
617 | ep->driver_data = c->cdev; /* claim */ | |
618 | hidg->in_ep = ep; | |
619 | ||
99c51500 DM |
620 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); |
621 | if (!ep) | |
622 | goto fail; | |
623 | ep->driver_data = c->cdev; /* claim */ | |
624 | hidg->out_ep = ep; | |
625 | ||
71adf118 FC |
626 | /* preallocate request and buffer */ |
627 | status = -ENOMEM; | |
628 | hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL); | |
629 | if (!hidg->req) | |
630 | goto fail; | |
631 | ||
71adf118 FC |
632 | hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL); |
633 | if (!hidg->req->buf) | |
634 | goto fail; | |
635 | ||
636 | /* set descriptor dynamic values */ | |
637 | hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; | |
638 | hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; | |
639 | hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); | |
640 | hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); | |
99c51500 DM |
641 | hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
642 | hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); | |
f286d487 KO |
643 | /* |
644 | * We can use hidg_desc struct here but we should not relay | |
645 | * that its content won't change after returning from this function. | |
646 | */ | |
71adf118 FC |
647 | hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; |
648 | hidg_desc.desc[0].wDescriptorLength = | |
649 | cpu_to_le16(hidg->report_desc_length); | |
650 | ||
10287bae SAS |
651 | hidg_hs_in_ep_desc.bEndpointAddress = |
652 | hidg_fs_in_ep_desc.bEndpointAddress; | |
653 | hidg_hs_out_ep_desc.bEndpointAddress = | |
654 | hidg_fs_out_ep_desc.bEndpointAddress; | |
71adf118 | 655 | |
10287bae SAS |
656 | status = usb_assign_descriptors(f, hidg_fs_descriptors, |
657 | hidg_hs_descriptors, NULL); | |
658 | if (status) | |
659 | goto fail; | |
71adf118 FC |
660 | |
661 | mutex_init(&hidg->lock); | |
662 | spin_lock_init(&hidg->spinlock); | |
663 | init_waitqueue_head(&hidg->write_queue); | |
664 | init_waitqueue_head(&hidg->read_queue); | |
99c51500 | 665 | INIT_LIST_HEAD(&hidg->completed_out_req); |
71adf118 FC |
666 | |
667 | /* create char device */ | |
668 | cdev_init(&hidg->cdev, &f_hidg_fops); | |
669 | dev = MKDEV(major, hidg->minor); | |
670 | status = cdev_add(&hidg->cdev, dev, 1); | |
671 | if (status) | |
d12a8727 | 672 | goto fail_free_descs; |
71adf118 | 673 | |
63406087 AP |
674 | device = device_create(hidg_class, NULL, dev, NULL, |
675 | "%s%d", "hidg", hidg->minor); | |
676 | if (IS_ERR(device)) { | |
677 | status = PTR_ERR(device); | |
678 | goto del; | |
679 | } | |
71adf118 FC |
680 | |
681 | return 0; | |
63406087 AP |
682 | del: |
683 | cdev_del(&hidg->cdev); | |
d12a8727 PM |
684 | fail_free_descs: |
685 | usb_free_all_descriptors(f); | |
71adf118 FC |
686 | fail: |
687 | ERROR(f->config->cdev, "hidg_bind FAILED\n"); | |
688 | if (hidg->req != NULL) { | |
689 | kfree(hidg->req->buf); | |
690 | if (hidg->in_ep != NULL) | |
691 | usb_ep_free_request(hidg->in_ep, hidg->req); | |
692 | } | |
693 | ||
71adf118 FC |
694 | return status; |
695 | } | |
696 | ||
cb382536 | 697 | static inline int hidg_get_minor(void) |
71adf118 | 698 | { |
cb382536 | 699 | int ret; |
71adf118 | 700 | |
cb382536 | 701 | ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); |
774cf72f AP |
702 | if (ret >= HIDG_MINORS) { |
703 | ida_simple_remove(&hidg_ida, ret); | |
704 | ret = -ENODEV; | |
705 | } | |
71adf118 | 706 | |
cb382536 AP |
707 | return ret; |
708 | } | |
71adf118 | 709 | |
21a9476a AP |
710 | static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) |
711 | { | |
712 | return container_of(to_config_group(item), struct f_hid_opts, | |
713 | func_inst.group); | |
71adf118 FC |
714 | } |
715 | ||
21a9476a AP |
716 | CONFIGFS_ATTR_STRUCT(f_hid_opts); |
717 | CONFIGFS_ATTR_OPS(f_hid_opts); | |
71adf118 | 718 | |
21a9476a AP |
719 | static void hid_attr_release(struct config_item *item) |
720 | { | |
721 | struct f_hid_opts *opts = to_f_hid_opts(item); | |
71adf118 | 722 | |
21a9476a AP |
723 | usb_put_function_instance(&opts->func_inst); |
724 | } | |
71adf118 | 725 | |
21a9476a AP |
726 | static struct configfs_item_operations hidg_item_ops = { |
727 | .release = hid_attr_release, | |
728 | .show_attribute = f_hid_opts_attr_show, | |
729 | .store_attribute = f_hid_opts_attr_store, | |
71adf118 FC |
730 | }; |
731 | ||
21a9476a AP |
732 | #define F_HID_OPT(name, prec, limit) \ |
733 | static ssize_t f_hid_opts_##name##_show(struct f_hid_opts *opts, char *page)\ | |
734 | { \ | |
735 | int result; \ | |
736 | \ | |
737 | mutex_lock(&opts->lock); \ | |
738 | result = sprintf(page, "%d\n", opts->name); \ | |
739 | mutex_unlock(&opts->lock); \ | |
740 | \ | |
741 | return result; \ | |
742 | } \ | |
743 | \ | |
744 | static ssize_t f_hid_opts_##name##_store(struct f_hid_opts *opts, \ | |
745 | const char *page, size_t len) \ | |
746 | { \ | |
747 | int ret; \ | |
748 | u##prec num; \ | |
749 | \ | |
750 | mutex_lock(&opts->lock); \ | |
751 | if (opts->refcnt) { \ | |
752 | ret = -EBUSY; \ | |
753 | goto end; \ | |
754 | } \ | |
755 | \ | |
756 | ret = kstrtou##prec(page, 0, &num); \ | |
757 | if (ret) \ | |
758 | goto end; \ | |
759 | \ | |
760 | if (num > limit) { \ | |
761 | ret = -EINVAL; \ | |
762 | goto end; \ | |
763 | } \ | |
764 | opts->name = num; \ | |
765 | ret = len; \ | |
766 | \ | |
767 | end: \ | |
768 | mutex_unlock(&opts->lock); \ | |
769 | return ret; \ | |
770 | } \ | |
771 | \ | |
772 | static struct f_hid_opts_attribute f_hid_opts_##name = \ | |
773 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, f_hid_opts_##name##_show,\ | |
774 | f_hid_opts_##name##_store) | |
775 | ||
776 | F_HID_OPT(subclass, 8, 255); | |
777 | F_HID_OPT(protocol, 8, 255); | |
39a2ac27 | 778 | F_HID_OPT(report_length, 16, 65535); |
21a9476a AP |
779 | |
780 | static ssize_t f_hid_opts_report_desc_show(struct f_hid_opts *opts, char *page) | |
781 | { | |
782 | int result; | |
783 | ||
784 | mutex_lock(&opts->lock); | |
785 | result = opts->report_desc_length; | |
786 | memcpy(page, opts->report_desc, opts->report_desc_length); | |
787 | mutex_unlock(&opts->lock); | |
788 | ||
789 | return result; | |
790 | } | |
791 | ||
792 | static ssize_t f_hid_opts_report_desc_store(struct f_hid_opts *opts, | |
793 | const char *page, size_t len) | |
794 | { | |
795 | int ret = -EBUSY; | |
796 | char *d; | |
797 | ||
798 | mutex_lock(&opts->lock); | |
799 | ||
800 | if (opts->refcnt) | |
801 | goto end; | |
802 | if (len > PAGE_SIZE) { | |
803 | ret = -ENOSPC; | |
804 | goto end; | |
805 | } | |
806 | d = kmemdup(page, len, GFP_KERNEL); | |
807 | if (!d) { | |
808 | ret = -ENOMEM; | |
809 | goto end; | |
810 | } | |
811 | kfree(opts->report_desc); | |
812 | opts->report_desc = d; | |
813 | opts->report_desc_length = len; | |
814 | opts->report_desc_alloc = true; | |
815 | ret = len; | |
816 | end: | |
817 | mutex_unlock(&opts->lock); | |
818 | return ret; | |
819 | } | |
820 | ||
821 | static struct f_hid_opts_attribute f_hid_opts_report_desc = | |
822 | __CONFIGFS_ATTR(report_desc, S_IRUGO | S_IWUSR, | |
823 | f_hid_opts_report_desc_show, | |
824 | f_hid_opts_report_desc_store); | |
825 | ||
826 | static struct configfs_attribute *hid_attrs[] = { | |
827 | &f_hid_opts_subclass.attr, | |
828 | &f_hid_opts_protocol.attr, | |
829 | &f_hid_opts_report_length.attr, | |
830 | &f_hid_opts_report_desc.attr, | |
71adf118 FC |
831 | NULL, |
832 | }; | |
833 | ||
21a9476a AP |
834 | static struct config_item_type hid_func_type = { |
835 | .ct_item_ops = &hidg_item_ops, | |
836 | .ct_attrs = hid_attrs, | |
837 | .ct_owner = THIS_MODULE, | |
838 | }; | |
71adf118 | 839 | |
cb382536 | 840 | static inline void hidg_put_minor(int minor) |
71adf118 | 841 | { |
cb382536 AP |
842 | ida_simple_remove(&hidg_ida, minor); |
843 | } | |
71adf118 | 844 | |
cb382536 AP |
845 | static void hidg_free_inst(struct usb_function_instance *f) |
846 | { | |
847 | struct f_hid_opts *opts; | |
71adf118 | 848 | |
cb382536 AP |
849 | opts = container_of(f, struct f_hid_opts, func_inst); |
850 | ||
851 | mutex_lock(&hidg_ida_lock); | |
852 | ||
853 | hidg_put_minor(opts->minor); | |
854 | if (idr_is_empty(&hidg_ida.idr)) | |
855 | ghid_cleanup(); | |
856 | ||
857 | mutex_unlock(&hidg_ida_lock); | |
858 | ||
859 | if (opts->report_desc_alloc) | |
860 | kfree(opts->report_desc); | |
861 | ||
862 | kfree(opts); | |
863 | } | |
864 | ||
865 | static struct usb_function_instance *hidg_alloc_inst(void) | |
866 | { | |
867 | struct f_hid_opts *opts; | |
868 | struct usb_function_instance *ret; | |
869 | int status = 0; | |
870 | ||
871 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); | |
872 | if (!opts) | |
873 | return ERR_PTR(-ENOMEM); | |
21a9476a | 874 | mutex_init(&opts->lock); |
cb382536 AP |
875 | opts->func_inst.free_func_inst = hidg_free_inst; |
876 | ret = &opts->func_inst; | |
877 | ||
878 | mutex_lock(&hidg_ida_lock); | |
879 | ||
880 | if (idr_is_empty(&hidg_ida.idr)) { | |
881 | status = ghid_setup(NULL, HIDG_MINORS); | |
882 | if (status) { | |
883 | ret = ERR_PTR(status); | |
884 | kfree(opts); | |
885 | goto unlock; | |
886 | } | |
887 | } | |
888 | ||
889 | opts->minor = hidg_get_minor(); | |
890 | if (opts->minor < 0) { | |
891 | ret = ERR_PTR(opts->minor); | |
892 | kfree(opts); | |
893 | if (idr_is_empty(&hidg_ida.idr)) | |
894 | ghid_cleanup(); | |
828f6148 | 895 | goto unlock; |
71adf118 | 896 | } |
21a9476a | 897 | config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); |
cb382536 AP |
898 | |
899 | unlock: | |
900 | mutex_unlock(&hidg_ida_lock); | |
901 | return ret; | |
902 | } | |
903 | ||
904 | static void hidg_free(struct usb_function *f) | |
905 | { | |
906 | struct f_hidg *hidg; | |
21a9476a | 907 | struct f_hid_opts *opts; |
cb382536 AP |
908 | |
909 | hidg = func_to_hidg(f); | |
21a9476a | 910 | opts = container_of(f->fi, struct f_hid_opts, func_inst); |
cb382536 AP |
911 | kfree(hidg->report_desc); |
912 | kfree(hidg); | |
21a9476a AP |
913 | mutex_lock(&opts->lock); |
914 | --opts->refcnt; | |
915 | mutex_unlock(&opts->lock); | |
cb382536 AP |
916 | } |
917 | ||
918 | static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) | |
919 | { | |
920 | struct f_hidg *hidg = func_to_hidg(f); | |
921 | ||
922 | device_destroy(hidg_class, MKDEV(major, hidg->minor)); | |
923 | cdev_del(&hidg->cdev); | |
924 | ||
925 | /* disable/free request and end point */ | |
926 | usb_ep_disable(hidg->in_ep); | |
cb382536 AP |
927 | kfree(hidg->req->buf); |
928 | usb_ep_free_request(hidg->in_ep, hidg->req); | |
929 | ||
930 | usb_free_all_descriptors(f); | |
931 | } | |
932 | ||
0fc57ea0 | 933 | static struct usb_function *hidg_alloc(struct usb_function_instance *fi) |
cb382536 AP |
934 | { |
935 | struct f_hidg *hidg; | |
936 | struct f_hid_opts *opts; | |
71adf118 FC |
937 | |
938 | /* allocate and initialize one new instance */ | |
cb382536 | 939 | hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); |
71adf118 | 940 | if (!hidg) |
cb382536 AP |
941 | return ERR_PTR(-ENOMEM); |
942 | ||
943 | opts = container_of(fi, struct f_hid_opts, func_inst); | |
944 | ||
21a9476a AP |
945 | mutex_lock(&opts->lock); |
946 | ++opts->refcnt; | |
947 | ||
cb382536 AP |
948 | hidg->minor = opts->minor; |
949 | hidg->bInterfaceSubClass = opts->subclass; | |
950 | hidg->bInterfaceProtocol = opts->protocol; | |
951 | hidg->report_length = opts->report_length; | |
952 | hidg->report_desc_length = opts->report_desc_length; | |
953 | if (opts->report_desc) { | |
954 | hidg->report_desc = kmemdup(opts->report_desc, | |
955 | opts->report_desc_length, | |
956 | GFP_KERNEL); | |
957 | if (!hidg->report_desc) { | |
958 | kfree(hidg); | |
21a9476a | 959 | mutex_unlock(&opts->lock); |
cb382536 AP |
960 | return ERR_PTR(-ENOMEM); |
961 | } | |
71adf118 FC |
962 | } |
963 | ||
21a9476a AP |
964 | mutex_unlock(&opts->lock); |
965 | ||
71adf118 | 966 | hidg->func.name = "hid"; |
71adf118 FC |
967 | hidg->func.bind = hidg_bind; |
968 | hidg->func.unbind = hidg_unbind; | |
969 | hidg->func.set_alt = hidg_set_alt; | |
970 | hidg->func.disable = hidg_disable; | |
971 | hidg->func.setup = hidg_setup; | |
cb382536 | 972 | hidg->func.free_func = hidg_free; |
71adf118 | 973 | |
99c51500 DM |
974 | /* this could me made configurable at some point */ |
975 | hidg->qlen = 4; | |
976 | ||
cb382536 | 977 | return &hidg->func; |
71adf118 FC |
978 | } |
979 | ||
cb382536 AP |
980 | DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); |
981 | MODULE_LICENSE("GPL"); | |
982 | MODULE_AUTHOR("Fabien Chouteau"); | |
983 | ||
cb382536 | 984 | int ghid_setup(struct usb_gadget *g, int count) |
71adf118 FC |
985 | { |
986 | int status; | |
987 | dev_t dev; | |
988 | ||
989 | hidg_class = class_create(THIS_MODULE, "hidg"); | |
06529407 | 990 | if (IS_ERR(hidg_class)) { |
0448d38c | 991 | status = PTR_ERR(hidg_class); |
06529407 | 992 | hidg_class = NULL; |
0448d38c | 993 | return status; |
06529407 | 994 | } |
71adf118 FC |
995 | |
996 | status = alloc_chrdev_region(&dev, 0, count, "hidg"); | |
0448d38c DC |
997 | if (status) { |
998 | class_destroy(hidg_class); | |
999 | hidg_class = NULL; | |
1000 | return status; | |
71adf118 FC |
1001 | } |
1002 | ||
0448d38c DC |
1003 | major = MAJOR(dev); |
1004 | minors = count; | |
1005 | ||
1006 | return 0; | |
71adf118 FC |
1007 | } |
1008 | ||
1009 | void ghid_cleanup(void) | |
1010 | { | |
1011 | if (major) { | |
1012 | unregister_chrdev_region(MKDEV(major, 0), minors); | |
1013 | major = minors = 0; | |
1014 | } | |
1015 | ||
1016 | class_destroy(hidg_class); | |
1017 | hidg_class = NULL; | |
1018 | } |