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>
54 (V4L2_STD_ATSC_8_VSB| \
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
[] = {
92 /* Mapping of standard bits to modulation system */
93 static const struct std_name std_items
[] = {
104 {"LC",V4L2_STD_SECAM_LC
},
106 {"Mj",V4L2_STD_NTSC_M_JP
},
107 {"443",V4L2_STD_NTSC_443
},
108 {"Mk",V4L2_STD_NTSC_M_KR
},
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
,
122 unsigned int bufSize
)
125 const struct std_name
*p
;
126 for (idx
= 0; idx
< arrSize
; idx
++) {
128 if (strlen(p
->name
) != bufSize
) continue;
129 if (!memcmp(bufPtr
,p
->name
,bufSize
)) return p
;
135 int pvr2_std_str_to_id(v4l2_std_id
*idPtr
,const char *bufPtr
,
136 unsigned int bufSize
)
139 v4l2_std_id cmsk
= 0;
144 const struct std_name
*sp
;
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
),
153 if (!sp
) return 0; // Illegal color system name
162 while (cnt
< bufSize
) {
168 if (ch
== '/') break;
171 sp
= find_std_name(std_items
, ARRAY_SIZE(std_items
),
173 if (!sp
) return 0; // Illegal modulation system ID
175 if (!t
) return 0; // Specific color + modulation system illegal
177 if (cnt
< bufSize
) cnt
++;
182 if (idPtr
) *idPtr
= id
;
187 unsigned int pvr2_std_id_to_str(char *bufPtr
, unsigned int bufSize
,
190 unsigned int idx1
,idx2
;
191 const struct std_name
*ip
,*gp
;
196 for (idx1
= 0; idx1
< ARRAY_SIZE(std_groups
); idx1
++) {
197 gp
= std_groups
+ idx1
;
199 for (idx2
= 0; idx2
< ARRAY_SIZE(std_items
); idx2
++) {
200 ip
= std_items
+ idx2
;
201 if (!(gp
->id
& ip
->id
& id
)) continue;
204 c2
= scnprintf(bufPtr
,bufSize
,";");
210 c2
= scnprintf(bufPtr
,bufSize
,
214 c2
= scnprintf(bufPtr
,bufSize
,"/");
219 c2
= scnprintf(bufPtr
,bufSize
,
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
|
249 .reserved
= {0,0,0,0}
260 .reserved
= {0,0,0,0}
261 }, { // This is a total wild guess
269 .reserved
= {0,0,0,0}
270 }, { // This is total wild guess
271 .id
= V4L2_STD_NTSC_443
,
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
)
287 for (idx
= 0; idx
< generic_standards_cnt
; idx
++) {
288 if (generic_standards
[idx
].id
& id
) {
289 return generic_standards
+ idx
;
295 static int pvr2_std_fill(struct v4l2_standard
*std
,v4l2_std_id id
)
297 struct v4l2_standard
*template;
300 template = match_std(id
);
301 if (!template) return 0;
303 memcpy(std
,template,sizeof(*template));
306 bcnt
= pvr2_std_id_to_str(std
->name
,sizeof(std
->name
)-1,id
);
308 pvr2_trace(PVR2_TRACE_STD
,"Set up standard idx=%u name=%s",
309 std
->index
,std
->name
);
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
,
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
) {
332 bcnt
= pvr2_std_id_to_str(buf
,sizeof(buf
),id
);
334 PVR2_TRACE_STD
,"Mapping standards mask=0x%x (%.*s)",
341 for (idmsk
= 1, cmsk
= id
; cmsk
; idmsk
<<= 1) {
342 if (!(idmsk
& cmsk
)) continue;
344 if (match_std(idmsk
)) {
351 for (idx2
= 0; idx2
< ARRAY_SIZE(std_mixes
); idx2
++) {
352 if ((id
& std_mixes
[idx2
]) == std_mixes
[idx2
]) std_cnt
++;
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
,
371 for (idx
= 0; idx
< std_cnt
; idx
++) stddefs
[idx
].index
= idx
;
375 /* Enumerate potential special cases */
376 for (idx2
= 0; (idx2
< ARRAY_SIZE(std_mixes
)) && (idx
< std_cnt
);
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;
385 if (!pvr2_std_fill(stddefs
+idx
,idmsk
)) continue;
393 v4l2_std_id
pvr2_std_get_usable(void)
400 Stuff for Emacs to see, in order to encourage consistent editing style:
401 *** Local Variables: ***
403 *** fill-column: 75 ***
405 *** c-basic-offset: 8 ***