hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
/*
 * Analogy for Linux, buffer related features
 *
 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
 * Copyright (C) 2008 Alexis Berlemont <alexis.berlemont@free.fr>
 *
 * Xenomai is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Xenomai is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Xenomai; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/mman.h>
#include <linux/vmalloc.h>
#include <asm/errno.h>
#include <asm/io.h>
#include <rtdm/analogy/device.h>
 
/* --- Initialization functions (init, alloc, free) --- */
 
/* The buffer charactistic is very close to the Comedi one: it is
   allocated with vmalloc() and all physical addresses of the pages which
   compose the virtual buffer are hold in a table */
 
void a4l_free_buffer(struct a4l_buffer * buf_desc)
{
   __a4l_dbg(1, core_dbg, "buf=%p buf->buf=%p\n", buf_desc, buf_desc->buf);
 
   if (buf_desc->pg_list != NULL) {
       rtdm_free(buf_desc->pg_list);
       buf_desc->pg_list = NULL;
   }
 
   if (buf_desc->buf != NULL) {
       char *vaddr, *vabase = buf_desc->buf;
       for (vaddr = vabase; vaddr < vabase + buf_desc->size;
            vaddr += PAGE_SIZE)
           ClearPageReserved(vmalloc_to_page(vaddr));
       vfree(buf_desc->buf);
       buf_desc->buf = NULL;
   }
}
 
int a4l_alloc_buffer(struct a4l_buffer *buf_desc, int buf_size)
{
   int ret = 0;
   char *vaddr, *vabase;
 
   buf_desc->size = buf_size;
   buf_desc->size = PAGE_ALIGN(buf_desc->size);
 
   buf_desc->buf = vmalloc_32(buf_desc->size);
   if (buf_desc->buf == NULL) {
       ret = -ENOMEM;
       goto out_virt_contig_alloc;
   }
 
   vabase = buf_desc->buf;
 
   for (vaddr = vabase; vaddr < vabase + buf_desc->size;
        vaddr += PAGE_SIZE)
       SetPageReserved(vmalloc_to_page(vaddr));
 
   buf_desc->pg_list = rtdm_malloc(((buf_desc->size) >> PAGE_SHIFT) *
                   sizeof(unsigned long));
   if (buf_desc->pg_list == NULL) {
       ret = -ENOMEM;
       goto out_virt_contig_alloc;
   }
 
   for (vaddr = vabase; vaddr < vabase + buf_desc->size;
        vaddr += PAGE_SIZE)
       buf_desc->pg_list[(vaddr - vabase) >> PAGE_SHIFT] =
           (unsigned long) page_to_phys(vmalloc_to_page(vaddr));
 
   __a4l_dbg(1, core_dbg, "buf=%p buf->buf=%p\n", buf_desc, buf_desc->buf);
 
out_virt_contig_alloc:
   if (ret != 0)
       a4l_free_buffer(buf_desc);
 
   return ret;
}
 
static void a4l_reinit_buffer(struct a4l_buffer *buf_desc)
{
   /* No command to process yet */
   buf_desc->cur_cmd = NULL;
 
   /* No more (or not yet) linked with a subdevice */
   buf_desc->subd = NULL;
 
   /* Initializes counts and flags */
   buf_desc->end_count = 0;
   buf_desc->prd_count = 0;
   buf_desc->cns_count = 0;
   buf_desc->tmp_count = 0;
   buf_desc->mng_count = 0;
 
   /* Flush pending events */
   buf_desc->flags = 0;
   a4l_flush_sync(&buf_desc->sync);
}
 
void a4l_init_buffer(struct a4l_buffer *buf_desc)
{
   memset(buf_desc, 0, sizeof(struct a4l_buffer));
   a4l_init_sync(&buf_desc->sync);
   a4l_reinit_buffer(buf_desc);
}
 
void a4l_cleanup_buffer(struct a4l_buffer *buf_desc)
{
   a4l_cleanup_sync(&buf_desc->sync);
}
 
