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>
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)
78 #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM)
80 /* Mapping of standard bits to color system */
81 static const struct std_name std_groups
[] = {
87 /* Mapping of standard bits to modulation system */
88 static const struct std_name std_items
[] = {
99 {"LC",V4L2_STD_SECAM_LC
},
101 {"Mj",V4L2_STD_NTSC_M_JP
},
102 {"443",V4L2_STD_NTSC_443
},
103 {"Mk",V4L2_STD_NTSC_M_KR
},
110 // Search an array of std_name structures and return a pointer to the
111 // element with the matching name.
112 static const struct std_name
*find_std_name(const struct std_name
*arrPtr
,
113 unsigned int arrSize
,
115 unsigned int bufSize
)
118 const struct std_name
*p
;
119 for (idx
= 0; idx
< arrSize
; idx
++) {
121 if (strlen(p
->name
) != bufSize
) continue;
122 if (!memcmp(bufPtr
,p
->name
,bufSize
)) return p
;
128 int pvr2_std_str_to_id(v4l2_std_id
*idPtr
,const char *bufPtr
,
129 unsigned int bufSize
)
132 v4l2_std_id cmsk
= 0;
137 const struct std_name
*sp
;
142 while ((cnt
< bufSize
) && (bufPtr
[cnt
] != '-')) cnt
++;
143 if (cnt
>= bufSize
) return 0; // No more characters
144 sp
= find_std_name(std_groups
, ARRAY_SIZE(std_groups
),
146 if (!sp
) return 0; // Illegal color system name
155 while (cnt
< bufSize
) {
161 if (ch
== '/') break;
164 sp
= find_std_name(std_items
, ARRAY_SIZE(std_items
),
166 if (!sp
) return 0; // Illegal modulation system ID
168 if (!t
) return 0; // Specific color + modulation system illegal
170 if (cnt
< bufSize
) cnt
++;
175 if (idPtr
) *idPtr
= id
;
180 unsigned int pvr2_std_id_to_str(char *bufPtr
, unsigned int bufSize
,
183 unsigned int idx1
,idx2
;
184 const struct std_name
*ip
,*gp
;
189 for (idx1
= 0; idx1
< ARRAY_SIZE(std_groups
); idx1
++) {
190 gp
= std_groups
+ idx1
;
192 for (idx2
= 0; idx2
< ARRAY_SIZE(std_items
); idx2
++) {
193 ip
= std_items
+ idx2
;
194 if (!(gp
->id
& ip
->id
& id
)) continue;
197 c2
= scnprintf(bufPtr
,bufSize
,";");
203 c2
= scnprintf(bufPtr
,bufSize
,
207 c2
= scnprintf(bufPtr
,bufSize
,"/");
212 c2
= scnprintf(bufPtr
,bufSize
,
223 // Template data for possible enumerated video standards. Here we group
224 // standards which share common frame rates and resolution.
225 static struct v4l2_standard generic_standards
[] = {
227 .id
= (TSTD_B
|TSTD_B1
|
242 .reserved
= {0,0,0,0}
253 .reserved
= {0,0,0,0}
254 }, { // This is a total wild guess
262 .reserved
= {0,0,0,0}
263 }, { // This is total wild guess
264 .id
= V4L2_STD_NTSC_443
,
271 .reserved
= {0,0,0,0}
275 #define generic_standards_cnt ARRAY_SIZE(generic_standards)
277 static struct v4l2_standard
*match_std(v4l2_std_id id
)
280 for (idx
= 0; idx
< generic_standards_cnt
; idx
++) {
281 if (generic_standards
[idx
].id
& id
) {
282 return generic_standards
+ idx
;
288 static int pvr2_std_fill(struct v4l2_standard
*std
,v4l2_std_id id
)
290 struct v4l2_standard
*template;
293 template = match_std(id
);
294 if (!template) return 0;
296 memcpy(std
,template,sizeof(*template));
299 bcnt
= pvr2_std_id_to_str(std
->name
,sizeof(std
->name
)-1,id
);
301 pvr2_trace(PVR2_TRACE_INIT
,"Set up standard idx=%u name=%s",
302 std
->index
,std
->name
);
306 /* These are special cases of combined standards that we should enumerate
307 separately if the component pieces are present. */
308 static 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
,
315 struct v4l2_standard
*pvr2_std_create_enum(unsigned int *countptr
,
318 unsigned int std_cnt
= 0;
319 unsigned int idx
,bcnt
,idx2
;
320 v4l2_std_id idmsk
,cmsk
,fmsk
;
321 struct v4l2_standard
*stddefs
;
323 if (pvrusb2_debug
& PVR2_TRACE_INIT
) {
325 bcnt
= pvr2_std_id_to_str(buf
,sizeof(buf
),id
);
327 PVR2_TRACE_INIT
,"Mapping standards mask=0x%x (%.*s)",
334 for (idmsk
= 1, cmsk
= id
; cmsk
; idmsk
<<= 1) {
335 if (!(idmsk
& cmsk
)) continue;
337 if (match_std(idmsk
)) {
344 for (idx2
= 0; idx2
< ARRAY_SIZE(std_mixes
); idx2
++) {
345 if ((id
& std_mixes
[idx2
]) == std_mixes
[idx2
]) std_cnt
++;
350 bcnt
= pvr2_std_id_to_str(buf
,sizeof(buf
),fmsk
);
352 PVR2_TRACE_ERROR_LEGS
,
354 " Failed to classify the following standard(s): %.*s",
358 pvr2_trace(PVR2_TRACE_INIT
,"Setting up %u unique standard(s)",
360 if (!std_cnt
) return NULL
; // paranoia
362 stddefs
= kzalloc(sizeof(struct v4l2_standard
) * std_cnt
,
364 for (idx
= 0; idx
< std_cnt
; idx
++) stddefs
[idx
].index
= idx
;
368 /* Enumerate potential special cases */
369 for (idx2
= 0; (idx2
< ARRAY_SIZE(std_mixes
)) && (idx
< std_cnt
);
371 if (!(id
& std_mixes
[idx2
])) continue;
372 if (pvr2_std_fill(stddefs
+idx
,std_mixes
[idx2
])) idx
++;
374 /* Now enumerate individual pieces */
375 for (idmsk
= 1, cmsk
= id
; cmsk
&& (idx
< std_cnt
); idmsk
<<= 1) {
376 if (!(idmsk
& cmsk
)) continue;
378 if (!pvr2_std_fill(stddefs
+idx
,idmsk
)) continue;
386 v4l2_std_id
pvr2_std_get_usable(void)
393 Stuff for Emacs to see, in order to encourage consistent editing style:
394 *** Local Variables: ***
396 *** fill-column: 75 ***
398 *** c-basic-offset: 8 ***