Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1 | /* |
| 2 | * cx18 ioctl control functions |
| 3 | * |
| 4 | * Derived from ivtv-controls.c |
| 5 | * |
| 6 | * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 21 | * 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include "cx18-driver.h" |
| 25 | #include "cx18-av-core.h" |
| 26 | #include "cx18-cards.h" |
| 27 | #include "cx18-ioctl.h" |
| 28 | #include "cx18-audio.h" |
| 29 | #include "cx18-i2c.h" |
| 30 | #include "cx18-mailbox.h" |
| 31 | #include "cx18-controls.h" |
| 32 | |
Hans Verkuil | 2ba5889 | 2009-02-13 10:57:48 -0300 | [diff] [blame^] | 33 | /* Must be sorted from low to high control ID! */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 34 | static const u32 user_ctrls[] = { |
| 35 | V4L2_CID_USER_CLASS, |
| 36 | V4L2_CID_BRIGHTNESS, |
| 37 | V4L2_CID_CONTRAST, |
| 38 | V4L2_CID_SATURATION, |
| 39 | V4L2_CID_HUE, |
| 40 | V4L2_CID_AUDIO_VOLUME, |
| 41 | V4L2_CID_AUDIO_BALANCE, |
| 42 | V4L2_CID_AUDIO_BASS, |
| 43 | V4L2_CID_AUDIO_TREBLE, |
| 44 | V4L2_CID_AUDIO_MUTE, |
| 45 | V4L2_CID_AUDIO_LOUDNESS, |
| 46 | 0 |
| 47 | }; |
| 48 | |
| 49 | static const u32 *ctrl_classes[] = { |
| 50 | user_ctrls, |
| 51 | cx2341x_mpeg_ctrls, |
| 52 | NULL |
| 53 | }; |
| 54 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 55 | int cx18_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 56 | { |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 57 | struct cx18 *cx = ((struct cx18_open_id *)fh)->cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 58 | const char *name; |
| 59 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 60 | qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id); |
| 61 | if (qctrl->id == 0) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | switch (qctrl->id) { |
| 65 | /* Standard V4L2 controls */ |
| 66 | case V4L2_CID_BRIGHTNESS: |
| 67 | case V4L2_CID_HUE: |
| 68 | case V4L2_CID_SATURATION: |
| 69 | case V4L2_CID_CONTRAST: |
| 70 | if (cx18_av_cmd(cx, VIDIOC_QUERYCTRL, qctrl)) |
| 71 | qctrl->flags |= V4L2_CTRL_FLAG_DISABLED; |
| 72 | return 0; |
| 73 | |
| 74 | case V4L2_CID_AUDIO_VOLUME: |
| 75 | case V4L2_CID_AUDIO_MUTE: |
| 76 | case V4L2_CID_AUDIO_BALANCE: |
| 77 | case V4L2_CID_AUDIO_BASS: |
| 78 | case V4L2_CID_AUDIO_TREBLE: |
| 79 | case V4L2_CID_AUDIO_LOUDNESS: |
| 80 | if (cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_QUERYCTRL, qctrl)) |
| 81 | qctrl->flags |= V4L2_CTRL_FLAG_DISABLED; |
| 82 | return 0; |
| 83 | |
| 84 | default: |
| 85 | if (cx2341x_ctrl_query(&cx->params, qctrl)) |
| 86 | qctrl->flags |= V4L2_CTRL_FLAG_DISABLED; |
| 87 | return 0; |
| 88 | } |
| 89 | strncpy(qctrl->name, name, sizeof(qctrl->name) - 1); |
| 90 | qctrl->name[sizeof(qctrl->name) - 1] = 0; |
| 91 | return 0; |
| 92 | } |
| 93 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 94 | int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 95 | { |
Hans Verkuil | e0e31cd | 2008-06-22 12:03:28 -0300 | [diff] [blame] | 96 | struct cx18 *cx = ((struct cx18_open_id *)fh)->cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 97 | struct v4l2_queryctrl qctrl; |
| 98 | |
| 99 | qctrl.id = qmenu->id; |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 100 | cx18_queryctrl(file, fh, &qctrl); |
Hans Verkuil | e0e31cd | 2008-06-22 12:03:28 -0300 | [diff] [blame] | 101 | return v4l2_ctrl_query_menu(qmenu, &qctrl, |
| 102 | cx2341x_ctrl_get_menu(&cx->params, qmenu->id)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 103 | } |
| 104 | |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 105 | static int cx18_try_ctrl(struct file *file, void *fh, |
| 106 | struct v4l2_ext_control *vctrl) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 107 | { |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 108 | struct v4l2_queryctrl qctrl; |
| 109 | const char **menu_items = NULL; |
| 110 | int err; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 111 | |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 112 | qctrl.id = vctrl->id; |
| 113 | err = cx18_queryctrl(file, fh, &qctrl); |
| 114 | if (err) |
| 115 | return err; |
| 116 | if (qctrl.type == V4L2_CTRL_TYPE_MENU) |
| 117 | menu_items = v4l2_ctrl_get_menu(qctrl.id); |
| 118 | return v4l2_ctrl_check(vctrl, &qctrl, menu_items); |
| 119 | } |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 120 | |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 121 | static int cx18_s_ctrl(struct cx18 *cx, struct v4l2_control *vctrl) |
| 122 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 123 | switch (vctrl->id) { |
| 124 | /* Standard V4L2 controls */ |
| 125 | case V4L2_CID_BRIGHTNESS: |
| 126 | case V4L2_CID_HUE: |
| 127 | case V4L2_CID_SATURATION: |
| 128 | case V4L2_CID_CONTRAST: |
| 129 | return cx18_av_cmd(cx, VIDIOC_S_CTRL, vctrl); |
| 130 | |
| 131 | case V4L2_CID_AUDIO_VOLUME: |
| 132 | case V4L2_CID_AUDIO_MUTE: |
| 133 | case V4L2_CID_AUDIO_BALANCE: |
| 134 | case V4L2_CID_AUDIO_BASS: |
| 135 | case V4L2_CID_AUDIO_TREBLE: |
| 136 | case V4L2_CID_AUDIO_LOUDNESS: |
| 137 | return cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl); |
| 138 | |
| 139 | default: |
Hans Verkuil | 37f89f9 | 2008-06-22 11:57:31 -0300 | [diff] [blame] | 140 | CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 146 | static int cx18_g_ctrl(struct cx18 *cx, struct v4l2_control *vctrl) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 147 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 148 | switch (vctrl->id) { |
| 149 | /* Standard V4L2 controls */ |
| 150 | case V4L2_CID_BRIGHTNESS: |
| 151 | case V4L2_CID_HUE: |
| 152 | case V4L2_CID_SATURATION: |
| 153 | case V4L2_CID_CONTRAST: |
| 154 | return cx18_av_cmd(cx, VIDIOC_G_CTRL, vctrl); |
| 155 | |
| 156 | case V4L2_CID_AUDIO_VOLUME: |
| 157 | case V4L2_CID_AUDIO_MUTE: |
| 158 | case V4L2_CID_AUDIO_BALANCE: |
| 159 | case V4L2_CID_AUDIO_BASS: |
| 160 | case V4L2_CID_AUDIO_TREBLE: |
| 161 | case V4L2_CID_AUDIO_LOUDNESS: |
| 162 | return cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl); |
| 163 | default: |
Hans Verkuil | 37f89f9 | 2008-06-22 11:57:31 -0300 | [diff] [blame] | 164 | CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int cx18_setup_vbi_fmt(struct cx18 *cx, enum v4l2_mpeg_stream_vbi_fmt fmt) |
| 171 | { |
| 172 | if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE)) |
| 173 | return -EINVAL; |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 174 | if (atomic_read(&cx->ana_capturing) > 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 175 | return -EBUSY; |
| 176 | |
| 177 | /* First try to allocate sliced VBI buffers if needed. */ |
| 178 | if (fmt && cx->vbi.sliced_mpeg_data[0] == NULL) { |
| 179 | int i; |
| 180 | |
| 181 | for (i = 0; i < CX18_VBI_FRAMES; i++) { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 182 | cx->vbi.sliced_mpeg_data[i] = |
| 183 | kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 184 | if (cx->vbi.sliced_mpeg_data[i] == NULL) { |
| 185 | while (--i >= 0) { |
| 186 | kfree(cx->vbi.sliced_mpeg_data[i]); |
| 187 | cx->vbi.sliced_mpeg_data[i] = NULL; |
| 188 | } |
| 189 | return -ENOMEM; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | cx->vbi.insert_mpeg = fmt; |
| 195 | |
| 196 | if (cx->vbi.insert_mpeg == 0) |
| 197 | return 0; |
| 198 | /* Need sliced data for mpeg insertion */ |
Mauro Carvalho Chehab | aed6abd | 2008-04-29 21:38:51 -0300 | [diff] [blame] | 199 | if (cx18_get_service_set(cx->vbi.sliced_in) == 0) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 200 | if (cx->is_60hz) |
| 201 | cx->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525; |
| 202 | else |
| 203 | cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625; |
Mauro Carvalho Chehab | aed6abd | 2008-04-29 21:38:51 -0300 | [diff] [blame] | 204 | cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 209 | int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 210 | { |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 211 | struct cx18 *cx = ((struct cx18_open_id *)fh)->cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 212 | struct v4l2_control ctrl; |
| 213 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 214 | if (c->ctrl_class == V4L2_CTRL_CLASS_USER) { |
| 215 | int i; |
| 216 | int err = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 217 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 218 | for (i = 0; i < c->count; i++) { |
| 219 | ctrl.id = c->controls[i].id; |
| 220 | ctrl.value = c->controls[i].value; |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 221 | err = cx18_g_ctrl(cx, &ctrl); |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 222 | c->controls[i].value = ctrl.value; |
| 223 | if (err) { |
| 224 | c->error_idx = i; |
| 225 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 226 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 227 | } |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 228 | return err; |
| 229 | } |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 230 | if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) |
| 231 | return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS); |
| 232 | return -EINVAL; |
| 233 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 234 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 235 | int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c) |
| 236 | { |
| 237 | struct cx18_open_id *id = fh; |
| 238 | struct cx18 *cx = id->cx; |
| 239 | int ret; |
| 240 | struct v4l2_control ctrl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 241 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 242 | ret = v4l2_prio_check(&cx->prio, &id->prio); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | |
| 246 | if (c->ctrl_class == V4L2_CTRL_CLASS_USER) { |
| 247 | int i; |
| 248 | int err = 0; |
| 249 | |
| 250 | for (i = 0; i < c->count; i++) { |
| 251 | ctrl.id = c->controls[i].id; |
| 252 | ctrl.value = c->controls[i].value; |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 253 | err = cx18_s_ctrl(cx, &ctrl); |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 254 | c->controls[i].value = ctrl.value; |
| 255 | if (err) { |
| 256 | c->error_idx = i; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | return err; |
| 261 | } |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 262 | if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) { |
Andy Walls | 50b86ba | 2008-11-23 19:16:44 -0300 | [diff] [blame] | 263 | struct cx18_api_func_private priv; |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 264 | struct cx2341x_mpeg_params p = cx->params; |
| 265 | int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing), |
| 266 | c, VIDIOC_S_EXT_CTRLS); |
| 267 | |
| 268 | if (err) |
| 269 | return err; |
| 270 | |
| 271 | if (p.video_encoding != cx->params.video_encoding) { |
| 272 | int is_mpeg1 = p.video_encoding == |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 273 | V4L2_MPEG_VIDEO_ENCODING_MPEG_1; |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 274 | struct v4l2_format fmt; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 275 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 276 | /* fix videodecoder resolution */ |
| 277 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 278 | fmt.fmt.pix.width = cx->params.width |
| 279 | / (is_mpeg1 ? 2 : 1); |
| 280 | fmt.fmt.pix.height = cx->params.height; |
| 281 | cx18_av_cmd(cx, VIDIOC_S_FMT, &fmt); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 282 | } |
Andy Walls | 50b86ba | 2008-11-23 19:16:44 -0300 | [diff] [blame] | 283 | priv.cx = cx; |
| 284 | priv.s = &cx->streams[id->type]; |
| 285 | err = cx2341x_update(&priv, cx18_api_func, &cx->params, &p); |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 286 | if (!err && cx->params.stream_vbi_fmt != p.stream_vbi_fmt) |
| 287 | err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt); |
| 288 | cx->params = p; |
| 289 | cx->dualwatch_stereo_mode = p.audio_properties & 0x0300; |
| 290 | cx18_audio_set_audio_clock_freq(cx, p.audio_properties & 0x03); |
| 291 | return err; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 292 | } |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 295 | |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 296 | int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c) |
| 297 | { |
| 298 | struct cx18 *cx = ((struct cx18_open_id *)fh)->cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 299 | |
Hans Verkuil | adb65bc | 2008-06-25 06:32:44 -0300 | [diff] [blame] | 300 | if (c->ctrl_class == V4L2_CTRL_CLASS_USER) { |
| 301 | int i; |
| 302 | int err = 0; |
| 303 | |
| 304 | for (i = 0; i < c->count; i++) { |
| 305 | err = cx18_try_ctrl(file, fh, &c->controls[i]); |
| 306 | if (err) { |
| 307 | c->error_idx = i; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | return err; |
| 312 | } |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 313 | if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) |
| 314 | return cx2341x_ext_ctrls(&cx->params, |
| 315 | atomic_read(&cx->ana_capturing), |
| 316 | c, VIDIOC_TRY_EXT_CTRLS); |
| 317 | return -EINVAL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 318 | } |