Johannes Zellner 7 месяцев назад
Родитель
Сommit
16185f71ca
3 измененных файлов с 226 добавлено и 3 удалено
  1. 8 2
      Makefile
  2. 177 0
      mousecmd.c
  3. 41 1
      terminal.c

+ 8 - 2
Makefile

@@ -41,6 +41,9 @@ hello.o: hello.c io.h
 reboot.o: reboot.c
 	$(CC) $(CFLAGS) -o $@ $<
 
+poweroff.o: poweroff.c
+	$(CC) $(CFLAGS) -o $@ $<
+
 disk.o: disk.c disk.h io.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -86,6 +89,9 @@ display.o: display.c io.h vga.h gpu.h ati.h fbtext.h
 cpuinfo.o: cpuinfo.c io.h timer.h idle.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+mousecmd.o: mousecmd.c io.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 idle.o: idle.c idle.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -104,7 +110,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o idle.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mousecmd.o idle.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -185,4 +191,4 @@ run-usb: usb.img root.img
 	qemu-system-x86_64 -M pc -boot c -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o idle.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mousecmd.o idle.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 177 - 0
mousecmd.c

@@ -0,0 +1,177 @@
+/* mouse command: poll PS/2 aux (mouse) for 10s, print packets (relative dx/dy + accumulators). */
+
+#include "io.h"
+#include "timer.h"
+
+#define PS2_DATA   0x60
+#define PS2_STATUS 0x64
+#define PS2_CMD    0x64
+
+#define STATUS_OUT_FULL 0x01
+#define STATUS_IN_FULL  0x02
+#define STATUS_AUX_DATA 0x20
+
+#define MOUSE_ACK     0xFA
+#define MOUSE_RESEND  0xFE
+
+static inline unsigned char inb(unsigned short port)
+{
+	unsigned char v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static inline void outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static void ps2_wait_input_empty(void)
+{
+	unsigned spin = 100000;
+	while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
+		;
+}
+
+static void mouse_cmd_write(unsigned char val)
+{
+	ps2_wait_input_empty();
+	outb(PS2_CMD, 0xD4);
+	ps2_wait_input_empty();
+	outb(PS2_DATA, val);
+}
+
+/* Read next byte from aux port (trackpoint/touchpad), with timeout in timer ticks (~ms). */
+static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
+{
+	unsigned int t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < timeout_ticks) {
+		unsigned char st = inb(PS2_STATUS);
+		if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
+		    (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
+			*out = inb(PS2_DATA);
+			return 0;
+		}
+	}
+	return -1;
+}
+
+static int mouse_expect_ack(void)
+{
+	unsigned char b;
+	if (mouse_read_aux(&b, 500) != 0)
+		return -1;
+	if (b == MOUSE_RESEND) {
+		/* one retry path left to caller */
+		return -2;
+	}
+	if (b != MOUSE_ACK)
+		return -1;
+	return 0;
+}
+
+static void enable_aux_port(void)
+{
+	ps2_wait_input_empty();
+	outb(PS2_CMD, 0xA8);
+}
+
+static int mouse_enable_streaming(void)
+{
+	int r;
+	enable_aux_port();
+	mouse_cmd_write(0xF4);
+	r = mouse_expect_ack();
+	if (r == -2) {
+		mouse_cmd_write(0xF4);
+		r = mouse_expect_ack();
+	}
+	return r;
+}
+
+static void mouse_disable_streaming(void)
+{
+	mouse_cmd_write(0xF5);
+	(void)mouse_expect_ack();
+}
+
+static void put_uint(unsigned int n)
+{
+	char buf[12];
+	int i = 10;
+	buf[11] = '\0';
+	if (n == 0) {
+		io_putchar('0');
+		return;
+	}
+	while (n && i >= 0) {
+		buf[i--] = (char)('0' + (n % 10u));
+		n /= 10u;
+	}
+	io_putstr(buf + i + 1);
+}
+
+static void put_sdec(int v)
+{
+	if (v < 0) {
+		io_putchar('-');
+		put_uint(0u - (unsigned int)v);
+	} else
+		put_uint((unsigned int)v);
+}
+
+void program_mouse_main(void)
+{
+	enum { RUN_MS = 10000 };
+	unsigned int t_start;
+	int acc_x = 0, acc_y = 0;
+	unsigned char pkt[3];
+	unsigned int pkt_idx = 0;
+
+	io_putstr("PS/2 mouse: 10s (aux poll, 3-byte packets)\n");
+
+	if (mouse_enable_streaming() != 0) {
+		io_putstr("mouse: enable failed (no ACK?)\n");
+		return;
+	}
+
+	t_start = get_ticks_ms();
+	while (get_ticks_ms() - t_start < RUN_MS) {
+		unsigned char st = inb(PS2_STATUS);
+		if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
+		    (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
+			unsigned char b = inb(PS2_DATA);
+			pkt[pkt_idx++] = b;
+			if (pkt_idx == 3) {
+				unsigned char b0 = pkt[0];
+				int dx = (int)(signed char)pkt[1];
+				int dy = (int)(signed char)pkt[2];
+				if (b0 & 0x10)
+					dx |= ~0xFF;
+				if (b0 & 0x20)
+					dy |= ~0xFF;
+				/* overflow bits in b0: do not add to accumulators */
+				if (!(b0 & 0xC0)) {
+					acc_x += dx;
+					acc_y += dy;
+				}
+				put_uint(get_ticks_ms() - t_start);
+				io_putchar(' ');
+				put_uint(b0 & 7);
+				io_putchar(' ');
+				put_sdec(dx);
+				io_putchar(' ');
+				put_sdec(dy);
+				io_putchar(' ');
+				put_sdec(acc_x);
+				io_putchar(' ');
+				put_sdec(acc_y);
+				io_putchar('\n');
+				pkt_idx = 0;
+			}
+		}
+	}
+
+	mouse_disable_streaming();
+	io_putstr("PS/2 mouse: done\n");
+}

