]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/cx18/cx18-fileops.c
V4L/DVB (11622): cx18: Allow IVTV format VBI insertion in MPEG-2 SVCD and DVD streams
[net-next-2.6.git] / drivers / media / video / cx18 / cx18-fileops.c
CommitLineData
1c1e45d1
HV
1/*
2 * cx18 file operation functions
3 *
4 * Derived from ivtv-fileops.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
1ed9dcc8 7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
1c1e45d1
HV
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.
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
40 associated VBI streams are also automatically claimed.
41 Possible error returns: -EBUSY if someone else has claimed
42 the stream or 0 on success. */
50510993 43static int cx18_claim_stream(struct cx18_open_id *id, int type)
1c1e45d1
HV
44{
45 struct cx18 *cx = id->cx;
46 struct cx18_stream *s = &cx->streams[type];
47 struct cx18_stream *s_vbi;
48 int vbi_type;
49
50 if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
51 /* someone already claimed this stream */
52 if (s->id == id->open_id) {
53 /* yes, this file descriptor did. So that's OK. */
54 return 0;
55 }
56 if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
57 /* VBI is handled already internally, now also assign
58 the file descriptor to this stream for external
59 reading of the stream. */
60 s->id = id->open_id;
61 CX18_DEBUG_INFO("Start Read VBI\n");
62 return 0;
63 }
64 /* someone else is using this stream already */
65 CX18_DEBUG_INFO("Stream %d is busy\n", type);
66 return -EBUSY;
67 }
68 s->id = id->open_id;
69
dd073434 70 /* CX18_ENC_STREAM_TYPE_MPG needs to claim CX18_ENC_STREAM_TYPE_VBI
1c1e45d1
HV
71 (provided VBI insertion is on and sliced VBI is selected), for all
72 other streams we're done */
73 if (type == CX18_ENC_STREAM_TYPE_MPG &&
dd073434 74 cx->vbi.insert_mpeg && !cx18_raw_vbi(cx)) {
1c1e45d1
HV
75 vbi_type = CX18_ENC_STREAM_TYPE_VBI;
76 } else {
77 return 0;
78 }
79 s_vbi = &cx->streams[vbi_type];
80
81 set_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
82
83 /* mark that it is used internally */
84 set_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags);
85 return 0;
86}
87
88/* This function releases a previously claimed stream. It will take into
89 account associated VBI streams. */
50510993 90static void cx18_release_stream(struct cx18_stream *s)
1c1e45d1
HV
91{
92 struct cx18 *cx = s->cx;
93 struct cx18_stream *s_vbi;
94
95 s->id = -1;
96 if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
97 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
98 /* this stream is still in use internally */
99 return;
100 }
101 if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
102 CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
103 return;
104 }
105
106 cx18_flush_queues(s);
107
108 /* CX18_ENC_STREAM_TYPE_MPG needs to release CX18_ENC_STREAM_TYPE_VBI,
109 for all other streams we're done */
110 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
111 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
112 else
113 return;
114
115 /* clear internal use flag */
116 if (!test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
117 /* was already cleared */
118 return;
119 }
120 if (s_vbi->id != -1) {
121 /* VBI stream still claimed by a file descriptor */
122 return;
123 }
124 clear_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
125 cx18_flush_queues(s_vbi);
126}
127
128static void cx18_dualwatch(struct cx18 *cx)
129{
130 struct v4l2_tuner vt;
0d82fe80
AW
131 u32 new_bitmap;
132 u32 new_stereo_mode;
133 const u32 stereo_mask = 0x0300;
134 const u32 dual = 0x0200;
d3c5e707 135 u32 h;
1c1e45d1
HV
136
137 new_stereo_mode = cx->params.audio_properties & stereo_mask;
138 memset(&vt, 0, sizeof(vt));
ff2a2001 139 cx18_call_all(cx, tuner, g_tuner, &vt);
1c1e45d1
HV
140 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
141 (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
142 new_stereo_mode = dual;
143
144 if (new_stereo_mode == cx->dualwatch_stereo_mode)
145 return;
146
d3c5e707
AW
147 new_bitmap = new_stereo_mode
148 | (cx->params.audio_properties & ~stereo_mask);
1c1e45d1 149
d3c5e707
AW
150 CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. "
151 "new audio_bitmask=0x%ux\n",
152 cx->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
1c1e45d1 153
d3c5e707
AW
154 h = cx18_find_handle(cx);
155 if (h == CX18_INVALID_TASK_HANDLE) {
156 CX18_DEBUG_INFO("dualwatch: can't find valid task handle\n");
157 return;
158 }
159
160 if (cx18_vapi(cx,
161 CX18_CPU_SET_AUDIO_PARAMETERS, 2, h, new_bitmap) == 0) {
1c1e45d1
HV
162 cx->dualwatch_stereo_mode = new_stereo_mode;
163 return;
164 }
165 CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
166}
167
168
169static struct cx18_buffer *cx18_get_buffer(struct cx18_stream *s, int non_block, int *err)
170{
171 struct cx18 *cx = s->cx;
172 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
173 struct cx18_buffer *buf;
174 DEFINE_WAIT(wait);
175
176 *err = 0;
177 while (1) {
178 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
302df970
AW
179 /* Process pending program info updates and pending
180 VBI data */
1c1e45d1
HV
181
182 if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
183 cx->dualwatch_jiffies = jiffies;
184 cx18_dualwatch(cx);
185 }
186 if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
187 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
188 while ((buf = cx18_dequeue(s_vbi, &s_vbi->q_full))) {
189 /* byteswap and process VBI data */
af009cf6 190 cx18_process_vbi_data(cx, buf,
af009cf6 191 s_vbi->type);
66c2a6b0 192 cx18_stream_put_buf_fw(s_vbi, buf);
1c1e45d1
HV
193 }
194 }
195 buf = &cx->vbi.sliced_mpeg_buf;
196 if (buf->readpos != buf->bytesused)
197 return buf;
198 }
199
1c1e45d1
HV
200 /* do we have new data? */
201 buf = cx18_dequeue(s, &s->q_full);
202 if (buf) {
203 if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
204 &buf->b_flags))
205 return buf;
206 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
207 /* byteswap MPG data */
208 cx18_buf_swap(buf);
209 else {
210 /* byteswap and process VBI data */
776fa869 211 cx18_process_vbi_data(cx, buf, s->type);
1c1e45d1
HV
212 }
213 return buf;
214 }
215
216 /* return if end of stream */
217 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
218 CX18_DEBUG_INFO("EOS %s\n", s->name);
219 return NULL;
220 }
221
222 /* return if file was opened with O_NONBLOCK */
223 if (non_block) {
224 *err = -EAGAIN;
225 return NULL;
226 }
227
228 /* wait for more data to arrive */
229 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
230 /* New buffers might have become available before we were added
231 to the waitqueue */
b04bce47 232 if (!atomic_read(&s->q_full.buffers))
1c1e45d1
HV
233 schedule();
234 finish_wait(&s->waitq, &wait);
235 if (signal_pending(current)) {
236 /* return if a signal was received */
237 CX18_DEBUG_INFO("User stopped %s\n", s->name);
238 *err = -EINTR;
239 return NULL;
240 }
241 }
242}
243
244static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
245{
246 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
247
248 cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
249 cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
250 cx->vbi.sliced_mpeg_buf.readpos = 0;
251}
252
253static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
254 struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
255{
256 struct cx18 *cx = s->cx;
257 size_t len = buf->bytesused - buf->readpos;
258
259 if (len > ucount)
260 len = ucount;
261 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
dd073434 262 !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
302df970
AW
263 /*
264 * Try to find a good splice point in the PS, just before
265 * an MPEG-2 Program Pack start code, and provide only
266 * up to that point to the user, so it's easy to insert VBI data
267 * the next time around.
0c629252
AW
268 *
269 * This will not work for an MPEG-2 TS and has only been
270 * verified by analysis to work for an MPEG-2 PS. Helen Buus
271 * pointed out this works for the CX23416 MPEG-2 DVD compatible
272 * stream, and research indicates both the MPEG 2 SVCD and DVD
273 * stream types use an MPEG-2 PS container.
302df970 274 */
302df970
AW
275 /*
276 * An MPEG-2 Program Stream (PS) is a series of
277 * MPEG-2 Program Packs terminated by an
278 * MPEG Program End Code after the last Program Pack.
279 * A Program Pack may hold a PS System Header packet and any
280 * number of Program Elementary Stream (PES) Packets
281 */
1c1e45d1
HV
282 const char *start = buf->buf + buf->readpos;
283 const char *p = start + 1;
284 const u8 *q;
285 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
286 int stuffing, i;
287
288 while (start + len > p) {
302df970 289 /* Scan for a 0 to find a potential MPEG-2 start code */
1c1e45d1
HV
290 q = memchr(p, 0, start + len - p);
291 if (q == NULL)
292 break;
293 p = q + 1;
302df970
AW
294 /*
295 * Keep looking if not a
296 * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
297 * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
298 */
1c1e45d1
HV
299 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
300 q[1] != 0 || q[2] != 1 || q[3] != ch)
301 continue;
302df970
AW
302
303 /* If expecting the primary video PES */
1c1e45d1 304 if (!cx->search_pack_header) {
302df970 305 /* Continue if it couldn't be a PES packet */
1c1e45d1
HV
306 if ((q[6] & 0xc0) != 0x80)
307 continue;
302df970
AW
308 /* Check if a PTS or PTS & DTS follow */
309 if (((q[7] & 0xc0) == 0x80 && /* PTS only */
310 (q[9] & 0xf0) == 0x20) || /* PTS only */
311 ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
312 (q[9] & 0xf0) == 0x30)) { /* DTS follows */
313 /* Assume we found the video PES hdr */
314 ch = 0xba; /* next want a Program Pack*/
1c1e45d1 315 cx->search_pack_header = 1;
302df970 316 p = q + 9; /* Skip this video PES hdr */
1c1e45d1
HV
317 }
318 continue;
319 }
302df970
AW
320
321 /* We may have found a Program Pack start code */
322
323 /* Get the count of stuffing bytes & verify them */
1c1e45d1
HV
324 stuffing = q[13] & 7;
325 /* all stuffing bytes must be 0xff */
326 for (i = 0; i < stuffing; i++)
327 if (q[14 + i] != 0xff)
328 break;
302df970
AW
329 if (i == stuffing && /* right number of stuffing bytes*/
330 (q[4] & 0xc4) == 0x44 && /* marker check */
331 (q[12] & 3) == 3 && /* marker check */
332 q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
1c1e45d1
HV
333 q[15 + stuffing] == 0 &&
334 q[16 + stuffing] == 1) {
302df970
AW
335 /* We declare we actually found a Program Pack*/
336 cx->search_pack_header = 0; /* expect vid PES */
1c1e45d1
HV
337 len = (char *)q - start;
338 cx18_setup_sliced_vbi_buf(cx);
339 break;
340 }
341 }
342 }
343 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
344 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
345 len, s->name);
346 return -EFAULT;
347 }
348 buf->readpos += len;
349 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
350 buf != &cx->vbi.sliced_mpeg_buf)
351 cx->mpg_data_received += len;
352 return len;
353}
354
355static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
356 size_t tot_count, int non_block)
357{
358 struct cx18 *cx = s->cx;
359 size_t tot_written = 0;
360 int single_frame = 0;
361
31554ae5 362 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
1c1e45d1
HV
363 /* shouldn't happen */
364 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
365 s->name);
366 return -EIO;
367 }
368
369 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
370 frames should arrive one-by-one, so make sure we never output more
371 than one VBI frame at a time */
dd073434 372 if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
1c1e45d1
HV
373 single_frame = 1;
374
375 for (;;) {
376 struct cx18_buffer *buf;
377 int rc;
378
379 buf = cx18_get_buffer(s, non_block, &rc);
380 /* if there is no data available... */
381 if (buf == NULL) {
382 /* if we got data, then return that regardless */
383 if (tot_written)
384 break;
385 /* EOS condition */
386 if (rc == 0) {
387 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
388 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
389 cx18_release_stream(s);
390 }
391 /* set errno */
392 return rc;
393 }
394
395 rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
396 tot_count - tot_written);
397
398 if (buf != &cx->vbi.sliced_mpeg_buf) {
66c2a6b0
AW
399 if (buf->readpos == buf->bytesused)
400 cx18_stream_put_buf_fw(s, buf);
401 else
b80e1074 402 cx18_push(s, buf, &s->q_full);
1c1e45d1
HV
403 } else if (buf->readpos == buf->bytesused) {
404 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
405
406 cx->vbi.sliced_mpeg_size[idx] = 0;
407 cx->vbi.inserted_frame++;
408 cx->vbi_data_inserted += buf->bytesused;
409 }
410 if (rc < 0)
411 return rc;
412 tot_written += rc;
413
414 if (tot_written == tot_count || single_frame)
415 break;
416 }
417 return tot_written;
418}
419
420static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
421 size_t count, loff_t *pos, int non_block)
422{
423 ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
424 struct cx18 *cx = s->cx;
425
426 CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
427 if (rc > 0)
428 pos += rc;
429 return rc;
430}
431
432int cx18_start_capture(struct cx18_open_id *id)
433{
434 struct cx18 *cx = id->cx;
435 struct cx18_stream *s = &cx->streams[id->type];
436 struct cx18_stream *s_vbi;
437
438 if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
439 /* you cannot read from these stream types. */
440 return -EPERM;
441 }
442
443 /* Try to claim this stream. */
444 if (cx18_claim_stream(id, s->type))
445 return -EBUSY;
446
447 /* If capture is already in progress, then we also have to
448 do nothing extra. */
449 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
450 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
451 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
452 return 0;
453 }
454
455 /* Start VBI capture if required */
456 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
457 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
458 test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
459 !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
460 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
461 automatically when the MPG stream is claimed.
462 We only need to start the VBI capturing. */
463 if (cx18_start_v4l2_encode_stream(s_vbi)) {
464 CX18_DEBUG_WARN("VBI capture start failed\n");
465
466 /* Failure, clean up and return an error */
467 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
468 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
469 /* also releases the associated VBI stream */
470 cx18_release_stream(s);
471 return -EIO;
472 }
473 CX18_DEBUG_INFO("VBI insertion started\n");
474 }
475
476 /* Tell the card to start capturing */
477 if (!cx18_start_v4l2_encode_stream(s)) {
478 /* We're done */
479 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
480 /* Resume a possibly paused encoder */
481 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
482 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
483 return 0;
484 }
485
486 /* failure, clean up */
487 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
488
489 /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
490 automatically when the MPG stream is released.
491 We only need to stop the VBI capturing. */
492 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
493 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
494 cx18_stop_v4l2_encode_stream(s_vbi, 0);
495 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
496 }
497 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
498 cx18_release_stream(s);
499 return -EIO;
500}
501
502ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
503 loff_t *pos)
504{
505 struct cx18_open_id *id = filp->private_data;
506 struct cx18 *cx = id->cx;
507 struct cx18_stream *s = &cx->streams[id->type];
508 int rc;
509
510 CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
511
512 mutex_lock(&cx->serialize_lock);
513 rc = cx18_start_capture(id);
514 mutex_unlock(&cx->serialize_lock);
515 if (rc)
516 return rc;
517 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
518}
519
520unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
521{
522 struct cx18_open_id *id = filp->private_data;
523 struct cx18 *cx = id->cx;
524 struct cx18_stream *s = &cx->streams[id->type];
525 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
526
527 /* Start a capture if there is none */
528 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
529 int rc;
530
531 mutex_lock(&cx->serialize_lock);
532 rc = cx18_start_capture(id);
533 mutex_unlock(&cx->serialize_lock);
534 if (rc) {
535 CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
536 s->name, rc);
537 return POLLERR;
538 }
539 CX18_DEBUG_FILE("Encoder poll started capture\n");
540 }
541
542 /* add stream's waitq to the poll list */
543 CX18_DEBUG_HI_FILE("Encoder poll\n");
544 poll_wait(filp, &s->waitq, wait);
545
b80e1074 546 if (atomic_read(&s->q_full.buffers))
1c1e45d1
HV
547 return POLLIN | POLLRDNORM;
548 if (eof)
549 return POLLHUP;
550 return 0;
551}
552
553void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
554{
555 struct cx18 *cx = id->cx;
556 struct cx18_stream *s = &cx->streams[id->type];
557
558 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
559
560 /* 'Unclaim' this stream */
561
562 /* Stop capturing */
563 if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
564 struct cx18_stream *s_vbi =
565 &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
566
567 CX18_DEBUG_INFO("close stopping capture\n");
568 /* Special case: a running VBI capture for VBI insertion
569 in the mpeg stream. Need to stop that too. */
570 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
571 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
572 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
573 CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
574 cx18_stop_v4l2_encode_stream(s_vbi, 0);
575 }
576 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
577 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
578 /* Also used internally, don't stop capturing */
579 s->id = -1;
580 else
581 cx18_stop_v4l2_encode_stream(s, gop_end);
582 }
583 if (!gop_end) {
584 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
585 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
586 cx18_release_stream(s);
587 }
588}
589
bec43661 590int cx18_v4l2_close(struct file *filp)
1c1e45d1
HV
591{
592 struct cx18_open_id *id = filp->private_data;
593 struct cx18 *cx = id->cx;
594 struct cx18_stream *s = &cx->streams[id->type];
595
596 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
597
598 v4l2_prio_close(&cx->prio, &id->prio);
599
600 /* Easy case first: this stream was never claimed by us */
601 if (s->id != id->open_id) {
602 kfree(id);
603 return 0;
604 }
605
606 /* 'Unclaim' this stream */
607
608 /* Stop radio */
609 mutex_lock(&cx->serialize_lock);
610 if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
611 /* Closing radio device, return to TV mode */
612 cx18_mute(cx);
613 /* Mark that the radio is no longer in use */
614 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
615 /* Switch tuner to TV */
f41737ec 616 cx18_call_all(cx, core, s_std, cx->std);
1c1e45d1
HV
617 /* Select correct audio input (i.e. TV tuner or Line in) */
618 cx18_audio_set_io(cx);
31554ae5 619 if (atomic_read(&cx->ana_capturing) > 0) {
1c1e45d1
HV
620 /* Undo video mute */
621 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
622 cx->params.video_mute |
623 (cx->params.video_mute_yuv << 8));
624 }
625 /* Done! Unmute and continue. */
626 cx18_unmute(cx);
627 cx18_release_stream(s);
628 } else {
629 cx18_stop_capture(id, 0);
630 }
631 kfree(id);
632 mutex_unlock(&cx->serialize_lock);
633 return 0;
634}
635
636static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
637{
638 struct cx18 *cx = s->cx;
639 struct cx18_open_id *item;
640
641 CX18_DEBUG_FILE("open %s\n", s->name);
642
643 /* Allocate memory */
644 item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
645 if (NULL == item) {
646 CX18_DEBUG_WARN("nomem on v4l2 open\n");
647 return -ENOMEM;
648 }
649 item->cx = cx;
650 item->type = s->type;
651 v4l2_prio_open(&cx->prio, &item->prio);
652
653 item->open_id = cx->open_id++;
654 filp->private_data = item;
655
656 if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
657 /* Try to claim this stream */
658 if (cx18_claim_stream(item, item->type)) {
659 /* No, it's already in use */
660 kfree(item);
661 return -EBUSY;
662 }
663
664 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
31554ae5 665 if (atomic_read(&cx->ana_capturing) > 0) {
1c1e45d1
HV
666 /* switching to radio while capture is
667 in progress is not polite */
668 cx18_release_stream(s);
669 kfree(item);
670 return -EBUSY;
671 }
672 }
673
674 /* Mark that the radio is being used. */
675 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
676 /* We have the radio */
677 cx18_mute(cx);
678 /* Switch tuner to radio */
ff2a2001 679 cx18_call_all(cx, tuner, s_radio);
1c1e45d1
HV
680 /* Select the correct audio input (i.e. radio tuner) */
681 cx18_audio_set_io(cx);
682 /* Done! Unmute and continue. */
683 cx18_unmute(cx);
684 }
685 return 0;
686}
687
bec43661 688int cx18_v4l2_open(struct file *filp)
1c1e45d1 689{
5811cf99
AW
690 int res;
691 struct video_device *video_dev = video_devdata(filp);
692 struct cx18_stream *s = video_get_drvdata(video_dev);
693 struct cx18 *cx = s->cx;;
1c1e45d1
HV
694
695 mutex_lock(&cx->serialize_lock);
696 if (cx18_init_on_first_open(cx)) {
5811cf99
AW
697 CX18_ERR("Failed to initialize on minor %d\n",
698 video_dev->minor);
1c1e45d1
HV
699 mutex_unlock(&cx->serialize_lock);
700 return -ENXIO;
701 }
702 res = cx18_serialized_open(s, filp);
703 mutex_unlock(&cx->serialize_lock);
704 return res;
705}
706
707void cx18_mute(struct cx18 *cx)
708{
d3c5e707
AW
709 u32 h;
710 if (atomic_read(&cx->ana_capturing)) {
711 h = cx18_find_handle(cx);
712 if (h != CX18_INVALID_TASK_HANDLE)
713 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
714 else
715 CX18_ERR("Can't find valid task handle for mute\n");
716 }
1c1e45d1
HV
717 CX18_DEBUG_INFO("Mute\n");
718}
719
720void cx18_unmute(struct cx18 *cx)
721{
d3c5e707 722 u32 h;
31554ae5 723 if (atomic_read(&cx->ana_capturing)) {
d3c5e707
AW
724 h = cx18_find_handle(cx);
725 if (h != CX18_INVALID_TASK_HANDLE) {
726 cx18_msleep_timeout(100, 0);
727 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
728 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
729 } else
730 CX18_ERR("Can't find valid task handle for unmute\n");
1c1e45d1
HV
731 }
732 CX18_DEBUG_INFO("Unmute\n");
733}