m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/char.c
blob: bb70d1b98f42731d85d06828862223be1b1e8310 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#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;
int next_minor = 0;

long doom_create_surface(struct file *filp, unsigned long arg)
{
	struct doomdev_ioctl_create_surface *params;

	params = (struct doomdev_ioctl_create_surface *)arg;
	return new_surface(filp, params);
}

void free_texture(struct texture_data *texture_data);
int texture_release(struct inode *inode, struct file *filp)
{
	struct texture_data *texture_data;

	texture_data = filp->private_data;

	free_texture(texture_data);

	kfree(texture_data);

	return 0;
}

struct file_operations texture_fops = {
	.owner = THIS_MODULE,
	.release = texture_release

};

int verify_texture_params(struct doomdev_ioctl_create_texture *params)
{
	if (params->size > 4 * 1024 * 1024) {
		return -EOVERFLOW;
	}

	if (params->height > 1023) {
		return -EOVERFLOW;
	}

	return 0;
}

int alloc_texture(struct doomdev_ioctl_create_texture *params,
		struct texture_data *texture_data)
{
	int err;
	int i;
	int not_written;
	int pages_needed;

	texture_data->size = params->size;
	texture_data->height = params->height;

	pages_needed = (params->size / HARDDOOM_PAGE_SIZE);
	if (params->size % HARDDOOM_PAGE_SIZE != 0) {
		pages_needed += 1;
	}

	texture_data->pages = pages_needed;
	texture_data->texture_cpu =
		dma_alloc_coherent(texture_data->doom_data->pci_device,
				params->size, &texture_data->texture_dev,
				GFP_KERNEL);
	ORFAIL_NULL(texture_data->texture_cpu, -ENOMEM, error_texture);

	texture_data->page_table_cpu =
		dma_alloc_coherent(texture_data->doom_data->pci_device,
				pages_needed * 4, &texture_data->page_table_dev,
				GFP_KERNEL);
	ORFAIL_NULL(texture_data->page_table_cpu, -ENOMEM, error_pt);

	for (i = 0; i < pages_needed; i++) {
		texture_data->page_table_cpu[i] =
			(HARDDOOM_PTE_PHYS_MASK &
			 (texture_data->texture_dev + HARDDOOM_PAGE_SIZE * i)) |
			HARDDOOM_PTE_VALID;
	}

	not_written = copy_from_user(texture_data->texture_cpu,
			(void __user *) params->data_ptr,
			params->size);

	if (not_written) {
		err = -EFAULT;
		goto error_copy;
	}

	return 0;

error_copy:
	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->pages * 4, texture_data->page_table_cpu,
			texture_data->page_table_dev);
error_pt:
	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->size, texture_data->texture_cpu,
			texture_data->texture_dev);
error_texture:
	return err;
}

void free_texture(struct texture_data *texture_data)
{
	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->pages * 4, texture_data->page_table_cpu,
			texture_data->page_table_dev);

	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->size, texture_data->texture_cpu,
			texture_data->texture_dev);
}

long doom_create_texture(struct file *filp, unsigned long arg)
{
	int err;
	struct doomdev_ioctl_create_texture *params;
	struct texture_data *texture_data;
	int fd;
	struct doom_data *doom_data;

	params = (struct doomdev_ioctl_create_texture *) arg;

	err = verify_texture_params(params);
	if (err < 0) {
		return err;
	}

	texture_data = kmalloc(sizeof(*texture_data), GFP_KERNEL);
	ORFAIL_NULL(texture_data, -ENOMEM, error_data);
	doom_data = container_of(filp->f_inode->i_cdev, struct doom_data, cdev);
	texture_data->doom_data = doom_data;

	ORFAIL(alloc_texture(params, texture_data), error_texture);

	fd = anon_inode_getfd("doom_texture", &texture_fops, texture_data, 0);
	ORFAIL(fd, error_inode);

	return fd;

error_inode:
	free_texture(texture_data);
error_texture:
	kfree(texture_data);
error_data:
	return err;
}

void free_flat(struct flat_data *flat_data);
int flat_release(struct inode *inode, struct file *filp)
{
	struct flat_data *flat_data;

	flat_data = filp->private_data;

	free_flat(flat_data);

	kfree(flat_data);

	return 0;
}

struct file_operations flat_fops = {
	.owner = THIS_MODULE,
	.release = flat_release

};

int alloc_flat(struct doomdev_ioctl_create_flat *params,
		struct flat_data *flat_data)
{
	int err;
	int not_written;

	flat_data->flat_cpu =
		dma_alloc_coherent(flat_data->doom_data->pci_device,
				HARDDOOM_FLAT_SIZE, &flat_data->flat_dev,
				GFP_KERNEL);
	ORFAIL_NULL(flat_data->flat_cpu, -ENOMEM, error_flat);

	not_written = copy_from_user(flat_data->flat_cpu,
			(void __user *) params->data_ptr, HARDDOOM_FLAT_SIZE);

