f_hid.c 29 KB

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