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