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