Skip to content

Commit

Permalink
pc-tty: add PS/2 mouse support and kbd/mouse vdevs
Browse files Browse the repository at this point in the history
JIRA: RTOS-896
  • Loading branch information
adamgreloch committed Aug 21, 2024
1 parent 040859c commit f0abac0
Show file tree
Hide file tree
Showing 9 changed files with 764 additions and 211 deletions.
5 changes: 5 additions & 0 deletions tty/pc-tty/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@
NAME := pc-tty
SRCS := $(wildcard tty/pc-tty/*.c)
DEP_LIBS := libtty libklog

ifneq ($(HAS_MOUSE), y)
SRCS := $(filter-out tty/pc-tty/ttypc_mouse.c, $(SRCS))
endif

include $(binary.mk)
130 changes: 130 additions & 0 deletions tty/pc-tty/event_queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include <sys/threads.h>

#include <fcntl.h>

#include "event_queue.h"

#define min(a, b) ({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _b : _a; \
})


struct _event_queue_t {
unsigned char *buf;
unsigned int count;
unsigned int r;
unsigned int w;
unsigned int sz;
handle_t mutex;
handle_t waitq;
};


int event_queue_init(event_queue_t **res, unsigned int sz)
{
if (sz == 0)
return EINVAL;

int err;
event_queue_t *eq = malloc(sizeof(event_queue_t));
if (eq == NULL) {
return -1;
}

memset(eq, 0, sizeof(event_queue_t));

eq->buf = malloc(sizeof(unsigned char) * sz);
if (eq->buf == NULL) {
return -1;
}

eq->sz = sz;

if ((err = mutexCreate(&eq->mutex)) < 0 || (err = condCreate(&eq->waitq)) < 0) {
free(eq->buf);
free(eq);
return err;
}

*res = eq;

return EOK;
}


void event_queue_put(event_queue_t *eq, unsigned char event, unsigned int min_size, unsigned int flags)
{
mutexLock(eq->mutex);
if (eq->count < eq->sz) {
eq->buf[eq->w] = event;
eq->w = (eq->w + 1) % eq->sz;
eq->count++;
if (eq->count >= min_size)
condSignal(eq->waitq);
} // else: discard (or handle blocking variant... TODO)
mutexUnlock(eq->mutex);
}


int event_queue_get(event_queue_t *eq, void *dest, unsigned int size, unsigned int flags)
{
int err;

mutexLock(eq->mutex);
do {
if (flags & O_NONBLOCK && eq->count < size) {
err = -EWOULDBLOCK;
break;
}

while (eq->count < size)
condWait(eq->waitq, eq->mutex, 0);

unsigned int bytes;

if (eq->w < eq->r)
memcpy(dest, eq->buf + eq->r, bytes = min(eq->count, eq->w - eq->r));
else {
memcpy(dest, eq->buf + eq->r, bytes = min(size, eq->sz - eq->r));

if (bytes < size) {
size -= bytes;
memcpy(dest + bytes, eq->buf, min(size, eq->w));
bytes += min(size, eq->w);
}
}

eq->r = (eq->r + bytes) % eq->sz;
eq->count -= bytes;

err = bytes;
} while (0);

mutexUnlock(eq->mutex);

return err;
}

unsigned int event_queue_count(event_queue_t *eq)
{
unsigned int res;
mutexLock(eq->mutex);
res = eq->count;
mutexUnlock(eq->mutex);
return res;
}


void event_queue_destroy(event_queue_t *eq)
{
resourceDestroy(eq->mutex);
resourceDestroy(eq->waitq);
free(eq->buf);
free(eq);
}
18 changes: 18 additions & 0 deletions tty/pc-tty/event_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _EVENT_QUEUE_H_
#define _EVENT_QUEUE_H_

#include <sys/types.h>

typedef struct _event_queue_t event_queue_t;

int event_queue_init(event_queue_t **res, unsigned int sz);

void event_queue_put(event_queue_t *eq, unsigned char event, unsigned int min_size, unsigned int flags);

int event_queue_get(event_queue_t *eq, void *dest, unsigned int size, unsigned int flags);

unsigned int event_queue_count(event_queue_t *eq);

void event_queue_destroy(event_queue_t *eq);

#endif
65 changes: 39 additions & 26 deletions tty/pc-tty/ttypc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,74 @@
#include <sys/types.h>

#include "ttypc_vt.h"
#include "event_queue.h"


/* Number of virtual terminals */
#define NVTS 4


/* Keyboard types */
enum { KBD_BIOS, KBD_PS2 };
enum { KBD_BIOS,
KBD_PS2 };


/* Keyboard key types */
enum {
KB_NONE = 0x0000,
KB_CTL = 0x0001,
KB_SHIFT = 0x0002,
KB_ALT = 0x0004,
KB_ALTGR = 0x0008,
KB_SCROLL = 0x0010,
KB_NUM = 0x0020,
KB_CAPS = 0x0040,
KB_FUNC = 0x0080,
KB_ASCII = 0x0100,
KB_KP = 0x0200,
KB_EXT = 0x0400
KB_NONE = 0x0000,
KB_CTL = 0x0001,
KB_SHIFT = 0x0002,
KB_ALT = 0x0004,
KB_ALTGR = 0x0008,
KB_SCROLL = 0x0010,
KB_NUM = 0x0020,
KB_CAPS = 0x0040,
KB_FUNC = 0x0080,
KB_ASCII = 0x0100,
KB_KP = 0x0200,
KB_EXT = 0x0400
};


struct _ttypc_t {
unsigned int port; /* Driver port */
unsigned int port; /* Driver port */

/* VGA */
volatile void *vga; /* VGA screen memory */
void *crtc; /* Video Display Controller (CRTC) */
unsigned color; /* Color support */
volatile void *vga; /* VGA screen memory */
void *crtc; /* Video Display Controller (CRTC) */
unsigned color; /* Color support */

/* KBD */
volatile void *kbd; /* Keyboard controller */
unsigned char ktype; /* Keyboard type */
unsigned char lockst; /* Lock keys state */
unsigned char shiftst; /* Shift keys state */
unsigned int kirq; /* Interrupt number */
handle_t klock; /* Interrupt mutex */
handle_t kcond; /* Interrupt condition variable */
handle_t kinth; /* Interrupt handle */
handle_t kmcond; /* Kbd/mouse interrupt condition variable */

unsigned int kirq; /* Kbd interrupt number */
handle_t klock; /* Kbd interrupt mutex */
handle_t kinth; /* Kbd interrupt handle */
event_queue_t *keq; /* Kbd event buffer */
unsigned int kport; /* Kbd device port */

unsigned int mirq; /* Mouse interrupt number */
handle_t minth; /* Mouse interrupt handle */
event_queue_t *meq; /* Mouse event buffer */
unsigned int mport; /* Mouse device port */

/* Virtual terminals */
ttypc_vt_t *vt; /* Active virtual terminal */
ttypc_vt_t vts[NVTS]; /* Virtual Terminals */
ttypc_vt_t *vt; /* Active virtual terminal */
ttypc_vt_t vts[NVTS]; /* Virtual Terminals */

/* Synchronization */
handle_t lock; /* Access mutex */
handle_t lock; /* Access mutex */

/* Thread stacks */
char kstack[2048] __attribute__ ((aligned(8)));
char pstack[2048] __attribute__ ((aligned(8)));
char kstack[2048] __attribute__((aligned(8)));
char pstack[2048] __attribute__((aligned(8)));

char kpstack[1024] __attribute__((aligned(8)));
char mpstack[1024] __attribute__((aligned(8)));
};


Expand Down
Loading

0 comments on commit f0abac0

Please sign in to comment.