hiddev.c 23 KB

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