PatchworkOS  3984a1d
A non-POSIX operating system.
Loading...
Searching...
No Matches
ps2_kbd.c
Go to the documentation of this file.
1#include "ps2_kbd.h"
2#include "ps2_scanmap.h"
3
4#include <kernel/cpu/irq.h>
5#include <kernel/log/log.h>
6
7#include <stdlib.h>
8
10{
11 ps2_kbd_t* kbd = data->private;
12
14 if (response == ERR)
15 {
16 return;
17 }
18
19 switch (response)
20 {
24 LOG_ERR("unexpected PS/2 keyboard response: %d\n", response);
25 return;
27 kbd->flags |= PS2_KBD_EXTENDED;
28 return;
30 kbd->flags |= PS2_KBD_RELEASE;
31 return;
32 default:
33 break;
34 }
35
38
39 if (kbd->flags & PS2_KBD_RELEASE)
40 {
41 kbd_release(kbd->kbd, code);
42 }
43 else
44 {
45 kbd_press(kbd->kbd, code);
46 }
47
48 kbd->flags = PS2_KBD_NONE;
49}
50
52{
54 {
55 LOG_ERR("failed to set PS/2 keyboard scan code set\n");
56 return ERR;
57 }
58
59 ps2_kbd_t* kbd = malloc(sizeof(ps2_kbd_t));
60 if (kbd == NULL)
61 {
62 LOG_ERR("failed to allocate memory for PS/2 keyboard data\n");
63 return ERR;
64 }
65 kbd->flags = PS2_KBD_NONE;
66 kbd->kbd = kbd_new(info->name);
67 if (kbd->kbd == NULL)
68 {
69 free(kbd);
70 LOG_ERR("failed to create PS/2 keyboard\n");
71 return ERR;
72 }
73
74 info->private = kbd;
75 return 0;
76}
77
79{
80 ps2_kbd_t* kbd = info->private;
81 if (kbd == NULL)
82 {
83 LOG_ERR("PS/2 keyboard data is NULL during IRQ registration\n");
84 errno = EINVAL;
85 return ERR;
86 }
87
89 {
90 LOG_ERR("failed to register PS/2 keyboard IRQ handler\n");
91 return ERR;
92 }
93
94 return 0;
95}
96
98{
99 if (info->private == NULL)
100 {
101 return;
102 }
103
104 ps2_kbd_t* kbd = info->private;
105
107 kbd_free(kbd->kbd);
108 free(kbd);
109
110 info->private = NULL;
111}
int64_t y
Definition main.c:153
static fd_t kbd
Definition dwm.c:23
static fd_t data
Definition dwm.c:21
void irq_handler_unregister(irq_func_t func, irq_virt_t virt)
Unregister an IRQ handler.
Definition irq.c:474
uint64_t irq_handler_register(irq_virt_t virt, irq_func_t func, void *private)
Register an IRQ handler for a virtual IRQ.
Definition irq.c:425
void kbd_release(kbd_t *kbd, keycode_t code)
Push a keyboard release event to the keyboard event queue.
Definition kbd.c:270
void kbd_press(kbd_t *kbd, keycode_t code)
Push a keyboard press event to the keyboard event queue.
Definition kbd.c:252
void kbd_free(kbd_t *kbd)
Frees a keyboard.
Definition kbd.c:225
kbd_t * kbd_new(const char *name)
Allocate and initialize a new keyboard.
Definition kbd.c:150
#define LOG_ERR(format,...)
Definition log.h:93
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
keycode_t
Keyboard keycode type.
Definition kbd.h:29
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t ps2_kbd_init(ps2_device_info_t *info)
Initialize a PS/2 keyboard device.
Definition ps2_kbd.c:51
void ps2_kbd_deinit(ps2_device_info_t *info)
Deinitialize a PS/2 keyboard device.
Definition ps2_kbd.c:97
uint64_t ps2_kbd_irq_register(ps2_device_info_t *info)
Register the IRQ handler for a PS/2 keyboard device.
Definition ps2_kbd.c:78
@ PS2_KBD_NONE
Definition ps2_kbd.h:21
@ PS2_KBD_EXTENDED
Definition ps2_kbd.h:22
@ PS2_KBD_RELEASE
Definition ps2_kbd.h:23
#define PS2_SCAN_CODE_SET
PS/2 scan code set to use.
Definition ps2_scanmap.h:18
keycode_t ps2_scancode_to_keycode(ps2_scancode_t scancode, bool isExtended)
Convert a PS/2 scancode to a generic keycode.
uint8_t ps2_scancode_t
PS/2 scancode.
Definition ps2_scanmap.h:23
uint64_t ps2_read_no_wait(void)
Read from the PS/2 controllers data port without waiting.
Definition ps2.c:375
uint64_t ps2_device_sub_cmd(ps2_device_t device, ps2_device_cmd_t command, uint8_t subCommand)
Send a command and a subcommand to a PS/2 device.
Definition ps2.c:539
@ PS2_DEV_CMD_SET_SCANCODE_SET
Definition ps2.h:116
@ PS2_DEV_RESPONSE_RESEND
Definition ps2.h:181
@ PS2_DEV_RESPONSE_ACK
Definition ps2.h:180
@ PS2_DEV_RESPONSE_BAT_OK
Definition ps2.h:182
@ PS2_DEV_RESPONSE_KBD_EXTENDED
Indicates that the following byte is an extended scancode.
Definition ps2.h:183
@ PS2_DEV_RESPONSE_KBD_RELEASE
Indicates that the following byte is a key release code.
Definition ps2.h:184
static void ps2_kbd_irq(irq_func_data_t *data)
Definition ps2_kbd.c:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
Data passed to IRQ functions.
Definition irq.h:64
PS/2 device information structure.
Definition ps2.h:202
irq_virt_t irq
IRQ assigned to the device by ACPI.
Definition ps2.h:206
ps2_device_t device
Device port.
Definition ps2.h:203
const char * name
Human-readable name of the device.
Definition ps2.h:205
void * private
Driver-specific private data.
Definition ps2.h:209
PS/2 keyboard private data.
Definition ps2_kbd.h:31