blob: 8462a7c1a338496365b3e26c1783f178d4885b98 [file] [log] [blame]
Hans de Goede327c4ab2008-09-03 17:12:14 -03001/*
2 * Pixart PAC207BCA / PAC73xx common functions
3 *
4 * Copyright (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl>
5 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6 * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
7 *
8 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
Hans de Goede8a5b2e92008-09-03 17:12:17 -030026/* We calculate the autogain at the end of the transfer of a frame, at this
Hans de Goede5fb2dde2010-02-17 11:59:19 -030027 moment a frame with the old settings is being captured and transmitted. So
28 if we adjust the gain or exposure we must ignore atleast the next frame for
29 the new settings to come into effect before doing any other adjustments. */
30#define PAC_AUTOGAIN_IGNORE_FRAMES 2
Hans de Goede8a5b2e92008-09-03 17:12:17 -030031
Hans de Goede327c4ab2008-09-03 17:12:14 -030032static const unsigned char pac_sof_marker[5] =
33 { 0xff, 0xff, 0x00, 0xff, 0x96 };
34
Marton Nemethe0d49e22009-10-05 05:41:30 -030035/*
36 The following state machine finds the SOF marker sequence
37 0xff, 0xff, 0x00, 0xff, 0x96 in a byte stream.
38
39 +----------+
40 | 0: START |<---------------\
41 +----------+<-\ |
42 | \---/otherwise |
43 v 0xff |
44 +----------+ otherwise |
45 | 1 |--------------->*
46 | | ^
47 +----------+ |
48 | |
49 v 0xff |
50 +----------+<-\0xff |
51 /->| |--/ |
52 | | 2 |--------------->*
53 | | | otherwise ^
54 | +----------+ |
55 | | |
56 | v 0x00 |
57 | +----------+ |
58 | | 3 | |
59 | | |--------------->*
60 | +----------+ otherwise ^
61 | | |
62 0xff | v 0xff |
63 | +----------+ |
64 \--| 4 | |
65 | |----------------/
66 +----------+ otherwise
67 |
68 v 0x96
69 +----------+
70 | FOUND |
71 +----------+
72*/
73
Marton Nemetha6b69e42009-11-02 08:05:51 -030074static unsigned char *pac_find_sof(u8 *sof_read,
Hans de Goede327c4ab2008-09-03 17:12:14 -030075 unsigned char *m, int len)
76{
Hans de Goede327c4ab2008-09-03 17:12:14 -030077 int i;
78
79 /* Search for the SOF marker (fixed part) in the header */
80 for (i = 0; i < len; i++) {
Marton Nemetha6b69e42009-11-02 08:05:51 -030081 switch (*sof_read) {
Marton Nemethe0d49e22009-10-05 05:41:30 -030082 case 0:
83 if (m[i] == 0xff)
Marton Nemetha6b69e42009-11-02 08:05:51 -030084 *sof_read = 1;
Marton Nemethe0d49e22009-10-05 05:41:30 -030085 break;
86 case 1:
87 if (m[i] == 0xff)
Marton Nemetha6b69e42009-11-02 08:05:51 -030088 *sof_read = 2;
Marton Nemethe0d49e22009-10-05 05:41:30 -030089 else
Marton Nemetha6b69e42009-11-02 08:05:51 -030090 *sof_read = 0;
Marton Nemethe0d49e22009-10-05 05:41:30 -030091 break;
92 case 2:
93 switch (m[i]) {
94 case 0x00:
Marton Nemetha6b69e42009-11-02 08:05:51 -030095 *sof_read = 3;
Marton Nemethe0d49e22009-10-05 05:41:30 -030096 break;
97 case 0xff:
98 /* stay in this state */
99 break;
100 default:
Marton Nemetha6b69e42009-11-02 08:05:51 -0300101 *sof_read = 0;
Marton Nemethe0d49e22009-10-05 05:41:30 -0300102 }
103 break;
104 case 3:
105 if (m[i] == 0xff)
Marton Nemetha6b69e42009-11-02 08:05:51 -0300106 *sof_read = 4;
Marton Nemethe0d49e22009-10-05 05:41:30 -0300107 else
Marton Nemetha6b69e42009-11-02 08:05:51 -0300108 *sof_read = 0;
Marton Nemethe0d49e22009-10-05 05:41:30 -0300109 break;
110 case 4:
111 switch (m[i]) {
112 case 0x96:
113 /* Pattern found */
Hans de Goede8a5b2e92008-09-03 17:12:17 -0300114 PDEBUG(D_FRAM,
Hans de Goede327c4ab2008-09-03 17:12:14 -0300115 "SOF found, bytes to analyze: %u."
116 " Frame starts at byte #%u",
117 len, i + 1);
Marton Nemetha6b69e42009-11-02 08:05:51 -0300118 *sof_read = 0;
Hans de Goede327c4ab2008-09-03 17:12:14 -0300119 return m + i + 1;
Marton Nemethe0d49e22009-10-05 05:41:30 -0300120 break;
121 case 0xff:
Marton Nemetha6b69e42009-11-02 08:05:51 -0300122 *sof_read = 2;
Marton Nemethe0d49e22009-10-05 05:41:30 -0300123 break;
124 default:
Marton Nemetha6b69e42009-11-02 08:05:51 -0300125 *sof_read = 0;
Hans de Goede327c4ab2008-09-03 17:12:14 -0300126 }
Marton Nemethe0d49e22009-10-05 05:41:30 -0300127 break;
128 default:
Marton Nemetha6b69e42009-11-02 08:05:51 -0300129 *sof_read = 0;
Hans de Goede327c4ab2008-09-03 17:12:14 -0300130 }
131 }
132
133 return NULL;
134}