hiddev.c 22 KB

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