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 = kmemdup(desc, sizeof(*desc) + desc->length, GFP_KERNEL);
510 			if (!curr)
511 				return NULL;
512 		}
513 
514 		if (!curr)
515 			return NULL;
516 
517 		curr->next = NULL;
518 		if (!head)
519 			head = curr;
520 		if (prev)
521 			prev->next = curr;
522 
523 		prev = curr;
524 		desc = desc->next;
525 	}
526 
527 	return head;
528 }
529 
530 void vidtv_psi_desc_destroy(struct vidtv_psi_desc *desc)
531 {
532 	struct vidtv_psi_desc_service_list_entry *sl_entry_tmp = NULL;
533 	struct vidtv_psi_desc_service_list_entry *sl_entry = NULL;
534 	struct vidtv_psi_desc *curr = desc;
535 	struct vidtv_psi_desc *tmp  = NULL;
536 
537 	while (curr) {
538 		tmp  = curr;
539 		curr = curr->next;
540 
541 		switch (tmp->type) {
542 		case SERVICE_DESCRIPTOR:
543 			kfree(((struct vidtv_psi_desc_service *)tmp)->provider_name);
544 			kfree(((struct vidtv_psi_desc_service *)tmp)->service_name);
545 
546 			break;
547 		case REGISTRATION_DESCRIPTOR:
548 			/* nothing to do */
549 			break;
550 
551 		case NETWORK_NAME_DESCRIPTOR:
552 			kfree(((struct vidtv_psi_desc_network_name *)tmp)->network_name);
553 			break;
554 
555 		case SERVICE_LIST_DESCRIPTOR:
556 			sl_entry = ((struct vidtv_psi_desc_service_list *)tmp)->service_list;
557 			while (sl_entry) {
558 				sl_entry_tmp = sl_entry;
559 				sl_entry = sl_entry->next;
560 				kfree(sl_entry_tmp);
561 			}
562 			break;
563 
564 		case SHORT_EVENT_DESCRIPTOR:
565 			kfree(((struct vidtv_psi_desc_short_event *)tmp)->iso_language_code);
566 			kfree(((struct vidtv_psi_desc_short_event *)tmp)->event_name);
567 			kfree(((struct vidtv_psi_desc_short_event *)tmp)->text);
568 		break;
569 
570 		default:
571 			pr_warn_ratelimited("Possible leak: not handling descriptor type %d\n",
572 					    tmp->type);
573 			break;
574 		}
575 
576 		kfree(tmp);
577 	}
578 }
579 
580 static u16
581 vidtv_psi_desc_comp_loop_len(struct vidtv_psi_desc *desc)
582 {
583 	u32 length = 0;
584 
585 	if (!desc)
586 		return 0;
587 
588 	while (desc) {
589 		length += sizeof_field(struct vidtv_psi_desc, type);
590 		length += sizeof_field(struct vidtv_psi_desc, length);
591 		length += desc->length; /* from 'length' field until the end of the descriptor */
592 		desc    = desc->next;
593 	}
594 
595 	return length;
596 }
597 
598 void vidtv_psi_desc_assign(struct vidtv_psi_desc **to,
599 			   struct vidtv_psi_desc *desc)
600 {
601 	if (desc == *to)
602 		return;
603 
604 	if (*to)
605 		vidtv_psi_desc_destroy(*to);
606 
607 	*to = desc;
608 }
609 
610 void vidtv_pmt_desc_assign(struct vidtv_psi_table_pmt *pmt,
611 			   struct vidtv_psi_desc **to,
612 			   struct vidtv_psi_desc *desc)
613 {
614 	vidtv_psi_desc_assign(to, desc);
615 	vidtv_psi_pmt_table_update_sec_len(pmt);
616 
617 	if (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN)
618 		vidtv_psi_desc_assign(to, NULL);
619 
620 	vidtv_psi_update_version_num(&pmt->header);
621 }
622 
623 void vidtv_sdt_desc_assign(struct vidtv_psi_table_sdt *sdt,
624 			   struct vidtv_psi_desc **to,
625 			   struct vidtv_psi_desc *desc)
626 {
627 	vidtv_psi_desc_assign(to, desc);
628 	vidtv_psi_sdt_table_update_sec_len(sdt);
629 
630 	if (vidtv_psi_get_sec_len(&sdt->header) > MAX_SECTION_LEN)
631 		vidtv_psi_desc_assign(to, NULL);
632 
633 	vidtv_psi_update_version_num(&sdt->header);
634 }
635 
636 static u32 vidtv_psi_desc_write_into(struct desc_write_args *args)
637 {
638 	struct psi_write_args psi_args = {
639 		.dest_buf           = args->dest_buf,
640 		.from               = &args->desc->type,
641 		.pid                = args->pid,
642 		.new_psi_section    = false,
643 		.continuity_counter = args->continuity_counter,
644 		.is_crc             = false,
645 		.dest_buf_sz        = args->dest_buf_sz,
646 		.crc                = args->crc,
647 		.len		    = sizeof_field(struct vidtv_psi_desc, type) +
648 				      sizeof_field(struct vidtv_psi_desc, length),
649 	};
650 	struct vidtv_psi_desc_service_list_entry *serv_list_entry = NULL;
651 	u32 nbytes = 0;
652 
653 	psi_args.dest_offset        = args->dest_offset + nbytes;
654 
655 	nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
656 
657 	switch (args->desc->type) {
658 	case SERVICE_DESCRIPTOR:
659 		psi_args.dest_offset = args->dest_offset + nbytes;
660 		psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_type) +
661 			       sizeof_field(struct vidtv_psi_desc_service, provider_name_len);
662 		psi_args.from = &((struct vidtv_psi_desc_service *)args->desc)->service_type;
663 
664 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
665 
666 		psi_args.dest_offset = args->dest_offset + nbytes;
667 		psi_args.len = ((struct vidtv_psi_desc_service *)args->desc)->provider_name_len;
668 		psi_args.from = ((struct vidtv_psi_desc_service *)args->desc)->provider_name;
669 
670 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
671 
672 		psi_args.dest_offset = args->dest_offset + nbytes;
673 		psi_args.len = sizeof_field(struct vidtv_psi_desc_service, service_name_len);
674 		psi_args.from = &((struct vidtv_psi_desc_service *)args->desc)->service_name_len;
675 
676 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
677 
678 		psi_args.dest_offset = args->dest_offset + nbytes;
679 		psi_args.len = ((struct vidtv_psi_desc_service *)args->desc)->service_name_len;
680 		psi_args.from = ((struct vidtv_psi_desc_service *)args->desc)->service_name;
681 
682 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
683 		break;
684 
685 	case NETWORK_NAME_DESCRIPTOR:
686 		psi_args.dest_offset = args->dest_offset + nbytes;
687 		psi_args.len = args->desc->length;
688 		psi_args.from = ((struct vidtv_psi_desc_network_name *)args->desc)->network_name;
689 
690 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
691 		break;
692 
693 	case SERVICE_LIST_DESCRIPTOR:
694 		serv_list_entry = ((struct vidtv_psi_desc_service_list *)args->desc)->service_list;
695 		while (serv_list_entry) {
696 			psi_args.dest_offset = args->dest_offset + nbytes;
697 			psi_args.len = sizeof(struct vidtv_psi_desc_service_list_entry) -
698 				       sizeof(struct vidtv_psi_desc_service_list_entry *);
699 			psi_args.from = serv_list_entry;
700 
701 			nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
702 
703 			serv_list_entry = serv_list_entry->next;
704 		}
705 		break;
706 
707 	case SHORT_EVENT_DESCRIPTOR:
708 		psi_args.dest_offset = args->dest_offset + nbytes;
709 		psi_args.len = ISO_LANGUAGE_CODE_LEN;
710 		psi_args.from = ((struct vidtv_psi_desc_short_event *)
711 				  args->desc)->iso_language_code;
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 = sizeof_field(struct vidtv_psi_desc_short_event, event_name_len);
717 		psi_args.from = &((struct vidtv_psi_desc_short_event *)
718 				  args->desc)->event_name_len;
719 
720 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
721 
722 		psi_args.dest_offset = args->dest_offset + nbytes;
723 		psi_args.len = ((struct vidtv_psi_desc_short_event *)args->desc)->event_name_len;
724 		psi_args.from = ((struct vidtv_psi_desc_short_event *)args->desc)->event_name;
725 
726 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
727 
728 		psi_args.dest_offset = args->dest_offset + nbytes;
729 		psi_args.len = sizeof_field(struct vidtv_psi_desc_short_event, text_len);
730 		psi_args.from = &((struct vidtv_psi_desc_short_event *)args->desc)->text_len;
731 
732 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
733 
734 		psi_args.dest_offset = args->dest_offset + nbytes;
735 		psi_args.len = ((struct vidtv_psi_desc_short_event *)args->desc)->text_len;
736 		psi_args.from = ((struct vidtv_psi_desc_short_event *)args->desc)->text;
737 
738 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
739 
740 		break;
741 
742 	case REGISTRATION_DESCRIPTOR:
743 	default:
744 		psi_args.dest_offset = args->dest_offset + nbytes;
745 		psi_args.len = args->desc->length;
746 		psi_args.from = &args->desc->data;
747 
748 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
749 		break;
750 	}
751 
752 	return nbytes;
753 }
754 
755 static u32
756 vidtv_psi_table_header_write_into(struct header_write_args *args)
757 {
758 	struct psi_write_args psi_args = {
759 		.dest_buf           = args->dest_buf,
760 		.from               = args->h,
761 		.len                = sizeof(struct vidtv_psi_table_header),
762 		.dest_offset        = args->dest_offset,
763 		.pid                = args->pid,
764 		.new_psi_section    = true,
765 		.continuity_counter = args->continuity_counter,
766 		.is_crc             = false,
767 		.dest_buf_sz        = args->dest_buf_sz,
768 		.crc                = args->crc,
769 	};
770 
771 	return vidtv_psi_ts_psi_write_into(&psi_args);
772 }
773 
774 void
775 vidtv_psi_pat_table_update_sec_len(struct vidtv_psi_table_pat *pat)
776 {
777 	u16 length = 0;
778 	u32 i;
779 
780 	/* see ISO/IEC 13818-1 : 2000 p.43 */
781 
782 	/* from immediately after 'section_length' until 'last_section_number'*/
783 	length += PAT_LEN_UNTIL_LAST_SECTION_NUMBER;
784 
785 	/* do not count the pointer */
786 	for (i = 0; i < pat->num_pat; ++i)
787 		length += sizeof(struct vidtv_psi_table_pat_program) -
788 			  sizeof(struct vidtv_psi_table_pat_program *);
789 
790 	length += CRC_SIZE_IN_BYTES;
791 
792 	vidtv_psi_set_sec_len(&pat->header, length);
793 }
794 
795 void vidtv_psi_pmt_table_update_sec_len(struct vidtv_psi_table_pmt *pmt)
796 {
797 	struct vidtv_psi_table_pmt_stream *s = pmt->stream;
798 	u16 desc_loop_len;
799 	u16 length = 0;
800 
801 	/* see ISO/IEC 13818-1 : 2000 p.46 */
802 
803 	/* from immediately after 'section_length' until 'program_info_length'*/
804 	length += PMT_LEN_UNTIL_PROGRAM_INFO_LENGTH;
805 
806 	desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor);
807 	vidtv_psi_set_desc_loop_len(&pmt->bitfield2, desc_loop_len, 10);
808 
809 	length += desc_loop_len;
810 
811 	while (s) {
812 		/* skip both pointers at the end */
813 		length += sizeof(struct vidtv_psi_table_pmt_stream) -
814 			  sizeof(struct vidtv_psi_desc *) -
815 			  sizeof(struct vidtv_psi_table_pmt_stream *);
816 
817 		desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor);
818 		vidtv_psi_set_desc_loop_len(&s->bitfield2, desc_loop_len, 10);
819 
820 		length += desc_loop_len;
821 
822 		s = s->next;
823 	}
824 
825 	length += CRC_SIZE_IN_BYTES;
826 
827 	vidtv_psi_set_sec_len(&pmt->header, length);
828 }
829 
830 void vidtv_psi_sdt_table_update_sec_len(struct vidtv_psi_table_sdt *sdt)
831 {
832 	struct vidtv_psi_table_sdt_service *s = sdt->service;
833 	u16 desc_loop_len;
834 	u16 length = 0;
835 
836 	/* see ETSI EN 300 468 V 1.10.1 p.24 */
837 
838 	/*
839 	 * from immediately after 'section_length' until
840 	 * 'reserved_for_future_use'
841 	 */
842 	length += SDT_LEN_UNTIL_RESERVED_FOR_FUTURE_USE;
843 
844 	while (s) {
845 		/* skip both pointers at the end */
846 		length += sizeof(struct vidtv_psi_table_sdt_service) -
847 			  sizeof(struct vidtv_psi_desc *) -
848 			  sizeof(struct vidtv_psi_table_sdt_service *);
849 
850 		desc_loop_len = vidtv_psi_desc_comp_loop_len(s->descriptor);
851 		vidtv_psi_set_desc_loop_len(&s->bitfield, desc_loop_len, 12);
852 
853 		length += desc_loop_len;
854 
855 		s = s->next;
856 	}
857 
858 	length += CRC_SIZE_IN_BYTES;
859 	vidtv_psi_set_sec_len(&sdt->header, length);
860 }
861 
862 struct vidtv_psi_table_pat_program*
863 vidtv_psi_pat_program_init(struct vidtv_psi_table_pat_program *head,
864 			   u16 service_id,
865 			   u16 program_map_pid)
866 {
867 	struct vidtv_psi_table_pat_program *program;
868 	const u16 RESERVED = 0x07;
869 
870 	program = kzalloc(sizeof(*program), GFP_KERNEL);
871 	if (!program)
872 		return NULL;
873 
874 	program->service_id = cpu_to_be16(service_id);
875 
876 	/* pid for the PMT section in the TS */
877 	program->bitfield = cpu_to_be16((RESERVED << 13) | program_map_pid);
878 	program->next = NULL;
879 
880 	if (head) {
881 		while (head->next)
882 			head = head->next;
883 
884 		head->next = program;
885 	}
886 
887 	return program;
888 }
889 
890 void
891 vidtv_psi_pat_program_destroy(struct vidtv_psi_table_pat_program *p)
892 {
893 	struct vidtv_psi_table_pat_program *tmp  = NULL;
894 	struct vidtv_psi_table_pat_program *curr = p;
895 
896 	while (curr) {
897 		tmp  = curr;
898 		curr = curr->next;
899 		kfree(tmp);
900 	}
901 }
902 
903 /* This function transfers ownership of p to the table */
904 void
905 vidtv_psi_pat_program_assign(struct vidtv_psi_table_pat *pat,
906 			     struct vidtv_psi_table_pat_program *p)
907 {
908 	struct vidtv_psi_table_pat_program *program;
909 	u16 program_count;
910 
911 	do {
912 		program_count = 0;
913 		program = p;
914 
915 		if (p == pat->program)
916 			return;
917 
918 		while (program) {
919 			++program_count;
920 			program = program->next;
921 		}
922 
923 		pat->num_pat = program_count;
924 		pat->program  = p;
925 
926 		/* Recompute section length */
927 		vidtv_psi_pat_table_update_sec_len(pat);
928 
929 		p = NULL;
930 	} while (vidtv_psi_get_sec_len(&pat->header) > MAX_SECTION_LEN);
931 
932 	vidtv_psi_update_version_num(&pat->header);
933 }
934 
935 struct vidtv_psi_table_pat *vidtv_psi_pat_table_init(u16 transport_stream_id)
936 {
937 	struct vidtv_psi_table_pat *pat;
938 	const u16 SYNTAX = 0x1;
939 	const u16 ZERO = 0x0;
940 	const u16 ONES = 0x03;
941 
942 	pat = kzalloc(sizeof(*pat), GFP_KERNEL);
943 	if (!pat)
944 		return NULL;
945 
946 	pat->header.table_id = 0x0;
947 
948 	pat->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12));
949 	pat->header.id           = cpu_to_be16(transport_stream_id);
950 	pat->header.current_next = 0x1;
951 
952 	pat->header.version = 0x1f;
953 
954 	pat->header.one2         = 0x03;
955 	pat->header.section_id   = 0x0;
956 	pat->header.last_section = 0x0;
957 
958 	vidtv_psi_pat_table_update_sec_len(pat);
959 
960 	return pat;
961 }
962 
963 u32 vidtv_psi_pat_write_into(struct vidtv_psi_pat_write_args *args)
964 {
965 	struct vidtv_psi_table_pat_program *p = args->pat->program;
966 	struct header_write_args h_args       = {
967 		.dest_buf           = args->buf,
968 		.dest_offset        = args->offset,
969 		.pid                = VIDTV_PAT_PID,
970 		.h                  = &args->pat->header,
971 		.continuity_counter = args->continuity_counter,
972 		.dest_buf_sz        = args->buf_sz,
973 	};
974 	struct psi_write_args psi_args        = {
975 		.dest_buf           = args->buf,
976 		.pid                = VIDTV_PAT_PID,
977 		.new_psi_section    = false,
978 		.continuity_counter = args->continuity_counter,
979 		.is_crc             = false,
980 		.dest_buf_sz        = args->buf_sz,
981 	};
982 	struct crc32_write_args c_args        = {
983 		.dest_buf           = args->buf,
984 		.pid                = VIDTV_PAT_PID,
985 		.dest_buf_sz        = args->buf_sz,
986 	};
987 	u32 crc = INITIAL_CRC;
988 	u32 nbytes = 0;
989 
990 	vidtv_psi_pat_table_update_sec_len(args->pat);
991 
992 	h_args.crc = &crc;
993 
994 	nbytes += vidtv_psi_table_header_write_into(&h_args);
995 
996 	/* note that the field 'u16 programs' is not really part of the PAT */
997 
998 	psi_args.crc = &crc;
999 
1000 	while (p) {
1001 		/* copy the PAT programs */
1002 		psi_args.from = p;
1003 		/* skip the pointer */
1004 		psi_args.len = sizeof(*p) -
1005 			       sizeof(struct vidtv_psi_table_pat_program *);
1006 		psi_args.dest_offset = args->offset + nbytes;
1007 		psi_args.continuity_counter = args->continuity_counter;
1008 
1009 		nbytes += vidtv_psi_ts_psi_write_into(&psi_args);
1010 
1011 		p = p->next;
1012 	}
1013 
1014 	c_args.dest_offset        = args->offset + nbytes;
1015 	c_args.continuity_counter = args->continuity_counter;
1016 	c_args.crc                = cpu_to_be32(crc);
1017 
1018 	/* Write the CRC32 at the end */
1019 	nbytes += table_section_crc32_write_into(&c_args);
1020 
1021 	return nbytes;
1022 }
1023 
1024 void
1025 vidtv_psi_pat_table_destroy(struct vidtv_psi_table_pat *p)
1026 {
1027 	vidtv_psi_pat_program_destroy(p->program);
1028 	kfree(p);
1029 }
1030 
1031 struct vidtv_psi_table_pmt_stream*
1032 vidtv_psi_pmt_stream_init(struct vidtv_psi_table_pmt_stream *head,
1033 			  enum vidtv_psi_stream_types stream_type,
1034 			  u16 es_pid)
1035 {
1036 	struct vidtv_psi_table_pmt_stream *stream;
1037 	const u16 RESERVED1 = 0x07;
1038 	const u16 RESERVED2 = 0x0f;
1039 	const u16 ZERO = 0x0;
1040 	u16 desc_loop_len;
1041 
1042 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1043 	if (!stream)
1044 		return NULL;
1045 
1046 	stream->type = stream_type;
1047 
1048 	stream->bitfield = cpu_to_be16((RESERVED1 << 13) | es_pid);
1049 
1050 	desc_loop_len = vidtv_psi_desc_comp_loop_len(stream->descriptor);
1051 
1052 	stream->bitfield2 = cpu_to_be16((RESERVED2 << 12) |
1053 					(ZERO << 10)      |
1054 					desc_loop_len);
1055 	stream->next = NULL;
1056 
1057 	if (head) {
1058 		while (head->next)
1059 			head = head->next;
1060 
1061 		head->next = stream;
1062 	}
1063 
1064 	return stream;
1065 }
1066 
1067 void vidtv_psi_pmt_stream_destroy(struct vidtv_psi_table_pmt_stream *s)
1068 {
1069 	struct vidtv_psi_table_pmt_stream *tmp_stream  = NULL;
1070 	struct vidtv_psi_table_pmt_stream *curr_stream = s;
1071 
1072 	while (curr_stream) {
1073 		tmp_stream  = curr_stream;
1074 		curr_stream = curr_stream->next;
1075 		vidtv_psi_desc_destroy(tmp_stream->descriptor);
1076 		kfree(tmp_stream);
1077 	}
1078 }
1079 
1080 void vidtv_psi_pmt_stream_assign(struct vidtv_psi_table_pmt *pmt,
1081 				 struct vidtv_psi_table_pmt_stream *s)
1082 {
1083 	do {
1084 		/* This function transfers ownership of s to the table */
1085 		if (s == pmt->stream)
1086 			return;
1087 
1088 		pmt->stream = s;
1089 		vidtv_psi_pmt_table_update_sec_len(pmt);
1090 
1091 		s = NULL;
1092 	} while (vidtv_psi_get_sec_len(&pmt->header) > MAX_SECTION_LEN);
1093 
1094 	vidtv_psi_update_version_num(&pmt->header);
1095 }
1096 
1097 u16 vidtv_psi_pmt_get_pid(struct vidtv_psi_table_pmt *section,
1098 			  struct vidtv_psi_table_pat *pat)
1099 {
1100 	struct vidtv_psi_table_pat_program *program = pat->program;
1101 
1102 	/*
1103 	 * service_id is the same as program_number in the
1104 	 * corresponding program_map_section
1105 	 * see ETSI EN 300 468 v1.15.1 p. 24
1106 	 */
1107 	while (program) {
1108 		if (program->service_id == section->header.id)
1109 			return vidtv_psi_get_pat_program_pid(program);
1110 
1111 		program = program->next;
1112 	}
1113 
1114 	return TS_LAST_VALID_PID + 1; /* not found */
1115 }
1116 
1117 struct vidtv_psi_table_pmt *vidtv_psi_pmt_table_init(u16 program_number,
1118 						     u16 pcr_pid)
1119 {
1120 	struct vidtv_psi_table_pmt *pmt;
1121 	const u16 RESERVED1 = 0x07;
1122 	const u16 RESERVED2 = 0x0f;
1123 	const u16 SYNTAX = 0x1;
1124 	const u16 ONES = 0x03;
1125 	const u16 ZERO = 0x0;
1126 	u16 desc_loop_len;
1127 
1128 	pmt = kzalloc(sizeof(*pmt), GFP_KERNEL);
1129 	if (!pmt)
1130 		return NULL;
1131 
1132 	if (!pcr_pid)
1133 		pcr_pid = 0x1fff;
1134 
1135 	pmt->header.table_id = 0x2;
1136 
1137 	pmt->header.bitfield = cpu_to_be16((SYNTAX << 15) | (ZERO << 14) | (ONES << 12));
1138 
1139 	pmt->header.id = cpu_to_be16(program_number);
1140 	pmt->header.current_next = 0x1;
1141 
1142 	pmt->header.version = 0x1f;
1143 
1144 	pmt->header.one2 = ONES;
1145 	pmt->header.section_id   = 0;
1146 	pmt->header.last_section = 0;
1147 
1148 	pmt->bitfield = cpu_to_be16((RESERVED1 << 13) | pcr_pid);
1149 
1150 	desc_loop_len = vidtv_psi_desc_comp_loop_len(pmt->descriptor);
1151 
1152 	pmt->bitfield2 = cpu_to_be16((RESERVED2 << 12) |
1153 				     (ZERO << 10)      |
1154 				     desc_loop_len);
1155 
1156 	vidtv_psi_pmt_table_update_sec_len(pmt);
1157 
1158 	return pmt;
1159 }
1160 
1161 u32 vidtv_psi_pmt_write_into(struct vidtv_psi_pmt_write_args *args)
1162 {
1163 	struct vidtv_psi_desc *table_descriptor   = args->pmt->descriptor;
1164 	struct vidtv_psi_table_pmt_stream *stream = args->pmt->stream;
1165 	struct vidtv_psi_desc *stream_descriptor;
1166 	u32 crc = INITIAL_CRC;
1167 	u32 nbytes = 0;
1168 	struct header_write_args h_args = {
1169 		.dest_buf           = args->buf,
1170 		.dest_offset        = args->offset,
1171 		.h                  = &args->pmt->header,
1172 		.pid                = args->pid,
1173 		.continuity_counter = args->continuity_counter,
1174 		.dest_buf_sz        = args->buf_sz,
1175 	};
1176 	struct psi_write_args psi_args  = {
1177 		.dest_buf = args->buf,
1178 		.from     = &args->pmt->bitfield,
1179 		.len      = sizeof_field(struct vidtv_psi_table_pmt, bitfield) +
1180 			    sizeof_field(struct vidtv_psi_table_pmt, bitfield2),
1181 		.pid                = args->pid,
1182 		.new_psi_section    = false,
1183 		.is_crc             = false,
1184 		.dest_buf_sz        = args->buf_sz,
1185 		.crc                = &crc,
1186 	};
1187 	struct desc_write_args d_args   = {
1188 		.dest_buf           = args->buf,
1189 		.desc               = table_descriptor,
1190 		.pid                = args->pid,
1191 		.dest_buf_sz        = args->buf_sz,
1192 	};
1193 	struct crc32_write_args c_args  = {
1194 		.dest_buf           = args->buf,
1195 		.pid                = args->pid,
1196 		.dest_buf_sz        = args->buf_sz,
1197 	};
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