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.

1045 lines
30 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /* gc_n64_usb : Gamecube or N64 controller to USB adapter firmware
  2. Copyright (C) 2007-2016 Raphael Assenat <raph@raphnet.net>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <avr/io.h>
  18. #include <avr/interrupt.h>
  19. #include <util/delay.h>
  20. #include "main.h"
  21. #include "util.h"
  22. #include "usart1.h"
  23. #include "usb.h"
  24. #include "gamepads.h"
  25. #include "bootloader.h"
  26. #include "gcn64_protocol.h"
  27. #include "n64.h"
  28. #include "gamecube.h"
  29. #include "usbpad.h"
  30. #include "eeprom.h"
  31. #include "hiddata.h"
  32. #include "usbstrings.h"
  33. #include "intervaltimer.h"
  34. #include "intervaltimer2.h"
  35. #include "requests.h"
  36. #include "stkchk.h"
  37. #define MAX_PLAYERS 2
  38. #define GCN64_USB_PID 0x0060
  39. #define N64_USB_PID 0x0061
  40. #define GC_USB_PID 0x0062
  41. #define DUAL_GCN64_USB_PID 0x0063
  42. #define DUAL_N64_USB_PID 0x0064
  43. #define DUAL_GC_USB_PID 0x0065
  44. #define KEYBOARD_PID 0x0066
  45. #define KEYBOARD_PID2 0x0067
  46. #define KEYBOARD_JS_PID 0x0068
  47. int keyboard_main(void);
  48. /* Those .c files are included rather than linked for we
  49. * want the sizeof() operator to work on the arrays */
  50. #include "reportdesc.c"
  51. #include "dataHidReport.c"
  52. #define MAX_READ_ERRORS 30
  53. static uint8_t error_count[MAX_PLAYERS] = { };
  54. struct cfg0 {
  55. struct usb_configuration_descriptor configdesc;
  56. struct usb_interface_descriptor interface;
  57. struct usb_hid_descriptor hid;
  58. struct usb_endpoint_descriptor ep1_in;
  59. struct usb_interface_descriptor interface_admin;
  60. struct usb_hid_descriptor hid_data;
  61. struct usb_endpoint_descriptor ep2_in;
  62. };
  63. static const struct cfg0 cfg0 PROGMEM = {
  64. .configdesc = {
  65. .bLength = sizeof(struct usb_configuration_descriptor),
  66. .bDescriptorType = CONFIGURATION_DESCRIPTOR,
  67. .wTotalLength = sizeof(cfg0), // includes all descriptors returned together
  68. .bNumInterfaces = 1 + 1, // one interface per player + one management interface
  69. .bConfigurationValue = 1,
  70. .bmAttributes = CFG_DESC_ATTR_RESERVED, // set Self-powred and remote-wakeup here if needed.
  71. .bMaxPower = 25, // for 50mA
  72. },
  73. // Main interface, HID (player 1)
  74. .interface = {
  75. .bLength = sizeof(struct usb_interface_descriptor),
  76. .bDescriptorType = INTERFACE_DESCRIPTOR,
  77. .bInterfaceNumber = 0,
  78. .bAlternateSetting = 0,
  79. .bNumEndpoints = 1,
  80. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  81. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  82. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  83. },
  84. .hid = {
  85. .bLength = sizeof(struct usb_hid_descriptor),
  86. .bDescriptorType = HID_DESCRIPTOR,
  87. .bcdHid = 0x0101,
  88. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  89. .bNumDescriptors = 1, // Only a report descriptor
  90. .bClassDescriptorType = REPORT_DESCRIPTOR,
  91. .wClassDescriptorLength = sizeof(gcn64_usbHidReportDescriptor),
  92. },
  93. .ep1_in = {
  94. .bLength = sizeof(struct usb_endpoint_descriptor),
  95. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  96. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 1, // 0x81
  97. .bmAttributes = TRANSFER_TYPE_INT,
  98. .wMaxPacketsize = 16,
  99. .bInterval = LS_FS_INTERVAL_MS(1),
  100. },
  101. // Second HID interface for config and update
  102. .interface_admin = {
  103. .bLength = sizeof(struct usb_interface_descriptor),
  104. .bDescriptorType = INTERFACE_DESCRIPTOR,
  105. .bInterfaceNumber = 1,
  106. .bAlternateSetting = 0,
  107. .bNumEndpoints = 1,
  108. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  109. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  110. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  111. },
  112. .hid_data = {
  113. .bLength = sizeof(struct usb_hid_descriptor),
  114. .bDescriptorType = HID_DESCRIPTOR,
  115. .bcdHid = 0x0101,
  116. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  117. .bNumDescriptors = 1, // Only a report descriptor
  118. .bClassDescriptorType = REPORT_DESCRIPTOR,
  119. .wClassDescriptorLength = sizeof(dataHidReport),
  120. },
  121. .ep2_in = {
  122. .bLength = sizeof(struct usb_endpoint_descriptor),
  123. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  124. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 2, // 0x82
  125. .bmAttributes = TRANSFER_TYPE_INT,
  126. .wMaxPacketsize = 64,
  127. .bInterval = LS_FS_INTERVAL_MS(1),
  128. },
  129. };
  130. static const struct cfg0 cfg0_kb PROGMEM = {
  131. .configdesc = {
  132. .bLength = sizeof(struct usb_configuration_descriptor),
  133. .bDescriptorType = CONFIGURATION_DESCRIPTOR,
  134. .wTotalLength = sizeof(cfg0), // includes all descriptors returned together
  135. .bNumInterfaces = 1 + 1, // one interface per player + one management interface
  136. .bConfigurationValue = 1,
  137. .bmAttributes = CFG_DESC_ATTR_RESERVED, // set Self-powred and remote-wakeup here if needed.
  138. .bMaxPower = 25, // for 50mA
  139. },
  140. // Main interface, HID Keyboard
  141. .interface = {
  142. .bLength = sizeof(struct usb_interface_descriptor),
  143. .bDescriptorType = INTERFACE_DESCRIPTOR,
  144. .bInterfaceNumber = 0,
  145. .bAlternateSetting = 0,
  146. .bNumEndpoints = 1,
  147. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  148. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  149. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  150. },
  151. .hid = {
  152. .bLength = sizeof(struct usb_hid_descriptor),
  153. .bDescriptorType = HID_DESCRIPTOR,
  154. .bcdHid = 0x0101,
  155. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  156. .bNumDescriptors = 1, // Only a report descriptor
  157. .bClassDescriptorType = REPORT_DESCRIPTOR,
  158. .wClassDescriptorLength = sizeof(gcKeyboardReport),
  159. },
  160. .ep1_in = {
  161. .bLength = sizeof(struct usb_endpoint_descriptor),
  162. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  163. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 1, // 0x81
  164. .bmAttributes = TRANSFER_TYPE_INT,
  165. .wMaxPacketsize = 16,
  166. .bInterval = LS_FS_INTERVAL_MS(1),
  167. },
  168. // Second HID interface for config and update
  169. .interface_admin = {
  170. .bLength = sizeof(struct usb_interface_descriptor),
  171. .bDescriptorType = INTERFACE_DESCRIPTOR,
  172. .bInterfaceNumber = 1,
  173. .bAlternateSetting = 0,
  174. .bNumEndpoints = 1,
  175. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  176. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  177. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  178. },
  179. .hid_data = {
  180. .bLength = sizeof(struct usb_hid_descriptor),
  181. .bDescriptorType = HID_DESCRIPTOR,
  182. .bcdHid = 0x0101,
  183. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  184. .bNumDescriptors = 1, // Only a report descriptor
  185. .bClassDescriptorType = REPORT_DESCRIPTOR,
  186. .wClassDescriptorLength = sizeof(dataHidReport),
  187. },
  188. .ep2_in = {
  189. .bLength = sizeof(struct usb_endpoint_descriptor),
  190. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  191. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 2, // 0x82
  192. .bmAttributes = TRANSFER_TYPE_INT,
  193. .wMaxPacketsize = 64,
  194. .bInterval = LS_FS_INTERVAL_MS(1),
  195. },
  196. };
  197. struct cfg0_2p {
  198. struct usb_configuration_descriptor configdesc;
  199. struct usb_interface_descriptor interface;
  200. struct usb_hid_descriptor hid;
  201. struct usb_endpoint_descriptor ep1_in;
  202. struct usb_interface_descriptor interface_p2;
  203. struct usb_hid_descriptor hid_p2;
  204. struct usb_endpoint_descriptor ep2_in;
  205. struct usb_interface_descriptor interface_admin;
  206. struct usb_hid_descriptor hid_data;
  207. struct usb_endpoint_descriptor ep3_in;
  208. };
  209. static const struct cfg0_2p cfg0_2p PROGMEM = {
  210. .configdesc = {
  211. .bLength = sizeof(struct usb_configuration_descriptor),
  212. .bDescriptorType = CONFIGURATION_DESCRIPTOR,
  213. .wTotalLength = sizeof(cfg0_2p), // includes all descriptors returned together
  214. .bNumInterfaces = 2 + 1, // one interface per player + one management interface
  215. .bConfigurationValue = 1,
  216. .bmAttributes = CFG_DESC_ATTR_RESERVED, // set Self-powred and remote-wakeup here if needed.
  217. .bMaxPower = 25, // for 50mA
  218. },
  219. // Main interface, HID (player 1)
  220. .interface = {
  221. .bLength = sizeof(struct usb_interface_descriptor),
  222. .bDescriptorType = INTERFACE_DESCRIPTOR,
  223. .bInterfaceNumber = 0,
  224. .bAlternateSetting = 0,
  225. .bNumEndpoints = 1,
  226. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  227. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  228. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  229. },
  230. .hid = {
  231. .bLength = sizeof(struct usb_hid_descriptor),
  232. .bDescriptorType = HID_DESCRIPTOR,
  233. .bcdHid = 0x0101,
  234. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  235. .bNumDescriptors = 1, // Only a report descriptor
  236. .bClassDescriptorType = REPORT_DESCRIPTOR,
  237. .wClassDescriptorLength = sizeof(gcn64_usbHidReportDescriptor),
  238. },
  239. .ep1_in = {
  240. .bLength = sizeof(struct usb_endpoint_descriptor),
  241. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  242. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 1, // 0x81
  243. .bmAttributes = TRANSFER_TYPE_INT,
  244. .wMaxPacketsize = 16,
  245. .bInterval = LS_FS_INTERVAL_MS(1),
  246. },
  247. // Main interface, HID (player 2)
  248. .interface_p2 = {
  249. .bLength = sizeof(struct usb_interface_descriptor),
  250. .bDescriptorType = INTERFACE_DESCRIPTOR,
  251. .bInterfaceNumber = 1,
  252. .bAlternateSetting = 0,
  253. .bNumEndpoints = 1,
  254. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  255. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  256. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  257. },
  258. .hid_p2 = {
  259. .bLength = sizeof(struct usb_hid_descriptor),
  260. .bDescriptorType = HID_DESCRIPTOR,
  261. .bcdHid = 0x0101,
  262. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  263. .bNumDescriptors = 1, // Only a report descriptor
  264. .bClassDescriptorType = REPORT_DESCRIPTOR,
  265. .wClassDescriptorLength = sizeof(gcn64_usbHidReportDescriptor),
  266. },
  267. .ep2_in = {
  268. .bLength = sizeof(struct usb_endpoint_descriptor),
  269. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  270. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 2, // 0x82
  271. .bmAttributes = TRANSFER_TYPE_INT,
  272. .wMaxPacketsize = 16,
  273. .bInterval = LS_FS_INTERVAL_MS(1),
  274. },
  275. // Second HID interface for config and update
  276. .interface_admin = {
  277. .bLength = sizeof(struct usb_interface_descriptor),
  278. .bDescriptorType = INTERFACE_DESCRIPTOR,
  279. .bInterfaceNumber = 2,
  280. .bAlternateSetting = 0,
  281. .bNumEndpoints = 1,
  282. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  283. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  284. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  285. },
  286. .hid_data = {
  287. .bLength = sizeof(struct usb_hid_descriptor),
  288. .bDescriptorType = HID_DESCRIPTOR,
  289. .bcdHid = 0x0101,
  290. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  291. .bNumDescriptors = 1, // Only a report descriptor
  292. .bClassDescriptorType = REPORT_DESCRIPTOR,
  293. .wClassDescriptorLength = sizeof(dataHidReport),
  294. },
  295. .ep3_in = {
  296. .bLength = sizeof(struct usb_endpoint_descriptor),
  297. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  298. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 3, // 0x83
  299. .bmAttributes = TRANSFER_TYPE_INT,
  300. .wMaxPacketsize = 64,
  301. .bInterval = LS_FS_INTERVAL_MS(1),
  302. },
  303. };
  304. static const struct cfg0_2p cfg0_2p_keyboard PROGMEM = {
  305. .configdesc = {
  306. .bLength = sizeof(struct usb_configuration_descriptor),
  307. .bDescriptorType = CONFIGURATION_DESCRIPTOR,
  308. .wTotalLength = sizeof(cfg0_2p), // includes all descriptors returned together
  309. .bNumInterfaces = 2 + 1, // one interface per player + one management interface
  310. .bConfigurationValue = 1,
  311. .bmAttributes = CFG_DESC_ATTR_RESERVED, // set Self-powred and remote-wakeup here if needed.
  312. .bMaxPower = 25, // for 50mA
  313. },
  314. // Joystick interface
  315. .interface = {
  316. .bLength = sizeof(struct usb_interface_descriptor),
  317. .bDescriptorType = INTERFACE_DESCRIPTOR,
  318. .bInterfaceNumber = 0,
  319. .bAlternateSetting = 0,
  320. .bNumEndpoints = 1,
  321. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  322. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  323. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  324. },
  325. .hid = {
  326. .bLength = sizeof(struct usb_hid_descriptor),
  327. .bDescriptorType = HID_DESCRIPTOR,
  328. .bcdHid = 0x0101,
  329. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  330. .bNumDescriptors = 1, // Only a report descriptor
  331. .bClassDescriptorType = REPORT_DESCRIPTOR,
  332. .wClassDescriptorLength = sizeof(gcn64_usbHidReportDescriptor),
  333. },
  334. .ep1_in = {
  335. .bLength = sizeof(struct usb_endpoint_descriptor),
  336. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  337. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 1, // 0x81
  338. .bmAttributes = TRANSFER_TYPE_INT,
  339. .wMaxPacketsize = 16,
  340. .bInterval = LS_FS_INTERVAL_MS(1),
  341. },
  342. // HID Keyboard interface
  343. .interface_p2 = {
  344. .bLength = sizeof(struct usb_interface_descriptor),
  345. .bDescriptorType = INTERFACE_DESCRIPTOR,
  346. .bInterfaceNumber = 1,
  347. .bAlternateSetting = 0,
  348. .bNumEndpoints = 1,
  349. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  350. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  351. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  352. },
  353. .hid_p2 = {
  354. .bLength = sizeof(struct usb_hid_descriptor),
  355. .bDescriptorType = HID_DESCRIPTOR,
  356. .bcdHid = 0x0101,
  357. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  358. .bNumDescriptors = 1, // Only a report descriptor
  359. .bClassDescriptorType = REPORT_DESCRIPTOR,
  360. .wClassDescriptorLength = sizeof(gcKeyboardReport),
  361. },
  362. .ep2_in = {
  363. .bLength = sizeof(struct usb_endpoint_descriptor),
  364. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  365. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 2, // 0x82
  366. .bmAttributes = TRANSFER_TYPE_INT,
  367. .wMaxPacketsize = 16,
  368. .bInterval = LS_FS_INTERVAL_MS(1),
  369. },
  370. // Second HID interface for config and update
  371. .interface_admin = {
  372. .bLength = sizeof(struct usb_interface_descriptor),
  373. .bDescriptorType = INTERFACE_DESCRIPTOR,
  374. .bInterfaceNumber = 2,
  375. .bAlternateSetting = 0,
  376. .bNumEndpoints = 1,
  377. .bInterfaceClass = USB_DEVICE_CLASS_HID,
  378. .bInterfaceSubClass = HID_SUBCLASS_NONE,
  379. .bInterfaceProtocol = HID_PROTOCOL_NONE,
  380. },
  381. .hid_data = {
  382. .bLength = sizeof(struct usb_hid_descriptor),
  383. .bDescriptorType = HID_DESCRIPTOR,
  384. .bcdHid = 0x0101,
  385. .bCountryCode = HID_COUNTRY_NOT_SUPPORTED,
  386. .bNumDescriptors = 1, // Only a report descriptor
  387. .bClassDescriptorType = REPORT_DESCRIPTOR,
  388. .wClassDescriptorLength = sizeof(dataHidReport),
  389. },
  390. .ep3_in = {
  391. .bLength = sizeof(struct usb_endpoint_descriptor),
  392. .bDescriptorType = ENDPOINT_DESCRIPTOR,
  393. .bEndpointAddress = USB_RQT_DEVICE_TO_HOST | 3, // 0x83
  394. .bmAttributes = TRANSFER_TYPE_INT,
  395. .wMaxPacketsize = 64,
  396. .bInterval = LS_FS_INTERVAL_MS(1),
  397. },
  398. };
  399. struct usb_device_descriptor device_descriptor = {
  400. .bLength = sizeof(struct usb_device_descriptor),
  401. .bDescriptorType = DEVICE_DESCRIPTOR,
  402. .bcdUSB = 0x0110,
  403. .bDeviceClass = 0, // set at interface
  404. .bDeviceSubClass = 0, // set at interface
  405. .bDeviceProtocol = 0,
  406. .bMaxPacketSize = 64,
  407. .idVendor = 0x289B,
  408. .idProduct = GCN64_USB_PID,
  409. .bcdDevice = VERSIONBCD,
  410. .bNumConfigurations = 1,
  411. .iManufacturer = 1,
  412. .iProduct = 2,
  413. .iSerialNumber = 3,
  414. };
  415. /** **/
  416. static uint16_t _usbpad_hid_get_report(void *ctx, struct usb_request *rq, const uint8_t **dat)
  417. {
  418. return usbpad_hid_get_report((struct usbpad*)ctx, rq, dat);
  419. }
  420. static uint8_t _usbpad_hid_set_report(void *ctx, const struct usb_request *rq, const uint8_t *dat, uint16_t len)
  421. {
  422. return usbpad_hid_set_report((struct usbpad*)ctx, rq, dat, len);
  423. }
  424. static struct usb_parameters usb_params = {
  425. .flags = USB_PARAM_FLAG_CONFDESC_PROGMEM |
  426. USB_PARAM_FLAG_REPORTDESC_PROGMEM,
  427. .devdesc = (PGM_VOID_P)&device_descriptor,
  428. .configdesc = (PGM_VOID_P)&cfg0, // Patched in main() for two players
  429. .configdesc_ttllen = sizeof(cfg0), // Patched in main() for two players
  430. .num_strings = NUM_USB_STRINGS,
  431. .strings = g_usb_strings,
  432. .n_hid_interfaces = 1 + 1, // One per player + one management interface (patched in main() for two players)
  433. .hid_params = {
  434. [0] = {
  435. .reportdesc = gcn64_usbHidReportDescriptor,
  436. .reportdesc_len = sizeof(gcn64_usbHidReportDescriptor),
  437. .getReport = _usbpad_hid_get_report,
  438. .setReport = _usbpad_hid_set_report,
  439. .endpoint_size = 16,
  440. },
  441. [1] = {
  442. .reportdesc = dataHidReport,
  443. .reportdesc_len = sizeof(dataHidReport),
  444. .getReport = hiddata_get_report,
  445. .setReport = hiddata_set_report,
  446. .endpoint_size = 64,
  447. },
  448. },
  449. };
  450. void hwinit(void)
  451. {
  452. /* PORTB
  453. *
  454. * 7: NC Output low
  455. * 6: NC Output low
  456. * 5: NC Output low
  457. * 4: NC Output low
  458. * 3: MISO Output low
  459. * 2: MOSI Output low
  460. * 1: SCK Output low
  461. * 0: NC Output low
  462. */
  463. PORTB = 0x00;
  464. DDRB = 0xFF;
  465. /* PORTC
  466. *
  467. * 7: NC Output low
  468. * 6: NC Output low
  469. * 5: NC Output low
  470. * 4: NC Output low
  471. * 3: (no such pin)
  472. * 2: NC Output low
  473. * 1: RESET (N/A: Reset input per fuses) (left floating)
  474. * 0: XTAL2 (N/A: Crystal oscillator) (left floating)
  475. */
  476. DDRC = 0xfc;
  477. PORTC = 0x00;
  478. /* PORTD
  479. *
  480. * 7: HWB Input (external pull-up)
  481. * 6: NC Output low
  482. * 5: NC Output low
  483. * 4: NC Output low
  484. * 3: IO3_MCU Input
  485. * 2: IO2_MCU Input
  486. * 1: IO1_MCU Input
  487. * 0: IO0_MCU Input
  488. */
  489. PORTD = 0x00;
  490. DDRD = 0x70;
  491. // System clock. External crystal is 16 Mhz and we want
  492. // to run at max. speed.
  493. CLKPR = 0x80;
  494. CLKPR = 0x0; // Division factor of 1
  495. PRR0 = 0;
  496. PRR1 = 0;
  497. }
  498. uint8_t num_players = 1;
  499. unsigned char current_pad_type[NUM_CHANNELS] = { };
  500. Gamepad *detectPad(unsigned char chn)
  501. {
  502. current_pad_type[chn] = gcn64_detectController(chn);
  503. switch (current_pad_type[chn])
  504. {
  505. case CONTROLLER_IS_ABSENT:
  506. case CONTROLLER_IS_UNKNOWN:
  507. return NULL;
  508. case CONTROLLER_IS_N64_MOUSE:
  509. case CONTROLLER_IS_N64:
  510. return n64GetGamepad();
  511. case CONTROLLER_IS_GC:
  512. return gamecubeGetGamepad();
  513. case CONTROLLER_IS_GC_KEYBOARD:
  514. return gamecubeGetKeyboard();
  515. }
  516. return NULL;
  517. }
  518. /* Called after eeprom content is loaded. */
  519. void eeprom_app_ready(void)
  520. {
  521. static wchar_t serial_from_eeprom[SERIAL_NUM_LEN+1];
  522. int i;
  523. for (i=0; i<SERIAL_NUM_LEN; i++) {
  524. serial_from_eeprom[i] = g_eeprom_data.cfg.serial[i];
  525. }
  526. serial_from_eeprom[i] = 0;
  527. g_usb_strings[USB_STRING_SERIAL_IDX] = serial_from_eeprom;
  528. }
  529. static struct usbpad usbpads[MAX_PLAYERS];
  530. static char g_polling_suspended = 0;
  531. static void setSuspendPolling(uint8_t suspend)
  532. {
  533. g_polling_suspended = suspend;
  534. }
  535. static void forceVibration(uint8_t channel, uint8_t force)
  536. {
  537. if (channel < MAX_PLAYERS) {
  538. usbpad_forceVibrate(&usbpads[channel], force);
  539. }
  540. }
  541. static uint8_t getSupportedModes(uint8_t *dst)
  542. {
  543. uint8_t idx = 0;
  544. switch (g_eeprom_data.cfg.mode)
  545. {
  546. // Allow toggling between keyboard and joystick modes on
  547. // single-port gamecube adapter
  548. case CFG_MODE_GC_ONLY:
  549. case CFG_MODE_KEYBOARD:
  550. dst[idx++] = CFG_MODE_GC_ONLY;
  551. dst[idx++] = CFG_MODE_KEYBOARD;
  552. break;
  553. // Allow toggling between two joysticks and joystick + keyboard modes
  554. // on dual-port gamecube adapter
  555. case CFG_MODE_2P_GC_ONLY:
  556. case CFG_MODE_KB_AND_JS:
  557. dst[idx++] = CFG_MODE_2P_GC_ONLY;
  558. dst[idx++] = CFG_MODE_KB_AND_JS;
  559. break;
  560. // On N64/GC adapters, there is a GC port so we should support
  561. // keyboards there. Use KEYBOARD_2 config here to avoid mixup
  562. // with the GC-only adapter variation.
  563. case CFG_MODE_STANDARD:
  564. case CFG_MODE_KEYBOARD_2:
  565. dst[idx++] = CFG_MODE_STANDARD;
  566. dst[idx++] = CFG_MODE_KEYBOARD_2;
  567. break;
  568. default:
  569. dst[idx++] = CFG_MODE_STANDARD;
  570. dst[idx++] = CFG_MODE_N64_ONLY;
  571. dst[idx++] = CFG_MODE_GC_ONLY;
  572. dst[idx++] = CFG_MODE_2P_STANDARD;
  573. dst[idx++] = CFG_MODE_2P_N64_ONLY;
  574. dst[idx++] = CFG_MODE_2P_GC_ONLY;
  575. dst[idx++] = CFG_MODE_KEYBOARD;
  576. dst[idx++] = CFG_MODE_KB_AND_JS;
  577. break;
  578. }
  579. return idx;
  580. }
  581. static struct hiddata_ops hiddata_ops = {
  582. .suspendPolling = setSuspendPolling,
  583. .forceVibration = forceVibration,
  584. .getSupportedModes = getSupportedModes,
  585. };
  586. #define STATE_WAIT_POLLTIME 0
  587. #define STATE_POLL_PAD 1
  588. #define STATE_WAIT_INTERRUPT_READY 2
  589. #define STATE_TRANSMIT 3
  590. #define STATE_WAIT_INTERRUPT_READY_P2 4
  591. #define STATE_TRANSMIT_P2 5
  592. int main(void)
  593. {
  594. Gamepad *pads[MAX_PLAYERS] = { };
  595. gamepad_data pad_data;
  596. uint8_t gamepad_vibrate = 0;
  597. uint8_t state = STATE_WAIT_POLLTIME;
  598. uint8_t channel;
  599. uint8_t i;
  600. hwinit();
  601. usart1_init();
  602. eeprom_init();
  603. intervaltimer_init();
  604. intervaltimer2_init();
  605. stkchk_init();
  606. switch (g_eeprom_data.cfg.mode)
  607. {
  608. default:
  609. case CFG_MODE_STANDARD:
  610. usbstrings_changeProductString_P(PSTR("GC/N64 to USB v"VERSIONSTR_SHORT));
  611. break;
  612. case CFG_MODE_N64_ONLY:
  613. usbstrings_changeProductString_P(PSTR("N64 to USB v"VERSIONSTR_SHORT));
  614. device_descriptor.idProduct = N64_USB_PID;
  615. break;
  616. case CFG_MODE_GC_ONLY:
  617. usbstrings_changeProductString_P(PSTR("Gamecube to USB v"VERSIONSTR_SHORT));
  618. device_descriptor.idProduct = GC_USB_PID;
  619. break;
  620. case CFG_MODE_2P_STANDARD:
  621. usbstrings_changeProductString_P(PSTR("Dual GC/N64 to USB v"VERSIONSTR_SHORT));
  622. device_descriptor.idProduct = DUAL_GCN64_USB_PID;
  623. num_players = 2;
  624. break;
  625. case CFG_MODE_2P_N64_ONLY:
  626. usbstrings_changeProductString_P(PSTR("Dual N64 to USB v"VERSIONSTR_SHORT));
  627. device_descriptor.idProduct = DUAL_N64_USB_PID;
  628. num_players = 2;
  629. break;
  630. case CFG_MODE_2P_GC_ONLY:
  631. usbstrings_changeProductString_P(PSTR("Dual Gamecube to USB v"VERSIONSTR_SHORT));
  632. device_descriptor.idProduct = DUAL_GC_USB_PID;
  633. num_players = 2;
  634. break;
  635. case CFG_MODE_KB_AND_JS:
  636. case CFG_MODE_KEYBOARD:
  637. case CFG_MODE_KEYBOARD_2:
  638. keyboard_main();
  639. break;
  640. }
  641. // 2-players common
  642. if (num_players == 2) {
  643. usb_params.configdesc = (PGM_VOID_P)&cfg0_2p;
  644. usb_params.configdesc_ttllen = sizeof(cfg0_2p);
  645. usb_params.n_hid_interfaces = 3;
  646. // Move the management interface is the last position
  647. memcpy(usb_params.hid_params + 2, usb_params.hid_params + 1, sizeof(struct usb_hid_parameters));
  648. // Add a second player interface between them
  649. memcpy(usb_params.hid_params + 1, usb_params.hid_params + 0, sizeof(struct usb_hid_parameters));
  650. }
  651. for (i=0; i<num_players; i++) {
  652. usbpad_init(&usbpads[i]);
  653. usb_params.hid_params[i].ctx = &usbpads[i];
  654. }
  655. sei();
  656. usb_init(&usb_params);
  657. // Timebase for force feedback 'loop count'
  658. intervaltimer2_set16ms();
  659. while (1)
  660. {
  661. static char last_v[MAX_PLAYERS] = { };
  662. if (stkchk_verify()) {
  663. enterBootLoader();
  664. }
  665. usb_doTasks();
  666. hiddata_doTask(&hiddata_ops);
  667. // Run vibration tasks
  668. if (intervaltimer2_get()) {
  669. for (channel=0; channel < num_players; channel++) {
  670. usbpad_vibrationTask(&usbpads[channel]);
  671. }
  672. }
  673. switch(state)
  674. {
  675. case STATE_WAIT_POLLTIME:
  676. if (!g_polling_suspended) {
  677. intervaltimer_set(g_eeprom_data.cfg.poll_interval[0]);
  678. if (intervaltimer_get()) {
  679. state = STATE_POLL_PAD;
  680. }
  681. }
  682. break;
  683. case STATE_POLL_PAD:
  684. for (channel=0; channel<num_players; channel++)
  685. {
  686. /* Try to auto-detect controller if none*/
  687. if (!pads[channel]) {
  688. pads[channel] = detectPad(channel);
  689. if (pads[channel] && (pads[channel]->hotplug)) {
  690. // For gamecube, this make sure the next
  691. // analog values we read become the center
  692. // reference.
  693. pads[channel]->hotplug(channel);
  694. }
  695. }
  696. /* Read from the pad by calling update */
  697. if (pads[channel]) {
  698. if (pads[channel]->update(channel)) {
  699. error_count[channel]++;
  700. if (error_count[channel] > MAX_READ_ERRORS) {
  701. pads[channel] = NULL;
  702. error_count[channel] = 0;
  703. continue;
  704. }
  705. } else {
  706. error_count[channel]=0;
  707. }
  708. if (pads[channel]->changed(channel))
  709. {
  710. pads[channel]->getReport(channel, &pad_data);
  711. usbpad_update(&usbpads[channel], &pad_data);
  712. state = STATE_WAIT_INTERRUPT_READY;
  713. continue;
  714. }
  715. } else {
  716. /* Just make sure the gamepad state holds valid data
  717. * to appear inactive (no buttons and axes in neutral) */
  718. usbpad_update(&usbpads[channel], NULL);
  719. }
  720. }
  721. /* If there were change on any of the gamepads, state will
  722. * be set to STATE_WAIT_INTERRUPT_READY. Otherwise, go back
  723. * to WAIT_POLLTIME. */
  724. if (state == STATE_POLL_PAD) {
  725. state = STATE_WAIT_POLLTIME;
  726. }
  727. break;
  728. case STATE_WAIT_INTERRUPT_READY:
  729. /* Wait until one of the interrupt endpoint is ready */
  730. if (usb_interruptReady_ep1() || (num_players>1 && usb_interruptReady_ep2())) {
  731. state = STATE_TRANSMIT;
  732. }
  733. break;
  734. case STATE_TRANSMIT:
  735. if (usb_interruptReady_ep1()) {
  736. usb_interruptSend_ep1(usbpad_getReportBuffer(&usbpads[0]), usbpad_getReportSize());
  737. }
  738. if (num_players>1 && usb_interruptReady_ep2()) {
  739. usb_interruptSend_ep2(usbpad_getReportBuffer(&usbpads[1]), usbpad_getReportSize());
  740. }
  741. state = STATE_WAIT_POLLTIME;
  742. break;
  743. }
  744. for (channel=0; channel < num_players; channel++) {
  745. gamepad_vibrate = usbpad_mustVibrate(&usbpads[channel]);
  746. if (last_v[channel] != gamepad_vibrate) {
  747. if (pads[channel] && pads[channel]->setVibration) {
  748. pads[channel]->setVibration(channel, gamepad_vibrate);
  749. }
  750. last_v[channel] = gamepad_vibrate;
  751. }
  752. }
  753. }
  754. return 0;
  755. }
  756. int keyboard_main(void)
  757. {
  758. Gamepad *pads[MAX_PLAYERS] = { };
  759. gamepad_data pad_data;
  760. uint8_t gamepad_vibrate = 0;
  761. uint8_t state = STATE_WAIT_POLLTIME;
  762. uint8_t channel;
  763. uint8_t i;
  764. hwinit();
  765. usart1_init();
  766. eeprom_init();
  767. intervaltimer_init();
  768. intervaltimer2_init();
  769. stkchk_init();
  770. switch (g_eeprom_data.cfg.mode)
  771. {
  772. default:
  773. case CFG_MODE_KEYBOARD_2:
  774. usbstrings_changeProductString_P(PSTR("KB to USB v"VERSIONSTR_SHORT));
  775. device_descriptor.idProduct = KEYBOARD_PID2;
  776. usb_params.configdesc = (PGM_VOID_P)&cfg0_kb;
  777. usb_params.configdesc_ttllen = sizeof(cfg0_kb);
  778. // replace Joystick report descriptor by keyboard
  779. usb_params.hid_params[0].reportdesc = gcKeyboardReport;
  780. usb_params.hid_params[0].reportdesc_len = sizeof(gcKeyboardReport);
  781. break;
  782. case CFG_MODE_KEYBOARD:
  783. usbstrings_changeProductString_P(PSTR("GC KB to USB v"VERSIONSTR_SHORT));
  784. device_descriptor.idProduct = KEYBOARD_PID;
  785. usb_params.configdesc = (PGM_VOID_P)&cfg0_kb;
  786. usb_params.configdesc_ttllen = sizeof(cfg0_kb);
  787. // replace Joystick report descriptor by keyboard
  788. usb_params.hid_params[0].reportdesc = gcKeyboardReport;
  789. usb_params.hid_params[0].reportdesc_len = sizeof(gcKeyboardReport);
  790. break;
  791. case CFG_MODE_KB_AND_JS:
  792. usbstrings_changeProductString_P(PSTR("GC KB+JS to USB v"VERSIONSTR_SHORT));
  793. device_descriptor.idProduct = KEYBOARD_JS_PID;
  794. usb_params.configdesc = (PGM_VOID_P)&cfg0_2p_keyboard;
  795. usb_params.configdesc_ttllen = sizeof(cfg0_2p_keyboard);
  796. // Move the management interface to the last position
  797. memcpy(usb_params.hid_params + 2, usb_params.hid_params + 1, sizeof(struct usb_hid_parameters));
  798. // Add a second player interface between them (still a joystick)
  799. memcpy(usb_params.hid_params + 1, usb_params.hid_params + 0, sizeof(struct usb_hid_parameters));
  800. // Convert second Joystick report descriptor to a keyboard
  801. usb_params.hid_params[1].reportdesc = gcKeyboardReport;
  802. usb_params.hid_params[1].reportdesc_len = sizeof(gcKeyboardReport);
  803. usb_params.n_hid_interfaces = 3;
  804. num_players = 2;
  805. break;
  806. }
  807. for (i=0; i<num_players; i++) {
  808. usbpad_init(&usbpads[i]);
  809. usb_params.hid_params[i].ctx = &usbpads[i];
  810. }
  811. sei();
  812. usb_init(&usb_params);
  813. // Timebase for force feedback 'loop count'
  814. intervaltimer2_set16ms();
  815. while (1)
  816. {
  817. static char last_v[MAX_PLAYERS] = { };
  818. if (stkchk_verify()) {
  819. enterBootLoader();
  820. }
  821. usb_doTasks();
  822. hiddata_doTask(&hiddata_ops);
  823. // Run vibration tasks
  824. if (intervaltimer2_get()) {
  825. for (channel=0; channel < num_players; channel++) {
  826. usbpad_vibrationTask(&usbpads[channel]);
  827. }
  828. }
  829. switch(state)
  830. {
  831. case STATE_WAIT_POLLTIME:
  832. if (!g_polling_suspended) {
  833. intervaltimer_set(g_eeprom_data.cfg.poll_interval[0]);
  834. if (intervaltimer_get()) {
  835. state = STATE_POLL_PAD;
  836. }
  837. }
  838. break;
  839. case STATE_POLL_PAD:
  840. for (channel=0; channel<num_players; channel++)
  841. {
  842. /* Try to auto-detect controller if none*/
  843. if (!pads[channel]) {
  844. pads[channel] = detectPad(channel);
  845. if (pads[channel] && (pads[channel]->hotplug)) {
  846. // For gamecube, this make sure the next
  847. // analog values we read become the center
  848. // reference.
  849. pads[channel]->hotplug(channel);
  850. }
  851. }
  852. /* Read from the pad by calling update */
  853. if (pads[channel]) {
  854. if (pads[channel]->update(channel)) {
  855. error_count[channel]++;
  856. if (error_count[channel] > MAX_READ_ERRORS) {
  857. pads[channel] = NULL;
  858. error_count[channel] = 0;
  859. continue;
  860. }
  861. } else {
  862. error_count[channel]=0;
  863. }
  864. if (pads[channel]->changed(channel))
  865. {
  866. pads[channel]->getReport(channel, &pad_data);
  867. if ((num_players == 1) && (channel == 0)) {
  868. // single-port adapter in keyboard mode (kb in port 1)
  869. usbpad_update_kb(&usbpads[channel], &pad_data);
  870. } else if ((num_players == 2) && (channel == 1)) {
  871. // dual-port adapter in keyboard mode (kb in port 2)
  872. usbpad_update_kb(&usbpads[channel], &pad_data);
  873. } else {
  874. usbpad_update(&usbpads[channel], &pad_data);
  875. }
  876. state = STATE_WAIT_INTERRUPT_READY;
  877. continue;
  878. }
  879. } else {
  880. /* Just make sure the gamepad state holds valid data
  881. * to appear inactive (no buttons and axes in neutral) */
  882. usbpad_update(&usbpads[channel], NULL);
  883. }
  884. }
  885. /* If there were change on any of the gamepads, state will
  886. * be set to STATE_WAIT_INTERRUPT_READY. Otherwise, go back
  887. * to WAIT_POLLTIME. */
  888. if (state == STATE_POLL_PAD) {
  889. state = STATE_WAIT_POLLTIME;
  890. }
  891. break;
  892. case STATE_WAIT_INTERRUPT_READY:
  893. /* Wait until one of the interrupt endpoint is ready */
  894. if (usb_interruptReady_ep1() || (num_players>1 && usb_interruptReady_ep2())) {
  895. state = STATE_TRANSMIT;
  896. }
  897. break;
  898. case STATE_TRANSMIT:
  899. if (usb_interruptReady_ep1()) {
  900. if (num_players == 1) {
  901. // Single-port adapters have the keyboard in port 1
  902. usb_interruptSend_ep1(usbpad_getReportBuffer(&usbpads[0]), usbpad_getReportSizeKB());
  903. } else {
  904. usb_interruptSend_ep1(usbpad_getReportBuffer(&usbpads[0]), usbpad_getReportSize());
  905. }
  906. }
  907. // Keyboard is always in second port on dual port adapters
  908. if (num_players>1 && usb_interruptReady_ep2()) {
  909. usb_interruptSend_ep2(usbpad_getReportBuffer(&usbpads[1]), usbpad_getReportSizeKB());
  910. }
  911. state = STATE_WAIT_POLLTIME;
  912. break;
  913. }
  914. for (channel=0; channel < num_players; channel++) {
  915. gamepad_vibrate = usbpad_mustVibrate(&usbpads[channel]);
  916. if (last_v[channel] != gamepad_vibrate) {
  917. if (pads[channel] && pads[channel]->setVibration) {
  918. pads[channel]->setVibration(channel, gamepad_vibrate);
  919. }
  920. last_v[channel] = gamepad_vibrate;
  921. }
  922. }
  923. }
  924. return 0;
  925. }