Steven Toth | 9b8b019 | 2010-07-31 14:39:44 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the NXP SAA7164 PCIe bridge |
| 3 | * |
| 4 | * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com> |
| 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 | * |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "saa7164.h" |
| 23 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 24 | #define ENCODER_MAX_BITRATE 6500000 |
| 25 | #define ENCODER_MIN_BITRATE 1000000 |
| 26 | #define ENCODER_DEF_BITRATE 5000000 |
| 27 | |
| 28 | static struct saa7164_tvnorm saa7164_tvnorms[] = { |
| 29 | { |
| 30 | .name = "NTSC-M", |
| 31 | .id = V4L2_STD_NTSC_M, |
| 32 | }, { |
| 33 | .name = "NTSC-JP", |
| 34 | .id = V4L2_STD_NTSC_M_JP, |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | static const u32 saa7164_v4l2_ctrls[] = { |
| 39 | V4L2_CID_BRIGHTNESS, |
| 40 | V4L2_CID_CONTRAST, |
| 41 | V4L2_CID_SATURATION, |
| 42 | V4L2_CID_HUE, |
| 43 | V4L2_CID_AUDIO_VOLUME, |
| 44 | V4L2_CID_SHARPNESS, |
| 45 | V4L2_CID_MPEG_VIDEO_ASPECT, |
| 46 | V4L2_CID_MPEG_STREAM_TYPE, |
| 47 | V4L2_CID_MPEG_AUDIO_MUTE, |
Steven Toth | 2600d71 | 2010-07-31 14:51:30 -0300 | [diff] [blame] | 48 | V4L2_CID_MPEG_VIDEO_BITRATE_MODE, |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 49 | V4L2_CID_MPEG_VIDEO_BITRATE, |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame^] | 50 | V4L2_CID_MPEG_VIDEO_B_FRAMES, |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 51 | 0 |
| 52 | }; |
| 53 | |
| 54 | /* Take the encoder configuration form the port struct and |
| 55 | * flush it to the hardware. |
| 56 | */ |
| 57 | static void saa7164_encoder_configure(struct saa7164_port *port) |
| 58 | { |
| 59 | struct saa7164_dev *dev = port->dev; |
| 60 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 61 | |
| 62 | port->encoder_params.width = port->width; |
| 63 | port->encoder_params.height = port->height; |
| 64 | port->encoder_params.is_50hz = |
| 65 | (port->encodernorm.id & V4L2_STD_625_50) != 0; |
| 66 | |
| 67 | /* Set up the DIF (enable it) for analog mode by default */ |
| 68 | saa7164_api_initialize_dif(port); |
| 69 | |
| 70 | /* Configure the correct video standard */ |
| 71 | saa7164_api_configure_dif(port, port->encodernorm.id); |
| 72 | |
| 73 | /* Ensure the audio decoder is correct configured */ |
| 74 | saa7164_api_set_audio_std(port); |
| 75 | } |
| 76 | |
| 77 | /* One time configuration at registration time */ |
| 78 | static int saa7164_encoder_initialize(struct saa7164_port *port) |
| 79 | { |
| 80 | struct saa7164_dev *dev = port->dev; |
| 81 | |
| 82 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 83 | |
| 84 | saa7164_encoder_configure(port); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* -- V4L2 --------------------------------------------------------- */ |
| 90 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id) |
| 91 | { |
| 92 | struct saa7164_fh *fh = file->private_data; |
| 93 | struct saa7164_port *port = fh->port; |
| 94 | struct saa7164_dev *dev = port->dev; |
| 95 | unsigned int i; |
| 96 | |
| 97 | dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)*id); |
| 98 | |
| 99 | for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) { |
| 100 | if (*id & saa7164_tvnorms[i].id) |
| 101 | break; |
| 102 | } |
| 103 | if (i == ARRAY_SIZE(saa7164_tvnorms)) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | port->encodernorm = saa7164_tvnorms[i]; |
| 107 | |
| 108 | /* Update the audio decoder while is not running in |
| 109 | * auto detect mode. |
| 110 | */ |
| 111 | saa7164_api_set_audio_std(port); |
| 112 | |
| 113 | dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)*id); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int vidioc_enum_input(struct file *file, void *priv, |
| 119 | struct v4l2_input *i) |
| 120 | { |
| 121 | int n; |
| 122 | |
| 123 | char *inputs[] = { "tuner", "composite", "svideo", "aux", |
| 124 | "composite", "svideo", "aux" }; |
| 125 | |
| 126 | if (i->index >= 7) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | strcpy(i->name, inputs[ i->index ]); |
| 130 | |
| 131 | if (i->index == 0) |
| 132 | i->type = V4L2_INPUT_TYPE_TUNER; |
| 133 | else |
| 134 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 135 | |
| 136 | for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++) |
| 137 | i->std |= saa7164_tvnorms[n].id; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 143 | { |
| 144 | struct saa7164_fh *fh = file->private_data; |
| 145 | struct saa7164_port *port = fh->port; |
| 146 | struct saa7164_dev *dev = port->dev; |
| 147 | |
| 148 | if (saa7164_api_get_videomux(port) != SAA_OK) |
| 149 | return -EIO; |
| 150 | |
| 151 | *i = (port->mux_input - 1); |
| 152 | |
| 153 | dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 159 | { |
| 160 | struct saa7164_fh *fh = file->private_data; |
| 161 | struct saa7164_port *port = fh->port; |
| 162 | struct saa7164_dev *dev = port->dev; |
| 163 | |
| 164 | dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i); |
| 165 | |
| 166 | if (i >= 7) |
| 167 | return -EINVAL; |
| 168 | |
| 169 | port->mux_input = i + 1; |
| 170 | |
| 171 | if (saa7164_api_set_videomux(port) != SAA_OK) |
| 172 | return -EIO; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 178 | struct v4l2_tuner *t) |
| 179 | { |
| 180 | struct saa7164_fh *fh = file->private_data; |
| 181 | struct saa7164_port *port = fh->port; |
| 182 | struct saa7164_dev *dev = port->dev; |
| 183 | |
| 184 | if (0 != t->index) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | strcpy(t->name, "tuner"); |
| 188 | t->type = V4L2_TUNER_ANALOG_TV; |
| 189 | t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO; |
| 190 | |
| 191 | dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 197 | struct v4l2_tuner *t) |
| 198 | { |
| 199 | |
| 200 | /* Update the A/V core */ |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 206 | struct v4l2_frequency *f) |
| 207 | { |
| 208 | struct saa7164_fh *fh = file->private_data; |
| 209 | struct saa7164_port *port = fh->port; |
| 210 | |
| 211 | f->type = V4L2_TUNER_ANALOG_TV; |
| 212 | f->frequency = port->freq; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 218 | struct v4l2_frequency *f) |
| 219 | { |
| 220 | struct saa7164_fh *fh = file->private_data; |
| 221 | struct saa7164_port *port = fh->port; |
| 222 | struct saa7164_dev *dev = port->dev; |
| 223 | struct saa7164_port *tsport; |
| 224 | struct dvb_frontend *fe; |
| 225 | |
| 226 | /* TODO: Pull this for the std */ |
| 227 | struct analog_parameters params = { |
| 228 | .mode = V4L2_TUNER_ANALOG_TV, |
| 229 | .audmode = V4L2_TUNER_MODE_STEREO, |
| 230 | .std = port->encodernorm.id, |
| 231 | .frequency = f->frequency |
| 232 | }; |
| 233 | |
| 234 | /* Stop the encoder */ |
| 235 | dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__, |
| 236 | f->frequency, f->tuner); |
| 237 | |
| 238 | if (f->tuner != 0) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | if (f->type != V4L2_TUNER_ANALOG_TV) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | port->freq = f->frequency; |
| 245 | |
| 246 | /* Update the hardware */ |
| 247 | if (port->nr == SAA7164_PORT_ENC1) |
| 248 | tsport = &dev->ports[ SAA7164_PORT_TS1 ]; |
| 249 | else |
| 250 | if (port->nr == SAA7164_PORT_ENC2) |
| 251 | tsport = &dev->ports[ SAA7164_PORT_TS2 ]; |
| 252 | else |
| 253 | BUG(); |
| 254 | |
| 255 | fe = tsport->dvb.frontend; |
| 256 | |
| 257 | if (fe && fe->ops.tuner_ops.set_analog_params) |
| 258 | fe->ops.tuner_ops.set_analog_params(fe, ¶ms); |
| 259 | else |
| 260 | printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__); |
| 261 | |
| 262 | saa7164_encoder_initialize(port); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 268 | struct v4l2_control *ctl) |
| 269 | { |
| 270 | struct saa7164_fh *fh = file->private_data; |
| 271 | struct saa7164_port *port = fh->port; |
| 272 | struct saa7164_dev *dev = port->dev; |
| 273 | |
| 274 | dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__, |
| 275 | ctl->id, ctl->value); |
| 276 | |
| 277 | switch (ctl->id) { |
| 278 | case V4L2_CID_BRIGHTNESS: |
| 279 | ctl->value = port->ctl_brightness; |
| 280 | break; |
| 281 | case V4L2_CID_CONTRAST: |
| 282 | ctl->value = port->ctl_contrast; |
| 283 | break; |
| 284 | case V4L2_CID_SATURATION: |
| 285 | ctl->value = port->ctl_saturation; |
| 286 | break; |
| 287 | case V4L2_CID_HUE: |
| 288 | ctl->value = port->ctl_hue; |
| 289 | break; |
| 290 | case V4L2_CID_SHARPNESS: |
| 291 | ctl->value = port->ctl_sharpness; |
| 292 | break; |
| 293 | case V4L2_CID_AUDIO_VOLUME: |
| 294 | ctl->value = port->ctl_volume; |
| 295 | break; |
| 296 | default: |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 304 | struct v4l2_control *ctl) |
| 305 | { |
| 306 | struct saa7164_fh *fh = file->private_data; |
| 307 | struct saa7164_port *port = fh->port; |
| 308 | struct saa7164_dev *dev = port->dev; |
| 309 | int ret = 0; |
| 310 | |
| 311 | dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__, |
| 312 | ctl->id, ctl->value); |
| 313 | |
| 314 | switch (ctl->id) { |
| 315 | case V4L2_CID_BRIGHTNESS: |
| 316 | if ((ctl->value >= 0) && (ctl->value <= 255)) { |
| 317 | port->ctl_brightness = ctl->value; |
| 318 | saa7164_api_set_usercontrol(port, |
| 319 | PU_BRIGHTNESS_CONTROL); |
| 320 | } else |
| 321 | ret = -EINVAL; |
| 322 | break; |
| 323 | case V4L2_CID_CONTRAST: |
| 324 | if ((ctl->value >= 0) && (ctl->value <= 255)) { |
| 325 | port->ctl_contrast = ctl->value; |
| 326 | saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL); |
| 327 | } else |
| 328 | ret = -EINVAL; |
| 329 | break; |
| 330 | case V4L2_CID_SATURATION: |
| 331 | if ((ctl->value >= 0) && (ctl->value <= 255)) { |
| 332 | port->ctl_saturation = ctl->value; |
| 333 | saa7164_api_set_usercontrol(port, |
| 334 | PU_SATURATION_CONTROL); |
| 335 | } else |
| 336 | ret = -EINVAL; |
| 337 | break; |
| 338 | case V4L2_CID_HUE: |
| 339 | if ((ctl->value >= 0) && (ctl->value <= 255)) { |
| 340 | port->ctl_hue = ctl->value; |
| 341 | saa7164_api_set_usercontrol(port, PU_HUE_CONTROL); |
| 342 | } else |
| 343 | ret = -EINVAL; |
| 344 | break; |
| 345 | case V4L2_CID_SHARPNESS: |
| 346 | if ((ctl->value >= 0) && (ctl->value <= 255)) { |
| 347 | port->ctl_sharpness = ctl->value; |
| 348 | saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL); |
| 349 | } else |
| 350 | ret = -EINVAL; |
| 351 | break; |
| 352 | case V4L2_CID_AUDIO_VOLUME: |
| 353 | if ((ctl->value >= -83) && (ctl->value <= 24)) { |
| 354 | port->ctl_volume = ctl->value; |
| 355 | saa7164_api_set_audio_volume(port, port->ctl_volume); |
| 356 | } else |
| 357 | ret = -EINVAL; |
| 358 | break; |
| 359 | default: |
| 360 | ret = -EINVAL; |
| 361 | } |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static int saa7164_get_ctrl(struct saa7164_port *port, |
| 367 | struct v4l2_ext_control *ctrl) |
| 368 | { |
| 369 | struct saa7164_encoder_params *params = &port->encoder_params; |
| 370 | |
| 371 | switch (ctrl->id) { |
| 372 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 373 | ctrl->value = params->bitrate; |
| 374 | break; |
| 375 | case V4L2_CID_MPEG_STREAM_TYPE: |
| 376 | ctrl->value = params->stream_type; |
| 377 | break; |
| 378 | case V4L2_CID_MPEG_AUDIO_MUTE: |
| 379 | ctrl->value = params->ctl_mute; |
| 380 | break; |
| 381 | case V4L2_CID_MPEG_VIDEO_ASPECT: |
| 382 | ctrl->value = params->ctl_aspect; |
| 383 | break; |
Steven Toth | 2600d71 | 2010-07-31 14:51:30 -0300 | [diff] [blame] | 384 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: |
| 385 | ctrl->value = params->bitrate_mode; |
| 386 | break; |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame^] | 387 | case V4L2_CID_MPEG_VIDEO_B_FRAMES: |
| 388 | ctrl->value = params->refdist; |
| 389 | break; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 390 | default: |
| 391 | return -EINVAL; |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int vidioc_g_ext_ctrls(struct file *file, void *priv, |
| 397 | struct v4l2_ext_controls *ctrls) |
| 398 | { |
| 399 | struct saa7164_fh *fh = file->private_data; |
| 400 | struct saa7164_port *port = fh->port; |
| 401 | int i, err = 0; |
| 402 | |
| 403 | if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) { |
| 404 | for (i = 0; i < ctrls->count; i++) { |
| 405 | struct v4l2_ext_control *ctrl = ctrls->controls + i; |
| 406 | |
| 407 | err = saa7164_get_ctrl(port, ctrl); |
| 408 | if (err) { |
| 409 | ctrls->error_idx = i; |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | return err; |
| 414 | |
| 415 | } |
| 416 | |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3) |
| 421 | { |
| 422 | int ret = -EINVAL; |
| 423 | |
| 424 | switch (ctrl->id) { |
| 425 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 426 | if ((ctrl->value >= ENCODER_MIN_BITRATE) && |
| 427 | (ctrl->value <= ENCODER_MAX_BITRATE)) |
| 428 | ret = 0; |
| 429 | break; |
| 430 | case V4L2_CID_MPEG_STREAM_TYPE: |
| 431 | if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) |
| 432 | ret = 0; |
| 433 | break; |
| 434 | case V4L2_CID_MPEG_AUDIO_MUTE: |
| 435 | if ((ctrl->value >= 0) && |
| 436 | (ctrl->value <= 1)) |
| 437 | ret = 0; |
| 438 | break; |
| 439 | case V4L2_CID_MPEG_VIDEO_ASPECT: |
| 440 | if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) && |
| 441 | (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100)) |
| 442 | ret = 0; |
| 443 | break; |
| 444 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: |
| 445 | if ((ctrl->value >= 0) && |
| 446 | (ctrl->value <= 255)) |
| 447 | ret = 0; |
| 448 | break; |
Steven Toth | 2600d71 | 2010-07-31 14:51:30 -0300 | [diff] [blame] | 449 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: |
| 450 | if ((ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) || |
| 451 | (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)) |
| 452 | ret = 0; |
| 453 | break; |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame^] | 454 | case V4L2_CID_MPEG_VIDEO_B_FRAMES: |
| 455 | if ((ctrl->value >= 1) && |
| 456 | (ctrl->value <= 3)) |
| 457 | ret = 0; |
| 458 | break; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 459 | default: |
| 460 | ret = -EINVAL; |
| 461 | } |
| 462 | |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | static int vidioc_try_ext_ctrls(struct file *file, void *priv, |
| 467 | struct v4l2_ext_controls *ctrls) |
| 468 | { |
| 469 | int i, err = 0; |
| 470 | |
| 471 | if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) { |
| 472 | for (i = 0; i < ctrls->count; i++) { |
| 473 | struct v4l2_ext_control *ctrl = ctrls->controls + i; |
| 474 | |
| 475 | err = saa7164_try_ctrl(ctrl, 0); |
| 476 | if (err) { |
| 477 | ctrls->error_idx = i; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | return err; |
| 482 | } |
| 483 | |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | static int saa7164_set_ctrl(struct saa7164_port *port, |
| 488 | struct v4l2_ext_control *ctrl) |
| 489 | { |
| 490 | struct saa7164_encoder_params *params = &port->encoder_params; |
| 491 | int ret = 0; |
| 492 | |
| 493 | switch (ctrl->id) { |
| 494 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 495 | params->bitrate = ctrl->value; |
| 496 | break; |
| 497 | case V4L2_CID_MPEG_STREAM_TYPE: |
| 498 | params->stream_type = ctrl->value; |
| 499 | break; |
| 500 | case V4L2_CID_MPEG_AUDIO_MUTE: |
| 501 | params->ctl_mute = ctrl->value; |
| 502 | ret = saa7164_api_audio_mute(port, params->ctl_mute); |
| 503 | if (ret != SAA_OK) { |
| 504 | printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, |
| 505 | ret); |
| 506 | ret = -EIO; |
| 507 | } |
| 508 | break; |
| 509 | case V4L2_CID_MPEG_VIDEO_ASPECT: |
| 510 | params->ctl_aspect = ctrl->value; |
| 511 | ret = saa7164_api_set_aspect_ratio(port); |
| 512 | if (ret != SAA_OK) { |
| 513 | printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, |
| 514 | ret); |
| 515 | ret = -EIO; |
| 516 | } |
| 517 | break; |
Steven Toth | 2600d71 | 2010-07-31 14:51:30 -0300 | [diff] [blame] | 518 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: |
| 519 | params->bitrate_mode = ctrl->value; |
| 520 | break; |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame^] | 521 | case V4L2_CID_MPEG_VIDEO_B_FRAMES: |
| 522 | params->refdist = ctrl->value; |
| 523 | break; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 524 | default: |
| 525 | return -EINVAL; |
| 526 | } |
| 527 | |
| 528 | /* TODO: Update the hardware */ |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | static int vidioc_s_ext_ctrls(struct file *file, void *priv, |
| 534 | struct v4l2_ext_controls *ctrls) |
| 535 | { |
| 536 | struct saa7164_fh *fh = file->private_data; |
| 537 | struct saa7164_port *port = fh->port; |
| 538 | int i, err = 0; |
| 539 | |
| 540 | if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) { |
| 541 | for (i = 0; i < ctrls->count; i++) { |
| 542 | struct v4l2_ext_control *ctrl = ctrls->controls + i; |
| 543 | |
| 544 | err = saa7164_try_ctrl(ctrl, 0); |
| 545 | if (err) { |
| 546 | ctrls->error_idx = i; |
| 547 | break; |
| 548 | } |
| 549 | err = saa7164_set_ctrl(port, ctrl); |
| 550 | if (err) { |
| 551 | ctrls->error_idx = i; |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | return err; |
| 556 | |
| 557 | } |
| 558 | |
| 559 | return -EINVAL; |
| 560 | } |
| 561 | |
| 562 | static int vidioc_querycap(struct file *file, void *priv, |
| 563 | struct v4l2_capability *cap) |
| 564 | { |
| 565 | struct saa7164_fh *fh = file->private_data; |
| 566 | struct saa7164_port *port = fh->port; |
| 567 | struct saa7164_dev *dev = port->dev; |
| 568 | |
| 569 | strcpy(cap->driver, dev->name); |
| 570 | strlcpy(cap->card, saa7164_boards[dev->board].name, |
| 571 | sizeof(cap->card)); |
| 572 | sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci)); |
| 573 | |
| 574 | cap->capabilities = |
| 575 | V4L2_CAP_VIDEO_CAPTURE | |
| 576 | V4L2_CAP_READWRITE | |
| 577 | V4L2_CAP_STREAMING | |
| 578 | 0; |
| 579 | |
| 580 | cap->capabilities |= V4L2_CAP_TUNER; |
| 581 | cap->version = 0; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
| 587 | struct v4l2_fmtdesc *f) |
| 588 | { |
| 589 | if (f->index != 0) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | strlcpy(f->description, "MPEG", sizeof(f->description)); |
| 593 | f->pixelformat = V4L2_PIX_FMT_MPEG; |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
| 599 | struct v4l2_format *f) |
| 600 | { |
| 601 | struct saa7164_fh *fh = file->private_data; |
| 602 | struct saa7164_port *port = fh->port; |
| 603 | struct saa7164_dev *dev = port->dev; |
| 604 | |
| 605 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 606 | f->fmt.pix.bytesperline = 0; |
| 607 | f->fmt.pix.sizeimage = |
| 608 | port->ts_packet_size * port->ts_packet_count; |
| 609 | f->fmt.pix.colorspace = 0; |
| 610 | f->fmt.pix.width = port->width; |
| 611 | f->fmt.pix.height = port->height; |
| 612 | |
| 613 | dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n", |
| 614 | port->width, port->height); |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 620 | struct v4l2_format *f) |
| 621 | { |
| 622 | struct saa7164_fh *fh = file->private_data; |
| 623 | struct saa7164_port *port = fh->port; |
| 624 | struct saa7164_dev *dev = port->dev; |
| 625 | |
| 626 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 627 | f->fmt.pix.bytesperline = 0; |
| 628 | f->fmt.pix.sizeimage = |
| 629 | port->ts_packet_size * port->ts_packet_count; |
| 630 | f->fmt.pix.colorspace = 0; |
| 631 | dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n", |
| 632 | port->width, port->height); |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 637 | struct v4l2_format *f) |
| 638 | { |
| 639 | struct saa7164_fh *fh = file->private_data; |
| 640 | struct saa7164_port *port = fh->port; |
| 641 | struct saa7164_dev *dev = port->dev; |
| 642 | |
| 643 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 644 | f->fmt.pix.bytesperline = 0; |
| 645 | f->fmt.pix.sizeimage = |
| 646 | port->ts_packet_size * port->ts_packet_count; |
| 647 | f->fmt.pix.colorspace = 0; |
| 648 | |
| 649 | dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n", |
| 650 | f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field); |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int vidioc_log_status(struct file *file, void *priv) |
| 656 | { |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static int fill_queryctrl(struct saa7164_encoder_params *params, |
| 661 | struct v4l2_queryctrl *c) |
| 662 | { |
| 663 | switch (c->id) { |
| 664 | case V4L2_CID_BRIGHTNESS: |
| 665 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127); |
| 666 | case V4L2_CID_CONTRAST: |
| 667 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66); |
| 668 | case V4L2_CID_SATURATION: |
| 669 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62); |
| 670 | case V4L2_CID_HUE: |
| 671 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128); |
| 672 | case V4L2_CID_SHARPNESS: |
| 673 | return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8); |
| 674 | case V4L2_CID_MPEG_AUDIO_MUTE: |
| 675 | return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0); |
| 676 | case V4L2_CID_AUDIO_VOLUME: |
| 677 | return v4l2_ctrl_query_fill(c, -83, 24, 1, 20); |
| 678 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 679 | return v4l2_ctrl_query_fill(c, |
| 680 | ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE, |
| 681 | 100000, ENCODER_DEF_BITRATE); |
| 682 | case V4L2_CID_MPEG_STREAM_TYPE: |
| 683 | return v4l2_ctrl_query_fill(c, |
| 684 | V4L2_MPEG_STREAM_TYPE_MPEG2_PS, |
| 685 | V4L2_MPEG_STREAM_TYPE_MPEG2_PS, |
| 686 | 0, V4L2_MPEG_STREAM_TYPE_MPEG2_PS); |
| 687 | case V4L2_CID_MPEG_VIDEO_ASPECT: |
| 688 | return v4l2_ctrl_query_fill(c, |
| 689 | V4L2_MPEG_VIDEO_ASPECT_1x1, |
| 690 | V4L2_MPEG_VIDEO_ASPECT_221x100, |
| 691 | 1, V4L2_MPEG_VIDEO_ASPECT_4x3); |
| 692 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: |
| 693 | return v4l2_ctrl_query_fill(c, 1, 255, 1, 15); |
Steven Toth | 2600d71 | 2010-07-31 14:51:30 -0300 | [diff] [blame] | 694 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: |
| 695 | return v4l2_ctrl_query_fill(c, |
| 696 | V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, |
| 697 | 1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame^] | 698 | case V4L2_CID_MPEG_VIDEO_B_FRAMES: |
| 699 | return v4l2_ctrl_query_fill(c, |
| 700 | 1, 3, 1, 1); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 701 | default: |
| 702 | return -EINVAL; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 707 | struct v4l2_queryctrl *c) |
| 708 | { |
| 709 | struct saa7164_fh *fh = priv; |
| 710 | struct saa7164_port *port = fh->port; |
| 711 | int i, next; |
| 712 | u32 id = c->id; |
| 713 | |
| 714 | memset(c, 0, sizeof(*c)); |
| 715 | |
| 716 | next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL); |
| 717 | c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL; |
| 718 | |
| 719 | for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) { |
| 720 | if (next) { |
| 721 | if (c->id < saa7164_v4l2_ctrls[i]) |
| 722 | c->id = saa7164_v4l2_ctrls[i]; |
| 723 | else |
| 724 | continue; |
| 725 | } |
| 726 | |
| 727 | if (c->id == saa7164_v4l2_ctrls[i]) |
| 728 | return fill_queryctrl(&port->encoder_params, c); |
| 729 | |
| 730 | if (c->id < saa7164_v4l2_ctrls[i]) |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | return -EINVAL; |
| 735 | } |
| 736 | |
| 737 | static int saa7164_encoder_stop_port(struct saa7164_port *port) |
| 738 | { |
| 739 | struct saa7164_dev *dev = port->dev; |
| 740 | int ret; |
| 741 | |
| 742 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 743 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { |
| 744 | printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n", |
| 745 | __func__, ret); |
| 746 | ret = -EIO; |
| 747 | } else { |
| 748 | dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__); |
| 749 | ret = 0; |
| 750 | } |
| 751 | |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | static int saa7164_encoder_acquire_port(struct saa7164_port *port) |
| 756 | { |
| 757 | struct saa7164_dev *dev = port->dev; |
| 758 | int ret; |
| 759 | |
| 760 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); |
| 761 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { |
| 762 | printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n", |
| 763 | __func__, ret); |
| 764 | ret = -EIO; |
| 765 | } else { |
| 766 | dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__); |
| 767 | ret = 0; |
| 768 | } |
| 769 | |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | static int saa7164_encoder_pause_port(struct saa7164_port *port) |
| 774 | { |
| 775 | struct saa7164_dev *dev = port->dev; |
| 776 | int ret; |
| 777 | |
| 778 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); |
| 779 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { |
| 780 | printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n", |
| 781 | __func__, ret); |
| 782 | ret = -EIO; |
| 783 | } else { |
| 784 | dprintk(DBGLVL_ENC, "%s() Paused\n", __func__); |
| 785 | ret = 0; |
| 786 | } |
| 787 | |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | /* Firmware is very windows centric, meaning you have to transition |
| 792 | * the part through AVStream / KS Windows stages, forwards or backwards. |
| 793 | * States are: stopped, acquired (h/w), paused, started. |
| 794 | * We have to leave here will all of the soft buffers on the free list, |
| 795 | * else the cfg_post() func won't have soft buffers to correctly configure. |
| 796 | */ |
| 797 | static int saa7164_encoder_stop_streaming(struct saa7164_port *port) |
| 798 | { |
| 799 | struct saa7164_dev *dev = port->dev; |
| 800 | struct saa7164_buffer *buf; |
| 801 | struct saa7164_user_buffer *ubuf; |
| 802 | struct list_head *c, *n; |
| 803 | int ret; |
| 804 | |
| 805 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); |
| 806 | |
| 807 | ret = saa7164_encoder_pause_port(port); |
| 808 | ret = saa7164_encoder_acquire_port(port); |
| 809 | ret = saa7164_encoder_stop_port(port); |
| 810 | |
| 811 | dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__, |
| 812 | port->nr); |
| 813 | |
| 814 | mutex_lock(&port->dmaqueue_lock); |
| 815 | |
| 816 | /* Reset the hard and soft buffer state */ |
| 817 | list_for_each_safe(c, n, &port->dmaqueue.list) { |
| 818 | buf = list_entry(c, struct saa7164_buffer, list); |
| 819 | buf->flags = SAA7164_BUFFER_FREE; |
| 820 | buf->pos = 0; |
| 821 | } |
| 822 | |
| 823 | list_for_each_safe(c, n, &port->list_buf_used.list) { |
| 824 | ubuf = list_entry(c, struct saa7164_user_buffer, list); |
| 825 | ubuf->pos = 0; |
| 826 | list_move_tail(&ubuf->list, &port->list_buf_free.list); |
| 827 | } |
| 828 | |
| 829 | mutex_unlock(&port->dmaqueue_lock); |
| 830 | dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr); |
| 831 | |
| 832 | return ret; |
| 833 | } |
| 834 | |
| 835 | static int saa7164_encoder_start_streaming(struct saa7164_port *port) |
| 836 | { |
| 837 | struct saa7164_dev *dev = port->dev; |
| 838 | int result, ret = 0; |
| 839 | |
| 840 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); |
| 841 | |
| 842 | /* Configure the encoder with any cache values */ |
| 843 | saa7164_api_set_encoder(port); |
| 844 | |
| 845 | saa7164_buffer_cfg_port(port); |
| 846 | |
| 847 | /* Acquire the hardware */ |
| 848 | result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); |
| 849 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 850 | printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n", |
| 851 | __func__, result); |
| 852 | |
| 853 | /* Stop the hardware, regardless */ |
| 854 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 855 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 856 | printk(KERN_ERR "%s() acquire/forced stop transition " |
| 857 | "failed, res = 0x%x\n", __func__, result); |
| 858 | } |
| 859 | ret = -EIO; |
| 860 | goto out; |
| 861 | } else |
| 862 | dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__); |
| 863 | |
| 864 | /* Pause the hardware */ |
| 865 | result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); |
| 866 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 867 | printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n", |
| 868 | __func__, result); |
| 869 | |
| 870 | /* Stop the hardware, regardless */ |
| 871 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 872 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 873 | printk(KERN_ERR "%s() pause/forced stop transition " |
| 874 | "failed, res = 0x%x\n", __func__, result); |
| 875 | } |
| 876 | |
| 877 | ret = -EIO; |
| 878 | goto out; |
| 879 | } else |
| 880 | dprintk(DBGLVL_ENC, "%s() Paused\n", __func__); |
| 881 | |
| 882 | /* Start the hardware */ |
| 883 | result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN); |
| 884 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 885 | printk(KERN_ERR "%s() run transition failed, result = 0x%x\n", |
| 886 | __func__, result); |
| 887 | |
| 888 | /* Stop the hardware, regardless */ |
| 889 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 890 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 891 | printk(KERN_ERR "%s() run/forced stop transition " |
| 892 | "failed, res = 0x%x\n", __func__, result); |
| 893 | } |
| 894 | |
| 895 | ret = -EIO; |
| 896 | } else |
| 897 | dprintk(DBGLVL_ENC, "%s() Running\n", __func__); |
| 898 | |
| 899 | out: |
| 900 | return ret; |
| 901 | } |
| 902 | |
| 903 | static int fops_open(struct file *file) |
| 904 | { |
| 905 | struct saa7164_dev *h, *dev = NULL; |
| 906 | struct saa7164_port *port = NULL; |
| 907 | struct saa7164_port *portc = NULL; |
| 908 | struct saa7164_port *portd = NULL; |
| 909 | struct saa7164_fh *fh; |
| 910 | struct list_head *list; |
| 911 | int minor = video_devdata(file)->minor; |
| 912 | |
| 913 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 914 | |
| 915 | /* TODO: Really, the BKL? - remove this */ |
| 916 | lock_kernel(); |
| 917 | list_for_each(list, &saa7164_devlist) { |
| 918 | h = list_entry(list, struct saa7164_dev, devlist); |
| 919 | |
| 920 | portc = &h->ports[ SAA7164_PORT_ENC1 ]; |
| 921 | portd = &h->ports[ SAA7164_PORT_ENC2 ]; |
| 922 | |
| 923 | if (portc->v4l_device && |
| 924 | portc->v4l_device->minor == minor) { |
| 925 | dev = h; |
| 926 | port = portc; |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | if (portd->v4l_device && |
| 931 | portd->v4l_device->minor == minor) { |
| 932 | dev = h; |
| 933 | port = portd; |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | } |
| 938 | |
| 939 | if (port == NULL) { |
| 940 | unlock_kernel(); |
| 941 | return -ENODEV; |
| 942 | } |
| 943 | |
| 944 | /* allocate + initialize per filehandle data */ |
| 945 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); |
| 946 | if (NULL == fh) { |
| 947 | unlock_kernel(); |
| 948 | return -ENOMEM; |
| 949 | } |
| 950 | |
| 951 | file->private_data = fh; |
| 952 | fh->port = port; |
| 953 | |
| 954 | unlock_kernel(); |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static int fops_release(struct file *file) |
| 960 | { |
| 961 | struct saa7164_fh *fh = file->private_data; |
| 962 | struct saa7164_port *port = fh->port; |
| 963 | struct saa7164_dev *dev = port->dev; |
| 964 | |
| 965 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 966 | |
| 967 | /* Shut device down on last close */ |
| 968 | if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) { |
| 969 | if (atomic_dec_return(&port->v4l_reader_count) == 0) { |
| 970 | /* stop mpeg capture then cancel buffers */ |
| 971 | saa7164_encoder_stop_streaming(port); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | file->private_data = NULL; |
| 976 | kfree(fh); |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | struct saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port) |
| 982 | { |
| 983 | struct saa7164_user_buffer *buf = 0; |
| 984 | struct saa7164_dev *dev = port->dev; |
| 985 | |
| 986 | mutex_lock(&port->dmaqueue_lock); |
| 987 | if (!list_empty(&port->list_buf_used.list)) { |
| 988 | buf = list_first_entry(&port->list_buf_used.list, |
| 989 | struct saa7164_user_buffer, list); |
| 990 | } |
| 991 | mutex_unlock(&port->dmaqueue_lock); |
| 992 | |
| 993 | dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, buf); |
| 994 | |
| 995 | return buf; |
| 996 | } |
| 997 | |
| 998 | static ssize_t fops_read(struct file *file, char __user *buffer, |
| 999 | size_t count, loff_t *pos) |
| 1000 | { |
| 1001 | struct saa7164_fh *fh = file->private_data; |
| 1002 | struct saa7164_port *port = fh->port; |
| 1003 | struct saa7164_user_buffer *ubuf = NULL; |
| 1004 | struct saa7164_dev *dev = port->dev; |
| 1005 | unsigned int ret = 0; |
| 1006 | int rem, cnt; |
| 1007 | u8 *p; |
| 1008 | |
| 1009 | if (*pos) |
| 1010 | return -ESPIPE; |
| 1011 | |
| 1012 | if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) { |
| 1013 | if (atomic_inc_return(&port->v4l_reader_count) == 1) { |
| 1014 | |
| 1015 | if (saa7164_encoder_initialize(port) < 0) |
| 1016 | return -EINVAL; |
| 1017 | |
| 1018 | saa7164_encoder_start_streaming(port); |
| 1019 | msleep(200); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /* blocking wait for buffer */ |
| 1024 | if ((file->f_flags & O_NONBLOCK) == 0) { |
| 1025 | if (wait_event_interruptible(port->wait_read, |
| 1026 | saa7164_enc_next_buf(port))) { |
| 1027 | return -ERESTARTSYS; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* Pull the first buffer from the used list */ |
| 1032 | ubuf = saa7164_enc_next_buf(port); |
| 1033 | |
| 1034 | while ((count > 0) && ubuf) { |
| 1035 | |
| 1036 | /* set remaining bytes to copy */ |
| 1037 | rem = ubuf->actual_size - ubuf->pos; |
| 1038 | cnt = rem > count ? count : rem; |
| 1039 | |
| 1040 | p = ubuf->data + ubuf->pos; |
| 1041 | |
| 1042 | dprintk(DBGLVL_ENC, |
| 1043 | "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n", |
| 1044 | __func__, (int)count, cnt, rem, ubuf, ubuf->pos); |
| 1045 | |
| 1046 | if (copy_to_user(buffer, p, cnt)) { |
| 1047 | printk(KERN_ERR "%s() copy_to_user failed\n", __func__); |
| 1048 | if (!ret) |
| 1049 | ret = -EFAULT; |
| 1050 | goto err; |
| 1051 | } |
| 1052 | |
| 1053 | ubuf->pos += cnt; |
| 1054 | count -= cnt; |
| 1055 | buffer += cnt; |
| 1056 | ret += cnt; |
| 1057 | |
| 1058 | if (ubuf->pos == ubuf->actual_size) { |
| 1059 | |
| 1060 | /* finished with current buffer, take next buffer */ |
| 1061 | |
| 1062 | /* Requeue the buffer on the free list */ |
| 1063 | ubuf->pos = 0; |
| 1064 | |
| 1065 | mutex_lock(&port->dmaqueue_lock); |
| 1066 | list_move_tail(&ubuf->list, &port->list_buf_free.list); |
| 1067 | mutex_unlock(&port->dmaqueue_lock); |
| 1068 | |
| 1069 | /* Dequeue next */ |
| 1070 | if ((file->f_flags & O_NONBLOCK) == 0) { |
| 1071 | if (wait_event_interruptible(port->wait_read, |
| 1072 | saa7164_enc_next_buf(port))) { |
| 1073 | break; |
| 1074 | } |
| 1075 | } |
| 1076 | ubuf = saa7164_enc_next_buf(port); |
| 1077 | } |
| 1078 | } |
| 1079 | err: |
| 1080 | if (!ret && !ubuf) |
| 1081 | ret = -EAGAIN; |
| 1082 | |
| 1083 | return ret; |
| 1084 | } |
| 1085 | |
| 1086 | static unsigned int fops_poll(struct file *file, poll_table *wait) |
| 1087 | { |
| 1088 | struct saa7164_fh *fh = (struct saa7164_fh *)file->private_data; |
| 1089 | struct saa7164_port *port = fh->port; |
| 1090 | struct saa7164_user_buffer *ubuf; |
| 1091 | unsigned int mask = 0; |
| 1092 | |
| 1093 | if (!video_is_registered(port->v4l_device)) { |
| 1094 | return -EIO; |
| 1095 | } |
| 1096 | |
| 1097 | if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) { |
| 1098 | if (atomic_inc_return(&port->v4l_reader_count) == 1) { |
| 1099 | if (saa7164_encoder_initialize(port) < 0) |
| 1100 | return -EINVAL; |
| 1101 | saa7164_encoder_start_streaming(port); |
| 1102 | msleep(200); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* blocking wait for buffer */ |
| 1107 | if ((file->f_flags & O_NONBLOCK) == 0) { |
| 1108 | if (wait_event_interruptible(port->wait_read, |
| 1109 | saa7164_enc_next_buf(port))) { |
| 1110 | return -ERESTARTSYS; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | /* Pull the first buffer from the used list */ |
| 1115 | ubuf = list_first_entry(&port->list_buf_used.list, |
| 1116 | struct saa7164_user_buffer, list); |
| 1117 | |
| 1118 | if (ubuf) |
| 1119 | mask |= POLLIN | POLLRDNORM; |
| 1120 | |
| 1121 | return mask; |
| 1122 | } |
| 1123 | |
| 1124 | static const struct v4l2_file_operations mpeg_fops = { |
| 1125 | .owner = THIS_MODULE, |
| 1126 | .open = fops_open, |
| 1127 | .release = fops_release, |
| 1128 | .read = fops_read, |
| 1129 | .poll = fops_poll, |
| 1130 | .unlocked_ioctl = video_ioctl2, |
| 1131 | }; |
| 1132 | |
| 1133 | int saa7164_g_chip_ident(struct file *file, void *fh, |
| 1134 | struct v4l2_dbg_chip_ident *chip) |
| 1135 | { |
| 1136 | struct saa7164_port *port = ((struct saa7164_fh *)fh)->port; |
| 1137 | struct saa7164_dev *dev = port->dev; |
| 1138 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | int saa7164_g_register(struct file *file, void *fh, |
| 1144 | struct v4l2_dbg_register *reg) |
| 1145 | { |
| 1146 | struct saa7164_port *port = ((struct saa7164_fh *)fh)->port; |
| 1147 | struct saa7164_dev *dev = port->dev; |
| 1148 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1149 | |
| 1150 | if (!capable(CAP_SYS_ADMIN)) |
| 1151 | return -EPERM; |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | int saa7164_s_register(struct file *file, void *fh, |
| 1157 | struct v4l2_dbg_register *reg) |
| 1158 | { |
| 1159 | struct saa7164_port *port = ((struct saa7164_fh *)fh)->port; |
| 1160 | struct saa7164_dev *dev = port->dev; |
| 1161 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1162 | |
| 1163 | if (!capable(CAP_SYS_ADMIN)) |
| 1164 | return -EPERM; |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | static const struct v4l2_ioctl_ops mpeg_ioctl_ops = { |
| 1170 | .vidioc_s_std = vidioc_s_std, |
| 1171 | .vidioc_enum_input = vidioc_enum_input, |
| 1172 | .vidioc_g_input = vidioc_g_input, |
| 1173 | .vidioc_s_input = vidioc_s_input, |
| 1174 | .vidioc_g_tuner = vidioc_g_tuner, |
| 1175 | .vidioc_s_tuner = vidioc_s_tuner, |
| 1176 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1177 | .vidioc_s_frequency = vidioc_s_frequency, |
| 1178 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1179 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1180 | .vidioc_querycap = vidioc_querycap, |
| 1181 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1182 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1183 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1184 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 1185 | .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls, |
| 1186 | .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls, |
| 1187 | .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls, |
| 1188 | .vidioc_log_status = vidioc_log_status, |
| 1189 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1190 | .vidioc_g_chip_ident = saa7164_g_chip_ident, |
| 1191 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1192 | .vidioc_g_register = saa7164_g_register, |
| 1193 | .vidioc_s_register = saa7164_s_register, |
| 1194 | #endif |
| 1195 | }; |
| 1196 | |
| 1197 | static struct video_device saa7164_mpeg_template = { |
| 1198 | .name = "saa7164", |
| 1199 | .fops = &mpeg_fops, |
| 1200 | .ioctl_ops = &mpeg_ioctl_ops, |
| 1201 | .minor = -1, |
| 1202 | .tvnorms = SAA7164_NORMS, |
| 1203 | .current_norm = V4L2_STD_NTSC_M, |
| 1204 | }; |
| 1205 | |
| 1206 | static struct video_device *saa7164_encoder_alloc( |
| 1207 | struct saa7164_port *port, |
| 1208 | struct pci_dev *pci, |
| 1209 | struct video_device *template, |
| 1210 | char *type) |
| 1211 | { |
| 1212 | struct video_device *vfd; |
| 1213 | struct saa7164_dev *dev = port->dev; |
| 1214 | |
| 1215 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1216 | |
| 1217 | vfd = video_device_alloc(); |
| 1218 | if (NULL == vfd) |
| 1219 | return NULL; |
| 1220 | |
| 1221 | *vfd = *template; |
| 1222 | snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, |
| 1223 | type, saa7164_boards[dev->board].name); |
| 1224 | |
| 1225 | vfd->parent = &pci->dev; |
| 1226 | vfd->release = video_device_release; |
| 1227 | return vfd; |
| 1228 | } |
| 1229 | |
| 1230 | int saa7164_encoder_register(struct saa7164_port *port) |
| 1231 | { |
| 1232 | struct saa7164_dev *dev = port->dev; |
| 1233 | struct saa7164_buffer *buf; |
| 1234 | struct saa7164_user_buffer *ubuf; |
| 1235 | int result = -ENODEV, i; |
| 1236 | int len = 0; |
| 1237 | |
| 1238 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1239 | |
| 1240 | if (port->type != SAA7164_MPEG_ENCODER) |
| 1241 | BUG(); |
| 1242 | |
| 1243 | /* Sanity check that the PCI configuration space is active */ |
| 1244 | if (port->hwcfg.BARLocation == 0) { |
| 1245 | printk(KERN_ERR "%s() failed " |
| 1246 | "(errno = %d), NO PCI configuration\n", |
| 1247 | __func__, result); |
| 1248 | result = -ENOMEM; |
| 1249 | goto failed; |
| 1250 | } |
| 1251 | |
| 1252 | /* Init and establish defaults */ |
| 1253 | /* TODO: Check the umber of lines for PS */ |
| 1254 | port->hw_streamingparams.bitspersample = 8; |
| 1255 | port->hw_streamingparams.samplesperline = 188; |
| 1256 | port->hw_streamingparams.numberoflines = |
| 1257 | (SAA7164_TS_NUMBER_OF_LINES * 188) / 188; |
| 1258 | |
| 1259 | port->hw_streamingparams.pitch = 188; |
| 1260 | port->hw_streamingparams.linethreshold = 0; |
| 1261 | port->hw_streamingparams.pagetablelistvirt = 0; |
| 1262 | port->hw_streamingparams.pagetablelistphys = 0; |
| 1263 | port->hw_streamingparams.numpagetables = 2 + |
| 1264 | ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE); |
| 1265 | |
| 1266 | port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount; |
| 1267 | |
| 1268 | /* Allocate the PCI resources, buffers (hard) */ |
| 1269 | for (i = 0; i < port->hwcfg.buffercount; i++) { |
| 1270 | buf = saa7164_buffer_alloc(port, |
| 1271 | port->hw_streamingparams.numberoflines * |
| 1272 | port->hw_streamingparams.pitch); |
| 1273 | |
| 1274 | if (!buf) { |
| 1275 | printk(KERN_ERR "%s() failed " |
| 1276 | "(errno = %d), unable to allocate buffer\n", |
| 1277 | __func__, result); |
| 1278 | result = -ENOMEM; |
| 1279 | goto failed; |
| 1280 | } else { |
| 1281 | |
| 1282 | mutex_lock(&port->dmaqueue_lock); |
| 1283 | list_add_tail(&buf->list, &port->dmaqueue.list); |
| 1284 | mutex_unlock(&port->dmaqueue_lock); |
| 1285 | |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | /* Allocate some kenrel kernel buffers for copying |
| 1290 | * to userpsace. |
| 1291 | */ |
| 1292 | len = port->hw_streamingparams.numberoflines * |
| 1293 | port->hw_streamingparams.pitch; |
| 1294 | |
| 1295 | for (i = 0; i < SAA7164_MAX_ENCODER_BUFFERS; i++) { |
| 1296 | |
| 1297 | ubuf = saa7164_buffer_alloc_user(dev, len); |
| 1298 | if (ubuf) { |
| 1299 | mutex_lock(&port->dmaqueue_lock); |
| 1300 | list_add_tail(&ubuf->list, &port->list_buf_free.list); |
| 1301 | mutex_unlock(&port->dmaqueue_lock); |
| 1302 | } |
| 1303 | |
| 1304 | } |
| 1305 | |
| 1306 | /* Establish encoder defaults here */ |
| 1307 | /* Set default TV standard */ |
| 1308 | port->encodernorm = saa7164_tvnorms[0]; |
| 1309 | port->width = 720; |
| 1310 | port->mux_input = 1; /* Composite */ |
| 1311 | port->encoder_profile = EU_PROFILE_PS_DVD; |
| 1312 | port->video_format = EU_VIDEO_FORMAT_MPEG_2; |
| 1313 | port->audio_format = 0; |
| 1314 | port->video_resolution = 0; |
| 1315 | port->ctl_brightness = 127; |
| 1316 | port->ctl_contrast = 66; |
| 1317 | port->ctl_hue = 128; |
| 1318 | port->ctl_saturation = 62; |
| 1319 | port->ctl_sharpness = 8; |
| 1320 | port->encoder_params.bitrate = ENCODER_DEF_BITRATE; |
Steven Toth | 2600d71 | 2010-07-31 14:51:30 -0300 | [diff] [blame] | 1321 | port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1322 | port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS; |
| 1323 | port->encoder_params.ctl_mute = 0; |
| 1324 | port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3; |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame^] | 1325 | port->encoder_params.refdist = 1; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1326 | |
| 1327 | if (port->encodernorm.id & V4L2_STD_525_60) |
| 1328 | port->height = 480; |
| 1329 | else |
| 1330 | port->height = 576; |
| 1331 | |
| 1332 | /* Allocate and register the video device node */ |
| 1333 | port->v4l_device = saa7164_encoder_alloc(port, |
| 1334 | dev->pci, &saa7164_mpeg_template, "mpeg"); |
| 1335 | |
| 1336 | if (port->v4l_device == NULL) { |
| 1337 | printk(KERN_INFO "%s: can't allocate mpeg device\n", |
| 1338 | dev->name); |
| 1339 | result = -ENOMEM; |
| 1340 | goto failed; |
| 1341 | } |
| 1342 | |
| 1343 | result = video_register_device(port->v4l_device, |
| 1344 | VFL_TYPE_GRABBER, -1); |
| 1345 | if (result < 0) { |
| 1346 | printk(KERN_INFO "%s: can't register mpeg device\n", |
| 1347 | dev->name); |
| 1348 | /* TODO: We're going to leak here if we don't dealloc |
| 1349 | The buffers above. The unreg function can't deal wit it. |
| 1350 | */ |
| 1351 | goto failed; |
| 1352 | } |
| 1353 | |
| 1354 | printk(KERN_INFO "%s: registered device video%d [mpeg]\n", |
| 1355 | dev->name, port->v4l_device->num); |
| 1356 | |
| 1357 | /* Configure the hardware defaults */ |
| 1358 | saa7164_api_set_videomux(port); |
| 1359 | saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL); |
| 1360 | saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL); |
| 1361 | saa7164_api_set_usercontrol(port, PU_HUE_CONTROL); |
| 1362 | saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL); |
| 1363 | saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL); |
| 1364 | saa7164_api_audio_mute(port, 0); |
| 1365 | saa7164_api_set_audio_volume(port, 20); |
| 1366 | saa7164_api_set_aspect_ratio(port); |
| 1367 | |
| 1368 | /* Disable audio standard detection, it's buggy */ |
| 1369 | saa7164_api_set_audio_detection(port, 0); |
| 1370 | |
| 1371 | saa7164_api_set_encoder(port); |
| 1372 | saa7164_api_get_encoder(port); |
| 1373 | |
| 1374 | result = 0; |
| 1375 | failed: |
| 1376 | return result; |
| 1377 | } |
| 1378 | |
| 1379 | void saa7164_encoder_unregister(struct saa7164_port *port) |
| 1380 | { |
| 1381 | struct saa7164_dev *dev = port->dev; |
| 1382 | struct saa7164_buffer *buf; |
| 1383 | struct saa7164_user_buffer *ubuf; |
| 1384 | struct list_head *c, *n, *p, *q, *l, *v; |
| 1385 | |
| 1386 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); |
| 1387 | |
| 1388 | if (port->type != SAA7164_MPEG_ENCODER) |
| 1389 | BUG(); |
| 1390 | |
| 1391 | if (port->v4l_device) { |
| 1392 | if (port->v4l_device->minor != -1) |
| 1393 | video_unregister_device(port->v4l_device); |
| 1394 | else |
| 1395 | video_device_release(port->v4l_device); |
| 1396 | |
| 1397 | port->v4l_device = NULL; |
| 1398 | } |
| 1399 | |
| 1400 | /* Remove any allocated buffers */ |
| 1401 | mutex_lock(&port->dmaqueue_lock); |
| 1402 | |
| 1403 | dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr); |
| 1404 | list_for_each_safe(c, n, &port->dmaqueue.list) { |
| 1405 | buf = list_entry(c, struct saa7164_buffer, list); |
| 1406 | list_del(c); |
| 1407 | saa7164_buffer_dealloc(buf); |
| 1408 | } |
| 1409 | |
| 1410 | dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr); |
| 1411 | list_for_each_safe(p, q, &port->list_buf_used.list) { |
| 1412 | ubuf = list_entry(p, struct saa7164_user_buffer, list); |
| 1413 | list_del(p); |
| 1414 | saa7164_buffer_dealloc_user(ubuf); |
| 1415 | } |
| 1416 | |
| 1417 | dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr); |
| 1418 | list_for_each_safe(l, v, &port->list_buf_free.list) { |
| 1419 | ubuf = list_entry(l, struct saa7164_user_buffer, list); |
| 1420 | list_del(l); |
| 1421 | saa7164_buffer_dealloc_user(ubuf); |
| 1422 | } |
| 1423 | |
| 1424 | mutex_unlock(&port->dmaqueue_lock); |
| 1425 | dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr); |
| 1426 | } |
| 1427 | |