f_hid.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. return;
  257. }
  258. static int hidg_setup(struct usb_function *f,
  259. const struct usb_ctrlrequest *ctrl)
  260. {
  261. struct f_hidg *hidg = func_to_hidg(f);
  262. struct usb_composite_dev *cdev = f->config->cdev;
  263. struct usb_request *req = cdev->req;
  264. int status = 0;
  265. __u16 value, length;
  266. value = __le16_to_cpu(ctrl->wValue);
  267. length = __le16_to_cpu(ctrl->wLength);
  268. VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
  269. "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
  270. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  271. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  272. | HID_REQ_GET_REPORT):
  273. VDBG(cdev, "get_report\n");
  274. /* send an empty report */
  275. length = min_t(unsigned, length, hidg->report_length);
  276. memset(req->buf, 0x0, length);
  277. goto respond;
  278. break;
  279. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  280. | HID_REQ_GET_PROTOCOL):
  281. VDBG(cdev, "get_protocol\n");
  282. goto stall;
  283. break;
  284. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  285. | HID_REQ_SET_REPORT):
  286. VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
  287. req->context = hidg;
  288. req->complete = hidg_set_report_complete;
  289. goto respond;
  290. break;
  291. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  292. | HID_REQ_SET_PROTOCOL):
  293. VDBG(cdev, "set_protocol\n");
  294. goto stall;
  295. break;
  296. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  297. | USB_REQ_GET_DESCRIPTOR):
  298. switch (value >> 8) {
  299. case HID_DT_REPORT:
  300. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  301. length = min_t(unsigned short, length,
  302. hidg->report_desc_length);
  303. memcpy(req->buf, hidg->report_desc, length);
  304. goto respond;
  305. break;
  306. default:
  307. VDBG(cdev, "Unknown decriptor request 0x%x\n",
  308. value >> 8);
  309. goto stall;
  310. break;
  311. }
  312. break;
  313. default:
  314. VDBG(cdev, "Unknown request 0x%x\n",
  315. ctrl->bRequest);
  316. goto stall;
  317. break;
  318. }
  319. stall:
  320. return -EOPNOTSUPP;
  321. respond:
  322. req->zero = 0;
  323. req->length = length;
  324. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  325. if (status < 0)
  326. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  327. return status;
  328. }
  329. static void hidg_disable(struct usb_function *f)
  330. {
  331. struct f_hidg *hidg = func_to_hidg(f);
  332. usb_ep_disable(hidg->in_ep);
  333. hidg->in_ep->driver_data = NULL;
  334. return;
  335. }
  336. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  337. {
  338. struct usb_composite_dev *cdev = f->config->cdev;
  339. struct f_hidg *hidg = func_to_hidg(f);
  340. const struct usb_endpoint_descriptor *ep_desc;
  341. int status = 0;
  342. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  343. if (hidg->in_ep != NULL) {
  344. /* restart endpoint */
  345. if (hidg->in_ep->driver_data != NULL)
  346. usb_ep_disable(hidg->in_ep);
  347. ep_desc = ep_choose(f->config->cdev->gadget,
  348. hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
  349. status = usb_ep_enable(hidg->in_ep, ep_desc);
  350. if (status < 0) {
  351. ERROR(cdev, "Enable endpoint FAILED!\n");
  352. goto fail;
  353. }
  354. hidg->in_ep->driver_data = hidg;
  355. }
  356. fail:
  357. return status;
  358. }
  359. const struct file_operations f_hidg_fops = {
  360. .owner = THIS_MODULE,
  361. .open = f_hidg_open,
  362. .release = f_hidg_release,
  363. .write = f_hidg_write,
  364. .read = f_hidg_read,
  365. .poll = f_hidg_poll,
  366. };
  367. static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
  368. {
  369. struct usb_ep *ep;
  370. struct f_hidg *hidg = func_to_hidg(f);
  371. int status;
  372. dev_t dev;
  373. /* allocate instance-specific interface IDs, and patch descriptors */
  374. status = usb_interface_id(c, f);
  375. if (status < 0)
  376. goto fail;
  377. hidg_interface_desc.bInterfaceNumber = status;
  378. /* allocate instance-specific endpoints */
  379. status = -ENODEV;
  380. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  381. if (!ep)
  382. goto fail;
  383. ep->driver_data = c->cdev; /* claim */
  384. hidg->in_ep = ep;
  385. /* preallocate request and buffer */
  386. status = -ENOMEM;
  387. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  388. if (!hidg->req)
  389. goto fail;
  390. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  391. if (!hidg->req->buf)
  392. goto fail;
  393. /* set descriptor dynamic values */
  394. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  395. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  396. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  397. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  398. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  399. hidg_desc.desc[0].wDescriptorLength =
  400. cpu_to_le16(hidg->report_desc_length);
  401. hidg->set_report_buff = NULL;
  402. /* copy descriptors */
  403. f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
  404. if (!f->descriptors)
  405. goto fail;
  406. hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
  407. f->descriptors,
  408. &hidg_fs_in_ep_desc);
  409. if (gadget_is_dualspeed(c->cdev->gadget)) {
  410. hidg_hs_in_ep_desc.bEndpointAddress =
  411. hidg_fs_in_ep_desc.bEndpointAddress;
  412. f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
  413. if (!f->hs_descriptors)
  414. goto fail;
  415. hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
  416. f->hs_descriptors,
  417. &hidg_hs_in_ep_desc);
  418. } else {
  419. hidg->hs_in_ep_desc = NULL;
  420. }
  421. mutex_init(&hidg->lock);
  422. spin_lock_init(&hidg->spinlock);
  423. init_waitqueue_head(&hidg->write_queue);
  424. init_waitqueue_head(&hidg->read_queue);
  425. /* create char device */
  426. cdev_init(&hidg->cdev, &f_hidg_fops);
  427. dev = MKDEV(major, hidg->minor);
  428. status = cdev_add(&hidg->cdev, dev, 1);
  429. if (status)
  430. goto fail;
  431. device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
  432. return 0;
  433. fail:
  434. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  435. if (hidg->req != NULL) {
  436. kfree(hidg->req->buf);
  437. if (hidg->in_ep != NULL)
  438. usb_ep_free_request(hidg->in_ep, hidg->req);
  439. }
  440. usb_free_descriptors(f->hs_descriptors);
  441. usb_free_descriptors(f->descriptors);
  442. return status;
  443. }
  444. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  445. {
  446. struct f_hidg *hidg = func_to_hidg(f);
  447. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  448. cdev_del(&hidg->cdev);
  449. /* disable/free request and end point */
  450. usb_ep_disable(hidg->in_ep);
  451. usb_ep_dequeue(hidg->in_ep, hidg->req);
  452. kfree(hidg->req->buf);
  453. usb_ep_free_request(hidg->in_ep, hidg->req);
  454. /* free descriptors copies */
  455. usb_free_descriptors(f->hs_descriptors);
  456. usb_free_descriptors(f->descriptors);
  457. kfree(hidg->report_desc);
  458. kfree(hidg->set_report_buff);
  459. kfree(hidg);
  460. }
  461. /*-------------------------------------------------------------------------*/
  462. /* Strings */
  463. #define CT_FUNC_HID_IDX 0
  464. static struct usb_string ct_func_string_defs[] = {
  465. [CT_FUNC_HID_IDX].s = "HID Interface",
  466. {}, /* end of list */
  467. };
  468. static struct usb_gadget_strings ct_func_string_table = {
  469. .language = 0x0409, /* en-US */
  470. .strings = ct_func_string_defs,
  471. };
  472. static struct usb_gadget_strings *ct_func_strings[] = {
  473. &ct_func_string_table,
  474. NULL,
  475. };
  476. /*-------------------------------------------------------------------------*/
  477. /* usb_configuration */
  478. int __init hidg_bind_config(struct usb_configuration *c,
  479. struct hidg_func_descriptor *fdesc, int index)
  480. {
  481. struct f_hidg *hidg;
  482. int status;
  483. if (index >= minors)
  484. return -ENOENT;
  485. /* maybe allocate device-global string IDs, and patch descriptors */
  486. if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
  487. status = usb_string_id(c->cdev);
  488. if (status < 0)
  489. return status;
  490. ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
  491. hidg_interface_desc.iInterface = status;
  492. }
  493. /* allocate and initialize one new instance */
  494. hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
  495. if (!hidg)
  496. return -ENOMEM;
  497. hidg->minor = index;
  498. hidg->bInterfaceSubClass = fdesc->subclass;
  499. hidg->bInterfaceProtocol = fdesc->protocol;
  500. hidg->report_length = fdesc->report_length;
  501. hidg->report_desc_length = fdesc->report_desc_length;
  502. hidg->report_desc = kmemdup(fdesc->report_desc,
  503. fdesc->report_desc_length,
  504. GFP_KERNEL);
  505. if (!hidg->report_desc) {
  506. kfree(hidg);
  507. return -ENOMEM;
  508. }
  509. hidg->func.name = "hid";
  510. hidg->func.strings = ct_func_strings;
  511. hidg->func.bind = hidg_bind;
  512. hidg->func.unbind = hidg_unbind;
  513. hidg->func.set_alt = hidg_set_alt;
  514. hidg->func.disable = hidg_disable;
  515. hidg->func.setup = hidg_setup;
  516. status = usb_add_function(c, &hidg->func);
  517. if (status)
  518. kfree(hidg);
  519. return status;
  520. }
  521. int __init ghid_setup(struct usb_gadget *g, int count)
  522. {
  523. int status;
  524. dev_t dev;
  525. hidg_class = class_create(THIS_MODULE, "hidg");
  526. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  527. if (!status) {
  528. major = MAJOR(dev);
  529. minors = count;
  530. }
  531. return status;
  532. }
  533. void ghid_cleanup(void)
  534. {
  535. if (major) {
  536. unregister_chrdev_region(MKDEV(major, 0), minors);
  537. major = minors = 0;
  538. }
  539. class_destroy(hidg_class);
  540. hidg_class = NULL;
  541. }