int a4l_setup_buffer(struct a4l_device_context *cxt, struct a4l_cmd_desc *cmd)
{
   struct a4l_buffer *buf_desc = cxt->buffer;
   int i;
 
   /* Retrieve the related subdevice */
   buf_desc->subd = a4l_get_subd(cxt->dev, cmd->idx_subd);
   if (buf_desc->subd == NULL) {
       __a4l_err("a4l_setup_buffer: subdevice index "
             "out of range (%d)\n", cmd->idx_subd);
       return -EINVAL;
   }
 
   if (test_and_set_bit(A4L_SUBD_BUSY_NR, &buf_desc->subd->status)) {
       __a4l_err("a4l_setup_buffer: subdevice %d already busy\n",
             cmd->idx_subd);
       return -EBUSY;
   }
 
   /* Checks if the transfer system has to work in bulk mode */
   if (cmd->flags & A4L_CMD_BULK)
       set_bit(A4L_BUF_BULK_NR, &buf_desc->flags);
 
   /* Sets the working command */
   buf_desc->cur_cmd = cmd;
 
   /* Link the subdevice with the context's buffer */
   buf_desc->subd->buf = buf_desc;
 
   /* Computes the count to reach, if need be */
   if (cmd->stop_src == TRIG_COUNT) {
       for (i = 0; i < cmd->nb_chan; i++) {
           struct a4l_channel *chft;
           chft = a4l_get_chfeat(buf_desc->subd,
                         CR_CHAN(cmd->chan_descs[i]));
           buf_desc->end_count += chft->nb_bits / 8;
       }
       buf_desc->end_count *= cmd->stop_arg;
   }
 
   __a4l_dbg(1, core_dbg, "end_count=%lu\n", buf_desc->end_count);
 
   return 0;
}
 
void a4l_cancel_buffer(struct a4l_device_context *cxt)
{
   struct a4l_buffer *buf_desc = cxt->buffer;
   struct a4l_subdevice *subd = buf_desc->subd;
 
   if (!subd || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return;
 
   /* If a "cancel" function is registered, call it
      (Note: this function is called before having checked
      if a command is under progress; we consider that
      the "cancel" function can be used as as to (re)initialize
      some component) */
   if (subd->cancel != NULL)
       subd->cancel(subd);
 
   if (buf_desc->cur_cmd != NULL) {
       a4l_free_cmddesc(buf_desc->cur_cmd);
       rtdm_free(buf_desc->cur_cmd);
       buf_desc->cur_cmd = NULL;
   }
 
   a4l_reinit_buffer(buf_desc);
 
   clear_bit(A4L_SUBD_BUSY_NR, &subd->status);
   subd->buf = NULL;
}
 
/* --- Munge related function --- */
 
int a4l_get_chan(struct a4l_subdevice *subd)
{
   int i, j, tmp_count, tmp_size = 0;
   struct a4l_cmd_desc *cmd;
 
   cmd = a4l_get_cmd(subd);
   if (!cmd)
       return -EINVAL;
 
   /* There is no need to check the channel idx,
      it has already been controlled in command_test */
 
   /* We assume channels can have different sizes;
      so, we have to compute the global size of the channels
      in this command... */
   for (i = 0; i < cmd->nb_chan; i++) {
       j = (subd->chan_desc->mode != A4L_CHAN_GLOBAL_CHANDESC) ?
           CR_CHAN(cmd->chan_descs[i]) : 0;
       tmp_size += subd->chan_desc->chans[j].nb_bits;
   }
 
   /* Translation bits -> bytes */
   tmp_size /= 8;
 
   tmp_count = subd->buf->mng_count % tmp_size;
 
   /* Translation bytes -> bits */
   tmp_count *= 8;
 
   /* ...and find the channel the last munged sample
      was related with */
   for (i = 0; tmp_count > 0 && i < cmd->nb_chan; i++) {
       j = (subd->chan_desc->mode != A4L_CHAN_GLOBAL_CHANDESC) ?
           CR_CHAN(cmd->chan_descs[i]) : 0;
       tmp_count -= subd->chan_desc->chans[j].nb_bits;
   }
 
   if (tmp_count == 0)
       return i;
   else
       return -EINVAL;
}
 
/* --- Transfer / copy functions --- */
 
/* The following functions are explained in the Doxygen section
   "Buffer management services" in driver_facilities.c */
 
int a4l_buf_prepare_absput(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_input(subd))
       return -EINVAL;
 
   return __pre_abs_put(buf, count);
}
 
 
int a4l_buf_commit_absput(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_input(subd))
       return -EINVAL;
 
   return __abs_put(buf, count);
}
 
