-
Notifications
You must be signed in to change notification settings - Fork 17
/
ctn91xx_mpeg.c
1498 lines (1202 loc) · 43.5 KB
/
ctn91xx_mpeg.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
#include "ctn91xx.h"
#include "ctn91xx_util.h"
#include "ctn91xx_driver.h"
#include "ctn91xx_mpeg.h"
#include "ctn91xx_reset.h"
#include <linux/dma-mapping.h>
#define MPEG_VERIFY 0
#if CHECK_NOTIFY_CC
#define CHECK_NOTIFY_CC_ACCUM_AMOUNT 100
#define CHECK_NOTIFY_SYNC_ACCUM_AMOUNT 1000
void notify_cc_check( ctn91xx_dev_t* dev, int tuner_index, uint8_t* buffer, uint32_t length )
{
int pkts = length/188;
int i = 0;
for (i = 0; i < pkts; i++)
{
uint8_t* pkt = buffer + (188 * i);
uint16_t pid = ((pkt[1] & 0x1f) << 8) | (pkt[2] & 0xff);
uint8_t continuity_counter;
uint16_t expected_cc;
uint8_t has_adaptation = (((pkt[3] >> 5) & 0x1));
uint8_t adaptation_length = pkt[4];
uint8_t adaptation_flags = pkt[5];
if( pid >= 0x1fff ) {
continue;
}
if( pkt[0] != 0x47 ) {
dev->pid_cc_notify[tuner_index][pid].out_of_sync++;
if( dev->pid_cc_notify[tuner_index][pid].out_of_sync % CHECK_NOTIFY_SYNC_ACCUM_AMOUNT == 0 ) {
if( printk_ratelimit() ) {
WARNING("NOTIFY board %d tuner %d pid %#x out_of_sync %d",
dev->board_number,
tuner_index,
pid,
dev->pid_cc_notify[tuner_index][pid].out_of_sync);
}
}
continue;
}
continuity_counter = (pkt[3] & 0x0f);
expected_cc = (dev->pid_cc_notify[tuner_index][pid].last_cc + 1) & 0xf;
if (expected_cc != continuity_counter
&& continuity_counter != dev->pid_cc_notify[tuner_index][pid].last_cc
&& (!has_adaptation || adaptation_length == 0 || !(adaptation_flags & 0x80))) //discontinuity_indicator
{
dev->pid_cc_notify[tuner_index][pid].discontinuities++;
dev->stats.discont_count[tuner_index]++;
/*
if(dev->pid_cc_notify[tuner_index][pid].discontinuities % CHECK_NOTIFY_CC_ACCUM_AMOUNT == 0 || dev->pid_cc_notify[tuner_index][pid].discontinuities == 2) {
if( printk_ratelimit() ) {
WARNING( "NOTIFY board %d tuner %d pid %#x discontinuities %d count %d expected_cc %#01x cc %#01x",
dev->board_number,
tuner_index,
pid,
dev->pid_cc_notify[tuner_index][pid].discontinuities,
dev->pid_cc_notify[tuner_index][pid].count,
expected_cc,
continuity_counter);
}
}*/
}
dev->pid_cc_notify[tuner_index][pid].count++;
dev->pid_cc_notify[tuner_index][pid].last_cc = continuity_counter;
}
}
#endif
void ctn91xx_reinit_mpeg_registers( ctn91xx_dev_t* dev )
{
#if HAS_MPEG_DMA
mpeg_reset( dev );
#endif
}
void ctn91xx_vbuffer_print_internal(ctn91xx_dev_t* dev, vbuffer_t* vbuffer);
void verify_buffer_state(ctn91xx_dev_t* dev, vbuffer_t* vbuffer, int who_called )
{
#if !MPEG_VERIFY
return;
#else
uint32_t j;
uint32_t num_bank_pages = default_num_pages_per_bank( vbuffer->tuner_index );
static uint32_t cnt=0;
uint32_t notify_abs = ( vbuffer->notify_idx < vbuffer->write_idx ? vbuffer->npages + vbuffer->notify_idx : vbuffer->notify_idx );
static uint32_t printed = 0;
if(printed) return;
cnt++;
if( !( notify_abs >= vbuffer->write_idx
&& ( notify_abs <= ( vbuffer->write_idx + num_bank_pages ) ) ) ) {
printed = 1;
WARNING("verify1 failed (%d, %d): notify_index=%d notify_abs=%d write_index=%d num_bank_pages=%d", who_called, cnt, vbuffer->notify_idx, notify_abs, vbuffer->write_idx, num_bank_pages);
}
for (j = 0; j < vbuffer->npages; j++) {
uint32_t j_abs = ( j < vbuffer->write_idx ? vbuffer->npages + j : j);
if(vbuffer->lock_cnt[j]) {
if(!(j_abs >= notify_abs && j_abs < vbuffer->write_idx + 2*num_bank_pages)) {
printed = 1;
WARNING("verify3 failed (%d, %d): j=%d j_abs=%d notify_index=%d notify_abs=%d write_index=%d", who_called, cnt, j, j_abs, vbuffer->notify_idx, notify_abs, vbuffer->write_idx);
}
}
if(vbuffer->dropped[j]) {
if(!(j_abs >= vbuffer->write_idx && j_abs < vbuffer->write_idx + num_bank_pages)) {
printed = 1;
WARNING("verify4 failed (%d, %d): j=%d j_abs=%d notify_index=%d notify_abs=%d write_index=%d", who_called, cnt, j, j_abs, vbuffer->notify_idx, notify_abs, vbuffer->write_idx);
}
}
}
if( printed ) {
ctn91xx_vbuffer_print_internal( dev, vbuffer );
}
#endif
}
#if HAS_MPEG_DMA
int is_filter_stream(int stream_index) {
return (stream_index + CTN91XX_ORIGIN_TUNER0 >= CTN91XX_ORIGIN_FILTER0);
}
int pages_per_mpeg_device(int stream_index) {
switch(stream_index) {
case (0)...(5):
return MPEG_BUFFER_NPAGES;
case (6)...(11):
default:
return FILTER_BUFFER_NPAGES;
}
}
int num_pages_per_bank(ctn91xx_dev_t* dev, int stream_index) {
return default_num_pages_per_bank(stream_index);
}
int num_pages_per_notify(ctn91xx_dev_t* dev, int stream_index) {
if(is_filter_stream(stream_index)) {
return ctn91xx_read8(dev->mpeg_filter_dma_base,
MPEG_FILTER_DMA_PAGES_PER_NOTIFY_BASE + ((stream_index+CTN91XX_ORIGIN_TUNER0-CTN91XX_ORIGIN_FILTER0)*8));
} else {
return ctn91xx_read8(dev->mpeg_dma_base, MPEG_DMA_PAGES_PER_NOTIFY_BASE + (stream_index*8));
}
}
int mpeg_bytes_per_page_from_stream(ctn91xx_dev_t* dev, int stream_index) {
if(is_filter_stream(stream_index)) {
return ctn91xx_read16(dev->mpeg_filter_dma_base,
MPEG_FILTER_DMA_BYTES_PER_PAGE_BASE + ((stream_index+CTN91XX_ORIGIN_TUNER0-CTN91XX_ORIGIN_FILTER0)*2));
} else {
return ctn91xx_read16(dev->mpeg_dma_base, MPEG_DMA_BYTES_PER_PAGE_BASE + (stream_index*2));
}
}
int default_num_pages_per_bank(int stream_index) {
switch(stream_index) {
case (0)...(5):
#if USE_MPEG_NOTIFY
#if USE_LEON
return 2;
#else
return 32;
#endif
#else
return 8;
#endif
case (6)...(11):
#if USE_MPEG_NOTIFY
#if USE_LEON
return 4;
#else
return 8;
#endif
#else
return 1;
#endif
default:
return 0;
}
}
int default_num_pages_per_notify(int stream_index) {
switch(stream_index) {
case (0)...(5):
#if USE_LEON
return 2;
#else
return 8;
#endif
case (6)...(11):
#if USE_MPEG_NOTIFY && USE_LEON
return 1;
#else
return 1;
#endif
default:
return 0;
}
}
int default_mpeg_bytes_per_page_from_stream(int stream_index)
{
switch(stream_index) {
case (0)...(5):
return 3948;
case (6)...(11):
return 188;
default:
return 0;
}
}
int get_addr_list_offset(ctn91xx_dev_t* dev, int stream_index, int bank)
{
int ret = 0;
switch(stream_index) {
case (0)...(5):
ret = 128 + (stream_index * 128 * 4);
if(bank == 1) {
ret += 64*4;
}
break;
case (6)...(11):
ret = 512 + 64*(stream_index + CTN91XX_ORIGIN_TUNER0 - CTN91XX_ORIGIN_FILTER0);
if(bank == 1) {
ret += 8*4;
}
break;
}
return ret;
}
void mpeg_vbuffer_from_tuner_index(uint8_t tuner_index, ctn91xx_dev_t* dev,
vbuffer_t** vbuffer)
{
if( !vbuffer ) {
ERROR("invalid arg");
return;
}
if( tuner_index > NUM_MPEG_DEVICES ) {
*vbuffer = NULL;
return;
}
*vbuffer = &dev->mpeg_buffer[tuner_index];
}
void mpeg_vbuffer_from_tuner(uint8_t tuner, ctn91xx_dev_t* dev,
vbuffer_t** vbuffer)
{
if( !vbuffer ) {
ERROR("invalid arg");
return;
}
if( tuner > CTN91XX_ORIGIN_TUNER0+CTN91XX_ORIGIN_FILTER5) {
*vbuffer = NULL;
return;
}
*vbuffer = &dev->mpeg_buffer[tuner - CTN91XX_ORIGIN_TUNER0];
}
void mpeg_user_cnt(uint8_t minor_num, ctn91xx_dev_t* dev, int** user_cnt, uint8_t* origin)
{
uint8_t device_number = CETON_MINOR_DEVICE_NUMBER( minor_num );
int * local_user_cnt = 0;
uint8_t local_origin = 0;
if (!dev || (!user_cnt && !origin)) {
ERROR("invalid arg");
return;
}
if( device_number < 1 || device_number > ( NUM_MPEG_DEVICES + 1 ) ) {
ERROR("invalid arg");
return;
}
device_number--;
local_origin = device_number + CTN91XX_ORIGIN_TUNER0;
local_user_cnt = &dev->mpeg_user_cnt[device_number];
if (origin) {
*origin = local_origin;
}
if (user_cnt) {
*user_cnt = local_user_cnt;
}
}
void mpeg_vbuffer_from_minor(uint8_t minor_num, ctn91xx_dev_t* dev,
vbuffer_t** vbuffer)
{
uint8_t device_number = CETON_MINOR_DEVICE_NUMBER( minor_num );
if (!dev || !vbuffer) {
ERROR("invalid arg");
return;
}
if( device_number < 1 || device_number > ( NUM_MPEG_DEVICES + 1 ) ) {
ERROR("invalid arg");
return;
}
device_number--;
*vbuffer = &dev->mpeg_buffer[device_number];
}
int* mpeg_user_cnt_from_tuner(ctn91xx_dev_t* dev, uint8_t origin)
{
int* ret = NULL;
if( origin > CTN91XX_ORIGIN_FILTER5 ) {
ERROR("invalid arg");
return NULL;
}
ret = &dev->mpeg_user_cnt[origin - CTN91XX_ORIGIN_TUNER0];
if( ret && ( *ret > 1 || *ret < 0 ) ) {
ERROR("mpeg user cnt invalid tuner: %d cnt %d", origin, *ret);
}
return ret;
}
int ctn91xx_mpeg_dma_stats( ctn91xx_dev_t* dev, unsigned long arg )
{
vbuffer_t* vbuffer = NULL;
dma_stats_t stats;
if( copy_from_user( &stats, (dma_stats_t*)arg, sizeof( dma_stats_t ) ) ) {
return -EIO;
}
mpeg_vbuffer_from_tuner( stats.tuner, dev, &vbuffer );
if( !vbuffer ) {
return -EINVAL;
}
stats.packet_count = vbuffer->packet_count;
if( copy_to_user( (dma_stats_t*)arg, &stats, sizeof( dma_stats_t ) ) ) {
return -EIO;
}
return 0;
}
static int ctn91xx_mpeg_open(struct inode* inode, struct file* filp)
{
ctn91xx_dev_t* dev = NULL;
int ret = 0;
uint8_t tuner;
int* user_cnt = 0;
int minor;
vbuffer_t* vbuffer = NULL;
dev = ctn91xx_lookup_dev_from_file(inode, filp);
minor = iminor(inode);
mpeg_user_cnt(minor, dev, &user_cnt, &tuner);
if(!user_cnt) {
ctn91xx_cleanup_dev_from_file(inode, filp);
return -EIO;
}
mutex_lock(&dev->fd_mutex);
if(!(*user_cnt)) {
(*user_cnt)++;
} else {
ctn91xx_cleanup_dev_from_file(inode, filp);
ret = -EBUSY;
goto cleanup;
}
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
{
spin_lock_w_flags(&vbuffer->lock);
vbuffer->read_idx = vbuffer->notify_idx;
spin_unlock_w_flags(&vbuffer->lock);
}
cleanup:
mutex_unlock(&dev->fd_mutex);
return ret;
}
static int ctn91xx_mpeg_release(struct inode* inode, struct file* filp)
{
ctn91xx_dev_t* dev = NULL;
uint8_t tuner;
int* user_cnt;
int minor;
dev = ctn91xx_lookup_dev_from_file(inode, filp);
minor = iminor(inode);
mpeg_user_cnt(iminor(inode), dev, &user_cnt, &tuner);
if(!user_cnt) {
ctn91xx_cleanup_dev_from_file(inode, filp);
return -EIO;
}
mutex_lock(&dev->fd_mutex);
if((*user_cnt)) {
(*user_cnt)--;
}
mutex_unlock(&dev->fd_mutex);
ctn91xx_cleanup_dev_from_file(inode, filp);
return 0;
}
static int ctn91xx_mpeg_mmap(struct file* filp, struct vm_area_struct* vma)
{
int ret = 0;
ctn91xx_dev_t* dev = NULL;
int minor;
vbuffer_t* vbuffer = NULL;
long length = vma->vm_end - vma->vm_start;
unsigned long start = vma->vm_start;
int page_idx = 0;
dev = ctn91xx_lookup_dev_from_file(NULL, filp);
minor = ctn91xx_lookup_minor_from_file(NULL, filp);
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
if( !vbuffer ) {
return -EINVAL;
}
if ( ( vma->vm_end - vma->vm_start ) > ( vbuffer->npages * PAGE_SIZE ) ) {
ERROR( "requested region size is too big" );
return -1;
}
while( length > 0 ) {
struct page* p = vbuffer->pages[page_idx++];
if( ( ret = vm_insert_page( vma, start, p ) ) < 0 ) {
return ret;
}
start += PAGE_SIZE;
length -= PAGE_SIZE;
}
return ret;
}
//vbuffer->lock needs to be locked when calling this
int mpeg_data_ready(vbuffer_t* vbuffer)
{
return (vbuffer->lock_cnt[vbuffer->read_idx] == 0) && (vbuffer->remaining[vbuffer->read_idx]);
}
int mpeg_data_ready_at_index( vbuffer_t* vbuffer, int idx )
{
return ( vbuffer->lock_cnt[idx] == 0 ) && ( vbuffer->remaining[idx] );
}
#define ACCUM_AMOUNT 100
static unsigned int ctn91xx_mpeg_poll(struct file* filp, struct poll_table_struct* wait)
{
ctn91xx_dev_t* dev = NULL;
int minor;
uint8_t tuner;
int tuner_index;
int* user_cnt;
uint32_t mask = 0;
vbuffer_t* vbuffer = 0;
dev = ctn91xx_lookup_dev_from_file(NULL, filp);
minor = ctn91xx_lookup_minor_from_file(NULL, filp);
mpeg_user_cnt((uint8_t)minor, dev, &user_cnt, &tuner);
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
tuner_index = tuner - CTN91XX_ORIGIN_TUNER0;
if(vbuffer) {
spin_lock_w_flags(&vbuffer->lock);
if(mpeg_data_ready(vbuffer) && *user_cnt) {
mask = POLLIN | POLLRDNORM;
}
spin_unlock_w_flags(&vbuffer->lock);
} else {
printk("vbuffer == NULL\n");
}
//Note: this debug stuff is not synced
if(((dev->stats.drop_count[tuner_index] - 1) % (ACCUM_AMOUNT/10)) == 0) {
if( printk_ratelimit() ) {
printk("(in poll): board %d drop_count %d tuner_index %d mask %d\n",
dev->board_number, dev->stats.drop_count[tuner_index], tuner_index, mask);
}
dev->stats.drop_count[tuner_index]++;
}
poll_wait(filp, &dev->mpeg_wait_queues[tuner_index], wait);
return mask;
}
ssize_t ctn91xx_mpeg_read_common( ctn91xx_dev_t* dev, vbuffer_t* vbuffer, char __user * data, size_t count, int tuner_index, int nonblocking );
ssize_t ctn91xx_mpeg_read(struct file* filp, char __user * data, size_t count, loff_t* offset)
{
int minor;
ctn91xx_dev_t* dev = NULL;
vbuffer_t* vbuffer = NULL;
int* user_cnt;
uint8_t tuner;
int tuner_index;
dev = ctn91xx_lookup_dev_from_file(NULL, filp);
minor = ctn91xx_lookup_minor_from_file(NULL, filp);
mpeg_user_cnt((uint8_t)minor, dev, &user_cnt, &tuner);
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
tuner_index = tuner - CTN91XX_ORIGIN_TUNER0;
return ctn91xx_mpeg_read_common( dev, vbuffer, data, count, tuner_index, filp->f_flags & O_NONBLOCK );
}
ssize_t ctn91xx_mpeg_read_common( ctn91xx_dev_t* dev, vbuffer_t* vbuffer, char __user * data,
size_t count, int tuner_index, int nonblocking)
{
DECLARE_WAITQUEUE(wait, current);
ssize_t retval = 0;
int done = 0;
int error = 0;
size_t saved_cnt = count;
spinlock_t* lock = 0;
if(!vbuffer) {
ERROR("bad vbuffer");
return -EIO;
}
//Note: this debug stuff is not synced
if(((dev->stats.drop_count[tuner_index] - 1) % (ACCUM_AMOUNT/10)) == 0) {
if( printk_ratelimit() ) {
printk("(in read): drop_count %d tuner_index %d\n",
dev->stats.drop_count[tuner_index], tuner_index);
}
dev->stats.drop_count[tuner_index]++;
}
lock = &vbuffer->lock;
add_wait_queue(&dev->mpeg_wait_queues[tuner_index], &wait);
do {
int should_break = 0;
restart_wait:
should_break = 0;
set_current_state(TASK_INTERRUPTIBLE);
//Check for data
{
spin_lock_w_flags(lock);
if(mpeg_data_ready(vbuffer)) {
should_break = 1;
}
spin_unlock_w_flags(lock);
}
if(should_break) {
break;
}
if(nonblocking) {
retval = -EAGAIN;
goto out;
}
if(!dev->mpeg_user_cnt[vbuffer->tuner_index]) {
retval = -EAGAIN;
goto out;
}
if(signal_pending(current)) {
retval = -ERESTARTSYS;
goto out;
}
schedule();
} while(1);
{
spin_lock_w_flags(lock);
while(!done) {
if(!mpeg_data_ready(vbuffer) || !dev->mpeg_user_cnt[vbuffer->tuner_index]) {
//go back and wait for the rest of my data
spin_unlock_w_flags(lock);
if (nonblocking) {
//We already used our data, none is left so just let them know how much they got
goto out;
} else {
goto restart_wait;
}
}
if(vbuffer->remaining[vbuffer->read_idx] >= count) {
size_t offset = vbuffer->sizes[vbuffer->read_idx] - vbuffer->remaining[vbuffer->read_idx];
spin_unlock_w_flags(lock);
if(copy_to_user(&(data[saved_cnt - count]),
&(vbuffer->buffers[vbuffer->read_idx][offset]), count)) {
error = 1;
}
spin_relock_w_flags(lock);
vbuffer->remaining[vbuffer->read_idx] -= count;
retval += count;
done = 1;
} else if(vbuffer->remaining[vbuffer->read_idx] > 0) { //remaining < count
size_t offset = vbuffer->sizes[vbuffer->read_idx] - vbuffer->remaining[vbuffer->read_idx];
spin_unlock_w_flags(lock);
if(copy_to_user(&(data[saved_cnt - count]),
&(vbuffer->buffers[vbuffer->read_idx][offset]), vbuffer->remaining[vbuffer->read_idx])) {
error = 1;
done = 1;
}
spin_relock_w_flags(lock);
retval += vbuffer->remaining[vbuffer->read_idx];
count -= vbuffer->remaining[vbuffer->read_idx];
vbuffer->remaining[vbuffer->read_idx] = 0;
} else {
ERROR("mpeg read, bad state");
error = 1;
spin_unlock_w_flags(lock);
goto out;
}
if(!vbuffer->remaining[vbuffer->read_idx]) {
vbuffer->read_idx = WRAPPED_PAGE_INDEX( vbuffer, vbuffer->read_idx + 1 );
}
}
spin_unlock_w_flags(lock);
}
out:
set_current_state(TASK_RUNNING);
remove_wait_queue(&dev->mpeg_wait_queues[tuner_index], &wait);
if(error) {
retval = -EIO;
}
return retval;
}
int ctn91xx_ioctl_mpeg_read_setup(ctn91xx_dev_t* dev, int minor, unsigned long arg)
{
vbuffer_t* vbuffer = NULL;
mpeg_stream_setup_t setup;
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
if( !vbuffer ) {
ERROR("invalid vbuffer");
return -EINVAL;
}
{
spin_lock_w_flags( &vbuffer->lock );
setup.read_index = vbuffer->read_idx;
spin_unlock_w_flags( &vbuffer->lock );
}
setup.npages = vbuffer->npages;
setup.bytes_per_page = default_mpeg_bytes_per_page_from_stream( vbuffer->tuner_index );
if( copy_to_user( (uint8_t*)arg, &setup, sizeof( mpeg_stream_setup_t ) ) ) {
ERROR("invalid user ptr");
return -EIO;
}
return 0;
}
int ctn91xx_ioctl_mpeg_read_complete(ctn91xx_dev_t* dev, int minor, unsigned long arg);
int ctn91xx_ioctl_mpeg_read_start(ctn91xx_dev_t* dev, int minor, unsigned long arg)
{
ssize_t retval = 0;
int done = 0;
int error = 0;
vbuffer_t* vbuffer = NULL;
mpeg_read_t mpeg_data;
size_t count;
spinlock_t* lock = 0;
if( copy_from_user( &mpeg_data, (mpeg_read_t*) arg, sizeof(mpeg_read_t) ) ) {
return -EINVAL;
}
if( mpeg_data.amount_last_read ) {
ERROR("unless USE_AMOUNT_LAST_READ is set, you shouldn't get here");
if( ctn91xx_ioctl_mpeg_read_complete( dev, minor, arg ) < 0 ) {
return -EINVAL;
}
}
count = mpeg_data.count;
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
if( !vbuffer ) {
return -EINVAL;
}
lock = &vbuffer->lock;
{
int temp_read_index;
spin_lock_w_flags( lock );
temp_read_index = vbuffer->read_idx;
while( !done ) {
int cur_remaining = vbuffer->remaining[temp_read_index];
if( !mpeg_data_ready_at_index( vbuffer, temp_read_index ) ) {
spin_unlock_w_flags( lock );
goto out;
}
if( cur_remaining >= count ) {
cur_remaining -= count;
retval += count;
done = 1;
} else if( cur_remaining > 0 ) { //remaining < count
retval += cur_remaining;
count -= cur_remaining;
cur_remaining = 0;
} else {
ERROR("mpeg read, bad state retval %zd count %zd cur_remaining %d",
retval, count, cur_remaining);
error = 1;
spin_unlock_w_flags(lock);
goto out;
}
if( !cur_remaining ) {
temp_read_index = WRAPPED_PAGE_INDEX(vbuffer, temp_read_index + 1 );
}
}
spin_unlock_w_flags( lock );
}
out:
if( error ) {
retval = -EIO;
}
return retval;
}
int ctn91xx_ioctl_mpeg_read_complete(ctn91xx_dev_t* dev, int minor, unsigned long arg)
{
int done = 0;
int error = 0;
size_t count;
int new_read_index = 0;
vbuffer_t* vbuffer = NULL;
mpeg_read_t mpeg_data;
spinlock_t* lock = 0;
if( copy_from_user( &mpeg_data, (mpeg_read_t*) arg, sizeof(mpeg_read_t) ) ) {
return -EINVAL;
}
count = mpeg_data.amount_last_read;
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
if( !vbuffer ) {
return -EINVAL;
}
new_read_index = vbuffer->read_idx;
lock = &vbuffer->lock;
{
spin_lock_w_flags( lock );
while( !done ) {
if( !mpeg_data_ready( vbuffer ) || !dev->mpeg_user_cnt[vbuffer->tuner_index]) {
spin_unlock_w_flags( lock );
goto out;
}
if( vbuffer->remaining[vbuffer->read_idx] >= count ) {
vbuffer->remaining[vbuffer->read_idx] -= count;
done = 1;
} else if( vbuffer->remaining[vbuffer->read_idx] > 0 ) { //remaining < count
count -= vbuffer->remaining[vbuffer->read_idx];
vbuffer->remaining[vbuffer->read_idx] = 0;
} else {
ERROR("mpeg read, bad state");
error = 1;
spin_unlock_w_flags( lock );
goto out;
}
if( !vbuffer->remaining[vbuffer->read_idx] ) {
vbuffer->read_idx = new_read_index = WRAPPED_PAGE_INDEX( vbuffer, vbuffer->read_idx + 1 );
}
}
spin_unlock_w_flags( lock );
}
out:
if( error ) {
return -EIO;
}
return new_read_index;
}
int ctn91xx_mpeg_send(ctn91xx_dev_t* dev, int minor, unsigned long arg)
{
int ret = 0;
int done = 0;
vbuffer_t* vbuffer = NULL;
mpeg_send_t send;
struct file* out_file = NULL;
spinlock_t* lock = 0;
ssize_t written = 0;
size_t count;
mpeg_vbuffer_from_minor((uint8_t)minor, dev, &vbuffer);
if( !vbuffer ) {
return -EINVAL;
}
if( copy_from_user( &send, (mpeg_send_t*) arg, sizeof(mpeg_send_t) ) ) {
return -EINVAL;
}
count = send.count;
if( copy_from_user( vbuffer->header_buffer, send.header, send.header_length ) ) {
ret =-EINVAL;
goto out;
}
out_file = fget( send.out_fd );
if( !out_file ) {
goto out;
}
if( !out_file->f_op || !out_file->f_op->sendpage ) {
goto fput_out;
}
written = out_file->f_op->sendpage(
out_file,
vbuffer->header_buffer_page,
0, send.header_length,
&out_file->f_pos, 1 /* more */ );
lock = &vbuffer->lock;
{
spin_lock_w_flags( lock );
while( !done ) {
size_t offset = vbuffer->sizes[vbuffer->read_idx] - vbuffer->remaining[vbuffer->read_idx];
if( !mpeg_data_ready( vbuffer ) || !dev->mpeg_user_cnt[vbuffer->tuner_index]) {
spin_unlock_w_flags( lock );
out_file->f_op->sendpage(
out_file,
NULL,
0,
0,
&out_file->f_pos, 0 /* no-more */ );
goto fput_out;
}
if( vbuffer->remaining[vbuffer->read_idx] >= count ) {
spin_unlock_w_flags( lock );
written = out_file->f_op->sendpage(
out_file,
vbuffer->pages[vbuffer->read_idx],
offset,
count,
&out_file->f_pos, 0 /* no-more */ );
if( written < 0 ) {
ERROR("sendpage, written %zd", written);
goto fput_out;
}
spin_relock_w_flags( lock );
vbuffer->remaining[vbuffer->read_idx] -= count;
ret += count;
done = 1;
} else if( vbuffer->remaining[vbuffer->read_idx] > 0 ) { //remaining < count
spin_unlock_w_flags( lock );
written = out_file->f_op->sendpage(
out_file,
vbuffer->pages[vbuffer->read_idx],
offset,
vbuffer->remaining[vbuffer->read_idx],
&out_file->f_pos, 1 /* more */ );
if( written < 0 ) {
ERROR("sendpage, written %zd", written);
goto fput_out;
}
spin_relock_w_flags( lock );
ret += vbuffer->remaining[vbuffer->read_idx];
count -= vbuffer->remaining[vbuffer->read_idx];
vbuffer->remaining[vbuffer->read_idx] = 0;
} else {
ERROR("mpeg send, bad state");
ret = -EINVAL;
spin_unlock_w_flags( lock );
goto fput_out;
}
if( !vbuffer->remaining[vbuffer->read_idx] ) {
vbuffer->read_idx = WRAPPED_PAGE_INDEX( vbuffer, vbuffer->read_idx + 1 );
}
}
spin_unlock_w_flags( lock );
}
fput_out:
fput( out_file );
out:
return ret;
}
ssize_t ctn91xx_mpeg_kernel_read( vbuffer_t* vbuffer, char* data, size_t count )