hiddev.c 22 KB

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