int a4l_buf_prepare_put(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_input(subd))
       return -EINVAL;
 
   return __pre_put(buf, count);
}
 
int a4l_buf_commit_put(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_input(subd))
       return -EINVAL;
 
   return __put(buf, count);
}
 
int a4l_buf_put(struct a4l_subdevice *subd, void *bufdata, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
   int err;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_input(subd))
       return -EINVAL;
 
   if (__count_to_put(buf) < count)
       return -EAGAIN;
 
   err = __produce(NULL, buf, bufdata, count);
   if (err < 0)
       return err;
 
   err = __put(buf, count);
 
   return err;
}
 
int a4l_buf_prepare_absget(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_output(subd))
       return -EINVAL;
 
   return __pre_abs_get(buf, count);
}
 
int a4l_buf_commit_absget(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_output(subd))
       return -EINVAL;
 
   return __abs_get(buf, count);
}
 
int a4l_buf_prepare_get(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_output(subd))
       return -EINVAL;
 
   return __pre_get(buf, count);
}
 
int a4l_buf_commit_get(struct a4l_subdevice *subd, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
 
   /* Basic checkings */
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_output(subd))
       return -EINVAL;
 
   return __get(buf, count);
}
 
int a4l_buf_get(struct a4l_subdevice *subd, void *bufdata, unsigned long count)
{
   struct a4l_buffer *buf = subd->buf;
   int err;
 
   /* Basic checkings */
 
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (!a4l_subd_is_output(subd))
       return -EINVAL;
 
   if (__count_to_get(buf) < count)
       return -EAGAIN;
 
   /* Update the counter */
   err = __consume(NULL, buf, bufdata, count);
   if (err < 0)
       return err;
 
   /* Perform the transfer */
   err = __get(buf, count);
 
   return err;
}
 
int a4l_buf_evt(struct a4l_subdevice *subd, unsigned long evts)
{
   struct a4l_buffer *buf = subd->buf;
   int tmp;
   unsigned long wake = 0, count = ULONG_MAX;
 
   /* Warning: here, there may be a condition race : the cancel
      function is called by the user side and a4l_buf_evt and all
      the a4l_buf_... functions are called by the kernel
      side. Nonetheless, the driver should be in charge of such
      race conditions, not the framework */
 
   /* Basic checking */
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   /* Here we save the data count available for the user side */
   if (evts == 0) {
       count = a4l_subd_is_input(subd) ?
           __count_to_get(buf) : __count_to_put(buf);
       wake = __count_to_end(buf) < buf->wake_count ?
           __count_to_end(buf) : buf->wake_count;
   } else {
       /* Even if it is a little more complex, atomic
          operations are used so as to prevent any kind of
          corner case */
       while ((tmp = ffs(evts) - 1) != -1) {
           set_bit(tmp, &buf->flags);
           clear_bit(tmp, &evts);
       }
   }
 
   if (count >= wake)
       /* Notify the user-space side */
       a4l_signal_sync(&buf->sync);
 
   return 0;
}
 
unsigned long a4l_buf_count(struct a4l_subdevice *subd)
{
   struct a4l_buffer *buf = subd->buf;
   unsigned long ret = 0;
 
   /* Basic checking */
   if (!buf || !test_bit(A4L_SUBD_BUSY_NR, &subd->status))
       return -ENOENT;
 
   if (a4l_subd_is_input(subd))
       ret = __count_to_put(buf);
   else if (a4l_subd_is_output(subd))
       ret = __count_to_get(buf);
 
   return ret;
}
 
/* --- Mmap functions --- */
 
void a4l_map(struct vm_area_struct *area)
{
   unsigned long *status = (unsigned long *)area->vm_private_data;
   set_bit(A4L_BUF_MAP_NR, status);
}
 
void a4l_unmap(struct vm_area_struct *area)
{
   unsigned long *status = (unsigned long *)area->vm_private_data;
   clear_bit(A4L_BUF_MAP_NR, status);
}
 
