f_hid.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. /*
  168. * Remove this from list to protect it from beign free()
  169. * while host disables our function
  170. */
  171. list_del(&list->list);
  172. req = list->req;
  173. count = min_t(unsigned int, count, req->actual - list->pos);
  174. spin_unlock_irqrestore(&hidg->spinlock, flags);
  175. /* copy to user outside spinlock */
  176. count -= copy_to_user(buffer, req->buf + list->pos, count);
  177. list->pos += count;
  178. /*
  179. * if this request is completely handled and transfered to
  180. * userspace, remove its entry from the list and requeue it
  181. * again. Otherwise, we will revisit it again upon the next
  182. * call, taking into account its current read position.
  183. */
  184. if (list->pos == req->actual) {
  185. kfree(list);
  186. req->length = hidg->report_length;
  187. ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
  188. if (ret < 0) {
  189. free_ep_req(hidg->out_ep, req);
  190. return ret;
  191. }
  192. } else {
  193. spin_lock_irqsave(&hidg->spinlock, flags);
  194. list_add(&list->list, &hidg->completed_out_req);
  195. spin_unlock_irqrestore(&hidg->spinlock, flags);
  196. wake_up(&hidg->read_queue);
  197. }
  198. return count;
  199. }
  200. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  201. {
  202. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  203. if (req->status != 0) {
  204. ERROR(hidg->func.config->cdev,
  205. "End Point Request ERROR: %d\n", req->status);
  206. }
  207. hidg->write_pending = 0;
  208. wake_up(&hidg->write_queue);
  209. }
  210. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  211. size_t count, loff_t *offp)
  212. {
  213. struct f_hidg *hidg = file->private_data;
  214. ssize_t status = -ENOMEM;
  215. if (!access_ok(VERIFY_READ, buffer, count))
  216. return -EFAULT;
  217. mutex_lock(&hidg->lock);
  218. #define WRITE_COND (!hidg->write_pending)
  219. /* write queue */
  220. while (!WRITE_COND) {
  221. mutex_unlock(&hidg->lock);
  222. if (file->f_flags & O_NONBLOCK)
  223. return -EAGAIN;
  224. if (wait_event_interruptible_exclusive(
  225. hidg->write_queue, WRITE_COND))
  226. return -ERESTARTSYS;
  227. mutex_lock(&hidg->lock);
  228. }
  229. count = min_t(unsigned, count, hidg->report_length);
  230. status = copy_from_user(hidg->req->buf, buffer, count);
  231. if (status != 0) {
  232. ERROR(hidg->func.config->cdev,
  233. "copy_from_user error\n");
  234. mutex_unlock(&hidg->lock);
  235. return -EINVAL;
  236. }
  237. hidg->req->status = 0;
  238. hidg->req->zero = 0;
  239. hidg->req->length = count;
  240. hidg->req->complete = f_hidg_req_complete;
  241. hidg->req->context = hidg;
  242. hidg->write_pending = 1;
  243. status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
  244. if (status < 0) {
  245. ERROR(hidg->func.config->cdev,
  246. "usb_ep_queue error on int endpoint %zd\n", status);
  247. hidg->write_pending = 0;
  248. wake_up(&hidg->write_queue);
  249. } else {
  250. status = count;
  251. }
  252. mutex_unlock(&hidg->lock);
  253. return status;
  254. }
  255. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  256. {
  257. struct f_hidg *hidg = file->private_data;
  258. unsigned int ret = 0;
  259. poll_wait(file, &hidg->read_queue, wait);
  260. poll_wait(file, &hidg->write_queue, wait);
  261. if (WRITE_COND)
  262. ret |= POLLOUT | POLLWRNORM;
  263. if (READ_COND)
  264. ret |= POLLIN | POLLRDNORM;
  265. return ret;
  266. }
  267. #undef WRITE_COND
  268. #undef READ_COND
  269. static int f_hidg_release(struct inode *inode, struct file *fd)
  270. {
  271. fd->private_data = NULL;
  272. return 0;
  273. }
  274. static int f_hidg_open(struct inode *inode, struct file *fd)
  275. {
  276. struct f_hidg *hidg =
  277. container_of(inode->i_cdev, struct f_hidg, cdev);
  278. fd->private_data = hidg;
  279. return 0;
  280. }
  281. /*-------------------------------------------------------------------------*/
  282. /* usb_function */
  283. static struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, unsigned length)
  284. {
  285. struct usb_request *req;
  286. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  287. if (req) {
  288. req->length = length;
  289. req->buf = kmalloc(length, GFP_ATOMIC);
  290. if (!req->buf) {
  291. usb_ep_free_request(ep, req);
  292. req = NULL;
  293. }
  294. }
  295. return req;
  296. }
  297. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  298. {
  299. struct f_hidg *hidg = (struct f_hidg *) req->context;
  300. struct f_hidg_req_list *req_list;
  301. unsigned long flags;
  302. req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
  303. if (!req_list)
  304. return;
  305. req_list->req = req;
  306. spin_lock_irqsave(&hidg->spinlock, flags);
  307. list_add_tail(&req_list->list, &hidg->completed_out_req);
  308. spin_unlock_irqrestore(&hidg->spinlock, flags);
  309. wake_up(&hidg->read_queue);
  310. }
  311. static int hidg_setup(struct usb_function *f,
  312. const struct usb_ctrlrequest *ctrl)
  313. {
  314. struct f_hidg *hidg = func_to_hidg(f);
  315. struct usb_composite_dev *cdev = f->config->cdev;
  316. struct usb_request *req = cdev->req;
  317. int status = 0;
  318. __u16 value, length;
  319. value = __le16_to_cpu(ctrl->wValue);
  320. length = __le16_to_cpu(ctrl->wLength);
  321. VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
  322. "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
  323. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  324. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  325. | HID_REQ_GET_REPORT):
  326. VDBG(cdev, "get_report\n");
  327. /* send an empty report */
  328. length = min_t(unsigned, length, hidg->report_length);
  329. memset(req->buf, 0x0, length);
  330. goto respond;
  331. break;
  332. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  333. | HID_REQ_GET_PROTOCOL):
  334. VDBG(cdev, "get_protocol\n");
  335. goto stall;
  336. break;
  337. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  338. | HID_REQ_SET_REPORT):
  339. VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
  340. goto stall;
  341. break;
  342. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  343. | HID_REQ_SET_PROTOCOL):
  344. VDBG(cdev, "set_protocol\n");
  345. goto stall;
  346. break;
  347. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  348. | USB_REQ_GET_DESCRIPTOR):
  349. switch (value >> 8) {
  350. case HID_DT_HID:
  351. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  352. length = min_t(unsigned short, length,
  353. hidg_desc.bLength);
  354. memcpy(req->buf, &hidg_desc, length);
  355. goto respond;
  356. break;
  357. case HID_DT_REPORT:
  358. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  359. length = min_t(unsigned short, length,
  360. hidg->report_desc_length);
  361. memcpy(req->buf, hidg->report_desc, length);
  362. goto respond;
  363. break;
  364. default:
  365. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  366. value >> 8);
  367. goto stall;
  368. break;
  369. }
  370. break;
  371. default:
  372. VDBG(cdev, "Unknown request 0x%x\n",
  373. ctrl->bRequest);
  374. goto stall;
  375. break;
  376. }
  377. stall:
  378. return -EOPNOTSUPP;
  379. respond:
  380. req->zero = 0;
  381. req->length = length;
  382. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  383. if (status < 0)
  384. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  385. return status;
  386. }
  387. static void hidg_disable(struct usb_function *f)
  388. {
  389. struct f_hidg *hidg = func_to_hidg(f);
  390. struct f_hidg_req_list *list, *next;
  391. unsigned long flags;
  392. usb_ep_disable(hidg->in_ep);
  393. hidg->in_ep->driver_data = NULL;
  394. usb_ep_disable(hidg->out_ep);
  395. hidg->out_ep->driver_data = NULL;
  396. spin_lock_irqsave(&hidg->spinlock, flags);
  397. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  398. free_ep_req(hidg->out_ep, list->req);
  399. list_del(&list->list);
  400. kfree(list);
  401. }
  402. spin_unlock_irqrestore(&hidg->spinlock, flags);
  403. }
  404. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  405. {
  406. struct usb_composite_dev *cdev = f->config->cdev;
  407. struct f_hidg *hidg = func_to_hidg(f);
  408. int i, status = 0;
  409. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  410. if (hidg->in_ep != NULL) {
  411. /* restart endpoint */
  412. if (hidg->in_ep->driver_data != NULL)
  413. usb_ep_disable(hidg->in_ep);
  414. status = config_ep_by_speed(f->config->cdev->gadget, f,
  415. hidg->in_ep);
  416. if (status) {
  417. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  418. goto fail;
  419. }
  420. status = usb_ep_enable(hidg->in_ep);
  421. if (status < 0) {
  422. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  423. goto fail;
  424. }
  425. hidg->in_ep->driver_data = hidg;
  426. }
  427. if (hidg->out_ep != NULL) {
  428. /* restart endpoint */
  429. if (hidg->out_ep->driver_data != NULL)
  430. usb_ep_disable(hidg->out_ep);
  431. status = config_ep_by_speed(f->config->cdev->gadget, f,
  432. hidg->out_ep);
  433. if (status) {
  434. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  435. goto fail;
  436. }
  437. status = usb_ep_enable(hidg->out_ep);
  438. if (status < 0) {
  439. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  440. goto fail;
  441. }
  442. hidg->out_ep->driver_data = hidg;
  443. /*
  444. * allocate a bunch of read buffers and queue them all at once.
  445. */
  446. for (i = 0; i < hidg->qlen && status == 0; i++) {
  447. struct usb_request *req =
  448. hidg_alloc_ep_req(hidg->out_ep,
  449. hidg->report_length);
  450. if (req) {
  451. req->complete = hidg_set_report_complete;
  452. req->context = hidg;
  453. status = usb_ep_queue(hidg->out_ep, req,
  454. GFP_ATOMIC);
  455. if (status)
  456. ERROR(cdev, "%s queue req --> %d\n",
  457. hidg->out_ep->name, status);
  458. } else {
  459. usb_ep_disable(hidg->out_ep);
  460. hidg->out_ep->driver_data = NULL;
  461. status = -ENOMEM;
  462. goto fail;
  463. }
  464. }
  465. }
  466. fail:
  467. return status;
  468. }
  469. const struct file_operations f_hidg_fops = {
  470. .owner = THIS_MODULE,
  471. .open = f_hidg_open,
  472. .release = f_hidg_release,
  473. .write = f_hidg_write,
  474. .read = f_hidg_read,
  475. .poll = f_hidg_poll,
  476. .llseek = noop_llseek,
  477. };
  478. static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
  479. {
  480. struct usb_ep *ep;
  481. struct f_hidg *hidg = func_to_hidg(f);
  482. int status;
  483. dev_t dev;
  484. /* allocate instance-specific interface IDs, and patch descriptors */
  485. status = usb_interface_id(c, f);
  486. if (status < 0)
  487. goto fail;
  488. hidg_interface_desc.bInterfaceNumber = status;
  489. /* allocate instance-specific endpoints */
  490. status = -ENODEV;
  491. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  492. if (!ep)
  493. goto fail;
  494. ep->driver_data = c->cdev; /* claim */
  495. hidg->in_ep = ep;
  496. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  497. if (!ep)
  498. goto fail;
  499. ep->driver_data = c->cdev; /* claim */
  500. hidg->out_ep = ep;
  501. /* preallocate request and buffer */
  502. status = -ENOMEM;
  503. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  504. if (!hidg->req)
  505. goto fail;
  506. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  507. if (!hidg->req->buf)
  508. goto fail;
  509. /* set descriptor dynamic values */
  510. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  511. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  512. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  513. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  514. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  515. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  516. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  517. hidg_desc.desc[0].wDescriptorLength =
  518. cpu_to_le16(hidg->report_desc_length);
  519. hidg_hs_in_ep_desc.bEndpointAddress =
  520. hidg_fs_in_ep_desc.bEndpointAddress;
  521. hidg_hs_out_ep_desc.bEndpointAddress =
  522. hidg_fs_out_ep_desc.bEndpointAddress;
  523. status = usb_assign_descriptors(f, hidg_fs_descriptors,
  524. hidg_hs_descriptors, NULL);
  525. if (status)
  526. goto fail;
  527. mutex_init(&hidg->lock);
  528. spin_lock_init(&hidg->spinlock);
  529. init_waitqueue_head(&hidg->write_queue);
  530. init_waitqueue_head(&hidg->read_queue);
  531. INIT_LIST_HEAD(&hidg->completed_out_req);
  532. /* create char device */
  533. cdev_init(&hidg->cdev, &f_hidg_fops);
  534. dev = MKDEV(major, hidg->minor);
  535. status = cdev_add(&hidg->cdev, dev, 1);
  536. if (status)
  537. goto fail;
  538. device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
  539. return 0;
  540. fail:
  541. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  542. if (hidg->req != NULL) {
  543. kfree(hidg->req->buf);
  544. if (hidg->in_ep != NULL)
  545. usb_ep_free_request(hidg->in_ep, hidg->req);
  546. }
  547. usb_free_all_descriptors(f);
  548. return status;
  549. }
  550. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  551. {
  552. struct f_hidg *hidg = func_to_hidg(f);
  553. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  554. cdev_del(&hidg->cdev);
  555. /* disable/free request and end point */
  556. usb_ep_disable(hidg->in_ep);
  557. usb_ep_dequeue(hidg->in_ep, hidg->req);
  558. kfree(hidg->req->buf);
  559. usb_ep_free_request(hidg->in_ep, hidg->req);
  560. usb_free_all_descriptors(f);
  561. kfree(hidg->report_desc);
  562. kfree(hidg);
  563. }
  564. /*-------------------------------------------------------------------------*/
  565. /* Strings */
  566. #define CT_FUNC_HID_IDX 0
  567. static struct usb_string ct_func_string_defs[] = {
  568. [CT_FUNC_HID_IDX].s = "HID Interface",
  569. {}, /* end of list */
  570. };
  571. static struct usb_gadget_strings ct_func_string_table = {
  572. .language = 0x0409, /* en-US */
  573. .strings = ct_func_string_defs,
  574. };
  575. static struct usb_gadget_strings *ct_func_strings[] = {
  576. &ct_func_string_table,
  577. NULL,
  578. };
  579. /*-------------------------------------------------------------------------*/
  580. /* usb_configuration */
  581. int __init hidg_bind_config(struct usb_configuration *c,
  582. struct hidg_func_descriptor *fdesc, int index)
  583. {
  584. struct f_hidg *hidg;
  585. int status;
  586. if (index >= minors)
  587. return -ENOENT;
  588. /* maybe allocate device-global string IDs, and patch descriptors */
  589. if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
  590. status = usb_string_id(c->cdev);
  591. if (status < 0)
  592. return status;
  593. ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
  594. hidg_interface_desc.iInterface = status;
  595. }
  596. /* allocate and initialize one new instance */
  597. hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
  598. if (!hidg)
  599. return -ENOMEM;
  600. hidg->minor = index;
  601. hidg->bInterfaceSubClass = fdesc->subclass;
  602. hidg->bInterfaceProtocol = fdesc->protocol;
  603. hidg->report_length = fdesc->report_length;
  604. hidg->report_desc_length = fdesc->report_desc_length;
  605. hidg->report_desc = kmemdup(fdesc->report_desc,
  606. fdesc->report_desc_length,
  607. GFP_KERNEL);
  608. if (!hidg->report_desc) {
  609. kfree(hidg);
  610. return -ENOMEM;
  611. }
  612. hidg->func.name = "hid";
  613. hidg->func.strings = ct_func_strings;
  614. hidg->func.bind = hidg_bind;
  615. hidg->func.unbind = hidg_unbind;
  616. hidg->func.set_alt = hidg_set_alt;
  617. hidg->func.disable = hidg_disable;
  618. hidg->func.setup = hidg_setup;
  619. /* this could me made configurable at some point */
  620. hidg->qlen = 4;
  621. status = usb_add_function(c, &hidg->func);
  622. if (status)
  623. kfree(hidg);
  624. return status;
  625. }
  626. int __init ghid_setup(struct usb_gadget *g, int count)
  627. {
  628. int status;
  629. dev_t dev;
  630. hidg_class = class_create(THIS_MODULE, "hidg");
  631. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  632. if (!status) {
  633. major = MAJOR(dev);
  634. minors = count;
  635. }
  636. return status;
  637. }
  638. void ghid_cleanup(void)
  639. {
  640. if (major) {
  641. unregister_chrdev_region(MKDEV(major, 0), minors);
  642. major = minors = 0;
  643. }
  644. class_destroy(hidg_class);
  645. hidg_class = NULL;
  646. }