f_hid.c 19 KB

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