f_hid.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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/utsname.h>
  13. #include <linux/module.h>
  14. #include <linux/hid.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/usb/g_hid.h>
  21. static int major, minors;
  22. static struct class *hidg_class;
  23. /*-------------------------------------------------------------------------*/
  24. /* HID gadget struct */
  25. struct f_hidg {
  26. /* configuration */
  27. unsigned char bInterfaceSubClass;
  28. unsigned char bInterfaceProtocol;
  29. unsigned short report_desc_length;
  30. char *report_desc;
  31. unsigned short report_length;
  32. /* recv report */
  33. char *set_report_buff;
  34. unsigned short set_report_length;
  35. spinlock_t spinlock;
  36. wait_queue_head_t read_queue;
  37. /* send report */
  38. struct mutex lock;
  39. bool write_pending;
  40. wait_queue_head_t write_queue;
  41. struct usb_request *req;
  42. int minor;
  43. struct cdev cdev;
  44. struct usb_function func;
  45. struct usb_ep *in_ep;
  46. };
  47. static inline struct f_hidg *func_to_hidg(struct usb_function *f)
  48. {
  49. return container_of(f, struct f_hidg, func);
  50. }
  51. /*-------------------------------------------------------------------------*/
  52. /* Static descriptors */
  53. static struct usb_interface_descriptor hidg_interface_desc = {
  54. .bLength = sizeof hidg_interface_desc,
  55. .bDescriptorType = USB_DT_INTERFACE,
  56. /* .bInterfaceNumber = DYNAMIC */
  57. .bAlternateSetting = 0,
  58. .bNumEndpoints = 1,
  59. .bInterfaceClass = USB_CLASS_HID,
  60. /* .bInterfaceSubClass = DYNAMIC */
  61. /* .bInterfaceProtocol = DYNAMIC */
  62. /* .iInterface = DYNAMIC */
  63. };
  64. static struct hid_descriptor hidg_desc = {
  65. .bLength = sizeof hidg_desc,
  66. .bDescriptorType = HID_DT_HID,
  67. .bcdHID = 0x0101,
  68. .bCountryCode = 0x00,
  69. .bNumDescriptors = 0x1,
  70. /*.desc[0].bDescriptorType = DYNAMIC */
  71. /*.desc[0].wDescriptorLenght = DYNAMIC */
  72. };
  73. /* High-Speed Support */
  74. static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
  75. .bLength = USB_DT_ENDPOINT_SIZE,
  76. .bDescriptorType = USB_DT_ENDPOINT,
  77. .bEndpointAddress = USB_DIR_IN,
  78. .bmAttributes = USB_ENDPOINT_XFER_INT,
  79. /*.wMaxPacketSize = DYNAMIC */
  80. .bInterval = 4, /* FIXME: Add this field in the
  81. * HID gadget configuration?
  82. * (struct hidg_func_descriptor)
  83. */
  84. };
  85. static struct usb_descriptor_header *hidg_hs_descriptors[] = {
  86. (struct usb_descriptor_header *)&hidg_interface_desc,
  87. (struct usb_descriptor_header *)&hidg_desc,
  88. (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
  89. NULL,
  90. };
  91. /* Full-Speed Support */
  92. static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
  93. .bLength = USB_DT_ENDPOINT_SIZE,
  94. .bDescriptorType = USB_DT_ENDPOINT,
  95. .bEndpointAddress = USB_DIR_IN,
  96. .bmAttributes = USB_ENDPOINT_XFER_INT,
  97. /*.wMaxPacketSize = DYNAMIC */
  98. .bInterval = 10, /* FIXME: Add this field in the
  99. * HID gadget configuration?
  100. * (struct hidg_func_descriptor)
  101. */
  102. };
  103. static struct usb_descriptor_header *hidg_fs_descriptors[] = {
  104. (struct usb_descriptor_header *)&hidg_interface_desc,
  105. (struct usb_descriptor_header *)&hidg_desc,
  106. (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
  107. NULL,
  108. };
  109. /*-------------------------------------------------------------------------*/
  110. /* Char Device */
  111. static ssize_t f_hidg_read(struct file *file, char __user *buffer,
  112. size_t count, loff_t *ptr)
  113. {
  114. struct f_hidg *hidg = file->private_data;
  115. char *tmp_buff = NULL;
  116. unsigned long flags;
  117. if (!count)
  118. return 0;
  119. if (!access_ok(VERIFY_WRITE, buffer, count))
  120. return -EFAULT;
  121. spin_lock_irqsave(&hidg->spinlock, flags);
  122. #define READ_COND (hidg->set_report_buff != NULL)
  123. while (!READ_COND) {
  124. spin_unlock_irqrestore(&hidg->spinlock, flags);
  125. if (file->f_flags & O_NONBLOCK)
  126. return -EAGAIN;
  127. if (wait_event_interruptible(hidg->read_queue, READ_COND))
  128. return -ERESTARTSYS;
  129. spin_lock_irqsave(&hidg->spinlock, flags);
  130. }
  131. count = min_t(unsigned, count, hidg->set_report_length);
  132. tmp_buff = hidg->set_report_buff;
  133. hidg->set_report_buff = NULL;
  134. spin_unlock_irqrestore(&hidg->spinlock, flags);
  135. if (tmp_buff != NULL) {
  136. /* copy to user outside spinlock */
  137. count -= copy_to_user(buffer, tmp_buff, count);
  138. kfree(tmp_buff);
  139. } else
  140. count = -ENOMEM;
  141. return count;
  142. }
  143. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  144. {
  145. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  146. if (req->status != 0) {
  147. ERROR(hidg->func.config->cdev,
  148. "End Point Request ERROR: %d\n", req->status);
  149. }
  150. hidg->write_pending = 0;
  151. wake_up(&hidg->write_queue);
  152. }
  153. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  154. size_t count, loff_t *offp)
  155. {
  156. struct f_hidg *hidg = file->private_data;
  157. ssize_t status = -ENOMEM;
  158. if (!access_ok(VERIFY_READ, buffer, count))
  159. return -EFAULT;
  160. mutex_lock(&hidg->lock);
  161. #define WRITE_COND (!hidg->write_pending)
  162. /* write queue */
  163. while (!WRITE_COND) {
  164. mutex_unlock(&hidg->lock);
  165. if (file->f_flags & O_NONBLOCK)
  166. return -EAGAIN;
  167. if (wait_event_interruptible_exclusive(
  168. hidg->write_queue, WRITE_COND))
  169. return -ERESTARTSYS;
  170. mutex_lock(&hidg->lock);
  171. }
  172. count = min_t(unsigned, count, hidg->report_length);
  173. status = copy_from_user(hidg->req->buf, buffer, count);
  174. if (status != 0) {
  175. ERROR(hidg->func.config->cdev,
  176. "copy_from_user error\n");
  177. mutex_unlock(&hidg->lock);
  178. return -EINVAL;
  179. }
  180. hidg->req->status = 0;
  181. hidg->req->zero = 0;
  182. hidg->req->length = count;
  183. hidg->req->complete = f_hidg_req_complete;
  184. hidg->req->context = hidg;
  185. hidg->write_pending = 1;
  186. status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
  187. if (status < 0) {
  188. ERROR(hidg->func.config->cdev,
  189. "usb_ep_queue error on int endpoint %zd\n", status);
  190. hidg->write_pending = 0;
  191. wake_up(&hidg->write_queue);
  192. } else {
  193. status = count;
  194. }
  195. mutex_unlock(&hidg->lock);
  196. return status;
  197. }
  198. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  199. {
  200. struct f_hidg *hidg = file->private_data;
  201. unsigned int ret = 0;
  202. poll_wait(file, &hidg->read_queue, wait);
  203. poll_wait(file, &hidg->write_queue, wait);
  204. if (WRITE_COND)
  205. ret |= POLLOUT | POLLWRNORM;
  206. if (READ_COND)
  207. ret |= POLLIN | POLLRDNORM;
  208. return ret;
  209. }
  210. #undef WRITE_COND
  211. #undef READ_COND
  212. static int f_hidg_release(struct inode *inode, struct file *fd)
  213. {
  214. fd->private_data = NULL;
  215. return 0;
  216. }
  217. static int f_hidg_open(struct inode *inode, struct file *fd)
  218. {
  219. struct f_hidg *hidg =
  220. container_of(inode->i_cdev, struct f_hidg, cdev);
  221. fd->private_data = hidg;
  222. return 0;
  223. }
  224. /*-------------------------------------------------------------------------*/
  225. /* usb_function */
  226. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  227. {
  228. struct f_hidg *hidg = (struct f_hidg *)req->context;
  229. if (req->status != 0 || req->buf == NULL || req->actual == 0) {
  230. ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__);
  231. return;
  232. }
  233. spin_lock(&hidg->spinlock);
  234. hidg->set_report_buff = krealloc(hidg->set_report_buff,
  235. req->actual, GFP_ATOMIC);
  236. if (hidg->set_report_buff == NULL) {
  237. spin_unlock(&hidg->spinlock);
  238. return;
  239. }
  240. hidg->set_report_length = req->actual;
  241. memcpy(hidg->set_report_buff, req->buf, req->actual);
  242. spin_unlock(&hidg->spinlock);
  243. wake_up(&hidg->read_queue);
  244. }
  245. static int hidg_setup(struct usb_function *f,
  246. const struct usb_ctrlrequest *ctrl)
  247. {
  248. struct f_hidg *hidg = func_to_hidg(f);
  249. struct usb_composite_dev *cdev = f->config->cdev;
  250. struct usb_request *req = cdev->req;
  251. int status = 0;
  252. __u16 value, length;
  253. value = __le16_to_cpu(ctrl->wValue);
  254. length = __le16_to_cpu(ctrl->wLength);
  255. VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
  256. "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
  257. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  258. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  259. | HID_REQ_GET_REPORT):
  260. VDBG(cdev, "get_report\n");
  261. /* send an empty report */
  262. length = min_t(unsigned, length, hidg->report_length);
  263. memset(req->buf, 0x0, length);
  264. goto respond;
  265. break;
  266. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  267. | HID_REQ_GET_PROTOCOL):
  268. VDBG(cdev, "get_protocol\n");
  269. goto stall;
  270. break;
  271. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  272. | HID_REQ_SET_REPORT):
  273. VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
  274. req->context = hidg;
  275. req->complete = hidg_set_report_complete;
  276. goto respond;
  277. break;
  278. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  279. | HID_REQ_SET_PROTOCOL):
  280. VDBG(cdev, "set_protocol\n");
  281. goto stall;
  282. break;
  283. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  284. | USB_REQ_GET_DESCRIPTOR):
  285. switch (value >> 8) {
  286. case HID_DT_HID:
  287. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  288. length = min_t(unsigned short, length,
  289. hidg_desc.bLength);
  290. memcpy(req->buf, &hidg_desc, length);
  291. goto respond;
  292. break;
  293. case HID_DT_REPORT:
  294. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  295. length = min_t(unsigned short, length,
  296. hidg->report_desc_length);
  297. memcpy(req->buf, hidg->report_desc, length);
  298. goto respond;
  299. break;
  300. default:
  301. VDBG(cdev, "Unknown decriptor request 0x%x\n",
  302. value >> 8);
  303. goto stall;
  304. break;
  305. }
  306. break;
  307. default:
  308. VDBG(cdev, "Unknown request 0x%x\n",
  309. ctrl->bRequest);
  310. goto stall;
  311. break;
  312. }
  313. stall:
  314. return -EOPNOTSUPP;
  315. respond:
  316. req->zero = 0;
  317. req->length = length;
  318. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  319. if (status < 0)
  320. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  321. return status;
  322. }
  323. static void hidg_disable(struct usb_function *f)
  324. {
  325. struct f_hidg *hidg = func_to_hidg(f);
  326. usb_ep_disable(hidg->in_ep);
  327. hidg->in_ep->driver_data = NULL;
  328. }
  329. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  330. {
  331. struct usb_composite_dev *cdev = f->config->cdev;
  332. struct f_hidg *hidg = func_to_hidg(f);
  333. int status = 0;
  334. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  335. if (hidg->in_ep != NULL) {
  336. /* restart endpoint */
  337. if (hidg->in_ep->driver_data != NULL)
  338. usb_ep_disable(hidg->in_ep);
  339. status = config_ep_by_speed(f->config->cdev->gadget, f,
  340. hidg->in_ep);
  341. if (status) {
  342. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  343. goto fail;
  344. }
  345. status = usb_ep_enable(hidg->in_ep);
  346. if (status < 0) {
  347. ERROR(cdev, "Enable endpoint FAILED!\n");
  348. goto fail;
  349. }
  350. hidg->in_ep->driver_data = hidg;
  351. }
  352. fail:
  353. return status;
  354. }
  355. const struct file_operations f_hidg_fops = {
  356. .owner = THIS_MODULE,
  357. .open = f_hidg_open,
  358. .release = f_hidg_release,
  359. .write = f_hidg_write,
  360. .read = f_hidg_read,
  361. .poll = f_hidg_poll,
  362. .llseek = noop_llseek,
  363. };
  364. static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
  365. {
  366. struct usb_ep *ep;
  367. struct f_hidg *hidg = func_to_hidg(f);
  368. int status;
  369. dev_t dev;
  370. /* allocate instance-specific interface IDs, and patch descriptors */
  371. status = usb_interface_id(c, f);
  372. if (status < 0)
  373. goto fail;
  374. hidg_interface_desc.bInterfaceNumber = status;
  375. /* allocate instance-specific endpoints */
  376. status = -ENODEV;
  377. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  378. if (!ep)
  379. goto fail;
  380. ep->driver_data = c->cdev; /* claim */
  381. hidg->in_ep = ep;
  382. /* preallocate request and buffer */
  383. status = -ENOMEM;
  384. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  385. if (!hidg->req)
  386. goto fail;
  387. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  388. if (!hidg->req->buf)
  389. goto fail;
  390. /* set descriptor dynamic values */
  391. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  392. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  393. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  394. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  395. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  396. hidg_desc.desc[0].wDescriptorLength =
  397. cpu_to_le16(hidg->report_desc_length);
  398. hidg->set_report_buff = NULL;
  399. /* copy descriptors */
  400. f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
  401. if (!f->descriptors)
  402. goto fail;
  403. if (gadget_is_dualspeed(c->cdev->gadget)) {
  404. hidg_hs_in_ep_desc.bEndpointAddress =
  405. hidg_fs_in_ep_desc.bEndpointAddress;
  406. f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
  407. if (!f->hs_descriptors)
  408. goto fail;
  409. }
  410. mutex_init(&hidg->lock);
  411. spin_lock_init(&hidg->spinlock);
  412. init_waitqueue_head(&hidg->write_queue);
  413. init_waitqueue_head(&hidg->read_queue);
  414. /* create char device */
  415. cdev_init(&hidg->cdev, &f_hidg_fops);
  416. dev = MKDEV(major, hidg->minor);
  417. status = cdev_add(&hidg->cdev, dev, 1);
  418. if (status)
  419. goto fail;
  420. device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
  421. return 0;
  422. fail:
  423. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  424. if (hidg->req != NULL) {
  425. kfree(hidg->req->buf);
  426. if (hidg->in_ep != NULL)
  427. usb_ep_free_request(hidg->in_ep, hidg->req);
  428. }
  429. usb_free_descriptors(f->hs_descriptors);
  430. usb_free_descriptors(f->descriptors);
  431. return status;
  432. }
  433. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  434. {
  435. struct f_hidg *hidg = func_to_hidg(f);
  436. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  437. cdev_del(&hidg->cdev);
  438. /* disable/free request and end point */
  439. usb_ep_disable(hidg->in_ep);
  440. usb_ep_dequeue(hidg->in_ep, hidg->req);
  441. kfree(hidg->req->buf);
  442. usb_ep_free_request(hidg->in_ep, hidg->req);
  443. /* free descriptors copies */
  444. usb_free_descriptors(f->hs_descriptors);
  445. usb_free_descriptors(f->descriptors);
  446. kfree(hidg->report_desc);
  447. kfree(hidg->set_report_buff);
  448. kfree(hidg);
  449. }
  450. /*-------------------------------------------------------------------------*/
  451. /* Strings */
  452. #define CT_FUNC_HID_IDX 0
  453. static struct usb_string ct_func_string_defs[] = {
  454. [CT_FUNC_HID_IDX].s = "HID Interface",
  455. {}, /* end of list */
  456. };
  457. static struct usb_gadget_strings ct_func_string_table = {
  458. .language = 0x0409, /* en-US */
  459. .strings = ct_func_string_defs,
  460. };
  461. static struct usb_gadget_strings *ct_func_strings[] = {
  462. &ct_func_string_table,
  463. NULL,
  464. };
  465. /*-------------------------------------------------------------------------*/
  466. /* usb_configuration */
  467. int __init hidg_bind_config(struct usb_configuration *c,
  468. struct hidg_func_descriptor *fdesc, int index)
  469. {
  470. struct f_hidg *hidg;
  471. int status;
  472. if (index >= minors)
  473. return -ENOENT;
  474. /* maybe allocate device-global string IDs, and patch descriptors */
  475. if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
  476. status = usb_string_id(c->cdev);
  477. if (status < 0)
  478. return status;
  479. ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
  480. hidg_interface_desc.iInterface = status;
  481. }
  482. /* allocate and initialize one new instance */
  483. hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
  484. if (!hidg)
  485. return -ENOMEM;
  486. hidg->minor = index;
  487. hidg->bInterfaceSubClass = fdesc->subclass;
  488. hidg->bInterfaceProtocol = fdesc->protocol;
  489. hidg->report_length = fdesc->report_length;
  490. hidg->report_desc_length = fdesc->report_desc_length;
  491. hidg->report_desc = kmemdup(fdesc->report_desc,
  492. fdesc->report_desc_length,
  493. GFP_KERNEL);
  494. if (!hidg->report_desc) {
  495. kfree(hidg);
  496. return -ENOMEM;
  497. }
  498. hidg->func.name = "hid";
  499. hidg->func.strings = ct_func_strings;
  500. hidg->func.bind = hidg_bind;
  501. hidg->func.unbind = hidg_unbind;
  502. hidg->func.set_alt = hidg_set_alt;
  503. hidg->func.disable = hidg_disable;
  504. hidg->func.setup = hidg_setup;
  505. status = usb_add_function(c, &hidg->func);
  506. if (status)
  507. kfree(hidg);
  508. return status;
  509. }
  510. int __init ghid_setup(struct usb_gadget *g, int count)
  511. {
  512. int status;
  513. dev_t dev;
  514. hidg_class = class_create(THIS_MODULE, "hidg");
  515. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  516. if (!status) {
  517. major = MAJOR(dev);
  518. minors = count;
  519. }
  520. return status;
  521. }
  522. void ghid_cleanup(void)
  523. {
  524. if (major) {
  525. unregister_chrdev_region(MKDEV(major, 0), minors);
  526. major = minors = 0;
  527. }
  528. class_destroy(hidg_class);
  529. hidg_class = NULL;
  530. }