hiddev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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 64
  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 hiddev *hiddev_table[HIDDEV_MINORS];
  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. int res;
  229. int i = iminor(inode) - HIDDEV_MINOR_BASE;
  230. if (i >= HIDDEV_MINORS || i < 0 || !hiddev_table[i])
  231. return -ENODEV;
  232. if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
  233. return -ENOMEM;
  234. mutex_init(&list->thread_lock);
  235. list->hiddev = hiddev_table[i];
  236. file->private_data = list;
  237. /*
  238. * no need for locking because the USB major number
  239. * is shared which usbcore guards against disconnect
  240. */
  241. if (list->hiddev->exist) {
  242. if (!list->hiddev->open++) {
  243. res = usbhid_open(hiddev_table[i]->hid);
  244. if (res < 0) {
  245. res = -EIO;
  246. goto bail;
  247. }
  248. }
  249. } else {
  250. res = -ENODEV;
  251. goto bail;
  252. }
  253. spin_lock_irq(&list->hiddev->list_lock);
  254. list_add_tail(&list->node, &hiddev_table[i]->list);
  255. spin_unlock_irq(&list->hiddev->list_lock);
  256. if (!list->hiddev->open++)
  257. if (list->hiddev->exist) {
  258. struct hid_device *hid = hiddev_table[i]->hid;
  259. res = usbhid_get_power(hid);
  260. if (res < 0) {
  261. res = -EIO;
  262. goto bail;
  263. }
  264. usbhid_open(hid);
  265. }
  266. return 0;
  267. bail:
  268. file->private_data = NULL;
  269. kfree(list);
  270. return res;
  271. }
  272. /*
  273. * "write" file op
  274. */
  275. static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
  276. {
  277. return -EINVAL;
  278. }
  279. /*
  280. * "read" file op
  281. */
  282. static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
  283. {
  284. DEFINE_WAIT(wait);
  285. struct hiddev_list *list = file->private_data;
  286. int event_size;
  287. int retval;
  288. event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
  289. sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
  290. if (count < event_size)
  291. return 0;
  292. /* lock against other threads */
  293. retval = mutex_lock_interruptible(&list->thread_lock);
  294. if (retval)
  295. return -ERESTARTSYS;
  296. while (retval == 0) {
  297. if (list->head == list->tail) {
  298. prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
  299. while (list->head == list->tail) {
  300. if (file->f_flags & O_NONBLOCK) {
  301. retval = -EAGAIN;
  302. break;
  303. }
  304. if (signal_pending(current)) {
  305. retval = -ERESTARTSYS;
  306. break;
  307. }
  308. if (!list->hiddev->exist) {
  309. retval = -EIO;
  310. break;
  311. }
  312. /* let O_NONBLOCK tasks run */
  313. mutex_unlock(&list->thread_lock);
  314. schedule();
  315. if (mutex_lock_interruptible(&list->thread_lock))
  316. return -EINTR;
  317. set_current_state(TASK_INTERRUPTIBLE);
  318. }
  319. finish_wait(&list->hiddev->wait, &wait);
  320. }
  321. if (retval) {
  322. mutex_unlock(&list->thread_lock);
  323. return retval;
  324. }
  325. while (list->head != list->tail &&
  326. retval + event_size <= count) {
  327. if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
  328. if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
  329. struct hiddev_event event;
  330. event.hid = list->buffer[list->tail].usage_code;
  331. event.value = list->buffer[list->tail].value;
  332. if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
  333. mutex_unlock(&list->thread_lock);
  334. return -EFAULT;
  335. }
  336. retval += sizeof(struct hiddev_event);
  337. }
  338. } else {
  339. if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
  340. (list->flags & HIDDEV_FLAG_REPORT) != 0) {
  341. if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
  342. mutex_unlock(&list->thread_lock);
  343. return -EFAULT;
  344. }
  345. retval += sizeof(struct hiddev_usage_ref);
  346. }
  347. }
  348. list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
  349. }
  350. }
  351. mutex_unlock(&list->thread_lock);
  352. return retval;
  353. }
  354. /*
  355. * "poll" file op
  356. * No kernel lock - fine
  357. */
  358. static unsigned int hiddev_poll(struct file *file, poll_table *wait)
  359. {
  360. struct hiddev_list *list = file->private_data;
  361. poll_wait(file, &list->hiddev->wait, wait);
  362. if (list->head != list->tail)
  363. return POLLIN | POLLRDNORM;
  364. if (!list->hiddev->exist)
  365. return POLLERR | POLLHUP;
  366. return 0;
  367. }
  368. /*
  369. * "ioctl" file op
  370. */
  371. static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
  372. {
  373. struct hid_device *hid = hiddev->hid;
  374. struct hiddev_report_info rinfo;
  375. struct hiddev_usage_ref_multi *uref_multi = NULL;
  376. struct hiddev_usage_ref *uref;
  377. struct hid_report *report;
  378. struct hid_field *field;
  379. int i;
  380. uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
  381. if (!uref_multi)
  382. return -ENOMEM;
  383. lock_kernel();
  384. uref = &uref_multi->uref;
  385. if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
  386. if (copy_from_user(uref_multi, user_arg,
  387. sizeof(*uref_multi)))
  388. goto fault;
  389. } else {
  390. if (copy_from_user(uref, user_arg, sizeof(*uref)))
  391. goto fault;
  392. }
  393. switch (cmd) {
  394. case HIDIOCGUCODE:
  395. rinfo.report_type = uref->report_type;
  396. rinfo.report_id = uref->report_id;
  397. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  398. goto inval;
  399. if (uref->field_index >= report->maxfield)
  400. goto inval;
  401. field = report->field[uref->field_index];
  402. if (uref->usage_index >= field->maxusage)
  403. goto inval;
  404. uref->usage_code = field->usage[uref->usage_index].hid;
  405. if (copy_to_user(user_arg, uref, sizeof(*uref)))
  406. goto fault;
  407. goto goodreturn;
  408. default:
  409. if (cmd != HIDIOCGUSAGE &&
  410. cmd != HIDIOCGUSAGES &&
  411. uref->report_type == HID_REPORT_TYPE_INPUT)
  412. goto inval;
  413. if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
  414. field = hiddev_lookup_usage(hid, uref);
  415. if (field == NULL)
  416. goto inval;
  417. } else {
  418. rinfo.report_type = uref->report_type;
  419. rinfo.report_id = uref->report_id;
  420. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  421. goto inval;
  422. if (uref->field_index >= report->maxfield)
  423. goto inval;
  424. field = report->field[uref->field_index];
  425. if (cmd == HIDIOCGCOLLECTIONINDEX) {
  426. if (uref->usage_index >= field->maxusage)
  427. goto inval;
  428. } else if (uref->usage_index >= field->report_count)
  429. goto inval;
  430. else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
  431. (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
  432. uref->usage_index + uref_multi->num_values > field->report_count))
  433. goto inval;
  434. }
  435. switch (cmd) {
  436. case HIDIOCGUSAGE:
  437. uref->value = field->value[uref->usage_index];
  438. if (copy_to_user(user_arg, uref, sizeof(*uref)))
  439. goto fault;
  440. goto goodreturn;
  441. case HIDIOCSUSAGE:
  442. field->value[uref->usage_index] = uref->value;
  443. goto goodreturn;
  444. case HIDIOCGCOLLECTIONINDEX:
  445. kfree(uref_multi);
  446. return field->usage[uref->usage_index].collection_index;
  447. case HIDIOCGUSAGES:
  448. for (i = 0; i < uref_multi->num_values; i++)
  449. uref_multi->values[i] =
  450. field->value[uref->usage_index + i];
  451. if (copy_to_user(user_arg, uref_multi,
  452. sizeof(*uref_multi)))
  453. goto fault;
  454. goto goodreturn;
  455. case HIDIOCSUSAGES:
  456. for (i = 0; i < uref_multi->num_values; i++)
  457. field->value[uref->usage_index + i] =
  458. uref_multi->values[i];
  459. goto goodreturn;
  460. }
  461. goodreturn:
  462. unlock_kernel();
  463. kfree(uref_multi);
  464. return 0;
  465. fault:
  466. unlock_kernel();
  467. kfree(uref_multi);
  468. return -EFAULT;
  469. inval:
  470. unlock_kernel();
  471. kfree(uref_multi);
  472. return -EINVAL;
  473. }
  474. }
  475. static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
  476. {
  477. struct hid_device *hid = hiddev->hid;
  478. struct usb_device *dev = hid_to_usb_dev(hid);
  479. int idx, len;
  480. char *buf;
  481. if (get_user(idx, (int __user *)user_arg))
  482. return -EFAULT;
  483. if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
  484. return -ENOMEM;
  485. if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
  486. kfree(buf);
  487. return -EINVAL;
  488. }
  489. if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
  490. kfree(buf);
  491. return -EFAULT;
  492. }
  493. kfree(buf);
  494. return len;
  495. }
  496. static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  497. {
  498. struct hiddev_list *list = file->private_data;
  499. struct hiddev *hiddev = list->hiddev;
  500. struct hid_device *hid = hiddev->hid;
  501. struct usb_device *dev = hid_to_usb_dev(hid);
  502. struct hiddev_collection_info cinfo;
  503. struct hiddev_report_info rinfo;
  504. struct hiddev_field_info finfo;
  505. struct hiddev_devinfo dinfo;
  506. struct hid_report *report;
  507. struct hid_field *field;
  508. struct usbhid_device *usbhid = hid->driver_data;
  509. void __user *user_arg = (void __user *)arg;
  510. int i, r;
  511. /* Called without BKL by compat methods so no BKL taken */
  512. /* FIXME: Who or what stop this racing with a disconnect ?? */
  513. if (!hiddev->exist)
  514. return -EIO;
  515. switch (cmd) {
  516. case HIDIOCGVERSION:
  517. return put_user(HID_VERSION, (int __user *)arg);
  518. case HIDIOCAPPLICATION:
  519. if (arg < 0 || arg >= hid->maxapplication)
  520. return -EINVAL;
  521. for (i = 0; i < hid->maxcollection; i++)
  522. if (hid->collection[i].type ==
  523. HID_COLLECTION_APPLICATION && arg-- == 0)
  524. break;
  525. if (i == hid->maxcollection)
  526. return -EINVAL;
  527. return hid->collection[i].usage;
  528. case HIDIOCGDEVINFO:
  529. dinfo.bustype = BUS_USB;
  530. dinfo.busnum = dev->bus->busnum;
  531. dinfo.devnum = dev->devnum;
  532. dinfo.ifnum = usbhid->ifnum;
  533. dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
  534. dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
  535. dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
  536. dinfo.num_applications = hid->maxapplication;
  537. if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
  538. return -EFAULT;
  539. return 0;
  540. case HIDIOCGFLAG:
  541. if (put_user(list->flags, (int __user *)arg))
  542. return -EFAULT;
  543. return 0;
  544. case HIDIOCSFLAG:
  545. {
  546. int newflags;
  547. if (get_user(newflags, (int __user *)arg))
  548. return -EFAULT;
  549. if ((newflags & ~HIDDEV_FLAGS) != 0 ||
  550. ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
  551. (newflags & HIDDEV_FLAG_UREF) == 0))
  552. return -EINVAL;
  553. list->flags = newflags;
  554. return 0;
  555. }
  556. case HIDIOCGSTRING:
  557. mutex_lock(&hiddev->existancelock);
  558. if (hiddev->exist)
  559. r = hiddev_ioctl_string(hiddev, cmd, user_arg);
  560. else
  561. r = -ENODEV;
  562. mutex_unlock(&hiddev->existancelock);
  563. return r;
  564. case HIDIOCINITREPORT:
  565. mutex_lock(&hiddev->existancelock);
  566. if (!hiddev->exist) {
  567. mutex_unlock(&hiddev->existancelock);
  568. return -ENODEV;
  569. }
  570. usbhid_init_reports(hid);
  571. mutex_unlock(&hiddev->existancelock);
  572. return 0;
  573. case HIDIOCGREPORT:
  574. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  575. return -EFAULT;
  576. if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
  577. return -EINVAL;
  578. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  579. return -EINVAL;
  580. mutex_lock(&hiddev->existancelock);
  581. if (hiddev->exist) {
  582. usbhid_submit_report(hid, report, USB_DIR_IN);
  583. usbhid_wait_io(hid);
  584. }
  585. mutex_unlock(&hiddev->existancelock);
  586. return 0;
  587. case HIDIOCSREPORT:
  588. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  589. return -EFAULT;
  590. if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
  591. return -EINVAL;
  592. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  593. return -EINVAL;
  594. mutex_lock(&hiddev->existancelock);
  595. if (hiddev->exist) {
  596. usbhid_submit_report(hid, report, USB_DIR_OUT);
  597. usbhid_wait_io(hid);
  598. }
  599. mutex_unlock(&hiddev->existancelock);
  600. return 0;
  601. case HIDIOCGREPORTINFO:
  602. if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
  603. return -EFAULT;
  604. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  605. return -EINVAL;
  606. rinfo.num_fields = report->maxfield;
  607. if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
  608. return -EFAULT;
  609. return 0;
  610. case HIDIOCGFIELDINFO:
  611. if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
  612. return -EFAULT;
  613. rinfo.report_type = finfo.report_type;
  614. rinfo.report_id = finfo.report_id;
  615. if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
  616. return -EINVAL;
  617. if (finfo.field_index >= report->maxfield)
  618. return -EINVAL;
  619. field = report->field[finfo.field_index];
  620. memset(&finfo, 0, sizeof(finfo));
  621. finfo.report_type = rinfo.report_type;
  622. finfo.report_id = rinfo.report_id;
  623. finfo.field_index = field->report_count - 1;
  624. finfo.maxusage = field->maxusage;
  625. finfo.flags = field->flags;
  626. finfo.physical = field->physical;
  627. finfo.logical = field->logical;
  628. finfo.application = field->application;
  629. finfo.logical_minimum = field->logical_minimum;
  630. finfo.logical_maximum = field->logical_maximum;
  631. finfo.physical_minimum = field->physical_minimum;
  632. finfo.physical_maximum = field->physical_maximum;
  633. finfo.unit_exponent = field->unit_exponent;
  634. finfo.unit = field->unit;
  635. if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
  636. return -EFAULT;
  637. return 0;
  638. case HIDIOCGUCODE:
  639. /* fall through */
  640. case HIDIOCGUSAGE:
  641. case HIDIOCSUSAGE:
  642. case HIDIOCGUSAGES:
  643. case HIDIOCSUSAGES:
  644. case HIDIOCGCOLLECTIONINDEX:
  645. mutex_lock(&hiddev->existancelock);
  646. if (hiddev->exist)
  647. r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
  648. else
  649. r = -ENODEV;
  650. mutex_unlock(&hiddev->existancelock);
  651. return r;
  652. case HIDIOCGCOLLECTIONINFO:
  653. if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
  654. return -EFAULT;
  655. if (cinfo.index >= hid->maxcollection)
  656. return -EINVAL;
  657. cinfo.type = hid->collection[cinfo.index].type;
  658. cinfo.usage = hid->collection[cinfo.index].usage;
  659. cinfo.level = hid->collection[cinfo.index].level;
  660. if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
  661. return -EFAULT;
  662. return 0;
  663. default:
  664. if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
  665. return -EINVAL;
  666. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
  667. int len;
  668. if (!hid->name)
  669. return 0;
  670. len = strlen(hid->name) + 1;
  671. if (len > _IOC_SIZE(cmd))
  672. len = _IOC_SIZE(cmd);
  673. return copy_to_user(user_arg, hid->name, len) ?
  674. -EFAULT : len;
  675. }
  676. if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
  677. int len;
  678. if (!hid->phys)
  679. return 0;
  680. len = strlen(hid->phys) + 1;
  681. if (len > _IOC_SIZE(cmd))
  682. len = _IOC_SIZE(cmd);
  683. return copy_to_user(user_arg, hid->phys, len) ?
  684. -EFAULT : len;
  685. }
  686. }
  687. return -EINVAL;
  688. }
  689. #ifdef CONFIG_COMPAT
  690. static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  691. {
  692. return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  693. }
  694. #endif
  695. static const struct file_operations hiddev_fops = {
  696. .owner = THIS_MODULE,
  697. .read = hiddev_read,
  698. .write = hiddev_write,
  699. .poll = hiddev_poll,
  700. .open = hiddev_open,
  701. .release = hiddev_release,
  702. .unlocked_ioctl = hiddev_ioctl,
  703. .fasync = hiddev_fasync,
  704. #ifdef CONFIG_COMPAT
  705. .compat_ioctl = hiddev_compat_ioctl,
  706. #endif
  707. };
  708. static struct usb_class_driver hiddev_class = {
  709. .name = "hiddev%d",
  710. .fops = &hiddev_fops,
  711. .minor_base = HIDDEV_MINOR_BASE,
  712. };
  713. /*
  714. * This is where hid.c calls us to connect a hid device to the hiddev driver
  715. */
  716. int hiddev_connect(struct hid_device *hid, unsigned int force)
  717. {
  718. struct hiddev *hiddev;
  719. struct usbhid_device *usbhid = hid->driver_data;
  720. int retval;
  721. if (!force) {
  722. unsigned int i;
  723. for (i = 0; i < hid->maxcollection; i++)
  724. if (hid->collection[i].type ==
  725. HID_COLLECTION_APPLICATION &&
  726. !IS_INPUT_APPLICATION(hid->collection[i].usage))
  727. break;
  728. if (i == hid->maxcollection)
  729. return -1;
  730. }
  731. if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
  732. return -1;
  733. init_waitqueue_head(&hiddev->wait);
  734. INIT_LIST_HEAD(&hiddev->list);
  735. spin_lock_init(&hiddev->list_lock);
  736. mutex_init(&hiddev->existancelock);
  737. hid->hiddev = hiddev;
  738. hiddev->hid = hid;
  739. hiddev->exist = 1;
  740. /* when lock_kernel() usage is fixed in usb_open(),
  741. * we could also fix it here */
  742. lock_kernel();
  743. retval = usb_register_dev(usbhid->intf, &hiddev_class);
  744. if (retval) {
  745. err_hid("Not able to get a minor for this device.");
  746. hid->hiddev = NULL;
  747. unlock_kernel();
  748. kfree(hiddev);
  749. return -1;
  750. } else {
  751. hid->minor = usbhid->intf->minor;
  752. hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
  753. }
  754. unlock_kernel();
  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. hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
  770. usb_deregister_dev(usbhid->intf, &hiddev_class);
  771. if (hiddev->open) {
  772. usbhid_close(hiddev->hid);
  773. wake_up_interruptible(&hiddev->wait);
  774. } else {
  775. kfree(hiddev);
  776. }
  777. }
  778. /* Currently this driver is a USB driver. It's not a conventional one in
  779. * the sense that it doesn't probe at the USB level. Instead it waits to
  780. * be connected by HID through the hiddev_connect / hiddev_disconnect
  781. * routines. The reason to register as a USB device is to gain part of the
  782. * minor number space from the USB major.
  783. *
  784. * In theory, should the HID code be generalized to more than one physical
  785. * medium (say, IEEE 1384), this driver will probably need to register its
  786. * own major number, and in doing so, no longer need to register with USB.
  787. * At that point the probe routine and hiddev_driver struct below will no
  788. * longer be useful.
  789. */
  790. /* We never attach in this manner, and rely on HID to connect us. This
  791. * is why there is no disconnect routine defined in the usb_driver either.
  792. */
  793. static int hiddev_usbd_probe(struct usb_interface *intf,
  794. const struct usb_device_id *hiddev_info)
  795. {
  796. return -ENODEV;
  797. }
  798. static /* const */ struct usb_driver hiddev_driver = {
  799. .name = "hiddev",
  800. .probe = hiddev_usbd_probe,
  801. };
  802. int __init hiddev_init(void)
  803. {
  804. return usb_register(&hiddev_driver);
  805. }
  806. void hiddev_exit(void)
  807. {
  808. usb_deregister(&hiddev_driver);
  809. }