Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
dev.c
Go to the documentation of this file.
1// Copyright (c) 2025 Mohammad Arfan
2
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#include "vfs/dev.h"
22#include <hash.h>
23#include <libk/serial.h>
24#include <memory/memory_utils.h>
26#include <memory/slab.h>
27#include <str.h>
28#include <type.h>
29
31static struct slab_cache* block_device_cache = 0;
32
36
38
40 auto bitmap = major_map[major].minor_bitmap;
41 for (int32_t b = 0; b < DEV_MINOR_BITMAP_COUNT; b++) {
42 if (bitmap[b] == 0xFF)
43 continue;
44
45 for (int32_t i = 0; i < 8; i++) {
46 if (bitmap[b] & (1 << i))
47 continue;
48
49 bitmap[b] |= (1 << i);
50 return b * 8 + i;
51 }
52 }
53 return -1;
54}
55
56// static void free_minor(uint32_t major, uint32_t minor) {
57// uint8_t* bitmap = major_map[major].minor_bitmap;
58// bitmap[minor / 8] &= ~(1 << (minor % 8));
59// }
60
61
65 sizeof(cdev_t), 0, 0);
66
68 if (!cdev)
69 return 0;
70
71 cdev->major = major;
72 auto minor = alloc_minor(major);
73 if (minor < 0) {
75 return 0;
76 }
78 cdev->ops = ops;
79
80 auto curr = &dev_chain;
81 while (*curr)
82 curr = &(*curr)->next;
83 *curr = cdev;
84
85 return cdev;
86}
87
89 cdev_ptr_t curr = dev_chain;
90 while (curr) {
91 if (curr->major == major && curr->minor == minor)
92 return curr;
93 curr = curr->next;
94 }
95 return 0;
96}
static cdev_ptr_t dev_chain
Definition dev.c:30
static int32_t alloc_minor(uint32_t major)
Definition dev.c:39
static struct minor_map major_map[256]
Definition dev.c:37
static struct slab_cache * block_device_cache
Definition dev.c:31
#define DEV_MINOR_BITMAP_COUNT
Definition dev.h:27
cdev_ptr_t create_dev(void *ops, uint32_t major)
uint32_t minor
Definition dev.h:1
#define DEV_MAJOR_MAX_COUNT
Definition dev.h:26
uint32_t major
Definition dev.h:0
cdev_ptr_t retrieve_dev(uint32_t major, uint32_t minor)
cdev_t * cdev_ptr_t
Definition dev.h:70
void * ops
Definition dev.h:2
void * vxSlabAlloc(struct slab_cache *cache)
Definition slab.c:93
void slab_free(struct slab_cache *cache, void *obj)
Definition slab.c:235
void vxCreateSlabCache(struct slab_cache **cache, const char *name, const size_t obj_size, size_t alignment, const uintptr_t virt_addr)
Definition slab.c:44
Definition dev.h:63
void * ops
Definition dev.h:66
uint32_t major
Definition dev.h:64
uint32_t minor
Definition dev.h:65
uint8_t minor_bitmap[32]
Definition dev.c:34
unsigned int uint32_t
Definition type.h:19
#define KERNEL_API
Definition type.h:93
signed int int32_t
Definition type.h:43
unsigned char uint8_t
Definition type.h:7