blob: c74eafd67f989c7e3f33db713375f26243964ed1 [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.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-fileops.h"
27#include "cx18-i2c.h"
28#include "cx18-queue.h"
29#include "cx18-vbi.h"
30#include "cx18-audio.h"
31#include "cx18-mailbox.h"
32#include "cx18-scb.h"
33#include "cx18-streams.h"
34#include "cx18-controls.h"
35#include "cx18-ioctl.h"
36#include "cx18-cards.h"
37
38/* This function tries to claim the stream for a specific file descriptor.
39 If no one else is using this stream then the stream is claimed and
Andy Walls79f3e962009-12-31 17:19:25 -030040 associated VBI and IDX streams are also automatically claimed.
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030041 Possible error returns: -EBUSY if someone else has claimed
42 the stream or 0 on success. */
Devin Heitmueller8ef22f72009-11-19 22:52:30 -030043int cx18_claim_stream(struct cx18_open_id *id, int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030044{
45 struct cx18 *cx = id->cx;
46 struct cx18_stream *s = &cx->streams[type];
Andy Walls79f3e962009-12-31 17:19:25 -030047 struct cx18_stream *s_assoc;
48
49 /* Nothing should ever try to directly claim the IDX stream */
50 if (type == CX18_ENC_STREAM_TYPE_IDX) {
51 CX18_WARN("MPEG Index stream cannot be claimed "
52 "directly, but something tried.\n");
53 return -EINVAL;
54 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030055
56 if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
57 /* someone already claimed this stream */
58 if (s->id == id->open_id) {
59 /* yes, this file descriptor did. So that's OK. */
60 return 0;
61 }
62 if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
63 /* VBI is handled already internally, now also assign
64 the file descriptor to this stream for external
65 reading of the stream. */
66 s->id = id->open_id;
67 CX18_DEBUG_INFO("Start Read VBI\n");
68 return 0;
69 }
70 /* someone else is using this stream already */
71 CX18_DEBUG_INFO("Stream %d is busy\n", type);
72 return -EBUSY;
73 }
74 s->id = id->open_id;
75
Andy Walls79f3e962009-12-31 17:19:25 -030076 /*
77 * CX18_ENC_STREAM_TYPE_MPG needs to claim:
78 * CX18_ENC_STREAM_TYPE_VBI, if VBI insertion is on for sliced VBI, or
79 * CX18_ENC_STREAM_TYPE_IDX, if VBI insertion is off for sliced VBI
80 * (We don't yet fix up MPEG Index entries for our inserted packets).
81 *
82 * For all other streams we're done.
83 */
84 if (type != CX18_ENC_STREAM_TYPE_MPG)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030085 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030086
Andy Walls79f3e962009-12-31 17:19:25 -030087 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
88 if (cx->vbi.insert_mpeg && !cx18_raw_vbi(cx))
89 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
90 else if (!cx18_stream_enabled(s_assoc))
91 return 0;
92
93 set_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030094
95 /* mark that it is used internally */
Andy Walls79f3e962009-12-31 17:19:25 -030096 set_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030097 return 0;
98}
Devin Heitmueller8ef22f72009-11-19 22:52:30 -030099EXPORT_SYMBOL(cx18_claim_stream);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300100
101/* This function releases a previously claimed stream. It will take into
102 account associated VBI streams. */
Devin Heitmueller8ef22f72009-11-19 22:52:30 -0300103void cx18_release_stream(struct cx18_stream *s)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300104{
105 struct cx18 *cx = s->cx;
Andy Walls79f3e962009-12-31 17:19:25 -0300106 struct cx18_stream *s_assoc;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300107
108 s->id = -1;
Andy Walls79f3e962009-12-31 17:19:25 -0300109 if (s->type == CX18_ENC_STREAM_TYPE_IDX) {
110 /*
111 * The IDX stream is only used internally, and can
112 * only be indirectly unclaimed by unclaiming the MPG stream.
113 */
114 return;
115 }
116
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300117 if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
118 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
119 /* this stream is still in use internally */
120 return;
121 }
122 if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
123 CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
124 return;
125 }
126
127 cx18_flush_queues(s);
128
Andy Walls79f3e962009-12-31 17:19:25 -0300129 /*
130 * CX18_ENC_STREAM_TYPE_MPG needs to release the
131 * CX18_ENC_STREAM_TYPE_VBI and/or CX18_ENC_STREAM_TYPE_IDX streams.
132 *
133 * For all other streams we're done.
134 */
135 if (s->type != CX18_ENC_STREAM_TYPE_MPG)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300136 return;
137
Andy Walls79f3e962009-12-31 17:19:25 -0300138 /* Unclaim the associated MPEG Index stream */
139 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
140 if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
141 clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
142 cx18_flush_queues(s_assoc);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300143 }
Andy Walls79f3e962009-12-31 17:19:25 -0300144
145 /* Unclaim the associated VBI stream */
146 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
147 if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
148 if (s_assoc->id == -1) {
149 /*
150 * The VBI stream is not still claimed by a file
151 * descriptor, so completely unclaim it.
152 */
153 clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
154 cx18_flush_queues(s_assoc);
155 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300156 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300157}
Devin Heitmueller8ef22f72009-11-19 22:52:30 -0300158EXPORT_SYMBOL(cx18_release_stream);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300159
160static void cx18_dualwatch(struct cx18 *cx)
161{
162 struct v4l2_tuner vt;
Andy Walls0d82fe82009-01-01 19:02:31 -0300163 u32 new_stereo_mode;
Andy Walls0d82fe82009-01-01 19:02:31 -0300164 const u32 dual = 0x0200;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300165
Hans Verkuila75b9be2010-12-31 10:22:52 -0300166 new_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300167 memset(&vt, 0, sizeof(vt));
Andy Wallsff2a2002009-02-20 23:52:13 -0300168 cx18_call_all(cx, tuner, g_tuner, &vt);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300169 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
170 (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
171 new_stereo_mode = dual;
172
173 if (new_stereo_mode == cx->dualwatch_stereo_mode)
174 return;
175
Hans Verkuila75b9be2010-12-31 10:22:52 -0300176 CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
177 cx->dualwatch_stereo_mode, new_stereo_mode);
178 if (v4l2_ctrl_s_ctrl(cx->cxhdl.audio_mode, new_stereo_mode))
179 CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300180}
181
182
Andy Walls52fcb3e2009-11-08 23:45:24 -0300183static struct cx18_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block,
184 int *err)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300185{
186 struct cx18 *cx = s->cx;
187 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
Andy Walls52fcb3e2009-11-08 23:45:24 -0300188 struct cx18_mdl *mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300189 DEFINE_WAIT(wait);
190
191 *err = 0;
192 while (1) {
193 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
Andy Walls9bff2d62009-12-31 22:27:28 -0300194 /* Process pending program updates and VBI data */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300195 if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
196 cx->dualwatch_jiffies = jiffies;
197 cx18_dualwatch(cx);
198 }
199 if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
200 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
Andy Walls52fcb3e2009-11-08 23:45:24 -0300201 while ((mdl = cx18_dequeue(s_vbi,
202 &s_vbi->q_full))) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300203 /* byteswap and process VBI data */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300204 cx18_process_vbi_data(cx, mdl,
Andy Wallsaf009cf2008-12-12 20:00:29 -0300205 s_vbi->type);
Andy Walls52fcb3e2009-11-08 23:45:24 -0300206 cx18_stream_put_mdl_fw(s_vbi, mdl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300207 }
208 }
Andy Walls52fcb3e2009-11-08 23:45:24 -0300209 mdl = &cx->vbi.sliced_mpeg_mdl;
210 if (mdl->readpos != mdl->bytesused)
211 return mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300212 }
213
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300214 /* do we have new data? */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300215 mdl = cx18_dequeue(s, &s->q_full);
216 if (mdl) {
217 if (!test_and_clear_bit(CX18_F_M_NEED_SWAP,
218 &mdl->m_flags))
219 return mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300220 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
221 /* byteswap MPG data */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300222 cx18_mdl_swap(mdl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300223 else {
224 /* byteswap and process VBI data */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300225 cx18_process_vbi_data(cx, mdl, s->type);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300226 }
Andy Walls52fcb3e2009-11-08 23:45:24 -0300227 return mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300228 }
229
230 /* return if end of stream */
231 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
232 CX18_DEBUG_INFO("EOS %s\n", s->name);
233 return NULL;
234 }
235
236 /* return if file was opened with O_NONBLOCK */
237 if (non_block) {
238 *err = -EAGAIN;
239 return NULL;
240 }
241
242 /* wait for more data to arrive */
243 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
244 /* New buffers might have become available before we were added
245 to the waitqueue */
Andy Wallsc37b11b2009-11-04 23:13:58 -0300246 if (!atomic_read(&s->q_full.depth))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300247 schedule();
248 finish_wait(&s->waitq, &wait);
249 if (signal_pending(current)) {
250 /* return if a signal was received */
251 CX18_DEBUG_INFO("User stopped %s\n", s->name);
252 *err = -EINTR;
253 return NULL;
254 }
255 }
256}
257
Andy Walls52fcb3e2009-11-08 23:45:24 -0300258static void cx18_setup_sliced_vbi_mdl(struct cx18 *cx)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300259{
Andy Walls52fcb3e2009-11-08 23:45:24 -0300260 struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl;
261 struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300262 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
263
Andy Walls52fcb3e2009-11-08 23:45:24 -0300264 buf->buf = cx->vbi.sliced_mpeg_data[idx];
265 buf->bytesused = cx->vbi.sliced_mpeg_size[idx];
266 buf->readpos = 0;
267
268 mdl->curr_buf = NULL;
269 mdl->bytesused = cx->vbi.sliced_mpeg_size[idx];
270 mdl->readpos = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300271}
272
273static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
Andy Walls52fcb3e2009-11-08 23:45:24 -0300274 struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300275{
276 struct cx18 *cx = s->cx;
277 size_t len = buf->bytesused - buf->readpos;
278
Andy Walls52fcb3e2009-11-08 23:45:24 -0300279 *stop = false;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300280 if (len > ucount)
281 len = ucount;
282 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
Andy Wallsdd073432008-12-12 16:24:04 -0300283 !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
Andy Walls302df972009-01-31 00:33:02 -0300284 /*
285 * Try to find a good splice point in the PS, just before
286 * an MPEG-2 Program Pack start code, and provide only
287 * up to that point to the user, so it's easy to insert VBI data
288 * the next time around.
Andy Walls0c629252009-04-26 16:34:36 -0300289 *
290 * This will not work for an MPEG-2 TS and has only been
291 * verified by analysis to work for an MPEG-2 PS. Helen Buus
292 * pointed out this works for the CX23416 MPEG-2 DVD compatible
293 * stream, and research indicates both the MPEG 2 SVCD and DVD
294 * stream types use an MPEG-2 PS container.
Andy Walls302df972009-01-31 00:33:02 -0300295 */
Andy Walls302df972009-01-31 00:33:02 -0300296 /*
297 * An MPEG-2 Program Stream (PS) is a series of
298 * MPEG-2 Program Packs terminated by an
299 * MPEG Program End Code after the last Program Pack.
300 * A Program Pack may hold a PS System Header packet and any
301 * number of Program Elementary Stream (PES) Packets
302 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300303 const char *start = buf->buf + buf->readpos;
304 const char *p = start + 1;
305 const u8 *q;
306 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
307 int stuffing, i;
308
309 while (start + len > p) {
Andy Walls302df972009-01-31 00:33:02 -0300310 /* Scan for a 0 to find a potential MPEG-2 start code */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300311 q = memchr(p, 0, start + len - p);
312 if (q == NULL)
313 break;
314 p = q + 1;
Andy Walls302df972009-01-31 00:33:02 -0300315 /*
316 * Keep looking if not a
317 * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
318 * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
319 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300320 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
321 q[1] != 0 || q[2] != 1 || q[3] != ch)
322 continue;
Andy Walls302df972009-01-31 00:33:02 -0300323
324 /* If expecting the primary video PES */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300325 if (!cx->search_pack_header) {
Andy Walls302df972009-01-31 00:33:02 -0300326 /* Continue if it couldn't be a PES packet */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300327 if ((q[6] & 0xc0) != 0x80)
328 continue;
Andy Walls302df972009-01-31 00:33:02 -0300329 /* Check if a PTS or PTS & DTS follow */
330 if (((q[7] & 0xc0) == 0x80 && /* PTS only */
331 (q[9] & 0xf0) == 0x20) || /* PTS only */
332 ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
333 (q[9] & 0xf0) == 0x30)) { /* DTS follows */
334 /* Assume we found the video PES hdr */
335 ch = 0xba; /* next want a Program Pack*/
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300336 cx->search_pack_header = 1;
Andy Walls302df972009-01-31 00:33:02 -0300337 p = q + 9; /* Skip this video PES hdr */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300338 }
339 continue;
340 }
Andy Walls302df972009-01-31 00:33:02 -0300341
342 /* We may have found a Program Pack start code */
343
344 /* Get the count of stuffing bytes & verify them */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300345 stuffing = q[13] & 7;
346 /* all stuffing bytes must be 0xff */
347 for (i = 0; i < stuffing; i++)
348 if (q[14 + i] != 0xff)
349 break;
Andy Walls302df972009-01-31 00:33:02 -0300350 if (i == stuffing && /* right number of stuffing bytes*/
351 (q[4] & 0xc4) == 0x44 && /* marker check */
352 (q[12] & 3) == 3 && /* marker check */
353 q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300354 q[15 + stuffing] == 0 &&
355 q[16 + stuffing] == 1) {
Andy Walls302df972009-01-31 00:33:02 -0300356 /* We declare we actually found a Program Pack*/
357 cx->search_pack_header = 0; /* expect vid PES */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300358 len = (char *)q - start;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300359 cx18_setup_sliced_vbi_mdl(cx);
360 *stop = true;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300361 break;
362 }
363 }
364 }
365 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
366 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
367 len, s->name);
368 return -EFAULT;
369 }
370 buf->readpos += len;
371 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
372 buf != &cx->vbi.sliced_mpeg_buf)
373 cx->mpg_data_received += len;
374 return len;
375}
376
Andy Walls52fcb3e2009-11-08 23:45:24 -0300377static size_t cx18_copy_mdl_to_user(struct cx18_stream *s,
378 struct cx18_mdl *mdl, char __user *ubuf, size_t ucount)
379{
380 size_t tot_written = 0;
381 int rc;
382 bool stop = false;
383
384 if (mdl->curr_buf == NULL)
385 mdl->curr_buf = list_first_entry(&mdl->buf_list,
386 struct cx18_buffer, list);
387
388 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
389 /*
390 * For some reason we've exhausted the buffers, but the MDL
391 * object still said some data was unread.
392 * Fix that and bail out.
393 */
394 mdl->readpos = mdl->bytesused;
395 return 0;
396 }
397
398 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
399
400 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
401 continue;
402
403 rc = cx18_copy_buf_to_user(s, mdl->curr_buf, ubuf + tot_written,
404 ucount - tot_written, &stop);
405 if (rc < 0)
406 return rc;
407 mdl->readpos += rc;
408 tot_written += rc;
409
410 if (stop || /* Forced stopping point for VBI insertion */
411 tot_written >= ucount || /* Reader request statisfied */
412 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
413 mdl->readpos >= mdl->bytesused) /* MDL buffers drained */
414 break;
415 }
416 return tot_written;
417}
418
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300419static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
420 size_t tot_count, int non_block)
421{
422 struct cx18 *cx = s->cx;
423 size_t tot_written = 0;
424 int single_frame = 0;
425
Hans Verkuil31554ae2008-05-25 11:21:27 -0300426 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300427 /* shouldn't happen */
428 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
429 s->name);
430 return -EIO;
431 }
432
433 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
434 frames should arrive one-by-one, so make sure we never output more
435 than one VBI frame at a time */
Andy Wallsdd073432008-12-12 16:24:04 -0300436 if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300437 single_frame = 1;
438
439 for (;;) {
Andy Walls52fcb3e2009-11-08 23:45:24 -0300440 struct cx18_mdl *mdl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300441 int rc;
442
Andy Walls52fcb3e2009-11-08 23:45:24 -0300443 mdl = cx18_get_mdl(s, non_block, &rc);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300444 /* if there is no data available... */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300445 if (mdl == NULL) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300446 /* if we got data, then return that regardless */
447 if (tot_written)
448 break;
449 /* EOS condition */
450 if (rc == 0) {
451 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
452 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
453 cx18_release_stream(s);
454 }
455 /* set errno */
456 return rc;
457 }
458
Andy Walls52fcb3e2009-11-08 23:45:24 -0300459 rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300460 tot_count - tot_written);
461
Andy Walls52fcb3e2009-11-08 23:45:24 -0300462 if (mdl != &cx->vbi.sliced_mpeg_mdl) {
463 if (mdl->readpos == mdl->bytesused)
464 cx18_stream_put_mdl_fw(s, mdl);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300465 else
Andy Walls52fcb3e2009-11-08 23:45:24 -0300466 cx18_push(s, mdl, &s->q_full);
467 } else if (mdl->readpos == mdl->bytesused) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300468 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
469
470 cx->vbi.sliced_mpeg_size[idx] = 0;
471 cx->vbi.inserted_frame++;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300472 cx->vbi_data_inserted += mdl->bytesused;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300473 }
474 if (rc < 0)
475 return rc;
476 tot_written += rc;
477
478 if (tot_written == tot_count || single_frame)
479 break;
480 }
481 return tot_written;
482}
483
484static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
485 size_t count, loff_t *pos, int non_block)
486{
487 ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
488 struct cx18 *cx = s->cx;
489
490 CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
491 if (rc > 0)
492 pos += rc;
493 return rc;
494}
495
496int cx18_start_capture(struct cx18_open_id *id)
497{
498 struct cx18 *cx = id->cx;
499 struct cx18_stream *s = &cx->streams[id->type];
500 struct cx18_stream *s_vbi;
Andy Walls79f3e962009-12-31 17:19:25 -0300501 struct cx18_stream *s_idx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300502
503 if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
504 /* you cannot read from these stream types. */
505 return -EPERM;
506 }
507
508 /* Try to claim this stream. */
509 if (cx18_claim_stream(id, s->type))
510 return -EBUSY;
511
512 /* If capture is already in progress, then we also have to
513 do nothing extra. */
514 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
515 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
516 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
517 return 0;
518 }
519
Andy Walls79f3e962009-12-31 17:19:25 -0300520 /* Start associated VBI or IDX stream capture if required */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300521 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
Andy Walls79f3e962009-12-31 17:19:25 -0300522 s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
523 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
524 /*
525 * The VBI and IDX streams should have been claimed
526 * automatically, if for internal use, when the MPG stream was
527 * claimed. We only need to start these streams capturing.
528 */
529 if (test_bit(CX18_F_S_INTERNAL_USE, &s_idx->s_flags) &&
530 !test_and_set_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
531 if (cx18_start_v4l2_encode_stream(s_idx)) {
532 CX18_DEBUG_WARN("IDX capture start failed\n");
533 clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
534 goto start_failed;
535 }
536 CX18_DEBUG_INFO("IDX capture started\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300537 }
Andy Walls79f3e962009-12-31 17:19:25 -0300538 if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
539 !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
540 if (cx18_start_v4l2_encode_stream(s_vbi)) {
541 CX18_DEBUG_WARN("VBI capture start failed\n");
542 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
543 goto start_failed;
544 }
545 CX18_DEBUG_INFO("VBI insertion started\n");
546 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300547 }
548
549 /* Tell the card to start capturing */
550 if (!cx18_start_v4l2_encode_stream(s)) {
551 /* We're done */
552 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
553 /* Resume a possibly paused encoder */
554 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
555 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
556 return 0;
557 }
558
Andy Walls79f3e962009-12-31 17:19:25 -0300559start_failed:
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300560 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
561
Andy Walls79f3e962009-12-31 17:19:25 -0300562 /*
563 * The associated VBI and IDX streams for internal use are released
564 * automatically when the MPG stream is released. We only need to stop
565 * the associated stream.
566 */
567 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
568 /* Stop the IDX stream which is always for internal use */
569 if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
570 cx18_stop_v4l2_encode_stream(s_idx, 0);
571 clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
572 }
573 /* Stop the VBI stream, if only running for internal use */
574 if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
575 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
576 cx18_stop_v4l2_encode_stream(s_vbi, 0);
577 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
578 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300579 }
580 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
Andy Walls79f3e962009-12-31 17:19:25 -0300581 cx18_release_stream(s); /* Also releases associated streams */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300582 return -EIO;
583}
584
585ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
586 loff_t *pos)
587{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300588 struct cx18_open_id *id = file2id(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300589 struct cx18 *cx = id->cx;
590 struct cx18_stream *s = &cx->streams[id->type];
591 int rc;
592
593 CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
594
595 mutex_lock(&cx->serialize_lock);
596 rc = cx18_start_capture(id);
597 mutex_unlock(&cx->serialize_lock);
598 if (rc)
599 return rc;
Steven Tothb7101de2011-04-06 08:32:56 -0300600
601 if ((id->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
602 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
603 return videobuf_read_stream(&id->vbuf_q, buf, count, pos, 0,
604 filp->f_flags & O_NONBLOCK);
605 }
606
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300607 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
608}
609
610unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
611{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300612 struct cx18_open_id *id = file2id(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300613 struct cx18 *cx = id->cx;
614 struct cx18_stream *s = &cx->streams[id->type];
615 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
616
617 /* Start a capture if there is none */
618 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
619 int rc;
620
621 mutex_lock(&cx->serialize_lock);
622 rc = cx18_start_capture(id);
623 mutex_unlock(&cx->serialize_lock);
624 if (rc) {
625 CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
626 s->name, rc);
627 return POLLERR;
628 }
629 CX18_DEBUG_FILE("Encoder poll started capture\n");
630 }
631
Steven Tothb7101de2011-04-06 08:32:56 -0300632 if ((id->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
633 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
634 return videobuf_poll_stream(filp, &id->vbuf_q, wait);
635 }
636
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300637 /* add stream's waitq to the poll list */
638 CX18_DEBUG_HI_FILE("Encoder poll\n");
639 poll_wait(filp, &s->waitq, wait);
640
Andy Wallsc37b11b2009-11-04 23:13:58 -0300641 if (atomic_read(&s->q_full.depth))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300642 return POLLIN | POLLRDNORM;
643 if (eof)
644 return POLLHUP;
645 return 0;
646}
647
Steven Tothb7101de2011-04-06 08:32:56 -0300648int cx18_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
649{
650 struct cx18_open_id *id = file->private_data;
651 struct cx18 *cx = id->cx;
652 struct cx18_stream *s = &cx->streams[id->type];
653 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
654
655 if ((id->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
656 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
657
658 /* Start a capture if there is none */
659 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
660 int rc;
661
662 mutex_lock(&cx->serialize_lock);
663 rc = cx18_start_capture(id);
664 mutex_unlock(&cx->serialize_lock);
665 if (rc) {
666 CX18_DEBUG_INFO(
667 "Could not start capture for %s (%d)\n",
668 s->name, rc);
669 return -EINVAL;
670 }
671 CX18_DEBUG_FILE("Encoder poll started capture\n");
672 }
673
674 return videobuf_mmap_mapper(&id->vbuf_q, vma);
675 }
676
677 return -EINVAL;
678}
679
680void cx18_vb_timeout(unsigned long data)
681{
682 struct cx18_stream *s = (struct cx18_stream *)data;
683 struct cx18_videobuf_buffer *buf;
684 unsigned long flags;
685
686 /* Return all of the buffers in error state, so the vbi/vid inode
687 * can return from blocking.
688 */
689 spin_lock_irqsave(&s->vb_lock, flags);
690 while (!list_empty(&s->vb_capture)) {
691 buf = list_entry(s->vb_capture.next,
692 struct cx18_videobuf_buffer, vb.queue);
693 list_del(&buf->vb.queue);
694 buf->vb.state = VIDEOBUF_ERROR;
695 wake_up(&buf->vb.done);
696 }
697 spin_unlock_irqrestore(&s->vb_lock, flags);
698}
699
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300700void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
701{
702 struct cx18 *cx = id->cx;
703 struct cx18_stream *s = &cx->streams[id->type];
Andy Walls79f3e962009-12-31 17:19:25 -0300704 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
705 struct cx18_stream *s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300706
707 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
708
709 /* 'Unclaim' this stream */
710
711 /* Stop capturing */
712 if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300713 CX18_DEBUG_INFO("close stopping capture\n");
Andy Walls79f3e962009-12-31 17:19:25 -0300714 if (id->type == CX18_ENC_STREAM_TYPE_MPG) {
715 /* Stop internal use associated VBI and IDX streams */
716 if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
717 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
718 CX18_DEBUG_INFO("close stopping embedded VBI "
719 "capture\n");
720 cx18_stop_v4l2_encode_stream(s_vbi, 0);
721 }
722 if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
723 CX18_DEBUG_INFO("close stopping IDX capture\n");
724 cx18_stop_v4l2_encode_stream(s_idx, 0);
725 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300726 }
727 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
728 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
729 /* Also used internally, don't stop capturing */
730 s->id = -1;
731 else
732 cx18_stop_v4l2_encode_stream(s, gop_end);
733 }
734 if (!gop_end) {
735 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
736 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
737 cx18_release_stream(s);
738 }
739}
740
Hans Verkuilbec43662008-12-30 06:58:20 -0300741int cx18_v4l2_close(struct file *filp)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300742{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300743 struct v4l2_fh *fh = filp->private_data;
744 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300745 struct cx18 *cx = id->cx;
746 struct cx18_stream *s = &cx->streams[id->type];
747
748 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
749
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300750 v4l2_fh_del(fh);
751 v4l2_fh_exit(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300752
753 /* Easy case first: this stream was never claimed by us */
754 if (s->id != id->open_id) {
755 kfree(id);
756 return 0;
757 }
758
759 /* 'Unclaim' this stream */
760
761 /* Stop radio */
762 mutex_lock(&cx->serialize_lock);
763 if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
764 /* Closing radio device, return to TV mode */
765 cx18_mute(cx);
766 /* Mark that the radio is no longer in use */
767 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
768 /* Switch tuner to TV */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300769 cx18_call_all(cx, core, s_std, cx->std);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300770 /* Select correct audio input (i.e. TV tuner or Line in) */
771 cx18_audio_set_io(cx);
Hans Verkuil31554ae2008-05-25 11:21:27 -0300772 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300773 /* Undo video mute */
774 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
Hans Verkuila75b9be2010-12-31 10:22:52 -0300775 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute) |
776 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8)));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300777 }
778 /* Done! Unmute and continue. */
779 cx18_unmute(cx);
780 cx18_release_stream(s);
781 } else {
782 cx18_stop_capture(id, 0);
Steven Tothb7101de2011-04-06 08:32:56 -0300783 if (id->type == CX18_ENC_STREAM_TYPE_YUV)
784 videobuf_mmap_free(&id->vbuf_q);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300785 }
786 kfree(id);
787 mutex_unlock(&cx->serialize_lock);
788 return 0;
789}
790
Steven Tothb7101de2011-04-06 08:32:56 -0300791void cx18_dma_free(struct videobuf_queue *q,
792 struct cx18_stream *s, struct cx18_videobuf_buffer *buf)
793{
794 videobuf_waiton(q, &buf->vb, 0, 0);
795 videobuf_vmalloc_free(&buf->vb);
796 buf->vb.state = VIDEOBUF_NEEDS_INIT;
797}
798
799static int cx18_prepare_buffer(struct videobuf_queue *q,
800 struct cx18_stream *s,
801 struct cx18_videobuf_buffer *buf,
802 u32 pixelformat,
803 unsigned int width, unsigned int height,
804 enum v4l2_field field)
805{
806 int rc = 0;
807
808 /* check settings */
809 buf->bytes_used = 0;
810
811 if ((width < 48) || (height < 32))
812 return -EINVAL;
813
814 buf->vb.size = (width * height * 16 /*fmt->depth*/) >> 3;
815 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
816 return -EINVAL;
817
818 /* alloc + fill struct (if changed) */
819 if (buf->vb.width != width || buf->vb.height != height ||
820 buf->vb.field != field || s->pixelformat != pixelformat ||
821 buf->tvnorm != s->tvnorm) {
822
823 buf->vb.width = width;
824 buf->vb.height = height;
825 buf->vb.field = field;
826 buf->tvnorm = s->tvnorm;
827 s->pixelformat = pixelformat;
828
829 cx18_dma_free(q, s, buf);
830 }
831
832 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
833 return -EINVAL;
834
835 if (buf->vb.field == 0)
836 buf->vb.field = V4L2_FIELD_INTERLACED;
837
838 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
839 buf->vb.width = width;
840 buf->vb.height = height;
841 buf->vb.field = field;
842 buf->tvnorm = s->tvnorm;
843 s->pixelformat = pixelformat;
844
845 rc = videobuf_iolock(q, &buf->vb, &s->fbuf);
846 if (rc != 0)
847 goto fail;
848 }
849 buf->vb.state = VIDEOBUF_PREPARED;
850 return 0;
851
852fail:
853 cx18_dma_free(q, s, buf);
854 return rc;
855
856}
857
858#define VB_MIN_BUFFERS 32
859#define VB_MIN_BUFSIZE 0x208000
860
861static int buffer_setup(struct videobuf_queue *q,
862 unsigned int *count, unsigned int *size)
863{
864 struct cx18_open_id *id = q->priv_data;
865 struct cx18 *cx = id->cx;
866 struct cx18_stream *s = &cx->streams[id->type];
867
868 *size = 2 * s->vbwidth * s->vbheight;
869 if (*count == 0)
870 *count = VB_MIN_BUFFERS;
871
872 while (*size * *count > VB_MIN_BUFFERS * VB_MIN_BUFSIZE)
873 (*count)--;
874
875 q->field = V4L2_FIELD_INTERLACED;
876 q->last = V4L2_FIELD_INTERLACED;
877
878 return 0;
879}
880
881static int buffer_prepare(struct videobuf_queue *q,
882 struct videobuf_buffer *vb,
883 enum v4l2_field field)
884{
885 struct cx18_videobuf_buffer *buf =
886 container_of(vb, struct cx18_videobuf_buffer, vb);
887 struct cx18_open_id *id = q->priv_data;
888 struct cx18 *cx = id->cx;
889 struct cx18_stream *s = &cx->streams[id->type];
890
891 return cx18_prepare_buffer(q, s, buf, s->pixelformat,
892 s->vbwidth, s->vbheight, field);
893}
894
895static void buffer_release(struct videobuf_queue *q,
896 struct videobuf_buffer *vb)
897{
898 struct cx18_videobuf_buffer *buf =
899 container_of(vb, struct cx18_videobuf_buffer, vb);
900 struct cx18_open_id *id = q->priv_data;
901 struct cx18 *cx = id->cx;
902 struct cx18_stream *s = &cx->streams[id->type];
903
904 cx18_dma_free(q, s, buf);
905}
906
907static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
908{
909 struct cx18_videobuf_buffer *buf =
910 container_of(vb, struct cx18_videobuf_buffer, vb);
911 struct cx18_open_id *id = q->priv_data;
912 struct cx18 *cx = id->cx;
913 struct cx18_stream *s = &cx->streams[id->type];
914
915 buf->vb.state = VIDEOBUF_QUEUED;
916
917 list_add_tail(&buf->vb.queue, &s->vb_capture);
918}
919
920static struct videobuf_queue_ops cx18_videobuf_qops = {
921 .buf_setup = buffer_setup,
922 .buf_prepare = buffer_prepare,
923 .buf_queue = buffer_queue,
924 .buf_release = buffer_release,
925};
926
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300927static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
928{
929 struct cx18 *cx = s->cx;
930 struct cx18_open_id *item;
931
932 CX18_DEBUG_FILE("open %s\n", s->name);
933
934 /* Allocate memory */
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300935 item = kzalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300936 if (NULL == item) {
937 CX18_DEBUG_WARN("nomem on v4l2 open\n");
938 return -ENOMEM;
939 }
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300940 v4l2_fh_init(&item->fh, s->video_dev);
941
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300942 item->cx = cx;
943 item->type = s->type;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300944
Steven Tothb7101de2011-04-06 08:32:56 -0300945 spin_lock_init(&item->s_lock);
946 item->vb_type = 0;
947
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300948 item->open_id = cx->open_id++;
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300949 filp->private_data = &item->fh;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300950
951 if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
952 /* Try to claim this stream */
953 if (cx18_claim_stream(item, item->type)) {
954 /* No, it's already in use */
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300955 v4l2_fh_exit(&item->fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300956 kfree(item);
957 return -EBUSY;
958 }
959
960 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
Hans Verkuil31554ae2008-05-25 11:21:27 -0300961 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300962 /* switching to radio while capture is
963 in progress is not polite */
964 cx18_release_stream(s);
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300965 v4l2_fh_exit(&item->fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300966 kfree(item);
967 return -EBUSY;
968 }
969 }
970
971 /* Mark that the radio is being used. */
972 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
973 /* We have the radio */
974 cx18_mute(cx);
975 /* Switch tuner to radio */
Andy Wallsff2a2002009-02-20 23:52:13 -0300976 cx18_call_all(cx, tuner, s_radio);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300977 /* Select the correct audio input (i.e. radio tuner) */
978 cx18_audio_set_io(cx);
979 /* Done! Unmute and continue. */
980 cx18_unmute(cx);
981 }
Steven Tothb7101de2011-04-06 08:32:56 -0300982 if (item->type == CX18_ENC_STREAM_TYPE_YUV) {
983 item->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
984 videobuf_queue_vmalloc_init(&item->vbuf_q, &cx18_videobuf_qops,
985 &cx->pci_dev->dev, &item->s_lock,
986 V4L2_BUF_TYPE_VIDEO_CAPTURE,
987 V4L2_FIELD_INTERLACED,
988 sizeof(struct cx18_videobuf_buffer),
989 item, &cx->serialize_lock);
990 }
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300991 v4l2_fh_add(&item->fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300992 return 0;
993}
994
Hans Verkuilbec43662008-12-30 06:58:20 -0300995int cx18_v4l2_open(struct file *filp)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300996{
Andy Walls5811cf92009-02-14 17:08:37 -0300997 int res;
998 struct video_device *video_dev = video_devdata(filp);
999 struct cx18_stream *s = video_get_drvdata(video_dev);
Joe Perches32a60952009-07-02 13:52:46 -03001000 struct cx18 *cx = s->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001001
1002 mutex_lock(&cx->serialize_lock);
1003 if (cx18_init_on_first_open(cx)) {
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001004 CX18_ERR("Failed to initialize on %s\n",
1005 video_device_node_name(video_dev));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001006 mutex_unlock(&cx->serialize_lock);
1007 return -ENXIO;
1008 }
1009 res = cx18_serialized_open(s, filp);
1010 mutex_unlock(&cx->serialize_lock);
1011 return res;
1012}
1013
1014void cx18_mute(struct cx18 *cx)
1015{
Andy Wallsd3c5e702008-08-23 16:42:29 -03001016 u32 h;
1017 if (atomic_read(&cx->ana_capturing)) {
1018 h = cx18_find_handle(cx);
1019 if (h != CX18_INVALID_TASK_HANDLE)
1020 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
1021 else
1022 CX18_ERR("Can't find valid task handle for mute\n");
1023 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001024 CX18_DEBUG_INFO("Mute\n");
1025}
1026
1027void cx18_unmute(struct cx18 *cx)
1028{
Andy Wallsd3c5e702008-08-23 16:42:29 -03001029 u32 h;
Hans Verkuil31554ae2008-05-25 11:21:27 -03001030 if (atomic_read(&cx->ana_capturing)) {
Andy Wallsd3c5e702008-08-23 16:42:29 -03001031 h = cx18_find_handle(cx);
1032 if (h != CX18_INVALID_TASK_HANDLE) {
1033 cx18_msleep_timeout(100, 0);
1034 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
1035 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
1036 } else
1037 CX18_ERR("Can't find valid task handle for unmute\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001038 }
1039 CX18_DEBUG_INFO("Unmute\n");
1040}