+ 41 - 1
terminal.c

@@ -11,6 +11,7 @@
 
 extern void program_hello_main(void);
 extern void program_reboot_main(void);
+extern void program_poweroff_main(void);
 extern void program_ls_main(void);
 extern void program_cat_main(const char *filename);
 extern void program_run_main(const char *filename);
@@ -18,6 +19,7 @@ extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
 extern void program_cpuinfo_main(void);
+extern void program_mouse_main(void);
 extern void program_power_main(const char *args);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
@@ -114,6 +116,22 @@ static int is_cmd_reboot(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_poweroff(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 8 > line_len)
+		return 0;
+	if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' ||
+	    line[i+4] != 'r' || line[i+5] != 'o' || line[i+6] != 'f' || line[i+7] != 'f')
+		return 0;
+	i += 8;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_ls(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -193,6 +211,22 @@ static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_mouse(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
+	    line[i+3] != 's' || line[i+4] != 'e')
+		return 0;
+	i += 5;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_space(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -533,6 +567,8 @@ void terminal_run(void)
 							io_putstr("error: too many processes\n");
 					} else if (is_cmd_reboot(line, line_len)) {
 						program_reboot_main();
+					} else if (is_cmd_poweroff(line, line_len)) {
+						program_poweroff_main();
 					} else if (is_cmd_ls(line, line_len)) {
 						if (process_create("ls", program_ls_main) < 0)
 							io_putstr("error: too many processes\n");
@@ -557,6 +593,9 @@ void terminal_run(void)
 					} else if (is_cmd_cpuinfo(line, line_len)) {
 						if (process_create("cpuinfo", program_cpuinfo_main) < 0)
 							io_putstr("error: too many processes\n");
+					} else if (is_cmd_mouse(line, line_len)) {
+						if (process_create("mouse", program_mouse_main) < 0)
+							io_putstr("error: too many processes\n");
 					} else if (is_cmd_power(line, line_len, power_args, POWER_ARGS_MAX)) {
 						if (process_create_arg("power", program_power_main, power_args) < 0)
 							io_putstr("error: too many processes\n");
@@ -573,11 +612,12 @@ void terminal_run(void)
 						else
 							io_putstr("killed\n");
 					} else if (is_cmd_help(line, line_len)) {
-						io_putstr("hello     reboot\n");
+						io_putstr("hello     reboot    poweroff\n");
 						io_putstr("ls        cat <file>\n");
 						io_putstr("run <file>  space\n");
 						io_putstr("clock     colors\n");
 						io_putstr("display   cpuinfo\n");
+						io_putstr("mouse\n");
 						io_putstr("power [info|set <low|balanced|max>]\n");
 						io_putstr("drive <0|1>\n");
 						io_putstr("ps        kill <pid>\n");