The PS/2 keyboard and mouse drivers are now using the new ring_buffer implementation

of the kernel. Also fixed unsafe userland communication, and other minor issues.
The packet_buffer encapsulates the ring_buffer and adds some handy functionality
(like locking).
Removed the global open masks - no reason for them to be defined globally.
Lots of cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12358 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-04-13 13:00:32 +00:00
parent ef2b38e7a9
commit 530d77200b
7 changed files with 487 additions and 345 deletions
@@ -16,6 +16,7 @@ KernelAddon ps2_hid : kernel drivers bin :
common.c common.c
keyboard.c keyboard.c
mouse.c mouse.c
packet_buffer.cpp
; ;
} }
@@ -8,18 +8,18 @@
* Stefano Ceccherini ([email protected]) * Stefano Ceccherini ([email protected])
*/ */
#include <Drivers.h> #include <Drivers.h>
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
#define DEVICE_MOUSE_NAME "input/mouse/ps2/0" #define DEVICE_MOUSE_NAME "input/mouse/ps2/0"
#define DEVICE_KEYBOARD_NAME "input/keyboard/at/0" #define DEVICE_KEYBOARD_NAME "input/keyboard/at/0"
int32 api_version = B_CUR_DRIVER_API_VERSION; int32 api_version = B_CUR_DRIVER_API_VERSION;
isa_module_info *gIsa = NULL; isa_module_info *gIsa = NULL;
int32 gMouseOpenMask;
int32 gKeyboardOpenMask;
device_hooks device_hooks
keyboard_hooks = { keyboard_hooks = {
@@ -186,10 +186,12 @@ write_aux_byte(unsigned char cmd)
} }
// driver stuff // #pragma mark -
// driver interface
status_t status_t
init_hardware() init_hardware(void)
{ {
return B_OK; return B_OK;
} }
@@ -223,7 +225,7 @@ find_device(const char *name)
status_t status_t
init_driver() init_driver(void)
{ {
status_t status; status_t status;
@@ -233,15 +235,12 @@ init_driver()
return status; return status;
} }
gMouseOpenMask = 0;
gKeyboardOpenMask = 0;
return B_OK; return B_OK;
} }
void void
uninit_driver() uninit_driver(void)
{ {
put_module(B_ISA_MODULE_NAME); put_module(B_ISA_MODULE_NAME);
} }
@@ -1,60 +1,23 @@
/* /*
* Copyright 2004 Haiku, Inc. * Copyright 2004-2005 Haiku, Inc.
* Distributed under the terms of the Haiku License. * Distributed under the terms of the MIT License.
* *
* common.h:
* PS/2 hid device driver * PS/2 hid device driver
*
* Authors (in chronological order): * Authors (in chronological order):
* Elad Lahav ([email protected]) * Elad Lahav ([email protected])
* Stefano Ceccherini ([email protected]) * Stefano Ceccherini ([email protected])
* Axel Dörfler, [email protected]
*/ */
#ifndef __PS2_COMMON_H
#define __PS2_COMMON_H
/*
* A PS/2 mouse is connected to the IBM 8042 controller, and gets its
* name from the IBM PS/2 personal computer, which was the first to
* use this device. All resources are shared between the keyboard, and
* the mouse, referred to as the "Auxiliary Device".
* I/O:
* ~~~
* The controller has 3 I/O registers:
* 1. Status (input), mapped to port 64h
* 2. Control (output), mapped to port 64h
* 3. Data (input/output), mapped to port 60h
* Data:
* ~~~~
* Since a mouse is an input only device, data can only be read, and
* not written. A packet read from the mouse data port is composed of
* three bytes:
* byte 0: status byte, where
* - bit 0: Y overflow (1 = true)
* - bit 1: X overflow (1 = true)
* - bit 2: MSB of Y offset
* - bit 3: MSB of X offset
* - bit 4: Syncronization bit (always 1)
* - bit 5: Middle button (1 = down)
* - bit 6: Right button (1 = down)
* - bit 7: Left button (1 = down)
* byte 1: X position change, since last probed (-127 to +127)
* byte 2: Y position change, since last probed (-127 to +127)
* Interrupts:
* ~~~~~~~~~~
* The PS/2 mouse device is connected to interrupt 12, which means that
* it uses the second interrupt controller (handles INT8 to INT15). In
* order for this interrupt to be enabled, both the 5th interrupt of
* the second controller AND the 3rd interrupt of the first controller
* (cascade mode) should be unmasked.
* The controller uses 3 consecutive interrupts to inform the computer
* that it has new data. On the first the data register holds the status
* byte, on the second the X offset, and on the 3rd the Y offset.
*/
#ifndef __PS2COMMON_H
#define __PS2COMMON_H
#include <ISA.h> #include <ISA.h>
#include <KernelExport.h> #include <KernelExport.h>
#include <OS.h> #include <OS.h>
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// definitions // definitions
@@ -101,9 +64,6 @@
#define INT_PS2_MOUSE 0x0C #define INT_PS2_MOUSE 0x0C
#define INT_PS2_KEYBOARD 0x01 #define INT_PS2_KEYBOARD 0x01
// other stuff
#define MOUSE_HISTORY_SIZE 256
// mouse device IDs // mouse device IDs
#define PS2_DEV_ID_STANDARD 0 #define PS2_DEV_ID_STANDARD 0
#define PS2_DEV_ID_INTELLIMOUSE 3 #define PS2_DEV_ID_INTELLIMOUSE 3
@@ -115,15 +75,13 @@
// debug defines // debug defines
#ifdef DEBUG #ifdef DEBUG
#define TRACE(x) dprintf x # define TRACE(x) dprintf x
#else #else
#define TRACE(x) # define TRACE(x) ;
#endif #endif
// global variables // global variables
extern isa_module_info *gIsa; extern isa_module_info *gIsa;
extern int32 gMouseOpenMask;
extern int32 gKeyboardOpenMask;
// prototypes // prototypes
bool wait_write_ctrl(void); bool wait_write_ctrl(void);
@@ -149,4 +107,4 @@ extern status_t mouse_read(void *cookie, off_t pos, void *buf, size_t *len);
extern status_t mouse_write(void * cookie, off_t pos, const void *buf, size_t *len); extern status_t mouse_write(void * cookie, off_t pos, const void *buf, size_t *len);
extern status_t mouse_ioctl(void *cookie, uint32 op, void *buf, size_t len); extern status_t mouse_ioctl(void *cookie, uint32 op, void *buf, size_t len);
#endif /* __PS2COMMON_H */ #endif /* __PS2_COMMON_H */
@@ -2,28 +2,23 @@
* Copyright 2004-2005 Haiku, Inc. * Copyright 2004-2005 Haiku, Inc.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* keyboard.c:
* PS/2 keyboard device driver * PS/2 keyboard device driver
*
* Authors (in chronological order): * Authors (in chronological order):
* Stefano Ceccherini ([email protected]) * Stefano Ceccherini ([email protected])
* Axel Dörfler, [email protected]
*/ */
// TODO: Uncomment locking code
// We probably need to lock to avoid misbehaving on SMP machines,
// although the kb_mouse replacement driver never locks.
#include "common.h" #include "common.h"
#ifdef COMPILE_FOR_R5 #include "kb_mouse_driver.h"
#include "cbuf_adapter.h" #include "packet_buffer.h"
#else
#include "cbuf.h"
#endif
#include <string.h> #include <string.h>
//#include <lock.h>
#include "kb_mouse_driver.h"
#define KEY_BUFFER_SIZE 100
// we will buffer 100 key strokes before we start dropping them
enum { enum {
LED_SCROLL = 1, LED_SCROLL = 1,
@@ -31,15 +26,14 @@ enum {
LED_CAPS = 4 LED_CAPS = 4
} leds_status; } leds_status;
enum { enum {
PS2_SET_LEDS = 0xed PS2_SET_LEDS = 0xed
} commands; } commands;
static int32 sKeyboardOpenMask;
static sem_id sKeyboardSem; static sem_id sKeyboardSem;
//static mutex keyboard_read_mutex; static struct packet_buffer *sKeyBuffer;
static cbuf *keyboard_buf;
static bool sIsExtended = false; static bool sIsExtended = false;
@@ -92,67 +86,72 @@ handle_keyboard_interrupt(void *data)
} else } else
keyInfo.is_keydown = true; keyInfo.is_keydown = true;
if (sIsExtended) if (sIsExtended) {
scancode |= 0x80; scancode |= 0x80;
sIsExtended = false;
}
keyInfo.timestamp = system_time(); keyInfo.timestamp = system_time();
keyInfo.scancode = scancode; keyInfo.scancode = scancode;
// TODO: Check return value while (packet_buffer_write(sKeyBuffer, (uint8 *)&keyInfo, sizeof(keyInfo)) == 0) {
cbuf_memcpy_to_chain(keyboard_buf, 0, (void *)&keyInfo, sizeof(keyInfo)); // if there is no space left in the buffer, we start dropping old key strokes
release_sem_etc(sKeyboardSem, 1, B_DO_NOT_RESCHEDULE); packet_buffer_flush(sKeyBuffer, sizeof(keyInfo));
}
sIsExtended = false; release_sem_etc(sKeyboardSem, 1, B_DO_NOT_RESCHEDULE);
return B_INVOKE_SCHEDULER; return B_INVOKE_SCHEDULER;
} }
static status_t static status_t
read_keyboard_packet(at_kbd_io *buffer) read_keyboard_packet(at_kbd_io *userBuffer)
{ {
status_t status; at_kbd_io packet;
status = acquire_sem_etc(sKeyboardSem, 1, B_CAN_INTERRUPT, 0);
status_t status = acquire_sem_etc(sKeyboardSem, 1, B_CAN_INTERRUPT, 0);
if (status < B_OK) if (status < B_OK)
return status; return status;
status = cbuf_memcpy_from_chain((void *)buffer, keyboard_buf, 0, sizeof(at_kbd_io)); if (packet_buffer_read(sKeyBuffer, (uint8 *)&packet, sizeof(at_kbd_io)) == 0) {
if (status < B_OK)
TRACE(("read_keyboard_packet(): error reading packet: %s\n", strerror(status))); TRACE(("read_keyboard_packet(): error reading packet: %s\n", strerror(status)));
return B_ERROR;
}
TRACE(("scancode: %x, keydown: %s\n", buffer->scancode, buffer->is_keydown ? "true" : "false")); TRACE(("scancode: %x, keydown: %s\n", packet.scancode, packet.is_keydown ? "true" : "false"));
return status;
return user_memcpy(userBuffer, &packet, sizeof(at_kbd_io));
} }
status_t // #pragma mark -
keyboard_open(const char *name, uint32 flags, void **cookie)
{
status_t status;
status_t
keyboard_open(const char *name, uint32 flags, void **_cookie)
{
TRACE(("keyboard open()\n")); TRACE(("keyboard open()\n"));
if (atomic_or(&gKeyboardOpenMask, 1) != 0)
if (atomic_or(&sKeyboardOpenMask, 1) != 0)
return B_BUSY; return B_BUSY;
sKeyboardSem = create_sem(0, "keyboard_sem"); sKeyboardSem = create_sem(0, "keyboard_sem");
if (sKeyboardSem < 0) { if (sKeyboardSem < 0) {
atomic_and(&gKeyboardOpenMask, 0); atomic_and(&sKeyboardOpenMask, 0);
status = sKeyboardSem; return sKeyboardSem;
return status;
} }
//if (mutex_init(&keyboard_read_mutex, "keyboard_read_mutex") < 0) sKeyBuffer = create_packet_buffer(KEY_BUFFER_SIZE * sizeof(at_kbd_io));
// panic("could not create keyboard read mutex!\n"); if (sKeyBuffer == NULL) {
panic("could not create keyboard packet buffer!\n");
atomic_and(&sKeyboardOpenMask, 0);
return B_NO_MEMORY;
}
keyboard_buf = cbuf_get_chain(1024); *_cookie = NULL;
if (keyboard_buf == NULL)
panic("could not create keyboard cbuf chain!\n");
status = install_io_interrupt_handler(1, &handle_keyboard_interrupt, NULL, 0); return install_io_interrupt_handler(1, &handle_keyboard_interrupt, NULL, 0);
*cookie = NULL;
return status;
} }
@@ -161,11 +160,10 @@ keyboard_close(void *cookie)
{ {
remove_io_interrupt_handler(1, &handle_keyboard_interrupt, NULL); remove_io_interrupt_handler(1, &handle_keyboard_interrupt, NULL);
cbuf_free_chain(keyboard_buf); delete_packet_buffer(sKeyBuffer);
delete_sem(sKeyboardSem); delete_sem(sKeyboardSem);
//mutex_destroy(&keyboard_read_mutex);
atomic_and(&gKeyboardOpenMask, 0); atomic_and(&sKeyboardOpenMask, 0);
return B_OK; return B_OK;
} }
@@ -188,30 +186,39 @@ keyboard_read(void *cookie, off_t pos, void *buffer, size_t *_length)
status_t status_t
keyboard_write(void *cookie, off_t pos, const void *buf, size_t *len) keyboard_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
{ {
TRACE(("keyboard write()\n")); TRACE(("keyboard write()\n"));
*len = 0; *_length = 0;
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
} }
status_t status_t
keyboard_ioctl(void *cookie, uint32 op, void *buf, size_t len) keyboard_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{ {
TRACE(("keyboard ioctl()\n")); TRACE(("keyboard ioctl()\n"));
switch (op) { switch (op) {
case KB_READ: case KB_READ:
TRACE(("KB_READ\n")); TRACE(("KB_READ\n"));
return read_keyboard_packet((at_kbd_io *)buf); return read_keyboard_packet((at_kbd_io *)buffer);
case KB_SET_LEDS: case KB_SET_LEDS:
set_leds((led_info *)buf); {
led_info info;
if (user_memcpy(&info, buffer, sizeof(led_info)) < B_OK)
return B_BAD_ADDRESS;
TRACE(("KB_SET_LEDS\n")); TRACE(("KB_SET_LEDS\n"));
set_leds(&info);
return B_OK; return B_OK;
}
case KB_SET_KEY_REPEATING: case KB_SET_KEY_REPEATING:
case KB_SET_KEY_NONREPEATING: case KB_SET_KEY_NONREPEATING:
TRACE(("ioctl 0x%x not implemented yet, returning B_OK\n", op)); TRACE(("ioctl 0x%x not implemented yet, returning B_OK\n", op));
return B_OK; return B_OK;
default: default:
TRACE(("invalid ioctl 0x%x\n", op)); TRACE(("invalid ioctl 0x%x\n", op));
return EINVAL; return EINVAL;
@@ -2,11 +2,12 @@
* Copyright 2001-2005 Haiku, Inc. * Copyright 2001-2005 Haiku, Inc.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* ps2mouse.c:
* PS/2 mouse device driver * PS/2 mouse device driver
*
* Authors (in chronological order): * Authors (in chronological order):
* Elad Lahav ([email protected]) * Elad Lahav ([email protected])
* Stefano Ceccherini ([email protected]) * Stefano Ceccherini ([email protected])
* Axel Dörfler, [email protected]
*/ */
/* /*
@@ -14,12 +15,14 @@
* name from the IBM PS/2 personal computer, which was the first to * name from the IBM PS/2 personal computer, which was the first to
* use this device. All resources are shared between the keyboard, and * use this device. All resources are shared between the keyboard, and
* the mouse, referred to as the "Auxiliary Device". * the mouse, referred to as the "Auxiliary Device".
*
* I/O: * I/O:
* ~~~ * ~~~
* The controller has 3 I/O registers: * The controller has 3 I/O registers:
* 1. Status (input), mapped to port 64h * 1. Status (input), mapped to port 64h
* 2. Control (output), mapped to port 64h * 2. Control (output), mapped to port 64h
* 3. Data (input/output), mapped to port 60h * 3. Data (input/output), mapped to port 60h
*
* Data: * Data:
* ~~~~ * ~~~~
* A packet read from the mouse data port is composed of * A packet read from the mouse data port is composed of
@@ -54,24 +57,21 @@
* byte, on the second the X offset, and on the 3rd the Y offset. * byte, on the second the X offset, and on the 3rd the Y offset.
*/ */
#include <Drivers.h>
#include <Errors.h>
#include <Drivers.h>
#include <string.h> #include <string.h>
#include "kb_mouse_driver.h" #include "kb_mouse_driver.h"
#include "common.h" #include "common.h"
#include "packet_buffer.h"
#ifdef COMPILE_FOR_R5
#include "cbuf_adapter.h" #define MOUSE_HISTORY_SIZE 256
#else // we record that many mouse packets before we start to drop them
#include <cbuf.h>
#endif
static sem_id sMouseSem; static sem_id sMouseSem;
static int32 sSync; static int32 sSync;
static cbuf *sMouseChain; static struct packet_buffer *sMouseBuffer;
static int32 sOpenMask; static int32 sOpenMask;
static bigtime_t sLastClickTime; static bigtime_t sLastClickTime;
@@ -81,6 +81,7 @@ static int sButtonsState;
static int32 sPacketSize; static int32 sPacketSize;
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// mouse functions // mouse functions
@@ -153,32 +154,30 @@ ps2_packet_to_movement(uint8 packet[], mouse_movement *pos)
int buttons = packet[0] & 7; int buttons = packet[0] & 7;
int xDelta = ((packet[0] & 0x10) ? 0xFFFFFF00 : 0) | packet[1]; int xDelta = ((packet[0] & 0x10) ? 0xFFFFFF00 : 0) | packet[1];
int yDelta = ((packet[0] & 0x20) ? 0xFFFFFF00 : 0) | packet[2]; int yDelta = ((packet[0] & 0x20) ? 0xFFFFFF00 : 0) | packet[2];
bigtime_t currentTime = system_time(); bigtime_t currentTime = system_time();
int8 wheel_ydelta = 0; int8 wheel_ydelta = 0;
int8 wheel_xdelta = 0; int8 wheel_xdelta = 0;
if (buttons != 0) { if (buttons != 0 && sButtonsState == 0) {
if (sButtonsState == 0) { if (sLastClickTime + sClickSpeed > currentTime)
if (sLastClickTime + sClickSpeed > currentTime) sClickCount++;
sClickCount++; else
else sClickCount = 1;
sClickCount = 1; }
}
}
sLastClickTime = currentTime; sLastClickTime = currentTime;
sButtonsState = buttons; sButtonsState = buttons;
if (sPacketSize == PS2_PACKET_INTELLIMOUSE) { if (sPacketSize == PS2_PACKET_INTELLIMOUSE) {
switch (packet[3] & 0x0F) { switch (packet[3] & 0x0F) {
case 0x01: wheel_ydelta = +1; break; // wheel 1 down case 0x01: wheel_ydelta = +1; break; // wheel 1 down
case 0x0F: wheel_ydelta = -1; break; // wheel 1 up case 0x0F: wheel_ydelta = -1; break; // wheel 1 up
case 0x02: wheel_xdelta = +1; break; // wheel 2 down case 0x02: wheel_xdelta = +1; break; // wheel 2 down
case 0x0E: wheel_xdelta = -1; break; // wheel 2 up case 0x0E: wheel_xdelta = -1; break; // wheel 2 up
} }
} }
if (pos) { if (pos) {
pos->xdelta = xDelta; pos->xdelta = xDelta;
pos->ydelta = yDelta; pos->ydelta = yDelta;
pos->buttons = buttons; pos->buttons = buttons;
@@ -188,33 +187,35 @@ ps2_packet_to_movement(uint8 packet[], mouse_movement *pos)
pos->wheel_ydelta = (int)wheel_ydelta; pos->wheel_ydelta = (int)wheel_ydelta;
pos->wheel_xdelta = (int)wheel_xdelta; pos->wheel_xdelta = (int)wheel_xdelta;
TRACE(("xdelta: %d, ydelta: %d, buttons %x, clicks: %d, timestamp %Ld\n", xDelta, yDelta, buttons, sClickCount, currentTime)); TRACE(("xdelta: %d, ydelta: %d, buttons %x, clicks: %d, timestamp %Ld\n",
xDelta, yDelta, buttons, sClickCount, currentTime));
} }
} }
/** Read a mouse event from the mouse events chain buffer. /** Read a mouse event from the mouse events chain buffer.
*/ */
static status_t static status_t
ps2_mouse_read(mouse_movement *pos) ps2_mouse_read(mouse_movement *userMovement)
{ {
status_t status;
uint8 packet[PS2_MAX_PACKET_SIZE]; uint8 packet[PS2_MAX_PACKET_SIZE];
mouse_movement movement;
status_t status;
TRACE(("ps2_mouse_read()\n")); TRACE(("ps2_mouse_read()\n"));
status = acquire_sem_etc(sMouseSem, 1, B_CAN_INTERRUPT, 0); status = acquire_sem_etc(sMouseSem, 1, B_CAN_INTERRUPT, 0);
if (status < B_OK) if (status < B_OK)
return status; return status;
status = cbuf_memcpy_from_chain(packet, sMouseChain, 0, sPacketSize); if (packet_buffer_read(sMouseBuffer, packet, sPacketSize) < (size_t)sPacketSize) {
if (status < B_OK) {
TRACE(("error copying buffer\n")); TRACE(("error copying buffer\n"));
return status; return status;
} }
ps2_packet_to_movement(packet, pos); ps2_packet_to_movement(packet, &movement);
return B_OK; return user_memcpy(userMovement, &movement, sizeof(mouse_movement));
} }
@@ -228,14 +229,11 @@ ps2_mouse_read(mouse_movement *pos)
* by read() operations. The full data is obtained using 3 consecutive * by read() operations. The full data is obtained using 3 consecutive
* calls to the handler, each holds a different byte on the data port. * calls to the handler, each holds a different byte on the data port.
*/ */
static int32 static int32
handle_mouse_interrupt(void* data) handle_mouse_interrupt(void* data)
{ {
int8 read; int8 read = gIsa->read_io_8(PS2_PORT_CTRL);
//TRACE(("mouse interrupt occurred!!!\n"));
read = gIsa->read_io_8(PS2_PORT_CTRL);
if (read < 0) { if (read < 0) {
TRACE(("Interrupt was not generated by the ps2 mouse\n")); TRACE(("Interrupt was not generated by the ps2 mouse\n"));
return B_UNHANDLED_INTERRUPT; return B_UNHANDLED_INTERRUPT;
@@ -243,7 +241,10 @@ handle_mouse_interrupt(void* data)
read = read_data_byte(); read = read_data_byte();
TRACE(("mouse interrupt : byte %x\n", read)); TRACE(("mouse interrupt : byte %x\n", read));
cbuf_memcpy_to_chain(sMouseChain, sSync, &read, sizeof(read)); while (packet_buffer_write(sMouseBuffer, &read, sizeof(read)) == 0) {
// we start dropping packets when the buffer is full
packet_buffer_flush(sMouseBuffer, sPacketSize);
}
if (sSync == 0 && !(read & 8)) { if (sSync == 0 && !(read & 8)) {
TRACE(("mouse resynched, bad data\n")); TRACE(("mouse resynched, bad data\n"));
@@ -262,12 +263,12 @@ handle_mouse_interrupt(void* data)
} }
///////////////////////////////////////////////////////////////////////// // #pragma mark -
// file operations // Device functions
status_t status_t
mouse_open(const char *name, uint32 flags, void **cookie) mouse_open(const char *name, uint32 flags, void **_cookie)
{ {
status_t status; status_t status;
int8 commandByte; int8 commandByte;
@@ -308,9 +309,9 @@ mouse_open(const char *name, uint32 flags, void **cookie)
return B_ERROR; // Something's wrong. Better quit return B_ERROR; // Something's wrong. Better quit
} }
sMouseChain = cbuf_get_chain(MOUSE_HISTORY_SIZE); sMouseBuffer = create_packet_buffer(MOUSE_HISTORY_SIZE * sPacketSize);
if (sMouseChain == NULL) { if (sMouseBuffer == NULL) {
TRACE(("can't allocate cbuf chain\n")); TRACE(("can't allocate mouse actions buffer\n"));
put_module(B_ISA_MODULE_NAME); put_module(B_ISA_MODULE_NAME);
return B_ERROR; return B_ERROR;
} }
@@ -320,14 +321,14 @@ mouse_open(const char *name, uint32 flags, void **cookie)
sMouseSem = create_sem(0, "ps2_mouse_sem"); sMouseSem = create_sem(0, "ps2_mouse_sem");
if (sMouseSem < 0) { if (sMouseSem < 0) {
TRACE(("failed creating PS/2 mouse semaphore!\n")); TRACE(("failed creating PS/2 mouse semaphore!\n"));
cbuf_free_chain(sMouseChain); delete_packet_buffer(sMouseBuffer);
put_module(B_ISA_MODULE_NAME); put_module(B_ISA_MODULE_NAME);
return sMouseSem; return sMouseSem;
} }
set_sem_owner(sMouseSem, B_SYSTEM_TEAM); set_sem_owner(sMouseSem, B_SYSTEM_TEAM);
*cookie = NULL; *_cookie = NULL;
commandByte = get_command_byte(); commandByte = get_command_byte();
commandByte |= PS2_BITS_AUX_INTERRUPT; commandByte |= PS2_BITS_AUX_INTERRUPT;
@@ -343,19 +344,17 @@ mouse_open(const char *name, uint32 flags, void **cookie)
TRACE(("mouse_open(): mouse succesfully enabled\n")); TRACE(("mouse_open(): mouse succesfully enabled\n"));
// register interrupt handler // register interrupt handler
status = install_io_interrupt_handler(INT_PS2_MOUSE, handle_mouse_interrupt, NULL, 0); return install_io_interrupt_handler(INT_PS2_MOUSE, handle_mouse_interrupt, NULL, 0);
return status;
} }
status_t status_t
mouse_close(void * cookie) mouse_close(void *cookie)
{ {
TRACE(("mouse_close()\n")); TRACE(("mouse_close()\n"));
ps2_enable_mouse(false); ps2_enable_mouse(false);
cbuf_free_chain(sMouseChain); delete_packet_buffer(sMouseBuffer);
delete_sem(sMouseSem); delete_sem(sMouseSem);
remove_io_interrupt_handler(INT_PS2_MOUSE, handle_mouse_interrupt, NULL); remove_io_interrupt_handler(INT_PS2_MOUSE, handle_mouse_interrupt, NULL);
@@ -367,32 +366,31 @@ mouse_close(void * cookie)
status_t status_t
mouse_freecookie(void * cookie) mouse_freecookie(void *cookie)
{ {
return B_OK; return B_OK;
} }
status_t status_t
mouse_read(void *cookie, off_t pos, void *buf, size_t *len) mouse_read(void *cookie, off_t pos, void *buffer, size_t *_length)
{ {
*len = 0; *_length = 0;
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
} }
status_t status_t
mouse_write(void * cookie, off_t pos, const void *buf, size_t *len) mouse_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
{ {
*len = 0; *_length = 0;
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
} }
status_t status_t
mouse_ioctl(void *cookie, uint32 op, void *buf, size_t len) mouse_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{ {
mouse_movement *pos = (mouse_movement *)buf;
switch (op) { switch (op) {
case MS_NUM_EVENTS: case MS_NUM_EVENTS:
{ {
@@ -401,28 +399,34 @@ mouse_ioctl(void *cookie, uint32 op, void *buf, size_t len)
get_sem_count(sMouseSem, &count); get_sem_count(sMouseSem, &count);
return count; return count;
} }
case MS_READ: case MS_READ:
TRACE(("MS_READ\n")); TRACE(("MS_READ\n"));
return ps2_mouse_read(pos); return ps2_mouse_read((mouse_movement *)buffer);
case MS_SET_TYPE: case MS_SET_TYPE:
TRACE(("MS_SET_TYPE not implemented\n")); TRACE(("MS_SET_TYPE not implemented\n"));
return EINVAL; return B_BAD_VALUE;
case MS_SET_MAP: case MS_SET_MAP:
TRACE(("MS_SET_MAP (set mouse mapping) not implemented\n")); TRACE(("MS_SET_MAP (set mouse mapping) not implemented\n"));
return EINVAL; return B_BAD_VALUE;
case MS_GET_ACCEL: case MS_GET_ACCEL:
TRACE(("MS_GET_ACCEL (get mouse acceleration) not implemented\n")); TRACE(("MS_GET_ACCEL (get mouse acceleration) not implemented\n"));
return EINVAL; return B_BAD_VALUE;
case MS_SET_ACCEL: case MS_SET_ACCEL:
TRACE(("MS_SET_ACCEL (set mouse acceleration) not implemented\n")); TRACE(("MS_SET_ACCEL (set mouse acceleration) not implemented\n"));
return EINVAL; return B_BAD_VALUE;
case MS_SET_CLICKSPEED: case MS_SET_CLICKSPEED:
TRACE(("MS_SETCLICK (set click speed)\n")); TRACE(("MS_SETCLICK (set click speed)\n"));
sClickSpeed = *(bigtime_t *)buf; return user_memcpy(&sClickSpeed, buffer, sizeof(bigtime_t));
return B_OK;
default: default:
TRACE(("unknown opcode: %ld\n", op)); TRACE(("unknown opcode: %ld\n", op));
return EINVAL; return B_BAD_VALUE;
} }
} }
@@ -0,0 +1,141 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "packet_buffer.h"
#include <KernelExport.h>
#include <util/ring_buffer.h>
#include <stdlib.h>
#include <string.h>
/** The idea behind this packet buffer is to have multi-threading safe
* implementation that can be used in interrupts on top of the
* ring_buffer implementation provided by the kernel.
* It uses a spinlock for synchronization.
*
* IOW if you don't have such high restrictions in your environment,
* you better don't want to use it at all.
*/
struct packet_buffer {
struct ring_buffer *buffer;
spinlock lock;
};
struct packet_buffer *
create_packet_buffer(size_t size)
{
struct packet_buffer *buffer = (packet_buffer *)malloc(sizeof(packet_buffer));
if (buffer == NULL)
return NULL;
buffer->buffer = create_ring_buffer(size);
if (buffer->buffer == NULL) {
free(buffer);
return NULL;
}
buffer->lock = 0;
return buffer;
}
void
delete_packet_buffer(struct packet_buffer *buffer)
{
delete_ring_buffer(buffer->buffer);
free(buffer);
}
void
packet_buffer_clear(struct packet_buffer *buffer)
{
cpu_status state = disable_interrupts();
acquire_spinlock(&buffer->lock);
ring_buffer_clear(buffer->buffer);
release_spinlock(&buffer->lock);
restore_interrupts(state);
}
size_t
packet_buffer_readable(struct packet_buffer *buffer)
{
cpu_status state = disable_interrupts();
acquire_spinlock(&buffer->lock);
size_t available = ring_buffer_readable(buffer->buffer);
release_spinlock(&buffer->lock);
restore_interrupts(state);
return available;
}
size_t
packet_buffer_writable(struct packet_buffer *buffer)
{
cpu_status state = disable_interrupts();
acquire_spinlock(&buffer->lock);
size_t left = ring_buffer_writable(buffer->buffer);
release_spinlock(&buffer->lock);
restore_interrupts(state);
return left;
}
void
packet_buffer_flush(struct packet_buffer *buffer, size_t length)
{
cpu_status state = disable_interrupts();
acquire_spinlock(&buffer->lock);
ring_buffer_flush(buffer->buffer, length);
release_spinlock(&buffer->lock);
restore_interrupts(state);
}
size_t
packet_buffer_read(struct packet_buffer *buffer, uint8 *data, size_t length)
{
cpu_status state = disable_interrupts();
acquire_spinlock(&buffer->lock);
size_t bytesRead = ring_buffer_read(buffer->buffer, data, length);
release_spinlock(&buffer->lock);
restore_interrupts(state);
return bytesRead;
}
size_t
packet_buffer_write(struct packet_buffer *buffer, const uint8 *data, size_t length)
{
cpu_status state = disable_interrupts();
acquire_spinlock(&buffer->lock);
size_t bytesWritten = ring_buffer_write(buffer->buffer, data, length);
release_spinlock(&buffer->lock);
restore_interrupts(state);
return bytesWritten;
}
@@ -0,0 +1,32 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef PACKET_BUFFER_H
#define PACKET_BUFFER_H
#include <OS.h>
struct packet_buffer;
#ifdef __cplusplus
extern "C" {
#endif
struct packet_buffer *create_packet_buffer(size_t size);
void delete_packet_buffer(struct packet_buffer *buffer);
void packet_buffer_clear(struct packet_buffer *buffer);
size_t packet_buffer_readable(struct packet_buffer *buffer);
size_t packet_buffer_writable(struct packet_buffer *buffer);
void packet_buffer_flush(struct packet_buffer *buffer, size_t bytes);
size_t packet_buffer_read(struct packet_buffer *buffer, uint8 *data, size_t length);
size_t packet_buffer_write(struct packet_buffer *buffer, const uint8 *data, size_t length);
#ifdef __cplusplus
}
#endif
#endif /* PACKET_BUFFER_H */