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. */ |
Adrian Bunk | 95f73c5 | 2008-07-22 14:20:12 -0300 | [diff] [blame] | 42 | static int ivtv_claim_stream(struct ivtv_open_id *id, int type) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 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 && |
Hans Verkuil | a8b8643 | 2008-10-04 08:05:30 -0300 | [diff] [blame] | 81 | itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 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 | |
Julia Lawall | 168c626 | 2008-04-16 16:13:15 -0300 | [diff] [blame] | 222 | if (time_after(jiffies, |
| 223 | itv->dualwatch_jiffies + |
| 224 | msecs_to_jiffies(1000))) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 225 | itv->dualwatch_jiffies = jiffies; |
| 226 | ivtv_dualwatch(itv); |
| 227 | } |
| 228 | |
| 229 | if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) && |
| 230 | !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) { |
| 231 | while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) { |
| 232 | /* byteswap and process VBI data */ |
| 233 | ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type); |
| 234 | ivtv_enqueue(s_vbi, buf, &s_vbi->q_free); |
| 235 | } |
| 236 | } |
| 237 | buf = &itv->vbi.sliced_mpeg_buf; |
| 238 | if (buf->readpos != buf->bytesused) { |
| 239 | return buf; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /* do we have leftover data? */ |
| 244 | buf = ivtv_dequeue(s, &s->q_io); |
| 245 | if (buf) |
| 246 | return buf; |
| 247 | |
| 248 | /* do we have new data? */ |
| 249 | buf = ivtv_dequeue(s, &s->q_full); |
| 250 | if (buf) { |
Hans Verkuil | f4071b8 | 2007-07-28 12:07:12 -0300 | [diff] [blame] | 251 | if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 252 | return buf; |
Hans Verkuil | f4071b8 | 2007-07-28 12:07:12 -0300 | [diff] [blame] | 253 | buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 254 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG) |
| 255 | /* byteswap MPG data */ |
| 256 | ivtv_buf_swap(buf); |
| 257 | else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) { |
| 258 | /* byteswap and process VBI data */ |
| 259 | ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type); |
| 260 | } |
| 261 | return buf; |
| 262 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 263 | |
| 264 | /* return if end of stream */ |
| 265 | 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] | 266 | IVTV_DEBUG_INFO("EOS %s\n", s->name); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Hans Verkuil | 559e196 | 2007-08-23 17:51:07 -0300 | [diff] [blame] | 270 | /* return if file was opened with O_NONBLOCK */ |
| 271 | if (non_block) { |
| 272 | *err = -EAGAIN; |
| 273 | return NULL; |
| 274 | } |
| 275 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 276 | /* wait for more data to arrive */ |
| 277 | prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); |
| 278 | /* New buffers might have become available before we were added to the waitqueue */ |
| 279 | if (!s->q_full.buffers) |
| 280 | schedule(); |
| 281 | finish_wait(&s->waitq, &wait); |
| 282 | if (signal_pending(current)) { |
| 283 | /* return if a signal was received */ |
| 284 | IVTV_DEBUG_INFO("User stopped %s\n", s->name); |
| 285 | *err = -EINTR; |
| 286 | return NULL; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv) |
| 292 | { |
| 293 | int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES; |
| 294 | |
| 295 | itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx]; |
| 296 | itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx]; |
| 297 | itv->vbi.sliced_mpeg_buf.readpos = 0; |
| 298 | } |
| 299 | |
| 300 | static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf, |
| 301 | char __user *ubuf, size_t ucount) |
| 302 | { |
| 303 | struct ivtv *itv = s->itv; |
| 304 | size_t len = buf->bytesused - buf->readpos; |
| 305 | |
| 306 | if (len > ucount) len = ucount; |
| 307 | if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG && |
Hans Verkuil | a8b8643 | 2008-10-04 08:05:30 -0300 | [diff] [blame] | 308 | !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 309 | const char *start = buf->buf + buf->readpos; |
| 310 | const char *p = start + 1; |
| 311 | const u8 *q; |
| 312 | u8 ch = itv->search_pack_header ? 0xba : 0xe0; |
| 313 | int stuffing, i; |
| 314 | |
| 315 | while (start + len > p && (q = memchr(p, 0, start + len - p))) { |
| 316 | p = q + 1; |
| 317 | if ((char *)q + 15 >= buf->buf + buf->bytesused || |
| 318 | q[1] != 0 || q[2] != 1 || q[3] != ch) { |
| 319 | continue; |
| 320 | } |
| 321 | if (!itv->search_pack_header) { |
| 322 | if ((q[6] & 0xc0) != 0x80) |
| 323 | continue; |
| 324 | if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) || |
| 325 | ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) { |
| 326 | ch = 0xba; |
| 327 | itv->search_pack_header = 1; |
| 328 | p = q + 9; |
| 329 | } |
| 330 | continue; |
| 331 | } |
| 332 | stuffing = q[13] & 7; |
| 333 | /* all stuffing bytes must be 0xff */ |
| 334 | for (i = 0; i < stuffing; i++) |
| 335 | if (q[14 + i] != 0xff) |
| 336 | break; |
| 337 | if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 && |
| 338 | q[14 + stuffing] == 0 && q[15 + stuffing] == 0 && |
| 339 | q[16 + stuffing] == 1) { |
| 340 | itv->search_pack_header = 0; |
| 341 | len = (char *)q - start; |
| 342 | ivtv_setup_sliced_vbi_buf(itv); |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) { |
| 348 | IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name); |
| 349 | return -EFAULT; |
| 350 | } |
| 351 | /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount, |
| 352 | buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len, |
| 353 | buf == &itv->vbi.sliced_mpeg_buf); */ |
| 354 | buf->readpos += len; |
| 355 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf) |
| 356 | itv->mpg_data_received += len; |
| 357 | return len; |
| 358 | } |
| 359 | |
| 360 | static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block) |
| 361 | { |
| 362 | struct ivtv *itv = s->itv; |
| 363 | size_t tot_written = 0; |
| 364 | int single_frame = 0; |
| 365 | |
| 366 | if (atomic_read(&itv->capturing) == 0 && s->id == -1) { |
| 367 | /* shouldn't happen */ |
| 368 | IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name); |
| 369 | return -EIO; |
| 370 | } |
| 371 | |
| 372 | /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should |
| 373 | arrive one-by-one, so make sure we never output more than one VBI frame at a time */ |
| 374 | if (s->type == IVTV_DEC_STREAM_TYPE_VBI || |
Hans Verkuil | a8b8643 | 2008-10-04 08:05:30 -0300 | [diff] [blame] | 375 | (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv))) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 376 | single_frame = 1; |
| 377 | |
| 378 | for (;;) { |
| 379 | struct ivtv_buffer *buf; |
| 380 | int rc; |
| 381 | |
| 382 | buf = ivtv_get_buffer(s, non_block, &rc); |
Hans Verkuil | 559e196 | 2007-08-23 17:51:07 -0300 | [diff] [blame] | 383 | /* if there is no data available... */ |
| 384 | if (buf == NULL) { |
| 385 | /* if we got data, then return that regardless */ |
| 386 | if (tot_written) |
| 387 | break; |
| 388 | /* EOS condition */ |
| 389 | if (rc == 0) { |
| 390 | clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 391 | clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 392 | ivtv_release_stream(s); |
| 393 | } |
| 394 | /* set errno */ |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 395 | return rc; |
Hans Verkuil | 559e196 | 2007-08-23 17:51:07 -0300 | [diff] [blame] | 396 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 397 | rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written); |
| 398 | if (buf != &itv->vbi.sliced_mpeg_buf) { |
| 399 | ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io); |
| 400 | } |
| 401 | else if (buf->readpos == buf->bytesused) { |
| 402 | int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES; |
| 403 | itv->vbi.sliced_mpeg_size[idx] = 0; |
| 404 | itv->vbi.inserted_frame++; |
| 405 | itv->vbi_data_inserted += buf->bytesused; |
| 406 | } |
| 407 | if (rc < 0) |
| 408 | return rc; |
| 409 | tot_written += rc; |
| 410 | |
| 411 | if (tot_written == tot_count || single_frame) |
| 412 | break; |
| 413 | } |
| 414 | return tot_written; |
| 415 | } |
| 416 | |
| 417 | static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count, |
| 418 | loff_t *pos, int non_block) |
| 419 | { |
| 420 | ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0; |
| 421 | struct ivtv *itv = s->itv; |
| 422 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 423 | 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] | 424 | if (rc > 0) |
| 425 | pos += rc; |
| 426 | return rc; |
| 427 | } |
| 428 | |
| 429 | int ivtv_start_capture(struct ivtv_open_id *id) |
| 430 | { |
| 431 | struct ivtv *itv = id->itv; |
| 432 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 433 | struct ivtv_stream *s_vbi; |
| 434 | |
| 435 | if (s->type == IVTV_ENC_STREAM_TYPE_RAD || |
| 436 | s->type == IVTV_DEC_STREAM_TYPE_MPG || |
| 437 | s->type == IVTV_DEC_STREAM_TYPE_YUV || |
| 438 | s->type == IVTV_DEC_STREAM_TYPE_VOUT) { |
| 439 | /* you cannot read from these stream types. */ |
| 440 | return -EPERM; |
| 441 | } |
| 442 | |
| 443 | /* Try to claim this stream. */ |
| 444 | if (ivtv_claim_stream(id, s->type)) |
| 445 | return -EBUSY; |
| 446 | |
| 447 | /* This stream does not need to start capturing */ |
| 448 | if (s->type == IVTV_DEC_STREAM_TYPE_VBI) { |
| 449 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | /* If capture is already in progress, then we also have to |
| 454 | do nothing extra. */ |
| 455 | if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
| 456 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | /* Start VBI capture if required */ |
| 461 | s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; |
| 462 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG && |
| 463 | test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) && |
| 464 | !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) { |
| 465 | /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed |
| 466 | automatically when the MPG stream is claimed. |
| 467 | We only need to start the VBI capturing. */ |
| 468 | if (ivtv_start_v4l2_encode_stream(s_vbi)) { |
| 469 | IVTV_DEBUG_WARN("VBI capture start failed\n"); |
| 470 | |
| 471 | /* Failure, clean up and return an error */ |
| 472 | clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags); |
| 473 | clear_bit(IVTV_F_S_STREAMING, &s->s_flags); |
| 474 | /* also releases the associated VBI stream */ |
| 475 | ivtv_release_stream(s); |
| 476 | return -EIO; |
| 477 | } |
| 478 | IVTV_DEBUG_INFO("VBI insertion started\n"); |
| 479 | } |
| 480 | |
| 481 | /* Tell the card to start capturing */ |
| 482 | if (!ivtv_start_v4l2_encode_stream(s)) { |
| 483 | /* We're done */ |
| 484 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 485 | /* Resume a possibly paused encoder */ |
| 486 | if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags)) |
| 487 | ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1); |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /* failure, clean up */ |
| 492 | IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name); |
| 493 | |
| 494 | /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released |
| 495 | automatically when the MPG stream is released. |
| 496 | We only need to stop the VBI capturing. */ |
| 497 | if (s->type == IVTV_ENC_STREAM_TYPE_MPG && |
| 498 | test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) { |
| 499 | ivtv_stop_v4l2_encode_stream(s_vbi, 0); |
| 500 | clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags); |
| 501 | } |
| 502 | clear_bit(IVTV_F_S_STREAMING, &s->s_flags); |
| 503 | ivtv_release_stream(s); |
| 504 | return -EIO; |
| 505 | } |
| 506 | |
| 507 | ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos) |
| 508 | { |
| 509 | struct ivtv_open_id *id = filp->private_data; |
| 510 | struct ivtv *itv = id->itv; |
| 511 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 512 | int rc; |
| 513 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 514 | 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] | 515 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 516 | mutex_lock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 517 | rc = ivtv_start_capture(id); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 518 | mutex_unlock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 519 | if (rc) |
| 520 | return rc; |
| 521 | return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK); |
| 522 | } |
| 523 | |
| 524 | int ivtv_start_decoding(struct ivtv_open_id *id, int speed) |
| 525 | { |
| 526 | struct ivtv *itv = id->itv; |
| 527 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 528 | |
| 529 | if (atomic_read(&itv->decoding) == 0) { |
| 530 | if (ivtv_claim_stream(id, s->type)) { |
| 531 | /* someone else is using this stream already */ |
| 532 | IVTV_DEBUG_WARN("start decode, stream already claimed\n"); |
| 533 | return -EBUSY; |
| 534 | } |
| 535 | ivtv_start_v4l2_decode_stream(s, 0); |
| 536 | } |
| 537 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG) |
| 538 | return ivtv_set_speed(itv, speed); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos) |
| 543 | { |
| 544 | struct ivtv_open_id *id = filp->private_data; |
| 545 | struct ivtv *itv = id->itv; |
| 546 | struct ivtv_stream *s = &itv->streams[id->type]; |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 547 | struct yuv_playback_info *yi = &itv->yuv_info; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 548 | struct ivtv_buffer *buf; |
| 549 | struct ivtv_queue q; |
| 550 | int bytes_written = 0; |
| 551 | int mode; |
| 552 | int rc; |
| 553 | DEFINE_WAIT(wait); |
| 554 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 555 | 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] | 556 | |
| 557 | if (s->type != IVTV_DEC_STREAM_TYPE_MPG && |
| 558 | s->type != IVTV_DEC_STREAM_TYPE_YUV && |
| 559 | s->type != IVTV_DEC_STREAM_TYPE_VOUT) |
| 560 | /* not decoder streams */ |
| 561 | return -EPERM; |
| 562 | |
| 563 | /* Try to claim this stream */ |
| 564 | if (ivtv_claim_stream(id, s->type)) |
| 565 | return -EBUSY; |
| 566 | |
| 567 | /* This stream does not need to start any decoding */ |
| 568 | if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) { |
Hans Verkuil | 2f3a989 | 2007-08-25 14:11:23 -0300 | [diff] [blame] | 569 | int elems = count / sizeof(struct v4l2_sliced_vbi_data); |
| 570 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 571 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
Hans Verkuil | 2f3a989 | 2007-08-25 14:11:23 -0300 | [diff] [blame] | 572 | ivtv_write_vbi(itv, (const struct v4l2_sliced_vbi_data *)user_buf, elems); |
| 573 | return elems * sizeof(struct v4l2_sliced_vbi_data); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV; |
| 577 | |
| 578 | if (ivtv_set_output_mode(itv, mode) != mode) { |
| 579 | ivtv_release_stream(s); |
| 580 | return -EBUSY; |
| 581 | } |
| 582 | ivtv_queue_init(&q); |
| 583 | set_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 584 | |
Ian Armstrong | 464e9f3 | 2008-06-21 09:25:23 -0300 | [diff] [blame] | 585 | /* Start decoder (returns 0 if already started) */ |
| 586 | mutex_lock(&itv->serialize_lock); |
| 587 | rc = ivtv_start_decoding(id, itv->speed); |
| 588 | mutex_unlock(&itv->serialize_lock); |
| 589 | if (rc) { |
| 590 | IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name); |
| 591 | |
| 592 | /* failure, clean up */ |
| 593 | clear_bit(IVTV_F_S_STREAMING, &s->s_flags); |
| 594 | clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 595 | return rc; |
| 596 | } |
| 597 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 598 | retry: |
Ian Armstrong | 77aded6 | 2007-11-05 14:27:09 -0300 | [diff] [blame] | 599 | /* If possible, just DMA the entire frame - Check the data transfer size |
| 600 | since we may get here before the stream has been fully set-up */ |
| 601 | if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) { |
| 602 | while (count >= itv->dma_data_req_size) { |
Ian Armstrong | 2e66896 | 2008-10-11 06:39:05 -0300 | [diff] [blame] | 603 | rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf); |
| 604 | |
| 605 | if (rc < 0) |
| 606 | return rc; |
| 607 | |
| 608 | bytes_written += itv->dma_data_req_size; |
| 609 | user_buf += itv->dma_data_req_size; |
| 610 | count -= itv->dma_data_req_size; |
Ian Armstrong | 77aded6 | 2007-11-05 14:27:09 -0300 | [diff] [blame] | 611 | } |
| 612 | if (count == 0) { |
| 613 | IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused); |
| 614 | return bytes_written; |
| 615 | } |
| 616 | } |
| 617 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 618 | for (;;) { |
| 619 | /* Gather buffers */ |
| 620 | while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io))) |
| 621 | ivtv_enqueue(s, buf, &q); |
| 622 | while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) { |
| 623 | ivtv_enqueue(s, buf, &q); |
| 624 | } |
| 625 | if (q.buffers) |
| 626 | break; |
| 627 | if (filp->f_flags & O_NONBLOCK) |
| 628 | return -EAGAIN; |
| 629 | prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); |
| 630 | /* New buffers might have become free before we were added to the waitqueue */ |
| 631 | if (!s->q_free.buffers) |
| 632 | schedule(); |
| 633 | finish_wait(&s->waitq, &wait); |
| 634 | if (signal_pending(current)) { |
| 635 | IVTV_DEBUG_INFO("User stopped %s\n", s->name); |
| 636 | return -EINTR; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* copy user data into buffers */ |
| 641 | while ((buf = ivtv_dequeue(s, &q))) { |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 642 | /* yuv is a pain. Don't copy more data than needed for a single |
| 643 | frame, otherwise we lose sync with the incoming stream */ |
| 644 | if (s->type == IVTV_DEC_STREAM_TYPE_YUV && |
| 645 | yi->stream_size + count > itv->dma_data_req_size) |
| 646 | rc = ivtv_buf_copy_from_user(s, buf, user_buf, |
| 647 | itv->dma_data_req_size - yi->stream_size); |
| 648 | else |
| 649 | rc = ivtv_buf_copy_from_user(s, buf, user_buf, count); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 650 | |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 651 | /* Make sure we really got all the user data */ |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 652 | if (rc < 0) { |
| 653 | ivtv_queue_move(s, &q, NULL, &s->q_free, 0); |
| 654 | return rc; |
| 655 | } |
| 656 | user_buf += rc; |
| 657 | count -= rc; |
| 658 | bytes_written += rc; |
| 659 | |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 660 | if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { |
| 661 | yi->stream_size += rc; |
| 662 | /* If we have a complete yuv frame, break loop now */ |
| 663 | if (yi->stream_size == itv->dma_data_req_size) { |
| 664 | ivtv_enqueue(s, buf, &s->q_full); |
| 665 | yi->stream_size = 0; |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 670 | if (buf->bytesused != s->buf_size) { |
| 671 | /* incomplete, leave in q_io for next time */ |
| 672 | ivtv_enqueue(s, buf, &s->q_io); |
| 673 | break; |
| 674 | } |
| 675 | /* Byteswap MPEG buffer */ |
| 676 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG) |
| 677 | ivtv_buf_swap(buf); |
| 678 | ivtv_enqueue(s, buf, &s->q_full); |
| 679 | } |
| 680 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 681 | if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) { |
| 682 | if (s->q_full.length >= itv->dma_data_req_size) { |
| 683 | int got_sig; |
| 684 | |
Ian Armstrong | 77aded6 | 2007-11-05 14:27:09 -0300 | [diff] [blame] | 685 | if (mode == OUT_YUV) |
| 686 | ivtv_yuv_setup_stream_frame(itv); |
| 687 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 688 | prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE); |
| 689 | while (!(got_sig = signal_pending(current)) && |
| 690 | test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) { |
| 691 | schedule(); |
| 692 | } |
| 693 | finish_wait(&itv->dma_waitq, &wait); |
| 694 | if (got_sig) { |
| 695 | IVTV_DEBUG_INFO("User interrupted %s\n", s->name); |
| 696 | return -EINTR; |
| 697 | } |
| 698 | |
| 699 | clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags); |
| 700 | ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size); |
| 701 | ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1); |
| 702 | } |
| 703 | } |
| 704 | /* more user data is available, wait until buffers become free |
| 705 | to transfer the rest. */ |
| 706 | if (count && !(filp->f_flags & O_NONBLOCK)) |
| 707 | goto retry; |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 708 | 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] | 709 | return bytes_written; |
| 710 | } |
| 711 | |
| 712 | unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait) |
| 713 | { |
| 714 | struct ivtv_open_id *id = filp->private_data; |
| 715 | struct ivtv *itv = id->itv; |
| 716 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 717 | int res = 0; |
| 718 | |
| 719 | /* add stream's waitq to the poll list */ |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 720 | IVTV_DEBUG_HI_FILE("Decoder poll\n"); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 721 | poll_wait(filp, &s->waitq, wait); |
| 722 | |
| 723 | set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags); |
| 724 | if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) || |
| 725 | test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags)) |
| 726 | res = POLLPRI; |
| 727 | |
| 728 | /* Allow write if buffers are available for writing */ |
| 729 | if (s->q_free.buffers) |
| 730 | res |= POLLOUT | POLLWRNORM; |
| 731 | return res; |
| 732 | } |
| 733 | |
| 734 | unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait) |
| 735 | { |
| 736 | struct ivtv_open_id *id = filp->private_data; |
| 737 | struct ivtv *itv = id->itv; |
| 738 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 739 | int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 740 | |
| 741 | /* Start a capture if there is none */ |
| 742 | if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 743 | int rc; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 744 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 745 | mutex_lock(&itv->serialize_lock); |
| 746 | rc = ivtv_start_capture(id); |
| 747 | mutex_unlock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 748 | if (rc) { |
| 749 | IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n", |
| 750 | s->name, rc); |
| 751 | return POLLERR; |
| 752 | } |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 753 | IVTV_DEBUG_FILE("Encoder poll started capture\n"); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /* add stream's waitq to the poll list */ |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 757 | IVTV_DEBUG_HI_FILE("Encoder poll\n"); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 758 | poll_wait(filp, &s->waitq, wait); |
| 759 | |
Hans Verkuil | 16928be | 2008-04-28 12:18:00 -0300 | [diff] [blame] | 760 | if (s->q_full.length || s->q_io.length) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 761 | return POLLIN | POLLRDNORM; |
Hans Verkuil | 16928be | 2008-04-28 12:18:00 -0300 | [diff] [blame] | 762 | if (eof) |
| 763 | return POLLHUP; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end) |
| 768 | { |
| 769 | struct ivtv *itv = id->itv; |
| 770 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 771 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 772 | IVTV_DEBUG_FILE("close() of %s\n", s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 773 | |
| 774 | /* 'Unclaim' this stream */ |
| 775 | |
| 776 | /* Stop capturing */ |
| 777 | if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
| 778 | struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; |
| 779 | |
| 780 | IVTV_DEBUG_INFO("close stopping capture\n"); |
| 781 | /* Special case: a running VBI capture for VBI insertion |
| 782 | in the mpeg stream. Need to stop that too. */ |
| 783 | if (id->type == IVTV_ENC_STREAM_TYPE_MPG && |
| 784 | test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) && |
| 785 | !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) { |
| 786 | IVTV_DEBUG_INFO("close stopping embedded VBI capture\n"); |
| 787 | ivtv_stop_v4l2_encode_stream(s_vbi, 0); |
| 788 | } |
| 789 | if ((id->type == IVTV_DEC_STREAM_TYPE_VBI || |
| 790 | id->type == IVTV_ENC_STREAM_TYPE_VBI) && |
| 791 | test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) { |
| 792 | /* Also used internally, don't stop capturing */ |
| 793 | s->id = -1; |
| 794 | } |
| 795 | else { |
| 796 | ivtv_stop_v4l2_encode_stream(s, gop_end); |
| 797 | } |
| 798 | } |
Hans Verkuil | 559e196 | 2007-08-23 17:51:07 -0300 | [diff] [blame] | 799 | if (!gop_end) { |
| 800 | clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 801 | clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 802 | ivtv_release_stream(s); |
| 803 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 804 | } |
| 805 | |
Hans Verkuil | 31ec135 | 2007-03-03 08:50:42 -0300 | [diff] [blame] | 806 | 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] | 807 | { |
| 808 | struct ivtv *itv = id->itv; |
| 809 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 810 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 811 | IVTV_DEBUG_FILE("close() of %s\n", s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 812 | |
| 813 | /* Stop decoding */ |
| 814 | if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { |
| 815 | IVTV_DEBUG_INFO("close stopping decode\n"); |
| 816 | |
| 817 | ivtv_stop_v4l2_decode_stream(s, flags, pts); |
Hans Verkuil | ad8ff0f | 2007-08-20 16:01:58 -0300 | [diff] [blame] | 818 | itv->output_mode = OUT_NONE; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 819 | } |
| 820 | clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); |
| 821 | clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); |
| 822 | if (id->type == IVTV_DEC_STREAM_TYPE_YUV && test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) { |
| 823 | /* Restore registers we've changed & clean up any mess we've made */ |
| 824 | ivtv_yuv_close(itv); |
| 825 | } |
Hans Verkuil | ad8ff0f | 2007-08-20 16:01:58 -0300 | [diff] [blame] | 826 | if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames) |
Ian Armstrong | cb50f54 | 2007-08-18 15:58:51 -0300 | [diff] [blame] | 827 | itv->output_mode = OUT_NONE; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 828 | |
| 829 | itv->speed = 0; |
Hans Verkuil | ac42514 | 2007-07-22 08:46:38 -0300 | [diff] [blame] | 830 | clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 831 | ivtv_release_stream(s); |
| 832 | } |
| 833 | |
| 834 | int ivtv_v4l2_close(struct inode *inode, struct file *filp) |
| 835 | { |
| 836 | struct ivtv_open_id *id = filp->private_data; |
| 837 | struct ivtv *itv = id->itv; |
| 838 | struct ivtv_stream *s = &itv->streams[id->type]; |
| 839 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 840 | IVTV_DEBUG_FILE("close %s\n", s->name); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 841 | |
Hans Verkuil | d46c17d | 2007-03-10 17:59:15 -0300 | [diff] [blame] | 842 | v4l2_prio_close(&itv->prio, &id->prio); |
| 843 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 844 | /* Easy case first: this stream was never claimed by us */ |
| 845 | if (s->id != id->open_id) { |
| 846 | kfree(id); |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | /* 'Unclaim' this stream */ |
| 851 | |
| 852 | /* Stop radio */ |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 853 | mutex_lock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 854 | if (id->type == IVTV_ENC_STREAM_TYPE_RAD) { |
| 855 | /* Closing radio device, return to TV mode */ |
| 856 | ivtv_mute(itv); |
| 857 | /* Mark that the radio is no longer in use */ |
| 858 | clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); |
| 859 | /* Switch tuner to TV */ |
| 860 | ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std); |
| 861 | /* Select correct audio input (i.e. TV tuner or Line in) */ |
| 862 | ivtv_audio_set_io(itv); |
Hans Verkuil | f2100d8 | 2007-05-17 08:08:45 -0300 | [diff] [blame] | 863 | if (itv->hw_flags & IVTV_HW_SAA711X) |
| 864 | { |
| 865 | struct v4l2_crystal_freq crystal_freq; |
| 866 | crystal_freq.freq = SAA7115_FREQ_32_11_MHZ; |
| 867 | crystal_freq.flags = 0; |
| 868 | ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq); |
| 869 | } |
Hans Verkuil | 2f7362e | 2007-10-14 17:27:56 -0300 | [diff] [blame] | 870 | if (atomic_read(&itv->capturing) > 0) { |
| 871 | /* Undo video mute */ |
| 872 | ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1, |
| 873 | itv->params.video_mute | (itv->params.video_mute_yuv << 8)); |
| 874 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 875 | /* Done! Unmute and continue. */ |
| 876 | ivtv_unmute(itv); |
| 877 | ivtv_release_stream(s); |
| 878 | } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) { |
Hans Verkuil | 5a338c3 | 2007-07-22 09:39:56 -0300 | [diff] [blame] | 879 | struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT]; |
| 880 | |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 881 | 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] | 882 | |
| 883 | /* If all output streams are closed, and if the user doesn't have |
Hans Verkuil | 2f3a989 | 2007-08-25 14:11:23 -0300 | [diff] [blame] | 884 | IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */ |
Hans Verkuil | 5a338c3 | 2007-07-22 09:39:56 -0300 | [diff] [blame] | 885 | if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) { |
Hans Verkuil | 2f3a989 | 2007-08-25 14:11:23 -0300 | [diff] [blame] | 886 | /* disable CC on TV-out */ |
| 887 | ivtv_disable_cc(itv); |
Hans Verkuil | 5a338c3 | 2007-07-22 09:39:56 -0300 | [diff] [blame] | 888 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 889 | } else { |
| 890 | ivtv_stop_capture(id, 0); |
| 891 | } |
| 892 | kfree(id); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 893 | mutex_unlock(&itv->serialize_lock); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 897 | static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp) |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 898 | { |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 899 | struct ivtv *itv = s->itv; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 900 | struct ivtv_open_id *item; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 901 | |
Hans Verkuil | 1aa32c2 | 2007-08-19 06:08:58 -0300 | [diff] [blame] | 902 | IVTV_DEBUG_FILE("open %s\n", s->name); |
Hans Verkuil | c976bc8 | 2007-07-22 12:52:40 -0300 | [diff] [blame] | 903 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 904 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG && |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 905 | test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags)) |
| 906 | return -EBUSY; |
| 907 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 908 | if (s->type == IVTV_DEC_STREAM_TYPE_YUV && |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 909 | test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags)) |
| 910 | return -EBUSY; |
| 911 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 912 | if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 913 | if (read_reg(0x82c) == 0) { |
| 914 | IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n"); |
| 915 | /* return -ENODEV; */ |
| 916 | } |
| 917 | ivtv_udma_alloc(itv); |
| 918 | } |
| 919 | |
| 920 | /* Allocate memory */ |
| 921 | item = kmalloc(sizeof(struct ivtv_open_id), GFP_KERNEL); |
| 922 | if (NULL == item) { |
| 923 | IVTV_DEBUG_WARN("nomem on v4l2 open\n"); |
| 924 | return -ENOMEM; |
| 925 | } |
| 926 | item->itv = itv; |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 927 | item->type = s->type; |
Hans Verkuil | d46c17d | 2007-03-10 17:59:15 -0300 | [diff] [blame] | 928 | v4l2_prio_open(&itv->prio, &item->prio); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 929 | |
| 930 | item->open_id = itv->open_id++; |
| 931 | filp->private_data = item; |
| 932 | |
| 933 | if (item->type == IVTV_ENC_STREAM_TYPE_RAD) { |
| 934 | /* Try to claim this stream */ |
| 935 | if (ivtv_claim_stream(item, item->type)) { |
| 936 | /* No, it's already in use */ |
| 937 | kfree(item); |
| 938 | return -EBUSY; |
| 939 | } |
| 940 | |
Hans Verkuil | 3562c43 | 2007-08-18 11:46:05 -0300 | [diff] [blame] | 941 | if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) { |
| 942 | if (atomic_read(&itv->capturing) > 0) { |
| 943 | /* switching to radio while capture is |
| 944 | in progress is not polite */ |
Hans Verkuil | af3420b | 2007-10-12 06:18:30 -0300 | [diff] [blame] | 945 | ivtv_release_stream(s); |
Hans Verkuil | 3562c43 | 2007-08-18 11:46:05 -0300 | [diff] [blame] | 946 | kfree(item); |
| 947 | return -EBUSY; |
| 948 | } |
| 949 | } |
| 950 | /* Mark that the radio is being used. */ |
| 951 | set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 952 | /* We have the radio */ |
| 953 | ivtv_mute(itv); |
| 954 | /* Switch tuner to radio */ |
| 955 | ivtv_call_i2c_clients(itv, AUDC_SET_RADIO, NULL); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 956 | /* Select the correct audio input (i.e. radio tuner) */ |
| 957 | ivtv_audio_set_io(itv); |
Hans Verkuil | f2100d8 | 2007-05-17 08:08:45 -0300 | [diff] [blame] | 958 | if (itv->hw_flags & IVTV_HW_SAA711X) |
| 959 | { |
| 960 | struct v4l2_crystal_freq crystal_freq; |
| 961 | crystal_freq.freq = SAA7115_FREQ_32_11_MHZ; |
| 962 | crystal_freq.flags = SAA7115_FREQ_FL_APLL; |
| 963 | ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq); |
| 964 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 965 | /* Done! Unmute and continue. */ |
| 966 | ivtv_unmute(itv); |
| 967 | } |
| 968 | |
| 969 | /* YUV or MPG Decoding Mode? */ |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 970 | if (s->type == IVTV_DEC_STREAM_TYPE_MPG) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 971 | clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 972 | } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 973 | set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 974 | /* For yuv, we need to know the dma size before we start */ |
| 975 | itv->dma_data_req_size = |
Ian Armstrong | 77aded6 | 2007-11-05 14:27:09 -0300 | [diff] [blame] | 976 | 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31); |
Ian Armstrong | c240ad0 | 2007-10-16 03:21:46 -0300 | [diff] [blame] | 977 | itv->yuv_info.stream_size = 0; |
| 978 | } |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | int ivtv_v4l2_open(struct inode *inode, struct file *filp) |
| 983 | { |
| 984 | int res, x, y = 0; |
| 985 | struct ivtv *itv = NULL; |
| 986 | struct ivtv_stream *s = NULL; |
| 987 | int minor = iminor(inode); |
| 988 | |
| 989 | /* Find which card this open was on */ |
| 990 | spin_lock(&ivtv_cards_lock); |
| 991 | for (x = 0; itv == NULL && x < ivtv_cards_active; x++) { |
Andy Walls | 07c87a83 | 2008-05-12 15:01:27 -0300 | [diff] [blame] | 992 | if (ivtv_cards[x] == NULL) |
| 993 | continue; |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 994 | /* find out which stream this open was on */ |
| 995 | for (y = 0; y < IVTV_MAX_STREAMS; y++) { |
| 996 | s = &ivtv_cards[x]->streams[y]; |
| 997 | if (s->v4l2dev && s->v4l2dev->minor == minor) { |
| 998 | itv = ivtv_cards[x]; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | spin_unlock(&ivtv_cards_lock); |
| 1004 | |
| 1005 | if (itv == NULL) { |
| 1006 | /* Couldn't find a device registered |
| 1007 | on that minor, shouldn't happen! */ |
Adrian Bunk | 7e7f05c | 2007-10-14 14:51:37 -0300 | [diff] [blame] | 1008 | printk(KERN_WARNING "No ivtv device found on minor %d\n", minor); |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 1009 | return -ENXIO; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1010 | } |
| 1011 | |
Hans Verkuil | baa4072 | 2007-08-19 07:10:55 -0300 | [diff] [blame] | 1012 | mutex_lock(&itv->serialize_lock); |
| 1013 | if (ivtv_init_on_first_open(itv)) { |
| 1014 | IVTV_ERR("Failed to initialize on minor %d\n", minor); |
| 1015 | mutex_unlock(&itv->serialize_lock); |
| 1016 | return -ENXIO; |
| 1017 | } |
| 1018 | res = ivtv_serialized_open(s, filp); |
| 1019 | mutex_unlock(&itv->serialize_lock); |
| 1020 | return res; |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | void ivtv_mute(struct ivtv *itv) |
| 1024 | { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1025 | if (atomic_read(&itv->capturing)) |
| 1026 | ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1027 | IVTV_DEBUG_INFO("Mute\n"); |
| 1028 | } |
| 1029 | |
| 1030 | void ivtv_unmute(struct ivtv *itv) |
| 1031 | { |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1032 | if (atomic_read(&itv->capturing)) { |
Hans Verkuil | 3562c43 | 2007-08-18 11:46:05 -0300 | [diff] [blame] | 1033 | ivtv_msleep_timeout(100, 0); |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1034 | ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12); |
| 1035 | ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0); |
| 1036 | } |
Hans Verkuil | 1a0adaf | 2007-04-27 12:31:25 -0300 | [diff] [blame] | 1037 | IVTV_DEBUG_INFO("Unmute\n"); |
| 1038 | } |