hiddev.c 20 KB

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