hiddev.c 22 KB

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