blob: da309288daa4c603fd92b6dbd33e3eca5a6a6508 [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
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"
Mike Iselyb2bbaa92006-06-25 20:03:59 -030024#include <asm/string.h>
25#include <linux/slab.h>
Mike Iselyd8554972006-06-26 20:58:46 -030026
27struct std_name {
28 const char *name;
29 v4l2_std_id id;
30};
31
32
33#define CSTD_PAL \
34 (V4L2_STD_PAL_B| \
35 V4L2_STD_PAL_B1| \
36 V4L2_STD_PAL_G| \
37 V4L2_STD_PAL_H| \
38 V4L2_STD_PAL_I| \
39 V4L2_STD_PAL_D| \
40 V4L2_STD_PAL_D1| \
41 V4L2_STD_PAL_K| \
42 V4L2_STD_PAL_M| \
43 V4L2_STD_PAL_N| \
44 V4L2_STD_PAL_Nc| \
45 V4L2_STD_PAL_60)
46
47#define CSTD_NTSC \
48 (V4L2_STD_NTSC_M| \
49 V4L2_STD_NTSC_M_JP| \
50 V4L2_STD_NTSC_M_KR| \
51 V4L2_STD_NTSC_443)
52
Mike Isely9de982d2007-12-03 02:10:04 -030053#define CSTD_ATSC \
54 (V4L2_STD_ATSC_8_VSB| \
55 V4L2_STD_ATSC_16_VSB)
56
Mike Iselyd8554972006-06-26 20:58:46 -030057#define CSTD_SECAM \
58 (V4L2_STD_SECAM_B| \
59 V4L2_STD_SECAM_D| \
60 V4L2_STD_SECAM_G| \
61 V4L2_STD_SECAM_H| \
62 V4L2_STD_SECAM_K| \
63 V4L2_STD_SECAM_K1| \
64 V4L2_STD_SECAM_L| \
65 V4L2_STD_SECAM_LC)
66
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)
81
82#define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_SECAM)
83
84/* Mapping of standard bits to color system */
Tobias Klauserc5a69d52007-02-17 20:11:19 +010085static const struct std_name std_groups[] = {
Mike Iselyd8554972006-06-26 20:58:46 -030086 {"PAL",CSTD_PAL},
87 {"NTSC",CSTD_NTSC},
88 {"SECAM",CSTD_SECAM},
Mike Isely9de982d2007-12-03 02:10:04 -030089 {"ATSC",CSTD_ATSC},
Mike Iselyd8554972006-06-26 20:58:46 -030090};
91
92/* Mapping of standard bits to modulation system */
Tobias Klauserc5a69d52007-02-17 20:11:19 +010093static const struct std_name std_items[] = {
Mike Iselyd8554972006-06-26 20:58:46 -030094 {"B",TSTD_B},
95 {"B1",TSTD_B1},
96 {"D",TSTD_D},
97 {"D1",TSTD_D1},
98 {"G",TSTD_G},
99 {"H",TSTD_H},
100 {"I",TSTD_I},
101 {"K",TSTD_K},
102 {"K1",TSTD_K1},
103 {"L",TSTD_L},
104 {"LC",V4L2_STD_SECAM_LC},
105 {"M",TSTD_M},
106 {"Mj",V4L2_STD_NTSC_M_JP},
107 {"443",V4L2_STD_NTSC_443},
108 {"Mk",V4L2_STD_NTSC_M_KR},
109 {"N",TSTD_N},
110 {"Nc",TSTD_Nc},
111 {"60",TSTD_60},
Mike Isely9de982d2007-12-03 02:10:04 -0300112 {"8VSB",V4L2_STD_ATSC_8_VSB},
113 {"16VSB",V4L2_STD_ATSC_16_VSB},
Mike Iselyd8554972006-06-26 20:58:46 -0300114};
115
116
117// Search an array of std_name structures and return a pointer to the
118// element with the matching name.
119static const struct std_name *find_std_name(const struct std_name *arrPtr,
120 unsigned int arrSize,
121 const char *bufPtr,
122 unsigned int bufSize)
123{
124 unsigned int idx;
125 const struct std_name *p;
126 for (idx = 0; idx < arrSize; idx++) {
127 p = arrPtr + idx;
128 if (strlen(p->name) != bufSize) continue;
129 if (!memcmp(bufPtr,p->name,bufSize)) return p;
130 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300131 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300132}
133
134
135int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
136 unsigned int bufSize)
137{
138 v4l2_std_id id = 0;
139 v4l2_std_id cmsk = 0;
140 v4l2_std_id t;
141 int mMode = 0;
142 unsigned int cnt;
143 char ch;
144 const struct std_name *sp;
145
146 while (bufSize) {
147 if (!mMode) {
148 cnt = 0;
149 while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
150 if (cnt >= bufSize) return 0; // No more characters
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300151 sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
152 bufPtr,cnt);
Mike Iselyd8554972006-06-26 20:58:46 -0300153 if (!sp) return 0; // Illegal color system name
154 cnt++;
155 bufPtr += cnt;
156 bufSize -= cnt;
157 mMode = !0;
158 cmsk = sp->id;
159 continue;
160 }
161 cnt = 0;
162 while (cnt < bufSize) {
163 ch = bufPtr[cnt];
164 if (ch == ';') {
165 mMode = 0;
166 break;
167 }
168 if (ch == '/') break;
169 cnt++;
170 }
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300171 sp = find_std_name(std_items, ARRAY_SIZE(std_items),
Mike Iselyd8554972006-06-26 20:58:46 -0300172 bufPtr,cnt);
173 if (!sp) return 0; // Illegal modulation system ID
174 t = sp->id & cmsk;
175 if (!t) return 0; // Specific color + modulation system illegal
176 id |= t;
177 if (cnt < bufSize) cnt++;
178 bufPtr += cnt;
179 bufSize -= cnt;
180 }
181
182 if (idPtr) *idPtr = id;
183 return !0;
184}
185
186
187unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
188 v4l2_std_id id)
189{
190 unsigned int idx1,idx2;
191 const struct std_name *ip,*gp;
192 int gfl,cfl;
193 unsigned int c1,c2;
194 cfl = 0;
195 c1 = 0;
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300196 for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300197 gp = std_groups + idx1;
198 gfl = 0;
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300199 for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300200 ip = std_items + idx2;
201 if (!(gp->id & ip->id & id)) continue;
202 if (!gfl) {
203 if (cfl) {
204 c2 = scnprintf(bufPtr,bufSize,";");
205 c1 += c2;
206 bufSize -= c2;
207 bufPtr += c2;
208 }
209 cfl = !0;
210 c2 = scnprintf(bufPtr,bufSize,
211 "%s-",gp->name);
212 gfl = !0;
213 } else {
214 c2 = scnprintf(bufPtr,bufSize,"/");
215 }
216 c1 += c2;
217 bufSize -= c2;
218 bufPtr += c2;
219 c2 = scnprintf(bufPtr,bufSize,
220 ip->name);
221 c1 += c2;
222 bufSize -= c2;
223 bufPtr += c2;
224 }
225 }
226 return c1;
227}
228
229
230// Template data for possible enumerated video standards. Here we group
231// standards which share common frame rates and resolution.
232static struct v4l2_standard generic_standards[] = {
233 {
234 .id = (TSTD_B|TSTD_B1|
235 TSTD_D|TSTD_D1|
236 TSTD_G|
237 TSTD_H|
238 TSTD_I|
239 TSTD_K|TSTD_K1|
240 TSTD_L|
241 V4L2_STD_SECAM_LC |
242 TSTD_N|TSTD_Nc),
243 .frameperiod =
244 {
245 .numerator = 1,
246 .denominator= 25
247 },
248 .framelines = 625,
249 .reserved = {0,0,0,0}
250 }, {
251 .id = (TSTD_M|
252 V4L2_STD_NTSC_M_JP|
253 V4L2_STD_NTSC_M_KR),
254 .frameperiod =
255 {
256 .numerator = 1001,
257 .denominator= 30000
258 },
259 .framelines = 525,
260 .reserved = {0,0,0,0}
261 }, { // This is a total wild guess
262 .id = (TSTD_60),
263 .frameperiod =
264 {
265 .numerator = 1001,
266 .denominator= 30000
267 },
268 .framelines = 525,
269 .reserved = {0,0,0,0}
270 }, { // This is total wild guess
271 .id = V4L2_STD_NTSC_443,
272 .frameperiod =
273 {
274 .numerator = 1001,
275 .denominator= 30000
276 },
277 .framelines = 525,
278 .reserved = {0,0,0,0}
279 }
280};
281
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300282#define generic_standards_cnt ARRAY_SIZE(generic_standards)
Mike Iselyd8554972006-06-26 20:58:46 -0300283
284static struct v4l2_standard *match_std(v4l2_std_id id)
285{
286 unsigned int idx;
287 for (idx = 0; idx < generic_standards_cnt; idx++) {
288 if (generic_standards[idx].id & id) {
289 return generic_standards + idx;
290 }
291 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300292 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300293}
294
295static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
296{
297 struct v4l2_standard *template;
298 int idx;
299 unsigned int bcnt;
300 template = match_std(id);
301 if (!template) return 0;
302 idx = std->index;
303 memcpy(std,template,sizeof(*template));
304 std->index = idx;
305 std->id = id;
306 bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
307 std->name[bcnt] = 0;
Mike Isely56585382007-09-08 22:32:12 -0300308 pvr2_trace(PVR2_TRACE_STD,"Set up standard idx=%u name=%s",
Mike Iselyd8554972006-06-26 20:58:46 -0300309 std->index,std->name);
310 return !0;
311}
312
313/* These are special cases of combined standards that we should enumerate
314 separately if the component pieces are present. */
315static 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,
320};
321
322struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
323 v4l2_std_id id)
324{
325 unsigned int std_cnt = 0;
326 unsigned int idx,bcnt,idx2;
327 v4l2_std_id idmsk,cmsk,fmsk;
328 struct v4l2_standard *stddefs;
329
Mike Isely56585382007-09-08 22:32:12 -0300330 if (pvrusb2_debug & PVR2_TRACE_STD) {
Mike Iselyd8554972006-06-26 20:58:46 -0300331 char buf[50];
332 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
333 pvr2_trace(
Mike Isely56585382007-09-08 22:32:12 -0300334 PVR2_TRACE_STD,"Mapping standards mask=0x%x (%.*s)",
Mike Iselyd8554972006-06-26 20:58:46 -0300335 (int)id,bcnt,buf);
336 }
337
338 *countptr = 0;
339 std_cnt = 0;
340 fmsk = 0;
341 for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
342 if (!(idmsk & cmsk)) continue;
343 cmsk &= ~idmsk;
344 if (match_std(idmsk)) {
345 std_cnt++;
346 continue;
347 }
348 fmsk |= idmsk;
349 }
350
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300351 for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300352 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
353 }
354
355 if (fmsk) {
356 char buf[50];
357 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
358 pvr2_trace(
359 PVR2_TRACE_ERROR_LEGS,
360 "WARNING:"
361 " Failed to classify the following standard(s): %.*s",
362 bcnt,buf);
363 }
364
Mike Isely56585382007-09-08 22:32:12 -0300365 pvr2_trace(PVR2_TRACE_STD,"Setting up %u unique standard(s)",
Mike Iselyd8554972006-06-26 20:58:46 -0300366 std_cnt);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300367 if (!std_cnt) return NULL; // paranoia
Mike Iselyd8554972006-06-26 20:58:46 -0300368
Mike Iselyca545f72007-01-20 00:37:11 -0300369 stddefs = kzalloc(sizeof(struct v4l2_standard) * std_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -0300370 GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300371 for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
372
373 idx = 0;
374
375 /* Enumerate potential special cases */
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300376 for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
377 idx2++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300378 if (!(id & std_mixes[idx2])) continue;
379 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
380 }
381 /* Now enumerate individual pieces */
382 for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
383 if (!(idmsk & cmsk)) continue;
384 cmsk &= ~idmsk;
385 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
386 idx++;
387 }
388
389 *countptr = std_cnt;
390 return stddefs;
391}
392
393v4l2_std_id pvr2_std_get_usable(void)
394{
395 return CSTD_ALL;
396}
397
398
399/*
400 Stuff for Emacs to see, in order to encourage consistent editing style:
401 *** Local Variables: ***
402 *** mode: c ***
403 *** fill-column: 75 ***
404 *** tab-width: 8 ***
405 *** c-basic-offset: 8 ***
406 *** End: ***
407 */