f_hid.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. };
  56. static inline struct f_hidg *func_to_hidg(struct usb_function *f)
  57. {
  58. return container_of(f, struct f_hidg, func);
  59. }
  60. /*-------------------------------------------------------------------------*/
  61. /* Static descriptors */
  62. static struct usb_interface_descriptor hidg_interface_desc = {
  63. .bLength = sizeof hidg_interface_desc,
  64. .bDescriptorType = USB_DT_INTERFACE,
  65. /* .bInterfaceNumber = DYNAMIC */
  66. .bAlternateSetting = 0,
  67. .bNumEndpoints = 1,
  68. .bInterfaceClass = USB_CLASS_HID,
  69. /* .bInterfaceSubClass = DYNAMIC */
  70. /* .bInterfaceProtocol = DYNAMIC */
  71. /* .iInterface = DYNAMIC */
  72. };
  73. static struct hid_descriptor hidg_desc = {
  74. .bLength = sizeof hidg_desc,
  75. .bDescriptorType = HID_DT_HID,
  76. .bcdHID = 0x0101,
  77. .bCountryCode = 0x00,
  78. .bNumDescriptors = 0x1,
  79. /*.desc[0].bDescriptorType = DYNAMIC */
  80. /*.desc[0].wDescriptorLenght = DYNAMIC */
  81. };
  82. /* High-Speed Support */
  83. static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
  84. .bLength = USB_DT_ENDPOINT_SIZE,
  85. .bDescriptorType = USB_DT_ENDPOINT,
  86. .bEndpointAddress = USB_DIR_IN,
  87. .bmAttributes = USB_ENDPOINT_XFER_INT,
  88. /*.wMaxPacketSize = DYNAMIC */
  89. .bInterval = 4, /* FIXME: Add this field in the
  90. * HID gadget configuration?
  91. * (struct hidg_func_descriptor)
  92. */
  93. };
  94. static struct usb_descriptor_header *hidg_hs_descriptors[] = {
  95. (struct usb_descriptor_header *)&hidg_interface_desc,
  96. (struct usb_descriptor_header *)&hidg_desc,
  97. (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
  98. NULL,
  99. };
  100. /* Full-Speed Support */
  101. static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
  102. .bLength = USB_DT_ENDPOINT_SIZE,
  103. .bDescriptorType = USB_DT_ENDPOINT,
  104. .bEndpointAddress = USB_DIR_IN,
  105. .bmAttributes = USB_ENDPOINT_XFER_INT,
  106. /*.wMaxPacketSize = DYNAMIC */
  107. .bInterval = 10, /* FIXME: Add this field in the
  108. * HID gadget configuration?
  109. * (struct hidg_func_descriptor)
  110. */
  111. };
  112. static struct usb_descriptor_header *hidg_fs_descriptors[] = {
  113. (struct usb_descriptor_header *)&hidg_interface_desc,
  114. (struct usb_descriptor_header *)&hidg_desc,
  115. (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
  116. NULL,
  117. };
  118. /*-------------------------------------------------------------------------*/
  119. /* Char Device */
  120. static ssize_t f_hidg_read(struct file *file, char __user *buffer,
  121. size_t count, loff_t *ptr)
  122. {
  123. struct f_hidg *hidg = file->private_data;
  124. char *tmp_buff = NULL;
  125. unsigned long flags;
  126. if (!count)
  127. return 0;
  128. if (!access_ok(VERIFY_WRITE, buffer, count))
  129. return -EFAULT;
  130. spin_lock_irqsave(&hidg->spinlock, flags);
  131. #define READ_COND (hidg->set_report_buff != NULL)
  132. while (!READ_COND) {
  133. spin_unlock_irqrestore(&hidg->spinlock, flags);
  134. if (file->f_flags & O_NONBLOCK)
  135. return -EAGAIN;
  136. if (wait_event_interruptible(hidg->read_queue, READ_COND))
  137. return -ERESTARTSYS;
  138. spin_lock_irqsave(&hidg->spinlock, flags);
  139. }
  140. count = min_t(unsigned, count, hidg->set_report_length);
  141. tmp_buff = hidg->set_report_buff;
  142. hidg->set_report_buff = NULL;
  143. spin_unlock_irqrestore(&hidg->spinlock, flags);
  144. if (tmp_buff != NULL) {
  145. /* copy to user outside spinlock */
  146. count -= copy_to_user(buffer, tmp_buff, count);
  147. kfree(tmp_buff);
  148. } else
  149. count = -ENOMEM;
  150. return count;
  151. }
  152. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  153. {
  154. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  155. if (req->status != 0) {
  156. ERROR(hidg->func.config->cdev,
  157. "End Point Request ERROR: %d\n", req->status);
  158. }
  159. hidg->write_pending = 0;
  160. wake_up(&hidg->write_queue);
  161. }
  162. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  163. size_t count, loff_t *offp)
  164. {
  165. struct f_hidg *hidg = file->private_data;
  166. ssize_t status = -ENOMEM;
  167. if (!access_ok(VERIFY_READ, buffer, count))
  168. return -EFAULT;
  169. mutex_lock(&hidg->lock);
  170. #define WRITE_COND (!hidg->write_pending)
  171. /* write queue */
  172. while (!WRITE_COND) {
  173. mutex_unlock(&hidg->lock);
  174. if (file->f_flags & O_NONBLOCK)
  175. return -EAGAIN;
  176. if (wait_event_interruptible_exclusive(
  177. hidg->write_queue, WRITE_COND))
  178. return -ERESTARTSYS;
  179. mutex_lock(&hidg->lock);
  180. }
  181. count = min_t(unsigned, count, hidg->report_length);
  182. status = copy_from_user(hidg->req->buf, buffer, count);
  183. if (status != 0) {
  184. ERROR(hidg->func.config->cdev,
  185. "copy_from_user error\n");
  186. mutex_unlock(&hidg->lock);
  187. return -EINVAL;
  188. }
  189. hidg->req->status = 0;
  190. hidg->req->zero = 0;
  191. hidg->req->length = count;
  192. hidg->req->complete = f_hidg_req_complete;
  193. hidg->req->context = hidg;
  194. hidg->write_pending = 1;
  195. status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
  196. if (status < 0) {
  197. ERROR(hidg->func.config->cdev,
  198. "usb_ep_queue error on int endpoint %zd\n", status);
  199. hidg->write_pending = 0;
  200. wake_up(&hidg->write_queue);
  201. } else {
  202. status = count;
  203. }
  204. mutex_unlock(&hidg->lock);
  205. return status;
  206. }
  207. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  208. {
  209. struct f_hidg *hidg = file->private_data;
  210. unsigned int ret = 0;
  211. poll_wait(file, &hidg->read_queue, wait);
  212. poll_wait(file, &hidg->write_queue, wait);
  213. if (WRITE_COND)
  214. ret |= POLLOUT | POLLWRNORM;
  215. if (READ_COND)
  216. ret |= POLLIN | POLLRDNORM;
  217. return ret;
  218. }
  219. #undef WRITE_COND
  220. #undef READ_COND
  221. static int f_hidg_release(struct inode *inode, struct file *fd)
  222. {
  223. fd->private_data = NULL;
  224. return 0;
  225. }
  226. static int f_hidg_open(struct inode *inode, struct file *fd)
  227. {
  228. struct f_hidg *hidg =
  229. container_of(inode->i_cdev, struct f_hidg, cdev);
  230. fd->private_data = hidg;
  231. return 0;
  232. }
  233. /*-------------------------------------------------------------------------*/
  234. /* usb_function */
  235. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  236. {
  237. struct f_hidg *hidg = (struct f_hidg *)req->context;
  238. if (req->status != 0 || req->buf == NULL || req->actual == 0) {
  239. ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__);
  240. return;
  241. }
  242. spin_lock(&hidg->spinlock);
  243. hidg->set_report_buff = krealloc(hidg->set_report_buff,
  244. req->actual, GFP_ATOMIC);
  245. if (hidg->set_report_buff == NULL) {
  246. spin_unlock(&hidg->spinlock);
  247. return;
  248. }
  249. hidg->set_report_length = req->actual;
  250. memcpy(hidg->set_report_buff, req->buf, req->actual);
  251. spin_unlock(&hidg->spinlock);
  252. wake_up(&hidg->read_queue);
  253. }
  254. static int hidg_setup(struct usb_function *f,
  255. const struct usb_ctrlrequest *ctrl)
  256. {
  257. struct f_hidg *hidg = func_to_hidg(f);
  258. struct usb_composite_dev *cdev = f->config->cdev;
  259. struct usb_request *req = cdev->req;
  260. int status = 0;
  261. __u16 value, length;
  262. value = __le16_to_cpu(ctrl->wValue);
  263. length = __le16_to_cpu(ctrl->wLength);
  264. VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
  265. "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
  266. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  267. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  268. | HID_REQ_GET_REPORT):
  269. VDBG(cdev, "get_report\n");
  270. /* send an empty report */
  271. length = min_t(unsigned, length, hidg->report_length);
  272. memset(req->buf, 0x0, length);
  273. goto respond;
  274. break;
  275. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  276. | HID_REQ_GET_PROTOCOL):
  277. VDBG(cdev, "get_protocol\n");
  278. goto stall;
  279. break;
  280. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  281. | HID_REQ_SET_REPORT):
  282. VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
  283. req->context = hidg;
  284. req->complete = hidg_set_report_complete;
  285. goto respond;
  286. break;
  287. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  288. | HID_REQ_SET_PROTOCOL):
  289. VDBG(cdev, "set_protocol\n");
  290. goto stall;
  291. break;
  292. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  293. | USB_REQ_GET_DESCRIPTOR):
  294. switch (value >> 8) {
  295. case HID_DT_HID:
  296. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  297. length = min_t(unsigned short, length,
  298. hidg_desc.bLength);
  299. memcpy(req->buf, &hidg_desc, length);
  300. goto respond;
  301. break;
  302. case HID_DT_REPORT:
  303. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  304. length = min_t(unsigned short, length,
  305. hidg->report_desc_length);
  306. memcpy(req->buf, hidg->report_desc, length);
  307. goto respond;
  308. break;
  309. default:
  310. VDBG(cdev, "Unknown decriptor request 0x%x\n",
  311. value >> 8);
  312. goto stall;
  313. break;
  314. }
  315. break;
  316. default:
  317. VDBG(cdev, "Unknown request 0x%x\n",
  318. ctrl->bRequest);
  319. goto stall;
  320. break;
  321. }
  322. stall:
  323. return -EOPNOTSUPP;
  324. respond:
  325. req->zero = 0;
  326. req->length = length;
  327. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  328. if (status < 0)
  329. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  330. return status;
  331. }
  332. static void hidg_disable(struct usb_function *f)
  333. {
  334. struct f_hidg *hidg = func_to_hidg(f);
  335. usb_ep_disable(hidg->in_ep);
  336. hidg->in_ep->driver_data = NULL;
  337. }
  338. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  339. {
  340. struct usb_composite_dev *cdev = f->config->cdev;
  341. struct f_hidg *hidg = func_to_hidg(f);
  342. int status = 0;
  343. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
  344. if (hidg->in_ep != NULL) {
  345. /* restart endpoint */
  346. if (hidg->in_ep->driver_data != NULL)
  347. usb_ep_disable(hidg->in_ep);
  348. status = config_ep_by_speed(f->config->cdev->gadget, f,
  349. hidg->in_ep);
  350. if (status) {
  351. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  352. goto fail;
  353. }
  354. status = usb_ep_enable(hidg->in_ep);
  355. if (status < 0) {
  356. ERROR(cdev, "Enable endpoint FAILED!\n");
  357. goto fail;
  358. }
  359. hidg->in_ep->driver_data = hidg;
  360. }
  361. fail:
  362. return status;
  363. }
  364. const struct file_operations f_hidg_fops = {
  365. .owner = THIS_MODULE,
  366. .open = f_hidg_open,
  367. .release = f_hidg_release,
  368. .write = f_hidg_write,
  369. .read = f_hidg_read,
  370. .poll = f_hidg_poll,
  371. .llseek = noop_llseek,
  372. };
  373. static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
  374. {
  375. struct usb_ep *ep;
  376. struct f_hidg *hidg = func_to_hidg(f);
  377. int status;
  378. dev_t dev;
  379. /* allocate instance-specific interface IDs, and patch descriptors */
  380. status = usb_interface_id(c, f);
  381. if (status < 0)
  382. goto fail;
  383. hidg_interface_desc.bInterfaceNumber = status;
  384. /* allocate instance-specific endpoints */
  385. status = -ENODEV;
  386. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  387. if (!ep)
  388. goto fail;
  389. ep->driver_data = c->cdev; /* claim */
  390. hidg->in_ep = ep;
  391. /* preallocate request and buffer */
  392. status = -ENOMEM;
  393. hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
  394. if (!hidg->req)
  395. goto fail;
  396. hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
  397. if (!hidg->req->buf)
  398. goto fail;
  399. /* set descriptor dynamic values */
  400. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  401. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  402. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  403. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  404. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  405. hidg_desc.desc[0].wDescriptorLength =
  406. cpu_to_le16(hidg->report_desc_length);
  407. hidg->set_report_buff = NULL;
  408. /* copy descriptors */
  409. f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
  410. if (!f->descriptors)
  411. goto fail;
  412. if (gadget_is_dualspeed(c->cdev->gadget)) {
  413. hidg_hs_in_ep_desc.bEndpointAddress =
  414. hidg_fs_in_ep_desc.bEndpointAddress;
  415. f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
  416. if (!f->hs_descriptors)
  417. goto fail;
  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. }