xref: /linux/drivers/media/usb/pvrusb2/pvrusb2-std.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5  */
6 
7 #include "pvrusb2-std.h"
8 #include "pvrusb2-debug.h"
9 #include <asm/string.h>
10 #include <linux/slab.h>
11 
12 struct std_name {
13 	const char *name;
14 	v4l2_std_id id;
15 };
16 
17 
18 #define CSTD_PAL \
19 	(V4L2_STD_PAL_B| \
20 	 V4L2_STD_PAL_B1| \
21 	 V4L2_STD_PAL_G| \
22 	 V4L2_STD_PAL_H| \
23 	 V4L2_STD_PAL_I| \
24 	 V4L2_STD_PAL_D| \
25 	 V4L2_STD_PAL_D1| \
26 	 V4L2_STD_PAL_K| \
27 	 V4L2_STD_PAL_M| \
28 	 V4L2_STD_PAL_N| \
29 	 V4L2_STD_PAL_Nc| \
30 	 V4L2_STD_PAL_60)
31 
32 #define CSTD_NTSC \
33 	(V4L2_STD_NTSC_M| \
34 	 V4L2_STD_NTSC_M_JP| \
35 	 V4L2_STD_NTSC_M_KR| \
36 	 V4L2_STD_NTSC_443)
37 
38 #define CSTD_ATSC \
39 	(V4L2_STD_ATSC_8_VSB| \
40 	 V4L2_STD_ATSC_16_VSB)
41 
42 #define CSTD_SECAM \
43 	(V4L2_STD_SECAM_B| \
44 	 V4L2_STD_SECAM_D| \
45 	 V4L2_STD_SECAM_G| \
46 	 V4L2_STD_SECAM_H| \
47 	 V4L2_STD_SECAM_K| \
48 	 V4L2_STD_SECAM_K1| \
49 	 V4L2_STD_SECAM_L| \
50 	 V4L2_STD_SECAM_LC)
51 
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)
66 
67 #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_ATSC|CSTD_SECAM)
68 
69 /* Mapping of standard bits to color system */
70 static const struct std_name std_groups[] = {
71 	{"PAL",CSTD_PAL},
72 	{"NTSC",CSTD_NTSC},
73 	{"SECAM",CSTD_SECAM},
74 	{"ATSC",CSTD_ATSC},
75 };
76 
77 /* Mapping of standard bits to modulation system */
78 static const struct std_name std_items[] = {
79 	{"B",TSTD_B},
80 	{"B1",TSTD_B1},
81 	{"D",TSTD_D},
82 	{"D1",TSTD_D1},
83 	{"G",TSTD_G},
84 	{"H",TSTD_H},
85 	{"I",TSTD_I},
86 	{"K",TSTD_K},
87 	{"K1",TSTD_K1},
88 	{"L",TSTD_L},
89 	{"LC",V4L2_STD_SECAM_LC},
90 	{"M",TSTD_M},
91 	{"Mj",V4L2_STD_NTSC_M_JP},
92 	{"443",V4L2_STD_NTSC_443},
93 	{"Mk",V4L2_STD_NTSC_M_KR},
94 	{"N",TSTD_N},
95 	{"Nc",TSTD_Nc},
96 	{"60",TSTD_60},
97 	{"8VSB",V4L2_STD_ATSC_8_VSB},
98 	{"16VSB",V4L2_STD_ATSC_16_VSB},
99 };
100 
101 
102 // Search an array of std_name structures and return a pointer to the
103 // element with the matching name.
find_std_name(const struct std_name * arrPtr,unsigned int arrSize,const char * bufPtr,unsigned int bufSize)104 static const struct std_name *find_std_name(const struct std_name *arrPtr,
105 					    unsigned int arrSize,
106 					    const char *bufPtr,
107 					    unsigned int bufSize)
108 {
109 	unsigned int idx;
110 	const struct std_name *p;
111 	for (idx = 0; idx < arrSize; idx++) {
112 		p = arrPtr + idx;
113 		if (strlen(p->name) != bufSize) continue;
114 		if (!memcmp(bufPtr,p->name,bufSize)) return p;
115 	}
116 	return NULL;
117 }
118 
119 
pvr2_std_str_to_id(v4l2_std_id * idPtr,const char * bufPtr,unsigned int bufSize)120 int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
121 		       unsigned int bufSize)
122 {
123 	v4l2_std_id id = 0;
124 	v4l2_std_id cmsk = 0;
125 	v4l2_std_id t;
126 	int mMode = 0;
127 	unsigned int cnt;
128 	char ch;
129 	const struct std_name *sp;
130 
131 	while (bufSize) {
132 		if (!mMode) {
133 			cnt = 0;
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),
137 					   bufPtr,cnt);
138 			if (!sp) return 0; // Illegal color system name
139 			cnt++;
140 			bufPtr += cnt;
141 			bufSize -= cnt;
142 			mMode = !0;
143 			cmsk = sp->id;
144 			continue;
145 		}
146 		cnt = 0;
147 		while (cnt < bufSize) {
148 			ch = bufPtr[cnt];
149 			if (ch == ';') {
150 				mMode = 0;
151 				break;
152 			}
153 			if (ch == '/') break;
154 			cnt++;
155 		}
156 		sp = find_std_name(std_items, ARRAY_SIZE(std_items),
157 				   bufPtr,cnt);
158 		if (!sp) return 0; // Illegal modulation system ID
159 		t = sp->id & cmsk;
160 		if (!t) return 0; // Specific color + modulation system illegal
161 		id |= t;
162 		if (cnt < bufSize) cnt++;
163 		bufPtr += cnt;
164 		bufSize -= cnt;
165 	}
166 
167 	if (idPtr) *idPtr = id;
168 	return !0;
169 }
170 
171 
pvr2_std_id_to_str(char * bufPtr,unsigned int bufSize,v4l2_std_id id)172 unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
173 				v4l2_std_id id)
174 {
175 	unsigned int idx1,idx2;
176 	const struct std_name *ip,*gp;
177 	int gfl,cfl;
178 	unsigned int c1,c2;
179 	cfl = 0;
180 	c1 = 0;
181 	for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
182 		gp = std_groups + idx1;
183 		gfl = 0;
184 		for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
185 			ip = std_items + idx2;
186 			if (!(gp->id & ip->id & id)) continue;
187 			if (!gfl) {
188 				if (cfl) {
189 					c2 = scnprintf(bufPtr,bufSize,";");
190 					c1 += c2;
191 					bufSize -= c2;
192 					bufPtr += c2;
193 				}
194 				cfl = !0;
195 				c2 = scnprintf(bufPtr,bufSize,
196 					       "%s-",gp->name);
197 				gfl = !0;
198 			} else {
199 				c2 = scnprintf(bufPtr,bufSize,"/");
200 			}
201 			c1 += c2;
202 			bufSize -= c2;
203 			bufPtr += c2;
204 			c2 = scnprintf(bufPtr,bufSize,
205 				       ip->name);
206 			c1 += c2;
207 			bufSize -= c2;
208 			bufPtr += c2;
209 		}
210 	}
211 	return c1;
212 }
213 
214 
pvr2_std_get_usable(void)215 v4l2_std_id pvr2_std_get_usable(void)
216 {
217 	return CSTD_ALL;
218 }
219