Commit | Line | Data |
---|---|---|
d855497e MI |
1 | /* |
2 | * | |
3 | * $Id$ | |
4 | * | |
5 | * Copyright (C) 2005 Mike Isely <isely@pobox.com> | |
6 | * | |
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 | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | * | |
20 | */ | |
21 | ||
22 | #include "pvrusb2-std.h" | |
23 | #include "pvrusb2-debug.h" | |
24 | ||
25 | struct std_name { | |
26 | const char *name; | |
27 | v4l2_std_id id; | |
28 | }; | |
29 | ||
30 | ||
31 | #define CSTD_PAL \ | |
32 | (V4L2_STD_PAL_B| \ | |
33 | V4L2_STD_PAL_B1| \ | |
34 | V4L2_STD_PAL_G| \ | |
35 | V4L2_STD_PAL_H| \ | |
36 | V4L2_STD_PAL_I| \ | |
37 | V4L2_STD_PAL_D| \ | |
38 | V4L2_STD_PAL_D1| \ | |
39 | V4L2_STD_PAL_K| \ | |
40 | V4L2_STD_PAL_M| \ | |
41 | V4L2_STD_PAL_N| \ | |
42 | V4L2_STD_PAL_Nc| \ | |
43 | V4L2_STD_PAL_60) | |
44 | ||
45 | #define CSTD_NTSC \ | |
46 | (V4L2_STD_NTSC_M| \ | |
47 | V4L2_STD_NTSC_M_JP| \ | |
48 | V4L2_STD_NTSC_M_KR| \ | |
49 | V4L2_STD_NTSC_443) | |
50 | ||
51 | #define CSTD_SECAM \ | |
52 | (V4L2_STD_SECAM_B| \ | |
53 | V4L2_STD_SECAM_D| \ | |
54 | V4L2_STD_SECAM_G| \ | |
55 | V4L2_STD_SECAM_H| \ | |
56 | V4L2_STD_SECAM_K| \ | |
57 | V4L2_STD_SECAM_K1| \ | |
58 | V4L2_STD_SECAM_L| \ | |
59 | V4L2_STD_SECAM_LC) | |
60 | ||
61 | #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B) | |
62 | #define TSTD_B1 (V4L2_STD_PAL_B1) | |
63 | #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D) | |
64 | #define TSTD_D1 (V4L2_STD_PAL_D1) | |
65 | #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G) | |
66 | #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H) | |
67 | #define TSTD_I (V4L2_STD_PAL_I) | |
68 | #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K) | |
69 | #define TSTD_K1 (V4L2_STD_SECAM_K1) | |
70 | #define TSTD_L (V4L2_STD_SECAM_L) | |
71 | #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M) | |
72 | #define TSTD_N (V4L2_STD_PAL_N) | |
73 | #define TSTD_Nc (V4L2_STD_PAL_Nc) | |
74 | #define TSTD_60 (V4L2_STD_PAL_60) | |
75 | ||
76 | #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM) | |
77 | ||
78 | /* Mapping of standard bits to color system */ | |
79 | const static struct std_name std_groups[] = { | |
80 | {"PAL",CSTD_PAL}, | |
81 | {"NTSC",CSTD_NTSC}, | |
82 | {"SECAM",CSTD_SECAM}, | |
83 | }; | |
84 | ||
85 | /* Mapping of standard bits to modulation system */ | |
86 | const static struct std_name std_items[] = { | |
87 | {"B",TSTD_B}, | |
88 | {"B1",TSTD_B1}, | |
89 | {"D",TSTD_D}, | |
90 | {"D1",TSTD_D1}, | |
91 | {"G",TSTD_G}, | |
92 | {"H",TSTD_H}, | |
93 | {"I",TSTD_I}, | |
94 | {"K",TSTD_K}, | |
95 | {"K1",TSTD_K1}, | |
96 | {"L",TSTD_L}, | |
97 | {"LC",V4L2_STD_SECAM_LC}, | |
98 | {"M",TSTD_M}, | |
99 | {"Mj",V4L2_STD_NTSC_M_JP}, | |
100 | {"443",V4L2_STD_NTSC_443}, | |
101 | {"Mk",V4L2_STD_NTSC_M_KR}, | |
102 | {"N",TSTD_N}, | |
103 | {"Nc",TSTD_Nc}, | |
104 | {"60",TSTD_60}, | |
105 | }; | |
106 | ||
107 | ||
108 | // Search an array of std_name structures and return a pointer to the | |
109 | // element with the matching name. | |
110 | static const struct std_name *find_std_name(const struct std_name *arrPtr, | |
111 | unsigned int arrSize, | |
112 | const char *bufPtr, | |
113 | unsigned int bufSize) | |
114 | { | |
115 | unsigned int idx; | |
116 | const struct std_name *p; | |
117 | for (idx = 0; idx < arrSize; idx++) { | |
118 | p = arrPtr + idx; | |
119 | if (strlen(p->name) != bufSize) continue; | |
120 | if (!memcmp(bufPtr,p->name,bufSize)) return p; | |
121 | } | |
122 | return 0; | |
123 | } | |
124 | ||
125 | ||
126 | int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr, | |
127 | unsigned int bufSize) | |
128 | { | |
129 | v4l2_std_id id = 0; | |
130 | v4l2_std_id cmsk = 0; | |
131 | v4l2_std_id t; | |
132 | int mMode = 0; | |
133 | unsigned int cnt; | |
134 | char ch; | |
135 | const struct std_name *sp; | |
136 | ||
137 | while (bufSize) { | |
138 | if (!mMode) { | |
139 | cnt = 0; | |
140 | while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++; | |
141 | if (cnt >= bufSize) return 0; // No more characters | |
142 | sp = find_std_name( | |
143 | std_groups, | |
144 | sizeof(std_groups)/sizeof(std_groups[0]), | |
145 | bufPtr,cnt); | |
146 | if (!sp) return 0; // Illegal color system name | |
147 | cnt++; | |
148 | bufPtr += cnt; | |
149 | bufSize -= cnt; | |
150 | mMode = !0; | |
151 | cmsk = sp->id; | |
152 | continue; | |
153 | } | |
154 | cnt = 0; | |
155 | while (cnt < bufSize) { | |
156 | ch = bufPtr[cnt]; | |
157 | if (ch == ';') { | |
158 | mMode = 0; | |
159 | break; | |
160 | } | |
161 | if (ch == '/') break; | |
162 | cnt++; | |
163 | } | |
164 | sp = find_std_name(std_items, | |
165 | sizeof(std_items)/sizeof(std_items[0]), | |
166 | bufPtr,cnt); | |
167 | if (!sp) return 0; // Illegal modulation system ID | |
168 | t = sp->id & cmsk; | |
169 | if (!t) return 0; // Specific color + modulation system illegal | |
170 | id |= t; | |
171 | if (cnt < bufSize) cnt++; | |
172 | bufPtr += cnt; | |
173 | bufSize -= cnt; | |
174 | } | |
175 | ||
176 | if (idPtr) *idPtr = id; | |
177 | return !0; | |
178 | } | |
179 | ||
180 | ||
181 | unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize, | |
182 | v4l2_std_id id) | |
183 | { | |
184 | unsigned int idx1,idx2; | |
185 | const struct std_name *ip,*gp; | |
186 | int gfl,cfl; | |
187 | unsigned int c1,c2; | |
188 | cfl = 0; | |
189 | c1 = 0; | |
190 | for (idx1 = 0; | |
191 | idx1 < sizeof(std_groups)/sizeof(std_groups[0]); | |
192 | idx1++) { | |
193 | gp = std_groups + idx1; | |
194 | gfl = 0; | |
195 | for (idx2 = 0; | |
196 | idx2 < sizeof(std_items)/sizeof(std_items[0]); | |
197 | idx2++) { | |
198 | ip = std_items + idx2; | |
199 | if (!(gp->id & ip->id & id)) continue; | |
200 | if (!gfl) { | |
201 | if (cfl) { | |
202 | c2 = scnprintf(bufPtr,bufSize,";"); | |
203 | c1 += c2; | |
204 | bufSize -= c2; | |
205 | bufPtr += c2; | |
206 | } | |
207 | cfl = !0; | |
208 | c2 = scnprintf(bufPtr,bufSize, | |
209 | "%s-",gp->name); | |
210 | gfl = !0; | |
211 | } else { | |
212 | c2 = scnprintf(bufPtr,bufSize,"/"); | |
213 | } | |
214 | c1 += c2; | |
215 | bufSize -= c2; | |
216 | bufPtr += c2; | |
217 | c2 = scnprintf(bufPtr,bufSize, | |
218 | ip->name); | |
219 | c1 += c2; | |
220 | bufSize -= c2; | |
221 | bufPtr += c2; | |
222 | } | |
223 | } | |
224 | return c1; | |
225 | } | |
226 | ||
227 | ||
228 | // Template data for possible enumerated video standards. Here we group | |
229 | // standards which share common frame rates and resolution. | |
230 | static struct v4l2_standard generic_standards[] = { | |
231 | { | |
232 | .id = (TSTD_B|TSTD_B1| | |
233 | TSTD_D|TSTD_D1| | |
234 | TSTD_G| | |
235 | TSTD_H| | |
236 | TSTD_I| | |
237 | TSTD_K|TSTD_K1| | |
238 | TSTD_L| | |
239 | V4L2_STD_SECAM_LC | | |
240 | TSTD_N|TSTD_Nc), | |
241 | .frameperiod = | |
242 | { | |
243 | .numerator = 1, | |
244 | .denominator= 25 | |
245 | }, | |
246 | .framelines = 625, | |
247 | .reserved = {0,0,0,0} | |
248 | }, { | |
249 | .id = (TSTD_M| | |
250 | V4L2_STD_NTSC_M_JP| | |
251 | V4L2_STD_NTSC_M_KR), | |
252 | .frameperiod = | |
253 | { | |
254 | .numerator = 1001, | |
255 | .denominator= 30000 | |
256 | }, | |
257 | .framelines = 525, | |
258 | .reserved = {0,0,0,0} | |
259 | }, { // This is a total wild guess | |
260 | .id = (TSTD_60), | |
261 | .frameperiod = | |
262 | { | |
263 | .numerator = 1001, | |
264 | .denominator= 30000 | |
265 | }, | |
266 | .framelines = 525, | |
267 | .reserved = {0,0,0,0} | |
268 | }, { // This is total wild guess | |
269 | .id = V4L2_STD_NTSC_443, | |
270 | .frameperiod = | |
271 | { | |
272 | .numerator = 1001, | |
273 | .denominator= 30000 | |
274 | }, | |
275 | .framelines = 525, | |
276 | .reserved = {0,0,0,0} | |
277 | } | |
278 | }; | |
279 | ||
280 | #define generic_standards_cnt (sizeof(generic_standards)/sizeof(generic_standards[0])) | |
281 | ||
282 | static struct v4l2_standard *match_std(v4l2_std_id id) | |
283 | { | |
284 | unsigned int idx; | |
285 | for (idx = 0; idx < generic_standards_cnt; idx++) { | |
286 | if (generic_standards[idx].id & id) { | |
287 | return generic_standards + idx; | |
288 | } | |
289 | } | |
290 | return 0; | |
291 | } | |
292 | ||
293 | static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id) | |
294 | { | |
295 | struct v4l2_standard *template; | |
296 | int idx; | |
297 | unsigned int bcnt; | |
298 | template = match_std(id); | |
299 | if (!template) return 0; | |
300 | idx = std->index; | |
301 | memcpy(std,template,sizeof(*template)); | |
302 | std->index = idx; | |
303 | std->id = id; | |
304 | bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id); | |
305 | std->name[bcnt] = 0; | |
306 | pvr2_trace(PVR2_TRACE_INIT,"Set up standard idx=%u name=%s", | |
307 | std->index,std->name); | |
308 | return !0; | |
309 | } | |
310 | ||
311 | /* These are special cases of combined standards that we should enumerate | |
312 | separately if the component pieces are present. */ | |
313 | static v4l2_std_id std_mixes[] = { | |
314 | V4L2_STD_PAL_B | V4L2_STD_PAL_G, | |
315 | V4L2_STD_PAL_D | V4L2_STD_PAL_K, | |
316 | V4L2_STD_SECAM_B | V4L2_STD_SECAM_G, | |
317 | V4L2_STD_SECAM_D | V4L2_STD_SECAM_K, | |
318 | }; | |
319 | ||
320 | struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr, | |
321 | v4l2_std_id id) | |
322 | { | |
323 | unsigned int std_cnt = 0; | |
324 | unsigned int idx,bcnt,idx2; | |
325 | v4l2_std_id idmsk,cmsk,fmsk; | |
326 | struct v4l2_standard *stddefs; | |
327 | ||
328 | if (pvrusb2_debug & PVR2_TRACE_INIT) { | |
329 | char buf[50]; | |
330 | bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id); | |
331 | pvr2_trace( | |
332 | PVR2_TRACE_INIT,"Mapping standards mask=0x%x (%.*s)", | |
333 | (int)id,bcnt,buf); | |
334 | } | |
335 | ||
336 | *countptr = 0; | |
337 | std_cnt = 0; | |
338 | fmsk = 0; | |
339 | for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) { | |
340 | if (!(idmsk & cmsk)) continue; | |
341 | cmsk &= ~idmsk; | |
342 | if (match_std(idmsk)) { | |
343 | std_cnt++; | |
344 | continue; | |
345 | } | |
346 | fmsk |= idmsk; | |
347 | } | |
348 | ||
349 | for (idx2 = 0; idx2 < sizeof(std_mixes)/sizeof(std_mixes[0]); idx2++) { | |
350 | if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++; | |
351 | } | |
352 | ||
353 | if (fmsk) { | |
354 | char buf[50]; | |
355 | bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk); | |
356 | pvr2_trace( | |
357 | PVR2_TRACE_ERROR_LEGS, | |
358 | "WARNING:" | |
359 | " Failed to classify the following standard(s): %.*s", | |
360 | bcnt,buf); | |
361 | } | |
362 | ||
363 | pvr2_trace(PVR2_TRACE_INIT,"Setting up %u unique standard(s)", | |
364 | std_cnt); | |
365 | if (!std_cnt) return 0; // paranoia | |
366 | ||
367 | stddefs = kmalloc(sizeof(struct v4l2_standard) * std_cnt, | |
368 | GFP_KERNEL); | |
369 | memset(stddefs,0,sizeof(struct v4l2_standard) * std_cnt); | |
370 | for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx; | |
371 | ||
372 | idx = 0; | |
373 | ||
374 | /* Enumerate potential special cases */ | |
375 | for (idx2 = 0; ((idx2 < sizeof(std_mixes)/sizeof(std_mixes[0])) && | |
376 | (idx < std_cnt)); idx2++) { | |
377 | if (!(id & std_mixes[idx2])) continue; | |
378 | if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++; | |
379 | } | |
380 | /* Now enumerate individual pieces */ | |
381 | for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) { | |
382 | if (!(idmsk & cmsk)) continue; | |
383 | cmsk &= ~idmsk; | |
384 | if (!pvr2_std_fill(stddefs+idx,idmsk)) continue; | |
385 | idx++; | |
386 | } | |
387 | ||
388 | *countptr = std_cnt; | |
389 | return stddefs; | |
390 | } | |
391 | ||
392 | v4l2_std_id pvr2_std_get_usable(void) | |
393 | { | |
394 | return CSTD_ALL; | |
395 | } | |
396 | ||
397 | ||
398 | /* | |
399 | Stuff for Emacs to see, in order to encourage consistent editing style: | |
400 | *** Local Variables: *** | |
401 | *** mode: c *** | |
402 | *** fill-column: 75 *** | |
403 | *** tab-width: 8 *** | |
404 | *** c-basic-offset: 8 *** | |
405 | *** End: *** | |
406 | */ |