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

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. unsigned char current_pad_type[NUM_CHANNELS] = { };
  499. Gamepad *detectPad(unsigned char chn)
  500. {
  501. current_pad_type[chn] = gcn64_detectController(chn);
  502. switch (current_pad_type[chn])
  503. {
  504. case CONTROLLER_IS_ABSENT:
  505. case CONTROLLER_IS_UNKNOWN:
  506. return NULL;
  507. case CONTROLLER_IS_N64:
  508. return n64GetGamepad();
  509. case CONTROLLER_IS_GC:
  510. return gamecubeGetGamepad();
  511. case CONTROLLER_IS_GC_KEYBOARD:
  512. return gamecubeGetKeyboard();
  513. }
  514. return NULL;
  515. }
  516. /* Called after eeprom content is loaded. */
  517. void eeprom_app_ready(void)
  518. {
  519. static wchar_t serial_from_eeprom[SERIAL_NUM_LEN+1];
  520. int i;
  521. for (i=0; i<SERIAL_NUM_LEN; i++) {
  522. serial_from_eeprom[i] = g_eeprom_data.cfg.serial[i];
  523. }
  524. serial_from_eeprom[i] = 0;
  525. g_usb_strings[USB_STRING_SERIAL_IDX] = serial_from_eeprom;
  526. }
  527. static struct usbpad usbpads[MAX_PLAYERS];
  528. static char g_polling_suspended = 0;
  529. static void setSuspendPolling(uint8_t suspend)
  530. {
  531. g_polling_suspended = suspend;
  532. }
  533. static void forceVibration(uint8_t channel, uint8_t force)
  534. {
  535. if (channel < MAX_PLAYERS) {
  536. usbpad_forceVibrate(&usbpads[channel], force);
  537. }
  538. }
  539. static uint8_t getSupportedModes(uint8_t *dst)
  540. {
  541. uint8_t idx = 0;
  542. switch (g_eeprom_data.cfg.mode)
  543. {
  544. // Allow toggling between keyboard and joystick modes on
  545. // single-port gamecube adapter
  546. case CFG_MODE_GC_ONLY:
  547. case CFG_MODE_KEYBOARD:
  548. dst[idx++] = CFG_MODE_GC_ONLY;
  549. dst[idx++] = CFG_MODE_KEYBOARD;
  550. break;
  551. // Allow toggling between two joysticks and joystick + keyboard modes
  552. // on dual-port gamecube adapter
  553. case CFG_MODE_2P_GC_ONLY:
  554. case CFG_MODE_KB_AND_JS:
  555. dst[idx++] = CFG_MODE_2P_GC_ONLY;
  556. dst[idx++] = CFG_MODE_KB_AND_JS;
  557. break;
  558. // On N64/GC adapters, there is a GC port so we should support
  559. // keyboards there. Use KEYBOARD_2 config here to avoid mixup
  560. // with the GC-only adapter variation.
  561. case CFG_MODE_STANDARD:
  562. case CFG_MODE_KEYBOARD_2:
  563. dst[idx++] = CFG_MODE_STANDARD;
  564. dst[idx++] = CFG_MODE_KEYBOARD_2;
  565. break;
  566. default:
  567. dst[idx++] = CFG_MODE_STANDARD;
  568. dst[idx++] = CFG_MODE_N64_ONLY;
  569. dst[idx++] = CFG_MODE_GC_ONLY;
  570. dst[idx++] = CFG_MODE_2P_STANDARD;
  571. dst[idx++] = CFG_MODE_2P_N64_ONLY;
  572. dst[idx++] = CFG_MODE_2P_GC_ONLY;
  573. dst[idx++] = CFG_MODE_KEYBOARD;
  574. dst[idx++] = CFG_MODE_KB_AND_JS;
  575. break;
  576. }
  577. return idx;
  578. }
  579. static struct hiddata_ops hiddata_ops = {
  580. .suspendPolling = setSuspendPolling,
  581. .forceVibration = forceVibration,
  582. .getSupportedModes = getSupportedModes,
  583. };
  584. #define STATE_WAIT_POLLTIME 0
  585. #define STATE_POLL_PAD 1
  586. #define STATE_WAIT_INTERRUPT_READY 2
  587. #define STATE_TRANSMIT 3
  588. #define STATE_WAIT_INTERRUPT_READY_P2 4
  589. #define STATE_TRANSMIT_P2 5
  590. int main(void)
  591. {
  592. Gamepad *pads[MAX_PLAYERS] = { };
  593. gamepad_data pad_data;
  594. uint8_t gamepad_vibrate = 0;
  595. uint8_t state = STATE_WAIT_POLLTIME;
  596. uint8_t channel;
  597. uint8_t num_players = 1;
  598. uint8_t i;
  599. hwinit();
  600. usart1_init();
  601. eeprom_init();
  602. intervaltimer_init();
  603. intervaltimer2_init();
  604. stkchk_init();
  605. switch (g_eeprom_data.cfg.mode)
  606. {
  607. default:
  608. case CFG_MODE_STANDARD:
  609. usbstrings_changeProductString_P(PSTR("GC/N64 to USB v"VERSIONSTR_SHORT));
  610. break;
  611. case CFG_MODE_N64_ONLY:
  612. usbstrings_changeProductString_P(PSTR("N64 to USB v"VERSIONSTR_SHORT));
  613. device_descriptor.idProduct = N64_USB_PID;
  614. break;
  615. case CFG_MODE_GC_ONLY:
  616. usbstrings_changeProductString_P(PSTR("Gamecube to USB v"VERSIONSTR_SHORT));
  617. device_descriptor.idProduct = GC_USB_PID;
  618. break;
  619. case CFG_MODE_2P_STANDARD:
  620. usbstrings_changeProductString_P(PSTR("Dual GC/N64 to USB v"VERSIONSTR_SHORT));
  621. device_descriptor.idProduct = DUAL_GCN64_USB_PID;
  622. num_players = 2;
  623. break;
  624. case CFG_MODE_2P_N64_ONLY:
  625. usbstrings_changeProductString_P(PSTR("Dual N64 to USB v"VERSIONSTR_SHORT));
  626. device_descriptor.idProduct = DUAL_N64_USB_PID;
  627. num_players = 2;
  628. break;
  629. case CFG_MODE_2P_GC_ONLY:
  630. usbstrings_changeProductString_P(PSTR("Dual Gamecube to USB v"VERSIONSTR_SHORT));
  631. device_descriptor.idProduct = DUAL_GC_USB_PID;
  632. num_players = 2;
  633. break;
  634. case CFG_MODE_KB_AND_JS:
  635. case CFG_MODE_KEYBOARD:
  636. case CFG_MODE_KEYBOARD_2:
  637. keyboard_main();
  638. break;
  639. }
  640. // 2-players common
  641. if (num_players == 2) {
  642. usb_params.configdesc = (PGM_VOID_P)&cfg0_2p;
  643. usb_params.configdesc_ttllen = sizeof(cfg0_2p);
  644. usb_params.n_hid_interfaces = 3;
  645. // Move the management interface is the last position
  646. memcpy(usb_params.hid_params + 2, usb_params.hid_params + 1, sizeof(struct usb_hid_parameters));
  647. // Add a second player interface between them
  648. memcpy(usb_params.hid_params + 1, usb_params.hid_params + 0, sizeof(struct usb_hid_parameters));
  649. }
  650. for (i=0; i<num_players; i++) {
  651. usbpad_init(&usbpads[i]);
  652. usb_params.hid_params[i].ctx = &usbpads[i];
  653. }
  654. sei();
  655. usb_init(&usb_params);
  656. // Timebase for force feedback 'loop count'
  657. intervaltimer2_set16ms();
  658. while (1)
  659. {
  660. static char last_v[MAX_PLAYERS] = { };
  661. if (stkchk_verify()) {
  662. enterBootLoader();
  663. }
  664. usb_doTasks();
  665. hiddata_doTask(&hiddata_ops);
  666. // Run vibration tasks
  667. if (intervaltimer2_get()) {
  668. for (channel=0; channel < num_players; channel++) {
  669. usbpad_vibrationTask(&usbpads[channel]);
  670. }
  671. }
  672. switch(state)
  673. {
  674. case STATE_WAIT_POLLTIME:
  675. if (!g_polling_suspended) {
  676. intervaltimer_set(g_eeprom_data.cfg.poll_interval[0]);
  677. if (intervaltimer_get()) {
  678. state = STATE_POLL_PAD;
  679. }
  680. }
  681. break;
  682. case STATE_POLL_PAD:
  683. for (channel=0; channel<num_players; channel++)
  684. {
  685. /* Try to auto-detect controller if none*/
  686. if (!pads[channel]) {
  687. pads[channel] = detectPad(channel);
  688. if (pads[channel] && (pads[channel]->hotplug)) {
  689. // For gamecube, this make sure the next
  690. // analog values we read become the center
  691. // reference.
  692. pads[channel]->hotplug(channel);
  693. }
  694. }
  695. /* Read from the pad by calling update */
  696. if (pads[channel]) {
  697. if (pads[channel]->update(channel)) {
  698. error_count[channel]++;
  699. if (error_count[channel] > MAX_READ_ERRORS) {
  700. pads[channel] = NULL;
  701. error_count[channel] = 0;
  702. continue;
  703. }
  704. } else {
  705. error_count[channel]=0;
  706. }
  707. if (pads[channel]->changed(channel))
  708. {
  709. pads[channel]->getReport(channel, &pad_data);
  710. usbpad_update(&usbpads[channel], &pad_data);
  711. state = STATE_WAIT_INTERRUPT_READY;
  712. continue;
  713. }
  714. } else {
  715. /* Just make sure the gamepad state holds valid data
  716. * to appear inactive (no buttons and axes in neutral) */
  717. usbpad_update(&usbpads[channel], NULL);
  718. }
  719. }
  720. /* If there were change on any of the gamepads, state will
  721. * be set to STATE_WAIT_INTERRUPT_READY. Otherwise, go back
  722. * to WAIT_POLLTIME. */
  723. if (state == STATE_POLL_PAD) {
  724. state = STATE_WAIT_POLLTIME;
  725. }
  726. break;
  727. case STATE_WAIT_INTERRUPT_READY:
  728. /* Wait until one of the interrupt endpoint is ready */
  729. if (usb_interruptReady_ep1() || (num_players>1 && usb_interruptReady_ep2())) {
  730. state = STATE_TRANSMIT;
  731. }
  732. break;
  733. case STATE_TRANSMIT:
  734. if (usb_interruptReady_ep1()) {
  735. usb_interruptSend_ep1(usbpad_getReportBuffer(&usbpads[0]), usbpad_getReportSize());
  736. }
  737. if (num_players>1 && usb_interruptReady_ep2()) {
  738. usb_interruptSend_ep2(usbpad_getReportBuffer(&usbpads[1]), usbpad_getReportSize());
  739. }
  740. state = STATE_WAIT_POLLTIME;
  741. break;
  742. }
  743. for (channel=0; channel < num_players; channel++) {
  744. gamepad_vibrate = usbpad_mustVibrate(&usbpads[channel]);
  745. if (last_v[channel] != gamepad_vibrate) {
  746. if (pads[channel] && pads[channel]->setVibration) {
  747. pads[channel]->setVibration(channel, gamepad_vibrate);
  748. }
  749. last_v[channel] = gamepad_vibrate;
  750. }
  751. }
  752. }
  753. return 0;
  754. }
  755. int keyboard_main(void)
  756. {
  757. Gamepad *pads[MAX_PLAYERS] = { };
  758. gamepad_data pad_data;
  759. uint8_t gamepad_vibrate = 0;
  760. uint8_t state = STATE_WAIT_POLLTIME;
  761. uint8_t channel;
  762. uint8_t num_players = 1;
  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. }