f_hid.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. .llseek = noop_llseek,
  367. };
  368. static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
  369. {
  370. struct usb_ep *ep;
  371. struct f_hidg *hidg = func_to_hidg(f);
  372. int status;
  373. dev_t dev;
  374. /* allocate instance-specific interface IDs, and patch descriptors */
  375. status = usb_interface_id(c, f);
  376. if (status < 0)
  377. goto fail;
  378. hidg_interface_desc.bInterfaceNumber = status;
  379. /* allocate instance-specific endpoints */
  380. status = -ENODEV;
  381. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  382. if (!ep)
  383. goto fail;
  384. ep->driver_data = c->cdev; /* claim */
  385. hidg->in_ep = ep;
  386. /* preallocate request and buffer */
  387. status = -ENOMEM;
  388. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  389. if (!hidg->req)
  390. goto fail;
  391. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  392. if (!hidg->req->buf)
  393. goto fail;
  394. /* set descriptor dynamic values */
  395. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  396. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  397. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  398. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  399. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  400. hidg_desc.desc[0].wDescriptorLength =
  401. cpu_to_le16(hidg->report_desc_length);
  402. hidg->set_report_buff = NULL;
  403. /* copy descriptors */
  404. f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
  405. if (!f->descriptors)
  406. goto fail;
  407. hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
  408. f->descriptors,
  409. &hidg_fs_in_ep_desc);
  410. if (gadget_is_dualspeed(c->cdev->gadget)) {
  411. hidg_hs_in_ep_desc.bEndpointAddress =
  412. hidg_fs_in_ep_desc.bEndpointAddress;
  413. f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
  414. if (!f->hs_descriptors)
  415. goto fail;
  416. hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
  417. f->hs_descriptors,
  418. &hidg_hs_in_ep_desc);
  419. } else {
  420. hidg->hs_in_ep_desc = NULL;
  421. }
  422. mutex_init(&hidg->lock);
  423. spin_lock_init(&hidg->spinlock);
  424. init_waitqueue_head(&hidg->write_queue);
  425. init_waitqueue_head(&hidg->read_queue);
  426. /* create char device */
  427. cdev_init(&hidg->cdev, &f_hidg_fops);
  428. dev = MKDEV(major, hidg->minor);
  429. status = cdev_add(&hidg->cdev, dev, 1);
  430. if (status)
  431. goto fail;
  432. device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
  433. return 0;
  434. fail:
  435. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  436. if (hidg->req != NULL) {
  437. kfree(hidg->req->buf);
  438. if (hidg->in_ep != NULL)
  439. usb_ep_free_request(hidg->in_ep, hidg->req);
  440. }
  441. usb_free_descriptors(f->hs_descriptors);
  442. usb_free_descriptors(f->descriptors);
  443. return status;
  444. }
  445. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  446. {
  447. struct f_hidg *hidg = func_to_hidg(f);
  448. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  449. cdev_del(&hidg->cdev);
  450. /* disable/free request and end point */
  451. usb_ep_disable(hidg->in_ep);
  452. usb_ep_dequeue(hidg->in_ep, hidg->req);
  453. kfree(hidg->req->buf);
  454. usb_ep_free_request(hidg->in_ep, hidg->req);
  455. /* free descriptors copies */
  456. usb_free_descriptors(f->hs_descriptors);
  457. usb_free_descriptors(f->descriptors);
  458. kfree(hidg->report_desc);
  459. kfree(hidg->set_report_buff);
  460. kfree(hidg);
  461. }
  462. /*-------------------------------------------------------------------------*/
  463. /* Strings */
  464. #define CT_FUNC_HID_IDX 0
  465. static struct usb_string ct_func_string_defs[] = {
  466. [CT_FUNC_HID_IDX].s = "HID Interface",
  467. {}, /* end of list */
  468. };
  469. static struct usb_gadget_strings ct_func_string_table = {
  470. .language = 0x0409, /* en-US */
  471. .strings = ct_func_string_defs,
  472. };
  473. static struct usb_gadget_strings *ct_func_strings[] = {
  474. &ct_func_string_table,
  475. NULL,
  476. };
  477. /*-------------------------------------------------------------------------*/
  478. /* usb_configuration */
  479. int __init hidg_bind_config(struct usb_configuration *c,
  480. struct hidg_func_descriptor *fdesc, int index)
  481. {
  482. struct f_hidg *hidg;
  483. int status;
  484. if (index >= minors)
  485. return -ENOENT;
  486. /* maybe allocate device-global string IDs, and patch descriptors */
  487. if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
  488. status = usb_string_id(c->cdev);
  489. if (status < 0)
  490. return status;
  491. ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
  492. hidg_interface_desc.iInterface = status;
  493. }
  494. /* allocate and initialize one new instance */
  495. hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
  496. if (!hidg)
  497. return -ENOMEM;
  498. hidg->minor = index;
  499. hidg->bInterfaceSubClass = fdesc->subclass;
  500. hidg->bInterfaceProtocol = fdesc->protocol;
  501. hidg->report_length = fdesc->report_length;
  502. hidg->report_desc_length = fdesc->report_desc_length;
  503. hidg->report_desc = kmemdup(fdesc->report_desc,
  504. fdesc->report_desc_length,
  505. GFP_KERNEL);
  506. if (!hidg->report_desc) {
  507. kfree(hidg);
  508. return -ENOMEM;
  509. }
  510. hidg->func.name = "hid";
  511. hidg->func.strings = ct_func_strings;
  512. hidg->func.bind = hidg_bind;
  513. hidg->func.unbind = hidg_unbind;
  514. hidg->func.set_alt = hidg_set_alt;
  515. hidg->func.disable = hidg_disable;
  516. hidg->func.setup = hidg_setup;
  517. status = usb_add_function(c, &hidg->func);
  518. if (status)
  519. kfree(hidg);
  520. return status;
  521. }
  522. int __init ghid_setup(struct usb_gadget *g, int count)
  523. {
  524. int status;
  525. dev_t dev;
  526. hidg_class = class_create(THIS_MODULE, "hidg");
  527. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  528. if (!status) {
  529. major = MAJOR(dev);
  530. minors = count;
  531. }
  532. return status;
  533. }
  534. void ghid_cleanup(void)
  535. {
  536. if (major) {
  537. unregister_chrdev_region(MKDEV(major, 0), minors);
  538. major = minors = 0;
  539. }
  540. class_destroy(hidg_class);
  541. hidg_class = NULL;
  542. }