My Kernel v0.1.0
device.h
1#pragma once
2
51#pragma once
52
53#include <kernel/error.h>
54#include <kernel/types.h>
55
56#include <libalgo/linked_list.h>
57
58#include <stddef.h>
59
60struct file_operations;
61
62typedef struct device_driver driver_t;
63
78typedef struct device {
79
80 node_t this;
81
82 const char *name;
84
93 const struct file_operations *fops;
94 struct vnode *vnode;
95
96} device_t;
97
103error_t device_register(device_t *);
104
106struct device *device_find(const char *name);
107
109struct file *device_open(device_t *);
110
112static inline void device_set_name(struct device *dev, const char *name)
113{
114 dev->name = name;
115}
116
118static inline const char *device_name(struct device *dev)
119{
120 return dev->name;
121}
122
134#define generate_device_rw_functions(_pfx, _dev_type, _reg_field, _off_type) \
135 __device_read(u8, b, _pfx, _dev_type, _reg_field, _off_type) \
136 __device_write(u8, b, _pfx, _dev_type, _reg_field, _off_type) \
137 __device_read(u16, w, _pfx, _dev_type, _reg_field, _off_type) \
138 __device_write(u16, w, _pfx, _dev_type, _reg_field, _off_type) \
139 __device_read(u32, l, _pfx, _dev_type, _reg_field, \
140 _off_type) \
141 __device_write(u32, l, _pfx, _dev_type, _reg_field, \
142 _off_type)
143
144#define __device_read(_type, _type_pfx, _pfx, _device_type, _device_reg_field, \
145 _offset_type) \
146 static MAYBE_UNUSED inline _type _pfx##_read##_type_pfx( \
147 _device_type *device, _offset_type offset) \
148 { \
149 return *(_type *)(device->_device_reg_field + offset); \
150 }
151
152#define __device_write(_type, _type_pfx, _pfx, _device_type, \
153 _device_reg_field, _offset_type) \
154 static MAYBE_UNUSED inline void _pfx##_write##_type_pfx( \
155 _device_type *device, _offset_type offset, _type val) \
156 { \
157 *(_type *)(device->_device_reg_field + offset) = val; \
158 }
159
static void device_set_name(struct device *dev, const char *name)
Set the name of the device.
Definition: device.h:112
struct file * device_open(device_t *)
Open a device for interacting with it.
Definition: device.c:58
static const char * device_name(struct device *dev)
Definition: device.h:118
error_t device_register(device_t *)
Register a new device.
Definition: device.c:25
struct device * device_find(const char *name)
Find a registered device by name.
Definition: device.c:66
The basic device driver structure.
Definition: driver.h:77
Represents a device inside the kernel.
Definition: device.h:78
const char * name
The name of the device.
Definition: device.h:82
struct vnode * vnode
This device's vnode, used by the VFS.
Definition: device.h:94
driver_t * driver
The driver for this device.
Definition: device.h:83
const struct file_operations * fops
Operations applied to the device's underlying file.
Definition: device.h:93
Operations that can be performed on an opened file.
Definition: file.h:69
Opened file description.
Definition: file.h:29
Intrusive doubly-linked list node.
Definition: linked_list.h:27
represents a single virtual node
Definition: vfs.h:205