Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1 | /* |
| 2 | file operation functions |
| 3 | Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com> |
| 4 | Copyright (C) 2004 Chris Kennedy <c@groovy.org> |
| 5 | Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include "ivtv-driver.h" |
| 23 | #include "ivtv-fileops.h" |
| 24 | #include "ivtv-i2c.h" |
| 25 | #include "ivtv-queue.h" |
| 26 | #include "ivtv-udma.h" |
| 27 | #include "ivtv-irq.h" |
| 28 | #include "ivtv-vbi.h" |
| 29 | #include "ivtv-mailbox.h" |
| 30 | #include "ivtv-audio.h" |
| 31 | #include "ivtv-streams.h" |
| 32 | #include "ivtv-yuv.h" |
| 33 | #include "ivtv-controls.h" |
| 34 | #include "ivtv-ioctl.h" |
Hans Verkuil | f2100d8 | 2007-05-17 08:08:45 -0300 | [diff] [blame] | 35 | #include "ivtv-cards.h" |
| 36 | #include <media/saa7115.h> |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 37 | |
| 38 | /* This function tries to claim the stream for a specific file descriptor. |
| 39 | If no one else is using this stream then the stream is claimed and |
| 40 | associated VBI streams are also automatically claimed. |
| 41 | Possible error returns: -EBUSY if someone else has claimed |
| 42 | the stream or 0 on success. */ |
| 43 | int ivtv_claim_stream(struct ivtv_open_id *id, int type) |
| 44 | { |
| 45 | struct ivtv *itv = id->itv; |
| 46 | struct ivtv_stream *s = &itv->streams[type]; |
| 47 | struct ivtv_stream *s_vbi; |
| 48 | int vbi_type; |
| 49 | |
| 50 | if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) { |
| 51 | /* someone already claimed this stream */ |
| 52 | if (s->id == id->open_id) { |
| 53 | /* yes, this file descriptor did. So that's OK. */ |
| 54 | return 0; |
| 55 | } |
| 56 | if (s->id == -1 && (type == IVTV_DEC_STREAM_TYPE_VBI || |
| 57 | type == IVTV_ENC_STREAM_TYPE_VBI)) { |
| 58 | /* VBI is handled already internally, now also assign |
| 59 | the file descriptor to this stream for external |
| 60 | reading of the stream. */ |
| 61 | s->id = id->open_id; |
| 62 | IVTV_DEBUG_INFO("Start Read VBI\n"); |
| 63 | return 0; |
| 64 | } |
| 65 | /* someone else is using this stream already */ |
| 66 | IVTV_DEBUG_INFO("Stream %d is busy\n", type); |
| 67 | return -EBUSY; |
| 68 | } |
| 69 | s->id = id->open_id; |
| 70 | if (type == IVTV_DEC_STREAM_TYPE_VBI) { |
| 71 | /* Enable reinsertion interrupt */ |
| 72 | ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); |
| 73 | } |
| 74 | |
| 75 | /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI, |
| 76 | IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI |
| 77 | (provided VBI insertion is on and sliced VBI is selected), for all |
| 78 | other streams we're done */ |
| 79 | if (type == IVTV_DEC_STREAM_TYPE_MPG) { |
| 80 | vbi_type = IVTV_DEC_STREAM_TYPE_VBI; |
| 81 | } else if (type == IVTV_ENC_STREAM_TYPE_MPG && |
| 82 | itv->vbi.insert_mpeg && itv->vbi.sliced_in->service_set) { |
| 83 | vbi_type = IVTV_ENC_STREAM_TYPE_VBI; |
| 84 | } else { |
| 85 | return 0; |
| 86 | } |
| 87 | s_vbi = &itv->streams[vbi_type]; |
| 88 | |
| 89 | if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) { |
| 90 | /* Enable reinsertion interrupt */ |
| 91 | if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI) |
| 92 | ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); |
| 93 | } |
| 94 | /* mark that it is used internally */ |
| 95 | set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* This function releases a previously claimed stream. It will take into |
| 100 | account associated VBI streams. */ |
| 101 | void ivtv_release_stream(struct ivtv_stream *s) |
| 102 | { |
| 103 | struct ivtv *itv = s->itv; |
| 104 | struct ivtv_stream *s_vbi; |
| 105 | |
| 106 | s->id = -1; |
| 107 | if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) && |
| 108 | test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) { |
| 109 | /* this stream is still in use internally */ |
| 110 | return; |
| 111 | } |
| 112 | if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) { |
| 113 | IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | ivtv_flush_queues(s); |
| 118 | |
| 119 | /* disable reinsertion interrupt */ |
| 120 | if (s->type == IVTV_DEC_STREAM_TYPE_VBI) |
| 121 | ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); |
| 122 | |
| 123 | /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI, |
| 124 | IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI, |
| 125 | for all other streams we're done */ |
| 126 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG) |
| 127 | s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI]; |
| 128 | else if (s->type == IVTV_ENC_STREAM_TYPE_MPG) |
| 129 | s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; |
| 130 | else |
| 131 | return; |
| 132 | |
| 133 | /* clear internal use flag */ |
| 134 | if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) { |
| 135 | /* was already cleared */ |
| 136 | return; |
| 137 | } |
| 138 | if (s_vbi->id != -1) { |
| 139 | /* VBI stream still claimed by a file descriptor */ |
| 140 | return; |
| 141 | } |
| 142 | /* disable reinsertion interrupt */ |
| 143 | if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI) |
| 144 | ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); |
| 145 | clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags); |
| 146 | ivtv_flush_queues(s_vbi); |
| 147 | } |
| 148 | |
| 149 | static void ivtv_dualwatch(struct ivtv *itv) |
| 150 | { |
| 151 | struct v4l2_tuner vt; |
| 152 | u16 new_bitmap; |
| 153 | u16 new_stereo_mode; |
| 154 | const u16 stereo_mask = 0x0300; |
| 155 | const u16 dual = 0x0200; |
| 156 | |
| 157 | new_stereo_mode = itv->params.audio_properties & stereo_mask; |
| 158 | memset(&vt, 0, sizeof(vt)); |
| 159 | ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, &vt); |
| 160 | if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2)) |
| 161 | new_stereo_mode = dual; |
| 162 | |
| 163 | if (new_stereo_mode == itv->dualwatch_stereo_mode) |
| 164 | return; |
| 165 | |
| 166 | new_bitmap = new_stereo_mode | (itv->params.audio_properties & ~stereo_mask); |
| 167 | |
| 168 | IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n", |
| 169 | itv->dualwatch_stereo_mode, new_stereo_mode, new_bitmap); |
| 170 | |
| 171 | if (ivtv_vapi(itv, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new_bitmap) == 0) { |
| 172 | itv->dualwatch_stereo_mode = new_stereo_mode; |
| 173 | return; |
| 174 | } |
| 175 | IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n"); |
| 176 | } |
| 177 | |
| 178 | static void ivtv_update_pgm_info(struct ivtv *itv) |
| 179 | { |
| 180 | u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24; |
| 181 | int cnt; |
| 182 | int i = 0; |
| 183 | |
| 184 | if (wr_idx >= itv->pgm_info_num) { |
| 185 | IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num); |
| 186 | return; |
| 187 | } |
| 188 | cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num; |
| 189 | while (i < cnt) { |
| 190 | int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num; |
| 191 | struct v4l2_enc_idx_entry *e = itv->pgm_info + idx; |
| 192 | u32 addr = itv->pgm_info_offset + 4 + idx * 24; |
Hans Verkuil | 5614b02 | 2007-08-23 17:48:41 -0300 | [diff] [blame] | 193 | const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1, |
| 194 | V4L2_ENC_IDX_FRAME_B, -1, -1, -1 }; |
| 195 | // 1=I, 2=P, 4=B |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 196 | |
| 197 | e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32); |
| 198 | if (e->offset > itv->mpg_data_received) { |
| 199 | break; |
| 200 | } |
| 201 | e->offset += itv->vbi_data_inserted; |
| 202 | e->length = read_enc(addr); |
| 203 | e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32); |
Hans Verkuil | 5614b02 | 2007-08-23 17:48:41 -0300 | [diff] [blame] | 204 | e->flags = mapping[read_enc(addr + 12) & 7]; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 205 | i++; |
| 206 | } |
| 207 | itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num; |
| 208 | } |
| 209 | |
| 210 | static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err) |
| 211 | { |
| 212 | struct ivtv *itv = s->itv; |
| 213 | struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; |
| 214 | struct ivtv_buffer *buf; |
| 215 | DEFINE_WAIT(wait); |
| 216 | |
| 217 | *err = 0; |
| 218 | while (1) { |
| 219 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG) { |
| 220 | /* Process pending program info updates and pending VBI data */ |
| 221 | ivtv_update_pgm_info(itv); |
| 222 | |
Mauro Carvalho Chehab | 201700d | 2007-07-19 11:21:04 -0300 | [diff] [blame] | 223 | if (jiffies - itv->dualwatch_jiffies > msecs_to_jiffies(1000)) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 224 | itv->dualwatch_jiffies = jiffies; |
| 225 | ivtv_dualwatch(itv); |
| 226 | } |
| 227 | |
| 228 | if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) && |
| 229 | !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) { |
| 230 | while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) { |
| 231 | /* byteswap and process VBI data */ |
| 232 | ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type); |
| 233 | ivtv_enqueue(s_vbi, buf, &s_vbi->q_free); |
| 234 | } |
| 235 | } |
| 236 | buf = &itv->vbi.sliced_mpeg_buf; |
| 237 | if (buf->readpos != buf->bytesused) { |
| 238 | return buf; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* do we have leftover data? */ |
| 243 | buf = ivtv_dequeue(s, &s->q_io); |
| 244 | if (buf) |
| 245 | return buf; |
| 246 | |
| 247 | /* do we have new data? */ |
| 248 | buf = ivtv_dequeue(s, &s->q_full); |
| 249 | if (buf) { |
Hans Verkuil | f4071b8 | 2007-07-28 12:07:12 -0300 | [diff] [blame] | 250 | if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 251 | return buf; |
Hans Verkuil | f4071b8 | 2007-07-28 12:07:12 -0300 | [diff] [blame] | 252 | buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 253 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG) |
| 254 | /* byteswap MPG data */ |
| 255 | ivtv_buf_swap(buf); |
| 256 | else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) { |
| 257 | /* byteswap and process VBI data */ |
| 258 | ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type); |
| 259 | } |
| 260 | return buf; |
| 261 | } |
| 262 | /* return if file was opened with O_NONBLOCK */ |
| 263 | if (non_block) { |
| 264 | *err = -EAGAIN; |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | /* return if end of stream */ |
| 269 | if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
| 270 | clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 271 | IVTV_DEBUG_INFO("EOS %s\n", s->name); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | /* wait for more data to arrive */ |
| 276 | prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); |
| 277 | /* New buffers might have become available before we were added to the waitqueue */ |
| 278 | if (!s->q_full.buffers) |
| 279 | schedule(); |
| 280 | finish_wait(&s->waitq, &wait); |
| 281 | if (signal_pending(current)) { |
| 282 | /* return if a signal was received */ |
| 283 | IVTV_DEBUG_INFO("User stopped %s\n", s->name); |
| 284 | *err = -EINTR; |
| 285 | return NULL; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv) |
| 291 | { |
| 292 | int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES; |
| 293 | |
| 294 | itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx]; |
| 295 | itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx]; |
| 296 | itv->vbi.sliced_mpeg_buf.readpos = 0; |
| 297 | } |
| 298 | |
| 299 | static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf, |
| 300 | char __user *ubuf, size_t ucount) |
| 301 | { |
| 302 | struct ivtv *itv = s->itv; |
| 303 | size_t len = buf->bytesused - buf->readpos; |
| 304 | |
| 305 | if (len > ucount) len = ucount; |
| 306 | if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG && |
| 307 | itv->vbi.sliced_in->service_set && buf != &itv->vbi.sliced_mpeg_buf) { |
| 308 | const char *start = buf->buf + buf->readpos; |
| 309 | const char *p = start + 1; |
| 310 | const u8 *q; |
| 311 | u8 ch = itv->search_pack_header ? 0xba : 0xe0; |
| 312 | int stuffing, i; |
| 313 | |
| 314 | while (start + len > p && (q = memchr(p, 0, start + len - p))) { |
| 315 | p = q + 1; |
| 316 | if ((char *)q + 15 >= buf->buf + buf->bytesused || |
| 317 | q[1] != 0 || q[2] != 1 || q[3] != ch) { |
| 318 | continue; |
| 319 | } |
| 320 | if (!itv->search_pack_header) { |
| 321 | if ((q[6] & 0xc0) != 0x80) |
| 322 | continue; |
| 323 | if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) || |
| 324 | ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) { |
| 325 | ch = 0xba; |
| 326 | itv->search_pack_header = 1; |
| 327 | p = q + 9; |
| 328 | } |
| 329 | continue; |
| 330 | } |
| 331 | stuffing = q[13] & 7; |
| 332 | /* all stuffing bytes must be 0xff */ |
| 333 | for (i = 0; i < stuffing; i++) |
| 334 | if (q[14 + i] != 0xff) |
| 335 | break; |
| 336 | if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 && |
| 337 | q[14 + stuffing] == 0 && q[15 + stuffing] == 0 && |
| 338 | q[16 + stuffing] == 1) { |
| 339 | itv->search_pack_header = 0; |
| 340 | len = (char *)q - start; |
| 341 | ivtv_setup_sliced_vbi_buf(itv); |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) { |
| 347 | IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name); |
| 348 | return -EFAULT; |
| 349 | } |
| 350 | /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount, |
| 351 | buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len, |
| 352 | buf == &itv->vbi.sliced_mpeg_buf); */ |
| 353 | buf->readpos += len; |
| 354 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf) |
| 355 | itv->mpg_data_received += len; |
| 356 | return len; |
| 357 | } |
| 358 | |
| 359 | static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block) |
| 360 | { |
| 361 | struct ivtv *itv = s->itv; |
| 362 | size_t tot_written = 0; |
| 363 | int single_frame = 0; |
| 364 | |
| 365 | if (atomic_read(&itv->capturing) == 0 && s->id == -1) { |
| 366 | /* shouldn't happen */ |
| 367 | IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name); |
| 368 | return -EIO; |
| 369 | } |
| 370 | |
| 371 | /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should |
| 372 | arrive one-by-one, so make sure we never output more than one VBI frame at a time */ |
| 373 | if (s->type == IVTV_DEC_STREAM_TYPE_VBI || |
| 374 | (s->type == IVTV_ENC_STREAM_TYPE_VBI && itv->vbi.sliced_in->service_set)) |
| 375 | single_frame = 1; |
| 376 | |
| 377 | for (;;) { |
| 378 | struct ivtv_buffer *buf; |
| 379 | int rc; |
| 380 | |
| 381 | buf = ivtv_get_buffer(s, non_block, &rc); |
| 382 | if (buf == NULL && rc == -EAGAIN && tot_written) |
| 383 | break; |
| 384 | if (buf == NULL) |
| 385 | return rc; |
| 386 | rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written); |
| 387 | if (buf != &itv->vbi.sliced_mpeg_buf) { |
| 388 | ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io); |
| 389 | } |
| 390 | else if (buf->readpos == buf->bytesused) { |
| 391 | int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES; |
| 392 | itv->vbi.sliced_mpeg_size[idx] = 0; |
| 393 | itv->vbi.inserted_frame++; |
| 394 | itv->vbi_data_inserted += buf->bytesused; |
| 395 | } |
| 396 | if (rc < 0) |
| 397 | return rc; |
| 398 | tot_written += rc; |
| 399 | |
| 400 | if (tot_written == tot_count || single_frame) |
| 401 | break; |
| 402 | } |
| 403 | return tot_written; |
| 404 | } |
| 405 | |
| 406 | static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count, |
| 407 | loff_t *pos, int non_block) |
| 408 | { |
| 409 | ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0; |
| 410 | struct ivtv *itv = s->itv; |
| 411 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 412 | IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 413 | if (rc > 0) |
| 414 | pos += rc; |
| 415 | return rc; |
| 416 | } |
| 417 | |
| 418 | int ivtv_start_capture(struct ivtv_open_id *id) |
| 419 | { |
| 420 | struct ivtv *itv = id->itv; |
| 421 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 422 | struct ivtv_stream *s_vbi; |
| 423 | |
| 424 | if (s->type == IVTV_ENC_STREAM_TYPE_RAD || |
| 425 | s->type == IVTV_DEC_STREAM_TYPE_MPG || |
| 426 | s->type == IVTV_DEC_STREAM_TYPE_YUV || |
| 427 | s->type == IVTV_DEC_STREAM_TYPE_VOUT) { |
| 428 | /* you cannot read from these stream types. */ |
| 429 | return -EPERM; |
| 430 | } |
| 431 | |
| 432 | /* Try to claim this stream. */ |
| 433 | if (ivtv_claim_stream(id, s->type)) |
| 434 | return -EBUSY; |
| 435 | |
| 436 | /* This stream does not need to start capturing */ |
| 437 | if (s->type == IVTV_DEC_STREAM_TYPE_VBI) { |
| 438 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | /* If capture is already in progress, then we also have to |
| 443 | do nothing extra. */ |
| 444 | if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
| 445 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /* Start VBI capture if required */ |
| 450 | s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; |
| 451 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG && |
| 452 | test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) && |
| 453 | !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) { |
| 454 | /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed |
| 455 | automatically when the MPG stream is claimed. |
| 456 | We only need to start the VBI capturing. */ |
| 457 | if (ivtv_start_v4l2_encode_stream(s_vbi)) { |
| 458 | IVTV_DEBUG_WARN("VBI capture start failed\n"); |
| 459 | |
| 460 | /* Failure, clean up and return an error */ |
| 461 | clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags); |
| 462 | clear_bit(IVTV_F_S_STREAMING, &s->s_flags); |
| 463 | /* also releases the associated VBI stream */ |
| 464 | ivtv_release_stream(s); |
| 465 | return -EIO; |
| 466 | } |
| 467 | IVTV_DEBUG_INFO("VBI insertion started\n"); |
| 468 | } |
| 469 | |
| 470 | /* Tell the card to start capturing */ |
| 471 | if (!ivtv_start_v4l2_encode_stream(s)) { |
| 472 | /* We're done */ |
| 473 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 474 | /* Resume a possibly paused encoder */ |
| 475 | if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags)) |
| 476 | ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* failure, clean up */ |
| 481 | IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name); |
| 482 | |
| 483 | /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released |
| 484 | automatically when the MPG stream is released. |
| 485 | We only need to stop the VBI capturing. */ |
| 486 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG && |
| 487 | test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) { |
| 488 | ivtv_stop_v4l2_encode_stream(s_vbi, 0); |
| 489 | clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags); |
| 490 | } |
| 491 | clear_bit(IVTV_F_S_STREAMING, &s->s_flags); |
| 492 | ivtv_release_stream(s); |
| 493 | return -EIO; |
| 494 | } |
| 495 | |
| 496 | ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos) |
| 497 | { |
| 498 | struct ivtv_open_id *id = filp->private_data; |
| 499 | struct ivtv *itv = id->itv; |
| 500 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 501 | int rc; |
| 502 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 503 | IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 504 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 505 | mutex_lock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 506 | rc = ivtv_start_capture(id); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 507 | mutex_unlock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 508 | if (rc) |
| 509 | return rc; |
| 510 | return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK); |
| 511 | } |
| 512 | |
| 513 | int ivtv_start_decoding(struct ivtv_open_id *id, int speed) |
| 514 | { |
| 515 | struct ivtv *itv = id->itv; |
| 516 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 517 | |
| 518 | if (atomic_read(&itv->decoding) == 0) { |
| 519 | if (ivtv_claim_stream(id, s->type)) { |
| 520 | /* someone else is using this stream already */ |
| 521 | IVTV_DEBUG_WARN("start decode, stream already claimed\n"); |
| 522 | return -EBUSY; |
| 523 | } |
| 524 | ivtv_start_v4l2_decode_stream(s, 0); |
| 525 | } |
| 526 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG) |
| 527 | return ivtv_set_speed(itv, speed); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos) |
| 532 | { |
| 533 | struct ivtv_open_id *id = filp->private_data; |
| 534 | struct ivtv *itv = id->itv; |
| 535 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 536 | struct ivtv_buffer *buf; |
| 537 | struct ivtv_queue q; |
| 538 | int bytes_written = 0; |
| 539 | int mode; |
| 540 | int rc; |
| 541 | DEFINE_WAIT(wait); |
| 542 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 543 | IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 544 | |
| 545 | if (s->type != IVTV_DEC_STREAM_TYPE_MPG && |
| 546 | s->type != IVTV_DEC_STREAM_TYPE_YUV && |
| 547 | s->type != IVTV_DEC_STREAM_TYPE_VOUT) |
| 548 | /* not decoder streams */ |
| 549 | return -EPERM; |
| 550 | |
| 551 | /* Try to claim this stream */ |
| 552 | if (ivtv_claim_stream(id, s->type)) |
| 553 | return -EBUSY; |
| 554 | |
| 555 | /* This stream does not need to start any decoding */ |
| 556 | if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) { |
| 557 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 558 | return ivtv_write_vbi(itv, user_buf, count); |
| 559 | } |
| 560 | |
| 561 | mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV; |
| 562 | |
| 563 | if (ivtv_set_output_mode(itv, mode) != mode) { |
| 564 | ivtv_release_stream(s); |
| 565 | return -EBUSY; |
| 566 | } |
| 567 | ivtv_queue_init(&q); |
| 568 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 569 | |
| 570 | retry: |
| 571 | for (;;) { |
| 572 | /* Gather buffers */ |
| 573 | while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io))) |
| 574 | ivtv_enqueue(s, buf, &q); |
| 575 | while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) { |
| 576 | ivtv_enqueue(s, buf, &q); |
| 577 | } |
| 578 | if (q.buffers) |
| 579 | break; |
| 580 | if (filp->f_flags & O_NONBLOCK) |
| 581 | return -EAGAIN; |
| 582 | prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); |
| 583 | /* New buffers might have become free before we were added to the waitqueue */ |
| 584 | if (!s->q_free.buffers) |
| 585 | schedule(); |
| 586 | finish_wait(&s->waitq, &wait); |
| 587 | if (signal_pending(current)) { |
| 588 | IVTV_DEBUG_INFO("User stopped %s\n", s->name); |
| 589 | return -EINTR; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* copy user data into buffers */ |
| 594 | while ((buf = ivtv_dequeue(s, &q))) { |
| 595 | /* Make sure we really got all the user data */ |
| 596 | rc = ivtv_buf_copy_from_user(s, buf, user_buf, count); |
| 597 | |
| 598 | if (rc < 0) { |
| 599 | ivtv_queue_move(s, &q, NULL, &s->q_free, 0); |
| 600 | return rc; |
| 601 | } |
| 602 | user_buf += rc; |
| 603 | count -= rc; |
| 604 | bytes_written += rc; |
| 605 | |
| 606 | if (buf->bytesused != s->buf_size) { |
| 607 | /* incomplete, leave in q_io for next time */ |
| 608 | ivtv_enqueue(s, buf, &s->q_io); |
| 609 | break; |
| 610 | } |
| 611 | /* Byteswap MPEG buffer */ |
| 612 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG) |
| 613 | ivtv_buf_swap(buf); |
| 614 | ivtv_enqueue(s, buf, &s->q_full); |
| 615 | } |
| 616 | |
| 617 | /* Start decoder (returns 0 if already started) */ |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 618 | mutex_lock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 619 | rc = ivtv_start_decoding(id, itv->speed); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 620 | mutex_unlock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 621 | if (rc) { |
| 622 | IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name); |
| 623 | |
| 624 | /* failure, clean up */ |
| 625 | clear_bit(IVTV_F_S_STREAMING, &s->s_flags); |
| 626 | clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 627 | return rc; |
| 628 | } |
| 629 | if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) { |
| 630 | if (s->q_full.length >= itv->dma_data_req_size) { |
| 631 | int got_sig; |
| 632 | |
| 633 | prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE); |
| 634 | while (!(got_sig = signal_pending(current)) && |
| 635 | test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) { |
| 636 | schedule(); |
| 637 | } |
| 638 | finish_wait(&itv->dma_waitq, &wait); |
| 639 | if (got_sig) { |
| 640 | IVTV_DEBUG_INFO("User interrupted %s\n", s->name); |
| 641 | return -EINTR; |
| 642 | } |
| 643 | |
| 644 | clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags); |
| 645 | ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size); |
| 646 | ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1); |
| 647 | } |
| 648 | } |
| 649 | /* more user data is available, wait until buffers become free |
| 650 | to transfer the rest. */ |
| 651 | if (count && !(filp->f_flags & O_NONBLOCK)) |
| 652 | goto retry; |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 653 | IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 654 | return bytes_written; |
| 655 | } |
| 656 | |
| 657 | unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait) |
| 658 | { |
| 659 | struct ivtv_open_id *id = filp->private_data; |
| 660 | struct ivtv *itv = id->itv; |
| 661 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 662 | int res = 0; |
| 663 | |
| 664 | /* add stream's waitq to the poll list */ |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 665 | IVTV_DEBUG_HI_FILE("Decoder poll\n"); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 666 | poll_wait(filp, &s->waitq, wait); |
| 667 | |
| 668 | set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags); |
| 669 | if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) || |
| 670 | test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags)) |
| 671 | res = POLLPRI; |
| 672 | |
| 673 | /* Allow write if buffers are available for writing */ |
| 674 | if (s->q_free.buffers) |
| 675 | res |= POLLOUT | POLLWRNORM; |
| 676 | return res; |
| 677 | } |
| 678 | |
| 679 | unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait) |
| 680 | { |
| 681 | struct ivtv_open_id *id = filp->private_data; |
| 682 | struct ivtv *itv = id->itv; |
| 683 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 684 | int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 685 | |
| 686 | /* Start a capture if there is none */ |
| 687 | if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 688 | int rc; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 689 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 690 | mutex_lock(&itv->serialize_lock); |
| 691 | rc = ivtv_start_capture(id); |
| 692 | mutex_unlock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 693 | if (rc) { |
| 694 | IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n", |
| 695 | s->name, rc); |
| 696 | return POLLERR; |
| 697 | } |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 698 | IVTV_DEBUG_FILE("Encoder poll started capture\n"); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /* add stream's waitq to the poll list */ |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 702 | IVTV_DEBUG_HI_FILE("Encoder poll\n"); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 703 | poll_wait(filp, &s->waitq, wait); |
| 704 | |
| 705 | if (eof || s->q_full.length) |
| 706 | return POLLIN | POLLRDNORM; |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end) |
| 711 | { |
| 712 | struct ivtv *itv = id->itv; |
| 713 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 714 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 715 | IVTV_DEBUG_FILE("close() of %s\n", s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 716 | |
| 717 | /* 'Unclaim' this stream */ |
| 718 | |
| 719 | /* Stop capturing */ |
| 720 | if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
| 721 | struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; |
| 722 | |
| 723 | IVTV_DEBUG_INFO("close stopping capture\n"); |
| 724 | /* Special case: a running VBI capture for VBI insertion |
| 725 | in the mpeg stream. Need to stop that too. */ |
| 726 | if (id->type == IVTV_ENC_STREAM_TYPE_MPG && |
| 727 | test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) && |
| 728 | !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) { |
| 729 | IVTV_DEBUG_INFO("close stopping embedded VBI capture\n"); |
| 730 | ivtv_stop_v4l2_encode_stream(s_vbi, 0); |
| 731 | } |
| 732 | if ((id->type == IVTV_DEC_STREAM_TYPE_VBI || |
| 733 | id->type == IVTV_ENC_STREAM_TYPE_VBI) && |
| 734 | test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) { |
| 735 | /* Also used internally, don't stop capturing */ |
| 736 | s->id = -1; |
| 737 | } |
| 738 | else { |
| 739 | ivtv_stop_v4l2_encode_stream(s, gop_end); |
| 740 | } |
| 741 | } |
| 742 | clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 743 | clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 744 | |
| 745 | ivtv_release_stream(s); |
| 746 | } |
| 747 | |
Hans Verkuil | 31ec135 | 2007-03-03 08:50:42 -0300 | [diff] [blame] | 748 | static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 749 | { |
| 750 | struct ivtv *itv = id->itv; |
| 751 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 752 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 753 | IVTV_DEBUG_FILE("close() of %s\n", s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 754 | |
| 755 | /* Stop decoding */ |
| 756 | if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
| 757 | IVTV_DEBUG_INFO("close stopping decode\n"); |
| 758 | |
| 759 | ivtv_stop_v4l2_decode_stream(s, flags, pts); |
Hans Verkuil | ad8ff0f | 2007-08-20 16:01:58 -0300 | [diff] [blame^] | 760 | itv->output_mode = OUT_NONE; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 761 | } |
| 762 | clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 763 | clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 764 | if (id->type == IVTV_DEC_STREAM_TYPE_YUV && test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) { |
| 765 | /* Restore registers we've changed & clean up any mess we've made */ |
| 766 | ivtv_yuv_close(itv); |
| 767 | } |
Hans Verkuil | ad8ff0f | 2007-08-20 16:01:58 -0300 | [diff] [blame^] | 768 | if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames) |
Ian Armstrong | cb50f54 | 2007-08-18 15:58:51 -0300 | [diff] [blame] | 769 | itv->output_mode = OUT_NONE; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 770 | |
| 771 | itv->speed = 0; |
Hans Verkuil | ac42514 | 2007-07-22 08:46:38 -0300 | [diff] [blame] | 772 | clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 773 | ivtv_release_stream(s); |
| 774 | } |
| 775 | |
| 776 | int ivtv_v4l2_close(struct inode *inode, struct file *filp) |
| 777 | { |
| 778 | struct ivtv_open_id *id = filp->private_data; |
| 779 | struct ivtv *itv = id->itv; |
| 780 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 781 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 782 | IVTV_DEBUG_FILE("close %s\n", s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 783 | |
Hans Verkuil | d46c17d | 2007-03-10 17:59:15 -0300 | [diff] [blame] | 784 | v4l2_prio_close(&itv->prio, &id->prio); |
| 785 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 786 | /* Easy case first: this stream was never claimed by us */ |
| 787 | if (s->id != id->open_id) { |
| 788 | kfree(id); |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /* 'Unclaim' this stream */ |
| 793 | |
| 794 | /* Stop radio */ |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 795 | mutex_lock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 796 | if (id->type == IVTV_ENC_STREAM_TYPE_RAD) { |
| 797 | /* Closing radio device, return to TV mode */ |
| 798 | ivtv_mute(itv); |
| 799 | /* Mark that the radio is no longer in use */ |
| 800 | clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); |
| 801 | /* Switch tuner to TV */ |
| 802 | ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std); |
| 803 | /* Select correct audio input (i.e. TV tuner or Line in) */ |
| 804 | ivtv_audio_set_io(itv); |
Hans Verkuil | f2100d8 | 2007-05-17 08:08:45 -0300 | [diff] [blame] | 805 | if (itv->hw_flags & IVTV_HW_SAA711X) |
| 806 | { |
| 807 | struct v4l2_crystal_freq crystal_freq; |
| 808 | crystal_freq.freq = SAA7115_FREQ_32_11_MHZ; |
| 809 | crystal_freq.flags = 0; |
| 810 | ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq); |
| 811 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 812 | /* Done! Unmute and continue. */ |
| 813 | ivtv_unmute(itv); |
| 814 | ivtv_release_stream(s); |
| 815 | } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) { |
Hans Verkuil | 5a338c3 | 2007-07-22 09:39:56 -0300 | [diff] [blame] | 816 | struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT]; |
| 817 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 818 | ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0); |
Hans Verkuil | 5a338c3 | 2007-07-22 09:39:56 -0300 | [diff] [blame] | 819 | |
| 820 | /* If all output streams are closed, and if the user doesn't have |
| 821 | IVTV_DEC_STREAM_TYPE_VOUT open, then disable VBI on TV-out. */ |
| 822 | if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) { |
| 823 | /* disable VBI on TV-out */ |
| 824 | ivtv_disable_vbi(itv); |
| 825 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 826 | } else { |
| 827 | ivtv_stop_capture(id, 0); |
| 828 | } |
| 829 | kfree(id); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 830 | mutex_unlock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 834 | static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 835 | { |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 836 | struct ivtv *itv = s->itv; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 837 | struct ivtv_open_id *item; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 838 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 839 | IVTV_DEBUG_FILE("open %s\n", s->name); |
Hans Verkuil | c976bc8 | 2007-07-22 12:52:40 -0300 | [diff] [blame] | 840 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 841 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG && |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 842 | test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags)) |
| 843 | return -EBUSY; |
| 844 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 845 | if (s->type == IVTV_DEC_STREAM_TYPE_YUV && |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 846 | test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags)) |
| 847 | return -EBUSY; |
| 848 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 849 | if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 850 | if (read_reg(0x82c) == 0) { |
| 851 | IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n"); |
| 852 | /* return -ENODEV; */ |
| 853 | } |
| 854 | ivtv_udma_alloc(itv); |
| 855 | } |
| 856 | |
| 857 | /* Allocate memory */ |
| 858 | item = kmalloc(sizeof(struct ivtv_open_id), GFP_KERNEL); |
| 859 | if (NULL == item) { |
| 860 | IVTV_DEBUG_WARN("nomem on v4l2 open\n"); |
| 861 | return -ENOMEM; |
| 862 | } |
| 863 | item->itv = itv; |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 864 | item->type = s->type; |
Hans Verkuil | d46c17d | 2007-03-10 17:59:15 -0300 | [diff] [blame] | 865 | v4l2_prio_open(&itv->prio, &item->prio); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 866 | |
| 867 | item->open_id = itv->open_id++; |
| 868 | filp->private_data = item; |
| 869 | |
| 870 | if (item->type == IVTV_ENC_STREAM_TYPE_RAD) { |
| 871 | /* Try to claim this stream */ |
| 872 | if (ivtv_claim_stream(item, item->type)) { |
| 873 | /* No, it's already in use */ |
| 874 | kfree(item); |
| 875 | return -EBUSY; |
| 876 | } |
| 877 | |
Hans Verkuil | 3562c43 | 2007-08-18 11:46:05 -0300 | [diff] [blame] | 878 | if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) { |
| 879 | if (atomic_read(&itv->capturing) > 0) { |
| 880 | /* switching to radio while capture is |
| 881 | in progress is not polite */ |
| 882 | kfree(item); |
| 883 | return -EBUSY; |
| 884 | } |
| 885 | } |
| 886 | /* Mark that the radio is being used. */ |
| 887 | set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 888 | /* We have the radio */ |
| 889 | ivtv_mute(itv); |
| 890 | /* Switch tuner to radio */ |
| 891 | ivtv_call_i2c_clients(itv, AUDC_SET_RADIO, NULL); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 892 | /* Select the correct audio input (i.e. radio tuner) */ |
| 893 | ivtv_audio_set_io(itv); |
Hans Verkuil | f2100d8 | 2007-05-17 08:08:45 -0300 | [diff] [blame] | 894 | if (itv->hw_flags & IVTV_HW_SAA711X) |
| 895 | { |
| 896 | struct v4l2_crystal_freq crystal_freq; |
| 897 | crystal_freq.freq = SAA7115_FREQ_32_11_MHZ; |
| 898 | crystal_freq.flags = SAA7115_FREQ_FL_APLL; |
| 899 | ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq); |
| 900 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 901 | /* Done! Unmute and continue. */ |
| 902 | ivtv_unmute(itv); |
| 903 | } |
| 904 | |
| 905 | /* YUV or MPG Decoding Mode? */ |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 906 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 907 | clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 908 | else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 909 | set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | int ivtv_v4l2_open(struct inode *inode, struct file *filp) |
| 914 | { |
| 915 | int res, x, y = 0; |
| 916 | struct ivtv *itv = NULL; |
| 917 | struct ivtv_stream *s = NULL; |
| 918 | int minor = iminor(inode); |
| 919 | |
| 920 | /* Find which card this open was on */ |
| 921 | spin_lock(&ivtv_cards_lock); |
| 922 | for (x = 0; itv == NULL && x < ivtv_cards_active; x++) { |
| 923 | /* find out which stream this open was on */ |
| 924 | for (y = 0; y < IVTV_MAX_STREAMS; y++) { |
| 925 | s = &ivtv_cards[x]->streams[y]; |
| 926 | if (s->v4l2dev && s->v4l2dev->minor == minor) { |
| 927 | itv = ivtv_cards[x]; |
| 928 | break; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | spin_unlock(&ivtv_cards_lock); |
| 933 | |
| 934 | if (itv == NULL) { |
| 935 | /* Couldn't find a device registered |
| 936 | on that minor, shouldn't happen! */ |
| 937 | IVTV_WARN("No ivtv device found on minor %d\n", minor); |
| 938 | return -ENXIO; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 939 | } |
| 940 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 941 | mutex_lock(&itv->serialize_lock); |
| 942 | if (ivtv_init_on_first_open(itv)) { |
| 943 | IVTV_ERR("Failed to initialize on minor %d\n", minor); |
| 944 | mutex_unlock(&itv->serialize_lock); |
| 945 | return -ENXIO; |
| 946 | } |
| 947 | res = ivtv_serialized_open(s, filp); |
| 948 | mutex_unlock(&itv->serialize_lock); |
| 949 | return res; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | void ivtv_mute(struct ivtv *itv) |
| 953 | { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 954 | if (atomic_read(&itv->capturing)) |
| 955 | ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 956 | IVTV_DEBUG_INFO("Mute\n"); |
| 957 | } |
| 958 | |
| 959 | void ivtv_unmute(struct ivtv *itv) |
| 960 | { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 961 | if (atomic_read(&itv->capturing)) { |
Hans Verkuil | 3562c43 | 2007-08-18 11:46:05 -0300 | [diff] [blame] | 962 | ivtv_msleep_timeout(100, 0); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 963 | ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12); |
| 964 | ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0); |
| 965 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 966 | IVTV_DEBUG_INFO("Unmute\n"); |
| 967 | } |