hiddev.c 21 KB

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