2 * cx18 file operation functions
4 * Derived from ivtv-fileops.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
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.
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.
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
25 #include "cx18-driver.h"
26 #include "cx18-fileops.h"
28 #include "cx18-queue.h"
30 #include "cx18-audio.h"
31 #include "cx18-mailbox.h"
33 #include "cx18-streams.h"
34 #include "cx18-controls.h"
35 #include "cx18-ioctl.h"
36 #include "cx18-cards.h"
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. */
43 static int cx18_claim_stream(struct cx18_open_id *id, int type)
45 struct cx18 *cx = id->cx;
46 struct cx18_stream *s = &cx->streams[type];
47 struct cx18_stream *s_vbi;
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. */
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. */
61 CX18_DEBUG_INFO("Start Read VBI\n");
64 /* someone else is using this stream already */
65 CX18_DEBUG_INFO("Stream %d is busy\n", type);
70 /* CX18_ENC_STREAM_TYPE_MPG needs to claim CX18_ENC_STREAM_TYPE_VBI
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 &&
74 cx->vbi.insert_mpeg && !cx18_raw_vbi(cx)) {
75 vbi_type = CX18_ENC_STREAM_TYPE_VBI;
79 s_vbi = &cx->streams[vbi_type];
81 set_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
83 /* mark that it is used internally */
84 set_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags);
88 /* This function releases a previously claimed stream. It will take into
89 account associated VBI streams. */
90 static void cx18_release_stream(struct cx18_stream *s)
92 struct cx18 *cx = s->cx;
93 struct cx18_stream *s_vbi;
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 */
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);
106 cx18_flush_queues(s);
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];
115 /* clear internal use flag */
116 if (!test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
117 /* was already cleared */
120 if (s_vbi->id != -1) {
121 /* VBI stream still claimed by a file descriptor */
124 clear_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
125 cx18_flush_queues(s_vbi);
128 static void cx18_dualwatch(struct cx18 *cx)
130 struct v4l2_tuner vt;
133 const u32 stereo_mask = 0x0300;
134 const u32 dual = 0x0200;
137 new_stereo_mode = cx->params.audio_properties & stereo_mask;
138 memset(&vt, 0, sizeof(vt));
139 cx18_call_all(cx, tuner, g_tuner, &vt);
140 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
141 (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
142 new_stereo_mode = dual;
144 if (new_stereo_mode == cx->dualwatch_stereo_mode)
147 new_bitmap = new_stereo_mode
148 | (cx->params.audio_properties & ~stereo_mask);
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);
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");
161 CX18_CPU_SET_AUDIO_PARAMETERS, 2, h, new_bitmap) == 0) {
162 cx->dualwatch_stereo_mode = new_stereo_mode;
165 CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
169 static struct cx18_buffer *cx18_get_buffer(struct cx18_stream *s, int non_block, int *err)
171 struct cx18 *cx = s->cx;
172 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
173 struct cx18_buffer *buf;
178 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
179 /* Process pending program info updates and pending
182 if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
183 cx->dualwatch_jiffies = jiffies;
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 */
190 cx18_process_vbi_data(cx, buf,
192 cx18_stream_put_buf_fw(s_vbi, buf);
195 buf = &cx->vbi.sliced_mpeg_buf;
196 if (buf->readpos != buf->bytesused)
200 /* do we have new data? */
201 buf = cx18_dequeue(s, &s->q_full);
203 if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
206 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
207 /* byteswap MPG data */
210 /* byteswap and process VBI data */
211 cx18_process_vbi_data(cx, buf, s->type);
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);
222 /* return if file was opened with O_NONBLOCK */
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
232 if (!atomic_read(&s->q_full.buffers))
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);
244 static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
246 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
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;
253 static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
254 struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
256 struct cx18 *cx = s->cx;
257 size_t len = buf->bytesused - buf->readpos;
261 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
262 !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
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.
269 /* FIXME - This only works for an MPEG-2 PS, not a TS */
271 * An MPEG-2 Program Stream (PS) is a series of
272 * MPEG-2 Program Packs terminated by an
273 * MPEG Program End Code after the last Program Pack.
274 * A Program Pack may hold a PS System Header packet and any
275 * number of Program Elementary Stream (PES) Packets
277 const char *start = buf->buf + buf->readpos;
278 const char *p = start + 1;
280 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
283 while (start + len > p) {
284 /* Scan for a 0 to find a potential MPEG-2 start code */
285 q = memchr(p, 0, start + len - p);
290 * Keep looking if not a
291 * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
292 * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
294 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
295 q[1] != 0 || q[2] != 1 || q[3] != ch)
298 /* If expecting the primary video PES */
299 if (!cx->search_pack_header) {
300 /* Continue if it couldn't be a PES packet */
301 if ((q[6] & 0xc0) != 0x80)
303 /* Check if a PTS or PTS & DTS follow */
304 if (((q[7] & 0xc0) == 0x80 && /* PTS only */
305 (q[9] & 0xf0) == 0x20) || /* PTS only */
306 ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
307 (q[9] & 0xf0) == 0x30)) { /* DTS follows */
308 /* Assume we found the video PES hdr */
309 ch = 0xba; /* next want a Program Pack*/
310 cx->search_pack_header = 1;
311 p = q + 9; /* Skip this video PES hdr */
316 /* We may have found a Program Pack start code */
318 /* Get the count of stuffing bytes & verify them */
319 stuffing = q[13] & 7;
320 /* all stuffing bytes must be 0xff */
321 for (i = 0; i < stuffing; i++)
322 if (q[14 + i] != 0xff)
324 if (i == stuffing && /* right number of stuffing bytes*/
325 (q[4] & 0xc4) == 0x44 && /* marker check */
326 (q[12] & 3) == 3 && /* marker check */
327 q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
328 q[15 + stuffing] == 0 &&
329 q[16 + stuffing] == 1) {
330 /* We declare we actually found a Program Pack*/
331 cx->search_pack_header = 0; /* expect vid PES */
332 len = (char *)q - start;
333 cx18_setup_sliced_vbi_buf(cx);
338 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
339 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
344 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
345 buf != &cx->vbi.sliced_mpeg_buf)
346 cx->mpg_data_received += len;
350 static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
351 size_t tot_count, int non_block)
353 struct cx18 *cx = s->cx;
354 size_t tot_written = 0;
355 int single_frame = 0;
357 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
358 /* shouldn't happen */
359 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
364 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
365 frames should arrive one-by-one, so make sure we never output more
366 than one VBI frame at a time */
367 if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
371 struct cx18_buffer *buf;
374 buf = cx18_get_buffer(s, non_block, &rc);
375 /* if there is no data available... */
377 /* if we got data, then return that regardless */
382 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
383 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
384 cx18_release_stream(s);
390 rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
391 tot_count - tot_written);
393 if (buf != &cx->vbi.sliced_mpeg_buf) {
394 if (buf->readpos == buf->bytesused)
395 cx18_stream_put_buf_fw(s, buf);
397 cx18_push(s, buf, &s->q_full);
398 } else if (buf->readpos == buf->bytesused) {
399 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
401 cx->vbi.sliced_mpeg_size[idx] = 0;
402 cx->vbi.inserted_frame++;
403 cx->vbi_data_inserted += buf->bytesused;
409 if (tot_written == tot_count || single_frame)
415 static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
416 size_t count, loff_t *pos, int non_block)
418 ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
419 struct cx18 *cx = s->cx;
421 CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
427 int cx18_start_capture(struct cx18_open_id *id)
429 struct cx18 *cx = id->cx;
430 struct cx18_stream *s = &cx->streams[id->type];
431 struct cx18_stream *s_vbi;
433 if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
434 /* you cannot read from these stream types. */
438 /* Try to claim this stream. */
439 if (cx18_claim_stream(id, s->type))
442 /* If capture is already in progress, then we also have to
444 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
445 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
446 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
450 /* Start VBI capture if required */
451 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
452 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
453 test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
454 !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
455 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
456 automatically when the MPG stream is claimed.
457 We only need to start the VBI capturing. */
458 if (cx18_start_v4l2_encode_stream(s_vbi)) {
459 CX18_DEBUG_WARN("VBI capture start failed\n");
461 /* Failure, clean up and return an error */
462 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
463 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
464 /* also releases the associated VBI stream */
465 cx18_release_stream(s);
468 CX18_DEBUG_INFO("VBI insertion started\n");
471 /* Tell the card to start capturing */
472 if (!cx18_start_v4l2_encode_stream(s)) {
474 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
475 /* Resume a possibly paused encoder */
476 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
477 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
481 /* failure, clean up */
482 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
484 /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
485 automatically when the MPG stream is released.
486 We only need to stop the VBI capturing. */
487 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
488 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
489 cx18_stop_v4l2_encode_stream(s_vbi, 0);
490 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
492 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
493 cx18_release_stream(s);
497 ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
500 struct cx18_open_id *id = filp->private_data;
501 struct cx18 *cx = id->cx;
502 struct cx18_stream *s = &cx->streams[id->type];
505 CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
507 mutex_lock(&cx->serialize_lock);
508 rc = cx18_start_capture(id);
509 mutex_unlock(&cx->serialize_lock);
512 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
515 unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
517 struct cx18_open_id *id = filp->private_data;
518 struct cx18 *cx = id->cx;
519 struct cx18_stream *s = &cx->streams[id->type];
520 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
522 /* Start a capture if there is none */
523 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
526 mutex_lock(&cx->serialize_lock);
527 rc = cx18_start_capture(id);
528 mutex_unlock(&cx->serialize_lock);
530 CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
534 CX18_DEBUG_FILE("Encoder poll started capture\n");
537 /* add stream's waitq to the poll list */
538 CX18_DEBUG_HI_FILE("Encoder poll\n");
539 poll_wait(filp, &s->waitq, wait);
541 if (atomic_read(&s->q_full.buffers))
542 return POLLIN | POLLRDNORM;
548 void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
550 struct cx18 *cx = id->cx;
551 struct cx18_stream *s = &cx->streams[id->type];
553 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
555 /* 'Unclaim' this stream */
558 if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
559 struct cx18_stream *s_vbi =
560 &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
562 CX18_DEBUG_INFO("close stopping capture\n");
563 /* Special case: a running VBI capture for VBI insertion
564 in the mpeg stream. Need to stop that too. */
565 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
566 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
567 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
568 CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
569 cx18_stop_v4l2_encode_stream(s_vbi, 0);
571 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
572 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
573 /* Also used internally, don't stop capturing */
576 cx18_stop_v4l2_encode_stream(s, gop_end);
579 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
580 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
581 cx18_release_stream(s);
585 int cx18_v4l2_close(struct file *filp)
587 struct cx18_open_id *id = filp->private_data;
588 struct cx18 *cx = id->cx;
589 struct cx18_stream *s = &cx->streams[id->type];
591 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
593 v4l2_prio_close(&cx->prio, &id->prio);
595 /* Easy case first: this stream was never claimed by us */
596 if (s->id != id->open_id) {
601 /* 'Unclaim' this stream */
604 mutex_lock(&cx->serialize_lock);
605 if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
606 /* Closing radio device, return to TV mode */
608 /* Mark that the radio is no longer in use */
609 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
610 /* Switch tuner to TV */
611 cx18_call_all(cx, core, s_std, cx->std);
612 /* Select correct audio input (i.e. TV tuner or Line in) */
613 cx18_audio_set_io(cx);
614 if (atomic_read(&cx->ana_capturing) > 0) {
615 /* Undo video mute */
616 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
617 cx->params.video_mute |
618 (cx->params.video_mute_yuv << 8));
620 /* Done! Unmute and continue. */
622 cx18_release_stream(s);
624 cx18_stop_capture(id, 0);
627 mutex_unlock(&cx->serialize_lock);
631 static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
633 struct cx18 *cx = s->cx;
634 struct cx18_open_id *item;
636 CX18_DEBUG_FILE("open %s\n", s->name);
638 /* Allocate memory */
639 item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
641 CX18_DEBUG_WARN("nomem on v4l2 open\n");
645 item->type = s->type;
646 v4l2_prio_open(&cx->prio, &item->prio);
648 item->open_id = cx->open_id++;
649 filp->private_data = item;
651 if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
652 /* Try to claim this stream */
653 if (cx18_claim_stream(item, item->type)) {
654 /* No, it's already in use */
659 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
660 if (atomic_read(&cx->ana_capturing) > 0) {
661 /* switching to radio while capture is
662 in progress is not polite */
663 cx18_release_stream(s);
669 /* Mark that the radio is being used. */
670 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
671 /* We have the radio */
673 /* Switch tuner to radio */
674 cx18_call_all(cx, tuner, s_radio);
675 /* Select the correct audio input (i.e. radio tuner) */
676 cx18_audio_set_io(cx);
677 /* Done! Unmute and continue. */
683 int cx18_v4l2_open(struct file *filp)
686 struct video_device *video_dev = video_devdata(filp);
687 struct cx18_stream *s = video_get_drvdata(video_dev);
688 struct cx18 *cx = s->cx;;
690 mutex_lock(&cx->serialize_lock);
691 if (cx18_init_on_first_open(cx)) {
692 CX18_ERR("Failed to initialize on minor %d\n",
694 mutex_unlock(&cx->serialize_lock);
697 res = cx18_serialized_open(s, filp);
698 mutex_unlock(&cx->serialize_lock);
702 void cx18_mute(struct cx18 *cx)
705 if (atomic_read(&cx->ana_capturing)) {
706 h = cx18_find_handle(cx);
707 if (h != CX18_INVALID_TASK_HANDLE)
708 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
710 CX18_ERR("Can't find valid task handle for mute\n");
712 CX18_DEBUG_INFO("Mute\n");
715 void cx18_unmute(struct cx18 *cx)
718 if (atomic_read(&cx->ana_capturing)) {
719 h = cx18_find_handle(cx);
720 if (h != CX18_INVALID_TASK_HANDLE) {
721 cx18_msleep_timeout(100, 0);
722 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
723 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
725 CX18_ERR("Can't find valid task handle for unmute\n");
727 CX18_DEBUG_INFO("Unmute\n");