hiddev.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. uref->usage_index =
  450. array_index_nospec(uref->usage_index,
  451. field->maxusage);
  452. } else if (uref->usage_index >= field->report_count)
  453. goto inval;
  454. }
  455. if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
  456. if (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
  457. uref->usage_index + uref_multi->num_values >
  458. field->report_count)
  459. goto inval;
  460. uref->usage_index =
  461. array_index_nospec(uref->usage_index,
  462. field->report_count -
  463. uref_multi->num_values);
  464. }
  465. switch (cmd) {
  466. case HIDIOCGUSAGE:
  467. uref->value = field->value[uref->usage_index];
  468. if (copy_to_user(user_arg, uref, sizeof(*uref)))
  469. goto fault;
  470. goto goodreturn;
  471. case HIDIOCSUSAGE:
  472. field->value[uref->usage_index] = uref->value;
  473. goto goodreturn;
  474. case HIDIOCGCOLLECTIONINDEX:
  475. i = field->usage[uref->usage_index].collection_index;
  476. kfree(uref_multi);
  477. return i;
  478. case HIDIOCGUSAGES:
  479. for (i = 0; i < uref_multi->num_values; i++)
  480. uref_multi->values[i] =
  481. field->value[uref->usage_index + i];
  482. if (copy_to_user(user_arg, uref_multi,
  483. sizeof(*uref_multi)))
  484. goto fault;
  485. goto goodreturn;
  486. case HIDIOCSUSAGES:
  487. for (i = 0; i < uref_multi->num_values; i++)
  488. field->value[uref->usage_index + i] =
  489. uref_multi->values[i];
  490. goto goodreturn;
  491. }
  492. goodreturn:
  493. kfree(uref_multi);
  494. return 0;
  495. fault:
  496. kfree(uref_multi);
  497. return -EFAULT;
  498. inval:
  499. kfree(uref_multi);
  500. return -EINVAL;
  501. }
  502. }
  503. static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
  504. {
  505. struct hid_device *hid = hiddev->hid;
  506. struct usb_device *dev = hid_to_usb_dev(hid);
  507. int idx, len;
  508. char *buf;
  509. if (get_user(idx, (int __user *)user_arg))
  510. return -EFAULT;
  511. if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
  512. return -ENOMEM;
  513. if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
  514. kfree(buf);
  515. return -EINVAL;
  516. }
  517. if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
  518. kfree(buf);
  519. return -EFAULT;
  520. }
  521. kfree(buf);
  522. return len;
  523. }
  524. static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  525. {
  526. struct hiddev_list *list = file->private_data;
  527. struct hiddev *hiddev = list->hiddev;
  528. struct hid_device *hid;
  529. struct hiddev_collection_info cinfo;
  530. struct hiddev_report_info rinfo;
  531. struct hiddev_field_info finfo;
  532. struct hiddev_devinfo dinfo;
  533. struct hid_report *report;
  534. struct hid_field *field;
  535. void __user *user_arg = (void __user *)arg;
  536. int i, r = -EINVAL;
  537. /* Called without BKL by compat methods so no BKL taken */
  538. mutex_lock(&hiddev->existancelock);
  539. if (!hiddev->exist) {
  540. r = -ENODEV;
  541. goto ret_unlock;
  542. }
  543. hid = hiddev->hid;
  544. switch (cmd) {
  545. case HIDIOCGVERSION:
  546. r = put_user(HID_VERSION, (int __user *)arg) ?
  547. -EFAULT : 0;
  548. break;
  549. case HIDIOCAPPLICATION:
  550. if (arg >= hid->maxapplication)
  551. break;
  552. for (i = 0; i < hid->maxcollection; i++)
  553. if (hid->collection[i].type ==
  554. HID_COLLECTION_APPLICATION && arg-- == 0)
  555. break;
  556. if (i < hid->maxcollection)
  557. r = hid->collection[i].usage;
  558. break;
  559. case HIDIOCGDEVINFO:
  560. {
  561. struct usb_device *dev = hid_to_usb_dev(hid);
  562. struct usbhid_device *usbhid = hid->driver_data;
  563. memset(&dinfo, 0, sizeof(dinfo));
  564. dinfo.bustype = BUS_USB;
  565. dinfo.busnum = dev->bus->busnum;
  566. dinfo.devnum = dev->devnum;
  567. dinfo.ifnum = usbhid->ifnum;
  568. dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
  569. dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
  570. dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
  571. dinfo.num_applications = hid->maxapplication;
  572. r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
  573. -EFAULT : 0;
  574. break;
  575. }
  576. case HIDIOCGFLAG:
  577. r = put_user(list->flags, (int __user *)arg) ?
  578. -EFAULT : 0;
  579. break;
  580. case HIDIOCSFLAG:
  581. {
  582. int newflags;
  583. if (get_user(newflags, (int __user *)arg)) {
  584. r = -EFAULT;
  585. break;
  586. }
  587. if ((newflags & ~HIDDEV_FLAGS) != 0 ||
  588. ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
  589. (newflags & HIDDEV_FLAG_UREF) == 0))
  590. break;
  591. list->flags = newflags;
  592. r = 0;
  593. break;
  594. }
  595. case HIDIOCGSTRING:
  596. r = hiddev_ioctl_string(hiddev, cmd, user_arg);
  597. break;
  598. case HIDIOCINITREPORT:
  599. usbhid_init_reports(hid);
  600. r = 0;
  601. break;
  602. case HIDIOCGREPORT:
  603. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
  604. r = -EFAULT;
  605. break;
  606. }
  607. if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
  608. break;
  609. report = hiddev_lookup_report(hid, &rinfo);
  610. if (report == NULL)
  611. break;
  612. hid_hw_request(hid, report, HID_REQ_GET_REPORT);
  613. hid_hw_wait(hid);
  614. r = 0;
  615. break;
  616. case HIDIOCSREPORT:
  617. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
  618. r = -EFAULT;
  619. break;
  620. }
  621. if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
  622. break;
  623. report = hiddev_lookup_report(hid, &rinfo);
  624. if (report == NULL)
  625. break;
  626. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  627. hid_hw_wait(hid);
  628. r = 0;
  629. break;
  630. case HIDIOCGREPORTINFO:
  631. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
  632. r = -EFAULT;
  633. break;
  634. }
  635. report = hiddev_lookup_report(hid, &rinfo);
  636. if (report == NULL)
  637. break;
  638. rinfo.num_fields = report->maxfield;
  639. r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
  640. -EFAULT : 0;
  641. break;
  642. case HIDIOCGFIELDINFO:
  643. if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
  644. r = -EFAULT;
  645. break;
  646. }
  647. rinfo.report_type = finfo.report_type;
  648. rinfo.report_id = finfo.report_id;
  649. report = hiddev_lookup_report(hid, &rinfo);
  650. if (report == NULL)
  651. break;
  652. if (finfo.field_index >= report->maxfield)
  653. break;
  654. finfo.field_index = array_index_nospec(finfo.field_index,
  655. report->maxfield);
  656. field = report->field[finfo.field_index];
  657. memset(&finfo, 0, sizeof(finfo));
  658. finfo.report_type = rinfo.report_type;
  659. finfo.report_id = rinfo.report_id;
  660. finfo.field_index = field->report_count - 1;
  661. finfo.maxusage = field->maxusage;
  662. finfo.flags = field->flags;
  663. finfo.physical = field->physical;
  664. finfo.logical = field->logical;
  665. finfo.application = field->application;
  666. finfo.logical_minimum = field->logical_minimum;
  667. finfo.logical_maximum = field->logical_maximum;
  668. finfo.physical_minimum = field->physical_minimum;
  669. finfo.physical_maximum = field->physical_maximum;
  670. finfo.unit_exponent = field->unit_exponent;
  671. finfo.unit = field->unit;
  672. r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
  673. -EFAULT : 0;
  674. break;
  675. case HIDIOCGUCODE:
  676. /* fall through */
  677. case HIDIOCGUSAGE:
  678. case HIDIOCSUSAGE:
  679. case HIDIOCGUSAGES:
  680. case HIDIOCSUSAGES:
  681. case HIDIOCGCOLLECTIONINDEX:
  682. r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
  683. break;
  684. case HIDIOCGCOLLECTIONINFO:
  685. if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
  686. r = -EFAULT;
  687. break;
  688. }
  689. if (cinfo.index >= hid->maxcollection)
  690. break;
  691. cinfo.index = array_index_nospec(cinfo.index,
  692. hid->maxcollection);
  693. cinfo.type = hid->collection[cinfo.index].type;
  694. cinfo.usage = hid->collection[cinfo.index].usage;
  695. cinfo.level = hid->collection[cinfo.index].level;
  696. r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
  697. -EFAULT : 0;
  698. break;
  699. default:
  700. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
  701. break;
  702. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
  703. int len = strlen(hid->name) + 1;
  704. if (len > _IOC_SIZE(cmd))
  705. len = _IOC_SIZE(cmd);
  706. r = copy_to_user(user_arg, hid->name, len) ?
  707. -EFAULT : len;
  708. break;
  709. }
  710. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
  711. int len = strlen(hid->phys) + 1;
  712. if (len > _IOC_SIZE(cmd))
  713. len = _IOC_SIZE(cmd);
  714. r = copy_to_user(user_arg, hid->phys, len) ?
  715. -EFAULT : len;
  716. break;
  717. }
  718. }
  719. ret_unlock:
  720. mutex_unlock(&hiddev->existancelock);
  721. return r;
  722. }
  723. #ifdef CONFIG_COMPAT
  724. static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  725. {
  726. return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  727. }
  728. #endif
  729. static const struct file_operations hiddev_fops = {
  730. .owner = THIS_MODULE,
  731. .read = hiddev_read,
  732. .write = hiddev_write,
  733. .poll = hiddev_poll,
  734. .open = hiddev_open,
  735. .release = hiddev_release,
  736. .unlocked_ioctl = hiddev_ioctl,
  737. .fasync = hiddev_fasync,
  738. #ifdef CONFIG_COMPAT
  739. .compat_ioctl = hiddev_compat_ioctl,
  740. #endif
  741. .llseek = noop_llseek,
  742. };
  743. static char *hiddev_devnode(struct device *dev, umode_t *mode)
  744. {
  745. return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
  746. }
  747. static struct usb_class_driver hiddev_class = {
  748. .name = "hiddev%d",
  749. .devnode = hiddev_devnode,
  750. .fops = &hiddev_fops,
  751. .minor_base = HIDDEV_MINOR_BASE,
  752. };
  753. /*
  754. * This is where hid.c calls us to connect a hid device to the hiddev driver
  755. */
  756. int hiddev_connect(struct hid_device *hid, unsigned int force)
  757. {
  758. struct hiddev *hiddev;
  759. struct usbhid_device *usbhid = hid->driver_data;
  760. int retval;
  761. if (!force) {
  762. unsigned int i;
  763. for (i = 0; i < hid->maxcollection; i++)
  764. if (hid->collection[i].type ==
  765. HID_COLLECTION_APPLICATION &&
  766. !IS_INPUT_APPLICATION(hid->collection[i].usage))
  767. break;
  768. if (i == hid->maxcollection)
  769. return -1;
  770. }
  771. if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
  772. return -1;
  773. init_waitqueue_head(&hiddev->wait);
  774. INIT_LIST_HEAD(&hiddev->list);
  775. spin_lock_init(&hiddev->list_lock);
  776. mutex_init(&hiddev->existancelock);
  777. hid->hiddev = hiddev;
  778. hiddev->hid = hid;
  779. hiddev->exist = 1;
  780. retval = usb_register_dev(usbhid->intf, &hiddev_class);
  781. if (retval) {
  782. hid_err(hid, "Not able to get a minor for this device\n");
  783. hid->hiddev = NULL;
  784. kfree(hiddev);
  785. return -1;
  786. }
  787. return 0;
  788. }
  789. /*
  790. * This is where hid.c calls us to disconnect a hiddev device from the
  791. * corresponding hid device (usually because the usb device has disconnected)
  792. */
  793. static struct usb_class_driver hiddev_class;
  794. void hiddev_disconnect(struct hid_device *hid)
  795. {
  796. struct hiddev *hiddev = hid->hiddev;
  797. struct usbhid_device *usbhid = hid->driver_data;
  798. usb_deregister_dev(usbhid->intf, &hiddev_class);
  799. mutex_lock(&hiddev->existancelock);
  800. hiddev->exist = 0;
  801. if (hiddev->open) {
  802. mutex_unlock(&hiddev->existancelock);
  803. usbhid_close(hiddev->hid);
  804. wake_up_interruptible(&hiddev->wait);
  805. } else {
  806. mutex_unlock(&hiddev->existancelock);
  807. kfree(hiddev);
  808. }
  809. }