Wire real PS/2 mouse packets through to input events
Replace the mouse driver's synthetic stream with the real path, the way the keyboard was wired: - The auxiliary port's IRQ12 is enumerated on the mouse's own ACPI node (PNP0F13), and the kernel only lets a device's claimer bind or ack its IRQs — so ps2-bus now claims that node alongside the controller whenever port 2 carries a device, binds IRQ12 to its one endpoint, and re-arms whichever line the notification's badge names. The forwarding loop already routed auxiliary bytes by status bit 5. - mouse-packet.zig (new, pure, host-tested): three-byte stream-mode packet assembly — bit-3 sync with resynchronization, ACK/BAT bytes dropped at packet start, nine-bit two's-complement movement, overflow packets discarded, and PS/2 positive-Y-up converted to the screen convention (positive down). - mouse.zig mirrors the keyboard driver: no hardware claim, attaches to the bus as its mouse, and publishes button_down/button_up per changed button plus motion events with the pressed-button mask. Verified end to end in QEMU via monitor mouse_move/mouse_button: motion round-trips in screen coordinates, buttons transition with the right mask, and keyboard events keep flowing alongside. The follow-up is the IntelliMouse magic-knock for a scroll wheel (four-byte packets) and scroll events.
This commit is contained in:
@@ -1,19 +1,52 @@
|
||||
//! PS/2 Mouse Driver
|
||||
//!
|
||||
//! Spawned by the ps2-bus driver once the controller is initialized and port 2
|
||||
//! Spawned by the ps2-bus driver once the controller is initialized and the port
|
||||
//! has passed its interface test and device reset. The bus driver hands us our
|
||||
//! device HID as argv[1]; we use it to locate our own device descriptor.
|
||||
//! device HID as argv[1].
|
||||
//!
|
||||
//! Like the keyboard, this driver never touches the hardware: the 8042's ports
|
||||
//! and both port IRQs are owned by the ps2-bus driver (the auxiliary port's
|
||||
//! IRQ12 lives on the PNP0F13 node, which the bus claims alongside the
|
||||
//! controller). The driver **attaches** to the bus and receives every byte the
|
||||
//! mouse sends as a forwarded asynchronous message. The bytes assemble into
|
||||
//! three-byte packets, and each packet becomes input-protocol events:
|
||||
//!
|
||||
//! packet -> button transitions -> button_down / button_up
|
||||
//! -> movement -> motion (dx/dy, screen convention)
|
||||
|
||||
const std = @import("std");
|
||||
const runtime = @import("runtime");
|
||||
const ps2 = @import("ps2-library.zig");
|
||||
const mouse_packet = @import("mouse-packet.zig");
|
||||
const device = runtime.device;
|
||||
const ipc = runtime.ipc;
|
||||
const protocol = runtime.input_protocol;
|
||||
|
||||
fn writeLine(comptime fmt: []const u8, arguments: anytype) void {
|
||||
var line: [128]u8 = undefined;
|
||||
_ = runtime.system.write(std.fmt.bufPrint(&line, fmt, arguments) catch return);
|
||||
}
|
||||
|
||||
/// Look up the ps2-bus service, retrying while the bus (which spawned us before
|
||||
/// registering) is still coming up.
|
||||
fn lookupBus() ?ipc.Handle {
|
||||
var attempts: usize = 0;
|
||||
while (attempts < 100) : (attempts += 1) {
|
||||
if (ipc.lookup(.ps2_bus)) |handle| return handle;
|
||||
runtime.system.sleep(50);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The protocol's pressed-button bitmask for a packet.
|
||||
fn buttonMask(packet: mouse_packet.Packet) u32 {
|
||||
var mask: u32 = 0;
|
||||
if (packet.left) mask |= protocol.mouse_button_left;
|
||||
if (packet.right) mask |= protocol.mouse_button_right;
|
||||
if (packet.middle) mask |= protocol.mouse_button_middle;
|
||||
return mask;
|
||||
}
|
||||
|
||||
pub fn main(init: runtime.process.Init) void {
|
||||
const hid = init.arguments.get(1).?;
|
||||
|
||||
@@ -27,34 +60,81 @@ pub fn main(init: runtime.process.Init) void {
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: out of memory\n");
|
||||
return;
|
||||
};
|
||||
const mouse_device_descriptor = device.findDeviceDescriptorByHid(buffer, hid) orelse {
|
||||
if (device.findDeviceDescriptorByHid(buffer, hid) == null) {
|
||||
writeLine("system/drivers/ps2-bus/mouse: no device for hid {s}\n", .{hid});
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
// The mouse's own node (PNP0F13) carries IRQ12 and is not claimed by the bus,
|
||||
// so this driver takes exclusive ownership of it. Port IO still goes through
|
||||
// the bus, which owns the shared 8042 ports.
|
||||
if (!device.claim(mouse_device_descriptor.id)) {
|
||||
writeLine("system/drivers/ps2-bus/mouse: unable to claim device for hid {s}\n", .{hid});
|
||||
// Attach to the bus: hand it our endpoint, and it forwards every byte the
|
||||
// mouse sends (it owns the controller; we own the decoding).
|
||||
const bus = lookupBus() orelse {
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: ps2-bus service unavailable\n");
|
||||
return;
|
||||
};
|
||||
const endpoint = ipc.createIpcEndpoint() orelse {
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: no endpoint\n");
|
||||
return;
|
||||
};
|
||||
var attach = ps2.AttachRequest{ .device_type = @intFromEnum(ps2.DeviceType.mouse) };
|
||||
var attach_reply: [@sizeOf(ps2.AttachReply)]u8 = undefined;
|
||||
const attached = ipc.callCap(bus, std.mem.asBytes(&attach), &attach_reply, endpoint) catch {
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: attach call failed\n");
|
||||
return;
|
||||
};
|
||||
if (attached.len < @sizeOf(ps2.AttachReply) or
|
||||
std.mem.bytesToValue(ps2.AttachReply, attach_reply[0..@sizeOf(ps2.AttachReply)]).status != 0)
|
||||
{
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: attach refused\n");
|
||||
return;
|
||||
}
|
||||
writeLine("system/drivers/ps2-bus/mouse: claimed device for hid {s}\n", .{hid});
|
||||
|
||||
// Broadcast mouse events through the input service so programs can listen for them
|
||||
// (docs/input.md). As with the keyboard, decoding real PS/2 mouse packets is a
|
||||
// follow-up; for now we publish the synthetic stand-in stream. The fan-out path is
|
||||
// real, only the source of the movement is placeholder.
|
||||
// Broadcast mouse events through the input service so programs can listen
|
||||
// for them (docs/input.md).
|
||||
var source = runtime.input.connectSource() orelse {
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: input service unavailable\n");
|
||||
return;
|
||||
};
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: ok\n");
|
||||
|
||||
var step: usize = 0;
|
||||
while (true) : (step +%= 1) {
|
||||
_ = source.publishMouseEvent(runtime.input.syntheticMouseEvent(step));
|
||||
runtime.system.sleep(200);
|
||||
var assembler = mouse_packet.Assembler{};
|
||||
var buttons: u32 = 0;
|
||||
var receive: [@sizeOf(ps2.ForwardedByte)]u8 = undefined;
|
||||
while (true) {
|
||||
const got = ipc.replyWait(endpoint, &.{}, &receive, null);
|
||||
if (!got.isMessage() or got.len < @sizeOf(ps2.ForwardedByte)) continue;
|
||||
const forwarded = std.mem.bytesToValue(ps2.ForwardedByte, receive[0..@sizeOf(ps2.ForwardedByte)]);
|
||||
|
||||
const packet = assembler.feed(@intCast(forwarded.byte & 0xFF)) orelse continue;
|
||||
const new_buttons = buttonMask(packet);
|
||||
|
||||
// A button transition per changed button, carrying the new whole mask.
|
||||
const changed = buttons ^ new_buttons;
|
||||
for ([_]u32{ protocol.mouse_button_left, protocol.mouse_button_right, protocol.mouse_button_middle }) |button| {
|
||||
if (changed & button == 0) continue;
|
||||
const kind: protocol.MouseEventKind = if (new_buttons & button != 0) .button_down else .button_up;
|
||||
_ = source.publishMouseEvent(.{
|
||||
.kind = @intFromEnum(kind),
|
||||
.button = button,
|
||||
.dx = 0,
|
||||
.dy = 0,
|
||||
.scroll_x = 0,
|
||||
.scroll_y = 0,
|
||||
.buttons = new_buttons,
|
||||
});
|
||||
}
|
||||
buttons = new_buttons;
|
||||
|
||||
if (packet.dx != 0 or packet.dy != 0) {
|
||||
_ = source.publishMouseEvent(.{
|
||||
.kind = @intFromEnum(protocol.MouseEventKind.motion),
|
||||
.button = 0,
|
||||
.dx = packet.dx,
|
||||
.dy = packet.dy,
|
||||
.scroll_x = 0,
|
||||
.scroll_y = 0,
|
||||
.buttons = new_buttons,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user