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. status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
  326. if (status < 0) {
  327. ERROR(hidg->func.config->cdev,
  328. "usb_ep_queue error on int endpoint %zd\n", status);
  329. goto release_write_pending_unlocked;
  330. } else {
  331. status = count;
  332. }
  333. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  334. return status;
  335. release_write_pending:
  336. spin_lock_irqsave(&hidg->write_spinlock, flags);
  337. release_write_pending_unlocked:
  338. hidg->write_pending = 0;
  339. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  340. wake_up(&hidg->write_queue);
  341. return status;
  342. }
  343. static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
  344. {
  345. struct f_hidg *hidg = file->private_data;
  346. __poll_t ret = 0;
  347. poll_wait(file, &hidg->read_queue, wait);
  348. poll_wait(file, &hidg->write_queue, wait);
  349. if (WRITE_COND)
  350. ret |= EPOLLOUT | EPOLLWRNORM;
  351. if (READ_COND)
  352. ret |= EPOLLIN | EPOLLRDNORM;
  353. return ret;
  354. }
  355. #undef WRITE_COND
  356. #undef READ_COND
  357. static int f_hidg_release(struct inode *inode, struct file *fd)
  358. {
  359. fd->private_data = NULL;
  360. return 0;
  361. }
  362. static int f_hidg_open(struct inode *inode, struct file *fd)
  363. {
  364. struct f_hidg *hidg =
  365. container_of(inode->i_cdev, struct f_hidg, cdev);
  366. fd->private_data = hidg;
  367. return 0;
  368. }
  369. /*-------------------------------------------------------------------------*/
  370. /* usb_function */
  371. static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
  372. unsigned length)
  373. {
  374. return alloc_ep_req(ep, length);
  375. }
  376. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  377. {
  378. struct f_hidg *hidg = (struct f_hidg *) req->context;
  379. struct usb_composite_dev *cdev = hidg->func.config->cdev;
  380. struct f_hidg_req_list *req_list;
  381. unsigned long flags;
  382. switch (req->status) {
  383. case 0:
  384. req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
  385. if (!req_list) {
  386. ERROR(cdev, "Unable to allocate mem for req_list\n");
  387. goto free_req;
  388. }
  389. req_list->req = req;
  390. spin_lock_irqsave(&hidg->read_spinlock, flags);
  391. list_add_tail(&req_list->list, &hidg->completed_out_req);
  392. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  393. wake_up(&hidg->read_queue);
  394. break;
  395. default:
  396. ERROR(cdev, "Set report failed %d\n", req->status);
  397. /* FALLTHROUGH */
  398. case -ECONNABORTED: /* hardware forced ep reset */
  399. case -ECONNRESET: /* request dequeued */
  400. case -ESHUTDOWN: /* disconnect from host */
  401. free_req:
  402. free_ep_req(ep, req);
  403. return;
  404. }
  405. }
  406. static int hidg_setup(struct usb_function *f,
  407. const struct usb_ctrlrequest *ctrl)
  408. {
  409. struct f_hidg *hidg = func_to_hidg(f);
  410. struct usb_composite_dev *cdev = f->config->cdev;
  411. struct usb_request *req = cdev->req;
  412. int status = 0;
  413. __u16 value, length;
  414. value = __le16_to_cpu(ctrl->wValue);
  415. length = __le16_to_cpu(ctrl->wLength);
  416. VDBG(cdev,
  417. "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
  418. __func__, ctrl->bRequestType, ctrl->bRequest, value);
  419. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  420. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  421. | HID_REQ_GET_REPORT):
  422. VDBG(cdev, "get_report\n");
  423. /* send an empty report */
  424. length = min_t(unsigned, length, hidg->report_length);
  425. memset(req->buf, 0x0, length);
  426. goto respond;
  427. break;
  428. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  429. | HID_REQ_GET_PROTOCOL):
  430. VDBG(cdev, "get_protocol\n");
  431. length = min_t(unsigned int, length, 1);
  432. ((u8 *) req->buf)[0] = hidg->protocol;
  433. goto respond;
  434. break;
  435. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  436. | HID_REQ_SET_REPORT):
  437. VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
  438. goto stall;
  439. break;
  440. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  441. | HID_REQ_SET_PROTOCOL):
  442. VDBG(cdev, "set_protocol\n");
  443. if (value > HID_REPORT_PROTOCOL)
  444. goto stall;
  445. length = 0;
  446. /*
  447. * We assume that programs implementing the Boot protocol
  448. * are also compatible with the Report Protocol
  449. */
  450. if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  451. hidg->protocol = value;
  452. goto respond;
  453. }
  454. goto stall;
  455. break;
  456. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  457. | USB_REQ_GET_DESCRIPTOR):
  458. switch (value >> 8) {
  459. case HID_DT_HID:
  460. {
  461. struct hid_descriptor hidg_desc_copy = hidg_desc;
  462. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  463. hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
  464. hidg_desc_copy.desc[0].wDescriptorLength =
  465. cpu_to_le16(hidg->report_desc_length);
  466. length = min_t(unsigned short, length,
  467. hidg_desc_copy.bLength);
  468. memcpy(req->buf, &hidg_desc_copy, length);
  469. goto respond;
  470. break;
  471. }
  472. case HID_DT_REPORT:
  473. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  474. length = min_t(unsigned short, length,
  475. hidg->report_desc_length);
  476. memcpy(req->buf, hidg->report_desc, length);
  477. goto respond;
  478. break;
  479. default:
  480. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  481. value >> 8);
  482. goto stall;
  483. break;
  484. }
  485. break;
  486. default:
  487. VDBG(cdev, "Unknown request 0x%x\n",
  488. ctrl->bRequest);
  489. goto stall;
  490. break;
  491. }
  492. stall:
  493. return -EOPNOTSUPP;
  494. respond:
  495. req->zero = 0;
  496. req->length = length;
  497. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  498. if (status < 0)
  499. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  500. return status;
  501. }
  502. static void hidg_disable(struct usb_function *f)
  503. {
  504. struct f_hidg *hidg = func_to_hidg(f);
  505. struct f_hidg_req_list *list, *next;
  506. unsigned long flags;
  507. usb_ep_disable(hidg->in_ep);
  508. usb_ep_disable(hidg->out_ep);
  509. spin_lock_irqsave(&hidg->read_spinlock, flags);
  510. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  511. free_ep_req(hidg->out_ep, list->req);
  512. list_del(&list->list);
  513. kfree(list);
  514. }
  515. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  516. spin_lock_irqsave(&hidg->write_spinlock, flags);
  517. if (!hidg->write_pending) {
  518. free_ep_req(hidg->in_ep, hidg->req);
  519. hidg->write_pending = 1;
  520. }
  521. hidg->req = NULL;
  522. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  523. }
  524. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  525. {
  526. struct usb_composite_dev *cdev = f->config->cdev;
  527. struct f_hidg *hidg = func_to_hidg(f);
  528. struct usb_request *req_in = NULL;
  529. unsigned long flags;
  530. int i, status = 0;
  531. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  532. if (hidg->in_ep != NULL) {
  533. /* restart endpoint */
  534. usb_ep_disable(hidg->in_ep);
  535. status = config_ep_by_speed(f->config->cdev->gadget, f,
  536. hidg->in_ep);
  537. if (status) {
  538. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  539. goto fail;
  540. }
  541. status = usb_ep_enable(hidg->in_ep);
  542. if (status < 0) {
  543. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  544. goto fail;
  545. }
  546. hidg->in_ep->driver_data = hidg;
  547. req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
  548. if (!req_in) {
  549. status = -ENOMEM;
  550. goto disable_ep_in;
  551. }
  552. }
  553. if (hidg->out_ep != NULL) {
  554. /* restart endpoint */
  555. usb_ep_disable(hidg->out_ep);
  556. status = config_ep_by_speed(f->config->cdev->gadget, f,
  557. hidg->out_ep);
  558. if (status) {
  559. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  560. goto free_req_in;
  561. }
  562. status = usb_ep_enable(hidg->out_ep);
  563. if (status < 0) {
  564. ERROR(cdev, "Enable OUT endpoint FAILED!\n");
  565. goto free_req_in;
  566. }
  567. hidg->out_ep->driver_data = hidg;
  568. /*
  569. * allocate a bunch of read buffers and queue them all at once.
  570. */
  571. for (i = 0; i < hidg->qlen && status == 0; i++) {
  572. struct usb_request *req =
  573. hidg_alloc_ep_req(hidg->out_ep,
  574. hidg->report_length);
  575. if (req) {
  576. req->complete = hidg_set_report_complete;
  577. req->context = hidg;
  578. status = usb_ep_queue(hidg->out_ep, req,
  579. GFP_ATOMIC);
  580. if (status) {
  581. ERROR(cdev, "%s queue req --> %d\n",
  582. hidg->out_ep->name, status);
  583. free_ep_req(hidg->out_ep, req);
  584. }
  585. } else {
  586. status = -ENOMEM;
  587. goto disable_out_ep;
  588. }
  589. }
  590. }
  591. if (hidg->in_ep != NULL) {
  592. spin_lock_irqsave(&hidg->write_spinlock, flags);
  593. hidg->req = req_in;
  594. hidg->write_pending = 0;
  595. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  596. wake_up(&hidg->write_queue);
  597. }
  598. return 0;
  599. disable_out_ep:
  600. usb_ep_disable(hidg->out_ep);
  601. free_req_in:
  602. if (req_in)
  603. free_ep_req(hidg->in_ep, req_in);
  604. disable_ep_in:
  605. if (hidg->in_ep)
  606. usb_ep_disable(hidg->in_ep);
  607. fail:
  608. return status;
  609. }
  610. static const struct file_operations f_hidg_fops = {
  611. .owner = THIS_MODULE,
  612. .open = f_hidg_open,
  613. .release = f_hidg_release,
  614. .write = f_hidg_write,
  615. .read = f_hidg_read,
  616. .poll = f_hidg_poll,
  617. .llseek = noop_llseek,
  618. };
  619. static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
  620. {
  621. struct usb_ep *ep;
  622. struct f_hidg *hidg = func_to_hidg(f);
  623. struct usb_string *us;
  624. struct device *device;
  625. int status;
  626. dev_t dev;
  627. /* maybe allocate device-global string IDs, and patch descriptors */
  628. us = usb_gstrings_attach(c->cdev, ct_func_strings,
  629. ARRAY_SIZE(ct_func_string_defs));
  630. if (IS_ERR(us))
  631. return PTR_ERR(us);
  632. hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
  633. /* allocate instance-specific interface IDs, and patch descriptors */
  634. status = usb_interface_id(c, f);
  635. if (status < 0)
  636. goto fail;
  637. hidg_interface_desc.bInterfaceNumber = status;
  638. /* allocate instance-specific endpoints */
  639. status = -ENODEV;
  640. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  641. if (!ep)
  642. goto fail;
  643. hidg->in_ep = ep;
  644. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  645. if (!ep)
  646. goto fail;
  647. hidg->out_ep = ep;
  648. /* set descriptor dynamic values */
  649. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  650. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  651. hidg->protocol = HID_REPORT_PROTOCOL;
  652. hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  653. hidg_ss_in_comp_desc.wBytesPerInterval =
  654. cpu_to_le16(hidg->report_length);
  655. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  656. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  657. hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  658. hidg_ss_out_comp_desc.wBytesPerInterval =
  659. cpu_to_le16(hidg->report_length);
  660. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  661. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  662. /*
  663. * We can use hidg_desc struct here but we should not relay
  664. * that its content won't change after returning from this function.
  665. */
  666. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  667. hidg_desc.desc[0].wDescriptorLength =
  668. cpu_to_le16(hidg->report_desc_length);
  669. hidg_hs_in_ep_desc.bEndpointAddress =
  670. hidg_fs_in_ep_desc.bEndpointAddress;
  671. hidg_hs_out_ep_desc.bEndpointAddress =
  672. hidg_fs_out_ep_desc.bEndpointAddress;
  673. hidg_ss_in_ep_desc.bEndpointAddress =
  674. hidg_fs_in_ep_desc.bEndpointAddress;
  675. hidg_ss_out_ep_desc.bEndpointAddress =
  676. hidg_fs_out_ep_desc.bEndpointAddress;
  677. status = usb_assign_descriptors(f, hidg_fs_descriptors,
  678. hidg_hs_descriptors, hidg_ss_descriptors, NULL);
  679. if (status)
  680. goto fail;
  681. spin_lock_init(&hidg->write_spinlock);
  682. hidg->write_pending = 1;
  683. hidg->req = NULL;
  684. spin_lock_init(&hidg->read_spinlock);
  685. init_waitqueue_head(&hidg->write_queue);
  686. init_waitqueue_head(&hidg->read_queue);
  687. INIT_LIST_HEAD(&hidg->completed_out_req);
  688. /* create char device */
  689. cdev_init(&hidg->cdev, &f_hidg_fops);
  690. dev = MKDEV(major, hidg->minor);
  691. status = cdev_add(&hidg->cdev, dev, 1);
  692. if (status)
  693. goto fail_free_descs;
  694. device = device_create(hidg_class, NULL, dev, NULL,
  695. "%s%d", "hidg", hidg->minor);
  696. if (IS_ERR(device)) {
  697. status = PTR_ERR(device);
  698. goto del;
  699. }
  700. return 0;
  701. del:
  702. cdev_del(&hidg->cdev);
  703. fail_free_descs:
  704. usb_free_all_descriptors(f);
  705. fail:
  706. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  707. if (hidg->req != NULL)
  708. free_ep_req(hidg->in_ep, hidg->req);
  709. return status;
  710. }
  711. static inline int hidg_get_minor(void)
  712. {
  713. int ret;
  714. ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
  715. if (ret >= HIDG_MINORS) {
  716. ida_simple_remove(&hidg_ida, ret);
  717. ret = -ENODEV;
  718. }
  719. return ret;
  720. }
  721. static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
  722. {
  723. return container_of(to_config_group(item), struct f_hid_opts,
  724. func_inst.group);
  725. }
  726. static void hid_attr_release(struct config_item *item)
  727. {
  728. struct f_hid_opts *opts = to_f_hid_opts(item);
  729. usb_put_function_instance(&opts->func_inst);
  730. }
  731. static struct configfs_item_operations hidg_item_ops = {
  732. .release = hid_attr_release,
  733. };
  734. #define F_HID_OPT(name, prec, limit) \
  735. static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
  736. { \
  737. struct f_hid_opts *opts = to_f_hid_opts(item); \
  738. int result; \
  739. \
  740. mutex_lock(&opts->lock); \
  741. result = sprintf(page, "%d\n", opts->name); \
  742. mutex_unlock(&opts->lock); \
  743. \
  744. return result; \
  745. } \
  746. \
  747. static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
  748. const char *page, size_t len) \
  749. { \
  750. struct f_hid_opts *opts = to_f_hid_opts(item); \
  751. int ret; \
  752. u##prec num; \
  753. \
  754. mutex_lock(&opts->lock); \
  755. if (opts->refcnt) { \
  756. ret = -EBUSY; \
  757. goto end; \
  758. } \
  759. \
  760. ret = kstrtou##prec(page, 0, &num); \
  761. if (ret) \
  762. goto end; \
  763. \
  764. if (num > limit) { \
  765. ret = -EINVAL; \
  766. goto end; \
  767. } \
  768. opts->name = num; \
  769. ret = len; \
  770. \
  771. end: \
  772. mutex_unlock(&opts->lock); \
  773. return ret; \
  774. } \
  775. \
  776. CONFIGFS_ATTR(f_hid_opts_, name)
  777. F_HID_OPT(subclass, 8, 255);
  778. F_HID_OPT(protocol, 8, 255);
  779. F_HID_OPT(report_length, 16, 65535);
  780. static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
  781. {
  782. struct f_hid_opts *opts = to_f_hid_opts(item);
  783. int result;
  784. mutex_lock(&opts->lock);
  785. result = opts->report_desc_length;
  786. memcpy(page, opts->report_desc, opts->report_desc_length);
  787. mutex_unlock(&opts->lock);
  788. return result;
  789. }
  790. static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
  791. const char *page, size_t len)
  792. {
  793. struct f_hid_opts *opts = to_f_hid_opts(item);
  794. int ret = -EBUSY;
  795. char *d;
  796. mutex_lock(&opts->lock);
  797. if (opts->refcnt)
  798. goto end;
  799. if (len > PAGE_SIZE) {
  800. ret = -ENOSPC;
  801. goto end;
  802. }
  803. d = kmemdup(page, len, GFP_KERNEL);
  804. if (!d) {
  805. ret = -ENOMEM;
  806. goto end;
  807. }
  808. kfree(opts->report_desc);
  809. opts->report_desc = d;
  810. opts->report_desc_length = len;
  811. opts->report_desc_alloc = true;
  812. ret = len;
  813. end:
  814. mutex_unlock(&opts->lock);
  815. return ret;
  816. }
  817. CONFIGFS_ATTR(f_hid_opts_, report_desc);
  818. static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
  819. {
  820. struct f_hid_opts *opts = to_f_hid_opts(item);
  821. return sprintf(page, "%d:%d\n", major, opts->minor);
  822. }
  823. CONFIGFS_ATTR_RO(f_hid_opts_, dev);
  824. static struct configfs_attribute *hid_attrs[] = {
  825. &f_hid_opts_attr_subclass,
  826. &f_hid_opts_attr_protocol,
  827. &f_hid_opts_attr_report_length,
  828. &f_hid_opts_attr_report_desc,
  829. &f_hid_opts_attr_dev,
  830. NULL,
  831. };
  832. static const struct config_item_type hid_func_type = {
  833. .ct_item_ops = &hidg_item_ops,
  834. .ct_attrs = hid_attrs,
  835. .ct_owner = THIS_MODULE,
  836. };
  837. static inline void hidg_put_minor(int minor)
  838. {
  839. ida_simple_remove(&hidg_ida, minor);
  840. }
  841. static void hidg_free_inst(struct usb_function_instance *f)
  842. {
  843. struct f_hid_opts *opts;
  844. opts = container_of(f, struct f_hid_opts, func_inst);
  845. mutex_lock(&hidg_ida_lock);
  846. hidg_put_minor(opts->minor);
  847. if (ida_is_empty(&hidg_ida))
  848. ghid_cleanup();
  849. mutex_unlock(&hidg_ida_lock);
  850. if (opts->report_desc_alloc)
  851. kfree(opts->report_desc);
  852. kfree(opts);
  853. }
  854. static struct usb_function_instance *hidg_alloc_inst(void)
  855. {
  856. struct f_hid_opts *opts;
  857. struct usb_function_instance *ret;
  858. int status = 0;
  859. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  860. if (!opts)
  861. return ERR_PTR(-ENOMEM);
  862. mutex_init(&opts->lock);
  863. opts->func_inst.free_func_inst = hidg_free_inst;
  864. ret = &opts->func_inst;
  865. mutex_lock(&hidg_ida_lock);
  866. if (ida_is_empty(&hidg_ida)) {
  867. status = ghid_setup(NULL, HIDG_MINORS);
  868. if (status) {
  869. ret = ERR_PTR(status);
  870. kfree(opts);
  871. goto unlock;
  872. }
  873. }
  874. opts->minor = hidg_get_minor();
  875. if (opts->minor < 0) {
  876. ret = ERR_PTR(opts->minor);
  877. kfree(opts);
  878. if (ida_is_empty(&hidg_ida))
  879. ghid_cleanup();
  880. goto unlock;
  881. }
  882. config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
  883. unlock:
  884. mutex_unlock(&hidg_ida_lock);
  885. return ret;
  886. }
  887. static void hidg_free(struct usb_function *f)
  888. {
  889. struct f_hidg *hidg;
  890. struct f_hid_opts *opts;
  891. hidg = func_to_hidg(f);
  892. opts = container_of(f->fi, struct f_hid_opts, func_inst);
  893. kfree(hidg->report_desc);
  894. kfree(hidg);
  895. mutex_lock(&opts->lock);
  896. --opts->refcnt;
  897. mutex_unlock(&opts->lock);
  898. }
  899. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  900. {
  901. struct f_hidg *hidg = func_to_hidg(f);
  902. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  903. cdev_del(&hidg->cdev);
  904. usb_free_all_descriptors(f);
  905. }
  906. static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
  907. {
  908. struct f_hidg *hidg;
  909. struct f_hid_opts *opts;
  910. /* allocate and initialize one new instance */
  911. hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
  912. if (!hidg)
  913. return ERR_PTR(-ENOMEM);
  914. opts = container_of(fi, struct f_hid_opts, func_inst);
  915. mutex_lock(&opts->lock);
  916. ++opts->refcnt;
  917. hidg->minor = opts->minor;
  918. hidg->bInterfaceSubClass = opts->subclass;
  919. hidg->bInterfaceProtocol = opts->protocol;
  920. hidg->report_length = opts->report_length;
  921. hidg->report_desc_length = opts->report_desc_length;
  922. if (opts->report_desc) {
  923. hidg->report_desc = kmemdup(opts->report_desc,
  924. opts->report_desc_length,
  925. GFP_KERNEL);
  926. if (!hidg->report_desc) {
  927. kfree(hidg);
  928. mutex_unlock(&opts->lock);
  929. return ERR_PTR(-ENOMEM);
  930. }
  931. }
  932. mutex_unlock(&opts->lock);
  933. hidg->func.name = "hid";
  934. hidg->func.bind = hidg_bind;
  935. hidg->func.unbind = hidg_unbind;
  936. hidg->func.set_alt = hidg_set_alt;
  937. hidg->func.disable = hidg_disable;
  938. hidg->func.setup = hidg_setup;
  939. hidg->func.free_func = hidg_free;
  940. /* this could me made configurable at some point */
  941. hidg->qlen = 4;
  942. return &hidg->func;
  943. }
  944. DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
  945. MODULE_LICENSE("GPL");
  946. MODULE_AUTHOR("Fabien Chouteau");
  947. int ghid_setup(struct usb_gadget *g, int count)
  948. {
  949. int status;
  950. dev_t dev;
  951. hidg_class = class_create(THIS_MODULE, "hidg");
  952. if (IS_ERR(hidg_class)) {
  953. status = PTR_ERR(hidg_class);
  954. hidg_class = NULL;
  955. return status;
  956. }
  957. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  958. if (status) {
  959. class_destroy(hidg_class);
  960. hidg_class = NULL;
  961. return status;
  962. }
  963. major = MAJOR(dev);
  964. minors = count;
  965. return 0;
  966. }
  967. void ghid_cleanup(void)
  968. {
  969. if (major) {
  970. unregister_chrdev_region(MKDEV(major, 0), minors);
  971. major = minors = 0;
  972. }
  973. class_destroy(hidg_class);
  974. hidg_class = NULL;
  975. }