copied some files from ps2_hid driver

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15982 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Marcus Overhagen 2006-01-16 20:31:08 +00:00
parent c2f9bd7dda
commit d61a52a78d
10 changed files with 1790 additions and 0 deletions

View File

@ -0,0 +1,141 @@
/*
* Copyright 2005, Axel Dörfler, axeld@pinc-software.de. 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;
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2005, Axel Dörfler, axeld@pinc-software.de. 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;
typedef struct packet_buffer 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 */

View File

@ -0,0 +1,348 @@
/*
* Copyright 2004-2005 Haiku, Inc.
* Distributed under the terms of the Haiku License.
*
* common.c:
* PS/2 hid device driver
* Authors (in chronological order):
* Stefano Ceccherini (burton666@libero.it)
* Axel Dörfler, axeld@pinc-software.de
*/
#include <string.h>
#include "common.h"
#include "ps2_service.h"
#include "ps2_dev.h"
int32 api_version = B_CUR_DRIVER_API_VERSION;
device_hooks sKeyboardDeviceHooks = {
keyboard_open,
keyboard_close,
keyboard_freecookie,
keyboard_ioctl,
keyboard_read,
keyboard_write,
NULL,
NULL,
NULL,
NULL
};
device_hooks sMouseDeviceHooks = {
mouse_open,
mouse_close,
mouse_freecookie,
mouse_ioctl,
mouse_read,
mouse_write,
NULL,
NULL,
NULL,
NULL
};
isa_module_info *gIsa = NULL;
sem_id gDeviceOpenSemaphore;
static sem_id sKbcSem;
static int32 sIgnoreInterrupts = 0;
static bool sMultiplexingActive = false;
inline uint8
ps2_read_ctrl()
{
return gIsa->read_io_8(PS2_PORT_CTRL);
}
inline uint8
ps2_read_data()
{
return gIsa->read_io_8(PS2_PORT_DATA);
}
inline void
ps2_write_ctrl(uint8 ctrl)
{
gIsa->write_io_8(PS2_PORT_CTRL, ctrl);
}
inline void
ps2_write_data(uint8 data)
{
gIsa->write_io_8(PS2_PORT_DATA, data);
}
status_t
ps2_wait_read()
{
int i;
for (i = 0; i < PS2_CTRL_WAIT_TIMEOUT / 50; i++) {
if (ps2_read_ctrl() & PS2_STATUS_OUTPUT_BUFFER_FULL)
return B_OK;
snooze(50);
}
return B_ERROR;
}
status_t
ps2_wait_write()
{
int i;
for (i = 0; i < PS2_CTRL_WAIT_TIMEOUT / 50; i++) {
if (!(ps2_read_ctrl() & PS2_STATUS_INPUT_BUFFER_FULL))
return B_OK;
snooze(50);
}
return B_ERROR;
}
// #pragma mark -
void
ps2_flush()
{
int i;
acquire_sem(sKbcSem);
atomic_add(&sIgnoreInterrupts, 1);
for (i = 0; i < 64; i++) {
uint8 ctrl;
uint8 data;
ctrl = ps2_read_ctrl();
if (!(ctrl & PS2_STATUS_OUTPUT_BUFFER_FULL))
return;
data = ps2_read_data();
TRACE(("ps2_flush: ctrl 0x%02x, data 0x%02x (%s)\n", ctrl, data, (ctrl & PS2_STATUS_MOUSE_DATA) ? "mouse" : "keyb"));
snooze(100);
}
atomic_add(&sIgnoreInterrupts, -1);
release_sem(sKbcSem);
}
status_t
ps2_command(uint8 cmd, const uint8 *out, int out_count, uint8 *in, int in_count)
{
status_t res;
int i;
acquire_sem(sKbcSem);
atomic_add(&sIgnoreInterrupts, 1);
res = ps2_wait_write();
if (res == B_OK)
ps2_write_ctrl(cmd);
for (i = 0; res == B_OK && i < out_count; i++) {
res = ps2_wait_write();
if (res == B_OK)
ps2_write_data(out[i]);
}
for (i = 0; res == B_OK && i < in_count; i++) {
res = ps2_wait_read();
if (res == B_OK)
in[i] = ps2_read_data();
}
atomic_add(&sIgnoreInterrupts, -1);
release_sem(sKbcSem);
return res;
}
status_t
ps2_keyboard_command(uint8 cmd, const uint8 *out, int out_count, uint8 *in, int in_count)
{
return B_OK;
}
status_t
ps2_mouse_command(uint8 cmd, const uint8 *out, int out_count, uint8 *in, int in_count)
{
return B_OK;
}
// #pragma mark -
inline status_t
ps2_get_command_byte(uint8 *byte)
{
return ps2_command(PS2_CTRL_READ_CMD, NULL, 0, byte, 1);
}
inline status_t
ps2_set_command_byte(uint8 byte)
{
return ps2_command(PS2_CTRL_WRITE_CMD, &byte, 1, NULL, 0);
}
// #pragma mark -
static int32
ps2_interrupt(void* cookie)
{
uint8 ctrl;
uint8 data;
ps2_dev *dev;
TRACE(("ps2_interrupt\n"));
ctrl = ps2_read_ctrl();
if (!(ctrl & PS2_STATUS_OUTPUT_BUFFER_FULL))
return B_UNHANDLED_INTERRUPT;
if (atomic_get(&sIgnoreInterrupts)) {
TRACE(("ps2_interrupt: ignoring, ctrl 0x%02x (%s)\n", ctrl, (ctrl & PS2_STATUS_MOUSE_DATA) ? "mouse" : "keyb"));
return B_HANDLED_INTERRUPT;
}
data = ps2_read_data();
TRACE(("ps2_interrupt: ctrl 0x%02x, data 0x%02x (%s)\n", ctrl, data, (ctrl & PS2_STATUS_MOUSE_DATA) ? "mouse" : "keyb"));
if (ctrl & PS2_STATUS_MOUSE_DATA) {
uint8 idx = 0;
dev = &ps2_device[PS2_DEVICE_MOUSE + idx];
} else {
dev = &ps2_device[PS2_DEVICE_KEYB];
}
return ps2_dev_handle_int(dev, data);
}
// #pragma mark - driver interface
status_t
init_hardware(void)
{
TRACE(("ps2_hid: init_hardware\n"));
return B_OK;
}
const char **
publish_devices(void)
{
TRACE(("ps2_hid: publish_devices\n"));
return NULL;
}
device_hooks *
find_device(const char *name)
{
TRACE(("ps2_hid: find_device\n"));
return NULL;
}
status_t
init_driver(void)
{
status_t status;
TRACE(("ps2_hid: init_driver\n"));
status = get_module(B_ISA_MODULE_NAME, (module_info **)&gIsa);
if (status < B_OK)
goto err_1;
sKbcSem = create_sem(1, "ps/2 keyb ctrl");
gDeviceOpenSemaphore = create_sem(1, "ps/2 open");
if (gDeviceOpenSemaphore < B_OK)
goto err_2;
status = ps2_dev_init();
status = ps2_service_init();
status = install_io_interrupt_handler(INT_PS2_KEYBOARD, &ps2_interrupt, NULL, 0);
if (status)
goto err_3;
status = install_io_interrupt_handler(INT_PS2_MOUSE, &ps2_interrupt, NULL, 0);
if (status)
goto err_4;
{
uint8 d;
status_t res;
res = ps2_get_command_byte(&d);
dprintf("ps2_get_command_byte: res 0x%08x, d 0x%02x\n", res, d);
d |= PS2_BITS_TRANSLATE_SCANCODES | PS2_BITS_KEYBOARD_INTERRUPT | PS2_BITS_AUX_INTERRUPT;
d &= ~(PS2_BITS_KEYBOARD_DISABLED | PS2_BITS_MOUSE_DISABLED);
res = ps2_set_command_byte(d);
dprintf("ps2_set_command_byte: res 0x%08x, d 0x%02x\n", res, d);
}
ps2_service_handle_device_added(&ps2_device[PS2_DEVICE_KEYB]);
ps2_service_handle_device_added(&ps2_device[PS2_DEVICE_MOUSE]);
if (sMultiplexingActive) {
ps2_service_handle_device_added(&ps2_device[PS2_DEVICE_MOUSE + 1]);
ps2_service_handle_device_added(&ps2_device[PS2_DEVICE_MOUSE + 2]);
ps2_service_handle_device_added(&ps2_device[PS2_DEVICE_MOUSE + 3]);
}
//goto err_5;
TRACE(("ps2_hid: init_driver done!\n"));
return B_OK;
err_5:
remove_io_interrupt_handler(INT_PS2_MOUSE, &ps2_interrupt, NULL);
err_4:
remove_io_interrupt_handler(INT_PS2_KEYBOARD, &ps2_interrupt, NULL);
err_3:
ps2_service_exit();
ps2_dev_exit();
delete_sem(gDeviceOpenSemaphore);
delete_sem(sKbcSem);
err_2:
put_module(B_ISA_MODULE_NAME);
err_1:
TRACE(("ps2_hid: init_driver failed!\n"));
return B_ERROR;
}
void
uninit_driver(void)
{
TRACE(("ps2_hid: uninit_driver\n"));
remove_io_interrupt_handler(INT_PS2_MOUSE, &ps2_interrupt, NULL);
remove_io_interrupt_handler(INT_PS2_KEYBOARD, &ps2_interrupt, NULL);
ps2_service_exit();
ps2_dev_exit();
delete_sem(gDeviceOpenSemaphore);
delete_sem(sKbcSem);
put_module(B_ISA_MODULE_NAME);
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2004-2005 Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* PS/2 hid device driver
*
* Authors (in chronological order):
* Elad Lahav (elad@eldarshany.com)
* Stefano Ceccherini (burton666@libero.it)
* Axel Dörfler, axeld@pinc-software.de
*/
#ifndef __PS2_COMMON_H
#define __PS2_COMMON_H
#include <ISA.h>
#include <Drivers.h>
#include <KernelExport.h>
#include <OS.h>
#include "ps2.h"
// debug defines
#ifdef DEBUG
# define TRACE(x) dprintf x
#else
# define TRACE(x) ;
#endif
// global variables
extern isa_module_info *gIsa;
extern sem_id gDeviceOpenSemaphore;
extern device_hooks sKeyboardDeviceHooks;
extern device_hooks sMouseDeviceHooks;
// prototypes from common.c
extern status_t ps2_wait_read();
extern status_t ps2_wait_write();
extern void ps2_flush();
extern status_t ps2_command(uint8 cmd, const uint8 *out, int out_count, uint8 *in, int in_count);
extern status_t ps2_keyboard_command(uint8 cmd, const uint8 *out, int out_count, uint8 *in, int in_count);
extern status_t ps2_mouse_command(uint8 cmd, const uint8 *out, int out_count, uint8 *in, int in_count);
extern status_t ps2_get_command_byte(uint8 *byte);
extern status_t ps2_set_command_byte(uint8 byte);
extern void ps2_claim_result(uint8 *buffer, size_t bytes);
extern void ps2_unclaim_result(void);
extern status_t ps2_wait_for_result(void);
extern bool ps2_handle_result(uint8 data);
extern void ps2_common_uninitialize(void);
extern status_t ps2_common_initialize(void);
// prototypes from keyboard.c & mouse.c
extern status_t probe_keyboard(void);
extern status_t probe_mouse(size_t *probed_packet_size);
extern int32 mouse_handle_int(uint8 data);
extern int32 keyboard_handle_int(uint8 data);
extern status_t keyboard_open(const char *name, uint32 flags, void **cookie);
extern status_t keyboard_close(void *cookie);
extern status_t keyboard_freecookie(void *cookie);
extern status_t keyboard_read(void *cookie, off_t pos, void *buf, size_t *len);
extern status_t keyboard_write(void * cookie, off_t pos, const void *buf, size_t *len);
extern status_t keyboard_ioctl(void *cookie, uint32 op, void *buf, size_t len);
extern status_t mouse_open(const char *name, uint32 flags, void **cookie);
extern status_t mouse_close(void *cookie);
extern status_t mouse_freecookie(void *cookie);
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_ioctl(void *cookie, uint32 op, void *buf, size_t len);
#endif /* __PS2_COMMON_H */

View File

@ -0,0 +1,80 @@
/*
* Copyright 2004-2005 Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* PS/2 interface definitions
*
* Authors (in chronological order):
* Elad Lahav (elad@eldarshany.com)
* Stefano Ceccherini (burton666@libero.it)
* Axel Dörfler, axeld@pinc-software.de
*/
#ifndef _PS2_H
#define _PS2_H
/** Interface definitions for the Intel 8042, 8741, or 8742 (PS/2) */
// I/O addresses
#define PS2_PORT_DATA 0x60
#define PS2_PORT_CTRL 0x64
// data port bits
#define PS2_STATUS_OUTPUT_BUFFER_FULL 0x01
#define PS2_STATUS_INPUT_BUFFER_FULL 0x02
#define PS2_STATUS_MOUSE_DATA 0x20
#define PS2_STATUS_TIMEOUT 0x40
// control words
#define PS2_CTRL_READ_CMD 0x20
#define PS2_CTRL_WRITE_CMD 0x60
#define PS2_CTRL_WRITE_AUX 0xd4
#define PS2_CTRL_MOUSE_DISABLE 0xa7
#define PS2_CTRL_MOUSE_ENABLE 0xa8
#define PS2_CTRL_MOUSE_TEST 0xa9
#define PS2_CTRL_KEYBOARD_SELF_TEST 0xaa
#define PS2_CTRL_KEYBOARD_ACTIVATE 0xae
#define PS2_CTRL_KEYBOARD_DEACTIVATE 0xad
// command bytes
#define PS2_CMD_DEV_INIT 0x43
// command bits
#define PS2_BITS_KEYBOARD_INTERRUPT 0x01
#define PS2_BITS_AUX_INTERRUPT 0x02
#define PS2_BITS_KEYBOARD_DISABLED 0x10
#define PS2_BITS_MOUSE_DISABLED 0x20
#define PS2_BITS_TRANSLATE_SCANCODES 0x40
// data words
#define PS2_CMD_TEST_PASSED 0xaa
#define PS2_CMD_GET_DEVICE_ID 0xf2
#define PS2_CMD_SET_SAMPLE_RATE 0xf3
#define PS2_CMD_ENABLE_MOUSE 0xf4
#define PS2_CMD_DISABLE_MOUSE 0xf5
#define PS2_CMD_RESET_MOUSE 0xff
// reply codes
#define PS2_REPLY_TEST_PASSED 0x55
#define PS2_REPLY_ACK 0xfa
#define PS2_REPLY_RESEND 0xfe
#define PS2_REPLY_ERROR 0xfc
// interrupts
#define INT_PS2_MOUSE 0x0c
#define INT_PS2_KEYBOARD 0x01
// mouse device IDs
#define PS2_DEV_ID_STANDARD 0
#define PS2_DEV_ID_INTELLIMOUSE 3
// packet sizes
#define PS2_PACKET_STANDARD 3
#define PS2_PACKET_INTELLIMOUSE 4
#define PS2_MAX_PACKET_SIZE 4
// Should be equal to the biggest packet size
// timeouts
#define PS2_CTRL_WAIT_TIMEOUT 500000
#endif /* _PS2_H */

View File

@ -0,0 +1,117 @@
/*
* Copyright 2005 Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* PS/2 hid device driver
*
* Authors (in chronological order):
* Marcus Overhagen (marcus@overhagen.de)
*/
#include <fs/devfs.h>
#include "ps2_dev.h"
#include "ps2_service.h"
#define PS2_DEV_COUNT 5
ps2_dev ps2_device[PS2_DEV_COUNT] =
{
{ .name = "input/mouse/ps2/0", .active = false, .result_sem = -1 },
{ .name = "input/mouse/ps2/1", .active = false, .result_sem = -1 },
{ .name = "input/mouse/ps2/2", .active = false, .result_sem = -1 },
{ .name = "input/mouse/ps2/3", .active = false, .result_sem = -1 },
{ .name = "input/keyboard/at/0", .active = false, .result_sem = -1, .flags = PS2_FLAG_KEYB }
};
status_t
ps2_dev_init(void)
{
int i;
for (i = 0; i < PS2_DEV_COUNT; i++) {
ps2_dev *dev = &ps2_device[i];
dev->result_sem = create_sem(0, "ps2 result");
if (dev->result_sem < 0)
goto err;
}
return B_OK;
err:
ps2_dev_exit();
return B_ERROR;
}
void
ps2_dev_exit(void)
{
int i;
for (i = 0; i < PS2_DEV_COUNT; i++) {
ps2_dev *dev = &ps2_device[i];
if (dev->result_sem >= 0) {
delete_sem(dev->result_sem);
dev->result_sem = -1;
}
}
}
void
ps2_dev_publish(ps2_dev *dev)
{
status_t status;
if (dev->active)
return;
TRACE(("ps2_dev_publish %s\n", dev->name));
dev->active = true;
status = devfs_publish_device(dev->name, NULL,
(dev->flags & PS2_FLAG_KEYB) ? &sKeyboardDeviceHooks : &sMouseDeviceHooks);
TRACE(("devfs_publish_device %s, status = 0x%08x\n", dev->name, status));
}
void
ps2_dev_unpublish(ps2_dev *dev)
{
if (!dev->active)
return;
TRACE(("ps2_dev_unpublish %s\n", dev->name));
dev->active = false;
}
int32
ps2_dev_handle_int(ps2_dev *dev, uint8 data)
{
if (!dev->active) {
ps2_service_handle_device_added(dev);
}
if (dev->result_buf_cnt) {
dev->result_buf[dev->result_buf_idx] = data;
dev->result_buf_idx++;
dev->result_buf_cnt--;
if (dev->result_buf_cnt == 0) {
release_sem_etc(dev->result_sem, 1, B_DO_NOT_RESCHEDULE);
return B_INVOKE_SCHEDULER;
}
return B_HANDLED_INTERRUPT;
}
if (!dev->active) {
return B_HANDLED_INTERRUPT;
}
// temporary hack...
if (dev->flags & PS2_FLAG_KEYB)
return keyboard_handle_int(data);
else
return mouse_handle_int(data);
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2005 Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* PS/2 hid device driver
*
* Authors (in chronological order):
* Marcus Overhagen (marcus@overhagen.de)
*/
#ifndef __PS2_DEV_H
#define __PS2_DEV_H
#include "common.h"
typedef struct
{
const char * name;
bool active;
uint32 flags;
sem_id result_sem;
uint8 * result_buf;
int result_buf_idx;
int result_buf_cnt;
} ps2_dev;
extern ps2_dev ps2_device[5];
#define PS2_DEVICE_MOUSE 0
#define PS2_DEVICE_KEYB 4
#define PS2_FLAG_KEYB 1
status_t ps2_dev_init(void);
void ps2_dev_exit(void);
void ps2_dev_publish(ps2_dev *dev);
void ps2_dev_unpublish(ps2_dev *dev);
int32 ps2_dev_handle_int(ps2_dev *dev, uint8 data);
#endif

View File

@ -0,0 +1,317 @@
/*
* Copyright 2004-2005 Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* PS/2 keyboard device driver
*
* Authors (in chronological order):
* Stefano Ceccherini (burton666@libero.it)
* Axel Dörfler, axeld@pinc-software.de
*/
#include "common.h"
#include "kb_mouse_driver.h"
#include "packet_buffer.h"
#include <string.h>
#define KEY_BUFFER_SIZE 100
// we will buffer 100 key strokes before we start dropping them
enum {
LED_SCROLL = 1,
LED_NUM = 2,
LED_CAPS = 4
} leds_status;
enum {
PS2_DATA_SET_LEDS = 0xed,
PS2_ENABLE_KEYBOARD = 0xf4,
PS2_DISABLE_KEYBOARD = 0xf5,
EXTENDED_KEY = 0xe0,
};
static int32 sKeyboardOpenMask;
static sem_id sKeyboardSem;
static struct packet_buffer *sKeyBuffer;
static bool sIsExtended = false;
static status_t
set_leds(led_info *ledInfo)
{
uint8 leds = 0;
TRACE(("ps2_hid: set keyboard LEDs\n"));
if (ledInfo->scroll_lock)
leds |= LED_SCROLL;
if (ledInfo->num_lock)
leds |= LED_NUM;
if (ledInfo->caps_lock)
leds |= LED_CAPS;
return ps2_keyboard_command(PS2_DATA_SET_LEDS, &leds, 1, NULL, 0);
}
int32 keyboard_handle_int(uint8 data)
{
at_kbd_io keyInfo;
uint8 scancode;
if (atomic_and(&sKeyboardOpenMask, 1) == 0)
return B_HANDLED_INTERRUPT;
// TODO: Handle braindead "pause" key special case
if (data == EXTENDED_KEY) {
sIsExtended = true;
TRACE(("Extended key\n"));
return B_HANDLED_INTERRUPT;
}
scancode = data;
TRACE(("scancode: %x\n", scancode));
// For now, F12 enters the kernel debugger
// ToDo: remove me later :-)
if (scancode == 88)
panic("keyboard requested halt.\n");
if (scancode & 0x80) {
keyInfo.is_keydown = false;
scancode -= 0x80;
} else
keyInfo.is_keydown = true;
if (sIsExtended) {
scancode |= 0x80;
sIsExtended = false;
}
keyInfo.timestamp = system_time();
keyInfo.scancode = scancode;
if (packet_buffer_write(sKeyBuffer, (uint8 *)&keyInfo, sizeof(keyInfo)) == 0) {
// If there is no space left in the buffer, we drop this key stroke. We avoid
// dropping old key strokes, to not destroy what already was typed.
return B_HANDLED_INTERRUPT;
}
release_sem_etc(sKeyboardSem, 1, B_DO_NOT_RESCHEDULE);
return B_INVOKE_SCHEDULER;
}
static status_t
read_keyboard_packet(at_kbd_io *userBuffer)
{
at_kbd_io packet;
status_t status;
TRACE(("read_keyboard_packet()\n"));
status = acquire_sem_etc(sKeyboardSem, 1, B_CAN_INTERRUPT, 0);
if (status < B_OK)
return status;
if (packet_buffer_read(sKeyBuffer, (uint8 *)&packet, sizeof(at_kbd_io)) == 0) {
TRACE(("read_keyboard_packet(): error reading packet: %s\n", strerror(status)));
return B_ERROR;
}
TRACE(("scancode: %x, keydown: %s\n", packet.scancode, packet.is_keydown ? "true" : "false"));
return user_memcpy(userBuffer, &packet, sizeof(at_kbd_io));
}
static status_t
enable_keyboard(void)
{
uint32 tries = 3;
while (tries-- > 0) {
// keyboard_empty_data();
if (ps2_keyboard_command(PS2_ENABLE_KEYBOARD, NULL, 0, NULL, 0) == B_OK)
return B_OK;
}
dprintf("enable_keyboard() failed\n");
return B_ERROR;
}
// #pragma mark -
status_t
probe_keyboard(void)
{
int32 tries;
// ToDo: for now there just is a keyboard ready to be used...
// Keyboard detection does not seem to be working always correctly
// not sure if this is the correct way
ps2_keyboard_command(PS2_CTRL_KEYBOARD_SELF_TEST, NULL, 0, NULL, 0);
ps2_keyboard_command(PS2_CTRL_KEYBOARD_SELF_TEST, NULL, 0, NULL, 0);
#if 0
// Keyboard self-test
if (ps2_write_ctrl(PS2_CTRL_KEYBOARD_SELF_TEST) != B_OK)
return B_ERROR;
tries = 3;
while (tries-- > 0) {
uint8 acknowledged;
if (ps2_read_data(&acknowledged) == B_OK && acknowledged == PS2_REPLY_ACK)
break;
}
// This selftest appears to fail quite frequently we'll just disable it
/*if (tries < 0)
return B_ERROR;*/
// Activate keyboard
if (ps2_write_ctrl(PS2_CTRL_KEYBOARD_ACTIVATE) != B_OK)
return B_ERROR;
// Enable keyboard
return enable_keyboard();
#endif
return B_OK;
}
// #pragma mark -
status_t
keyboard_open(const char *name, uint32 flags, void **_cookie)
{
status_t status;
TRACE(("keyboard open()\n"));
if (atomic_or(&sKeyboardOpenMask, 1) != 0)
return B_BUSY;
acquire_sem(gDeviceOpenSemaphore);
sKeyboardSem = create_sem(0, "keyboard_sem");
if (sKeyboardSem < 0) {
status = sKeyboardSem;
goto err2;
}
sKeyBuffer = create_packet_buffer(KEY_BUFFER_SIZE * sizeof(at_kbd_io));
if (sKeyBuffer == NULL) {
status = B_NO_MEMORY;
goto err3;
}
release_sem(gDeviceOpenSemaphore);
*_cookie = NULL;
TRACE(("keyboard_open(): done.\n"));
return B_OK;
err4:
delete_packet_buffer(sKeyBuffer);
err3:
delete_sem(sKeyboardSem);
err2:
err1:
atomic_and(&sKeyboardOpenMask, 0);
release_sem(gDeviceOpenSemaphore);
return status;
}
status_t
keyboard_close(void *cookie)
{
TRACE(("keyboard_close()\n"));
delete_packet_buffer(sKeyBuffer);
delete_sem(sKeyboardSem);
atomic_and(&sKeyboardOpenMask, 0);
return B_OK;
}
status_t
keyboard_freecookie(void *cookie)
{
return B_OK;
}
status_t
keyboard_read(void *cookie, off_t pos, void *buffer, size_t *_length)
{
TRACE(("keyboard read()\n"));
*_length = 0;
return B_NOT_ALLOWED;
}
status_t
keyboard_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
{
TRACE(("keyboard write()\n"));
*_length = 0;
return B_NOT_ALLOWED;
}
status_t
keyboard_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{
TRACE(("keyboard ioctl()\n"));
switch (op) {
case KB_READ:
TRACE(("KB_READ\n"));
return read_keyboard_packet((at_kbd_io *)buffer);
case KB_SET_LEDS:
{
led_info info;
if (user_memcpy(&info, buffer, sizeof(led_info)) < B_OK)
return B_BAD_ADDRESS;
TRACE(("KB_SET_LEDS\n"));
return set_leds(&info);
}
case KB_SET_KEY_REPEATING:
case KB_SET_KEY_NONREPEATING:
TRACE(("ps2_hid: ioctl 0x%lx not implemented yet, returning B_OK\n", op));
return B_OK;
default:
TRACE(("ps2_hid: invalid ioctl 0x%lx\n", op));
return EINVAL;
}
}

View File

@ -0,0 +1,452 @@
/*
* Copyright 2001-2005 Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* PS/2 mouse device driver
*
* Authors (in chronological order):
* Elad Lahav (elad@eldarshany.com)
* Stefano Ceccherini (burton666@libero.it)
* Axel Dörfler, axeld@pinc-software.de
*/
/*
* 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:
* ~~~~
* A packet read from the mouse data port is composed of
* three bytes:
* byte 0: status byte, where
* - bit 7: Y overflow (1 = true)
* - bit 6: X overflow (1 = true)
* - bit 5: MSB of Y offset
* - bit 4: MSB of X offset
* - bit 3: Syncronization bit (always 1)
* - bit 2: Middle button (1 = down)
* - bit 1: Right button (1 = down)
* - bit 0: 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)
*
* Intellimouse mice send a four byte packet, where the first three
* bytes are the same as standard mice, and the last one reports the
* Z position, which is, usually, the wheel movement.
*
* 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.
* This is all done inside install_io_interrupt_handler(), no need to
* worry about it anymore
* 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.
*/
#include <Drivers.h>
#include <string.h>
#include "kb_mouse_driver.h"
#include "common.h"
#include "packet_buffer.h"
#define MOUSE_HISTORY_SIZE 256
// we record that many mouse packets before we start to drop them
static sem_id sMouseSem;
static struct packet_buffer *sMouseBuffer;
static int32 sOpenMask;
static bigtime_t sLastClickTime;
static bigtime_t sClickSpeed;
static int32 sClickCount;
static int sButtonsState;
static size_t sPacketSize;
static size_t sPacketIndex;
static uint8 sPacketBuffer[PS2_MAX_PACKET_SIZE];
static status_t
ps2_reset_mouse()
{
uint8 read;
status_t status;
TRACE(("ps2_reset_mouse()\n"));
status = ps2_mouse_command(PS2_CMD_RESET_MOUSE, NULL, 0, &read, 1);
TRACE(("reset mouse: status 0x%08x, data 0x%02x\n", status, read));
return B_OK;
}
/** Set sampling rate of the ps2 port.
*/
static status_t
ps2_set_sample_rate(uint8 rate)
{
int32 tries = 5;
while (--tries > 0) {
status_t status = ps2_mouse_command(PS2_CMD_SET_SAMPLE_RATE, &rate, 1, NULL, 0);
if (status == B_OK)
return B_OK;
}
return B_ERROR;
}
/** Converts a packet received by the mouse to a "movement".
*/
static void
ps2_packet_to_movement(uint8 packet[], mouse_movement *pos)
{
int buttons = packet[0] & 7;
int xDelta = ((packet[0] & 0x10) ? 0xFFFFFF00 : 0) | packet[1];
int yDelta = ((packet[0] & 0x20) ? 0xFFFFFF00 : 0) | packet[2];
bigtime_t currentTime = system_time();
int8 wheel_ydelta = 0;
int8 wheel_xdelta = 0;
if (buttons != 0 && sButtonsState == 0) {
if (sLastClickTime + sClickSpeed > currentTime)
sClickCount++;
else
sClickCount = 1;
}
sLastClickTime = currentTime;
sButtonsState = buttons;
if (sPacketSize == PS2_PACKET_INTELLIMOUSE) {
switch (packet[3] & 0x0F) {
case 0x01: wheel_ydelta = +1; break; // wheel 1 down
case 0x0F: wheel_ydelta = -1; break; // wheel 1 up
case 0x02: wheel_xdelta = +1; break; // wheel 2 down
case 0x0E: wheel_xdelta = -1; break; // wheel 2 up
}
}
// dprintf("packet: %02x %02x %02x %02x: xd %d, yd %d, 0x%x (%d), w-xd %d, w-yd %d\n",
// packet[0], packet[1], packet[2], packet[3],
// xDelta, yDelta, buttons, sClickCount, wheel_xdelta, wheel_ydelta);
if (pos) {
pos->xdelta = xDelta;
pos->ydelta = yDelta;
pos->buttons = buttons;
pos->clicks = sClickCount;
pos->modifiers = 0;
pos->timestamp = currentTime;
pos->wheel_ydelta = (int)wheel_ydelta;
pos->wheel_xdelta = (int)wheel_xdelta;
TRACE(("xdelta: %d, ydelta: %d, buttons %x, clicks: %ld, timestamp %Ld\n",
xDelta, yDelta, buttons, sClickCount, currentTime));
}
}
/** Read a mouse event from the mouse events chain buffer.
*/
static status_t
mouse_read_event(mouse_movement *userMovement)
{
uint8 packet[PS2_MAX_PACKET_SIZE];
mouse_movement movement;
status_t status;
TRACE(("mouse_read_event()\n"));
status = acquire_sem_etc(sMouseSem, 1, B_CAN_INTERRUPT, 0);
if (status < B_OK)
return status;
if (packet_buffer_read(sMouseBuffer, packet, sPacketSize) != sPacketSize) {
TRACE(("error copying buffer\n"));
return B_ERROR;
}
if (!(packet[0] & 8))
panic("ps2_hid: got broken data from packet_buffer_read\n");
ps2_packet_to_movement(packet, &movement);
return user_memcpy(userMovement, &movement, sizeof(mouse_movement));
}
/** Enables or disables mouse reporting for the PS/2 port.
*/
static status_t
set_mouse_enabled(bool enable)
{
int32 tries = 5;
while (true) {
if (ps2_mouse_command(enable ? PS2_CMD_ENABLE_MOUSE : PS2_CMD_DISABLE_MOUSE, NULL, 0, NULL, 0) == B_OK)
return B_OK;
if (--tries <= 0)
break;
}
return B_ERROR;
}
/** Interrupt handler for the mouse device. Called whenever the I/O
* controller generates an interrupt for the PS/2 mouse. Reads mouse
* information from the data port, and stores it, so it can be accessed
* by read() operations. The full data is obtained using 3 consecutive
* calls to the handler, each holds a different byte on the data port.
*/
int32 mouse_handle_int(uint8 data)
{
if (atomic_and(&sOpenMask, 1) == 0)
return B_HANDLED_INTERRUPT;
if (sPacketIndex == 0 && !(data & 8)) {
TRACE(("bad mouse data, trying resync\n"));
return B_HANDLED_INTERRUPT;
}
sPacketBuffer[sPacketIndex++] = data;
if (sPacketIndex != sPacketSize) {
// packet not yet complete
return B_HANDLED_INTERRUPT;
}
// complete packet is assembled
sPacketIndex = 0;
if (packet_buffer_write(sMouseBuffer, sPacketBuffer, sPacketSize) != sPacketSize) {
// buffer is full, drop new data
return B_HANDLED_INTERRUPT;
}
release_sem_etc(sMouseSem, 1, B_DO_NOT_RESCHEDULE);
return B_INVOKE_SCHEDULER;
}
// #pragma mark -
status_t
probe_mouse(size_t *probed_packet_size)
{
uint8 deviceId = 0;
// get device id
ps2_mouse_command(PS2_CMD_GET_DEVICE_ID, NULL, 0, &deviceId, 1);
TRACE(("probe_mouse(): device id: %2x\n", deviceId));
if (deviceId == 0) {
int32 tries = 5;
while (--tries > 0) {
// try to switch to intellimouse mode
if (ps2_set_sample_rate(200) == B_OK
&& ps2_set_sample_rate(100) == B_OK
&& ps2_set_sample_rate(80) == B_OK) {
// get device id, again
ps2_mouse_command(PS2_CMD_GET_DEVICE_ID, NULL, 0, &deviceId, 1);
break;
}
}
}
if (deviceId == PS2_DEV_ID_STANDARD) {
TRACE(("Standard PS/2 mouse found\n"));
if (probed_packet_size)
*probed_packet_size = PS2_PACKET_STANDARD;
} else if (deviceId == PS2_DEV_ID_INTELLIMOUSE) {
TRACE(("Extended PS/2 mouse found\n"));
if (probed_packet_size)
*probed_packet_size = PS2_PACKET_INTELLIMOUSE;
} else {
// Something's wrong. Better quit
return B_ERROR;
}
return B_OK;
}
// #pragma mark -
// Device functions
status_t
mouse_open(const char *name, uint32 flags, void **_cookie)
{
status_t status;
int8 commandByte;
TRACE(("mouse_open()\n"));
if (atomic_or(&sOpenMask, 1) != 0)
return B_BUSY;
acquire_sem(gDeviceOpenSemaphore);
status = probe_mouse(&sPacketSize);
if (status != B_OK)
goto err1;
sPacketIndex = 0;
sMouseBuffer = create_packet_buffer(MOUSE_HISTORY_SIZE * sPacketSize);
if (sMouseBuffer == NULL) {
TRACE(("can't allocate mouse actions buffer\n"));
status = B_NO_MEMORY;
goto err2;
}
// create the mouse semaphore, used for synchronization between
// the interrupt handler and the read operation
sMouseSem = create_sem(0, "ps2_mouse_sem");
if (sMouseSem < 0) {
TRACE(("failed creating PS/2 mouse semaphore!\n"));
status = sMouseSem;
goto err3;
}
*_cookie = NULL;
status = set_mouse_enabled(true);
if (status < B_OK) {
TRACE(("mouse_open(): cannot enable PS/2 mouse\n"));
goto err4;
}
release_sem(gDeviceOpenSemaphore);
TRACE(("mouse_open(): mouse succesfully enabled\n"));
return B_OK;
err4:
delete_sem(sMouseSem);
err3:
delete_packet_buffer(sMouseBuffer);
err2:
err1:
atomic_and(&sOpenMask, 0);
release_sem(gDeviceOpenSemaphore);
return status;
}
status_t
mouse_close(void *cookie)
{
TRACE(("mouse_close()\n"));
set_mouse_enabled(false);
delete_packet_buffer(sMouseBuffer);
delete_sem(sMouseSem);
atomic_and(&sOpenMask, 0);
return B_OK;
}
status_t
mouse_freecookie(void *cookie)
{
return B_OK;
}
status_t
mouse_read(void *cookie, off_t pos, void *buffer, size_t *_length)
{
*_length = 0;
return B_NOT_ALLOWED;
}
status_t
mouse_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
{
*_length = 0;
return B_NOT_ALLOWED;
}
status_t
mouse_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{
switch (op) {
case MS_NUM_EVENTS:
{
int32 count;
TRACE(("MS_NUM_EVENTS\n"));
get_sem_count(sMouseSem, &count);
return count;
}
case MS_READ:
TRACE(("MS_READ\n"));
return mouse_read_event((mouse_movement *)buffer);
case MS_SET_TYPE:
TRACE(("MS_SET_TYPE not implemented\n"));
return B_BAD_VALUE;
case MS_SET_MAP:
TRACE(("MS_SET_MAP (set mouse mapping) not implemented\n"));
return B_BAD_VALUE;
case MS_GET_ACCEL:
TRACE(("MS_GET_ACCEL (get mouse acceleration) not implemented\n"));
return B_BAD_VALUE;
case MS_SET_ACCEL:
TRACE(("MS_SET_ACCEL (set mouse acceleration) not implemented\n"));
return B_BAD_VALUE;
case MS_SET_CLICKSPEED:
TRACE(("MS_SETCLICK (set click speed)\n"));
return user_memcpy(&sClickSpeed, buffer, sizeof(bigtime_t));
default:
TRACE(("unknown opcode: %ld\n", op));
return B_BAD_VALUE;
}
}

View File

@ -0,0 +1,178 @@
/*
* Copyright 2005 Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* PS/2 hid device driver
*
* Authors (in chronological order):
* Marcus Overhagen (marcus@overhagen.de)
*/
#include "ps2_service.h"
#include "packet_buffer.h"
static sem_id sServiceSem;
static thread_id sServiceThread;
static volatile bool sServiceTerminate;
static packet_buffer * sServiceCmdBuffer;
#define PS2_SERVICE_INTERVAL 200000
typedef struct
{
uint32 id;
ps2_dev * dev;
} ps2_service_cmd;
enum
{
PS2_SERVICE_HANDLE_DEVICE_ADDED = 1,
PS2_SERVICE_HANDLE_DEVICE_REMOVED,
};
void
ps2_service_handle_device_added(ps2_dev *dev)
{
ps2_service_cmd cmd;
TRACE(("ps2_service_handle_device_added %s\n", dev->name));
cmd.id = PS2_SERVICE_HANDLE_DEVICE_ADDED;
cmd.dev = dev;
packet_buffer_write(sServiceCmdBuffer, (const uint8 *)&cmd, sizeof(cmd));
release_sem_etc(sServiceSem, 1, B_DO_NOT_RESCHEDULE);
TRACE(("ps2_service_handle_device_added done\n"));
}
void
ps2_service_handle_device_removed(ps2_dev *dev)
{
ps2_service_cmd cmd;
TRACE(("ps2_service_handle_device_removed %s\n", dev->name));
cmd.id = PS2_SERVICE_HANDLE_DEVICE_REMOVED;
cmd.dev = dev;
packet_buffer_write(sServiceCmdBuffer, (const uint8 *)&cmd, sizeof(cmd));
release_sem_etc(sServiceSem, 1, B_DO_NOT_RESCHEDULE);
}
static status_t
ps2_service_probe_device(ps2_dev *dev)
{
uint8 d;
status_t res;
TRACE(("ps2_service_probe_device %s\n", dev->name));
if (dev->flags & PS2_FLAG_KEYB) {
res = ps2_command(0xae, NULL, 0, NULL, 0);
dprintf("KBD enable: res 0x%08x\n", res);
res = ps2_command(0xab, NULL, 0, &d, 1);
dprintf("KBD test: res 0x%08x, d 0x%02x\n", res, d);
} else {
res = ps2_command(0xa8, NULL, 0, NULL, 0);
dprintf("AUX enable: res 0x%08x\n", res);
res = ps2_command(0xa9, NULL, 0, &d, 1);
dprintf("AUX test: res 0x%08x, d 0x%02x\n", res, d);
}
return B_OK;
}
static int32
ps2_service_thread(void *arg)
{
TRACE(("ps2_service_thread started\n"));
for (;;) {
status_t status;
status = acquire_sem_etc(sServiceSem, 1, B_RELATIVE_TIMEOUT, PS2_SERVICE_INTERVAL);
if (sServiceTerminate)
break;
if (status == B_OK) {
// process service commands
ps2_service_cmd cmd;
TRACE(("ps2_service_thread: reading cmd\n"));
packet_buffer_read(sServiceCmdBuffer, (uint8 *)&cmd, sizeof(cmd));
switch (cmd.id) {
case PS2_SERVICE_HANDLE_DEVICE_ADDED:
TRACE(("PS2_SERVICE_HANDLE_DEVICE_ADDED %s\n", cmd.dev->name));
if (ps2_service_probe_device(cmd.dev) == B_OK)
ps2_dev_publish(cmd.dev);
break;
case PS2_SERVICE_HANDLE_DEVICE_REMOVED:
TRACE(("PS2_SERVICE_HANDLE_DEVICE_REMOVED %s\n", cmd.dev->name));
ps2_dev_unpublish(cmd.dev);
break;
default:
TRACE(("PS2_SERVICE: unknown id %lu\n", cmd.id));
break;
}
} else if (status == B_TIMED_OUT) {
// do periodic processing
} else {
dprintf("ps2_service_thread: Error, status 0x%08x, terminating\n", status);
break;
}
}
return 0;
}
status_t
ps2_service_init(void)
{
TRACE(("ps2_service_init\n"));
sServiceCmdBuffer = create_packet_buffer(sizeof(ps2_service_cmd) * 50);
if (sServiceCmdBuffer == NULL)
goto err1;
sServiceSem = create_sem(0, "ps2 service");
if (sServiceSem < B_OK)
goto err2;
sServiceThread = spawn_kernel_thread(ps2_service_thread, "ps2 service", 20, NULL);
if (sServiceThread < B_OK)
goto err3;
sServiceTerminate = false;
resume_thread(sServiceThread);
TRACE(("ps2_service_init done\n"));
return B_OK;
err3:
delete_sem(sServiceSem);
err2:
delete_packet_buffer(sServiceCmdBuffer);
err1:
TRACE(("ps2_service_init failed\n"));
return B_ERROR;
}
void
ps2_service_exit(void)
{
TRACE(("ps2_service_exit\n"));
sServiceTerminate = true;
release_sem(sServiceSem);
wait_for_thread(sServiceThread, NULL);
delete_sem(sServiceSem);
delete_packet_buffer(sServiceCmdBuffer);
}