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