blob: 59e78fb17575d890d92a76ae7927e7594b2f382b [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 file operation functions
3 *
4 * Derived from ivtv-fileops.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
Andy Walls6afdeaf2010-05-23 18:53:35 -03007 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03008 *
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 Verkuil1c1e45d2008-04-28 20:24:33 -030018 */
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 Verkuileaa80c42015-04-02 08:34:29 -030032#include <media/v4l2-event.h>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030033
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 Walls79f3e962009-12-31 17:19:25 -030036 associated VBI and IDX streams are also automatically claimed.
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030037 Possible error returns: -EBUSY if someone else has claimed
38 the stream or 0 on success. */
Devin Heitmueller8ef22f72009-11-19 22:52:30 -030039int cx18_claim_stream(struct cx18_open_id *id, int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030040{
41 struct cx18 *cx = id->cx;
42 struct cx18_stream *s = &cx->streams[type];
Andy Walls79f3e962009-12-31 17:19:25 -030043 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 Chehab6beb1382016-10-18 17:44:03 -020047 CX18_WARN("MPEG Index stream cannot be claimed directly, but something tried.\n");
Andy Walls79f3e962009-12-31 17:19:25 -030048 return -EINVAL;
49 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030050
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 Walls79f3e962009-12-31 17:19:25 -030071 /*
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 Verkuil1c1e45d2008-04-28 20:24:33 -030080 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030081
Andy Walls79f3e962009-12-31 17:19:25 -030082 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 Verkuil1c1e45d2008-04-28 20:24:33 -030089
90 /* mark that it is used internally */
Andy Walls79f3e962009-12-31 17:19:25 -030091 set_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030092 return 0;
93}
Devin Heitmueller8ef22f72009-11-19 22:52:30 -030094EXPORT_SYMBOL(cx18_claim_stream);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030095
96/* This function releases a previously claimed stream. It will take into
97 account associated VBI streams. */
Devin Heitmueller8ef22f72009-11-19 22:52:30 -030098void cx18_release_stream(struct cx18_stream *s)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030099{
100 struct cx18 *cx = s->cx;
Andy Walls79f3e962009-12-31 17:19:25 -0300101 struct cx18_stream *s_assoc;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300102
103 s->id = -1;
Andy Walls79f3e962009-12-31 17:19:25 -0300104 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300112 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 Walls79f3e962009-12-31 17:19:25 -0300124 /*
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 Verkuil1c1e45d2008-04-28 20:24:33 -0300131 return;
132
Andy Walls79f3e962009-12-31 17:19:25 -0300133 /* 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300138 }
Andy Walls79f3e962009-12-31 17:19:25 -0300139
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 Verkuil1c1e45d2008-04-28 20:24:33 -0300151 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300152}
Devin Heitmueller8ef22f72009-11-19 22:52:30 -0300153EXPORT_SYMBOL(cx18_release_stream);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300154
155static void cx18_dualwatch(struct cx18 *cx)
156{
157 struct v4l2_tuner vt;
Andy Walls0d82fe82009-01-01 19:02:31 -0300158 u32 new_stereo_mode;
Andy Walls0d82fe82009-01-01 19:02:31 -0300159 const u32 dual = 0x0200;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300160
Hans Verkuila75b9be2010-12-31 10:22:52 -0300161 new_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300162 memset(&vt, 0, sizeof(vt));
Andy Wallsff2a2002009-02-20 23:52:13 -0300163 cx18_call_all(cx, tuner, g_tuner, &vt);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300164 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 Verkuila75b9be2010-12-31 10:22:52 -0300171 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300175}
176
177
Andy Walls52fcb3e2009-11-08 23:45:24 -0300178static struct cx18_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block,
179 int *err)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300180{
181 struct cx18 *cx = s->cx;
182 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
Andy Walls52fcb3e2009-11-08 23:45:24 -0300183 struct cx18_mdl *mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300184 DEFINE_WAIT(wait);
185
186 *err = 0;
187 while (1) {
188 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
Andy Walls9bff2d62009-12-31 22:27:28 -0300189 /* Process pending program updates and VBI data */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300190 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 Walls52fcb3e2009-11-08 23:45:24 -0300196 while ((mdl = cx18_dequeue(s_vbi,
197 &s_vbi->q_full))) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300198 /* byteswap and process VBI data */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300199 cx18_process_vbi_data(cx, mdl,
Andy Wallsaf009cf2008-12-12 20:00:29 -0300200 s_vbi->type);
Andy Walls52fcb3e2009-11-08 23:45:24 -0300201 cx18_stream_put_mdl_fw(s_vbi, mdl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300202 }
203 }
Andy Walls52fcb3e2009-11-08 23:45:24 -0300204 mdl = &cx->vbi.sliced_mpeg_mdl;
205 if (mdl->readpos != mdl->bytesused)
206 return mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300207 }
208
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300209 /* do we have new data? */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300210 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300215 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
216 /* byteswap MPG data */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300217 cx18_mdl_swap(mdl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300218 else {
219 /* byteswap and process VBI data */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300220 cx18_process_vbi_data(cx, mdl, s->type);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300221 }
Andy Walls52fcb3e2009-11-08 23:45:24 -0300222 return mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300223 }
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 Wallsc37b11b2009-11-04 23:13:58 -0300241 if (!atomic_read(&s->q_full.depth))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300242 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 Walls52fcb3e2009-11-08 23:45:24 -0300253static void cx18_setup_sliced_vbi_mdl(struct cx18 *cx)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300254{
Andy Walls52fcb3e2009-11-08 23:45:24 -0300255 struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl;
256 struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300257 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
258
Andy Walls52fcb3e2009-11-08 23:45:24 -0300259 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300266}
267
268static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
Andy Walls52fcb3e2009-11-08 23:45:24 -0300269 struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300270{
271 struct cx18 *cx = s->cx;
272 size_t len = buf->bytesused - buf->readpos;
273
Andy Walls52fcb3e2009-11-08 23:45:24 -0300274 *stop = false;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300275 if (len > ucount)
276 len = ucount;
277 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
Andy Wallsdd073432008-12-12 16:24:04 -0300278 !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
Andy Walls302df972009-01-31 00:33:02 -0300279 /*
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 Walls0c629252009-04-26 16:34:36 -0300284 *
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 Walls302df972009-01-31 00:33:02 -0300290 */
Andy Walls302df972009-01-31 00:33:02 -0300291 /*
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 Verkuil1c1e45d2008-04-28 20:24:33 -0300298 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 Walls302df972009-01-31 00:33:02 -0300305 /* Scan for a 0 to find a potential MPEG-2 start code */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300306 q = memchr(p, 0, start + len - p);
307 if (q == NULL)
308 break;
309 p = q + 1;
Andy Walls302df972009-01-31 00:33:02 -0300310 /*
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 Verkuil1c1e45d2008-04-28 20:24:33 -0300315 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
316 q[1] != 0 || q[2] != 1 || q[3] != ch)
317 continue;
Andy Walls302df972009-01-31 00:33:02 -0300318
319 /* If expecting the primary video PES */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300320 if (!cx->search_pack_header) {
Andy Walls302df972009-01-31 00:33:02 -0300321 /* Continue if it couldn't be a PES packet */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300322 if ((q[6] & 0xc0) != 0x80)
323 continue;
Andy Walls302df972009-01-31 00:33:02 -0300324 /* 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300331 cx->search_pack_header = 1;
Andy Walls302df972009-01-31 00:33:02 -0300332 p = q + 9; /* Skip this video PES hdr */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300333 }
334 continue;
335 }
Andy Walls302df972009-01-31 00:33:02 -0300336
337 /* We may have found a Program Pack start code */
338
339 /* Get the count of stuffing bytes & verify them */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300340 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 Walls302df972009-01-31 00:33:02 -0300345 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300349 q[15 + stuffing] == 0 &&
350 q[16 + stuffing] == 1) {
Andy Walls302df972009-01-31 00:33:02 -0300351 /* We declare we actually found a Program Pack*/
352 cx->search_pack_header = 0; /* expect vid PES */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300353 len = (char *)q - start;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300354 cx18_setup_sliced_vbi_mdl(cx);
355 *stop = true;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300356 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 Walls52fcb3e2009-11-08 23:45:24 -0300372static 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 Chehab16790552019-02-18 14:28:59 -0500406 tot_written >= ucount || /* Reader request satisfied */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300407 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300414static 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 Verkuil31554ae2008-05-25 11:21:27 -0300421 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300422 /* 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 Wallsdd073432008-12-12 16:24:04 -0300431 if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300432 single_frame = 1;
433
434 for (;;) {
Andy Walls52fcb3e2009-11-08 23:45:24 -0300435 struct cx18_mdl *mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300436 int rc;
437
Andy Walls52fcb3e2009-11-08 23:45:24 -0300438 mdl = cx18_get_mdl(s, non_block, &rc);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300439 /* if there is no data available... */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300440 if (mdl == NULL) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300441 /* 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 Walls52fcb3e2009-11-08 23:45:24 -0300454 rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300455 tot_count - tot_written);
456
Andy Walls52fcb3e2009-11-08 23:45:24 -0300457 if (mdl != &cx->vbi.sliced_mpeg_mdl) {
458 if (mdl->readpos == mdl->bytesused)
459 cx18_stream_put_mdl_fw(s, mdl);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300460 else
Andy Walls52fcb3e2009-11-08 23:45:24 -0300461 cx18_push(s, mdl, &s->q_full);
462 } else if (mdl->readpos == mdl->bytesused) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300463 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
464
465 cx->vbi.sliced_mpeg_size[idx] = 0;
466 cx->vbi.inserted_frame++;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300467 cx->vbi_data_inserted += mdl->bytesused;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300468 }
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
479static 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 Carpenter7afb0df2019-02-22 01:37:02 -0500487 *pos += rc;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300488 return rc;
489}
490
491int 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 Walls79f3e962009-12-31 17:19:25 -0300496 struct cx18_stream *s_idx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300497
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 Walls79f3e962009-12-31 17:19:25 -0300515 /* Start associated VBI or IDX stream capture if required */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300516 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
Andy Walls79f3e962009-12-31 17:19:25 -0300517 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300532 }
Andy Walls79f3e962009-12-31 17:19:25 -0300533 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300542 }
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 Walls79f3e962009-12-31 17:19:25 -0300554start_failed:
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300555 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
556
Andy Walls79f3e962009-12-31 17:19:25 -0300557 /*
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 Verkuil1c1e45d2008-04-28 20:24:33 -0300574 }
575 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
Andy Walls79f3e962009-12-31 17:19:25 -0300576 cx18_release_stream(s); /* Also releases associated streams */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300577 return -EIO;
578}
579
580ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
581 loff_t *pos)
582{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300583 struct cx18_open_id *id = file2id(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300584 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 Tothb7101de2011-04-06 08:32:56 -0300595
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300596 if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
Steven Tothb7101de2011-04-06 08:32:56 -0300597 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300598 return videobuf_read_stream(&s->vbuf_q, buf, count, pos, 0,
Steven Tothb7101de2011-04-06 08:32:56 -0300599 filp->f_flags & O_NONBLOCK);
600 }
601
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300602 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
603}
604
Al Viroc23e0cb2017-07-03 03:02:56 -0400605__poll_t cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300606{
Al Viro01699432017-07-03 03:14:15 -0400607 __poll_t req_events = poll_requested_events(wait);
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300608 struct cx18_open_id *id = file2id(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300609 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 Viroc23e0cb2017-07-03 03:02:56 -0400612 __poll_t res = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300613
614 /* Start a capture if there is none */
Hans Verkuileaa80c42015-04-02 08:34:29 -0300615 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags) &&
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800616 (req_events & (EPOLLIN | EPOLLRDNORM))) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300617 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 Torvaldsa9a08842018-02-11 14:34:03 -0800625 return EPOLLERR;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300626 }
627 CX18_DEBUG_FILE("Encoder poll started capture\n");
628 }
629
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300630 if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
Steven Tothb7101de2011-04-06 08:32:56 -0300631 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
Al Viroc23e0cb2017-07-03 03:02:56 -0400632 __poll_t videobuf_poll = videobuf_poll_stream(filp, &s->vbuf_q, wait);
Hans Verkuileaa80c42015-04-02 08:34:29 -0300633
634 if (v4l2_event_pending(&id->fh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800635 res |= EPOLLPRI;
636 if (eof && videobuf_poll == EPOLLERR)
637 return res | EPOLLHUP;
Hans Verkuileaa80c42015-04-02 08:34:29 -0300638 return res | videobuf_poll;
Steven Tothb7101de2011-04-06 08:32:56 -0300639 }
640
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300641 /* add stream's waitq to the poll list */
642 CX18_DEBUG_HI_FILE("Encoder poll\n");
Hans Verkuileaa80c42015-04-02 08:34:29 -0300643 if (v4l2_event_pending(&id->fh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800644 res |= EPOLLPRI;
Hans Verkuileaa80c42015-04-02 08:34:29 -0300645 else
646 poll_wait(filp, &s->waitq, wait);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300647
Andy Wallsc37b11b2009-11-04 23:13:58 -0300648 if (atomic_read(&s->q_full.depth))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800649 return res | EPOLLIN | EPOLLRDNORM;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300650 if (eof)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800651 return res | EPOLLHUP;
Hans Verkuileaa80c42015-04-02 08:34:29 -0300652 return res;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300653}
654
Steven Tothb7101de2011-04-06 08:32:56 -0300655int 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 Farnsworth1bf58422011-05-03 08:57:40 -0300662 if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
Steven Tothb7101de2011-04-06 08:32:56 -0300663 (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 Farnsworth1bf58422011-05-03 08:57:40 -0300678 CX18_DEBUG_FILE("Encoder mmap started capture\n");
Steven Tothb7101de2011-04-06 08:32:56 -0300679 }
680
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300681 return videobuf_mmap_mapper(&s->vbuf_q, vma);
Steven Tothb7101de2011-04-06 08:32:56 -0300682 }
683
684 return -EINVAL;
685}
686
Kees Cook162e6372017-10-24 11:22:42 -0400687void cx18_vb_timeout(struct timer_list *t)
Steven Tothb7101de2011-04-06 08:32:56 -0300688{
Kees Cook162e6372017-10-24 11:22:42 -0400689 struct cx18_stream *s = from_timer(s, t, vb_timeout);
Steven Tothb7101de2011-04-06 08:32:56 -0300690 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300707void 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 Walls79f3e962009-12-31 17:19:25 -0300711 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300713
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 Verkuil1c1e45d2008-04-28 20:24:33 -0300720 CX18_DEBUG_INFO("close stopping capture\n");
Andy Walls79f3e962009-12-31 17:19:25 -0300721 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 Chehab6beb1382016-10-18 17:44:03 -0200725 CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
Andy Walls79f3e962009-12-31 17:19:25 -0300726 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300732 }
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 Verkuilbec43662008-12-30 06:58:20 -0300747int cx18_v4l2_close(struct file *filp)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300748{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300749 struct v4l2_fh *fh = filp->private_data;
750 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300751 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300756 mutex_lock(&cx->serialize_lock);
Hans Verkuil4d68e702011-10-11 05:23:36 -0300757 /* Stop radio */
758 if (id->type == CX18_ENC_STREAM_TYPE_RAD &&
759 v4l2_fh_is_singular_file(filp)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300760 /* 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 Pinchart8774bed2014-04-28 16:53:01 -0300765 cx18_call_all(cx, video, s_std, cx->std);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300766 /* Select correct audio input (i.e. TV tuner or Line in) */
767 cx18_audio_set_io(cx);
Hans Verkuil31554ae2008-05-25 11:21:27 -0300768 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300769 /* Undo video mute */
770 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
Hans Verkuila75b9be2010-12-31 10:22:52 -0300771 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute) |
772 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8)));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300773 }
774 /* Done! Unmute and continue. */
775 cx18_unmute(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300776 }
Hans Verkuil4d68e702011-10-11 05:23:36 -0300777
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 Verkuil1c1e45d2008-04-28 20:24:33 -0300784 kfree(id);
785 mutex_unlock(&cx->serialize_lock);
786 return 0;
787}
788
789static 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 Verkuil0b5f2652011-03-12 06:35:33 -0300797 item = kzalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300798 if (NULL == item) {
799 CX18_DEBUG_WARN("nomem on v4l2 open\n");
800 return -ENOMEM;
801 }
Hans Verkuil08569d62015-03-09 13:34:03 -0300802 v4l2_fh_init(&item->fh, &s->video_dev);
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300803
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300804 item->cx = cx;
805 item->type = s->type;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300806
807 item->open_id = cx->open_id++;
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300808 filp->private_data = &item->fh;
Hans Verkuil4d68e702011-10-11 05:23:36 -0300809 v4l2_fh_add(&item->fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300810
Hans Verkuil4d68e702011-10-11 05:23:36 -0300811 if (item->type == CX18_ENC_STREAM_TYPE_RAD &&
812 v4l2_fh_is_singular_file(filp)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300813 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
Hans Verkuil31554ae2008-05-25 11:21:27 -0300814 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300815 /* switching to radio while capture is
816 in progress is not polite */
Hans Verkuil4d68e702011-10-11 05:23:36 -0300817 v4l2_fh_del(&item->fh);
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300818 v4l2_fh_exit(&item->fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300819 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 Wallsff2a2002009-02-20 23:52:13 -0300829 cx18_call_all(cx, tuner, s_radio);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300830 /* 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 Verkuilbec43662008-12-30 06:58:20 -0300838int cx18_v4l2_open(struct file *filp)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300839{
Andy Walls5811cf92009-02-14 17:08:37 -0300840 int res;
841 struct video_device *video_dev = video_devdata(filp);
842 struct cx18_stream *s = video_get_drvdata(video_dev);
Joe Perches32a60952009-07-02 13:52:46 -0300843 struct cx18 *cx = s->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300844
845 mutex_lock(&cx->serialize_lock);
846 if (cx18_init_on_first_open(cx)) {
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200847 CX18_ERR("Failed to initialize on %s\n",
848 video_device_node_name(video_dev));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300849 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
857void cx18_mute(struct cx18 *cx)
858{
Andy Wallsd3c5e702008-08-23 16:42:29 -0300859 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300867 CX18_DEBUG_INFO("Mute\n");
868}
869
870void cx18_unmute(struct cx18 *cx)
871{
Andy Wallsd3c5e702008-08-23 16:42:29 -0300872 u32 h;
Hans Verkuil31554ae2008-05-25 11:21:27 -0300873 if (atomic_read(&cx->ana_capturing)) {
Andy Wallsd3c5e702008-08-23 16:42:29 -0300874 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 Verkuil1c1e45d2008-04-28 20:24:33 -0300881 }
882 CX18_DEBUG_INFO("Unmute\n");
883}