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