f_hid.c 18 KB

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