2022-02-19 10:24:47 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// Copyright (C) 2021-2022 Martin Whitaker.
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-07-16 06:34:08 -05:00
|
|
|
#include "heap.h"
|
2022-02-19 10:24:47 -06:00
|
|
|
#include "io.h"
|
|
|
|
#include "memrw32.h"
|
|
|
|
#include "memsize.h"
|
2022-04-09 16:42:59 -05:00
|
|
|
#include "pci.h"
|
2022-02-19 10:24:47 -06:00
|
|
|
#include "usb.h"
|
|
|
|
|
2022-03-11 11:30:25 -06:00
|
|
|
#include "string.h"
|
2022-02-19 10:24:47 -06:00
|
|
|
#include "unistd.h"
|
|
|
|
|
|
|
|
#include "uhci.h"
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Values defined by the UHCI specification.
|
|
|
|
|
|
|
|
// Basic limits
|
|
|
|
|
|
|
|
#define UHCI_FL_LENGTH 1024 // Maximum number of entries in periodic frame list
|
|
|
|
|
2022-04-09 16:42:59 -05:00
|
|
|
// Register addresses in PCI Config space
|
|
|
|
|
|
|
|
#define UHCI_LEGSUP 0xc0 // Legacy Support register
|
|
|
|
|
|
|
|
// Legacy Support register
|
|
|
|
|
|
|
|
#define UHCI_LEGSUP_TBY60R 0x0100 // Trap By 0x60 Read Status/Clear
|
|
|
|
#define UHCI_LEGSUP_TBY60W 0x0200 // Trap By 0x60 Write Status/Clear
|
|
|
|
#define UHCI_LEGSUP_TBY64R 0x0400 // Trap By 0x64 Read Status/Clear
|
|
|
|
#define UHCI_LEGSUP_TBY64W 0x0800 // Trap By 0x64 Write Status/Clear
|
|
|
|
|
|
|
|
#define UHCI_LEGSUP_A20PTS 0x8000 // End of A20GATE Pass Through Status/Clear
|
|
|
|
|
|
|
|
#define UHCI_LEGSUP_CLEAR (UHCI_LEGSUP_TBY60R | UHCI_LEGSUP_TBY60W | UHCI_LEGSUP_TBY64R | UHCI_LEGSUP_TBY64W | UHCI_LEGSUP_A20PTS)
|
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
// Register addresses in I/O space
|
|
|
|
|
|
|
|
#define UHCI_USBCMD (io_base+0x00) // USB Command register
|
|
|
|
#define UHCI_USBSTS (io_base+0x02) // USB Status register
|
|
|
|
#define UHCI_USBINTR (io_base+0x04) // USB Interrupt Enable register
|
|
|
|
#define UHCI_FRNUM (io_base+0x06) // Frame Number register
|
|
|
|
#define UHCI_FLBASE (io_base+0x08) // Frame List Base Address register
|
2022-04-09 16:42:59 -05:00
|
|
|
#define UHCI_SOF (io_base+0x0c) // Start of Frame modify register
|
2022-02-19 10:24:47 -06:00
|
|
|
#define UHCI_PORT_SC(n) (io_base+0x10+2*n) // Port n Status and Control register (n = 0..MaxPortIdx)
|
|
|
|
|
|
|
|
// USB Command register
|
|
|
|
|
|
|
|
#define UHCI_USBCMD_R_S 0x0001 // Run/Stop
|
|
|
|
#define UHCI_USBCMD_HCR 0x0002 // Host Controller Reset
|
|
|
|
#define UHCI_USBCMD_GR 0x0004 // Global Reset
|
|
|
|
#define UHCI_USBCMD_MAXP 0x0080 // Max Packet
|
|
|
|
|
2022-08-15 10:51:48 -05:00
|
|
|
// USB Status register
|
2022-02-19 10:24:47 -06:00
|
|
|
|
|
|
|
#define UHCI_USBSTS_INT 0x0001 // Interrupt
|
|
|
|
#define UHCI_USBSTS_ERR 0x0002 // Error interrupt
|
|
|
|
#define UHCI_USBSTS_RESUME 0x0004 // Resume Detect
|
|
|
|
#define UHCI_USBSTS_HSE 0x0008 // Host System Error
|
|
|
|
#define UHCI_USBSTS_HCPE 0x0010 // Host Controller Processor Error
|
|
|
|
#define UHCI_USBSTS_HCH 0x0020 // Host Controller Halted
|
|
|
|
|
2022-04-09 16:42:59 -05:00
|
|
|
// USB Interrupt Enable register
|
|
|
|
|
|
|
|
#define UHCI_USBINTR_NONE 0x0000 // No interrupts enabled
|
|
|
|
|
|
|
|
// USB SOF register
|
|
|
|
|
|
|
|
#define UHCI_SOF_DEFAULT 0x40 // Default timing (1ms with 12MHz clock)
|
|
|
|
|
2022-03-27 15:55:05 -05:00
|
|
|
// Port Status and Control register
|
2022-02-19 10:24:47 -06:00
|
|
|
|
|
|
|
#define UHCI_PORT_SC_CCS 0x0001 // Current Connect Status
|
|
|
|
#define UHCI_PORT_SC_CCSC 0x0002 // Current Connect Status Change
|
|
|
|
#define UHCI_PORT_SC_PED 0x0004 // Port Enable/Disable
|
|
|
|
#define UHCI_PORT_SC_PEDC 0x0008 // Port Enable/Disable Change
|
|
|
|
#define UHCI_PORT_SC_RESUME 0x0040 // Resume Detect
|
|
|
|
#define UHCI_PORT_SC_VALID 0x0080 // Reserved bit (always set)
|
|
|
|
#define UHCI_PORT_SC_LSDA 0x0100 // Low Speed Device Attached
|
|
|
|
#define UHCI_PORT_SC_PR 0x0200 // Port Reset
|
|
|
|
#define UHCI_PORT_SC_SUSPEND 0x1000 // Suspend
|
|
|
|
|
2022-03-27 15:55:05 -05:00
|
|
|
// Link Pointer
|
2022-02-19 10:24:47 -06:00
|
|
|
|
|
|
|
#define UHCI_LP_TERMINATE 0x00000001 // Terminate (T) bit
|
|
|
|
|
|
|
|
#define UHCI_LP_TYPE_TD (0 << 1) // Type is Transfer Descriptor
|
|
|
|
#define UHCI_LP_TYPE_QH (1 << 1) // Type is Queue Head
|
|
|
|
|
|
|
|
#define UHCI_LP_BREADTH_FIRST (0 << 2)
|
|
|
|
#define UHCI_LP_DEPTH_FIRST (1 << 2)
|
|
|
|
|
|
|
|
// Queue Element Transfer Descriptor data structure
|
|
|
|
|
|
|
|
// - control_status member (32 bits)
|
|
|
|
|
|
|
|
#define UHCI_TD_BS_ERR 0x00020000 // Bitstuff Error
|
|
|
|
#define UHCI_TD_CRC_TO 0x00040000 // CRC or Time Out Error
|
|
|
|
#define UHCI_TD_NAK_RX 0x00080000 // NAK received
|
|
|
|
#define UHCI_TD_BABBLE 0x00100000 // Babble Detected
|
|
|
|
#define UHCI_TD_DB_ERR 0x00200000 // Data Buffer Error
|
|
|
|
#define UHCI_TD_STALLED 0x00400000 // Halted
|
|
|
|
#define UHCI_TD_ACTIVE 0x00800000 // Active
|
|
|
|
|
|
|
|
#define UHCI_TD_IOC_N (0 << 24) // Interrupt On Completion is off
|
|
|
|
#define UHCI_TD_IOC_Y (1 << 24) // Interrupt On Completion is on
|
|
|
|
|
|
|
|
#define UHCI_TD_ISO (1 << 25) // Isochronous Select
|
|
|
|
|
|
|
|
#define UHCI_TD_FULL_SPEED (0 << 26) // Full Speed
|
|
|
|
#define UHCI_TD_LOW_SPEED (1 << 26) // Low Speed
|
|
|
|
|
|
|
|
#define UHCI_TD_CERR(n) ((n) << 27) // Error Counter = n (n = 1..3)
|
|
|
|
|
|
|
|
#define UHCI_TD_SPD (1 << 29) // Short Packet Detect
|
|
|
|
|
|
|
|
// - token member (32 bits)
|
|
|
|
|
|
|
|
#define UHCI_TD_PID_OUT 0x000000e1 // PID Code is OUT
|
|
|
|
#define UHCI_TD_PID_IN 0x00000069 // PID Code is IN
|
|
|
|
#define UHCI_TD_PID_SETUP 0x0000002d // PID Code is SETUP
|
|
|
|
|
|
|
|
#define UHCI_TD_DEVICE_ADDR(n) ((n) << 8) // Device Address = n (n = 0..127)
|
|
|
|
|
|
|
|
#define UHCI_TD_ENDPOINT(n) ((n) << 15) // Endpoint = n (n = 0..15)
|
|
|
|
|
|
|
|
#define UHCI_TD_DT(n) ((n) << 19) // Data Toggle = n (n = 0,1)
|
|
|
|
|
|
|
|
#define UHCI_TD_LENGTH(n) ((((n) - 1) & 0x7ff) << 21)
|
|
|
|
|
|
|
|
// Values specific to this driver.
|
|
|
|
|
|
|
|
#define MAX_UHCI_PORTS 8 // the UHCI spec. doesn't define this, so pick a sensible number
|
|
|
|
|
|
|
|
#define MAX_KEYBOARDS 8 // per host controller
|
|
|
|
|
2022-04-11 15:17:09 -05:00
|
|
|
#define MAX_PACKETS 32 // per data transfer (must be >= MAX_KEYBOARDS)
|
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
#define WS_QH_SIZE (1 + MAX_KEYBOARDS) // Queue Head Descriptors
|
2022-04-11 15:17:09 -05:00
|
|
|
#define WS_TD_SIZE (2 + MAX_PACKETS) // Queue Transfer Descriptors
|
2022-02-19 10:24:47 -06:00
|
|
|
|
|
|
|
#define MILLISEC 1000 // in microseconds
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Data structures defined by the UHCI specification.
|
|
|
|
|
|
|
|
typedef volatile struct {
|
|
|
|
uint32_t link_ptr;
|
|
|
|
uint32_t control_status;
|
|
|
|
uint32_t token;
|
|
|
|
uint32_t buffer_ptr;
|
|
|
|
uint32_t driver_data[4];
|
|
|
|
} uhci_td_t __attribute__ ((aligned (16)));
|
|
|
|
|
|
|
|
typedef volatile struct {
|
|
|
|
uint32_t qh_link_ptr;
|
|
|
|
uint32_t qe_link_ptr;
|
|
|
|
uint32_t padding[2];
|
|
|
|
} uhci_qh_t __attribute__ ((aligned (16)));
|
|
|
|
|
|
|
|
// Data structures specific to this implementation.
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
hcd_workspace_t base_ws;
|
|
|
|
|
|
|
|
// System memory data structures used by the host controller.
|
2022-05-01 10:44:46 -05:00
|
|
|
uhci_qh_t qh[WS_QH_SIZE] __attribute__ ((aligned (16)));
|
|
|
|
uhci_td_t td[WS_TD_SIZE] __attribute__ ((aligned (16)));
|
2022-02-19 10:24:47 -06:00
|
|
|
|
|
|
|
// Keyboard data transfer buffers.
|
|
|
|
hid_kbd_rpt_t kbd_rpt[MAX_KEYBOARDS];
|
|
|
|
|
2022-05-01 10:44:46 -05:00
|
|
|
// Saved keyboard reports.
|
|
|
|
hid_kbd_rpt_t prev_kbd_rpt[MAX_KEYBOARDS];
|
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
// I/O base address of the host controller registers.
|
|
|
|
uint16_t io_base;
|
|
|
|
|
|
|
|
// Number of keyboards detected.
|
|
|
|
int num_keyboards;
|
|
|
|
} workspace_t __attribute__ ((aligned (256)));
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Private Functions
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool io_wait_until_clr(uint16_t io_reg, uint16_t bit_mask, int max_time)
|
|
|
|
{
|
|
|
|
int timer = max_time >> 3;
|
|
|
|
while (inw(io_reg) & bit_mask) {
|
|
|
|
if (timer == 0) return false;
|
|
|
|
usleep(8);
|
|
|
|
timer--;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool io_wait_until_set(uint16_t io_reg, uint16_t bit_mask, int max_time)
|
|
|
|
{
|
|
|
|
int timer = max_time >> 3;
|
|
|
|
while (~inw(io_reg) & bit_mask) {
|
|
|
|
if (timer == 0) return false;
|
|
|
|
usleep(8);
|
|
|
|
timer--;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool reset_host_controller(uint16_t io_base)
|
|
|
|
{
|
|
|
|
outw(inw(UHCI_USBCMD) | UHCI_USBCMD_HCR, UHCI_USBCMD);
|
|
|
|
|
|
|
|
usleep(1*MILLISEC); // allow the controller some time to recover from reset
|
|
|
|
|
|
|
|
return io_wait_until_clr(UHCI_USBCMD, UHCI_USBCMD_HCR, 1000*MILLISEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool start_host_controller(uint16_t io_base)
|
|
|
|
{
|
2022-04-06 16:22:46 -05:00
|
|
|
outw(UHCI_USBCMD_R_S | UHCI_USBCMD_MAXP, UHCI_USBCMD);
|
2022-02-19 10:24:47 -06:00
|
|
|
return io_wait_until_clr(UHCI_USBSTS, UHCI_USBSTS_HCH, 1000*MILLISEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool halt_host_controller(uint16_t io_base)
|
|
|
|
{
|
2022-04-06 16:22:46 -05:00
|
|
|
outw(inw(UHCI_USBCMD) & ~UHCI_USBCMD_R_S, UHCI_USBCMD);
|
2022-02-19 10:24:47 -06:00
|
|
|
return io_wait_until_set(UHCI_USBSTS, UHCI_USBSTS_HCH, 1000*MILLISEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool reset_uhci_port(uint16_t io_base, int port_idx)
|
|
|
|
{
|
|
|
|
uint16_t port_status = inw(UHCI_PORT_SC(port_idx)) & ~UHCI_PORT_SC_PED;
|
|
|
|
outw(port_status | UHCI_PORT_SC_PR, UHCI_PORT_SC(port_idx));
|
|
|
|
|
|
|
|
usleep(50*MILLISEC); // USB port reset time
|
|
|
|
|
|
|
|
outw(port_status & ~UHCI_PORT_SC_PR, UHCI_PORT_SC(port_idx));
|
|
|
|
return io_wait_until_clr(UHCI_PORT_SC(port_idx), UHCI_PORT_SC_PR, 5*MILLISEC);
|
|
|
|
}
|
|
|
|
|
2022-04-06 16:22:46 -05:00
|
|
|
static bool enable_uhci_port(uint16_t io_base, int port_idx)
|
|
|
|
{
|
|
|
|
uint16_t port_status = inw(UHCI_PORT_SC(port_idx));
|
|
|
|
outw(port_status | UHCI_PORT_SC_PED, UHCI_PORT_SC(port_idx));
|
|
|
|
return io_wait_until_set(UHCI_PORT_SC(port_idx), UHCI_PORT_SC_PED, 1000*MILLISEC);
|
|
|
|
}
|
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
static void disable_uhci_port(uint16_t io_base, int port_idx)
|
|
|
|
{
|
|
|
|
uint16_t port_status = inw(UHCI_PORT_SC(port_idx));
|
|
|
|
outw(port_status & ~UHCI_PORT_SC_PED, UHCI_PORT_SC(port_idx));
|
|
|
|
(void)io_wait_until_clr(UHCI_PORT_SC(port_idx), UHCI_PORT_SC_PED, 1000*MILLISEC);
|
|
|
|
}
|
|
|
|
|
2022-04-11 15:17:09 -05:00
|
|
|
static void build_uhci_td(uhci_td_t *td, const usb_ep_t *ep, uint32_t pid, uint32_t dt, uint32_t options,
|
2022-02-19 10:24:47 -06:00
|
|
|
const void *buffer, size_t length)
|
|
|
|
{
|
2022-04-10 05:53:01 -05:00
|
|
|
uint32_t device_speed = (ep->device_speed == USB_SPEED_LOW) ? UHCI_TD_LOW_SPEED : UHCI_TD_FULL_SPEED;
|
2022-02-19 10:24:47 -06:00
|
|
|
|
2022-04-11 15:17:09 -05:00
|
|
|
if (options & UHCI_TD_IOC_Y) {
|
2022-04-07 11:43:39 -05:00
|
|
|
td->link_ptr = UHCI_LP_TERMINATE;
|
|
|
|
} else {
|
|
|
|
td->link_ptr = (uintptr_t)(td + 1) | UHCI_LP_TYPE_TD | UHCI_LP_DEPTH_FIRST;
|
|
|
|
}
|
2022-02-19 10:24:47 -06:00
|
|
|
td->control_status = UHCI_TD_CERR(3)
|
|
|
|
| device_speed
|
2022-04-11 15:17:09 -05:00
|
|
|
| options
|
2022-02-19 10:24:47 -06:00
|
|
|
| UHCI_TD_ACTIVE;
|
|
|
|
td->token = UHCI_TD_LENGTH(length)
|
|
|
|
| dt
|
|
|
|
| UHCI_TD_ENDPOINT(ep->endpoint_num)
|
|
|
|
| UHCI_TD_DEVICE_ADDR(ep->device_id)
|
|
|
|
| pid;
|
|
|
|
td->buffer_ptr = (uintptr_t)buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t get_uhci_done(workspace_t *ws)
|
|
|
|
{
|
|
|
|
uint16_t io_base = ws->io_base;
|
2022-03-27 15:55:05 -05:00
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
uint16_t status = inw(UHCI_USBSTS) & (UHCI_USBSTS_INT | UHCI_USBSTS_ERR);
|
|
|
|
if (status != 0) {
|
2022-04-11 15:17:09 -05:00
|
|
|
if (status & UHCI_USBSTS_ERR || ws->qh[0].qe_link_ptr != UHCI_LP_TERMINATE) {
|
|
|
|
#if 1
|
|
|
|
uintptr_t td_addr = ws->qh[0].qe_link_ptr & 0xfffffff0;
|
|
|
|
uhci_td_t *td = (uhci_td_t *)td_addr;
|
|
|
|
print_usb_info(" transfer failed TD %08x status %08x token %08x",
|
|
|
|
td_addr, (uintptr_t)td->control_status, (uintptr_t)td->token);
|
|
|
|
#endif
|
|
|
|
write32(&ws->qh[0].qe_link_ptr, UHCI_LP_TERMINATE);
|
|
|
|
status |= UHCI_USBSTS_ERR;
|
|
|
|
}
|
2022-02-19 10:24:47 -06:00
|
|
|
outw(UHCI_USBSTS_INT | UHCI_USBSTS_ERR, UHCI_USBSTS);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool wait_for_uhci_done(workspace_t *ws)
|
|
|
|
{
|
|
|
|
// Rely on the controller to timeout if the device doesn't respond.
|
|
|
|
uint16_t status = 0;
|
|
|
|
while (true) {
|
|
|
|
status = get_uhci_done(ws);
|
|
|
|
if (status != 0) break;
|
|
|
|
usleep(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ~status & UHCI_USBSTS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Driver Methods
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool reset_root_hub_port(const usb_hcd_t *hcd, int port_num)
|
|
|
|
{
|
|
|
|
const workspace_t *ws = (const workspace_t *)hcd->ws;
|
|
|
|
|
|
|
|
return reset_uhci_port(ws->io_base, port_num - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setup_request(const usb_hcd_t *hcd, const usb_ep_t *ep, const usb_setup_pkt_t *setup_pkt)
|
|
|
|
{
|
|
|
|
workspace_t *ws = (workspace_t *)hcd->ws;
|
|
|
|
|
|
|
|
write32(&ws->qh[0].qe_link_ptr, UHCI_LP_TERMINATE);
|
|
|
|
build_uhci_td(&ws->td[0], ep, UHCI_TD_PID_SETUP, UHCI_TD_DT(0), UHCI_TD_IOC_N, setup_pkt, sizeof(usb_setup_pkt_t));
|
|
|
|
build_uhci_td(&ws->td[1], ep, UHCI_TD_PID_IN, UHCI_TD_DT(1), UHCI_TD_IOC_Y, 0, 0);
|
|
|
|
write32(&ws->qh[0].qe_link_ptr, (uintptr_t)(&ws->td[0]) | UHCI_LP_TYPE_TD);
|
|
|
|
return wait_for_uhci_done(ws);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_data_request(const usb_hcd_t *hcd, const usb_ep_t *ep, const usb_setup_pkt_t *setup_pkt,
|
|
|
|
const void *buffer, size_t length)
|
|
|
|
{
|
|
|
|
workspace_t *ws = (workspace_t *)hcd->ws;
|
|
|
|
|
2022-04-11 15:17:09 -05:00
|
|
|
size_t packet_size = ep->max_packet_size;
|
|
|
|
if (length > (MAX_PACKETS * packet_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
write32(&ws->qh[0].qe_link_ptr, UHCI_LP_TERMINATE);
|
2022-04-11 15:17:09 -05:00
|
|
|
int pkt_num = 0;
|
|
|
|
build_uhci_td(&ws->td[pkt_num], ep, UHCI_TD_PID_SETUP, UHCI_TD_DT(pkt_num & 1), UHCI_TD_IOC_N, setup_pkt, sizeof(usb_setup_pkt_t)); pkt_num++;
|
|
|
|
while (length > packet_size) {
|
|
|
|
build_uhci_td(&ws->td[pkt_num], ep, UHCI_TD_PID_IN, UHCI_TD_DT(pkt_num & 1), UHCI_TD_SPD, buffer, packet_size); pkt_num++;
|
|
|
|
buffer = (uint8_t *)buffer + packet_size;
|
|
|
|
length -= packet_size;
|
|
|
|
}
|
|
|
|
build_uhci_td(&ws->td[pkt_num], ep, UHCI_TD_PID_IN, UHCI_TD_DT(pkt_num & 1), UHCI_TD_IOC_N, buffer, length); pkt_num++;
|
|
|
|
build_uhci_td(&ws->td[pkt_num], ep, UHCI_TD_PID_OUT, UHCI_TD_DT(1), UHCI_TD_IOC_Y, 0, 0);
|
2022-02-19 10:24:47 -06:00
|
|
|
write32(&ws->qh[0].qe_link_ptr, (uintptr_t)(&ws->td[0]) | UHCI_LP_TYPE_TD);
|
|
|
|
return wait_for_uhci_done(ws);
|
|
|
|
}
|
|
|
|
|
2022-05-01 10:44:46 -05:00
|
|
|
static void poll_keyboards(const usb_hcd_t *hcd)
|
2022-02-19 10:24:47 -06:00
|
|
|
{
|
|
|
|
workspace_t *ws = (workspace_t *)hcd->ws;
|
|
|
|
uint16_t io_base = ws->io_base;
|
|
|
|
|
|
|
|
uint32_t status = inw(UHCI_USBSTS) & (UHCI_USBSTS_INT | UHCI_USBSTS_ERR);
|
|
|
|
if (status != 0) {
|
|
|
|
outw(UHCI_USBSTS_INT | UHCI_USBSTS_ERR, UHCI_USBSTS);
|
|
|
|
|
|
|
|
for (int kbd_idx = 0; kbd_idx < ws->num_keyboards; kbd_idx++) {
|
2022-04-07 11:51:27 -05:00
|
|
|
uhci_qh_t *kbd_qh = &ws->qh[1 + kbd_idx];
|
2022-04-11 15:17:09 -05:00
|
|
|
uhci_td_t *kbd_td = &ws->td[2 + kbd_idx];
|
2022-02-19 10:24:47 -06:00
|
|
|
|
|
|
|
status = kbd_td->control_status;
|
|
|
|
if (status & UHCI_TD_ACTIVE) continue;
|
|
|
|
|
|
|
|
hid_kbd_rpt_t *kbd_rpt = &ws->kbd_rpt[kbd_idx];
|
|
|
|
|
|
|
|
uint32_t error_mask = UHCI_TD_STALLED | UHCI_TD_DB_ERR | UHCI_TD_BABBLE | UHCI_TD_NAK_RX | UHCI_TD_CRC_TO | UHCI_TD_BS_ERR;
|
|
|
|
if (~status & error_mask) {
|
2022-05-01 10:44:46 -05:00
|
|
|
hid_kbd_rpt_t *prev_kbd_rpt = &ws->prev_kbd_rpt[kbd_idx];
|
2022-05-01 16:40:42 -05:00
|
|
|
if (process_usb_keyboard_report(hcd, kbd_rpt, prev_kbd_rpt)) {
|
|
|
|
*prev_kbd_rpt = *kbd_rpt;
|
|
|
|
}
|
2022-05-01 16:05:40 -05:00
|
|
|
|
|
|
|
write32(&kbd_td->token, read32(&kbd_td->token) ^ UHCI_TD_DT(1));
|
2022-02-19 10:24:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reenable the TD.
|
|
|
|
write32(&kbd_td->control_status, kbd_td->driver_data[0]);
|
2022-04-07 11:51:27 -05:00
|
|
|
write32(&kbd_qh->qe_link_ptr, (uintptr_t)kbd_td | UHCI_LP_TYPE_TD);
|
2022-02-19 10:24:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Driver Method Table
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static const hcd_methods_t methods = {
|
|
|
|
.reset_root_hub_port = reset_root_hub_port,
|
|
|
|
.allocate_slot = NULL,
|
|
|
|
.release_slot = NULL,
|
|
|
|
.assign_address = assign_usb_address, // use default method
|
|
|
|
.configure_hub_ep = NULL,
|
|
|
|
.configure_kbd_ep = NULL,
|
|
|
|
.setup_request = setup_request,
|
|
|
|
.get_data_request = get_data_request,
|
2022-05-01 10:44:46 -05:00
|
|
|
.poll_keyboards = poll_keyboards
|
2022-02-19 10:24:47 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Public Functions
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2022-10-07 03:32:09 -05:00
|
|
|
bool uhci_reset(int bus, int dev, int func, uint16_t io_base)
|
2022-02-19 10:24:47 -06:00
|
|
|
{
|
2022-04-09 16:42:59 -05:00
|
|
|
// Disable PCI and SMM interrupts.
|
|
|
|
pci_config_write16(bus, dev, func, UHCI_LEGSUP, UHCI_LEGSUP_CLEAR);
|
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
// Ensure the controller is halted and then reset it.
|
|
|
|
if (!halt_host_controller(io_base)) return false;
|
|
|
|
if (!reset_host_controller(io_base)) return false;
|
|
|
|
|
2022-10-07 03:32:09 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool uhci_probe(uint16_t io_base, usb_hcd_t *hcd)
|
|
|
|
{
|
2022-07-16 06:34:08 -05:00
|
|
|
// Record the heap state to allow us to free memory.
|
2022-07-14 11:30:37 -05:00
|
|
|
uintptr_t initial_heap_mark = heap_mark(HEAP_TYPE_LM_1);
|
2022-07-16 06:34:08 -05:00
|
|
|
|
|
|
|
// Allocate the frame list. This needs to be aligned on a 4K page boundary.
|
2022-07-14 11:30:37 -05:00
|
|
|
uintptr_t fl_addr = heap_alloc(HEAP_TYPE_LM_1, UHCI_FL_LENGTH * sizeof(uint32_t), PAGE_SIZE);
|
2022-07-16 06:34:08 -05:00
|
|
|
if (fl_addr == 0) {
|
|
|
|
goto no_keyboards_found;
|
|
|
|
}
|
2022-02-19 10:24:47 -06:00
|
|
|
uint32_t *fl = (uint32_t *)fl_addr;
|
|
|
|
|
2022-07-16 06:34:08 -05:00
|
|
|
// Allocate and initialise a workspace for this controller. This needs to be permanently mapped into virtual memory.
|
2022-07-14 11:30:37 -05:00
|
|
|
uintptr_t workspace_addr = heap_alloc(HEAP_TYPE_LM_1, sizeof(workspace_t), PAGE_SIZE);
|
2022-07-16 06:34:08 -05:00
|
|
|
if (workspace_addr == 0) {
|
|
|
|
goto no_keyboards_found;
|
|
|
|
}
|
2022-02-19 10:24:47 -06:00
|
|
|
workspace_t *ws = (workspace_t *)workspace_addr;
|
|
|
|
|
|
|
|
memset(ws, 0, sizeof(workspace_t));
|
|
|
|
|
|
|
|
ws->io_base = io_base;
|
|
|
|
|
|
|
|
// Initialise the asynchronous queue head.
|
|
|
|
ws->qh[0].qh_link_ptr = UHCI_LP_TERMINATE;
|
|
|
|
ws->qh[0].qe_link_ptr = UHCI_LP_TERMINATE;
|
|
|
|
|
|
|
|
// Initialise the frame list to execute the asynchronous queue.
|
|
|
|
for (int i = 0; i < UHCI_FL_LENGTH; i++) {
|
|
|
|
fl[i] = (uintptr_t)(&ws->qh[0]) | UHCI_LP_TYPE_QH;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the driver object for this controller.
|
|
|
|
hcd->methods = &methods;
|
|
|
|
hcd->ws = &ws->base_ws;
|
|
|
|
|
|
|
|
// Initialise the host controller.
|
2022-04-09 16:42:59 -05:00
|
|
|
outw(UHCI_USBINTR_NONE, UHCI_USBINTR);
|
|
|
|
outw(0, UHCI_FRNUM);
|
|
|
|
outl(fl_addr, UHCI_FLBASE);
|
|
|
|
outb(UHCI_SOF_DEFAULT, UHCI_SOF);
|
2022-02-19 10:24:47 -06:00
|
|
|
if (!start_host_controller(io_base)) {
|
2022-07-16 06:34:08 -05:00
|
|
|
goto no_keyboards_found;
|
2022-02-19 10:24:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a hub descriptor for the root hub.
|
|
|
|
usb_hub_t root_hub;
|
2022-09-08 12:09:43 -05:00
|
|
|
memset(&root_hub, 0, sizeof(root_hub));
|
2022-02-19 10:24:47 -06:00
|
|
|
root_hub.ep0 = NULL;
|
|
|
|
root_hub.num_ports = MAX_UHCI_PORTS;
|
|
|
|
|
|
|
|
usleep(100*MILLISEC); // USB maximum device attach time
|
|
|
|
|
|
|
|
// Scan the ports, looking for hubs and keyboards.
|
|
|
|
usb_ep_t keyboards[MAX_KEYBOARDS];
|
|
|
|
int num_keyboards = 0;
|
|
|
|
int num_devices = 0;
|
|
|
|
for (int port_idx = 0; port_idx < root_hub.num_ports; port_idx++) {
|
|
|
|
// If we've filled the keyboard info table, abort now.
|
|
|
|
if (num_keyboards >= MAX_KEYBOARDS) break;
|
|
|
|
|
|
|
|
// Check if we've passed the last port.
|
|
|
|
uint16_t port_status = inw(UHCI_PORT_SC(port_idx));
|
|
|
|
if ((~port_status & UHCI_PORT_SC_VALID) || port_status == 0xffff) {
|
|
|
|
root_hub.num_ports = port_idx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if anything is connected to this port.
|
|
|
|
if (~port_status & UHCI_PORT_SC_CCS) continue;
|
|
|
|
|
|
|
|
// Reset the port.
|
|
|
|
if (!reset_uhci_port(io_base, port_idx)) continue;
|
|
|
|
|
|
|
|
usleep(10*MILLISEC); // USB reset recovery time
|
|
|
|
|
2022-04-06 16:22:46 -05:00
|
|
|
// Enable the port.
|
|
|
|
if (!enable_uhci_port(io_base, port_idx)) continue;
|
|
|
|
|
2022-02-19 10:24:47 -06:00
|
|
|
port_status = inw(UHCI_PORT_SC(port_idx));
|
|
|
|
|
|
|
|
// Check the port is active.
|
|
|
|
if (~port_status & UHCI_PORT_SC_PED) continue;
|
|
|
|
|
|
|
|
num_devices++;
|
|
|
|
|
|
|
|
// Get the port speed.
|
|
|
|
usb_speed_t port_speed = port_status & UHCI_PORT_SC_LSDA ? USB_SPEED_LOW : USB_SPEED_FULL;
|
|
|
|
|
|
|
|
// Look for keyboards attached directly or indirectly to this port.
|
|
|
|
if (find_attached_usb_keyboards(hcd, &root_hub, 1 + port_idx, port_speed, num_devices,
|
|
|
|
&num_devices, keyboards, MAX_KEYBOARDS, &num_keyboards)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find any keyboard interfaces, we can disable the port.
|
|
|
|
disable_uhci_port(io_base, port_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
print_usb_info(" Found %i device%s, %i keyboard%s",
|
|
|
|
num_devices, num_devices != 1 ? "s" : "",
|
|
|
|
num_keyboards, num_keyboards != 1 ? "s" : "");
|
|
|
|
|
|
|
|
if (num_keyboards == 0) {
|
|
|
|
(void)halt_host_controller(io_base);
|
2022-07-16 06:34:08 -05:00
|
|
|
goto no_keyboards_found;
|
2022-02-19 10:24:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ws->num_keyboards = num_keyboards;
|
|
|
|
|
|
|
|
// Initialise the interrupt QH and TD for each keyboard interface and find the minimum interval.
|
|
|
|
int min_interval = UHCI_FL_LENGTH;
|
|
|
|
uint32_t first_qh_ptr = UHCI_LP_TERMINATE;
|
|
|
|
for (int kbd_idx = 0; kbd_idx < num_keyboards; kbd_idx++) {
|
|
|
|
usb_ep_t *kbd = &keyboards[kbd_idx];
|
|
|
|
|
|
|
|
uhci_qh_t *kbd_qh = &ws->qh[1 + kbd_idx];
|
2022-04-11 15:17:09 -05:00
|
|
|
uhci_td_t *kbd_td = &ws->td[2 + kbd_idx];
|
2022-02-19 10:24:47 -06:00
|
|
|
|
|
|
|
hid_kbd_rpt_t *kbd_rpt = &ws->kbd_rpt[kbd_idx];
|
|
|
|
|
|
|
|
build_uhci_td(kbd_td, kbd, UHCI_TD_PID_IN, UHCI_TD_DT(0), UHCI_TD_IOC_Y, kbd_rpt, sizeof(hid_kbd_rpt_t));
|
|
|
|
kbd_td->driver_data[0] = kbd_td->control_status;
|
|
|
|
|
|
|
|
kbd_qh->qe_link_ptr = (uintptr_t)kbd_td | UHCI_LP_TYPE_TD;
|
|
|
|
|
|
|
|
kbd_qh->qh_link_ptr = first_qh_ptr;
|
|
|
|
first_qh_ptr = (uintptr_t)kbd_qh | UHCI_LP_TYPE_QH;
|
|
|
|
|
|
|
|
if (kbd->interval < min_interval) {
|
|
|
|
min_interval = kbd->interval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-initialise the frame list to execute the periodic schedule.
|
|
|
|
for (int i = 0; i < UHCI_FL_LENGTH; i++) {
|
|
|
|
write32(&fl[i], UHCI_LP_TERMINATE);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < UHCI_FL_LENGTH; i += min_interval) {
|
|
|
|
write32(&fl[i], first_qh_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-07-16 06:34:08 -05:00
|
|
|
|
|
|
|
no_keyboards_found:
|
2022-07-14 11:30:37 -05:00
|
|
|
heap_rewind(HEAP_TYPE_LM_1, initial_heap_mark);
|
2022-07-16 06:34:08 -05:00
|
|
|
return false;
|
2022-02-19 10:24:47 -06:00
|
|
|
}
|