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