static struct vm_operations_struct a4l_vm_ops = {
   .open = a4l_map,
   .close = a4l_unmap,
};
 
int a4l_ioctl_mmap(struct a4l_device_context *cxt, void *arg)
{
   struct rtdm_fd *fd = rtdm_private_to_fd(cxt);
   a4l_mmap_t map_cfg;
   struct a4l_device *dev;
   struct a4l_buffer *buf;
   int ret;
 
   /* The mmap operation cannot be performed in a
      real-time context */
   if (rtdm_in_rt_context()) {
       return -ENOSYS;
   }
 
   dev = a4l_get_dev(cxt);
   buf = cxt->buffer;
 
   /* Basic checkings */
 
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_ioctl_mmap: cannot mmap on "
             "an unattached device\n");
       return -EINVAL;
   }
 
   if (test_bit(A4L_BUF_MAP_NR, &buf->flags)) {
       __a4l_err("a4l_ioctl_mmap: buffer already mapped\n");
       return -EBUSY;
   }
 
   if (rtdm_safe_copy_from_user(fd,
                    &map_cfg, arg, sizeof(a4l_mmap_t)) != 0)
       return -EFAULT;
 
   /* Check the size to be mapped */
   if ((map_cfg.size & ~(PAGE_MASK)) != 0 || map_cfg.size > buf->size)
       return -EFAULT;
 
   /* All the magic is here */
   ret = rtdm_mmap_to_user(fd,
               buf->buf,
               map_cfg.size,
               PROT_READ | PROT_WRITE,
               &map_cfg.ptr, &a4l_vm_ops, &buf->flags);
 
   if (ret < 0) {
       __a4l_err("a4l_ioctl_mmap: internal error, "
             "rtdm_mmap_to_user failed (err=%d)\n", ret);
       return ret;
   }
 
   return rtdm_safe_copy_to_user(fd,
                     arg, &map_cfg, sizeof(a4l_mmap_t));
}
 
/* --- IOCTL / FOPS functions --- */
 
int a4l_ioctl_cancel(struct a4l_device_context * cxt, void *arg)
{
   unsigned int idx_subd = (unsigned long)arg;
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_subdevice *subd;
 
   /* Basically check the device */
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_ioctl_cancel: operation not supported on "
             "an unattached device\n");
       return -EINVAL;
   }
 
   if (cxt->buffer->subd == NULL) {
       __a4l_err("a4l_ioctl_cancel: "
             "no acquisition to cancel on this context\n");
       return -EINVAL;
   }
 
   if (idx_subd >= dev->transfer.nb_subd) {
       __a4l_err("a4l_ioctl_cancel: bad subdevice index\n");
       return -EINVAL;
   }
 
   subd = dev->transfer.subds[idx_subd];
 
   if (subd != cxt->buffer->subd) {
       __a4l_err("a4l_ioctl_cancel: "
             "current context works on another subdevice "
             "(%d!=%d)\n", cxt->buffer->subd->idx, subd->idx);
       return -EINVAL;
   }
 
   a4l_cancel_buffer(cxt);
   return 0;
}
 
/* The ioctl BUFCFG is only useful for changing the size of the
   asynchronous buffer.
   (BUFCFG = free of the current buffer + allocation of a new one) */
 
