Storage class should be before const qualifier
[deliverable/linux.git] / drivers / media / video / pvrusb2 / pvrusb2-std.c
CommitLineData
d855497e
MI
1/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 *
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
10 *
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.
15 *
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
19 *
20 */
21
22#include "pvrusb2-std.h"
23#include "pvrusb2-debug.h"
b2bbaa93
MI
24#include <asm/string.h>
25#include <linux/slab.h>
d855497e
MI
26
27struct std_name {
28 const char *name;
29 v4l2_std_id id;
30};
31
32
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)
46
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)
52
53#define CSTD_SECAM \
54 (V4L2_STD_SECAM_B| \
55 V4L2_STD_SECAM_D| \
56 V4L2_STD_SECAM_G| \
57 V4L2_STD_SECAM_H| \
58 V4L2_STD_SECAM_K| \
59 V4L2_STD_SECAM_K1| \
60 V4L2_STD_SECAM_L| \
61 V4L2_STD_SECAM_LC)
62
63#define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
64#define TSTD_B1 (V4L2_STD_PAL_B1)
65#define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
66#define TSTD_D1 (V4L2_STD_PAL_D1)
67#define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
68#define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
69#define TSTD_I (V4L2_STD_PAL_I)
70#define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
71#define TSTD_K1 (V4L2_STD_SECAM_K1)
72#define TSTD_L (V4L2_STD_SECAM_L)
73#define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
74#define TSTD_N (V4L2_STD_PAL_N)
75#define TSTD_Nc (V4L2_STD_PAL_Nc)
76#define TSTD_60 (V4L2_STD_PAL_60)
77
78#define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM)
79
80/* Mapping of standard bits to color system */
c5a69d57 81static const struct std_name std_groups[] = {
d855497e
MI
82 {"PAL",CSTD_PAL},
83 {"NTSC",CSTD_NTSC},
84 {"SECAM",CSTD_SECAM},
85};
86
87/* Mapping of standard bits to modulation system */
c5a69d57 88static const struct std_name std_items[] = {
d855497e
MI
89 {"B",TSTD_B},
90 {"B1",TSTD_B1},
91 {"D",TSTD_D},
92 {"D1",TSTD_D1},
93 {"G",TSTD_G},
94 {"H",TSTD_H},
95 {"I",TSTD_I},
96 {"K",TSTD_K},
97 {"K1",TSTD_K1},
98 {"L",TSTD_L},
99 {"LC",V4L2_STD_SECAM_LC},
100 {"M",TSTD_M},
101 {"Mj",V4L2_STD_NTSC_M_JP},
102 {"443",V4L2_STD_NTSC_443},
103 {"Mk",V4L2_STD_NTSC_M_KR},
104 {"N",TSTD_N},
105 {"Nc",TSTD_Nc},
106 {"60",TSTD_60},
107};
108
109
110// Search an array of std_name structures and return a pointer to the
111// element with the matching name.
112static const struct std_name *find_std_name(const struct std_name *arrPtr,
113 unsigned int arrSize,
114 const char *bufPtr,
115 unsigned int bufSize)
116{
117 unsigned int idx;
118 const struct std_name *p;
119 for (idx = 0; idx < arrSize; idx++) {
120 p = arrPtr + idx;
121 if (strlen(p->name) != bufSize) continue;
122 if (!memcmp(bufPtr,p->name,bufSize)) return p;
123 }
a0fd1cb1 124 return NULL;
d855497e
MI
125}
126
127
128int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
129 unsigned int bufSize)
130{
131 v4l2_std_id id = 0;
132 v4l2_std_id cmsk = 0;
133 v4l2_std_id t;
134 int mMode = 0;
135 unsigned int cnt;
136 char ch;
137 const struct std_name *sp;
138
139 while (bufSize) {
140 if (!mMode) {
141 cnt = 0;
142 while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
143 if (cnt >= bufSize) return 0; // No more characters
144 sp = find_std_name(
145 std_groups,
146 sizeof(std_groups)/sizeof(std_groups[0]),
147 bufPtr,cnt);
148 if (!sp) return 0; // Illegal color system name
149 cnt++;
150 bufPtr += cnt;
151 bufSize -= cnt;
152 mMode = !0;
153 cmsk = sp->id;
154 continue;
155 }
156 cnt = 0;
157 while (cnt < bufSize) {
158 ch = bufPtr[cnt];
159 if (ch == ';') {
160 mMode = 0;
161 break;
162 }
163 if (ch == '/') break;
164 cnt++;
165 }
166 sp = find_std_name(std_items,
167 sizeof(std_items)/sizeof(std_items[0]),
168 bufPtr,cnt);
169 if (!sp) return 0; // Illegal modulation system ID
170 t = sp->id & cmsk;
171 if (!t) return 0; // Specific color + modulation system illegal
172 id |= t;
173 if (cnt < bufSize) cnt++;
174 bufPtr += cnt;
175 bufSize -= cnt;
176 }
177
178 if (idPtr) *idPtr = id;
179 return !0;
180}
181
182
183unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
184 v4l2_std_id id)
185{
186 unsigned int idx1,idx2;
187 const struct std_name *ip,*gp;
188 int gfl,cfl;
189 unsigned int c1,c2;
190 cfl = 0;
191 c1 = 0;
192 for (idx1 = 0;
193 idx1 < sizeof(std_groups)/sizeof(std_groups[0]);
194 idx1++) {
195 gp = std_groups + idx1;
196 gfl = 0;
197 for (idx2 = 0;
198 idx2 < sizeof(std_items)/sizeof(std_items[0]);
199 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;
208 }
209 cfl = !0;
210 c2 = scnprintf(bufPtr,bufSize,
211 "%s-",gp->name);
212 gfl = !0;
213 } else {
214 c2 = scnprintf(bufPtr,bufSize,"/");
215 }
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;
224 }
225 }
226 return c1;
227}
228
229
230// Template data for possible enumerated video standards. Here we group
231// standards which share common frame rates and resolution.
232static struct v4l2_standard generic_standards[] = {
233 {
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 =
244 {
245 .numerator = 1,
246 .denominator= 25
247 },
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 =
255 {
256 .numerator = 1001,
257 .denominator= 30000
258 },
259 .framelines = 525,
260 .reserved = {0,0,0,0}
261 }, { // This is a total wild guess
262 .id = (TSTD_60),
263 .frameperiod =
264 {
265 .numerator = 1001,
266 .denominator= 30000
267 },
268 .framelines = 525,
269 .reserved = {0,0,0,0}
270 }, { // This is total wild guess
271 .id = V4L2_STD_NTSC_443,
272 .frameperiod =
273 {
274 .numerator = 1001,
275 .denominator= 30000
276 },
277 .framelines = 525,
278 .reserved = {0,0,0,0}
279 }
280};
281
282#define generic_standards_cnt (sizeof(generic_standards)/sizeof(generic_standards[0]))
283
284static struct v4l2_standard *match_std(v4l2_std_id id)
285{
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;
290 }
291 }
a0fd1cb1 292 return NULL;
d855497e
MI
293}
294
295static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
296{
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_INIT,"Set up standard idx=%u name=%s",
309 std->index,std->name);
310 return !0;
311}
312
313/* These are special cases of combined standards that we should enumerate
314 separately if the component pieces are present. */
315static 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,
320};
321
322struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
323 v4l2_std_id id)
324{
325 unsigned int std_cnt = 0;
326 unsigned int idx,bcnt,idx2;
327 v4l2_std_id idmsk,cmsk,fmsk;
328 struct v4l2_standard *stddefs;
329
330 if (pvrusb2_debug & PVR2_TRACE_INIT) {
331 char buf[50];
332 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
333 pvr2_trace(
334 PVR2_TRACE_INIT,"Mapping standards mask=0x%x (%.*s)",
335 (int)id,bcnt,buf);
336 }
337
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;
347 }
348 fmsk |= idmsk;
349 }
350
351 for (idx2 = 0; idx2 < sizeof(std_mixes)/sizeof(std_mixes[0]); idx2++) {
352 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
353 }
354
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);
363 }
364
365 pvr2_trace(PVR2_TRACE_INIT,"Setting up %u unique standard(s)",
366 std_cnt);
a0fd1cb1 367 if (!std_cnt) return NULL; // paranoia
d855497e
MI
368
369 stddefs = kmalloc(sizeof(struct v4l2_standard) * std_cnt,
370 GFP_KERNEL);
371 memset(stddefs,0,sizeof(struct v4l2_standard) * std_cnt);
372 for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
373
374 idx = 0;
375
376 /* Enumerate potential special cases */
377 for (idx2 = 0; ((idx2 < sizeof(std_mixes)/sizeof(std_mixes[0])) &&
378 (idx < std_cnt)); idx2++) {
379 if (!(id & std_mixes[idx2])) continue;
380 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
381 }
382 /* Now enumerate individual pieces */
383 for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
384 if (!(idmsk & cmsk)) continue;
385 cmsk &= ~idmsk;
386 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
387 idx++;
388 }
389
390 *countptr = std_cnt;
391 return stddefs;
392}
393
394v4l2_std_id pvr2_std_get_usable(void)
395{
396 return CSTD_ALL;
397}
398
399
400/*
401 Stuff for Emacs to see, in order to encourage consistent editing style:
402 *** Local Variables: ***
403 *** mode: c ***
404 *** fill-column: 75 ***
405 *** tab-width: 8 ***
406 *** c-basic-offset: 8 ***
407 *** End: ***
408 */
This page took 0.158122 seconds and 5 git commands to generate.