1
0
mirror of https://github.com/mist-devel/mist-firmware.git synced 2026-04-30 05:45:56 +00:00

Amiga keyboard rate limit

This commit is contained in:
harbaum
2014-04-25 11:08:26 +00:00
parent 750161816d
commit 70a23d9c4d
9 changed files with 118 additions and 84 deletions

View File

@@ -9,7 +9,7 @@ TODAY = `date +"%m/%d/%y"`
PRJ = firmware
SRC = Cstartup_SAM7.c fat.c fdd.c firmware.c fpga.c hardware.c hdd.c main.c menu.c mmc.c osd.c syscalls.c user_io.c boot_print.c boot_logo.c rafile.c config.c tos.c ikbd.c
SRC += usb/max3421e.c usb/usb.c usb/hub.c usb/hid.c usb/hidparser.c usb/timer.c usb/asix.c
SRC += usb/max3421e.c usb/usb.c usb/hub.c usb/hid.c usb/hidparser.c usb/timer.c usb/asix.c usb/storage.c
SRC += cdc_enumerate.c cdc_control.c
OBJ = $(SRC:.c=.o)

31
debug.h
View File

@@ -4,14 +4,6 @@
#include "hardware.h"
// ------------ usb debugging -----------
#if 1
#define hidp_debugf(...) iprintf(__VA_ARGS__)
#else
#define hidp_debugf(...)
#endif
// ------------ generic debugging -----------
#if 0
@@ -57,12 +49,33 @@
#define bit8_debugf(...)
#endif
// ------------ usb debugging -----------
#if 1
#if 0
#define hidp_debugf(...) iprintf(__VA_ARGS__)
#else
#define hidp_debugf(...)
#endif
#if 0
// usb asix debug output in blue
#define asix_debugf(a, ...) iprintf("\033[1;34mASIX: " a "\033[0m\n", ##__VA_ARGS__)
#else
#define asix_debugf(...)
#endif
#if 1
// usb hid debug output in green
#define hid_debugf(a, ...) iprintf("\033[1;32mHID: " a "\033[0m\n", ##__VA_ARGS__)
#else
#define hid_debugf(...)
#endif
#if 1
// usb mass storage debug output in purple
#define storage_debugf(a, ...) iprintf("\033[1;35mSTORAGE: " a "\033[0m\n", ##__VA_ARGS__)
#else
#define storage_debugf(...)
#endif
#endif // DEBUG_H

2
main.c
View File

@@ -135,6 +135,8 @@ int main(void)
if (!MMC_Init())
FatalError(1);
// TODO: If MMC fails try to wait for USB storage
spiclk = MCLK / ((AT91C_SPI_CSR[0] & AT91C_SPI_SCBR) >> 8) / 1000000;
iprintf("spiclk: %u MHz\r", spiclk);

View File

