Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1 | /* |
| 2 | * cx18 init/start/stop/exit stream functions |
| 3 | * |
| 4 | * Derived from ivtv-streams.c |
| 5 | * |
| 6 | * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 21 | * 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include "cx18-driver.h" |
| 25 | #include "cx18-fileops.h" |
| 26 | #include "cx18-mailbox.h" |
| 27 | #include "cx18-i2c.h" |
| 28 | #include "cx18-queue.h" |
| 29 | #include "cx18-ioctl.h" |
| 30 | #include "cx18-streams.h" |
| 31 | #include "cx18-cards.h" |
| 32 | #include "cx18-scb.h" |
| 33 | #include "cx18-av-core.h" |
| 34 | #include "cx18-dvb.h" |
| 35 | |
| 36 | #define CX18_DSP0_INTERRUPT_MASK 0xd0004C |
| 37 | |
| 38 | static struct file_operations cx18_v4l2_enc_fops = { |
Hans Verkuil | daf20d9 | 2008-05-12 11:21:58 -0300 | [diff] [blame] | 39 | .owner = THIS_MODULE, |
| 40 | .read = cx18_v4l2_read, |
| 41 | .open = cx18_v4l2_open, |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 42 | /* FIXME change to video_ioctl2 if serialization lock can be removed */ |
Hans Verkuil | daf20d9 | 2008-05-12 11:21:58 -0300 | [diff] [blame] | 43 | .ioctl = cx18_v4l2_ioctl, |
| 44 | .compat_ioctl = v4l_compat_ioctl32, |
| 45 | .release = cx18_v4l2_close, |
| 46 | .poll = cx18_v4l2_enc_poll, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* offset from 0 to register ts v4l2 minors on */ |
| 50 | #define CX18_V4L2_ENC_TS_OFFSET 16 |
| 51 | /* offset from 0 to register pcm v4l2 minors on */ |
| 52 | #define CX18_V4L2_ENC_PCM_OFFSET 24 |
| 53 | /* offset from 0 to register yuv v4l2 minors on */ |
| 54 | #define CX18_V4L2_ENC_YUV_OFFSET 32 |
| 55 | |
| 56 | static struct { |
| 57 | const char *name; |
| 58 | int vfl_type; |
| 59 | int minor_offset; |
| 60 | int dma; |
| 61 | enum v4l2_buf_type buf_type; |
| 62 | struct file_operations *fops; |
| 63 | } cx18_stream_info[] = { |
| 64 | { /* CX18_ENC_STREAM_TYPE_MPG */ |
| 65 | "encoder MPEG", |
| 66 | VFL_TYPE_GRABBER, 0, |
| 67 | PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 68 | &cx18_v4l2_enc_fops |
| 69 | }, |
| 70 | { /* CX18_ENC_STREAM_TYPE_TS */ |
| 71 | "TS", |
| 72 | VFL_TYPE_GRABBER, -1, |
| 73 | PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 74 | &cx18_v4l2_enc_fops |
| 75 | }, |
| 76 | { /* CX18_ENC_STREAM_TYPE_YUV */ |
| 77 | "encoder YUV", |
| 78 | VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET, |
| 79 | PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 80 | &cx18_v4l2_enc_fops |
| 81 | }, |
| 82 | { /* CX18_ENC_STREAM_TYPE_VBI */ |
| 83 | "encoder VBI", |
| 84 | VFL_TYPE_VBI, 0, |
| 85 | PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE, |
| 86 | &cx18_v4l2_enc_fops |
| 87 | }, |
| 88 | { /* CX18_ENC_STREAM_TYPE_PCM */ |
| 89 | "encoder PCM audio", |
| 90 | VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET, |
| 91 | PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE, |
| 92 | &cx18_v4l2_enc_fops |
| 93 | }, |
| 94 | { /* CX18_ENC_STREAM_TYPE_IDX */ |
| 95 | "encoder IDX", |
| 96 | VFL_TYPE_GRABBER, -1, |
| 97 | PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 98 | &cx18_v4l2_enc_fops |
| 99 | }, |
| 100 | { /* CX18_ENC_STREAM_TYPE_RAD */ |
| 101 | "encoder radio", |
| 102 | VFL_TYPE_RADIO, 0, |
| 103 | PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE, |
| 104 | &cx18_v4l2_enc_fops |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | static void cx18_stream_init(struct cx18 *cx, int type) |
| 109 | { |
| 110 | struct cx18_stream *s = &cx->streams[type]; |
| 111 | struct video_device *dev = s->v4l2dev; |
| 112 | u32 max_size = cx->options.megabytes[type] * 1024 * 1024; |
| 113 | |
| 114 | /* we need to keep v4l2dev, so restore it afterwards */ |
| 115 | memset(s, 0, sizeof(*s)); |
| 116 | s->v4l2dev = dev; |
| 117 | |
| 118 | /* initialize cx18_stream fields */ |
| 119 | s->cx = cx; |
| 120 | s->type = type; |
| 121 | s->name = cx18_stream_info[type].name; |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame^] | 122 | s->handle = CX18_INVALID_TASK_HANDLE; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 123 | |
| 124 | s->dma = cx18_stream_info[type].dma; |
| 125 | s->buf_size = cx->stream_buf_size[type]; |
| 126 | if (s->buf_size) |
| 127 | s->buffers = max_size / s->buf_size; |
| 128 | if (s->buffers > 63) { |
| 129 | /* Each stream has a maximum of 63 buffers, |
| 130 | ensure we do not exceed that. */ |
| 131 | s->buffers = 63; |
| 132 | s->buf_size = (max_size / s->buffers) & ~0xfff; |
| 133 | } |
| 134 | spin_lock_init(&s->qlock); |
| 135 | init_waitqueue_head(&s->waitq); |
| 136 | s->id = -1; |
| 137 | cx18_queue_init(&s->q_free); |
| 138 | cx18_queue_init(&s->q_full); |
| 139 | cx18_queue_init(&s->q_io); |
| 140 | } |
| 141 | |
| 142 | static int cx18_prep_dev(struct cx18 *cx, int type) |
| 143 | { |
| 144 | struct cx18_stream *s = &cx->streams[type]; |
| 145 | u32 cap = cx->v4l2_cap; |
| 146 | int minor_offset = cx18_stream_info[type].minor_offset; |
| 147 | int minor; |
| 148 | |
| 149 | /* These four fields are always initialized. If v4l2dev == NULL, then |
| 150 | this stream is not in use. In that case no other fields but these |
| 151 | four can be used. */ |
| 152 | s->v4l2dev = NULL; |
| 153 | s->cx = cx; |
| 154 | s->type = type; |
| 155 | s->name = cx18_stream_info[type].name; |
| 156 | |
| 157 | /* Check whether the radio is supported */ |
| 158 | if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO)) |
| 159 | return 0; |
| 160 | |
| 161 | /* Check whether VBI is supported */ |
| 162 | if (type == CX18_ENC_STREAM_TYPE_VBI && |
| 163 | !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE))) |
| 164 | return 0; |
| 165 | |
| 166 | /* card number + user defined offset + device offset */ |
| 167 | minor = cx->num + cx18_first_minor + minor_offset; |
| 168 | |
| 169 | /* User explicitly selected 0 buffers for these streams, so don't |
| 170 | create them. */ |
| 171 | if (cx18_stream_info[type].dma != PCI_DMA_NONE && |
| 172 | cx->options.megabytes[type] == 0) { |
| 173 | CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | cx18_stream_init(cx, type); |
| 178 | |
| 179 | if (minor_offset == -1) |
| 180 | return 0; |
| 181 | |
| 182 | /* allocate and initialize the v4l2 video device structure */ |
| 183 | s->v4l2dev = video_device_alloc(); |
| 184 | if (s->v4l2dev == NULL) { |
| 185 | CX18_ERR("Couldn't allocate v4l2 video_device for %s\n", |
| 186 | s->name); |
| 187 | return -ENOMEM; |
| 188 | } |
| 189 | |
Hans Verkuil | 37f89f9 | 2008-06-22 11:57:31 -0300 | [diff] [blame] | 190 | snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d", |
| 191 | cx->num); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 192 | |
| 193 | s->v4l2dev->minor = minor; |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 194 | s->v4l2dev->parent = &cx->dev->dev; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 195 | s->v4l2dev->fops = cx18_stream_info[type].fops; |
| 196 | s->v4l2dev->release = video_device_release; |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 197 | s->v4l2dev->tvnorms = V4L2_STD_ALL; |
| 198 | cx18_set_funcs(s->v4l2dev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* Initialize v4l2 variables and register v4l2 devices */ |
| 203 | int cx18_streams_setup(struct cx18 *cx) |
| 204 | { |
| 205 | int type; |
| 206 | |
| 207 | /* Setup V4L2 Devices */ |
| 208 | for (type = 0; type < CX18_MAX_STREAMS; type++) { |
| 209 | /* Prepare device */ |
| 210 | if (cx18_prep_dev(cx, type)) |
| 211 | break; |
| 212 | |
| 213 | /* Allocate Stream */ |
| 214 | if (cx18_stream_alloc(&cx->streams[type])) |
| 215 | break; |
| 216 | } |
| 217 | if (type == CX18_MAX_STREAMS) |
| 218 | return 0; |
| 219 | |
| 220 | /* One or more streams could not be initialized. Clean 'em all up. */ |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 221 | cx18_streams_cleanup(cx, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 222 | return -ENOMEM; |
| 223 | } |
| 224 | |
| 225 | static int cx18_reg_dev(struct cx18 *cx, int type) |
| 226 | { |
| 227 | struct cx18_stream *s = &cx->streams[type]; |
| 228 | int vfl_type = cx18_stream_info[type].vfl_type; |
| 229 | int minor; |
| 230 | |
| 231 | /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something? |
| 232 | * We need a VFL_TYPE_TS defined. |
| 233 | */ |
| 234 | if (strcmp("TS", s->name) == 0) { |
| 235 | /* just return if no DVB is supported */ |
| 236 | if ((cx->card->hw_all & CX18_HW_DVB) == 0) |
| 237 | return 0; |
| 238 | if (cx18_dvb_register(s) < 0) { |
| 239 | CX18_ERR("DVB failed to register\n"); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (s->v4l2dev == NULL) |
| 245 | return 0; |
| 246 | |
| 247 | minor = s->v4l2dev->minor; |
| 248 | |
| 249 | /* Register device. First try the desired minor, then any free one. */ |
| 250 | if (video_register_device(s->v4l2dev, vfl_type, minor) && |
| 251 | video_register_device(s->v4l2dev, vfl_type, -1)) { |
| 252 | CX18_ERR("Couldn't register v4l2 device for %s minor %d\n", |
| 253 | s->name, minor); |
| 254 | video_device_release(s->v4l2dev); |
| 255 | s->v4l2dev = NULL; |
| 256 | return -ENOMEM; |
| 257 | } |
| 258 | minor = s->v4l2dev->minor; |
| 259 | |
| 260 | switch (vfl_type) { |
| 261 | case VFL_TYPE_GRABBER: |
| 262 | CX18_INFO("Registered device video%d for %s (%d MB)\n", |
| 263 | minor, s->name, cx->options.megabytes[type]); |
| 264 | break; |
| 265 | |
| 266 | case VFL_TYPE_RADIO: |
| 267 | CX18_INFO("Registered device radio%d for %s\n", |
| 268 | minor - MINOR_VFL_TYPE_RADIO_MIN, s->name); |
| 269 | break; |
| 270 | |
| 271 | case VFL_TYPE_VBI: |
| 272 | if (cx->options.megabytes[type]) |
| 273 | CX18_INFO("Registered device vbi%d for %s (%d MB)\n", |
| 274 | minor - MINOR_VFL_TYPE_VBI_MIN, |
| 275 | s->name, cx->options.megabytes[type]); |
| 276 | else |
| 277 | CX18_INFO("Registered device vbi%d for %s\n", |
| 278 | minor - MINOR_VFL_TYPE_VBI_MIN, s->name); |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* Register v4l2 devices */ |
| 286 | int cx18_streams_register(struct cx18 *cx) |
| 287 | { |
| 288 | int type; |
| 289 | int err = 0; |
| 290 | |
| 291 | /* Register V4L2 devices */ |
| 292 | for (type = 0; type < CX18_MAX_STREAMS; type++) |
| 293 | err |= cx18_reg_dev(cx, type); |
| 294 | |
| 295 | if (err == 0) |
| 296 | return 0; |
| 297 | |
| 298 | /* One or more streams could not be initialized. Clean 'em all up. */ |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 299 | cx18_streams_cleanup(cx, 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 300 | return -ENOMEM; |
| 301 | } |
| 302 | |
| 303 | /* Unregister v4l2 devices */ |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 304 | void cx18_streams_cleanup(struct cx18 *cx, int unregister) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 305 | { |
| 306 | struct video_device *vdev; |
| 307 | int type; |
| 308 | |
| 309 | /* Teardown all streams */ |
| 310 | for (type = 0; type < CX18_MAX_STREAMS; type++) { |
Hans Verkuil | fac3639 | 2008-07-18 10:07:10 -0300 | [diff] [blame] | 311 | if (cx->streams[type].dvb.enabled) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 312 | cx18_dvb_unregister(&cx->streams[type]); |
Hans Verkuil | fac3639 | 2008-07-18 10:07:10 -0300 | [diff] [blame] | 313 | cx->streams[type].dvb.enabled = false; |
| 314 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 315 | |
| 316 | vdev = cx->streams[type].v4l2dev; |
| 317 | |
| 318 | cx->streams[type].v4l2dev = NULL; |
| 319 | if (vdev == NULL) |
| 320 | continue; |
| 321 | |
| 322 | cx18_stream_free(&cx->streams[type]); |
| 323 | |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 324 | /* Unregister or release device */ |
| 325 | if (unregister) |
| 326 | video_unregister_device(vdev); |
| 327 | else |
| 328 | video_device_release(vdev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | static void cx18_vbi_setup(struct cx18_stream *s) |
| 333 | { |
| 334 | struct cx18 *cx = s->cx; |
| 335 | int raw = cx->vbi.sliced_in->service_set == 0; |
| 336 | u32 data[CX2341X_MBOX_MAX_DATA]; |
| 337 | int lines; |
| 338 | |
| 339 | if (cx->is_60hz) { |
| 340 | cx->vbi.count = 12; |
| 341 | cx->vbi.start[0] = 10; |
| 342 | cx->vbi.start[1] = 273; |
| 343 | } else { /* PAL/SECAM */ |
| 344 | cx->vbi.count = 18; |
| 345 | cx->vbi.start[0] = 6; |
| 346 | cx->vbi.start[1] = 318; |
| 347 | } |
| 348 | |
| 349 | /* setup VBI registers */ |
| 350 | cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in); |
| 351 | |
| 352 | /* determine number of lines and total number of VBI bytes. |
| 353 | A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1 |
| 354 | The '- 1' byte is probably an unused U or V byte. Or something... |
| 355 | A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal |
| 356 | header, 42 data bytes + checksum (to be confirmed) */ |
| 357 | if (raw) { |
| 358 | lines = cx->vbi.count * 2; |
| 359 | } else { |
| 360 | lines = cx->is_60hz ? 24 : 38; |
| 361 | if (cx->is_60hz) |
| 362 | lines += 2; |
| 363 | } |
| 364 | |
| 365 | cx->vbi.enc_size = lines * |
| 366 | (raw ? cx->vbi.raw_size : cx->vbi.sliced_size); |
| 367 | |
| 368 | data[0] = s->handle; |
| 369 | /* Lines per field */ |
| 370 | data[1] = (lines / 2) | ((lines / 2) << 16); |
| 371 | /* bytes per line */ |
| 372 | data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size); |
| 373 | /* Every X number of frames a VBI interrupt arrives |
| 374 | (frames as in 25 or 30 fps) */ |
| 375 | data[3] = 1; |
| 376 | /* Setup VBI for the cx25840 digitizer */ |
| 377 | if (raw) { |
| 378 | data[4] = 0x20602060; |
| 379 | data[5] = 0x30703070; |
| 380 | } else { |
| 381 | data[4] = 0xB0F0B0F0; |
| 382 | data[5] = 0xA0E0A0E0; |
| 383 | } |
| 384 | |
| 385 | CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n", |
| 386 | data[0], data[1], data[2], data[3], data[4], data[5]); |
| 387 | |
| 388 | if (s->type == CX18_ENC_STREAM_TYPE_VBI) |
| 389 | cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data); |
| 390 | } |
| 391 | |
| 392 | int cx18_start_v4l2_encode_stream(struct cx18_stream *s) |
| 393 | { |
| 394 | u32 data[MAX_MB_ARGUMENTS]; |
| 395 | struct cx18 *cx = s->cx; |
| 396 | struct list_head *p; |
| 397 | int ts = 0; |
| 398 | int captype = 0; |
| 399 | |
| 400 | if (s->v4l2dev == NULL && s->dvb.enabled == 0) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | CX18_DEBUG_INFO("Start encoder stream %s\n", s->name); |
| 404 | |
| 405 | switch (s->type) { |
| 406 | case CX18_ENC_STREAM_TYPE_MPG: |
| 407 | captype = CAPTURE_CHANNEL_TYPE_MPEG; |
| 408 | cx->mpg_data_received = cx->vbi_data_inserted = 0; |
| 409 | cx->dualwatch_jiffies = jiffies; |
| 410 | cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300; |
| 411 | cx->search_pack_header = 0; |
| 412 | break; |
| 413 | |
| 414 | case CX18_ENC_STREAM_TYPE_TS: |
| 415 | captype = CAPTURE_CHANNEL_TYPE_TS; |
| 416 | ts = 1; |
| 417 | break; |
| 418 | case CX18_ENC_STREAM_TYPE_YUV: |
| 419 | captype = CAPTURE_CHANNEL_TYPE_YUV; |
| 420 | break; |
| 421 | case CX18_ENC_STREAM_TYPE_PCM: |
| 422 | captype = CAPTURE_CHANNEL_TYPE_PCM; |
| 423 | break; |
| 424 | case CX18_ENC_STREAM_TYPE_VBI: |
| 425 | captype = cx->vbi.sliced_in->service_set ? |
| 426 | CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI; |
| 427 | cx->vbi.frame = 0; |
| 428 | cx->vbi.inserted_frame = 0; |
| 429 | memset(cx->vbi.sliced_mpeg_size, |
| 430 | 0, sizeof(cx->vbi.sliced_mpeg_size)); |
| 431 | break; |
| 432 | default: |
| 433 | return -EINVAL; |
| 434 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 435 | |
| 436 | /* mute/unmute video */ |
| 437 | cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, |
| 438 | s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)); |
| 439 | |
| 440 | /* Clear Streamoff flags in case left from last capture */ |
| 441 | clear_bit(CX18_F_S_STREAMOFF, &s->s_flags); |
| 442 | |
| 443 | cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE); |
| 444 | s->handle = data[0]; |
| 445 | cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype); |
| 446 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 447 | if (atomic_read(&cx->ana_capturing) == 0 && !ts) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 448 | /* Stuff from Windows, we don't know what it is */ |
| 449 | cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0); |
| 450 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1); |
| 451 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0); |
| 452 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1); |
| 453 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12); |
| 454 | |
| 455 | cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3, |
| 456 | s->handle, cx->digitizer, cx->digitizer); |
| 457 | |
| 458 | /* Setup VBI */ |
| 459 | if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE) |
| 460 | cx18_vbi_setup(s); |
| 461 | |
| 462 | /* assign program index info. |
| 463 | Mask 7: select I/P/B, Num_req: 400 max */ |
| 464 | cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0); |
| 465 | |
| 466 | /* Setup API for Stream */ |
| 467 | cx2341x_update(cx, cx18_api_func, NULL, &cx->params); |
| 468 | } |
| 469 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 470 | if (atomic_read(&cx->tot_capturing) == 0) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 471 | clear_bit(CX18_F_I_EOS, &cx->i_flags); |
| 472 | write_reg(7, CX18_DSP0_INTERRUPT_MASK); |
| 473 | } |
| 474 | |
| 475 | cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle, |
Al Viro | 990c81c | 2008-05-21 00:32:01 -0300 | [diff] [blame] | 476 | (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem, |
| 477 | (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 478 | |
| 479 | list_for_each(p, &s->q_free.list) { |
| 480 | struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list); |
| 481 | |
| 482 | writel(buf->dma_handle, &cx->scb->cpu_mdl[buf->id].paddr); |
| 483 | writel(s->buf_size, &cx->scb->cpu_mdl[buf->id].length); |
| 484 | cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle, |
Al Viro | 990c81c | 2008-05-21 00:32:01 -0300 | [diff] [blame] | 485 | (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem, |
| 486 | 1, buf->id, s->buf_size); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 487 | } |
| 488 | /* begin_capture */ |
| 489 | if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) { |
| 490 | CX18_DEBUG_WARN("Error starting capture!\n"); |
| 491 | cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle); |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | /* you're live! sit back and await interrupts :) */ |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 496 | if (!ts) |
| 497 | atomic_inc(&cx->ana_capturing); |
| 498 | atomic_inc(&cx->tot_capturing); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | void cx18_stop_all_captures(struct cx18 *cx) |
| 503 | { |
| 504 | int i; |
| 505 | |
| 506 | for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) { |
| 507 | struct cx18_stream *s = &cx->streams[i]; |
| 508 | |
| 509 | if (s->v4l2dev == NULL && s->dvb.enabled == 0) |
| 510 | continue; |
| 511 | if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) |
| 512 | cx18_stop_v4l2_encode_stream(s, 0); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end) |
| 517 | { |
| 518 | struct cx18 *cx = s->cx; |
| 519 | unsigned long then; |
| 520 | |
| 521 | if (s->v4l2dev == NULL && s->dvb.enabled == 0) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | /* This function assumes that you are allowed to stop the capture |
| 525 | and that we are actually capturing */ |
| 526 | |
| 527 | CX18_DEBUG_INFO("Stop Capture\n"); |
| 528 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 529 | if (atomic_read(&cx->tot_capturing) == 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 530 | return 0; |
| 531 | |
| 532 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) |
| 533 | cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end); |
| 534 | else |
| 535 | cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle); |
| 536 | |
| 537 | then = jiffies; |
| 538 | |
| 539 | if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) { |
| 540 | CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n"); |
| 541 | } |
| 542 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 543 | if (s->type != CX18_ENC_STREAM_TYPE_TS) |
| 544 | atomic_dec(&cx->ana_capturing); |
| 545 | atomic_dec(&cx->tot_capturing); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 546 | |
| 547 | /* Clear capture and no-read bits */ |
| 548 | clear_bit(CX18_F_S_STREAMING, &s->s_flags); |
| 549 | |
| 550 | cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle); |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame^] | 551 | s->handle = CX18_INVALID_TASK_HANDLE; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 552 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 553 | if (atomic_read(&cx->tot_capturing) > 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 554 | return 0; |
| 555 | |
| 556 | write_reg(5, CX18_DSP0_INTERRUPT_MASK); |
| 557 | wake_up(&s->waitq); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | u32 cx18_find_handle(struct cx18 *cx) |
| 563 | { |
| 564 | int i; |
| 565 | |
| 566 | /* find first available handle to be used for global settings */ |
| 567 | for (i = 0; i < CX18_MAX_STREAMS; i++) { |
| 568 | struct cx18_stream *s = &cx->streams[i]; |
| 569 | |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame^] | 570 | if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE)) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 571 | return s->handle; |
| 572 | } |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame^] | 573 | return CX18_INVALID_TASK_HANDLE; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 574 | } |