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