-
Notifications
You must be signed in to change notification settings - Fork 1
/
sms1xxx-demux.c
1358 lines (1205 loc) · 36.7 KB
/
sms1xxx-demux.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
/* SMS1XXX - Siano DVB-T USB driver for FreeBSD 8.0 and higher:
*
* Copyright (C) 2008-2014, Ganaël Laplanche, http://contribs.martymac.org
*
* This driver contains code taken from the FreeBSD dvbusb driver:
*
* Copyright (C) 2006-2007, Raaf
* Copyright (C) 2004-2006, Patrick Boettcher
*
* This driver contains code taken from the Linux siano driver:
*
* Siano Mobile Silicon, Inc.
* MDTV receiver kernel modules.
* Copyright (C) 2006-2009, Uri Shkolnik
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* * * *
* Demuxer implementation
* (handles dvr0 and demux0.x devices).
* * * *
*/
/* uio(9) stuff */
#include <sys/types.h>
#include <sys/uio.h>
/* mutex(9) stuff */
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
/* EVENTHANDLER(9) stuff */
#include <sys/eventhandler.h>
/* poll(2) stuff */
#include <sys/poll.h>
/* File-descriptor ioctls */
#include <sys/filio.h>
/* Read flags (O_NONBLOCK) */
#include <sys/fcntl.h>
/* Our stuff */
#include "sms1xxx.h"
#include "sms1xxx-demux.h"
#include "sms1xxx-usb.h"
/* Linux stuff */
#include "linux/dvb/dmx.h"
/********************************
* Dvr (private functions only) *
********************************/
static d_open_t sms1xxx_dvr_open;
static d_close_t sms1xxx_dvr_close;
static d_read_t sms1xxx_dvr_read;
static d_poll_t sms1xxx_dvr_poll;
static struct cdevsw sms1xxx_dvr_cdevsw = {
.d_version = D_VERSION,
.d_flags = D_NEEDGIANT,
.d_open = sms1xxx_dvr_open,
.d_close = sms1xxx_dvr_close,
.d_read = sms1xxx_dvr_read,
.d_poll = sms1xxx_dvr_poll,
.d_name = "sms1xxx_dvr",
};
static int
sms1xxx_dvr_open(struct cdev *dev, int flag, int mode, struct thread *p)
{
struct sms1xxx_softc *sc = dev->si_drv1;
#ifdef SMS1XXX_DEBUG
int unit = device_get_unit(sc->sc_dev);
TRACE(TRACE_OPEN,"flag=%d, mode=%d, unit=%d\n", flag, mode, unit);
#endif
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
if(sc->dvr.state & DVR_OPEN)
return (EBUSY);
sc->dvr.state |= DVR_OPEN;
return (0);
}
static int
sms1xxx_dvr_close(struct cdev *dev, int flag, int mode, struct thread *p)
{
struct sms1xxx_softc *sc = dev->si_drv1;
#ifdef SMS1XXX_DEBUG
int unit = device_get_unit(sc->sc_dev);
TRACE(TRACE_OPEN,"flag=%d, mode=%d, filter=%d\n",
flag, mode, unit);
#endif
/* Wake up readers ! */
if(sc->dvr.state & DVR_SLEEP) {
wakeup(&sc->dvr);
}
if(sc->dvr.state & DVR_POLL) {
sc->dvr.state &= ~DVR_POLL;
selwakeuppri(&sc->dvr.rsel,PZERO);
}
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
sc->dvr.state &= ~DVR_OPEN;
return (0);
}
/* sms1xxx_dvr_write_packet(): write a packet to dvr
Always return 0 (packet written or no buffer space left) */
static int
sms1xxx_dvr_write_packet(struct sms1xxx_softc *sc, u8 *p)
{
if(sc->dvr.wavail < PACKET_SIZE) {
sc->dvr.nobufs++;
}
else {
memcpy(sc->dvr.buf+sc->dvr.woff,p,PACKET_SIZE);
sc->dvr.woff += PACKET_SIZE;
if(sc->dvr.woff == sc->dvr.size)
sc->dvr.woff = 0;
mtx_lock(&sc->dvr.lock);
sc->dvr.wavail -= PACKET_SIZE;
sc->dvr.ravail += PACKET_SIZE;
#ifdef SMS1XXX_DIAGNOSTIC
if(sc->dvr.wavail < sc->stats.min_wavail)
sc->stats.min_wavail = sc->dvr.wavail;
if(sc->dvr.ravail > sc->stats.max_ravail)
sc->stats.max_ravail = sc->dvr.ravail;
#endif
mtx_unlock(&sc->dvr.lock);
if(sc->dvr.nobufs != 0) {
WARN("%u packets dropped due to no "
"buffer space\n", sc->dvr.nobufs);
sc->dvr.nobufs = 0;
}
}
/* Wake up readers ! */
if(sc->dvr.ravail >= THRESHOLD) {
if(sc->dvr.state & DVR_SLEEP)
wakeup(&sc->dvr);
if(sc->dvr.state & DVR_POLL) {
sc->dvr.state &= ~DVR_POLL;
selwakeuppri(&sc->dvr.rsel, PZERO);
}
}
return (0);
}
static int
sms1xxx_dvr_read_packets(struct sms1xxx_softc *sc, struct uio *uio)
{
int err = 0;
u32 todo, total;
if(sc->dvr.ravail < THRESHOLD)
return (EBUSY);
TRACE(TRACE_DVR_READ,"wants: %zu bytes, got: %u bytes\n",
uio->uio_resid, sc->dvr.ravail);
total = MIN(uio->uio_resid, sc->dvr.ravail);
todo = MIN(total, sc->dvr.size - sc->dvr.roff);
if(total > sc->dvr.size) {
ERR("total > dvrsize, this shouldn't happen! %d > %d\n",
total, sc->dvr.size);
return (-EOVERFLOW);
}
err = uiomove(sc->dvr.buf + sc->dvr.roff, todo, uio);
if(err)
return (-err);
if(todo < total) {
sc->dvr.roff = total - todo;
err = uiomove(sc->dvr.buf, sc->dvr.roff, uio);
if(err)
return (-err);
}
else {
sc->dvr.roff += total;
}
if(sc->dvr.roff == sc->dvr.size)
sc->dvr.roff = 0;
mtx_lock(&sc->dvr.lock);
sc->dvr.ravail -= total;
sc->dvr.wavail += total;
#ifdef SMS1XXX_DIAGNOSTIC
if(sc->dvr.wavail > sc->stats.max_wavail)
sc->stats.max_wavail = sc->dvr.wavail;
if(sc->dvr.ravail < sc->stats.min_ravail)
sc->stats.min_ravail = sc->dvr.ravail;
#endif
mtx_unlock(&sc->dvr.lock);
return (0);
}
static int
sms1xxx_dvr_read(struct cdev *dev, struct uio *uio, int flag)
{
struct sms1xxx_softc *sc = dev->si_drv1;
int err = 0;
TRACE(TRACE_DVR_READ,"\n");
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
if(sc->dvr.state & DVR_SLEEP) {
ERR("read already in progress\n");
return (EBUSY);
}
while((err = sms1xxx_dvr_read_packets(sc,uio)) > 0) {
if(flag & O_NONBLOCK)
return (EWOULDBLOCK);
sc->dvr.state |= DVR_SLEEP;
err = tsleep(&sc->dvr, PZERO | PCATCH, "dvrrd", 0);
sc->dvr.state &= ~DVR_SLEEP;
if(sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
else if(err)
return (err);
}
return (-err);
}
static int
sms1xxx_dvr_poll(struct cdev *dev, int events, struct thread *p)
{
struct sms1xxx_softc *sc = dev->si_drv1;
int revents = 0;
TRACE(TRACE_DVR_READ,"thread=%p, events=%d\n", p, events);
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return ((events & (POLLIN | POLLOUT | POLLRDNORM |
POLLWRNORM)) | POLLHUP);
}
/* XXX POLLERR POLLPRI */
if(events & (POLLIN | POLLRDNORM)) {
if(sc->dvr.ravail < THRESHOLD) {
selrecord(p,&sc->dvr.rsel);
sc->dvr.state |= DVR_POLL;
TRACE(TRACE_DVR_READ,"will block\n");
}
else {
revents |= events & (POLLIN | POLLRDNORM);
TRACE(TRACE_DVR_READ,"poll not blocking\n");
}
}
if(events & (POLLOUT | POLLWRNORM)) {
/* Write is not allowed, so no point blocking on it */
revents |= events & (POLLOUT | POLLWRNORM);
}
return (revents);
}
/***********
* Demuxer *
***********/
static d_open_t sms1xxx_demux_open;
static d_close_t sms1xxx_demux_close;
static d_ioctl_t sms1xxx_demux_ioctl;
static d_read_t sms1xxx_demux_read;
static d_poll_t sms1xxx_demux_poll;
static struct cdevsw sms1xxx_demux_cdevsw = {
.d_version = D_VERSION,
.d_flags = D_NEEDGIANT | D_NEEDMINOR,
.d_open = sms1xxx_demux_open,
.d_close = sms1xxx_demux_close,
.d_ioctl = sms1xxx_demux_ioctl,
.d_read = sms1xxx_demux_read,
.d_poll = sms1xxx_demux_poll,
.d_name = "sms1xxx_demux",
};
/*****************************
* Demuxer private functions *
*****************************/
/* Callback function for destroy_dev_sched_cb()
Avoids race with dev_rel() when fastly opening/closing demux0 */
static void
sms1xxx_demux_filter_reset(void *arg)
{
struct filter *f = arg;
f->dev = NULL;
f->pid = PIDFREE;
f->type = FILTER_TYPE_NONE;
return;
}
/* Called on each sms1xxx_demux_start() */
static int
sms1xxx_demux_stream_ref(struct sms1xxx_softc *sc)
{
int err = 0;
TRACE(TRACE_USB,"refcnt %d -> %d\n",sc->streamrefs,sc->streamrefs + 1);
sc->streamrefs++;
/*
if(sc->streamrefs == 1) {
err = sms1xxx_usb_stream_start(sc);
if(err)
sc->streamrefs = 0;
}
*/
return (err);
}
/* Called on each sms1xxx_demux_stop() */
static int
sms1xxx_demux_stream_deref(struct sms1xxx_softc *sc)
{
int err = 0;
TRACE(TRACE_USB,"refcnt %d -> %d\n",sc->streamrefs,sc->streamrefs - 1);
sc->streamrefs--;
/*
XXX Find a way to completely stop the device when no more PID set ?
if(sc->streamrefs <= 0) {
err = sms1xxx_usb_stream_stop(sc);
if(err)
sc->streamrefs++;
}
*/
return (err);
}
static int
sms1xxx_demux_start(struct sms1xxx_softc *sc, struct filter *f)
{
int err = 0;
TRACE(TRACE_FILTERS, "sc=%p, filter=%p\n", sc, f);
if(f->pid == PIDEMPTY)
return (EINVAL);
f->pid &= ~PIDSTOPPED;
if(!(f->state & FILTER_STREAMING)) {
if((err = sms1xxx_demux_stream_ref(sc)) == 0)
f->state |= FILTER_STREAMING;
}
return (err);
}
static int
sms1xxx_demux_stop(struct sms1xxx_softc *sc, struct filter *f)
{
int err = 0;
TRACE(TRACE_FILTERS, "sc=%p, filter=%p\n", sc, f);
if(f->pid == PIDEMPTY)
return (EINVAL);
if(f->state & FILTER_STREAMING) {
if((err = sms1xxx_demux_stream_deref(sc)) == 0)
f->state &= ~FILTER_STREAMING;
}
f->pid |= PIDSTOPPED;
return (err);
}
static void
sms1xxx_demux_sectbuf_reset(struct sms1xxx_softc *sc, struct filter *f,
int wake, char *msg)
{
TRACE(TRACE_SECT,"pid: %hu reason: %s\n", f->pid, msg);
mtx_lock(&sc->filterlock);
f->woff = 0;
f->roff = 0;
f->wavail = SECTBUFSIZE;
f->ravail = 0;
f->wtodo = 0;
f->rtodo = 0;
f->size = SECTBUFSIZE;
f->cnt = 0;
mtx_unlock(&sc->filterlock);
if(wake) {
f->state |= FILTER_OVERFLOW;
/* Wake up readers ! */
if(f->state & FILTER_SLEEP)
wakeup(f);
if(f->state & FILTER_POLL) {
f->state &= ~FILTER_POLL;
selwakeuppri(&f->rsel, PZERO);
}
}
}
/* sms1xxx_demux_write_section(): write a section to demux filter
Return 0 if section handled (written or no buffer space left)
Return 1 if section data is corrupt */
static int
sms1xxx_demux_write_section(struct sms1xxx_softc *sc, u8 *p,
struct filter *f)
{
u8 hlen, len, total, count;
u8 *payload;
if(!TS_HAS_PAYLOAD(p))
return (1);
hlen = TS_GET_HDR_LEN(p);
if(hlen >= PACKET_SIZE) {
sms1xxx_demux_sectbuf_reset(sc, f, 1, "corrupt packet");
return (1);
}
payload = p + hlen;
len = PACKET_SIZE - hlen;
if(TS_HAS_PUSI(p)) {
u8 off = TS_GET_SECT_OFF(payload);
if((hlen + off + 3) > PACKET_SIZE) {
sms1xxx_demux_sectbuf_reset(sc, f, 1, "corrupt packet");
return (1);
}
#ifdef SMS1XXX_DIAGNOSTIC
/* XXX Check if the old section is finished */
if(f->wtodo != 0) {
WARN("pid: %hu section not finished but new pusi "
"hlen %d off %d todo %d\n", f->pid,
hlen, off, f->wtodo);
}
#endif
/* Section start */
payload += off; len -= off;
if((TS_GET_SECT_TBLID(payload) & f->mask) != f->value) {
f->wtodo = 0;
return (0);
}
f->wtodo = TS_GET_SECT_LEN(payload);
TRACE(TRACE_SECT,"pid: %hu writing new section len: %d\n",
f->pid, f->wtodo);
}
if(f->wtodo == 0)
return (0);
total = MIN(f->wtodo,len);
if(total > f->wavail) {
#ifdef SMS1XXX_DIAGNOSTIC
WARN("pid: %hu no section buffer space available\n",f->pid);
#endif
sms1xxx_demux_sectbuf_reset(sc, f, 1, "no buffer space");
return (0);
}
if(total > f->size) {
ERR("total > f->size, this shouldn't happen! %d > %d\n",
total,f->size);
return (1);
}
TRACE(TRACE_SECT,"pid: %hu writing %d bytes to section\n",
f->pid,total);
count = MIN(total,f->size - f->woff);
memcpy(f->buf + f->woff,payload,count);
if(count < total) {
f->woff = total - count;
memcpy(f->buf,payload+count,f->woff);
}
else {
f->woff += total;
}
if(f->woff == f->size)
f->woff = 0;
f->wtodo -= total;
if(f->wtodo == 0) {
mtx_lock(&sc->filterlock);
f->wavail -= total;
f->ravail += total;
f->cnt++;
f->state &= ~FILTER_OVERFLOW;
mtx_unlock(&sc->filterlock);
TRACE(TRACE_SECT,"pid: %hu writing section done cnt: %d\n"
,f->pid, f->cnt);
/* Wake up readers ! */
if(f->state & FILTER_SLEEP)
wakeup(f);
if(f->state & FILTER_POLL) {
f->state &= ~FILTER_POLL;
selwakeuppri(&f->rsel, PZERO);
}
}
else {
mtx_lock(&sc->filterlock);
f->wavail -= total;
f->ravail += total;
mtx_unlock(&sc->filterlock);
}
#ifdef SMS1XXX_DIAGNOSTIC
if(total < len && TS_GET_SECT_TBLID(payload+total) != 0xFF)
WARN("possible section(s) discarded\n");
#endif
return (0);
}
/* sms1xxx_demux_write_packet(): write a packet to demux filter
Always return 0 (packet written or no buffer space left) */
static int
sms1xxx_demux_write_packet(struct sms1xxx_softc *sc, u8 *p,
struct filter *f)
{
if(f->wavail < PACKET_SIZE) {
#ifdef SMS1XXX_DIAGNOSTIC
WARN("pid: %hu no buffer space available\n",f->pid);
#endif
sms1xxx_demux_sectbuf_reset(sc, f, 1, "no buffer space");
return (0);
}
TRACE(TRACE_FILTERS,"pid: %hu writing %d bytes to buffer\n",
f->pid,PACKET_SIZE);
memcpy(f->buf + f->woff,p,PACKET_SIZE);
f->woff += PACKET_SIZE;
if(f->woff == f->size)
f->woff = 0;
mtx_lock(&sc->filterlock);
f->wavail -= PACKET_SIZE;
f->ravail += PACKET_SIZE;
/* XXX Update cnt given the remaining bytes available and PACKET_SIZE
instead of counting how many packets have actually been written */
f->cnt = f->ravail / PACKET_SIZE;
f->state &= ~FILTER_OVERFLOW;
mtx_unlock(&sc->filterlock);
TRACE(TRACE_FILTERS,"pid: %hu writing packet done\n" ,f->pid);
/* Wake up readers ! */
if(f->state & FILTER_SLEEP)
wakeup(f);
if(f->state & FILTER_POLL) {
f->state &= ~FILTER_POLL;
selwakeuppri(&f->rsel, PZERO);
}
return (0);
}
static void
sms1xxx_demux_clone(void *arg, struct ucred *cred, char *name,
int namelen, struct cdev **dev)
{
char buf[40];
int filtnr = 0;
int unit;
struct sms1xxx_softc *sc = arg;
TRACE(TRACE_OPEN,"name=%s\n",name);
if (*dev != NULL || sc == NULL)
return;
snprintf(buf,39,"dvb/adapter%d/demux0",device_get_unit(sc->sc_dev));
if(strcmp(name,buf) != 0)
return;
for(filtnr = 0; filtnr < MAX_FILTERS; ++filtnr) {
if(sc->filter[filtnr].pid == PIDFREE)
break;
}
if(filtnr >= MAX_FILTERS) {
ERR("no more filter free\n");
return;
}
if(sc->filter[filtnr].dev != NULL) {
ERR("filter %d not free\n", filtnr);
return;
}
/* Clones are shared among devices, so handle unit numbers
manually by reserving a range of MAX_FILTERS units per-device
(clone_create will fail setting unit number automatically :
unit number assignment is done by crawling sc's (only) clones
and will not detect clones created for other devices, resulting
in trying to set twice the same unit number). */
unit = device_get_unit(sc->sc_dev) * MAX_FILTERS + filtnr;
if(clone_create(&sc->demux_clones, &sms1xxx_demux_cdevsw, &unit, dev, 0)) {
*dev = make_dev_credf(MAKEDEV_REF, &sms1xxx_demux_cdevsw,
unit, NULL, UID_ROOT, GID_WHEEL,
0666, "dvb/adapter%d/demux0.%d",
device_get_unit(sc->sc_dev), filtnr);
if(*dev != NULL) {
TRACE(TRACE_MODULE,"created demux0.%d device, addr=%p\n",
filtnr, *dev);
(*dev)->si_drv1 = sc;
(*dev)->si_drv2 = &sc->filter[filtnr]; /* map filter to device */
sc->filter[filtnr].dev = *dev; /* map device to filter */
sc->filter[filtnr].pid = PIDCLONED;
}
}
}
static int
sms1xxx_demux_open(struct cdev *dev, int flag, int mode, struct thread *p)
{
int err = 0;
struct sms1xxx_softc *sc;
struct filter *f;
TRACE(TRACE_OPEN,"flag=%d, mode=%d, f=%p\n", flag, mode, dev->si_drv2);
sc = dev->si_drv1;
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
f = dev->si_drv2;
if (f == NULL) {
ERR("no filter found\n");
return (ENOENT);
}
if(f->pid != PIDCLONED) {
TRACE(TRACE_OPEN,"busy!\n");
return (EBUSY);
}
err = sms1xxx_usb_ref(sc);
if(!err)
f->pid = PIDEMPTY;
return (err);
}
static int
sms1xxx_demux_read_section(struct sms1xxx_softc *sc, struct filter *f,
struct uio *uio)
{
int err = 0;
u32 roff, rtodo, count, total;
TRACE(TRACE_SECT,"pid: %hu cnt: %d overflow: %s\n", f->pid,
f->cnt, f->state & FILTER_OVERFLOW ? "yes" : "no" );
if(f->cnt == 0)
return (EBUSY);
else if(f->state & FILTER_OVERFLOW) {
f->state &= ~FILTER_OVERFLOW;
return (-EOVERFLOW);
}
mtx_lock(&sc->filterlock);
if(f->rtodo == 0) {
if(f->roff <= (f->size - 3)) {
f->rtodo = TS_GET_SECT_LEN(f->buf + f->roff);
}
else if(f->roff == (f->size - 2)) {
f->rtodo = ((((((f->buf[f->roff+1]) << 8)|(f->buf[0])))
& 0xFFF) + 3);
}
else {
f->rtodo = TS_GET_SECT_LEN(f->buf - 1);
}
TRACE(TRACE_SECT,"pid: %hu reading new section len: %d\n",
f->pid, f->rtodo);
}
rtodo = f->rtodo;
roff = f->roff;
total = MIN(uio->uio_resid,rtodo);
count = MIN(total, f->size - roff);
mtx_unlock(&sc->filterlock);
if(total > f->size) {
ERR("total > fsize, this shouldn't happen! %d > %d\n",
total, f->size);
return (-EOVERFLOW);
}
TRACE(TRACE_SECT,"pid: %hu reading %d bytes from section\n",
f->pid, total);
err = uiomove(f->buf + roff, count, uio);
if(err)
return (-err);
if(count < total) {
roff = total - count;
err = uiomove(f->buf,roff, uio);
if(err)
return (-err);
}
else {
roff += total;
}
if(roff == f->size)
roff = 0;
rtodo -= total;
if(rtodo == 0) {
TRACE(TRACE_SECT,"pid: %hu reading section done cnt: %d\n",
f->pid, f->cnt - 1);
}
mtx_lock(&sc->filterlock);
if(f->state & FILTER_OVERFLOW) {
f->state &= ~FILTER_OVERFLOW;
mtx_unlock(&sc->filterlock);
if(rtodo == 0)
return (0);
else
return (-EOVERFLOW);
}
f->cnt--;
f->rtodo = rtodo;
f->roff = roff;
f->ravail -= total;
f->wavail += total;
mtx_unlock(&sc->filterlock);
return (0);
}
static int
sms1xxx_demux_read_packets(struct sms1xxx_softc *sc, struct filter *f,
struct uio *uio)
{
int err = 0;
u32 todo, total;
TRACE(TRACE_FILTERS,"pid: %hu cnt: %d overflow: %s\n", f->pid,
f->cnt, f->state & FILTER_OVERFLOW ? "yes" : "no" );
if(f->ravail < THRESHOLD)
return (EBUSY);
else if(f->state & FILTER_OVERFLOW) {
f->state &= ~FILTER_OVERFLOW;
return (-EOVERFLOW);
}
mtx_lock(&sc->filterlock);
total = MIN(uio->uio_resid, f->ravail);
todo = MIN(total, f->size - f->roff);
mtx_unlock(&sc->filterlock);
if(total > f->size) {
ERR("total > fsize, this shouldn't happen! %d > %d\n",
total, f->size);
return (-EOVERFLOW);
}
TRACE(TRACE_FILTERS,"pid: %hu reading %d bytes from buffer\n",
f->pid, total);
err = uiomove(f->buf + f->roff, todo, uio);
if(err)
return (-err);
if(todo < total) {
f->roff = total - todo;
err = uiomove(f->buf,f->roff, uio);
if(err)
return (-err);
}
else {
f->roff += total;
}
if(f->roff == f->size)
f->roff = 0;
mtx_lock(&sc->filterlock);
f->ravail -= total;
f->wavail += total;
/* XXX Update cnt given the remaining bytes available and PACKET_SIZE
instead of counting how many packets have actually been read */
f->cnt = f->ravail / PACKET_SIZE;
mtx_unlock(&sc->filterlock);
return (0);
}
static int
sms1xxx_demux_read(struct cdev *dev, struct uio *uio, int flag)
{
int err = 0;
struct sms1xxx_softc *sc;
struct filter *f;
static int(*sms1xxx_demux_read_function)(struct sms1xxx_softc *,
struct filter *,struct uio *);
TRACE(TRACE_READ,"flag=%d, f=%p\n", flag, dev->si_drv2);
sc = dev->si_drv1;
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
f = dev->si_drv2;
if (f == NULL) {
ERR("no filter found\n");
return (EBADF);
}
if(f->state & FILTER_SLEEP) {
ERR("read already in progress\n");
return (EBUSY);
}
/* Initialize read redirector */
if (f->type == FILTER_TYPE_SEC)
sms1xxx_demux_read_function = &sms1xxx_demux_read_section;
else /* f->type == FILTER_TYPE_PES */
sms1xxx_demux_read_function = &sms1xxx_demux_read_packets;
/* Call appropriate read function */
while((err = (*sms1xxx_demux_read_function)(sc,f,uio)) > 0) {
if(flag & O_NONBLOCK)
return (EWOULDBLOCK);
f->state |= FILTER_SLEEP;
err = tsleep(f, PZERO | PCATCH, "dmxrd", 0);
f->state &= ~FILTER_SLEEP;
if(sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
else if(err)
return (err);
}
return (-err);
}
static int
sms1xxx_demux_poll(struct cdev *dev, int events, struct thread *p)
{
int revents = 0;
struct sms1xxx_softc *sc;
struct filter *f;
TRACE(TRACE_POLL,"thread=%p, events=%d, f=%p\n", p, events, dev->si_drv2);
sc = dev->si_drv1;
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return ((events & (POLLIN | POLLOUT | POLLRDNORM |
POLLWRNORM)) | POLLHUP);
}
f = dev->si_drv2;
if (f == NULL) {
ERR("no filter found\n");
return (EFAULT);
}
/* XXX POLLERR POLLPRI */
if(events & (POLLIN | POLLRDNORM)) {
if(f->cnt == 0) {
selrecord(p,&f->rsel);
f->state |= FILTER_POLL;
TRACE(TRACE_POLL,"will block\n");
}
else {
revents |= events & (POLLIN | POLLRDNORM);
TRACE(TRACE_POLL,"poll not blocking\n");
}
}
if(events & (POLLOUT | POLLWRNORM)) {
/* Write is not allowed, so no point blocking on it */
revents |= events & (POLLOUT | POLLWRNORM);
}
return (revents);
}
static int
sms1xxx_demux_close(struct cdev *dev, int flag, int mode, struct thread *p)
{
struct sms1xxx_softc *sc;
struct filter *f;
TRACE(TRACE_OPEN,"flag=%d, mode=%d, f=%p\n", flag, mode, dev->si_drv2);
sc = dev->si_drv1;
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
f = dev->si_drv2;
if (f == NULL) {
ERR("no filter found\n");
return (EBADF);
}
/* Remove PID filter if not already done */
if(!(f->pid & PIDSTOPPED)) {
if(pid_value(f->pid) <= PIDMAX)
sc->device->pid_filter(sc,pid_value(f->pid),0);
sms1xxx_demux_stop(sc,f);
}
f->pid = PIDEMPTY;
sms1xxx_usb_deref(sc);
if(f->buf != NULL) {
free(f->buf,M_USBDEV);
f->buf = NULL;
}
f->pid = PIDCLONED;
/* Destroy the clone */
TRACE(TRACE_MODULE,"destroying demux device, addr=%p\n", dev);
dev->si_drv1 = NULL;
dev->si_drv2 = NULL;
destroy_dev_sched_cb(dev, sms1xxx_demux_filter_reset, f);
return (0);
}
/* Return first 5 PIDs set within demux' PES filters
This function differs from sms1xxx_usb_getpidfilterlist() which
returns *hardware* PID filters set */
static int
sms1xxx_demux_getpidfilterlist(struct sms1xxx_softc *sc, u16 *pids)
{
int j = 0;
bzero(pids, 5 * sizeof(u16));
for(int i = 0; (i < MAX_FILTERS) && (j < 5) ; ++i) {
if((sc->filter[i].type == FILTER_TYPE_PES) &&
(pid_value(sc->filter[i].pid) <= PIDMAX)) {
pids[j] = sc->filter[i].pid;
j++;
}
}
return (0);
}
static int
sms1xxx_demux_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
struct thread *pthread)
{
int err = 0;
struct sms1xxx_softc *sc;
struct filter *f;
void *arg = addr;
TRACE(TRACE_MODULE,"f=%p\n", dev->si_drv2);
sc = dev->si_drv1;
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
f = dev->si_drv2;
if (f == NULL) {
ERR("no filter found\n");
return (EBADF);
}
if(f->state & FILTER_BUSY) {
ERR("ioctl on filter in progress\n");
return (EBUSY);
}
/* Just return 0 for this special ioctl */
if(cmd == FIONBIO)
return (err);
f->state |= FILTER_BUSY;
switch(cmd) {
case DMX_START:
if(pid_value(f->pid) > PIDMAX)
err = EINVAL;
if(!err)