f_hid.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  356. length = min_t(unsigned short, length,
  357. hidg_desc.bLength);
  358. memcpy(req->buf, &hidg_desc, length);
  359. goto respond;
  360. break;
  361. case HID_DT_REPORT:
  362. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  363. length = min_t(unsigned short, length,
  364. hidg->report_desc_length);
  365. memcpy(req->buf, hidg->report_desc, length);
  366. goto respond;
  367. break;
  368. default:
  369. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  370. value >> 8);
  371. goto stall;
  372. break;
  373. }
  374. break;
  375. default:
  376. VDBG(cdev, "Unknown request 0x%x\n",
  377. ctrl->bRequest);
  378. goto stall;
  379. break;
  380. }
  381. stall:
  382. return -EOPNOTSUPP;
  383. respond:
  384. req->zero = 0;
  385. req->length = length;
  386. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  387. if (status < 0)
  388. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  389. return status;
  390. }
  391. static void hidg_disable(struct usb_function *f)
  392. {
  393. struct f_hidg *hidg = func_to_hidg(f);
  394. struct f_hidg_req_list *list, *next;
  395. usb_ep_disable(hidg->in_ep);
  396. hidg->in_ep->driver_data = NULL;
  397. usb_ep_disable(hidg->out_ep);
  398. hidg->out_ep->driver_data = NULL;
  399. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  400. list_del(&list->list);
  401. kfree(list);
  402. }
  403. }
  404. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  405. {
  406. struct usb_composite_dev *cdev = f->config->cdev;
  407. struct f_hidg *hidg = func_to_hidg(f);
  408. int i, status = 0;
  409. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  410. if (hidg->in_ep != NULL) {
  411. /* restart endpoint */
  412. if (hidg->in_ep->driver_data != NULL)
  413. usb_ep_disable(hidg->in_ep);
  414. status = config_ep_by_speed(f->config->cdev->gadget, f,
  415. hidg->in_ep);
  416. if (status) {
  417. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  418. goto fail;
  419. }
  420. status = usb_ep_enable(hidg->in_ep);
  421. if (status < 0) {
  422. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  423. goto fail;
  424. }
  425. hidg->in_ep->driver_data = hidg;
  426. }
  427. if (hidg->out_ep != NULL) {
  428. /* restart endpoint */
  429. if (hidg->out_ep->driver_data != NULL)
  430. usb_ep_disable(hidg->out_ep);
  431. status = config_ep_by_speed(f->config->cdev->gadget, f,
  432. hidg->out_ep);
  433. if (status) {
  434. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  435. goto fail;
  436. }
  437. status = usb_ep_enable(hidg->out_ep);
  438. if (status < 0) {
  439. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  440. goto fail;
  441. }
  442. hidg->out_ep->driver_data = hidg;
  443. /*
  444. * allocate a bunch of read buffers and queue them all at once.
  445. */
  446. for (i = 0; i < hidg->qlen && status == 0; i++) {
  447. struct usb_request *req =
  448. hidg_alloc_ep_req(hidg->out_ep,
  449. hidg->report_length);
  450. if (req) {
  451. req->complete = hidg_set_report_complete;
  452. req->context = hidg;
  453. status = usb_ep_queue(hidg->out_ep, req,
  454. GFP_ATOMIC);
  455. if (status)
  456. ERROR(cdev, "%s queue req --> %d\n",
  457. hidg->out_ep->name, status);
  458. } else {
  459. usb_ep_disable(hidg->out_ep);
  460. hidg->out_ep->driver_data = NULL;
  461. status = -ENOMEM;
  462. goto fail;
  463. }
  464. }
  465. }
  466. fail:
  467. return status;
  468. }
  469. static const struct file_operations f_hidg_fops = {
  470. .owner = THIS_MODULE,
  471. .open = f_hidg_open,
  472. .release = f_hidg_release,
  473. .write = f_hidg_write,
  474. .read = f_hidg_read,
  475. .poll = f_hidg_poll,
  476. .llseek = noop_llseek,
  477. };
  478. static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
  479. {
  480. struct usb_ep *ep;
  481. struct f_hidg *hidg = func_to_hidg(f);
  482. struct usb_string *us;
  483. struct device *device;
  484. int status;
  485. dev_t dev;
  486. /* maybe allocate device-global string IDs, and patch descriptors */
  487. us = usb_gstrings_attach(c->cdev, ct_func_strings,
  488. ARRAY_SIZE(ct_func_string_defs));
  489. if (IS_ERR(us))
  490. return PTR_ERR(us);
  491. hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
  492. /* allocate instance-specific interface IDs, and patch descriptors */
  493. status = usb_interface_id(c, f);
  494. if (status < 0)
  495. goto fail;
  496. hidg_interface_desc.bInterfaceNumber = status;
  497. /* allocate instance-specific endpoints */
  498. status = -ENODEV;
  499. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  500. if (!ep)
  501. goto fail;
  502. ep->driver_data = c->cdev; /* claim */
  503. hidg->in_ep = ep;
  504. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  505. if (!ep)
  506. goto fail;
  507. ep->driver_data = c->cdev; /* claim */
  508. hidg->out_ep = ep;
  509. /* preallocate request and buffer */
  510. status = -ENOMEM;
  511. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  512. if (!hidg->req)
  513. goto fail;
  514. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  515. if (!hidg->req->buf)
  516. goto fail;
  517. /* set descriptor dynamic values */
  518. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  519. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  520. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  521. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  522. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  523. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  524. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  525. hidg_desc.desc[0].wDescriptorLength =
  526. cpu_to_le16(hidg->report_desc_length);
  527. hidg_hs_in_ep_desc.bEndpointAddress =
  528. hidg_fs_in_ep_desc.bEndpointAddress;
  529. hidg_hs_out_ep_desc.bEndpointAddress =
  530. hidg_fs_out_ep_desc.bEndpointAddress;
  531. status = usb_assign_descriptors(f, hidg_fs_descriptors,
  532. hidg_hs_descriptors, NULL);
  533. if (status)
  534. goto fail;
  535. mutex_init(&hidg->lock);
  536. spin_lock_init(&hidg->spinlock);
  537. init_waitqueue_head(&hidg->write_queue);
  538. init_waitqueue_head(&hidg->read_queue);
  539. INIT_LIST_HEAD(&hidg->completed_out_req);
  540. /* create char device */
  541. cdev_init(&hidg->cdev, &f_hidg_fops);
  542. dev = MKDEV(major, hidg->minor);
  543. status = cdev_add(&hidg->cdev, dev, 1);
  544. if (status)
  545. goto fail_free_descs;
  546. device = device_create(hidg_class, NULL, dev, NULL,
  547. "%s%d", "hidg", hidg->minor);
  548. if (IS_ERR(device)) {
  549. status = PTR_ERR(device);
  550. goto del;
  551. }
  552. return 0;
  553. del:
  554. cdev_del(&hidg->cdev);
  555. fail_free_descs:
  556. usb_free_all_descriptors(f);
  557. fail:
  558. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  559. if (hidg->req != NULL) {
  560. kfree(hidg->req->buf);
  561. if (hidg->in_ep != NULL)
  562. usb_ep_free_request(hidg->in_ep, hidg->req);
  563. }
  564. return status;
  565. }
  566. static inline int hidg_get_minor(void)
  567. {
  568. int ret;
  569. ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
  570. return ret;
  571. }
  572. static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
  573. {
  574. return container_of(to_config_group(item), struct f_hid_opts,
  575. func_inst.group);
  576. }
  577. CONFIGFS_ATTR_STRUCT(f_hid_opts);
  578. CONFIGFS_ATTR_OPS(f_hid_opts);
  579. static void hid_attr_release(struct config_item *item)
  580. {
  581. struct f_hid_opts *opts = to_f_hid_opts(item);
  582. usb_put_function_instance(&opts->func_inst);
  583. }
  584. static struct configfs_item_operations hidg_item_ops = {
  585. .release = hid_attr_release,
  586. .show_attribute = f_hid_opts_attr_show,
  587. .store_attribute = f_hid_opts_attr_store,
  588. };
  589. #define F_HID_OPT(name, prec, limit) \
  590. static ssize_t f_hid_opts_##name##_show(struct f_hid_opts *opts, char *page)\
  591. { \
  592. int result; \
  593. \
  594. mutex_lock(&opts->lock); \
  595. result = sprintf(page, "%d\n", opts->name); \
  596. mutex_unlock(&opts->lock); \
  597. \
  598. return result; \
  599. } \
  600. \
  601. static ssize_t f_hid_opts_##name##_store(struct f_hid_opts *opts, \
  602. const char *page, size_t len) \
  603. { \
  604. int ret; \
  605. u##prec num; \
  606. \
  607. mutex_lock(&opts->lock); \
  608. if (opts->refcnt) { \
  609. ret = -EBUSY; \
  610. goto end; \
  611. } \
  612. \
  613. ret = kstrtou##prec(page, 0, &num); \
  614. if (ret) \
  615. goto end; \
  616. \
  617. if (num > limit) { \
  618. ret = -EINVAL; \
  619. goto end; \
  620. } \
  621. opts->name = num; \
  622. ret = len; \
  623. \
  624. end: \
  625. mutex_unlock(&opts->lock); \
  626. return ret; \
  627. } \
  628. \
  629. static struct f_hid_opts_attribute f_hid_opts_##name = \
  630. __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, f_hid_opts_##name##_show,\
  631. f_hid_opts_##name##_store)
  632. F_HID_OPT(subclass, 8, 255);
  633. F_HID_OPT(protocol, 8, 255);
  634. F_HID_OPT(report_length, 16, 65535);
  635. static ssize_t f_hid_opts_report_desc_show(struct f_hid_opts *opts, char *page)
  636. {
  637. int result;
  638. mutex_lock(&opts->lock);
  639. result = opts->report_desc_length;
  640. memcpy(page, opts->report_desc, opts->report_desc_length);
  641. mutex_unlock(&opts->lock);
  642. return result;
  643. }
  644. static ssize_t f_hid_opts_report_desc_store(struct f_hid_opts *opts,
  645. const char *page, size_t len)
  646. {
  647. int ret = -EBUSY;
  648. char *d;
  649. mutex_lock(&opts->lock);
  650. if (opts->refcnt)
  651. goto end;
  652. if (len > PAGE_SIZE) {
  653. ret = -ENOSPC;
  654. goto end;
  655. }
  656. d = kmemdup(page, len, GFP_KERNEL);
  657. if (!d) {
  658. ret = -ENOMEM;
  659. goto end;
  660. }
  661. kfree(opts->report_desc);
  662. opts->report_desc = d;
  663. opts->report_desc_length = len;
  664. opts->report_desc_alloc = true;
  665. ret = len;
  666. end:
  667. mutex_unlock(&opts->lock);
  668. return ret;
  669. }
  670. static struct f_hid_opts_attribute f_hid_opts_report_desc =
  671. __CONFIGFS_ATTR(report_desc, S_IRUGO | S_IWUSR,
  672. f_hid_opts_report_desc_show,
  673. f_hid_opts_report_desc_store);
  674. static struct configfs_attribute *hid_attrs[] = {
  675. &f_hid_opts_subclass.attr,
  676. &f_hid_opts_protocol.attr,
  677. &f_hid_opts_report_length.attr,
  678. &f_hid_opts_report_desc.attr,
  679. NULL,
  680. };
  681. static struct config_item_type hid_func_type = {
  682. .ct_item_ops = &hidg_item_ops,
  683. .ct_attrs = hid_attrs,
  684. .ct_owner = THIS_MODULE,
  685. };
  686. static inline void hidg_put_minor(int minor)
  687. {
  688. ida_simple_remove(&hidg_ida, minor);
  689. }
  690. static void hidg_free_inst(struct usb_function_instance *f)
  691. {
  692. struct f_hid_opts *opts;
  693. opts = container_of(f, struct f_hid_opts, func_inst);
  694. mutex_lock(&hidg_ida_lock);
  695. hidg_put_minor(opts->minor);
  696. if (idr_is_empty(&hidg_ida.idr))
  697. ghid_cleanup();
  698. mutex_unlock(&hidg_ida_lock);
  699. if (opts->report_desc_alloc)
  700. kfree(opts->report_desc);
  701. kfree(opts);
  702. }
  703. static struct usb_function_instance *hidg_alloc_inst(void)
  704. {
  705. struct f_hid_opts *opts;
  706. struct usb_function_instance *ret;
  707. int status = 0;
  708. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  709. if (!opts)
  710. return ERR_PTR(-ENOMEM);
  711. mutex_init(&opts->lock);
  712. opts->func_inst.free_func_inst = hidg_free_inst;
  713. ret = &opts->func_inst;
  714. mutex_lock(&hidg_ida_lock);
  715. if (idr_is_empty(&hidg_ida.idr)) {
  716. status = ghid_setup(NULL, HIDG_MINORS);
  717. if (status) {
  718. ret = ERR_PTR(status);
  719. kfree(opts);
  720. goto unlock;
  721. }
  722. }
  723. opts->minor = hidg_get_minor();
  724. if (opts->minor < 0) {
  725. ret = ERR_PTR(opts->minor);
  726. kfree(opts);
  727. if (idr_is_empty(&hidg_ida.idr))
  728. ghid_cleanup();
  729. goto unlock;
  730. }
  731. config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
  732. unlock:
  733. mutex_unlock(&hidg_ida_lock);
  734. return ret;
  735. }
  736. static void hidg_free(struct usb_function *f)
  737. {
  738. struct f_hidg *hidg;
  739. struct f_hid_opts *opts;
  740. hidg = func_to_hidg(f);
  741. opts = container_of(f->fi, struct f_hid_opts, func_inst);
  742. kfree(hidg->report_desc);
  743. kfree(hidg);
  744. mutex_lock(&opts->lock);
  745. --opts->refcnt;
  746. mutex_unlock(&opts->lock);
  747. }
  748. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  749. {
  750. struct f_hidg *hidg = func_to_hidg(f);
  751. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  752. cdev_del(&hidg->cdev);
  753. /* disable/free request and end point */
  754. usb_ep_disable(hidg->in_ep);
  755. usb_ep_dequeue(hidg->in_ep, hidg->req);
  756. kfree(hidg->req->buf);
  757. usb_ep_free_request(hidg->in_ep, hidg->req);
  758. usb_free_all_descriptors(f);
  759. }
  760. static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
  761. {
  762. struct f_hidg *hidg;
  763. struct f_hid_opts *opts;
  764. /* allocate and initialize one new instance */
  765. hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
  766. if (!hidg)
  767. return ERR_PTR(-ENOMEM);
  768. opts = container_of(fi, struct f_hid_opts, func_inst);
  769. mutex_lock(&opts->lock);
  770. ++opts->refcnt;
  771. hidg->minor = opts->minor;
  772. hidg->bInterfaceSubClass = opts->subclass;
  773. hidg->bInterfaceProtocol = opts->protocol;
  774. hidg->report_length = opts->report_length;
  775. hidg->report_desc_length = opts->report_desc_length;
  776. if (opts->report_desc) {
  777. hidg->report_desc = kmemdup(opts->report_desc,
  778. opts->report_desc_length,
  779. GFP_KERNEL);
  780. if (!hidg->report_desc) {
  781. kfree(hidg);
  782. mutex_unlock(&opts->lock);
  783. return ERR_PTR(-ENOMEM);
  784. }
  785. }
  786. mutex_unlock(&opts->lock);
  787. hidg->func.name = "hid";
  788. hidg->func.bind = hidg_bind;
  789. hidg->func.unbind = hidg_unbind;
  790. hidg->func.set_alt = hidg_set_alt;
  791. hidg->func.disable = hidg_disable;
  792. hidg->func.setup = hidg_setup;
  793. hidg->func.free_func = hidg_free;
  794. /* this could me made configurable at some point */
  795. hidg->qlen = 4;
  796. return &hidg->func;
  797. }
  798. DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
  799. MODULE_LICENSE("GPL");
  800. MODULE_AUTHOR("Fabien Chouteau");
  801. int ghid_setup(struct usb_gadget *g, int count)
  802. {
  803. int status;
  804. dev_t dev;
  805. hidg_class = class_create(THIS_MODULE, "hidg");
  806. if (IS_ERR(hidg_class)) {
  807. status = PTR_ERR(hidg_class);
  808. hidg_class = NULL;
  809. return status;
  810. }
  811. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  812. if (status) {
  813. class_destroy(hidg_class);
  814. hidg_class = NULL;
  815. return status;
  816. }
  817. major = MAJOR(dev);
  818. minors = count;
  819. return 0;
  820. }
  821. void ghid_cleanup(void)
  822. {
  823. if (major) {
  824. unregister_chrdev_region(MKDEV(major, 0), minors);
  825. major = minors = 0;
  826. }
  827. class_destroy(hidg_class);
  828. hidg_class = NULL;
  829. }