int a4l_ioctl_bufcfg(struct a4l_device_context * cxt, void *arg)
{
   struct rtdm_fd *fd = rtdm_private_to_fd(cxt);
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   struct a4l_subdevice *subd = buf->subd;
   a4l_bufcfg_t buf_cfg;
 
   /* As Linux API is used to allocate a virtual buffer,
      the calling process must not be in primary mode */
   if (rtdm_in_rt_context()) {
       return -ENOSYS;
   }
 
   /* Basic checking */
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_ioctl_bufcfg: unattached device\n");
       return -EINVAL;
   }
 
   if (rtdm_safe_copy_from_user(fd,
                    &buf_cfg,
                    arg, sizeof(a4l_bufcfg_t)) != 0)
       return -EFAULT;
 
   if (buf_cfg.buf_size > A4L_BUF_MAXSIZE) {
       __a4l_err("a4l_ioctl_bufcfg: buffer size too big (<=16MB)\n");
       return -EINVAL;
   }
 
   if (buf_cfg.idx_subd == A4L_BUF_DEFMAGIC) {
       cxt->dev->transfer.default_bufsize = buf_cfg.buf_size;
       return 0;
   }
 
   if (subd && test_bit(A4L_SUBD_BUSY_NR, &subd->status)) {
       __a4l_err("a4l_ioctl_bufcfg: acquisition in progress\n");
       return -EBUSY;
   }
 
   if (test_bit(A4L_BUF_MAP, &buf->flags)) {
       __a4l_err("a4l_ioctl_bufcfg: please unmap before "
             "configuring buffer\n");
       return -EPERM;
   }
 
   /* Free the buffer... */
   a4l_free_buffer(buf);
 
   /* ...to reallocate it */
   return a4l_alloc_buffer(buf, buf_cfg.buf_size);
}
 
/* The ioctl BUFCFG2 allows the user space process to define the
   minimal amount of data which should trigger a wake-up. If the ABI
   could be broken, this facility would be handled by the original
   BUFCFG ioctl. At the next major release, this ioctl will vanish. */
 
int a4l_ioctl_bufcfg2(struct a4l_device_context * cxt, void *arg)
{
   struct rtdm_fd *fd = rtdm_private_to_fd(cxt);
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   a4l_bufcfg2_t buf_cfg;
 
   /* Basic checking */
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_ioctl_bufcfg2: unattached device\n");
       return -EINVAL;
   }
 
   if (rtdm_safe_copy_from_user(fd,
                    &buf_cfg,
                    arg, sizeof(a4l_bufcfg2_t)) != 0)
       return -EFAULT;
 
   if (buf_cfg.wake_count > buf->size) {
       __a4l_err("a4l_ioctl_bufcfg2: "
             "wake-up threshold too big (> buffer size: %lu)\n",
             buf->size);
       return -EINVAL;
   }
 
   buf->wake_count = buf_cfg.wake_count;
 
   return 0;
}
 
/* The BUFINFO ioctl provides two basic roles:
   - tell the user app the size of the asynchronous buffer
   - display the read/write counters (how many bytes to read/write) */
 
int a4l_ioctl_bufinfo(struct a4l_device_context * cxt, void *arg)
{
   struct rtdm_fd *fd = rtdm_private_to_fd(cxt);
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   struct a4l_subdevice *subd = buf->subd;
   a4l_bufinfo_t info;
 
   unsigned long tmp_cnt;
   int ret;
 
   if (!rtdm_in_rt_context() && rtdm_rt_capable(fd))
       return -ENOSYS;
 
   /* Basic checking */
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_ioctl_bufinfo: unattached device\n");
       return -EINVAL;
   }
 
   if (rtdm_safe_copy_from_user(fd,
                    &info, arg, sizeof(a4l_bufinfo_t)) != 0)
       return -EFAULT;
 
 
   /* If a transfer is not occuring, simply return buffer
      informations, otherwise make the transfer progress */
   if (!subd || !test_bit(A4L_SUBD_BUSY_NR, &subd->status)) {
       info.rw_count = 0;
       goto a4l_ioctl_bufinfo_out;
   }
 
   ret = __handle_event(buf);
 
   if (a4l_subd_is_input(subd)) {
 
       /* Updates consume count if rw_count is not null */
       if (info.rw_count != 0)
           buf->cns_count += info.rw_count;
 
       /* Retrieves the data amount to read */
       tmp_cnt = info.rw_count = __count_to_get(buf);
 
       __a4l_dbg(1, core_dbg, "count to read=%lu\n", tmp_cnt);
 
       if ((ret < 0 && ret != -ENOENT) ||
           (ret == -ENOENT && tmp_cnt == 0)) {
           a4l_cancel_buffer(cxt);
           return ret;
       }
   } else if (a4l_subd_is_output(subd)) {
 
       if (ret < 0) {
           a4l_cancel_buffer(cxt);
           if (info.rw_count != 0)
               return ret;
       }
 
       /* If rw_count is not null,
          there is something to write / munge  */
       if (info.rw_count != 0 && info.rw_count <= __count_to_put(buf)) {
 
           /* Updates the production pointer */
           buf->prd_count += info.rw_count;
 
           /* Sets the munge count */
           tmp_cnt = info.rw_count;
       } else
           tmp_cnt = 0;
 
       /* Retrieves the data amount which is writable */
       info.rw_count = __count_to_put(buf);
 
       __a4l_dbg(1, core_dbg, " count to write=%lu\n", info.rw_count);
 
   } else {
       __a4l_err("a4l_ioctl_bufinfo: inappropriate subdevice\n");
       return -EINVAL;
   }
 
   /* Performs the munge if need be */
   if (subd->munge != NULL) {
 
       /* Call the munge callback */
       __munge(subd, subd->munge, buf, tmp_cnt);
 
       /* Updates munge count */
       buf->mng_count += tmp_cnt;
   }
 
