f_hid.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/hid.h>
  14. #include <linux/idr.h>
  15. #include <linux/cdev.h>
  16. #include <linux/mutex.h>
  17. #include <linux/poll.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/wait.h>
  20. #include <linux/sched.h>
  21. #include <linux/usb/g_hid.h>
  22. #include "u_f.h"
  23. #include "u_hid.h"
  24. #define HIDG_MINORS 4
  25. static int major, minors;
  26. static struct class *hidg_class;
  27. static DEFINE_IDA(hidg_ida);
  28. static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
  29. /*-------------------------------------------------------------------------*/
  30. /* HID gadget struct */
  31. struct f_hidg_req_list {
  32. struct usb_request *req;
  33. unsigned int pos;
  34. struct list_head list;
  35. };
  36. struct f_hidg {
  37. /* configuration */
  38. unsigned char bInterfaceSubClass;
  39. unsigned char bInterfaceProtocol;
  40. unsigned short report_desc_length;
  41. char *report_desc;
  42. unsigned short report_length;
  43. /* recv report */
  44. struct list_head completed_out_req;
  45. spinlock_t spinlock;
  46. wait_queue_head_t read_queue;
  47. unsigned int qlen;
  48. /* send report */
  49. struct mutex lock;
  50. bool write_pending;
  51. wait_queue_head_t write_queue;
  52. struct usb_request *req;
  53. int minor;
  54. struct cdev cdev;
  55. struct usb_function func;
  56. struct usb_ep *in_ep;
  57. struct usb_ep *out_ep;
  58. };
  59. static inline struct f_hidg *func_to_hidg(struct usb_function *f)
  60. {
  61. return container_of(f, struct f_hidg, func);
  62. }
  63. /*-------------------------------------------------------------------------*/
  64. /* Static descriptors */
  65. static struct usb_interface_descriptor hidg_interface_desc = {
  66. .bLength = sizeof hidg_interface_desc,
  67. .bDescriptorType = USB_DT_INTERFACE,
  68. /* .bInterfaceNumber = DYNAMIC */
  69. .bAlternateSetting = 0,
  70. .bNumEndpoints = 2,
  71. .bInterfaceClass = USB_CLASS_HID,
  72. /* .bInterfaceSubClass = DYNAMIC */
  73. /* .bInterfaceProtocol = DYNAMIC */
  74. /* .iInterface = DYNAMIC */
  75. };
  76. static struct hid_descriptor hidg_desc = {
  77. .bLength = sizeof hidg_desc,
  78. .bDescriptorType = HID_DT_HID,
  79. .bcdHID = 0x0101,
  80. .bCountryCode = 0x00,
  81. .bNumDescriptors = 0x1,
  82. /*.desc[0].bDescriptorType = DYNAMIC */
  83. /*.desc[0].wDescriptorLenght = DYNAMIC */
  84. };
  85. /* High-Speed Support */
  86. static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
  87. .bLength = USB_DT_ENDPOINT_SIZE,
  88. .bDescriptorType = USB_DT_ENDPOINT,
  89. .bEndpointAddress = USB_DIR_IN,
  90. .bmAttributes = USB_ENDPOINT_XFER_INT,
  91. /*.wMaxPacketSize = DYNAMIC */
  92. .bInterval = 4, /* FIXME: Add this field in the
  93. * HID gadget configuration?
  94. * (struct hidg_func_descriptor)
  95. */
  96. };
  97. static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
  98. .bLength = USB_DT_ENDPOINT_SIZE,
  99. .bDescriptorType = USB_DT_ENDPOINT,
  100. .bEndpointAddress = USB_DIR_OUT,
  101. .bmAttributes = USB_ENDPOINT_XFER_INT,
  102. /*.wMaxPacketSize = DYNAMIC */
  103. .bInterval = 4, /* FIXME: Add this field in the
  104. * HID gadget configuration?
  105. * (struct hidg_func_descriptor)
  106. */
  107. };
  108. static struct usb_descriptor_header *hidg_hs_descriptors[] = {
  109. (struct usb_descriptor_header *)&hidg_interface_desc,
  110. (struct usb_descriptor_header *)&hidg_desc,
  111. (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
  112. (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
  113. NULL,
  114. };
  115. /* Full-Speed Support */
  116. static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
  117. .bLength = USB_DT_ENDPOINT_SIZE,
  118. .bDescriptorType = USB_DT_ENDPOINT,
  119. .bEndpointAddress = USB_DIR_IN,
  120. .bmAttributes = USB_ENDPOINT_XFER_INT,
  121. /*.wMaxPacketSize = DYNAMIC */
  122. .bInterval = 10, /* FIXME: Add this field in the
  123. * HID gadget configuration?
  124. * (struct hidg_func_descriptor)
  125. */
  126. };
  127. static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
  128. .bLength = USB_DT_ENDPOINT_SIZE,
  129. .bDescriptorType = USB_DT_ENDPOINT,
  130. .bEndpointAddress = USB_DIR_OUT,
  131. .bmAttributes = USB_ENDPOINT_XFER_INT,
  132. /*.wMaxPacketSize = DYNAMIC */
  133. .bInterval = 10, /* FIXME: Add this field in the
  134. * HID gadget configuration?
  135. * (struct hidg_func_descriptor)
  136. */
  137. };
  138. static struct usb_descriptor_header *hidg_fs_descriptors[] = {
  139. (struct usb_descriptor_header *)&hidg_interface_desc,
  140. (struct usb_descriptor_header *)&hidg_desc,
  141. (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
  142. (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
  143. NULL,
  144. };
  145. /*-------------------------------------------------------------------------*/
  146. /* Strings */
  147. #define CT_FUNC_HID_IDX 0
  148. static struct usb_string ct_func_string_defs[] = {
  149. [CT_FUNC_HID_IDX].s = "HID Interface",
  150. {}, /* end of list */
  151. };
  152. static struct usb_gadget_strings ct_func_string_table = {
  153. .language = 0x0409, /* en-US */
  154. .strings = ct_func_string_defs,
  155. };
  156. static struct usb_gadget_strings *ct_func_strings[] = {
  157. &ct_func_string_table,
  158. NULL,
  159. };
  160. /*-------------------------------------------------------------------------*/
  161. /* Char Device */
  162. static ssize_t f_hidg_read(struct file *file, char __user *buffer,
  163. size_t count, loff_t *ptr)
  164. {
  165. struct f_hidg *hidg = file->private_data;
  166. struct f_hidg_req_list *list;
  167. struct usb_request *req;
  168. unsigned long flags;
  169. int ret;
  170. if (!count)
  171. return 0;
  172. if (!access_ok(VERIFY_WRITE, buffer, count))
  173. return -EFAULT;
  174. spin_lock_irqsave(&hidg->spinlock, flags);
  175. #define READ_COND (!list_empty(&hidg->completed_out_req))
  176. /* wait for at least one buffer to complete */
  177. while (!READ_COND) {
  178. spin_unlock_irqrestore(&hidg->spinlock, flags);
  179. if (file->f_flags & O_NONBLOCK)
  180. return -EAGAIN;
  181. if (wait_event_interruptible(hidg->read_queue, READ_COND))
  182. return -ERESTARTSYS;
  183. spin_lock_irqsave(&hidg->spinlock, flags);
  184. }
  185. /* pick the first one */
  186. list = list_first_entry(&hidg->completed_out_req,
  187. struct f_hidg_req_list, list);
  188. req = list->req;
  189. count = min_t(unsigned int, count, req->actual - list->pos);
  190. spin_unlock_irqrestore(&hidg->spinlock, flags);
  191. /* copy to user outside spinlock */
  192. count -= copy_to_user(buffer, req->buf + list->pos, count);
  193. list->pos += count;
  194. /*
  195. * if this request is completely handled and transfered to
  196. * userspace, remove its entry from the list and requeue it
  197. * again. Otherwise, we will revisit it again upon the next
  198. * call, taking into account its current read position.
  199. */
  200. if (list->pos == req->actual) {
  201. spin_lock_irqsave(&hidg->spinlock, flags);
  202. list_del(&list->list);
  203. kfree(list);
  204. spin_unlock_irqrestore(&hidg->spinlock, flags);
  205. req->length = hidg->report_length;
  206. ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
  207. if (ret < 0)
  208. return ret;
  209. }
  210. return count;
  211. }
  212. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  213. {
  214. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  215. if (req->status != 0) {
  216. ERROR(hidg->func.config->cdev,
  217. "End Point Request ERROR: %d\n", req->status);
  218. }
  219. hidg->write_pending = 0;
  220. wake_up(&hidg->write_queue);
  221. }
  222. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  223. size_t count, loff_t *offp)
  224. {
  225. struct f_hidg *hidg = file->private_data;
  226. ssize_t status = -ENOMEM;
  227. if (!access_ok(VERIFY_READ, buffer, count))
  228. return -EFAULT;
  229. mutex_lock(&hidg->lock);
  230. #define WRITE_COND (!hidg->write_pending)
  231. /* write queue */
  232. while (!WRITE_COND) {
  233. mutex_unlock(&hidg->lock);
  234. if (file->f_flags & O_NONBLOCK)
  235. return -EAGAIN;
  236. if (wait_event_interruptible_exclusive(
  237. hidg->write_queue, WRITE_COND))
  238. return -ERESTARTSYS;
  239. mutex_lock(&hidg->lock);
  240. }
  241. count = min_t(unsigned, count, hidg->report_length);
  242. status = copy_from_user(hidg->req->buf, buffer, count);
  243. if (status != 0) {
  244. ERROR(hidg->func.config->cdev,
  245. "copy_from_user error\n");
  246. mutex_unlock(&hidg->lock);
  247. return -EINVAL;
  248. }
  249. hidg->req->status = 0;
  250. hidg->req->zero = 0;
  251. hidg->req->length = count;
  252. hidg->req->complete = f_hidg_req_complete;
  253. hidg->req->context = hidg;
  254. hidg->write_pending = 1;
  255. status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
  256. if (status < 0) {
  257. ERROR(hidg->func.config->cdev,
  258. "usb_ep_queue error on int endpoint %zd\n", status);
  259. hidg->write_pending = 0;
  260. wake_up(&hidg->write_queue);
  261. } else {
  262. status = count;
  263. }
  264. mutex_unlock(&hidg->lock);
  265. return status;
  266. }
  267. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  268. {
  269. struct f_hidg *hidg = file->private_data;
  270. unsigned int ret = 0;
  271. poll_wait(file, &hidg->read_queue, wait);
  272. poll_wait(file, &hidg->write_queue, wait);
  273. if (WRITE_COND)
  274. ret |= POLLOUT | POLLWRNORM;
  275. if (READ_COND)
  276. ret |= POLLIN | POLLRDNORM;
  277. return ret;
  278. }
  279. #undef WRITE_COND
  280. #undef READ_COND
  281. static int f_hidg_release(struct inode *inode, struct file *fd)
  282. {
  283. fd->private_data = NULL;
  284. return 0;
  285. }
  286. static int f_hidg_open(struct inode *inode, struct file *fd)
  287. {
  288. struct f_hidg *hidg =
  289. container_of(inode->i_cdev, struct f_hidg, cdev);
  290. fd->private_data = hidg;
  291. return 0;
  292. }
  293. /*-------------------------------------------------------------------------*/
  294. /* usb_function */
  295. static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
  296. unsigned length)
  297. {
  298. return alloc_ep_req(ep, length, length);
  299. }
  300. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  301. {
  302. struct f_hidg *hidg = (struct f_hidg *) req->context;
  303. struct f_hidg_req_list *req_list;
  304. unsigned long flags;
  305. req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
  306. if (!req_list)
  307. return;
  308. req_list->req = req;
  309. spin_lock_irqsave(&hidg->spinlock, flags);
  310. list_add_tail(&req_list->list, &hidg->completed_out_req);
  311. spin_unlock_irqrestore(&hidg->spinlock, flags);
  312. wake_up(&hidg->read_queue);
  313. }
  314. static int hidg_setup(struct usb_function *f,
  315. const struct usb_ctrlrequest *ctrl)
  316. {
  317. struct f_hidg *hidg = func_to_hidg(f);
  318. struct usb_composite_dev *cdev = f->config->cdev;
  319. struct usb_request *req = cdev->req;
  320. int status = 0;
  321. __u16 value, length;
  322. value = __le16_to_cpu(ctrl->wValue);
  323. length = __le16_to_cpu(ctrl->wLength);
  324. VDBG(cdev,
  325. "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
  326. __func__, ctrl->bRequestType, ctrl->bRequest, value);
  327. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  328. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  329. | HID_REQ_GET_REPORT):
  330. VDBG(cdev, "get_report\n");
  331. /* send an empty report */
  332. length = min_t(unsigned, length, hidg->report_length);
  333. memset(req->buf, 0x0, length);
  334. goto respond;
  335. break;
  336. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  337. | HID_REQ_GET_PROTOCOL):
  338. VDBG(cdev, "get_protocol\n");
  339. goto stall;
  340. break;
  341. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  342. | HID_REQ_SET_REPORT):
  343. VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
  344. goto stall;
  345. break;
  346. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  347. | HID_REQ_SET_PROTOCOL):
  348. VDBG(cdev, "set_protocol\n");
  349. goto stall;
  350. break;
  351. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  352. | USB_REQ_GET_DESCRIPTOR):
  353. switch (value >> 8) {
  354. case HID_DT_HID:
  355. {
  356. struct hid_descriptor hidg_desc_copy = hidg_desc;
  357. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  358. hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
  359. hidg_desc_copy.desc[0].wDescriptorLength =
  360. cpu_to_le16(hidg->report_desc_length);
  361. length = min_t(unsigned short, length,
  362. hidg_desc_copy.bLength);
  363. memcpy(req->buf, &hidg_desc_copy, length);
  364. goto respond;
  365. break;
  366. }
  367. case HID_DT_REPORT:
  368. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  369. length = min_t(unsigned short, length,
  370. hidg->report_desc_length);
  371. memcpy(req->buf, hidg->report_desc, length);
  372. goto respond;
  373. break;
  374. default:
  375. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  376. value >> 8);
  377. goto stall;
  378. break;
  379. }
  380. break;
  381. default:
  382. VDBG(cdev, "Unknown request 0x%x\n",
  383. ctrl->bRequest);
  384. goto stall;
  385. break;
  386. }
  387. stall:
  388. return -EOPNOTSUPP;
  389. respond:
  390. req->zero = 0;
  391. req->length = length;
  392. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  393. if (status < 0)
  394. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  395. return status;
  396. }
  397. static void hidg_disable(struct usb_function *f)
  398. {
  399. struct f_hidg *hidg = func_to_hidg(f);
  400. struct f_hidg_req_list *list, *next;
  401. usb_ep_disable(hidg->in_ep);
  402. hidg->in_ep->driver_data = NULL;
  403. usb_ep_disable(hidg->out_ep);
  404. hidg->out_ep->driver_data = NULL;
  405. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  406. list_del(&list->list);
  407. kfree(list);
  408. }
  409. }
  410. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  411. {
  412. struct usb_composite_dev *cdev = f->config->cdev;
  413. struct f_hidg *hidg = func_to_hidg(f);
  414. int i, status = 0;
  415. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  416. if (hidg->in_ep != NULL) {
  417. /* restart endpoint */
  418. if (hidg->in_ep->driver_data != NULL)
  419. usb_ep_disable(hidg->in_ep);
  420. status = config_ep_by_speed(f->config->cdev->gadget, f,
  421. hidg->in_ep);
  422. if (status) {
  423. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  424. goto fail;
  425. }
  426. status = usb_ep_enable(hidg->in_ep);
  427. if (status < 0) {
  428. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  429. goto fail;
  430. }
  431. hidg->in_ep->driver_data = hidg;
  432. }
  433. if (hidg->out_ep != NULL) {
  434. /* restart endpoint */
  435. if (hidg->out_ep->driver_data != NULL)
  436. usb_ep_disable(hidg->out_ep);
  437. status = config_ep_by_speed(f->config->cdev->gadget, f,
  438. hidg->out_ep);
  439. if (status) {
  440. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  441. goto fail;
  442. }
  443. status = usb_ep_enable(hidg->out_ep);
  444. if (status < 0) {
  445. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  446. goto fail;
  447. }
  448. hidg->out_ep->driver_data = hidg;
  449. /*
  450. * allocate a bunch of read buffers and queue them all at once.
  451. */
  452. for (i = 0; i < hidg->qlen && status == 0; i++) {
  453. struct usb_request *req =
  454. hidg_alloc_ep_req(hidg->out_ep,
  455. hidg->report_length);
  456. if (req) {
  457. req->complete = hidg_set_report_complete;
  458. req->context = hidg;
  459. status = usb_ep_queue(hidg->out_ep, req,
  460. GFP_ATOMIC);
  461. if (status)
  462. ERROR(cdev, "%s queue req --> %d\n",
  463. hidg->out_ep->name, status);
  464. } else {
  465. usb_ep_disable(hidg->out_ep);
  466. hidg->out_ep->driver_data = NULL;
  467. status = -ENOMEM;
  468. goto fail;
  469. }
  470. }
  471. }
  472. fail:
  473. return status;
  474. }
  475. static const struct file_operations f_hidg_fops = {
  476. .owner = THIS_MODULE,
  477. .open = f_hidg_open,
  478. .release = f_hidg_release,
  479. .write = f_hidg_write,
  480. .read = f_hidg_read,
  481. .poll = f_hidg_poll,
  482. .llseek = noop_llseek,
  483. };
  484. static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
  485. {
  486. struct usb_ep *ep;
  487. struct f_hidg *hidg = func_to_hidg(f);
  488. struct usb_string *us;
  489. struct device *device;
  490. int status;
  491. dev_t dev;
  492. /* maybe allocate device-global string IDs, and patch descriptors */
  493. us = usb_gstrings_attach(c->cdev, ct_func_strings,
  494. ARRAY_SIZE(ct_func_string_defs));
  495. if (IS_ERR(us))
  496. return PTR_ERR(us);
  497. hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
  498. /* allocate instance-specific interface IDs, and patch descriptors */
  499. status = usb_interface_id(c, f);
  500. if (status < 0)
  501. goto fail;
  502. hidg_interface_desc.bInterfaceNumber = status;
  503. /* allocate instance-specific endpoints */
  504. status = -ENODEV;
  505. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  506. if (!ep)
  507. goto fail;
  508. ep->driver_data = c->cdev; /* claim */
  509. hidg->in_ep = ep;
  510. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  511. if (!ep)
  512. goto fail;
  513. ep->driver_data = c->cdev; /* claim */
  514. hidg->out_ep = ep;
  515. /* preallocate request and buffer */
  516. status = -ENOMEM;
  517. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  518. if (!hidg->req)
  519. goto fail;
  520. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  521. if (!hidg->req->buf)
  522. goto fail;
  523. /* set descriptor dynamic values */
  524. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  525. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  526. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  527. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  528. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  529. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  530. /*
  531. * We can use hidg_desc struct here but we should not relay
  532. * that its content won't change after returning from this function.
  533. */
  534. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  535. hidg_desc.desc[0].wDescriptorLength =
  536. cpu_to_le16(hidg->report_desc_length);
  537. hidg_hs_in_ep_desc.bEndpointAddress =
  538. hidg_fs_in_ep_desc.bEndpointAddress;
  539. hidg_hs_out_ep_desc.bEndpointAddress =
  540. hidg_fs_out_ep_desc.bEndpointAddress;
  541. status = usb_assign_descriptors(f, hidg_fs_descriptors,
  542. hidg_hs_descriptors, NULL);
  543. if (status)
  544. goto fail;
  545. mutex_init(&hidg->lock);
  546. spin_lock_init(&hidg->spinlock);
  547. init_waitqueue_head(&hidg->write_queue);
  548. init_waitqueue_head(&hidg->read_queue);
  549. INIT_LIST_HEAD(&hidg->completed_out_req);
  550. /* create char device */
  551. cdev_init(&hidg->cdev, &f_hidg_fops);
  552. dev = MKDEV(major, hidg->minor);
  553. status = cdev_add(&hidg->cdev, dev, 1);
  554. if (status)
  555. goto fail_free_descs;
  556. device = device_create(hidg_class, NULL, dev, NULL,
  557. "%s%d", "hidg", hidg->minor);
  558. if (IS_ERR(device)) {
  559. status = PTR_ERR(device);
  560. goto del;
  561. }
  562. return 0;
  563. del:
  564. cdev_del(&hidg->cdev);
  565. fail_free_descs:
  566. usb_free_all_descriptors(f);
  567. fail:
  568. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  569. if (hidg->req != NULL) {
  570. kfree(hidg->req->buf);
  571. if (hidg->in_ep != NULL)
  572. usb_ep_free_request(hidg->in_ep, hidg->req);
  573. }
  574. return status;
  575. }
  576. static inline int hidg_get_minor(void)
  577. {
  578. int ret;
  579. ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
  580. if (ret >= HIDG_MINORS) {
  581. ida_simple_remove(&hidg_ida, ret);
  582. ret = -ENODEV;
  583. }
  584. return ret;
  585. }
  586. static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
  587. {
  588. return container_of(to_config_group(item), struct f_hid_opts,
  589. func_inst.group);
  590. }
  591. CONFIGFS_ATTR_STRUCT(f_hid_opts);
  592. CONFIGFS_ATTR_OPS(f_hid_opts);
  593. static void hid_attr_release(struct config_item *item)
  594. {
  595. struct f_hid_opts *opts = to_f_hid_opts(item);
  596. usb_put_function_instance(&opts->func_inst);
  597. }
  598. static struct configfs_item_operations hidg_item_ops = {
  599. .release = hid_attr_release,
  600. .show_attribute = f_hid_opts_attr_show,
  601. .store_attribute = f_hid_opts_attr_store,
  602. };
  603. #define F_HID_OPT(name, prec, limit) \
  604. static ssize_t f_hid_opts_##name##_show(struct f_hid_opts *opts, char *page)\
  605. { \
  606. int result; \
  607. \
  608. mutex_lock(&opts->lock); \
  609. result = sprintf(page, "%d\n", opts->name); \
  610. mutex_unlock(&opts->lock); \
  611. \
  612. return result; \
  613. } \
  614. \
  615. static ssize_t f_hid_opts_##name##_store(struct f_hid_opts *opts, \
  616. const char *page, size_t len) \
  617. { \
  618. int ret; \
  619. u##prec num; \
  620. \
  621. mutex_lock(&opts->lock); \
  622. if (opts->refcnt) { \
  623. ret = -EBUSY; \
  624. goto end; \
  625. } \
  626. \
  627. ret = kstrtou##prec(page, 0, &num); \
  628. if (ret) \
  629. goto end; \
  630. \
  631. if (num > limit) { \
  632. ret = -EINVAL; \
  633. goto end; \
  634. } \
  635. opts->name = num; \
  636. ret = len; \
  637. \
  638. end: \
  639. mutex_unlock(&opts->lock); \
  640. return ret; \
  641. } \
  642. \
  643. static struct f_hid_opts_attribute f_hid_opts_##name = \
  644. __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, f_hid_opts_##name##_show,\
  645. f_hid_opts_##name##_store)
  646. F_HID_OPT(subclass, 8, 255);
  647. F_HID_OPT(protocol, 8, 255);
  648. F_HID_OPT(report_length, 16, 65535);
  649. static ssize_t f_hid_opts_report_desc_show(struct f_hid_opts *opts, char *page)
  650. {
  651. int result;
  652. mutex_lock(&opts->lock);
  653. result = opts->report_desc_length;
  654. memcpy(page, opts->report_desc, opts->report_desc_length);
  655. mutex_unlock(&opts->lock);
  656. return result;
  657. }
  658. static ssize_t f_hid_opts_report_desc_store(struct f_hid_opts *opts,
  659. const char *page, size_t len)
  660. {
  661. int ret = -EBUSY;
  662. char *d;
  663. mutex_lock(&opts->lock);
  664. if (opts->refcnt)
  665. goto end;
  666. if (len > PAGE_SIZE) {
  667. ret = -ENOSPC;
  668. goto end;
  669. }
  670. d = kmemdup(page, len, GFP_KERNEL);
  671. if (!d) {
  672. ret = -ENOMEM;
  673. goto end;
  674. }
  675. kfree(opts->report_desc);
  676. opts->report_desc = d;
  677. opts->report_desc_length = len;
  678. opts->report_desc_alloc = true;
  679. ret = len;
  680. end:
  681. mutex_unlock(&opts->lock);
  682. return ret;
  683. }
  684. static struct f_hid_opts_attribute f_hid_opts_report_desc =
  685. __CONFIGFS_ATTR(report_desc, S_IRUGO | S_IWUSR,
  686. f_hid_opts_report_desc_show,
  687. f_hid_opts_report_desc_store);
  688. static struct configfs_attribute *hid_attrs[] = {
  689. &f_hid_opts_subclass.attr,
  690. &f_hid_opts_protocol.attr,
  691. &f_hid_opts_report_length.attr,
  692. &f_hid_opts_report_desc.attr,
  693. NULL,
  694. };
  695. static struct config_item_type hid_func_type = {
  696. .ct_item_ops = &hidg_item_ops,
  697. .ct_attrs = hid_attrs,
  698. .ct_owner = THIS_MODULE,
  699. };
  700. static inline void hidg_put_minor(int minor)
  701. {
  702. ida_simple_remove(&hidg_ida, minor);
  703. }
  704. static void hidg_free_inst(struct usb_function_instance *f)
  705. {
  706. struct f_hid_opts *opts;
  707. opts = container_of(f, struct f_hid_opts, func_inst);
  708. mutex_lock(&hidg_ida_lock);
  709. hidg_put_minor(opts->minor);
  710. if (idr_is_empty(&hidg_ida.idr))
  711. ghid_cleanup();
  712. mutex_unlock(&hidg_ida_lock);
  713. if (opts->report_desc_alloc)
  714. kfree(opts->report_desc);
  715. kfree(opts);
  716. }
  717. static struct usb_function_instance *hidg_alloc_inst(void)
  718. {
  719. struct f_hid_opts *opts;
  720. struct usb_function_instance *ret;
  721. int status = 0;
  722. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  723. if (!opts)
  724. return ERR_PTR(-ENOMEM);
  725. mutex_init(&opts->lock);
  726. opts->func_inst.free_func_inst = hidg_free_inst;
  727. ret = &opts->func_inst;
  728. mutex_lock(&hidg_ida_lock);
  729. if (idr_is_empty(&hidg_ida.idr)) {
  730. status = ghid_setup(NULL, HIDG_MINORS);
  731. if (status) {
  732. ret = ERR_PTR(status);
  733. kfree(opts);
  734. goto unlock;
  735. }
  736. }
  737. opts->minor = hidg_get_minor();
  738. if (opts->minor < 0) {
  739. ret = ERR_PTR(opts->minor);
  740. kfree(opts);
  741. if (idr_is_empty(&hidg_ida.idr))
  742. ghid_cleanup();
  743. goto unlock;
  744. }
  745. config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
  746. unlock:
  747. mutex_unlock(&hidg_ida_lock);
  748. return ret;
  749. }
  750. static void hidg_free(struct usb_function *f)
  751. {
  752. struct f_hidg *hidg;
  753. struct f_hid_opts *opts;
  754. hidg = func_to_hidg(f);
  755. opts = container_of(f->fi, struct f_hid_opts, func_inst);
  756. kfree(hidg->report_desc);
  757. kfree(hidg);
  758. mutex_lock(&opts->lock);
  759. --opts->refcnt;
  760. mutex_unlock(&opts->lock);
  761. }
  762. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  763. {
  764. struct f_hidg *hidg = func_to_hidg(f);
  765. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  766. cdev_del(&hidg->cdev);
  767. /* disable/free request and end point */
  768. usb_ep_disable(hidg->in_ep);
  769. kfree(hidg->req->buf);
  770. usb_ep_free_request(hidg->in_ep, hidg->req);
  771. usb_free_all_descriptors(f);
  772. }
  773. static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
  774. {
  775. struct f_hidg *hidg;
  776. struct f_hid_opts *opts;
  777. /* allocate and initialize one new instance */
  778. hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
  779. if (!hidg)
  780. return ERR_PTR(-ENOMEM);
  781. opts = container_of(fi, struct f_hid_opts, func_inst);
  782. mutex_lock(&opts->lock);
  783. ++opts->refcnt;
  784. hidg->minor = opts->minor;
  785. hidg->bInterfaceSubClass = opts->subclass;
  786. hidg->bInterfaceProtocol = opts->protocol;
  787. hidg->report_length = opts->report_length;
  788. hidg->report_desc_length = opts->report_desc_length;
  789. if (opts->report_desc) {
  790. hidg->report_desc = kmemdup(opts->report_desc,
  791. opts->report_desc_length,
  792. GFP_KERNEL);
  793. if (!hidg->report_desc) {
  794. kfree(hidg);
  795. mutex_unlock(&opts->lock);
  796. return ERR_PTR(-ENOMEM);
  797. }
  798. }
  799. mutex_unlock(&opts->lock);
  800. hidg->func.name = "hid";
  801. hidg->func.bind = hidg_bind;
  802. hidg->func.unbind = hidg_unbind;
  803. hidg->func.set_alt = hidg_set_alt;
  804. hidg->func.disable = hidg_disable;
  805. hidg->func.setup = hidg_setup;
  806. hidg->func.free_func = hidg_free;
  807. /* this could me made configurable at some point */
  808. hidg->qlen = 4;
  809. return &hidg->func;
  810. }
  811. DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
  812. MODULE_LICENSE("GPL");
  813. MODULE_AUTHOR("Fabien Chouteau");
  814. int ghid_setup(struct usb_gadget *g, int count)
  815. {
  816. int status;
  817. dev_t dev;
  818. hidg_class = class_create(THIS_MODULE, "hidg");
  819. if (IS_ERR(hidg_class)) {
  820. status = PTR_ERR(hidg_class);
  821. hidg_class = NULL;
  822. return status;
  823. }
  824. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  825. if (status) {
  826. class_destroy(hidg_class);
  827. hidg_class = NULL;
  828. return status;
  829. }
  830. major = MAJOR(dev);
  831. minors = count;
  832. return 0;
  833. }
  834. void ghid_cleanup(void)
  835. {
  836. if (major) {
  837. unregister_chrdev_region(MKDEV(major, 0), minors);
  838. major = minors = 0;
  839. }
  840. class_destroy(hidg_class);
  841. hidg_class = NULL;
  842. }