4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "pvrusb2-std.h"
22 #include "pvrusb2-debug.h"
23 #include <asm/string.h>
24 #include <linux/slab.h>
53 (V4L2_STD_ATSC_8_VSB| \
66 #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
67 #define TSTD_B1 (V4L2_STD_PAL_B1)
68 #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
69 #define TSTD_D1 (V4L2_STD_PAL_D1)
70 #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
71 #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
72 #define TSTD_I (V4L2_STD_PAL_I)
73 #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
74 #define TSTD_K1 (V4L2_STD_SECAM_K1)
75 #define TSTD_L (V4L2_STD_SECAM_L)
76 #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
77 #define TSTD_N (V4L2_STD_PAL_N)
78 #define TSTD_Nc (V4L2_STD_PAL_Nc)
79 #define TSTD_60 (V4L2_STD_PAL_60)
81 #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_ATSC|CSTD_SECAM)
83 /* Mapping of standard bits to color system */
84 static const struct std_name std_groups
[] = {
91 /* Mapping of standard bits to modulation system */
92 static const struct std_name std_items
[] = {
103 {"LC",V4L2_STD_SECAM_LC
},
105 {"Mj",V4L2_STD_NTSC_M_JP
},
106 {"443",V4L2_STD_NTSC_443
},
107 {"Mk",V4L2_STD_NTSC_M_KR
},
111 {"8VSB",V4L2_STD_ATSC_8_VSB
},
112 {"16VSB",V4L2_STD_ATSC_16_VSB
},
116 // Search an array of std_name structures and return a pointer to the
117 // element with the matching name.
118 static const struct std_name
*find_std_name(const struct std_name
*arrPtr
,
119 unsigned int arrSize
,
121 unsigned int bufSize
)
124 const struct std_name
*p
;
125 for (idx
= 0; idx
< arrSize
; idx
++) {
127 if (strlen(p
->name
) != bufSize
) continue;
128 if (!memcmp(bufPtr
,p
->name
,bufSize
)) return p
;
134 int pvr2_std_str_to_id(v4l2_std_id
*idPtr
,const char *bufPtr
,
135 unsigned int bufSize
)
138 v4l2_std_id cmsk
= 0;
143 const struct std_name
*sp
;
148 while ((cnt
< bufSize
) && (bufPtr
[cnt
] != '-')) cnt
++;
149 if (cnt
>= bufSize
) return 0; // No more characters
150 sp
= find_std_name(std_groups
, ARRAY_SIZE(std_groups
),
152 if (!sp
) return 0; // Illegal color system name
161 while (cnt
< bufSize
) {
167 if (ch
== '/') break;
170 sp
= find_std_name(std_items
, ARRAY_SIZE(std_items
),
172 if (!sp
) return 0; // Illegal modulation system ID
174 if (!t
) return 0; // Specific color + modulation system illegal
176 if (cnt
< bufSize
) cnt
++;
181 if (idPtr
) *idPtr
= id
;
186 unsigned int pvr2_std_id_to_str(char *bufPtr
, unsigned int bufSize
,
189 unsigned int idx1
,idx2
;
190 const struct std_name
*ip
,*gp
;
195 for (idx1
= 0; idx1
< ARRAY_SIZE(std_groups
); idx1
++) {
196 gp
= std_groups
+ idx1
;
198 for (idx2
= 0; idx2
< ARRAY_SIZE(std_items
); idx2
++) {
199 ip
= std_items
+ idx2
;
200 if (!(gp
->id
& ip
->id
& id
)) continue;
203 c2
= scnprintf(bufPtr
,bufSize
,";");
209 c2
= scnprintf(bufPtr
,bufSize
,
213 c2
= scnprintf(bufPtr
,bufSize
,"/");
218 c2
= scnprintf(bufPtr
,bufSize
,
229 // Template data for possible enumerated video standards. Here we group
230 // standards which share common frame rates and resolution.
231 static struct v4l2_standard generic_standards
[] = {
233 .id
= (TSTD_B
|TSTD_B1
|
248 .reserved
= {0,0,0,0}
259 .reserved
= {0,0,0,0}
260 }, { // This is a total wild guess
268 .reserved
= {0,0,0,0}
269 }, { // This is total wild guess
270 .id
= V4L2_STD_NTSC_443
,
277 .reserved
= {0,0,0,0}
281 static struct v4l2_standard
*match_std(v4l2_std_id id
)
284 for (idx
= 0; idx
< ARRAY_SIZE(generic_standards
); idx
++) {
285 if (generic_standards
[idx
].id
& id
) {
286 return generic_standards
+ idx
;
292 static int pvr2_std_fill(struct v4l2_standard
*std
,v4l2_std_id id
)
294 struct v4l2_standard
*template;
297 template = match_std(id
);
298 if (!template) return 0;
300 memcpy(std
,template,sizeof(*template));
303 bcnt
= pvr2_std_id_to_str(std
->name
,sizeof(std
->name
)-1,id
);
305 pvr2_trace(PVR2_TRACE_STD
,"Set up standard idx=%u name=%s",
306 std
->index
,std
->name
);
310 /* These are special cases of combined standards that we should enumerate
311 separately if the component pieces are present. */
312 static v4l2_std_id std_mixes
[] = {
313 V4L2_STD_PAL_B
| V4L2_STD_PAL_G
,
314 V4L2_STD_PAL_D
| V4L2_STD_PAL_K
,
315 V4L2_STD_SECAM_B
| V4L2_STD_SECAM_G
,
316 V4L2_STD_SECAM_D
| V4L2_STD_SECAM_K
,
319 struct v4l2_standard
*pvr2_std_create_enum(unsigned int *countptr
,
322 unsigned int std_cnt
= 0;
323 unsigned int idx
,bcnt
,idx2
;
324 v4l2_std_id idmsk
,cmsk
,fmsk
;
325 struct v4l2_standard
*stddefs
;
327 if (pvrusb2_debug
& PVR2_TRACE_STD
) {
329 bcnt
= pvr2_std_id_to_str(buf
,sizeof(buf
),id
);
331 PVR2_TRACE_STD
,"Mapping standards mask=0x%x (%.*s)",
338 for (idmsk
= 1, cmsk
= id
; cmsk
; idmsk
<<= 1) {
339 if (!(idmsk
& cmsk
)) continue;
341 if (match_std(idmsk
)) {
348 for (idx2
= 0; idx2
< ARRAY_SIZE(std_mixes
); idx2
++) {
349 if ((id
& std_mixes
[idx2
]) == std_mixes
[idx2
]) std_cnt
++;
352 /* Don't complain about ATSC standard values */
357 bcnt
= pvr2_std_id_to_str(buf
,sizeof(buf
),fmsk
);
359 PVR2_TRACE_ERROR_LEGS
,
361 " Failed to classify the following standard(s): %.*s",
365 pvr2_trace(PVR2_TRACE_STD
,"Setting up %u unique standard(s)",
367 if (!std_cnt
) return NULL
; // paranoia
369 stddefs
= kzalloc(sizeof(struct v4l2_standard
) * std_cnt
,
374 for (idx
= 0; idx
< std_cnt
; idx
++)
375 stddefs
[idx
].index
= idx
;
379 /* Enumerate potential special cases */
380 for (idx2
= 0; (idx2
< ARRAY_SIZE(std_mixes
)) && (idx
< std_cnt
);
382 if (!(id
& std_mixes
[idx2
])) continue;
383 if (pvr2_std_fill(stddefs
+idx
,std_mixes
[idx2
])) idx
++;
385 /* Now enumerate individual pieces */
386 for (idmsk
= 1, cmsk
= id
; cmsk
&& (idx
< std_cnt
); idmsk
<<= 1) {
387 if (!(idmsk
& cmsk
)) continue;
389 if (!pvr2_std_fill(stddefs
+idx
,idmsk
)) continue;
397 v4l2_std_id
pvr2_std_get_usable(void)
404 Stuff for Emacs to see, in order to encourage consistent editing style:
405 *** Local Variables: ***
407 *** fill-column: 75 ***
409 *** c-basic-offset: 8 ***