Update the console cursor position code to reflect the changes.
[monolithium.git] / kernel / include / device.h
1 /*
2  * device.h
3  * 
4  * Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
5  * 
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _DEVICE_H_
21 #define _DEVICE_H_
22
23 #include <common.h>
24 #include <sync.h>
25 #include <list.h>
26 #include <cache.h>
27 #include <filesystem.h>
28
29 #define MAX_DEVICE_NAME 32
30
31 #define BLOCK_DEVICE_REMOVABLE_MEDIA  (1 << 0)
32
33 #define IOCTL_BLOCK_DEV_INFO        0xB0000000
34 #define IOCTL_BLOCK_DEV_MEDIA_INFO  0xB0000001
35 #define IOCTL_BLOCK_DEV_MEDIA_LOAD  0xB0000002
36 #define IOCTL_BLOCK_DEV_MEDIA_EJECT 0xB0000003
37
38 #define IOCTL_CHAR_DEV_CHECK_INPUT  0xC0000000
39 #define IOCTL_CHAR_DEV_CHECK_OUTPUT 0xC0000001
40
41 #ifndef DEVICE_TYPEDEF
42 #define DEVICE_TYPEDEF
43 typedef struct device device_t;
44 #endif
45
46 typedef dword_t (*device_init_proc_t)(void);
47 typedef dword_t (*device_cleanup_proc_t)(void);
48 typedef dword_t (*device_ioctl_proc_t)(
49     device_t *device,
50     dword_t control_code,
51     const void *in_buffer,
52     size_t in_length,
53     void *out_buffer,
54     size_t out_length
55 );
56
57 typedef dword_t (*block_dev_read_proc_t)(device_t *device, void *buffer, qword_t offset, size_t length, size_t *bytes_read);
58 typedef dword_t (*block_dev_write_proc_t)(device_t *device, const void *buffer, qword_t offset, size_t length, size_t *bytes_written);
59
60 typedef dword_t (*char_dev_read_proc_t)(device_t *device, void *buffer, size_t length, size_t *bytes_read);
61 typedef dword_t (*char_dev_write_proc_t)(device_t *device, const void *buffer, size_t length, size_t *bytes_written);
62
63 typedef enum
64 {
65     BLOCK_DEVICE,
66     CHAR_DEVICE
67 } device_type_t;
68
69 typedef struct
70 {
71     list_entry_t list;
72     dword_t mounted_devices;
73     device_init_proc_t init_proc;
74     device_cleanup_proc_t cleanup_proc;
75     block_dev_read_proc_t read_proc;
76     block_dev_write_proc_t write_proc;
77     device_ioctl_proc_t ioctl_proc;
78 } block_dev_driver_t;
79
80 typedef struct
81 {
82     list_entry_t list;
83     device_init_proc_t init_proc;
84     device_cleanup_proc_t cleanup_proc;
85     char_dev_read_proc_t read_proc;
86     char_dev_write_proc_t write_proc;
87     device_ioctl_proc_t ioctl_proc;
88 } char_dev_driver_t;
89
90 typedef struct device
91 {
92     list_entry_t list;
93     device_type_t type;
94     char name[MAX_DEVICE_NAME];
95     resource_t resource;
96     dword_t flags;
97     qword_t capacity;
98
99     union
100     {
101         void *driver;
102         block_dev_driver_t *block_driver;
103         char_dev_driver_t *char_driver;
104     };
105 } device_t;
106
107 typedef struct
108 {
109     dword_t heads;
110     dword_t tracks;
111     dword_t sectors_per_track;
112     dword_t bytes_per_sector;
113 } block_dev_media_info_t;
114
115 typedef struct
116 {
117     file_t header;
118     device_t *device;
119 } device_file_t;
120
121 typedef struct
122 {
123     file_instance_t header;
124     device_t *next_device;
125 } device_dir_inst_t;
126
127 device_t *get_block_device(const char *name);
128 dword_t register_block_dev_driver(block_dev_driver_t *driver);
129 dword_t unregister_block_dev_driver(block_dev_driver_t *driver);
130 dword_t register_block_device(device_t *device);
131 dword_t unregister_block_device(device_t *device);
132 device_t *get_char_device(const char *name);
133 dword_t register_char_dev_driver(char_dev_driver_t *driver);
134 dword_t unregister_char_dev_driver(char_dev_driver_t *driver);
135 dword_t register_char_device(device_t *device);
136 dword_t unregister_char_device(device_t *device);
137 dword_t device_read(device_t *device, void *buffer, qword_t offset, size_t length, size_t *bytes_read);
138 dword_t device_write(device_t *device, const void *buffer, qword_t offset, size_t length, size_t *bytes_written);
139 dword_t device_ioctl_internal(device_t *device, dword_t control_code, const void *in_buffer, size_t in_length, void *out_buffer, size_t out_length);
140 dword_t device_ioctl(handle_t device, dword_t control_code, const void *in_buffer, size_t in_length, void *out_buffer, size_t out_length);
141 void device_init(void);
142
143 #endif