The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
device.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file device.h
5 * @brief generic device model: a named/typed handle plus a registry.
6 *
7 * drivers wrap their concrete device in a device_t and register it here during
8 * init, so the rest of the kernel has one place to enumerate what hardware is
9 * present. this is the groundwork for a future devfs that surfaces registered
10 * devices as /dev/hda, /dev/tty0, and so on.
11 *
12 * the model intentionally stays thin: it does not replace the driver-specific
13 * init flow (which has its own ordering), it just records the result.
14 */
15
16#include "common/stdint.h"
17#include "arch/arch.h"
18#include "common/error.h"
19
20// broad device classes, enough to group the registry today.
21typedef enum
22{
24 DEVICE_TYPE_BLOCK, // block storage (disk, partition)
25 DEVICE_TYPE_CHAR, // character stream (tty, serial)
26 DEVICE_TYPE_INPUT, // input source (keyboard)
27 DEVICE_TYPE_TIMER, // timer/clock source
29
30// longest device name the registry stores (including the null terminator).
31#define DEVICE_NAME_MAX 16
32
33struct device;
34typedef struct device device_t;
35struct file;
36
37/**
38 * optional per-device operations. all fields may be NULL; the model does not
39 * require any of them. concrete device families extend behaviour through their
40 * own interfaces (block_device_t, tty_t, ...) and use this only for lifecycle.
41 */
42typedef struct
43{
44 void (*shutdown)(device_t *dev); // quiesce the device (optional)
45
46 /**
47 * device-specific control operation, the transport for ioctl(2)-style
48 * requests (e.g. the framebuffer's FBIOGET_VSCREENINFO). optional: a NULL
49 * hook means the device supports no ioctls.
50 * @param dev the device.
51 * @param request driver-defined request code.
52 * @param arg request-specific argument (often a user pointer, already
53 * bounds-checked by the syscall layer before it reaches here).
54 * @return 0 on success, or a negative errno on failure (-ENOTTY for an
55 * unrecognized request).
56 */
57 int32_t (*ioctl)(device_t *dev, uint32_t request, void *arg);
58
59 /**
60 * map the device's memory into a process address space (optional; NULL
61 * means the device is not mappable, i.e. -ENODEV). the device maps its own
62 * frames at @p virt in @p as; the caller (SYS_mmap) has already
63 * reserved the virtual range and validated it. eager mapping, no demand
64 * paging. the frames are device memory (outside PMM), so process teardown
65 * leaves them alone.
66 * @param dev the device.
67 * @param as the target process's address space.
68 * @param virt page-aligned destination virtual address.
69 * @param length number of bytes to map (page-multiple).
70 * @param prot PROT_* protection bits.
71 * @param offset byte offset into the device memory (page-aligned).
72 * @return 0 on success, or a negative errno on failure.
73 */
75 size_t length, int prot, uint64_t offset);
76
77 /**
78 * byte-oriented read at an explicit offset (optional; NULL means the device
79 * is not readable). the caller (devfs) owns the cursor and passes it in.
80 * @return number of bytes read (may be short at end of device).
81 */
82 uint32_t (*read)(device_t *dev, uint64_t offset, void *buf, uint32_t len);
83
84 /**
85 * byte-oriented write at an explicit offset (optional; NULL means the
86 * device is not writable).
87 * @return number of bytes written.
88 */
89 uint32_t (*write)(device_t *dev, uint64_t offset, const void *buf, uint32_t len);
90
91 /**
92 * total byte size of the device's addressable memory (optional; NULL means
93 * unknown/zero). used for read clamping and lseek(SEEK_END).
94 */
96 /**
97 * open the device as a stream of its own (optional). when set, open(2)
98 * of the /dev node gives the file this hook makes, with the device's
99 * own file operations (blocking, O_NONBLOCK, poll, a release when the
100 * last descriptor closes), and read, write and ioctl above are not
101 * used for it.
102 * @param dev the device.
103 * @param flags the O_ access mode and status flags of the open.
104 * @param out receives the new open-file description.
105 * @return KERR_OK, or an error (KERR_BUSY if the device is in use).
106 */
107 kerr_t (*open)(device_t *dev, uint32_t flags, struct file **out);
109
110/**
111 * a registered device.
112 */
113struct device
114{
115 char name[DEVICE_NAME_MAX]; // stable name, e.g. "hda", "tty0", "kbd"
116 device_type_t type; // device class
117 const device_ops_t *ops; // optional lifecycle ops (may be NULL)
118 void *driver_data; // the concrete device the driver owns
119 uint32_t number; // the device number st_rdev shows (DEVICE_NUMBER), or 0
120};
121
122/**
123 * a device number from its major and minor numbers, packed as Linux and
124 * the libc's makedev (sys/sysmacros.h) pack them. ginnOS uses Linux's
125 * numbers for the terminals: tty0 4:0, ptmx 5:2, pts/N 136:N.
126 */
127#define DEVICE_NUMBER(major, minor) \
128 ((((major) & 0xfffu) << 8) | ((minor) & 0xffu) | (((minor) & 0xfff00u) << 12))
129
130/**
131 * reset the device registry. call once during early boot.
132 */
133void device_registry_init(void);
134
135/**
136 * register a device. the device_t is owned by the caller and must outlive its
137 * registration (drivers use static or long-lived storage).
138 * @param dev the device to add.
139 * @return true on success, false if the registry is full or dev is invalid.
140 */
141bool device_register(device_t *dev);
142
143/**
144 * number of currently registered devices.
145 */
147
148/**
149 * get a registered device by index (0 .. device_count()-1).
150 * @return the device, or NULL if the index is out of range.
151 */
153
154/**
155 * find a registered device by name.
156 * @return the device, or NULL if no device has that name.
157 */
158device_t *device_find(const char *name);
uintptr_t addr_space_t
an address space handle.
Definition arch.h:105
void device_registry_init(void)
reset the device registry.
Definition device.c:40
device_t * device_find(const char *name)
find a registered device by name.
Definition device.c:94
uint32_t device_count(void)
number of currently registered devices.
Definition device.c:72
bool device_register(device_t *dev)
register a device.
Definition device.c:50
device_t * device_get(uint32_t index)
get a registered device by index (0 .
Definition device.c:80
#define DEVICE_NAME_MAX
Definition device.h:31
struct device device_t
Definition device.h:34
device_type_t
Definition device.h:22
@ DEVICE_TYPE_UNKNOWN
Definition device.h:23
@ DEVICE_TYPE_CHAR
Definition device.h:25
@ DEVICE_TYPE_INPUT
Definition device.h:26
@ DEVICE_TYPE_TIMER
Definition device.h:27
@ DEVICE_TYPE_BLOCK
Definition device.h:24
kerr_t
unified kernel error codes.
Definition error.h:14
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
int int32_t
Definition stdint.h:14
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
optional per-device operations.
Definition device.h:43
kerr_t(*) open(device_t *dev, uint32_t flags, struct file **out)
open the device as a stream of its own (optional).
Definition device.h:107
void(*) shutdown(device_t *dev)
Definition device.h:44
int32_t(*) ioctl(device_t *dev, uint32_t request, void *arg)
device-specific control operation, the transport for ioctl(2)-style requests (e.g.
Definition device.h:57
uint32_t(*) read(device_t *dev, uint64_t offset, void *buf, uint32_t len)
byte-oriented read at an explicit offset (optional; NULL means the device is not readable).
Definition device.h:82
uint64_t(*) size(device_t *dev)
total byte size of the device's addressable memory (optional; NULL means unknown/zero).
Definition device.h:95
uint32_t(*) write(device_t *dev, uint64_t offset, const void *buf, uint32_t len)
byte-oriented write at an explicit offset (optional; NULL means the device is not writable).
Definition device.h:89
int32_t(*) mmap(device_t *dev, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
map the device's memory into a process address space (optional; NULL means the device is not mappable...
Definition device.h:74
a registered device.
Definition device.h:114
const device_ops_t * ops
Definition device.h:117
char name[DEVICE_NAME_MAX]
Definition device.h:115
uint32_t number
Definition device.h:119
device_type_t type
Definition device.h:116
void * driver_data
Definition device.h:118
an open-file description.
Definition file.h:64