hiddev.c 21 KB

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