1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License (Version 2) as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 /*
15  * This file contains code to generate a firmware image for the GO7007SB
16  * encoder.  Much of the firmware is read verbatim from a file, but some of
17  * it concerning bitrate control and other things that can be configured at
18  * run-time are generated dynamically.  Note that the format headers
19  * generated here do not affect the functioning of the encoder; they are
20  * merely parroted back to the host at the start of each frame.
21  */
22 
23 #include <linux/module.h>
24 #include <linux/time.h>
25 #include <linux/mm.h>
26 #include <linux/device.h>
27 #include <linux/i2c.h>
28 #include <linux/firmware.h>
29 #include <linux/slab.h>
30 #include <asm/byteorder.h>
31 
32 #include "go7007-priv.h"
33 
34 #define GO7007_FW_NAME "go7007/go7007tv.bin"
35 
36 /* Constants used in the source firmware image to describe code segments */
37 
38 #define	FLAG_MODE_MJPEG		(1)
39 #define	FLAG_MODE_MPEG1		(1<<1)
40 #define	FLAG_MODE_MPEG2		(1<<2)
41 #define	FLAG_MODE_MPEG4		(1<<3)
42 #define	FLAG_MODE_H263		(1<<4)
43 #define FLAG_MODE_ALL		(FLAG_MODE_MJPEG | FLAG_MODE_MPEG1 | \
44 					FLAG_MODE_MPEG2 | FLAG_MODE_MPEG4 | \
45 					FLAG_MODE_H263)
46 #define FLAG_SPECIAL		(1<<8)
47 
48 #define SPECIAL_FRM_HEAD	0
49 #define SPECIAL_BRC_CTRL	1
50 #define SPECIAL_CONFIG		2
51 #define SPECIAL_SEQHEAD		3
52 #define SPECIAL_AV_SYNC		4
53 #define SPECIAL_FINAL		5
54 #define SPECIAL_AUDIO		6
55 #define SPECIAL_MODET		7
56 
57 /* Little data class for creating MPEG headers bit-by-bit */
58 
59 struct code_gen {
60 	unsigned char *p; /* destination */
61 	u32 a; /* collects bits at the top of the variable */
62 	int b; /* bit position of most recently-written bit */
63 	int len; /* written out so far */
64 };
65 
66 #define CODE_GEN(name, dest) struct code_gen name = { dest, 0, 32, 0 }
67 
68 #define CODE_ADD(name, val, length) do { \
69 	name.b -= (length); \
70 	name.a |= (val) << name.b; \
71 	while (name.b <= 24) { \
72 		*name.p = name.a >> 24; \
73 		++name.p; \
74 		name.a <<= 8; \
75 		name.b += 8; \
76 		name.len += 8; \
77 	} \
78 } while (0)
79 
80 #define CODE_LENGTH(name) (name.len + (32 - name.b))
81 
82 /* Tables for creating the bitrate control data */
83 
84 static const s16 converge_speed_ip[101] = {
85 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
86 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
87 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
88 	1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
89 	2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
90 	3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
91 	5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
92 	9, 10, 10, 11, 12, 13, 14, 15, 16, 17,
93 	19, 20, 22, 23, 25, 27, 30, 32, 35, 38,
94 	41, 45, 49, 53, 58, 63, 69, 76, 83, 91,
95 	100
96 };
97 
98 static const s16 converge_speed_ipb[101] = {
99 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
100 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
101 	3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
102 	4, 4, 4, 4, 5, 5, 5, 5, 5, 6,
103 	6, 6, 6, 7, 7, 7, 7, 8, 8, 9,
104 	9, 9, 10, 10, 11, 12, 12, 13, 14, 14,
105 	15, 16, 17, 18, 19, 20, 22, 23, 25, 26,
106 	28, 30, 32, 34, 37, 40, 42, 46, 49, 53,
107 	57, 61, 66, 71, 77, 83, 90, 97, 106, 115,
108 	125, 135, 147, 161, 175, 191, 209, 228, 249, 273,
109 	300
110 };
111 
112 static const s16 LAMBDA_table[4][101] = {
113 	{	16, 16, 16, 16, 17, 17, 17, 18, 18, 18,
114 		19, 19, 19, 20, 20, 20, 21, 21, 22, 22,
115 		22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
116 		27, 27, 28, 28, 29, 29, 30, 31, 31, 32,
117 		32, 33, 33, 34, 35, 35, 36, 37, 37, 38,
118 		39, 39, 40, 41, 42, 42, 43, 44, 45, 46,
119 		46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
120 		56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
121 		67, 68, 69, 70, 72, 73, 74, 76, 77, 78,
122 		80, 81, 83, 84, 86, 87, 89, 90, 92, 94,
123 		96
124 	},
125 	{
126 		20, 20, 20, 21, 21, 21, 22, 22, 23, 23,
127 		23, 24, 24, 25, 25, 26, 26, 27, 27, 28,
128 		28, 29, 29, 30, 30, 31, 31, 32, 33, 33,
129 		34, 34, 35, 36, 36, 37, 38, 38, 39, 40,
130 		40, 41, 42, 43, 43, 44, 45, 46, 47, 48,
131 		48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
132 		58, 59, 60, 61, 62, 64, 65, 66, 67, 68,
133 		70, 71, 72, 73, 75, 76, 78, 79, 80, 82,
134 		83, 85, 86, 88, 90, 91, 93, 95, 96, 98,
135 		100, 102, 103, 105, 107, 109, 111, 113, 115, 117,
136 		120
137 	},
138 	{
139 		24, 24, 24, 25, 25, 26, 26, 27, 27, 28,
140 		28, 29, 29, 30, 30, 31, 31, 32, 33, 33,
141 		34, 34, 35, 36, 36, 37, 38, 38, 39, 40,
142 		41, 41, 42, 43, 44, 44, 45, 46, 47, 48,
143 		49, 50, 50, 51, 52, 53, 54, 55, 56, 57,
144 		58, 59, 60, 62, 63, 64, 65, 66, 67, 69,
145 		70, 71, 72, 74, 75, 76, 78, 79, 81, 82,
146 		84, 85, 87, 88, 90, 92, 93, 95, 97, 98,
147 		100, 102, 104, 106, 108, 110, 112, 114, 116, 118,
148 		120, 122, 124, 127, 129, 131, 134, 136, 138, 141,
149 		144
150 	},
151 	{
152 		32, 32, 33, 33, 34, 34, 35, 36, 36, 37,
153 		38, 38, 39, 40, 41, 41, 42, 43, 44, 44,
154 		45, 46, 47, 48, 49, 50, 50, 51, 52, 53,
155 		54, 55, 56, 57, 58, 59, 60, 62, 63, 64,
156 		65, 66, 67, 69, 70, 71, 72, 74, 75, 76,
157 		78, 79, 81, 82, 84, 85, 87, 88, 90, 92,
158 		93, 95, 97, 98, 100, 102, 104, 106, 108, 110,
159 		112, 114, 116, 118, 120, 122, 124, 127, 129, 131,
160 		134, 136, 139, 141, 144, 146, 149, 152, 154, 157,
161 		160, 163, 166, 169, 172, 175, 178, 181, 185, 188,
162 		192
163 	}
164 };
165 
166 /* MPEG blank frame generation tables */
167 
168 enum mpeg_frame_type {
169 	PFRAME,
170 	BFRAME_PRE,
171 	BFRAME_POST,
172 	BFRAME_BIDIR,
173 	BFRAME_EMPTY
174 };
175 
176 static const u32 addrinctab[33][2] = {
177 	{ 0x01, 1 },	{ 0x03, 3 },	{ 0x02, 3 },	{ 0x03, 4 },
178 	{ 0x02, 4 },	{ 0x03, 5 },	{ 0x02, 5 },	{ 0x07, 7 },
179 	{ 0x06, 7 },	{ 0x0b, 8 },	{ 0x0a, 8 },	{ 0x09, 8 },
180 	{ 0x08, 8 },	{ 0x07, 8 },	{ 0x06, 8 },	{ 0x17, 10 },
181 	{ 0x16, 10 },	{ 0x15, 10 },	{ 0x14, 10 },	{ 0x13, 10 },
182 	{ 0x12, 10 },	{ 0x23, 11 },	{ 0x22, 11 },	{ 0x21, 11 },
183 	{ 0x20, 11 },	{ 0x1f, 11 },	{ 0x1e, 11 },	{ 0x1d, 11 },
184 	{ 0x1c, 11 },	{ 0x1b, 11 },	{ 0x1a, 11 },	{ 0x19, 11 },
185 	{ 0x18, 11 }
186 };
187 
188 /* Standard JPEG tables */
189 
190 static const u8 default_intra_quant_table[] = {
191 	 8, 16, 19, 22, 26, 27, 29, 34,
192 	16, 16, 22, 24, 27, 29, 34, 37,
193 	19, 22, 26, 27, 29, 34, 34, 38,
194 	22, 22, 26, 27, 29, 34, 37, 40,
195 	22, 26, 27, 29, 32, 35, 40, 48,
196 	26, 27, 29, 32, 35, 40, 48, 58,
197 	26, 27, 29, 34, 38, 46, 56, 69,
198 	27, 29, 35, 38, 46, 56, 69, 83
199 };
200 
201 static const u8 bits_dc_luminance[] = {
202 	0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
203 };
204 
205 static const u8 val_dc_luminance[] = {
206 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
207 };
208 
209 static const u8 bits_dc_chrominance[] = {
210 	0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
211 };
212 
213 static const u8 val_dc_chrominance[] = {
214 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
215 };
216 
217 static const u8 bits_ac_luminance[] = {
218 	0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
219 };
220 
221 static const u8 val_ac_luminance[] = {
222 	0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
223 	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
224 	0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
225 	0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
226 	0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
227 	0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
228 	0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
229 	0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
230 	0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
231 	0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
232 	0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
233 	0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
234 	0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
235 	0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
236 	0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
237 	0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
238 	0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
239 	0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
240 	0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
241 	0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
242 	0xf9, 0xfa
243 };
244 
245 static const u8 bits_ac_chrominance[] = {
246 	0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
247 };
248 
249 static const u8 val_ac_chrominance[] = {
250 	0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
251 	0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
252 	0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
253 	0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
254 	0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
255 	0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
256 	0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
257 	0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
258 	0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
259 	0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
260 	0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
261 	0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
262 	0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
263 	0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
264 	0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
265 	0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
266 	0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
267 	0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
268 	0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
269 	0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
270 	0xf9, 0xfa
271 };
272 
273 /* Zig-zag mapping for quant table
274  *
275  * OK, let's do this mapping on the actual table above so it doesn't have
276  * to be done on the fly.
277  */
278 static const int zz[64] = {
279 	0,   1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
280 	12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
281 	35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
282 	58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
283 };
284 
285 static int copy_packages(__le16 *dest, u16 *src, int pkg_cnt, int space)
286 {
287 	int i, cnt = pkg_cnt * 32;
288 
289 	if (space < cnt)
290 		return -1;
291 
292 	for (i = 0; i < cnt; ++i)
293 		dest[i] = cpu_to_le16p(src + i);
294 
295 	return cnt;
296 }
297 
298 static int mjpeg_frame_header(struct go7007 *go, unsigned char *buf, int q)
299 {
300 	int i, p = 0;
301 
302 	buf[p++] = 0xff;
303 	buf[p++] = 0xd8;
304 	buf[p++] = 0xff;
305 	buf[p++] = 0xdb;
306 	buf[p++] = 0;
307 	buf[p++] = 2 + 65;
308 	buf[p++] = 0;
309 	buf[p++] = default_intra_quant_table[0];
310 	for (i = 1; i < 64; ++i)
311 		/* buf[p++] = (default_intra_quant_table[i] * q) >> 3; */
312 		buf[p++] = (default_intra_quant_table[zz[i]] * q) >> 3;
313 	buf[p++] = 0xff;
314 	buf[p++] = 0xc0;
315 	buf[p++] = 0;
316 	buf[p++] = 17;
317 	buf[p++] = 8;
318 	buf[p++] = go->height >> 8;
319 	buf[p++] = go->height & 0xff;
320 	buf[p++] = go->width >> 8;
321 	buf[p++] = go->width & 0xff;
322 	buf[p++] = 3;
323 	buf[p++] = 1;
324 	buf[p++] = 0x22;
325 	buf[p++] = 0;
326 	buf[p++] = 2;
327 	buf[p++] = 0x11;
328 	buf[p++] = 0;
329 	buf[p++] = 3;
330 	buf[p++] = 0x11;
331 	buf[p++] = 0;
332 	buf[p++] = 0xff;
333 	buf[p++] = 0xc4;
334 	buf[p++] = 418 >> 8;
335 	buf[p++] = 418 & 0xff;
336 	buf[p++] = 0x00;
337 	memcpy(buf + p, bits_dc_luminance + 1, 16);
338 	p += 16;
339 	memcpy(buf + p, val_dc_luminance, sizeof(val_dc_luminance));
340 	p += sizeof(val_dc_luminance);
341 	buf[p++] = 0x01;
342 	memcpy(buf + p, bits_dc_chrominance + 1, 16);
343 	p += 16;
344 	memcpy(buf + p, val_dc_chrominance, sizeof(val_dc_chrominance));
345 	p += sizeof(val_dc_chrominance);
346 	buf[p++] = 0x10;
347 	memcpy(buf + p, bits_ac_luminance + 1, 16);
348 	p += 16;
349 	memcpy(buf + p, val_ac_luminance, sizeof(val_ac_luminance));
350 	p += sizeof(val_ac_luminance);
351 	buf[p++] = 0x11;
352 	memcpy(buf + p, bits_ac_chrominance + 1, 16);
353 	p += 16;
354 	memcpy(buf + p, val_ac_chrominance, sizeof(val_ac_chrominance));
355 	p += sizeof(val_ac_chrominance);
356 	buf[p++] = 0xff;
357 	buf[p++] = 0xda;
358 	buf[p++] = 0;
359 	buf[p++] = 12;
360 	buf[p++] = 3;
361 	buf[p++] = 1;
362 	buf[p++] = 0x00;
363 	buf[p++] = 2;
364 	buf[p++] = 0x11;
365 	buf[p++] = 3;
366 	buf[p++] = 0x11;
367 	buf[p++] = 0;
368 	buf[p++] = 63;
369 	buf[p++] = 0;
370 	return p;
371 }
372 
373 static int gen_mjpeghdr_to_package(struct go7007 *go, __le16 *code, int space)
374 {
375 	u8 *buf;
376 	u16 mem = 0x3e00;
377 	unsigned int addr = 0x19;
378 	int size = 0, i, off = 0, chunk;
379 
380 	buf = kzalloc(4096, GFP_KERNEL);
381 	if (buf == NULL)
382 		return -ENOMEM;
383 
384 	for (i = 1; i < 32; ++i) {
385 		mjpeg_frame_header(go, buf + size, i);
386 		size += 80;
387 	}
388 	chunk = mjpeg_frame_header(go, buf + size, 1);
389 	memmove(buf + size, buf + size + 80, chunk - 80);
390 	size += chunk - 80;
391 
392 	for (i = 0; i < size; i += chunk * 2) {
393 		if (space - off < 32) {
394 			off = -1;
395 			goto done;
396 		}
397 
398 		code[off + 1] = __cpu_to_le16(0x8000 | mem);
399 
400 		chunk = 28;
401 		if (mem + chunk > 0x4000)
402 			chunk = 0x4000 - mem;
403 		if (i + 2 * chunk > size)
404 			chunk = (size - i) / 2;
405 
406 		if (chunk < 28) {
407 			code[off] = __cpu_to_le16(0x4000 | chunk);
408 			code[off + 31] = __cpu_to_le16(addr++);
409 			mem = 0x3e00;
410 		} else {
411 			code[off] = __cpu_to_le16(0x1000 | 28);
412 			code[off + 31] = 0;
413 			mem += 28;
414 		}
415 
416 		memcpy(&code[off + 2], buf + i, chunk * 2);
417 		off += 32;
418 	}
419 done:
420 	kfree(buf);
421 	return off;
422 }
423 
424 static int mpeg1_frame_header(struct go7007 *go, unsigned char *buf,
425 		int modulo, int pict_struct, enum mpeg_frame_type frame)
426 {
427 	int i, j, mb_code, mb_len;
428 	int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
429 	CODE_GEN(c, buf + 6);
430 
431 	switch (frame) {
432 	case PFRAME:
433 		mb_code = 0x1;
434 		mb_len = 3;
435 		break;
436 	case BFRAME_PRE:
437 		mb_code = 0x2;
438 		mb_len = 4;
439 		break;
440 	case BFRAME_POST:
441 		mb_code = 0x2;
442 		mb_len = 3;
443 		break;
444 	case BFRAME_BIDIR:
445 		mb_code = 0x2;
446 		mb_len = 2;
447 		break;
448 	default: /* keep the compiler happy */
449 		mb_code = mb_len = 0;
450 		break;
451 	}
452 
453 	CODE_ADD(c, frame == PFRAME ? 0x2 : 0x3, 13);
454 	CODE_ADD(c, 0xffff, 16);
455 	CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 0x7 : 0x4, 4);
456 	if (frame != PFRAME)
457 		CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 0x7 : 0x4, 4);
458 	else
459 		CODE_ADD(c, 0, 4); /* Is this supposed to be here?? */
460 	CODE_ADD(c, 0, 3); /* What is this?? */
461 	/* Byte-align with zeros */
462 	j = 8 - (CODE_LENGTH(c) % 8);
463 	if (j != 8)
464 		CODE_ADD(c, 0, j);
465 
466 	if (go->format == V4L2_PIX_FMT_MPEG2) {
467 		CODE_ADD(c, 0x1, 24);
468 		CODE_ADD(c, 0xb5, 8);
469 		CODE_ADD(c, 0x844, 12);
470 		CODE_ADD(c, frame == PFRAME ? 0xff : 0x44, 8);
471 		if (go->interlace_coding) {
472 			CODE_ADD(c, pict_struct, 4);
473 			if (go->dvd_mode)
474 				CODE_ADD(c, 0x000, 11);
475 			else
476 				CODE_ADD(c, 0x200, 11);
477 		} else {
478 			CODE_ADD(c, 0x3, 4);
479 			CODE_ADD(c, 0x20c, 11);
480 		}
481 		/* Byte-align with zeros */
482 		j = 8 - (CODE_LENGTH(c) % 8);
483 		if (j != 8)
484 			CODE_ADD(c, 0, j);
485 	}
486 
487 	for (i = 0; i < rows; ++i) {
488 		CODE_ADD(c, 1, 24);
489 		CODE_ADD(c, i + 1, 8);
490 		CODE_ADD(c, 0x2, 6);
491 		CODE_ADD(c, 0x1, 1);
492 		CODE_ADD(c, mb_code, mb_len);
493 		if (go->interlace_coding) {
494 			CODE_ADD(c, 0x1, 2);
495 			CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
496 		}
497 		if (frame == BFRAME_BIDIR) {
498 			CODE_ADD(c, 0x3, 2);
499 			if (go->interlace_coding)
500 				CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
501 		}
502 		CODE_ADD(c, 0x3, 2);
503 		for (j = (go->width >> 4) - 2; j >= 33; j -= 33)
504 			CODE_ADD(c, 0x8, 11);
505 		CODE_ADD(c, addrinctab[j][0], addrinctab[j][1]);
506 		CODE_ADD(c, mb_code, mb_len);
507 		if (go->interlace_coding) {
508 			CODE_ADD(c, 0x1, 2);
509 			CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
510 		}
511 		if (frame == BFRAME_BIDIR) {
512 			CODE_ADD(c, 0x3, 2);
513 			if (go->interlace_coding)
514 				CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
515 		}
516 		CODE_ADD(c, 0x3, 2);
517 
518 		/* Byte-align with zeros */
519 		j = 8 - (CODE_LENGTH(c) % 8);
520 		if (j != 8)
521 			CODE_ADD(c, 0, j);
522 	}
523 
524 	i = CODE_LENGTH(c) + 4 * 8;
525 	buf[2] = 0x00;
526 	buf[3] = 0x00;
527 	buf[4] = 0x01;
528 	buf[5] = 0x00;
529 	return i;
530 }
531 
532 static int mpeg1_sequence_header(struct go7007 *go, unsigned char *buf, int ext)
533 {
534 	int i, aspect_ratio, picture_rate;
535 	CODE_GEN(c, buf + 6);
536 
537 	if (go->format == V4L2_PIX_FMT_MPEG1) {
538 		switch (go->aspect_ratio) {
539 		case GO7007_RATIO_4_3:
540 			aspect_ratio = go->standard == GO7007_STD_NTSC ? 3 : 2;
541 			break;
542 		case GO7007_RATIO_16_9:
543 			aspect_ratio = go->standard == GO7007_STD_NTSC ? 5 : 4;
544 			break;
545 		default:
546 			aspect_ratio = 1;
547 			break;
548 		}
549 	} else {
550 		switch (go->aspect_ratio) {
551 		case GO7007_RATIO_4_3:
552 			aspect_ratio = 2;
553 			break;
554 		case GO7007_RATIO_16_9:
555 			aspect_ratio = 3;
556 			break;
557 		default:
558 			aspect_ratio = 1;
559 			break;
560 		}
561 	}
562 	switch (go->sensor_framerate) {
563 	case 24000:
564 		picture_rate = 1;
565 		break;
566 	case 24024:
567 		picture_rate = 2;
568 		break;
569 	case 25025:
570 		picture_rate = go->interlace_coding ? 6 : 3;
571 		break;
572 	case 30000:
573 		picture_rate = go->interlace_coding ? 7 : 4;
574 		break;
575 	case 30030:
576 		picture_rate = go->interlace_coding ? 8 : 5;
577 		break;
578 	default:
579 		picture_rate = 5; /* 30 fps seems like a reasonable default */
580 		break;
581 	}
582 
583 	CODE_ADD(c, go->width, 12);
584 	CODE_ADD(c, go->height, 12);
585 	CODE_ADD(c, aspect_ratio, 4);
586 	CODE_ADD(c, picture_rate, 4);
587 	CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 20000 : 0x3ffff, 18);
588 	CODE_ADD(c, 1, 1);
589 	CODE_ADD(c, go->format == V4L2_PIX_FMT_MPEG2 ? 112 : 20, 10);
590 	CODE_ADD(c, 0, 3);
591 
592 	/* Byte-align with zeros */
593 	i = 8 - (CODE_LENGTH(c) % 8);
594 	if (i != 8)
595 		CODE_ADD(c, 0, i);
596 
597 	if (go->format == V4L2_PIX_FMT_MPEG2) {
598 		CODE_ADD(c, 0x1, 24);
599 		CODE_ADD(c, 0xb5, 8);
600 		CODE_ADD(c, 0x148, 12);
601 		if (go->interlace_coding)
602 			CODE_ADD(c, 0x20001, 20);
603 		else
604 			CODE_ADD(c, 0xa0001, 20);
605 		CODE_ADD(c, 0, 16);
606 
607 		/* Byte-align with zeros */
608 		i = 8 - (CODE_LENGTH(c) % 8);
609 		if (i != 8)
610 			CODE_ADD(c, 0, i);
611 
612 		if (ext) {
613 			CODE_ADD(c, 0x1, 24);
614 			CODE_ADD(c, 0xb52, 12);
615 			CODE_ADD(c, go->standard == GO7007_STD_NTSC ? 2 : 1, 3);
616 			CODE_ADD(c, 0x105, 9);
617 			CODE_ADD(c, 0x505, 16);
618 			CODE_ADD(c, go->width, 14);
619 			CODE_ADD(c, 1, 1);
620 			CODE_ADD(c, go->height, 14);
621 
622 			/* Byte-align with zeros */
623 			i = 8 - (CODE_LENGTH(c) % 8);
624 			if (i != 8)
625 				CODE_ADD(c, 0, i);
626 		}
627 	}
628 
629 	i = CODE_LENGTH(c) + 4 * 8;
630 	buf[0] = i & 0xff;
631 	buf[1] = i >> 8;
632 	buf[2] = 0x00;
633 	buf[3] = 0x00;
634 	buf[4] = 0x01;
635 	buf[5] = 0xb3;
636 	return i;
637 }
638 
639 static int gen_mpeg1hdr_to_package(struct go7007 *go,
640 					__le16 *code, int space, int *framelen)
641 {
642 	u8 *buf;
643 	u16 mem = 0x3e00;
644 	unsigned int addr = 0x19;
645 	int i, off = 0, chunk;
646 
647 	buf = kzalloc(5120, GFP_KERNEL);
648 	if (buf == NULL)
649 		return -ENOMEM;
650 
651 	framelen[0] = mpeg1_frame_header(go, buf, 0, 1, PFRAME);
652 	if (go->interlace_coding)
653 		framelen[0] += mpeg1_frame_header(go, buf + framelen[0] / 8,
654 							0, 2, PFRAME);
655 	buf[0] = framelen[0] & 0xff;
656 	buf[1] = framelen[0] >> 8;
657 	i = 368;
658 	framelen[1] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_PRE);
659 	if (go->interlace_coding)
660 		framelen[1] += mpeg1_frame_header(go, buf + i + framelen[1] / 8,
661 							0, 2, BFRAME_PRE);
662 	buf[i] = framelen[1] & 0xff;
663 	buf[i + 1] = framelen[1] >> 8;
664 	i += 1632;
665 	framelen[2] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_POST);
666 	if (go->interlace_coding)
667 		framelen[2] += mpeg1_frame_header(go, buf + i + framelen[2] / 8,
668 							0, 2, BFRAME_POST);
669 	buf[i] = framelen[2] & 0xff;
670 	buf[i + 1] = framelen[2] >> 8;
671 	i += 1432;
672 	framelen[3] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_BIDIR);
673 	if (go->interlace_coding)
674 		framelen[3] += mpeg1_frame_header(go, buf + i + framelen[3] / 8,
675 							0, 2, BFRAME_BIDIR);
676 	buf[i] = framelen[3] & 0xff;
677 	buf[i + 1] = framelen[3] >> 8;
678 	i += 1632 + 16;
679 	mpeg1_sequence_header(go, buf + i, 0);
680 	i += 40;
681 	for (i = 0; i < 5120; i += chunk * 2) {
682 		if (space - off < 32) {
683 			off = -1;
684 			goto done;
685 		}
686 
687 		code[off + 1] = __cpu_to_le16(0x8000 | mem);
688 
689 		chunk = 28;
690 		if (mem + chunk > 0x4000)
691 			chunk = 0x4000 - mem;
692 		if (i + 2 * chunk > 5120)
693 			chunk = (5120 - i) / 2;
694 
695 		if (chunk < 28) {
696 			code[off] = __cpu_to_le16(0x4000 | chunk);
697 			code[off + 31] = __cpu_to_le16(addr);
698 			if (mem + chunk == 0x4000) {
699 				mem = 0x3e00;
700 				++addr;
701 			}
702 		} else {
703 			code[off] = __cpu_to_le16(0x1000 | 28);
704 			code[off + 31] = 0;
705 			mem += 28;
706 		}
707 
708 		memcpy(&code[off + 2], buf + i, chunk * 2);
709 		off += 32;
710 	}
711 done:
712 	kfree(buf);
713 	return off;
714 }
715 
716 static int vti_bitlen(struct go7007 *go)
717 {
718 	unsigned int i, max_time_incr = go->sensor_framerate / go->fps_scale;
719 
720 	for (i = 31; (max_time_incr & ((1 << i) - 1)) == max_time_incr; --i)
721 		;
722 	return i + 1;
723 }
724 
725 static int mpeg4_frame_header(struct go7007 *go, unsigned char *buf,
726 		int modulo, enum mpeg_frame_type frame)
727 {
728 	int i;
729 	CODE_GEN(c, buf + 6);
730 	int mb_count = (go->width >> 4) * (go->height >> 4);
731 
732 	CODE_ADD(c, frame == PFRAME ? 0x1 : 0x2, 2);
733 	if (modulo)
734 		CODE_ADD(c, 0x1, 1);
735 	CODE_ADD(c, 0x1, 2);
736 	CODE_ADD(c, 0, vti_bitlen(go));
737 	CODE_ADD(c, 0x3, 2);
738 	if (frame == PFRAME)
739 		CODE_ADD(c, 0, 1);
740 	CODE_ADD(c, 0xc, 11);
741 	if (frame != PFRAME)
742 		CODE_ADD(c, 0x4, 3);
743 	if (frame != BFRAME_EMPTY) {
744 		for (i = 0; i < mb_count; ++i) {
745 			switch (frame) {
746 			case PFRAME:
747 				CODE_ADD(c, 0x1, 1);
748 				break;
749 			case BFRAME_PRE:
750 				CODE_ADD(c, 0x47, 8);
751 				break;
752 			case BFRAME_POST:
753 				CODE_ADD(c, 0x27, 7);
754 				break;
755 			case BFRAME_BIDIR:
756 				CODE_ADD(c, 0x5f, 8);
757 				break;
758 			case BFRAME_EMPTY: /* keep compiler quiet */
759 				break;
760 			}
761 		}
762 	}
763 
764 	/* Byte-align with a zero followed by ones */
765 	i = 8 - (CODE_LENGTH(c) % 8);
766 	CODE_ADD(c, 0, 1);
767 	CODE_ADD(c, (1 << (i - 1)) - 1, i - 1);
768 
769 	i = CODE_LENGTH(c) + 4 * 8;
770 	buf[0] = i & 0xff;
771 	buf[1] = i >> 8;
772 	buf[2] = 0x00;
773 	buf[3] = 0x00;
774 	buf[4] = 0x01;
775 	buf[5] = 0xb6;
776 	return i;
777 }
778 
779 static int mpeg4_sequence_header(struct go7007 *go, unsigned char *buf, int ext)
780 {
781 	const unsigned char head[] = { 0x00, 0x00, 0x01, 0xb0, go->pali,
782 		0x00, 0x00, 0x01, 0xb5, 0x09,
783 		0x00, 0x00, 0x01, 0x00,
784 		0x00, 0x00, 0x01, 0x20, };
785 	int i, aspect_ratio;
786 	int fps = go->sensor_framerate / go->fps_scale;
787 	CODE_GEN(c, buf + 2 + sizeof(head));
788 
789 	switch (go->aspect_ratio) {
790 	case GO7007_RATIO_4_3:
791 		aspect_ratio = go->standard == GO7007_STD_NTSC ? 3 : 2;
792 		break;
793 	case GO7007_RATIO_16_9:
794 		aspect_ratio = go->standard == GO7007_STD_NTSC ? 5 : 4;
795 		break;
796 	default:
797 		aspect_ratio = 1;
798 		break;
799 	}
800 
801 	memcpy(buf + 2, head, sizeof(head));
802 	CODE_ADD(c, 0x191, 17);
803 	CODE_ADD(c, aspect_ratio, 4);
804 	CODE_ADD(c, 0x1, 4);
805 	CODE_ADD(c, fps, 16);
806 	CODE_ADD(c, 0x3, 2);
807 	CODE_ADD(c, 1001, vti_bitlen(go));
808 	CODE_ADD(c, 1, 1);
809 	CODE_ADD(c, go->width, 13);
810 	CODE_ADD(c, 1, 1);
811 	CODE_ADD(c, go->height, 13);
812 	CODE_ADD(c, 0x2830, 14);
813 
814 	/* Byte-align */
815 	i = 8 - (CODE_LENGTH(c) % 8);
816 	CODE_ADD(c, 0, 1);
817 	CODE_ADD(c, (1 << (i - 1)) - 1, i - 1);
818 
819 	i = CODE_LENGTH(c) + sizeof(head) * 8;
820 	buf[0] = i & 0xff;
821 	buf[1] = i >> 8;
822 	return i;
823 }
824 
825 static int gen_mpeg4hdr_to_package(struct go7007 *go,
826 					__le16 *code, int space, int *framelen)
827 {
828 	u8 *buf;
829 	u16 mem = 0x3e00;
830 	unsigned int addr = 0x19;
831 	int i, off = 0, chunk;
832 
833 	buf = kzalloc(5120, GFP_KERNEL);
834 	if (buf == NULL)
835 		return -ENOMEM;
836 
837 	framelen[0] = mpeg4_frame_header(go, buf, 0, PFRAME);
838 	i = 368;
839 	framelen[1] = mpeg4_frame_header(go, buf + i, 0, BFRAME_PRE);
840 	i += 1632;
841 	framelen[2] = mpeg4_frame_header(go, buf + i, 0, BFRAME_POST);
842 	i += 1432;
843 	framelen[3] = mpeg4_frame_header(go, buf + i, 0, BFRAME_BIDIR);
844 	i += 1632;
845 	mpeg4_frame_header(go, buf + i, 0, BFRAME_EMPTY);
846 	i += 16;
847 	mpeg4_sequence_header(go, buf + i, 0);
848 	i += 40;
849 	for (i = 0; i < 5120; i += chunk * 2) {
850 		if (space - off < 32) {
851 			off = -1;
852 			goto done;
853 		}
854 
855 		code[off + 1] = __cpu_to_le16(0x8000 | mem);
856 
857 		chunk = 28;
858 		if (mem + chunk > 0x4000)
859 			chunk = 0x4000 - mem;
860 		if (i + 2 * chunk > 5120)
861 			chunk = (5120 - i) / 2;
862 
863 		if (chunk < 28) {
864 			code[off] = __cpu_to_le16(0x4000 | chunk);
865 			code[off + 31] = __cpu_to_le16(addr);
866 			if (mem + chunk == 0x4000) {
867 				mem = 0x3e00;
868 				++addr;
869 			}
870 		} else {
871 			code[off] = __cpu_to_le16(0x1000 | 28);
872 			code[off + 31] = 0;
873 			mem += 28;
874 		}
875 
876 		memcpy(&code[off + 2], buf + i, chunk * 2);
877 		off += 32;
878 	}
879 	mem = 0x3e00;
880 	addr = go->ipb ? 0x14f9 : 0x0af9;
881 	memset(buf, 0, 5120);
882 	framelen[4] = mpeg4_frame_header(go, buf, 1, PFRAME);
883 	i = 368;
884 	framelen[5] = mpeg4_frame_header(go, buf + i, 1, BFRAME_PRE);
885 	i += 1632;
886 	framelen[6] = mpeg4_frame_header(go, buf + i, 1, BFRAME_POST);
887 	i += 1432;
888 	framelen[7] = mpeg4_frame_header(go, buf + i, 1, BFRAME_BIDIR);
889 	i += 1632;
890 	mpeg4_frame_header(go, buf + i, 1, BFRAME_EMPTY);
891 	i += 16;
892 	for (i = 0; i < 5120; i += chunk * 2) {
893 		if (space - off < 32) {
894 			off = -1;
895 			goto done;
896 		}
897 
898 		code[off + 1] = __cpu_to_le16(0x8000 | mem);
899 
900 		chunk = 28;
901 		if (mem + chunk > 0x4000)
902 			chunk = 0x4000 - mem;
903 		if (i + 2 * chunk > 5120)
904 			chunk = (5120 - i) / 2;
905 
906 		if (chunk < 28) {
907 			code[off] = __cpu_to_le16(0x4000 | chunk);
908 			code[off + 31] = __cpu_to_le16(addr);
909 			if (mem + chunk == 0x4000) {
910 				mem = 0x3e00;
911 				++addr;
912 			}
913 		} else {
914 			code[off] = __cpu_to_le16(0x1000 | 28);
915 			code[off + 31] = 0;
916 			mem += 28;
917 		}
918 
919 		memcpy(&code[off + 2], buf + i, chunk * 2);
920 		off += 32;
921 	}
922 done:
923 	kfree(buf);
924 	return off;
925 }
926 
927 static int brctrl_to_package(struct go7007 *go,
928 					__le16 *code, int space, int *framelen)
929 {
930 	int converge_speed = 0;
931 	int lambda = (go->format == V4L2_PIX_FMT_MJPEG || go->dvd_mode) ?
932 				100 : 0;
933 	int peak_rate = 6 * go->bitrate / 5;
934 	int vbv_buffer = go->format == V4L2_PIX_FMT_MJPEG ?
935 				go->bitrate :
936 				(go->dvd_mode ? 900000 : peak_rate);
937 	int fps = go->sensor_framerate / go->fps_scale;
938 	int q = 0;
939 	/* Bizarre math below depends on rounding errors in division */
940 	u32 sgop_expt_addr = go->bitrate / 32 * (go->ipb ? 3 : 1) * 1001 / fps;
941 	u32 sgop_peak_addr = peak_rate / 32 * 1001 / fps;
942 	u32 total_expt_addr = go->bitrate / 32 * 1000 / fps * (fps / 1000);
943 	u32 vbv_alert_addr = vbv_buffer * 3 / (4 * 32);
944 	u32 cplx[] = {
945 		q > 0 ? sgop_expt_addr * q :
946 			2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
947 		q > 0 ? sgop_expt_addr * q :
948 			2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
949 		q > 0 ? sgop_expt_addr * q :
950 			2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
951 		q > 0 ? sgop_expt_addr * q :
952 			2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
953 	};
954 	u32 calc_q = q > 0 ? q : cplx[0] / sgop_expt_addr;
955 	u16 pack[] = {
956 		0x200e,		0x0000,
957 		0xBF20,		go->ipb ? converge_speed_ipb[converge_speed]
958 					: converge_speed_ip[converge_speed],
959 		0xBF21,		go->ipb ? 2 : 0,
960 		0xBF22,		go->ipb ? LAMBDA_table[0][lambda / 2 + 50]
961 					: 32767,
962 		0xBF23,		go->ipb ? LAMBDA_table[1][lambda] : 32767,
963 		0xBF24,		32767,
964 		0xBF25,		lambda > 99 ? 32767 : LAMBDA_table[3][lambda],
965 		0xBF26,		sgop_expt_addr & 0x0000FFFF,
966 		0xBF27,		sgop_expt_addr >> 16,
967 		0xBF28,		sgop_peak_addr & 0x0000FFFF,
968 		0xBF29,		sgop_peak_addr >> 16,
969 		0xBF2A,		vbv_alert_addr & 0x0000FFFF,
970 		0xBF2B,		vbv_alert_addr >> 16,
971 		0xBF2C,		0,
972 		0xBF2D,		0,
973 		0,		0,
974 
975 		0x200e,		0x0000,
976 		0xBF2E,		vbv_alert_addr & 0x0000FFFF,
977 		0xBF2F,		vbv_alert_addr >> 16,
978 		0xBF30,		cplx[0] & 0x0000FFFF,
979 		0xBF31,		cplx[0] >> 16,
980 		0xBF32,		cplx[1] & 0x0000FFFF,
981 		0xBF33,		cplx[1] >> 16,
982 		0xBF34,		cplx[2] & 0x0000FFFF,
983 		0xBF35,		cplx[2] >> 16,
984 		0xBF36,		cplx[3] & 0x0000FFFF,
985 		0xBF37,		cplx[3] >> 16,
986 		0xBF38,		0,
987 		0xBF39,		0,
988 		0xBF3A,		total_expt_addr & 0x0000FFFF,
989 		0xBF3B,		total_expt_addr >> 16,
990 		0,		0,
991 
992 		0x200e,		0x0000,
993 		0xBF3C,		total_expt_addr & 0x0000FFFF,
994 		0xBF3D,		total_expt_addr >> 16,
995 		0xBF3E,		0,
996 		0xBF3F,		0,
997 		0xBF48,		0,
998 		0xBF49,		0,
999 		0xBF4A,		calc_q < 4 ? 4 : (calc_q > 124 ? 124 : calc_q),
1000 		0xBF4B,		4,
1001 		0xBF4C,		0,
1002 		0xBF4D,		0,
1003 		0xBF4E,		0,
1004 		0xBF4F,		0,
1005 		0xBF50,		0,
1006 		0xBF51,		0,
1007 		0,		0,
1008 
1009 		0x200e,		0x0000,
1010 		0xBF40,		sgop_expt_addr & 0x0000FFFF,
1011 		0xBF41,		sgop_expt_addr >> 16,
1012 		0xBF42,		0,
1013 		0xBF43,		0,
1014 		0xBF44,		0,
1015 		0xBF45,		0,
1016 		0xBF46,		(go->width >> 4) * (go->height >> 4),
1017 		0xBF47,		0,
1018 		0xBF64,		0,
1019 		0xBF65,		0,
1020 		0xBF18,		framelen[4],
1021 		0xBF19,		framelen[5],
1022 		0xBF1A,		framelen[6],
1023 		0xBF1B,		framelen[7],
1024 		0,		0,
1025 
1026 #if 0
1027 		/* Remove once we don't care about matching */
1028 		0x200e,		0x0000,
1029 		0xBF56,		4,
1030 		0xBF57,		0,
1031 		0xBF58,		5,
1032 		0xBF59,		0,
1033 		0xBF5A,		6,
1034 		0xBF5B,		0,
1035 		0xBF5C,		8,
1036 		0xBF5D,		0,
1037 		0xBF5E,		1,
1038 		0xBF5F,		0,
1039 		0xBF60,		1,
1040 		0xBF61,		0,
1041 		0xBF62,		0,
1042 		0xBF63,		0,
1043 		0,		0,
1044 #else
1045 		0x2008,		0x0000,
1046 		0xBF56,		4,
1047 		0xBF57,		0,
1048 		0xBF58,		5,
1049 		0xBF59,		0,
1050 		0xBF5A,		6,
1051 		0xBF5B,		0,
1052 		0xBF5C,		8,
1053 		0xBF5D,		0,
1054 		0,		0,
1055 		0,		0,
1056 		0,		0,
1057 		0,		0,
1058 		0,		0,
1059 		0,		0,
1060 		0,		0,
1061 #endif
1062 
1063 		0x200e,		0x0000,
1064 		0xBF10,		0,
1065 		0xBF11,		0,
1066 		0xBF12,		0,
1067 		0xBF13,		0,
1068 		0xBF14,		0,
1069 		0xBF15,		0,
1070 		0xBF16,		0,
1071 		0xBF17,		0,
1072 		0xBF7E,		0,
1073 		0xBF7F,		1,
1074 		0xBF52,		framelen[0],
1075 		0xBF53,		framelen[1],
1076 		0xBF54,		framelen[2],
1077 		0xBF55,		framelen[3],
1078 		0,		0,
1079 	};
1080 
1081 	return copy_packages(code, pack, 6, space);
1082 }
1083 
1084 static int config_package(struct go7007 *go, __le16 *code, int space)
1085 {
1086 	int fps = go->sensor_framerate / go->fps_scale / 1000;
1087 	int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
1088 	int brc_window_size = fps;
1089 	int q_min = 2, q_max = 31;
1090 	int THACCoeffSet0 = 0;
1091 	u16 pack[] = {
1092 		0x200e,		0x0000,
1093 		0xc002,		0x14b4,
1094 		0xc003,		0x28b4,
1095 		0xc004,		0x3c5a,
1096 		0xdc05,		0x2a77,
1097 		0xc6c3,		go->format == V4L2_PIX_FMT_MPEG4 ? 0 :
1098 				(go->format == V4L2_PIX_FMT_H263 ? 0 : 1),
1099 		0xc680,		go->format == V4L2_PIX_FMT_MPEG4 ? 0xf1 :
1100 				(go->format == V4L2_PIX_FMT_H263 ? 0x61 :
1101 									0xd3),
1102 		0xc780,		0x0140,
1103 		0xe009,		0x0001,
1104 		0xc60f,		0x0008,
1105 		0xd4ff,		0x0002,
1106 		0xe403,		2340,
1107 		0xe406,		75,
1108 		0xd411,		0x0001,
1109 		0xd410,		0xa1d6,
1110 		0x0001,		0x2801,
1111 
1112 		0x200d,		0x0000,
1113 		0xe402,		0x018b,
1114 		0xe401,		0x8b01,
1115 		0xd472,		(go->board_info->sensor_flags &
1116 							GO7007_SENSOR_TV) &&
1117 						(!go->interlace_coding) ?
1118 					0x01b0 : 0x0170,
1119 		0xd475,		(go->board_info->sensor_flags &
1120 							GO7007_SENSOR_TV) &&
1121 						(!go->interlace_coding) ?
1122 					0x0008 : 0x0009,
1123 		0xc404,		go->interlace_coding ? 0x44 :
1124 				(go->format == V4L2_PIX_FMT_MPEG4 ? 0x11 :
1125 				(go->format == V4L2_PIX_FMT_MPEG1 ? 0x02 :
1126 				(go->format == V4L2_PIX_FMT_MPEG2 ? 0x04 :
1127 				(go->format == V4L2_PIX_FMT_H263  ? 0x08 :
1128 								     0x20)))),
1129 		0xbf0a,		(go->format == V4L2_PIX_FMT_MPEG4 ? 8 :
1130 				(go->format == V4L2_PIX_FMT_MPEG1 ? 1 :
1131 				(go->format == V4L2_PIX_FMT_MPEG2 ? 2 :
1132 				(go->format == V4L2_PIX_FMT_H263 ? 4 : 16)))) |
1133 				((go->repeat_seqhead ? 1 : 0) << 6) |
1134 				((go->dvd_mode ? 1 : 0) << 9) |
1135 				((go->gop_header_enable ? 1 : 0) << 10),
1136 		0xbf0b,		0,
1137 		0xdd5a,		go->ipb ? 0x14 : 0x0a,
1138 		0xbf0c,		0,
1139 		0xbf0d,		0,
1140 		0xc683,		THACCoeffSet0,
1141 		0xc40a,		(go->width << 4) | rows,
1142 		0xe01a,		go->board_info->hpi_buffer_cap,
1143 		0,		0,
1144 		0,		0,
1145 
1146 		0x2008,		0,
1147 		0xe402,		0x88,
1148 		0xe401,		0x8f01,
1149 		0xbf6a,		0,
1150 		0xbf6b,		0,
1151 		0xbf6c,		0,
1152 		0xbf6d,		0,
1153 		0xbf6e,		0,
1154 		0xbf6f,		0,
1155 		0,		0,
1156 		0,		0,
1157 		0,		0,
1158 		0,		0,
1159 		0,		0,
1160 		0,		0,
1161 		0,		0,
1162 
1163 		0x200e,		0,
1164 		0xbf66,		brc_window_size,
1165 		0xbf67,		0,
1166 		0xbf68,		q_min,
1167 		0xbf69,		q_max,
1168 		0xbfe0,		0,
1169 		0xbfe1,		0,
1170 		0xbfe2,		0,
1171 		0xbfe3,		go->ipb ? 3 : 1,
1172 		0xc031,		go->board_info->sensor_flags &
1173 					GO7007_SENSOR_VBI ? 1 : 0,
1174 		0xc01c,		0x1f,
1175 		0xdd8c,		0x15,
1176 		0xdd94,		0x15,
1177 		0xdd88,		go->ipb ? 0x1401 : 0x0a01,
1178 		0xdd90,		go->ipb ? 0x1401 : 0x0a01,
1179 		0,		0,
1180 
1181 		0x200e,		0,
1182 		0xbfe4,		0,
1183 		0xbfe5,		0,
1184 		0xbfe6,		0,
1185 		0xbfe7,		fps << 8,
1186 		0xbfe8,		0x3a00,
1187 		0xbfe9,		0,
1188 		0xbfea,		0,
1189 		0xbfeb,		0,
1190 		0xbfec,		(go->interlace_coding ? 1 << 15 : 0) |
1191 					(go->modet_enable ? 0xa : 0) |
1192 					(go->board_info->sensor_flags &
1193 						GO7007_SENSOR_VBI ? 1 : 0),
1194 		0xbfed,		0,
1195 		0xbfee,		0,
1196 		0xbfef,		0,
1197 		0xbff0,		go->board_info->sensor_flags &
1198 					GO7007_SENSOR_TV ? 0xf060 : 0xb060,
1199 		0xbff1,		0,
1200 		0,		0,
1201 	};
1202 
1203 	return copy_packages(code, pack, 5, space);
1204 }
1205 
1206 static int seqhead_to_package(struct go7007 *go, __le16 *code, int space,
1207 	int (*sequence_header_func)(struct go7007 *go,
1208 		unsigned char *buf, int ext))
1209 {
1210 	int vop_time_increment_bitlength = vti_bitlen(go);
1211 	int fps = go->sensor_framerate / go->fps_scale *
1212 					(go->interlace_coding ? 2 : 1);
1213 	unsigned char buf[40] = { };
1214 	int len = sequence_header_func(go, buf, 1);
1215 	u16 pack[] = {
1216 		0x2006,		0,
1217 		0xbf08,		fps,
1218 		0xbf09,		0,
1219 		0xbff2,		vop_time_increment_bitlength,
1220 		0xbff3,		(1 << vop_time_increment_bitlength) - 1,
1221 		0xbfe6,		0,
1222 		0xbfe7,		(fps / 1000) << 8,
1223 		0,		0,
1224 		0,		0,
1225 		0,		0,
1226 		0,		0,
1227 		0,		0,
1228 		0,		0,
1229 		0,		0,
1230 		0,		0,
1231 		0,		0,
1232 
1233 		0x2007,		0,
1234 		0xc800,		buf[2] << 8 | buf[3],
1235 		0xc801,		buf[4] << 8 | buf[5],
1236 		0xc802,		buf[6] << 8 | buf[7],
1237 		0xc803,		buf[8] << 8 | buf[9],
1238 		0xc406,		64,
1239 		0xc407,		len - 64,
1240 		0xc61b,		1,
1241 		0,		0,
1242 		0,		0,
1243 		0,		0,
1244 		0,		0,
1245 		0,		0,
1246 		0,		0,
1247 		0,		0,
1248 		0,		0,
1249 
1250 		0x200e,		0,
1251 		0xc808,		buf[10] << 8 | buf[11],
1252 		0xc809,		buf[12] << 8 | buf[13],
1253 		0xc80a,		buf[14] << 8 | buf[15],
1254 		0xc80b,		buf[16] << 8 | buf[17],
1255 		0xc80c,		buf[18] << 8 | buf[19],
1256 		0xc80d,		buf[20] << 8 | buf[21],
1257 		0xc80e,		buf[22] << 8 | buf[23],
1258 		0xc80f,		buf[24] << 8 | buf[25],
1259 		0xc810,		buf[26] << 8 | buf[27],
1260 		0xc811,		buf[28] << 8 | buf[29],
1261 		0xc812,		buf[30] << 8 | buf[31],
1262 		0xc813,		buf[32] << 8 | buf[33],
1263 		0xc814,		buf[34] << 8 | buf[35],
1264 		0xc815,		buf[36] << 8 | buf[37],
1265 		0,		0,
1266 		0,		0,
1267 		0,		0,
1268 	};
1269 
1270 	return copy_packages(code, pack, 3, space);
1271 }
1272 
1273 static int relative_prime(int big, int little)
1274 {
1275 	int remainder;
1276 
1277 	while (little != 0) {
1278 		remainder = big % little;
1279 		big = little;
1280 		little = remainder;
1281 	}
1282 	return big;
1283 }
1284 
1285 static int avsync_to_package(struct go7007 *go, __le16 *code, int space)
1286 {
1287 	int arate = go->board_info->audio_rate * 1001 * go->fps_scale;
1288 	int ratio = arate / go->sensor_framerate;
1289 	int adjratio = ratio * 215 / 100;
1290 	int rprime = relative_prime(go->sensor_framerate,
1291 					arate % go->sensor_framerate);
1292 	int f1 = (arate % go->sensor_framerate) / rprime;
1293 	int f2 = (go->sensor_framerate - arate % go->sensor_framerate) / rprime;
1294 	u16 pack[] = {
1295 		0x200e,		0,
1296 		0xbf98,		(u16)((-adjratio) & 0xffff),
1297 		0xbf99,		(u16)((-adjratio) >> 16),
1298 		0xbf92,		0,
1299 		0xbf93,		0,
1300 		0xbff4,		f1 > f2 ? f1 : f2,
1301 		0xbff5,		f1 < f2 ? f1 : f2,
1302 		0xbff6,		f1 < f2 ? ratio : ratio + 1,
1303 		0xbff7,		f1 > f2 ? ratio : ratio + 1,
1304 		0xbff8,		0,
1305 		0xbff9,		0,
1306 		0xbffa,		adjratio & 0xffff,
1307 		0xbffb,		adjratio >> 16,
1308 		0xbf94,		0,
1309 		0xbf95,		0,
1310 		0,		0,
1311 	};
1312 
1313 	return copy_packages(code, pack, 1, space);
1314 }
1315 
1316 static int final_package(struct go7007 *go, __le16 *code, int space)
1317 {
1318 	int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
1319 	u16 pack[] = {
1320 		0x8000,
1321 		0,
1322 		0,
1323 		0,
1324 		0,
1325 		0,
1326 		0,
1327 		2,
1328 		((go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
1329 						(!go->interlace_coding) ?
1330 					(1 << 14) | (1 << 9) : 0) |
1331 			((go->encoder_subsample ? 1 : 0) << 8) |
1332 			(go->board_info->sensor_flags &
1333 				GO7007_SENSOR_CONFIG_MASK),
1334 		((go->encoder_v_halve ? 1 : 0) << 14) |
1335 			(go->encoder_v_halve ? rows << 9 : rows << 8) |
1336 			(go->encoder_h_halve ? 1 << 6 : 0) |
1337 			(go->encoder_h_halve ? go->width >> 3 : go->width >> 4),
1338 		(1 << 15) | (go->encoder_v_offset << 6) |
1339 			(1 << 7) | (go->encoder_h_offset >> 2),
1340 		(1 << 6),
1341 		0,
1342 		0,
1343 		((go->fps_scale - 1) << 8) |
1344 			(go->board_info->sensor_flags & GO7007_SENSOR_TV ?
1345 						(1 << 7) : 0) |
1346 			0x41,
1347 		go->ipb ? 0xd4c : 0x36b,
1348 		(rows << 8) | (go->width >> 4),
1349 		go->format == V4L2_PIX_FMT_MPEG4 ? 0x0404 : 0,
1350 		(1 << 15) | ((go->interlace_coding ? 1 : 0) << 13) |
1351 			((go->closed_gop ? 1 : 0) << 12) |
1352 			((go->format == V4L2_PIX_FMT_MPEG4 ? 1 : 0) << 11) |
1353 		/*	(1 << 9) |   */
1354 			((go->ipb ? 3 : 0) << 7) |
1355 			((go->modet_enable ? 1 : 0) << 2) |
1356 			((go->dvd_mode ? 1 : 0) << 1) | 1,
1357 		(go->format == V4L2_PIX_FMT_MPEG1 ? 0x89a0 :
1358 			(go->format == V4L2_PIX_FMT_MPEG2 ? 0x89a0 :
1359 			(go->format == V4L2_PIX_FMT_MJPEG ? 0x89a0 :
1360 			(go->format == V4L2_PIX_FMT_MPEG4 ? 0x8920 :
1361 			(go->format == V4L2_PIX_FMT_H263 ? 0x8920 : 0))))),
1362 		go->ipb ? 0x1f15 : 0x1f0b,
1363 		go->ipb ? 0x0015 : 0x000b,
1364 		go->ipb ? 0xa800 : 0x5800,
1365 		0xffff,
1366 		0x0020 + 0x034b * 0,
1367 		0x0020 + 0x034b * 1,
1368 		0x0020 + 0x034b * 2,
1369 		0x0020 + 0x034b * 3,
1370 		0x0020 + 0x034b * 4,
1371 		0x0020 + 0x034b * 5,
1372 		go->ipb ? (go->gop_size / 3) : go->gop_size,
1373 		(go->height >> 4) * (go->width >> 4) * 110 / 100,
1374 	};
1375 
1376 	return copy_packages(code, pack, 1, space);
1377 }
1378 
1379 static int audio_to_package(struct go7007 *go, __le16 *code, int space)
1380 {
1381 	int clock_config = ((go->board_info->audio_flags &
1382 				GO7007_AUDIO_I2S_MASTER ? 1 : 0) << 11) |
1383 			((go->board_info->audio_flags &
1384 				GO7007_AUDIO_OKI_MODE ? 1 : 0) << 8) |
1385 			(((go->board_info->audio_bclk_div / 4) - 1) << 4) |
1386 			(go->board_info->audio_main_div - 1);
1387 	u16 pack[] = {
1388 		0x200d,		0,
1389 		0x9002,		0,
1390 		0x9002,		0,
1391 		0x9031,		0,
1392 		0x9032,		0,
1393 		0x9033,		0,
1394 		0x9034,		0,
1395 		0x9035,		0,
1396 		0x9036,		0,
1397 		0x9037,		0,
1398 		0x9040,		0,
1399 		0x9000,		clock_config,
1400 		0x9001,		(go->board_info->audio_flags & 0xffff) |
1401 					(1 << 9),
1402 		0x9000,		((go->board_info->audio_flags &
1403 						GO7007_AUDIO_I2S_MASTER ?
1404 						1 : 0) << 10) |
1405 					clock_config,
1406 		0,		0,
1407 		0,		0,
1408 		0x2005,		0,
1409 		0x9041,		0,
1410 		0x9042,		256,
1411 		0x9043,		0,
1412 		0x9044,		16,
1413 		0x9045,		16,
1414 		0,		0,
1415 		0,		0,
1416 		0,		0,
1417 		0,		0,
1418 		0,		0,
1419 		0,		0,
1420 		0,		0,
1421 		0,		0,
1422 		0,		0,
1423 		0,		0,
1424 	};
1425 
1426 	return copy_packages(code, pack, 2, space);
1427 }
1428 
1429 static int modet_to_package(struct go7007 *go, __le16 *code, int space)
1430 {
1431 	bool has_modet0 = go->modet[0].enable;
1432 	bool has_modet1 = go->modet[1].enable;
1433 	bool has_modet2 = go->modet[2].enable;
1434 	bool has_modet3 = go->modet[3].enable;
1435 	int ret, mb, i, addr, cnt = 0;
1436 	u16 pack[32];
1437 	u16 thresholds[] = {
1438 		0x200e,		0,
1439 		0xbf82,		has_modet0 ? go->modet[0].pixel_threshold : 32767,
1440 		0xbf83,		has_modet1 ? go->modet[1].pixel_threshold : 32767,
1441 		0xbf84,		has_modet2 ? go->modet[2].pixel_threshold : 32767,
1442 		0xbf85,		has_modet3 ? go->modet[3].pixel_threshold : 32767,
1443 		0xbf86,		has_modet0 ? go->modet[0].motion_threshold : 32767,
1444 		0xbf87,		has_modet1 ? go->modet[1].motion_threshold : 32767,
1445 		0xbf88,		has_modet2 ? go->modet[2].motion_threshold : 32767,
1446 		0xbf89,		has_modet3 ? go->modet[3].motion_threshold : 32767,
1447 		0xbf8a,		has_modet0 ? go->modet[0].mb_threshold : 32767,
1448 		0xbf8b,		has_modet1 ? go->modet[1].mb_threshold : 32767,
1449 		0xbf8c,		has_modet2 ? go->modet[2].mb_threshold : 32767,
1450 		0xbf8d,		has_modet3 ? go->modet[3].mb_threshold : 32767,
1451 		0xbf8e,		0,
1452 		0xbf8f,		0,
1453 		0,		0,
1454 	};
1455 
1456 	ret = copy_packages(code, thresholds, 1, space);
1457 	if (ret < 0)
1458 		return -1;
1459 	cnt += ret;
1460 
1461 	addr = 0xbac0;
1462 	memset(pack, 0, 64);
1463 	i = 0;
1464 	for (mb = 0; mb < 1624; ++mb) {
1465 		pack[i * 2 + 3] <<= 2;
1466 		pack[i * 2 + 3] |= go->modet_map[mb];
1467 		if (mb % 8 != 7)
1468 			continue;
1469 		pack[i * 2 + 2] = addr++;
1470 		++i;
1471 		if (i == 10 || mb == 1623) {
1472 			pack[0] = 0x2000 | i;
1473 			ret = copy_packages(code + cnt, pack, 1, space - cnt);
1474 			if (ret < 0)
1475 				return -1;
1476 			cnt += ret;
1477 			i = 0;
1478 			memset(pack, 0, 64);
1479 		}
1480 		pack[i * 2 + 3] = 0;
1481 	}
1482 
1483 	memset(pack, 0, 64);
1484 	i = 0;
1485 	for (addr = 0xbb90; addr < 0xbbfa; ++addr) {
1486 		pack[i * 2 + 2] = addr;
1487 		pack[i * 2 + 3] = 0;
1488 		++i;
1489 		if (i == 10 || addr == 0xbbf9) {
1490 			pack[0] = 0x2000 | i;
1491 			ret = copy_packages(code + cnt, pack, 1, space - cnt);
1492 			if (ret < 0)
1493 				return -1;
1494 			cnt += ret;
1495 			i = 0;
1496 			memset(pack, 0, 64);
1497 		}
1498 	}
1499 	return cnt;
1500 }
1501 
1502 static noinline_for_stack int do_special(struct go7007 *go, u16 type,
1503 					 __le16 *code, int space, int *framelen)
1504 {
1505 	switch (type) {
1506 	case SPECIAL_FRM_HEAD:
1507 		switch (go->format) {
1508 		case V4L2_PIX_FMT_MJPEG:
1509 			return gen_mjpeghdr_to_package(go, code, space);
1510 		case V4L2_PIX_FMT_MPEG1:
1511 		case V4L2_PIX_FMT_MPEG2:
1512 			return gen_mpeg1hdr_to_package(go, code, space,
1513 								framelen);
1514 		case V4L2_PIX_FMT_MPEG4:
1515 			return gen_mpeg4hdr_to_package(go, code, space,
1516 								framelen);
1517 		default:
1518 			break;
1519 		}
1520 		break;
1521 	case SPECIAL_BRC_CTRL:
1522 		return brctrl_to_package(go, code, space, framelen);
1523 	case SPECIAL_CONFIG:
1524 		return config_package(go, code, space);
1525 	case SPECIAL_SEQHEAD:
1526 		switch (go->format) {
1527 		case V4L2_PIX_FMT_MPEG1:
1528 		case V4L2_PIX_FMT_MPEG2:
1529 			return seqhead_to_package(go, code, space,
1530 					mpeg1_sequence_header);
1531 		case V4L2_PIX_FMT_MPEG4:
1532 			return seqhead_to_package(go, code, space,
1533 					mpeg4_sequence_header);
1534 		default:
1535 			return 0;
1536 		}
1537 	case SPECIAL_AV_SYNC:
1538 		return avsync_to_package(go, code, space);
1539 	case SPECIAL_FINAL:
1540 		return final_package(go, code, space);
1541 	case SPECIAL_AUDIO:
1542 		return audio_to_package(go, code, space);
1543 	case SPECIAL_MODET:
1544 		return modet_to_package(go, code, space);
1545 	}
1546 	dev_err(go->dev,
1547 		"firmware file contains unsupported feature %04x\n", type);
1548 	return -1;
1549 }
1550 
1551 int go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen)
1552 {
1553 	const struct firmware *fw_entry;
1554 	__le16 *code, *src;
1555 	int framelen[8] = { }; /* holds the lengths of empty frame templates */
1556 	int codespace = 64 * 1024, i = 0, srclen, chunk_len, chunk_flags;
1557 	int mode_flag;
1558 	int ret;
1559 
1560 	switch (go->format) {
1561 	case V4L2_PIX_FMT_MJPEG:
1562 		mode_flag = FLAG_MODE_MJPEG;
1563 		break;
1564 	case V4L2_PIX_FMT_MPEG1:
1565 		mode_flag = FLAG_MODE_MPEG1;
1566 		break;
1567 	case V4L2_PIX_FMT_MPEG2:
1568 		mode_flag = FLAG_MODE_MPEG2;
1569 		break;
1570 	case V4L2_PIX_FMT_MPEG4:
1571 		mode_flag = FLAG_MODE_MPEG4;
1572 		break;
1573 	default:
1574 		return -1;
1575 	}
1576 	if (request_firmware(&fw_entry, GO7007_FW_NAME, go->dev)) {
1577 		dev_err(go->dev,
1578 			"unable to load firmware from file \"%s\"\n",
1579 			GO7007_FW_NAME);
1580 		return -1;
1581 	}
1582 	code = kcalloc(codespace, 2, GFP_KERNEL);
1583 	if (code == NULL)
1584 		goto fw_failed;
1585 
1586 	src = (__le16 *)fw_entry->data;
1587 	srclen = fw_entry->size / 2;
1588 	while (srclen >= 2) {
1589 		chunk_flags = __le16_to_cpu(src[0]);
1590 		chunk_len = __le16_to_cpu(src[1]);
1591 		if (chunk_len + 2 > srclen) {
1592 			dev_err(go->dev,
1593 				"firmware file \"%s\" appears to be corrupted\n",
1594 				GO7007_FW_NAME);
1595 			goto fw_failed;
1596 		}
1597 		if (chunk_flags & mode_flag) {
1598 			if (chunk_flags & FLAG_SPECIAL) {
1599 				ret = do_special(go, __le16_to_cpu(src[2]),
1600 					&code[i], codespace - i, framelen);
1601 				if (ret < 0) {
1602 					dev_err(go->dev,
1603 						"insufficient memory for firmware construction\n");
1604 					goto fw_failed;
1605 				}
1606 				i += ret;
1607 			} else {
1608 				if (codespace - i < chunk_len) {
1609 					dev_err(go->dev,
1610 						"insufficient memory for firmware construction\n");
1611 					goto fw_failed;
1612 				}
1613 				memcpy(&code[i], &src[2], chunk_len * 2);
1614 				i += chunk_len;
1615 			}
1616 		}
1617 		srclen -= chunk_len + 2;
1618 		src += chunk_len + 2;
1619 	}
1620 	release_firmware(fw_entry);
1621 	*fw = (u8 *)code;
1622 	*fwlen = i * 2;
1623 	return 0;
1624 
1625 fw_failed:
1626 	kfree(code);
1627 	release_firmware(fw_entry);
1628 	return -1;
1629 }
1630 
1631 MODULE_FIRMWARE(GO7007_FW_NAME);
1632