a4l_ioctl_bufinfo_out:
 
   /* Sets the buffer size */
   info.buf_size = buf->size;
 
   /* Sends the structure back to user space */
   if (rtdm_safe_copy_to_user(fd,
                  arg, &info, sizeof(a4l_bufinfo_t)) != 0)
       return -EFAULT;
 
   return 0;
}
 
/* The ioctl BUFINFO2 tells the user application the minimal amount of
data which should trigger a wake-up. If the ABI could be broken, this
facility would be handled by the original BUFINFO ioctl. At the next
major release, this ioctl will vanish. */
 
int a4l_ioctl_bufinfo2(struct a4l_device_context * cxt, void *arg)
{
   struct rtdm_fd *fd = rtdm_private_to_fd(cxt);
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   a4l_bufcfg2_t buf_cfg;
 
   /* Basic checking */
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_ioctl_bufcfg2: unattached device\n");
       return -EINVAL;
   }
 
   buf_cfg.wake_count = buf->wake_count;
 
   if (rtdm_safe_copy_to_user(fd,
                  arg, &buf_cfg, sizeof(a4l_bufcfg2_t)) != 0)
       return -EFAULT;
 
   return 0;
}
 
/* The function a4l_read_buffer can be considered as the kernel entry
   point of the RTDM syscall read. This syscall is supposed to be used
   only during asynchronous acquisitions */
ssize_t a4l_read_buffer(struct a4l_device_context * cxt, void *bufdata, size_t nbytes)
{
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   struct a4l_subdevice *subd = buf->subd;
   ssize_t count = 0;
 
   /* Basic checkings */
 
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_read: unattached device\n");
       return -EINVAL;
   }
 
   if (!subd || !test_bit(A4L_SUBD_BUSY_NR, &subd->status)) {
       __a4l_err("a4l_read: idle subdevice on this context\n");
       return -ENOENT;
   }
 
   if (!a4l_subd_is_input(subd)) {
       __a4l_err("a4l_read: operation requires an input subdevice \n");
       return -EINVAL;
   }
 
   while (count < nbytes) {
 
       unsigned long tmp_cnt;
 
       /* Check the events */
       int ret = __handle_event(buf);
 
       __dump_buffer_counters(buf);
 
       /* Compute the data amount to copy */
       tmp_cnt = __count_to_get(buf);
 
       /* Check tmp_cnt count is not higher than
          the global count to read */
       if (tmp_cnt > nbytes - count)
           tmp_cnt = nbytes - count;
 
       /* We check whether there is an error */
       if (ret < 0 && ret != -ENOENT) {
           __a4l_err("a4l_read: failed to handle event %d \n", ret);
           a4l_cancel_buffer(cxt);
           count = ret;
           goto out_a4l_read;
       }
 
       /* We check whether the acquisition is over */
       if (ret == -ENOENT && tmp_cnt == 0) {
           __a4l_info("a4l_read: acquisition done - all data "
                  "requested by the client was delivered \n");
           a4l_cancel_buffer(cxt);
           count = 0;
           goto out_a4l_read;
       }
 
       if (tmp_cnt > 0) {
 
           /* Performs the munge if need be */
           if (subd->munge != NULL) {
               __munge(subd, subd->munge, buf, tmp_cnt);
 
               /* Updates munge count */
               buf->mng_count += tmp_cnt;
           }
 
           /* Performs the copy */
           ret = __consume(cxt, buf, bufdata + count, tmp_cnt);
 
           if (ret < 0) {
               count = ret;
               goto out_a4l_read;
           }
 
           /* Updates consume count */
           buf->cns_count += tmp_cnt;
           a4l_dbg(1, core_dbg, dev, "buf->cns_cnt=%ld \n", buf->cns_count);
 
           /* Updates the return value */
           count += tmp_cnt;
 
           /* If the driver does not work in bulk mode,
              we must leave this function */
           if (!test_bit(A4L_BUF_BULK, &buf->flags))
               goto out_a4l_read;
       }
       else {
           /* If the acquisition is not over, we must not
              leave the function without having read a least byte */
           ret = a4l_wait_sync(&(buf->sync), rtdm_in_rt_context());
           if (ret < 0) {
               if (ret == -ERESTARTSYS)
                   ret = -EINTR;
               count = ret;
               goto out_a4l_read;
           }
       }
   }
 
