f_hid.c 24 KB

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