1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
* cyber - Add CYBER to your system
* Copyright (C) 2017 Felix Morgner
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cyber.h"
#include "cyber_device.h"
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
/*
* UDEV
*/
typedef struct device *device_ptr;
typedef struct device const *const_device_ptr;
typedef struct kobj_uevent_env *uevent_env_ptr;
typedef struct kobj_uevent_env const *const_uevent_env_ptr;
static int _handle_uevent(const_device_ptr, uevent_env_ptr environment)
{
return add_uevent_var(environment, "DEVMODE=%#o", 0666);
}
/*
* Device Class
*/
typedef struct class *class_ptr;
typedef struct class const *const_class_ptr;
typedef struct class_attribute *attribute_ptr;
typedef struct class_attribute const *const_attribute_ptr;
/// Get the amount of available cyber.
static ssize_t available_show(const_class_ptr, const_attribute_ptr, char * __user output_buffer)
{
return sprintf(output_buffer, "infinite\n");
}
/// Get the storage technology in use.
static ssize_t storage_technology_show(const_class_ptr, const_attribute_ptr, char * __user output_buffer)
{
return sprintf(output_buffer, "condensed di-hydrogen-monoxide\n");
}
static CLASS_ATTR_RO(available);
static CLASS_ATTR_RO(storage_technology);
static int _init_device_class(cyber_device * device)
{
int error = 0;
device->class.name = CLS_NAME;
device->class.dev_uevent = _handle_uevent;
if((error = class_register(&device->class)))
{
printk(KERN_ALERT DEV_NAME ": Failed to register CYBER device class!\n");
return error;
}
if((error = class_create_file(&device->class, &class_attr_available)) || (error = class_create_file(&device->class, &class_attr_storage_technology)))
{
printk(KERN_ALERT DEV_NAME ": Failed to register sysfs CYBER attributes!\n");
return error;
}
return 0;
}
static void _shutdown_device_class(cyber_device * device)
{
class_unregister(&device->class);
}
/// Character Device
typedef struct file_operations *file_operations_ptr;
typedef struct file_operations const *const_file_operations_ptr;
static int _init_character_device(cyber_device * device, const_file_operations_ptr file_ops)
{
int error = 0;
if((error = alloc_chrdev_region(&device->number, 0, 1, DEV_NAME)))
{
printk(KERN_ALERT DEV_NAME ": Failed to allocate character device!\n");
}
cdev_init(&device->character, file_ops);
device->character.owner = THIS_MODULE;
device->character.ops = file_ops;
if((error = cdev_add(&device->character, device->number, 1)))
{
printk(KERN_ALERT DEV_NAME ": Failed to add character device (Error: %d)!\n", error);
}
return error;
}
static void _shutdown_character_device(cyber_device * device)
{
cdev_del(&device->character);
unregister_chrdev_region(device->number, 1);
}
/// Kernel Device
static void _close_kernel_device(struct device * device)
{
printk(KERN_INFO DEV_NAME ": Closing kernel CYBER device\n");
}
static int _init_kernel_device(cyber_device * device)
{
int error = 0;
device->kernel.class = &device->class;
device->kernel.devt = device->number;
device->kernel.init_name = DEV_NAME;
device->kernel.release = _close_kernel_device;
if((error = device_register(&device->kernel)))
{
printk(KERN_ALERT DEV_NAME ": Failed to create CYBER device!\n");
}
return error;
}
static void _shutdown_kernel_device(cyber_device * device)
{
device_unregister(&device->kernel);
}
/// Device
int cyber_device_init(cyber_device * device, const_file_operations_ptr file_ops)
{
int error = 0;
printk(KERN_INFO DEV_NAME ": Initializing CYBER device\n");
if((error = _init_character_device(device, file_ops)))
{
return error;
}
if((error = _init_device_class(device)))
{
_shutdown_character_device(device);
return error;
}
if((error = _init_kernel_device(device)))
{
_shutdown_character_device(device);
_shutdown_device_class(device);
return error;
}
printk(KERN_INFO DEV_NAME ": New CYBER device with major %d minor %d\n", MAJOR(device->number), MINOR(device->number));
return error;
}
void cyber_device_shutdown(cyber_device * device)
{
printk(KERN_INFO DEV_NAME ": Shutting down CYBER device\n");
_shutdown_kernel_device(device);
_shutdown_device_class(device);
_shutdown_character_device(device);
}
void cyber_device_release(struct device * device)
{
printk(KERN_INFO DEV_NAME ": Closing kernel CYBER device\n");
}
|