hiddev.c 23 KB

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