	if (not_written) {
		err = -EFAULT;
		goto error_copy;
	}

	return 0;

error_copy:
	dma_free_coherent(flat_data->doom_data->pci_device, HARDDOOM_FLAT_SIZE,
			flat_data->flat_cpu, flat_data->flat_dev);
error_flat:
	return err;

}

void free_flat(struct flat_data *flat_data)
{
	dma_free_coherent(flat_data->doom_data->pci_device, HARDDOOM_FLAT_SIZE,
			flat_data->flat_cpu, flat_data->flat_dev);
}

long doom_create_flat(struct file *filp, unsigned long arg)
{
	int err;
	struct doomdev_ioctl_create_flat *params;
	struct flat_data *flat_data;
	int fd;
	struct doom_data *doom_data;

	params = (struct doomdev_ioctl_create_flat *) arg;

	flat_data = kmalloc(sizeof(*flat_data), GFP_KERNEL);
	ORFAIL_NULL(flat_data, -ENOMEM, error_data);
	doom_data = container_of(filp->f_inode->i_cdev, struct doom_data, cdev);
	flat_data->doom_data = doom_data;

	ORFAIL(alloc_flat(params, flat_data), error_flat);

	fd = anon_inode_getfd("doom_flat", &flat_fops, flat_data, 0);
	ORFAIL(fd, error_inode);

	return fd;

error_inode:
	free_flat(flat_data);
error_flat:
	kfree(flat_data);
error_data:
	return err;
}

void free_colors(struct colors_data *colors_data);
int colors_release(struct inode *inode, struct file *filp)
{
	struct colors_data *colors_data;

	colors_data = filp->private_data;

	free_colors(colors_data);

	kfree(colors_data);

	return 0;
}

struct file_operations colors_fops = {
	.owner = THIS_MODULE,
	.release = colors_release

};

int alloc_colors(struct doomdev_ioctl_create_colormaps *params,
		struct colors_data *colors_data)
{
	int err;
	int not_written;

	colors_data->number = params->num;

	colors_data->colors_cpu =
		dma_alloc_coherent(colors_data->doom_data->pci_device,
				HARDDOOM_COLORMAP_SIZE * params->num,
				&colors_data->colors_dev,
				GFP_KERNEL);
	ORFAIL_NULL(colors_data->colors_cpu, -ENOMEM, error_colors);

	not_written = copy_from_user(colors_data->colors_cpu,
			(void __user *) params->data_ptr,
			HARDDOOM_COLORMAP_SIZE * params->num);

	if (not_written) {
		err = -EFAULT;
		goto error_copy;
	}

	return 0;

error_copy:
	dma_free_coherent(colors_data->doom_data->pci_device,
			HARDDOOM_COLORMAP_SIZE * params->num,
			colors_data->colors_cpu, colors_data->colors_dev);
error_colors:
	return err;

}

void free_colors(struct colors_data *colors_data)
{
	dma_free_coherent(colors_data->doom_data->pci_device,
			HARDDOOM_COLORMAP_SIZE * colors_data->number,
			colors_data->colors_cpu, colors_data->colors_dev);
}

long doom_create_colormaps(struct file *filp, unsigned long arg)
{
	int err;
	struct doomdev_ioctl_create_colormaps *params;
	struct colors_data *colors_data;
	int fd;
	struct doom_data *doom_data;

	params = (struct doomdev_ioctl_create_colormaps *) arg;

	colors_data = kmalloc(sizeof(*colors_data), GFP_KERNEL);
	ORFAIL_NULL(colors_data, -ENOMEM, error_data);
	doom_data = container_of(filp->f_inode->i_cdev, struct doom_data, cdev);
	colors_data->doom_data = doom_data;

	ORFAIL(alloc_colors(params, colors_data), error_colors);

	fd = anon_inode_getfd("doom_colors", &colors_fops, colors_data, 0);
	ORFAIL(fd, error_inode);

	return fd;

error_inode:
	free_colors(colors_data);
error_colors:
	kfree(colors_data);
error_data:
	return err;
}

long doom_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case DOOMDEV_IOCTL_CREATE_SURFACE:
		return doom_create_surface(filp, arg);
	case DOOMDEV_IOCTL_CREATE_TEXTURE:
		return doom_create_texture(filp, arg);
	case DOOMDEV_IOCTL_CREATE_FLAT:
		return doom_create_flat(filp, arg);
	case DOOMDEV_IOCTL_CREATE_COLORMAPS:
		return doom_create_colormaps(filp, arg);
	default:
		return -EINVAL;
	}
}

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 new_doomdev(struct pci_dev *dev)
{
	int err = 0;
	int minor;
	struct doom_data *doom_data;
	dev_t devt;

	if (next_minor >= DOOMDEV_COUNT) {
		return -ENOMEM;
	}

	doom_data = pci_get_drvdata(dev);

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

	minor = next_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:
	cdev_del(&doom_data->cdev);
error_add:
	return err;
}

void destroy_doomdev(struct doom_data *doom_data)
{
	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);
}