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
|
#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) {
p("some bytes not copied\n");
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_texture", &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;
}
long doom_create_colormaps(struct file *filp, unsigned long arg)
{
return -1;
}
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);
}
|