hiddev.c 22 KB

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