2014-06-08 10:51:01 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
2014-06-08 10:58:31 +04:00
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2014-06-08 10:13:29 +04:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2014 Kevin Lange
|
2014-06-08 10:45:54 +04:00
|
|
|
*
|
2011-12-11 03:34:10 +04:00
|
|
|
* Mouse driver
|
2014-06-08 10:45:54 +04:00
|
|
|
*
|
2011-12-11 03:34:10 +04:00
|
|
|
*/
|
2011-10-29 09:33:45 +04:00
|
|
|
#include <system.h>
|
2011-12-15 05:43:14 +04:00
|
|
|
#include <logging.h>
|
2012-02-03 02:16:29 +04:00
|
|
|
#include <pipe.h>
|
2014-03-17 02:13:27 +04:00
|
|
|
#include <module.h>
|
2012-02-03 02:16:29 +04:00
|
|
|
#include <mouse.h>
|
2015-06-02 06:55:19 +03:00
|
|
|
#include <args.h>
|
2011-10-29 09:33:45 +04:00
|
|
|
|
2014-03-17 02:13:27 +04:00
|
|
|
static uint8_t mouse_cycle = 0;
|
2015-04-27 04:05:14 +03:00
|
|
|
static int8_t mouse_byte[4];
|
2011-10-29 09:33:45 +04:00
|
|
|
|
2012-12-08 12:24:43 +04:00
|
|
|
#define PACKETS_IN_PIPE 1024
|
2012-02-03 06:37:50 +04:00
|
|
|
#define DISCARD_POINT 32
|
|
|
|
|
2012-02-05 08:29:46 +04:00
|
|
|
#define MOUSE_IRQ 12
|
2011-10-29 09:33:45 +04:00
|
|
|
|
2012-02-05 08:29:46 +04:00
|
|
|
#define MOUSE_PORT 0x60
|
|
|
|
#define MOUSE_STATUS 0x64
|
|
|
|
#define MOUSE_ABIT 0x02
|
|
|
|
#define MOUSE_BBIT 0x01
|
|
|
|
#define MOUSE_WRITE 0xD4
|
|
|
|
#define MOUSE_F_BIT 0x20
|
2012-12-01 07:32:38 +04:00
|
|
|
#define MOUSE_V_BIT 0x08
|
2012-02-03 06:37:50 +04:00
|
|
|
|
2015-04-27 04:05:14 +03:00
|
|
|
#define MOUSE_DEFAULT 0
|
|
|
|
#define MOUSE_SCROLLWHEEL 1
|
|
|
|
#define MOUSE_BUTTONS 2
|
|
|
|
|
|
|
|
static int8_t mouse_mode = MOUSE_DEFAULT;
|
|
|
|
|
2014-03-17 02:13:27 +04:00
|
|
|
static fs_node_t * mouse_pipe;
|
2011-10-29 09:33:45 +04:00
|
|
|
|
2014-03-17 02:13:27 +04:00
|
|
|
static void mouse_wait(uint8_t a_type) {
|
2011-10-29 09:33:45 +04:00
|
|
|
uint32_t timeout = 100000;
|
|
|
|
if (!a_type) {
|
|
|
|
while (--timeout) {
|
2012-02-05 08:29:46 +04:00
|
|
|
if ((inportb(MOUSE_STATUS) & MOUSE_BBIT) == 1) {
|
2011-10-29 09:33:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-12-08 06:33:07 +04:00
|
|
|
debug_print(INFO, "mouse timeout");
|
2011-10-29 09:33:45 +04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
while (--timeout) {
|
2012-02-05 08:29:46 +04:00
|
|
|
if (!((inportb(MOUSE_STATUS) & MOUSE_ABIT))) {
|
2011-10-29 09:33:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-12-08 06:33:07 +04:00
|
|
|
debug_print(INFO, "mouse timeout");
|
2011-10-29 09:33:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 02:13:27 +04:00
|
|
|
static void mouse_write(uint8_t write) {
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_wait(1);
|
2012-02-05 08:29:46 +04:00
|
|
|
outportb(MOUSE_STATUS, MOUSE_WRITE);
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_wait(1);
|
2012-02-05 08:29:46 +04:00
|
|
|
outportb(MOUSE_PORT, write);
|
2011-10-29 09:33:45 +04:00
|
|
|
}
|
|
|
|
|
2014-03-17 02:13:27 +04:00
|
|
|
static uint8_t mouse_read(void) {
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_wait(0);
|
2012-02-05 08:29:46 +04:00
|
|
|
char t = inportb(MOUSE_PORT);
|
2011-10-31 10:30:48 +04:00
|
|
|
return t;
|
2011-10-29 09:33:45 +04:00
|
|
|
}
|
|
|
|
|
2015-05-19 07:07:06 +03:00
|
|
|
static int mouse_handler(struct regs *r) {
|
2012-02-05 08:29:46 +04:00
|
|
|
uint8_t status = inportb(MOUSE_STATUS);
|
|
|
|
while (status & MOUSE_BBIT) {
|
|
|
|
int8_t mouse_in = inportb(MOUSE_PORT);
|
|
|
|
if (status & MOUSE_F_BIT) {
|
|
|
|
switch (mouse_cycle) {
|
|
|
|
case 0:
|
|
|
|
mouse_byte[0] = mouse_in;
|
2015-06-02 06:55:19 +03:00
|
|
|
if (!(mouse_in & MOUSE_V_BIT)) { goto read_next; }
|
2012-02-05 08:29:46 +04:00
|
|
|
++mouse_cycle;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mouse_byte[1] = mouse_in;
|
|
|
|
++mouse_cycle;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mouse_byte[2] = mouse_in;
|
2015-04-27 04:05:14 +03:00
|
|
|
if (mouse_mode == MOUSE_SCROLLWHEEL || mouse_mode == MOUSE_BUTTONS) {
|
|
|
|
++mouse_cycle;
|
2012-02-05 08:29:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-04-27 04:05:14 +03:00
|
|
|
goto finish_packet;
|
|
|
|
case 3:
|
|
|
|
mouse_byte[3] = mouse_in;
|
|
|
|
goto finish_packet;
|
|
|
|
}
|
2015-06-02 06:55:19 +03:00
|
|
|
goto read_next;
|
2015-04-27 04:05:14 +03:00
|
|
|
finish_packet:
|
|
|
|
mouse_cycle = 0;
|
|
|
|
if (mouse_byte[0] & 0x80 || mouse_byte[0] & 0x40) {
|
|
|
|
/* x/y overflow? bad packet! */
|
2015-06-02 06:55:19 +03:00
|
|
|
goto read_next;
|
2015-04-27 04:05:14 +03:00
|
|
|
}
|
2015-06-02 06:55:19 +03:00
|
|
|
/* We now have a full mouse packet ready to use */
|
2015-04-27 04:05:14 +03:00
|
|
|
mouse_device_packet_t packet;
|
|
|
|
packet.magic = MOUSE_MAGIC;
|
|
|
|
packet.x_difference = mouse_byte[1];
|
|
|
|
packet.y_difference = mouse_byte[2];
|
|
|
|
packet.buttons = 0;
|
|
|
|
if (mouse_byte[0] & 0x01) {
|
|
|
|
packet.buttons |= LEFT_CLICK;
|
|
|
|
}
|
|
|
|
if (mouse_byte[0] & 0x02) {
|
|
|
|
packet.buttons |= RIGHT_CLICK;
|
|
|
|
}
|
|
|
|
if (mouse_byte[0] & 0x04) {
|
|
|
|
packet.buttons |= MIDDLE_CLICK;
|
|
|
|
}
|
2012-02-05 08:29:46 +04:00
|
|
|
|
2015-04-27 04:05:14 +03:00
|
|
|
if (mouse_mode == MOUSE_SCROLLWHEEL && mouse_byte[3]) {
|
|
|
|
if (mouse_byte[3] > 0) {
|
|
|
|
packet.buttons |= MOUSE_SCROLL_DOWN;
|
|
|
|
} else if (mouse_byte[3] < 0) {
|
|
|
|
packet.buttons |= MOUSE_SCROLL_UP;
|
|
|
|
}
|
2012-02-05 08:29:46 +04:00
|
|
|
}
|
2015-04-27 04:05:14 +03:00
|
|
|
|
|
|
|
mouse_device_packet_t bitbucket;
|
|
|
|
while (pipe_size(mouse_pipe) > (int)(DISCARD_POINT * sizeof(packet))) {
|
|
|
|
read_fs(mouse_pipe, 0, sizeof(packet), (uint8_t *)&bitbucket);
|
|
|
|
}
|
|
|
|
write_fs(mouse_pipe, 0, sizeof(packet), (uint8_t *)&packet);
|
2012-02-05 08:29:46 +04:00
|
|
|
}
|
2015-06-02 06:55:19 +03:00
|
|
|
read_next:
|
2012-02-05 08:29:46 +04:00
|
|
|
status = inportb(MOUSE_STATUS);
|
|
|
|
}
|
|
|
|
irq_ack(MOUSE_IRQ);
|
2015-05-19 07:07:06 +03:00
|
|
|
return 1;
|
2012-02-05 08:29:46 +04:00
|
|
|
}
|
|
|
|
|
2014-05-31 09:27:45 +04:00
|
|
|
static int ioctl_mouse(fs_node_t * node, int request, void * argp) {
|
|
|
|
if (request == 1) {
|
|
|
|
mouse_cycle = 0;
|
2014-06-01 23:56:47 +04:00
|
|
|
return 0;
|
2014-05-31 09:27:45 +04:00
|
|
|
}
|
2014-06-01 23:56:47 +04:00
|
|
|
return -1;
|
2014-05-31 09:27:45 +04:00
|
|
|
}
|
|
|
|
|
2014-03-17 02:13:27 +04:00
|
|
|
static int mouse_install(void) {
|
2012-12-08 06:33:07 +04:00
|
|
|
debug_print(NOTICE, "Initializing PS/2 mouse interface");
|
2015-04-27 04:05:14 +03:00
|
|
|
uint8_t status, result;
|
2011-10-31 10:30:48 +04:00
|
|
|
IRQ_OFF;
|
2012-02-03 06:37:50 +04:00
|
|
|
mouse_pipe = make_pipe(sizeof(mouse_device_packet_t) * PACKETS_IN_PIPE);
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_wait(1);
|
2012-02-05 08:29:46 +04:00
|
|
|
outportb(MOUSE_STATUS, 0xA8);
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_wait(1);
|
2012-02-05 08:29:46 +04:00
|
|
|
outportb(MOUSE_STATUS, 0x20);
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_wait(0);
|
|
|
|
status = inportb(0x60) | 2;
|
|
|
|
mouse_wait(1);
|
2012-02-05 08:29:46 +04:00
|
|
|
outportb(MOUSE_STATUS, 0x60);
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_wait(1);
|
2012-02-05 08:29:46 +04:00
|
|
|
outportb(MOUSE_PORT, status);
|
2011-10-29 09:33:45 +04:00
|
|
|
mouse_write(0xF6);
|
|
|
|
mouse_read();
|
|
|
|
mouse_write(0xF4);
|
|
|
|
mouse_read();
|
2015-04-27 04:05:14 +03:00
|
|
|
/* Try to enable scroll wheel (but not buttons) */
|
2015-06-02 06:55:19 +03:00
|
|
|
if (!args_present("nomousescroll")) {
|
|
|
|
mouse_write(0xF2);
|
|
|
|
mouse_read();
|
|
|
|
result = mouse_read();
|
|
|
|
mouse_write(0xF3);
|
|
|
|
mouse_read();
|
|
|
|
mouse_write(200);
|
|
|
|
mouse_read();
|
|
|
|
mouse_write(0xF3);
|
|
|
|
mouse_read();
|
|
|
|
mouse_write(100);
|
|
|
|
mouse_read();
|
|
|
|
mouse_write(0xF3);
|
|
|
|
mouse_read();
|
|
|
|
mouse_write(80);
|
|
|
|
mouse_read();
|
|
|
|
mouse_write(0xF2);
|
|
|
|
mouse_read();
|
|
|
|
result = mouse_read();
|
|
|
|
if (result == 3) {
|
|
|
|
mouse_mode = MOUSE_SCROLLWHEEL;
|
|
|
|
}
|
2015-04-27 04:05:14 +03:00
|
|
|
}
|
|
|
|
|
2012-02-05 08:29:46 +04:00
|
|
|
irq_install_handler(MOUSE_IRQ, mouse_handler);
|
2015-06-02 06:55:19 +03:00
|
|
|
IRQ_RES;
|
2014-03-17 02:13:27 +04:00
|
|
|
|
|
|
|
uint8_t tmp = inportb(0x61);
|
|
|
|
outportb(0x61, tmp | 0x80);
|
|
|
|
outportb(0x61, tmp & 0x7F);
|
|
|
|
inportb(MOUSE_PORT);
|
|
|
|
|
2014-03-17 08:50:04 +04:00
|
|
|
mouse_pipe->flags = FS_CHARDEVICE;
|
2014-05-31 09:27:45 +04:00
|
|
|
mouse_pipe->ioctl = ioctl_mouse;
|
2014-03-17 08:50:04 +04:00
|
|
|
|
2014-03-17 02:13:27 +04:00
|
|
|
vfs_mount("/dev/mouse", mouse_pipe);
|
|
|
|
return 0;
|
2011-10-29 09:33:45 +04:00
|
|
|
}
|
2014-03-17 02:13:27 +04:00
|
|
|
|
|
|
|
static int mouse_uninstall(void) {
|
|
|
|
/* TODO */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_DEF(ps2mouse, mouse_install, mouse_uninstall);
|