hiddev.c 24 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/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. static struct usb_driver hiddev_driver;
  65. /*
  66. * Find a report, given the report's type and ID. The ID can be specified
  67. * indirectly by REPORT_ID_FIRST (which returns the first report of the given
  68. * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
  69. * given type which follows old_id.
  70. */
  71. static struct hid_report *
  72. hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
  73. {
  74. unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
  75. unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
  76. struct hid_report_enum *report_enum;
  77. struct hid_report *report;
  78. struct list_head *list;
  79. if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
  80. rinfo->report_type > HID_REPORT_TYPE_MAX)
  81. return NULL;
  82. report_enum = hid->report_enum +
  83. (rinfo->report_type - HID_REPORT_TYPE_MIN);
  84. switch (flags) {
  85. case 0: /* Nothing to do -- report_id is already set correctly */
  86. break;
  87. case HID_REPORT_ID_FIRST:
  88. if (list_empty(&report_enum->report_list))
  89. return NULL;
  90. list = report_enum->report_list.next;
  91. report = list_entry(list, struct hid_report, list);
  92. rinfo->report_id = report->id;
  93. break;
  94. case HID_REPORT_ID_NEXT:
  95. report = report_enum->report_id_hash[rid];
  96. if (!report)
  97. return NULL;
  98. list = report->list.next;
  99. if (list == &report_enum->report_list)
  100. return NULL;
  101. report = list_entry(list, struct hid_report, list);
  102. rinfo->report_id = report->id;
  103. break;
  104. default:
  105. return NULL;
  106. }
  107. return report_enum->report_id_hash[rinfo->report_id];
  108. }
  109. /*
  110. * Perform an exhaustive search of the report table for a usage, given its
  111. * type and usage id.
  112. */
  113. static struct hid_field *
  114. hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
  115. {
  116. int i, j;
  117. struct hid_report *report;
  118. struct hid_report_enum *report_enum;
  119. struct hid_field *field;
  120. if (uref->report_type < HID_REPORT_TYPE_MIN ||
  121. uref->report_type > HID_REPORT_TYPE_MAX)
  122. return NULL;
  123. report_enum = hid->report_enum +
  124. (uref->report_type - HID_REPORT_TYPE_MIN);
  125. list_for_each_entry(report, &report_enum->report_list, list) {
  126. for (i = 0; i < report->maxfield; i++) {
  127. field = report->field[i];
  128. for (j = 0; j < field->maxusage; j++) {
  129. if (field->usage[j].hid == uref->usage_code) {
  130. uref->report_id = report->id;
  131. uref->field_index = i;
  132. uref->usage_index = j;
  133. return field;
  134. }
  135. }
  136. }
  137. }
  138. return NULL;
  139. }
  140. static void hiddev_send_event(struct hid_device *hid,
  141. struct hiddev_usage_ref *uref)
  142. {
  143. struct hiddev *hiddev = hid->hiddev;
  144. struct hiddev_list *list;
  145. unsigned long flags;
  146. spin_lock_irqsave(&hiddev->list_lock, flags);
  147. list_for_each_entry(list, &hiddev->list, node) {
  148. if (uref->field_index != HID_FIELD_INDEX_NONE ||
  149. (list->flags & HIDDEV_FLAG_REPORT) != 0) {
  150. list->buffer[list->head] = *uref;
  151. list->head = (list->head + 1) &
  152. (HIDDEV_BUFFER_SIZE - 1);
  153. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  154. }
  155. }
  156. spin_unlock_irqrestore(&hiddev->list_lock, flags);
  157. wake_up_interruptible(&hiddev->wait);
  158. }
  159. /*
  160. * This is where hid.c calls into hiddev to pass an event that occurred over
  161. * the interrupt pipe
  162. */
  163. void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
  164. struct hid_usage *usage, __s32 value)
  165. {
  166. unsigned type = field->report_type;
  167. struct hiddev_usage_ref uref;
  168. uref.report_type =
  169. (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
  170. ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
  171. ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
  172. uref.report_id = field->report->id;
  173. uref.field_index = field->index;
  174. uref.usage_index = (usage - field->usage);
  175. uref.usage_code = usage->hid;
  176. uref.value = value;
  177. hiddev_send_event(hid, &uref);
  178. }
  179. EXPORT_SYMBOL_GPL(hiddev_hid_event);
  180. void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
  181. {
  182. unsigned type = report->type;
  183. struct hiddev_usage_ref uref;
  184. memset(&uref, 0, sizeof(uref));
  185. uref.report_type =
  186. (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
  187. ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
  188. ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
  189. uref.report_id = report->id;
  190. uref.field_index = HID_FIELD_INDEX_NONE;
  191. hiddev_send_event(hid, &uref);
  192. }
  193. /*
  194. * fasync file op
  195. */
  196. static int hiddev_fasync(int fd, struct file *file, int on)
  197. {
  198. struct hiddev_list *list = file->private_data;
  199. return fasync_helper(fd, file, on, &list->fasync);
  200. }
  201. /*
  202. * release file op
  203. */
  204. static int hiddev_release(struct inode * inode, struct file * file)
  205. {
  206. struct hiddev_list *list = file->private_data;
  207. unsigned long flags;
  208. spin_lock_irqsave(&list->hiddev->list_lock, flags);
  209. list_del(&list->node);
  210. spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
  211. if (!--list->hiddev->open) {
  212. if (list->hiddev->exist) {
  213. usbhid_close(list->hiddev->hid);
  214. usbhid_put_power(list->hiddev->hid);
  215. } else {
  216. kfree(list->hiddev);
  217. }
  218. }
  219. kfree(list);
  220. return 0;
  221. }
  222. /*
  223. * open file op
  224. */
  225. static int hiddev_open(struct inode *inode, struct file *file)
  226. {
  227. struct hiddev_list *list;
  228. struct usb_interface *intf;
  229. struct hid_device *hid;
  230. struct hiddev *hiddev;
  231. int res;
  232. intf = usbhid_find_interface(iminor(inode));
  233. if (!intf)
  234. return -ENODEV;
  235. hid = usb_get_intfdata(intf);
  236. hiddev = hid->hiddev;
  237. if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
  238. return -ENOMEM;
  239. mutex_init(&list->thread_lock);
  240. list->hiddev = hiddev;
  241. file->private_data = list;
  242. /*
  243. * no need for locking because the USB major number
  244. * is shared which usbcore guards against disconnect
  245. */
  246. if (list->hiddev->exist) {
  247. if (!list->hiddev->open++) {
  248. res = usbhid_open(hiddev->hid);
  249. if (res < 0) {
  250. res = -EIO;
  251. goto bail;
  252. }
  253. }
  254. } else {
  255. res = -ENODEV;
  256. goto bail;
  257. }
  258. spin_lock_irq(&list->hiddev->list_lock);
  259. list_add_tail(&list->node, &hiddev->list);
  260. spin_unlock_irq(&list->hiddev->list_lock);
  261. if (!list->hiddev->open++)
  262. if (list->hiddev->exist) {
  263. struct hid_device *hid = hiddev->hid;
  264. res = usbhid_get_power(hid);
  265. if (res < 0) {
  266. res = -EIO;
  267. goto bail;
  268. }
  269. usbhid_open(hid);
  270. }
  271. return 0;
  272. bail:
  273. file->private_data = NULL;
  274. kfree(list);
  275. return res;
  276. }
  277. /*
  278. * "write" file op
  279. */
  280. static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
  281. {
  282. return -EINVAL;
  283. }
  284. /*
  285. * "read" file op
  286. */
  287. static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
  288. {
  289. DEFINE_WAIT(wait);
  290. struct hiddev_list *list = file->private_data;
  291. int event_size;
  292. int retval;
  293. event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
  294. sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
  295. if (count < event_size)
  296. return 0;
  297. /* lock against other threads */
  298. retval = mutex_lock_interruptible(&list->thread_lock);
  299. if (retval)
  300. return -ERESTARTSYS;
  301. while (retval == 0) {
  302. if (list->head == list->tail) {
  303. prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
  304. while (list->head == list->tail) {
  305. if (file->f_flags & O_NONBLOCK) {
  306. retval = -EAGAIN;
  307. break;
  308. }
  309. if (signal_pending(current)) {
  310. retval = -ERESTARTSYS;
  311. break;
  312. }
  313. if (!list->hiddev->exist) {
  314. retval = -EIO;
  315. break;
  316. }
  317. /* let O_NONBLOCK tasks run */
  318. mutex_unlock(&list->thread_lock);
  319. schedule();
  320. if (mutex_lock_interruptible(&list->thread_lock))
  321. return -EINTR;
  322. set_current_state(TASK_INTERRUPTIBLE);
  323. }
  324. finish_wait(&list->hiddev->wait, &wait);
  325. }
  326. if (retval) {
  327. mutex_unlock(&list->thread_lock);
  328. return retval;
  329. }
  330. while (list->head != list->tail &&
  331. retval + event_size <= count) {
  332. if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
  333. if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
  334. struct hiddev_event event;
  335. event.hid = list->buffer[list->tail].usage_code;
  336. event.value = list->buffer[list->tail].value;
  337. if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
  338. mutex_unlock(&list->thread_lock);
  339. return -EFAULT;
  340. }
  341. retval += sizeof(struct hiddev_event);
  342. }
  343. } else {
  344. if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
  345. (list->flags & HIDDEV_FLAG_REPORT) != 0) {
  346. if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
  347. mutex_unlock(&list->thread_lock);
  348. return -EFAULT;
  349. }
  350. retval += sizeof(struct hiddev_usage_ref);
  351. }
  352. }
  353. list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
  354. }
  355. }
  356. mutex_unlock(&list->thread_lock);
  357. return retval;
  358. }
  359. /*
  360. * "poll" file op
  361. * No kernel lock - fine
  362. */
  363. static unsigned int hiddev_poll(struct file *file, poll_table *wait)
  364. {
  365. struct hiddev_list *list = file->private_data;
  366. poll_wait(file, &list->hiddev->wait, wait);
  367. if (list->head != list->tail)
  368. return POLLIN | POLLRDNORM;
  369. if (!list->hiddev->exist)
  370. return POLLERR | POLLHUP;
  371. return 0;
  372. }
  373. /*
  374. * "ioctl" file op
  375. */
  376. static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
  377. {
  378. struct hid_device *hid = hiddev->hid;
  379. struct hiddev_report_info rinfo;
  380. struct hiddev_usage_ref_multi *uref_multi = NULL;
  381. struct hiddev_usage_ref *uref;
  382. struct hid_report *report;
  383. struct hid_field *field;
  384. int i;
  385. uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
  386. if (!uref_multi)
  387. return -ENOMEM;
  388. uref = &uref_multi->uref;
  389. if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
  390. if (copy_from_user(uref_multi, user_arg,
  391. sizeof(*uref_multi)))
  392. goto fault;
  393. } else {
  394. if (copy_from_user(uref, user_arg, sizeof(*uref)))
  395. goto fault;
  396. }
  397. switch (cmd) {
  398. case HIDIOCGUCODE:
  399. rinfo.report_type = uref->report_type;
  400. rinfo.report_id = uref->report_id;
  401. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  402. goto inval;
  403. if (uref->field_index >= report->maxfield)
  404. goto inval;
  405. field = report->field[uref->field_index];
  406. if (uref->usage_index >= field->maxusage)
  407. goto inval;
  408. uref->usage_code = field->usage[uref->usage_index].hid;
  409. if (copy_to_user(user_arg, uref, sizeof(*uref)))
  410. goto fault;
  411. goto goodreturn;
  412. default:
  413. if (cmd != HIDIOCGUSAGE &&
  414. cmd != HIDIOCGUSAGES &&
  415. uref->report_type == HID_REPORT_TYPE_INPUT)
  416. goto inval;
  417. if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
  418. field = hiddev_lookup_usage(hid, uref);
  419. if (field == NULL)
  420. goto inval;
  421. } else {
  422. rinfo.report_type = uref->report_type;
  423. rinfo.report_id = uref->report_id;
  424. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  425. goto inval;
  426. if (uref->field_index >= report->maxfield)
  427. goto inval;
  428. field = report->field[uref->field_index];
  429. if (cmd == HIDIOCGCOLLECTIONINDEX) {
  430. if (uref->usage_index >= field->maxusage)
  431. goto inval;
  432. } else if (uref->usage_index >= field->report_count)
  433. goto inval;
  434. else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
  435. (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
  436. uref->usage_index + uref_multi->num_values > field->report_count))
  437. goto inval;
  438. }
  439. switch (cmd) {
  440. case HIDIOCGUSAGE:
  441. uref->value = field->value[uref->usage_index];
  442. if (copy_to_user(user_arg, uref, sizeof(*uref)))
  443. goto fault;
  444. goto goodreturn;
  445. case HIDIOCSUSAGE:
  446. field->value[uref->usage_index] = uref->value;
  447. goto goodreturn;
  448. case HIDIOCGCOLLECTIONINDEX:
  449. i = field->usage[uref->usage_index].collection_index;
  450. kfree(uref_multi);
  451. return i;
  452. case HIDIOCGUSAGES:
  453. for (i = 0; i < uref_multi->num_values; i++)
  454. uref_multi->values[i] =
  455. field->value[uref->usage_index + i];
  456. if (copy_to_user(user_arg, uref_multi,
  457. sizeof(*uref_multi)))
  458. goto fault;
  459. goto goodreturn;
  460. case HIDIOCSUSAGES:
  461. for (i = 0; i < uref_multi->num_values; i++)
  462. field->value[uref->usage_index + i] =
  463. uref_multi->values[i];
  464. goto goodreturn;
  465. }
  466. goodreturn:
  467. kfree(uref_multi);
  468. return 0;
  469. fault:
  470. kfree(uref_multi);
  471. return -EFAULT;
  472. inval:
  473. kfree(uref_multi);
  474. return -EINVAL;
  475. }
  476. }
  477. static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
  478. {
  479. struct hid_device *hid = hiddev->hid;
  480. struct usb_device *dev = hid_to_usb_dev(hid);
  481. int idx, len;
  482. char *buf;
  483. if (get_user(idx, (int __user *)user_arg))
  484. return -EFAULT;
  485. if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
  486. return -ENOMEM;
  487. if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
  488. kfree(buf);
  489. return -EINVAL;
  490. }
  491. if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
  492. kfree(buf);
  493. return -EFAULT;
  494. }
  495. kfree(buf);
  496. return len;
  497. }
  498. static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  499. {
  500. struct hiddev_list *list = file->private_data;
  501. struct hiddev *hiddev = list->hiddev;
  502. struct hid_device *hid = hiddev->hid;
  503. struct usb_device *dev;
  504. struct hiddev_collection_info cinfo;
  505. struct hiddev_report_info rinfo;
  506. struct hiddev_field_info finfo;
  507. struct hiddev_devinfo dinfo;
  508. struct hid_report *report;
  509. struct hid_field *field;
  510. struct usbhid_device *usbhid = hid->driver_data;
  511. void __user *user_arg = (void __user *)arg;
  512. int i, r;
  513. /* Called without BKL by compat methods so no BKL taken */
  514. /* FIXME: Who or what stop this racing with a disconnect ?? */
  515. if (!hiddev->exist || !hid)
  516. return -EIO;
  517. dev = hid_to_usb_dev(hid);
  518. switch (cmd) {
  519. case HIDIOCGVERSION:
  520. return put_user(HID_VERSION, (int __user *)arg);
  521. case HIDIOCAPPLICATION:
  522. if (arg < 0 || arg >= hid->maxapplication)
  523. return -EINVAL;
  524. for (i = 0; i < hid->maxcollection; i++)
  525. if (hid->collection[i].type ==
  526. HID_COLLECTION_APPLICATION && arg-- == 0)
  527. break;
  528. if (i == hid->maxcollection)
  529. return -EINVAL;
  530. return hid->collection[i].usage;
  531. case HIDIOCGDEVINFO:
  532. dinfo.bustype = BUS_USB;
  533. dinfo.busnum = dev->bus->busnum;
  534. dinfo.devnum = dev->devnum;
  535. dinfo.ifnum = usbhid->ifnum;
  536. dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
  537. dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
  538. dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
  539. dinfo.num_applications = hid->maxapplication;
  540. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  541. return -EFAULT;
  542. return 0;
  543. case HIDIOCGFLAG:
  544. if (put_user(list->flags, (int __user *)arg))
  545. return -EFAULT;
  546. return 0;
  547. case HIDIOCSFLAG:
  548. {
  549. int newflags;
  550. if (get_user(newflags, (int __user *)arg))
  551. return -EFAULT;
  552. if ((newflags & ~HIDDEV_FLAGS) != 0 ||
  553. ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
  554. (newflags & HIDDEV_FLAG_UREF) == 0))
  555. return -EINVAL;
  556. list->flags = newflags;
  557. return 0;
  558. }
  559. case HIDIOCGSTRING:
  560. mutex_lock(&hiddev->existancelock);
  561. if (hiddev->exist)
  562. r = hiddev_ioctl_string(hiddev, cmd, user_arg);
  563. else
  564. r = -ENODEV;
  565. mutex_unlock(&hiddev->existancelock);
  566. return r;
  567. case HIDIOCINITREPORT:
  568. mutex_lock(&hiddev->existancelock);
  569. if (!hiddev->exist) {
  570. mutex_unlock(&hiddev->existancelock);
  571. return -ENODEV;
  572. }
  573. usbhid_init_reports(hid);
  574. mutex_unlock(&hiddev->existancelock);
  575. return 0;
  576. case HIDIOCGREPORT:
  577. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  578. return -EFAULT;
  579. if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
  580. return -EINVAL;
  581. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  582. return -EINVAL;
  583. mutex_lock(&hiddev->existancelock);
  584. if (hiddev->exist) {
  585. usbhid_submit_report(hid, report, USB_DIR_IN);
  586. usbhid_wait_io(hid);
  587. }
  588. mutex_unlock(&hiddev->existancelock);
  589. return 0;
  590. case HIDIOCSREPORT:
  591. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  592. return -EFAULT;
  593. if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
  594. return -EINVAL;
  595. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  596. return -EINVAL;
  597. mutex_lock(&hiddev->existancelock);
  598. if (hiddev->exist) {
  599. usbhid_submit_report(hid, report, USB_DIR_OUT);
  600. usbhid_wait_io(hid);
  601. }
  602. mutex_unlock(&hiddev->existancelock);
  603. return 0;
  604. case HIDIOCGREPORTINFO:
  605. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  606. return -EFAULT;
  607. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  608. return -EINVAL;
  609. rinfo.num_fields = report->maxfield;
  610. if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
  611. return -EFAULT;
  612. return 0;
  613. case HIDIOCGFIELDINFO:
  614. if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
  615. return -EFAULT;
  616. rinfo.report_type = finfo.report_type;
  617. rinfo.report_id = finfo.report_id;
  618. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  619. return -EINVAL;
  620. if (finfo.field_index >= report->maxfield)
  621. return -EINVAL;
  622. field = report->field[finfo.field_index];
  623. memset(&finfo, 0, sizeof(finfo));
  624. finfo.report_type = rinfo.report_type;
  625. finfo.report_id = rinfo.report_id;
  626. finfo.field_index = field->report_count - 1;
  627. finfo.maxusage = field->maxusage;
  628. finfo.flags = field->flags;
  629. finfo.physical = field->physical;
  630. finfo.logical = field->logical;
  631. finfo.application = field->application;
  632. finfo.logical_minimum = field->logical_minimum;
  633. finfo.logical_maximum = field->logical_maximum;
  634. finfo.physical_minimum = field->physical_minimum;
  635. finfo.physical_maximum = field->physical_maximum;
  636. finfo.unit_exponent = field->unit_exponent;
  637. finfo.unit = field->unit;
  638. if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
  639. return -EFAULT;
  640. return 0;
  641. case HIDIOCGUCODE:
  642. /* fall through */
  643. case HIDIOCGUSAGE:
  644. case HIDIOCSUSAGE:
  645. case HIDIOCGUSAGES:
  646. case HIDIOCSUSAGES:
  647. case HIDIOCGCOLLECTIONINDEX:
  648. mutex_lock(&hiddev->existancelock);
  649. if (hiddev->exist)
  650. r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
  651. else
  652. r = -ENODEV;
  653. mutex_unlock(&hiddev->existancelock);
  654. return r;
  655. case HIDIOCGCOLLECTIONINFO:
  656. if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
  657. return -EFAULT;
  658. if (cinfo.index >= hid->maxcollection)
  659. return -EINVAL;
  660. cinfo.type = hid->collection[cinfo.index].type;
  661. cinfo.usage = hid->collection[cinfo.index].usage;
  662. cinfo.level = hid->collection[cinfo.index].level;
  663. if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
  664. return -EFAULT;
  665. return 0;
  666. default:
  667. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
  668. return -EINVAL;
  669. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
  670. int len;
  671. if (!hid->name)
  672. return 0;
  673. len = strlen(hid->name) + 1;
  674. if (len > _IOC_SIZE(cmd))
  675. len = _IOC_SIZE(cmd);
  676. return copy_to_user(user_arg, hid->name, len) ?
  677. -EFAULT : len;
  678. }
  679. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
  680. int len;
  681. if (!hid->phys)
  682. return 0;
  683. len = strlen(hid->phys) + 1;
  684. if (len > _IOC_SIZE(cmd))
  685. len = _IOC_SIZE(cmd);
  686. return copy_to_user(user_arg, hid->phys, len) ?
  687. -EFAULT : len;
  688. }
  689. }
  690. return -EINVAL;
  691. }
  692. #ifdef CONFIG_COMPAT
  693. static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  694. {
  695. return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  696. }
  697. #endif
  698. static const struct file_operations hiddev_fops = {
  699. .owner = THIS_MODULE,
  700. .read = hiddev_read,
  701. .write = hiddev_write,
  702. .poll = hiddev_poll,
  703. .open = hiddev_open,
  704. .release = hiddev_release,
  705. .unlocked_ioctl = hiddev_ioctl,
  706. .fasync = hiddev_fasync,
  707. #ifdef CONFIG_COMPAT
  708. .compat_ioctl = hiddev_compat_ioctl,
  709. #endif
  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. }
  777. /* Currently this driver is a USB driver. It's not a conventional one in
  778. * the sense that it doesn't probe at the USB level. Instead it waits to
  779. * be connected by HID through the hiddev_connect / hiddev_disconnect
  780. * routines. The reason to register as a USB device is to gain part of the
  781. * minor number space from the USB major.
  782. *
  783. * In theory, should the HID code be generalized to more than one physical
  784. * medium (say, IEEE 1384), this driver will probably need to register its
  785. * own major number, and in doing so, no longer need to register with USB.
  786. * At that point the probe routine and hiddev_driver struct below will no
  787. * longer be useful.
  788. */
  789. /* We never attach in this manner, and rely on HID to connect us. This
  790. * is why there is no disconnect routine defined in the usb_driver either.
  791. */
  792. static int hiddev_usbd_probe(struct usb_interface *intf,
  793. const struct usb_device_id *hiddev_info)
  794. {
  795. return -ENODEV;
  796. }
  797. static /* const */ struct usb_driver hiddev_driver = {
  798. .name = "hiddev",
  799. .probe = hiddev_usbd_probe,
  800. };
  801. int __init hiddev_init(void)
  802. {
  803. return usb_register(&hiddev_driver);
  804. }
  805. void hiddev_exit(void)
  806. {
  807. usb_deregister(&hiddev_driver);
  808. }