V4L/DVB (4202): allow selecting CX2341x port mode
[linux-2.6] / drivers / media / video / cx2341x.c
1 /*
2  * cx2341x - generic code for cx23415/6 based devices
3  *
4  * Copyright (C) 2006 Hans Verkuil <hverkuil@xs4all.nl>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/videodev2.h>
29 #include <linux/i2c.h>
30
31 #include <media/tuner.h>
32 #include <media/cx2341x.h>
33 #include <media/v4l2-common.h>
34
35 MODULE_DESCRIPTION("cx23415/6 driver");
36 MODULE_AUTHOR("Hans Verkuil");
37 MODULE_LICENSE("GPL");
38
39 static int debug = 0;
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "Debug level (0-1)");
42
43 const u32 cx2341x_mpeg_ctrls[] = {
44         V4L2_CID_MPEG_CLASS,
45         V4L2_CID_MPEG_STREAM_TYPE,
46         V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
47         V4L2_CID_MPEG_AUDIO_ENCODING,
48         V4L2_CID_MPEG_AUDIO_L2_BITRATE,
49         V4L2_CID_MPEG_AUDIO_MODE,
50         V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
51         V4L2_CID_MPEG_AUDIO_EMPHASIS,
52         V4L2_CID_MPEG_AUDIO_CRC,
53         V4L2_CID_MPEG_VIDEO_ENCODING,
54         V4L2_CID_MPEG_VIDEO_ASPECT,
55         V4L2_CID_MPEG_VIDEO_B_FRAMES,
56         V4L2_CID_MPEG_VIDEO_GOP_SIZE,
57         V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
58         V4L2_CID_MPEG_VIDEO_PULLDOWN,
59         V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
60         V4L2_CID_MPEG_VIDEO_BITRATE,
61         V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
62         V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
63         V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
64         V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
65         V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
66         V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
67         V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
68         V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
69         V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
70         V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
71         V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
72         V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
73         V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
74         0
75 };
76
77
78 /* Map the control ID to the correct field in the cx2341x_mpeg_params
79    struct. Return -EINVAL if the ID is unknown, else return 0. */
80 static int cx2341x_get_ctrl(struct cx2341x_mpeg_params *params,
81                 struct v4l2_ext_control *ctrl)
82 {
83         switch (ctrl->id) {
84         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
85                 ctrl->value = params->audio_sampling_freq;
86                 break;
87         case V4L2_CID_MPEG_AUDIO_ENCODING:
88                 ctrl->value = params->audio_encoding;
89                 break;
90         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
91                 ctrl->value = params->audio_l2_bitrate;
92                 break;
93         case V4L2_CID_MPEG_AUDIO_MODE:
94                 ctrl->value = params->audio_mode;
95                 break;
96         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
97                 ctrl->value = params->audio_mode_extension;
98                 break;
99         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
100                 ctrl->value = params->audio_emphasis;
101                 break;
102         case V4L2_CID_MPEG_AUDIO_CRC:
103                 ctrl->value = params->audio_crc;
104                 break;
105         case V4L2_CID_MPEG_VIDEO_ENCODING:
106                 ctrl->value = params->video_encoding;
107                 break;
108         case V4L2_CID_MPEG_VIDEO_ASPECT:
109                 ctrl->value = params->video_aspect;
110                 break;
111         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
112                 ctrl->value = params->video_b_frames;
113                 break;
114         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
115                 ctrl->value = params->video_gop_size;
116                 break;
117         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
118                 ctrl->value = params->video_gop_closure;
119                 break;
120         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
121                 ctrl->value = params->video_pulldown;
122                 break;
123         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
124                 ctrl->value = params->video_bitrate_mode;
125                 break;
126         case V4L2_CID_MPEG_VIDEO_BITRATE:
127                 ctrl->value = params->video_bitrate;
128                 break;
129         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
130                 ctrl->value = params->video_bitrate_peak;
131                 break;
132         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
133                 ctrl->value = params->video_temporal_decimation;
134                 break;
135         case V4L2_CID_MPEG_STREAM_TYPE:
136                 ctrl->value = params->stream_type;
137                 break;
138         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
139                 ctrl->value = params->video_spatial_filter_mode;
140                 break;
141         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
142                 ctrl->value = params->video_spatial_filter;
143                 break;
144         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
145                 ctrl->value = params->video_luma_spatial_filter_type;
146                 break;
147         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
148                 ctrl->value = params->video_chroma_spatial_filter_type;
149                 break;
150         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
151                 ctrl->value = params->video_temporal_filter_mode;
152                 break;
153         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
154                 ctrl->value = params->video_temporal_filter;
155                 break;
156         case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
157                 ctrl->value = params->video_median_filter_type;
158                 break;
159         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
160                 ctrl->value = params->video_luma_median_filter_top;
161                 break;
162         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
163                 ctrl->value = params->video_luma_median_filter_bottom;
164                 break;
165         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
166                 ctrl->value = params->video_chroma_median_filter_top;
167                 break;
168         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
169                 ctrl->value = params->video_chroma_median_filter_bottom;
170                 break;
171         default:
172                 return -EINVAL;
173         }
174         return 0;
175 }
176
177 /* Map the control ID to the correct field in the cx2341x_mpeg_params
178    struct. Return -EINVAL if the ID is unknown, else return 0. */
179 static int cx2341x_set_ctrl(struct cx2341x_mpeg_params *params,
180                 struct v4l2_ext_control *ctrl)
181 {
182         switch (ctrl->id) {
183         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
184                 params->audio_sampling_freq = ctrl->value;
185                 break;
186         case V4L2_CID_MPEG_AUDIO_ENCODING:
187                 params->audio_encoding = ctrl->value;
188                 break;
189         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
190                 params->audio_l2_bitrate = ctrl->value;
191                 break;
192         case V4L2_CID_MPEG_AUDIO_MODE:
193                 params->audio_mode = ctrl->value;
194                 break;
195         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
196                 params->audio_mode_extension = ctrl->value;
197                 break;
198         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
199                 params->audio_emphasis = ctrl->value;
200                 break;
201         case V4L2_CID_MPEG_AUDIO_CRC:
202                 params->audio_crc = ctrl->value;
203                 break;
204         case V4L2_CID_MPEG_VIDEO_ASPECT:
205                 params->video_aspect = ctrl->value;
206                 break;
207         case V4L2_CID_MPEG_VIDEO_B_FRAMES: {
208                 int b = ctrl->value + 1;
209                 int gop = params->video_gop_size;
210                 params->video_b_frames = ctrl->value;
211                 params->video_gop_size = b * ((gop + b - 1) / b);
212                 /* Max GOP size = 34 */
213                 while (params->video_gop_size > 34)
214                         params->video_gop_size -= b;
215                 break;
216         }
217         case V4L2_CID_MPEG_VIDEO_GOP_SIZE: {
218                 int b = params->video_b_frames + 1;
219                 int gop = ctrl->value;
220                 params->video_gop_size = b * ((gop + b - 1) / b);
221                 /* Max GOP size = 34 */
222                 while (params->video_gop_size > 34)
223                         params->video_gop_size -= b;
224                 ctrl->value = params->video_gop_size;
225                 break;
226         }
227         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
228                 params->video_gop_closure = ctrl->value;
229                 break;
230         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
231                 params->video_pulldown = ctrl->value;
232                 break;
233         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
234                 /* MPEG-1 only allows CBR */
235                 if (params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1 &&
236                     ctrl->value != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
237                         return -EINVAL;
238                 params->video_bitrate_mode = ctrl->value;
239                 break;
240         case V4L2_CID_MPEG_VIDEO_BITRATE:
241                 params->video_bitrate = ctrl->value;
242                 break;
243         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
244                 params->video_bitrate_peak = ctrl->value;
245                 break;
246         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
247                 params->video_temporal_decimation = ctrl->value;
248                 break;
249         case V4L2_CID_MPEG_STREAM_TYPE:
250                 params->stream_type = ctrl->value;
251                 params->video_encoding =
252                         (params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_SS ||
253                          params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_VCD) ?
254                         V4L2_MPEG_VIDEO_ENCODING_MPEG_1 : V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
255                 if (params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1) {
256                         /* MPEG-1 implies CBR */
257                         params->video_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
258                 }
259                 break;
260         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
261                 params->video_spatial_filter_mode = ctrl->value;
262                 break;
263         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
264                 params->video_spatial_filter = ctrl->value;
265                 break;
266         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
267                 params->video_luma_spatial_filter_type = ctrl->value;
268                 break;
269         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
270                 params->video_chroma_spatial_filter_type = ctrl->value;
271                 break;
272         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
273                 params->video_temporal_filter_mode = ctrl->value;
274                 break;
275         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
276                 params->video_temporal_filter = ctrl->value;
277                 break;
278         case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
279                 params->video_median_filter_type = ctrl->value;
280                 break;
281         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
282                 params->video_luma_median_filter_top = ctrl->value;
283                 break;
284         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
285                 params->video_luma_median_filter_bottom = ctrl->value;
286                 break;
287         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
288                 params->video_chroma_median_filter_top = ctrl->value;
289                 break;
290         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
291                 params->video_chroma_median_filter_bottom = ctrl->value;
292                 break;
293         default:
294                 return -EINVAL;
295         }
296         return 0;
297 }
298
299 static int cx2341x_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
300 {
301         const char *name;
302
303         qctrl->flags = 0;
304         switch (qctrl->id) {
305         /* MPEG controls */
306         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
307                 name = "Spatial Filter Mode";
308                 break;
309         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
310                 name = "Spatial Filter";
311                 break;
312         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
313                 name = "Spatial Luma Filter Type";
314                 break;
315         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
316                 name = "Spatial Chroma Filter Type";
317                 break;
318         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
319                 name = "Temporal Filter Mode";
320                 break;
321         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
322                 name = "Temporal Filter";
323                 break;
324         case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
325                 name = "Median Filter Type";
326                 break;
327         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
328                 name = "Median Luma Filter Maximum";
329                 break;
330         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
331                 name = "Median Luma Filter Minimum";
332                 break;
333         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
334                 name = "Median Chroma Filter Maximum";
335                 break;
336         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
337                 name = "Median Chroma Filter Minimum";
338                 break;
339
340         default:
341                 return v4l2_ctrl_query_fill(qctrl, min, max, step, def);
342         }
343         switch (qctrl->id) {
344         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
345         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
346         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
347         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
348         case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
349                 qctrl->type = V4L2_CTRL_TYPE_MENU;
350                 min = 0;
351                 step = 1;
352                 break;
353         default:
354                 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
355                 break;
356         }
357         switch (qctrl->id) {
358         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
359         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
360         case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
361                 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
362                 break;
363         }
364         qctrl->minimum = min;
365         qctrl->maximum = max;
366         qctrl->step = step;
367         qctrl->default_value = def;
368         qctrl->reserved[0] = qctrl->reserved[1] = 0;
369         snprintf(qctrl->name, sizeof(qctrl->name), name);
370         return 0;
371 }
372
373 int cx2341x_ctrl_query(struct cx2341x_mpeg_params *params, struct v4l2_queryctrl *qctrl)
374 {
375         int err;
376
377         switch (qctrl->id) {
378         case V4L2_CID_MPEG_AUDIO_ENCODING:
379                 return v4l2_ctrl_query_fill(qctrl,
380                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
381                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_2, 1,
382                                 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
383
384         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
385                 return v4l2_ctrl_query_fill(qctrl,
386                                 V4L2_MPEG_AUDIO_L2_BITRATE_192K,
387                                 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
388                                 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
389
390         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
391         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
392                 return -EINVAL;
393
394         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
395                 err = v4l2_ctrl_query_fill_std(qctrl);
396                 if (err == 0 && params->audio_mode != V4L2_MPEG_AUDIO_MODE_JOINT_STEREO)
397                         qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
398                 return err;
399
400         case V4L2_CID_MPEG_VIDEO_ENCODING:
401                 /* this setting is read-only for the cx2341x since the
402                    V4L2_CID_MPEG_STREAM_TYPE really determines the
403                    MPEG-1/2 setting */
404                 err = v4l2_ctrl_query_fill_std(qctrl);
405                 if (err == 0)
406                         qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
407                 return err;
408
409         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
410                 err = v4l2_ctrl_query_fill_std(qctrl);
411                 if (err == 0 && params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
412                         qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
413                 return err;
414
415         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
416                 err = v4l2_ctrl_query_fill_std(qctrl);
417                 if (err == 0 && params->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
418                         qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
419                 return err;
420
421         /* CX23415/6 specific */
422         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
423                 return cx2341x_ctrl_query_fill(qctrl,
424                                 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL,
425                                 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO, 1,
426                                 V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL);
427
428         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
429                 cx2341x_ctrl_query_fill(qctrl, 0, 15, 1, 0);
430                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
431                 if (params->video_spatial_filter_mode == V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
432                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
433                 return 0;
434
435         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
436                 cx2341x_ctrl_query_fill(qctrl,
437                                 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF,
438                                 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_2D_SYM_NON_SEPARABLE, 1,
439                                 V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF);
440                 if (params->video_spatial_filter_mode == V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
441                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
442                 return 0;
443
444         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
445                 cx2341x_ctrl_query_fill(qctrl,
446                                 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF,
447                                 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR, 1,
448                                 V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF);
449                 if (params->video_spatial_filter_mode == V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
450                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
451                 return 0;
452
453         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
454                 return cx2341x_ctrl_query_fill(qctrl,
455                                 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL,
456                                 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO, 1,
457                                 V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL);
458
459         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
460                 cx2341x_ctrl_query_fill(qctrl, 0, 31, 1, 0);
461                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
462                 if (params->video_temporal_filter_mode == V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO)
463                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
464                 return 0;
465
466         case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
467                 return cx2341x_ctrl_query_fill(qctrl,
468                                 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF,
469                                 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_DIAG, 1,
470                                 V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF);
471
472         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
473                 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 255);
474                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
475                 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
476                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
477                 return 0;
478
479         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
480                 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 0);
481                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
482                 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
483                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
484                 return 0;
485
486         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
487                 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 255);
488                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
489                 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
490                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
491                 return 0;
492
493         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
494                 cx2341x_ctrl_query_fill(qctrl, 0, 255, 1, 0);
495                 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
496                 if (params->video_median_filter_type == V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
497                        qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
498                 return 0;
499
500         default:
501                 return v4l2_ctrl_query_fill_std(qctrl);
502
503         }
504 }
505
506 const char **cx2341x_ctrl_get_menu(u32 id)
507 {
508         static const char *mpeg_stream_type[] = {
509                 "MPEG-2 Program Stream",
510                 "",
511                 "MPEG-1 System Stream",
512                 "MPEG-2 DVD-compatible Stream",
513                 "MPEG-1 VCD-compatible Stream",
514                 "MPEG-2 SVCD-compatible Stream",
515                 NULL
516         };
517
518         static const char *cx2341x_video_spatial_filter_mode_menu[] = {
519                 "Manual",
520                 "Auto",
521                 NULL
522         };
523
524         static const char *cx2341x_video_luma_spatial_filter_type_menu[] = {
525                 "Off",
526                 "1D Horizontal",
527                 "1D Vertical",
528                 "2D H/V Separable",
529                 "2D Symmetric non-separable",
530                 NULL
531         };
532
533         static const char *cx2341x_video_chroma_spatial_filter_type_menu[] = {
534                 "Off",
535                 "1D Horizontal",
536                 NULL
537         };
538
539         static const char *cx2341x_video_temporal_filter_mode_menu[] = {
540                 "Manual",
541                 "Auto",
542                 NULL
543         };
544
545         static const char *cx2341x_video_median_filter_type_menu[] = {
546                 "Off",
547                 "Horizontal",
548                 "Vertical",
549                 "Horizontal/Vertical",
550                 "Diagonal",
551                 NULL
552         };
553
554         switch (id) {
555         case V4L2_CID_MPEG_STREAM_TYPE:
556                 return mpeg_stream_type;
557         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
558         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
559                 return NULL;
560         case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
561                 return cx2341x_video_spatial_filter_mode_menu;
562         case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
563                 return cx2341x_video_luma_spatial_filter_type_menu;
564         case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
565                 return cx2341x_video_chroma_spatial_filter_type_menu;
566         case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
567                 return cx2341x_video_temporal_filter_mode_menu;
568         case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
569                 return cx2341x_video_median_filter_type_menu;
570         default:
571                 return v4l2_ctrl_get_menu(id);
572         }
573 }
574
575 static void cx2341x_calc_audio_properties(struct cx2341x_mpeg_params *params)
576 {
577         params->audio_properties = (params->audio_sampling_freq << 0) |
578                 ((3 - params->audio_encoding) << 2) |
579                 ((1 + params->audio_l2_bitrate) << 4) |
580                 (params->audio_mode << 8) |
581                 (params->audio_mode_extension << 10) |
582                 (((params->audio_emphasis == V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17) ?
583                   3 :
584                   params->audio_emphasis) << 12) |
585                 (params->audio_crc << 14);
586 }
587
588 int cx2341x_ext_ctrls(struct cx2341x_mpeg_params *params,
589                   struct v4l2_ext_controls *ctrls, int cmd)
590 {
591         int err = 0;
592         int i;
593
594         if (cmd == VIDIOC_G_EXT_CTRLS) {
595                 for (i = 0; i < ctrls->count; i++) {
596                         struct v4l2_ext_control *ctrl = ctrls->controls + i;
597
598                         err = cx2341x_get_ctrl(params, ctrl);
599                         if (err) {
600                                 ctrls->error_idx = i;
601                                 break;
602                         }
603                 }
604                 return err;
605         }
606         for (i = 0; i < ctrls->count; i++) {
607                 struct v4l2_ext_control *ctrl = ctrls->controls + i;
608                 struct v4l2_queryctrl qctrl;
609                 const char **menu_items = NULL;
610
611                 qctrl.id = ctrl->id;
612                 err = cx2341x_ctrl_query(params, &qctrl);
613                 if (err)
614                         break;
615                 if (qctrl.type == V4L2_CTRL_TYPE_MENU)
616                         menu_items = cx2341x_ctrl_get_menu(qctrl.id);
617                 err = v4l2_ctrl_check(ctrl, &qctrl, menu_items);
618                 if (err)
619                         break;
620                 err = cx2341x_set_ctrl(params, ctrl);
621                 if (err)
622                         break;
623         }
624         if (err == 0 && params->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
625                         params->video_bitrate_peak < params->video_bitrate) {
626                 err = -ERANGE;
627                 ctrls->error_idx = ctrls->count;
628         }
629         if (err) {
630                 ctrls->error_idx = i;
631         }
632         else {
633                 cx2341x_calc_audio_properties(params);
634         }
635         return err;
636 }
637
638 void cx2341x_fill_defaults(struct cx2341x_mpeg_params *p)
639 {
640         static struct cx2341x_mpeg_params default_params = {
641         /* misc */
642         .port = CX2341X_PORT_MEMORY,
643         .width = 720,
644         .height = 480,
645         .is_50hz = 0,
646
647         /* stream */
648         .stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
649
650         /* audio */
651         .audio_sampling_freq = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
652         .audio_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
653         .audio_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_224K,
654         .audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO,
655         .audio_mode_extension = V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
656         .audio_emphasis = V4L2_MPEG_AUDIO_EMPHASIS_NONE,
657         .audio_crc = V4L2_MPEG_AUDIO_CRC_NONE,
658
659         /* video */
660         .video_encoding = V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
661         .video_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
662         .video_b_frames = 2,
663         .video_gop_size = 12,
664         .video_gop_closure = 1,
665         .video_pulldown = 0,
666         .video_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
667         .video_bitrate = 6000000,
668         .video_bitrate_peak = 8000000,
669         .video_temporal_decimation = 0,
670
671         /* encoding filters */
672         .video_spatial_filter_mode = V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL,
673         .video_spatial_filter = 0,
674         .video_luma_spatial_filter_type = V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_1D_HOR,
675         .video_chroma_spatial_filter_type = V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR,
676         .video_temporal_filter_mode = V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL,
677         .video_temporal_filter = 0,
678         .video_median_filter_type = V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF,
679         .video_luma_median_filter_top = 255,
680         .video_luma_median_filter_bottom = 0,
681         .video_chroma_median_filter_top = 255,
682         .video_chroma_median_filter_bottom = 0,
683         };
684
685         *p = default_params;
686         cx2341x_calc_audio_properties(p);
687 }
688
689 static int cx2341x_api(void *priv, cx2341x_mbox_func func, int cmd, int args, ...)
690 {
691         u32 data[CX2341X_MBOX_MAX_DATA];
692         va_list vargs;
693         int i;
694
695         va_start(vargs, args);
696
697         for (i = 0; i < args; i++) {
698                 data[i] = va_arg(vargs, int);
699         }
700         va_end(vargs);
701         return func(priv, cmd, args, 0, data);
702 }
703
704 int cx2341x_update(void *priv, cx2341x_mbox_func func,
705                 const struct cx2341x_mpeg_params *old, const struct cx2341x_mpeg_params *new)
706 {
707         static int mpeg_stream_type[] = {
708                 0,      /* MPEG-2 PS */
709                 1,      /* MPEG-2 TS */
710                 2,      /* MPEG-1 SS */
711                 14,     /* DVD */
712                 11,     /* VCD */
713                 12,     /* SVCD */
714         };
715
716         int err = 0;
717
718         cx2341x_api(priv, func, CX2341X_ENC_SET_OUTPUT_PORT, 2, new->port, 0);
719
720         if (old == NULL || old->is_50hz != new->is_50hz) {
721                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_RATE, 1, new->is_50hz);
722                 if (err) return err;
723         }
724
725         if (old == NULL || old->width != new->width || old->height != new->height ||
726                         old->video_encoding != new->video_encoding) {
727                 u16 w = new->width;
728                 u16 h = new->height;
729
730                 if (new->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1) {
731                         w /= 2;
732                         h /= 2;
733                 }
734                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_SIZE, 2, h, w);
735                 if (err) return err;
736         }
737
738         if (old == NULL || old->stream_type != new->stream_type) {
739                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_STREAM_TYPE, 1, mpeg_stream_type[new->stream_type]);
740                 if (err) return err;
741         }
742         if (old == NULL || old->video_aspect != new->video_aspect) {
743                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_ASPECT_RATIO, 1, 1 + new->video_aspect);
744                 if (err) return err;
745         }
746         if (old == NULL || old->video_b_frames != new->video_b_frames ||
747                 old->video_gop_size != new->video_gop_size) {
748                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_PROPERTIES, 2,
749                                 new->video_gop_size, new->video_b_frames + 1);
750                 if (err) return err;
751         }
752         if (old == NULL || old->video_gop_closure != new->video_gop_closure) {
753                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_CLOSURE, 1, new->video_gop_closure);
754                 if (err) return err;
755         }
756         if (old == NULL || old->video_pulldown != new->video_pulldown) {
757                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_3_2_PULLDOWN, 1, new->video_pulldown);
758                 if (err) return err;
759         }
760         if (old == NULL || old->audio_properties != new->audio_properties) {
761                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new->audio_properties);
762                 if (err) return err;
763         }
764         if (old == NULL || old->video_bitrate_mode != new->video_bitrate_mode ||
765                 old->video_bitrate != new->video_bitrate ||
766                 old->video_bitrate_peak != new->video_bitrate_peak) {
767                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_BIT_RATE, 5,
768                                 new->video_bitrate_mode, new->video_bitrate,
769                                 new->video_bitrate_peak / 400, 0, 0);
770                 if (err) return err;
771         }
772         if (old == NULL || old->video_spatial_filter_mode != new->video_spatial_filter_mode ||
773                 old->video_temporal_filter_mode != new->video_temporal_filter_mode ||
774                 old->video_median_filter_type != new->video_median_filter_type) {
775                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_MODE, 2,
776                                 new->video_spatial_filter_mode | (new->video_temporal_filter_mode << 1),
777                                 new->video_median_filter_type);
778                 if (err) return err;
779         }
780         if (old == NULL ||
781                 old->video_luma_median_filter_bottom != new->video_luma_median_filter_bottom ||
782                 old->video_luma_median_filter_top != new->video_luma_median_filter_top ||
783                 old->video_chroma_median_filter_bottom != new->video_chroma_median_filter_bottom ||
784                 old->video_chroma_median_filter_top != new->video_chroma_median_filter_top) {
785                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_CORING_LEVELS, 4,
786                                 new->video_luma_median_filter_bottom,
787                                 new->video_luma_median_filter_top,
788                                 new->video_chroma_median_filter_bottom,
789                                 new->video_chroma_median_filter_top);
790                 if (err) return err;
791         }
792         if (old == NULL ||
793                 old->video_luma_spatial_filter_type != new->video_luma_spatial_filter_type ||
794                 old->video_chroma_spatial_filter_type != new->video_chroma_spatial_filter_type) {
795                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_SPATIAL_FILTER_TYPE, 2,
796                         new->video_luma_spatial_filter_type, new->video_chroma_spatial_filter_type);
797                 if (err) return err;
798         }
799         if (old == NULL ||
800                 old->video_spatial_filter != new->video_spatial_filter ||
801                 old->video_temporal_filter != new->video_temporal_filter) {
802                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_PROPS, 2,
803                         new->video_spatial_filter, new->video_temporal_filter);
804                 if (err) return err;
805         }
806         if (old == NULL || old->video_temporal_decimation != new->video_temporal_decimation) {
807                 err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_DROP_RATE, 1,
808                         new->video_temporal_decimation);
809                 if (err) return err;
810         }
811         return 0;
812 }
813
814 static const char *cx2341x_menu_item(struct cx2341x_mpeg_params *p, u32 id)
815 {
816         const char **menu = cx2341x_ctrl_get_menu(id);
817         struct v4l2_ext_control ctrl;
818
819         if (menu == NULL)
820                 goto invalid;
821         ctrl.id = id;
822         if (cx2341x_get_ctrl(p, &ctrl))
823                 goto invalid;
824         while (ctrl.value-- && *menu) menu++;
825         if (*menu == NULL)
826                 goto invalid;
827         return *menu;
828
829 invalid:
830         return "<invalid>";
831 }
832
833 void cx2341x_log_status(struct cx2341x_mpeg_params *p, int card_id)
834 {
835         int is_mpeg1 = p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
836
837         /* Stream */
838         printk(KERN_INFO "cx2341x-%d: Stream: %s\n",
839                 card_id,
840                 cx2341x_menu_item(p, V4L2_CID_MPEG_STREAM_TYPE));
841
842         /* Video */
843         printk(KERN_INFO "cx2341x-%d: Video:  %dx%d, %d fps\n",
844                 card_id,
845                 p->width / (is_mpeg1 ? 2 : 1), p->height / (is_mpeg1 ? 2 : 1),
846                 p->is_50hz ? 25 : 30);
847         printk(KERN_INFO "cx2341x-%d: Video:  %s, %s, %s, %d",
848                 card_id,
849                 cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_ENCODING),
850                 cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_ASPECT),
851                 cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_BITRATE_MODE),
852                 p->video_bitrate);
853         if (p->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
854                 printk(", Peak %d", p->video_bitrate_peak);
855         }
856         printk("\n");
857         printk(KERN_INFO "cx2341x-%d: Video:  GOP Size %d, %d B-Frames, %sGOP Closure, %s3:2 Pulldown\n",
858                 card_id,
859                 p->video_gop_size, p->video_b_frames,
860                 p->video_gop_closure ? "" : "No ",
861                 p->video_pulldown ? "" : "No ");
862         if (p->video_temporal_decimation) {
863                 printk(KERN_INFO "cx2341x-%d: Video: Temporal Decimation %d\n",
864                         card_id, p->video_temporal_decimation);
865         }
866
867         /* Audio */
868         printk(KERN_INFO "cx2341x-%d: Audio:  %s, %s, %s, %s",
869                 card_id,
870                 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ),
871                 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_ENCODING),
872                 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_L2_BITRATE),
873                 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_MODE));
874         if (p->audio_mode == V4L2_MPEG_AUDIO_MODE_JOINT_STEREO) {
875                 printk(", %s",
876                         cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_MODE_EXTENSION));
877         }
878         printk(", %s, %s\n",
879                 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_EMPHASIS),
880                 cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_CRC));
881
882         /* Encoding filters */
883         printk(KERN_INFO "cx2341x-%d: Spatial Filter:  %s, Luma %s, Chroma %s, %d\n",
884                 card_id,
885                 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE),
886                 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE),
887                 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE),
888                 p->video_spatial_filter);
889         printk(KERN_INFO "cx2341x-%d: Temporal Filter: %s, %d\n",
890                 card_id,
891                 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE),
892                 p->video_temporal_filter);
893         printk(KERN_INFO "cx2341x-%d: Median Filter:   %s, Luma [%d, %d], Chroma [%d, %d]\n",
894                 card_id,
895                 cx2341x_menu_item(p, V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE),
896                 p->video_luma_median_filter_bottom,
897                 p->video_luma_median_filter_top,
898                 p->video_chroma_median_filter_bottom,
899                 p->video_chroma_median_filter_top);
900 }
901
902 EXPORT_SYMBOL(cx2341x_fill_defaults);
903 EXPORT_SYMBOL(cx2341x_ctrl_query);
904 EXPORT_SYMBOL(cx2341x_ctrl_get_menu);
905 EXPORT_SYMBOL(cx2341x_ext_ctrls);
906 EXPORT_SYMBOL(cx2341x_update);
907 EXPORT_SYMBOL(cx2341x_log_status);
908 EXPORT_SYMBOL(cx2341x_mpeg_ctrls);
909
910 /*
911  * Local variables:
912  * c-basic-offset: 8
913  * End:
914  */
915