1 // SPDX-License-Identifier: GPL-2.0-only
7 #include "pvrusb2-std.h"
8 #include "pvrusb2-debug.h"
9 #include <asm/string.h>
10 #include <linux/slab.h>
39 (V4L2_STD_ATSC_8_VSB| \
52 #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
53 #define TSTD_B1 (V4L2_STD_PAL_B1)
54 #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
55 #define TSTD_D1 (V4L2_STD_PAL_D1)
56 #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
57 #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
58 #define TSTD_I (V4L2_STD_PAL_I)
59 #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
60 #define TSTD_K1 (V4L2_STD_SECAM_K1)
61 #define TSTD_L (V4L2_STD_SECAM_L)
62 #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
63 #define TSTD_N (V4L2_STD_PAL_N)
64 #define TSTD_Nc (V4L2_STD_PAL_Nc)
65 #define TSTD_60 (V4L2_STD_PAL_60)
67 #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_ATSC|CSTD_SECAM)
69 /* Mapping of standard bits to color system */
70 static const struct std_name std_groups[] = {
77 /* Mapping of standard bits to modulation system */
78 static const struct std_name std_items[] = {
89 {"LC",V4L2_STD_SECAM_LC},
91 {"Mj",V4L2_STD_NTSC_M_JP},
92 {"443",V4L2_STD_NTSC_443},
93 {"Mk",V4L2_STD_NTSC_M_KR},
97 {"8VSB",V4L2_STD_ATSC_8_VSB},
98 {"16VSB",V4L2_STD_ATSC_16_VSB},
102 // Search an array of std_name structures and return a pointer to the
103 // element with the matching name.
104 static const struct std_name *find_std_name(const struct std_name *arrPtr,
105 unsigned int arrSize,
107 unsigned int bufSize)
110 const struct std_name *p;
111 for (idx = 0; idx < arrSize; idx++) {
113 if (strlen(p->name) != bufSize) continue;
114 if (!memcmp(bufPtr,p->name,bufSize)) return p;
120 int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
121 unsigned int bufSize)
124 v4l2_std_id cmsk = 0;
129 const struct std_name *sp;
134 while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
135 if (cnt >= bufSize) return 0; // No more characters
136 sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
138 if (!sp) return 0; // Illegal color system name
147 while (cnt < bufSize) {
153 if (ch == '/') break;
156 sp = find_std_name(std_items, ARRAY_SIZE(std_items),
158 if (!sp) return 0; // Illegal modulation system ID
160 if (!t) return 0; // Specific color + modulation system illegal
162 if (cnt < bufSize) cnt++;
167 if (idPtr) *idPtr = id;
172 unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
175 unsigned int idx1,idx2;
176 const struct std_name *ip,*gp;
181 for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
182 gp = std_groups + idx1;
184 for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
185 ip = std_items + idx2;
186 if (!(gp->id & ip->id & id)) continue;
189 c2 = scnprintf(bufPtr,bufSize,";");
195 c2 = scnprintf(bufPtr,bufSize,
199 c2 = scnprintf(bufPtr,bufSize,"/");
204 c2 = scnprintf(bufPtr,bufSize,
215 // Template data for possible enumerated video standards. Here we group
216 // standards which share common frame rates and resolution.
217 static struct v4l2_standard generic_standards[] = {
219 .id = (TSTD_B|TSTD_B1|
234 .reserved = {0,0,0,0}
245 .reserved = {0,0,0,0}
246 }, { // This is a total wild guess
254 .reserved = {0,0,0,0}
255 }, { // This is total wild guess
256 .id = V4L2_STD_NTSC_443,
263 .reserved = {0,0,0,0}
267 static struct v4l2_standard *match_std(v4l2_std_id id)
270 for (idx = 0; idx < ARRAY_SIZE(generic_standards); idx++) {
271 if (generic_standards[idx].id & id) {
272 return generic_standards + idx;
278 static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
280 struct v4l2_standard *template;
283 template = match_std(id);
284 if (!template) return 0;
286 memcpy(std,template,sizeof(*template));
289 bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
291 pvr2_trace(PVR2_TRACE_STD,"Set up standard idx=%u name=%s",
292 std->index,std->name);
296 /* These are special cases of combined standards that we should enumerate
297 separately if the component pieces are present. */
298 static v4l2_std_id std_mixes[] = {
299 V4L2_STD_PAL_B | V4L2_STD_PAL_G,
300 V4L2_STD_PAL_D | V4L2_STD_PAL_K,
301 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
302 V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
305 struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
308 unsigned int std_cnt = 0;
309 unsigned int idx,bcnt,idx2;
310 v4l2_std_id idmsk,cmsk,fmsk;
311 struct v4l2_standard *stddefs;
313 if (pvrusb2_debug & PVR2_TRACE_STD) {
315 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
317 PVR2_TRACE_STD,"Mapping standards mask=0x%x (%.*s)",
324 for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
325 if (!(idmsk & cmsk)) continue;
327 if (match_std(idmsk)) {
334 for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
335 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
338 /* Don't complain about ATSC standard values */
343 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
345 PVR2_TRACE_ERROR_LEGS,
346 "***WARNING*** Failed to classify the following standard(s): %.*s",
350 pvr2_trace(PVR2_TRACE_STD,"Setting up %u unique standard(s)",
352 if (!std_cnt) return NULL; // paranoia
354 stddefs = kcalloc(std_cnt, sizeof(struct v4l2_standard),
359 for (idx = 0; idx < std_cnt; idx++)
360 stddefs[idx].index = idx;
364 /* Enumerate potential special cases */
365 for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
367 if (!(id & std_mixes[idx2])) continue;
368 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
370 /* Now enumerate individual pieces */
371 for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
372 if (!(idmsk & cmsk)) continue;
374 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
382 v4l2_std_id pvr2_std_get_usable(void)