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 * Copyright (C) 2020 Daniel W. S. Almeida 10 */ 11 12 #define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__ 13 14 #include <linux/bcd.h> 15 #include <linux/crc32.h> 16 #include <linux/kernel.h> 17 #include <linux/ktime.h> 18 #include <linux/printk.h> 19 #include <linux/ratelimit.h> 20 #include <linux/slab.h> 21 #include <linux/string.h> 22 #include <linux/string.h> 23 #include <linux/time.h> 24 #include <linux/types.h> 25 26 #include "vidtv_common.h" 27 #include "vidtv_psi.h" 28 #include "vidtv_ts.h" 29 30 #define CRC_SIZE_IN_BYTES 4 31 #define MAX_VERSION_NUM 32 32 #define INITIAL_CRC 0xffffffff 33 #define ISO_LANGUAGE_CODE_LEN 3 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 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 u16 vidtv_psi_get_sec_len(struct vidtv_psi_table_header *h) 96 { 97 u16 mask; 98 u16 ret; 99 100 mask = GENMASK(11, 0); 101 102 ret = be16_to_cpu(h->bitfield) & mask; 103 return ret; 104 } 105 106 u16 vidtv_psi_get_pat_program_pid(struct vidtv_psi_table_pat_program *p) 107 { 108 u16 mask; 109 u16 ret; 110 111 mask = GENMASK(12, 0); 112 113 ret = be16_to_cpu(p->bitfield) & mask; 114 return ret; 115 } 116 117 u16 vidtv_psi_pmt_stream_get_elem_pid(struct vidtv_psi_table_pmt_stream *s) 118 { 119 u16 mask; 120 u16 ret; 121 122 mask = GENMASK(12, 0); 123 124 ret = be16_to_cpu(s->bitfield) & mask; 125 return ret; 126 } 127 128 static void vidtv_psi_set_desc_loop_len(__be16 *bitfield, u16 new_len, 129 u8 desc_len_nbits) 130 { 131 __be16 new; 132 u16 mask; 133 134 mask = GENMASK(15, desc_len_nbits); 135 136 new = cpu_to_be16((be16_to_cpu(*bitfield) & mask) | new_len); 137 *bitfield = new; 138 } 139 140 static void vidtv_psi_set_sec_len(struct vidtv_psi_table_header *h, u16 new_len) 141 { 142 u16 old_len = vidtv_psi_get_sec_len(h); 143 __be16 new; 144 u16 mask; 145 146 mask = GENMASK(15, 13); 147 148 new = cpu_to_be16((be16_to_cpu(h->bitfield) & mask) | new_len); 149 150 if (old_len > MAX_SECTION_LEN) 151 pr_warn_ratelimited("section length: %d > %d, old len was %d\n", 152 new_len, 153 MAX_SECTION_LEN, 154 old_len); 155 156 h->bitfield = new; 157 } 158 159 /* 160 * Packetize PSI sections into TS packets: 161 * push a TS header (4bytes) every 184 bytes 162 * manage the continuity_counter 163 * add stuffing (i.e. padding bytes) after the CRC 164 */ 165 static u32 vidtv_psi_ts_psi_write_into(struct psi_write_args *args) 166 { 167 struct vidtv_mpeg_ts ts_header = { 168 .sync_byte = TS_SYNC_BYTE, 169 .bitfield = cpu_to_be16((args->new_psi_section << 14) | args->pid), 170 .scrambling = 0, 171 .payload = 1, 172 .adaptation_field = 0, /* no adaptation field */ 173 }; 174 u32 nbytes_past_boundary = (args->dest_offset % TS_PACKET_LEN); 175 bool aligned = (nbytes_past_boundary == 0); 176 u32 remaining_len = args->len; 177 u32 payload_write_len = 0; 178 u32 payload_offset = 0; 179 u32 nbytes = 0; 180 181 if (!args->crc && !args->is_crc) 182 pr_warn_ratelimited("Missing CRC for chunk\n"); 183 184 if (args->crc) 185 *args->crc = dvb_crc32(*args->crc, args->from, args->len); 186 187 if (args->new_psi_section && !aligned) { 188 pr_warn_ratelimited("Cannot write a new PSI section in a misaligned buffer\n"); 189 190 /* forcibly align and hope for the best */ 191 nbytes += vidtv_memset(args->dest_buf, 192 args->dest_offset + nbytes, 193 args->dest_buf_sz, 194 TS_FILL_BYTE, 195 TS_PACKET_LEN - nbytes_past_boundary); 196 } 197 198 while (remaining_len) { 199 nbytes_past_boundary = (args->dest_offset + nbytes) % TS_PACKET_LEN; 200 aligned = (nbytes_past_boundary == 0); 201 202 if (aligned) { 203 /* if at a packet boundary, write a new TS header */ 204 ts_header.continuity_counter = *args->continuity_counter; 205 206 nbytes += vidtv_memcpy(args->dest_buf, 207 args->dest_offset + nbytes, 208 args->dest_buf_sz, 209 &ts_header, 210 sizeof(ts_header)); 211 /* 212 * This will trigger a discontinuity if the buffer is full, 213 * effectively dropping the packet. 214 */ 215 vidtv_ts_inc_cc(args->continuity_counter); 216 } 217 218 /* write the pointer_field in the first byte of the payload */ 219 if (args->new_psi_section) 220 nbytes += vidtv_memset(args->dest_buf, 221 args->dest_offset + nbytes, 222 args->dest_buf_sz, 223 0x0, 224 1); 225 226 /* write as much of the payload as possible */ 227 nbytes_past_boundary = (args->dest_offset + nbytes) % TS_PACKET_LEN; 228 payload_write_len = min(TS_PACKET_LEN - nbytes_past_boundary, remaining_len); 229 230 nbytes += vidtv_memcpy(args->dest_buf, 231 args->dest_offset + nbytes, 232 args->dest_buf_sz, 233 args->from + payload_offset, 234 payload_write_len); 235 236 /* 'payload_write_len' written from a total of 'len' requested*/ 237 remaining_len -= payload_write_len; 238 payload_offset += payload_write_len; 239 } 240 241 /* 242 * fill the rest of the packet if there is any remaining space unused 243 */ 244 245 nbytes_past_boundary = (args->dest_offset + nbytes) % TS_PACKET_LEN; 246 247 if (args->is_crc) 248 nbytes += vidtv_memset(args->dest_buf, 249 args->dest_offset + nbytes, 250 args->dest_buf_sz, 251 TS_FILL_BYTE, 252 TS_PACKET_LEN - nbytes_past_boundary); 253 254 return nbytes; 255 } 256 257 static u32 table_section_crc32_write_into(struct crc32_write_args *args) 258 { 259 struct psi_write_args psi_args = { 260 .dest_buf = args->dest_buf, 261 .from = &args->crc, 262 .len = CRC_SIZE_IN_BYTES, 263 .dest_offset = args->dest_offset, 264 .pid = args->pid, 265 .new_psi_section = false, 266 .continuity_counter = args->continuity_counter, 267 .is_crc = true, 268 .dest_buf_sz = args->dest_buf_sz, 269 }; 270 271 /* the CRC is the last entry in the section */ 272 273 return vidtv_psi_ts_psi_write_into(&psi_args); 274 } 275 276 static void vidtv_psi_desc_chain(struct vidtv_psi_desc *head, struct vidtv_psi_desc *desc) 277 { 278 if (head) { 279 while (head->next) 280 head = head->next; 281 282 head->next = desc; 283 } 284 } 285 286 struct vidtv_psi_desc_service *vidtv_psi_service_desc_init(struct vidtv_psi_desc *head, 287 enum service_type service_type, 288 char *service_name, 289 char *provider_name) 290 { 291 struct vidtv_psi_desc_service *desc; 292 u32 service_name_len = service_name ? strlen(service_name) : 0; 293 u32 provider_name_len = provider_name ? strlen(provider_name) : 0; 294 295 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 296 if (!desc) 297 return NULL; 298 299 desc->type = SERVICE_DESCRIPTOR; 300 301 desc->length = sizeof_field(struct vidtv_psi_desc_service, service_type) 302 + sizeof_field(struct vidtv_psi_desc_service, provider_name_len) 303 + provider_name_len 304 + sizeof_field(struct vidtv_psi_desc_service, service_name_len) 305 + service_name_len; 306 307 desc->service_type = service_type; 308 309 desc->service_name_len = service_name_len; 310 311 if (service_name && service_name_len) 312 desc->service_name = kstrdup(service_name, GFP_KERNEL); 313 314 desc->provider_name_len = provider_name_len; 315 316 if (provider_name && provider_name_len) 317 desc->provider_name = kstrdup(provider_name, GFP_KERNEL); 318 319 vidtv_psi_desc_chain(head, (struct vidtv_psi_desc *)desc); 320 return desc; 321 } 322 323 struct vidtv_psi_desc_registration 324 *vidtv_psi_registration_desc_init(struct vidtv_psi_desc *head, 325 __be32 format_id, 326 u8 *additional_ident_info, 327 u32 additional_info_len) 328 { 329 struct vidtv_psi_desc_registration *desc; 330 331 desc = kzalloc(sizeof(*desc) + sizeof(format_id) + additional_info_len, GFP_KERNEL); 332 if (!desc) 333 return NULL; 334 335 desc->type = REGISTRATION_DESCRIPTOR; 336 337 desc->length = sizeof_field(struct vidtv_psi_desc_registration, format_id) 338 + additional_info_len; 339 340 desc->format_id = format_id; 341 342 if (additional_ident_info && additional_info_len) 343 memcpy(desc->additional_identification_info, 344 additional_ident_info, 345 additional_info_len); 346 347 vidtv_psi_desc_chain(head, (struct vidtv_psi_desc *)desc); 348 return desc; 349 } 350 351 struct vidtv_psi_desc_network_name 352 *vidtv_psi_network_name_desc_init(struct vidtv_psi_desc *head, char *network_name) 353 { 354 u32 network_name_len = network_name ? strlen(network_name) : 0; 355 struct vidtv_psi_desc_network_name *desc; 356 357 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 358 if (!desc) 359 return NULL; 360 361 desc->type = NETWORK_NAME_DESCRIPTOR; 362 363 desc->length = network_name_len; 364 365 if (network_name && network_name_len) 366 desc->network_name = kstrdup(network_name, GFP_KERNEL); 367 368 vidtv_psi_desc_chain(head, (struct vidtv_psi_desc *)desc); 369 return desc; 370 } 371 372 struct vidtv_psi_desc_service_list 373 *vidtv_psi_service_list_desc_init(struct vidtv_psi_desc *head, 374 struct vidtv_psi_desc_service_list_entry *entry) 375 { 376 struct vidtv_psi_desc_service_list_entry *curr_e = NULL; 377 struct vidtv_psi_desc_service_list_entry *head_e = NULL; 378 struct vidtv_psi_desc_service_list_entry *prev_e = NULL; 379 struct vidtv_psi_desc_service_list *desc; 380 u16 length = 0; 381 382 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 383 if (!desc) 384 return NULL; 385 386 desc->type = SERVICE_LIST_DESCRIPTOR; 387 388 while (entry) { 389 curr_e = kzalloc(sizeof(*curr_e), GFP_KERNEL); 390 if (!curr_e) { 391 while (head_e) { 392 curr_e = head_e; 393 head_e = head_e->next; 394 kfree(curr_e); 395 } 396 kfree(desc); 397 return NULL; 398 } 399 400 curr_e->service_id = entry->service_id; 401 curr_e->service_type = entry->service_type; 402 403 length += sizeof(struct vidtv_psi_desc_service_list_entry) - 404 sizeof(struct vidtv_psi_desc_service_list_entry *); 405 406 if (!head_e) 407 head_e = curr_e; 408 if (prev_e) 409 prev_e->next = curr_e; 410 411 prev_e = curr_e; 412 entry = entry->next; 413 } 414 415 desc->length = length; 416 desc->service_list = head_e; 417 418 vidtv_psi_desc_chain(head, (struct vidtv_psi_desc *)desc); 419 return desc; 420 } 421 422 struct vidtv_psi_desc_short_event 423 *vidtv_psi_short_event_desc_init(struct vidtv_psi_desc *head, 424 char *iso_language_code, 425 char *event_name, 426 char *text) 427 { 428 u32 iso_len = iso_language_code ? strlen(iso_language_code) : 0; 429 u32 event_name_len = event_name ? strlen(event_name) : 0; 430 struct vidtv_psi_desc_short_event *desc; 431 u32 text_len = text ? strlen(text) : 0; 432 433 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 434 if (!desc) 435 return NULL; 436 437 desc->type = SHORT_EVENT_DESCRIPTOR; 438 439 desc->length = ISO_LANGUAGE_CODE_LEN + 440 sizeof_field(struct vidtv_psi_desc_short_event, event_name_len) + 441 event_name_len + 442 sizeof_field(struct vidtv_psi_desc_short_event, text_len) + 443 text_len; 444 445 desc->event_name_len = event_name_len; 446 desc->text_len = text_len; 447 448 if (iso_len != ISO_LANGUAGE_CODE_LEN) 449 iso_language_code = "eng"; 450 451 desc->iso_language_code = kstrdup(iso_language_code, GFP_KERNEL); 452 453 if (event_name && event_name_len) 454 desc->event_name = kstrdup(event_name, GFP_KERNEL); 455 456 if (text && text_len) 457 desc->text = kstrdup(text, GFP_KERNEL); 458 459 vidtv_psi_desc_chain(head, (struct vidtv_psi_desc *)desc); 460 return desc; 461 } 462 463 struct vidtv_psi_desc *vidtv_psi_desc_clone(struct vidtv_psi_desc *desc) 464 { 465 struct vidtv_psi_desc_network_name *desc_network_name; 466 struct vidtv_psi_desc_service_list *desc_service_list; 467 struct vidtv_psi_desc_short_event *desc_short_event; 468 struct vidtv_psi_desc_service *service; 469 struct vidtv_psi_desc *head = NULL; 470 struct vidtv_psi_desc *prev = NULL; 471 struct vidtv_psi_desc *curr = NULL; 472 473 while (desc) { 474 switch (desc->type) { 475 case SERVICE_DESCRIPTOR: 476 service = (struct vidtv_psi_desc_service *)desc; 477 curr = (struct vidtv_psi_desc *) 478 vidtv_psi_service_desc_init(head, 479 service->service_type, 480 service->service_name, 481 service->provider_name); 482 break; 483 484 case NETWORK_NAME_DESCRIPTOR: 485 desc_network_name = (struct vidtv_psi_desc_network_name *)desc; 486 curr = (struct vidtv_psi_desc *) 487 vidtv_psi_network_name_desc_init(head, 488 desc_network_name->network_name); 489 break; 490 491 case SERVICE_LIST_DESCRIPTOR: 492 desc_service_list = (struct vidtv_psi_desc_service_list *)desc; 493 curr = (struct vidtv_psi_desc *) 494 vidtv_psi_service_list_desc_init(head, 495 desc_service_list->service_list); 496 break; 497 498 case SHORT_EVENT_DESCRIPTOR: 499 desc_short_event = (struct vidtv_psi_desc_short_event *)desc; 500 curr = (struct vidtv_psi_desc *) 501 vidtv_psi_short_event_desc_init(head, 502 desc_short_event->iso_language_code, 503 desc_short_event->event_name, 504 desc_short_event->text); 505 break; 506 507 case REGISTRATION_DESCRIPTOR: 508 default: 509 curr = kzalloc(sizeof(*desc) + desc->length, GFP_KERNEL); 510 if (!curr) 511 return NULL; 512 memcpy(curr, desc, sizeof(*desc) + desc->length); 513 } 514 515 if (!curr) 516 return NULL; 517 518 curr->next = NULL; 519 if (!head) 520 head = curr; 521 if (prev) 522 prev->next = curr; 523 524 prev = curr; 525 desc = desc->next; 526 } 527 528 return head; 529 } 530 531 void vidtv_psi_desc_destroy(struct vidtv_psi_desc *desc) 532 { 533 struct vidtv_psi_desc_service_list_entry *sl_entry_tmp = NULL; 534 struct vidtv_psi_desc_service_list_entry *sl_entry = NULL; 535 struct vidtv_psi_desc *curr = desc; 536 struct vidtv_psi_desc *tmp = NULL; 537 538 while (curr) { 539 tmp = curr; 540 curr = curr->next; 541 542 switch (tmp->type) { 543 case SERVICE_DESCRIPTOR: 544 kfree(((struct vidtv_psi_desc_service *)tmp)->provider_name); 545 kfree(((struct vidtv_psi_desc_service *)tmp)->service_name); 546 547 break; 548 case REGISTRATION_DESCRIPTOR: 549 /* nothing to do */ 550 break; 551 552 case NETWORK_NAME_DESCRIPTOR: 553 kfree(((struct vidtv_psi_desc_network_name *)tmp)->network_name); 554 break; 555 556 case SERVICE_LIST_DESCRIPTOR: 557 sl_entry = ((struct vidtv_psi_desc_service_list *)tmp)->service_list; 558 while (sl_entry) { 559 sl_entry_tmp = sl_entry; 560 sl_entry = sl_entry->next; 561 kfree(sl_entry_tmp); 562 } 563 break; 564 565 case SHORT_EVENT_DESCRIPTOR: 566 kfree(((struct vidtv_psi_desc_short_event *)tmp)->iso_language_code); 567 kfree(((struct vidtv_psi_desc_short_event *)tmp)->event_name); 568 kfree(((struct vidtv_psi_desc_short_event *)tmp)->text); 569 break; 570 571 default: 572 pr_warn_ratelimited("Possible leak: not handling descriptor type %d\n", 573 tmp->type); 574 break; 575 } 576 577 kfree(tmp); 578 } 579 } 580 581 static u16 582 vidtv_psi_desc_comp_loop_len(struct vidtv_psi_desc *desc) 583 { 584 u32 length = 0; 585 586 if (!desc) 587 return 0; 588 589 while (desc) { 590 length += sizeof_field(struct vidtv_psi_desc, type); 591 length += sizeof_field(struct vidtv_psi_desc, length); 592 length += desc->length; /* from 'length' field until the end of the descriptor */ 593 desc = desc->next; 594 } 595 596 return length; 597 } 598 599 void vidtv_psi_desc_assign(struct vidtv_psi_desc **to, 600 struct vidtv_psi_desc *desc) 601 { 602 if (desc == *to) 603 return; 604 605 if (*to) 606 vidtv_psi_desc_destroy(*to); 607 608 *to = desc; 609 } 610 611 void vidtv_pmt_desc_assign(struct vidtv_psi_table_pmt *pmt, 612 struct vidtv_psi_desc **to, 613 struct vidtv_psi_desc *desc) 614 { 615 vidtv_psi_desc_assign(to, desc); 616 vidtv_psi_pmt_table_update_sec_len(pmt); 617 618 if (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN) 619 vidtv_psi_desc_assign(to, NULL); 620 621 vidtv_psi_update_version_num(&pmt->header); 622 } 623 624 void vidtv_sdt_desc_assign(struct vidtv_psi_table_sdt *sdt, 625 struct vidtv_psi_desc **to, 626 struct vidtv_psi_desc *desc) 627 { 628 vidtv_psi_desc_assign(to, desc); 629 vidtv_psi_sdt_table_update_sec_len(sdt); 630 631 if (vidtv_psi_get_sec_len(&sdt->header) > MAX_SECTION_LEN) 632 vidtv_psi_desc_assign(to, NULL); 633 634 vidtv_psi_update_version_num(&sdt->header); 635 } 636 637 static u32 vidtv_psi_desc_write_into(struct desc_write_args *args) 638 { 639 struct psi_write_args psi_args = { 640 .dest_buf = args->dest_buf, 641 .from = &args->desc->type, 642 .pid = args->pid, 643 .new_psi_section = false, 644 .continuity_counter = args->continuity_counter, 645 .is_crc = false, 646 .dest_buf_sz = args->dest_buf_sz, 647 .crc = args->crc, 648 .len = sizeof_field(struct vidtv_psi_desc, type) + 649 sizeof_field(struct vidtv_psi_desc, length), 650 }; 651 struct vidtv_psi_desc_service_list_entry *serv_list_entry = NULL; 652 u32 nbytes = 0; 653 654 psi_args.dest_offset = args->dest_offset + nbytes; 655 656 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 657 658 switch (args->desc->type) { 659 case SERVICE_DESCRIPTOR: 660 psi_args.dest_offset = args->dest_offset + nbytes; 661 psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_type) + 662 sizeof_field(struct vidtv_psi_desc_service, provider_name_len); 663 psi_args.from = &((struct vidtv_psi_desc_service *)args->desc)->service_type; 664 665 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 666 667 psi_args.dest_offset = args->dest_offset + nbytes; 668 psi_args.len = ((struct vidtv_psi_desc_service *)args->desc)->provider_name_len; 669 psi_args.from = ((struct vidtv_psi_desc_service *)args->desc)->provider_name; 670 671 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 672 673 psi_args.dest_offset = args->dest_offset + nbytes; 674 psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_name_len); 675 psi_args.from = &((struct vidtv_psi_desc_service *)args->desc)->service_name_len; 676 677 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 678 679 psi_args.dest_offset = args->dest_offset + nbytes; 680 psi_args.len = ((struct vidtv_psi_desc_service *)args->desc)->service_name_len; 681 psi_args.from = ((struct vidtv_psi_desc_service *)args->desc)->service_name; 682 683 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 684 break; 685 686 case NETWORK_NAME_DESCRIPTOR: 687 psi_args.dest_offset = args->dest_offset + nbytes; 688 psi_args.len = args->desc->length; 689 psi_args.from = ((struct vidtv_psi_desc_network_name *)args->desc)->network_name; 690 691 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 692 break; 693 694 case SERVICE_LIST_DESCRIPTOR: 695 serv_list_entry = ((struct vidtv_psi_desc_service_list *)args->desc)->service_list; 696 while (serv_list_entry) { 697 psi_args.dest_offset = args->dest_offset + nbytes; 698 psi_args.len = sizeof(struct vidtv_psi_desc_service_list_entry) - 699 sizeof(struct vidtv_psi_desc_service_list_entry *); 700 psi_args.from = serv_list_entry; 701 702 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 703 704 serv_list_entry = serv_list_entry->next; 705 } 706 break; 707 708 case SHORT_EVENT_DESCRIPTOR: 709 psi_args.dest_offset = args->dest_offset + nbytes; 710 psi_args.len = ISO_LANGUAGE_CODE_LEN; 711 psi_args.from = ((struct vidtv_psi_desc_short_event *) 712 args->desc)->iso_language_code; 713 714 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 715 716 psi_args.dest_offset = args->dest_offset + nbytes; 717 psi_args.len = sizeof_field(struct vidtv_psi_desc_short_event, event_name_len); 718 psi_args.from = &((struct vidtv_psi_desc_short_event *) 719 args->desc)->event_name_len; 720 721 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 722 723 psi_args.dest_offset = args->dest_offset + nbytes; 724 psi_args.len = ((struct vidtv_psi_desc_short_event *)args->desc)->event_name_len; 725 psi_args.from = ((struct vidtv_psi_desc_short_event *)args->desc)->event_name; 726 727 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 728 729 psi_args.dest_offset = args->dest_offset + nbytes; 730 psi_args.len = sizeof_field(struct vidtv_psi_desc_short_event, text_len); 731 psi_args.from = &((struct vidtv_psi_desc_short_event *)args->desc)->text_len; 732 733 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 734 735 psi_args.dest_offset = args->dest_offset + nbytes; 736 psi_args.len = ((struct vidtv_psi_desc_short_event *)args->desc)->text_len; 737 psi_args.from = ((struct vidtv_psi_desc_short_event *)args->desc)->text; 738 739 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 740 741 break; 742 743 case REGISTRATION_DESCRIPTOR: 744 default: 745 psi_args.dest_offset = args->dest_offset + nbytes; 746 psi_args.len = args->desc->length; 747 psi_args.from = &args->desc->data; 748 749 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 750 break; 751 } 752 753 return nbytes; 754 } 755 756 static u32 757 vidtv_psi_table_header_write_into(struct header_write_args *args) 758 { 759 struct psi_write_args psi_args = { 760 .dest_buf = args->dest_buf, 761 .from = args->h, 762 .len = sizeof(struct vidtv_psi_table_header), 763 .dest_offset = args->dest_offset, 764 .pid = args->pid, 765 .new_psi_section = true, 766 .continuity_counter = args->continuity_counter, 767 .is_crc = false, 768 .dest_buf_sz = args->dest_buf_sz, 769 .crc = args->crc, 770 }; 771 772 return vidtv_psi_ts_psi_write_into(&psi_args); 773 } 774 775 void 776 vidtv_psi_pat_table_update_sec_len(struct vidtv_psi_table_pat *pat) 777 { 778 u16 length = 0; 779 u32 i; 780 781 /* see ISO/IEC 13818-1 : 2000 p.43 */ 782 783 /* from immediately after 'section_length' until 'last_section_number'*/ 784 length += PAT_LEN_UNTIL_LAST_SECTION_NUMBER; 785 786 /* do not count the pointer */ 787 for (i = 0; i < pat->num_pat; ++i) 788 length += sizeof(struct vidtv_psi_table_pat_program) - 789 sizeof(struct vidtv_psi_table_pat_program *); 790 791 length += CRC_SIZE_IN_BYTES; 792 793 vidtv_psi_set_sec_len(&pat->header, length); 794 } 795 796 void vidtv_psi_pmt_table_update_sec_len(struct vidtv_psi_table_pmt *pmt) 797 { 798 struct vidtv_psi_table_pmt_stream *s = pmt->stream; 799 u16 desc_loop_len; 800 u16 length = 0; 801 802 /* see ISO/IEC 13818-1 : 2000 p.46 */ 803 804 /* from immediately after 'section_length' until 'program_info_length'*/ 805 length += PMT_LEN_UNTIL_PROGRAM_INFO_LENGTH; 806 807 desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor); 808 vidtv_psi_set_desc_loop_len(&pmt->bitfield2, desc_loop_len, 10); 809 810 length += desc_loop_len; 811 812 while (s) { 813 /* skip both pointers at the end */ 814 length += sizeof(struct vidtv_psi_table_pmt_stream) - 815 sizeof(struct vidtv_psi_desc *) - 816 sizeof(struct vidtv_psi_table_pmt_stream *); 817 818 desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor); 819 vidtv_psi_set_desc_loop_len(&s->bitfield2, desc_loop_len, 10); 820 821 length += desc_loop_len; 822 823 s = s->next; 824 } 825 826 length += CRC_SIZE_IN_BYTES; 827 828 vidtv_psi_set_sec_len(&pmt->header, length); 829 } 830 831 void vidtv_psi_sdt_table_update_sec_len(struct vidtv_psi_table_sdt *sdt) 832 { 833 struct vidtv_psi_table_sdt_service *s = sdt->service; 834 u16 desc_loop_len; 835 u16 length = 0; 836 837 /* see ETSI EN 300 468 V 1.10.1 p.24 */ 838 839 /* 840 * from immediately after 'section_length' until 841 * 'reserved_for_future_use' 842 */ 843 length += SDT_LEN_UNTIL_RESERVED_FOR_FUTURE_USE; 844 845 while (s) { 846 /* skip both pointers at the end */ 847 length += sizeof(struct vidtv_psi_table_sdt_service) - 848 sizeof(struct vidtv_psi_desc *) - 849 sizeof(struct vidtv_psi_table_sdt_service *); 850 851 desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor); 852 vidtv_psi_set_desc_loop_len(&s->bitfield, desc_loop_len, 12); 853 854 length += desc_loop_len; 855 856 s = s->next; 857 } 858 859 length += CRC_SIZE_IN_BYTES; 860 vidtv_psi_set_sec_len(&sdt->header, length); 861 } 862 863 struct vidtv_psi_table_pat_program* 864 vidtv_psi_pat_program_init(struct vidtv_psi_table_pat_program *head, 865 u16 service_id, 866 u16 program_map_pid) 867 { 868 struct vidtv_psi_table_pat_program *program; 869 const u16 RESERVED = 0x07; 870 871 program = kzalloc(sizeof(*program), GFP_KERNEL); 872 if (!program) 873 return NULL; 874 875 program->service_id = cpu_to_be16(service_id); 876 877 /* pid for the PMT section in the TS */ 878 program->bitfield = cpu_to_be16((RESERVED << 13) | program_map_pid); 879 program->next = NULL; 880 881 if (head) { 882 while (head->next) 883 head = head->next; 884 885 head->next = program; 886 } 887 888 return program; 889 } 890 891 void 892 vidtv_psi_pat_program_destroy(struct vidtv_psi_table_pat_program *p) 893 { 894 struct vidtv_psi_table_pat_program *tmp = NULL; 895 struct vidtv_psi_table_pat_program *curr = p; 896 897 while (curr) { 898 tmp = curr; 899 curr = curr->next; 900 kfree(tmp); 901 } 902 } 903 904 /* This function transfers ownership of p to the table */ 905 void 906 vidtv_psi_pat_program_assign(struct vidtv_psi_table_pat *pat, 907 struct vidtv_psi_table_pat_program *p) 908 { 909 struct vidtv_psi_table_pat_program *program; 910 u16 program_count; 911 912 do { 913 program_count = 0; 914 program = p; 915 916 if (p == pat->program) 917 return; 918 919 while (program) { 920 ++program_count; 921 program = program->next; 922 } 923 924 pat->num_pat = program_count; 925 pat->program = p; 926 927 /* Recompute section length */ 928 vidtv_psi_pat_table_update_sec_len(pat); 929 930 p = NULL; 931 } while (vidtv_psi_get_sec_len(&pat->header) > MAX_SECTION_LEN); 932 933 vidtv_psi_update_version_num(&pat->header); 934 } 935 936 struct vidtv_psi_table_pat *vidtv_psi_pat_table_init(u16 transport_stream_id) 937 { 938 struct vidtv_psi_table_pat *pat; 939 const u16 SYNTAX = 0x1; 940 const u16 ZERO = 0x0; 941 const u16 ONES = 0x03; 942 943 pat = kzalloc(sizeof(*pat), GFP_KERNEL); 944 if (!pat) 945 return NULL; 946 947 pat->header.table_id = 0x0; 948 949 pat->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12)); 950 pat->header.id = cpu_to_be16(transport_stream_id); 951 pat->header.current_next = 0x1; 952 953 pat->header.version = 0x1f; 954 955 pat->header.one2 = 0x03; 956 pat->header.section_id = 0x0; 957 pat->header.last_section = 0x0; 958 959 vidtv_psi_pat_table_update_sec_len(pat); 960 961 return pat; 962 } 963 964 u32 vidtv_psi_pat_write_into(struct vidtv_psi_pat_write_args *args) 965 { 966 struct vidtv_psi_table_pat_program *p = args->pat->program; 967 struct header_write_args h_args = { 968 .dest_buf = args->buf, 969 .dest_offset = args->offset, 970 .pid = VIDTV_PAT_PID, 971 .h = &args->pat->header, 972 .continuity_counter = args->continuity_counter, 973 .dest_buf_sz = args->buf_sz, 974 }; 975 struct psi_write_args psi_args = { 976 .dest_buf = args->buf, 977 .pid = VIDTV_PAT_PID, 978 .new_psi_section = false, 979 .continuity_counter = args->continuity_counter, 980 .is_crc = false, 981 .dest_buf_sz = args->buf_sz, 982 }; 983 struct crc32_write_args c_args = { 984 .dest_buf = args->buf, 985 .pid = VIDTV_PAT_PID, 986 .dest_buf_sz = args->buf_sz, 987 }; 988 u32 crc = INITIAL_CRC; 989 u32 nbytes = 0; 990 991 vidtv_psi_pat_table_update_sec_len(args->pat); 992 993 h_args.crc = &crc; 994 995 nbytes += vidtv_psi_table_header_write_into(&h_args); 996 997 /* note that the field 'u16 programs' is not really part of the PAT */ 998 999 psi_args.crc = &crc; 1000 1001 while (p) { 1002 /* copy the PAT programs */ 1003 psi_args.from = p; 1004 /* skip the pointer */ 1005 psi_args.len = sizeof(*p) - 1006 sizeof(struct vidtv_psi_table_pat_program *); 1007 psi_args.dest_offset = args->offset + nbytes; 1008 psi_args.continuity_counter = args->continuity_counter; 1009 1010 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1011 1012 p = p->next; 1013 } 1014 1015 c_args.dest_offset = args->offset + nbytes; 1016 c_args.continuity_counter = args->continuity_counter; 1017 c_args.crc = cpu_to_be32(crc); 1018 1019 /* Write the CRC32 at the end */ 1020 nbytes += table_section_crc32_write_into(&c_args); 1021 1022 return nbytes; 1023 } 1024 1025 void 1026 vidtv_psi_pat_table_destroy(struct vidtv_psi_table_pat *p) 1027 { 1028 vidtv_psi_pat_program_destroy(p->program); 1029 kfree(p); 1030 } 1031 1032 struct vidtv_psi_table_pmt_stream* 1033 vidtv_psi_pmt_stream_init(struct vidtv_psi_table_pmt_stream *head, 1034 enum vidtv_psi_stream_types stream_type, 1035 u16 es_pid) 1036 { 1037 struct vidtv_psi_table_pmt_stream *stream; 1038 const u16 RESERVED1 = 0x07; 1039 const u16 RESERVED2 = 0x0f; 1040 const u16 ZERO = 0x0; 1041 u16 desc_loop_len; 1042 1043 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 1044 if (!stream) 1045 return NULL; 1046 1047 stream->type = stream_type; 1048 1049 stream->bitfield = cpu_to_be16((RESERVED1 << 13) | es_pid); 1050 1051 desc_loop_len = vidtv_psi_desc_comp_loop_len(stream->descriptor); 1052 1053 stream->bitfield2 = cpu_to_be16((RESERVED2 << 12) | 1054 (ZERO << 10) | 1055 desc_loop_len); 1056 stream->next = NULL; 1057 1058 if (head) { 1059 while (head->next) 1060 head = head->next; 1061 1062 head->next = stream; 1063 } 1064 1065 return stream; 1066 } 1067 1068 void vidtv_psi_pmt_stream_destroy(struct vidtv_psi_table_pmt_stream *s) 1069 { 1070 struct vidtv_psi_table_pmt_stream *tmp_stream = NULL; 1071 struct vidtv_psi_table_pmt_stream *curr_stream = s; 1072 1073 while (curr_stream) { 1074 tmp_stream = curr_stream; 1075 curr_stream = curr_stream->next; 1076 vidtv_psi_desc_destroy(tmp_stream->descriptor); 1077 kfree(tmp_stream); 1078 } 1079 } 1080 1081 void vidtv_psi_pmt_stream_assign(struct vidtv_psi_table_pmt *pmt, 1082 struct vidtv_psi_table_pmt_stream *s) 1083 { 1084 do { 1085 /* This function transfers ownership of s to the table */ 1086 if (s == pmt->stream) 1087 return; 1088 1089 pmt->stream = s; 1090 vidtv_psi_pmt_table_update_sec_len(pmt); 1091 1092 s = NULL; 1093 } while (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN); 1094 1095 vidtv_psi_update_version_num(&pmt->header); 1096 } 1097 1098 u16 vidtv_psi_pmt_get_pid(struct vidtv_psi_table_pmt *section, 1099 struct vidtv_psi_table_pat *pat) 1100 { 1101 struct vidtv_psi_table_pat_program *program = pat->program; 1102 1103 /* 1104 * service_id is the same as program_number in the 1105 * corresponding program_map_section 1106 * see ETSI EN 300 468 v1.15.1 p. 24 1107 */ 1108 while (program) { 1109 if (program->service_id == section->header.id) 1110 return vidtv_psi_get_pat_program_pid(program); 1111 1112 program = program->next; 1113 } 1114 1115 return TS_LAST_VALID_PID + 1; /* not found */ 1116 } 1117 1118 struct vidtv_psi_table_pmt *vidtv_psi_pmt_table_init(u16 program_number, 1119 u16 pcr_pid) 1120 { 1121 struct vidtv_psi_table_pmt *pmt; 1122 const u16 RESERVED1 = 0x07; 1123 const u16 RESERVED2 = 0x0f; 1124 const u16 SYNTAX = 0x1; 1125 const u16 ONES = 0x03; 1126 const u16 ZERO = 0x0; 1127 u16 desc_loop_len; 1128 1129 pmt = kzalloc(sizeof(*pmt), GFP_KERNEL); 1130 if (!pmt) 1131 return NULL; 1132 1133 if (!pcr_pid) 1134 pcr_pid = 0x1fff; 1135 1136 pmt->header.table_id = 0x2; 1137 1138 pmt->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12)); 1139 1140 pmt->header.id = cpu_to_be16(program_number); 1141 pmt->header.current_next = 0x1; 1142 1143 pmt->header.version = 0x1f; 1144 1145 pmt->header.one2 = ONES; 1146 pmt->header.section_id = 0; 1147 pmt->header.last_section = 0; 1148 1149 pmt->bitfield = cpu_to_be16((RESERVED1 << 13) | pcr_pid); 1150 1151 desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor); 1152 1153 pmt->bitfield2 = cpu_to_be16((RESERVED2 << 12) | 1154 (ZERO << 10) | 1155 desc_loop_len); 1156 1157 vidtv_psi_pmt_table_update_sec_len(pmt); 1158 1159 return pmt; 1160 } 1161 1162 u32 vidtv_psi_pmt_write_into(struct vidtv_psi_pmt_write_args *args) 1163 { 1164 struct vidtv_psi_desc *table_descriptor = args->pmt->descriptor; 1165 struct vidtv_psi_table_pmt_stream *stream = args->pmt->stream; 1166 struct vidtv_psi_desc *stream_descriptor; 1167 struct header_write_args h_args = { 1168 .dest_buf = args->buf, 1169 .dest_offset = args->offset, 1170 .h = &args->pmt->header, 1171 .pid = args->pid, 1172 .continuity_counter = args->continuity_counter, 1173 .dest_buf_sz = args->buf_sz, 1174 }; 1175 struct psi_write_args psi_args = { 1176 .dest_buf = args->buf, 1177 .from = &args->pmt->bitfield, 1178 .len = sizeof_field(struct vidtv_psi_table_pmt, bitfield) + 1179 sizeof_field(struct vidtv_psi_table_pmt, bitfield2), 1180 .pid = args->pid, 1181 .new_psi_section = false, 1182 .is_crc = false, 1183 .dest_buf_sz = args->buf_sz, 1184 }; 1185 struct desc_write_args d_args = { 1186 .dest_buf = args->buf, 1187 .desc = table_descriptor, 1188 .pid = args->pid, 1189 .dest_buf_sz = args->buf_sz, 1190 }; 1191 struct crc32_write_args c_args = { 1192 .dest_buf = args->buf, 1193 .pid = args->pid, 1194 .dest_buf_sz = args->buf_sz, 1195 }; 1196 u32 crc = INITIAL_CRC; 1197 u32 nbytes = 0; 1198 1199 vidtv_psi_pmt_table_update_sec_len(args->pmt); 1200 1201 h_args.crc = &crc; 1202 1203 nbytes += vidtv_psi_table_header_write_into(&h_args); 1204 1205 /* write the two bitfields */ 1206 psi_args.dest_offset = args->offset + nbytes; 1207 psi_args.continuity_counter = args->continuity_counter; 1208 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1209 1210 while (table_descriptor) { 1211 /* write the descriptors, if any */ 1212 d_args.dest_offset = args->offset + nbytes; 1213 d_args.continuity_counter = args->continuity_counter; 1214 d_args.crc = &crc; 1215 1216 nbytes += vidtv_psi_desc_write_into(&d_args); 1217 1218 table_descriptor = table_descriptor->next; 1219 } 1220 1221 psi_args.len += sizeof_field(struct vidtv_psi_table_pmt_stream, type); 1222 while (stream) { 1223 /* write the streams, if any */ 1224 psi_args.from = stream; 1225 psi_args.dest_offset = args->offset + nbytes; 1226 psi_args.continuity_counter = args->continuity_counter; 1227 1228 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1229 1230 stream_descriptor = stream->descriptor; 1231 1232 while (stream_descriptor) { 1233 /* write the stream descriptors, if any */ 1234 d_args.dest_offset = args->offset + nbytes; 1235 d_args.desc = stream_descriptor; 1236 d_args.continuity_counter = args->continuity_counter; 1237 d_args.crc = &crc; 1238 1239 nbytes += vidtv_psi_desc_write_into(&d_args); 1240 1241 stream_descriptor = stream_descriptor->next; 1242 } 1243 1244 stream = stream->next; 1245 } 1246 1247 c_args.dest_offset = args->offset + nbytes; 1248 c_args.crc = cpu_to_be32(crc); 1249 c_args.continuity_counter = args->continuity_counter; 1250 1251 /* Write the CRC32 at the end */ 1252 nbytes += table_section_crc32_write_into(&c_args); 1253 1254 return nbytes; 1255 } 1256 1257 void vidtv_psi_pmt_table_destroy(struct vidtv_psi_table_pmt *pmt) 1258 { 1259 vidtv_psi_desc_destroy(pmt->descriptor); 1260 vidtv_psi_pmt_stream_destroy(pmt->stream); 1261 kfree(pmt); 1262 } 1263 1264 struct vidtv_psi_table_sdt *vidtv_psi_sdt_table_init(u16 network_id, 1265 u16 transport_stream_id) 1266 { 1267 struct vidtv_psi_table_sdt *sdt; 1268 const u16 RESERVED = 0xff; 1269 const u16 SYNTAX = 0x1; 1270 const u16 ONES = 0x03; 1271 const u16 ONE = 0x1; 1272 1273 sdt = kzalloc(sizeof(*sdt), GFP_KERNEL); 1274 if (!sdt) 1275 return NULL; 1276 1277 sdt->header.table_id = 0x42; 1278 sdt->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ONE << 14) | (ONES << 12)); 1279 1280 /* 1281 * This is a 16-bit field which serves as a label for identification 1282 * of the TS, about which the SDT informs, from any other multiplex 1283 * within the delivery system. 1284 */ 1285 sdt->header.id = cpu_to_be16(transport_stream_id); 1286 sdt->header.current_next = ONE; 1287 1288 sdt->header.version = 0x1f; 1289 1290 sdt->header.one2 = ONES; 1291 sdt->header.section_id = 0; 1292 sdt->header.last_section = 0; 1293 1294 /* 1295 * FIXME: The network_id range from 0xFF01 to 0xFFFF is used to 1296 * indicate temporary private use. For now, let's use the first 1297 * value. 1298 * This can be changed to something more useful, when support for 1299 * NIT gets added 1300 */ 1301 sdt->network_id = cpu_to_be16(network_id); 1302 sdt->reserved = RESERVED; 1303 1304 vidtv_psi_sdt_table_update_sec_len(sdt); 1305 1306 return sdt; 1307 } 1308 1309 u32 vidtv_psi_sdt_write_into(struct vidtv_psi_sdt_write_args *args) 1310 { 1311 struct header_write_args h_args = { 1312 .dest_buf = args->buf, 1313 .dest_offset = args->offset, 1314 .h = &args->sdt->header, 1315 .pid = VIDTV_SDT_PID, 1316 .dest_buf_sz = args->buf_sz, 1317 }; 1318 struct psi_write_args psi_args = { 1319 .dest_buf = args->buf, 1320 .len = sizeof_field(struct vidtv_psi_table_sdt, network_id) + 1321 sizeof_field(struct vidtv_psi_table_sdt, reserved), 1322 .pid = VIDTV_SDT_PID, 1323 .new_psi_section = false, 1324 .is_crc = false, 1325 .dest_buf_sz = args->buf_sz, 1326 }; 1327 struct desc_write_args d_args = { 1328 .dest_buf = args->buf, 1329 .pid = VIDTV_SDT_PID, 1330 .dest_buf_sz = args->buf_sz, 1331 }; 1332 struct crc32_write_args c_args = { 1333 .dest_buf = args->buf, 1334 .pid = VIDTV_SDT_PID, 1335 .dest_buf_sz = args->buf_sz, 1336 }; 1337 struct vidtv_psi_table_sdt_service *service = args->sdt->service; 1338 struct vidtv_psi_desc *service_desc; 1339 u32 nbytes = 0; 1340 u32 crc = INITIAL_CRC; 1341 1342 /* see ETSI EN 300 468 v1.15.1 p. 11 */ 1343 1344 vidtv_psi_sdt_table_update_sec_len(args->sdt); 1345 1346 h_args.continuity_counter = args->continuity_counter; 1347 h_args.crc = &crc; 1348 1349 nbytes += vidtv_psi_table_header_write_into(&h_args); 1350 1351 psi_args.from = &args->sdt->network_id; 1352 psi_args.dest_offset = args->offset + nbytes; 1353 psi_args.continuity_counter = args->continuity_counter; 1354 psi_args.crc = &crc; 1355 1356 /* copy u16 network_id + u8 reserved)*/ 1357 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1358 1359 /* skip both pointers at the end */ 1360 psi_args.len = sizeof(struct vidtv_psi_table_sdt_service) - 1361 sizeof(struct vidtv_psi_desc *) - 1362 sizeof(struct vidtv_psi_table_sdt_service *); 1363 1364 while (service) { 1365 /* copy the services, if any */ 1366 psi_args.from = service; 1367 psi_args.dest_offset = args->offset + nbytes; 1368 psi_args.continuity_counter = args->continuity_counter; 1369 1370 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1371 1372 service_desc = service->descriptor; 1373 1374 while (service_desc) { 1375 /* copy the service descriptors, if any */ 1376 d_args.dest_offset = args->offset + nbytes; 1377 d_args.desc = service_desc; 1378 d_args.continuity_counter = args->continuity_counter; 1379 d_args.crc = &crc; 1380 1381 nbytes += vidtv_psi_desc_write_into(&d_args); 1382 1383 service_desc = service_desc->next; 1384 } 1385 1386 service = service->next; 1387 } 1388 1389 c_args.dest_offset = args->offset + nbytes; 1390 c_args.crc = cpu_to_be32(crc); 1391 c_args.continuity_counter = args->continuity_counter; 1392 1393 /* Write the CRC at the end */ 1394 nbytes += table_section_crc32_write_into(&c_args); 1395 1396 return nbytes; 1397 } 1398 1399 void vidtv_psi_sdt_table_destroy(struct vidtv_psi_table_sdt *sdt) 1400 { 1401 vidtv_psi_sdt_service_destroy(sdt->service); 1402 kfree(sdt); 1403 } 1404 1405 struct vidtv_psi_table_sdt_service 1406 *vidtv_psi_sdt_service_init(struct vidtv_psi_table_sdt_service *head, 1407 u16 service_id, 1408 bool eit_schedule, 1409 bool eit_present_following) 1410 { 1411 struct vidtv_psi_table_sdt_service *service; 1412 1413 service = kzalloc(sizeof(*service), GFP_KERNEL); 1414 if (!service) 1415 return NULL; 1416 1417 /* 1418 * ETSI 300 468: this is a 16bit field which serves as a label to 1419 * identify this service from any other service within the TS. 1420 * The service id is the same as the program number in the 1421 * corresponding program_map_section 1422 */ 1423 service->service_id = cpu_to_be16(service_id); 1424 service->EIT_schedule = eit_schedule; 1425 service->EIT_present_following = eit_present_following; 1426 service->reserved = 0x3f; 1427 1428 service->bitfield = cpu_to_be16(RUNNING << 13); 1429 1430 if (head) { 1431 while (head->next) 1432 head = head->next; 1433 1434 head->next = service; 1435 } 1436 1437 return service; 1438 } 1439 1440 void 1441 vidtv_psi_sdt_service_destroy(struct vidtv_psi_table_sdt_service *service) 1442 { 1443 struct vidtv_psi_table_sdt_service *curr = service; 1444 struct vidtv_psi_table_sdt_service *tmp = NULL; 1445 1446 while (curr) { 1447 tmp = curr; 1448 curr = curr->next; 1449 vidtv_psi_desc_destroy(tmp->descriptor); 1450 kfree(tmp); 1451 } 1452 } 1453 1454 void 1455 vidtv_psi_sdt_service_assign(struct vidtv_psi_table_sdt *sdt, 1456 struct vidtv_psi_table_sdt_service *service) 1457 { 1458 do { 1459 if (service == sdt->service) 1460 return; 1461 1462 sdt->service = service; 1463 1464 /* recompute section length */ 1465 vidtv_psi_sdt_table_update_sec_len(sdt); 1466 1467 service = NULL; 1468 } while (vidtv_psi_get_sec_len(&sdt->header) > MAX_SECTION_LEN); 1469 1470 vidtv_psi_update_version_num(&sdt->header); 1471 } 1472 1473 /* 1474 * PMTs contain information about programs. For each program, 1475 * there is one PMT section. This function will create a section 1476 * for each program found in the PAT 1477 */ 1478 struct vidtv_psi_table_pmt** 1479 vidtv_psi_pmt_create_sec_for_each_pat_entry(struct vidtv_psi_table_pat *pat, 1480 u16 pcr_pid) 1481 1482 { 1483 struct vidtv_psi_table_pat_program *program; 1484 struct vidtv_psi_table_pmt **pmt_secs; 1485 u32 i = 0, num_pmt = 0; 1486 1487 /* 1488 * The number of PMT entries is the number of PAT entries 1489 * that contain service_id. That exclude special tables, like NIT 1490 */ 1491 program = pat->program; 1492 while (program) { 1493 if (program->service_id) 1494 num_pmt++; 1495 program = program->next; 1496 } 1497 1498 pmt_secs = kcalloc(num_pmt, 1499 sizeof(struct vidtv_psi_table_pmt *), 1500 GFP_KERNEL); 1501 if (!pmt_secs) 1502 return NULL; 1503 1504 for (program = pat->program; program; program = program->next) { 1505 if (!program->service_id) 1506 continue; 1507 pmt_secs[i] = vidtv_psi_pmt_table_init(be16_to_cpu(program->service_id), 1508 pcr_pid); 1509 1510 if (!pmt_secs[i]) { 1511 while (i > 0) { 1512 i--; 1513 vidtv_psi_pmt_table_destroy(pmt_secs[i]); 1514 } 1515 return NULL; 1516 } 1517 i++; 1518 } 1519 pat->num_pmt = num_pmt; 1520 1521 return pmt_secs; 1522 } 1523 1524 /* find the PMT section associated with 'program_num' */ 1525 struct vidtv_psi_table_pmt 1526 *vidtv_psi_find_pmt_sec(struct vidtv_psi_table_pmt **pmt_sections, 1527 u16 nsections, 1528 u16 program_num) 1529 { 1530 struct vidtv_psi_table_pmt *sec = NULL; 1531 u32 i; 1532 1533 for (i = 0; i < nsections; ++i) { 1534 sec = pmt_sections[i]; 1535 if (be16_to_cpu(sec->header.id) == program_num) 1536 return sec; 1537 } 1538 1539 return NULL; /* not found */ 1540 } 1541 1542 static void vidtv_psi_nit_table_update_sec_len(struct vidtv_psi_table_nit *nit) 1543 { 1544 u16 length = 0; 1545 struct vidtv_psi_table_transport *t = nit->transport; 1546 u16 desc_loop_len; 1547 u16 transport_loop_len = 0; 1548 1549 /* 1550 * from immediately after 'section_length' until 1551 * 'network_descriptor_length' 1552 */ 1553 length += NIT_LEN_UNTIL_NETWORK_DESCRIPTOR_LEN; 1554 1555 desc_loop_len = vidtv_psi_desc_comp_loop_len(nit->descriptor); 1556 vidtv_psi_set_desc_loop_len(&nit->bitfield, desc_loop_len, 12); 1557 1558 length += desc_loop_len; 1559 1560 length += sizeof_field(struct vidtv_psi_table_nit, bitfield2); 1561 1562 while (t) { 1563 /* skip both pointers at the end */ 1564 transport_loop_len += sizeof(struct vidtv_psi_table_transport) - 1565 sizeof(struct vidtv_psi_desc *) - 1566 sizeof(struct vidtv_psi_table_transport *); 1567 1568 length += transport_loop_len; 1569 1570 desc_loop_len = vidtv_psi_desc_comp_loop_len(t->descriptor); 1571 vidtv_psi_set_desc_loop_len(&t->bitfield, desc_loop_len, 12); 1572 1573 length += desc_loop_len; 1574 1575 t = t->next; 1576 } 1577 1578 // Actually sets the transport stream loop len, maybe rename this function later 1579 vidtv_psi_set_desc_loop_len(&nit->bitfield2, transport_loop_len, 12); 1580 length += CRC_SIZE_IN_BYTES; 1581 1582 vidtv_psi_set_sec_len(&nit->header, length); 1583 } 1584 1585 struct vidtv_psi_table_nit 1586 *vidtv_psi_nit_table_init(u16 network_id, 1587 u16 transport_stream_id, 1588 char *network_name, 1589 struct vidtv_psi_desc_service_list_entry *service_list) 1590 { 1591 struct vidtv_psi_table_transport *transport; 1592 struct vidtv_psi_table_nit *nit; 1593 const u16 SYNTAX = 0x1; 1594 const u16 ONES = 0x03; 1595 const u16 ONE = 0x1; 1596 1597 nit = kzalloc(sizeof(*nit), GFP_KERNEL); 1598 if (!nit) 1599 return NULL; 1600 1601 transport = kzalloc(sizeof(*transport), GFP_KERNEL); 1602 if (!transport) 1603 goto free_nit; 1604 1605 nit->header.table_id = 0x40; // ACTUAL_NETWORK 1606 1607 nit->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ONE << 14) | (ONES << 12)); 1608 1609 nit->header.id = cpu_to_be16(network_id); 1610 nit->header.current_next = ONE; 1611 1612 nit->header.version = 0x1f; 1613 1614 nit->header.one2 = ONES; 1615 nit->header.section_id = 0; 1616 nit->header.last_section = 0; 1617 1618 nit->bitfield = cpu_to_be16(0xf); 1619 nit->bitfield2 = cpu_to_be16(0xf); 1620 1621 nit->descriptor = (struct vidtv_psi_desc *) 1622 vidtv_psi_network_name_desc_init(NULL, network_name); 1623 if (!nit->descriptor) 1624 goto free_transport; 1625 1626 transport->transport_id = cpu_to_be16(transport_stream_id); 1627 transport->network_id = cpu_to_be16(network_id); 1628 transport->bitfield = cpu_to_be16(0xf); 1629 transport->descriptor = (struct vidtv_psi_desc *) 1630 vidtv_psi_service_list_desc_init(NULL, service_list); 1631 if (!transport->descriptor) 1632 goto free_nit_desc; 1633 1634 nit->transport = transport; 1635 1636 vidtv_psi_nit_table_update_sec_len(nit); 1637 1638 return nit; 1639 1640 free_nit_desc: 1641 vidtv_psi_desc_destroy((struct vidtv_psi_desc *)nit->descriptor); 1642 1643 free_transport: 1644 kfree(transport); 1645 free_nit: 1646 kfree(nit); 1647 return NULL; 1648 } 1649 1650 u32 vidtv_psi_nit_write_into(struct vidtv_psi_nit_write_args *args) 1651 { 1652 struct header_write_args h_args = { 1653 .dest_buf = args->buf, 1654 .dest_offset = args->offset, 1655 .h = &args->nit->header, 1656 .pid = VIDTV_NIT_PID, 1657 .dest_buf_sz = args->buf_sz, 1658 }; 1659 struct psi_write_args psi_args = { 1660 .dest_buf = args->buf, 1661 .from = &args->nit->bitfield, 1662 .len = sizeof_field(struct vidtv_psi_table_nit, bitfield), 1663 .pid = VIDTV_NIT_PID, 1664 .new_psi_section = false, 1665 .is_crc = false, 1666 .dest_buf_sz = args->buf_sz, 1667 }; 1668 struct desc_write_args d_args = { 1669 .dest_buf = args->buf, 1670 .pid = VIDTV_NIT_PID, 1671 .dest_buf_sz = args->buf_sz, 1672 }; 1673 struct crc32_write_args c_args = { 1674 .dest_buf = args->buf, 1675 .pid = VIDTV_NIT_PID, 1676 .dest_buf_sz = args->buf_sz, 1677 }; 1678 struct vidtv_psi_desc *table_descriptor = args->nit->descriptor; 1679 struct vidtv_psi_table_transport *transport = args->nit->transport; 1680 struct vidtv_psi_desc *transport_descriptor; 1681 u32 crc = INITIAL_CRC; 1682 u32 nbytes = 0; 1683 1684 vidtv_psi_nit_table_update_sec_len(args->nit); 1685 1686 h_args.continuity_counter = args->continuity_counter; 1687 h_args.crc = &crc; 1688 1689 nbytes += vidtv_psi_table_header_write_into(&h_args); 1690 1691 /* write the bitfield */ 1692 1693 psi_args.dest_offset = args->offset + nbytes; 1694 psi_args.continuity_counter = args->continuity_counter; 1695 psi_args.crc = &crc; 1696 1697 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1698 1699 while (table_descriptor) { 1700 /* write the descriptors, if any */ 1701 d_args.dest_offset = args->offset + nbytes; 1702 d_args.desc = table_descriptor; 1703 d_args.continuity_counter = args->continuity_counter; 1704 d_args.crc = &crc; 1705 1706 nbytes += vidtv_psi_desc_write_into(&d_args); 1707 1708 table_descriptor = table_descriptor->next; 1709 } 1710 1711 /* write the second bitfield */ 1712 psi_args.from = &args->nit->bitfield2; 1713 psi_args.len = sizeof_field(struct vidtv_psi_table_nit, bitfield2); 1714 psi_args.dest_offset = args->offset + nbytes; 1715 1716 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1717 1718 psi_args.len = sizeof_field(struct vidtv_psi_table_transport, transport_id) + 1719 sizeof_field(struct vidtv_psi_table_transport, network_id) + 1720 sizeof_field(struct vidtv_psi_table_transport, bitfield); 1721 while (transport) { 1722 /* write the transport sections, if any */ 1723 psi_args.from = transport; 1724 psi_args.dest_offset = args->offset + nbytes; 1725 1726 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1727 1728 transport_descriptor = transport->descriptor; 1729 1730 while (transport_descriptor) { 1731 /* write the transport descriptors, if any */ 1732 d_args.dest_offset = args->offset + nbytes; 1733 d_args.desc = transport_descriptor; 1734 d_args.continuity_counter = args->continuity_counter; 1735 d_args.crc = &crc; 1736 1737 nbytes += vidtv_psi_desc_write_into(&d_args); 1738 1739 transport_descriptor = transport_descriptor->next; 1740 } 1741 1742 transport = transport->next; 1743 } 1744 1745 c_args.dest_offset = args->offset + nbytes; 1746 c_args.crc = cpu_to_be32(crc); 1747 c_args.continuity_counter = args->continuity_counter; 1748 1749 /* Write the CRC32 at the end */ 1750 nbytes += table_section_crc32_write_into(&c_args); 1751 1752 return nbytes; 1753 } 1754 1755 static void vidtv_psi_transport_destroy(struct vidtv_psi_table_transport *t) 1756 { 1757 struct vidtv_psi_table_transport *tmp_t = NULL; 1758 struct vidtv_psi_table_transport *curr_t = t; 1759 1760 while (curr_t) { 1761 tmp_t = curr_t; 1762 curr_t = curr_t->next; 1763 vidtv_psi_desc_destroy(tmp_t->descriptor); 1764 kfree(tmp_t); 1765 } 1766 } 1767 1768 void vidtv_psi_nit_table_destroy(struct vidtv_psi_table_nit *nit) 1769 { 1770 vidtv_psi_desc_destroy(nit->descriptor); 1771 vidtv_psi_transport_destroy(nit->transport); 1772 kfree(nit); 1773 } 1774 1775 void vidtv_psi_eit_table_update_sec_len(struct vidtv_psi_table_eit *eit) 1776 { 1777 struct vidtv_psi_table_eit_event *e = eit->event; 1778 u16 desc_loop_len; 1779 u16 length = 0; 1780 1781 /* 1782 * from immediately after 'section_length' until 1783 * 'last_table_id' 1784 */ 1785 length += EIT_LEN_UNTIL_LAST_TABLE_ID; 1786 1787 while (e) { 1788 /* skip both pointers at the end */ 1789 length += sizeof(struct vidtv_psi_table_eit_event) - 1790 sizeof(struct vidtv_psi_desc *) - 1791 sizeof(struct vidtv_psi_table_eit_event *); 1792 1793 desc_loop_len = vidtv_psi_desc_comp_loop_len(e->descriptor); 1794 vidtv_psi_set_desc_loop_len(&e->bitfield, desc_loop_len, 12); 1795 1796 length += desc_loop_len; 1797 1798 e = e->next; 1799 } 1800 1801 length += CRC_SIZE_IN_BYTES; 1802 1803 vidtv_psi_set_sec_len(&eit->header, length); 1804 } 1805 1806 void vidtv_psi_eit_event_assign(struct vidtv_psi_table_eit *eit, 1807 struct vidtv_psi_table_eit_event *e) 1808 { 1809 do { 1810 if (e == eit->event) 1811 return; 1812 1813 eit->event = e; 1814 vidtv_psi_eit_table_update_sec_len(eit); 1815 1816 e = NULL; 1817 } while (vidtv_psi_get_sec_len(&eit->header) > EIT_MAX_SECTION_LEN); 1818 1819 vidtv_psi_update_version_num(&eit->header); 1820 } 1821 1822 struct vidtv_psi_table_eit 1823 *vidtv_psi_eit_table_init(u16 network_id, 1824 u16 transport_stream_id, 1825 __be16 service_id) 1826 { 1827 struct vidtv_psi_table_eit *eit; 1828 const u16 SYNTAX = 0x1; 1829 const u16 ONE = 0x1; 1830 const u16 ONES = 0x03; 1831 1832 eit = kzalloc(sizeof(*eit), GFP_KERNEL); 1833 if (!eit) 1834 return NULL; 1835 1836 eit->header.table_id = 0x4e; //actual_transport_stream: present/following 1837 1838 eit->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ONE << 14) | (ONES << 12)); 1839 1840 eit->header.id = service_id; 1841 eit->header.current_next = ONE; 1842 1843 eit->header.version = 0x1f; 1844 1845 eit->header.one2 = ONES; 1846 eit->header.section_id = 0; 1847 eit->header.last_section = 0; 1848 1849 eit->transport_id = cpu_to_be16(transport_stream_id); 1850 eit->network_id = cpu_to_be16(network_id); 1851 1852 eit->last_segment = eit->header.last_section; /* not implemented */ 1853 eit->last_table_id = eit->header.table_id; /* not implemented */ 1854 1855 vidtv_psi_eit_table_update_sec_len(eit); 1856 1857 return eit; 1858 } 1859 1860 u32 vidtv_psi_eit_write_into(struct vidtv_psi_eit_write_args *args) 1861 { 1862 struct header_write_args h_args = { 1863 .dest_buf = args->buf, 1864 .dest_offset = args->offset, 1865 .h = &args->eit->header, 1866 .pid = VIDTV_EIT_PID, 1867 .dest_buf_sz = args->buf_sz, 1868 }; 1869 struct psi_write_args psi_args = { 1870 .dest_buf = args->buf, 1871 .len = sizeof_field(struct vidtv_psi_table_eit, transport_id) + 1872 sizeof_field(struct vidtv_psi_table_eit, network_id) + 1873 sizeof_field(struct vidtv_psi_table_eit, last_segment) + 1874 sizeof_field(struct vidtv_psi_table_eit, last_table_id), 1875 .pid = VIDTV_EIT_PID, 1876 .new_psi_section = false, 1877 .is_crc = false, 1878 .dest_buf_sz = args->buf_sz, 1879 }; 1880 struct desc_write_args d_args = { 1881 .dest_buf = args->buf, 1882 .pid = VIDTV_EIT_PID, 1883 .dest_buf_sz = args->buf_sz, 1884 }; 1885 struct crc32_write_args c_args = { 1886 .dest_buf = args->buf, 1887 .pid = VIDTV_EIT_PID, 1888 .dest_buf_sz = args->buf_sz, 1889 }; 1890 struct vidtv_psi_table_eit_event *event = args->eit->event; 1891 struct vidtv_psi_desc *event_descriptor; 1892 u32 crc = INITIAL_CRC; 1893 u32 nbytes = 0; 1894 1895 vidtv_psi_eit_table_update_sec_len(args->eit); 1896 1897 h_args.continuity_counter = args->continuity_counter; 1898 h_args.crc = &crc; 1899 1900 nbytes += vidtv_psi_table_header_write_into(&h_args); 1901 1902 psi_args.from = &args->eit->transport_id; 1903 psi_args.dest_offset = args->offset + nbytes; 1904 psi_args.continuity_counter = args->continuity_counter; 1905 psi_args.crc = &crc; 1906 1907 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1908 1909 /* skip both pointers at the end */ 1910 psi_args.len = sizeof(struct vidtv_psi_table_eit_event) - 1911 sizeof(struct vidtv_psi_desc *) - 1912 sizeof(struct vidtv_psi_table_eit_event *); 1913 while (event) { 1914 /* copy the events, if any */ 1915 psi_args.from = event; 1916 psi_args.dest_offset = args->offset + nbytes; 1917 1918 nbytes += vidtv_psi_ts_psi_write_into(&psi_args); 1919 1920 event_descriptor = event->descriptor; 1921 1922 while (event_descriptor) { 1923 /* copy the event descriptors, if any */ 1924 d_args.dest_offset = args->offset + nbytes; 1925 d_args.desc = event_descriptor; 1926 d_args.continuity_counter = args->continuity_counter; 1927 d_args.crc = &crc; 1928 1929 nbytes += vidtv_psi_desc_write_into(&d_args); 1930 1931 event_descriptor = event_descriptor->next; 1932 } 1933 1934 event = event->next; 1935 } 1936 1937 c_args.dest_offset = args->offset + nbytes; 1938 c_args.crc = cpu_to_be32(crc); 1939 c_args.continuity_counter = args->continuity_counter; 1940 1941 /* Write the CRC at the end */ 1942 nbytes += table_section_crc32_write_into(&c_args); 1943 1944 return nbytes; 1945 } 1946 1947 struct vidtv_psi_table_eit_event 1948 *vidtv_psi_eit_event_init(struct vidtv_psi_table_eit_event *head, u16 event_id) 1949 { 1950 const u8 DURATION[] = {0x23, 0x59, 0x59}; /* BCD encoded */ 1951 struct vidtv_psi_table_eit_event *e; 1952 struct timespec64 ts; 1953 struct tm time; 1954 int mjd, l; 1955 __be16 mjd_be; 1956 1957 e = kzalloc(sizeof(*e), GFP_KERNEL); 1958 if (!e) 1959 return NULL; 1960 1961 e->event_id = cpu_to_be16(event_id); 1962 1963 ts = ktime_to_timespec64(ktime_get_real()); 1964 time64_to_tm(ts.tv_sec, 0, &time); 1965 1966 /* Convert date to Modified Julian Date - per EN 300 468 Annex C */ 1967 if (time.tm_mon < 2) 1968 l = 1; 1969 else 1970 l = 0; 1971 1972 mjd = 14956 + time.tm_mday; 1973 mjd += (time.tm_year - l) * 36525 / 100; 1974 mjd += (time.tm_mon + 2 + l * 12) * 306001 / 10000; 1975 mjd_be = cpu_to_be16(mjd); 1976 1977 /* 1978 * Store MJD and hour/min/sec to the event. 1979 * 1980 * Let's make the event to start on a full hour 1981 */ 1982 memcpy(e->start_time, &mjd_be, sizeof(mjd_be)); 1983 e->start_time[2] = bin2bcd(time.tm_hour); 1984 e->start_time[3] = 0; 1985 e->start_time[4] = 0; 1986 1987 /* 1988 * TODO: for now, the event will last for a day. Should be 1989 * enough for testing purposes, but if one runs the driver 1990 * for more than that, the current event will become invalid. 1991 * So, we need a better code here in order to change the start 1992 * time once the event expires. 1993 */ 1994 memcpy(e->duration, DURATION, sizeof(e->duration)); 1995 1996 e->bitfield = cpu_to_be16(RUNNING << 13); 1997 1998 if (head) { 1999 while (head->next) 2000 head = head->next; 2001 2002 head->next = e; 2003 } 2004 2005 return e; 2006 } 2007 2008 void vidtv_psi_eit_event_destroy(struct vidtv_psi_table_eit_event *e) 2009 { 2010 struct vidtv_psi_table_eit_event *tmp_e = NULL; 2011 struct vidtv_psi_table_eit_event *curr_e = e; 2012 2013 while (curr_e) { 2014 tmp_e = curr_e; 2015 curr_e = curr_e->next; 2016 vidtv_psi_desc_destroy(tmp_e->descriptor); 2017 kfree(tmp_e); 2018 } 2019 } 2020 2021 void vidtv_psi_eit_table_destroy(struct vidtv_psi_table_eit *eit) 2022 { 2023 vidtv_psi_eit_event_destroy(eit->event); 2024 kfree(eit); 2025 } 2026