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