|
1 | 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
2 |
| -* * |
| 2 | + * * |
3 | 3 | * Ghost, a micro-kernel based operating system for the x86 architecture *
|
4 | 4 | * Copyright (C) 2025, Max Schlüssel <[email protected]> *
|
5 | 5 | * *
|
|
18 | 18 | * *
|
19 | 19 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
20 | 20 |
|
| 21 | +#include "manager.hpp" |
21 | 22 | #include <ghost.h>
|
22 | 23 | #include <libpci/driver.hpp>
|
| 24 | +#include <libdevice/interface.hpp> |
23 | 25 | #include <cstdio>
|
| 26 | +#include <unordered_map> |
24 | 27 |
|
25 |
| -// TODO: |
26 |
| -// This is the device manager that is responsible to keep track of devices in |
27 |
| -// the system, give each an ID and manage access to them. It also starts the |
28 |
| -// right drivers once it knows which devices exist. |
| 28 | +void _deviceManagerCheckPciDevices(); |
| 29 | +void _deviceManagerAwaitCommands(); |
| 30 | +void _deviceManagerHandleRegisterDevice(g_tid sender, g_message_transaction tx, |
| 31 | + g_device_manager_register_device_request* content); |
29 | 32 |
|
30 |
| -void deviceManagerCheckPciDevices(); |
| 33 | +static g_user_mutex devicesLock = g_mutex_initialize_r(true); |
| 34 | +static std::unordered_map<g_device_id, device_t*> devices; |
| 35 | +static g_device_id nextDeviceId = 1; |
31 | 36 |
|
32 | 37 | int main()
|
33 | 38 | {
|
34 |
| - deviceManagerCheckPciDevices(); |
| 39 | + g_tid comHandler = g_create_task((void*) _deviceManagerAwaitCommands); |
| 40 | + _deviceManagerCheckPciDevices(); |
| 41 | + |
| 42 | + g_join(comHandler); |
35 | 43 | }
|
36 | 44 |
|
37 |
| -void deviceManagerCheckPciDevices() |
| 45 | +void _deviceManagerCheckPciDevices() |
38 | 46 | {
|
39 | 47 | int num;
|
40 | 48 | g_pci_device_data* devices;
|
@@ -85,3 +93,57 @@ void deviceManagerCheckPciDevices()
|
85 | 93 | g_spawn("/applications/vbedriver.bin", "", "", G_SECURITY_LEVEL_DRIVER);
|
86 | 94 | }
|
87 | 95 | }
|
| 96 | + |
| 97 | +void _deviceManagerAwaitCommands() |
| 98 | +{ |
| 99 | + if(!g_task_register_name(G_DEVICE_MANAGER_NAME)) |
| 100 | + { |
| 101 | + klog("failed to register as %s", G_DEVICE_MANAGER_NAME); |
| 102 | + g_exit(-1); |
| 103 | + } |
| 104 | + |
| 105 | + size_t bufLen = 1024; |
| 106 | + uint8_t buf[bufLen]; |
| 107 | + |
| 108 | + while(true) |
| 109 | + { |
| 110 | + auto status = g_receive_message(buf, bufLen); |
| 111 | + if(status != G_MESSAGE_RECEIVE_STATUS_SUCCESSFUL) |
| 112 | + continue; |
| 113 | + auto message = (g_message_header*) buf; |
| 114 | + auto content = (g_device_manager_header*) G_MESSAGE_CONTENT(message); |
| 115 | + |
| 116 | + if(content->command == G_DEVICE_MANAGER_REGISTER_DEVICE) |
| 117 | + { |
| 118 | + _deviceManagerHandleRegisterDevice(message->sender, message->transaction, |
| 119 | + (g_device_manager_register_device_request*) content); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void _deviceManagerHandleRegisterDevice(g_tid sender, g_message_transaction tx, |
| 125 | + g_device_manager_register_device_request* content) |
| 126 | +{ |
| 127 | + g_mutex_acquire(devicesLock); |
| 128 | + auto id = nextDeviceId++; |
| 129 | + auto device = new device_t(); |
| 130 | + device->id = id; |
| 131 | + device->handler = content->handler; |
| 132 | + device->type = content->type; |
| 133 | + devices[id] = device; |
| 134 | + g_mutex_release(devicesLock); |
| 135 | + |
| 136 | + // Respond to registerer |
| 137 | + g_device_manager_register_device_response response{}; |
| 138 | + response.status = G_DEVICE_MANAGER_SUCCESS; |
| 139 | + response.id = id; |
| 140 | + g_send_message_t(sender, &response, sizeof(response), tx); |
| 141 | + |
| 142 | + // Post to topic |
| 143 | + g_device_event_device_registered event{}; |
| 144 | + event.header.event = G_DEVICE_EVENT_DEVICE_REGISTERED; |
| 145 | + event.id = device->id; |
| 146 | + event.type = content->type; |
| 147 | + event.driver = device->handler; |
| 148 | + g_send_topic_message(G_DEVICE_EVENT_TOPIC, &event, sizeof(event)); |
| 149 | +} |
0 commit comments