f_hid.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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/cdev.h>
  15. #include <linux/mutex.h>
  16. #include <linux/poll.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/wait.h>
  19. #include <linux/usb/g_hid.h>
  20. static int major, minors;
  21. static struct class *hidg_class;
  22. /*-------------------------------------------------------------------------*/
  23. /* HID gadget struct */
  24. struct f_hidg_req_list {
  25. struct usb_request *req;
  26. unsigned int pos;
  27. struct list_head list;
  28. };
  29. struct f_hidg {
  30. /* configuration */
  31. unsigned char bInterfaceSubClass;
  32. unsigned char bInterfaceProtocol;
  33. unsigned short report_desc_length;
  34. char *report_desc;
  35. unsigned short report_length;
  36. /* recv report */
  37. struct list_head completed_out_req;
  38. spinlock_t spinlock;
  39. wait_queue_head_t read_queue;
  40. unsigned int qlen;
  41. /* send report */
  42. struct mutex lock;
  43. bool write_pending;
  44. wait_queue_head_t write_queue;
  45. struct usb_request *req;
  46. int minor;
  47. struct cdev cdev;
  48. struct usb_function func;
  49. struct usb_ep *in_ep;
  50. struct usb_ep *out_ep;
  51. };
  52. static inline struct f_hidg *func_to_hidg(struct usb_function *f)
  53. {
  54. return container_of(f, struct f_hidg, func);
  55. }
  56. /*-------------------------------------------------------------------------*/
  57. /* Static descriptors */
  58. static struct usb_interface_descriptor hidg_interface_desc = {
  59. .bLength = sizeof hidg_interface_desc,
  60. .bDescriptorType = USB_DT_INTERFACE,
  61. /* .bInterfaceNumber = DYNAMIC */
  62. .bAlternateSetting = 0,
  63. .bNumEndpoints = 2,
  64. .bInterfaceClass = USB_CLASS_HID,
  65. /* .bInterfaceSubClass = DYNAMIC */
  66. /* .bInterfaceProtocol = DYNAMIC */
  67. /* .iInterface = DYNAMIC */
  68. };
  69. static struct hid_descriptor hidg_desc = {
  70. .bLength = sizeof hidg_desc,
  71. .bDescriptorType = HID_DT_HID,
  72. .bcdHID = 0x0101,
  73. .bCountryCode = 0x00,
  74. .bNumDescriptors = 0x1,
  75. /*.desc[0].bDescriptorType = DYNAMIC */
  76. /*.desc[0].wDescriptorLenght = DYNAMIC */
  77. };
  78. /* High-Speed Support */
  79. static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
  80. .bLength = USB_DT_ENDPOINT_SIZE,
  81. .bDescriptorType = USB_DT_ENDPOINT,
  82. .bEndpointAddress = USB_DIR_IN,
  83. .bmAttributes = USB_ENDPOINT_XFER_INT,
  84. /*.wMaxPacketSize = DYNAMIC */
  85. .bInterval = 4, /* FIXME: Add this field in the
  86. * HID gadget configuration?
  87. * (struct hidg_func_descriptor)
  88. */
  89. };
  90. static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
  91. .bLength = USB_DT_ENDPOINT_SIZE,
  92. .bDescriptorType = USB_DT_ENDPOINT,
  93. .bEndpointAddress = USB_DIR_OUT,
  94. .bmAttributes = USB_ENDPOINT_XFER_INT,
  95. /*.wMaxPacketSize = DYNAMIC */
  96. .bInterval = 4, /* FIXME: Add this field in the
  97. * HID gadget configuration?
  98. * (struct hidg_func_descriptor)
  99. */
  100. };
  101. static struct usb_descriptor_header *hidg_hs_descriptors[] = {
  102. (struct usb_descriptor_header *)&hidg_interface_desc,
  103. (struct usb_descriptor_header *)&hidg_desc,
  104. (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
  105. (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
  106. NULL,
  107. };
  108. /* Full-Speed Support */
  109. static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
  110. .bLength = USB_DT_ENDPOINT_SIZE,
  111. .bDescriptorType = USB_DT_ENDPOINT,
  112. .bEndpointAddress = USB_DIR_IN,
  113. .bmAttributes = USB_ENDPOINT_XFER_INT,
  114. /*.wMaxPacketSize = DYNAMIC */
  115. .bInterval = 10, /* FIXME: Add this field in the
  116. * HID gadget configuration?
  117. * (struct hidg_func_descriptor)
  118. */
  119. };
  120. static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
  121. .bLength = USB_DT_ENDPOINT_SIZE,
  122. .bDescriptorType = USB_DT_ENDPOINT,
  123. .bEndpointAddress = USB_DIR_OUT,
  124. .bmAttributes = USB_ENDPOINT_XFER_INT,
  125. /*.wMaxPacketSize = DYNAMIC */
  126. .bInterval = 10, /* FIXME: Add this field in the
  127. * HID gadget configuration?
  128. * (struct hidg_func_descriptor)
  129. */
  130. };
  131. static struct usb_descriptor_header *hidg_fs_descriptors[] = {
  132. (struct usb_descriptor_header *)&hidg_interface_desc,
  133. (struct usb_descriptor_header *)&hidg_desc,
  134. (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
  135. (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
  136. NULL,
  137. };
  138. /*-------------------------------------------------------------------------*/
  139. /* Char Device */
  140. static ssize_t f_hidg_read(struct file *file, char __user *buffer,
  141. size_t count, loff_t *ptr)
  142. {
  143. struct f_hidg *hidg = file->private_data;
  144. struct f_hidg_req_list *list;
  145. struct usb_request *req;
  146. unsigned long flags;
  147. int ret;
  148. if (!count)
  149. return 0;
  150. if (!access_ok(VERIFY_WRITE, buffer, count))
  151. return -EFAULT;
  152. spin_lock_irqsave(&hidg->spinlock, flags);
  153. #define READ_COND (!list_empty(&hidg->completed_out_req))
  154. /* wait for at least one buffer to complete */
  155. while (!READ_COND) {
  156. spin_unlock_irqrestore(&hidg->spinlock, flags);
  157. if (file->f_flags & O_NONBLOCK)
  158. return -EAGAIN;
  159. if (wait_event_interruptible(hidg->read_queue, READ_COND))
  160. return -ERESTARTSYS;
  161. spin_lock_irqsave(&hidg->spinlock, flags);
  162. }
  163. /* pick the first one */
  164. list = list_first_entry(&hidg->completed_out_req,
  165. struct f_hidg_req_list, list);
  166. req = list->req;
  167. count = min_t(unsigned int, count, req->actual - list->pos);
  168. spin_unlock_irqrestore(&hidg->spinlock, flags);
  169. /* copy to user outside spinlock */
  170. count -= copy_to_user(buffer, req->buf + list->pos, count);
  171. list->pos += count;
  172. /*
  173. * if this request is completely handled and transfered to
  174. * userspace, remove its entry from the list and requeue it
  175. * again. Otherwise, we will revisit it again upon the next
  176. * call, taking into account its current read position.
  177. */
  178. if (list->pos == req->actual) {
  179. spin_lock_irqsave(&hidg->spinlock, flags);
  180. list_del(&list->list);
  181. kfree(list);
  182. spin_unlock_irqrestore(&hidg->spinlock, flags);
  183. req->length = hidg->report_length;
  184. ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
  185. if (ret < 0)
  186. return ret;
  187. }
  188. return count;
  189. }
  190. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  191. {
  192. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  193. if (req->status != 0) {
  194. ERROR(hidg->func.config->cdev,
  195. "End Point Request ERROR: %d\n", req->status);
  196. }
  197. hidg->write_pending = 0;
  198. wake_up(&hidg->write_queue);
  199. }
  200. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  201. size_t count, loff_t *offp)
  202. {
  203. struct f_hidg *hidg = file->private_data;
  204. ssize_t status = -ENOMEM;
  205. if (!access_ok(VERIFY_READ, buffer, count))
  206. return -EFAULT;
  207. mutex_lock(&hidg->lock);
  208. #define WRITE_COND (!hidg->write_pending)
  209. /* write queue */
  210. while (!WRITE_COND) {
  211. mutex_unlock(&hidg->lock);
  212. if (file->f_flags & O_NONBLOCK)
  213. return -EAGAIN;
  214. if (wait_event_interruptible_exclusive(
  215. hidg->write_queue, WRITE_COND))
  216. return -ERESTARTSYS;
  217. mutex_lock(&hidg->lock);
  218. }
  219. count = min_t(unsigned, count, hidg->report_length);
  220. status = copy_from_user(hidg->req->buf, buffer, count);
  221. if (status != 0) {
  222. ERROR(hidg->func.config->cdev,
  223. "copy_from_user error\n");
  224. mutex_unlock(&hidg->lock);
  225. return -EINVAL;
  226. }
  227. hidg->req->status = 0;
  228. hidg->req->zero = 0;
  229. hidg->req->length = count;
  230. hidg->req->complete = f_hidg_req_complete;
  231. hidg->req->context = hidg;
  232. hidg->write_pending = 1;
  233. status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
  234. if (status < 0) {
  235. ERROR(hidg->func.config->cdev,
  236. "usb_ep_queue error on int endpoint %zd\n", status);
  237. hidg->write_pending = 0;
  238. wake_up(&hidg->write_queue);
  239. } else {
  240. status = count;
  241. }
  242. mutex_unlock(&hidg->lock);
  243. return status;
  244. }
  245. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  246. {
  247. struct f_hidg *hidg = file->private_data;
  248. unsigned int ret = 0;
  249. poll_wait(file, &hidg->read_queue, wait);
  250. poll_wait(file, &hidg->write_queue, wait);
  251. if (WRITE_COND)
  252. ret |= POLLOUT | POLLWRNORM;
  253. if (READ_COND)
  254. ret |= POLLIN | POLLRDNORM;
  255. return ret;
  256. }
  257. #undef WRITE_COND
  258. #undef READ_COND
  259. static int f_hidg_release(struct inode *inode, struct file *fd)
  260. {
  261. fd->private_data = NULL;
  262. return 0;
  263. }
  264. static int f_hidg_open(struct inode *inode, struct file *fd)
  265. {
  266. struct f_hidg *hidg =
  267. container_of(inode->i_cdev, struct f_hidg, cdev);
  268. fd->private_data = hidg;
  269. return 0;
  270. }
  271. /*-------------------------------------------------------------------------*/
  272. /* usb_function */
  273. static struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, unsigned length)
  274. {
  275. struct usb_request *req;
  276. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  277. if (req) {
  278. req->length = length;
  279. req->buf = kmalloc(length, GFP_ATOMIC);
  280. if (!req->buf) {
  281. usb_ep_free_request(ep, req);
  282. req = NULL;
  283. }
  284. }
  285. return req;
  286. }
  287. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  288. {
  289. struct f_hidg *hidg = (struct f_hidg *) req->context;
  290. struct f_hidg_req_list *req_list;
  291. unsigned long flags;
  292. req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
  293. if (!req_list)
  294. return;
  295. req_list->req = req;
  296. spin_lock_irqsave(&hidg->spinlock, flags);
  297. list_add_tail(&req_list->list, &hidg->completed_out_req);
  298. spin_unlock_irqrestore(&hidg->spinlock, flags);
  299. wake_up(&hidg->read_queue);
  300. }
  301. static int hidg_setup(struct usb_function *f,
  302. const struct usb_ctrlrequest *ctrl)
  303. {
  304. struct f_hidg *hidg = func_to_hidg(f);
  305. struct usb_composite_dev *cdev = f->config->cdev;
  306. struct usb_request *req = cdev->req;
  307. int status = 0;
  308. __u16 value, length;
  309. value = __le16_to_cpu(ctrl->wValue);
  310. length = __le16_to_cpu(ctrl->wLength);
  311. VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
  312. "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
  313. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  314. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  315. | HID_REQ_GET_REPORT):
  316. VDBG(cdev, "get_report\n");
  317. /* send an empty report */
  318. length = min_t(unsigned, length, hidg->report_length);
  319. memset(req->buf, 0x0, length);
  320. goto respond;
  321. break;
  322. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  323. | HID_REQ_GET_PROTOCOL):
  324. VDBG(cdev, "get_protocol\n");
  325. goto stall;
  326. break;
  327. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  328. | HID_REQ_SET_REPORT):
  329. VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
  330. goto stall;
  331. break;
  332. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  333. | HID_REQ_SET_PROTOCOL):
  334. VDBG(cdev, "set_protocol\n");
  335. goto stall;
  336. break;
  337. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  338. | USB_REQ_GET_DESCRIPTOR):
  339. switch (value >> 8) {
  340. case HID_DT_HID:
  341. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  342. length = min_t(unsigned short, length,
  343. hidg_desc.bLength);
  344. memcpy(req->buf, &hidg_desc, length);
  345. goto respond;
  346. break;
  347. case HID_DT_REPORT:
  348. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  349. length = min_t(unsigned short, length,
  350. hidg->report_desc_length);
  351. memcpy(req->buf, hidg->report_desc, length);
  352. goto respond;
  353. break;
  354. default:
  355. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  356. value >> 8);
  357. goto stall;
  358. break;
  359. }
  360. break;
  361. default:
  362. VDBG(cdev, "Unknown request 0x%x\n",
  363. ctrl->bRequest);
  364. goto stall;
  365. break;
  366. }
  367. stall:
  368. return -EOPNOTSUPP;
  369. respond:
  370. req->zero = 0;
  371. req->length = length;
  372. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  373. if (status < 0)
  374. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  375. return status;
  376. }
  377. static void hidg_disable(struct usb_function *f)
  378. {
  379. struct f_hidg *hidg = func_to_hidg(f);
  380. struct f_hidg_req_list *list, *next;
  381. usb_ep_disable(hidg->in_ep);
  382. hidg->in_ep->driver_data = NULL;
  383. usb_ep_disable(hidg->out_ep);
  384. hidg->out_ep->driver_data = NULL;
  385. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  386. list_del(&list->list);
  387. kfree(list);
  388. }
  389. }
  390. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  391. {
  392. struct usb_composite_dev *cdev = f->config->cdev;
  393. struct f_hidg *hidg = func_to_hidg(f);
  394. int i, status = 0;
  395. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  396. if (hidg->in_ep != NULL) {
  397. /* restart endpoint */
  398. if (hidg->in_ep->driver_data != NULL)
  399. usb_ep_disable(hidg->in_ep);
  400. status = config_ep_by_speed(f->config->cdev->gadget, f,
  401. hidg->in_ep);
  402. if (status) {
  403. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  404. goto fail;
  405. }
  406. status = usb_ep_enable(hidg->in_ep);
  407. if (status < 0) {
  408. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  409. goto fail;
  410. }
  411. hidg->in_ep->driver_data = hidg;
  412. }
  413. if (hidg->out_ep != NULL) {
  414. /* restart endpoint */
  415. if (hidg->out_ep->driver_data != NULL)
  416. usb_ep_disable(hidg->out_ep);
  417. status = config_ep_by_speed(f->config->cdev->gadget, f,
  418. hidg->out_ep);
  419. if (status) {
  420. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  421. goto fail;
  422. }
  423. status = usb_ep_enable(hidg->out_ep);
  424. if (status < 0) {
  425. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  426. goto fail;
  427. }
  428. hidg->out_ep->driver_data = hidg;
  429. /*
  430. * allocate a bunch of read buffers and queue them all at once.
  431. */
  432. for (i = 0; i < hidg->qlen && status == 0; i++) {
  433. struct usb_request *req =
  434. hidg_alloc_ep_req(hidg->out_ep,
  435. hidg->report_length);
  436. if (req) {
  437. req->complete = hidg_set_report_complete;
  438. req->context = hidg;
  439. status = usb_ep_queue(hidg->out_ep, req,
  440. GFP_ATOMIC);
  441. if (status)
  442. ERROR(cdev, "%s queue req --> %d\n",
  443. hidg->out_ep->name, status);
  444. } else {
  445. usb_ep_disable(hidg->out_ep);
  446. hidg->out_ep->driver_data = NULL;
  447. status = -ENOMEM;
  448. goto fail;
  449. }
  450. }
  451. }
  452. fail:
  453. return status;
  454. }
  455. const struct file_operations f_hidg_fops = {
  456. .owner = THIS_MODULE,
  457. .open = f_hidg_open,
  458. .release = f_hidg_release,
  459. .write = f_hidg_write,
  460. .read = f_hidg_read,
  461. .poll = f_hidg_poll,
  462. .llseek = noop_llseek,
  463. };
  464. static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
  465. {
  466. struct usb_ep *ep;
  467. struct f_hidg *hidg = func_to_hidg(f);
  468. int status;
  469. dev_t dev;
  470. /* allocate instance-specific interface IDs, and patch descriptors */
  471. status = usb_interface_id(c, f);
  472. if (status < 0)
  473. goto fail;
  474. hidg_interface_desc.bInterfaceNumber = status;
  475. /* allocate instance-specific endpoints */
  476. status = -ENODEV;
  477. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  478. if (!ep)
  479. goto fail;
  480. ep->driver_data = c->cdev; /* claim */
  481. hidg->in_ep = ep;
  482. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  483. if (!ep)
  484. goto fail;
  485. ep->driver_data = c->cdev; /* claim */
  486. hidg->out_ep = ep;
  487. /* preallocate request and buffer */
  488. status = -ENOMEM;
  489. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  490. if (!hidg->req)
  491. goto fail;
  492. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  493. if (!hidg->req->buf)
  494. goto fail;
  495. /* set descriptor dynamic values */
  496. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  497. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  498. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  499. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  500. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  501. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  502. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  503. hidg_desc.desc[0].wDescriptorLength =
  504. cpu_to_le16(hidg->report_desc_length);
  505. /* copy descriptors */
  506. f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
  507. if (!f->descriptors)
  508. goto fail;
  509. if (gadget_is_dualspeed(c->cdev->gadget)) {
  510. hidg_hs_in_ep_desc.bEndpointAddress =
  511. hidg_fs_in_ep_desc.bEndpointAddress;
  512. hidg_hs_out_ep_desc.bEndpointAddress =
  513. hidg_fs_out_ep_desc.bEndpointAddress;
  514. f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
  515. if (!f->hs_descriptors)
  516. goto fail;
  517. }
  518. mutex_init(&hidg->lock);
  519. spin_lock_init(&hidg->spinlock);
  520. init_waitqueue_head(&hidg->write_queue);
  521. init_waitqueue_head(&hidg->read_queue);
  522. INIT_LIST_HEAD(&hidg->completed_out_req);
  523. /* create char device */
  524. cdev_init(&hidg->cdev, &f_hidg_fops);
  525. dev = MKDEV(major, hidg->minor);
  526. status = cdev_add(&hidg->cdev, dev, 1);
  527. if (status)
  528. goto fail;
  529. device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
  530. return 0;
  531. fail:
  532. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  533. if (hidg->req != NULL) {
  534. kfree(hidg->req->buf);
  535. if (hidg->in_ep != NULL)
  536. usb_ep_free_request(hidg->in_ep, hidg->req);
  537. }
  538. usb_free_descriptors(f->hs_descriptors);
  539. usb_free_descriptors(f->descriptors);
  540. return status;
  541. }
  542. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  543. {
  544. struct f_hidg *hidg = func_to_hidg(f);
  545. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  546. cdev_del(&hidg->cdev);
  547. /* disable/free request and end point */
  548. usb_ep_disable(hidg->in_ep);
  549. usb_ep_dequeue(hidg->in_ep, hidg->req);
  550. kfree(hidg->req->buf);
  551. usb_ep_free_request(hidg->in_ep, hidg->req);
  552. /* free descriptors copies */
  553. usb_free_descriptors(f->hs_descriptors);
  554. usb_free_descriptors(f->descriptors);
  555. kfree(hidg->report_desc);
  556. kfree(hidg);
  557. }
  558. /*-------------------------------------------------------------------------*/
  559. /* Strings */
  560. #define CT_FUNC_HID_IDX 0
  561. static struct usb_string ct_func_string_defs[] = {
  562. [CT_FUNC_HID_IDX].s = "HID Interface",
  563. {}, /* end of list */
  564. };
  565. static struct usb_gadget_strings ct_func_string_table = {
  566. .language = 0x0409, /* en-US */
  567. .strings = ct_func_string_defs,
  568. };
  569. static struct usb_gadget_strings *ct_func_strings[] = {
  570. &ct_func_string_table,
  571. NULL,
  572. };
  573. /*-------------------------------------------------------------------------*/
  574. /* usb_configuration */
  575. int __init hidg_bind_config(struct usb_configuration *c,
  576. struct hidg_func_descriptor *fdesc, int index)
  577. {
  578. struct f_hidg *hidg;
  579. int status;
  580. if (index >= minors)
  581. return -ENOENT;
  582. /* maybe allocate device-global string IDs, and patch descriptors */
  583. if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
  584. status = usb_string_id(c->cdev);
  585. if (status < 0)
  586. return status;
  587. ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
  588. hidg_interface_desc.iInterface = status;
  589. }
  590. /* allocate and initialize one new instance */
  591. hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
  592. if (!hidg)
  593. return -ENOMEM;
  594. hidg->minor = index;
  595. hidg->bInterfaceSubClass = fdesc->subclass;
  596. hidg->bInterfaceProtocol = fdesc->protocol;
  597. hidg->report_length = fdesc->report_length;
  598. hidg->report_desc_length = fdesc->report_desc_length;
  599. hidg->report_desc = kmemdup(fdesc->report_desc,
  600. fdesc->report_desc_length,
  601. GFP_KERNEL);
  602. if (!hidg->report_desc) {
  603. kfree(hidg);
  604. return -ENOMEM;
  605. }
  606. hidg->func.name = "hid";
  607. hidg->func.strings = ct_func_strings;
  608. hidg->func.bind = hidg_bind;
  609. hidg->func.unbind = hidg_unbind;
  610. hidg->func.set_alt = hidg_set_alt;
  611. hidg->func.disable = hidg_disable;
  612. hidg->func.setup = hidg_setup;
  613. /* this could me made configurable at some point */
  614. hidg->qlen = 4;
  615. status = usb_add_function(c, &hidg->func);
  616. if (status)
  617. kfree(hidg);
  618. return status;
  619. }
  620. int __init ghid_setup(struct usb_gadget *g, int count)
  621. {
  622. int status;
  623. dev_t dev;
  624. hidg_class = class_create(THIS_MODULE, "hidg");
  625. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  626. if (!status) {
  627. major = MAJOR(dev);
  628. minors = count;
  629. }
  630. return status;
  631. }
  632. void ghid_cleanup(void)
  633. {
  634. if (major) {
  635. unregister_chrdev_region(MKDEV(major, 0), minors);
  636. major = minors = 0;
  637. }
  638. class_destroy(hidg_class);
  639. hidg_class = NULL;
  640. }