f_hid.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. /*
  189. * Remove this from list to protect it from beign free()
  190. * while host disables our function
  191. */
  192. list_del(&list->list);
  193. req = list->req;
  194. count = min_t(unsigned int, count, req->actual - list->pos);
  195. spin_unlock_irqrestore(&hidg->spinlock, flags);
  196. /* copy to user outside spinlock */
  197. count -= copy_to_user(buffer, req->buf + list->pos, count);
  198. list->pos += count;
  199. /*
  200. * if this request is completely handled and transfered to
  201. * userspace, remove its entry from the list and requeue it
  202. * again. Otherwise, we will revisit it again upon the next
  203. * call, taking into account its current read position.
  204. */
  205. if (list->pos == req->actual) {
  206. kfree(list);
  207. req->length = hidg->report_length;
  208. ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
  209. if (ret < 0) {
  210. free_ep_req(hidg->out_ep, req);
  211. return ret;
  212. }
  213. } else {
  214. spin_lock_irqsave(&hidg->spinlock, flags);
  215. list_add(&list->list, &hidg->completed_out_req);
  216. spin_unlock_irqrestore(&hidg->spinlock, flags);
  217. wake_up(&hidg->read_queue);
  218. }
  219. return count;
  220. }
  221. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  222. {
  223. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  224. if (req->status != 0) {
  225. ERROR(hidg->func.config->cdev,
  226. "End Point Request ERROR: %d\n", req->status);
  227. }
  228. hidg->write_pending = 0;
  229. wake_up(&hidg->write_queue);
  230. }
  231. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  232. size_t count, loff_t *offp)
  233. {
  234. struct f_hidg *hidg = file->private_data;
  235. ssize_t status = -ENOMEM;
  236. if (!access_ok(VERIFY_READ, buffer, count))
  237. return -EFAULT;
  238. mutex_lock(&hidg->lock);
  239. #define WRITE_COND (!hidg->write_pending)
  240. /* write queue */
  241. while (!WRITE_COND) {
  242. mutex_unlock(&hidg->lock);
  243. if (file->f_flags & O_NONBLOCK)
  244. return -EAGAIN;
  245. if (wait_event_interruptible_exclusive(
  246. hidg->write_queue, WRITE_COND))
  247. return -ERESTARTSYS;
  248. mutex_lock(&hidg->lock);
  249. }
  250. count = min_t(unsigned, count, hidg->report_length);
  251. status = copy_from_user(hidg->req->buf, buffer, count);
  252. if (status != 0) {
  253. ERROR(hidg->func.config->cdev,
  254. "copy_from_user error\n");
  255. mutex_unlock(&hidg->lock);
  256. return -EINVAL;
  257. }
  258. hidg->req->status = 0;
  259. hidg->req->zero = 0;
  260. hidg->req->length = count;
  261. hidg->req->complete = f_hidg_req_complete;
  262. hidg->req->context = hidg;
  263. hidg->write_pending = 1;
  264. status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
  265. if (status < 0) {
  266. ERROR(hidg->func.config->cdev,
  267. "usb_ep_queue error on int endpoint %zd\n", status);
  268. hidg->write_pending = 0;
  269. wake_up(&hidg->write_queue);
  270. } else {
  271. status = count;
  272. }
  273. mutex_unlock(&hidg->lock);
  274. return status;
  275. }
  276. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  277. {
  278. struct f_hidg *hidg = file->private_data;
  279. unsigned int ret = 0;
  280. poll_wait(file, &hidg->read_queue, wait);
  281. poll_wait(file, &hidg->write_queue, wait);
  282. if (WRITE_COND)
  283. ret |= POLLOUT | POLLWRNORM;
  284. if (READ_COND)
  285. ret |= POLLIN | POLLRDNORM;
  286. return ret;
  287. }
  288. #undef WRITE_COND
  289. #undef READ_COND
  290. static int f_hidg_release(struct inode *inode, struct file *fd)
  291. {
  292. fd->private_data = NULL;
  293. return 0;
  294. }
  295. static int f_hidg_open(struct inode *inode, struct file *fd)
  296. {
  297. struct f_hidg *hidg =
  298. container_of(inode->i_cdev, struct f_hidg, cdev);
  299. fd->private_data = hidg;
  300. return 0;
  301. }
  302. /*-------------------------------------------------------------------------*/
  303. /* usb_function */
  304. static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
  305. unsigned length)
  306. {
  307. return alloc_ep_req(ep, length, length);
  308. }
  309. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  310. {
  311. struct f_hidg *hidg = (struct f_hidg *) req->context;
  312. struct f_hidg_req_list *req_list;
  313. unsigned long flags;
  314. req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
  315. if (!req_list)
  316. return;
  317. req_list->req = req;
  318. spin_lock_irqsave(&hidg->spinlock, flags);
  319. list_add_tail(&req_list->list, &hidg->completed_out_req);
  320. spin_unlock_irqrestore(&hidg->spinlock, flags);
  321. wake_up(&hidg->read_queue);
  322. }
  323. static int hidg_setup(struct usb_function *f,
  324. const struct usb_ctrlrequest *ctrl)
  325. {
  326. struct f_hidg *hidg = func_to_hidg(f);
  327. struct usb_composite_dev *cdev = f->config->cdev;
  328. struct usb_request *req = cdev->req;
  329. int status = 0;
  330. __u16 value, length;
  331. value = __le16_to_cpu(ctrl->wValue);
  332. length = __le16_to_cpu(ctrl->wLength);
  333. VDBG(cdev,
  334. "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
  335. __func__, ctrl->bRequestType, ctrl->bRequest, value);
  336. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  337. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  338. | HID_REQ_GET_REPORT):
  339. VDBG(cdev, "get_report\n");
  340. /* send an empty report */
  341. length = min_t(unsigned, length, hidg->report_length);
  342. memset(req->buf, 0x0, length);
  343. goto respond;
  344. break;
  345. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  346. | HID_REQ_GET_PROTOCOL):
  347. VDBG(cdev, "get_protocol\n");
  348. goto stall;
  349. break;
  350. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  351. | HID_REQ_SET_REPORT):
  352. VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
  353. goto stall;
  354. break;
  355. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  356. | HID_REQ_SET_PROTOCOL):
  357. VDBG(cdev, "set_protocol\n");
  358. goto stall;
  359. break;
  360. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  361. | USB_REQ_GET_DESCRIPTOR):
  362. switch (value >> 8) {
  363. case HID_DT_HID:
  364. {
  365. struct hid_descriptor hidg_desc_copy = hidg_desc;
  366. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  367. hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
  368. hidg_desc_copy.desc[0].wDescriptorLength =
  369. cpu_to_le16(hidg->report_desc_length);
  370. length = min_t(unsigned short, length,
  371. hidg_desc_copy.bLength);
  372. memcpy(req->buf, &hidg_desc_copy, length);
  373. goto respond;
  374. break;
  375. }
  376. case HID_DT_REPORT:
  377. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  378. length = min_t(unsigned short, length,
  379. hidg->report_desc_length);
  380. memcpy(req->buf, hidg->report_desc, length);
  381. goto respond;
  382. break;
  383. default:
  384. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  385. value >> 8);
  386. goto stall;
  387. break;
  388. }
  389. break;
  390. default:
  391. VDBG(cdev, "Unknown request 0x%x\n",
  392. ctrl->bRequest);
  393. goto stall;
  394. break;
  395. }
  396. stall:
  397. return -EOPNOTSUPP;
  398. respond:
  399. req->zero = 0;
  400. req->length = length;
  401. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  402. if (status < 0)
  403. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  404. return status;
  405. }
  406. static void hidg_disable(struct usb_function *f)
  407. {
  408. struct f_hidg *hidg = func_to_hidg(f);
  409. struct f_hidg_req_list *list, *next;
  410. unsigned long flags;
  411. usb_ep_disable(hidg->in_ep);
  412. usb_ep_disable(hidg->out_ep);
  413. spin_lock_irqsave(&hidg->spinlock, flags);
  414. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  415. free_ep_req(hidg->out_ep, list->req);
  416. list_del(&list->list);
  417. kfree(list);
  418. }
  419. spin_unlock_irqrestore(&hidg->spinlock, flags);
  420. }
  421. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  422. {
  423. struct usb_composite_dev *cdev = f->config->cdev;
  424. struct f_hidg *hidg = func_to_hidg(f);
  425. int i, status = 0;
  426. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  427. if (hidg->in_ep != NULL) {
  428. /* restart endpoint */
  429. usb_ep_disable(hidg->in_ep);
  430. status = config_ep_by_speed(f->config->cdev->gadget, f,
  431. hidg->in_ep);
  432. if (status) {
  433. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  434. goto fail;
  435. }
  436. status = usb_ep_enable(hidg->in_ep);
  437. if (status < 0) {
  438. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  439. goto fail;
  440. }
  441. hidg->in_ep->driver_data = hidg;
  442. }
  443. if (hidg->out_ep != NULL) {
  444. /* restart endpoint */
  445. usb_ep_disable(hidg->out_ep);
  446. status = config_ep_by_speed(f->config->cdev->gadget, f,
  447. hidg->out_ep);
  448. if (status) {
  449. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  450. goto fail;
  451. }
  452. status = usb_ep_enable(hidg->out_ep);
  453. if (status < 0) {
  454. ERROR(cdev, "Enable OUT endpoint FAILED!\n");
  455. goto fail;
  456. }
  457. hidg->out_ep->driver_data = hidg;
  458. /*
  459. * allocate a bunch of read buffers and queue them all at once.
  460. */
  461. for (i = 0; i < hidg->qlen && status == 0; i++) {
  462. struct usb_request *req =
  463. hidg_alloc_ep_req(hidg->out_ep,
  464. hidg->report_length);
  465. if (req) {
  466. req->complete = hidg_set_report_complete;
  467. req->context = hidg;
  468. status = usb_ep_queue(hidg->out_ep, req,
  469. GFP_ATOMIC);
  470. if (status)
  471. ERROR(cdev, "%s queue req --> %d\n",
  472. hidg->out_ep->name, status);
  473. } else {
  474. usb_ep_disable(hidg->out_ep);
  475. status = -ENOMEM;
  476. goto fail;
  477. }
  478. }
  479. }
  480. fail:
  481. return status;
  482. }
  483. static const struct file_operations f_hidg_fops = {
  484. .owner = THIS_MODULE,
  485. .open = f_hidg_open,
  486. .release = f_hidg_release,
  487. .write = f_hidg_write,
  488. .read = f_hidg_read,
  489. .poll = f_hidg_poll,
  490. .llseek = noop_llseek,
  491. };
  492. static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
  493. {
  494. struct usb_ep *ep;
  495. struct f_hidg *hidg = func_to_hidg(f);
  496. struct usb_string *us;
  497. struct device *device;
  498. int status;
  499. dev_t dev;
  500. /* maybe allocate device-global string IDs, and patch descriptors */
  501. us = usb_gstrings_attach(c->cdev, ct_func_strings,
  502. ARRAY_SIZE(ct_func_string_defs));
  503. if (IS_ERR(us))
  504. return PTR_ERR(us);
  505. hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
  506. /* allocate instance-specific interface IDs, and patch descriptors */
  507. status = usb_interface_id(c, f);
  508. if (status < 0)
  509. goto fail;
  510. hidg_interface_desc.bInterfaceNumber = status;
  511. /* allocate instance-specific endpoints */
  512. status = -ENODEV;
  513. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  514. if (!ep)
  515. goto fail;
  516. hidg->in_ep = ep;
  517. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  518. if (!ep)
  519. goto fail;
  520. hidg->out_ep = ep;
  521. /* preallocate request and buffer */
  522. status = -ENOMEM;
  523. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  524. if (!hidg->req)
  525. goto fail;
  526. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  527. if (!hidg->req->buf)
  528. goto fail;
  529. /* set descriptor dynamic values */
  530. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  531. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  532. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  533. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  534. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  535. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  536. /*
  537. * We can use hidg_desc struct here but we should not relay
  538. * that its content won't change after returning from this function.
  539. */
  540. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  541. hidg_desc.desc[0].wDescriptorLength =
  542. cpu_to_le16(hidg->report_desc_length);
  543. hidg_hs_in_ep_desc.bEndpointAddress =
  544. hidg_fs_in_ep_desc.bEndpointAddress;
  545. hidg_hs_out_ep_desc.bEndpointAddress =
  546. hidg_fs_out_ep_desc.bEndpointAddress;
  547. status = usb_assign_descriptors(f, hidg_fs_descriptors,
  548. hidg_hs_descriptors, NULL);
  549. if (status)
  550. goto fail;
  551. mutex_init(&hidg->lock);
  552. spin_lock_init(&hidg->spinlock);
  553. init_waitqueue_head(&hidg->write_queue);
  554. init_waitqueue_head(&hidg->read_queue);
  555. INIT_LIST_HEAD(&hidg->completed_out_req);
  556. /* create char device */
  557. cdev_init(&hidg->cdev, &f_hidg_fops);
  558. dev = MKDEV(major, hidg->minor);
  559. status = cdev_add(&hidg->cdev, dev, 1);
  560. if (status)
  561. goto fail_free_descs;
  562. device = device_create(hidg_class, NULL, dev, NULL,
  563. "%s%d", "hidg", hidg->minor);
  564. if (IS_ERR(device)) {
  565. status = PTR_ERR(device);
  566. goto del;
  567. }
  568. return 0;
  569. del:
  570. cdev_del(&hidg->cdev);
  571. fail_free_descs:
  572. usb_free_all_descriptors(f);
  573. fail:
  574. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  575. if (hidg->req != NULL) {
  576. kfree(hidg->req->buf);
  577. if (hidg->in_ep != NULL)
  578. usb_ep_free_request(hidg->in_ep, hidg->req);
  579. }
  580. return status;
  581. }
  582. static inline int hidg_get_minor(void)
  583. {
  584. int ret;
  585. ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
  586. if (ret >= HIDG_MINORS) {
  587. ida_simple_remove(&hidg_ida, ret);
  588. ret = -ENODEV;
  589. }
  590. return ret;
  591. }
  592. static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
  593. {
  594. return container_of(to_config_group(item), struct f_hid_opts,
  595. func_inst.group);
  596. }
  597. static void hid_attr_release(struct config_item *item)
  598. {
  599. struct f_hid_opts *opts = to_f_hid_opts(item);
  600. usb_put_function_instance(&opts->func_inst);
  601. }
  602. static struct configfs_item_operations hidg_item_ops = {
  603. .release = hid_attr_release,
  604. };
  605. #define F_HID_OPT(name, prec, limit) \
  606. static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
  607. { \
  608. struct f_hid_opts *opts = to_f_hid_opts(item); \
  609. int result; \
  610. \
  611. mutex_lock(&opts->lock); \
  612. result = sprintf(page, "%d\n", opts->name); \
  613. mutex_unlock(&opts->lock); \
  614. \
  615. return result; \
  616. } \
  617. \
  618. static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
  619. const char *page, size_t len) \
  620. { \
  621. struct f_hid_opts *opts = to_f_hid_opts(item); \
  622. int ret; \
  623. u##prec num; \
  624. \
  625. mutex_lock(&opts->lock); \
  626. if (opts->refcnt) { \
  627. ret = -EBUSY; \
  628. goto end; \
  629. } \
  630. \
  631. ret = kstrtou##prec(page, 0, &num); \
  632. if (ret) \
  633. goto end; \
  634. \
  635. if (num > limit) { \
  636. ret = -EINVAL; \
  637. goto end; \
  638. } \
  639. opts->name = num; \
  640. ret = len; \
  641. \
  642. end: \
  643. mutex_unlock(&opts->lock); \
  644. return ret; \
  645. } \
  646. \
  647. CONFIGFS_ATTR(f_hid_opts_, name)
  648. F_HID_OPT(subclass, 8, 255);
  649. F_HID_OPT(protocol, 8, 255);
  650. F_HID_OPT(report_length, 16, 65535);
  651. static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
  652. {
  653. struct f_hid_opts *opts = to_f_hid_opts(item);
  654. int result;
  655. mutex_lock(&opts->lock);
  656. result = opts->report_desc_length;
  657. memcpy(page, opts->report_desc, opts->report_desc_length);
  658. mutex_unlock(&opts->lock);
  659. return result;
  660. }
  661. static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
  662. const char *page, size_t len)
  663. {
  664. struct f_hid_opts *opts = to_f_hid_opts(item);
  665. int ret = -EBUSY;
  666. char *d;
  667. mutex_lock(&opts->lock);
  668. if (opts->refcnt)
  669. goto end;
  670. if (len > PAGE_SIZE) {
  671. ret = -ENOSPC;
  672. goto end;
  673. }
  674. d = kmemdup(page, len, GFP_KERNEL);
  675. if (!d) {
  676. ret = -ENOMEM;
  677. goto end;
  678. }
  679. kfree(opts->report_desc);
  680. opts->report_desc = d;
  681. opts->report_desc_length = len;
  682. opts->report_desc_alloc = true;
  683. ret = len;
  684. end:
  685. mutex_unlock(&opts->lock);
  686. return ret;
  687. }
  688. CONFIGFS_ATTR(f_hid_opts_, report_desc);
  689. static struct configfs_attribute *hid_attrs[] = {
  690. &f_hid_opts_attr_subclass,
  691. &f_hid_opts_attr_protocol,
  692. &f_hid_opts_attr_report_length,
  693. &f_hid_opts_attr_report_desc,
  694. NULL,
  695. };
  696. static struct config_item_type hid_func_type = {
  697. .ct_item_ops = &hidg_item_ops,
  698. .ct_attrs = hid_attrs,
  699. .ct_owner = THIS_MODULE,
  700. };
  701. static inline void hidg_put_minor(int minor)
  702. {
  703. ida_simple_remove(&hidg_ida, minor);
  704. }
  705. static void hidg_free_inst(struct usb_function_instance *f)
  706. {
  707. struct f_hid_opts *opts;
  708. opts = container_of(f, struct f_hid_opts, func_inst);
  709. mutex_lock(&hidg_ida_lock);
  710. hidg_put_minor(opts->minor);
  711. if (idr_is_empty(&hidg_ida.idr))
  712. ghid_cleanup();
  713. mutex_unlock(&hidg_ida_lock);
  714. if (opts->report_desc_alloc)
  715. kfree(opts->report_desc);
  716. kfree(opts);
  717. }
  718. static struct usb_function_instance *hidg_alloc_inst(void)
  719. {
  720. struct f_hid_opts *opts;
  721. struct usb_function_instance *ret;
  722. int status = 0;
  723. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  724. if (!opts)
  725. return ERR_PTR(-ENOMEM);
  726. mutex_init(&opts->lock);
  727. opts->func_inst.free_func_inst = hidg_free_inst;
  728. ret = &opts->func_inst;
  729. mutex_lock(&hidg_ida_lock);
  730. if (idr_is_empty(&hidg_ida.idr)) {
  731. status = ghid_setup(NULL, HIDG_MINORS);
  732. if (status) {
  733. ret = ERR_PTR(status);
  734. kfree(opts);
  735. goto unlock;
  736. }
  737. }
  738. opts->minor = hidg_get_minor();
  739. if (opts->minor < 0) {
  740. ret = ERR_PTR(opts->minor);
  741. kfree(opts);
  742. if (idr_is_empty(&hidg_ida.idr))
  743. ghid_cleanup();
  744. goto unlock;
  745. }
  746. config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
  747. unlock:
  748. mutex_unlock(&hidg_ida_lock);
  749. return ret;
  750. }
  751. static void hidg_free(struct usb_function *f)
  752. {
  753. struct f_hidg *hidg;
  754. struct f_hid_opts *opts;
  755. hidg = func_to_hidg(f);
  756. opts = container_of(f->fi, struct f_hid_opts, func_inst);
  757. kfree(hidg->report_desc);
  758. kfree(hidg);
  759. mutex_lock(&opts->lock);
  760. --opts->refcnt;
  761. mutex_unlock(&opts->lock);
  762. }
  763. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  764. {
  765. struct f_hidg *hidg = func_to_hidg(f);
  766. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  767. cdev_del(&hidg->cdev);
  768. /* disable/free request and end point */
  769. usb_ep_disable(hidg->in_ep);
  770. kfree(hidg->req->buf);
  771. usb_ep_free_request(hidg->in_ep, hidg->req);
  772. usb_free_all_descriptors(f);
  773. }
  774. static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
  775. {
  776. struct f_hidg *hidg;
  777. struct f_hid_opts *opts;
  778. /* allocate and initialize one new instance */
  779. hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
  780. if (!hidg)
  781. return ERR_PTR(-ENOMEM);
  782. opts = container_of(fi, struct f_hid_opts, func_inst);
  783. mutex_lock(&opts->lock);
  784. ++opts->refcnt;
  785. hidg->minor = opts->minor;
  786. hidg->bInterfaceSubClass = opts->subclass;
  787. hidg->bInterfaceProtocol = opts->protocol;
  788. hidg->report_length = opts->report_length;
  789. hidg->report_desc_length = opts->report_desc_length;
  790. if (opts->report_desc) {
  791. hidg->report_desc = kmemdup(opts->report_desc,
  792. opts->report_desc_length,
  793. GFP_KERNEL);
  794. if (!hidg->report_desc) {
  795. kfree(hidg);
  796. mutex_unlock(&opts->lock);
  797. return ERR_PTR(-ENOMEM);
  798. }
  799. }
  800. mutex_unlock(&opts->lock);
  801. hidg->func.name = "hid";
  802. hidg->func.bind = hidg_bind;
  803. hidg->func.unbind = hidg_unbind;
  804. hidg->func.set_alt = hidg_set_alt;
  805. hidg->func.disable = hidg_disable;
  806. hidg->func.setup = hidg_setup;
  807. hidg->func.free_func = hidg_free;
  808. /* this could me made configurable at some point */
  809. hidg->qlen = 4;
  810. return &hidg->func;
  811. }
  812. DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
  813. MODULE_LICENSE("GPL");
  814. MODULE_AUTHOR("Fabien Chouteau");
  815. int ghid_setup(struct usb_gadget *g, int count)
  816. {
  817. int status;
  818. dev_t dev;
  819. hidg_class = class_create(THIS_MODULE, "hidg");
  820. if (IS_ERR(hidg_class)) {
  821. status = PTR_ERR(hidg_class);
  822. hidg_class = NULL;
  823. return status;
  824. }
  825. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  826. if (status) {
  827. class_destroy(hidg_class);
  828. hidg_class = NULL;
  829. return status;
  830. }
  831. major = MAJOR(dev);
  832. minors = count;
  833. return 0;
  834. }
  835. void ghid_cleanup(void)
  836. {
  837. if (major) {
  838. unregister_chrdev_region(MKDEV(major, 0), minors);
  839. major = minors = 0;
  840. }
  841. class_destroy(hidg_class);
  842. hidg_class = NULL;
  843. }