f_hid.c 24 KB

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