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