V4L/DVB (7688): pvrusb2: Clean up dvb streaming start/stop
[linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-std.c
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 #include <asm/string.h>
25 #include <linux/slab.h>
26
27 struct 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
53 #define CSTD_ATSC \
54         (V4L2_STD_ATSC_8_VSB| \
55          V4L2_STD_ATSC_16_VSB)
56
57 #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_ATSC|CSTD_SECAM)
83
84 /* Mapping of standard bits to color system */
85 static const struct std_name std_groups[] = {
86         {"PAL",CSTD_PAL},
87         {"NTSC",CSTD_NTSC},
88         {"SECAM",CSTD_SECAM},
89         {"ATSC",CSTD_ATSC},
90 };
91
92 /* Mapping of standard bits to modulation system */
93 static const struct std_name std_items[] = {
94         {"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},
112         {"8VSB",V4L2_STD_ATSC_8_VSB},
113         {"16VSB",V4L2_STD_ATSC_16_VSB},
114 };
115
116
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,
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         }
131         return NULL;
132 }
133
134
135 int 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
151                         sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
152                                            bufPtr,cnt);
153                         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                 }
171                 sp = find_std_name(std_items, ARRAY_SIZE(std_items),
172                                    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
187 unsigned 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;
196         for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
197                 gp = std_groups + idx1;
198                 gfl = 0;
199                 for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
200                         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.
232 static 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
282 #define generic_standards_cnt ARRAY_SIZE(generic_standards)
283
284 static 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         }
292         return NULL;
293 }
294
295 static 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;
308         pvr2_trace(PVR2_TRACE_STD,"Set up standard idx=%u name=%s",
309                    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. */
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,
320 };
321
322 struct 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
330         if (pvrusb2_debug & PVR2_TRACE_STD) {
331                 char buf[100];
332                 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
333                 pvr2_trace(
334                         PVR2_TRACE_STD,"Mapping standards mask=0x%x (%.*s)",
335                         (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
351         for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
352                 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
353         }
354
355         /* Don't complain about ATSC standard values */
356         fmsk &= ~CSTD_ATSC;
357
358         if (fmsk) {
359                 char buf[100];
360                 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
361                 pvr2_trace(
362                         PVR2_TRACE_ERROR_LEGS,
363                         "WARNING:"
364                         " Failed to classify the following standard(s): %.*s",
365                         bcnt,buf);
366         }
367
368         pvr2_trace(PVR2_TRACE_STD,"Setting up %u unique standard(s)",
369                    std_cnt);
370         if (!std_cnt) return NULL; // paranoia
371
372         stddefs = kzalloc(sizeof(struct v4l2_standard) * std_cnt,
373                           GFP_KERNEL);
374         for (idx = 0; idx < std_cnt; idx++) stddefs[idx].index = idx;
375
376         idx = 0;
377
378         /* Enumerate potential special cases */
379         for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
380              idx2++) {
381                 if (!(id & std_mixes[idx2])) continue;
382                 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
383         }
384         /* Now enumerate individual pieces */
385         for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
386                 if (!(idmsk & cmsk)) continue;
387                 cmsk &= ~idmsk;
388                 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
389                 idx++;
390         }
391
392         *countptr = std_cnt;
393         return stddefs;
394 }
395
396 v4l2_std_id pvr2_std_get_usable(void)
397 {
398         return CSTD_ALL;
399 }
400
401
402 /*
403   Stuff for Emacs to see, in order to encourage consistent editing style:
404   *** Local Variables: ***
405   *** mode: c ***
406   *** fill-column: 75 ***
407   *** tab-width: 8 ***
408   *** c-basic-offset: 8 ***
409   *** End: ***
410   */