From a6086c451e1bfc9d83d8582800ff71f049865ba5 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Mon, 8 May 2023 00:50:00 +0600 Subject: [PATCH 1/9] usb: Start finalizing work on OHCI --- src/include/86box/usb.h | 53 ++++++++++++++++------------- src/usb.c | 75 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 25 deletions(-) diff --git a/src/include/86box/usb.h b/src/include/86box/usb.h index 4c6fc289b..1f5413cd7 100644 --- a/src/include/86box/usb.h +++ b/src/include/86box/usb.h @@ -24,6 +24,33 @@ extern "C" { typedef struct usb_t usb_t; +/* 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); + /* Device reset. */ + void (*device_reset)(void* priv); + /* Get address. */ + uint8_t (*device_get_address)(void* priv); + + void* priv; +} usb_device_t; + +enum usb_bus_types +{ + USB_BUS_OHCI = 0, + USB_BUS_UHCI, + USB_BUS_MAX +}; + /* USB device creation parameters struct */ typedef struct { @@ -52,6 +79,8 @@ typedef struct usb_t pc_timer_t ohci_frame_timer; pc_timer_t ohci_port_reset_timer[2]; uint8_t ohci_interrupt_counter : 3; + usb_device_t* ohci_devices[2]; + usb_device_t* uhci_devices[2]; usb_params_t* usb_params; } usb_t; @@ -79,30 +108,6 @@ typedef struct #pragma pack(pop) -/* 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); - /* Device reset */ - void (*device_reset)(void* priv); - - void* priv; -} usb_device_t; - -enum usb_bus_types -{ - USB_BUS_OHCI = 0, - USB_BUS_UHCI = 1 -}; - /* Global variables. */ extern const device_t usb_device; diff --git a/src/usb.c b/src/usb.c index 03b5f6546..0b9a8255f 100644 --- a/src/usb.c +++ b/src/usb.c @@ -371,12 +371,85 @@ ohci_set_interrupt(usb_t *dev, uint8_t bit) ohci_update_irq(dev); } +/* Next two functions ported over from QEMU. */ +static int ohci_copy_td_input(usb_t* dev, usb_td_t *td, + uint8_t *buf, int len) +{ + uint32_t ptr, n; + + ptr = td->CBP; + n = 0x1000 - (ptr & 0xfff); + if (n > len) { + n = len; + } + dma_bm_write(ptr, buf, n, 1); + if (n == len) { + return 0; + } + ptr = td->BE & ~0xfffu; + buf += n; + dma_bm_write(ptr, buf, len - n, 1); + return 0; +} + +static int ohci_copy_td_output(usb_t* dev, usb_td_t *td, + uint8_t *buf, int len) +{ + uint32_t ptr, n; + + ptr = td->CBP; + n = 0x1000 - (ptr & 0xfff); + if (n > len) { + n = len; + } + dma_bm_read(ptr, buf, n, 1); + if (n == len) { + return 0; + } + ptr = td->BE & ~0xfffu; + buf += n; + dma_bm_read(ptr, buf, len - n, 1); + return 0; +} + +uint8_t +ohci_service_transfer_desc(usb_t* dev, usb_ed_t* endpoint_desc) +{ + +} + uint8_t ohci_service_endpoint_desc(usb_t* dev, uint32_t head) { usb_ed_t endpoint_desc; + uint8_t active = 0; + uint32_t next = 0; + uint32_t cur = 0; + uint32_t limit_counter = 0; + + if (head == 0) + return 0; - return 0; + for (cur = head; cur && limit_counter++ < ENDPOINT_DESC_LIMIT; cur = next) { + dma_bm_read(cur, (uint8_t*)&endpoint_desc, sizeof(usb_ed_t), 4); + + next = endpoint_desc.NextED & ~(0xFu); + + if (endpoint_desc.flags.Skip || endpoint_desc.flags_2.Halted) + continue; + + if (endpoint_desc.flags.Format) { + fatal("OHCI: Isochronous transfers not implemented!\n"); + } + + while ((endpoint_desc.HeadP & ~(0xFu)) != endpoint_desc.TailP) { + ohci_service_transfer_desc(dev, &endpoint_desc); + } + + dma_bm_write(cur, (uint8_t*)&endpoint_desc, sizeof(usb_ed_t), 4); + } + + return active; } void From 9c09d4260ed4bd88365a442977efc195d048e359 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Mon, 8 May 2023 16:51:16 +0600 Subject: [PATCH 2/9] usb: Finish work on OHCI --- src/include/86box/usb.h | 25 +++- src/usb.c | 260 ++++++++++++++++++++++++++++++++-------- 2 files changed, 229 insertions(+), 56 deletions(-) diff --git a/src/include/86box/usb.h b/src/include/86box/usb.h index 1f5413cd7..b9497b92e 100644 --- a/src/include/86box/usb.h +++ b/src/include/86box/usb.h @@ -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; diff --git a/src/usb.c b/src/usb.c index 0b9a8255f..f6262e11f 100644 --- a/src/usb.c +++ b/src/usb.c @@ -22,6 +22,7 @@ #include #include #include +#include #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; From 2fac3e5dc59b26e0e5e43c25be7bd56e52e93d95 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Mon, 8 May 2023 17:13:34 +0600 Subject: [PATCH 3/9] usb: don't process EOF on very first SOF --- src/usb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/usb.c b/src/usb.c index f6262e11f..1dd6aa02c 100644 --- a/src/usb.c +++ b/src/usb.c @@ -552,7 +552,8 @@ void ohci_end_of_frame(usb_t* dev) { usb_hcca_t hcca; - /* TODO: Put endpoint and transfer descriptor processing here. */ + if (dev->ohci_initial_start) + return; dma_bm_read(dev->ohci_mmio[OHCI_HcHCCA].l, (uint8_t*)&hcca, sizeof(usb_hcca_t), 4); if (dev->ohci_mmio[OHCI_HcControl].l & OHCI_HcControl_PeriodicListEnable) { @@ -605,6 +606,7 @@ ohci_end_of_frame(usb_t* dev) void ohci_start_of_frame(usb_t* dev) { + dev->ohci_initial_start = 0; ohci_set_interrupt(dev, OHCI_HcInterruptEnable_SO); } From 50b0fe499086fa36b85ecc214fa5332a7c109945 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Mon, 8 May 2023 22:30:28 +0600 Subject: [PATCH 4/9] usb: Add ability to attach and detach devices for real --- src/include/86box/usb.h | 1 + src/usb.c | 50 +++++++++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/include/86box/usb.h b/src/include/86box/usb.h index b9497b92e..4dbec55c3 100644 --- a/src/include/86box/usb.h +++ b/src/include/86box/usb.h @@ -123,6 +123,7 @@ typedef struct /* Global variables. */ extern const device_t usb_device; +extern usb_t* usb_device_inst; /* Functions. */ extern void uhci_update_io_mapping(usb_t *dev, uint8_t base_l, uint8_t base_h, int enable); diff --git a/src/usb.c b/src/usb.c index 1dd6aa02c..2ef5bb895 100644 --- a/src/usb.c +++ b/src/usb.c @@ -129,6 +129,8 @@ enum OHCI_HcControl_BulkListEnable = 1 << 4 }; +usb_t* usb_device_inst = NULL; + static void usb_interrupt_ohci(usb_t *dev, uint32_t level) { @@ -851,8 +853,12 @@ ohci_mmio_write(uint32_t addr, uint8_t val, void *p) } if (val & 0x08) dev->ohci_mmio[addr >> 2].b[addr & 3] &= ~0x04; - if (val & 0x04) - dev->ohci_mmio[addr >> 2].b[addr & 3] |= 0x04; + if (val & 0x04) { + if (old & 0x01) + dev->ohci_mmio[addr >> 2].b[addr & 3] |= 0x04; + else + dev->ohci_mmio[(addr + 2) >> 2].b[(addr + 2) & 3] |= 0x01; + } if (val & 0x02) { if (old & 0x01) dev->ohci_mmio[addr >> 2].b[addr & 3] |= 0x02; @@ -946,13 +952,16 @@ usb_attach_device(usb_t *dev, usb_device_t* device, uint8_t bus_type) { 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] = 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); + if (old != dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].l) { + dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * i)].b[2] |= 0x1; + ohci_set_interrupt(dev, OHCI_HcInterruptEnable_RHSC); + } return i; } } @@ -968,23 +977,24 @@ usb_detach_device(usb_t *dev, uint8_t port, uint8_t bus_type) 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; + if (port > 2) + return; + if (dev->ohci_devices[port]) { + uint32_t old = dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].l; + dev->ohci_devices[port] = NULL; + if (dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].b[0] & 0x1) { + dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].b[0] &= ~0x1; + dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].b[2] |= 0x1; } + if (dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].b[0] & 0x2) { + dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].b[0] &= ~0x2; + dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].b[2] |= 0x2; + } + if (old != dev->ohci_mmio[OHCI_HcRhPortStatus1 + (4 * port)].l) + ohci_set_interrupt(dev, OHCI_HcInterruptEnable_RHSC); + return; } + } break; } @@ -1046,6 +1056,8 @@ usb_init_ext(const device_t *info, void *params) usb_reset(dev); + usb_device_inst = dev; + return dev; } From 41ca38341c31d2a4ad433f5f92e9093b9ac2184a Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Mon, 8 May 2023 23:27:52 +0600 Subject: [PATCH 5/9] usb: Structure definitions for USB device descriptors --- src/include/86box/usb.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/include/86box/usb.h b/src/include/86box/usb.h index 4dbec55c3..d94d8a137 100644 --- a/src/include/86box/usb.h +++ b/src/include/86box/usb.h @@ -107,6 +107,34 @@ typedef struct uint8_t bDescriptorType; } usb_desc_base_t; +typedef struct +{ + usb_desc_base_t base; + + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; +} usb_desc_interface_t; + +typedef struct +{ + usb_desc_base_t base; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint8_t wMaxPacketSize; + uint8_t bInterval; +} usb_desc_endpoint_t; + +typedef struct +{ + usb_desc_base_t base; + uint16_t bString[]; +} usb_desc_string_t; + typedef struct { usb_desc_base_t base; @@ -117,6 +145,8 @@ typedef struct uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; + + usb_desc_interface_t interface_descs[]; } usb_desc_conf_t; #pragma pack(pop) From 8f7752e63cb52d4af2bda4eaa7e7be870682670e Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Tue, 9 May 2023 00:14:15 +0600 Subject: [PATCH 6/9] usb: Add a bit of logging --- src/usb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/usb.c b/src/usb.c index 2ef5bb895..894e0a864 100644 --- a/src/usb.c +++ b/src/usb.c @@ -681,6 +681,9 @@ ohci_mmio_write(uint32_t addr, uint8_t val, void *p) switch (addr) { case OHCI_aHcControl: old = dev->ohci_mmio[OHCI_HcControl].b[0]; +#ifdef ENABLE_USB_LOG + usb_log("OHCI: OHCI state 0x%X\n", (val & 0xc0)); +#endif if ((val & 0xc0) == 0x00) { /* UsbReset */ dev->ohci_mmio[OHCI_HcRhPortStatus1].b[2] = dev->ohci_mmio[OHCI_HcRhPortStatus2].b[2] = 0x16; From d0845ccadeb360203693b13138c905446d273ee5 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Wed, 10 May 2023 17:09:13 +0600 Subject: [PATCH 7/9] usb: Infrastructure changes Make OHCI OwnershipChange work properly --- src/chipset/ali1543.c | 1 + src/chipset/ali6117.c | 1 - src/chipset/intel_piix.c | 1 + src/chipset/sis_5571.c | 1 + src/chipset/stpc.c | 1 + src/chipset/via_pipc.c | 1 + src/include/86box/usb.h | 96 ++++++++++++++++++++++++++++------------ src/usb.c | 4 +- 8 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/chipset/ali1543.c b/src/chipset/ali1543.c index 2e2f74305..26673edec 100644 --- a/src/chipset/ali1543.c +++ b/src/chipset/ali1543.c @@ -20,6 +20,7 @@ #include #include #include +#include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/timer.h> diff --git a/src/chipset/ali6117.c b/src/chipset/ali6117.c index 98451067a..796ca880f 100644 --- a/src/chipset/ali6117.c +++ b/src/chipset/ali6117.c @@ -30,7 +30,6 @@ #include <86box/pit.h> #include <86box/device.h> #include <86box/port_92.h> -#include <86box/usb.h> #include <86box/hdc.h> #include <86box/hdc_ide.h> #include <86box/chipset.h> diff --git a/src/chipset/intel_piix.c b/src/chipset/intel_piix.c index 470978611..f550d9503 100644 --- a/src/chipset/intel_piix.c +++ b/src/chipset/intel_piix.c @@ -23,6 +23,7 @@ #include #include #include +#include #define HAVE_STDARG_H #include <86box/86box.h> #include "cpu.h" diff --git a/src/chipset/sis_5571.c b/src/chipset/sis_5571.c index c158e2d63..a20f5fadd 100644 --- a/src/chipset/sis_5571.c +++ b/src/chipset/sis_5571.c @@ -20,6 +20,7 @@ #include #include #include +#include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/device.h> diff --git a/src/chipset/stpc.c b/src/chipset/stpc.c index 2e4b045f2..822f462ad 100644 --- a/src/chipset/stpc.c +++ b/src/chipset/stpc.c @@ -20,6 +20,7 @@ #include #include #include +#include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/mem.h> diff --git a/src/chipset/via_pipc.c b/src/chipset/via_pipc.c index c2abc4465..067b733a2 100644 --- a/src/chipset/via_pipc.c +++ b/src/chipset/via_pipc.c @@ -26,6 +26,7 @@ #include #include #include +#include #define HAVE_STDARG_H #include <86box/86box.h> #include "cpu.h" diff --git a/src/include/86box/usb.h b/src/include/86box/usb.h index d94d8a137..455282f71 100644 --- a/src/include/86box/usb.h +++ b/src/include/86box/usb.h @@ -23,6 +23,7 @@ extern "C" { #endif typedef struct usb_t usb_t; +typedef struct usb_device_t usb_device_t; enum usb_pid { @@ -39,22 +40,6 @@ enum usb_errors USB_ERROR_UNDERRUN = 3 }; -/* USB endpoint device struct. Incomplete and unused. */ -typedef struct -{ - uint16_t vendor_id; - uint16_t device_id; - - /* 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. */ - uint8_t (*device_get_address)(void* priv); - - void* priv; -} usb_device_t; - enum usb_bus_types { USB_BUS_OHCI = 0, @@ -107,6 +92,35 @@ typedef struct uint8_t bDescriptorType; } usb_desc_base_t; +typedef struct +{ + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} usb_desc_setup_t; + +typedef struct +{ + usb_desc_base_t base; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint16_t wMaxPacketSize; + uint8_t bInterval; +} usb_desc_endpoint_t; + +typedef struct +{ + usb_desc_base_t base; + + uint16_t bcdHID; + uint8_t bCountryCode; + uint8_t bNumDescriptors; + uint8_t bDescriptorType; + uint16_t wDescriptorLength; +} usb_desc_hid_t; + typedef struct { usb_desc_base_t base; @@ -123,16 +137,7 @@ typedef struct typedef struct { usb_desc_base_t base; - uint8_t bEndpointAddress; - uint8_t bmAttributes; - uint8_t wMaxPacketSize; - uint8_t bInterval; -} usb_desc_endpoint_t; - -typedef struct -{ - usb_desc_base_t base; - uint16_t bString[]; + char16_t bString[]; } usb_desc_string_t; typedef struct @@ -145,12 +150,47 @@ typedef struct uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; - - usb_desc_interface_t interface_descs[]; } usb_desc_conf_t; +typedef struct +{ + usb_desc_base_t base; + + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +} usb_desc_device_t; + #pragma pack(pop) +/* USB endpoint device struct. Incomplete and unused. */ +typedef struct usb_device_t +{ + usb_desc_device_t device_desc; + struct { + usb_desc_conf_t conf_desc; + usb_desc_base_t* other_descs[16]; + } conf_desc_items; + + /* 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. */ + uint8_t (*device_get_address)(void* priv); + + void* priv; +} usb_device_t; + /* Global variables. */ extern const device_t usb_device; extern usb_t* usb_device_inst; diff --git a/src/usb.c b/src/usb.c index 894e0a864..5d661bd72 100644 --- a/src/usb.c +++ b/src/usb.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #define HAVE_STDARG_H #include <86box/86box.h> @@ -702,8 +703,9 @@ ohci_mmio_write(uint32_t addr, uint8_t val, void *p) /* bit OwnershipChangeRequest triggers an ownership change (SMM <-> OS) */ if (val & 0x08) { dev->ohci_mmio[OHCI_HcInterruptStatus].b[3] = 0x40; - if ((dev->ohci_mmio[OHCI_HcInterruptEnable].b[3] & 0xc0) == 0xc0) + if ((dev->ohci_mmio[OHCI_HcInterruptEnable].b[3] & 0x40) == 0x40) { smi_raise(); + } } /* bit HostControllerReset must be cleared for the controller to be seen as initialized */ From 98ebfce4601df14b68634a4cce1a01eb7204bf51 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Wed, 10 May 2023 17:19:35 +0600 Subject: [PATCH 8/9] usb: Revert usage of uchar.h --- src/chipset/ali1543.c | 1 - src/chipset/intel_piix.c | 1 - src/chipset/sis_5571.c | 1 - src/chipset/stpc.c | 1 - src/chipset/via_pipc.c | 1 - src/include/86box/usb.h | 14 +++++++++++++- src/usb.c | 1 - 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/chipset/ali1543.c b/src/chipset/ali1543.c index 26673edec..2e2f74305 100644 --- a/src/chipset/ali1543.c +++ b/src/chipset/ali1543.c @@ -20,7 +20,6 @@ #include #include #include -#include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/timer.h> diff --git a/src/chipset/intel_piix.c b/src/chipset/intel_piix.c index f550d9503..470978611 100644 --- a/src/chipset/intel_piix.c +++ b/src/chipset/intel_piix.c @@ -23,7 +23,6 @@ #include #include #include -#include #define HAVE_STDARG_H #include <86box/86box.h> #include "cpu.h" diff --git a/src/chipset/sis_5571.c b/src/chipset/sis_5571.c index a20f5fadd..c158e2d63 100644 --- a/src/chipset/sis_5571.c +++ b/src/chipset/sis_5571.c @@ -20,7 +20,6 @@ #include #include #include -#include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/device.h> diff --git a/src/chipset/stpc.c b/src/chipset/stpc.c index 822f462ad..2e4b045f2 100644 --- a/src/chipset/stpc.c +++ b/src/chipset/stpc.c @@ -20,7 +20,6 @@ #include #include #include -#include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/mem.h> diff --git a/src/chipset/via_pipc.c b/src/chipset/via_pipc.c index 067b733a2..c2abc4465 100644 --- a/src/chipset/via_pipc.c +++ b/src/chipset/via_pipc.c @@ -26,7 +26,6 @@ #include #include #include -#include #define HAVE_STDARG_H #include <86box/86box.h> #include "cpu.h" diff --git a/src/include/86box/usb.h b/src/include/86box/usb.h index 455282f71..d0801b99c 100644 --- a/src/include/86box/usb.h +++ b/src/include/86box/usb.h @@ -92,6 +92,18 @@ typedef struct uint8_t bDescriptorType; } usb_desc_base_t; +enum usb_desc_setup_req_types +{ + USB_SETUP_TYPE_DEVICE = 0x0, + USB_SETUP_TYPE_INTERFACE = 0x1, + USB_SETUP_TYPE_ENDPOING = 0x2, + USB_SETUP_TYPE_OTHER = 0x3, +}; + +#define USB_SETUP_TYPE_MAX 0x1F + +#define USB_SETUP_DEV_TO_HOST 0x80 + typedef struct { uint8_t bmRequestType; @@ -137,7 +149,7 @@ typedef struct typedef struct { usb_desc_base_t base; - char16_t bString[]; + uint16_t bString[]; } usb_desc_string_t; typedef struct diff --git a/src/usb.c b/src/usb.c index 5d661bd72..fce5fe0b0 100644 --- a/src/usb.c +++ b/src/usb.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #define HAVE_STDARG_H #include <86box/86box.h> From 889392539666e87292cf2cf647c5c4559bbeabd7 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Wed, 10 May 2023 17:28:40 +0600 Subject: [PATCH 9/9] usb: Return early on invalid directions --- src/usb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/usb.c b/src/usb.c index fce5fe0b0..85c2a6fc8 100644 --- a/src/usb.c +++ b/src/usb.c @@ -390,7 +390,7 @@ 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; + uint8_t dir, pid_token = 255; uint32_t len = 0, pktlen = 0; uint32_t actual_length = 0; uint32_t i = 0; @@ -418,6 +418,8 @@ ohci_service_transfer_desc(usb_t* dev, usb_ed_t* endpoint_desc) case 2: /* IN */ pid_token = USB_PID_IN; break; + default: + return 1; } if (td.CBP && td.BE) {