hiddev.c 20 KB

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