|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 ukoOS Contributors |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef UKO_OS_KERNEL__DEVICE_H |
| 8 | +#define UKO_OS_KERNEL__DEVICE_H 1 |
| 9 | + |
| 10 | +#include <list.h> |
| 11 | +#include <print.h> |
| 12 | + |
| 13 | +/** |
| 14 | + * The kind of device. These should be statically allocated. |
| 15 | + */ |
| 16 | +struct device_class { |
| 17 | + /** |
| 18 | + * The name of the device class. This should be a short name suitable as a |
| 19 | + * directory name. |
| 20 | + */ |
| 21 | + const char *name; |
| 22 | + |
| 23 | + /** |
| 24 | + * A short description of the device class in prose. |
| 25 | + */ |
| 26 | + const char *description; |
| 27 | + |
| 28 | + /** |
| 29 | + * The `list_head` for this class's entry in the `device_classes` list. |
| 30 | + */ |
| 31 | + struct list_head device_classes; |
| 32 | + |
| 33 | + /** |
| 34 | + * The list of `struct device`s that have this class. |
| 35 | + */ |
| 36 | + struct list_head members; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Defines a device class and creates an initializer to register it. |
| 41 | + */ |
| 42 | +#define DEFINE_DEVICE_CLASS(IDENTIFIER, NAME, DESCRIPTION) \ |
| 43 | + struct device_class IDENTIFIER; \ |
| 44 | + DEFINE_INIT(-20) { register_device_class(&IDENTIFIER); } \ |
| 45 | + struct device_class IDENTIFIER = { \ |
| 46 | + .name = (NAME), \ |
| 47 | + .description = (DESCRIPTION), \ |
| 48 | + .device_classes = LIST_INIT((IDENTIFIER).device_classes), \ |
| 49 | + .members = LIST_INIT((IDENTIFIER).members), \ |
| 50 | + } |
| 51 | + |
| 52 | +/** |
| 53 | + * The list of registered `struct device_class`es. |
| 54 | + */ |
| 55 | +extern struct list_head device_classes; |
| 56 | + |
| 57 | +/** |
| 58 | + * Registers a device class. |
| 59 | + * |
| 60 | + * - `name` and `description` must be filled out. |
| 61 | + * - `device_classes` and `members` must be self-linked. |
| 62 | + */ |
| 63 | +[[gnu::nonnull(1)]] |
| 64 | +void register_device_class(struct device_class *device_class); |
| 65 | + |
| 66 | +/** |
| 67 | + * A node in the tree of devices attached to the system. |
| 68 | + */ |
| 69 | +struct device { |
| 70 | + /** |
| 71 | + * The name of the device. This must be unique amongst its siblings. |
| 72 | + * |
| 73 | + * This should be `alloc`-allocated. |
| 74 | + */ |
| 75 | + char *name; |
| 76 | + |
| 77 | + /** |
| 78 | + * The parent of the node. `nullptr` for the root node. |
| 79 | + */ |
| 80 | + struct device *parent; |
| 81 | + |
| 82 | + /** |
| 83 | + * The `list_head` for this device's entry in `parent->children`. |
| 84 | + */ |
| 85 | + struct list_head parent_children; |
| 86 | + |
| 87 | + /** |
| 88 | + * The children of the node. |
| 89 | + */ |
| 90 | + struct list_head children; |
| 91 | + |
| 92 | + /** |
| 93 | + * The kind of device this is. |
| 94 | + */ |
| 95 | + struct device_class *device_class; |
| 96 | + |
| 97 | + /** |
| 98 | + * The `list_head` for this device's entry in `device_class->members`. |
| 99 | + */ |
| 100 | + struct list_head device_class_members; |
| 101 | +}; |
| 102 | + |
| 103 | +/** |
| 104 | + * Returns an initializer for the `struct device` `DEVICE`. |
| 105 | + * |
| 106 | + * ### Example |
| 107 | + * |
| 108 | + * ```c |
| 109 | + * struct my_device { |
| 110 | + * // ... |
| 111 | + * struct device device; |
| 112 | + * // ... |
| 113 | + * }; |
| 114 | + * |
| 115 | + * struct my_device *my_device = alloc(sizeof(*my_device)); |
| 116 | + * my_device->device = DEVICE_INIT(my_device->device, "example"); |
| 117 | + * ``` |
| 118 | + */ |
| 119 | +#define DEVICE_INIT(DEVICE, NAME_FORMAT, ...) \ |
| 120 | + (struct device) { \ |
| 121 | + .name = format(NAME_FORMAT __VA_OPT__(, ) __VA_ARGS__), .parent = nullptr, \ |
| 122 | + .parent_children = LIST_INIT((DEVICE).parent_children), \ |
| 123 | + .children = LIST_INIT((DEVICE).children), .device_class = nullptr, \ |
| 124 | + .device_class_members = LIST_INIT((DEVICE).device_class_members), \ |
| 125 | + } |
| 126 | + |
| 127 | +/** |
| 128 | + * The root of the tree of devices. |
| 129 | + */ |
| 130 | +extern struct device root_device; |
| 131 | + |
| 132 | +/** |
| 133 | + * Registers a device in the tree of devices and with its device class. |
| 134 | + * |
| 135 | + * - `name` must be filled out. |
| 136 | + * - `parent` and `device_class` must be `nullptr`. |
| 137 | + * - `parent_children`, `children`, and `device_class_members` must be |
| 138 | + * self-linked. |
| 139 | + */ |
| 140 | +[[gnu::nonnull(1, 2, 3)]] |
| 141 | +void register_device(struct device *parent, struct device_class *device_class, |
| 142 | + struct device *child); |
| 143 | + |
| 144 | +/** |
| 145 | + * Prints the tree of devices. |
| 146 | + */ |
| 147 | +void print_devices(void); |
| 148 | + |
| 149 | +#endif // UKO_OS_KERNEL__DEVICE_H |
0 commit comments