1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-vbi-gen.c - vbi generator support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/ktime.h>
11 #include <linux/string.h>
12 #include <linux/videodev2.h>
13 
14 #include "vivid-vbi-gen.h"
15 
wss_insert(u8 * wss,u32 val,unsigned size)16 static void wss_insert(u8 *wss, u32 val, unsigned size)
17 {
18 	while (size--)
19 		*wss++ = (val & (1 << size)) ? 0xc0 : 0x10;
20 }
21 
vivid_vbi_gen_wss_raw(const struct v4l2_sliced_vbi_data * data,u8 * buf,unsigned sampling_rate)22 static void vivid_vbi_gen_wss_raw(const struct v4l2_sliced_vbi_data *data,
23 		u8 *buf, unsigned sampling_rate)
24 {
25 	const unsigned rate = 5000000;	/* WSS has a 5 MHz transmission rate */
26 	u8 wss[29 + 24 + 24 + 24 + 18 + 18] = { 0 };
27 	const unsigned zero = 0x07;
28 	const unsigned one = 0x38;
29 	unsigned bit = 0;
30 	u16 wss_data;
31 	int i;
32 
33 	wss_insert(wss + bit, 0x1f1c71c7, 29); bit += 29;
34 	wss_insert(wss + bit, 0x1e3c1f, 24); bit += 24;
35 
36 	wss_data = (data->data[1] << 8) | data->data[0];
37 	for (i = 0; i <= 13; i++, bit += 6)
38 		wss_insert(wss + bit, (wss_data & (1 << i)) ? one : zero, 6);
39 
40 	for (i = 0, bit = 0; bit < sizeof(wss); bit++) {
41 		unsigned n = ((bit + 1) * sampling_rate) / rate;
42 
43 		while (i < n)
44 			buf[i++] = wss[bit];
45 	}
46 }
47 
vivid_vbi_gen_teletext_raw(const struct v4l2_sliced_vbi_data * data,u8 * buf,unsigned sampling_rate)48 static void vivid_vbi_gen_teletext_raw(const struct v4l2_sliced_vbi_data *data,
49 		u8 *buf, unsigned sampling_rate)
50 {
51 	const unsigned rate = 6937500 / 10;	/* Teletext has a 6.9375 MHz transmission rate */
52 	u8 teletext[45] = { 0x55, 0x55, 0x27 };
53 	unsigned bit = 0;
54 	int i;
55 
56 	memcpy(teletext + 3, data->data, sizeof(teletext) - 3);
57 	/* prevents 32 bit overflow */
58 	sampling_rate /= 10;
59 
60 	for (i = 0, bit = 0; bit < sizeof(teletext) * 8; bit++) {
61 		unsigned n = ((bit + 1) * sampling_rate) / rate;
62 		u8 val = (teletext[bit / 8] & (1 << (bit & 7))) ? 0xc0 : 0x10;
63 
64 		while (i < n)
65 			buf[i++] = val;
66 	}
67 }
68 
cc_insert(u8 * cc,u8 ch)69 static void cc_insert(u8 *cc, u8 ch)
70 {
71 	unsigned tot = 0;
72 	unsigned i;
73 
74 	for (i = 0; i < 7; i++) {
75 		cc[2 * i] = cc[2 * i + 1] = (ch & (1 << i)) ? 1 : 0;
76 		tot += cc[2 * i];
77 	}
78 	cc[14] = cc[15] = !(tot & 1);
79 }
80 
81 #define CC_PREAMBLE_BITS (14 + 4 + 2)
82 
vivid_vbi_gen_cc_raw(const struct v4l2_sliced_vbi_data * data,u8 * buf,unsigned sampling_rate)83 static void vivid_vbi_gen_cc_raw(const struct v4l2_sliced_vbi_data *data,
84 		u8 *buf, unsigned sampling_rate)
85 {
86 	const unsigned rate = 1000000;	/* CC has a 1 MHz transmission rate */
87 
88 	u8 cc[CC_PREAMBLE_BITS + 2 * 16] = {
89 		/* Clock run-in: 7 cycles */
90 		0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
91 		/* 2 cycles of 0 */
92 		0, 0, 0, 0,
93 		/* Start bit of 1 (each bit is two cycles) */
94 		1, 1
95 	};
96 	unsigned bit, i;
97 
98 	cc_insert(cc + CC_PREAMBLE_BITS, data->data[0]);
99 	cc_insert(cc + CC_PREAMBLE_BITS + 16, data->data[1]);
100 
101 	for (i = 0, bit = 0; bit < sizeof(cc); bit++) {
102 		unsigned n = ((bit + 1) * sampling_rate) / rate;
103 
104 		while (i < n)
105 			buf[i++] = cc[bit] ? 0xc0 : 0x10;
106 	}
107 }
108 
vivid_vbi_gen_raw(const struct vivid_vbi_gen_data * vbi,const struct v4l2_vbi_format * vbi_fmt,u8 * buf)109 void vivid_vbi_gen_raw(const struct vivid_vbi_gen_data *vbi,
110 		const struct v4l2_vbi_format *vbi_fmt, u8 *buf)
111 {
112 	unsigned idx;
113 
114 	for (idx = 0; idx < 25; idx++) {
115 		const struct v4l2_sliced_vbi_data *data = vbi->data + idx;
116 		unsigned start_2nd_field;
117 		unsigned line = data->line;
118 		u8 *linebuf = buf;
119 
120 		start_2nd_field = (data->id & V4L2_SLICED_VBI_525) ? 263 : 313;
121 		if (data->field)
122 			line += start_2nd_field;
123 		line -= vbi_fmt->start[data->field];
124 
125 		if (vbi_fmt->flags & V4L2_VBI_INTERLACED)
126 			linebuf += (line * 2 + data->field) *
127 				vbi_fmt->samples_per_line;
128 		else
129 			linebuf += (line + data->field * vbi_fmt->count[0]) *
130 				vbi_fmt->samples_per_line;
131 		if (data->id == V4L2_SLICED_CAPTION_525)
132 			vivid_vbi_gen_cc_raw(data, linebuf, vbi_fmt->sampling_rate);
133 		else if (data->id == V4L2_SLICED_WSS_625)
134 			vivid_vbi_gen_wss_raw(data, linebuf, vbi_fmt->sampling_rate);
135 		else if (data->id == V4L2_SLICED_TELETEXT_B)
136 			vivid_vbi_gen_teletext_raw(data, linebuf, vbi_fmt->sampling_rate);
137 	}
138 }
139 
140 static const u8 vivid_cc_sequence1[30] = {
141 	0x14, 0x20,	/* Resume Caption Loading */
142 	'H',  'e',
143 	'l',  'l',
144 	'o',  ' ',
145 	'w',  'o',
146 	'r',  'l',
147 	'd',  '!',
148 	0x14, 0x2f,	/* End of Caption */
149 };
150 
151 static const u8 vivid_cc_sequence2[30] = {
152 	0x14, 0x20,	/* Resume Caption Loading */
153 	'C',  'l',
154 	'o',  's',
155 	'e',  'd',
156 	' ',  'c',
157 	'a',  'p',
158 	't',  'i',
159 	'o',  'n',
160 	's',  ' ',
161 	't',  'e',
162 	's',  't',
163 	0x14, 0x2f,	/* End of Caption */
164 };
165 
calc_parity(u8 val)166 static u8 calc_parity(u8 val)
167 {
168 	unsigned i;
169 	unsigned tot = 0;
170 
171 	for (i = 0; i < 7; i++)
172 		tot += (val & (1 << i)) ? 1 : 0;
173 	return val | ((tot & 1) ? 0 : 0x80);
174 }
175 
vivid_vbi_gen_set_time_of_day(u8 * packet)176 static void vivid_vbi_gen_set_time_of_day(u8 *packet)
177 {
178 	struct tm tm;
179 	u8 checksum, i;
180 
181 	time64_to_tm(ktime_get_real_seconds(), 0, &tm);
182 	packet[0] = calc_parity(0x07);
183 	packet[1] = calc_parity(0x01);
184 	packet[2] = calc_parity(0x40 | tm.tm_min);
185 	packet[3] = calc_parity(0x40 | tm.tm_hour);
186 	packet[4] = calc_parity(0x40 | tm.tm_mday);
187 	if (tm.tm_mday == 1 && tm.tm_mon == 2 &&
188 	    sys_tz.tz_minuteswest > tm.tm_min + tm.tm_hour * 60)
189 		packet[4] = calc_parity(0x60 | tm.tm_mday);
190 	packet[5] = calc_parity(0x40 | (1 + tm.tm_mon));
191 	packet[6] = calc_parity(0x40 | (1 + tm.tm_wday));
192 	packet[7] = calc_parity(0x40 | ((tm.tm_year - 90) & 0x3f));
193 	packet[8] = calc_parity(0x0f);
194 	for (checksum = i = 0; i <= 8; i++)
195 		checksum += packet[i] & 0x7f;
196 	packet[9] = calc_parity(0x100 - checksum);
197 	packet[10] = calc_parity(0x07);
198 	packet[11] = calc_parity(0x04);
199 	if (sys_tz.tz_minuteswest >= 0)
200 		packet[12] = calc_parity(0x40 | ((sys_tz.tz_minuteswest / 60) & 0x1f));
201 	else
202 		packet[12] = calc_parity(0x40 | ((24 + sys_tz.tz_minuteswest / 60) & 0x1f));
203 	packet[13] = calc_parity(0);
204 	packet[14] = calc_parity(0x0f);
205 	for (checksum = 0, i = 10; i <= 14; i++)
206 		checksum += packet[i] & 0x7f;
207 	packet[15] = calc_parity(0x100 - checksum);
208 }
209 
210 static const u8 hamming[16] = {
211 	0x15, 0x02, 0x49, 0x5e, 0x64, 0x73, 0x38, 0x2f,
212 	0xd0, 0xc7, 0x8c, 0x9b, 0xa1, 0xb6, 0xfd, 0xea
213 };
214 
vivid_vbi_gen_teletext(u8 * packet,unsigned line,unsigned frame)215 static void vivid_vbi_gen_teletext(u8 *packet, unsigned line, unsigned frame)
216 {
217 	unsigned offset = 2;
218 	unsigned i;
219 
220 	packet[0] = hamming[1 + ((line & 1) << 3)];
221 	packet[1] = hamming[line >> 1];
222 	memset(packet + 2, 0x20, 40);
223 	if (line == 0) {
224 		/* subcode */
225 		packet[2] = hamming[frame % 10];
226 		packet[3] = hamming[frame / 10];
227 		packet[4] = hamming[0];
228 		packet[5] = hamming[0];
229 		packet[6] = hamming[0];
230 		packet[7] = hamming[0];
231 		packet[8] = hamming[0];
232 		packet[9] = hamming[1];
233 		offset = 10;
234 	}
235 	packet += offset;
236 	memcpy(packet, "Page: 100 Row: 10", 17);
237 	packet[7] = '0' + frame / 10;
238 	packet[8] = '0' + frame % 10;
239 	packet[15] = '0' + line / 10;
240 	packet[16] = '0' + line % 10;
241 	for (i = 0; i < 42 - offset; i++)
242 		packet[i] = calc_parity(packet[i]);
243 }
244 
vivid_vbi_gen_sliced(struct vivid_vbi_gen_data * vbi,bool is_60hz,unsigned seqnr)245 void vivid_vbi_gen_sliced(struct vivid_vbi_gen_data *vbi,
246 		bool is_60hz, unsigned seqnr)
247 {
248 	struct v4l2_sliced_vbi_data *data0 = vbi->data;
249 	struct v4l2_sliced_vbi_data *data1 = vbi->data + 1;
250 	unsigned frame = seqnr % 60;
251 
252 	memset(vbi->data, 0, sizeof(vbi->data));
253 
254 	if (!is_60hz) {
255 		unsigned i;
256 
257 		for (i = 0; i <= 11; i++) {
258 			data0->id = V4L2_SLICED_TELETEXT_B;
259 			data0->line = 7 + i;
260 			vivid_vbi_gen_teletext(data0->data, i, frame);
261 			data0++;
262 		}
263 		data0->id = V4L2_SLICED_WSS_625;
264 		data0->line = 23;
265 		/* 4x3 video aspect ratio */
266 		data0->data[0] = 0x08;
267 		data0++;
268 		for (i = 0; i <= 11; i++) {
269 			data0->id = V4L2_SLICED_TELETEXT_B;
270 			data0->field = 1;
271 			data0->line = 7 + i;
272 			vivid_vbi_gen_teletext(data0->data, 12 + i, frame);
273 			data0++;
274 		}
275 		return;
276 	}
277 
278 	data0->id = V4L2_SLICED_CAPTION_525;
279 	data0->line = 21;
280 	data1->id = V4L2_SLICED_CAPTION_525;
281 	data1->field = 1;
282 	data1->line = 21;
283 
284 	if (frame < 15) {
285 		data0->data[0] = calc_parity(vivid_cc_sequence1[2 * frame]);
286 		data0->data[1] = calc_parity(vivid_cc_sequence1[2 * frame + 1]);
287 	} else if (frame >= 30 && frame < 45) {
288 		frame -= 30;
289 		data0->data[0] = calc_parity(vivid_cc_sequence2[2 * frame]);
290 		data0->data[1] = calc_parity(vivid_cc_sequence2[2 * frame + 1]);
291 	} else {
292 		data0->data[0] = calc_parity(0);
293 		data0->data[1] = calc_parity(0);
294 	}
295 
296 	frame = seqnr % (30 * 60);
297 	switch (frame) {
298 	case 0:
299 		vivid_vbi_gen_set_time_of_day(vbi->time_of_day_packet);
300 		fallthrough;
301 	case 1 ... 7:
302 		data1->data[0] = vbi->time_of_day_packet[frame * 2];
303 		data1->data[1] = vbi->time_of_day_packet[frame * 2 + 1];
304 		break;
305 	default:
306 		data1->data[0] = calc_parity(0);
307 		data1->data[1] = calc_parity(0);
308 		break;
309 	}
310 }
311