f_hid.c 19 KB

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