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