Initial import of ViperOS
This commit is contained in:
37
VKernel/Kernel/drivers/usb/hid/hid.py
Normal file
37
VKernel/Kernel/drivers/usb/hid/hid.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import t, c
|
||||
import drivers.usb.usb as usb
|
||||
|
||||
HID_BOOT_PROTOCOL: t.CDefine = 0
|
||||
HID_REPORT_PROTOCOL: t.CDefine = 1
|
||||
|
||||
HID_KBD_REPORT_SIZE: t.CDefine = 8
|
||||
HID_MOUSE_REPORT_SIZE: t.CDefine = 4
|
||||
|
||||
class hid_kbd_report(t.CStruct):
|
||||
modifier: t.CUInt8T
|
||||
reserved: t.CUInt8T
|
||||
keycode: t.CArray[t.CUInt8T, 6]
|
||||
|
||||
class hid_mouse_report(t.CStruct):
|
||||
buttons: t.CUInt8T
|
||||
dx: t.CInt8T
|
||||
dy: t.CInt8T
|
||||
wheel: t.CInt8T
|
||||
|
||||
def init_keyboard(dev: usb.usb_device | t.CPtr) -> t.CInt:
|
||||
if dev.iface_class != 0x03: return -1
|
||||
if dev.iface_protocol != 0x01: return -2
|
||||
if dev.ep_in == 0: return -3
|
||||
return 0
|
||||
|
||||
def init_mouse(dev: usb.usb_device | t.CPtr) -> t.CInt:
|
||||
if dev.iface_class != 0x03: return -1
|
||||
if dev.iface_protocol != 0x02: return -2
|
||||
if dev.ep_in == 0: return -3
|
||||
return 0
|
||||
|
||||
def read_keyboard(dev: usb.usb_device | t.CPtr, report: hid_kbd_report | t.CPtr) -> t.CInt:
|
||||
return usb.read_interrupt(dev, c.Addr(report))
|
||||
|
||||
def read_mouse(dev: usb.usb_device | t.CPtr, report: hid_mouse_report | t.CPtr) -> t.CInt:
|
||||
return usb.read_interrupt(dev, c.Addr(report))
|
||||
111
VKernel/Kernel/drivers/usb/hid/usb_kbd.py
Normal file
111
VKernel/Kernel/drivers/usb/hid/usb_kbd.py
Normal file
@@ -0,0 +1,111 @@
|
||||
import t, c
|
||||
import drivers.usb.usb as usb
|
||||
import drivers.usb.hid.hid as hid
|
||||
import drivers.usb.uhci as uhci
|
||||
import drivers.serial.uart.serial as serial
|
||||
|
||||
KBD_BUF_SIZE: t.CDefine = 1024
|
||||
KBD_EVT_PRESS: t.CDefine = 0
|
||||
KBD_EVT_RELEASE: t.CDefine = 1
|
||||
|
||||
class usb_kbd_event(t.CStruct):
|
||||
modifier: t.CUInt8T
|
||||
keycode: t.CUInt8T
|
||||
event_type: t.CUInt8T
|
||||
|
||||
kbd_buf: t.CArray[usb_kbd_event, KBD_BUF_SIZE]
|
||||
kbd_buf_head: t.CInt = 0
|
||||
kbd_buf_tail: t.CInt = 0
|
||||
kbd_buf_count: t.CInt = 0
|
||||
kbd_dev: usb.usb_device | t.CPtr = None
|
||||
kbd_report: hid.hid_kbd_report
|
||||
kbd_prev_keycodes: t.CArray[t.CUInt8T, 6]
|
||||
kbd_initialized: t.CInt = 0
|
||||
kbd_slot_id: t.CInt = -1
|
||||
|
||||
def init() -> t.CInt:
|
||||
global kbd_dev, kbd_initialized, kbd_slot_id
|
||||
dev: usb.usb_device | t.CPtr = usb.find_hid_device(0x01)
|
||||
if dev is None:
|
||||
serial.puts("[usb_kbd] no USB keyboard found\n")
|
||||
return -1
|
||||
kbd_dev = dev
|
||||
if hid.init_keyboard(dev) != 0:
|
||||
serial.puts("[usb_kbd] init failed\n")
|
||||
return -2
|
||||
kbd_initialized = 1
|
||||
serial.puts("[usb_kbd] initialized\n")
|
||||
kbd_slot_id = uhci.register_int_callback(c.Addr(_on_int_complete))
|
||||
if kbd_slot_id < 0:
|
||||
serial.puts("[usb_kbd] register callback failed\n")
|
||||
kbd_initialized = 0
|
||||
return -3
|
||||
uhci.schedule_int_transfer(kbd_slot_id, dev.addr, dev.ep_in, dev.ls, t.CUInt8T(dev.ep_in_max), dev.ep_in_toggle, c.Addr(kbd_report))
|
||||
return 0
|
||||
|
||||
def _on_int_complete() -> t.CInt:
|
||||
global kbd_buf_head, kbd_buf_tail, kbd_buf_count, kbd_prev_keycodes
|
||||
if kbd_initialized == 0: return 0
|
||||
i: t.CInt
|
||||
j: t.CInt
|
||||
for i in range(6):
|
||||
code: t.CUInt8T = kbd_report.keycode[i]
|
||||
if code == 0: continue
|
||||
found: t.CInt = 0
|
||||
for j in range(6):
|
||||
if code == kbd_prev_keycodes[j]:
|
||||
found = 1
|
||||
break
|
||||
if found == 0:
|
||||
if kbd_buf_count < KBD_BUF_SIZE:
|
||||
kbd_buf[kbd_buf_tail].modifier = kbd_report.modifier
|
||||
kbd_buf[kbd_buf_tail].keycode = code
|
||||
kbd_buf[kbd_buf_tail].event_type = KBD_EVT_PRESS
|
||||
kbd_buf_tail = kbd_buf_tail + 1
|
||||
if kbd_buf_tail >= KBD_BUF_SIZE:
|
||||
kbd_buf_tail = 0
|
||||
kbd_buf_count += 1
|
||||
for i in range(6):
|
||||
prev: t.CUInt8T = kbd_prev_keycodes[i]
|
||||
if prev == 0: continue
|
||||
found2: t.CInt = 0
|
||||
for j in range(6):
|
||||
if prev == kbd_report.keycode[j]:
|
||||
found2 = 1
|
||||
break
|
||||
if found2 == 0:
|
||||
if kbd_buf_count < KBD_BUF_SIZE:
|
||||
kbd_buf[kbd_buf_tail].modifier = kbd_report.modifier
|
||||
kbd_buf[kbd_buf_tail].keycode = prev
|
||||
kbd_buf[kbd_buf_tail].event_type = KBD_EVT_RELEASE
|
||||
kbd_buf_tail = kbd_buf_tail + 1
|
||||
if kbd_buf_tail >= KBD_BUF_SIZE:
|
||||
kbd_buf_tail = 0
|
||||
kbd_buf_count += 1
|
||||
for i in range(6):
|
||||
kbd_prev_keycodes[i] = kbd_report.keycode[i]
|
||||
dev: usb.usb_device | t.CPtr = kbd_dev
|
||||
if dev is not None and kbd_slot_id >= 0:
|
||||
uhci.schedule_int_transfer(kbd_slot_id, dev.addr, dev.ep_in, dev.ls, t.CUInt8T(dev.ep_in_max), dev.ep_in_toggle, c.Addr(kbd_report))
|
||||
return 0
|
||||
|
||||
def has_data() -> t.CInt:
|
||||
if kbd_buf_count > 0: return 1
|
||||
return 0
|
||||
|
||||
def poll() -> t.CInt:
|
||||
if kbd_initialized == 0: return 0
|
||||
if kbd_buf_count > 0: return 1
|
||||
return 0
|
||||
|
||||
def read_event(out: usb_kbd_event | t.CPtr) -> t.CInt:
|
||||
global kbd_buf_head, kbd_buf_count
|
||||
if kbd_buf_count == 0: return -1
|
||||
out.modifier = kbd_buf[kbd_buf_head].modifier
|
||||
out.keycode = kbd_buf[kbd_buf_head].keycode
|
||||
out.event_type = kbd_buf[kbd_buf_head].event_type
|
||||
kbd_buf_head = kbd_buf_head + 1
|
||||
if kbd_buf_head >= KBD_BUF_SIZE:
|
||||
kbd_buf_head = 0
|
||||
kbd_buf_count -= 1
|
||||
return 0
|
||||
77
VKernel/Kernel/drivers/usb/hid/usb_mouse.py
Normal file
77
VKernel/Kernel/drivers/usb/hid/usb_mouse.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import t, c
|
||||
import drivers.usb.usb as usb
|
||||
import drivers.usb.hid.hid as hid
|
||||
import drivers.usb.uhci as uhci
|
||||
import drivers.serial.uart.serial as serial
|
||||
|
||||
MOUSE_BUF_SIZE: t.CDefine = 1024
|
||||
|
||||
class usb_mouse_packet(t.CStruct):
|
||||
buttons: t.CUInt8T
|
||||
dx: t.CInt16T
|
||||
dy: t.CInt16T
|
||||
wheel: t.CInt8T
|
||||
|
||||
mse_buf: t.CArray[usb_mouse_packet, MOUSE_BUF_SIZE]
|
||||
mse_buf_head: t.CInt = 0
|
||||
mse_buf_tail: t.CInt = 0
|
||||
mse_buf_count: t.CInt = 0
|
||||
mse_dev: usb.usb_device | t.CPtr = None
|
||||
mse_report: hid.hid_mouse_report
|
||||
mse_initialized: t.CInt = 0
|
||||
mse_slot_id: t.CInt = -1
|
||||
|
||||
def init() -> t.CInt:
|
||||
global mse_dev, mse_initialized, mse_slot_id
|
||||
dev: usb.usb_device | t.CPtr = usb.find_hid_device(0x02)
|
||||
if dev is None:
|
||||
serial.puts("[usb_mse] no USB mouse found\n")
|
||||
return -1
|
||||
mse_dev = dev
|
||||
if hid.init_mouse(dev) != 0:
|
||||
serial.puts("[usb_mse] init failed\n")
|
||||
return -2
|
||||
mse_initialized = 1
|
||||
serial.puts("[usb_mse] initialized\n")
|
||||
mse_slot_id = uhci.register_int_callback(c.Addr(_on_int_complete))
|
||||
if mse_slot_id < 0:
|
||||
serial.puts("[usb_mse] register callback failed\n")
|
||||
mse_initialized = 0
|
||||
return -3
|
||||
uhci.schedule_int_transfer(mse_slot_id, dev.addr, dev.ep_in, dev.ls, t.CUInt8T(dev.ep_in_max), dev.ep_in_toggle, c.Addr(mse_report))
|
||||
return 0
|
||||
|
||||
def _on_int_complete() -> t.CInt:
|
||||
global mse_buf_head, mse_buf_tail, mse_buf_count
|
||||
if mse_initialized == 0: return 0
|
||||
if mse_report.dx != 0 or mse_report.dy != 0 or mse_report.buttons != 0:
|
||||
if mse_buf_count < MOUSE_BUF_SIZE:
|
||||
mse_buf[mse_buf_tail].buttons = mse_report.buttons
|
||||
mse_buf[mse_buf_tail].dx = t.CInt16T(mse_report.dx)
|
||||
mse_buf[mse_buf_tail].dy = t.CInt16T(mse_report.dy)
|
||||
mse_buf[mse_buf_tail].wheel = mse_report.wheel
|
||||
mse_buf_tail = mse_buf_tail + 1
|
||||
if mse_buf_tail >= MOUSE_BUF_SIZE:
|
||||
mse_buf_tail = 0
|
||||
mse_buf_count += 1
|
||||
dev: usb.usb_device | t.CPtr = mse_dev
|
||||
if dev is not None and mse_slot_id >= 0:
|
||||
uhci.schedule_int_transfer(mse_slot_id, dev.addr, dev.ep_in, dev.ls, t.CUInt8T(dev.ep_in_max), dev.ep_in_toggle, c.Addr(mse_report))
|
||||
return 0
|
||||
|
||||
def has_data() -> t.CInt:
|
||||
if mse_buf_count > 0: return 1
|
||||
return 0
|
||||
|
||||
def read_packet(out: usb_mouse_packet | t.CPtr) -> t.CInt:
|
||||
global mse_buf_head, mse_buf_count
|
||||
if mse_buf_count == 0: return -1
|
||||
out.buttons = mse_buf[mse_buf_head].buttons
|
||||
out.dx = mse_buf[mse_buf_head].dx
|
||||
out.dy = mse_buf[mse_buf_head].dy
|
||||
out.wheel = mse_buf[mse_buf_head].wheel
|
||||
mse_buf_head = mse_buf_head + 1
|
||||
if mse_buf_head >= MOUSE_BUF_SIZE:
|
||||
mse_buf_head = 0
|
||||
mse_buf_count -= 1
|
||||
return 0
|
||||
Reference in New Issue
Block a user