out_a4l_read:
 
   return count;
}
 
/* The function a4l_write_buffer can be considered as the kernel entry
   point of the RTDM syscall write. This syscall is supposed to be
   used only during asynchronous acquisitions */
ssize_t a4l_write_buffer(struct a4l_device_context *cxt, const void *bufdata, size_t nbytes)
{
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   struct a4l_subdevice *subd = buf->subd;
   ssize_t count = 0;
 
   /* Basic checkings */
 
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_write: unattached device\n");
       return -EINVAL;
   }
 
   if (!subd || !test_bit(A4L_SUBD_BUSY_NR, &subd->status)) {
       __a4l_err("a4l_write: idle subdevice on this context\n");
       return -ENOENT;
   }
 
   if (!a4l_subd_is_output(subd)) {
       __a4l_err("a4l_write: operation requires an output subdevice \n");
       return -EINVAL;
   }
 
   while (count < nbytes) {
 
       unsigned long tmp_cnt;
 
       /* Check the events */
       int ret = __handle_event(buf);
 
       __dump_buffer_counters(buf);
 
       /* Compute the data amount to copy */
       tmp_cnt = __count_to_put(buf);
 
       /* Check tmp_cnt count is not higher than
          the global count to write */
       if (tmp_cnt > nbytes - count)
           tmp_cnt = nbytes - count;
 
       if (ret < 0) {
           count = (ret == -ENOENT) ? -EINVAL : ret;
           __a4l_err("a4l_write: failed to handle event %d \n", ret);
           a4l_cancel_buffer(cxt);
           goto out_a4l_write;
       }
 
       if (tmp_cnt > 0) {
 
 
           /* Performs the copy */
           ret = __produce(cxt,
                   buf, (void *)bufdata + count, tmp_cnt);
           if (ret < 0) {
               count = ret;
               goto out_a4l_write;
           }
 
           /* Performs the munge if need be */
           if (subd->munge != NULL) {
               __munge(subd, subd->munge, buf, tmp_cnt);
 
               /* Updates munge count */
               buf->mng_count += tmp_cnt;
           }
 
           /* Updates produce count */
           buf->prd_count += tmp_cnt;
           a4l_dbg(1, core_dbg, dev , "buf->prd_cnt=%ld \n", buf->prd_count);
 
           /* Updates the return value */
           count += tmp_cnt;
 
           /* If the driver does not work in bulk mode,
              we must leave this function */
           if (!test_bit(A4L_BUF_BULK, &buf->flags))
               goto out_a4l_write;
       } else {
           /* The buffer is full, we have to wait for a slot to free */
           ret = a4l_wait_sync(&(buf->sync), rtdm_in_rt_context());
           if (ret < 0) {
               __a4l_err("a4l_write: failed to wait for free slot (%d)\n", ret);
               if (ret == -ERESTARTSYS)
                   ret = -EINTR;
               count = ret;
               goto out_a4l_write;
           }
       }
   }
 
out_a4l_write:
 
   return count;
}
 
int a4l_select(struct a4l_device_context *cxt,
          rtdm_selector_t *selector,
          enum rtdm_selecttype type, unsigned fd_index)
{
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   struct a4l_subdevice *subd = buf->subd;
 
