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
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
|
// SPDX-License-Identifier: GPL-2.0
/*
* This file contains the logic to work with MPEG Program-Specific Information.
* These are defined both in ISO/IEC 13818-1 (systems) and ETSI EN 300 468.
* PSI is carried in the form of table structures, and although each table might
* technically be broken into one or more sections, we do not do this here,
* hence 'table' and 'section' are interchangeable for vidtv.
*
* This code currently supports three tables: PAT, PMT and SDT. These are the
* bare minimum to get userspace to recognize our MPEG transport stream. It can
* be extended to support more PSI tables in the future.
*
* Copyright (C) 2020 Daniel W. S. Almeida
*/
#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/crc32.h>
#include <linux/string.h>
#include <linux/printk.h>
#include <linux/ratelimit.h>
#include <linux/string.h>
#include <asm/byteorder.h>
#include "vidtv_psi.h"
#include "vidtv_common.h"
#include "vidtv_ts.h"
#define CRC_SIZE_IN_BYTES 4
#define MAX_VERSION_NUM 32
static const u32 CRC_LUT[256] = {
/* from libdvbv5 */
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
};
static inline u32 dvb_crc32(u32 crc, u8 *data, u32 len)
{
/* from libdvbv5 */
while (len--)
crc = (crc << 8) ^ CRC_LUT[((crc >> 24) ^ *data++) & 0xff];
return crc;
}
static void vidtv_psi_update_version_num(struct vidtv_psi_table_header *h)
{
h->version++;
}
static inline u16 vidtv_psi_sdt_serv_get_desc_loop_len(struct vidtv_psi_table_sdt_service *s)
{
u16 mask;
u16 ret;
mask = GENMASK(11, 0);
ret = be16_to_cpu(s->bitfield) & mask;
return ret;
}
static inline u16 vidtv_psi_pmt_stream_get_desc_loop_len(struct vidtv_psi_table_pmt_stream *s)
{
u16 mask;
u16 ret;
mask = GENMASK(9, 0);
ret = be16_to_cpu(s->bitfield2) & mask;
return ret;
}
static inline u16 vidtv_psi_pmt_get_desc_loop_len(struct vidtv_psi_table_pmt *p)
{
u16 mask;
u16 ret;
mask = GENMASK(9, 0);
ret = be16_to_cpu(p->bitfield2) & mask;
return ret;
}
static inline u16 vidtv_psi_get_sec_len(struct vidtv_psi_table_header *h)
{
u16 mask;
u16 ret;
mask = GENMASK(11, 0);
ret = be16_to_cpu(h->bitfield) & mask;
return ret;
}
inline u16 vidtv_psi_get_pat_program_pid(struct vidtv_psi_table_pat_program *p)
{
u16 mask;
u16 ret;
mask = GENMASK(12, 0);
ret = be16_to_cpu(p->bitfield) & mask;
return ret;
}
inline u16 vidtv_psi_pmt_stream_get_elem_pid(struct vidtv_psi_table_pmt_stream *s)
{
u16 mask;
u16 ret;
mask = GENMASK(12, 0);
ret = be16_to_cpu(s->bitfield) & mask;
return ret;
}
static inline void vidtv_psi_set_desc_loop_len(__be16 *bitfield, u16 new_len, u8 desc_len_nbits)
{
u16 mask;
__be16 new;
mask = GENMASK(15, desc_len_nbits);
new = cpu_to_be16((be16_to_cpu(*bitfield) & mask) | new_len);
*bitfield = new;
}
static void vidtv_psi_set_sec_len(struct vidtv_psi_table_header *h, u16 new_len)
{
u16 old_len = vidtv_psi_get_sec_len(h);
__be16 new;
u16 mask;
mask = GENMASK(15, 13);
new = cpu_to_be16((be16_to_cpu(h->bitfield) & mask) | new_len);
if (old_len > MAX_SECTION_LEN)
pr_warn_ratelimited("section length: %d > %d, old len was %d\n",
new_len,
MAX_SECTION_LEN,
old_len);
h->bitfield = new;
}
static u32 vidtv_psi_ts_psi_write_into(struct psi_write_args args)
{
/*
* Packetize PSI sections into TS packets:
* push a TS header (4bytes) every 184 bytes
* manage the continuity_counter
* add stuffing (i.e. padding bytes) after the CRC
*/
u32 nbytes_past_boundary = (args.dest_offset % TS_PACKET_LEN);
bool aligned = (nbytes_past_boundary == 0);
struct vidtv_mpeg_ts ts_header = {};
/* number of bytes written by this function */
u32 nbytes = 0;
/* how much there is left to write */
u32 remaining_len = args.len;
/* how much we can be written in this packet */
u32 payload_write_len = 0;
/* where we are in the source */
u32 payload_offset = 0;
const u16 PAYLOAD_START = args.new_psi_section;
if (!args.crc && !args.is_crc)
pr_warn_ratelimited("Missing CRC for chunk\n");
if (args.crc)
*args.crc = dvb_crc32(*args.crc, args.from, args.len);
if (args.new_psi_section && !aligned) {
pr_warn_ratelimited("Cannot write a new PSI section in a misaligned buffer\n");
/* forcibly align and hope for the best */
nbytes += vidtv_memset(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
TS_FILL_BYTE,
TS_PACKET_LEN - nbytes_past_boundary);
}
while (remaining_len) {
nbytes_past_boundary = (args.dest_offset + nbytes) % TS_PACKET_LEN;
aligned = (nbytes_past_boundary == 0);
if (aligned) {
/* if at a packet boundary, write a new TS header */
ts_header.sync_byte = TS_SYNC_BYTE;
ts_header.bitfield = cpu_to_be16((PAYLOAD_START << 14) | args.pid);
ts_header.scrambling = 0;
ts_header.continuity_counter = *args.continuity_counter;
ts_header.payload = 1;
/* no adaptation field */
ts_header.adaptation_field = 0;
/* copy the header */
nbytes += vidtv_memcpy(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
&ts_header,
sizeof(ts_header));
/*
* This will trigger a discontinuity if the buffer is full,
* effectively dropping the packet.
*/
vidtv_ts_inc_cc(args.continuity_counter);
}
/* write the pointer_field in the first byte of the payload */
if (args.new_psi_section)
nbytes += vidtv_memset(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
0x0,
1);
/* write as much of the payload as possible */
nbytes_past_boundary = (args.dest_offset + nbytes) % TS_PACKET_LEN;
payload_write_len = min(TS_PACKET_LEN - nbytes_past_boundary, remaining_len);
nbytes += vidtv_memcpy(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
args.from + payload_offset,
payload_write_len);
/* 'payload_write_len' written from a total of 'len' requested*/
remaining_len -= payload_write_len;
payload_offset += payload_write_len;
}
/*
* fill the rest of the packet if there is any remaining space unused
*/
nbytes_past_boundary = (args.dest_offset + nbytes) % TS_PACKET_LEN;
if (args.is_crc)
nbytes += vidtv_memset(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
TS_FILL_BYTE,
TS_PACKET_LEN - nbytes_past_boundary);
return nbytes;
}
static u32 table_section_crc32_write_into(struct crc32_write_args args)
{
/* the CRC is the last entry in the section */
u32 nbytes = 0;
struct psi_write_args psi_args = {};
psi_args.dest_buf = args.dest_buf;
psi_args.from = &args.crc;
psi_args.len = CRC_SIZE_IN_BYTES;
psi_args.dest_offset = args.dest_offset;
psi_args.pid = args.pid;
psi_args.new_psi_section = false;
psi_args.continuity_counter = args.continuity_counter;
psi_args.is_crc = true;
psi_args.dest_buf_sz = args.dest_buf_sz;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
return nbytes;
}
struct vidtv_psi_desc_service *vidtv_psi_service_desc_init(struct vidtv_psi_desc *head,
enum service_type service_type,
char *service_name,
char *provider_name)
{
struct vidtv_psi_desc_service *desc;
u32 service_name_len = service_name ? strlen(service_name) : 0;
u32 provider_name_len = provider_name ? strlen(provider_name) : 0;
desc = kzalloc(sizeof(*desc), GFP_KERNEL);
desc->type = SERVICE_DESCRIPTOR;
desc->length = sizeof_field(struct vidtv_psi_desc_service, service_type)
+ sizeof_field(struct vidtv_psi_desc_service, provider_name_len)
+ provider_name_len
+ sizeof_field(struct vidtv_psi_desc_service, service_name_len)
+ service_name_len;
desc->service_type = service_type;
desc->service_name_len = service_name_len;
if (service_name && service_name_len)
desc->service_name = kstrdup(service_name, GFP_KERNEL);
desc->provider_name_len = provider_name_len;
if (provider_name && provider_name_len)
desc->provider_name = kstrdup(provider_name, GFP_KERNEL);
if (head) {
while (head->next)
head = head->next;
head->next = (struct vidtv_psi_desc *)desc;
}
return desc;
}
struct vidtv_psi_desc_registration
*vidtv_psi_registration_desc_init(struct vidtv_psi_desc *head,
__be32 format_id,
u8 *additional_ident_info,
u32 additional_info_len)
{
struct vidtv_psi_desc_registration *desc;
desc = kzalloc(sizeof(*desc) + sizeof(format_id) + additional_info_len, GFP_KERNEL);
desc->type = REGISTRATION_DESCRIPTOR;
desc->length = sizeof_field(struct vidtv_psi_desc_registration, format_id)
+ additional_info_len;
desc->format_id = format_id;
if (additional_ident_info && additional_info_len)
memcpy(desc->additional_identification_info,
additional_ident_info,
additional_info_len);
if (head) {
while (head->next)
head = head->next;
head->next = (struct vidtv_psi_desc *)desc;
}
return desc;
}
struct vidtv_psi_desc *vidtv_psi_desc_clone(struct vidtv_psi_desc *desc)
{
struct vidtv_psi_desc *head = NULL;
struct vidtv_psi_desc *prev = NULL;
struct vidtv_psi_desc *curr = NULL;
struct vidtv_psi_desc_service *service;
while (desc) {
switch (desc->type) {
case SERVICE_DESCRIPTOR:
service = (struct vidtv_psi_desc_service *)desc;
curr = (struct vidtv_psi_desc *)
vidtv_psi_service_desc_init(head,
service->service_type,
service->service_name,
service->provider_name);
break;
case REGISTRATION_DESCRIPTOR:
default:
curr = kzalloc(sizeof(*desc) + desc->length, GFP_KERNEL);
memcpy(curr, desc, sizeof(*desc) + desc->length);
break;
}
if (curr)
curr->next = NULL;
if (!head)
head = curr;
if (prev)
prev->next = curr;
prev = curr;
desc = desc->next;
}
return head;
}
void vidtv_psi_desc_destroy(struct vidtv_psi_desc *desc)
{
struct vidtv_psi_desc *curr = desc;
struct vidtv_psi_desc *tmp = NULL;
while (curr) {
tmp = curr;
curr = curr->next;
switch (tmp->type) {
case SERVICE_DESCRIPTOR:
kfree(((struct vidtv_psi_desc_service *)tmp)->provider_name);
kfree(((struct vidtv_psi_desc_service *)tmp)->service_name);
break;
case REGISTRATION_DESCRIPTOR:
/* nothing to do */
break;
default:
pr_warn_ratelimited("Possible leak: not handling descriptor type %d\n",
tmp->type);
break;
}
kfree(tmp);
}
}
static u16
vidtv_psi_desc_comp_loop_len(struct vidtv_psi_desc *desc)
{
u32 length = 0;
if (!desc)
return 0;
while (desc) {
length += sizeof_field(struct vidtv_psi_desc, type);
length += sizeof_field(struct vidtv_psi_desc, length);
length += desc->length; /* from 'length' field until the end of the descriptor */
desc = desc->next;
}
return length;
}
void vidtv_psi_desc_assign(struct vidtv_psi_desc **to,
struct vidtv_psi_desc *desc)
{
if (desc == *to)
return;
if (*to)
vidtv_psi_desc_destroy(*to);
*to = desc;
}
void vidtv_pmt_desc_assign(struct vidtv_psi_table_pmt *pmt,
struct vidtv_psi_desc **to,
struct vidtv_psi_desc *desc)
{
vidtv_psi_desc_assign(to, desc);
vidtv_psi_pmt_table_update_sec_len(pmt);
if (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN)
vidtv_psi_desc_assign(to, NULL);
vidtv_psi_update_version_num(&pmt->header);
}
void vidtv_sdt_desc_assign(struct vidtv_psi_table_sdt *sdt,
struct vidtv_psi_desc **to,
struct vidtv_psi_desc *desc)
{
vidtv_psi_desc_assign(to, desc);
vidtv_psi_sdt_table_update_sec_len(sdt);
if (vidtv_psi_get_sec_len(&sdt->header) > MAX_SECTION_LEN)
vidtv_psi_desc_assign(to, NULL);
vidtv_psi_update_version_num(&sdt->header);
}
static u32 vidtv_psi_desc_write_into(struct desc_write_args args)
{
/* the number of bytes written by this function */
u32 nbytes = 0;
struct psi_write_args psi_args = {};
psi_args.dest_buf = args.dest_buf;
psi_args.from = &args.desc->type;
psi_args.len = sizeof_field(struct vidtv_psi_desc, type) +
sizeof_field(struct vidtv_psi_desc, length);
psi_args.dest_offset = args.dest_offset + nbytes;
psi_args.pid = args.pid;
psi_args.new_psi_section = false;
psi_args.continuity_counter = args.continuity_counter;
psi_args.is_crc = false;
psi_args.dest_buf_sz = args.dest_buf_sz;
psi_args.crc = args.crc;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
switch (args.desc->type) {
case SERVICE_DESCRIPTOR:
psi_args.dest_offset = args.dest_offset + nbytes;
psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_type) +
sizeof_field(struct vidtv_psi_desc_service, provider_name_len);
psi_args.from = &((struct vidtv_psi_desc_service *)args.desc)->service_type;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
psi_args.dest_offset = args.dest_offset + nbytes;
psi_args.len = ((struct vidtv_psi_desc_service *)args.desc)->provider_name_len;
psi_args.from = ((struct vidtv_psi_desc_service *)args.desc)->provider_name;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
psi_args.dest_offset = args.dest_offset + nbytes;
psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_name_len);
psi_args.from = &((struct vidtv_psi_desc_service *)args.desc)->service_name_len;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
psi_args.dest_offset = args.dest_offset + nbytes;
psi_args.len = ((struct vidtv_psi_desc_service *)args.desc)->service_name_len;
psi_args.from = ((struct vidtv_psi_desc_service *)args.desc)->service_name;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
break;
case REGISTRATION_DESCRIPTOR:
default:
psi_args.dest_offset = args.dest_offset + nbytes;
psi_args.len = args.desc->length;
psi_args.from = &args.desc->data;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
break;
}
return nbytes;
}
static u32
vidtv_psi_table_header_write_into(struct header_write_args args)
{
/* the number of bytes written by this function */
u32 nbytes = 0;
struct psi_write_args psi_args = {};
psi_args.dest_buf = args.dest_buf;
psi_args.from = args.h;
psi_args.len = sizeof(struct vidtv_psi_table_header);
psi_args.dest_offset = args.dest_offset;
psi_args.pid = args.pid;
psi_args.new_psi_section = true;
psi_args.continuity_counter = args.continuity_counter;
psi_args.is_crc = false;
psi_args.dest_buf_sz = args.dest_buf_sz;
psi_args.crc = args.crc;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
return nbytes;
}
void
vidtv_psi_pat_table_update_sec_len(struct vidtv_psi_table_pat *pat)
{
/* see ISO/IEC 13818-1 : 2000 p.43 */
u16 length = 0;
u32 i;
/* from immediately after 'section_length' until 'last_section_number'*/
length += PAT_LEN_UNTIL_LAST_SECTION_NUMBER;
/* do not count the pointer */
for (i = 0; i < pat->programs; ++i)
length += sizeof(struct vidtv_psi_table_pat_program) -
sizeof(struct vidtv_psi_table_pat_program *);
length += CRC_SIZE_IN_BYTES;
vidtv_psi_set_sec_len(&pat->header, length);
}
void vidtv_psi_pmt_table_update_sec_len(struct vidtv_psi_table_pmt *pmt)
{
/* see ISO/IEC 13818-1 : 2000 p.46 */
u16 length = 0;
struct vidtv_psi_table_pmt_stream *s = pmt->stream;
u16 desc_loop_len;
/* from immediately after 'section_length' until 'program_info_length'*/
length += PMT_LEN_UNTIL_PROGRAM_INFO_LENGTH;
desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor);
vidtv_psi_set_desc_loop_len(&pmt->bitfield2, desc_loop_len, 10);
length += desc_loop_len;
while (s) {
/* skip both pointers at the end */
length += sizeof(struct vidtv_psi_table_pmt_stream) -
sizeof(struct vidtv_psi_desc *) -
sizeof(struct vidtv_psi_table_pmt_stream *);
desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor);
vidtv_psi_set_desc_loop_len(&s->bitfield2, desc_loop_len, 10);
length += desc_loop_len;
s = s->next;
}
length += CRC_SIZE_IN_BYTES;
vidtv_psi_set_sec_len(&pmt->header, length);
}
void vidtv_psi_sdt_table_update_sec_len(struct vidtv_psi_table_sdt *sdt)
{
/* see ETSI EN 300 468 V 1.10.1 p.24 */
u16 length = 0;
struct vidtv_psi_table_sdt_service *s = sdt->service;
u16 desc_loop_len;
/*
* from immediately after 'section_length' until
* 'reserved_for_future_use'
*/
length += SDT_LEN_UNTIL_RESERVED_FOR_FUTURE_USE;
while (s) {
/* skip both pointers at the end */
length += sizeof(struct vidtv_psi_table_sdt_service) -
sizeof(struct vidtv_psi_desc *) -
sizeof(struct vidtv_psi_table_sdt_service *);
desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor);
vidtv_psi_set_desc_loop_len(&s->bitfield, desc_loop_len, 12);
length += desc_loop_len;
s = s->next;
}
length += CRC_SIZE_IN_BYTES;
vidtv_psi_set_sec_len(&sdt->header, length);
}
struct vidtv_psi_table_pat_program*
vidtv_psi_pat_program_init(struct vidtv_psi_table_pat_program *head,
u16 service_id,
u16 program_map_pid)
{
struct vidtv_psi_table_pat_program *program;
const u16 RESERVED = 0x07;
program = kzalloc(sizeof(*program), GFP_KERNEL);
program->service_id = cpu_to_be16(service_id);
/* pid for the PMT section in the TS */
program->bitfield = cpu_to_be16((RESERVED << 13) | program_map_pid);
program->next = NULL;
if (head) {
while (head->next)
head = head->next;
head->next = program;
}
return program;
}
void
vidtv_psi_pat_program_destroy(struct vidtv_psi_table_pat_program *p)
{
struct vidtv_psi_table_pat_program *curr = p;
struct vidtv_psi_table_pat_program *tmp = NULL;
while (curr) {
tmp = curr;
curr = curr->next;
kfree(tmp);
}
}
void
vidtv_psi_pat_program_assign(struct vidtv_psi_table_pat *pat,
struct vidtv_psi_table_pat_program *p)
{
/* This function transfers ownership of p to the table */
u16 program_count = 0;
struct vidtv_psi_table_pat_program *program = p;
if (p == pat->program)
return;
while (program) {
++program_count;
program = program->next;
}
pat->programs = program_count;
pat->program = p;
/* Recompute section length */
vidtv_psi_pat_table_update_sec_len(pat);
if (vidtv_psi_get_sec_len(&pat->header) > MAX_SECTION_LEN)
vidtv_psi_pat_program_assign(pat, NULL);
vidtv_psi_update_version_num(&pat->header);
}
struct vidtv_psi_table_pat *vidtv_psi_pat_table_init(u16 transport_stream_id)
{
struct vidtv_psi_table_pat *pat = kzalloc(sizeof(*pat), GFP_KERNEL);
const u16 SYNTAX = 0x1;
const u16 ZERO = 0x0;
const u16 ONES = 0x03;
pat->header.table_id = 0x0;
pat->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12));
pat->header.id = cpu_to_be16(transport_stream_id);
pat->header.current_next = 0x1;
pat->header.version = 0x1f;
pat->header.one2 = 0x03;
pat->header.section_id = 0x0;
pat->header.last_section = 0x0;
pat->programs = 0;
vidtv_psi_pat_table_update_sec_len(pat);
return pat;
}
u32 vidtv_psi_pat_write_into(struct vidtv_psi_pat_write_args args)
{
/* the number of bytes written by this function */
u32 nbytes = 0;
const u16 pat_pid = VIDTV_PAT_PID;
u32 crc = 0xffffffff;
struct vidtv_psi_table_pat_program *p = args.pat->program;
struct header_write_args h_args = {};
struct psi_write_args psi_args = {};
struct crc32_write_args c_args = {};
vidtv_psi_pat_table_update_sec_len(args.pat);
h_args.dest_buf = args.buf;
h_args.dest_offset = args.offset;
h_args.h = &args.pat->header;
h_args.pid = pat_pid;
h_args.continuity_counter = args.continuity_counter;
h_args.dest_buf_sz = args.buf_sz;
h_args.crc = &crc;
nbytes += vidtv_psi_table_header_write_into(h_args);
/* note that the field 'u16 programs' is not really part of the PAT */
psi_args.dest_buf = args.buf;
psi_args.pid = pat_pid;
psi_args.new_psi_section = false;
psi_args.continuity_counter = args.continuity_counter;
psi_args.is_crc = false;
psi_args.dest_buf_sz = args.buf_sz;
psi_args.crc = &crc;
while (p) {
/* copy the PAT programs */
psi_args.from = p;
/* skip the pointer */
psi_args.len = sizeof(*p) -
sizeof(struct vidtv_psi_table_pat_program *);
psi_args.dest_offset = args.offset + nbytes;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
p = p->next;
}
c_args.dest_buf = args.buf;
c_args.dest_offset = args.offset + nbytes;
c_args.crc = cpu_to_be32(crc);
c_args.pid = pat_pid;
c_args.continuity_counter = args.continuity_counter;
c_args.dest_buf_sz = args.buf_sz;
/* Write the CRC32 at the end */
nbytes += table_section_crc32_write_into(c_args);
return nbytes;
}
void
vidtv_psi_pat_table_destroy(struct vidtv_psi_table_pat *p)
{
vidtv_psi_pat_program_destroy(p->program);
kfree(p);
}
struct vidtv_psi_table_pmt_stream*
vidtv_psi_pmt_stream_init(struct vidtv_psi_table_pmt_stream *head,
enum vidtv_psi_stream_types stream_type,
u16 es_pid)
{
struct vidtv_psi_table_pmt_stream *stream;
const u16 RESERVED1 = 0x07;
const u16 RESERVED2 = 0x0f;
const u16 ZERO = 0x0;
u16 desc_loop_len;
stream = kzalloc(sizeof(*stream), GFP_KERNEL);
stream->type = stream_type;
stream->bitfield = cpu_to_be16((RESERVED1 << 13) | es_pid);
desc_loop_len = vidtv_psi_desc_comp_loop_len(stream->descriptor);
stream->bitfield2 = cpu_to_be16((RESERVED2 << 12) |
(ZERO << 10) |
desc_loop_len);
stream->next = NULL;
if (head) {
while (head->next)
head = head->next;
head->next = stream;
}
return stream;
}
void vidtv_psi_pmt_stream_destroy(struct vidtv_psi_table_pmt_stream *s)
{
struct vidtv_psi_table_pmt_stream *curr_stream = s;
struct vidtv_psi_table_pmt_stream *tmp_stream = NULL;
while (curr_stream) {
tmp_stream = curr_stream;
curr_stream = curr_stream->next;
vidtv_psi_desc_destroy(tmp_stream->descriptor);
kfree(tmp_stream);
}
}
void vidtv_psi_pmt_stream_assign(struct vidtv_psi_table_pmt *pmt,
struct vidtv_psi_table_pmt_stream *s)
{
/* This function transfers ownership of s to the table */
if (s == pmt->stream)
return;
pmt->stream = s;
vidtv_psi_pmt_table_update_sec_len(pmt);
if (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN)
vidtv_psi_pmt_stream_assign(pmt, NULL);
vidtv_psi_update_version_num(&pmt->header);
}
u16 vidtv_psi_pmt_get_pid(struct vidtv_psi_table_pmt *section,
struct vidtv_psi_table_pat *pat)
{
struct vidtv_psi_table_pat_program *program = pat->program;
/*
* service_id is the same as program_number in the
* corresponding program_map_section
* see ETSI EN 300 468 v1.15.1 p. 24
*/
while (program) {
if (program->service_id == section->header.id)
return vidtv_psi_get_pat_program_pid(program);
program = program->next;
}
return TS_LAST_VALID_PID + 1; /* not found */
}
struct vidtv_psi_table_pmt *vidtv_psi_pmt_table_init(u16 program_number,
u16 pcr_pid)
{
struct vidtv_psi_table_pmt *pmt = kzalloc(sizeof(*pmt), GFP_KERNEL);
const u16 SYNTAX = 0x1;
const u16 ZERO = 0x0;
const u16 ONES = 0x03;
const u16 RESERVED1 = 0x07;
const u16 RESERVED2 = 0x0f;
u16 desc_loop_len;
if (!pcr_pid)
pcr_pid = 0x1fff;
pmt->header.table_id = 0x2;
pmt->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12));
pmt->header.id = cpu_to_be16(program_number);
pmt->header.current_next = 0x1;
pmt->header.version = 0x1f;
pmt->header.one2 = ONES;
pmt->header.section_id = 0;
pmt->header.last_section = 0;
pmt->bitfield = cpu_to_be16((RESERVED1 << 13) | pcr_pid);
desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor);
pmt->bitfield2 = cpu_to_be16((RESERVED2 << 12) |
(ZERO << 10) |
desc_loop_len);
vidtv_psi_pmt_table_update_sec_len(pmt);
return pmt;
}
u32 vidtv_psi_pmt_write_into(struct vidtv_psi_pmt_write_args args)
{
/* the number of bytes written by this function */
u32 nbytes = 0;
u32 crc = 0xffffffff;
struct vidtv_psi_desc *table_descriptor = args.pmt->descriptor;
struct vidtv_psi_table_pmt_stream *stream = args.pmt->stream;
struct vidtv_psi_desc *stream_descriptor = (stream) ?
args.pmt->stream->descriptor :
NULL;
struct header_write_args h_args = {};
struct psi_write_args psi_args = {};
struct desc_write_args d_args = {};
struct crc32_write_args c_args = {};
vidtv_psi_pmt_table_update_sec_len(args.pmt);
h_args.dest_buf = args.buf;
h_args.dest_offset = args.offset;
h_args.h = &args.pmt->header;
h_args.pid = args.pid;
h_args.continuity_counter = args.continuity_counter;
h_args.dest_buf_sz = args.buf_sz;
h_args.crc = &crc;
nbytes += vidtv_psi_table_header_write_into(h_args);
/* write the two bitfields */
psi_args.dest_buf = args.buf;
psi_args.from = &args.pmt->bitfield;
psi_args.len = sizeof_field(struct vidtv_psi_table_pmt, bitfield) +
sizeof_field(struct vidtv_psi_table_pmt, bitfield2);
psi_args.dest_offset = args.offset + nbytes;
psi_args.pid = args.pid;
psi_args.new_psi_section = false;
psi_args.continuity_counter = args.continuity_counter;
psi_args.is_crc = false;
psi_args.dest_buf_sz = args.buf_sz;
psi_args.crc = &crc;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
while (table_descriptor) {
/* write the descriptors, if any */
d_args.dest_buf = args.buf;
d_args.dest_offset = args.offset + nbytes;
d_args.desc = table_descriptor;
d_args.pid = args.pid;
d_args.continuity_counter = args.continuity_counter;
d_args.dest_buf_sz = args.buf_sz;
d_args.crc = &crc;
nbytes += vidtv_psi_desc_write_into(d_args);
table_descriptor = table_descriptor->next;
}
while (stream) {
/* write the streams, if any */
psi_args.from = stream;
psi_args.len = sizeof_field(struct vidtv_psi_table_pmt_stream, type) +
sizeof_field(struct vidtv_psi_table_pmt_stream, bitfield) +
sizeof_field(struct vidtv_psi_table_pmt_stream, bitfield2);
psi_args.dest_offset = args.offset + nbytes;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
while (stream_descriptor) {
/* write the stream descriptors, if any */
d_args.dest_buf = args.buf;
d_args.dest_offset = args.offset + nbytes;
d_args.desc = stream_descriptor;
d_args.pid = args.pid;
d_args.continuity_counter = args.continuity_counter;
d_args.dest_buf_sz = args.buf_sz;
d_args.crc = &crc;
nbytes += vidtv_psi_desc_write_into(d_args);
stream_descriptor = stream_descriptor->next;
}
stream = stream->next;
}
c_args.dest_buf = args.buf;
c_args.dest_offset = args.offset + nbytes;
c_args.crc = cpu_to_be32(crc);
c_args.pid = args.pid;
c_args.continuity_counter = args.continuity_counter;
c_args.dest_buf_sz = args.buf_sz;
/* Write the CRC32 at the end */
nbytes += table_section_crc32_write_into(c_args);
return nbytes;
}
void vidtv_psi_pmt_table_destroy(struct vidtv_psi_table_pmt *pmt)
{
vidtv_psi_desc_destroy(pmt->descriptor);
vidtv_psi_pmt_stream_destroy(pmt->stream);
kfree(pmt);
}
struct vidtv_psi_table_sdt *vidtv_psi_sdt_table_init(u16 transport_stream_id)
{
struct vidtv_psi_table_sdt *sdt = kzalloc(sizeof(*sdt), GFP_KERNEL);
const u16 SYNTAX = 0x1;
const u16 ONE = 0x1;
const u16 ONES = 0x03;
const u16 RESERVED = 0xff;
sdt->header.table_id = 0x42;
sdt->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ONE << 14) | (ONES << 12));
/*
* This is a 16-bit field which serves as a label for identification
* of the TS, about which the SDT informs, from any other multiplex
* within the delivery system.
*/
sdt->header.id = cpu_to_be16(transport_stream_id);
sdt->header.current_next = ONE;
sdt->header.version = 0x1f;
sdt->header.one2 = ONES;
sdt->header.section_id = 0;
sdt->header.last_section = 0;
/*
* FIXME: The network_id range from 0xFF01 to 0xFFFF is used to
* indicate temporary private use. For now, let's use the first
* value.
* This can be changed to something more useful, when support for
* NIT gets added
*/
sdt->network_id = cpu_to_be16(0xff01);
sdt->reserved = RESERVED;
vidtv_psi_sdt_table_update_sec_len(sdt);
return sdt;
}
u32 vidtv_psi_sdt_write_into(struct vidtv_psi_sdt_write_args args)
{
u32 nbytes = 0;
u16 sdt_pid = VIDTV_SDT_PID; /* see ETSI EN 300 468 v1.15.1 p. 11 */
u32 crc = 0xffffffff;
struct vidtv_psi_table_sdt_service *service = args.sdt->service;
struct vidtv_psi_desc *service_desc = (args.sdt->service) ?
args.sdt->service->descriptor :
NULL;
struct header_write_args h_args = {};
struct psi_write_args psi_args = {};
struct desc_write_args d_args = {};
struct crc32_write_args c_args = {};
vidtv_psi_sdt_table_update_sec_len(args.sdt);
h_args.dest_buf = args.buf;
h_args.dest_offset = args.offset;
h_args.h = &args.sdt->header;
h_args.pid = sdt_pid;
h_args.continuity_counter = args.continuity_counter;
h_args.dest_buf_sz = args.buf_sz;
h_args.crc = &crc;
nbytes += vidtv_psi_table_header_write_into(h_args);
psi_args.dest_buf = args.buf;
psi_args.from = &args.sdt->network_id;
psi_args.len = sizeof_field(struct vidtv_psi_table_sdt, network_id) +
sizeof_field(struct vidtv_psi_table_sdt, reserved);
psi_args.dest_offset = args.offset + nbytes;
psi_args.pid = sdt_pid;
psi_args.new_psi_section = false;
psi_args.continuity_counter = args.continuity_counter;
psi_args.is_crc = false;
psi_args.dest_buf_sz = args.buf_sz;
psi_args.crc = &crc;
/* copy u16 network_id + u8 reserved)*/
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
while (service) {
/* copy the services, if any */
psi_args.from = service;
/* skip both pointers at the end */
psi_args.len = sizeof(struct vidtv_psi_table_sdt_service) -
sizeof(struct vidtv_psi_desc *) -
sizeof(struct vidtv_psi_table_sdt_service *);
psi_args.dest_offset = args.offset + nbytes;
nbytes += vidtv_psi_ts_psi_write_into(psi_args);
while (service_desc) {
/* copy the service descriptors, if any */
d_args.dest_buf = args.buf;
d_args.dest_offset = args.offset + nbytes;
d_args.desc = service_desc;
d_args.pid = sdt_pid;
d_args.continuity_counter = args.continuity_counter;
d_args.dest_buf_sz = args.buf_sz;
d_args.crc = &crc;
nbytes += vidtv_psi_desc_write_into(d_args);
service_desc = service_desc->next;
}
service = service->next;
}
c_args.dest_buf = args.buf;
c_args.dest_offset = args.offset + nbytes;
c_args.crc = cpu_to_be32(crc);
c_args.pid = sdt_pid;
c_args.continuity_counter = args.continuity_counter;
c_args.dest_buf_sz = args.buf_sz;
/* Write the CRC at the end */
nbytes += table_section_crc32_write_into(c_args);
return nbytes;
}
void vidtv_psi_sdt_table_destroy(struct vidtv_psi_table_sdt *sdt)
{
vidtv_psi_sdt_service_destroy(sdt->service);
kfree(sdt);
}
struct vidtv_psi_table_sdt_service
*vidtv_psi_sdt_service_init(struct vidtv_psi_table_sdt_service *head,
u16 service_id)
{
struct vidtv_psi_table_sdt_service *service;
service = kzalloc(sizeof(*service), GFP_KERNEL);
/*
* ETSI 300 468: this is a 16bit field which serves as a label to
* identify this service from any other service within the TS.
* The service id is the same as the program number in the
* corresponding program_map_section
*/
service->service_id = cpu_to_be16(service_id);
service->EIT_schedule = 0x0;
service->EIT_present_following = 0x0;
service->reserved = 0x3f;
service->bitfield = cpu_to_be16(RUNNING << 13);
if (head) {
while (head->next)
head = head->next;
head->next = service;
}
return service;
}
void
vidtv_psi_sdt_service_destroy(struct vidtv_psi_table_sdt_service *service)
{
struct vidtv_psi_table_sdt_service *curr = service;
struct vidtv_psi_table_sdt_service *tmp = NULL;
while (curr) {
tmp = curr;
curr = curr->next;
vidtv_psi_desc_destroy(tmp->descriptor);
kfree(tmp);
}
}
void
vidtv_psi_sdt_service_assign(struct vidtv_psi_table_sdt *sdt,
struct vidtv_psi_table_sdt_service *service)
{
if (service == sdt->service)
return;
sdt->service = service;
/* recompute section length */
vidtv_psi_sdt_table_update_sec_len(sdt);
if (vidtv_psi_get_sec_len(&sdt->header) > MAX_SECTION_LEN)
vidtv_psi_sdt_service_assign(sdt, NULL);
vidtv_psi_update_version_num(&sdt->header);
}
struct vidtv_psi_table_pmt**
vidtv_psi_pmt_create_sec_for_each_pat_entry(struct vidtv_psi_table_pat *pat, u16 pcr_pid)
{
/*
* PMTs contain information about programs. For each program,
* there is one PMT section. This function will create a section
* for each program found in the PAT
*/
struct vidtv_psi_table_pat_program *program = pat->program;
struct vidtv_psi_table_pmt **pmt_secs;
u32 i = 0;
/* a section for each program_id */
pmt_secs = kcalloc(pat->programs,
sizeof(struct vidtv_psi_table_pmt *),
GFP_KERNEL);
while (program) {
pmt_secs[i] = vidtv_psi_pmt_table_init(be16_to_cpu(program->service_id), pcr_pid);
++i;
program = program->next;
}
return pmt_secs;
}
struct vidtv_psi_table_pmt
*vidtv_psi_find_pmt_sec(struct vidtv_psi_table_pmt **pmt_sections,
u16 nsections,
u16 program_num)
{
/* find the PMT section associated with 'program_num' */
struct vidtv_psi_table_pmt *sec = NULL;
u32 i;
for (i = 0; i < nsections; ++i) {
sec = pmt_sections[i];
if (be16_to_cpu(sec->header.id) == program_num)
return sec;
}
return NULL; /* not found */
}
|