f_hid.c 26 KB

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