Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / video / pvrusb2 / pvrusb2-std.c
blobda309288daa4c603fd92b6dbd33e3eca5a6a6508
1 /*
3 * $Id$
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "pvrusb2-std.h"
23 #include "pvrusb2-debug.h"
24 #include <asm/string.h>
25 #include <linux/slab.h>
27 struct std_name {
28 const char *name;
29 v4l2_std_id id;
33 #define CSTD_PAL \
34 (V4L2_STD_PAL_B| \
35 V4L2_STD_PAL_B1| \
36 V4L2_STD_PAL_G| \
37 V4L2_STD_PAL_H| \
38 V4L2_STD_PAL_I| \
39 V4L2_STD_PAL_D| \
40 V4L2_STD_PAL_D1| \
41 V4L2_STD_PAL_K| \
42 V4L2_STD_PAL_M| \
43 V4L2_STD_PAL_N| \
44 V4L2_STD_PAL_Nc| \
45 V4L2_STD_PAL_60)
47 #define CSTD_NTSC \
48 (V4L2_STD_NTSC_M| \
49 V4L2_STD_NTSC_M_JP| \
50 V4L2_STD_NTSC_M_KR| \
51 V4L2_STD_NTSC_443)
53 #define CSTD_ATSC \
54 (V4L2_STD_ATSC_8_VSB| \
55 V4L2_STD_ATSC_16_VSB)
57 #define CSTD_SECAM \
58 (V4L2_STD_SECAM_B| \
59 V4L2_STD_SECAM_D| \
60 V4L2_STD_SECAM_G| \
61 V4L2_STD_SECAM_H| \
62 V4L2_STD_SECAM_K| \
63 V4L2_STD_SECAM_K1| \
64 V4L2_STD_SECAM_L| \
65 V4L2_STD_SECAM_LC)
67 #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
68 #define TSTD_B1 (V4L2_STD_PAL_B1)
69 #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
70 #define TSTD_D1 (V4L2_STD_PAL_D1)
71 #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
72 #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
73 #define TSTD_I (V4L2_STD_PAL_I)
74 #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
75 #define TSTD_K1 (V4L2_STD_SECAM_K1)
76 #define TSTD_L (V4L2_STD_SECAM_L)
77 #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
78 #define TSTD_N (V4L2_STD_PAL_N)
79 #define TSTD_Nc (V4L2_STD_PAL_Nc)
80 #define TSTD_60 (V4L2_STD_PAL_60)
82 #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM)
84 /* Mapping of standard bits to color system */
85 static const struct std_name std_groups[] = {
86 {"PAL",CSTD_PAL},
87 {"NTSC",CSTD_NTSC},
88 {"SECAM",CSTD_SECAM},
89 {"ATSC",CSTD_ATSC},
92 /* Mapping of standard bits to modulation system */
93 static const struct std_name std_items[] = {
94 {"B",TSTD_B},
95 {"B1",TSTD_B1},
96 {"D",TSTD_D},
97 {"D1",TSTD_D1},
98 {"G",TSTD_G},
99 {"H",TSTD_H},
100 {"I",TSTD_I},
101 {"K",TSTD_K},
102 {"K1",TSTD_K1},
103 {"L",TSTD_L},
104 {"LC",V4L2_STD_SECAM_LC},
105 {"M",TSTD_M},
106 {"Mj",V4L2_STD_NTSC_M_JP},
107 {"443",V4L2_STD_NTSC_443},
108 {"Mk",V4L2_STD_NTSC_M_KR},
109 {"N",TSTD_N},
110 {"Nc",TSTD_Nc},
111 {"60",TSTD_60},
112 {"8VSB",V4L2_STD_ATSC_8_VSB},
113 {"16VSB",V4L2_STD_ATSC_16_VSB},
117 // Search an array of std_name structures and return a pointer to the
118 // element with the matching name.
119 static const struct std_name *find_std_name(const struct std_name *arrPtr,
120 unsigned int arrSize,
121 const char *bufPtr,
122 unsigned int bufSize)
124 unsigned int idx;
125 const struct std_name *p;
126 for (idx = 0; idx < arrSize; idx++) {
127 p = arrPtr + idx;
128 if (strlen(p->name) != bufSize) continue;
129 if (!memcmp(bufPtr,p->name,bufSize)) return p;
131 return NULL;
135 int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
136 unsigned int bufSize)
138 v4l2_std_id id = 0;
139 v4l2_std_id cmsk = 0;
140 v4l2_std_id t;
141 int mMode = 0;
142 unsigned int cnt;
143 char ch;
144 const struct std_name *sp;
146 while (bufSize) {
147 if (!mMode) {
148 cnt = 0;
149 while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
150 if (cnt >= bufSize) return 0; // No more characters
151 sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
152 bufPtr,cnt);
153 if (!sp) return 0; // Illegal color system name
154 cnt++;
155 bufPtr += cnt;
156 bufSize -= cnt;
157 mMode = !0;
158 cmsk = sp->id;
159 continue;
161 cnt = 0;
162 while (cnt < bufSize) {
163 ch = bufPtr[cnt];
164 if (ch == ';') {
165 mMode = 0;
166 break;
168 if (ch == '/') break;
169 cnt++;
171 sp = find_std_name(std_items, ARRAY_SIZE(std_items),
172 bufPtr,cnt);
173 if (!sp) return 0; // Illegal modulation system ID
174 t = sp->id & cmsk;
175 if (!t) return 0; // Specific color + modulation system illegal
176 id |= t;
177 if (cnt < bufSize) cnt++;
178 bufPtr += cnt;
179 bufSize -= cnt;
182 if (idPtr) *idPtr = id;
183 return !0;
187 unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
188 v4l2_std_id id)
190 unsigned int idx1,idx2;
191 const struct std_name *ip,*gp;
192 int gfl,cfl;
193 unsigned int c1,c2;
194 cfl = 0;
195 c1 = 0;
196 for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
197 gp = std_groups + idx1;
198 gfl = 0;
199 for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
200 ip = std_items + idx2;
201 if (!(gp->id & ip->id & id)) continue;
202 if (!gfl) {
203 if (cfl) {
204 c2 = scnprintf(bufPtr,bufSize,";");
205 c1 += c2;
206 bufSize -= c2;
207 bufPtr += c2;
209 cfl = !0;
210 c2 = scnprintf(bufPtr,bufSize,
211 "%s-",gp->name);
212 gfl = !0;
213 } else {
214 c2 = scnprintf(bufPtr,bufSize,"/");
216 c1 += c2;
217 bufSize -= c2;
218 bufPtr += c2;
219 c2 = scnprintf(bufPtr,bufSize,
220 ip->name);
221 c1 += c2;
222 bufSize -= c2;
223 bufPtr += c2;
226 return c1;
230 // Template data for possible enumerated video standards. Here we group
231 // standards which share common frame rates and resolution.
232 static struct v4l2_standard generic_standards[] = {
234 .id = (TSTD_B|TSTD_B1|
235 TSTD_D|TSTD_D1|
236 TSTD_G|
237 TSTD_H|
238 TSTD_I|
239 TSTD_K|TSTD_K1|
240 TSTD_L|
241 V4L2_STD_SECAM_LC |
242 TSTD_N|TSTD_Nc),
243 .frameperiod =
245 .numerator = 1,
246 .denominator= 25
248 .framelines = 625,
249 .reserved = {0,0,0,0}
250 }, {
251 .id = (TSTD_M|
252 V4L2_STD_NTSC_M_JP|
253 V4L2_STD_NTSC_M_KR),
254 .frameperiod =
256 .numerator = 1001,
257 .denominator= 30000
259 .framelines = 525,
260 .reserved = {0,0,0,0}
261 }, { // This is a total wild guess
262 .id = (TSTD_60),
263 .frameperiod =
265 .numerator = 1001,
266 .denominator= 30000
268 .framelines = 525,
269 .reserved = {0,0,0,0}
270 }, { // This is total wild guess
271 .id = V4L2_STD_NTSC_443,
272 .frameperiod =
274 .numerator = 1001,
275 .denominator= 30000
277 .framelines = 525,
278 .reserved = {0,0,0,0}
282 #define generic_standards_cnt ARRAY_SIZE(generic_standards)
284 static struct v4l2_standard *match_std(v4l2_std_id id)
286 unsigned int idx;
287 for (idx = 0; idx < generic_standards_cnt; idx++) {
288 if (generic_standards[idx].id & id) {
289 return generic_standards + idx;
292 return NULL;
295 static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
297 struct v4l2_standard *template;
298 int idx;
299 unsigned int bcnt;
300 template = match_std(id);
301 if (!template) return 0;
302 idx = std->index;
303 memcpy(std,template,sizeof(*template));
304 std->index = idx;
305 std->id = id;
306 bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
307 std->name[bcnt] = 0;
308 pvr2_trace(PVR2_TRACE_STD,"Set up standard idx=%u name=%s",
309 std->index,std->name);
310 return !0;
313 /* These are special cases of combined standards that we should enumerate
314 separately if the component pieces are present. */
315 static v4l2_std_id std_mixes[] = {
316 V4L2_STD_PAL_B | V4L2_STD_PAL_G,
317 V4L2_STD_PAL_D | V4L2_STD_PAL_K,
318 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
319 V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
322 struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
323 v4l2_std_id id)
325 unsigned int std_cnt = 0;
326 unsigned int idx,bcnt,idx2;
327 v4l2_std_id idmsk,cmsk,fmsk;
328 struct v4l2_standard *stddefs;
330 if (pvrusb2_debug & PVR2_TRACE_STD) {
331 char buf[50];
332 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
333 pvr2_trace(
334 PVR2_TRACE_STD,"Mapping standards mask=0x%x (%.*s)",
335 (int)id,bcnt,buf);
338 *countptr = 0;
339 std_cnt = 0;
340 fmsk = 0;
341 for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
342 if (!(idmsk & cmsk)) continue;
343 cmsk &= ~idmsk;
344 if (match_std(idmsk)) {
345 std_cnt++;
346 continue;
348 fmsk |= idmsk;
351 for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
352 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
355 if (fmsk) {
356 char buf[50];
357 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
358 pvr2_trace(
359 PVR2_TRACE_ERROR_LEGS,
360 "WARNING:"
361 " Failed to classify the following standard(s): %.*s",
362 bcnt,buf);
365 pvr2_trace(PVR2_TRACE_STD,"Setting up %u unique standard(s)",
366 std_cnt);
367 if (!std_cnt) return NULL; // paranoia
369 stddefs = kzalloc(sizeof(struct v4l2_standard) * std_cnt,
370 GFP_KERNEL);
371 for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
373 idx = 0;
375 /* Enumerate potential special cases */
376 for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
377 idx2++) {
378 if (!(id & std_mixes[idx2])) continue;
379 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
381 /* Now enumerate individual pieces */
382 for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
383 if (!(idmsk & cmsk)) continue;
384 cmsk &= ~idmsk;
385 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
386 idx++;
389 *countptr = std_cnt;
390 return stddefs;
393 v4l2_std_id pvr2_std_get_usable(void)
395 return CSTD_ALL;
400 Stuff for Emacs to see, in order to encourage consistent editing style:
401 *** Local Variables: ***
402 *** mode: c ***
403 *** fill-column: 75 ***
404 *** tab-width: 8 ***
405 *** c-basic-offset: 8 ***
406 *** End: ***