hiddev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * Copyright (c) 2001 Paul Stewart
  3. * Copyright (c) 2001 Vojtech Pavlik
  4. *
  5. * HID char devices, giving access to raw HID device events.
  6. *
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Should you need to contact me, the author, you can do so either by
  24. * e-mail - mail your message to Paul Stewart <stewart@wetlogic.net>
  25. */
  26. #include <linux/poll.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/input.h>
  32. #include <linux/usb.h>
  33. #include <linux/hid.h>
  34. #include <linux/hiddev.h>
  35. #include "usbhid.h"
  36. #ifdef CONFIG_USB_DYNAMIC_MINORS
  37. #define HIDDEV_MINOR_BASE 0
  38. #define HIDDEV_MINORS 256
  39. #else
  40. #define HIDDEV_MINOR_BASE 96
  41. #define HIDDEV_MINORS 16
  42. #endif
  43. #define HIDDEV_BUFFER_SIZE 64
  44. struct hiddev {
  45. int exist;
  46. int open;
  47. wait_queue_head_t wait;
  48. struct hid_device *hid;
  49. struct list_head list;
  50. };
  51. struct hiddev_list {
  52. struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
  53. int head;
  54. int tail;
  55. unsigned flags;
  56. struct fasync_struct *fasync;
  57. struct hiddev *hiddev;
  58. struct list_head node;
  59. };
  60. static struct hiddev *hiddev_table[HIDDEV_MINORS];
  61. /*
  62. * Find a report, given the report's type and ID. The ID can be specified
  63. * indirectly by REPORT_ID_FIRST (which returns the first report of the given
  64. * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
  65. * given type which follows old_id.
  66. */
  67. static struct hid_report *
  68. hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
  69. {
  70. unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
  71. unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
  72. struct hid_report_enum *report_enum;
  73. struct hid_report *report;
  74. struct list_head *list;
  75. if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
  76. rinfo->report_type > HID_REPORT_TYPE_MAX)
  77. return NULL;
  78. report_enum = hid->report_enum +
  79. (rinfo->report_type - HID_REPORT_TYPE_MIN);
  80. switch (flags) {
  81. case 0: /* Nothing to do -- report_id is already set correctly */
  82. break;
  83. case HID_REPORT_ID_FIRST:
  84. if (list_empty(&report_enum->report_list))
  85. return NULL;
  86. list = report_enum->report_list.next;
  87. report = list_entry(list, struct hid_report, list);
  88. rinfo->report_id = report->id;
  89. break;
  90. case HID_REPORT_ID_NEXT:
  91. report = report_enum->report_id_hash[rid];
  92. if (!report)
  93. return NULL;
  94. list = report->list.next;
  95. if (list == &report_enum->report_list)
  96. return NULL;
  97. report = list_entry(list, struct hid_report, list);
  98. rinfo->report_id = report->id;
  99. break;
  100. default:
  101. return NULL;
  102. }
  103. return report_enum->report_id_hash[rinfo->report_id];
  104. }
  105. /*
  106. * Perform an exhaustive search of the report table for a usage, given its
  107. * type and usage id.
  108. */
  109. static struct hid_field *
  110. hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
  111. {
  112. int i, j;
  113. struct hid_report *report;
  114. struct hid_report_enum *report_enum;
  115. struct hid_field *field;
  116. if (uref->report_type < HID_REPORT_TYPE_MIN ||
  117. uref->report_type > HID_REPORT_TYPE_MAX)
  118. return NULL;
  119. report_enum = hid->report_enum +
  120. (uref->report_type - HID_REPORT_TYPE_MIN);
  121. list_for_each_entry(report, &report_enum->report_list, list) {
  122. for (i = 0; i < report->maxfield; i++) {
  123. field = report->field[i];
  124. for (j = 0; j < field->maxusage; j++) {
  125. if (field->usage[j].hid == uref->usage_code) {
  126. uref->report_id = report->id;
  127. uref->field_index = i;
  128. uref->usage_index = j;
  129. return field;
  130. }
  131. }
  132. }
  133. }
  134. return NULL;
  135. }
  136. static void hiddev_send_event(struct hid_device *hid,
  137. struct hiddev_usage_ref *uref)
  138. {
  139. struct hiddev *hiddev = hid->hiddev;
  140. struct hiddev_list *list;
  141. list_for_each_entry(list, &hiddev->list, node) {
  142. if (uref->field_index != HID_FIELD_INDEX_NONE ||
  143. (list->flags & HIDDEV_FLAG_REPORT) != 0) {
  144. list->buffer[list->head] = *uref;
  145. list->head = (list->head + 1) &
  146. (HIDDEV_BUFFER_SIZE - 1);
  147. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  148. }
  149. }
  150. wake_up_interruptible(&hiddev->wait);
  151. }
  152. /*
  153. * This is where hid.c calls into hiddev to pass an event that occurred over
  154. * the interrupt pipe
  155. */
  156. void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
  157. struct hid_usage *usage, __s32 value)
  158. {
  159. unsigned type = field->report_type;
  160. struct hiddev_usage_ref uref;
  161. uref.report_type =
  162. (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
  163. ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
  164. ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
  165. uref.report_id = field->report->id;
  166. uref.field_index = field->index;
  167. uref.usage_index = (usage - field->usage);
  168. uref.usage_code = usage->hid;
  169. uref.value = value;
  170. hiddev_send_event(hid, &uref);
  171. }
  172. EXPORT_SYMBOL_GPL(hiddev_hid_event);
  173. void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
  174. {
  175. unsigned type = report->type;
  176. struct hiddev_usage_ref uref;
  177. memset(&uref, 0, sizeof(uref));
  178. uref.report_type =
  179. (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
  180. ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
  181. ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
  182. uref.report_id = report->id;
  183. uref.field_index = HID_FIELD_INDEX_NONE;
  184. hiddev_send_event(hid, &uref);
  185. }
  186. /*
  187. * fasync file op
  188. */
  189. static int hiddev_fasync(int fd, struct file *file, int on)
  190. {
  191. int retval;
  192. struct hiddev_list *list = file->private_data;
  193. retval = fasync_helper(fd, file, on, &list->fasync);
  194. return retval < 0 ? retval : 0;
  195. }
  196. /*
  197. * release file op
  198. */
  199. static int hiddev_release(struct inode * inode, struct file * file)
  200. {
  201. struct hiddev_list *list = file->private_data;
  202. hiddev_fasync(-1, file, 0);
  203. list_del(&list->node);
  204. if (!--list->hiddev->open) {
  205. if (list->hiddev->exist)
  206. usbhid_close(list->hiddev->hid);
  207. else
  208. kfree(list->hiddev);
  209. }
  210. kfree(list);
  211. return 0;
  212. }
  213. /*
  214. * open file op
  215. */
  216. static int hiddev_open(struct inode *inode, struct file *file)
  217. {
  218. struct hiddev_list *list;
  219. int i = iminor(inode) - HIDDEV_MINOR_BASE;
  220. if (i >= HIDDEV_MINORS || !hiddev_table[i])
  221. return -ENODEV;
  222. if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
  223. return -ENOMEM;
  224. list->hiddev = hiddev_table[i];
  225. list_add_tail(&list->node, &hiddev_table[i]->list);
  226. file->private_data = list;
  227. if (!list->hiddev->open++)
  228. if (list->hiddev->exist)
  229. usbhid_open(hiddev_table[i]->hid);
  230. return 0;
  231. }
  232. /*
  233. * "write" file op
  234. */
  235. static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
  236. {
  237. return -EINVAL;
  238. }
  239. /*
  240. * "read" file op
  241. */
  242. static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
  243. {
  244. DECLARE_WAITQUEUE(wait, current);
  245. struct hiddev_list *list = file->private_data;
  246. int event_size;
  247. int retval = 0;
  248. event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
  249. sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
  250. if (count < event_size)
  251. return 0;
  252. while (retval == 0) {
  253. if (list->head == list->tail) {
  254. add_wait_queue(&list->hiddev->wait, &wait);
  255. set_current_state(TASK_INTERRUPTIBLE);
  256. while (list->head == list->tail) {
  257. if (file->f_flags & O_NONBLOCK) {
  258. retval = -EAGAIN;
  259. break;
  260. }
  261. if (signal_pending(current)) {
  262. retval = -ERESTARTSYS;
  263. break;
  264. }
  265. if (!list->hiddev->exist) {
  266. retval = -EIO;
  267. break;
  268. }
  269. schedule();
  270. set_current_state(TASK_INTERRUPTIBLE);
  271. }
  272. set_current_state(TASK_RUNNING);
  273. remove_wait_queue(&list->hiddev->wait, &wait);
  274. }
  275. if (retval)
  276. return retval;
  277. while (list->head != list->tail &&
  278. retval + event_size <= count) {
  279. if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
  280. if (list->buffer[list->tail].field_index !=
  281. HID_FIELD_INDEX_NONE) {
  282. struct hiddev_event event;
  283. event.hid = list->buffer[list->tail].usage_code;
  284. event.value = list->buffer[list->tail].value;
  285. if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
  286. return -EFAULT;
  287. retval += sizeof(struct hiddev_event);
  288. }
  289. } else {
  290. if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
  291. (list->flags & HIDDEV_FLAG_REPORT) != 0) {
  292. if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
  293. return -EFAULT;
  294. retval += sizeof(struct hiddev_usage_ref);
  295. }
  296. }
  297. list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
  298. }
  299. }
  300. return retval;
  301. }
  302. /*
  303. * "poll" file op
  304. * No kernel lock - fine
  305. */
  306. static unsigned int hiddev_poll(struct file *file, poll_table *wait)
  307. {
  308. struct hiddev_list *list = file->private_data;
  309. poll_wait(file, &list->hiddev->wait, wait);
  310. if (list->head != list->tail)
  311. return POLLIN | POLLRDNORM;
  312. if (!list->hiddev->exist)
  313. return POLLERR | POLLHUP;
  314. return 0;
  315. }
  316. /*
  317. * "ioctl" file op
  318. */
  319. static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  320. {
  321. struct hiddev_list *list = file->private_data;
  322. struct hiddev *hiddev = list->hiddev;
  323. struct hid_device *hid = hiddev->hid;
  324. struct usb_device *dev = hid_to_usb_dev(hid);
  325. struct hiddev_collection_info cinfo;
  326. struct hiddev_report_info rinfo;
  327. struct hiddev_field_info finfo;
  328. struct hiddev_usage_ref_multi *uref_multi = NULL;
  329. struct hiddev_usage_ref *uref;
  330. struct hiddev_devinfo dinfo;
  331. struct hid_report *report;
  332. struct hid_field *field;
  333. struct usbhid_device *usbhid = hid->driver_data;
  334. void __user *user_arg = (void __user *)arg;
  335. int i;
  336. if (!hiddev->exist)
  337. return -EIO;
  338. switch (cmd) {
  339. case HIDIOCGVERSION:
  340. return put_user(HID_VERSION, (int __user *)arg);
  341. case HIDIOCAPPLICATION:
  342. if (arg < 0 || arg >= hid->maxapplication)
  343. return -EINVAL;
  344. for (i = 0; i < hid->maxcollection; i++)
  345. if (hid->collection[i].type ==
  346. HID_COLLECTION_APPLICATION && arg-- == 0)
  347. break;
  348. if (i == hid->maxcollection)
  349. return -EINVAL;
  350. return hid->collection[i].usage;
  351. case HIDIOCGDEVINFO:
  352. dinfo.bustype = BUS_USB;
  353. dinfo.busnum = dev->bus->busnum;
  354. dinfo.devnum = dev->devnum;
  355. dinfo.ifnum = usbhid->ifnum;
  356. dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
  357. dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
  358. dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
  359. dinfo.num_applications = hid->maxapplication;
  360. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  361. return -EFAULT;
  362. return 0;
  363. case HIDIOCGFLAG:
  364. if (put_user(list->flags, (int __user *)arg))
  365. return -EFAULT;
  366. return 0;
  367. case HIDIOCSFLAG:
  368. {
  369. int newflags;
  370. if (get_user(newflags, (int __user *)arg))
  371. return -EFAULT;
  372. if ((newflags & ~HIDDEV_FLAGS) != 0 ||
  373. ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
  374. (newflags & HIDDEV_FLAG_UREF) == 0))
  375. return -EINVAL;
  376. list->flags = newflags;
  377. return 0;
  378. }
  379. case HIDIOCGSTRING:
  380. {
  381. int idx, len;
  382. char *buf;
  383. if (get_user(idx, (int __user *)arg))
  384. return -EFAULT;
  385. if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
  386. return -ENOMEM;
  387. if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
  388. kfree(buf);
  389. return -EINVAL;
  390. }
  391. if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
  392. kfree(buf);
  393. return -EFAULT;
  394. }
  395. kfree(buf);
  396. return len;
  397. }
  398. case HIDIOCINITREPORT:
  399. usbhid_init_reports(hid);
  400. return 0;
  401. case HIDIOCGREPORT:
  402. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  403. return -EFAULT;
  404. if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
  405. return -EINVAL;
  406. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  407. return -EINVAL;
  408. usbhid_submit_report(hid, report, USB_DIR_IN);
  409. usbhid_wait_io(hid);
  410. return 0;
  411. case HIDIOCSREPORT:
  412. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  413. return -EFAULT;
  414. if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
  415. return -EINVAL;
  416. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  417. return -EINVAL;
  418. usbhid_submit_report(hid, report, USB_DIR_OUT);
  419. usbhid_wait_io(hid);
  420. return 0;
  421. case HIDIOCGREPORTINFO:
  422. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  423. return -EFAULT;
  424. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  425. return -EINVAL;
  426. rinfo.num_fields = report->maxfield;
  427. if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
  428. return -EFAULT;
  429. return 0;
  430. case HIDIOCGFIELDINFO:
  431. if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
  432. return -EFAULT;
  433. rinfo.report_type = finfo.report_type;
  434. rinfo.report_id = finfo.report_id;
  435. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  436. return -EINVAL;
  437. if (finfo.field_index >= report->maxfield)
  438. return -EINVAL;
  439. field = report->field[finfo.field_index];
  440. memset(&finfo, 0, sizeof(finfo));
  441. finfo.report_type = rinfo.report_type;
  442. finfo.report_id = rinfo.report_id;
  443. finfo.field_index = field->report_count - 1;
  444. finfo.maxusage = field->maxusage;
  445. finfo.flags = field->flags;
  446. finfo.physical = field->physical;
  447. finfo.logical = field->logical;
  448. finfo.application = field->application;
  449. finfo.logical_minimum = field->logical_minimum;
  450. finfo.logical_maximum = field->logical_maximum;
  451. finfo.physical_minimum = field->physical_minimum;
  452. finfo.physical_maximum = field->physical_maximum;
  453. finfo.unit_exponent = field->unit_exponent;
  454. finfo.unit = field->unit;
  455. if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
  456. return -EFAULT;
  457. return 0;
  458. case HIDIOCGUCODE:
  459. uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
  460. if (!uref_multi)
  461. return -ENOMEM;
  462. uref = &uref_multi->uref;
  463. if (copy_from_user(uref, user_arg, sizeof(*uref)))
  464. goto fault;
  465. rinfo.report_type = uref->report_type;
  466. rinfo.report_id = uref->report_id;
  467. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  468. goto inval;
  469. if (uref->field_index >= report->maxfield)
  470. goto inval;
  471. field = report->field[uref->field_index];
  472. if (uref->usage_index >= field->maxusage)
  473. goto inval;
  474. uref->usage_code = field->usage[uref->usage_index].hid;
  475. if (copy_to_user(user_arg, uref, sizeof(*uref)))
  476. goto fault;
  477. kfree(uref_multi);
  478. return 0;
  479. case HIDIOCGUSAGE:
  480. case HIDIOCSUSAGE:
  481. case HIDIOCGUSAGES:
  482. case HIDIOCSUSAGES:
  483. case HIDIOCGCOLLECTIONINDEX:
  484. uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
  485. if (!uref_multi)
  486. return -ENOMEM;
  487. uref = &uref_multi->uref;
  488. if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
  489. if (copy_from_user(uref_multi, user_arg,
  490. sizeof(*uref_multi)))
  491. goto fault;
  492. } else {
  493. if (copy_from_user(uref, user_arg, sizeof(*uref)))
  494. goto fault;
  495. }
  496. if (cmd != HIDIOCGUSAGE &&
  497. cmd != HIDIOCGUSAGES &&
  498. uref->report_type == HID_REPORT_TYPE_INPUT)
  499. goto inval;
  500. if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
  501. field = hiddev_lookup_usage(hid, uref);
  502. if (field == NULL)
  503. goto inval;
  504. } else {
  505. rinfo.report_type = uref->report_type;
  506. rinfo.report_id = uref->report_id;
  507. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  508. goto inval;
  509. if (uref->field_index >= report->maxfield)
  510. goto inval;
  511. field = report->field[uref->field_index];
  512. if (cmd == HIDIOCGCOLLECTIONINDEX) {
  513. if (uref->usage_index >= field->maxusage)
  514. goto inval;
  515. } else if (uref->usage_index >= field->report_count)
  516. goto inval;
  517. else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
  518. (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
  519. uref->usage_index + uref_multi->num_values > field->report_count))
  520. goto inval;
  521. }
  522. switch (cmd) {
  523. case HIDIOCGUSAGE:
  524. uref->value = field->value[uref->usage_index];
  525. if (copy_to_user(user_arg, uref, sizeof(*uref)))
  526. goto fault;
  527. goto goodreturn;
  528. case HIDIOCSUSAGE:
  529. field->value[uref->usage_index] = uref->value;
  530. goto goodreturn;
  531. case HIDIOCGCOLLECTIONINDEX:
  532. kfree(uref_multi);
  533. return field->usage[uref->usage_index].collection_index;
  534. case HIDIOCGUSAGES:
  535. for (i = 0; i < uref_multi->num_values; i++)
  536. uref_multi->values[i] =
  537. field->value[uref->usage_index + i];
  538. if (copy_to_user(user_arg, uref_multi,
  539. sizeof(*uref_multi)))
  540. goto fault;
  541. goto goodreturn;
  542. case HIDIOCSUSAGES:
  543. for (i = 0; i < uref_multi->num_values; i++)
  544. field->value[uref->usage_index + i] =
  545. uref_multi->values[i];
  546. goto goodreturn;
  547. }
  548. goodreturn:
  549. kfree(uref_multi);
  550. return 0;
  551. fault:
  552. kfree(uref_multi);
  553. return -EFAULT;
  554. inval:
  555. kfree(uref_multi);
  556. return -EINVAL;
  557. case HIDIOCGCOLLECTIONINFO:
  558. if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
  559. return -EFAULT;
  560. if (cinfo.index >= hid->maxcollection)
  561. return -EINVAL;
  562. cinfo.type = hid->collection[cinfo.index].type;
  563. cinfo.usage = hid->collection[cinfo.index].usage;
  564. cinfo.level = hid->collection[cinfo.index].level;
  565. if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
  566. return -EFAULT;
  567. return 0;
  568. default:
  569. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
  570. return -EINVAL;
  571. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
  572. int len;
  573. if (!hid->name)
  574. return 0;
  575. len = strlen(hid->name) + 1;
  576. if (len > _IOC_SIZE(cmd))
  577. len = _IOC_SIZE(cmd);
  578. return copy_to_user(user_arg, hid->name, len) ?
  579. -EFAULT : len;
  580. }
  581. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
  582. int len;
  583. if (!hid->phys)
  584. return 0;
  585. len = strlen(hid->phys) + 1;
  586. if (len > _IOC_SIZE(cmd))
  587. len = _IOC_SIZE(cmd);
  588. return copy_to_user(user_arg, hid->phys, len) ?
  589. -EFAULT : len;
  590. }
  591. }
  592. return -EINVAL;
  593. }
  594. static const struct file_operations hiddev_fops = {
  595. .owner = THIS_MODULE,
  596. .read = hiddev_read,
  597. .write = hiddev_write,
  598. .poll = hiddev_poll,
  599. .open = hiddev_open,
  600. .release = hiddev_release,
  601. .ioctl = hiddev_ioctl,
  602. .fasync = hiddev_fasync,
  603. };
  604. static struct usb_class_driver hiddev_class = {
  605. .name = "hiddev%d",
  606. .fops = &hiddev_fops,
  607. .minor_base = HIDDEV_MINOR_BASE,
  608. };
  609. /*
  610. * This is where hid.c calls us to connect a hid device to the hiddev driver
  611. */
  612. int hiddev_connect(struct hid_device *hid)
  613. {
  614. struct hiddev *hiddev;
  615. struct usbhid_device *usbhid = hid->driver_data;
  616. int i;
  617. int retval;
  618. for (i = 0; i < hid->maxcollection; i++)
  619. if (hid->collection[i].type ==
  620. HID_COLLECTION_APPLICATION &&
  621. !IS_INPUT_APPLICATION(hid->collection[i].usage))
  622. break;
  623. if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
  624. return -1;
  625. if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
  626. return -1;
  627. retval = usb_register_dev(usbhid->intf, &hiddev_class);
  628. if (retval) {
  629. err("Not able to get a minor for this device.");
  630. kfree(hiddev);
  631. return -1;
  632. }
  633. init_waitqueue_head(&hiddev->wait);
  634. INIT_LIST_HEAD(&hiddev->list);
  635. hiddev->hid = hid;
  636. hiddev->exist = 1;
  637. hid->minor = usbhid->intf->minor;
  638. hid->hiddev = hiddev;
  639. hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
  640. return 0;
  641. }
  642. /*
  643. * This is where hid.c calls us to disconnect a hiddev device from the
  644. * corresponding hid device (usually because the usb device has disconnected)
  645. */
  646. static struct usb_class_driver hiddev_class;
  647. void hiddev_disconnect(struct hid_device *hid)
  648. {
  649. struct hiddev *hiddev = hid->hiddev;
  650. struct usbhid_device *usbhid = hid->driver_data;
  651. hiddev->exist = 0;
  652. hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
  653. usb_deregister_dev(usbhid->intf, &hiddev_class);
  654. if (hiddev->open) {
  655. usbhid_close(hiddev->hid);
  656. wake_up_interruptible(&hiddev->wait);
  657. } else {
  658. kfree(hiddev);
  659. }
  660. }
  661. /* Currently this driver is a USB driver. It's not a conventional one in
  662. * the sense that it doesn't probe at the USB level. Instead it waits to
  663. * be connected by HID through the hiddev_connect / hiddev_disconnect
  664. * routines. The reason to register as a USB device is to gain part of the
  665. * minor number space from the USB major.
  666. *
  667. * In theory, should the HID code be generalized to more than one physical
  668. * medium (say, IEEE 1384), this driver will probably need to register its
  669. * own major number, and in doing so, no longer need to register with USB.
  670. * At that point the probe routine and hiddev_driver struct below will no
  671. * longer be useful.
  672. */
  673. /* We never attach in this manner, and rely on HID to connect us. This
  674. * is why there is no disconnect routine defined in the usb_driver either.
  675. */
  676. static int hiddev_usbd_probe(struct usb_interface *intf,
  677. const struct usb_device_id *hiddev_info)
  678. {
  679. return -ENODEV;
  680. }
  681. static /* const */ struct usb_driver hiddev_driver = {
  682. .name = "hiddev",
  683. .probe = hiddev_usbd_probe,
  684. };
  685. int __init hiddev_init(void)
  686. {
  687. return usb_register(&hiddev_driver);
  688. }
  689. void hiddev_exit(void)
  690. {
  691. usb_deregister(&hiddev_driver);
  692. }