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