@@ -264,24 +264,9 @@ static uint8_t asix_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len) {
/* scan through all descriptors */
p = &buf;
while(len > 0) {
switch(p->conf_desc.bDescriptorType) {
case USB_DESCRIPTOR_CONFIGURATION:
iprintf("conf descriptor size %d\n", p->conf_desc.bLength);
// we already had this, so we simply ignore it
break;
case USB_DESCRIPTOR_INTERFACE:
iprintf("iface descriptor size %d\n", p->iface_desc.bLength);
/* check the interface descriptors for supported class */
break;
case USB_DESCRIPTOR_ENDPOINT:
iprintf("endpoint descriptor size %d\n", p->ep_desc.bLength);
if(p->conf_desc.bDescriptorType == USB_DESCRIPTOR_ENDPOINT) {
if(epidx < 3) {
// hexdump(p, p->conf_desc.bLength, 0);
// Handle interrupt endpoints
if ((p->ep_desc.bmAttributes & 0x03) == 3 &&
(p->ep_desc.bEndpointAddress & 0x80) == 0x80) {
@@ -314,19 +299,15 @@ static uint8_t asix_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len) {
info->ep[epidx].bmNakPower = USB_NAK_NOWAIT;
epidx++;
}
break;
default:
iprintf("unsupported descriptor type %d size %d\n", p->raw[1], p->raw[0]);
}
// advance to next descriptor
len -= p->conf_desc.bLength;
p = (union buf_u*)(p->raw + p->conf_desc.bLength);
}
if(len != 0) {
iprintf("URGS, underrun: %d\n", len);
asix_debugf("Config underrun: %d\n", len);
return USB_ERROR_CONFIGURAION_SIZE_MISMATCH;
}
@@ -385,20 +366,20 @@ static uint8_t usb_asix_init(usb_device_t *dev) {
}
// Set Configuration Value
iprintf("conf value = %d\n", buf.conf_desc.bConfigurationValue);
// iprintf("conf value = %d\n", buf.conf_desc.bConfigurationValue);
rcode = usb_set_conf(dev, buf.conf_desc.bConfigurationValue);
uint8_t num_of_conf = buf.dev_desc.bNumConfigurations;
iprintf("number of configurations: %d\n", num_of_conf);
// iprintf("number of configurations: %d\n", num_of_conf);
for(i=0; i<num_of_conf; i++) {
if(rcode = usb_get_conf_descr(dev, sizeof(usb_configuration_descriptor_t), i, &buf.conf_desc))
return rcode;
iprintf("conf descriptor %d has total size %d\n", i, buf.conf_desc.wTotalLength);
// iprintf("conf descriptor %d has total size %d\n", i, buf.conf_desc.wTotalLength);
// extract number of interfaces
iprintf("number of interfaces: %d\n", buf.conf_desc.bNumInterfaces);
// iprintf("number of interfaces: %d\n", buf.conf_desc.bNumInterfaces);
// parse directly if it already fitted completely into the buffer
if((rcode = asix_parse_conf(dev, i, buf.conf_desc.wTotalLength)) != 0) {
@@ -499,10 +480,10 @@ static uint8_t usb_asix_init(usb_device_t *dev) {
return rcode;
rx_ctl = asix_read_rx_ctl(dev);
iprintf("ASIX: RX_CTL is 0x%04x after all initializations\n", rx_ctl);
asix_debugf("RX_CTL is 0x%04x after all initializations\n", rx_ctl);
rx_ctl = asix_read_medium_status(dev);
iprintf("ASIX: Medium Status is 0x%04x after all initializations\n", rx_ctl);
asix_debugf("Medium Status is 0x%04x after all initializations\n", rx_ctl);
info->bPollEnable = true;

View File

@@ -4,13 +4,14 @@
#include "max3421e.h"
#include "timer.h"
#include "hidparser.h"
#include "debug.h"
#include "../user_io.h"
#include "../hardware.h"
// joystick todo:
// + renumber on unplug
// + shift legacy joysticks up
// - emulate extra joysticks (at printerport, ...)
// + emulate extra joysticks (at printerport, ...)
// - second fire button (no known system uses it, but OSD may have a use ...)
static unsigned char kbd_led_state = 0; // default: all leds off
@@ -22,21 +23,21 @@ uint8_t hid_get_joysticks(void) {
//get HID report descriptor
static uint8_t hid_get_report_descr(usb_device_t *dev, uint8_t iface, uint16_t size) {
iprintf("%s(%x, if=%d, size=%d)\n", __FUNCTION__, dev->bAddress, iface, size);
// hid_debugf("%s(%x, if=%d, size=%d)", __FUNCTION__, dev->bAddress, iface, size);
uint8_t buf[size];
usb_hid_info_t *info = &(dev->hid_info);
uint8_t rcode = usb_ctrl_req( dev, HID_REQ_HIDREPORT, USB_REQUEST_GET_DESCRIPTOR, 0x00,
HID_DESCRIPTOR_REPORT, iface, size, buf);
if(!rcode) {
iprintf("HID report descriptor:\n");
hid_debugf("HID report descriptor:");
hexdump(buf, size, 0);
// we got a report descriptor. Try to parse it
if(parse_report_descriptor(buf, size)) {
if(hid_conf[0].type == CONFIG_TYPE_JOYSTICK) {
iprintf("Detected USB joystick #%d\n", joysticks);
hid_debugf("Detected USB joystick #%d", joysticks);
info->iface_info[iface].device_type = HID_DEVICE_JOYSTICK;
info->iface_info[iface].conf = hid_conf[0];
@@ -49,14 +50,14 @@ static uint8_t hid_get_report_descr(usb_device_t *dev, uint8_t iface, uint16_t s
}
static uint8_t hid_set_idle(usb_device_t *dev, uint8_t iface, uint8_t reportID, uint8_t duration ) {
iprintf("%s(%x, if=%d id=%d, dur=%d)\n", __FUNCTION__, dev->bAddress, iface, reportID, duration);
// hid_debugf("%s(%x, if=%d id=%d, dur=%d)", __FUNCTION__, dev->bAddress, iface, reportID, duration);
return( usb_ctrl_req( dev, HID_REQ_HIDOUT, HID_REQUEST_SET_IDLE, reportID,
duration, iface, 0x0000, NULL));
}
static uint8_t hid_set_protocol(usb_device_t *dev, uint8_t iface, uint8_t protocol) {
iprintf("%s(%x, if=%d proto=%d)\n", __FUNCTION__, dev->bAddress, iface, protocol);
// hid_debugf("%s(%x, if=%d proto=%d)", __FUNCTION__, dev->bAddress, iface, protocol);
return( usb_ctrl_req( dev, HID_REQ_HIDOUT, HID_REQUEST_SET_PROTOCOL, protocol,
0x00, iface, 0x0000, NULL));
@@ -64,7 +65,7 @@ static uint8_t hid_set_protocol(usb_device_t *dev, uint8_t iface, uint8_t protoc
static uint8_t hid_set_report(usb_device_t *dev, uint8_t iface, uint8_t report_type, uint8_t report_id,
uint16_t nbytes, uint8_t* dataptr ) {
// iprintf("%s(%x, if=%d data=%x)\n", __FUNCTION__, dev->bAddress, iface, dataptr[0]);
// hid_debugf("%s(%x, if=%d data=%x)", __FUNCTION__, dev->bAddress, iface, dataptr[0]);
return( usb_ctrl_req(dev, HID_REQ_HIDOUT, HID_REQUEST_SET_REPORT, report_id,
report_type, iface, nbytes, dataptr));
@@ -94,13 +95,13 @@ static uint8_t usb_hid_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len)
while(len > 0) {
switch(p->conf_desc.bDescriptorType) {
case USB_DESCRIPTOR_CONFIGURATION:
iprintf("conf descriptor size %d\n", p->conf_desc.bLength);
// hid_debugf("conf descriptor size %d", p->conf_desc.bLength);
// we already had this, so we simply ignore it
break;
case USB_DESCRIPTOR_INTERFACE:
isGoodInterface = false;
iprintf("iface descriptor size %d\n", p->iface_desc.bLength);
// hid_debugf("iface descriptor size %d", p->iface_desc.bLength);
/* check the interface descriptors for supported class */
@@ -118,27 +119,27 @@ static uint8_t usb_hid_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len)
info->iface_info[info->bNumIfaces].conf.type = CONFIG_TYPE_NONE;
if(p->iface_desc.bInterfaceSubClass == HID_BOOT_INTF_SUBCLASS) {
iprintf("Iface %d is Boot sub class\n", info->bNumIfaces);
// hid_debugf("Iface %d is Boot sub class", info->bNumIfaces);
info->iface_info[info->bNumIfaces].has_boot_mode = true;
}
switch(p->iface_desc.bInterfaceProtocol) {
case HID_PROTOCOL_NONE:
iprintf("HID protocol is NONE\n");
hid_debugf("HID protocol is NONE");
break;
case HID_PROTOCOL_KEYBOARD:
iprintf("HID protocol is KEYBOARD\n");
hid_debugf("HID protocol is KEYBOARD");
info->iface_info[info->bNumIfaces].device_type = HID_DEVICE_KEYBOARD;
break;
case HID_PROTOCOL_MOUSE:
iprintf("HID protocol is MOUSE\n");
hid_debugf("HID protocol is MOUSE");
info->iface_info[info->bNumIfaces].device_type = HID_DEVICE_MOUSE;
break;
default:
iprintf("HID protocol is %d\n", p->iface_desc.bInterfaceProtocol);
hid_debugf("HID protocol is %d", p->iface_desc.bInterfaceProtocol);
break;
}
}
@@ -146,13 +147,13 @@ static uint8_t usb_hid_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len)
break;
case USB_DESCRIPTOR_ENDPOINT:
iprintf("endpoint descriptor size %d\n", p->ep_desc.bLength);
// hid_debugf("endpoint descriptor size %d", p->ep_desc.bLength);
if(isGoodInterface) {
// only interrupt in endpoints are supported
if ((p->ep_desc.bmAttributes & 0x03) == 3 && (p->ep_desc.bEndpointAddress & 0x80) == 0x80) {
iprintf("endpint %d, interval = %dms\n",
hid_debugf("endpoint %d, interval = %dms",
p->ep_desc.bEndpointAddress & 0x0F, p->ep_desc.bInterval);
// Fill in the endpoint info structure
@@ -167,14 +168,14 @@ static uint8_t usb_hid_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len)
break;
case HID_DESCRIPTOR_HID:
iprintf("hid descriptor size %d\n", p->ep_desc.bLength);
hid_debugf("hid descriptor size %d", p->ep_desc.bLength);
if(isGoodInterface) {
// we need a report descriptor
if(p->hid_desc.bDescrType == HID_DESCRIPTOR_REPORT) {
uint16_t len = p->hid_desc.wDescriptorLength[0] +
256 * p->hid_desc.wDescriptorLength[1];
iprintf(" -> report descriptor size = %d\n", len);
hid_debugf(" -> report descriptor size = %d", len);
info->iface_info[info->bNumIfaces].report_desc_size = len;
}
@@ -182,7 +183,7 @@ static uint8_t usb_hid_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len)
break;
default:
iprintf("unsupported descriptor type %d size %d\n", p->raw[1], p->raw[0]);
hid_debugf("unsupported descriptor type %d size %d", p->raw[1], p->raw[0]);
}
// advance to next descriptor
@@ -191,7 +192,7 @@ static uint8_t usb_hid_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len)
}
if(len != 0) {
iprintf("URGS, underrun: %d\n", len);
hid_debugf("Config underrun: %d", len);
return USB_ERROR_CONFIGURAION_SIZE_MISMATCH;
}
@@ -199,9 +200,7 @@ static uint8_t usb_hid_parse_conf(usb_device_t *dev, uint8_t conf, uint16_t len)
}
static uint8_t usb_hid_init(usb_device_t *dev) {
iprintf("%s()\n", __FUNCTION__);
iprintf("init with address %x\n", dev->bAddress);
hid_debugf("%s(%x)", __FUNCTION__, dev->bAddress);
uint8_t rcode;
uint8_t i;
@@ -226,35 +225,29 @@ static uint8_t usb_hid_init(usb_device_t *dev) {
}
// try to re-read full device descriptor from newly assigned address
if(rcode = usb_get_dev_descr( dev, sizeof(usb_device_descriptor_t), &buf.dev_desc )) {
puts("failed to get device descriptor");
if(rcode = usb_get_dev_descr( dev, sizeof(usb_device_descriptor_t), &buf.dev_desc ))
return rcode;
}
uint8_t num_of_conf = buf.dev_desc.bNumConfigurations;
iprintf("number of configurations: %d\n", num_of_conf);
// hid_debugf("number of configurations: %d", num_of_conf);
for(i=0; i<num_of_conf; i++) {
if(rcode = usb_get_conf_descr(dev, sizeof(usb_configuration_descriptor_t), i, &buf.conf_desc))
return rcode;
iprintf("conf descriptor %d has total size %d\n", i, buf.conf_desc.wTotalLength);
// hid_debugf("conf descriptor %d has total size %d", i, buf.conf_desc.wTotalLength);
// extract number of interfaces
iprintf("number of interfaces: %d\n", buf.conf_desc.bNumInterfaces);
// parse directly if it already fitted completely into the buffer
usb_hid_parse_conf(dev, i, buf.conf_desc.wTotalLength);
}
// check if we found valid hid interfaces
if(!info->bNumIfaces) {
puts("no hid interfaces found");
hid_debugf("no hid interfaces found");
return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
}
// Set Configuration Value
iprintf("conf value = %d\n", buf.conf_desc.bConfigurationValue);
rcode = usb_set_conf(dev, buf.conf_desc.bConfigurationValue);
// process all supported interfaces
@@ -297,7 +290,7 @@ static uint8_t usb_hid_release(usb_device_t *dev) {
for(i=0;i<info->bNumIfaces;i++) {
if(info->iface_info[i].device_type == HID_DEVICE_JOYSTICK) {
uint8_t c_jindex = info->iface_info[i].jindex;
iprintf("releasing joystick #%d, renumbering\n", c_jindex);
hid_debugf("releasing joystick #%d, renumbering", c_jindex);
// walk through all devices and search for sticks with a higher id
@@ -311,7 +304,7 @@ static uint8_t usb_hid_release(usb_device_t *dev) {
for(k=0;k<MAX_IFACES;k++) {
if(dev[j].hid_info.iface_info[k].device_type == HID_DEVICE_JOYSTICK) {
if(dev[j].hid_info.iface_info[k].jindex > c_jindex) {
iprintf("decreasing jindex of dev #%d from %d to %d\n", j,
hid_debugf("decreasing jindex of dev #%d from %d to %d", j,
dev[j].hid_info.iface_info[k].jindex, dev[j].hid_info.iface_info[k].jindex-1);
dev[j].hid_info.iface_info[k].jindex--;
}
@@ -336,7 +329,7 @@ static uint8_t usb_hid_poll(usb_device_t *dev) {
if (info->qNextPollTime <= timer_get_msec()) {
int8_t i;
for(i=0;i<info->bNumIfaces;i++) {
// iprintf("poll %d...\n", info->ep[i].epAddr);
// hid_debugf("poll %d...", info->ep[i].epAddr);
uint16_t read = info->ep[i].maxPktSize;
uint8_t buf[info->ep[i].maxPktSize];
@@ -345,7 +338,7 @@ static uint8_t usb_hid_poll(usb_device_t *dev) {
if (rcode) {
if (rcode != hrNAK)
iprintf("%s() error: %d\n", __FUNCTION__, rcode);
hid_debugf("%s() error: %d", __FUNCTION__, rcode);
} else {
// successfully received some bytes
@@ -372,7 +365,7 @@ static uint8_t usb_hid_poll(usb_device_t *dev) {
uint8_t jmap = 0;
uint8_t ax;
// iprintf("Joystick data:\n");
// hid_debugf("Joystick data:");
// hexdump(buf, read, 0);
// currently only byte sized axes are allowed

View File

@@ -8,7 +8,7 @@
#include "debug.h"
#if 0
#define hidp_extreme_debugf(...) iprintf(__VA_ARGS__)
#define hidp_extreme_debugf(...) hidp_debugf(__VA_ARGS__)
#else
#define hidp_extreme_debugf(...)
#endif

View File

@@ -342,6 +342,7 @@ static const usb_device_class_config_t *class_list[] = {
&usb_hub_class,
&usb_hid_class,
&usb_asix_class,
&usb_storage_class,
NULL
};

View File

@@ -96,6 +96,7 @@ typedef struct {
#include "hub.h"
#include "hid.h"
#include "asix.h"
#include "storage.h"
// entry used for list of connected devices
typedef struct usb_device_entry {
@@ -110,6 +111,7 @@ typedef struct usb_device_entry {
usb_hub_info_t hub_info;
usb_hid_info_t hid_info;
usb_asix_info_t asix_info;
usb_storage_info_t storage_info;
};
} usb_device_t;

View File

@@ -248,6 +248,44 @@ static uint8_t joystick_renumber(uint8_t j) {
return j;
}
// 16 byte fifo for amiga key codes to limit max key rate sent into the core
#define KBD_FIFO_SIZE 16 // must be power of 2
static unsigned short kbd_fifo[KBD_FIFO_SIZE];
static unsigned char kbd_fifo_r=0, kbd_fifo_w=0;
static long kbd_timer = 0;
static void kbd_fifo_minimig_send(unsigned short code) {
EnableIO();
if(code & OSD) SPI(UIO_KBD_OSD); // code for OSD
else SPI(UIO_KEYBOARD);
SPI(code & 0xff);
DisableIO();
kbd_timer = GetTimer(10); // next key after 10ms earliest
}
static void kbd_fifo_enqueue(unsigned short code) {
// if fifo full just drop the value. This should never happen
if(((kbd_fifo_w+1)&(KBD_FIFO_SIZE-1)) == kbd_fifo_r)
return;
// store in queue
kbd_fifo[kbd_fifo_w] = code;
kbd_fifo_w = (kbd_fifo_w + 1)&(KBD_FIFO_SIZE-1);
}
// send pending bytes if timer has run up
static void kbd_fifo_poll() {
if(kbd_fifo_w == kbd_fifo_r)
return;
if(!CheckTimer(kbd_timer))
return;
kbd_fifo_minimig_send(kbd_fifo[kbd_fifo_r]);
kbd_fifo_r = (kbd_fifo_r + 1)&(KBD_FIFO_SIZE-1);
}
void user_io_poll() {
if((core_type != CORE_TYPE_MINIMIG) &&
(core_type != CORE_TYPE_PACE) &&
@@ -373,6 +411,10 @@ void user_io_poll() {
}
}
if(core_type == CORE_TYPE_MINIMIG) {
kbd_fifo_poll();
}
if(core_type == CORE_TYPE_MIST) {
// do some tos specific monitoring here
tos_poll();
@@ -444,11 +486,11 @@ char user_io_user_button() {
static void send_keycode(unsigned short code) {
if(core_type == CORE_TYPE_MINIMIG) {
EnableIO();
if(code & OSD) SPI(UIO_KBD_OSD); // code for OSD
else SPI(UIO_KEYBOARD);
SPI(code & 0xff);
DisableIO();
// send immediately if possible
if(CheckTimer(kbd_timer) &&(kbd_fifo_w == kbd_fifo_r) )
kbd_fifo_minimig_send(code);
else
kbd_fifo_enqueue(code);
}
if(core_type == CORE_TYPE_MIST)