f_hid.c 17 KB

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