-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmtcadummy_drv.c
756 lines (676 loc) · 25.2 KB
/
mtcadummy_drv.c
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#include "version.h"
#include "mtcadummy.h"
#include "mtcadummy_firmware.h"
#include "mtcadummy_proc.h"
#include "mtcadummy_rw_no_struct.h"
/*#include "mtcadrv_io.h" already done in mtcadummy.h*/
/* do we need ALL of these? */
#include <linux/device.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/version.h>
/*#include <linux/workqueue.h>*/
#include <asm/delay.h>
#include <linux/slab.h> /* kmalloc() */
#include "llrfdrv_io_compat.h"
#include "pcieuni_io_compat.h"
MODULE_AUTHOR("Martin Killenberg");
MODULE_DESCRIPTION("MTCA board dummy driver");
MODULE_VERSION(MTCADUMMY_MODULE_VERSION);
MODULE_LICENSE("Dual BSD/GPL");
/* 0 means automatic? */
int mtcaDummyMinorNr = 0;
int mtcaDummyMajorNr = 0;
struct class* mtcaDummyClass;
struct file_operations mtcaDummyFileOps;
struct file_operations llrfDummyFileOps;
struct file_operations noIoctlDummyFileOps;
struct file_operations pcieuniDummyFileOps;
DEFINE_MUTEX(controlMutex);
/* this is the part I want to avoid, or is it not avoidable?*/
mtcaDummyData dummyPrivateData[MTCADUMMY_NR_DEVS];
struct mtcaDummyControl controlData = {.data = dummyPrivateData,
.open_error = 0,
.read_error = 0,
.write_error = 0,
.spi_error = 0};
/* initialise the device when loading the driver */
static int __init mtcaDummy_init_module(void) {
int status;
dev_t dev;
int i, j;
dbg_print("%s\n", "MODULE INIT");
status = alloc_chrdev_region(&dev, mtcaDummyMinorNr, MTCADUMMY_NR_DEVS, MTCADUMMY_NAME);
if(status) {
dbg_print("Error in alloc_chrdev_region: %d\n", status);
goto err_alloc_chrdev_region;
}
mtcaDummyMajorNr = MAJOR(dev);
dbg_print("DEV MAJOR NR: %d\n", mtcaDummyMajorNr);
/* see linux kernel commit 1aaba11da9aa7d7d6b52a74d45b31cac118295a1 */
#if LINUX_VERSION_CODE > KERNEL_VERSION(6, 3, 0)
mtcaDummyClass = class_create(MTCADUMMY_NAME);
#else
mtcaDummyClass = class_create(THIS_MODULE, MTCADUMMY_NAME);
#endif
if(IS_ERR(mtcaDummyClass)) {
dbg_print("Error in class_create: %ld\n", PTR_ERR(mtcaDummyClass));
goto err_class_create;
}
memset(dummyPrivateData, 0, sizeof(dummyPrivateData));
for(i = 0; i < MTCADUMMY_NR_DEVS; i++) {
dev_t deviceNumber = MKDEV(mtcaDummyMajorNr, mtcaDummyMinorNr + i);
const char* deviceNameWithPlaceholder = NULL;
/* before we initialise the character device we have to initialise all the
* local variables and the mutex. These have to be in place before the
* device file becomes available. */
/* try to allocate memory, this might fail */
dummyPrivateData[i].systemBar = kmalloc(MTCADUMMY_N_REGISTERS * sizeof(u32), GFP_KERNEL);
if(dummyPrivateData[i].systemBar == NULL) {
goto err_allocate_systemBar;
}
memset(dummyPrivateData[i].systemBar, 0, MTCADUMMY_N_REGISTERS * sizeof(u32));
dummyPrivateData[i].dmaBar = kmalloc(MTCADUMMY_DMA_SIZE, GFP_KERNEL);
if(dummyPrivateData[i].dmaBar == NULL) {
goto err_allocate_dmaBar;
}
memset(dummyPrivateData[i].dmaBar, 0, MTCADUMMY_DMA_SIZE);
/* before initialising diver object run the "firmware" initialisation, i.e.
* set the allocated registers*/
mtcadummy_initialiseSystemBar(dummyPrivateData[i].systemBar);
mutex_init(&dummyPrivateData[i].devMutex);
atomic_set(&dummyPrivateData[i].inUse, 0);
dummyPrivateData[i].slotNr = i;
dummyPrivateData[i].minor = mtcaDummyMinorNr + i;
dummyPrivateData[i].major = mtcaDummyMajorNr;
/* small "hack" to have different ioctls for one device */
switch(i) {
case 4:
cdev_init(&dummyPrivateData[i].cdev, &llrfDummyFileOps);
dummyPrivateData[i].cdev.ops = &llrfDummyFileOps;
deviceNameWithPlaceholder = LLRFDUMMY_NAME "s%d";
break;
case 5:
cdev_init(&dummyPrivateData[i].cdev, &noIoctlDummyFileOps);
dummyPrivateData[i].cdev.ops = &noIoctlDummyFileOps;
deviceNameWithPlaceholder = NOIOCTLDUMMY_NAME "s%d";
break;
case 6:
cdev_init(&dummyPrivateData[i].cdev, &pcieuniDummyFileOps);
dummyPrivateData[i].cdev.ops = &pcieuniDummyFileOps;
deviceNameWithPlaceholder = PCIEUNIDUMMY_NAME "s%d";
break;
default:
cdev_init(&dummyPrivateData[i].cdev, &mtcaDummyFileOps);
dummyPrivateData[i].cdev.ops = &mtcaDummyFileOps;
deviceNameWithPlaceholder = MTCADUMMY_NAME "s%d";
}
dummyPrivateData[i].cdev.owner = THIS_MODULE;
status = cdev_add(&dummyPrivateData[i].cdev, deviceNumber, 1);
if(status) {
dbg_print("Error in cdev_add: %d\n", status);
goto err_cdev_init;
}
if(device_create(mtcaDummyClass, NULL, deviceNumber, NULL, deviceNameWithPlaceholder, i) == NULL) {
dbg_print("%s\n", "Error in device_create");
goto err_device_create;
}
}
mtcadummy_create_proc(&controlData);
dbg_print("%s\n", "MODULE INIT DONE");
return 0;
/* undo the steps for device i. As i is still at the same position where it
was when we jumped out of the loop, we can use it here. */
err_device_create:
cdev_del(&dummyPrivateData[i].cdev);
err_cdev_init:
mutex_destroy(&dummyPrivateData[i].devMutex);
kfree(dummyPrivateData[i].dmaBar);
err_allocate_dmaBar:
kfree(dummyPrivateData[i].systemBar);
err_allocate_systemBar:
/* Unroll all already registered device files and their mutexes and memory. */
/* As i still contains the position where the loop stopped we can use it here.
*/
for(j = 0; j < i; j++) {
device_destroy(mtcaDummyClass, MKDEV(mtcaDummyMajorNr, mtcaDummyMinorNr + j));
cdev_del(&dummyPrivateData[j].cdev);
mutex_destroy(&dummyPrivateData[j].devMutex);
kfree(dummyPrivateData[j].systemBar);
kfree(dummyPrivateData[j].dmaBar);
}
class_destroy(mtcaDummyClass);
err_class_create:
unregister_chrdev_region(MKDEV(mtcaDummyMajorNr, mtcaDummyMinorNr), MTCADUMMY_NR_DEVS);
err_alloc_chrdev_region:
return -1;
}
static void __exit mtcaDummy_cleanup_module(void) {
int i = 0;
dev_t deviceNumber;
dbg_print("%s\n", "MODULE CLEANUP");
mtcadummy_remove_proc();
deviceNumber = MKDEV(mtcaDummyMajorNr, mtcaDummyMinorNr);
for(i = 0; i < MTCADUMMY_NR_DEVS; i++) {
device_destroy(mtcaDummyClass, MKDEV(mtcaDummyMajorNr, mtcaDummyMinorNr + i));
cdev_del(&dummyPrivateData[i].cdev);
mutex_destroy(&dummyPrivateData[i].devMutex);
kfree(dummyPrivateData[i].systemBar);
kfree(dummyPrivateData[i].dmaBar);
}
class_destroy(mtcaDummyClass);
unregister_chrdev_region(deviceNumber, MTCADUMMY_NR_DEVS);
}
static int mtcaDummy_open(struct inode* inode, struct file* filp) {
bool openFail;
mtcaDummyData* privData;
if (mutex_lock_interruptible(&controlMutex) != 0) {
dbg_print("%s", "Failed to acquire control mutex while opening file...");
return -ERESTARTSYS;
}
openFail = controlData.open_error;
mutex_unlock(&controlMutex);
if (openFail) {
return -EINVAL;
}
privData = container_of(inode->i_cdev, mtcaDummyData, cdev);
filp->private_data = privData;
// count up inUse
atomic_inc(&privData->inUse);
dbg_print("DEVICE IN SLOT: %d OPENED BY: %s PID: %d\n", privData->slotNr, current->comm, current->pid);
return 0;
}
static int mtcaDummy_release(struct inode* inode, struct file* filp) {
mtcaDummyData* privData;
privData = filp->private_data;
// count down inUse
atomic_dec(&privData->inUse);
dbg_print("DEVICE IN SLOT: %d CLOSED BY: %s PID: %d\n", privData->slotNr, current->comm, current->pid);
return 0;
}
static ssize_t mtcaDummy_read(struct file* filp, char __user* buf, size_t count, loff_t* f_pos) {
mtcaDummyData* privData;
device_rw readReqInfo;
u32* barBaseAddress;
size_t barSize;
bool readFail;
if (mutex_lock_interruptible(&controlMutex) != 0) {
dbg_print("%s", "Failed to acquire control mutex while opening file...");
return -ERESTARTSYS;
}
readFail = controlData.read_error;
mutex_unlock(&controlMutex);
if (readFail) {
return -EFAULT;
}
privData = filp->private_data;
if(mutex_lock_interruptible(&privData->devMutex)) {
dbg_print("mutex_lock_interruptible %s\n", "- locking attempt was interrupted by a signal");
return -ERESTARTSYS;
}
if(count < sizeof(readReqInfo)) {
dbg_print("Number of data to read is less then required structure size. "
"Minimum is %ld: requested size is %ld\n",
sizeof(readReqInfo), count);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if(copy_from_user(&readReqInfo, buf, sizeof(readReqInfo))) {
dbg_print("copy_from_user - %s\n", "Cannot copy data from user");
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if(readReqInfo.mode_rw == RW_INFO) {
readReqInfo.offset_rw = MTCADUMMY_DRV_VERSION_MIN;
readReqInfo.data_rw = MTCADUMMY_DRV_VERSION_MAJ;
readReqInfo.barx_rw = privData->slotNr;
readReqInfo.mode_rw = MTCADUMMY_DRV_VERSION_MAJ + 1;
readReqInfo.size_rw = MTCADUMMY_DRV_VERSION_MIN + 1;
if(copy_to_user(buf, &readReqInfo, sizeof(device_rw))) {
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
mutex_unlock(&privData->devMutex);
return sizeof(device_rw);
}
else if(readReqInfo.mode_rw == RW_D32) {
switch(readReqInfo.barx_rw) {
case 0:
barBaseAddress = privData->systemBar;
barSize = MTCADUMMY_N_REGISTERS * 4;
break;
case 2:
barBaseAddress = privData->dmaBar;
barSize = MTCADUMMY_DMA_SIZE;
break;
default:
dbg_print("Incorrect bar number %d, only 0 and 2 are supported\n", readReqInfo.barx_rw);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if((readReqInfo.offset_rw % 4) || (readReqInfo.offset_rw >= barSize)) {
dbg_print("Incorrect data offset %d\n", readReqInfo.offset_rw);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if(mtcadummy_performPreReadAction(readReqInfo.offset_rw, readReqInfo.barx_rw, privData->slotNr)) {
dbg_print("Simulating read access to bad register at offset %d, %s\n", readReqInfo.offset_rw,
"intentinally causing an I/O error");
mutex_unlock(&privData->devMutex);
return -EIO;
}
readReqInfo.data_rw = barBaseAddress[readReqInfo.offset_rw / 4];
if(copy_to_user(buf, &readReqInfo, sizeof(device_rw))) {
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
mutex_unlock(&privData->devMutex);
return sizeof(readReqInfo);
}
else if(readReqInfo.mode_rw == RW_D8 || readReqInfo.mode_rw == RW_D16) {
dbg_print("Reading of RW_D8 or RW_D16 %s\n", "not supported");
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
else if(readReqInfo.mode_rw == RW_DMA) {
u32 dma_size;
u32 dma_offset;
dma_size = readReqInfo.size_rw;
dma_offset = readReqInfo.offset_rw;
if(dma_size == 0) {
mutex_unlock(&privData->devMutex);
return 0;
}
if((dma_size + dma_offset) > MTCADUMMY_DMA_SIZE) {
dbg_print("%s\n", "Reqested dma transfer is too large");
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if(copy_to_user(buf, (void*)(privData->dmaBar + (dma_offset / 4)), dma_size)) {
dbg_print("%s\n", "Error in copy_to_user");
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
mutex_unlock(&privData->devMutex);
return dma_size;
}
else {
dbg_print("Command %d not supported\n", readReqInfo.mode_rw);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
static ssize_t mtcaDummy_write(struct file* filp, const char __user* buf, size_t count, loff_t* f_pos) {
mtcaDummyData* privData;
device_rw writeReqInfo;
u32* barBaseAddress;
size_t barSize;
bool writeFail;
if (mutex_lock_interruptible(&controlMutex) != 0) {
dbg_print("%s", "Failed to acquire control mutex while opening file...");
return -ERESTARTSYS;
}
writeFail = controlData.write_error;
mutex_unlock(&controlMutex);
if (writeFail) {
return -EFAULT;
}
privData = filp->private_data;
if(mutex_lock_interruptible(&privData->devMutex)) {
dbg_print("mutex_lock_interruptible %s\n", "- locking attempt was interrupted by a signal");
return -ERESTARTSYS;
}
if(count < sizeof(writeReqInfo)) {
dbg_print("Number of data to write is less then required structure size. "
"Minimum is %ld: requested size is %ld\n",
sizeof(writeReqInfo), count);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if(copy_from_user(&writeReqInfo, buf, sizeof(writeReqInfo))) {
dbg_print("copy_from_user - %s\n", "Cannot copy data from user");
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if(writeReqInfo.mode_rw == RW_D32) {
switch(writeReqInfo.barx_rw) {
case 0:
barBaseAddress = privData->systemBar;
barSize = MTCADUMMY_N_REGISTERS * 4;
break;
case 2:
barBaseAddress = privData->dmaBar;
barSize = MTCADUMMY_DMA_SIZE;
break;
default:
dbg_print("Incorrect bar number %d, only 0 and 2 are supported\n", writeReqInfo.barx_rw);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
if((writeReqInfo.offset_rw % 4) || (writeReqInfo.offset_rw >= barSize)) {
dbg_print("Incorrect data offset %d\n", writeReqInfo.offset_rw);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
barBaseAddress[writeReqInfo.offset_rw / 4] = writeReqInfo.data_rw;
// invoce the simulation of the "firmware"
if(mtcadummy_performActionOnWrite(writeReqInfo.offset_rw, writeReqInfo.barx_rw, privData->slotNr)) {
dbg_print("Simulating write access to bad register at offset %d, %s.\n", writeReqInfo.offset_rw,
"intentionally causing an I/O error");
mutex_unlock(&privData->devMutex);
return -EIO;
}
/* not mapped memory, no need to wait here: wmb(); */
mutex_unlock(&privData->devMutex);
return sizeof(writeReqInfo);
}
else if(writeReqInfo.mode_rw == RW_D8 || writeReqInfo.mode_rw == RW_D16) {
dbg_print("Writing of RW_D8 or RW_D16 %s\n", "not supported");
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
else if(writeReqInfo.mode_rw == RW_DMA) {
dbg_print("DMA write %s\n", "not supported");
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
else {
dbg_print("Command %d not supported\n", writeReqInfo.mode_rw);
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
mutex_unlock(&privData->devMutex);
return -EFAULT;
}
static long mtcaDummy_ioctl(struct file* filp, unsigned int cmd, unsigned long arg) {
mtcaDummyData* privateData;
int status = 0;
device_ioctrl_data dataStruct;
device_ioctrl_dma dmaStruct;
long returnValue = 0;
if(_IOC_TYPE(cmd) != PCIEDOOCS_IOC) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -ENOTTY;
}
/* there are two ranges of ioctl commands: 'normal' and ioctl */
if(((_IOC_NR(cmd) < PCIEDOOCS_IOC_MINNR) || (_IOC_NR(cmd) > PCIEDOOCS_IOC_MAXNR)) &&
((_IOC_NR(cmd) < PCIEDOOCS_IOC_DMA_MINNR) || (_IOC_NR(cmd) > PCIEDOOCS_IOC_DMA_MAXNR))) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -ENOTTY;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,0,0)
if(_IOC_DIR(cmd) & _IOC_READ)
status = !access_ok(VERIFY_WRITE, (void __user*)arg, _IOC_SIZE(cmd));
else if(_IOC_DIR(cmd) & _IOC_WRITE)
status = !access_ok(VERIFY_READ, (void __user*)arg, _IOC_SIZE(cmd));
#else
status = !access_ok((void __user*)arg, _IOC_SIZE(cmd));
#endif
if(status) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -EFAULT;
}
privateData = filp->private_data;
if(mutex_lock_interruptible(&privateData->devMutex)) {
dbg_print("mutex_lock_interruptible %s\n", "- locking attempt was interrupted by a signal");
return -ERESTARTSYS;
}
switch(cmd) {
case PCIEDEV_PHYSICAL_SLOT:
if(copy_from_user(&dataStruct, (device_ioctrl_data*)arg, sizeof(device_ioctrl_data))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
dataStruct.data = privateData->slotNr;
if(copy_to_user((device_ioctrl_data*)arg, &dataStruct, sizeof(device_ioctrl_data))) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
case PCIEDEV_DRIVER_VERSION:
case PCIEDEV_FIRMWARE_VERSION:
/* dummy driver and firmware version are identical */
if(copy_from_user(&dataStruct, (device_ioctrl_data*)arg, sizeof(device_ioctrl_data))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
dataStruct.data = MTCADUMMY_DRV_VERSION_MAJ;
dataStruct.offset = MTCADUMMY_DRV_VERSION_MIN;
if(copy_to_user((device_ioctrl_data*)arg, &dataStruct, sizeof(device_ioctrl_data))) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
case PCIEDEV_READ_DMA:
// like the current struct-read implementation we ignore the bar information
// (variable 'pattern')
if(copy_from_user(&dmaStruct, (device_ioctrl_dma*)arg, sizeof(device_ioctrl_dma))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
if(dmaStruct.dma_offset + dmaStruct.dma_size > MTCADUMMY_DMA_SIZE) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
// check that the offset is a multiple of 4
if(dmaStruct.dma_offset % 4 != 0) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
// as there is no real dma we also ignore the control register variable
// ('cmd')
// adding to the dmaBAR, which is a pointer, adds multiples of the word
// size, so the values added has to be in words, not bytes
if(copy_to_user((u32*)arg, privateData->dmaBar + (dmaStruct.dma_offset / 4), dmaStruct.dma_size)) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
default:
returnValue = -ENOTTY;
}
mutex_unlock(&privateData->devMutex);
return returnValue;
}
/* An alternatice ioctl section for the llrfdummy device.
*
* Unfortunately this is a copy and paste hack for backward compatibility and
* to be removed. It's not worth the efford to make the constants variables
* which can be changed for llrf and pciedev, just to safe some lines of
* duplicity. This is a temporary solution!
*/
static long llrfDummy_ioctl(struct file* filp, unsigned int cmd, unsigned long arg) {
mtcaDummyData* privateData;
int status = 0;
device_ioctrl_data dataStruct;
long returnValue = 0;
if(_IOC_TYPE(cmd) != LLRFDRV_IOC) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -ENOTTY;
}
if(((_IOC_NR(cmd) < LLRFDRV_IOC_MINNR) || (_IOC_NR(cmd) > LLRFDRV_IOC_MAXNR))) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -ENOTTY;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,0,0)
if(_IOC_DIR(cmd) & _IOC_READ)
status = !access_ok(VERIFY_WRITE, (void __user*)arg, _IOC_SIZE(cmd));
else if(_IOC_DIR(cmd) & _IOC_WRITE)
status = !access_ok(VERIFY_READ, (void __user*)arg, _IOC_SIZE(cmd));
#else
status = !access_ok((void __user*)arg, _IOC_SIZE(cmd));
#endif
if(status) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -EFAULT;
}
privateData = filp->private_data;
if(mutex_lock_interruptible(&privateData->devMutex)) {
dbg_print("mutex_lock_interruptible %s\n", "- locking attempt was interrupted by a signal");
return -ERESTARTSYS;
}
switch(cmd) {
case LLRFDRV_PHYSICAL_SLOT:
if(copy_from_user(&dataStruct, (device_ioctrl_data*)arg, sizeof(device_ioctrl_data))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
dataStruct.data = privateData->slotNr;
if(copy_to_user((device_ioctrl_data*)arg, &dataStruct, sizeof(device_ioctrl_data))) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
case LLRFDRV_DRIVER_VERSION:
case LLRFDRV_FIRMWARE_VERSION:
/* dummy driver and firmware version are identical */
if(copy_from_user(&dataStruct, (device_ioctrl_data*)arg, sizeof(device_ioctrl_data))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
dataStruct.data = MTCADUMMY_DRV_VERSION_MAJ;
dataStruct.offset = MTCADUMMY_DRV_VERSION_MIN;
if(copy_to_user((device_ioctrl_data*)arg, &dataStruct, sizeof(device_ioctrl_data))) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
default:
returnValue = -ENOTTY;
}
mutex_unlock(&privateData->devMutex);
return returnValue;
}
/* Just another copy of the ioctl sequence. It really is time to phase out the
* old drivers.
*/
static long pcieuniDummy_ioctl(struct file* filp, unsigned int cmd, unsigned long arg) {
mtcaDummyData* privateData;
int status = 0;
device_ioctrl_data dataStruct;
device_ioctrl_dma dmaStruct;
long returnValue = 0;
if(_IOC_TYPE(cmd) != PCIEUNI_IOC) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -ENOTTY;
}
/* there are two ranges of ioct commands: 'normal' and ioclt */
if(((_IOC_NR(cmd) < PCIEUNI_IOC_MINNR) || (_IOC_NR(cmd) > PCIEUNI_IOC_MAXNR)) &&
((_IOC_NR(cmd) < PCIEUNI_IOC_DMA_MINNR) || (_IOC_NR(cmd) > PCIEUNI_IOC_DMA_MAXNR))) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -ENOTTY;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,0,0)
if(_IOC_DIR(cmd) & _IOC_READ)
status = !access_ok(VERIFY_WRITE, (void __user*)arg, _IOC_SIZE(cmd));
else if(_IOC_DIR(cmd) & _IOC_WRITE)
status = !access_ok(VERIFY_READ, (void __user*)arg, _IOC_SIZE(cmd));
#else
status = !access_ok((void __user*)arg, _IOC_SIZE(cmd));
#endif
if(status) {
dbg_print("Incorrect ioctl command %d\n", cmd);
return -EFAULT;
}
privateData = filp->private_data;
if(mutex_lock_interruptible(&privateData->devMutex)) {
dbg_print("mutex_lock_interruptible %s\n", "- locking attempt was interrupted by a signal");
return -ERESTARTSYS;
}
switch(cmd) {
case PCIEUNI_PHYSICAL_SLOT:
if(copy_from_user(&dataStruct, (device_ioctrl_data*)arg, sizeof(device_ioctrl_data))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
dataStruct.data = privateData->slotNr;
if(copy_to_user((device_ioctrl_data*)arg, &dataStruct, sizeof(device_ioctrl_data))) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
case PCIEUNI_DRIVER_VERSION:
case PCIEUNI_FIRMWARE_VERSION:
/* dummy driver and firmware version are identical */
if(copy_from_user(&dataStruct, (device_ioctrl_data*)arg, sizeof(device_ioctrl_data))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
dataStruct.data = MTCADUMMY_DRV_VERSION_MAJ;
dataStruct.offset = MTCADUMMY_DRV_VERSION_MIN;
if(copy_to_user((device_ioctrl_data*)arg, &dataStruct, sizeof(device_ioctrl_data))) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
case PCIEUNI_READ_DMA:
// like the current struct-read implementation we ignore the bar information
// (variable 'pattern')
if(copy_from_user(&dmaStruct, (device_ioctrl_dma*)arg, sizeof(device_ioctrl_dma))) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
if(dmaStruct.dma_offset + dmaStruct.dma_size > MTCADUMMY_DMA_SIZE) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
// check that the offset is a multiple of 4
if(dmaStruct.dma_offset % 4 != 0) {
mutex_unlock(&privateData->devMutex);
return -EFAULT;
}
// as there is no real dma we also ignore the control register variable
// ('cmd')
// adding to the dmaBAR, which is a pointer, adds multiples of the word
// size, so the values added has to be in words, not bytes
if(copy_to_user((u32*)arg, privateData->dmaBar + (dmaStruct.dma_offset / 4), dmaStruct.dma_size)) {
returnValue = -EFAULT;
// mutex will be unlocked directly after the switch
}
break;
default:
returnValue = -ENOTTY;
}
mutex_unlock(&privateData->devMutex);
return returnValue;
}
struct file_operations mtcaDummyFileOps = {
.owner = THIS_MODULE,
.read = mtcaDummy_read,
.write = mtcaDummy_write,
.unlocked_ioctl = mtcaDummy_ioctl,
.open = mtcaDummy_open,
.release = mtcaDummy_release,
};
struct file_operations llrfDummyFileOps = {
.owner = THIS_MODULE,
.read = mtcaDummy_read,
.write = mtcaDummy_write,
.unlocked_ioctl = llrfDummy_ioctl,
.open = mtcaDummy_open,
.release = mtcaDummy_release,
};
struct file_operations noIocltDummyFileOps = {
.owner = THIS_MODULE,
.read = mtcaDummy_read,
.write = mtcaDummy_write,
.open = mtcaDummy_open,
.release = mtcaDummy_release,
};
struct file_operations pcieuniDummyFileOps = {
.owner = THIS_MODULE,
.read = mtcaDummy_read_no_struct,
.write = mtcaDummy_write_no_struct,
.unlocked_ioctl = pcieuniDummy_ioctl,
.open = mtcaDummy_open,
.release = mtcaDummy_release,
};
module_init(mtcaDummy_init_module);
module_exit(mtcaDummy_cleanup_module);