V4L/DVB (6709): pvrusb2: minor rework for default video standard handling
[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
eca8ebfc
AD
144 sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
145 bufPtr,cnt);
d855497e
MI
146 if (!sp) return 0; // Illegal color system name
147 cnt++;
148 bufPtr += cnt;
149 bufSize -= cnt;
150 mMode = !0;
151 cmsk = sp->id;
152 continue;
153 }
154 cnt = 0;
155 while (cnt < bufSize) {
156 ch = bufPtr[cnt];
157 if (ch == ';') {
158 mMode = 0;
159 break;
160 }
161 if (ch == '/') break;
162 cnt++;
163 }
eca8ebfc 164 sp = find_std_name(std_items, ARRAY_SIZE(std_items),
d855497e
MI
165 bufPtr,cnt);
166 if (!sp) return 0; // Illegal modulation system ID
167 t = sp->id & cmsk;
168 if (!t) return 0; // Specific color + modulation system illegal
169 id |= t;
170 if (cnt < bufSize) cnt++;
171 bufPtr += cnt;
172 bufSize -= cnt;
173 }
174
175 if (idPtr) *idPtr = id;
176 return !0;
177}
178
179
180unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
181 v4l2_std_id id)
182{
183 unsigned int idx1,idx2;
184 const struct std_name *ip,*gp;
185 int gfl,cfl;
186 unsigned int c1,c2;
187 cfl = 0;
188 c1 = 0;
eca8ebfc 189 for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
d855497e
MI
190 gp = std_groups + idx1;
191 gfl = 0;
eca8ebfc 192 for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
d855497e
MI
193 ip = std_items + idx2;
194 if (!(gp->id & ip->id & id)) continue;
195 if (!gfl) {
196 if (cfl) {
197 c2 = scnprintf(bufPtr,bufSize,";");
198 c1 += c2;
199 bufSize -= c2;
200 bufPtr += c2;
201 }
202 cfl = !0;
203 c2 = scnprintf(bufPtr,bufSize,
204 "%s-",gp->name);
205 gfl = !0;
206 } else {
207 c2 = scnprintf(bufPtr,bufSize,"/");
208 }
209 c1 += c2;
210 bufSize -= c2;
211 bufPtr += c2;
212 c2 = scnprintf(bufPtr,bufSize,
213 ip->name);
214 c1 += c2;
215 bufSize -= c2;
216 bufPtr += c2;
217 }
218 }
219 return c1;
220}
221
222
223// Template data for possible enumerated video standards. Here we group
224// standards which share common frame rates and resolution.
225static struct v4l2_standard generic_standards[] = {
226 {
227 .id = (TSTD_B|TSTD_B1|
228 TSTD_D|TSTD_D1|
229 TSTD_G|
230 TSTD_H|
231 TSTD_I|
232 TSTD_K|TSTD_K1|
233 TSTD_L|
234 V4L2_STD_SECAM_LC |
235 TSTD_N|TSTD_Nc),
236 .frameperiod =
237 {
238 .numerator = 1,
239 .denominator= 25
240 },
241 .framelines = 625,
242 .reserved = {0,0,0,0}
243 }, {
244 .id = (TSTD_M|
245 V4L2_STD_NTSC_M_JP|
246 V4L2_STD_NTSC_M_KR),
247 .frameperiod =
248 {
249 .numerator = 1001,
250 .denominator= 30000
251 },
252 .framelines = 525,
253 .reserved = {0,0,0,0}
254 }, { // This is a total wild guess
255 .id = (TSTD_60),
256 .frameperiod =
257 {
258 .numerator = 1001,
259 .denominator= 30000
260 },
261 .framelines = 525,
262 .reserved = {0,0,0,0}
263 }, { // This is total wild guess
264 .id = V4L2_STD_NTSC_443,
265 .frameperiod =
266 {
267 .numerator = 1001,
268 .denominator= 30000
269 },
270 .framelines = 525,
271 .reserved = {0,0,0,0}
272 }
273};
274
eca8ebfc 275#define generic_standards_cnt ARRAY_SIZE(generic_standards)
d855497e
MI
276
277static struct v4l2_standard *match_std(v4l2_std_id id)
278{
279 unsigned int idx;
280 for (idx = 0; idx < generic_standards_cnt; idx++) {
281 if (generic_standards[idx].id & id) {
282 return generic_standards + idx;
283 }
284 }
a0fd1cb1 285 return NULL;
d855497e
MI
286}
287
288static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
289{
290 struct v4l2_standard *template;
291 int idx;
292 unsigned int bcnt;
293 template = match_std(id);
294 if (!template) return 0;
295 idx = std->index;
296 memcpy(std,template,sizeof(*template));
297 std->index = idx;
298 std->id = id;
299 bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
300 std->name[bcnt] = 0;
56585386 301 pvr2_trace(PVR2_TRACE_STD,"Set up standard idx=%u name=%s",
d855497e
MI
302 std->index,std->name);
303 return !0;
304}
305
306/* These are special cases of combined standards that we should enumerate
307 separately if the component pieces are present. */
308static v4l2_std_id std_mixes[] = {
309 V4L2_STD_PAL_B | V4L2_STD_PAL_G,
310 V4L2_STD_PAL_D | V4L2_STD_PAL_K,
311 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
312 V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
313};
314
315struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
316 v4l2_std_id id)
317{
318 unsigned int std_cnt = 0;
319 unsigned int idx,bcnt,idx2;
320 v4l2_std_id idmsk,cmsk,fmsk;
321 struct v4l2_standard *stddefs;
322
56585386 323 if (pvrusb2_debug & PVR2_TRACE_STD) {
d855497e
MI
324 char buf[50];
325 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
326 pvr2_trace(
56585386 327 PVR2_TRACE_STD,"Mapping standards mask=0x%x (%.*s)",
d855497e
MI
328 (int)id,bcnt,buf);
329 }
330
331 *countptr = 0;
332 std_cnt = 0;
333 fmsk = 0;
334 for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
335 if (!(idmsk & cmsk)) continue;
336 cmsk &= ~idmsk;
337 if (match_std(idmsk)) {
338 std_cnt++;
339 continue;
340 }
341 fmsk |= idmsk;
342 }
343
eca8ebfc 344 for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
d855497e
MI
345 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
346 }
347
348 if (fmsk) {
349 char buf[50];
350 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
351 pvr2_trace(
352 PVR2_TRACE_ERROR_LEGS,
353 "WARNING:"
354 " Failed to classify the following standard(s): %.*s",
355 bcnt,buf);
356 }
357
56585386 358 pvr2_trace(PVR2_TRACE_STD,"Setting up %u unique standard(s)",
d855497e 359 std_cnt);
a0fd1cb1 360 if (!std_cnt) return NULL; // paranoia
d855497e 361
ca545f7c 362 stddefs = kzalloc(sizeof(struct v4l2_standard) * std_cnt,
d855497e 363 GFP_KERNEL);
d855497e
MI
364 for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
365
366 idx = 0;
367
368 /* Enumerate potential special cases */
eca8ebfc
AD
369 for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
370 idx2++) {
d855497e
MI
371 if (!(id & std_mixes[idx2])) continue;
372 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
373 }
374 /* Now enumerate individual pieces */
375 for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
376 if (!(idmsk & cmsk)) continue;
377 cmsk &= ~idmsk;
378 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
379 idx++;
380 }
381
382 *countptr = std_cnt;
383 return stddefs;
384}
385
386v4l2_std_id pvr2_std_get_usable(void)
387{
388 return CSTD_ALL;
389}
390
391
392/*
393 Stuff for Emacs to see, in order to encourage consistent editing style:
394 *** Local Variables: ***
395 *** mode: c ***
396 *** fill-column: 75 ***
397 *** tab-width: 8 ***
398 *** c-basic-offset: 8 ***
399 *** End: ***
400 */
This page took 0.421207 seconds and 5 git commands to generate.