You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.6KB

  1. /* $FreeBSD$ */
  2. /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
  3. /*-
  4. * SPDX-License-Identifier: BSD-2-Clause-NetBSD
  5. *
  6. * Copyright (c) 1998 The NetBSD Foundation, Inc.
  7. * All rights reserved.
  8. *
  9. * This code is derived from software contributed to The NetBSD Foundation
  10. * by Lennart Augustsson (lennart@augustsson.net) at
  11. * Carlstedt Research & Technology.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  23. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  24. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  26. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/param.h>
  35. #include <sys/kdb.h>
  36. #include <sys/lock.h>
  37. #include <sys/module.h>
  38. #include <sys/proc.h>
  39. #include "hid.h"
  40. int
  41. hid_report_size_1(const void *buf, hid_size_t len, enum hid_kind k, uint8_t id)
  42. {
  43. struct hid_data *d;
  44. struct hid_item h;
  45. uint32_t temp;
  46. uint32_t hpos;
  47. uint32_t lpos;
  48. int report_id = 0;
  49. hpos = 0;
  50. lpos = 0xFFFFFFFF;
  51. for (d = hid_start_parse(buf, len, 1 << k); hid_get_item(d, &h);) {
  52. if (h.kind == k && h.report_ID == id) {
  53. /* compute minimum */
  54. if (lpos > h.loc.pos)
  55. lpos = h.loc.pos;
  56. /* compute end position */
  57. temp = h.loc.pos + (h.loc.size * h.loc.count);
  58. /* compute maximum */
  59. if (hpos < temp)
  60. hpos = temp;
  61. if (h.report_ID != 0)
  62. report_id = 1;
  63. }
  64. }
  65. hid_end_parse(d);
  66. /* safety check - can happen in case of currupt descriptors */
  67. if (lpos > hpos)
  68. temp = 0;
  69. else
  70. temp = hpos - lpos;
  71. /* return length in bytes rounded up */
  72. return ((temp + 7) / 8 + report_id);
  73. }
  74. int
  75. hid_tlc_locate(const void *desc, hid_size_t size, int32_t u, enum hid_kind k,
  76. uint8_t tlc_index, uint8_t index, struct hid_location *loc,
  77. uint32_t *flags, uint8_t *id, struct hid_absinfo *ai)
  78. {
  79. struct hid_data *d;
  80. struct hid_item h;
  81. d = hid_start_parse(desc, size, 1 << k);
  82. HID_TLC_FOREACH_ITEM(d, &h, tlc_index) {
  83. if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
  84. if (index--)
  85. continue;
  86. if (loc != NULL)
  87. *loc = h.loc;
  88. if (flags != NULL)
  89. *flags = h.flags;
  90. if (id != NULL)
  91. *id = h.report_ID;
  92. if (ai != NULL && (h.flags & HIO_RELATIVE) == 0)
  93. *ai = (struct hid_absinfo) {
  94. .max = h.logical_maximum,
  95. .min = h.logical_minimum,
  96. .res = hid_item_resolution(&h),
  97. };
  98. hid_end_parse(d);
  99. return (1);
  100. }
  101. }
  102. if (loc != NULL)
  103. loc->size = 0;
  104. if (flags != NULL)
  105. *flags = 0;
  106. if (id != NULL)
  107. *id = 0;
  108. hid_end_parse(d);
  109. return (0);
  110. }
  111. int
  112. hid_in_polling_mode(void)
  113. {
  114. return (HID_IN_POLLING_MODE_VALUE());
  115. }
  116. MODULE_DEPEND(hid, usb, 1, 1, 1);
  117. MODULE_VERSION(hid, 1);