m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/char.c
blob: cdf402903f246bed03eda8487cbec669e1808ee6 (plain)
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
196
197
198
199
200
#include "char.h"

#include <linux/anon_inodes.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

#include "doomdev.h"
#include "harddoom.h"
#include "util.h"
#include "pci.h"
#include "surface.h"

#define DOOMDEV_COUNT 256
#define DOOMDEV_NAME "doom"

dev_t first;
int major;
bool minors[DOOMDEV_COUNT];

long do_new_surface(struct file *filp, unsigned long arg)
{
	struct doomdev_ioctl_create_surface param;
	int not_copied;
	int err;

	not_copied = copy_from_user(&param, (void __iomem *) arg, sizeof(param));
	if (not_copied) {
		err = -EFAULT;
		goto error_param;
	}

	err = new_surface(filp, &param);

error_param:
	return err;
}

long do_new_texture(struct file *filp, unsigned long arg)
{
	struct doomdev_ioctl_create_texture param;
	int not_copied;
	int err;

	not_copied = copy_from_user(&param, (void __iomem *) arg, sizeof(param));
	if (not_copied) {
		err = -EFAULT;
		goto error_param;
	}

	err = new_texture(filp, &param);

error_param:
	return err;
}

long do_new_flat(struct file *filp, unsigned long arg)
{
	struct doomdev_ioctl_create_flat param;
	int not_copied;
	int err;

	not_copied = copy_from_user(&param, (void __iomem *) arg, sizeof(param));
	if (not_copied) {
		err = -EFAULT;
		goto error_param;
	}

	err = new_flat(filp, &param);

error_param:
	return err;
}

long do_new_colors(struct file *filp, unsigned long arg)
{
	struct doomdev_ioctl_create_colormaps param;
	int not_copied;
	int err;

	not_copied = copy_from_user(&param, (void __iomem *) arg, sizeof(param));
	if (not_copied) {
		err = -EFAULT;
		goto error_param;
	}

	err = new_colors(filp, &param);

error_param:
	return err;
}

long doom_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case DOOMDEV_IOCTL_CREATE_SURFACE:
		return do_new_surface(filp, arg);
	case DOOMDEV_IOCTL_CREATE_TEXTURE:
		return do_new_texture(filp, arg);
	case DOOMDEV_IOCTL_CREATE_FLAT:
		return do_new_flat(filp, arg);
	case DOOMDEV_IOCTL_CREATE_COLORMAPS:
		return do_new_colors(filp, arg);
	default:
		return -ENOTTY;
	}
}

int doom_open(struct inode *inode, struct file *filp)
{
	return 0;
}

struct file_operations doomdev_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = doom_ioctl,
	.compat_ioctl = doom_ioctl,
	.open = doom_open
};

struct class *doom_class;

int alloc_minor(void) {
	int i;

	for (i = 0; i < DOOMDEV_COUNT; i++) {
		if (!minors[i]) {
			minors[i] = true;
			return i;
		}
	}

	return -EOVERFLOW;
}

void free_minor(int minor) {
	minors[minor] = false;
}

int new_doomdev(struct pci_dev *dev)
{
	int err = 0;
	int minor;
	struct doom_data *doom_data;
	dev_t devt;

	doom_data = pci_get_drvdata(dev);

	cdev_init(&doom_data->cdev, &doomdev_fops);
	ORFAIL(cdev_add(&doom_data->cdev, first, 1), error_add);

	minor = alloc_minor();
	ORFAIL(minor, error_minor);
	devt = MKDEV(major, minor);
	doom_data->device = device_create(doom_class, &dev->dev, devt, NULL,
				"doom%d", minor);
	ORFAIL_PTR(doom_data->device, error_create);

	return 0;

error_create:
	free_minor(minor);
error_minor:
	cdev_del(&doom_data->cdev);
error_add:
	return err;
}

void destroy_doomdev(struct doom_data *doom_data)
{
	free_minor(MINOR(doom_data->device->devt));
	device_destroy(doom_class, doom_data->device->devt);
	cdev_del(&doom_data->cdev);
}

int char_init(void)
{
	int err = 0;

	ORFAIL(alloc_chrdev_region(&first, 0, DOOMDEV_COUNT, DOOMDEV_NAME),
			error_region);
	major = MAJOR(first);
	doom_class = class_create(THIS_MODULE, "doom");
	ORFAIL_PTR(doom_class, error_create);

	return 0;

error_create:
	unregister_chrdev_region(first, DOOMDEV_COUNT);
error_region:
	return err;
}

void char_cleanup(void)
{
	unregister_chrdev_region(first, DOOMDEV_COUNT);
	class_unregister(doom_class);
	class_destroy(doom_class);
}