GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / media / usb / gspca / pac_common.h
1 /*
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  */
21
22 /* We calculate the autogain at the end of the transfer of a frame, at this
23    moment a frame with the old settings is being captured and transmitted. So
24    if we adjust the gain or exposure we must ignore atleast the next frame for
25    the new settings to come into effect before doing any other adjustments. */
26 #define PAC_AUTOGAIN_IGNORE_FRAMES      2
27
28 static const unsigned char pac_sof_marker[5] =
29                 { 0xff, 0xff, 0x00, 0xff, 0x96 };
30
31 /*
32    The following state machine finds the SOF marker sequence
33    0xff, 0xff, 0x00, 0xff, 0x96 in a byte stream.
34
35            +----------+
36            | 0: START |<---------------\
37            +----------+<-\             |
38              |       \---/otherwise    |
39              v 0xff                    |
40            +----------+ otherwise      |
41            |     1    |--------------->*
42            |          |                ^
43            +----------+                |
44              |                         |
45              v 0xff                    |
46            +----------+<-\0xff         |
47         /->|          |--/             |
48         |  |     2    |--------------->*
49         |  |          | otherwise      ^
50         |  +----------+                |
51         |    |                         |
52         |    v 0x00                    |
53         |  +----------+                |
54         |  |     3    |                |
55         |  |          |--------------->*
56         |  +----------+ otherwise      ^
57         |    |                         |
58    0xff |    v 0xff                    |
59         |  +----------+                |
60         \--|     4    |                |
61            |          |----------------/
62            +----------+ otherwise
63              |
64              v 0x96
65            +----------+
66            |  FOUND   |
67            +----------+
68 */
69
70 static unsigned char *pac_find_sof(struct gspca_dev *gspca_dev, u8 *sof_read,
71                                         unsigned char *m, int len)
72 {
73         int i;
74
75         /* Search for the SOF marker (fixed part) in the header */
76         for (i = 0; i < len; i++) {
77                 switch (*sof_read) {
78                 case 0:
79                         if (m[i] == 0xff)
80                                 *sof_read = 1;
81                         break;
82                 case 1:
83                         if (m[i] == 0xff)
84                                 *sof_read = 2;
85                         else
86                                 *sof_read = 0;
87                         break;
88                 case 2:
89                         switch (m[i]) {
90                         case 0x00:
91                                 *sof_read = 3;
92                                 break;
93                         case 0xff:
94                                 /* stay in this state */
95                                 break;
96                         default:
97                                 *sof_read = 0;
98                         }
99                         break;
100                 case 3:
101                         if (m[i] == 0xff)
102                                 *sof_read = 4;
103                         else
104                                 *sof_read = 0;
105                         break;
106                 case 4:
107                         switch (m[i]) {
108                         case 0x96:
109                                 /* Pattern found */
110                                 gspca_dbg(gspca_dev, D_FRAM,
111                                           "SOF found, bytes to analyze: %u - Frame starts at byte #%u\n",
112                                           len, i + 1);
113                                 *sof_read = 0;
114                                 return m + i + 1;
115                                 break;
116                         case 0xff:
117                                 *sof_read = 2;
118                                 break;
119                         default:
120                                 *sof_read = 0;
121                         }
122                         break;
123                 default:
124                         *sof_read = 0;
125                 }
126         }
127
128         return NULL;
129 }