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", |
Gavin Hurlbut | f89076c | 2010-09-27 23:50:43 -0300 | [diff] [blame] | 242 | "composite 2", "svideo 2", "aux 2" }; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 243 | |
| 244 | if (i->index >= 7) |
| 245 | return -EINVAL; |
| 246 | |
Mauro Carvalho Chehab | c7e242ba | 2010-10-11 17:39:06 -0300 | [diff] [blame] | 247 | strcpy(i->name, inputs[i->index]); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 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) |
Mauro Carvalho Chehab | c7e242ba | 2010-10-11 17:39:06 -0300 | [diff] [blame] | 364 | tsport = &dev->ports[SAA7164_PORT_TS1]; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 365 | else |
| 366 | if (port->nr == SAA7164_PORT_ENC2) |
Mauro Carvalho Chehab | c7e242ba | 2010-10-11 17:39:06 -0300 | [diff] [blame] | 367 | tsport = &dev->ports[SAA7164_PORT_TS2]; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 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 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 711 | 0; |
| 712 | |
| 713 | cap->capabilities |= V4L2_CAP_TUNER; |
| 714 | cap->version = 0; |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
| 720 | struct v4l2_fmtdesc *f) |
| 721 | { |
| 722 | if (f->index != 0) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | strlcpy(f->description, "MPEG", sizeof(f->description)); |
| 726 | f->pixelformat = V4L2_PIX_FMT_MPEG; |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
| 732 | struct v4l2_format *f) |
| 733 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 734 | struct saa7164_encoder_fh *fh = file->private_data; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 735 | struct saa7164_port *port = fh->port; |
| 736 | struct saa7164_dev *dev = port->dev; |
| 737 | |
| 738 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 739 | f->fmt.pix.bytesperline = 0; |
| 740 | f->fmt.pix.sizeimage = |
| 741 | port->ts_packet_size * port->ts_packet_count; |
| 742 | f->fmt.pix.colorspace = 0; |
| 743 | f->fmt.pix.width = port->width; |
| 744 | f->fmt.pix.height = port->height; |
| 745 | |
| 746 | dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n", |
| 747 | port->width, port->height); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 753 | struct v4l2_format *f) |
| 754 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 755 | struct saa7164_encoder_fh *fh = file->private_data; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 756 | struct saa7164_port *port = fh->port; |
| 757 | struct saa7164_dev *dev = port->dev; |
| 758 | |
| 759 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 760 | f->fmt.pix.bytesperline = 0; |
| 761 | f->fmt.pix.sizeimage = |
| 762 | port->ts_packet_size * port->ts_packet_count; |
| 763 | f->fmt.pix.colorspace = 0; |
| 764 | dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n", |
| 765 | port->width, port->height); |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 770 | struct v4l2_format *f) |
| 771 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 772 | struct saa7164_encoder_fh *fh = file->private_data; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 773 | struct saa7164_port *port = fh->port; |
| 774 | struct saa7164_dev *dev = port->dev; |
| 775 | |
| 776 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 777 | f->fmt.pix.bytesperline = 0; |
| 778 | f->fmt.pix.sizeimage = |
| 779 | port->ts_packet_size * port->ts_packet_count; |
| 780 | f->fmt.pix.colorspace = 0; |
| 781 | |
| 782 | dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n", |
| 783 | f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field); |
| 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | static int vidioc_log_status(struct file *file, void *priv) |
| 789 | { |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static int fill_queryctrl(struct saa7164_encoder_params *params, |
| 794 | struct v4l2_queryctrl *c) |
| 795 | { |
| 796 | switch (c->id) { |
| 797 | case V4L2_CID_BRIGHTNESS: |
| 798 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127); |
| 799 | case V4L2_CID_CONTRAST: |
| 800 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66); |
| 801 | case V4L2_CID_SATURATION: |
| 802 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62); |
| 803 | case V4L2_CID_HUE: |
| 804 | return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128); |
| 805 | case V4L2_CID_SHARPNESS: |
| 806 | return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8); |
| 807 | case V4L2_CID_MPEG_AUDIO_MUTE: |
| 808 | return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0); |
| 809 | case V4L2_CID_AUDIO_VOLUME: |
| 810 | return v4l2_ctrl_query_fill(c, -83, 24, 1, 20); |
| 811 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
| 812 | return v4l2_ctrl_query_fill(c, |
| 813 | ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE, |
| 814 | 100000, ENCODER_DEF_BITRATE); |
| 815 | case V4L2_CID_MPEG_STREAM_TYPE: |
| 816 | return v4l2_ctrl_query_fill(c, |
| 817 | V4L2_MPEG_STREAM_TYPE_MPEG2_PS, |
Steven Toth | f91d095 | 2010-07-31 15:03:31 -0300 | [diff] [blame] | 818 | V4L2_MPEG_STREAM_TYPE_MPEG2_TS, |
| 819 | 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 820 | case V4L2_CID_MPEG_VIDEO_ASPECT: |
| 821 | return v4l2_ctrl_query_fill(c, |
| 822 | V4L2_MPEG_VIDEO_ASPECT_1x1, |
| 823 | V4L2_MPEG_VIDEO_ASPECT_221x100, |
| 824 | 1, V4L2_MPEG_VIDEO_ASPECT_4x3); |
| 825 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: |
| 826 | return v4l2_ctrl_query_fill(c, 1, 255, 1, 15); |
Steven Toth | 2600d71 | 2010-07-31 14:51:30 -0300 | [diff] [blame] | 827 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: |
| 828 | return v4l2_ctrl_query_fill(c, |
| 829 | V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, |
| 830 | 1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame] | 831 | case V4L2_CID_MPEG_VIDEO_B_FRAMES: |
| 832 | return v4l2_ctrl_query_fill(c, |
| 833 | 1, 3, 1, 1); |
Steven Toth | 968b11b | 2010-07-31 14:59:38 -0300 | [diff] [blame] | 834 | case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: |
| 835 | return v4l2_ctrl_query_fill(c, |
| 836 | ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE, |
| 837 | 100000, ENCODER_DEF_BITRATE); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 838 | default: |
| 839 | return -EINVAL; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 844 | struct v4l2_queryctrl *c) |
| 845 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 846 | struct saa7164_encoder_fh *fh = priv; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 847 | struct saa7164_port *port = fh->port; |
| 848 | int i, next; |
| 849 | u32 id = c->id; |
| 850 | |
| 851 | memset(c, 0, sizeof(*c)); |
| 852 | |
| 853 | next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL); |
| 854 | c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL; |
| 855 | |
| 856 | for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) { |
| 857 | if (next) { |
| 858 | if (c->id < saa7164_v4l2_ctrls[i]) |
| 859 | c->id = saa7164_v4l2_ctrls[i]; |
| 860 | else |
| 861 | continue; |
| 862 | } |
| 863 | |
| 864 | if (c->id == saa7164_v4l2_ctrls[i]) |
| 865 | return fill_queryctrl(&port->encoder_params, c); |
| 866 | |
| 867 | if (c->id < saa7164_v4l2_ctrls[i]) |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | return -EINVAL; |
| 872 | } |
| 873 | |
| 874 | static int saa7164_encoder_stop_port(struct saa7164_port *port) |
| 875 | { |
| 876 | struct saa7164_dev *dev = port->dev; |
| 877 | int ret; |
| 878 | |
| 879 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 880 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { |
| 881 | printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n", |
| 882 | __func__, ret); |
| 883 | ret = -EIO; |
| 884 | } else { |
| 885 | dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__); |
| 886 | ret = 0; |
| 887 | } |
| 888 | |
| 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | static int saa7164_encoder_acquire_port(struct saa7164_port *port) |
| 893 | { |
| 894 | struct saa7164_dev *dev = port->dev; |
| 895 | int ret; |
| 896 | |
| 897 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); |
| 898 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { |
| 899 | printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n", |
| 900 | __func__, ret); |
| 901 | ret = -EIO; |
| 902 | } else { |
| 903 | dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__); |
| 904 | ret = 0; |
| 905 | } |
| 906 | |
| 907 | return ret; |
| 908 | } |
| 909 | |
| 910 | static int saa7164_encoder_pause_port(struct saa7164_port *port) |
| 911 | { |
| 912 | struct saa7164_dev *dev = port->dev; |
| 913 | int ret; |
| 914 | |
| 915 | ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); |
| 916 | if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { |
| 917 | printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n", |
| 918 | __func__, ret); |
| 919 | ret = -EIO; |
| 920 | } else { |
| 921 | dprintk(DBGLVL_ENC, "%s() Paused\n", __func__); |
| 922 | ret = 0; |
| 923 | } |
| 924 | |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | /* Firmware is very windows centric, meaning you have to transition |
| 929 | * the part through AVStream / KS Windows stages, forwards or backwards. |
| 930 | * States are: stopped, acquired (h/w), paused, started. |
| 931 | * We have to leave here will all of the soft buffers on the free list, |
| 932 | * else the cfg_post() func won't have soft buffers to correctly configure. |
| 933 | */ |
| 934 | static int saa7164_encoder_stop_streaming(struct saa7164_port *port) |
| 935 | { |
| 936 | struct saa7164_dev *dev = port->dev; |
| 937 | struct saa7164_buffer *buf; |
| 938 | struct saa7164_user_buffer *ubuf; |
| 939 | struct list_head *c, *n; |
| 940 | int ret; |
| 941 | |
| 942 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); |
| 943 | |
| 944 | ret = saa7164_encoder_pause_port(port); |
| 945 | ret = saa7164_encoder_acquire_port(port); |
| 946 | ret = saa7164_encoder_stop_port(port); |
| 947 | |
| 948 | dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__, |
| 949 | port->nr); |
| 950 | |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 951 | /* Reset the state of any allocated buffer resources */ |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 952 | mutex_lock(&port->dmaqueue_lock); |
| 953 | |
| 954 | /* Reset the hard and soft buffer state */ |
| 955 | list_for_each_safe(c, n, &port->dmaqueue.list) { |
| 956 | buf = list_entry(c, struct saa7164_buffer, list); |
| 957 | buf->flags = SAA7164_BUFFER_FREE; |
| 958 | buf->pos = 0; |
| 959 | } |
| 960 | |
| 961 | list_for_each_safe(c, n, &port->list_buf_used.list) { |
| 962 | ubuf = list_entry(c, struct saa7164_user_buffer, list); |
| 963 | ubuf->pos = 0; |
| 964 | list_move_tail(&ubuf->list, &port->list_buf_free.list); |
| 965 | } |
| 966 | |
| 967 | mutex_unlock(&port->dmaqueue_lock); |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 968 | |
| 969 | /* Free any allocated resources */ |
| 970 | saa7164_encoder_buffers_dealloc(port); |
| 971 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 972 | dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr); |
| 973 | |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | static int saa7164_encoder_start_streaming(struct saa7164_port *port) |
| 978 | { |
| 979 | struct saa7164_dev *dev = port->dev; |
| 980 | int result, ret = 0; |
| 981 | |
| 982 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); |
| 983 | |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 984 | port->done_first_interrupt = 0; |
| 985 | |
| 986 | /* allocate all of the PCIe DMA buffer resources on the fly, |
| 987 | * allowing switching between TS and PS payloads without |
| 988 | * requiring a complete driver reload. |
| 989 | */ |
| 990 | saa7164_encoder_buffers_alloc(port); |
| 991 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 992 | /* Configure the encoder with any cache values */ |
| 993 | saa7164_api_set_encoder(port); |
Steven Toth | 12d3203 | 2010-07-31 15:13:45 -0300 | [diff] [blame] | 994 | saa7164_api_get_encoder(port); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 995 | |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 996 | /* Place the empty buffers on the hardware */ |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 997 | saa7164_buffer_cfg_port(port); |
| 998 | |
| 999 | /* Acquire the hardware */ |
| 1000 | result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); |
| 1001 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 1002 | printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n", |
| 1003 | __func__, result); |
| 1004 | |
| 1005 | /* Stop the hardware, regardless */ |
| 1006 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 1007 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 1008 | printk(KERN_ERR "%s() acquire/forced stop transition " |
| 1009 | "failed, res = 0x%x\n", __func__, result); |
| 1010 | } |
| 1011 | ret = -EIO; |
| 1012 | goto out; |
| 1013 | } else |
| 1014 | dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__); |
| 1015 | |
| 1016 | /* Pause the hardware */ |
| 1017 | result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); |
| 1018 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 1019 | printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n", |
| 1020 | __func__, result); |
| 1021 | |
| 1022 | /* Stop the hardware, regardless */ |
| 1023 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 1024 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 1025 | printk(KERN_ERR "%s() pause/forced stop transition " |
| 1026 | "failed, res = 0x%x\n", __func__, result); |
| 1027 | } |
| 1028 | |
| 1029 | ret = -EIO; |
| 1030 | goto out; |
| 1031 | } else |
| 1032 | dprintk(DBGLVL_ENC, "%s() Paused\n", __func__); |
| 1033 | |
| 1034 | /* Start the hardware */ |
| 1035 | result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN); |
| 1036 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 1037 | printk(KERN_ERR "%s() run transition failed, result = 0x%x\n", |
| 1038 | __func__, result); |
| 1039 | |
| 1040 | /* Stop the hardware, regardless */ |
| 1041 | result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); |
| 1042 | if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { |
| 1043 | printk(KERN_ERR "%s() run/forced stop transition " |
| 1044 | "failed, res = 0x%x\n", __func__, result); |
| 1045 | } |
| 1046 | |
| 1047 | ret = -EIO; |
| 1048 | } else |
| 1049 | dprintk(DBGLVL_ENC, "%s() Running\n", __func__); |
| 1050 | |
| 1051 | out: |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | static int fops_open(struct file *file) |
| 1056 | { |
Steven Toth | 214ce3f | 2010-10-06 21:52:22 -0300 | [diff] [blame^] | 1057 | struct saa7164_dev *dev; |
| 1058 | struct saa7164_port *port; |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 1059 | struct saa7164_encoder_fh *fh; |
Steven Toth | 214ce3f | 2010-10-06 21:52:22 -0300 | [diff] [blame^] | 1060 | |
| 1061 | port = (struct saa7164_port *)video_get_drvdata(video_devdata(file)); |
| 1062 | if (!port) |
| 1063 | return -ENODEV; |
| 1064 | |
| 1065 | dev = port->dev; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1066 | |
| 1067 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1068 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1069 | /* allocate + initialize per filehandle data */ |
| 1070 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); |
Steven Toth | 214ce3f | 2010-10-06 21:52:22 -0300 | [diff] [blame^] | 1071 | if (NULL == fh) |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1072 | return -ENOMEM; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1073 | |
| 1074 | file->private_data = fh; |
| 1075 | fh->port = port; |
| 1076 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | static int fops_release(struct file *file) |
| 1081 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 1082 | struct saa7164_encoder_fh *fh = file->private_data; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1083 | struct saa7164_port *port = fh->port; |
| 1084 | struct saa7164_dev *dev = port->dev; |
| 1085 | |
| 1086 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1087 | |
| 1088 | /* Shut device down on last close */ |
| 1089 | if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) { |
| 1090 | if (atomic_dec_return(&port->v4l_reader_count) == 0) { |
| 1091 | /* stop mpeg capture then cancel buffers */ |
| 1092 | saa7164_encoder_stop_streaming(port); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | file->private_data = NULL; |
| 1097 | kfree(fh); |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | struct saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port) |
| 1103 | { |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 1104 | struct saa7164_user_buffer *ubuf = 0; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1105 | struct saa7164_dev *dev = port->dev; |
Steven Toth | 12d3203 | 2010-07-31 15:13:45 -0300 | [diff] [blame] | 1106 | u32 crc; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1107 | |
| 1108 | mutex_lock(&port->dmaqueue_lock); |
| 1109 | if (!list_empty(&port->list_buf_used.list)) { |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 1110 | ubuf = list_first_entry(&port->list_buf_used.list, |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1111 | struct saa7164_user_buffer, list); |
Steven Toth | 12d3203 | 2010-07-31 15:13:45 -0300 | [diff] [blame] | 1112 | |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 1113 | if (crc_checking) { |
| 1114 | crc = crc32(0, ubuf->data, ubuf->actual_size); |
| 1115 | if (crc != ubuf->crc) { |
| 1116 | printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n", __func__, |
| 1117 | ubuf, ubuf->crc, crc); |
| 1118 | } |
Steven Toth | 12d3203 | 2010-07-31 15:13:45 -0300 | [diff] [blame] | 1119 | } |
| 1120 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1121 | } |
| 1122 | mutex_unlock(&port->dmaqueue_lock); |
| 1123 | |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 1124 | dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1125 | |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 1126 | return ubuf; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | static ssize_t fops_read(struct file *file, char __user *buffer, |
| 1130 | size_t count, loff_t *pos) |
| 1131 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 1132 | struct saa7164_encoder_fh *fh = file->private_data; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1133 | struct saa7164_port *port = fh->port; |
| 1134 | struct saa7164_user_buffer *ubuf = NULL; |
| 1135 | struct saa7164_dev *dev = port->dev; |
Gavin Hurlbut | f20a07d | 2010-09-29 15:18:20 -0300 | [diff] [blame] | 1136 | int ret = 0; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1137 | int rem, cnt; |
| 1138 | u8 *p; |
| 1139 | |
Steven Toth | 58acca1 | 2010-07-31 15:10:52 -0300 | [diff] [blame] | 1140 | port->last_read_msecs_diff = port->last_read_msecs; |
| 1141 | port->last_read_msecs = jiffies_to_msecs(jiffies); |
| 1142 | port->last_read_msecs_diff = port->last_read_msecs - |
| 1143 | port->last_read_msecs_diff; |
| 1144 | |
| 1145 | saa7164_histogram_update(&port->read_interval, |
| 1146 | port->last_read_msecs_diff); |
| 1147 | |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1148 | if (*pos) { |
| 1149 | printk(KERN_ERR "%s() ESPIPE\n", __func__); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1150 | return -ESPIPE; |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1151 | } |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1152 | |
| 1153 | if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) { |
| 1154 | if (atomic_inc_return(&port->v4l_reader_count) == 1) { |
| 1155 | |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1156 | if (saa7164_encoder_initialize(port) < 0) { |
| 1157 | printk(KERN_ERR "%s() EINVAL\n", __func__); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1158 | return -EINVAL; |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1159 | } |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1160 | |
| 1161 | saa7164_encoder_start_streaming(port); |
| 1162 | msleep(200); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | /* blocking wait for buffer */ |
| 1167 | if ((file->f_flags & O_NONBLOCK) == 0) { |
| 1168 | if (wait_event_interruptible(port->wait_read, |
| 1169 | saa7164_enc_next_buf(port))) { |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1170 | printk(KERN_ERR "%s() ERESTARTSYS\n", __func__); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1171 | return -ERESTARTSYS; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /* Pull the first buffer from the used list */ |
| 1176 | ubuf = saa7164_enc_next_buf(port); |
| 1177 | |
| 1178 | while ((count > 0) && ubuf) { |
| 1179 | |
| 1180 | /* set remaining bytes to copy */ |
| 1181 | rem = ubuf->actual_size - ubuf->pos; |
| 1182 | cnt = rem > count ? count : rem; |
| 1183 | |
| 1184 | p = ubuf->data + ubuf->pos; |
| 1185 | |
| 1186 | dprintk(DBGLVL_ENC, |
| 1187 | "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n", |
| 1188 | __func__, (int)count, cnt, rem, ubuf, ubuf->pos); |
| 1189 | |
| 1190 | if (copy_to_user(buffer, p, cnt)) { |
| 1191 | printk(KERN_ERR "%s() copy_to_user failed\n", __func__); |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1192 | if (!ret) { |
| 1193 | printk(KERN_ERR "%s() EFAULT\n", __func__); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1194 | ret = -EFAULT; |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1195 | } |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1196 | goto err; |
| 1197 | } |
| 1198 | |
| 1199 | ubuf->pos += cnt; |
| 1200 | count -= cnt; |
| 1201 | buffer += cnt; |
| 1202 | ret += cnt; |
| 1203 | |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1204 | if (ubuf->pos > ubuf->actual_size) { |
| 1205 | printk(KERN_ERR "read() pos > actual, huh?\n"); |
| 1206 | } |
| 1207 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1208 | if (ubuf->pos == ubuf->actual_size) { |
| 1209 | |
| 1210 | /* finished with current buffer, take next buffer */ |
| 1211 | |
| 1212 | /* Requeue the buffer on the free list */ |
| 1213 | ubuf->pos = 0; |
| 1214 | |
| 1215 | mutex_lock(&port->dmaqueue_lock); |
| 1216 | list_move_tail(&ubuf->list, &port->list_buf_free.list); |
| 1217 | mutex_unlock(&port->dmaqueue_lock); |
| 1218 | |
| 1219 | /* Dequeue next */ |
| 1220 | if ((file->f_flags & O_NONBLOCK) == 0) { |
| 1221 | if (wait_event_interruptible(port->wait_read, |
| 1222 | saa7164_enc_next_buf(port))) { |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | ubuf = saa7164_enc_next_buf(port); |
| 1227 | } |
| 1228 | } |
| 1229 | err: |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1230 | if (!ret && !ubuf) { |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1231 | ret = -EAGAIN; |
Steven Toth | 46eeb8d | 2010-07-31 15:11:59 -0300 | [diff] [blame] | 1232 | } |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1233 | |
| 1234 | return ret; |
| 1235 | } |
| 1236 | |
| 1237 | static unsigned int fops_poll(struct file *file, poll_table *wait) |
| 1238 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 1239 | struct saa7164_encoder_fh *fh = (struct saa7164_encoder_fh *)file->private_data; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1240 | struct saa7164_port *port = fh->port; |
| 1241 | struct saa7164_user_buffer *ubuf; |
| 1242 | unsigned int mask = 0; |
| 1243 | |
Steven Toth | 58acca1 | 2010-07-31 15:10:52 -0300 | [diff] [blame] | 1244 | port->last_poll_msecs_diff = port->last_poll_msecs; |
| 1245 | port->last_poll_msecs = jiffies_to_msecs(jiffies); |
| 1246 | port->last_poll_msecs_diff = port->last_poll_msecs - |
| 1247 | port->last_poll_msecs_diff; |
| 1248 | |
| 1249 | saa7164_histogram_update(&port->poll_interval, |
| 1250 | port->last_poll_msecs_diff); |
| 1251 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1252 | if (!video_is_registered(port->v4l_device)) { |
| 1253 | return -EIO; |
| 1254 | } |
| 1255 | |
| 1256 | if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) { |
| 1257 | if (atomic_inc_return(&port->v4l_reader_count) == 1) { |
| 1258 | if (saa7164_encoder_initialize(port) < 0) |
| 1259 | return -EINVAL; |
| 1260 | saa7164_encoder_start_streaming(port); |
| 1261 | msleep(200); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | /* blocking wait for buffer */ |
| 1266 | if ((file->f_flags & O_NONBLOCK) == 0) { |
| 1267 | if (wait_event_interruptible(port->wait_read, |
| 1268 | saa7164_enc_next_buf(port))) { |
| 1269 | return -ERESTARTSYS; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /* Pull the first buffer from the used list */ |
| 1274 | ubuf = list_first_entry(&port->list_buf_used.list, |
| 1275 | struct saa7164_user_buffer, list); |
| 1276 | |
| 1277 | if (ubuf) |
| 1278 | mask |= POLLIN | POLLRDNORM; |
| 1279 | |
| 1280 | return mask; |
| 1281 | } |
| 1282 | |
| 1283 | static const struct v4l2_file_operations mpeg_fops = { |
| 1284 | .owner = THIS_MODULE, |
| 1285 | .open = fops_open, |
| 1286 | .release = fops_release, |
| 1287 | .read = fops_read, |
| 1288 | .poll = fops_poll, |
| 1289 | .unlocked_ioctl = video_ioctl2, |
| 1290 | }; |
| 1291 | |
| 1292 | int saa7164_g_chip_ident(struct file *file, void *fh, |
| 1293 | struct v4l2_dbg_chip_ident *chip) |
| 1294 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 1295 | struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1296 | struct saa7164_dev *dev = port->dev; |
| 1297 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | int saa7164_g_register(struct file *file, void *fh, |
| 1303 | struct v4l2_dbg_register *reg) |
| 1304 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 1305 | struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1306 | struct saa7164_dev *dev = port->dev; |
| 1307 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1308 | |
| 1309 | if (!capable(CAP_SYS_ADMIN)) |
| 1310 | return -EPERM; |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | int saa7164_s_register(struct file *file, void *fh, |
| 1316 | struct v4l2_dbg_register *reg) |
| 1317 | { |
Steven Toth | 96d8420 | 2010-07-31 16:04:59 -0300 | [diff] [blame] | 1318 | struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1319 | struct saa7164_dev *dev = port->dev; |
| 1320 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1321 | |
| 1322 | if (!capable(CAP_SYS_ADMIN)) |
| 1323 | return -EPERM; |
| 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static const struct v4l2_ioctl_ops mpeg_ioctl_ops = { |
| 1329 | .vidioc_s_std = vidioc_s_std, |
| 1330 | .vidioc_enum_input = vidioc_enum_input, |
| 1331 | .vidioc_g_input = vidioc_g_input, |
| 1332 | .vidioc_s_input = vidioc_s_input, |
| 1333 | .vidioc_g_tuner = vidioc_g_tuner, |
| 1334 | .vidioc_s_tuner = vidioc_s_tuner, |
| 1335 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1336 | .vidioc_s_frequency = vidioc_s_frequency, |
| 1337 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1338 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1339 | .vidioc_querycap = vidioc_querycap, |
| 1340 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1341 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1342 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1343 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 1344 | .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls, |
| 1345 | .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls, |
| 1346 | .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls, |
| 1347 | .vidioc_log_status = vidioc_log_status, |
| 1348 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1349 | .vidioc_g_chip_ident = saa7164_g_chip_ident, |
| 1350 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1351 | .vidioc_g_register = saa7164_g_register, |
| 1352 | .vidioc_s_register = saa7164_s_register, |
| 1353 | #endif |
| 1354 | }; |
| 1355 | |
| 1356 | static struct video_device saa7164_mpeg_template = { |
| 1357 | .name = "saa7164", |
| 1358 | .fops = &mpeg_fops, |
| 1359 | .ioctl_ops = &mpeg_ioctl_ops, |
| 1360 | .minor = -1, |
| 1361 | .tvnorms = SAA7164_NORMS, |
| 1362 | .current_norm = V4L2_STD_NTSC_M, |
| 1363 | }; |
| 1364 | |
| 1365 | static struct video_device *saa7164_encoder_alloc( |
| 1366 | struct saa7164_port *port, |
| 1367 | struct pci_dev *pci, |
| 1368 | struct video_device *template, |
| 1369 | char *type) |
| 1370 | { |
| 1371 | struct video_device *vfd; |
| 1372 | struct saa7164_dev *dev = port->dev; |
| 1373 | |
| 1374 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1375 | |
| 1376 | vfd = video_device_alloc(); |
| 1377 | if (NULL == vfd) |
| 1378 | return NULL; |
| 1379 | |
| 1380 | *vfd = *template; |
| 1381 | snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, |
| 1382 | type, saa7164_boards[dev->board].name); |
| 1383 | |
| 1384 | vfd->parent = &pci->dev; |
| 1385 | vfd->release = video_device_release; |
| 1386 | return vfd; |
| 1387 | } |
| 1388 | |
| 1389 | int saa7164_encoder_register(struct saa7164_port *port) |
| 1390 | { |
| 1391 | struct saa7164_dev *dev = port->dev; |
Steven Toth | 1b0e8e4 | 2010-07-31 16:01:00 -0300 | [diff] [blame] | 1392 | int result = -ENODEV; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1393 | |
| 1394 | dprintk(DBGLVL_ENC, "%s()\n", __func__); |
| 1395 | |
| 1396 | if (port->type != SAA7164_MPEG_ENCODER) |
| 1397 | BUG(); |
| 1398 | |
| 1399 | /* Sanity check that the PCI configuration space is active */ |
| 1400 | if (port->hwcfg.BARLocation == 0) { |
| 1401 | printk(KERN_ERR "%s() failed " |
| 1402 | "(errno = %d), NO PCI configuration\n", |
| 1403 | __func__, result); |
| 1404 | result = -ENOMEM; |
| 1405 | goto failed; |
| 1406 | } |
| 1407 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1408 | /* Establish encoder defaults here */ |
| 1409 | /* Set default TV standard */ |
| 1410 | port->encodernorm = saa7164_tvnorms[0]; |
| 1411 | port->width = 720; |
| 1412 | port->mux_input = 1; /* Composite */ |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1413 | port->video_format = EU_VIDEO_FORMAT_MPEG_2; |
| 1414 | port->audio_format = 0; |
| 1415 | port->video_resolution = 0; |
| 1416 | port->ctl_brightness = 127; |
| 1417 | port->ctl_contrast = 66; |
| 1418 | port->ctl_hue = 128; |
| 1419 | port->ctl_saturation = 62; |
| 1420 | port->ctl_sharpness = 8; |
| 1421 | port->encoder_params.bitrate = ENCODER_DEF_BITRATE; |
Steven Toth | 968b11b | 2010-07-31 14:59:38 -0300 | [diff] [blame] | 1422 | port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE; |
Steven Toth | 5fa56cc | 2010-07-31 15:05:35 -0300 | [diff] [blame] | 1423 | port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1424 | port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS; |
| 1425 | port->encoder_params.ctl_mute = 0; |
| 1426 | port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3; |
Steven Toth | 3ed43cf | 2010-07-31 14:58:35 -0300 | [diff] [blame] | 1427 | port->encoder_params.refdist = 1; |
Steven Toth | 5fa56cc | 2010-07-31 15:05:35 -0300 | [diff] [blame] | 1428 | port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1429 | |
| 1430 | if (port->encodernorm.id & V4L2_STD_525_60) |
| 1431 | port->height = 480; |
| 1432 | else |
| 1433 | port->height = 576; |
| 1434 | |
| 1435 | /* Allocate and register the video device node */ |
| 1436 | port->v4l_device = saa7164_encoder_alloc(port, |
| 1437 | dev->pci, &saa7164_mpeg_template, "mpeg"); |
| 1438 | |
| 1439 | if (port->v4l_device == NULL) { |
| 1440 | printk(KERN_INFO "%s: can't allocate mpeg device\n", |
| 1441 | dev->name); |
| 1442 | result = -ENOMEM; |
| 1443 | goto failed; |
| 1444 | } |
| 1445 | |
Steven Toth | 214ce3f | 2010-10-06 21:52:22 -0300 | [diff] [blame^] | 1446 | video_set_drvdata(port->v4l_device, port); |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1447 | result = video_register_device(port->v4l_device, |
| 1448 | VFL_TYPE_GRABBER, -1); |
| 1449 | if (result < 0) { |
| 1450 | printk(KERN_INFO "%s: can't register mpeg device\n", |
| 1451 | dev->name); |
| 1452 | /* TODO: We're going to leak here if we don't dealloc |
| 1453 | The buffers above. The unreg function can't deal wit it. |
| 1454 | */ |
| 1455 | goto failed; |
| 1456 | } |
| 1457 | |
| 1458 | printk(KERN_INFO "%s: registered device video%d [mpeg]\n", |
| 1459 | dev->name, port->v4l_device->num); |
| 1460 | |
| 1461 | /* Configure the hardware defaults */ |
| 1462 | saa7164_api_set_videomux(port); |
| 1463 | saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL); |
| 1464 | saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL); |
| 1465 | saa7164_api_set_usercontrol(port, PU_HUE_CONTROL); |
| 1466 | saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL); |
| 1467 | saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL); |
| 1468 | saa7164_api_audio_mute(port, 0); |
| 1469 | saa7164_api_set_audio_volume(port, 20); |
| 1470 | saa7164_api_set_aspect_ratio(port); |
| 1471 | |
| 1472 | /* Disable audio standard detection, it's buggy */ |
| 1473 | saa7164_api_set_audio_detection(port, 0); |
| 1474 | |
| 1475 | saa7164_api_set_encoder(port); |
| 1476 | saa7164_api_get_encoder(port); |
| 1477 | |
| 1478 | result = 0; |
| 1479 | failed: |
| 1480 | return result; |
| 1481 | } |
| 1482 | |
| 1483 | void saa7164_encoder_unregister(struct saa7164_port *port) |
| 1484 | { |
| 1485 | struct saa7164_dev *dev = port->dev; |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1486 | |
| 1487 | dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); |
| 1488 | |
| 1489 | if (port->type != SAA7164_MPEG_ENCODER) |
| 1490 | BUG(); |
| 1491 | |
| 1492 | if (port->v4l_device) { |
| 1493 | if (port->v4l_device->minor != -1) |
| 1494 | video_unregister_device(port->v4l_device); |
| 1495 | else |
| 1496 | video_device_release(port->v4l_device); |
| 1497 | |
| 1498 | port->v4l_device = NULL; |
| 1499 | } |
| 1500 | |
Steven Toth | 7615e43 | 2010-07-31 14:44:53 -0300 | [diff] [blame] | 1501 | dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr); |
| 1502 | } |
| 1503 | |