1262569Simp/* SPDX-License-Identifier: GPL-2.0-or-later */
2262569Simp/*
3262569Simp * Pixart PAC207BCA / PAC73xx common functions
4262569Simp *
5262569Simp * Copyright (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl>
6262569Simp * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
7262569Simp * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
8262569Simp *
9262569Simp * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
10262569Simp */
11262569Simp
12262569Simp/* We calculate the autogain at the end of the transfer of a frame, at this
13270864Simp   moment a frame with the old settings is being captured and transmitted. So
14262569Simp   if we adjust the gain or exposure we must ignore at least the next frame for
15262569Simp   the new settings to come into effect before doing any other adjustments. */
16262569Simp#define PAC_AUTOGAIN_IGNORE_FRAMES	2
17262569Simp
18262569Simpstatic const unsigned char pac_sof_marker[5] =
19262569Simp		{ 0xff, 0xff, 0x00, 0xff, 0x96 };
20262569Simp
21262569Simp/*
22262569Simp   The following state machine finds the SOF marker sequence
23262569Simp   0xff, 0xff, 0x00, 0xff, 0x96 in a byte stream.
24262569Simp
25262569Simp	   +----------+
26262569Simp	   | 0: START |<---------------\
27	   +----------+<-\             |
28	     |       \---/otherwise    |
29	     v 0xff                    |
30	   +----------+ otherwise      |
31	   |     1    |--------------->*
32	   |          |                ^
33	   +----------+                |
34	     |                         |
35	     v 0xff                    |
36	   +----------+<-\0xff         |
37	/->|          |--/             |
38	|  |     2    |--------------->*
39	|  |          | otherwise      ^
40	|  +----------+                |
41	|    |                         |
42	|    v 0x00                    |
43	|  +----------+                |
44	|  |     3    |                |
45	|  |          |--------------->*
46	|  +----------+ otherwise      ^
47	|    |                         |
48   0xff |    v 0xff                    |
49	|  +----------+                |
50	\--|     4    |                |
51	   |          |----------------/
52	   +----------+ otherwise
53	     |
54	     v 0x96
55	   +----------+
56	   |  FOUND   |
57	   +----------+
58*/
59
60static unsigned char *pac_find_sof(struct gspca_dev *gspca_dev, u8 *sof_read,
61					unsigned char *m, int len)
62{
63	int i;
64
65	/* Search for the SOF marker (fixed part) in the header */
66	for (i = 0; i < len; i++) {
67		switch (*sof_read) {
68		case 0:
69			if (m[i] == 0xff)
70				*sof_read = 1;
71			break;
72		case 1:
73			if (m[i] == 0xff)
74				*sof_read = 2;
75			else
76				*sof_read = 0;
77			break;
78		case 2:
79			switch (m[i]) {
80			case 0x00:
81				*sof_read = 3;
82				break;
83			case 0xff:
84				/* stay in this state */
85				break;
86			default:
87				*sof_read = 0;
88			}
89			break;
90		case 3:
91			if (m[i] == 0xff)
92				*sof_read = 4;
93			else
94				*sof_read = 0;
95			break;
96		case 4:
97			switch (m[i]) {
98			case 0x96:
99				/* Pattern found */
100				gspca_dbg(gspca_dev, D_FRAM,
101					  "SOF found, bytes to analyze: %u - Frame starts at byte #%u\n",
102					  len, i + 1);
103				*sof_read = 0;
104				return m + i + 1;
105				break;
106			case 0xff:
107				*sof_read = 2;
108				break;
109			default:
110				*sof_read = 0;
111			}
112			break;
113		default:
114			*sof_read = 0;
115		}
116	}
117
118	return NULL;
119}
120