您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

235 行
7.3KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #if 1
  26. bool MouseController::claim_collection(Device_t *dev, uint32_t topusage)
  27. {
  28. // only claim Desktop/Mouse
  29. if (topusage != 0x10002) return false;
  30. // only claim from one physical device
  31. if (mydevice != NULL && dev != mydevice) return false;
  32. mydevice = dev;
  33. collections_claimed++;
  34. return true;
  35. }
  36. void MouseController::disconnect_collection(Device_t *dev)
  37. {
  38. if (--collections_claimed == 0) {
  39. mydevice = NULL;
  40. }
  41. }
  42. void MouseController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  43. {
  44. // TODO: check if absolute coordinates
  45. }
  46. void MouseController::hid_input_data(uint32_t usage, int32_t value)
  47. {
  48. //Serial.printf("Mouse: usage=%X, value=%d\n", usage, value);
  49. uint32_t usage_page = usage >> 16;
  50. usage &= 0xFFFF;
  51. if (usage_page == 9 && usage >= 1 && usage <= 8) {
  52. if (value == 0) {
  53. buttons &= ~(1 << (usage -1));
  54. } else {
  55. buttons |= (1 << (usage -1));
  56. }
  57. } else if (usage_page == 1) {
  58. switch (usage) {
  59. case 0x30:
  60. mouseX = value;
  61. break;
  62. case 0x31:
  63. mouseY = value;
  64. break;
  65. case 0x32: // Apple uses this for horizontal scroll
  66. wheelH = value;
  67. break;
  68. case 0x38:
  69. wheel = value;
  70. break;
  71. }
  72. } else if (usage_page == 12) {
  73. if (usage == 0x238) { // Microsoft uses this for horizontal scroll
  74. wheelH = value;
  75. }
  76. }
  77. }
  78. void MouseController::hid_input_end()
  79. {
  80. mouseEvent = true;
  81. }
  82. void MouseController::mouseDataClear() {
  83. mouseEvent = false;
  84. buttons = 0;
  85. mouseX = 0;
  86. mouseY = 0;
  87. wheel = 0;
  88. wheelH = 0;
  89. }
  90. #else
  91. void MouseController::init()
  92. {
  93. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  94. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  95. driver_ready_for_device(this);
  96. }
  97. bool MouseController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  98. {
  99. println("MouseController claim this=", (uint32_t)this, HEX);
  100. // only claim at interface level
  101. if (type != 1) return false;
  102. if (len < 9+9+7) return false;
  103. uint32_t numendpoint = descriptors[4];
  104. if (numendpoint < 1) return false;
  105. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  106. if (descriptors[6] != 1) return false; // bInterfaceSubClass, 1 = Boot Device
  107. if (descriptors[7] != 2) return false; // bInterfaceProtocol, 2 = Mouse
  108. if (descriptors[9] != 9) return false;
  109. if (descriptors[10] != 33) return false; // HID descriptor (ignored, Boot Protocol)
  110. if (descriptors[18] != 7) return false;
  111. if (descriptors[19] != 5) return false; // endpoint descriptor
  112. uint32_t endpoint = descriptors[20];
  113. println("ep(mouse) = ", endpoint, HEX);
  114. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  115. endpoint &= 0x0F;
  116. if (endpoint == 0) return false;
  117. if (descriptors[21] != 3) return false; // must be interrupt type
  118. uint32_t size = descriptors[22] | (descriptors[23] << 8);
  119. println("descriptors[22] = ",descriptors[22]);
  120. println("descriptors[23] = ",descriptors[23]);
  121. println("packet size(mouse) = ", size);
  122. // packey size seems to be 20 for (wireless type 2) or 6 bytes for wired
  123. packetSize = size;
  124. if ((size != 20) && (size != 6)) return false;
  125. if(packetSize == 6) packetSize = 8; // Minimum packet size needed is 8
  126. uint32_t interval = descriptors[24];
  127. println("polling interval = ", interval);
  128. datapipe = new_Pipe(dev, 3, endpoint, 1, packetSize, interval);
  129. datapipe->callback_function = callback;
  130. queue_Data_Transfer(datapipe, report, packetSize, this);
  131. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  132. queue_Control_Transfer(dev, &setup, NULL, this);
  133. return true;
  134. }
  135. void MouseController::control(const Transfer_t *transfer)
  136. {
  137. }
  138. void MouseController::callback(const Transfer_t *transfer)
  139. {
  140. //println("MouseController Callback (static)");
  141. if (transfer->driver) {
  142. ((MouseController *)(transfer->driver))->new_data(transfer);
  143. }
  144. }
  145. void MouseController::disconnect()
  146. {
  147. // TODO: free resources
  148. }
  149. // Arduino defined this static weak symbol callback, and their
  150. // examples use it as the only way to detect new key presses,
  151. // so unfortunate as static weak callbacks are, it probably
  152. // needs to be supported for compatibility
  153. extern "C" {
  154. void __MouseControllerEmptyCallback() { }
  155. }
  156. //void mouseEvent() __attribute__ ((weak, alias("__mouseEventEmptyCallback")));
  157. void MouseController::new_data(const Transfer_t *transfer)
  158. {
  159. println("MouseController Callback (member)");
  160. print(" Mouse Data: ");
  161. print_hexbytes(transfer->buffer, 8);
  162. // The mouse button report byte is 1 for left button, 2 for right button, 4 for wheel
  163. // button (three button mouse).
  164. //
  165. // The mouse x and y report consists of two 12 bit signed values packed into three
  166. // bytes and are relative values. Shown below are the three x/y bytes of a wireless
  167. // mouse report packet.
  168. //
  169. // byte 3 is the lower 8 bits of the x coordinate.
  170. // byte 5 is the upper 8 bits of the y coordinate.
  171. // byte 4 lower 4 bits are the upper 4 bits of the x coordinate.
  172. // byte 4 upper 4 bits are the lower 4 bits of the y coordinate.
  173. //
  174. // Example of one increment in all four directions:
  175. // x x
  176. // ff 0f 00 = left -1 00 01 00 = right 1
  177. // y y
  178. // 00 f0 ff = up -1 00 10 00 = down 1
  179. //
  180. // The wheel report is a signed byte that is 1 for forward and -1 for backward movement.
  181. // It is cleared to zero with any x and/or y movement.
  182. //
  183. // Wireless Logitech mouse reports have byte 0 set to 0x02 indicating a type 2 report.
  184. // Not sure what this really means yet but all bytes of the report packet are shifted
  185. // ahead by one byte and there is a single byte that is always zero after the button
  186. // report byte.
  187. if(packetSize == 20) {
  188. buttons = report[1];
  189. mouseX = ((report[4] & 0x0f) << 8) | ((report[3] & 0xff));
  190. mouseY = ((report[5] & 0xff) << 4) | ((report[4] >> 4) & 0x0f);
  191. wheel = report[6];
  192. } else {
  193. buttons = report[0];
  194. mouseX = ((report[2] & 0x0f) << 8) | ((report[1] & 0xff));
  195. mouseY = ((report[3] & 0xff) << 4) | ((report[2] >> 4) & 0x0f);
  196. wheel = report[4];
  197. }
  198. mouseEvent = true;
  199. memcpy(prev_report, report, 20);
  200. queue_Data_Transfer(datapipe, report, 20, this);
  201. }
  202. void MouseController::mouseDataClear() {
  203. mouseEvent = false;
  204. buttons = 0;
  205. mouseX = 0;
  206. mouseY = 0;
  207. wheel = 0;
  208. }
  209. #endif