From 857fdc8099459c8be8a72250cd39835eb69fc818 Mon Sep 17 00:00:00 2001 From: Fabian Vogt Date: Fri, 26 Mar 2021 16:14:38 +0100 Subject: [PATCH] Allow coexistance of usb and usb_cx2 usblink calls the correct functions depending on emulate_cx2 now. NavNet handling still needs fixing. --- TODO.md | 1 - core/mem.c | 1 + core/usb.c | 21 +-------------------- core/usb.h | 8 ++++++++ core/usb_cx2.cpp | 12 ------------ core/usb_cx2.h | 6 ++++++ core/usblink.c | 28 +++++++++++++++++----------- core/usblink_cx2.cpp | 1 - 8 files changed, 33 insertions(+), 45 deletions(-) diff --git a/TODO.md b/TODO.md index 3dc30b8c..2991685f 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,6 @@ ##CX II: * Standby needs fast timer + GPIO bits * Check other aladdin PMU bits -* USB needs refactoring, like usblink_cx2_reset called from usblink_reset * CX usblink doesn't work anymore * Flash creation needs changes * Load product from manuf diff --git a/core/mem.c b/core/mem.c index 4f566717..c07616a2 100644 --- a/core/mem.c +++ b/core/mem.c @@ -372,6 +372,7 @@ bool memory_initialize(uint32_t sdram_size) write_word_map[0xB4 >> 2] = nope_write_word; } add_reset_proc(usb_reset); + add_reset_proc(usb_cx2_reset); add_reset_proc(usblink_reset); read_word_map[0xC0 >> 2] = lcd_read_word; diff --git a/core/usb.c b/core/usb.c index 90b8e133..79bc3f32 100644 --- a/core/usb.c +++ b/core/usb.c @@ -21,53 +21,36 @@ struct usb_qh { // Queue head uint32_t bufptr[5]; } overlay; uint32_t reserved; - struct usb_setup { - uint8_t bmRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; - } setup; + struct usb_setup setup; }; static void usb_int_check() { int_set(INT_USB, (usb.usbsts & usb.usbintr) | ((usb.otgsc >> 24) & (usb.otgsc >> 16))); } -void usb_cx2_reset(); -void usb_cx2_bus_reset_on(); -void usb_cx2_bus_reset_off(); -void usb_cx2_receive_setup_packet(const void *packet); - void usb_reset() { - return usb_cx2_reset(); memset(&usb, 0, sizeof usb); usb.usbcmd = 0x80000; usb.portsc = 0xEC000004; usb.otgsc = 0x0F20; // 1120 if nothing plugged in usb_int_check(); usblink_reset(); - gui_debug_printf("usb_reset\n"); } void usb_bus_reset_on() { - return usb_cx2_bus_reset_on(); usb.portsc &= ~1; usb.portsc |= 0x0C000100; usb.deviceaddr = 0; usb.usbsts |= 0x40; usb.epsr = 0; usb_int_check(); - gui_debug_printf("usb reset on\n"); } void usb_bus_reset_off() { - return usb_cx2_bus_reset_off(); usb.portsc &= ~0x0C000100; usb.portsc |= 1; usb.usbsts |= 4; usb_int_check(); - gui_debug_printf("usb reset off\n"); } static void usb_prime(struct usb_qh *qh, uint32_t epbit) { @@ -102,8 +85,6 @@ static void usb_complete(struct usb_qh *qh, uint32_t epbit, uint32_t size) { } void usb_receive_setup_packet(int endpoint, const void *packet) { - return usb_cx2_receive_setup_packet(packet); - struct usb_qh *qh = (struct usb_qh *)(intptr_t)phys_mem_ptr(usb.eplistaddr + (endpoint * 0x80), 0x30); if (!qh) error("USB: bad QH"); diff --git a/core/usb.h b/core/usb.h index d3449a63..a6caf220 100644 --- a/core/usb.h +++ b/core/usb.h @@ -22,6 +22,14 @@ typedef struct usb_state { uint32_t epcomplete; // +1BC } usb_state; +typedef struct usb_setup { + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} __attribute__((packed)) usb_setup; + extern usb_state usb; void usb_reset(void); typedef struct emu_snapshot emu_snapshot; diff --git a/core/usb_cx2.cpp b/core/usb_cx2.cpp index 0eb674b1..a62ac25f 100644 --- a/core/usb_cx2.cpp +++ b/core/usb_cx2.cpp @@ -6,15 +6,6 @@ #include "usb_cx2.h" #include "usblink_cx2.h" -// TODO: Move into a separate header for usb stuff and add packed attrib -struct usb_setup { - uint8_t bmRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; -}; - usb_cx2_state usb_cx2; static void usb_cx2_int_check() @@ -112,7 +103,6 @@ static void usb_cx2_packet_from_calc(uint8_t ep, uint8_t *packet, size_t size) warn("Packet not handled"); } -extern "C" { void usb_cx2_reset() { usb_cx2 = {}; @@ -131,7 +121,6 @@ void usb_cx2_reset() usb_cx2.gisr[2] |= 1; usb_cx2_int_check(); - usblink_cx2_reset(); } void usb_cx2_bus_reset_on() @@ -173,7 +162,6 @@ void usb_cx2_receive_setup_packet(const usb_setup *packet) usb_cx2_int_check(); } -} void usb_cx2_fdma_update(int fdma) { diff --git a/core/usb_cx2.h b/core/usb_cx2.h index ae1a1650..ef3f5ff1 100644 --- a/core/usb_cx2.h +++ b/core/usb_cx2.h @@ -1,6 +1,7 @@ #ifndef USB_CX2_H #define USB_CX2_H +#include #include #ifdef __cplusplus @@ -64,14 +65,19 @@ typedef struct usb_cx2_state { extern struct usb_cx2_state usb_cx2; void usb_cx2_reset(void); + typedef struct emu_snapshot emu_snapshot; bool usb_cx2_suspend(emu_snapshot *snapshot); bool usb_cx2_resume(const emu_snapshot *snapshot); + uint8_t usb_cx2_read_byte(uint32_t addr); uint16_t usb_cx2_read_half(uint32_t addr); uint32_t usb_cx2_read_word(uint32_t addr); void usb_cx2_write_word(uint32_t addr, uint32_t value); +void usb_cx2_bus_reset_on(); +void usb_cx2_bus_reset_off(); +void usb_cx2_receive_setup_packet(const struct usb_setup *packet); bool usb_cx2_packet_to_calc(uint8_t ep, const uint8_t *packet, size_t size); #ifdef __cplusplus diff --git a/core/usblink.c b/core/usblink.c index ae752ab5..f13276c0 100644 --- a/core/usblink.c +++ b/core/usblink.c @@ -758,14 +758,6 @@ extern void usb_bus_reset_off(void); extern void usb_receive_setup_packet(int endpoint, void *packet); extern void usb_receive_packet(int endpoint, void *packet, uint32_t size); -struct usb_setup { - uint8_t bmRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; -}; - void usblink_reset() { if (put_file_state) { put_file_state = 0; @@ -776,6 +768,7 @@ void usblink_reset() { gui_usblink_changed(usblink_connected); usblink_state = 0; usblink_sending = false; + usblink_cx2_reset(); } void usblink_connect() { @@ -791,14 +784,27 @@ void usblink_connect() { void usblink_timer() { switch (usblink_state) { case 1: - usb_bus_reset_on(); + if(emulate_cx2) + usb_cx2_bus_reset_on(); + else + usb_bus_reset_on(); + usblink_state++; break; case 2: { - usb_bus_reset_off(); //printf("Sending SET_ADDRESS\n"); struct usb_setup packet = { 0, 5, 1, 0, 0 }; - usb_receive_setup_packet(0, &packet); + if(emulate_cx2) + { + usb_cx2_bus_reset_off(); + usb_cx2_receive_setup_packet(&packet); + } + else + { + usb_bus_reset_off(); + usb_receive_setup_packet(0, &packet); + } + usblink_state++; break; } diff --git a/core/usblink_cx2.cpp b/core/usblink_cx2.cpp index b2646dd8..62d408ca 100644 --- a/core/usblink_cx2.cpp +++ b/core/usblink_cx2.cpp @@ -387,5 +387,4 @@ void usblink_cx2_reset() { usblink_cx2_state.seqno = 0; usblink_cx2_state.handshake_complete = false; - usblink_reset(); }