forked from flink-project/flinklinux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflink_core.c
1466 lines (1370 loc) · 49.1 KB
/
flink_core.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************
* _________ _____ _____ ____ _____ ___ ____ *
* |_ ___ | |_ _| |_ _| |_ \|_ _| |_ ||_ _| *
* | |_ \_| | | | | | \ | | | |_/ / *
* | _| | | _ | | | |\ \| | | __'. *
* _| |_ _| |__/ | _| |_ _| |_\ |_ _| | \ \_ *
* |_____| |________| |_____| |_____|\____| |____||____| *
* *
*******************************************************************
* *
* Core Module *
* *
*******************************************************************/
/** @file flink_core.c
* @brief Core module for flink.
*
* Contains functions to initialize, add and remove flink devices
* and subdevices
*
* @author Martin Züger
* @author Urs Graf
* @author Patrick Good
*
* Changelog
* Date Who What
* 28.10.23 Good Added interrupt capability
* -> Added ioctl #50 #51 #52
* -> Added IRQ service routine
* -> Added flink_device_init_irq(...)
* -> Adjusted flink_device_init(...) & flink_device_delete(...)
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h>
#include <linux/signal.h>
#include <linux/sched/signal.h>
#include "flink.h"
#define MODULE_NAME THIS_MODULE->name
#define SYSFS_CLASS_NAME "flink"
#define MAX_DEV_NAME_LENGTH 15
MODULE_AUTHOR("Martin Zueger <[email protected]>");
MODULE_DESCRIPTION("fLink core module");
MODULE_LICENSE("Dual BSD/GPL");
static LIST_HEAD(device_list);
static LIST_HEAD(loaded_if_modules);
static struct class* sysfs_class;
// ###### Internal Function Prototypes ######
// do NOT call this directly!!! this function is called over an irq number
static irqreturn_t flink_threaded_irq_handler(int irq, void *dev_id);
// ############ File operations ############
int flink_open(struct inode* i, struct file* f) {
struct flink_device* fdev = flink_get_device_by_cdev(i->i_cdev);
struct flink_private_data* p_data = kmalloc(sizeof(struct flink_private_data), GFP_KERNEL);
memset(p_data, 0, sizeof(*p_data));
p_data->fdev = fdev;
f->private_data = p_data;
#if defined(DBG)
printk(KERN_DEBUG "[%s] Device node opened.", MODULE_NAME);
#endif
return 0;
}
int flink_relase(struct inode* i, struct file* f) {
kfree(f->private_data);
#if defined(DBG)
printk(KERN_DEBUG "[%s] Device node closed.", MODULE_NAME);
#endif
return 0;
}
ssize_t flink_read(struct file* f, char __user* data, size_t size, loff_t* offset) {
struct flink_private_data* pdata = (struct flink_private_data*)(f->private_data);
#if defined(DBG)
printk(KERN_DEBUG "[%s] Reading from device...", MODULE_NAME);
#endif
if(pdata != NULL && pdata->current_subdevice != NULL) {
struct flink_subdevice* subdev = pdata->current_subdevice;
struct flink_device* fdev = subdev->parent;
unsigned long rsize = 0;
u32 roffset = (u32)*offset;;
#if defined(DBG)
printk(KERN_DEBUG " -> Device: %u/%u", fdev->id, subdev->id);
printk(KERN_DEBUG " -> Size: 0x%x (%u bytes)", (unsigned int)size, (unsigned int)size);
printk(KERN_DEBUG " -> Offset: 0x%x", (u32)*offset);
#endif
if(roffset > subdev->mem_size) {
return 0;
}
switch(size) {
case 1: {
u8 rdata = 0;
rdata = fdev->bus_ops->read8(fdev, subdev->base_addr + roffset);
rsize = copy_to_user(data, &rdata, sizeof(rdata));
if(rsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying to user space failed: %lu bytes not copied!", rsize);
#endif
return 0;
}
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", rdata);
#endif
return sizeof(rdata);
}
case 2: {
u16 rdata = 0;
rdata = fdev->bus_ops->read16(fdev, subdev->base_addr + roffset);
rsize = copy_to_user(data, &rdata, sizeof(rdata));
if(rsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying to user space failed: %lu bytes not copied!", rsize);
#endif
return 0;
}
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", rdata);
#endif
return sizeof(rdata);
}
case 4: {
u32 rdata = 0;
rdata = fdev->bus_ops->read32(fdev, subdev->base_addr + roffset);
rsize = copy_to_user(data, &rdata, sizeof(rdata));
if(rsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying to user space failed: %lu bytes not copied!", rsize);
#endif
return 0;
}
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", rdata);
#endif
return sizeof(rdata);
}
default:
#if defined(DBG)
printk(KERN_DEBUG " -> Size of transfer not supported: %lu bytes!", (long unsigned int)size);
#endif
return 0;
}
}
return 0;
}
ssize_t flink_write(struct file* f, const char __user* data, size_t size, loff_t* offset) {
struct flink_private_data* pdata = (struct flink_private_data*)(f->private_data);
#if defined(DBG)
printk(KERN_DEBUG "[%s] Writing to device...", MODULE_NAME);
#endif
if(pdata != NULL && pdata->current_subdevice != NULL) {
struct flink_subdevice* subdev = pdata->current_subdevice;
struct flink_device* fdev = subdev->parent;
u32 woffset = (u32)*offset;
unsigned long wsize = 0;
#if defined(DBG)
printk(KERN_DEBUG " -> Device: %u/%u", fdev->id, subdev->id);
printk(KERN_DEBUG " -> Size: 0x%x (%u bytes)", (unsigned int)size, (unsigned int)size);
printk(KERN_DEBUG " -> Offset: 0x%x", (u32)*offset);
#endif
if(woffset > subdev->mem_size) {
return 0;
}
switch(size) {
case 1: {
u8 wdata = 0;
wsize = copy_from_user(&wdata, data, sizeof(wdata));
if(wsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying from user space failed: %lu bytes not copied!", wsize);
#endif
return 0;
}
fdev->bus_ops->write8(fdev, subdev->base_addr + woffset, wdata);
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", wdata);
#endif
return sizeof(wdata);
}
case 2: {
u16 wdata = 0;
wsize = copy_from_user(&wdata, data, sizeof(wdata));
if(wsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying from user space failed: %lu bytes not copied!", wsize);
#endif
return 0;
}
fdev->bus_ops->write16(fdev, subdev->base_addr + woffset, wdata);
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", wdata);
#endif
return sizeof(wdata);
}
case 4: {
u32 wdata = 0;
wsize = copy_from_user(&wdata, data, sizeof(wdata));
if(wsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying from user space failed: %lu bytes not copied!", wsize);
#endif
return 0;
}
fdev->bus_ops->write32(fdev, subdev->base_addr + woffset, wdata);
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", wdata);
#endif
return sizeof(wdata);
}
default:
#if defined(DBG)
printk(KERN_DEBUG " -> Size of transfer not supported: %lu bytes!", (long unsigned int)size);
#endif
return 0;
}
}
return 0;
}
long flink_ioctl(struct file* f, unsigned int cmd, unsigned long arg) {
int error = 0;
struct flink_private_data* pdata = (struct flink_private_data*)(f->private_data);
struct flink_subdevice* src;
u8 id;
struct ioctl_bit_container_t rwbit_container;
struct ioctl_container_t rw_container;
unsigned long rsize = 0;
unsigned long wsize = 0;
u32 temp;
// for irq register and unregister
struct flink_process_data* fsignal;
struct task_struct* user_task = get_current();
bool found_entry = false;
u32 requested_irq_nr = 0;
struct flink_irq_data* hwirq;
#if defined(DBG)
printk(KERN_DEBUG "[%s] I/O control call...", MODULE_NAME);
#endif
switch(cmd) {
case SELECT_SUBDEVICE:
#if defined(DBG)
printk(KERN_DEBUG " -> SELECT_SUBDEVICE (0x%x)", SELECT_SUBDEVICE);
#endif
error = copy_from_user(&id, (void __user *)arg, sizeof(id));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
return flink_select_subdevice(f, id, 0);
case SELECT_SUBDEVICE_EXCL:
#if defined(DBG)
printk(KERN_DEBUG " -> SELECT_SUBDEVICE_EXCL (0x%x)", SELECT_SUBDEVICE_EXCL);
#endif
error = copy_from_user(&id, (void __user *)arg, sizeof(id));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
return flink_select_subdevice(f, id, 1);
case READ_NOF_SUBDEVICES:
#if defined(DBG)
printk(KERN_DEBUG " -> READ_NOF_SUBDEVICES (0x%x) -> %u", READ_NOF_SUBDEVICES, pdata->fdev->nof_subdevices);
#endif
error = copy_to_user((void __user *)arg, &(pdata->fdev->nof_subdevices), sizeof(pdata->fdev->nof_subdevices));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying to userspace: %i", error);
#endif
return -EINVAL;
}
break;
case READ_SUBDEVICE_INFO:
#if defined(DBG)
printk(KERN_DEBUG " -> READ_SUBDEVICE_INFO (0x%x)", READ_SUBDEVICE_INFO);
#endif
error = copy_from_user(&id, (void __user *)arg, sizeof(id));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
if(id >= pdata->fdev->nof_subdevices) {
#if defined(DBG)
printk(KERN_DEBUG " -> Illegal subdevice id");
#endif
return -EINVAL;
}
src = flink_get_subdevice_by_id(pdata->fdev, id);
if(src == NULL) {
#if defined(DBG)
printk(KERN_DEBUG " -> Getting kernel subdevice structure failed.");
#endif
return -EINVAL;
}
error = copy_to_user((void __user *)arg, &(src->id), FLINKLIB_SUBDEVICE_SIZE);
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying to userspace: %i", error);
#endif
return -EINVAL;
}
break;
case READ_SINGLE_BIT:
#if defined(DBG)
printk(KERN_DEBUG " -> READ_SINGLE_BIT (0x%x)", READ_SINGLE_BIT);
#endif
error = copy_from_user(&rwbit_container, (void __user *)arg, sizeof(rwbit_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
temp = pdata->fdev->bus_ops->read32(pdata->fdev, pdata->current_subdevice->base_addr + rwbit_container.offset);
#if defined(DBG)
printk(KERN_DEBUG " -> Read from device: 0x%x", temp);
#endif
rwbit_container.value = ((temp & (1 << rwbit_container.bit)) != 0);
#if defined(DBG)
printk(KERN_DEBUG " -> Bit value: 0x%x", rwbit_container.value);
#endif
error = copy_to_user((void __user *)arg, &rwbit_container, sizeof(rwbit_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying to userspace: %i", error);
#endif
return -EINVAL;
}
break;
case WRITE_SINGLE_BIT:
#if defined(DBG)
printk(KERN_DEBUG " -> WRITE_SINGLE_BIT (0x%x)", WRITE_SINGLE_BIT);
#endif
error = copy_from_user(&rwbit_container, (void __user *)arg, sizeof(rwbit_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
else {
#if defined(DBG)
printk(KERN_DEBUG " -> Copied from user space: offset = 0x%x, bit = %u, value = %u", rwbit_container.offset, rwbit_container.bit, rwbit_container.value);
#endif
}
temp = pdata->fdev->bus_ops->read32(pdata->fdev, pdata->current_subdevice->base_addr + rwbit_container.offset);
#if defined(DBG)
printk(KERN_DEBUG " -> Read from device: 0x%x", temp);
#endif
if(rwbit_container.value != 0) { // set bit
temp |= (1 << rwbit_container.bit);
#if defined(DBG)
printk(KERN_DEBUG " -> Setting bit by writing 0x%x to device", temp);
#endif
}
else { // clear bit
temp &= ~(1 << rwbit_container.bit);
#if defined(DBG)
printk(KERN_DEBUG " -> Clearing bit by writing 0x%x to device", temp);
#endif
}
pdata->fdev->bus_ops->write32(pdata->fdev, pdata->current_subdevice->base_addr + rwbit_container.offset, temp);
break;
case SELECT_AND_READ_BIT:
#if defined(DBG)
printk(KERN_DEBUG " -> SELECT_AND_READ_BIT (0x%x)", SELECT_AND_READ_BIT);
#endif
error = copy_from_user(&rwbit_container, (void __user *)arg, sizeof(rwbit_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
src = flink_get_subdevice_by_id(pdata->fdev, rwbit_container.subdevice);
if(src == NULL) {
#if defined(DBG)
printk(KERN_DEBUG " -> Getting kernel subdevice structure failed.");
#endif
return -EINVAL;
}
temp = pdata->fdev->bus_ops->read32(pdata->fdev, src->base_addr + rwbit_container.offset);
#if defined(DBG)
printk(KERN_DEBUG " -> Read from device: 0x%x", temp);
#endif
rwbit_container.value = ((temp & (1 << rwbit_container.bit)) != 0);
#if defined(DBG)
printk(KERN_DEBUG " -> Bit value: 0x%x", rwbit_container.value);
#endif
error = copy_to_user((void __user *)arg, &rwbit_container, sizeof(rwbit_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying to userspace: %i", error);
#endif
return -EINVAL;
}
break;
case SELECT_AND_WRITE_BIT:
#if defined(DBG)
printk(KERN_DEBUG " -> SELECT_AND_WRITE_BIT (0x%x)", SELECT_AND_WRITE_BIT);
#endif
error = copy_from_user(&rwbit_container, (void __user *)arg, sizeof(rwbit_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
else {
#if defined(DBG)
printk(KERN_DEBUG " -> Copied from user space: offset = 0x%x, bit = %u, value = %u", rwbit_container.offset, rwbit_container.bit, rwbit_container.value);
#endif
}
src = flink_get_subdevice_by_id(pdata->fdev, rwbit_container.subdevice);
if(src == NULL) {
#if defined(DBG)
printk(KERN_DEBUG " -> Getting kernel subdevice structure failed.");
#endif
return -EINVAL;
}
temp = pdata->fdev->bus_ops->read32(pdata->fdev, src->base_addr + rwbit_container.offset);
#if defined(DBG)
printk(KERN_DEBUG " -> Read from device: 0x%x", temp);
#endif
if(rwbit_container.value != 0) { // set bit
temp |= (1 << rwbit_container.bit);
#if defined(DBG)
printk(KERN_DEBUG " -> Setting bit by writing 0x%x to device", temp);
#endif
}
else { // clear bit
temp &= ~(1 << rwbit_container.bit);
#if defined(DBG)
printk(KERN_DEBUG " -> Clearing bit by writing 0x%x to device", temp);
#endif
}
pdata->fdev->bus_ops->write32(pdata->fdev, src->base_addr + rwbit_container.offset, temp);
break;
case SELECT_AND_READ:
#if defined(DBG)
printk(KERN_DEBUG " -> SELECT_AND_READ (0x%x)", SELECT_AND_READ);
#endif
error = copy_from_user(&rw_container, (void __user *)arg, sizeof(rw_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
if (rw_container.data == NULL) {
#if defined(DBG)
printk(KERN_DEBUG " -> NULL pointer");
#endif
return -EINVAL;
}
src = flink_get_subdevice_by_id(pdata->fdev, rw_container.subdevice);
if(src == NULL) {
#if defined(DBG)
printk(KERN_DEBUG " -> Getting kernel subdevice structure failed.");
#endif
return -EINVAL;
}
if (rw_container.offset > src->mem_size) {
#if defined(DBG)
printk(KERN_DEBUG " -> offset > mem_size");
#endif
return -EINVAL;
}
switch(rw_container.size) {
case 1: {
u8 rdata = 0;
rdata = pdata->fdev->bus_ops->read8(pdata->fdev, src->base_addr + rw_container.offset);
rsize = copy_to_user((void __user *)rw_container.data, &rdata, sizeof(rdata));
if(rsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying to user space failed: %lu bytes not copied!", rsize);
#endif
return 0;
}
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", rdata);
#endif
return sizeof(rdata);
}
case 2: {
u16 rdata = 0;
rdata = pdata->fdev->bus_ops->read16(pdata->fdev, src->base_addr + rw_container.offset);
rsize = copy_to_user((void __user *)rw_container.data, &rdata, sizeof(rdata));
if(rsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying to user space failed: %lu bytes not copied!", rsize);
#endif
return 0;
}
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", rdata);
#endif
return sizeof(rdata);
}
case 4: {
u32 rdata = 0;
rdata = pdata->fdev->bus_ops->read32(pdata->fdev, src->base_addr + rw_container.offset);
rsize = copy_to_user((void __user *)rw_container.data, &rdata, sizeof(rdata));
if(rsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying to user space failed: %lu bytes not copied!", rsize);
#endif
return 0;
}
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", rdata);
#endif
return sizeof(rdata);
}
default:
return -EINVAL;
}
break;
case SELECT_AND_WRITE:
#if defined(DBG)
printk(KERN_DEBUG " -> SELECT_AND_WRITE (0x%x)", SELECT_AND_WRITE);
#endif
error = copy_from_user(&rw_container, (void __user *)arg, sizeof(rw_container));
if(error != 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Error while copying from userspace: %i", error);
#endif
return -EINVAL;
}
if (rw_container.data == NULL) {
#if defined(DBG)
printk(KERN_DEBUG " -> NULL pointer");
#endif
return -EINVAL;
}
src = flink_get_subdevice_by_id(pdata->fdev, rw_container.subdevice);
if(src == NULL) {
#if defined(DBG)
printk(KERN_DEBUG " -> Getting kernel subdevice structure failed.");
#endif
return -EINVAL;
}
if (rw_container.offset > src->mem_size) {
#if defined(DBG)
printk(KERN_DEBUG " -> offset > mem_size");
#endif
return -EINVAL;
}
switch(rw_container.size) {
case 1: {
u8 wdata = 0;
wsize = copy_from_user(&wdata, (void __user *)rw_container.data, sizeof(wdata));
if(wsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying from user space failed: %lu bytes not copied!", wsize);
#endif
return -EINVAL;
}
pdata->fdev->bus_ops->write8(pdata->fdev, src->base_addr + rw_container.offset, wdata);
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", wdata);
#endif
return sizeof(wdata);
}
case 2: {
u16 wdata = 0;
wsize = copy_from_user(&wdata, (void __user *)rw_container.data, sizeof(wdata));
if(wsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying from user space failed: %lu bytes not copied!", wsize);
#endif
return -EINVAL;
}
pdata->fdev->bus_ops->write16(pdata->fdev, src->base_addr + rw_container.offset, wdata);
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", wdata);
#endif
return sizeof(wdata);
}
case 4: {
u32 wdata = 0;
wsize = copy_from_user(&wdata, (void __user *)rw_container.data, sizeof(wdata));
if(wsize > 0) {
#if defined(DBG)
printk(KERN_DEBUG " -> Copying from user space failed: %lu bytes not copied!", wsize);
#endif
return -EINVAL;
}
pdata->fdev->bus_ops->write32(pdata->fdev, src->base_addr + rw_container.offset, wdata);
#if defined(DBG)
printk(KERN_DEBUG " -> Value: 0x%x", wdata);
#endif
return sizeof(wdata);
}
default:
return -EINVAL;
}
break;
case REGISTER_IRQ:
#if defined(DBG)
printk(KERN_DEBUG "[%s] Register IRQ (0x%x)", MODULE_NAME, REGISTER_IRQ);
#endif
if(unlikely(pdata->fdev->nof_irqs == 0)) {
printk(KERN_WARNING "[%s] Irq functionality not available", MODULE_NAME);
return -EPERM;
}
error = copy_from_user(&rw_container, (void __user *)arg, sizeof(rw_container));
if(unlikely(error != 0)) {
printk(KERN_WARNING "[%s] Error while copying from userspace: %i", MODULE_NAME, error);
return -EINVAL;
}
if(unlikely(rw_container.size != 4)) {
printk(KERN_WARNING "[%s] size must have lenght of 4 bytes aka uint32_t", MODULE_NAME);
return -EINVAL;
}
wsize = copy_from_user(&requested_irq_nr, (void __user *)rw_container.data, sizeof(requested_irq_nr));
if(unlikely(wsize > 0)) {
printk(KERN_WARNING "[%s] Copying from user space failed: %lu bytes not copied!", MODULE_NAME, wsize);
return -EINVAL;
}
if(unlikely(requested_irq_nr >= pdata->fdev->nof_irqs)) {
printk(KERN_WARNING "[%s] IRQ number %lu is too high. Number must be between 0 and %lu", MODULE_NAME, (long unsigned int)requested_irq_nr, (long unsigned int)pdata->fdev->nof_irqs-1);
return -EINVAL;
}
// generate IRQ structure and link to the correct entry in the list, which is sorted by PID
list_for_each_entry(hwirq, &(pdata->fdev->hw_irq_data), list) {
if (hwirq->irq_nr == requested_irq_nr) {
mutex_lock(&(hwirq->lock_for_ioctl)); // It's not allowed for two processes to read and write the list at the same time.
list_for_each_entry(fsignal, &(hwirq->flink_process_data), list) {
if(unlikely(fsignal->user_task->pid == user_task->pid)) {
printk(KERN_WARNING "[%s] IRQ %lu is already registered oh the pid", MODULE_NAME, (long unsigned int)hwirq->irq_nr);
mutex_unlock(&(hwirq->lock_for_ioctl));
return -EINVAL;
}
}
fsignal = kzalloc(sizeof(struct flink_process_data), GFP_KERNEL);
if(unlikely(!fsignal)) {
printk(KERN_ERR "[%s] Failed to allocate memory for signal witch depends on irq %lu", MODULE_NAME, (long unsigned int)hwirq->irq_nr);
mutex_unlock(&(hwirq->lock_for_ioctl));
return -ENOMEM;
}
INIT_LIST_HEAD(&(fsignal->list));
fsignal->user_task = user_task;
hwirq->signal_nr_with_offset = pdata->fdev->signal_offset + hwirq->irq_nr;
spin_lock_bh(&(hwirq->irq_lock));
list_add(&(fsignal->list), &(hwirq->flink_process_data)); // This is very critical when an IRQ is fired (IRQ reads this list and here it has been modified).
spin_unlock_bh(&(hwirq->irq_lock));
mutex_unlock(&(hwirq->lock_for_ioctl));
hwirq->signal_count++;
#if defined(DBG)
printk(KERN_DEBUG " -> Signal %lu for process %lu registerd", hwirq->signal_nr_with_offset, user_task->pid);
#endif
return hwirq->signal_nr_with_offset;
}
}
break;
case UNREGISTER_IRQ:
#if defined(DBG)
printk(KERN_DEBUG "[%s] Unregister IRQ (0x%x)", MODULE_NAME, UNREGISTER_IRQ);
#endif
if(unlikely(pdata->fdev->nof_irqs == 0)) {
printk(KERN_WARNING "[%s] Irq functionality not available", MODULE_NAME);
return -EPERM;
}
error = copy_from_user(&rw_container, (void __user *)arg, sizeof(rw_container));
if(unlikely(error != 0)) {
printk(KERN_WARNING "[%s] Error while copying from userspace: %i", MODULE_NAME, error);
return -EINVAL;
}
if(unlikely(rw_container.size != 4)) {
printk(KERN_WARNING "[%s] size must have lenght of 4 bytes aka uint32_t", MODULE_NAME);
return -EINVAL;
}
wsize = copy_from_user(&requested_irq_nr, (void __user *)rw_container.data, sizeof(requested_irq_nr));
if(unlikely(wsize > 0)) {
printk(KERN_WARNING "[%s] Copying from user space failed: %lu bytes not copied!", MODULE_NAME, wsize);
return -EINVAL;
}
if(unlikely(requested_irq_nr >= pdata->fdev->nof_irqs)) {
printk(KERN_WARNING "[%s] IRQ number %lu is too high. Number must be between 0 and %lu", MODULE_NAME, (long unsigned int)requested_irq_nr, (long unsigned int)pdata->fdev->nof_irqs-1);
return -EINVAL;
}
list_for_each_entry(hwirq, &(pdata->fdev->hw_irq_data), list) {
if(hwirq->irq_nr == requested_irq_nr) {
if(unlikely(hwirq->signal_count == 0)){
printk(KERN_WARNING "[%s] No signal registered on the requested IRQ: %lu", MODULE_NAME, (long unsigned int)hwirq->irq_nr);
return -EINVAL;
}
mutex_lock(&(hwirq->lock_for_ioctl)); // It's not allowed for two processes to read and write the list at the same time.
found_entry = false;
list_for_each_entry(fsignal, &(hwirq->flink_process_data), list) {
if(fsignal->user_task->pid == user_task->pid) {
#if defined(DBG)
printk(KERN_DEBUG " -> Found list entry to remove");
#endif
found_entry = true;
break;
}
}
if(likely(found_entry)) {
spin_lock_bh(&(hwirq->irq_lock));
list_del(&(fsignal->list)); // This is very critical when an IRQ is fired (IRQ reads this list and here it has been modified).
spin_unlock_bh(&(hwirq->irq_lock));
mutex_unlock(&(hwirq->lock_for_ioctl));
if(fsignal) {
kfree(fsignal);
}
#if defined(DBG)
printk(KERN_DEBUG " -> Signal %lu for process %lu unregisterd", hwirq->signal_nr_with_offset, user_task->pid);
#endif
} else {
mutex_unlock(&(hwirq->lock_for_ioctl));
#if defined(DBG)
printk(KERN_DEBUG " -> No list entry found to remove");
#endif
return -EINVAL;
}
break;
}
}
break;
case GET_SIGNAL_OFFSET:
#if defined(DBG)
printk(KERN_DEBUG "[%s] Get signal offset (0x%x)", MODULE_NAME, GET_SIGNAL_OFFSET);
#endif
if(unlikely(pdata->fdev->nof_irqs == 0)) {
printk(KERN_WARNING "[%s] Irq functionality not available", MODULE_NAME);
return -EPERM;
}
error = copy_from_user(&rw_container, (void __user *)arg, sizeof(rw_container));
if(unlikely(error != 0)) {
printk(KERN_WARNING "[%s] Error while copying from userspace: %i", MODULE_NAME, error);
return -EINVAL;
}
if(unlikely(rw_container.size != 4)) {
printk(KERN_WARNING "[%s] Size must have lenght of 4 bytes aka uint32_t", MODULE_NAME);
return -EINVAL;
}
rsize = copy_to_user((void __user *)rw_container.data, &(pdata->fdev->signal_offset), sizeof(pdata->fdev->signal_offset));
if(unlikely(rsize > 0)) {
printk(KERN_WARNING "[%s] Copying to user space failed: %lu bytes not copied!", MODULE_NAME, rsize);
return 0;
}
#if defined(DBG)
printk(KERN_DEBUG " -> Signal offset: 0x%x", pdata->fdev->signal_offset);
#endif
return sizeof(pdata->fdev->signal_offset);
default:
#if defined(DBG)
printk(KERN_DEBUG " -> Error: illegal ioctl command: 0x%x!", cmd);
#endif
return -EINVAL;
}
return 0;
}
loff_t flink_llseek(struct file* f, loff_t off, int whence) {
struct flink_private_data* pdata = (struct flink_private_data*)(f->private_data);
#if defined(DBG)
printk(KERN_DEBUG "[%s] llseek call...", MODULE_NAME);
#endif
if(pdata != NULL && pdata->current_subdevice != NULL) {
loff_t newpos;
switch(whence) {
case 0: /* SEEK_SET */
newpos = off;
break;
case 1: /* SEEK_CUR */
newpos = f->f_pos + off;
break;
case 2: /* SEEK_END */
newpos = pdata->current_subdevice->mem_size + off;
break;
default: /* can't happen */
return -EINVAL;
}
if(newpos < 0) return -EINVAL;
f->f_pos = newpos;
#if defined(DBG)
printk(KERN_DEBUG " -> new position: 0x%x", (u32)newpos);
#endif
return newpos;
}
return -EINVAL;
}
struct file_operations flink_fops = {
.owner = THIS_MODULE,
.open = flink_open,
.release = flink_relase,
.read = flink_read,
.write = flink_write,
.unlocked_ioctl = flink_ioctl,
.llseek = flink_llseek
};
// ############ Initialization ############
static int __init flink_init(void) {
int error = 0;
// Create sysfs class
sysfs_class = class_create(THIS_MODULE, SYSFS_CLASS_NAME);
// ---- All done ----
printk(KERN_INFO "[%s] Module sucessfully loaded\n", MODULE_NAME);
return 0;
// ---- ERROR HANDLING ----
return error;
}
module_init(flink_init);
// ############ Cleanup ############
static void __exit flink_exit(void) {
// Destroy sysfs class
class_destroy(sysfs_class);
// ---- All done ----
printk(KERN_INFO "[%s] Module sucessfully unloaded\n", MODULE_NAME);
}
module_exit(flink_exit);
// ############ Device and module handling functions ############
/*******************************************************************
* *
* Internal (private) methods *
* *
*******************************************************************/
/**
* create_device_node() - creates a device node for a flink device
* @fdev: the flink device to create a device node for
*/
static int create_device_node(struct flink_device* fdev) {
static unsigned int dev_counter = 0;
int error = 0;
dev_t dev;
// Allocate, register and initialize char device
error = alloc_chrdev_region(&dev, dev_counter, 1, MODULE_NAME);
if(error) {
printk(KERN_ERR "[%s] Allocation of char dev region failed!", MODULE_NAME);
goto alloc_chardev_region_failed;
}
fdev->char_device = cdev_alloc();
if(fdev->char_device == NULL) {
printk(KERN_ERR "[%s] Allocation of char dev failed!", MODULE_NAME);
goto cdev_alloc_failed;
}
cdev_init(fdev->char_device, &flink_fops);
fdev->char_device->owner = THIS_MODULE;
error = cdev_add(fdev->char_device, dev, 1);
if(error) {
printk(KERN_ERR "[%s] Adding the char dev to the system failed!", MODULE_NAME);
goto cdev_add_failed;
}
// create device node
fdev->sysfs_device = device_create(sysfs_class, NULL, dev, NULL, "flink%u", dev_counter);
if(IS_ERR(fdev->sysfs_device)) {
printk(KERN_ERR "[%s] Creation of sysfs device failed!", MODULE_NAME);
goto device_create_failed;
}
#if defined(DBG)
printk(KERN_DEBUG "[%s] Device node created: flink%u", MODULE_NAME, dev_counter);
#endif
dev_counter++;
return 0;
// Cleanup on error
device_create_failed:
cdev_del(fdev->char_device);
cdev_add_failed:
fdev->char_device = NULL;
cdev_alloc_failed:
unregister_chrdev_region(dev, 1);
alloc_chardev_region_failed:
// nothing to do
return error;
}
/**
* scan_for_subdevices() - scan flink device for subdevices
* @fdev: the flink device to scan
*
* Scans the device for available subdevices and adds them to
* the device structure. The number of added subdevices is returned.
*/
static unsigned int scan_for_subdevices(struct flink_device* fdev) {
unsigned int subdevice_counter = 0;
u32 current_address = 0;
u32 last_address = current_address + fdev->bus_ops->address_space_size(fdev) - 1;
u32 current_function = 0;
u32 current_mem_size = 0;
u32 total_mem_size = 0;
struct flink_subdevice* new_subdev;
#if defined(DBG)
printk(KERN_DEBUG "[%s] Scanning device #%u for subdevices...", MODULE_NAME, fdev->id);
printk(KERN_DEBUG " -> Start address: 0x%x", current_address);
printk(KERN_DEBUG " -> Last valid address: 0x%x", last_address);
#endif
while(current_address < last_address && subdevice_counter < MAX_NOF_SUBDEVICES) {
current_function = (fdev->bus_ops->read32(fdev, current_address + SUBDEV_FUNCTION_OFFSET));
current_mem_size = fdev->bus_ops->read32(fdev, current_address + SUBDEV_SIZE_OFFSET);
#if defined(DBG)
printk(KERN_DEBUG "[%s] subdevice size: 0x%x (current address: 0x%x)\n", MODULE_NAME, current_mem_size, current_address);
#endif
if(current_mem_size > MAIN_HEADER_SIZE + SUB_HEADER_SIZE) {
// Create and initialize new subdevice
new_subdev = flink_subdevice_alloc();
flink_subdevice_init(new_subdev);
new_subdev->function_id = (u16)(current_function >> 16);
new_subdev->sub_function_id = (u8)((current_function >> 8) & 0xFF);
new_subdev->function_version = (u8)(current_function & 0xFF);
new_subdev->base_addr = current_address;
new_subdev->mem_size = current_mem_size;
new_subdev->nof_channels = fdev->bus_ops->read32(fdev, current_address + SUBDEV_NOFCHANNELS_OFFSET);
new_subdev->unique_id = fdev->bus_ops->read32(fdev, current_address + SUBDEV_UNIQUE_ID_OFFSET);
// Add subdevice to flink device
flink_subdevice_add(fdev, new_subdev);
subdevice_counter++;
// if subdevice is info subdevice -> read memory length
if(new_subdev->function_id == INFO_FUNCTION_ID) {
total_mem_size = fdev->bus_ops->read32(fdev, current_address + MAIN_HEADER_SIZE + SUB_HEADER_SIZE);
last_address = total_mem_size - 1;
#if defined(DBG)
printk(KERN_DEBUG "[%s] Info subdevice found: total memory length=0x%x", MODULE_NAME, total_mem_size);
#endif
}
// Increment address counter and reset temp variables
current_address += current_mem_size;
current_function = 0;
current_mem_size = 0;
}
else {
#if defined(DBG)
printk(KERN_ALERT "[%s] aborting\n", MODULE_NAME);
#endif
break;
}
}
return subdevice_counter;
}
// irq handler do not call this function directly. Only register it with request_irq()
static irqreturn_t flink_threaded_irq_handler(int irq, void *dev_id) {
struct siginfo info;
struct flink_irq_data* irq_data = (struct flink_irq_data*)(dev_id);
struct flink_process_data* signal_data;
#if defined(DBG_IRQ)
printk(KERN_DEBUG "[%s] IRQ nr: %lu rised", MODULE_NAME, irq);
#endif
if (unlikely(irq != irq_data->irq_nr_with_offset)) {
#if defined(DBG_IRQ)
printk(KERN_DEBUG " -> IRQ nr: %lu calld the wrong handler (Handler irq nr: %lu)", irq, irq_data->irq_nr_with_offset);
#endif
return IRQ_NONE;
}