1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This file contains the logic to work with MPEG Program-Specific Information. 4 * These are defined both in ISO/IEC 13818-1 (systems) and ETSI EN 300 468. 5 * PSI is carried in the form of table structures, and although each table might 6 * technically be broken into one or more sections, we do not do this here, 7 * hence 'table' and 'section' are interchangeable for vidtv. 8 * 9 * This code currently supports three tables: PAT, PMT and SDT. These are the 10 * bare minimum to get userspace to recognize our MPEG transport stream. It can 11 * be extended to support more PSI tables in the future. 12 * 13 * Copyright (C) 2020 Daniel W. S. Almeida 14 */ 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__ 17 18 #include <linux/kernel.h> 19 #include <linux/types.h> 20 #include <linux/slab.h> 21 #include <linux/crc32.h> 22 #include <linux/string.h> 23 #include <linux/printk.h> 24 #include <linux/ratelimit.h> 25 #include <linux/string.h> 26 #include <asm/byteorder.h> 27 28 #include "vidtv_psi.h" 29 #include "vidtv_common.h" 30 #include "vidtv_ts.h" 31 32 #define CRC_SIZE_IN_BYTES 4 33 #define MAX_VERSION_NUM 32 34 35 static const u32 CRC_LUT[256] = { 36 /* from libdvbv5 */ 37 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 38 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 39 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7, 40 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, 41 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 42 0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 43 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef, 44 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, 45 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 46 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 47 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0, 48 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, 49 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 50 0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 51 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, 52 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, 53 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 54 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 55 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050, 56 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 57 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 58 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 59 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1, 60 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, 61 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 62 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 63 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9, 64 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, 65 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 66 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 67 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71, 68 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, 69 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 70 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 71 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, 72 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, 73 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 74 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 75 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676, 76 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, 77 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 78 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 79 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 80 }; 81 82 static inline u32 dvb_crc32(u32 crc, u8 *data, u32 len) 83 { 84 /* from libdvbv5 */ 85 while (len--) 86 crc = (crc << 8) ^ CRC_LUT[((crc >> 24) ^ *data++) & 0xff]; 87 return crc; 88 } 89 90 static void vidtv_psi_update_version_num(struct vidtv_psi_table_header *h) 91 { 92 h->version++; 93 } 94 95 static inline u16 vidtv_psi_sdt_serv_get_desc_loop_len(struct vidtv_psi_table_sdt_service *s) 96 { 97 u16 mask; 98 u16 ret; 99 100 mask = GENMASK(11, 0); 101 102 ret = be16_to_cpu(s->bitfield) & mask; 103 return ret; 104 } 105 106 static inline u16 vidtv_psi_pmt_stream_get_desc_loop_len(struct vidtv_psi_table_pmt_stream *s) 107 { 108 u16 mask; 109 u16 ret; 110 111 mask = GENMASK(9, 0); 112 113 ret = be16_to_cpu(s->bitfield2) & mask; 114 return ret; 115 } 116 117 static inline u16 vidtv_psi_pmt_get_desc_loop_len(struct vidtv_psi_table_pmt *p) 118 { 119 u16 mask; 120 u16 ret; 121 122 mask = GENMASK(9, 0); 123 124 ret = be16_to_cpu(p->bitfield2) & mask; 125 return ret; 126 } 127 128 static inline u16 vidtv_psi_get_sec_len(struct vidtv_psi_table_header *h) 129 { 130 u16 mask; 131 u16 ret; 132 133 mask = GENMASK(11, 0); 134 135 ret = be16_to_cpu(h->bitfield) & mask; 136 return ret; 137 } 138 139 inline u16 vidtv_psi_get_pat_program_pid(struct vidtv_psi_table_pat_program *p) 140 { 141 u16 mask; 142 u16 ret; 143 144 mask = GENMASK(12, 0); 145 146 ret = be16_to_cpu(p->bitfield) & mask; 147 return ret; 148 } 149 150 inline u16 vidtv_psi_pmt_stream_get_elem_pid(struct vidtv_psi_table_pmt_stream *s) 151 { 152 u16 mask; 153 u16 ret; 154 155 mask = GENMASK(12, 0); 156 157 ret = be16_to_cpu(s->bitfield) & mask; 158 return ret; 159 } 160 161 static inline void vidtv_psi_set_desc_loop_len(__be16 *bitfield, u16 new_len, u8 desc_len_nbits) 162 { 163 u16 mask; 164 __be16 new; 165 166 mask = GENMASK(15, desc_len_nbits); 167 168 new = cpu_to_be16((be16_to_cpu(*bitfield) & mask) | new_len); 169 *bitfield = new; 170 } 171 172 static void vidtv_psi_set_sec_len(struct vidtv_psi_table_header *h, u16 new_len) 173 { 174 u16 old_len = vidtv_psi_get_sec_len(h); 175 __be16 new; 176 u16 mask; 177 178 mask = GENMASK(15, 13); 179 180 new = cpu_to_be16((be16_to_cpu(h->bitfield) & mask) | new_len); 181 182 if (old_len > MAX_SECTION_LEN) 183 pr_warn_ratelimited("section length: %d > %d, old len was %d\n", 184 new_len, 185 MAX_SECTION_LEN, 186 old_len); 187 188 h->bitfield = new; 189 } 190 191 static u32 vidtv_psi_ts_psi_write_into(struct psi_write_args args) 192 { 193 /* 194 * Packetize PSI sections into TS packets: 195 * push a TS header (4bytes) every 184 bytes 196 * manage the continuity_counter 197 * add stuffing (i.e. padding bytes) after the CRC 198 */ 199 200 u32 nbytes_past_boundary = (args.dest_offset % TS_PACKET_LEN); 201 bool aligned = (nbytes_past_boundary == 0); 202 struct vidtv_mpeg_ts ts_header = {}; 203 204 /* number of bytes written by this function */ 205 u32 nbytes = 0; 206 /* how much there is left to write */ 207 u32 remaining_len = args.len; 208 /* how much we can be written in this packet */ 209 u32 payload_write_len = 0; 210 /* where we are in the source */ 211 u32 payload_offset = 0; 212 213 const u16 PAYLOAD_START = args.new_psi_section; 214 215 if (!args.crc && !args.is_crc) 216 pr_warn_ratelimited("Missing CRC for chunk\n"); 217 218 if (args.crc) 219 *args.crc = dvb_crc32(*args.crc, args.from, args.len); 220 221 if (args.new_psi_section && !aligned) { 222 pr_warn_ratelimited("Cannot write a new PSI section in a misaligned buffer\n"); 223 224 /* forcibly align and hope for the best */ 225 nbytes += vidtv_memset(args.dest_buf, 226 args.dest_offset + nbytes, 227 args.dest_buf_sz, 228 TS_FILL_BYTE, 229 TS_PACKET_LEN - nbytes_past_boundary); 230 } 231 232 while (remaining_len) { 233 nbytes_past_boundary = (args.dest_offset + nbytes) % TS_PACKET_LEN; 234 aligned = (nbytes_past_boundary == 0); 235 236 if (aligned) { 237 /* if at a packet boundary, write a new TS header */ 238 ts_header.sync_byte = TS_SYNC_BYTE; 239 ts_header.bitfield = cpu_to_be16((PAYLOAD_START << 14) | args.pid); 240 ts_header.scrambling = 0; 241 ts_header.continuity_counter = *args.continuity_counter; 242 ts_header.payload = 1; 243 /* no adaptation field */ 244 ts_header.adaptation_field = 0; 245 246 /* copy the header */ 247 nbytes += vidtv_memcpy(args.dest_buf, 248 args.dest_offset + nbytes, 249 args.dest_buf_sz, 250 &ts_header, 251 sizeof(ts_header)); 252 /* 253 * This will trigger a discontinuity if the buffer is full, 254 * effectively dropping the packet. 255 */ 256 vidtv_ts_inc_cc(args.continuity_counter); 257 } 258 259 /* write the pointer_field in the first byte of the payload */ 260 if (args.new_psi_section) 261 nbytes += vidtv_memset(args.dest_buf, 262 args.dest_offset + nbytes, 263 args.dest_buf_sz, 264 0x0, 265 1); 266 267 /* write as much of the payload as possible */ 268 nbytes_past_boundary = (args.dest_offset + nbytes) % TS_PACKET_LEN; 269 payload_write_len = min(TS_PACKET_LEN - nbytes_past_boundary, remaining_len); 270 271 nbytes += vidtv_memcpy(args.dest_buf, 272 args.dest_offset + nbytes, 273 args.dest_buf_sz, 274 args.from + payload_offset, 275 payload_write_len); 276 277 /* 'payload_write_len' written from a total of 'len' requested*/ 278 remaining_len -= payload_write_len; 279 payload_offset += payload_write_len; 280 } 281 282 /* 283 * fill the rest of the packet if there is any remaining space unused 284 */ 285 286 nbytes_past_boundary = (args.dest_offset + nbytes) % TS_PACKET_LEN; 287 288 if (args.is_crc) 289 nbytes += vidtv_memset(args.dest_buf, 290 args.dest_offset + nbytes, 291 args.dest_buf_sz, 292 TS_FILL_BYTE, 293 TS_PACKET_LEN - nbytes_past_boundary); 294 295 return nbytes; 296 } 297 298 static u32 table_section_crc32_write_into(struct crc32_write_args args) 299 { 300 /* the CRC is the last entry in the section */ 301 u32 nbytes = 0; 302 struct psi_write_args psi_args = {}; 303 304 psi_args.dest_buf = args.dest_buf; 305 psi_args.from = &args.crc; 306 psi_args.len = CRC_SIZE_IN_BYTES; 307 psi_args.dest_offset = args.dest_offset; 308 psi_args.pid = args.pid; 309 psi_args.new_psi_section = false; 310 psi_args.continuity_counter = args.continuity_counter; 311 psi_args.is_crc = true; 312 psi_args.dest_buf_sz = args.dest_buf_sz; 313 314 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 315 316 return nbytes; 317 } 318 319 struct vidtv_psi_desc_service *vidtv_psi_service_desc_init(struct vidtv_psi_desc *head, 320 enum service_type service_type, 321 char *service_name, 322 char *provider_name) 323 { 324 struct vidtv_psi_desc_service *desc; 325 u32 service_name_len = service_name ? strlen(service_name) : 0; 326 u32 provider_name_len = provider_name ? strlen(provider_name) : 0; 327 328 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 329 330 desc->type = SERVICE_DESCRIPTOR; 331 332 desc->length = sizeof_field(struct vidtv_psi_desc_service, service_type) 333 + sizeof_field(struct vidtv_psi_desc_service, provider_name_len) 334 + provider_name_len 335 + sizeof_field(struct vidtv_psi_desc_service, service_name_len) 336 + service_name_len; 337 338 desc->service_type = service_type; 339 340 desc->service_name_len = service_name_len; 341 342 if (service_name && service_name_len) 343 desc->service_name = kstrdup(service_name, GFP_KERNEL); 344 345 desc->provider_name_len = provider_name_len; 346 347 if (provider_name && provider_name_len) 348 desc->provider_name = kstrdup(provider_name, GFP_KERNEL); 349 350 if (head) { 351 while (head->next) 352 head = head->next; 353 354 head->next = (struct vidtv_psi_desc *)desc; 355 } 356 return desc; 357 } 358 359 struct vidtv_psi_desc_registration 360 *vidtv_psi_registration_desc_init(struct vidtv_psi_desc *head, 361 __be32 format_id, 362 u8 *additional_ident_info, 363 u32 additional_info_len) 364 { 365 struct vidtv_psi_desc_registration *desc; 366 367 desc = kzalloc(sizeof(*desc) + sizeof(format_id) + additional_info_len, GFP_KERNEL); 368 369 desc->type = REGISTRATION_DESCRIPTOR; 370 371 desc->length = sizeof_field(struct vidtv_psi_desc_registration, format_id) 372 + additional_info_len; 373 374 desc->format_id = format_id; 375 376 if (additional_ident_info && additional_info_len) 377 memcpy(desc->additional_identification_info, 378 additional_ident_info, 379 additional_info_len); 380 381 if (head) { 382 while (head->next) 383 head = head->next; 384 385 head->next = (struct vidtv_psi_desc *)desc; 386 } 387 388 return desc; 389 } 390 391 struct vidtv_psi_desc *vidtv_psi_desc_clone(struct vidtv_psi_desc *desc) 392 { 393 struct vidtv_psi_desc *head = NULL; 394 struct vidtv_psi_desc *prev = NULL; 395 struct vidtv_psi_desc *curr = NULL; 396 397 struct vidtv_psi_desc_service *service; 398 399 while (desc) { 400 switch (desc->type) { 401 case SERVICE_DESCRIPTOR: 402 service = (struct vidtv_psi_desc_service *)desc; 403 curr = (struct vidtv_psi_desc *) 404 vidtv_psi_service_desc_init(head, 405 service->service_type, 406 service->service_name, 407 service->provider_name); 408 break; 409 410 case REGISTRATION_DESCRIPTOR: 411 default: 412 curr = kzalloc(sizeof(*desc) + desc->length, GFP_KERNEL); 413 memcpy(curr, desc, sizeof(*desc) + desc->length); 414 break; 415 } 416 417 if (curr) 418 curr->next = NULL; 419 if (!head) 420 head = curr; 421 if (prev) 422 prev->next = curr; 423 424 prev = curr; 425 desc = desc->next; 426 } 427 428 return head; 429 } 430 431 void vidtv_psi_desc_destroy(struct vidtv_psi_desc *desc) 432 { 433 struct vidtv_psi_desc *curr = desc; 434 struct vidtv_psi_desc *tmp = NULL; 435 436 while (curr) { 437 tmp = curr; 438 curr = curr->next; 439 440 switch (tmp->type) { 441 case SERVICE_DESCRIPTOR: 442 kfree(((struct vidtv_psi_desc_service *)tmp)->provider_name); 443 kfree(((struct vidtv_psi_desc_service *)tmp)->service_name); 444 445 break; 446 case REGISTRATION_DESCRIPTOR: 447 /* nothing to do */ 448 break; 449 450 default: 451 pr_warn_ratelimited("Possible leak: not handling descriptor type %d\n", 452 tmp->type); 453 break; 454 } 455 456 kfree(tmp); 457 } 458 } 459 460 static u16 461 vidtv_psi_desc_comp_loop_len(struct vidtv_psi_desc *desc) 462 { 463 u32 length = 0; 464 465 if (!desc) 466 return 0; 467 468 while (desc) { 469 length += sizeof_field(struct vidtv_psi_desc, type); 470 length += sizeof_field(struct vidtv_psi_desc, length); 471 length += desc->length; /* from 'length' field until the end of the descriptor */ 472 desc = desc->next; 473 } 474 475 return length; 476 } 477 478 void vidtv_psi_desc_assign(struct vidtv_psi_desc **to, 479 struct vidtv_psi_desc *desc) 480 { 481 if (desc == *to) 482 return; 483 484 if (*to) 485 vidtv_psi_desc_destroy(*to); 486 487 *to = desc; 488 } 489 490 void vidtv_pmt_desc_assign(struct vidtv_psi_table_pmt *pmt, 491 struct vidtv_psi_desc **to, 492 struct vidtv_psi_desc *desc) 493 { 494 vidtv_psi_desc_assign(to, desc); 495 vidtv_psi_pmt_table_update_sec_len(pmt); 496 497 if (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN) 498 vidtv_psi_desc_assign(to, NULL); 499 500 vidtv_psi_update_version_num(&pmt->header); 501 } 502 503 void vidtv_sdt_desc_assign(struct vidtv_psi_table_sdt *sdt, 504 struct vidtv_psi_desc **to, 505 struct vidtv_psi_desc *desc) 506 { 507 vidtv_psi_desc_assign(to, desc); 508 vidtv_psi_sdt_table_update_sec_len(sdt); 509 510 if (vidtv_psi_get_sec_len(&sdt->header) > MAX_SECTION_LEN) 511 vidtv_psi_desc_assign(to, NULL); 512 513 vidtv_psi_update_version_num(&sdt->header); 514 } 515 516 static u32 vidtv_psi_desc_write_into(struct desc_write_args args) 517 { 518 /* the number of bytes written by this function */ 519 u32 nbytes = 0; 520 struct psi_write_args psi_args = {}; 521 522 psi_args.dest_buf = args.dest_buf; 523 psi_args.from = &args.desc->type; 524 525 psi_args.len = sizeof_field(struct vidtv_psi_desc, type) + 526 sizeof_field(struct vidtv_psi_desc, length); 527 528 psi_args.dest_offset = args.dest_offset + nbytes; 529 psi_args.pid = args.pid; 530 psi_args.new_psi_section = false; 531 psi_args.continuity_counter = args.continuity_counter; 532 psi_args.is_crc = false; 533 psi_args.dest_buf_sz = args.dest_buf_sz; 534 psi_args.crc = args.crc; 535 536 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 537 538 switch (args.desc->type) { 539 case SERVICE_DESCRIPTOR: 540 psi_args.dest_offset = args.dest_offset + nbytes; 541 psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_type) + 542 sizeof_field(struct vidtv_psi_desc_service, provider_name_len); 543 psi_args.from = &((struct vidtv_psi_desc_service *)args.desc)->service_type; 544 545 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 546 547 psi_args.dest_offset = args.dest_offset + nbytes; 548 psi_args.len = ((struct vidtv_psi_desc_service *)args.desc)->provider_name_len; 549 psi_args.from = ((struct vidtv_psi_desc_service *)args.desc)->provider_name; 550 551 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 552 553 psi_args.dest_offset = args.dest_offset + nbytes; 554 psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_name_len); 555 psi_args.from = &((struct vidtv_psi_desc_service *)args.desc)->service_name_len; 556 557 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 558 559 psi_args.dest_offset = args.dest_offset + nbytes; 560 psi_args.len = ((struct vidtv_psi_desc_service *)args.desc)->service_name_len; 561 psi_args.from = ((struct vidtv_psi_desc_service *)args.desc)->service_name; 562 563 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 564 break; 565 566 case REGISTRATION_DESCRIPTOR: 567 default: 568 psi_args.dest_offset = args.dest_offset + nbytes; 569 psi_args.len = args.desc->length; 570 psi_args.from = &args.desc->data; 571 572 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 573 break; 574 } 575 576 return nbytes; 577 } 578 579 static u32 580 vidtv_psi_table_header_write_into(struct header_write_args args) 581 { 582 /* the number of bytes written by this function */ 583 u32 nbytes = 0; 584 struct psi_write_args psi_args = {}; 585 586 psi_args.dest_buf = args.dest_buf; 587 psi_args.from = args.h; 588 psi_args.len = sizeof(struct vidtv_psi_table_header); 589 psi_args.dest_offset = args.dest_offset; 590 psi_args.pid = args.pid; 591 psi_args.new_psi_section = true; 592 psi_args.continuity_counter = args.continuity_counter; 593 psi_args.is_crc = false; 594 psi_args.dest_buf_sz = args.dest_buf_sz; 595 psi_args.crc = args.crc; 596 597 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 598 599 return nbytes; 600 } 601 602 void 603 vidtv_psi_pat_table_update_sec_len(struct vidtv_psi_table_pat *pat) 604 { 605 /* see ISO/IEC 13818-1 : 2000 p.43 */ 606 u16 length = 0; 607 u32 i; 608 609 /* from immediately after 'section_length' until 'last_section_number'*/ 610 length += PAT_LEN_UNTIL_LAST_SECTION_NUMBER; 611 612 /* do not count the pointer */ 613 for (i = 0; i < pat->programs; ++i) 614 length += sizeof(struct vidtv_psi_table_pat_program) - 615 sizeof(struct vidtv_psi_table_pat_program *); 616 617 length += CRC_SIZE_IN_BYTES; 618 619 vidtv_psi_set_sec_len(&pat->header, length); 620 } 621 622 void vidtv_psi_pmt_table_update_sec_len(struct vidtv_psi_table_pmt *pmt) 623 { 624 /* see ISO/IEC 13818-1 : 2000 p.46 */ 625 u16 length = 0; 626 struct vidtv_psi_table_pmt_stream *s = pmt->stream; 627 u16 desc_loop_len; 628 629 /* from immediately after 'section_length' until 'program_info_length'*/ 630 length += PMT_LEN_UNTIL_PROGRAM_INFO_LENGTH; 631 632 desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor); 633 vidtv_psi_set_desc_loop_len(&pmt->bitfield2, desc_loop_len, 10); 634 635 length += desc_loop_len; 636 637 while (s) { 638 /* skip both pointers at the end */ 639 length += sizeof(struct vidtv_psi_table_pmt_stream) - 640 sizeof(struct vidtv_psi_desc *) - 641 sizeof(struct vidtv_psi_table_pmt_stream *); 642 643 desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor); 644 vidtv_psi_set_desc_loop_len(&s->bitfield2, desc_loop_len, 10); 645 646 length += desc_loop_len; 647 648 s = s->next; 649 } 650 651 length += CRC_SIZE_IN_BYTES; 652 653 vidtv_psi_set_sec_len(&pmt->header, length); 654 } 655 656 void vidtv_psi_sdt_table_update_sec_len(struct vidtv_psi_table_sdt *sdt) 657 { 658 /* see ETSI EN 300 468 V 1.10.1 p.24 */ 659 u16 length = 0; 660 struct vidtv_psi_table_sdt_service *s = sdt->service; 661 u16 desc_loop_len; 662 663 /* 664 * from immediately after 'section_length' until 665 * 'reserved_for_future_use' 666 */ 667 length += SDT_LEN_UNTIL_RESERVED_FOR_FUTURE_USE; 668 669 while (s) { 670 /* skip both pointers at the end */ 671 length += sizeof(struct vidtv_psi_table_sdt_service) - 672 sizeof(struct vidtv_psi_desc *) - 673 sizeof(struct vidtv_psi_table_sdt_service *); 674 675 desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor); 676 vidtv_psi_set_desc_loop_len(&s->bitfield, desc_loop_len, 12); 677 678 length += desc_loop_len; 679 680 s = s->next; 681 } 682 683 length += CRC_SIZE_IN_BYTES; 684 685 vidtv_psi_set_sec_len(&sdt->header, length); 686 } 687 688 struct vidtv_psi_table_pat_program* 689 vidtv_psi_pat_program_init(struct vidtv_psi_table_pat_program *head, 690 u16 service_id, 691 u16 program_map_pid) 692 { 693 struct vidtv_psi_table_pat_program *program; 694 const u16 RESERVED = 0x07; 695 696 program = kzalloc(sizeof(*program), GFP_KERNEL); 697 698 program->service_id = cpu_to_be16(service_id); 699 700 /* pid for the PMT section in the TS */ 701 program->bitfield = cpu_to_be16((RESERVED << 13) | program_map_pid); 702 program->next = NULL; 703 704 if (head) { 705 while (head->next) 706 head = head->next; 707 708 head->next = program; 709 } 710 711 return program; 712 } 713 714 void 715 vidtv_psi_pat_program_destroy(struct vidtv_psi_table_pat_program *p) 716 { 717 struct vidtv_psi_table_pat_program *curr = p; 718 struct vidtv_psi_table_pat_program *tmp = NULL; 719 720 while (curr) { 721 tmp = curr; 722 curr = curr->next; 723 kfree(tmp); 724 } 725 } 726 727 void 728 vidtv_psi_pat_program_assign(struct vidtv_psi_table_pat *pat, 729 struct vidtv_psi_table_pat_program *p) 730 { 731 /* This function transfers ownership of p to the table */ 732 733 u16 program_count = 0; 734 struct vidtv_psi_table_pat_program *program = p; 735 736 if (p == pat->program) 737 return; 738 739 while (program) { 740 ++program_count; 741 program = program->next; 742 } 743 744 pat->programs = program_count; 745 pat->program = p; 746 747 /* Recompute section length */ 748 vidtv_psi_pat_table_update_sec_len(pat); 749 750 if (vidtv_psi_get_sec_len(&pat->header) > MAX_SECTION_LEN) 751 vidtv_psi_pat_program_assign(pat, NULL); 752 753 vidtv_psi_update_version_num(&pat->header); 754 } 755 756 struct vidtv_psi_table_pat *vidtv_psi_pat_table_init(u16 transport_stream_id) 757 { 758 struct vidtv_psi_table_pat *pat = kzalloc(sizeof(*pat), GFP_KERNEL); 759 const u16 SYNTAX = 0x1; 760 const u16 ZERO = 0x0; 761 const u16 ONES = 0x03; 762 763 pat->header.table_id = 0x0; 764 765 pat->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12)); 766 pat->header.id = cpu_to_be16(transport_stream_id); 767 pat->header.current_next = 0x1; 768 769 pat->header.version = 0x1f; 770 771 pat->header.one2 = 0x03; 772 pat->header.section_id = 0x0; 773 pat->header.last_section = 0x0; 774 775 pat->programs = 0; 776 777 vidtv_psi_pat_table_update_sec_len(pat); 778 779 return pat; 780 } 781 782 u32 vidtv_psi_pat_write_into(struct vidtv_psi_pat_write_args args) 783 { 784 /* the number of bytes written by this function */ 785 u32 nbytes = 0; 786 const u16 pat_pid = VIDTV_PAT_PID; 787 u32 crc = 0xffffffff; 788 789 struct vidtv_psi_table_pat_program *p = args.pat->program; 790 791 struct header_write_args h_args = {}; 792 struct psi_write_args psi_args = {}; 793 struct crc32_write_args c_args = {}; 794 795 vidtv_psi_pat_table_update_sec_len(args.pat); 796 797 h_args.dest_buf = args.buf; 798 h_args.dest_offset = args.offset; 799 h_args.h = &args.pat->header; 800 h_args.pid = pat_pid; 801 h_args.continuity_counter = args.continuity_counter; 802 h_args.dest_buf_sz = args.buf_sz; 803 h_args.crc = &crc; 804 805 nbytes += vidtv_psi_table_header_write_into(h_args); 806 807 /* note that the field 'u16 programs' is not really part of the PAT */ 808 809 psi_args.dest_buf = args.buf; 810 psi_args.pid = pat_pid; 811 psi_args.new_psi_section = false; 812 psi_args.continuity_counter = args.continuity_counter; 813 psi_args.is_crc = false; 814 psi_args.dest_buf_sz = args.buf_sz; 815 psi_args.crc = &crc; 816 817 while (p) { 818 /* copy the PAT programs */ 819 psi_args.from = p; 820 /* skip the pointer */ 821 psi_args.len = sizeof(*p) - 822 sizeof(struct vidtv_psi_table_pat_program *); 823 psi_args.dest_offset = args.offset + nbytes; 824 825 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 826 827 p = p->next; 828 } 829 830 c_args.dest_buf = args.buf; 831 c_args.dest_offset = args.offset + nbytes; 832 c_args.crc = cpu_to_be32(crc); 833 c_args.pid = pat_pid; 834 c_args.continuity_counter = args.continuity_counter; 835 c_args.dest_buf_sz = args.buf_sz; 836 837 /* Write the CRC32 at the end */ 838 nbytes += table_section_crc32_write_into(c_args); 839 840 return nbytes; 841 } 842 843 void 844 vidtv_psi_pat_table_destroy(struct vidtv_psi_table_pat *p) 845 { 846 vidtv_psi_pat_program_destroy(p->program); 847 kfree(p); 848 } 849 850 struct vidtv_psi_table_pmt_stream* 851 vidtv_psi_pmt_stream_init(struct vidtv_psi_table_pmt_stream *head, 852 enum vidtv_psi_stream_types stream_type, 853 u16 es_pid) 854 { 855 struct vidtv_psi_table_pmt_stream *stream; 856 const u16 RESERVED1 = 0x07; 857 const u16 RESERVED2 = 0x0f; 858 const u16 ZERO = 0x0; 859 u16 desc_loop_len; 860 861 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 862 863 stream->type = stream_type; 864 865 stream->bitfield = cpu_to_be16((RESERVED1 << 13) | es_pid); 866 867 desc_loop_len = vidtv_psi_desc_comp_loop_len(stream->descriptor); 868 869 stream->bitfield2 = cpu_to_be16((RESERVED2 << 12) | 870 (ZERO << 10) | 871 desc_loop_len); 872 stream->next = NULL; 873 874 if (head) { 875 while (head->next) 876 head = head->next; 877 878 head->next = stream; 879 } 880 881 return stream; 882 } 883 884 void vidtv_psi_pmt_stream_destroy(struct vidtv_psi_table_pmt_stream *s) 885 { 886 struct vidtv_psi_table_pmt_stream *curr_stream = s; 887 struct vidtv_psi_table_pmt_stream *tmp_stream = NULL; 888 889 while (curr_stream) { 890 tmp_stream = curr_stream; 891 curr_stream = curr_stream->next; 892 vidtv_psi_desc_destroy(tmp_stream->descriptor); 893 kfree(tmp_stream); 894 } 895 } 896 897 void vidtv_psi_pmt_stream_assign(struct vidtv_psi_table_pmt *pmt, 898 struct vidtv_psi_table_pmt_stream *s) 899 { 900 /* This function transfers ownership of s to the table */ 901 if (s == pmt->stream) 902 return; 903 904 pmt->stream = s; 905 vidtv_psi_pmt_table_update_sec_len(pmt); 906 907 if (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN) 908 vidtv_psi_pmt_stream_assign(pmt, NULL); 909 910 vidtv_psi_update_version_num(&pmt->header); 911 } 912 913 u16 vidtv_psi_pmt_get_pid(struct vidtv_psi_table_pmt *section, 914 struct vidtv_psi_table_pat *pat) 915 { 916 struct vidtv_psi_table_pat_program *program = pat->program; 917 918 /* 919 * service_id is the same as program_number in the 920 * corresponding program_map_section 921 * see ETSI EN 300 468 v1.15.1 p. 24 922 */ 923 while (program) { 924 if (program->service_id == section->header.id) 925 return vidtv_psi_get_pat_program_pid(program); 926 927 program = program->next; 928 } 929 930 return TS_LAST_VALID_PID + 1; /* not found */ 931 } 932 933 struct vidtv_psi_table_pmt *vidtv_psi_pmt_table_init(u16 program_number, 934 u16 pcr_pid) 935 { 936 struct vidtv_psi_table_pmt *pmt = kzalloc(sizeof(*pmt), GFP_KERNEL); 937 const u16 SYNTAX = 0x1; 938 const u16 ZERO = 0x0; 939 const u16 ONES = 0x03; 940 const u16 RESERVED1 = 0x07; 941 const u16 RESERVED2 = 0x0f; 942 u16 desc_loop_len; 943 944 if (!pcr_pid) 945 pcr_pid = 0x1fff; 946 947 pmt->header.table_id = 0x2; 948 949 pmt->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12)); 950 951 pmt->header.id = cpu_to_be16(program_number); 952 pmt->header.current_next = 0x1; 953 954 pmt->header.version = 0x1f; 955 956 pmt->header.one2 = ONES; 957 pmt->header.section_id = 0; 958 pmt->header.last_section = 0; 959 960 pmt->bitfield = cpu_to_be16((RESERVED1 << 13) | pcr_pid); 961 962 desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor); 963 964 pmt->bitfield2 = cpu_to_be16((RESERVED2 << 12) | 965 (ZERO << 10) | 966 desc_loop_len); 967 968 vidtv_psi_pmt_table_update_sec_len(pmt); 969 970 return pmt; 971 } 972 973 u32 vidtv_psi_pmt_write_into(struct vidtv_psi_pmt_write_args args) 974 { 975 /* the number of bytes written by this function */ 976 u32 nbytes = 0; 977 u32 crc = 0xffffffff; 978 979 struct vidtv_psi_desc *table_descriptor = args.pmt->descriptor; 980 struct vidtv_psi_table_pmt_stream *stream = args.pmt->stream; 981 struct vidtv_psi_desc *stream_descriptor = (stream) ? 982 args.pmt->stream->descriptor : 983 NULL; 984 985 struct header_write_args h_args = {}; 986 struct psi_write_args psi_args = {}; 987 struct desc_write_args d_args = {}; 988 struct crc32_write_args c_args = {}; 989 990 vidtv_psi_pmt_table_update_sec_len(args.pmt); 991 992 h_args.dest_buf = args.buf; 993 h_args.dest_offset = args.offset; 994 h_args.h = &args.pmt->header; 995 h_args.pid = args.pid; 996 h_args.continuity_counter = args.continuity_counter; 997 h_args.dest_buf_sz = args.buf_sz; 998 h_args.crc = &crc; 999 1000 nbytes += vidtv_psi_table_header_write_into(h_args); 1001 1002 /* write the two bitfields */ 1003 psi_args.dest_buf = args.buf; 1004 psi_args.from = &args.pmt->bitfield; 1005 psi_args.len = sizeof_field(struct vidtv_psi_table_pmt, bitfield) + 1006 sizeof_field(struct vidtv_psi_table_pmt, bitfield2); 1007 1008 psi_args.dest_offset = args.offset + nbytes; 1009 psi_args.pid = args.pid; 1010 psi_args.new_psi_section = false; 1011 psi_args.continuity_counter = args.continuity_counter; 1012 psi_args.is_crc = false; 1013 psi_args.dest_buf_sz = args.buf_sz; 1014 psi_args.crc = &crc; 1015 1016 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 1017 1018 while (table_descriptor) { 1019 /* write the descriptors, if any */ 1020 d_args.dest_buf = args.buf; 1021 d_args.dest_offset = args.offset + nbytes; 1022 d_args.desc = table_descriptor; 1023 d_args.pid = args.pid; 1024 d_args.continuity_counter = args.continuity_counter; 1025 d_args.dest_buf_sz = args.buf_sz; 1026 d_args.crc = &crc; 1027 1028 nbytes += vidtv_psi_desc_write_into(d_args); 1029 1030 table_descriptor = table_descriptor->next; 1031 } 1032 1033 while (stream) { 1034 /* write the streams, if any */ 1035 psi_args.from = stream; 1036 psi_args.len = sizeof_field(struct vidtv_psi_table_pmt_stream, type) + 1037 sizeof_field(struct vidtv_psi_table_pmt_stream, bitfield) + 1038 sizeof_field(struct vidtv_psi_table_pmt_stream, bitfield2); 1039 psi_args.dest_offset = args.offset + nbytes; 1040 1041 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 1042 1043 while (stream_descriptor) { 1044 /* write the stream descriptors, if any */ 1045 d_args.dest_buf = args.buf; 1046 d_args.dest_offset = args.offset + nbytes; 1047 d_args.desc = stream_descriptor; 1048 d_args.pid = args.pid; 1049 d_args.continuity_counter = args.continuity_counter; 1050 d_args.dest_buf_sz = args.buf_sz; 1051 d_args.crc = &crc; 1052 1053 nbytes += vidtv_psi_desc_write_into(d_args); 1054 1055 stream_descriptor = stream_descriptor->next; 1056 } 1057 1058 stream = stream->next; 1059 } 1060 1061 c_args.dest_buf = args.buf; 1062 c_args.dest_offset = args.offset + nbytes; 1063 c_args.crc = cpu_to_be32(crc); 1064 c_args.pid = args.pid; 1065 c_args.continuity_counter = args.continuity_counter; 1066 c_args.dest_buf_sz = args.buf_sz; 1067 1068 /* Write the CRC32 at the end */ 1069 nbytes += table_section_crc32_write_into(c_args); 1070 1071 return nbytes; 1072 } 1073 1074 void vidtv_psi_pmt_table_destroy(struct vidtv_psi_table_pmt *pmt) 1075 { 1076 vidtv_psi_desc_destroy(pmt->descriptor); 1077 vidtv_psi_pmt_stream_destroy(pmt->stream); 1078 kfree(pmt); 1079 } 1080 1081 struct vidtv_psi_table_sdt *vidtv_psi_sdt_table_init(u16 transport_stream_id) 1082 { 1083 struct vidtv_psi_table_sdt *sdt = kzalloc(sizeof(*sdt), GFP_KERNEL); 1084 const u16 SYNTAX = 0x1; 1085 const u16 ONE = 0x1; 1086 const u16 ONES = 0x03; 1087 const u16 RESERVED = 0xff; 1088 1089 sdt->header.table_id = 0x42; 1090 1091 sdt->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ONE << 14) | (ONES << 12)); 1092 1093 /* 1094 * This is a 16-bit field which serves as a label for identification 1095 * of the TS, about which the SDT informs, from any other multiplex 1096 * within the delivery system. 1097 */ 1098 sdt->header.id = cpu_to_be16(transport_stream_id); 1099 sdt->header.current_next = ONE; 1100 1101 sdt->header.version = 0x1f; 1102 1103 sdt->header.one2 = ONES; 1104 sdt->header.section_id = 0; 1105 sdt->header.last_section = 0; 1106 1107 /* 1108 * FIXME: The network_id range from 0xFF01 to 0xFFFF is used to 1109 * indicate temporary private use. For now, let's use the first 1110 * value. 1111 * This can be changed to something more useful, when support for 1112 * NIT gets added 1113 */ 1114 sdt->network_id = cpu_to_be16(0xff01); 1115 sdt->reserved = RESERVED; 1116 1117 vidtv_psi_sdt_table_update_sec_len(sdt); 1118 1119 return sdt; 1120 } 1121 1122 u32 vidtv_psi_sdt_write_into(struct vidtv_psi_sdt_write_args args) 1123 { 1124 u32 nbytes = 0; 1125 u16 sdt_pid = VIDTV_SDT_PID; /* see ETSI EN 300 468 v1.15.1 p. 11 */ 1126 1127 u32 crc = 0xffffffff; 1128 1129 struct vidtv_psi_table_sdt_service *service = args.sdt->service; 1130 struct vidtv_psi_desc *service_desc = (args.sdt->service) ? 1131 args.sdt->service->descriptor : 1132 NULL; 1133 1134 struct header_write_args h_args = {}; 1135 struct psi_write_args psi_args = {}; 1136 struct desc_write_args d_args = {}; 1137 struct crc32_write_args c_args = {}; 1138 1139 vidtv_psi_sdt_table_update_sec_len(args.sdt); 1140 1141 h_args.dest_buf = args.buf; 1142 h_args.dest_offset = args.offset; 1143 h_args.h = &args.sdt->header; 1144 h_args.pid = sdt_pid; 1145 h_args.continuity_counter = args.continuity_counter; 1146 h_args.dest_buf_sz = args.buf_sz; 1147 h_args.crc = &crc; 1148 1149 nbytes += vidtv_psi_table_header_write_into(h_args); 1150 1151 psi_args.dest_buf = args.buf; 1152 psi_args.from = &args.sdt->network_id; 1153 1154 psi_args.len = sizeof_field(struct vidtv_psi_table_sdt, network_id) + 1155 sizeof_field(struct vidtv_psi_table_sdt, reserved); 1156 1157 psi_args.dest_offset = args.offset + nbytes; 1158 psi_args.pid = sdt_pid; 1159 psi_args.new_psi_section = false; 1160 psi_args.continuity_counter = args.continuity_counter; 1161 psi_args.is_crc = false; 1162 psi_args.dest_buf_sz = args.buf_sz; 1163 psi_args.crc = &crc; 1164 1165 /* copy u16 network_id + u8 reserved)*/ 1166 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 1167 1168 while (service) { 1169 /* copy the services, if any */ 1170 psi_args.from = service; 1171 /* skip both pointers at the end */ 1172 psi_args.len = sizeof(struct vidtv_psi_table_sdt_service) - 1173 sizeof(struct vidtv_psi_desc *) - 1174 sizeof(struct vidtv_psi_table_sdt_service *); 1175 psi_args.dest_offset = args.offset + nbytes; 1176 1177 nbytes += vidtv_psi_ts_psi_write_into(psi_args); 1178 1179 while (service_desc) { 1180 /* copy the service descriptors, if any */ 1181 d_args.dest_buf = args.buf; 1182 d_args.dest_offset = args.offset + nbytes; 1183 d_args.desc = service_desc; 1184 d_args.pid = sdt_pid; 1185 d_args.continuity_counter = args.continuity_counter; 1186 d_args.dest_buf_sz = args.buf_sz; 1187 d_args.crc = &crc; 1188 1189 nbytes += vidtv_psi_desc_write_into(d_args); 1190 1191 service_desc = service_desc->next; 1192 } 1193 1194 service = service->next; 1195 } 1196 1197 c_args.dest_buf = args.buf; 1198 c_args.dest_offset = args.offset + nbytes; 1199 c_args.crc = cpu_to_be32(crc); 1200 c_args.pid = sdt_pid; 1201 c_args.continuity_counter = args.continuity_counter; 1202 c_args.dest_buf_sz = args.buf_sz; 1203 1204 /* Write the CRC at the end */ 1205 nbytes += table_section_crc32_write_into(c_args); 1206 1207 return nbytes; 1208 } 1209 1210 void vidtv_psi_sdt_table_destroy(struct vidtv_psi_table_sdt *sdt) 1211 { 1212 vidtv_psi_sdt_service_destroy(sdt->service); 1213 kfree(sdt); 1214 } 1215 1216 struct vidtv_psi_table_sdt_service 1217 *vidtv_psi_sdt_service_init(struct vidtv_psi_table_sdt_service *head, 1218 u16 service_id) 1219 { 1220 struct vidtv_psi_table_sdt_service *service; 1221 1222 service = kzalloc(sizeof(*service), GFP_KERNEL); 1223 1224 /* 1225 * ETSI 300 468: this is a 16bit field which serves as a label to 1226 * identify this service from any other service within the TS. 1227 * The service id is the same as the program number in the 1228 * corresponding program_map_section 1229 */ 1230 service->service_id = cpu_to_be16(service_id); 1231 service->EIT_schedule = 0x0; 1232 service->EIT_present_following = 0x0; 1233 service->reserved = 0x3f; 1234 1235 service->bitfield = cpu_to_be16(RUNNING << 13); 1236 1237 if (head) { 1238 while (head->next) 1239 head = head->next; 1240 1241 head->next = service; 1242 } 1243 1244 return service; 1245 } 1246 1247 void 1248 vidtv_psi_sdt_service_destroy(struct vidtv_psi_table_sdt_service *service) 1249 { 1250 struct vidtv_psi_table_sdt_service *curr = service; 1251 struct vidtv_psi_table_sdt_service *tmp = NULL; 1252 1253 while (curr) { 1254 tmp = curr; 1255 curr = curr->next; 1256 vidtv_psi_desc_destroy(tmp->descriptor); 1257 kfree(tmp); 1258 } 1259 } 1260 1261 void 1262 vidtv_psi_sdt_service_assign(struct vidtv_psi_table_sdt *sdt, 1263 struct vidtv_psi_table_sdt_service *service) 1264 { 1265 if (service == sdt->service) 1266 return; 1267 1268 sdt->service = service; 1269 1270 /* recompute section length */ 1271 vidtv_psi_sdt_table_update_sec_len(sdt); 1272 1273 if (vidtv_psi_get_sec_len(&sdt->header) > MAX_SECTION_LEN) 1274 vidtv_psi_sdt_service_assign(sdt, NULL); 1275 1276 vidtv_psi_update_version_num(&sdt->header); 1277 } 1278 1279 struct vidtv_psi_table_pmt** 1280 vidtv_psi_pmt_create_sec_for_each_pat_entry(struct vidtv_psi_table_pat *pat, u16 pcr_pid) 1281 1282 { 1283 /* 1284 * PMTs contain information about programs. For each program, 1285 * there is one PMT section. This function will create a section 1286 * for each program found in the PAT 1287 */ 1288 struct vidtv_psi_table_pat_program *program = pat->program; 1289 struct vidtv_psi_table_pmt **pmt_secs; 1290 u32 i = 0; 1291 1292 /* a section for each program_id */ 1293 pmt_secs = kcalloc(pat->programs, 1294 sizeof(struct vidtv_psi_table_pmt *), 1295 GFP_KERNEL); 1296 1297 while (program) { 1298 pmt_secs[i] = vidtv_psi_pmt_table_init(be16_to_cpu(program->service_id), pcr_pid); 1299 ++i; 1300 program = program->next; 1301 } 1302 1303 return pmt_secs; 1304 } 1305 1306 struct vidtv_psi_table_pmt 1307 *vidtv_psi_find_pmt_sec(struct vidtv_psi_table_pmt **pmt_sections, 1308 u16 nsections, 1309 u16 program_num) 1310 { 1311 /* find the PMT section associated with 'program_num' */ 1312 struct vidtv_psi_table_pmt *sec = NULL; 1313 u32 i; 1314 1315 for (i = 0; i < nsections; ++i) { 1316 sec = pmt_sections[i]; 1317 if (be16_to_cpu(sec->header.id) == program_num) 1318 return sec; 1319 } 1320 1321 return NULL; /* not found */ 1322 } 1323