usb: Finish work on OHCI
This commit is contained in:
@@ -24,18 +24,29 @@ extern "C" {
|
||||
|
||||
typedef struct usb_t usb_t;
|
||||
|
||||
enum usb_pid
|
||||
{
|
||||
USB_PID_OUT = 0xE1,
|
||||
USB_PID_IN = 0x69,
|
||||
USB_PID_SETUP = 0x2D
|
||||
};
|
||||
|
||||
enum usb_errors
|
||||
{
|
||||
USB_ERROR_NO_ERROR = 0,
|
||||
USB_ERROR_NAK = 1,
|
||||
USB_ERROR_OVERRUN = 2,
|
||||
USB_ERROR_UNDERRUN = 3
|
||||
};
|
||||
|
||||
/* USB endpoint device struct. Incomplete and unused. */
|
||||
typedef struct
|
||||
{
|
||||
uint16_t vendor_id;
|
||||
uint16_t device_id;
|
||||
|
||||
/* Reads from endpoint. Non-zero value indicates error. */
|
||||
uint8_t (*device_in)(void* priv, uint8_t* data, uint32_t len);
|
||||
/* Writes to endpoint. Non-zero value indicates error. */
|
||||
uint8_t (*device_out)(void* priv, uint8_t* data, uint32_t len);
|
||||
/* Process setup packets. */
|
||||
uint8_t (*device_setup)(void* priv, uint8_t* data);
|
||||
/* General-purpose function for I/O. Non-zero value indicates error. */
|
||||
uint8_t (*device_process)(void* priv, uint8_t* data, uint32_t *len, uint8_t pid_token, uint8_t endpoint, uint8_t underrun_not_allowed);
|
||||
/* Device reset. */
|
||||
void (*device_reset)(void* priv);
|
||||
/* Get address. */
|
||||
@@ -81,6 +92,8 @@ typedef struct usb_t
|
||||
uint8_t ohci_interrupt_counter : 3;
|
||||
usb_device_t* ohci_devices[2];
|
||||
usb_device_t* uhci_devices[2];
|
||||
uint8_t ohci_usb_buf[4096];
|
||||
uint8_t ohci_initial_start;
|
||||
|
||||
usb_params_t* usb_params;
|
||||
} usb_t;
|
||||
|
260
src/usb.c
260
src/usb.c
@@ -22,6 +22,7 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <wchar.h>
|
||||
#include <assert.h>
|
||||
#define HAVE_STDARG_H
|
||||
#include <86box/86box.h>
|
||||
#include <86box/device.h>
|
||||
@@ -238,20 +239,7 @@ typedef struct
|
||||
/* Transfer descriptors */
|
||||
typedef struct
|
||||
{
|
||||
union
|
||||
{
|
||||
uint32_t Control;
|
||||
struct
|
||||
{
|
||||
uint32_t Reserved : 18;
|
||||
uint8_t bufferRounding : 1;
|
||||
uint8_t Direction : 2;
|
||||
uint8_t DelayInterrupt : 3;
|
||||
uint8_t DataToggle : 2;
|
||||
uint8_t ErrorCount : 2;
|
||||
uint8_t ConditionCode : 4;
|
||||
} flags;
|
||||
};
|
||||
uint32_t Control;
|
||||
uint32_t CBP;
|
||||
uint32_t NextTD;
|
||||
uint32_t BE;
|
||||
@@ -260,31 +248,9 @@ typedef struct
|
||||
/* Endpoint descriptors */
|
||||
typedef struct
|
||||
{
|
||||
union
|
||||
{
|
||||
uint32_t Control;
|
||||
struct
|
||||
{
|
||||
uint8_t FunctionAddress : 7;
|
||||
uint8_t EndpointNumber : 4;
|
||||
uint8_t Direction : 2;
|
||||
bool Speed : 1;
|
||||
bool Skip : 1;
|
||||
bool Format : 1;
|
||||
uint16_t MaximumPacketSize : 11;
|
||||
uint8_t Reserved : 5;
|
||||
} flags;
|
||||
};
|
||||
uint32_t Control;
|
||||
uint32_t TailP;
|
||||
union
|
||||
{
|
||||
uint32_t HeadP;
|
||||
struct
|
||||
{
|
||||
bool Halted : 1;
|
||||
bool toggleCarry : 1;
|
||||
} flags_2;
|
||||
};
|
||||
uint32_t HeadP;
|
||||
uint32_t NextED;
|
||||
} usb_ed_t;
|
||||
|
||||
@@ -368,6 +334,8 @@ ohci_set_interrupt(usb_t *dev, uint8_t bit)
|
||||
|
||||
dev->ohci_mmio[OHCI_HcInterruptStatus].b[0] |= bit;
|
||||
|
||||
/* TODO: Does setting UnrecoverableError also assert PERR# on any emulated USB chipsets? */
|
||||
|
||||
ohci_update_irq(dev);
|
||||
}
|
||||
|
||||
@@ -412,10 +380,136 @@ static int ohci_copy_td_output(usb_t* dev, usb_td_t *td,
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define OHCI_TD_DIR(val) ((val >> 19) & 3)
|
||||
#define OHCI_ED_DIR(val) ((val >> 11) & 3)
|
||||
|
||||
uint8_t
|
||||
ohci_service_transfer_desc(usb_t* dev, usb_ed_t* endpoint_desc)
|
||||
{
|
||||
uint32_t td_addr = endpoint_desc->HeadP & ~(0xf);
|
||||
usb_td_t td;
|
||||
uint8_t dir, pid_token;
|
||||
uint32_t len = 0, pktlen = 0;
|
||||
uint32_t actual_length = 0;
|
||||
uint32_t i = 0;
|
||||
uint8_t device_result = 0;
|
||||
usb_device_t* target = NULL;
|
||||
|
||||
dma_bm_read(td_addr, (uint8_t*)&td, sizeof(usb_td_t), 4);
|
||||
|
||||
switch (dir = OHCI_ED_DIR(endpoint_desc->Control)) {
|
||||
case 1:
|
||||
case 2:
|
||||
break;
|
||||
default:
|
||||
dir = OHCI_TD_DIR(td.Control);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (dir) {
|
||||
case 0: /* Setup */
|
||||
pid_token = USB_PID_SETUP;
|
||||
break;
|
||||
case 1: /* OUT */
|
||||
pid_token = USB_PID_OUT;
|
||||
break;
|
||||
case 2: /* IN */
|
||||
pid_token = USB_PID_IN;
|
||||
break;
|
||||
}
|
||||
|
||||
if (td.CBP && td.BE) {
|
||||
if ((td.CBP & 0xfffff000) != (td.BE & 0xfffff000)) {
|
||||
len = (td.BE & 0xfff) + 0x1001 - (td.CBP & 0xfff);
|
||||
} else {
|
||||
if (td.CBP > td.BE) {
|
||||
ohci_set_interrupt(dev, OHCI_HcInterruptEnable_UE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
len = (td.BE - td.CBP) + 1;
|
||||
}
|
||||
if (len > sizeof(dev->ohci_usb_buf)) {
|
||||
len = sizeof(dev->ohci_usb_buf);
|
||||
}
|
||||
|
||||
pktlen = len;
|
||||
if (len && pid_token != USB_PID_IN) {
|
||||
pktlen = (endpoint_desc->Control >> 16) & 0xFFF;
|
||||
if (pktlen > len) {
|
||||
pktlen = len;
|
||||
}
|
||||
ohci_copy_td_output(dev, &td, dev->ohci_usb_buf, pktlen);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
if (!dev->ohci_devices[i])
|
||||
continue;
|
||||
|
||||
assert(dev->ohci_devices[i]->device_get_address != NULL);
|
||||
|
||||
if (dev->ohci_devices[i]->device_get_address(dev->ohci_devices[i]->priv) != (endpoint_desc->Control & 0x7f))
|
||||
continue;
|
||||
|
||||
target = dev->ohci_devices[i];
|
||||
break;
|
||||
}
|
||||
|
||||
if (!target)
|
||||
return 1;
|
||||
|
||||
device_result = target->device_process(target->priv, dev->ohci_usb_buf, &actual_length, pid_token, (endpoint_desc->Control & 0x780) >> 7, !(endpoint_desc->Control & (1 << 18)));
|
||||
|
||||
if ((actual_length == pktlen) || (pid_token == USB_PID_IN && (endpoint_desc->Control & (1 << 18)) && device_result == USB_ERROR_NO_ERROR)) {
|
||||
if (len == actual_length) {
|
||||
td.CBP = 0;
|
||||
} else {
|
||||
if ((td.CBP & 0xfff) + actual_length > 0xfff) {
|
||||
td.CBP = (td.BE & ~0xfff) + ((td.CBP + actual_length) & 0xfff);
|
||||
} else {
|
||||
td.CBP += actual_length;
|
||||
}
|
||||
}
|
||||
|
||||
td.Control |= (1 << 25); /* dataToggle[1] */
|
||||
td.Control ^= (1 << 24); /* dataToggle[0] */
|
||||
td.Control &= ~0xFC000000; /* Set both ErrorCount and ConditionCode to 0. */
|
||||
|
||||
if (pid_token != USB_PID_IN && len != actual_length) {
|
||||
goto exit_no_retire;
|
||||
}
|
||||
|
||||
endpoint_desc->HeadP &= ~0x2;
|
||||
if (td.Control & (1 << 24)) {
|
||||
endpoint_desc->HeadP |= 0x2;
|
||||
}
|
||||
} else {
|
||||
if (actual_length != 0xFFFFFFFF && actual_length >= 0) {
|
||||
td.Control &= ~0xF0000000;
|
||||
td.Control |= 0x90000000;
|
||||
} else {
|
||||
switch (device_result) {
|
||||
case USB_ERROR_NAK:
|
||||
return 1;
|
||||
}
|
||||
dev->ohci_interrupt_counter = 0;
|
||||
}
|
||||
|
||||
endpoint_desc->HeadP |= 0x1;
|
||||
}
|
||||
|
||||
endpoint_desc->HeadP &= 0xf;
|
||||
endpoint_desc->HeadP |= td.NextTD & ~0xf;
|
||||
td.NextTD = dev->ohci_mmio[OHCI_HcDoneHead].l;
|
||||
dev->ohci_mmio[OHCI_HcDoneHead].l = td_addr;
|
||||
i = (td.Control >> 21) & 7;
|
||||
if (i < dev->ohci_interrupt_counter) {
|
||||
dev->ohci_interrupt_counter = i;
|
||||
}
|
||||
exit_no_retire:
|
||||
dma_bm_write(td_addr, (uint8_t*)&td, sizeof(usb_td_t), 4);
|
||||
return !(td.Control & 0xF0000000);
|
||||
}
|
||||
|
||||
uint8_t
|
||||
@@ -435,13 +529,15 @@ ohci_service_endpoint_desc(usb_t* dev, uint32_t head)
|
||||
|
||||
next = endpoint_desc.NextED & ~(0xFu);
|
||||
|
||||
if (endpoint_desc.flags.Skip || endpoint_desc.flags_2.Halted)
|
||||
if ((endpoint_desc.Control & (1 << 13)) || (endpoint_desc.HeadP & (1 << 0)))
|
||||
continue;
|
||||
|
||||
if (endpoint_desc.flags.Format) {
|
||||
if (endpoint_desc.Control & 0x8000) {
|
||||
fatal("OHCI: Isochronous transfers not implemented!\n");
|
||||
}
|
||||
|
||||
active = 1;
|
||||
|
||||
while ((endpoint_desc.HeadP & ~(0xFu)) != endpoint_desc.TailP) {
|
||||
ohci_service_transfer_desc(dev, &endpoint_desc);
|
||||
}
|
||||
@@ -549,6 +645,23 @@ ohci_port_reset_callback_2(void* priv)
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus2].b[2] |= 0x10;
|
||||
}
|
||||
|
||||
static void
|
||||
ohci_soft_reset(usb_t* dev)
|
||||
{
|
||||
uint32_t old_HcControl = (dev->ohci_mmio[OHCI_HcControl].l & 0x100) | 0xc0;
|
||||
memset(dev->ohci_mmio, 0x00, 4096);
|
||||
dev->ohci_mmio[OHCI_HcRevision].b[0] = 0x10;
|
||||
dev->ohci_mmio[OHCI_HcRevision].b[1] = 0x01;
|
||||
dev->ohci_mmio[OHCI_HcRhDescriptorA].b[0] = 0x02;
|
||||
dev->ohci_mmio[OHCI_HcRhDescriptorA].b[1] = 0x02;
|
||||
dev->ohci_mmio[OHCI_HcFmInterval].l = 0x27782edf; /* FrameInterval = 11999, FSLargestDataPacket = 10104 */
|
||||
dev->ohci_mmio[OHCI_HcLSThreshold].l = 0x628;
|
||||
dev->ohci_mmio[OHCI_HcInterruptEnable].l |= (1 << 31);
|
||||
dev->ohci_mmio[OHCI_HcControl].l = old_HcControl;
|
||||
dev->ohci_interrupt_counter = 7;
|
||||
ohci_update_irq(dev);
|
||||
}
|
||||
|
||||
static void
|
||||
ohci_mmio_write(uint32_t addr, uint8_t val, void *p)
|
||||
{
|
||||
@@ -563,9 +676,19 @@ ohci_mmio_write(uint32_t addr, uint8_t val, void *p)
|
||||
|
||||
switch (addr) {
|
||||
case OHCI_aHcControl:
|
||||
old = dev->ohci_mmio[OHCI_HcControl].b[0];
|
||||
if ((val & 0xc0) == 0x00) {
|
||||
/* UsbReset */
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus1].b[2] = dev->ohci_mmio[OHCI_HcRhPortStatus2].b[2] = 0x16;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (dev->ohci_devices[i]) {
|
||||
dev->ohci_devices[i]->device_reset(dev->ohci_devices[i]->priv);
|
||||
}
|
||||
}
|
||||
} else if ((val & 0xc0) == 0x80 && (old & 0xc0) != (val & 0xc0)) {
|
||||
dev->ohci_mmio[OHCI_HcFmRemaining].l = 0;
|
||||
dev->ohci_initial_start = 1;
|
||||
timer_on_auto(&dev->ohci_frame_timer, 1000.);
|
||||
}
|
||||
break;
|
||||
case OHCI_aHcCommandStatus:
|
||||
@@ -578,10 +701,7 @@ ohci_mmio_write(uint32_t addr, uint8_t val, void *p)
|
||||
|
||||
/* bit HostControllerReset must be cleared for the controller to be seen as initialized */
|
||||
if (val & 0x01) {
|
||||
memset(dev->ohci_mmio, 0x00, 4096);
|
||||
dev->ohci_mmio[OHCI_HcRevision].b[0] = 0x10;
|
||||
dev->ohci_mmio[OHCI_HcRevision].b[1] = 0x01;
|
||||
dev->ohci_mmio[OHCI_HcRhDescriptorA].b[0] = 0x02;
|
||||
ohci_soft_reset(dev);
|
||||
val &= ~0x01;
|
||||
}
|
||||
break;
|
||||
@@ -722,6 +842,8 @@ ohci_mmio_write(uint32_t addr, uint8_t val, void *p)
|
||||
if (old & 0x01) {
|
||||
dev->ohci_mmio[addr >> 2].b[addr & 3] |= 0x10;
|
||||
timer_on_auto(&dev->ohci_port_reset_timer[(addr - OHCI_aHcRhPortStatus1) / 4], 10000.);
|
||||
if (dev->ohci_devices[(addr - OHCI_aHcRhPortStatus1) >> 2])
|
||||
dev->ohci_devices[(addr - OHCI_aHcRhPortStatus1) >> 2]->device_reset(dev->ohci_devices[(addr - OHCI_aHcRhPortStatus1) >> 2]->priv);
|
||||
} else
|
||||
dev->ohci_mmio[(addr + 2) >> 2].b[(addr + 2) & 3] |= 0x01;
|
||||
}
|
||||
@@ -817,13 +939,54 @@ ohci_update_mem_mapping(usb_t *dev, uint8_t base1, uint8_t base2, uint8_t base3,
|
||||
uint8_t
|
||||
usb_attach_device(usb_t *dev, usb_device_t* device, uint8_t bus_type)
|
||||
{
|
||||
switch (bus_type) {
|
||||
case USB_BUS_OHCI:
|
||||
{
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (!dev->ohci_devices[i]) {
|
||||
dev->ohci_devices[i] = device;
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[0] |= 0x1;
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[2] |= 0x1;
|
||||
if ((dev->ohci_mmio[OHCI_HcControl].b[0] & 0xc0) == 0xc0) {
|
||||
ohci_set_interrupt(dev, OHCI_HcInterruptEnable_RD);
|
||||
}
|
||||
ohci_set_interrupt(dev, OHCI_HcInterruptEnable_RHSC);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
|
||||
void
|
||||
usb_detach_device(usb_t *dev, uint8_t port, uint8_t bus_type)
|
||||
{
|
||||
/* Unused. */
|
||||
switch (bus_type) {
|
||||
case USB_BUS_OHCI:
|
||||
{
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (dev->ohci_devices[i]) {
|
||||
uint32_t old = dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].l;
|
||||
dev->ohci_devices[i] = NULL;
|
||||
if (dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[0] & 0x1) {
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[0] &= ~0x1;
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[2] |= 0x1;
|
||||
}
|
||||
if (dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[0] & 0x2) {
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[0] &= ~0x2;
|
||||
dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[2] |= 0x2;
|
||||
}
|
||||
if (old != dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].l)
|
||||
ohci_set_interrupt(dev, OHCI_HcInterruptEnable_RHSC);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -835,11 +998,8 @@ usb_reset(void *priv)
|
||||
dev->uhci_io[0x0c] = 0x40;
|
||||
dev->uhci_io[0x10] = dev->uhci_io[0x12] = 0x80;
|
||||
|
||||
memset(dev->ohci_mmio, 0x00, sizeof(dev->ohci_mmio));
|
||||
dev->ohci_mmio[OHCI_HcRevision].b[0] = 0x10;
|
||||
dev->ohci_mmio[OHCI_HcRevision].b[1] = 0x01;
|
||||
dev->ohci_mmio[OHCI_HcRhDescriptorA].b[0] = 0x02;
|
||||
dev->ohci_mmio[OHCI_HcRhDescriptorA].b[1] = 0x02;
|
||||
ohci_soft_reset(dev);
|
||||
dev->ohci_mmio[OHCI_HcControl].l = 0x00;
|
||||
|
||||
io_removehandler(dev->uhci_io_base, 0x20, uhci_reg_read, NULL, NULL, uhci_reg_write, uhci_reg_writew, NULL, dev);
|
||||
dev->uhci_enable = 0;
|
||||
|
Reference in New Issue
Block a user