gc_n64_usb-v3/hiddata.c

180 lines
4.3 KiB
C
Raw Normal View History

/* gc_n64_usb : Gamecube or N64 controller to USB firmware
2016-10-10 15:01:22 -04:00
Copyright (C) 2007-2016 Raphael Assenat <raph@raphnet.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include "requests.h"
#include "config.h"
#include "hiddata.h"
2015-12-06 23:12:47 -05:00
#include "usbpad.h"
#include "bootloader.h"
#include "gcn64_protocol.h"
2015-10-17 23:19:50 -04:00
#include "version.h"
2015-10-31 01:39:24 -04:00
#include "main.h"
2015-10-27 23:57:27 -04:00
// dataHidReport is 40 bytes.
#define CMDBUF_SIZE 41
#define STATE_IDLE 0
#define STATE_NEW_COMMAND 1 // New command in buffer
#define STATE_COMMAND_DONE 2 // Result in buffer
//#define DEBUG
extern char g_polling_suspended;
static volatile uint8_t state = STATE_IDLE;
static unsigned char cmdbuf[CMDBUF_SIZE];
static volatile unsigned char cmdbuf_len = 0;
/*** Get/Set report called from interrupt context! */
uint16_t hiddata_get_report(void *ctx, struct usb_request *rq, const uint8_t **dat)
{
// printf("Get data\n");
if (state == STATE_COMMAND_DONE) {
*dat = cmdbuf;
state = STATE_IDLE;
#ifdef DEBUG
2015-10-28 00:16:32 -04:00
printf_P(PSTR("hiddata idle, sent %d bytes\r\n"), cmdbuf_len);
#endif
return cmdbuf_len;
}
return 0;
}
/*** Get/Set report called from interrupt context! */
uint8_t hiddata_set_report(void *ctx, const struct usb_request *rq, const uint8_t *dat, uint16_t len)
{
#ifdef DEBUG
int i;
2015-10-28 00:16:32 -04:00
printf_P(PSTR("Set data %d\n"), len);
for (i=0; i<len; i++) {
2015-11-03 23:48:48 -05:00
printf_P(PSTR("%02x "), dat[i]);
}
2015-10-28 00:16:32 -04:00
printf_P(PSTR("\r\n"));
#endif
state = STATE_NEW_COMMAND;
memcpy(cmdbuf, dat, len);
cmdbuf_len = len;
return 0;
}
static void hiddata_processCommandBuffer(struct hiddata_ops *ops)
{
unsigned char channel;
2015-10-24 14:17:30 -04:00
#ifdef DEBUG
int i;
#endif
if (cmdbuf_len < 1) {
state = STATE_IDLE;
return;
}
// printf("Process cmd 0x%02x\r\n", cmdbuf[0]);
switch(cmdbuf[0])
{
case RQ_GCN64_JUMP_TO_BOOTLOADER:
enterBootLoader();
break;
case RQ_GCN64_RAW_SI_COMMAND:
// TODO : Range checking
// cmdbuf[] : RQ, CHN, LEN, data[]
channel = cmdbuf[1];
if (channel >= NUM_CHANNELS)
break;
cmdbuf_len = gcn64_transaction(channel, cmdbuf+3, cmdbuf[2], cmdbuf + 3, CMDBUF_SIZE-3);
cmdbuf[2] = cmdbuf_len;
cmdbuf_len += 3; // Answer: RQ, CHN, LEN, data[]
break;
case RQ_GCN64_GET_CONFIG_PARAM:
// Cmd : RQ, PARAM
// Answer: RQ, PARAM, data[]
cmdbuf_len = config_getParam(cmdbuf[1], cmdbuf + 2, CMDBUF_SIZE-2);
cmdbuf_len += 2; // Datalen + RQ + PARAM
break;
case RQ_GCN64_SET_CONFIG_PARAM:
// Cmd: RQ, PARAM, data[]
config_setParam(cmdbuf[1], cmdbuf+2);
// Answer: RQ, PARAM
cmdbuf_len = 2;
break;
case RQ_GCN64_SUSPEND_POLLING:
// CMD: RQ, PARAM
if (ops && ops->suspendPolling) {
ops->suspendPolling(cmdbuf[1]);
}
break;
2015-10-17 23:19:50 -04:00
case RQ_GCN64_GET_VERSION:
// CMD: RQ
2015-10-24 15:34:50 -04:00
// Answer: RQ, version string (zero-terminated)
2015-10-17 23:19:50 -04:00
strcpy((char*)(cmdbuf + 1), g_version);
2015-10-24 15:34:50 -04:00
cmdbuf_len = 1 + strlen(g_version) + 1;
2015-10-17 23:19:50 -04:00
break;
2015-10-27 23:57:27 -04:00
case RQ_GCN64_GET_SIGNATURE:
strcpy_P((char*)(cmdbuf + 1), g_signature);
2015-10-31 01:39:24 -04:00
cmdbuf_len = 1 + strlen_P(g_signature) + 1;
break;
case RQ_GCN64_GET_CONTROLLER_TYPE:
// CMD : RQ, CHN
// Answer: RQ, CHN, TYPE
channel = cmdbuf[1];
if (channel >= NUM_CHANNELS)
break;
cmdbuf[2] = current_pad_type[channel];
2015-10-31 01:39:24 -04:00
cmdbuf_len = 3;
2015-10-27 23:57:27 -04:00
break;
2015-11-06 23:27:30 -05:00
case RQ_GCN64_SET_VIBRATION:
// CMD : RQ, CHN, Vibrate
// Answer: RQ, CHN, Vibrate
if (ops && ops->forceVibration) {
ops->forceVibration(cmdbuf[1], cmdbuf[2]);
}
2015-11-06 23:27:30 -05:00
cmdbuf_len = 3;
break;
}
#ifdef DEBUG
printf("Pending data %d\n", cmdbuf_len);
for (i=0; i<cmdbuf_len; i++) {
2015-11-03 23:48:48 -05:00
printf("%02x ", cmdbuf[i]);
}
printf("\r\n");
#endif
state = STATE_COMMAND_DONE;
}
void hiddata_doTask(struct hiddata_ops *ops)
{
switch (state)
{
default:
state = STATE_IDLE;
case STATE_IDLE:
break;
case STATE_NEW_COMMAND:
hiddata_processCommandBuffer(ops);
break;
case STATE_COMMAND_DONE:
break;
}
}