hiddev.c 21 KB

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