f_hid.c 25 KB

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