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