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