   /* Basic checkings */
 
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_select: unattached device\n");
       return -EINVAL;
   }
 
   if (!subd || !test_bit(A4L_SUBD_BUSY, &subd->status)) {
       __a4l_err("a4l_select: idle subdevice on this context\n");
       return -ENOENT;
   }
 
   /* Check the RTDM select type
      (RTDM_SELECTTYPE_EXCEPT is not supported) */
 
   if(type != RTDM_SELECTTYPE_READ &&
      type != RTDM_SELECTTYPE_WRITE) {
       __a4l_err("a4l_select: wrong select argument\n");
       return -EINVAL;
   }
 
   if (type == RTDM_SELECTTYPE_READ && !a4l_subd_is_input(subd)) {
       __a4l_err("a4l_select: current context "
             "does not work with an input subdevice\n");
       return -EINVAL;
   }
 
   if (type == RTDM_SELECTTYPE_WRITE && !a4l_subd_is_output(subd)) {
       __a4l_err("a4l_select: current context "
             "does not work with an input subdevice\n");
       return -EINVAL;
   }
 
   /* Performs a bind on the Analogy synchronization element */
   return a4l_select_sync(&(buf->sync), selector, type, fd_index);
}
 
int a4l_ioctl_poll(struct a4l_device_context * cxt, void *arg)
{
   struct rtdm_fd *fd = rtdm_private_to_fd(cxt);
   int ret = 0;
   unsigned long tmp_cnt = 0;
   struct a4l_device *dev = a4l_get_dev(cxt);
   struct a4l_buffer *buf = cxt->buffer;
   struct a4l_subdevice *subd = buf->subd;
   a4l_poll_t poll;
 
   if (!rtdm_in_rt_context() && rtdm_rt_capable(fd))
       return -ENOSYS;
 
   /* Basic checking */
 
   if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) {
       __a4l_err("a4l_poll: unattached device\n");
       return -EINVAL;
   }
 
   if (!subd || !test_bit(A4L_SUBD_BUSY_NR, &subd->status)) {
       __a4l_err("a4l_poll: idle subdevice on this context\n");
       return -ENOENT;
   }
 
   if (rtdm_safe_copy_from_user(fd,
                    &poll, arg, sizeof(a4l_poll_t)) != 0)
       return -EFAULT;
 
   /* Checks the buffer events */
   a4l_flush_sync(&buf->sync);
   ret = __handle_event(buf);
 
   /* Retrieves the data amount to compute
      according to the subdevice type */
   if (a4l_subd_is_input(subd)) {
 
       tmp_cnt = __count_to_get(buf);
 
       /* Check if some error occured */
       if (ret < 0 && ret != -ENOENT) {
           a4l_cancel_buffer(cxt);
           return ret;
       }
 
       /* Check whether the acquisition is over */
       if (ret == -ENOENT && tmp_cnt == 0) {
           a4l_cancel_buffer(cxt);
           return 0;
       }
   } else {
 
       /* If some error was detected, cancel the transfer */
       if (ret < 0) {
           a4l_cancel_buffer(cxt);
           return ret;
       }
 
       tmp_cnt = __count_to_put(buf);
   }
 
   if (poll.arg == A4L_NONBLOCK || tmp_cnt != 0)
       goto out_poll;
 
   if (poll.arg == A4L_INFINITE)
       ret = a4l_wait_sync(&(buf->sync), rtdm_in_rt_context());
   else {
       unsigned long long ns = ((unsigned long long)poll.arg) *
           ((unsigned long long)NSEC_PER_MSEC);
       ret = a4l_timedwait_sync(&(buf->sync), rtdm_in_rt_context(), ns);
   }
 
   if (ret == 0) {
       /* Retrieves the count once more */
       if (a4l_subd_is_input(dev->transfer.subds[poll.idx_subd]))
           tmp_cnt = __count_to_get(buf);
       else
           tmp_cnt = __count_to_put(buf);
   }
   else
       return ret;
 
out_poll:
 
   poll.arg = tmp_cnt;
 
   ret = rtdm_safe_copy_to_user(fd,
                    arg, &poll, sizeof(a4l_poll_t));
 
   return ret;
}