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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. #include "keylayouts.h" // from Teensyduino core library
  26. typedef struct {
  27. KEYCODE_TYPE code;
  28. uint8_t ascii;
  29. } keycode_extra_t;
  30. typedef struct {
  31. KEYCODE_TYPE code;
  32. KEYCODE_TYPE codeNumlockOff;
  33. uint8_t charNumlockOn; // We will assume when num lock is on we have all characters...
  34. } keycode_numlock_t;
  35. #ifdef M
  36. #undef M
  37. #endif
  38. #define M(n) ((n) & KEYCODE_MASK)
  39. keycode_extra_t keycode_extras[] = {
  40. {M(KEY_ENTER), '\n'},
  41. {M(KEY_ESC), 0x1b},
  42. {M(KEY_TAB), 0x9 },
  43. {M(KEY_UP), KEYD_UP },
  44. {M(KEY_DOWN), KEYD_DOWN },
  45. {M(KEY_LEFT), KEYD_LEFT },
  46. {M(KEY_RIGHT), KEYD_RIGHT },
  47. {M(KEY_INSERT), KEYD_INSERT },
  48. {M(KEY_DELETE), KEYD_DELETE },
  49. {M(KEY_PAGE_UP), KEYD_PAGE_UP },
  50. {M(KEY_PAGE_DOWN), KEYD_PAGE_DOWN },
  51. {M(KEY_HOME), KEYD_HOME },
  52. {M(KEY_END), KEYD_END },
  53. {M(KEY_F1), KEYD_F1 },
  54. {M(KEY_F2), KEYD_F2 },
  55. {M(KEY_F3), KEYD_F3 },
  56. {M(KEY_F4), KEYD_F4 },
  57. {M(KEY_F5), KEYD_F5 },
  58. {M(KEY_F6), KEYD_F6 },
  59. {M(KEY_F7), KEYD_F7 },
  60. {M(KEY_F8), KEYD_F8 },
  61. {M(KEY_F9), KEYD_F9 },
  62. {M(KEY_F10), KEYD_F10 },
  63. {M(KEY_F11), KEYD_F11 },
  64. {M(KEY_F12), KEYD_F12 }
  65. };
  66. // Some of these mapped to key + shift.
  67. keycode_numlock_t keycode_numlock[] = {
  68. {M(KEYPAD_SLASH), '/', '/'},
  69. {M(KEYPAD_ASTERIX), '*', '*'},
  70. {M(KEYPAD_MINUS), '-', '-'},
  71. {M(KEYPAD_PLUS), '+', '+'},
  72. {M(KEYPAD_ENTER), '\n', '\n'},
  73. {M(KEYPAD_1), 0x80 | M(KEY_END), '1'},
  74. {M(KEYPAD_2), 0x80 | M(KEY_DOWN), '2'},
  75. {M(KEYPAD_3), 0x80 | M(KEY_PAGE_DOWN), '3'},
  76. {M(KEYPAD_4), 0x80 | M(KEY_LEFT), '4'},
  77. {M(KEYPAD_5), 0x00, '5'},
  78. {M(KEYPAD_6), 0x80 | M(KEY_RIGHT), '6'},
  79. {M(KEYPAD_7), 0x80 | M(KEY_HOME), '7'},
  80. {M(KEYPAD_8), 0x80 | M(KEY_UP), '8'},
  81. {M(KEYPAD_9), 0x80 | M(KEY_PAGE_UP), '9'},
  82. {M(KEYPAD_0), 0x80 | M(KEY_INSERT), '0'},
  83. {M(KEYPAD_PERIOD), 0x80 | M(KEY_DELETE), '.'}
  84. };
  85. #define print USBHost::print_
  86. #define println USBHost::println_
  87. void KeyboardController::init()
  88. {
  89. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  90. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  91. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  92. driver_ready_for_device(this);
  93. USBHIDParser::driver_ready_for_hid_collection(this);
  94. }
  95. bool KeyboardController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  96. {
  97. println("KeyboardController claim this=", (uint32_t)this, HEX);
  98. // only claim at interface level
  99. if (type != 1) return false;
  100. if (len < 9+9+7) return false;
  101. uint32_t numendpoint = descriptors[4];
  102. if (numendpoint < 1) return false;
  103. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  104. if (descriptors[6] != 1) return false; // bInterfaceSubClass, 1 = Boot Device
  105. if (descriptors[7] != 1) return false; // bInterfaceProtocol, 1 = Keyboard
  106. if (descriptors[9] != 9) return false;
  107. if (descriptors[10] != 33) return false; // HID descriptor (ignored, Boot Protocol)
  108. if (descriptors[18] != 7) return false;
  109. if (descriptors[19] != 5) return false; // endpoint descriptor
  110. uint32_t endpoint = descriptors[20];
  111. println("ep = ", endpoint, HEX);
  112. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  113. endpoint &= 0x0F;
  114. if (endpoint == 0) return false;
  115. if (descriptors[21] != 3) return false; // must be interrupt type
  116. uint32_t size = descriptors[22] | (descriptors[23] << 8);
  117. println("packet size = ", size);
  118. if ((size < 8) || (size > 64)) {
  119. return false; // Keyboard Boot Protocol is 8 bytes, but maybe others have longer...
  120. }
  121. #ifdef USBHS_KEYBOARD_INTERVAL
  122. uint32_t interval = USBHS_KEYBOARD_INTERVAL;
  123. #else
  124. uint32_t interval = descriptors[24];
  125. #endif
  126. println("polling interval = ", interval);
  127. datapipe = new_Pipe(dev, 3, endpoint, 1, 8, interval);
  128. datapipe->callback_function = callback;
  129. queue_Data_Transfer(datapipe, report, 8, this);
  130. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  131. queue_Control_Transfer(dev, &setup, NULL, this);
  132. return true;
  133. }
  134. void KeyboardController::control(const Transfer_t *transfer)
  135. {
  136. }
  137. void KeyboardController::callback(const Transfer_t *transfer)
  138. {
  139. //println("KeyboardController Callback (static)");
  140. if (transfer->driver) {
  141. ((KeyboardController *)(transfer->driver))->new_data(transfer);
  142. }
  143. }
  144. void KeyboardController::disconnect()
  145. {
  146. // TODO: free resources
  147. }
  148. // Arduino defined this static weak symbol callback, and their
  149. // examples use it as the only way to detect new key presses,
  150. // so unfortunate as static weak callbacks are, it probably
  151. // needs to be supported for compatibility
  152. extern "C" {
  153. void __keyboardControllerEmptyCallback() { }
  154. }
  155. void keyPressed() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  156. void keyReleased() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  157. static bool contains(uint8_t b, const uint8_t *data)
  158. {
  159. if (data[2] == b || data[3] == b || data[4] == b) return true;
  160. if (data[5] == b || data[6] == b || data[7] == b) return true;
  161. return false;
  162. }
  163. void KeyboardController::new_data(const Transfer_t *transfer)
  164. {
  165. println("KeyboardController Callback (member)");
  166. print(" KB Data: ");
  167. print_hexbytes(transfer->buffer, 8);
  168. for (int i=2; i < 8; i++) {
  169. uint32_t key = prev_report[i];
  170. if (key >= 4 && !contains(key, report)) {
  171. key_release(prev_report[0], key);
  172. }
  173. }
  174. for (int i=2; i < 8; i++) {
  175. uint32_t key = report[i];
  176. if (key >= 4 && !contains(key, prev_report)) {
  177. key_press(report[0], key);
  178. }
  179. }
  180. memcpy(prev_report, report, 8);
  181. queue_Data_Transfer(datapipe, report, 8, this);
  182. }
  183. void KeyboardController::numLock(bool f) {
  184. if (leds_.numLock != f) {
  185. leds_.numLock = f;
  186. updateLEDS();
  187. }
  188. }
  189. void KeyboardController::capsLock(bool f) {
  190. if (leds_.capsLock != f) {
  191. leds_.capsLock = f;
  192. updateLEDS();
  193. }
  194. }
  195. void KeyboardController::scrollLock(bool f) {
  196. if (leds_.scrollLock != f) {
  197. leds_.scrollLock = f;
  198. updateLEDS();
  199. }
  200. }
  201. void KeyboardController::key_press(uint32_t mod, uint32_t key)
  202. {
  203. // TODO: queue events, perform callback from Task
  204. println(" press, key=", key);
  205. modifiers = mod;
  206. keyOEM = key;
  207. keyCode = convert_to_unicode(mod, key);
  208. println(" unicode = ", keyCode);
  209. if (keyPressedFunction) {
  210. keyPressedFunction(keyCode);
  211. } else {
  212. keyPressed();
  213. }
  214. }
  215. void KeyboardController::key_release(uint32_t mod, uint32_t key)
  216. {
  217. // TODO: queue events, perform callback from Task
  218. println(" release, key=", key);
  219. modifiers = mod;
  220. keyOEM = key;
  221. // Look for modifier keys
  222. if (key == M(KEY_NUM_LOCK)) {
  223. numLock(!leds_.numLock);
  224. // Lets toggle Numlock
  225. } else if (key == M(KEY_CAPS_LOCK)) {
  226. capsLock(!leds_.capsLock);
  227. } else if (key == M(KEY_SCROLL_LOCK)) {
  228. scrollLock(!leds_.scrollLock);
  229. } else {
  230. keyCode = convert_to_unicode(mod, key);
  231. if (keyReleasedFunction) {
  232. keyReleasedFunction(keyCode);
  233. } else {
  234. keyReleased();
  235. }
  236. }
  237. }
  238. uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
  239. {
  240. // WIP: special keys
  241. // TODO: dead key sequences
  242. if (key & SHIFT_MASK) {
  243. // Many of these keys will look like they are other keys with shift mask...
  244. // Check for any of our mapped extra keys
  245. for (uint8_t i = 0; i < (sizeof(keycode_numlock)/sizeof(keycode_numlock[0])); i++) {
  246. if (keycode_numlock[i].code == key) {
  247. // See if the user is using numlock or not...
  248. if (leds_.numLock) {
  249. return keycode_numlock[i].charNumlockOn;
  250. } else {
  251. key = keycode_numlock[i].codeNumlockOff;
  252. if (!(key & 0x80)) return key; // we have hard coded value
  253. key &= 0x7f; // mask off the extra and break out to process as other characters...
  254. break;
  255. }
  256. }
  257. }
  258. }
  259. // Check for any of our mapped extra keys - Done early as some of these keys are
  260. // above and some below the SHIFT_MASK value
  261. for (uint8_t i = 0; i < (sizeof(keycode_extras)/sizeof(keycode_extras[0])); i++) {
  262. if (keycode_extras[i].code == key) {
  263. return keycode_extras[i].ascii;
  264. }
  265. }
  266. // If we made it here without doing something then return 0;
  267. if (key & SHIFT_MASK) return 0;
  268. if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK;
  269. if (leds_.capsLock) key ^= SHIFT_MASK; // Caps lock will switch the Shift;
  270. for (int i=0; i < 96; i++) {
  271. if (keycodes_ascii[i] == key) {
  272. if ((mod & 1) || (mod & 0x10)) return (i+32) & 0x1f; // Control key is down
  273. return i + 32;
  274. }
  275. }
  276. #ifdef ISO_8859_1_A0
  277. for (int i=0; i < 96; i++) {
  278. if (keycodes_iso_8859_1[i] == key) return i + 160;
  279. }
  280. #endif
  281. return 0;
  282. }
  283. void KeyboardController::LEDS(uint8_t leds) {
  284. println("Keyboard setLEDS ", leds, HEX);
  285. leds_.byte = leds;
  286. updateLEDS();
  287. }
  288. void KeyboardController::updateLEDS() {
  289. // Now lets tell keyboard new state.
  290. mk_setup(setup, 0x21, 9, 0x200, 0, sizeof(leds_.byte)); // hopefully this sets leds
  291. queue_Control_Transfer(device, &setup, &leds_.byte, this);
  292. }
  293. //=============================================================================
  294. // Keyboard Extras - Combined from other object
  295. //=============================================================================
  296. #define TOPUSAGE_SYS_CONTROL 0x10080
  297. #define TOPUSAGE_CONSUMER_CONTROL 0x0c0001
  298. hidclaim_t KeyboardController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  299. {
  300. // Lets try to claim a few specific Keyboard related collection/reports
  301. //Serial.printf("KBH Claim %x\n", topusage);
  302. if ((topusage != TOPUSAGE_SYS_CONTROL)
  303. && (topusage != TOPUSAGE_CONSUMER_CONTROL)
  304. ) return CLAIM_NO;
  305. // only claim from one physical device
  306. //Serial.println("KeyboardController claim collection");
  307. // Lets only claim if this is the same device as claimed Keyboard...
  308. if (dev != device) return CLAIM_NO;
  309. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  310. mydevice = dev;
  311. collections_claimed_++;
  312. return CLAIM_REPORT;
  313. }
  314. void KeyboardController::disconnect_collection(Device_t *dev)
  315. {
  316. if (--collections_claimed_ == 0) {
  317. mydevice = NULL;
  318. }
  319. }
  320. void KeyboardController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  321. {
  322. //Serial.printf("KPC:hid_input_begin TUSE: %x TYPE: %x Range:%x %x\n", topusage, type, lgmin, lgmax);
  323. topusage_ = topusage; // remember which report we are processing.
  324. hid_input_begin_ = true;
  325. hid_input_data_ = false;
  326. }
  327. void KeyboardController::hid_input_data(uint32_t usage, int32_t value)
  328. {
  329. // Hack ignore 0xff00 high words as these are user values...
  330. if ((usage & 0xffff0000) == 0xff000000) return;
  331. //Serial.printf("KeyboardController: topusage= %x usage=%X, value=%d\n", topusage_, usage, value);
  332. // See if the value is in our keys_down list
  333. usage &= 0xffff; // only keep the actual key
  334. if (usage == 0) return; // lets not process 0, if only 0 happens, we will handle it on the end to remove existing pressed items.
  335. // Remember if we have received any logical key up events. Some keyboard appear to send them
  336. // others do no...
  337. hid_input_data_ = true;
  338. uint8_t key_index;
  339. for (key_index = 0; key_index < count_keys_down_; key_index++) {
  340. if (keys_down[key_index] == usage) {
  341. if (value) return; // still down
  342. if (extrasKeyReleasedFunction) {
  343. extrasKeyReleasedFunction(topusage_, usage);
  344. }
  345. // Remove from list
  346. count_keys_down_--;
  347. for (;key_index < count_keys_down_; key_index++) {
  348. keys_down[key_index] = keys_down[key_index+1];
  349. }
  350. return;
  351. }
  352. }
  353. // Was not in list
  354. if (!value) return; // still 0
  355. if (extrasKeyPressedFunction) {
  356. extrasKeyPressedFunction(topusage_, usage);
  357. }
  358. if (count_keys_down_ < MAX_KEYS_DOWN) {
  359. keys_down[count_keys_down_++] = usage;
  360. }
  361. }
  362. void KeyboardController::hid_input_end()
  363. {
  364. //Serial.println("KPC:hid_input_end");
  365. if (hid_input_begin_) {
  366. // See if we received any data from parser if not, assume all keys released...
  367. if (!hid_input_data_ ) {
  368. if (extrasKeyReleasedFunction) {
  369. while (count_keys_down_) {
  370. count_keys_down_--;
  371. extrasKeyReleasedFunction(topusage_, keys_down[count_keys_down_]);
  372. }
  373. }
  374. count_keys_down_ = 0;
  375. }
  376. hid_input_begin_ = false;
  377. }
  378. }