Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1 | /* |
| 2 | * cx18 file operation functions |
| 3 | * |
| 4 | * Derived from ivtv-fileops.c |
| 5 | * |
| 6 | * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> |
Andy Walls | 6afdeaf | 2010-05-23 18:53:35 -0300 | [diff] [blame] | 7 | * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net> |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "cx18-driver.h" |
| 21 | #include "cx18-fileops.h" |
| 22 | #include "cx18-i2c.h" |
| 23 | #include "cx18-queue.h" |
| 24 | #include "cx18-vbi.h" |
| 25 | #include "cx18-audio.h" |
| 26 | #include "cx18-mailbox.h" |
| 27 | #include "cx18-scb.h" |
| 28 | #include "cx18-streams.h" |
| 29 | #include "cx18-controls.h" |
| 30 | #include "cx18-ioctl.h" |
| 31 | #include "cx18-cards.h" |
Hans Verkuil | eaa80c4 | 2015-04-02 08:34:29 -0300 | [diff] [blame] | 32 | #include <media/v4l2-event.h> |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 33 | |
| 34 | /* This function tries to claim the stream for a specific file descriptor. |
| 35 | If no one else is using this stream then the stream is claimed and |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 36 | associated VBI and IDX streams are also automatically claimed. |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 37 | Possible error returns: -EBUSY if someone else has claimed |
| 38 | the stream or 0 on success. */ |
Devin Heitmueller | 8ef22f7 | 2009-11-19 22:52:30 -0300 | [diff] [blame] | 39 | int cx18_claim_stream(struct cx18_open_id *id, int type) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 40 | { |
| 41 | struct cx18 *cx = id->cx; |
| 42 | struct cx18_stream *s = &cx->streams[type]; |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 43 | struct cx18_stream *s_assoc; |
| 44 | |
| 45 | /* Nothing should ever try to directly claim the IDX stream */ |
| 46 | if (type == CX18_ENC_STREAM_TYPE_IDX) { |
Mauro Carvalho Chehab | 6beb138 | 2016-10-18 17:44:03 -0200 | [diff] [blame] | 47 | CX18_WARN("MPEG Index stream cannot be claimed directly, but something tried.\n"); |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 48 | return -EINVAL; |
| 49 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 50 | |
| 51 | if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) { |
| 52 | /* someone already claimed this stream */ |
| 53 | if (s->id == id->open_id) { |
| 54 | /* yes, this file descriptor did. So that's OK. */ |
| 55 | return 0; |
| 56 | } |
| 57 | if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) { |
| 58 | /* VBI is handled already internally, now also assign |
| 59 | the file descriptor to this stream for external |
| 60 | reading of the stream. */ |
| 61 | s->id = id->open_id; |
| 62 | CX18_DEBUG_INFO("Start Read VBI\n"); |
| 63 | return 0; |
| 64 | } |
| 65 | /* someone else is using this stream already */ |
| 66 | CX18_DEBUG_INFO("Stream %d is busy\n", type); |
| 67 | return -EBUSY; |
| 68 | } |
| 69 | s->id = id->open_id; |
| 70 | |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 71 | /* |
| 72 | * CX18_ENC_STREAM_TYPE_MPG needs to claim: |
| 73 | * CX18_ENC_STREAM_TYPE_VBI, if VBI insertion is on for sliced VBI, or |
| 74 | * CX18_ENC_STREAM_TYPE_IDX, if VBI insertion is off for sliced VBI |
| 75 | * (We don't yet fix up MPEG Index entries for our inserted packets). |
| 76 | * |
| 77 | * For all other streams we're done. |
| 78 | */ |
| 79 | if (type != CX18_ENC_STREAM_TYPE_MPG) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 80 | return 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 81 | |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 82 | s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX]; |
| 83 | if (cx->vbi.insert_mpeg && !cx18_raw_vbi(cx)) |
| 84 | s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI]; |
| 85 | else if (!cx18_stream_enabled(s_assoc)) |
| 86 | return 0; |
| 87 | |
| 88 | set_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 89 | |
| 90 | /* mark that it is used internally */ |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 91 | set_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 92 | return 0; |
| 93 | } |
Devin Heitmueller | 8ef22f7 | 2009-11-19 22:52:30 -0300 | [diff] [blame] | 94 | EXPORT_SYMBOL(cx18_claim_stream); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 95 | |
| 96 | /* This function releases a previously claimed stream. It will take into |
| 97 | account associated VBI streams. */ |
Devin Heitmueller | 8ef22f7 | 2009-11-19 22:52:30 -0300 | [diff] [blame] | 98 | void cx18_release_stream(struct cx18_stream *s) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 99 | { |
| 100 | struct cx18 *cx = s->cx; |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 101 | struct cx18_stream *s_assoc; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 102 | |
| 103 | s->id = -1; |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 104 | if (s->type == CX18_ENC_STREAM_TYPE_IDX) { |
| 105 | /* |
| 106 | * The IDX stream is only used internally, and can |
| 107 | * only be indirectly unclaimed by unclaiming the MPG stream. |
| 108 | */ |
| 109 | return; |
| 110 | } |
| 111 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 112 | if (s->type == CX18_ENC_STREAM_TYPE_VBI && |
| 113 | test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) { |
| 114 | /* this stream is still in use internally */ |
| 115 | return; |
| 116 | } |
| 117 | if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) { |
| 118 | CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | cx18_flush_queues(s); |
| 123 | |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 124 | /* |
| 125 | * CX18_ENC_STREAM_TYPE_MPG needs to release the |
| 126 | * CX18_ENC_STREAM_TYPE_VBI and/or CX18_ENC_STREAM_TYPE_IDX streams. |
| 127 | * |
| 128 | * For all other streams we're done. |
| 129 | */ |
| 130 | if (s->type != CX18_ENC_STREAM_TYPE_MPG) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 131 | return; |
| 132 | |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 133 | /* Unclaim the associated MPEG Index stream */ |
| 134 | s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX]; |
| 135 | if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) { |
| 136 | clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags); |
| 137 | cx18_flush_queues(s_assoc); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 138 | } |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 139 | |
| 140 | /* Unclaim the associated VBI stream */ |
| 141 | s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI]; |
| 142 | if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) { |
| 143 | if (s_assoc->id == -1) { |
| 144 | /* |
| 145 | * The VBI stream is not still claimed by a file |
| 146 | * descriptor, so completely unclaim it. |
| 147 | */ |
| 148 | clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags); |
| 149 | cx18_flush_queues(s_assoc); |
| 150 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 151 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 152 | } |
Devin Heitmueller | 8ef22f7 | 2009-11-19 22:52:30 -0300 | [diff] [blame] | 153 | EXPORT_SYMBOL(cx18_release_stream); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 154 | |
| 155 | static void cx18_dualwatch(struct cx18 *cx) |
| 156 | { |
| 157 | struct v4l2_tuner vt; |
Andy Walls | 0d82fe8 | 2009-01-01 19:02:31 -0300 | [diff] [blame] | 158 | u32 new_stereo_mode; |
Andy Walls | 0d82fe8 | 2009-01-01 19:02:31 -0300 | [diff] [blame] | 159 | const u32 dual = 0x0200; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 160 | |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 161 | new_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 162 | memset(&vt, 0, sizeof(vt)); |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 163 | cx18_call_all(cx, tuner, g_tuner, &vt); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 164 | if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && |
| 165 | (vt.rxsubchans & V4L2_TUNER_SUB_LANG2)) |
| 166 | new_stereo_mode = dual; |
| 167 | |
| 168 | if (new_stereo_mode == cx->dualwatch_stereo_mode) |
| 169 | return; |
| 170 | |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 171 | CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n", |
| 172 | cx->dualwatch_stereo_mode, new_stereo_mode); |
| 173 | if (v4l2_ctrl_s_ctrl(cx->cxhdl.audio_mode, new_stereo_mode)) |
| 174 | CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 178 | static struct cx18_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block, |
| 179 | int *err) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 180 | { |
| 181 | struct cx18 *cx = s->cx; |
| 182 | struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI]; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 183 | struct cx18_mdl *mdl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 184 | DEFINE_WAIT(wait); |
| 185 | |
| 186 | *err = 0; |
| 187 | while (1) { |
| 188 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) { |
Andy Walls | 9bff2d6 | 2009-12-31 22:27:28 -0300 | [diff] [blame] | 189 | /* Process pending program updates and VBI data */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 190 | if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) { |
| 191 | cx->dualwatch_jiffies = jiffies; |
| 192 | cx18_dualwatch(cx); |
| 193 | } |
| 194 | if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) && |
| 195 | !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 196 | while ((mdl = cx18_dequeue(s_vbi, |
| 197 | &s_vbi->q_full))) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 198 | /* byteswap and process VBI data */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 199 | cx18_process_vbi_data(cx, mdl, |
Andy Walls | af009cf | 2008-12-12 20:00:29 -0300 | [diff] [blame] | 200 | s_vbi->type); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 201 | cx18_stream_put_mdl_fw(s_vbi, mdl); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 202 | } |
| 203 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 204 | mdl = &cx->vbi.sliced_mpeg_mdl; |
| 205 | if (mdl->readpos != mdl->bytesused) |
| 206 | return mdl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 207 | } |
| 208 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 209 | /* do we have new data? */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 210 | mdl = cx18_dequeue(s, &s->q_full); |
| 211 | if (mdl) { |
| 212 | if (!test_and_clear_bit(CX18_F_M_NEED_SWAP, |
| 213 | &mdl->m_flags)) |
| 214 | return mdl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 215 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) |
| 216 | /* byteswap MPG data */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 217 | cx18_mdl_swap(mdl); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 218 | else { |
| 219 | /* byteswap and process VBI data */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 220 | cx18_process_vbi_data(cx, mdl, s->type); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 221 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 222 | return mdl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* return if end of stream */ |
| 226 | if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) { |
| 227 | CX18_DEBUG_INFO("EOS %s\n", s->name); |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | /* return if file was opened with O_NONBLOCK */ |
| 232 | if (non_block) { |
| 233 | *err = -EAGAIN; |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | /* wait for more data to arrive */ |
| 238 | prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); |
| 239 | /* New buffers might have become available before we were added |
| 240 | to the waitqueue */ |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 241 | if (!atomic_read(&s->q_full.depth)) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 242 | schedule(); |
| 243 | finish_wait(&s->waitq, &wait); |
| 244 | if (signal_pending(current)) { |
| 245 | /* return if a signal was received */ |
| 246 | CX18_DEBUG_INFO("User stopped %s\n", s->name); |
| 247 | *err = -EINTR; |
| 248 | return NULL; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 253 | static void cx18_setup_sliced_vbi_mdl(struct cx18 *cx) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 254 | { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 255 | struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl; |
| 256 | struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 257 | int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES; |
| 258 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 259 | buf->buf = cx->vbi.sliced_mpeg_data[idx]; |
| 260 | buf->bytesused = cx->vbi.sliced_mpeg_size[idx]; |
| 261 | buf->readpos = 0; |
| 262 | |
| 263 | mdl->curr_buf = NULL; |
| 264 | mdl->bytesused = cx->vbi.sliced_mpeg_size[idx]; |
| 265 | mdl->readpos = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static size_t cx18_copy_buf_to_user(struct cx18_stream *s, |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 269 | struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 270 | { |
| 271 | struct cx18 *cx = s->cx; |
| 272 | size_t len = buf->bytesused - buf->readpos; |
| 273 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 274 | *stop = false; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 275 | if (len > ucount) |
| 276 | len = ucount; |
| 277 | if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG && |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 278 | !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 279 | /* |
| 280 | * Try to find a good splice point in the PS, just before |
| 281 | * an MPEG-2 Program Pack start code, and provide only |
| 282 | * up to that point to the user, so it's easy to insert VBI data |
| 283 | * the next time around. |
Andy Walls | 0c62925 | 2009-04-26 16:34:36 -0300 | [diff] [blame] | 284 | * |
| 285 | * This will not work for an MPEG-2 TS and has only been |
| 286 | * verified by analysis to work for an MPEG-2 PS. Helen Buus |
| 287 | * pointed out this works for the CX23416 MPEG-2 DVD compatible |
| 288 | * stream, and research indicates both the MPEG 2 SVCD and DVD |
| 289 | * stream types use an MPEG-2 PS container. |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 290 | */ |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 291 | /* |
| 292 | * An MPEG-2 Program Stream (PS) is a series of |
| 293 | * MPEG-2 Program Packs terminated by an |
| 294 | * MPEG Program End Code after the last Program Pack. |
| 295 | * A Program Pack may hold a PS System Header packet and any |
| 296 | * number of Program Elementary Stream (PES) Packets |
| 297 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 298 | const char *start = buf->buf + buf->readpos; |
| 299 | const char *p = start + 1; |
| 300 | const u8 *q; |
| 301 | u8 ch = cx->search_pack_header ? 0xba : 0xe0; |
| 302 | int stuffing, i; |
| 303 | |
| 304 | while (start + len > p) { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 305 | /* Scan for a 0 to find a potential MPEG-2 start code */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 306 | q = memchr(p, 0, start + len - p); |
| 307 | if (q == NULL) |
| 308 | break; |
| 309 | p = q + 1; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 310 | /* |
| 311 | * Keep looking if not a |
| 312 | * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba |
| 313 | * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0 |
| 314 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 315 | if ((char *)q + 15 >= buf->buf + buf->bytesused || |
| 316 | q[1] != 0 || q[2] != 1 || q[3] != ch) |
| 317 | continue; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 318 | |
| 319 | /* If expecting the primary video PES */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 320 | if (!cx->search_pack_header) { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 321 | /* Continue if it couldn't be a PES packet */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 322 | if ((q[6] & 0xc0) != 0x80) |
| 323 | continue; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 324 | /* Check if a PTS or PTS & DTS follow */ |
| 325 | if (((q[7] & 0xc0) == 0x80 && /* PTS only */ |
| 326 | (q[9] & 0xf0) == 0x20) || /* PTS only */ |
| 327 | ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */ |
| 328 | (q[9] & 0xf0) == 0x30)) { /* DTS follows */ |
| 329 | /* Assume we found the video PES hdr */ |
| 330 | ch = 0xba; /* next want a Program Pack*/ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 331 | cx->search_pack_header = 1; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 332 | p = q + 9; /* Skip this video PES hdr */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 333 | } |
| 334 | continue; |
| 335 | } |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 336 | |
| 337 | /* We may have found a Program Pack start code */ |
| 338 | |
| 339 | /* Get the count of stuffing bytes & verify them */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 340 | stuffing = q[13] & 7; |
| 341 | /* all stuffing bytes must be 0xff */ |
| 342 | for (i = 0; i < stuffing; i++) |
| 343 | if (q[14 + i] != 0xff) |
| 344 | break; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 345 | if (i == stuffing && /* right number of stuffing bytes*/ |
| 346 | (q[4] & 0xc4) == 0x44 && /* marker check */ |
| 347 | (q[12] & 3) == 3 && /* marker check */ |
| 348 | q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 349 | q[15 + stuffing] == 0 && |
| 350 | q[16 + stuffing] == 1) { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 351 | /* We declare we actually found a Program Pack*/ |
| 352 | cx->search_pack_header = 0; /* expect vid PES */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 353 | len = (char *)q - start; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 354 | cx18_setup_sliced_vbi_mdl(cx); |
| 355 | *stop = true; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) { |
| 361 | CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n", |
| 362 | len, s->name); |
| 363 | return -EFAULT; |
| 364 | } |
| 365 | buf->readpos += len; |
| 366 | if (s->type == CX18_ENC_STREAM_TYPE_MPG && |
| 367 | buf != &cx->vbi.sliced_mpeg_buf) |
| 368 | cx->mpg_data_received += len; |
| 369 | return len; |
| 370 | } |
| 371 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 372 | static size_t cx18_copy_mdl_to_user(struct cx18_stream *s, |
| 373 | struct cx18_mdl *mdl, char __user *ubuf, size_t ucount) |
| 374 | { |
| 375 | size_t tot_written = 0; |
| 376 | int rc; |
| 377 | bool stop = false; |
| 378 | |
| 379 | if (mdl->curr_buf == NULL) |
| 380 | mdl->curr_buf = list_first_entry(&mdl->buf_list, |
| 381 | struct cx18_buffer, list); |
| 382 | |
| 383 | if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) { |
| 384 | /* |
| 385 | * For some reason we've exhausted the buffers, but the MDL |
| 386 | * object still said some data was unread. |
| 387 | * Fix that and bail out. |
| 388 | */ |
| 389 | mdl->readpos = mdl->bytesused; |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) { |
| 394 | |
| 395 | if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused) |
| 396 | continue; |
| 397 | |
| 398 | rc = cx18_copy_buf_to_user(s, mdl->curr_buf, ubuf + tot_written, |
| 399 | ucount - tot_written, &stop); |
| 400 | if (rc < 0) |
| 401 | return rc; |
| 402 | mdl->readpos += rc; |
| 403 | tot_written += rc; |
| 404 | |
| 405 | if (stop || /* Forced stopping point for VBI insertion */ |
Mauro Carvalho Chehab | 1679055 | 2019-02-18 14:28:59 -0500 | [diff] [blame] | 406 | tot_written >= ucount || /* Reader request satisfied */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 407 | mdl->curr_buf->readpos < mdl->curr_buf->bytesused || |
| 408 | mdl->readpos >= mdl->bytesused) /* MDL buffers drained */ |
| 409 | break; |
| 410 | } |
| 411 | return tot_written; |
| 412 | } |
| 413 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 414 | static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf, |
| 415 | size_t tot_count, int non_block) |
| 416 | { |
| 417 | struct cx18 *cx = s->cx; |
| 418 | size_t tot_written = 0; |
| 419 | int single_frame = 0; |
| 420 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 421 | if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 422 | /* shouldn't happen */ |
| 423 | CX18_DEBUG_WARN("Stream %s not initialized before read\n", |
| 424 | s->name); |
| 425 | return -EIO; |
| 426 | } |
| 427 | |
| 428 | /* Each VBI buffer is one frame, the v4l2 API says that for VBI the |
| 429 | frames should arrive one-by-one, so make sure we never output more |
| 430 | than one VBI frame at a time */ |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 431 | if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx)) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 432 | single_frame = 1; |
| 433 | |
| 434 | for (;;) { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 435 | struct cx18_mdl *mdl; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 436 | int rc; |
| 437 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 438 | mdl = cx18_get_mdl(s, non_block, &rc); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 439 | /* if there is no data available... */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 440 | if (mdl == NULL) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 441 | /* if we got data, then return that regardless */ |
| 442 | if (tot_written) |
| 443 | break; |
| 444 | /* EOS condition */ |
| 445 | if (rc == 0) { |
| 446 | clear_bit(CX18_F_S_STREAMOFF, &s->s_flags); |
| 447 | clear_bit(CX18_F_S_APPL_IO, &s->s_flags); |
| 448 | cx18_release_stream(s); |
| 449 | } |
| 450 | /* set errno */ |
| 451 | return rc; |
| 452 | } |
| 453 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 454 | rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 455 | tot_count - tot_written); |
| 456 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 457 | if (mdl != &cx->vbi.sliced_mpeg_mdl) { |
| 458 | if (mdl->readpos == mdl->bytesused) |
| 459 | cx18_stream_put_mdl_fw(s, mdl); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 460 | else |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 461 | cx18_push(s, mdl, &s->q_full); |
| 462 | } else if (mdl->readpos == mdl->bytesused) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 463 | int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES; |
| 464 | |
| 465 | cx->vbi.sliced_mpeg_size[idx] = 0; |
| 466 | cx->vbi.inserted_frame++; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 467 | cx->vbi_data_inserted += mdl->bytesused; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 468 | } |
| 469 | if (rc < 0) |
| 470 | return rc; |
| 471 | tot_written += rc; |
| 472 | |
| 473 | if (tot_written == tot_count || single_frame) |
| 474 | break; |
| 475 | } |
| 476 | return tot_written; |
| 477 | } |
| 478 | |
| 479 | static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf, |
| 480 | size_t count, loff_t *pos, int non_block) |
| 481 | { |
| 482 | ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0; |
| 483 | struct cx18 *cx = s->cx; |
| 484 | |
| 485 | CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc); |
| 486 | if (rc > 0) |
Dan Carpenter | 7afb0df | 2019-02-22 01:37:02 -0500 | [diff] [blame] | 487 | *pos += rc; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 488 | return rc; |
| 489 | } |
| 490 | |
| 491 | int cx18_start_capture(struct cx18_open_id *id) |
| 492 | { |
| 493 | struct cx18 *cx = id->cx; |
| 494 | struct cx18_stream *s = &cx->streams[id->type]; |
| 495 | struct cx18_stream *s_vbi; |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 496 | struct cx18_stream *s_idx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 497 | |
| 498 | if (s->type == CX18_ENC_STREAM_TYPE_RAD) { |
| 499 | /* you cannot read from these stream types. */ |
| 500 | return -EPERM; |
| 501 | } |
| 502 | |
| 503 | /* Try to claim this stream. */ |
| 504 | if (cx18_claim_stream(id, s->type)) |
| 505 | return -EBUSY; |
| 506 | |
| 507 | /* If capture is already in progress, then we also have to |
| 508 | do nothing extra. */ |
| 509 | if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) || |
| 510 | test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) { |
| 511 | set_bit(CX18_F_S_APPL_IO, &s->s_flags); |
| 512 | return 0; |
| 513 | } |
| 514 | |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 515 | /* Start associated VBI or IDX stream capture if required */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 516 | s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI]; |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 517 | s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX]; |
| 518 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) { |
| 519 | /* |
| 520 | * The VBI and IDX streams should have been claimed |
| 521 | * automatically, if for internal use, when the MPG stream was |
| 522 | * claimed. We only need to start these streams capturing. |
| 523 | */ |
| 524 | if (test_bit(CX18_F_S_INTERNAL_USE, &s_idx->s_flags) && |
| 525 | !test_and_set_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) { |
| 526 | if (cx18_start_v4l2_encode_stream(s_idx)) { |
| 527 | CX18_DEBUG_WARN("IDX capture start failed\n"); |
| 528 | clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags); |
| 529 | goto start_failed; |
| 530 | } |
| 531 | CX18_DEBUG_INFO("IDX capture started\n"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 532 | } |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 533 | if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) && |
| 534 | !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) { |
| 535 | if (cx18_start_v4l2_encode_stream(s_vbi)) { |
| 536 | CX18_DEBUG_WARN("VBI capture start failed\n"); |
| 537 | clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags); |
| 538 | goto start_failed; |
| 539 | } |
| 540 | CX18_DEBUG_INFO("VBI insertion started\n"); |
| 541 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* Tell the card to start capturing */ |
| 545 | if (!cx18_start_v4l2_encode_stream(s)) { |
| 546 | /* We're done */ |
| 547 | set_bit(CX18_F_S_APPL_IO, &s->s_flags); |
| 548 | /* Resume a possibly paused encoder */ |
| 549 | if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags)) |
| 550 | cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle); |
| 551 | return 0; |
| 552 | } |
| 553 | |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 554 | start_failed: |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 555 | CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name); |
| 556 | |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 557 | /* |
| 558 | * The associated VBI and IDX streams for internal use are released |
| 559 | * automatically when the MPG stream is released. We only need to stop |
| 560 | * the associated stream. |
| 561 | */ |
| 562 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) { |
| 563 | /* Stop the IDX stream which is always for internal use */ |
| 564 | if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) { |
| 565 | cx18_stop_v4l2_encode_stream(s_idx, 0); |
| 566 | clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags); |
| 567 | } |
| 568 | /* Stop the VBI stream, if only running for internal use */ |
| 569 | if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) && |
| 570 | !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) { |
| 571 | cx18_stop_v4l2_encode_stream(s_vbi, 0); |
| 572 | clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags); |
| 573 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 574 | } |
| 575 | clear_bit(CX18_F_S_STREAMING, &s->s_flags); |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 576 | cx18_release_stream(s); /* Also releases associated streams */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 577 | return -EIO; |
| 578 | } |
| 579 | |
| 580 | ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count, |
| 581 | loff_t *pos) |
| 582 | { |
Hans Verkuil | 0b5f265 | 2011-03-12 06:35:33 -0300 | [diff] [blame] | 583 | struct cx18_open_id *id = file2id(filp); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 584 | struct cx18 *cx = id->cx; |
| 585 | struct cx18_stream *s = &cx->streams[id->type]; |
| 586 | int rc; |
| 587 | |
| 588 | CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name); |
| 589 | |
| 590 | mutex_lock(&cx->serialize_lock); |
| 591 | rc = cx18_start_capture(id); |
| 592 | mutex_unlock(&cx->serialize_lock); |
| 593 | if (rc) |
| 594 | return rc; |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 595 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 596 | if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 597 | (id->type == CX18_ENC_STREAM_TYPE_YUV)) { |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 598 | return videobuf_read_stream(&s->vbuf_q, buf, count, pos, 0, |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 599 | filp->f_flags & O_NONBLOCK); |
| 600 | } |
| 601 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 602 | return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK); |
| 603 | } |
| 604 | |
Al Viro | c23e0cb | 2017-07-03 03:02:56 -0400 | [diff] [blame] | 605 | __poll_t cx18_v4l2_enc_poll(struct file *filp, poll_table *wait) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 606 | { |
Al Viro | 0169943 | 2017-07-03 03:14:15 -0400 | [diff] [blame] | 607 | __poll_t req_events = poll_requested_events(wait); |
Hans Verkuil | 0b5f265 | 2011-03-12 06:35:33 -0300 | [diff] [blame] | 608 | struct cx18_open_id *id = file2id(filp); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 609 | struct cx18 *cx = id->cx; |
| 610 | struct cx18_stream *s = &cx->streams[id->type]; |
| 611 | int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags); |
Al Viro | c23e0cb | 2017-07-03 03:02:56 -0400 | [diff] [blame] | 612 | __poll_t res = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 613 | |
| 614 | /* Start a capture if there is none */ |
Hans Verkuil | eaa80c4 | 2015-04-02 08:34:29 -0300 | [diff] [blame] | 615 | if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags) && |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 616 | (req_events & (EPOLLIN | EPOLLRDNORM))) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 617 | int rc; |
| 618 | |
| 619 | mutex_lock(&cx->serialize_lock); |
| 620 | rc = cx18_start_capture(id); |
| 621 | mutex_unlock(&cx->serialize_lock); |
| 622 | if (rc) { |
| 623 | CX18_DEBUG_INFO("Could not start capture for %s (%d)\n", |
| 624 | s->name, rc); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 625 | return EPOLLERR; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 626 | } |
| 627 | CX18_DEBUG_FILE("Encoder poll started capture\n"); |
| 628 | } |
| 629 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 630 | if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 631 | (id->type == CX18_ENC_STREAM_TYPE_YUV)) { |
Al Viro | c23e0cb | 2017-07-03 03:02:56 -0400 | [diff] [blame] | 632 | __poll_t videobuf_poll = videobuf_poll_stream(filp, &s->vbuf_q, wait); |
Hans Verkuil | eaa80c4 | 2015-04-02 08:34:29 -0300 | [diff] [blame] | 633 | |
| 634 | if (v4l2_event_pending(&id->fh)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 635 | res |= EPOLLPRI; |
| 636 | if (eof && videobuf_poll == EPOLLERR) |
| 637 | return res | EPOLLHUP; |
Hans Verkuil | eaa80c4 | 2015-04-02 08:34:29 -0300 | [diff] [blame] | 638 | return res | videobuf_poll; |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 639 | } |
| 640 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 641 | /* add stream's waitq to the poll list */ |
| 642 | CX18_DEBUG_HI_FILE("Encoder poll\n"); |
Hans Verkuil | eaa80c4 | 2015-04-02 08:34:29 -0300 | [diff] [blame] | 643 | if (v4l2_event_pending(&id->fh)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 644 | res |= EPOLLPRI; |
Hans Verkuil | eaa80c4 | 2015-04-02 08:34:29 -0300 | [diff] [blame] | 645 | else |
| 646 | poll_wait(filp, &s->waitq, wait); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 647 | |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 648 | if (atomic_read(&s->q_full.depth)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 649 | return res | EPOLLIN | EPOLLRDNORM; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 650 | if (eof) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 651 | return res | EPOLLHUP; |
Hans Verkuil | eaa80c4 | 2015-04-02 08:34:29 -0300 | [diff] [blame] | 652 | return res; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 653 | } |
| 654 | |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 655 | int cx18_v4l2_mmap(struct file *file, struct vm_area_struct *vma) |
| 656 | { |
| 657 | struct cx18_open_id *id = file->private_data; |
| 658 | struct cx18 *cx = id->cx; |
| 659 | struct cx18_stream *s = &cx->streams[id->type]; |
| 660 | int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags); |
| 661 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 662 | if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 663 | (id->type == CX18_ENC_STREAM_TYPE_YUV)) { |
| 664 | |
| 665 | /* Start a capture if there is none */ |
| 666 | if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) { |
| 667 | int rc; |
| 668 | |
| 669 | mutex_lock(&cx->serialize_lock); |
| 670 | rc = cx18_start_capture(id); |
| 671 | mutex_unlock(&cx->serialize_lock); |
| 672 | if (rc) { |
| 673 | CX18_DEBUG_INFO( |
| 674 | "Could not start capture for %s (%d)\n", |
| 675 | s->name, rc); |
| 676 | return -EINVAL; |
| 677 | } |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 678 | CX18_DEBUG_FILE("Encoder mmap started capture\n"); |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 679 | } |
| 680 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 681 | return videobuf_mmap_mapper(&s->vbuf_q, vma); |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | return -EINVAL; |
| 685 | } |
| 686 | |
Kees Cook | 162e637 | 2017-10-24 11:22:42 -0400 | [diff] [blame] | 687 | void cx18_vb_timeout(struct timer_list *t) |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 688 | { |
Kees Cook | 162e637 | 2017-10-24 11:22:42 -0400 | [diff] [blame] | 689 | struct cx18_stream *s = from_timer(s, t, vb_timeout); |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 690 | struct cx18_videobuf_buffer *buf; |
| 691 | unsigned long flags; |
| 692 | |
| 693 | /* Return all of the buffers in error state, so the vbi/vid inode |
| 694 | * can return from blocking. |
| 695 | */ |
| 696 | spin_lock_irqsave(&s->vb_lock, flags); |
| 697 | while (!list_empty(&s->vb_capture)) { |
| 698 | buf = list_entry(s->vb_capture.next, |
| 699 | struct cx18_videobuf_buffer, vb.queue); |
| 700 | list_del(&buf->vb.queue); |
| 701 | buf->vb.state = VIDEOBUF_ERROR; |
| 702 | wake_up(&buf->vb.done); |
| 703 | } |
| 704 | spin_unlock_irqrestore(&s->vb_lock, flags); |
| 705 | } |
| 706 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 707 | void cx18_stop_capture(struct cx18_open_id *id, int gop_end) |
| 708 | { |
| 709 | struct cx18 *cx = id->cx; |
| 710 | struct cx18_stream *s = &cx->streams[id->type]; |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 711 | struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI]; |
| 712 | struct cx18_stream *s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX]; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 713 | |
| 714 | CX18_DEBUG_IOCTL("close() of %s\n", s->name); |
| 715 | |
| 716 | /* 'Unclaim' this stream */ |
| 717 | |
| 718 | /* Stop capturing */ |
| 719 | if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 720 | CX18_DEBUG_INFO("close stopping capture\n"); |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 721 | if (id->type == CX18_ENC_STREAM_TYPE_MPG) { |
| 722 | /* Stop internal use associated VBI and IDX streams */ |
| 723 | if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) && |
| 724 | !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) { |
Mauro Carvalho Chehab | 6beb138 | 2016-10-18 17:44:03 -0200 | [diff] [blame] | 725 | CX18_DEBUG_INFO("close stopping embedded VBI capture\n"); |
Andy Walls | 79f3e96 | 2009-12-31 17:19:25 -0300 | [diff] [blame] | 726 | cx18_stop_v4l2_encode_stream(s_vbi, 0); |
| 727 | } |
| 728 | if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) { |
| 729 | CX18_DEBUG_INFO("close stopping IDX capture\n"); |
| 730 | cx18_stop_v4l2_encode_stream(s_idx, 0); |
| 731 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 732 | } |
| 733 | if (id->type == CX18_ENC_STREAM_TYPE_VBI && |
| 734 | test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) |
| 735 | /* Also used internally, don't stop capturing */ |
| 736 | s->id = -1; |
| 737 | else |
| 738 | cx18_stop_v4l2_encode_stream(s, gop_end); |
| 739 | } |
| 740 | if (!gop_end) { |
| 741 | clear_bit(CX18_F_S_APPL_IO, &s->s_flags); |
| 742 | clear_bit(CX18_F_S_STREAMOFF, &s->s_flags); |
| 743 | cx18_release_stream(s); |
| 744 | } |
| 745 | } |
| 746 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 747 | int cx18_v4l2_close(struct file *filp) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 748 | { |
Hans Verkuil | 0b5f265 | 2011-03-12 06:35:33 -0300 | [diff] [blame] | 749 | struct v4l2_fh *fh = filp->private_data; |
| 750 | struct cx18_open_id *id = fh2id(fh); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 751 | struct cx18 *cx = id->cx; |
| 752 | struct cx18_stream *s = &cx->streams[id->type]; |
| 753 | |
| 754 | CX18_DEBUG_IOCTL("close() of %s\n", s->name); |
| 755 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 756 | mutex_lock(&cx->serialize_lock); |
Hans Verkuil | 4d68e70 | 2011-10-11 05:23:36 -0300 | [diff] [blame] | 757 | /* Stop radio */ |
| 758 | if (id->type == CX18_ENC_STREAM_TYPE_RAD && |
| 759 | v4l2_fh_is_singular_file(filp)) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 760 | /* Closing radio device, return to TV mode */ |
| 761 | cx18_mute(cx); |
| 762 | /* Mark that the radio is no longer in use */ |
| 763 | clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags); |
| 764 | /* Switch tuner to TV */ |
Laurent Pinchart | 8774bed | 2014-04-28 16:53:01 -0300 | [diff] [blame] | 765 | cx18_call_all(cx, video, s_std, cx->std); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 766 | /* Select correct audio input (i.e. TV tuner or Line in) */ |
| 767 | cx18_audio_set_io(cx); |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 768 | if (atomic_read(&cx->ana_capturing) > 0) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 769 | /* Undo video mute */ |
| 770 | cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle, |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 771 | (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute) | |
| 772 | (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8))); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 773 | } |
| 774 | /* Done! Unmute and continue. */ |
| 775 | cx18_unmute(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 776 | } |
Hans Verkuil | 4d68e70 | 2011-10-11 05:23:36 -0300 | [diff] [blame] | 777 | |
| 778 | v4l2_fh_del(fh); |
| 779 | v4l2_fh_exit(fh); |
| 780 | |
| 781 | /* 'Unclaim' this stream */ |
| 782 | if (s->id == id->open_id) |
| 783 | cx18_stop_capture(id, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 784 | kfree(id); |
| 785 | mutex_unlock(&cx->serialize_lock); |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static int cx18_serialized_open(struct cx18_stream *s, struct file *filp) |
| 790 | { |
| 791 | struct cx18 *cx = s->cx; |
| 792 | struct cx18_open_id *item; |
| 793 | |
| 794 | CX18_DEBUG_FILE("open %s\n", s->name); |
| 795 | |
| 796 | /* Allocate memory */ |
Hans Verkuil | 0b5f265 | 2011-03-12 06:35:33 -0300 | [diff] [blame] | 797 | item = kzalloc(sizeof(struct cx18_open_id), GFP_KERNEL); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 798 | if (NULL == item) { |
| 799 | CX18_DEBUG_WARN("nomem on v4l2 open\n"); |
| 800 | return -ENOMEM; |
| 801 | } |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 802 | v4l2_fh_init(&item->fh, &s->video_dev); |
Hans Verkuil | 0b5f265 | 2011-03-12 06:35:33 -0300 | [diff] [blame] | 803 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 804 | item->cx = cx; |
| 805 | item->type = s->type; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 806 | |
| 807 | item->open_id = cx->open_id++; |
Hans Verkuil | 0b5f265 | 2011-03-12 06:35:33 -0300 | [diff] [blame] | 808 | filp->private_data = &item->fh; |
Hans Verkuil | 4d68e70 | 2011-10-11 05:23:36 -0300 | [diff] [blame] | 809 | v4l2_fh_add(&item->fh); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 810 | |
Hans Verkuil | 4d68e70 | 2011-10-11 05:23:36 -0300 | [diff] [blame] | 811 | if (item->type == CX18_ENC_STREAM_TYPE_RAD && |
| 812 | v4l2_fh_is_singular_file(filp)) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 813 | if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) { |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 814 | if (atomic_read(&cx->ana_capturing) > 0) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 815 | /* switching to radio while capture is |
| 816 | in progress is not polite */ |
Hans Verkuil | 4d68e70 | 2011-10-11 05:23:36 -0300 | [diff] [blame] | 817 | v4l2_fh_del(&item->fh); |
Hans Verkuil | 0b5f265 | 2011-03-12 06:35:33 -0300 | [diff] [blame] | 818 | v4l2_fh_exit(&item->fh); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 819 | kfree(item); |
| 820 | return -EBUSY; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* Mark that the radio is being used. */ |
| 825 | set_bit(CX18_F_I_RADIO_USER, &cx->i_flags); |
| 826 | /* We have the radio */ |
| 827 | cx18_mute(cx); |
| 828 | /* Switch tuner to radio */ |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 829 | cx18_call_all(cx, tuner, s_radio); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 830 | /* Select the correct audio input (i.e. radio tuner) */ |
| 831 | cx18_audio_set_io(cx); |
| 832 | /* Done! Unmute and continue. */ |
| 833 | cx18_unmute(cx); |
| 834 | } |
| 835 | return 0; |
| 836 | } |
| 837 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 838 | int cx18_v4l2_open(struct file *filp) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 839 | { |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 840 | int res; |
| 841 | struct video_device *video_dev = video_devdata(filp); |
| 842 | struct cx18_stream *s = video_get_drvdata(video_dev); |
Joe Perches | 32a6095 | 2009-07-02 13:52:46 -0300 | [diff] [blame] | 843 | struct cx18 *cx = s->cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 844 | |
| 845 | mutex_lock(&cx->serialize_lock); |
| 846 | if (cx18_init_on_first_open(cx)) { |
Laurent Pinchart | 50462eb | 2009-12-10 11:47:13 -0200 | [diff] [blame] | 847 | CX18_ERR("Failed to initialize on %s\n", |
| 848 | video_device_node_name(video_dev)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 849 | mutex_unlock(&cx->serialize_lock); |
| 850 | return -ENXIO; |
| 851 | } |
| 852 | res = cx18_serialized_open(s, filp); |
| 853 | mutex_unlock(&cx->serialize_lock); |
| 854 | return res; |
| 855 | } |
| 856 | |
| 857 | void cx18_mute(struct cx18 *cx) |
| 858 | { |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame] | 859 | u32 h; |
| 860 | if (atomic_read(&cx->ana_capturing)) { |
| 861 | h = cx18_find_handle(cx); |
| 862 | if (h != CX18_INVALID_TASK_HANDLE) |
| 863 | cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1); |
| 864 | else |
| 865 | CX18_ERR("Can't find valid task handle for mute\n"); |
| 866 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 867 | CX18_DEBUG_INFO("Mute\n"); |
| 868 | } |
| 869 | |
| 870 | void cx18_unmute(struct cx18 *cx) |
| 871 | { |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame] | 872 | u32 h; |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 873 | if (atomic_read(&cx->ana_capturing)) { |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame] | 874 | h = cx18_find_handle(cx); |
| 875 | if (h != CX18_INVALID_TASK_HANDLE) { |
| 876 | cx18_msleep_timeout(100, 0); |
| 877 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12); |
| 878 | cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0); |
| 879 | } else |
| 880 | CX18_ERR("Can't find valid task handle for unmute\n"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 881 | } |
| 882 | CX18_DEBUG_INFO("Unmute\n"); |
| 883 | } |