f_hid.c 29 KB

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