f_hid.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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 read_spinlock;
  46. wait_queue_head_t read_queue;
  47. unsigned int qlen;
  48. /* send report */
  49. spinlock_t write_spinlock;
  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. /* Super-Speed Support */
  86. static struct usb_endpoint_descriptor hidg_ss_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_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
  98. .bLength = sizeof(hidg_ss_in_comp_desc),
  99. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  100. /* .bMaxBurst = 0, */
  101. /* .bmAttributes = 0, */
  102. /* .wBytesPerInterval = DYNAMIC */
  103. };
  104. static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
  105. .bLength = USB_DT_ENDPOINT_SIZE,
  106. .bDescriptorType = USB_DT_ENDPOINT,
  107. .bEndpointAddress = USB_DIR_OUT,
  108. .bmAttributes = USB_ENDPOINT_XFER_INT,
  109. /*.wMaxPacketSize = DYNAMIC */
  110. .bInterval = 4, /* FIXME: Add this field in the
  111. * HID gadget configuration?
  112. * (struct hidg_func_descriptor)
  113. */
  114. };
  115. static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
  116. .bLength = sizeof(hidg_ss_out_comp_desc),
  117. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  118. /* .bMaxBurst = 0, */
  119. /* .bmAttributes = 0, */
  120. /* .wBytesPerInterval = DYNAMIC */
  121. };
  122. static struct usb_descriptor_header *hidg_ss_descriptors[] = {
  123. (struct usb_descriptor_header *)&hidg_interface_desc,
  124. (struct usb_descriptor_header *)&hidg_desc,
  125. (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
  126. (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
  127. (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
  128. (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
  129. NULL,
  130. };
  131. /* High-Speed Support */
  132. static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
  133. .bLength = USB_DT_ENDPOINT_SIZE,
  134. .bDescriptorType = USB_DT_ENDPOINT,
  135. .bEndpointAddress = USB_DIR_IN,
  136. .bmAttributes = USB_ENDPOINT_XFER_INT,
  137. /*.wMaxPacketSize = DYNAMIC */
  138. .bInterval = 4, /* FIXME: Add this field in the
  139. * HID gadget configuration?
  140. * (struct hidg_func_descriptor)
  141. */
  142. };
  143. static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
  144. .bLength = USB_DT_ENDPOINT_SIZE,
  145. .bDescriptorType = USB_DT_ENDPOINT,
  146. .bEndpointAddress = USB_DIR_OUT,
  147. .bmAttributes = USB_ENDPOINT_XFER_INT,
  148. /*.wMaxPacketSize = DYNAMIC */
  149. .bInterval = 4, /* FIXME: Add this field in the
  150. * HID gadget configuration?
  151. * (struct hidg_func_descriptor)
  152. */
  153. };
  154. static struct usb_descriptor_header *hidg_hs_descriptors[] = {
  155. (struct usb_descriptor_header *)&hidg_interface_desc,
  156. (struct usb_descriptor_header *)&hidg_desc,
  157. (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
  158. (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
  159. NULL,
  160. };
  161. /* Full-Speed Support */
  162. static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
  163. .bLength = USB_DT_ENDPOINT_SIZE,
  164. .bDescriptorType = USB_DT_ENDPOINT,
  165. .bEndpointAddress = USB_DIR_IN,
  166. .bmAttributes = USB_ENDPOINT_XFER_INT,
  167. /*.wMaxPacketSize = DYNAMIC */
  168. .bInterval = 10, /* FIXME: Add this field in the
  169. * HID gadget configuration?
  170. * (struct hidg_func_descriptor)
  171. */
  172. };
  173. static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
  174. .bLength = USB_DT_ENDPOINT_SIZE,
  175. .bDescriptorType = USB_DT_ENDPOINT,
  176. .bEndpointAddress = USB_DIR_OUT,
  177. .bmAttributes = USB_ENDPOINT_XFER_INT,
  178. /*.wMaxPacketSize = DYNAMIC */
  179. .bInterval = 10, /* FIXME: Add this field in the
  180. * HID gadget configuration?
  181. * (struct hidg_func_descriptor)
  182. */
  183. };
  184. static struct usb_descriptor_header *hidg_fs_descriptors[] = {
  185. (struct usb_descriptor_header *)&hidg_interface_desc,
  186. (struct usb_descriptor_header *)&hidg_desc,
  187. (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
  188. (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
  189. NULL,
  190. };
  191. /*-------------------------------------------------------------------------*/
  192. /* Strings */
  193. #define CT_FUNC_HID_IDX 0
  194. static struct usb_string ct_func_string_defs[] = {
  195. [CT_FUNC_HID_IDX].s = "HID Interface",
  196. {}, /* end of list */
  197. };
  198. static struct usb_gadget_strings ct_func_string_table = {
  199. .language = 0x0409, /* en-US */
  200. .strings = ct_func_string_defs,
  201. };
  202. static struct usb_gadget_strings *ct_func_strings[] = {
  203. &ct_func_string_table,
  204. NULL,
  205. };
  206. /*-------------------------------------------------------------------------*/
  207. /* Char Device */
  208. static ssize_t f_hidg_read(struct file *file, char __user *buffer,
  209. size_t count, loff_t *ptr)
  210. {
  211. struct f_hidg *hidg = file->private_data;
  212. struct f_hidg_req_list *list;
  213. struct usb_request *req;
  214. unsigned long flags;
  215. int ret;
  216. if (!count)
  217. return 0;
  218. if (!access_ok(VERIFY_WRITE, buffer, count))
  219. return -EFAULT;
  220. spin_lock_irqsave(&hidg->read_spinlock, flags);
  221. #define READ_COND (!list_empty(&hidg->completed_out_req))
  222. /* wait for at least one buffer to complete */
  223. while (!READ_COND) {
  224. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  225. if (file->f_flags & O_NONBLOCK)
  226. return -EAGAIN;
  227. if (wait_event_interruptible(hidg->read_queue, READ_COND))
  228. return -ERESTARTSYS;
  229. spin_lock_irqsave(&hidg->read_spinlock, flags);
  230. }
  231. /* pick the first one */
  232. list = list_first_entry(&hidg->completed_out_req,
  233. struct f_hidg_req_list, list);
  234. /*
  235. * Remove this from list to protect it from beign free()
  236. * while host disables our function
  237. */
  238. list_del(&list->list);
  239. req = list->req;
  240. count = min_t(unsigned int, count, req->actual - list->pos);
  241. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  242. /* copy to user outside spinlock */
  243. count -= copy_to_user(buffer, req->buf + list->pos, count);
  244. list->pos += count;
  245. /*
  246. * if this request is completely handled and transfered to
  247. * userspace, remove its entry from the list and requeue it
  248. * again. Otherwise, we will revisit it again upon the next
  249. * call, taking into account its current read position.
  250. */
  251. if (list->pos == req->actual) {
  252. kfree(list);
  253. req->length = hidg->report_length;
  254. ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
  255. if (ret < 0) {
  256. free_ep_req(hidg->out_ep, req);
  257. return ret;
  258. }
  259. } else {
  260. spin_lock_irqsave(&hidg->read_spinlock, flags);
  261. list_add(&list->list, &hidg->completed_out_req);
  262. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  263. wake_up(&hidg->read_queue);
  264. }
  265. return count;
  266. }
  267. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  268. {
  269. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  270. unsigned long flags;
  271. if (req->status != 0) {
  272. ERROR(hidg->func.config->cdev,
  273. "End Point Request ERROR: %d\n", req->status);
  274. }
  275. spin_lock_irqsave(&hidg->write_spinlock, flags);
  276. hidg->write_pending = 0;
  277. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  278. wake_up(&hidg->write_queue);
  279. }
  280. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  281. size_t count, loff_t *offp)
  282. {
  283. struct f_hidg *hidg = file->private_data;
  284. struct usb_request *req;
  285. unsigned long flags;
  286. ssize_t status = -ENOMEM;
  287. if (!access_ok(VERIFY_READ, buffer, count))
  288. return -EFAULT;
  289. spin_lock_irqsave(&hidg->write_spinlock, flags);
  290. #define WRITE_COND (!hidg->write_pending)
  291. try_again:
  292. /* write queue */
  293. while (!WRITE_COND) {
  294. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  295. if (file->f_flags & O_NONBLOCK)
  296. return -EAGAIN;
  297. if (wait_event_interruptible_exclusive(
  298. hidg->write_queue, WRITE_COND))
  299. return -ERESTARTSYS;
  300. spin_lock_irqsave(&hidg->write_spinlock, flags);
  301. }
  302. hidg->write_pending = 1;
  303. req = hidg->req;
  304. count = min_t(unsigned, count, hidg->report_length);
  305. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  306. status = copy_from_user(req->buf, buffer, count);
  307. if (status != 0) {
  308. ERROR(hidg->func.config->cdev,
  309. "copy_from_user error\n");
  310. status = -EINVAL;
  311. goto release_write_pending;
  312. }
  313. spin_lock_irqsave(&hidg->write_spinlock, flags);
  314. /* when our function has been disabled by host */
  315. if (!hidg->req) {
  316. free_ep_req(hidg->in_ep, req);
  317. /*
  318. * TODO
  319. * Should we fail with error here?
  320. */
  321. goto try_again;
  322. }
  323. req->status = 0;
  324. req->zero = 0;
  325. req->length = count;
  326. req->complete = f_hidg_req_complete;
  327. req->context = hidg;
  328. status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
  329. if (status < 0) {
  330. ERROR(hidg->func.config->cdev,
  331. "usb_ep_queue error on int endpoint %zd\n", status);
  332. goto release_write_pending_unlocked;
  333. } else {
  334. status = count;
  335. }
  336. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  337. return status;
  338. release_write_pending:
  339. spin_lock_irqsave(&hidg->write_spinlock, flags);
  340. release_write_pending_unlocked:
  341. hidg->write_pending = 0;
  342. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  343. wake_up(&hidg->write_queue);
  344. return status;
  345. }
  346. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  347. {
  348. struct f_hidg *hidg = file->private_data;
  349. unsigned int ret = 0;
  350. poll_wait(file, &hidg->read_queue, wait);
  351. poll_wait(file, &hidg->write_queue, wait);
  352. if (WRITE_COND)
  353. ret |= POLLOUT | POLLWRNORM;
  354. if (READ_COND)
  355. ret |= POLLIN | POLLRDNORM;
  356. return ret;
  357. }
  358. #undef WRITE_COND
  359. #undef READ_COND
  360. static int f_hidg_release(struct inode *inode, struct file *fd)
  361. {
  362. fd->private_data = NULL;
  363. return 0;
  364. }
  365. static int f_hidg_open(struct inode *inode, struct file *fd)
  366. {
  367. struct f_hidg *hidg =
  368. container_of(inode->i_cdev, struct f_hidg, cdev);
  369. fd->private_data = hidg;
  370. return 0;
  371. }
  372. /*-------------------------------------------------------------------------*/
  373. /* usb_function */
  374. static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
  375. unsigned length)
  376. {
  377. return alloc_ep_req(ep, length);
  378. }
  379. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  380. {
  381. struct f_hidg *hidg = (struct f_hidg *) req->context;
  382. struct usb_composite_dev *cdev = hidg->func.config->cdev;
  383. struct f_hidg_req_list *req_list;
  384. unsigned long flags;
  385. switch (req->status) {
  386. case 0:
  387. req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
  388. if (!req_list) {
  389. ERROR(cdev, "Unable to allocate mem for req_list\n");
  390. goto free_req;
  391. }
  392. req_list->req = req;
  393. spin_lock_irqsave(&hidg->read_spinlock, flags);
  394. list_add_tail(&req_list->list, &hidg->completed_out_req);
  395. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  396. wake_up(&hidg->read_queue);
  397. break;
  398. default:
  399. ERROR(cdev, "Set report failed %d\n", req->status);
  400. /* FALLTHROUGH */
  401. case -ECONNABORTED: /* hardware forced ep reset */
  402. case -ECONNRESET: /* request dequeued */
  403. case -ESHUTDOWN: /* disconnect from host */
  404. free_req:
  405. free_ep_req(ep, req);
  406. return;
  407. }
  408. }
  409. static int hidg_setup(struct usb_function *f,
  410. const struct usb_ctrlrequest *ctrl)
  411. {
  412. struct f_hidg *hidg = func_to_hidg(f);
  413. struct usb_composite_dev *cdev = f->config->cdev;
  414. struct usb_request *req = cdev->req;
  415. int status = 0;
  416. __u16 value, length;
  417. value = __le16_to_cpu(ctrl->wValue);
  418. length = __le16_to_cpu(ctrl->wLength);
  419. VDBG(cdev,
  420. "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
  421. __func__, ctrl->bRequestType, ctrl->bRequest, value);
  422. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  423. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  424. | HID_REQ_GET_REPORT):
  425. VDBG(cdev, "get_report\n");
  426. /* send an empty report */
  427. length = min_t(unsigned, length, hidg->report_length);
  428. memset(req->buf, 0x0, length);
  429. goto respond;
  430. break;
  431. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  432. | HID_REQ_GET_PROTOCOL):
  433. VDBG(cdev, "get_protocol\n");
  434. goto stall;
  435. break;
  436. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  437. | HID_REQ_SET_REPORT):
  438. VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
  439. goto stall;
  440. break;
  441. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  442. | HID_REQ_SET_PROTOCOL):
  443. VDBG(cdev, "set_protocol\n");
  444. goto stall;
  445. break;
  446. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  447. | USB_REQ_GET_DESCRIPTOR):
  448. switch (value >> 8) {
  449. case HID_DT_HID:
  450. {
  451. struct hid_descriptor hidg_desc_copy = hidg_desc;
  452. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  453. hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
  454. hidg_desc_copy.desc[0].wDescriptorLength =
  455. cpu_to_le16(hidg->report_desc_length);
  456. length = min_t(unsigned short, length,
  457. hidg_desc_copy.bLength);
  458. memcpy(req->buf, &hidg_desc_copy, length);
  459. goto respond;
  460. break;
  461. }
  462. case HID_DT_REPORT:
  463. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  464. length = min_t(unsigned short, length,
  465. hidg->report_desc_length);
  466. memcpy(req->buf, hidg->report_desc, length);
  467. goto respond;
  468. break;
  469. default:
  470. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  471. value >> 8);
  472. goto stall;
  473. break;
  474. }
  475. break;
  476. default:
  477. VDBG(cdev, "Unknown request 0x%x\n",
  478. ctrl->bRequest);
  479. goto stall;
  480. break;
  481. }
  482. stall:
  483. return -EOPNOTSUPP;
  484. respond:
  485. req->zero = 0;
  486. req->length = length;
  487. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  488. if (status < 0)
  489. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  490. return status;
  491. }
  492. static void hidg_disable(struct usb_function *f)
  493. {
  494. struct f_hidg *hidg = func_to_hidg(f);
  495. struct f_hidg_req_list *list, *next;
  496. unsigned long flags;
  497. usb_ep_disable(hidg->in_ep);
  498. usb_ep_disable(hidg->out_ep);
  499. spin_lock_irqsave(&hidg->read_spinlock, flags);
  500. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  501. free_ep_req(hidg->out_ep, list->req);
  502. list_del(&list->list);
  503. kfree(list);
  504. }
  505. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  506. spin_lock_irqsave(&hidg->write_spinlock, flags);
  507. if (!hidg->write_pending) {
  508. free_ep_req(hidg->in_ep, hidg->req);
  509. hidg->write_pending = 1;
  510. }
  511. hidg->req = NULL;
  512. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  513. }
  514. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  515. {
  516. struct usb_composite_dev *cdev = f->config->cdev;
  517. struct f_hidg *hidg = func_to_hidg(f);
  518. struct usb_request *req_in = NULL;
  519. unsigned long flags;
  520. int i, status = 0;
  521. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  522. if (hidg->in_ep != NULL) {
  523. /* restart endpoint */
  524. usb_ep_disable(hidg->in_ep);
  525. status = config_ep_by_speed(f->config->cdev->gadget, f,
  526. hidg->in_ep);
  527. if (status) {
  528. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  529. goto fail;
  530. }
  531. status = usb_ep_enable(hidg->in_ep);
  532. if (status < 0) {
  533. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  534. goto fail;
  535. }
  536. hidg->in_ep->driver_data = hidg;
  537. req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
  538. if (!req_in) {
  539. status = -ENOMEM;
  540. goto disable_ep_in;
  541. }
  542. }
  543. if (hidg->out_ep != NULL) {
  544. /* restart endpoint */
  545. usb_ep_disable(hidg->out_ep);
  546. status = config_ep_by_speed(f->config->cdev->gadget, f,
  547. hidg->out_ep);
  548. if (status) {
  549. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  550. goto free_req_in;
  551. }
  552. status = usb_ep_enable(hidg->out_ep);
  553. if (status < 0) {
  554. ERROR(cdev, "Enable OUT endpoint FAILED!\n");
  555. goto free_req_in;
  556. }
  557. hidg->out_ep->driver_data = hidg;
  558. /*
  559. * allocate a bunch of read buffers and queue them all at once.
  560. */
  561. for (i = 0; i < hidg->qlen && status == 0; i++) {
  562. struct usb_request *req =
  563. hidg_alloc_ep_req(hidg->out_ep,
  564. hidg->report_length);
  565. if (req) {
  566. req->complete = hidg_set_report_complete;
  567. req->context = hidg;
  568. status = usb_ep_queue(hidg->out_ep, req,
  569. GFP_ATOMIC);
  570. if (status) {
  571. ERROR(cdev, "%s queue req --> %d\n",
  572. hidg->out_ep->name, status);
  573. free_ep_req(hidg->out_ep, req);
  574. }
  575. } else {
  576. status = -ENOMEM;
  577. goto disable_out_ep;
  578. }
  579. }
  580. }
  581. if (hidg->in_ep != NULL) {
  582. spin_lock_irqsave(&hidg->write_spinlock, flags);
  583. hidg->req = req_in;
  584. hidg->write_pending = 0;
  585. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  586. wake_up(&hidg->write_queue);
  587. }
  588. return 0;
  589. disable_out_ep:
  590. usb_ep_disable(hidg->out_ep);
  591. free_req_in:
  592. if (req_in)
  593. free_ep_req(hidg->in_ep, req_in);
  594. disable_ep_in:
  595. if (hidg->in_ep)
  596. usb_ep_disable(hidg->in_ep);
  597. fail:
  598. return status;
  599. }
  600. static const struct file_operations f_hidg_fops = {
  601. .owner = THIS_MODULE,
  602. .open = f_hidg_open,
  603. .release = f_hidg_release,
  604. .write = f_hidg_write,
  605. .read = f_hidg_read,
  606. .poll = f_hidg_poll,
  607. .llseek = noop_llseek,
  608. };
  609. static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
  610. {
  611. struct usb_ep *ep;
  612. struct f_hidg *hidg = func_to_hidg(f);
  613. struct usb_string *us;
  614. struct device *device;
  615. int status;
  616. dev_t dev;
  617. /* maybe allocate device-global string IDs, and patch descriptors */
  618. us = usb_gstrings_attach(c->cdev, ct_func_strings,
  619. ARRAY_SIZE(ct_func_string_defs));
  620. if (IS_ERR(us))
  621. return PTR_ERR(us);
  622. hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
  623. /* allocate instance-specific interface IDs, and patch descriptors */
  624. status = usb_interface_id(c, f);
  625. if (status < 0)
  626. goto fail;
  627. hidg_interface_desc.bInterfaceNumber = status;
  628. /* allocate instance-specific endpoints */
  629. status = -ENODEV;
  630. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  631. if (!ep)
  632. goto fail;
  633. hidg->in_ep = ep;
  634. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  635. if (!ep)
  636. goto fail;
  637. hidg->out_ep = ep;
  638. /* set descriptor dynamic values */
  639. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  640. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  641. hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  642. hidg_ss_in_comp_desc.wBytesPerInterval =
  643. cpu_to_le16(hidg->report_length);
  644. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  645. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  646. hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  647. hidg_ss_out_comp_desc.wBytesPerInterval =
  648. cpu_to_le16(hidg->report_length);
  649. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  650. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  651. /*
  652. * We can use hidg_desc struct here but we should not relay
  653. * that its content won't change after returning from this function.
  654. */
  655. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  656. hidg_desc.desc[0].wDescriptorLength =
  657. cpu_to_le16(hidg->report_desc_length);
  658. hidg_hs_in_ep_desc.bEndpointAddress =
  659. hidg_fs_in_ep_desc.bEndpointAddress;
  660. hidg_hs_out_ep_desc.bEndpointAddress =
  661. hidg_fs_out_ep_desc.bEndpointAddress;
  662. hidg_ss_in_ep_desc.bEndpointAddress =
  663. hidg_fs_in_ep_desc.bEndpointAddress;
  664. hidg_ss_out_ep_desc.bEndpointAddress =
  665. hidg_fs_out_ep_desc.bEndpointAddress;
  666. status = usb_assign_descriptors(f, hidg_fs_descriptors,
  667. hidg_hs_descriptors, hidg_ss_descriptors, NULL);
  668. if (status)
  669. goto fail;
  670. spin_lock_init(&hidg->write_spinlock);
  671. hidg->write_pending = 1;
  672. hidg->req = NULL;
  673. spin_lock_init(&hidg->read_spinlock);
  674. init_waitqueue_head(&hidg->write_queue);
  675. init_waitqueue_head(&hidg->read_queue);
  676. INIT_LIST_HEAD(&hidg->completed_out_req);
  677. /* create char device */
  678. cdev_init(&hidg->cdev, &f_hidg_fops);
  679. dev = MKDEV(major, hidg->minor);
  680. status = cdev_add(&hidg->cdev, dev, 1);
  681. if (status)
  682. goto fail_free_descs;
  683. device = device_create(hidg_class, NULL, dev, NULL,
  684. "%s%d", "hidg", hidg->minor);
  685. if (IS_ERR(device)) {
  686. status = PTR_ERR(device);
  687. goto del;
  688. }
  689. return 0;
  690. del:
  691. cdev_del(&hidg->cdev);
  692. fail_free_descs:
  693. usb_free_all_descriptors(f);
  694. fail:
  695. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  696. if (hidg->req != NULL)
  697. free_ep_req(hidg->in_ep, hidg->req);
  698. return status;
  699. }
  700. static inline int hidg_get_minor(void)
  701. {
  702. int ret;
  703. ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
  704. if (ret >= HIDG_MINORS) {
  705. ida_simple_remove(&hidg_ida, ret);
  706. ret = -ENODEV;
  707. }
  708. return ret;
  709. }
  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);
  714. }
  715. static void hid_attr_release(struct config_item *item)
  716. {
  717. struct f_hid_opts *opts = to_f_hid_opts(item);
  718. usb_put_function_instance(&opts->func_inst);
  719. }
  720. static struct configfs_item_operations hidg_item_ops = {
  721. .release = hid_attr_release,
  722. };
  723. #define F_HID_OPT(name, prec, limit) \
  724. static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
  725. { \
  726. struct f_hid_opts *opts = to_f_hid_opts(item); \
  727. int result; \
  728. \
  729. mutex_lock(&opts->lock); \
  730. result = sprintf(page, "%d\n", opts->name); \
  731. mutex_unlock(&opts->lock); \
  732. \
  733. return result; \
  734. } \
  735. \
  736. static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
  737. const char *page, size_t len) \
  738. { \
  739. struct f_hid_opts *opts = to_f_hid_opts(item); \
  740. int ret; \
  741. u##prec num; \
  742. \
  743. mutex_lock(&opts->lock); \
  744. if (opts->refcnt) { \
  745. ret = -EBUSY; \
  746. goto end; \
  747. } \
  748. \
  749. ret = kstrtou##prec(page, 0, &num); \
  750. if (ret) \
  751. goto end; \
  752. \
  753. if (num > limit) { \
  754. ret = -EINVAL; \
  755. goto end; \
  756. } \
  757. opts->name = num; \
  758. ret = len; \
  759. \
  760. end: \
  761. mutex_unlock(&opts->lock); \
  762. return ret; \
  763. } \
  764. \
  765. CONFIGFS_ATTR(f_hid_opts_, name)
  766. F_HID_OPT(subclass, 8, 255);
  767. F_HID_OPT(protocol, 8, 255);
  768. F_HID_OPT(report_length, 16, 65535);
  769. static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
  770. {
  771. struct f_hid_opts *opts = to_f_hid_opts(item);
  772. int result;
  773. mutex_lock(&opts->lock);
  774. result = opts->report_desc_length;
  775. memcpy(page, opts->report_desc, opts->report_desc_length);
  776. mutex_unlock(&opts->lock);
  777. return result;
  778. }
  779. static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
  780. const char *page, size_t len)
  781. {
  782. struct f_hid_opts *opts = to_f_hid_opts(item);
  783. int ret = -EBUSY;
  784. char *d;
  785. mutex_lock(&opts->lock);
  786. if (opts->refcnt)
  787. goto end;
  788. if (len > PAGE_SIZE) {
  789. ret = -ENOSPC;
  790. goto end;
  791. }
  792. d = kmemdup(page, len, GFP_KERNEL);
  793. if (!d) {
  794. ret = -ENOMEM;
  795. goto end;
  796. }
  797. kfree(opts->report_desc);
  798. opts->report_desc = d;
  799. opts->report_desc_length = len;
  800. opts->report_desc_alloc = true;
  801. ret = len;
  802. end:
  803. mutex_unlock(&opts->lock);
  804. return ret;
  805. }
  806. CONFIGFS_ATTR(f_hid_opts_, report_desc);
  807. static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
  808. {
  809. struct f_hid_opts *opts = to_f_hid_opts(item);
  810. return sprintf(page, "%d:%d\n", major, opts->minor);
  811. }
  812. CONFIGFS_ATTR_RO(f_hid_opts_, dev);
  813. static struct configfs_attribute *hid_attrs[] = {
  814. &f_hid_opts_attr_subclass,
  815. &f_hid_opts_attr_protocol,
  816. &f_hid_opts_attr_report_length,
  817. &f_hid_opts_attr_report_desc,
  818. &f_hid_opts_attr_dev,
  819. NULL,
  820. };
  821. static struct config_item_type hid_func_type = {
  822. .ct_item_ops = &hidg_item_ops,
  823. .ct_attrs = hid_attrs,
  824. .ct_owner = THIS_MODULE,
  825. };
  826. static inline void hidg_put_minor(int minor)
  827. {
  828. ida_simple_remove(&hidg_ida, minor);
  829. }
  830. static void hidg_free_inst(struct usb_function_instance *f)
  831. {
  832. struct f_hid_opts *opts;
  833. opts = container_of(f, struct f_hid_opts, func_inst);
  834. mutex_lock(&hidg_ida_lock);
  835. hidg_put_minor(opts->minor);
  836. if (ida_is_empty(&hidg_ida))
  837. ghid_cleanup();
  838. mutex_unlock(&hidg_ida_lock);
  839. if (opts->report_desc_alloc)
  840. kfree(opts->report_desc);
  841. kfree(opts);
  842. }
  843. static struct usb_function_instance *hidg_alloc_inst(void)
  844. {
  845. struct f_hid_opts *opts;
  846. struct usb_function_instance *ret;
  847. int status = 0;
  848. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  849. if (!opts)
  850. return ERR_PTR(-ENOMEM);
  851. mutex_init(&opts->lock);
  852. opts->func_inst.free_func_inst = hidg_free_inst;
  853. ret = &opts->func_inst;
  854. mutex_lock(&hidg_ida_lock);
  855. if (ida_is_empty(&hidg_ida)) {
  856. status = ghid_setup(NULL, HIDG_MINORS);
  857. if (status) {
  858. ret = ERR_PTR(status);
  859. kfree(opts);
  860. goto unlock;
  861. }
  862. }
  863. opts->minor = hidg_get_minor();
  864. if (opts->minor < 0) {
  865. ret = ERR_PTR(opts->minor);
  866. kfree(opts);
  867. if (ida_is_empty(&hidg_ida))
  868. ghid_cleanup();
  869. goto unlock;
  870. }
  871. config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
  872. unlock:
  873. mutex_unlock(&hidg_ida_lock);
  874. return ret;
  875. }
  876. static void hidg_free(struct usb_function *f)
  877. {
  878. struct f_hidg *hidg;
  879. struct f_hid_opts *opts;
  880. hidg = func_to_hidg(f);
  881. opts = container_of(f->fi, struct f_hid_opts, func_inst);
  882. kfree(hidg->report_desc);
  883. kfree(hidg);
  884. mutex_lock(&opts->lock);
  885. --opts->refcnt;
  886. mutex_unlock(&opts->lock);
  887. }
  888. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  889. {
  890. struct f_hidg *hidg = func_to_hidg(f);
  891. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  892. cdev_del(&hidg->cdev);
  893. usb_free_all_descriptors(f);
  894. }
  895. static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
  896. {
  897. struct f_hidg *hidg;
  898. struct f_hid_opts *opts;
  899. /* allocate and initialize one new instance */
  900. hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
  901. if (!hidg)
  902. return ERR_PTR(-ENOMEM);
  903. opts = container_of(fi, struct f_hid_opts, func_inst);
  904. mutex_lock(&opts->lock);
  905. ++opts->refcnt;
  906. hidg->minor = opts->minor;
  907. hidg->bInterfaceSubClass = opts->subclass;
  908. hidg->bInterfaceProtocol = opts->protocol;
  909. hidg->report_length = opts->report_length;
  910. hidg->report_desc_length = opts->report_desc_length;
  911. if (opts->report_desc) {
  912. hidg->report_desc = kmemdup(opts->report_desc,
  913. opts->report_desc_length,
  914. GFP_KERNEL);
  915. if (!hidg->report_desc) {
  916. kfree(hidg);
  917. mutex_unlock(&opts->lock);
  918. return ERR_PTR(-ENOMEM);
  919. }
  920. }
  921. mutex_unlock(&opts->lock);
  922. hidg->func.name = "hid";
  923. hidg->func.bind = hidg_bind;
  924. hidg->func.unbind = hidg_unbind;
  925. hidg->func.set_alt = hidg_set_alt;
  926. hidg->func.disable = hidg_disable;
  927. hidg->func.setup = hidg_setup;
  928. hidg->func.free_func = hidg_free;
  929. /* this could me made configurable at some point */
  930. hidg->qlen = 4;
  931. return &hidg->func;
  932. }
  933. DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
  934. MODULE_LICENSE("GPL");
  935. MODULE_AUTHOR("Fabien Chouteau");
  936. int ghid_setup(struct usb_gadget *g, int count)
  937. {
  938. int status;
  939. dev_t dev;
  940. hidg_class = class_create(THIS_MODULE, "hidg");
  941. if (IS_ERR(hidg_class)) {
  942. status = PTR_ERR(hidg_class);
  943. hidg_class = NULL;
  944. return status;
  945. }
  946. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  947. if (status) {
  948. class_destroy(hidg_class);
  949. hidg_class = NULL;
  950. return status;
  951. }
  952. major = MAJOR(dev);
  953. minors = count;
  954. return 0;
  955. }
  956. void ghid_cleanup(void)
  957. {
  958. if (major) {
  959. unregister_chrdev_region(MKDEV(major, 0), minors);
  960. major = minors = 0;
  961. }
  962. class_destroy(hidg_class);
  963. hidg_class = NULL;
  964. }