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.

498 lines
17KB

  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. #define print USBHost::print_
  26. #define println USBHost::println_
  27. // PID/VID to joystick mapping - Only the XBOXOne is used to claim the USB interface directly,
  28. // The others are used after claim-hid code to know which one we have and to use it for
  29. // doing other features.
  30. JoystickController::product_vendor_mapping_t JoystickController::pid_vid_mapping[] = {
  31. { 0x045e, 0x02ea, XBOXONE, false },{ 0x045e, 0x02dd, XBOXONE, false },
  32. { 0x054C, 0x0268, PS3, true},
  33. { 0x054C, 0x05C4, PS4, true}, {0x054C, 0x09CC, PS4, true }
  34. };
  35. //-----------------------------------------------------------------------------
  36. void JoystickController::init()
  37. {
  38. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  39. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  40. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  41. driver_ready_for_device(this);
  42. USBHIDParser::driver_ready_for_hid_collection(this);
  43. }
  44. //-----------------------------------------------------------------------------
  45. JoystickController::joytype_t JoystickController::mapVIDPIDtoJoystickType(uint16_t idVendor, uint16_t idProduct, bool exclude_hid_devices)
  46. {
  47. for (uint8_t i = 0; i < (sizeof(pid_vid_mapping)/sizeof(pid_vid_mapping[0])); i++) {
  48. if ((idVendor == pid_vid_mapping[i].idVendor) && (idProduct == pid_vid_mapping[i].idProduct)) {
  49. println("Match PID/VID: ", i, DEC);
  50. if (exclude_hid_devices && pid_vid_mapping[i].hidDevice) return UNKNOWN;
  51. return pid_vid_mapping[i].joyType;
  52. }
  53. }
  54. return UNKNOWN; // Not in our list
  55. }
  56. //*****************************************************************************
  57. // Some simple query functions depend on which interface we are using...
  58. //*****************************************************************************
  59. uint16_t JoystickController::idVendor()
  60. {
  61. if (device != nullptr) return device->idVendor;
  62. if (mydevice != nullptr) return mydevice->idVendor;
  63. return 0;
  64. }
  65. uint16_t JoystickController::idProduct()
  66. {
  67. if (device != nullptr) return device->idProduct;
  68. if (mydevice != nullptr) return mydevice->idProduct;
  69. return 0;
  70. }
  71. const uint8_t *JoystickController::manufacturer()
  72. {
  73. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  74. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  75. return nullptr;
  76. }
  77. const uint8_t *JoystickController::product()
  78. {
  79. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  80. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  81. return nullptr;
  82. }
  83. const uint8_t *JoystickController::serialNumber()
  84. {
  85. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  86. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  87. return nullptr;
  88. }
  89. bool JoystickController::setRumble(uint8_t lValue, uint8_t rValue, uint8_t timeout)
  90. {
  91. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  92. // joystick types.
  93. rumble_lValue_ = lValue;
  94. rumble_rValue_ = rValue;
  95. rumble_timeout_ = timeout;
  96. switch (joystickType) {
  97. default:
  98. break;
  99. case PS3:
  100. return transmitPS3UserFeedbackMsg();
  101. case PS4:
  102. return transmitPS4UserFeedbackMsg();
  103. case XBOXONE:
  104. // Lets try sending a request to the XBox 1.
  105. txbuf_[0] = 0x9;
  106. txbuf_[1] = 0x0;
  107. txbuf_[2] = 0x0;
  108. txbuf_[3] = 0x09; // Substructure (what substructure rest of this packet has)
  109. txbuf_[4] = 0x00; // Mode
  110. txbuf_[5] = 0x0f; // Rumble mask (what motors are activated) (0000 lT rT L R)
  111. txbuf_[6] = 0x0; // lT force
  112. txbuf_[7] = 0x0; // rT force
  113. txbuf_[8] = lValue; // L force
  114. txbuf_[9] = rValue; // R force
  115. txbuf_[10] = 0xff; // Length of pulse
  116. txbuf_[11] = 0x00; // Period between pulses
  117. txbuf_[12] = 0x00; // Repeat
  118. if (!queue_Data_Transfer(txpipe_, txbuf_, 13, this)) {
  119. println("XBoxOne rumble transfer fail");
  120. }
  121. return true; //
  122. }
  123. return false;
  124. }
  125. bool JoystickController::setLEDs(uint8_t lr, uint8_t lg, uint8_t lb)
  126. {
  127. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  128. // joystick types.
  129. if ((leds_[0] != lr) || (leds_[1] != lg) || (leds_[2] != lb)) {
  130. leds_[0] = lr;
  131. leds_[1] = lg;
  132. leds_[2] = lb;
  133. switch (joystickType) {
  134. case PS3:
  135. return transmitPS3UserFeedbackMsg();
  136. case PS4:
  137. return transmitPS4UserFeedbackMsg();
  138. default:
  139. return false;
  140. }
  141. }
  142. return false;
  143. }
  144. bool JoystickController::transmitPS4UserFeedbackMsg() {
  145. if (!driver_) return false;
  146. uint8_t packet[32];
  147. memset(packet, 0, sizeof(packet));
  148. packet[0] = 0x05; // Report ID
  149. packet[1]= 0xFF;
  150. packet[4] = rumble_lValue_; // Small Rumble
  151. packet[5] = rumble_rValue_; // Big rumble
  152. packet[6] = leds_[0]; // RGB value
  153. packet[7] = leds_[1];
  154. packet[8] = leds_[2];
  155. // 9, 10 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
  156. Serial.printf("Joystick update Rumble/LEDs");
  157. return driver_->sendPacket(packet, 32);
  158. }
  159. static const uint8_t PS3_USER_FEEDBACK_INIT[] = {
  160. 0x00, 0x00, 0x00, 0x00, 0x00,
  161. 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0xff, 0x27, 0x10, 0x00, 0x32,
  163. 0xff, 0x27, 0x10, 0x00, 0x32,
  164. 0xff, 0x27, 0x10, 0x00, 0x32,
  165. 0xff, 0x27, 0x10, 0x00, 0x32,
  166. 0x00, 0x00, 0x00, 0x00, 0x00,
  167. 0x00, 0x00, 0x00, 0x00, 0x00,
  168. 0x00, 0x00, 0x00, 0x00, 0x00,
  169. 0x00, 0x00, 0x00 };
  170. bool JoystickController::transmitPS3UserFeedbackMsg() {
  171. if (!driver_) return false;
  172. memcpy(txbuf_, PS3_USER_FEEDBACK_INIT, 48);
  173. txbuf_[1] = rumble_lValue_? rumble_timeout_ : 0;
  174. txbuf_[2] = rumble_lValue_; // Small Rumble
  175. txbuf_[3] = rumble_rValue_? rumble_timeout_ : 0;
  176. txbuf_[4] = rumble_rValue_; // Big rumble
  177. txbuf_[9] = leds_[0] << 1; // RGB value
  178. //Serial.printf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[1], txbuf_[2], txbuf_[3], txbuf_[4], txbuf_[9]);
  179. return driver_->sendControlPacket(0x21, 9, 0x201, 0, 48, txbuf_);
  180. }
  181. //*****************************************************************************
  182. // Support for Joysticks that Use HID data.
  183. //*****************************************************************************
  184. hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  185. {
  186. // only claim Desktop/Joystick and Desktop/Gamepad
  187. if (topusage != 0x10004 && topusage != 0x10005) return CLAIM_NO;
  188. // only claim from one physical device
  189. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  190. mydevice = dev;
  191. collections_claimed++;
  192. anychange = true; // always report values on first read
  193. driver_ = driver; // remember the driver.
  194. driver_->setTXBuffers(txbuf_, nullptr, sizeof(txbuf_));
  195. // Lets see if we know what type of joystick this is. That is, is it a PS3 or PS4 or ...
  196. joystickType = mapVIDPIDtoJoystickType(mydevice->idVendor, mydevice->idProduct, false);
  197. switch (joystickType) {
  198. case PS3:
  199. additional_axis_usage_page_ = 0x1;
  200. additional_axis_usage_start_ = 0x100;
  201. additional_axis_usage_count_ = 39;
  202. axis_change_notify_mask_ = (uint64_t)-1; // Start off assume all bits
  203. break;
  204. case PS4:
  205. additional_axis_usage_page_ = 0xFF00;
  206. additional_axis_usage_start_ = 0x21;
  207. additional_axis_usage_count_ = 54;
  208. axis_change_notify_mask_ = (uint64_t)0xfffffffffffff3ffl; // Start off assume all bits - 10 and 11
  209. break;
  210. default:
  211. additional_axis_usage_page_ = 0;
  212. additional_axis_usage_start_ = 0;
  213. additional_axis_usage_count_ = 0;
  214. axis_change_notify_mask_ = 0x3ff; // Start off assume only the 10 bits...
  215. }
  216. Serial.printf("Claim Additional axis: %x %x %d\n", additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  217. return CLAIM_REPORT;
  218. }
  219. void JoystickController::disconnect_collection(Device_t *dev)
  220. {
  221. if (--collections_claimed == 0) {
  222. mydevice = NULL;
  223. driver_ = nullptr;
  224. axis_mask_ = 0;
  225. axis_changed_mask_ = 0;
  226. }
  227. }
  228. void JoystickController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  229. {
  230. // TODO: set up translation from logical min/max to consistent 16 bit scale
  231. }
  232. void JoystickController::hid_input_data(uint32_t usage, int32_t value)
  233. {
  234. //Serial.printf("Joystick: usage=%X, value=%d\n", usage, value);
  235. uint32_t usage_page = usage >> 16;
  236. usage &= 0xFFFF;
  237. if (usage_page == 9 && usage >= 1 && usage <= 32) {
  238. uint32_t bit = 1 << (usage -1);
  239. if (value == 0) {
  240. if (buttons & bit) {
  241. buttons &= ~bit;
  242. anychange = true;
  243. }
  244. } else {
  245. if (!(buttons & bit)) {
  246. buttons |= bit;
  247. anychange = true;
  248. }
  249. }
  250. } else if (usage_page == 1 && usage >= 0x30 && usage <= 0x39) {
  251. // TODO: need scaling of value to consistent API, 16 bit signed?
  252. // TODO: many joysticks repeat slider usage. Detect & map to axis?
  253. uint32_t i = usage - 0x30;
  254. axis_mask_ |= (1 << i); // Keep record of which axis we have data on.
  255. if (axis[i] != value) {
  256. axis[i] = value;
  257. axis_changed_mask_ |= (1 << i);
  258. if (axis_changed_mask_ & axis_change_notify_mask_)
  259. anychange = true;
  260. }
  261. } else if (usage_page == additional_axis_usage_page_) {
  262. // see if the usage is witin range.
  263. //Serial.printf("UP: usage_page=%x usage=%x User: %x %d\n", usage_page, usage, user_buttons_usage_start, user_buttons_count_);
  264. if ((usage >= additional_axis_usage_start_) && (usage < (additional_axis_usage_start_ + additional_axis_usage_count_))) {
  265. // We are in the user range.
  266. uint16_t usage_index = usage - additional_axis_usage_start_ + STANDARD_AXIS_COUNT;
  267. if (usage_index < (sizeof(axis)/sizeof(axis[0]))) {
  268. if (axis[usage_index] != value) {
  269. axis[usage_index] = value;
  270. if (usage_index > 63) usage_index = 63; // don't overflow our mask
  271. axis_changed_mask_ |= ((uint64_t)1 << usage_index); // Keep track of which ones changed.
  272. if (axis_changed_mask_ & axis_change_notify_mask_)
  273. anychange = true; // We have changes...
  274. }
  275. axis_mask_ |= ((uint64_t)1 << usage_index); // Keep record of which axis we have data on.
  276. }
  277. //Serial.printf("UB: index=%x value=%x\n", usage_index, value);
  278. }
  279. } else {
  280. Serial.printf("UP: usage_page=%x usage=%x add: %x %x %d\n", usage_page, usage, additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  281. }
  282. // TODO: hat switch?
  283. }
  284. void JoystickController::hid_input_end()
  285. {
  286. if (anychange) {
  287. joystickEvent = true;
  288. }
  289. }
  290. bool JoystickController::hid_process_out_data(const Transfer_t *transfer)
  291. {
  292. Serial.printf("JoystickController::hid_process_out_data\n");
  293. return true;
  294. }
  295. void JoystickController::joystickDataClear() {
  296. joystickEvent = false;
  297. anychange = false;
  298. axis_changed_mask_ = 0;
  299. axis_mask_ = 0;
  300. }
  301. //*****************************************************************************
  302. // Support for Joysticks that are class specific and do not use HID
  303. // Example: XBox One controller.
  304. //*****************************************************************************
  305. static uint8_t start_input[] = {0x05, 0x20, 0x00, 0x01, 0x00};
  306. bool JoystickController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  307. {
  308. println("JoystickController claim this=", (uint32_t)this, HEX);
  309. // only claim at device level
  310. if (type != 0) return false;
  311. print_hexbytes(descriptors, len);
  312. JoystickController::joytype_t jtype = mapVIDPIDtoJoystickType(dev->idVendor, dev->idProduct, true);
  313. println("Jtype=", (uint8_t)jtype, DEC);
  314. if (jtype == UNKNOWN)
  315. return false;
  316. // 0 1 2 3 4 5 6 7 8 *9 10 1 2 3 4 5 *6 7 8 9 20 1 2 3 4 5 6 7 8 9 30 1...
  317. // 09 04 00 00 02 FF 47 D0 00 07 05 02 03 40 00 04 07 05 82 03 40 00 04 09 04 01 00 00 FF 47 D0 00
  318. // Lets do some verifications to make sure.
  319. if (len < 9+7+7) return false;
  320. uint32_t count_end_points = descriptors[4];
  321. if (count_end_points < 2) return false;
  322. if (descriptors[5] != 0xff) return false; // bInterfaceClass, 3 = HID
  323. uint32_t rxep = 0;
  324. uint32_t txep = 0;
  325. rx_size_ = 0;
  326. tx_size_ = 0;
  327. uint32_t descriptor_index = 9;
  328. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  329. if (descriptors[descriptor_index] != 7) return false; // length 7
  330. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  331. if ((descriptors[descriptor_index+3] == 3) // Type 3...
  332. && (descriptors[descriptor_index+4] <= 64)
  333. && (descriptors[descriptor_index+5] == 0)) {
  334. // have a bulk EP size
  335. if (descriptors[descriptor_index+2] & 0x80 ) {
  336. rxep = descriptors[descriptor_index+2];
  337. rx_size_ = descriptors[descriptor_index+4];
  338. } else {
  339. txep = descriptors[descriptor_index+2];
  340. tx_size_ = descriptors[descriptor_index+4];
  341. }
  342. }
  343. descriptor_index += 7; // setup to look at next one...
  344. }
  345. if ((rxep == 0) || (txep == 0)) return false; // did not find two end points.
  346. print("JoystickController, rxep=", rxep & 15);
  347. print("(", rx_size_);
  348. print("), txep=", txep);
  349. print("(", tx_size_);
  350. println(")");
  351. rxpipe_ = new_Pipe(dev, 2, rxep & 15, 1, rx_size_);
  352. if (!rxpipe_) return false;
  353. txpipe_ = new_Pipe(dev, 2, txep, 0, tx_size_);
  354. if (!txpipe_) {
  355. //free_Pipe(rxpipe_);
  356. return false;
  357. }
  358. rxpipe_->callback_function = rx_callback;
  359. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  360. txpipe_->callback_function = tx_callback;
  361. queue_Data_Transfer(txpipe_, start_input, sizeof(start_input), this);
  362. memset(axis, 0, sizeof(axis)); // clear out any data.
  363. joystickType = jtype; // remember we are an XBox One.
  364. return true;
  365. }
  366. void JoystickController::control(const Transfer_t *transfer)
  367. {
  368. }
  369. /************************************************************/
  370. // Interrupt-based Data Movement
  371. /************************************************************/
  372. void JoystickController::rx_callback(const Transfer_t *transfer)
  373. {
  374. if (!transfer->driver) return;
  375. ((JoystickController *)(transfer->driver))->rx_data(transfer);
  376. }
  377. void JoystickController::tx_callback(const Transfer_t *transfer)
  378. {
  379. if (!transfer->driver) return;
  380. ((JoystickController *)(transfer->driver))->tx_data(transfer);
  381. }
  382. /************************************************************/
  383. // Interrupt-based Data Movement
  384. // XBox one input data when type == 0x20
  385. // Information came from several places on the web including:
  386. // https://github.com/quantus/xbox-one-controller-protocol
  387. /************************************************************/
  388. typedef struct {
  389. uint8_t type;
  390. uint8_t const_0;
  391. uint16_t id;
  392. // From online references button order:
  393. // sync, dummy, start, back, a, b, x, y
  394. // dpad up, down left, right
  395. // lb, rb, left stick, right stick
  396. // Axis:
  397. // lt, rt, lx, xy, rx, ry
  398. //
  399. uint16_t buttons;
  400. int16_t axis[6];
  401. } xbox1data20_t;
  402. static const uint8_t xbox_axis_order_mapping[] = {4, 5, 0, 1, 2, 3};
  403. void JoystickController::rx_data(const Transfer_t *transfer)
  404. {
  405. // print("JoystickController::rx_data: ");
  406. // print_hexbytes((uint8_t*)transfer->buffer, transfer->length);
  407. axis_mask_ = 0x3f;
  408. axis_changed_mask_ = 0; // assume none for now
  409. xbox1data20_t *xb1d = (xbox1data20_t *)transfer->buffer;
  410. if ((xb1d->type == 0x20) && (transfer->length >= sizeof (xbox1data20_t))) {
  411. // We have a data transfer. Lets see what is new...
  412. if (xb1d->buttons != buttons) {
  413. buttons = xb1d->buttons;
  414. anychange = true;
  415. }
  416. for (uint8_t i = 0; i < sizeof (xbox_axis_order_mapping); i++) {
  417. // The first two values were unsigned.
  418. int axis_value = (i < 2)? (int)(uint16_t)xb1d->axis[i] : xb1d->axis[i];
  419. if (axis_value != axis[xbox_axis_order_mapping[i]]) {
  420. axis[xbox_axis_order_mapping[i]] = axis_value;
  421. anychange = true;
  422. }
  423. }
  424. joystickEvent = true;
  425. }
  426. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  427. }
  428. void JoystickController::tx_data(const Transfer_t *transfer)
  429. {
  430. }
  431. void JoystickController::disconnect()
  432. {
  433. axis_mask_ = 0;
  434. axis_changed_mask_ = 0;
  435. // TODO: free resources
  436. }