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