blob: 0b1dbc67e1ab89c13dca10a2fe740d2ee62b5707 [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 Walls1ed9dcc2008-11-22 01:37:34 -03007 * Copyright (C) 2008 Andy Walls <awalls@radix.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
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. */
Adrian Bunk50510992008-05-05 18:25:22 -030043static int 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];
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
Andy Wallsdd073432008-12-12 16:24:04 -030070 /* CX18_ENC_STREAM_TYPE_MPG needs to claim CX18_ENC_STREAM_TYPE_VBI
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030071 (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 &&
Andy Wallsdd073432008-12-12 16:24:04 -030074 cx->vbi.insert_mpeg && !cx18_raw_vbi(cx)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030075 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. */
Adrian Bunk50510992008-05-05 18:25:22 -030090static void cx18_release_stream(struct cx18_stream *s)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030091{
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;
Andy Walls0d82fe82009-01-01 19:02:31 -0300131 u32 new_bitmap;
132 u32 new_stereo_mode;
133 const u32 stereo_mask = 0x0300;
134 const u32 dual = 0x0200;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300135 u32 h;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300136
137 new_stereo_mode = cx->params.audio_properties & stereo_mask;
138 memset(&vt, 0, sizeof(vt));
139 cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, &vt);
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
Andy Wallsd3c5e702008-08-23 16:42:29 -0300147 new_bitmap = new_stereo_mode
148 | (cx->params.audio_properties & ~stereo_mask);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300149
Andy Wallsd3c5e702008-08-23 16:42:29 -0300150 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);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300153
Andy Wallsd3c5e702008-08-23 16:42:29 -0300154 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) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300162 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) {
Andy Walls302df972009-01-31 00:33:02 -0300179 /* Process pending program info updates and pending
180 VBI data */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300181
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 */
Andy Wallsaf009cf2008-12-12 20:00:29 -0300190 cx18_process_vbi_data(cx, buf,
191 s_vbi->dma_pts,
192 s_vbi->type);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300193 cx18_stream_put_buf_fw(s_vbi, buf);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300194 }
195 }
196 buf = &cx->vbi.sliced_mpeg_buf;
197 if (buf->readpos != buf->bytesused)
198 return buf;
199 }
200
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300201 /* do we have new data? */
202 buf = cx18_dequeue(s, &s->q_full);
203 if (buf) {
204 if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
205 &buf->b_flags))
206 return buf;
207 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
208 /* byteswap MPG data */
209 cx18_buf_swap(buf);
210 else {
211 /* byteswap and process VBI data */
212 cx18_process_vbi_data(cx, buf,
213 s->dma_pts, s->type);
214 }
215 return buf;
216 }
217
218 /* return if end of stream */
219 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
220 CX18_DEBUG_INFO("EOS %s\n", s->name);
221 return NULL;
222 }
223
224 /* return if file was opened with O_NONBLOCK */
225 if (non_block) {
226 *err = -EAGAIN;
227 return NULL;
228 }
229
230 /* wait for more data to arrive */
231 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
232 /* New buffers might have become available before we were added
233 to the waitqueue */
Andy Wallsb04bce42008-08-22 21:03:11 -0300234 if (!atomic_read(&s->q_full.buffers))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300235 schedule();
236 finish_wait(&s->waitq, &wait);
237 if (signal_pending(current)) {
238 /* return if a signal was received */
239 CX18_DEBUG_INFO("User stopped %s\n", s->name);
240 *err = -EINTR;
241 return NULL;
242 }
243 }
244}
245
246static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
247{
248 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
249
250 cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
251 cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
252 cx->vbi.sliced_mpeg_buf.readpos = 0;
253}
254
255static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
256 struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
257{
258 struct cx18 *cx = s->cx;
259 size_t len = buf->bytesused - buf->readpos;
260
261 if (len > ucount)
262 len = ucount;
263 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
Andy Wallsdd073432008-12-12 16:24:04 -0300264 !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
Andy Walls302df972009-01-31 00:33:02 -0300265 /*
266 * Try to find a good splice point in the PS, just before
267 * an MPEG-2 Program Pack start code, and provide only
268 * up to that point to the user, so it's easy to insert VBI data
269 * the next time around.
270 */
271 /* FIXME - This only works for an MPEG-2 PS, not a TS */
272 /*
273 * An MPEG-2 Program Stream (PS) is a series of
274 * MPEG-2 Program Packs terminated by an
275 * MPEG Program End Code after the last Program Pack.
276 * A Program Pack may hold a PS System Header packet and any
277 * number of Program Elementary Stream (PES) Packets
278 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300279 const char *start = buf->buf + buf->readpos;
280 const char *p = start + 1;
281 const u8 *q;
282 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
283 int stuffing, i;
284
285 while (start + len > p) {
Andy Walls302df972009-01-31 00:33:02 -0300286 /* Scan for a 0 to find a potential MPEG-2 start code */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300287 q = memchr(p, 0, start + len - p);
288 if (q == NULL)
289 break;
290 p = q + 1;
Andy Walls302df972009-01-31 00:33:02 -0300291 /*
292 * Keep looking if not a
293 * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
294 * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
295 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300296 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
297 q[1] != 0 || q[2] != 1 || q[3] != ch)
298 continue;
Andy Walls302df972009-01-31 00:33:02 -0300299
300 /* If expecting the primary video PES */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300301 if (!cx->search_pack_header) {
Andy Walls302df972009-01-31 00:33:02 -0300302 /* Continue if it couldn't be a PES packet */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300303 if ((q[6] & 0xc0) != 0x80)
304 continue;
Andy Walls302df972009-01-31 00:33:02 -0300305 /* Check if a PTS or PTS & DTS follow */
306 if (((q[7] & 0xc0) == 0x80 && /* PTS only */
307 (q[9] & 0xf0) == 0x20) || /* PTS only */
308 ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
309 (q[9] & 0xf0) == 0x30)) { /* DTS follows */
310 /* Assume we found the video PES hdr */
311 ch = 0xba; /* next want a Program Pack*/
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300312 cx->search_pack_header = 1;
Andy Walls302df972009-01-31 00:33:02 -0300313 p = q + 9; /* Skip this video PES hdr */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300314 }
315 continue;
316 }
Andy Walls302df972009-01-31 00:33:02 -0300317
318 /* We may have found a Program Pack start code */
319
320 /* Get the count of stuffing bytes & verify them */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300321 stuffing = q[13] & 7;
322 /* all stuffing bytes must be 0xff */
323 for (i = 0; i < stuffing; i++)
324 if (q[14 + i] != 0xff)
325 break;
Andy Walls302df972009-01-31 00:33:02 -0300326 if (i == stuffing && /* right number of stuffing bytes*/
327 (q[4] & 0xc4) == 0x44 && /* marker check */
328 (q[12] & 3) == 3 && /* marker check */
329 q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300330 q[15 + stuffing] == 0 &&
331 q[16 + stuffing] == 1) {
Andy Walls302df972009-01-31 00:33:02 -0300332 /* We declare we actually found a Program Pack*/
333 cx->search_pack_header = 0; /* expect vid PES */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300334 len = (char *)q - start;
335 cx18_setup_sliced_vbi_buf(cx);
336 break;
337 }
338 }
339 }
340 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
341 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
342 len, s->name);
343 return -EFAULT;
344 }
345 buf->readpos += len;
346 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
347 buf != &cx->vbi.sliced_mpeg_buf)
348 cx->mpg_data_received += len;
349 return len;
350}
351
352static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
353 size_t tot_count, int non_block)
354{
355 struct cx18 *cx = s->cx;
356 size_t tot_written = 0;
357 int single_frame = 0;
358
Hans Verkuil31554ae2008-05-25 11:21:27 -0300359 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300360 /* shouldn't happen */
361 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
362 s->name);
363 return -EIO;
364 }
365
366 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
367 frames should arrive one-by-one, so make sure we never output more
368 than one VBI frame at a time */
Andy Wallsdd073432008-12-12 16:24:04 -0300369 if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300370 single_frame = 1;
371
372 for (;;) {
373 struct cx18_buffer *buf;
374 int rc;
375
376 buf = cx18_get_buffer(s, non_block, &rc);
377 /* if there is no data available... */
378 if (buf == NULL) {
379 /* if we got data, then return that regardless */
380 if (tot_written)
381 break;
382 /* EOS condition */
383 if (rc == 0) {
384 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
385 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
386 cx18_release_stream(s);
387 }
388 /* set errno */
389 return rc;
390 }
391
392 rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
393 tot_count - tot_written);
394
395 if (buf != &cx->vbi.sliced_mpeg_buf) {
Andy Walls66c2a6b2008-12-08 23:02:45 -0300396 if (buf->readpos == buf->bytesused)
397 cx18_stream_put_buf_fw(s, buf);
398 else
Andy Wallsb80e1072008-11-28 00:04:21 -0300399 cx18_push(s, buf, &s->q_full);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300400 } else if (buf->readpos == buf->bytesused) {
401 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
402
403 cx->vbi.sliced_mpeg_size[idx] = 0;
404 cx->vbi.inserted_frame++;
405 cx->vbi_data_inserted += buf->bytesused;
406 }
407 if (rc < 0)
408 return rc;
409 tot_written += rc;
410
411 if (tot_written == tot_count || single_frame)
412 break;
413 }
414 return tot_written;
415}
416
417static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
418 size_t count, loff_t *pos, int non_block)
419{
420 ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
421 struct cx18 *cx = s->cx;
422
423 CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
424 if (rc > 0)
425 pos += rc;
426 return rc;
427}
428
429int cx18_start_capture(struct cx18_open_id *id)
430{
431 struct cx18 *cx = id->cx;
432 struct cx18_stream *s = &cx->streams[id->type];
433 struct cx18_stream *s_vbi;
434
435 if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
436 /* you cannot read from these stream types. */
437 return -EPERM;
438 }
439
440 /* Try to claim this stream. */
441 if (cx18_claim_stream(id, s->type))
442 return -EBUSY;
443
444 /* If capture is already in progress, then we also have to
445 do nothing extra. */
446 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
447 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
448 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
449 return 0;
450 }
451
452 /* Start VBI capture if required */
453 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
454 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
455 test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
456 !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
457 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
458 automatically when the MPG stream is claimed.
459 We only need to start the VBI capturing. */
460 if (cx18_start_v4l2_encode_stream(s_vbi)) {
461 CX18_DEBUG_WARN("VBI capture start failed\n");
462
463 /* Failure, clean up and return an error */
464 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
465 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
466 /* also releases the associated VBI stream */
467 cx18_release_stream(s);
468 return -EIO;
469 }
470 CX18_DEBUG_INFO("VBI insertion started\n");
471 }
472
473 /* Tell the card to start capturing */
474 if (!cx18_start_v4l2_encode_stream(s)) {
475 /* We're done */
476 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
477 /* Resume a possibly paused encoder */
478 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
479 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
480 return 0;
481 }
482
483 /* failure, clean up */
484 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
485
486 /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
487 automatically when the MPG stream is released.
488 We only need to stop the VBI capturing. */
489 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
490 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
491 cx18_stop_v4l2_encode_stream(s_vbi, 0);
492 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
493 }
494 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
495 cx18_release_stream(s);
496 return -EIO;
497}
498
499ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
500 loff_t *pos)
501{
502 struct cx18_open_id *id = filp->private_data;
503 struct cx18 *cx = id->cx;
504 struct cx18_stream *s = &cx->streams[id->type];
505 int rc;
506
507 CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
508
509 mutex_lock(&cx->serialize_lock);
510 rc = cx18_start_capture(id);
511 mutex_unlock(&cx->serialize_lock);
512 if (rc)
513 return rc;
514 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
515}
516
517unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
518{
519 struct cx18_open_id *id = filp->private_data;
520 struct cx18 *cx = id->cx;
521 struct cx18_stream *s = &cx->streams[id->type];
522 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
523
524 /* Start a capture if there is none */
525 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
526 int rc;
527
528 mutex_lock(&cx->serialize_lock);
529 rc = cx18_start_capture(id);
530 mutex_unlock(&cx->serialize_lock);
531 if (rc) {
532 CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
533 s->name, rc);
534 return POLLERR;
535 }
536 CX18_DEBUG_FILE("Encoder poll started capture\n");
537 }
538
539 /* add stream's waitq to the poll list */
540 CX18_DEBUG_HI_FILE("Encoder poll\n");
541 poll_wait(filp, &s->waitq, wait);
542
Andy Wallsb80e1072008-11-28 00:04:21 -0300543 if (atomic_read(&s->q_full.buffers))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300544 return POLLIN | POLLRDNORM;
545 if (eof)
546 return POLLHUP;
547 return 0;
548}
549
550void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
551{
552 struct cx18 *cx = id->cx;
553 struct cx18_stream *s = &cx->streams[id->type];
554
555 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
556
557 /* 'Unclaim' this stream */
558
559 /* Stop capturing */
560 if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
561 struct cx18_stream *s_vbi =
562 &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
563
564 CX18_DEBUG_INFO("close stopping capture\n");
565 /* Special case: a running VBI capture for VBI insertion
566 in the mpeg stream. Need to stop that too. */
567 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
568 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
569 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
570 CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
571 cx18_stop_v4l2_encode_stream(s_vbi, 0);
572 }
573 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
574 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
575 /* Also used internally, don't stop capturing */
576 s->id = -1;
577 else
578 cx18_stop_v4l2_encode_stream(s, gop_end);
579 }
580 if (!gop_end) {
581 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
582 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
583 cx18_release_stream(s);
584 }
585}
586
Hans Verkuilbec43662008-12-30 06:58:20 -0300587int cx18_v4l2_close(struct file *filp)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300588{
589 struct cx18_open_id *id = filp->private_data;
590 struct cx18 *cx = id->cx;
591 struct cx18_stream *s = &cx->streams[id->type];
592
593 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
594
595 v4l2_prio_close(&cx->prio, &id->prio);
596
597 /* Easy case first: this stream was never claimed by us */
598 if (s->id != id->open_id) {
599 kfree(id);
600 return 0;
601 }
602
603 /* 'Unclaim' this stream */
604
605 /* Stop radio */
606 mutex_lock(&cx->serialize_lock);
607 if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
608 /* Closing radio device, return to TV mode */
609 cx18_mute(cx);
610 /* Mark that the radio is no longer in use */
611 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
612 /* Switch tuner to TV */
613 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
614 /* Select correct audio input (i.e. TV tuner or Line in) */
615 cx18_audio_set_io(cx);
Hans Verkuil31554ae2008-05-25 11:21:27 -0300616 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300617 /* Undo video mute */
618 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
619 cx->params.video_mute |
620 (cx->params.video_mute_yuv << 8));
621 }
622 /* Done! Unmute and continue. */
623 cx18_unmute(cx);
624 cx18_release_stream(s);
625 } else {
626 cx18_stop_capture(id, 0);
627 }
628 kfree(id);
629 mutex_unlock(&cx->serialize_lock);
630 return 0;
631}
632
633static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
634{
635 struct cx18 *cx = s->cx;
636 struct cx18_open_id *item;
637
638 CX18_DEBUG_FILE("open %s\n", s->name);
639
640 /* Allocate memory */
641 item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
642 if (NULL == item) {
643 CX18_DEBUG_WARN("nomem on v4l2 open\n");
644 return -ENOMEM;
645 }
646 item->cx = cx;
647 item->type = s->type;
648 v4l2_prio_open(&cx->prio, &item->prio);
649
650 item->open_id = cx->open_id++;
651 filp->private_data = item;
652
653 if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
654 /* Try to claim this stream */
655 if (cx18_claim_stream(item, item->type)) {
656 /* No, it's already in use */
657 kfree(item);
658 return -EBUSY;
659 }
660
661 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
Hans Verkuil31554ae2008-05-25 11:21:27 -0300662 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300663 /* switching to radio while capture is
664 in progress is not polite */
665 cx18_release_stream(s);
666 kfree(item);
667 return -EBUSY;
668 }
669 }
670
671 /* Mark that the radio is being used. */
672 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
673 /* We have the radio */
674 cx18_mute(cx);
675 /* Switch tuner to radio */
676 cx18_call_i2c_clients(cx, AUDC_SET_RADIO, NULL);
677 /* Select the correct audio input (i.e. radio tuner) */
678 cx18_audio_set_io(cx);
679 /* Done! Unmute and continue. */
680 cx18_unmute(cx);
681 }
682 return 0;
683}
684
Hans Verkuilbec43662008-12-30 06:58:20 -0300685int cx18_v4l2_open(struct file *filp)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300686{
687 int res, x, y = 0;
688 struct cx18 *cx = NULL;
689 struct cx18_stream *s = NULL;
Hans Verkuilbec43662008-12-30 06:58:20 -0300690 int minor = video_devdata(filp)->minor;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300691
692 /* Find which card this open was on */
693 spin_lock(&cx18_cards_lock);
694 for (x = 0; cx == NULL && x < cx18_cards_active; x++) {
695 /* find out which stream this open was on */
696 for (y = 0; y < CX18_MAX_STREAMS; y++) {
Andy Walls07c87a82008-05-12 15:01:27 -0300697 if (cx18_cards[x] == NULL)
698 continue;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300699 s = &cx18_cards[x]->streams[y];
Andy Walls3d059132009-01-10 21:54:39 -0300700 if (s->video_dev && s->video_dev->minor == minor) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300701 cx = cx18_cards[x];
702 break;
703 }
704 }
705 }
706 spin_unlock(&cx18_cards_lock);
707
708 if (cx == NULL) {
709 /* Couldn't find a device registered
710 on that minor, shouldn't happen! */
711 printk(KERN_WARNING "No cx18 device found on minor %d\n",
712 minor);
713 return -ENXIO;
714 }
715
716 mutex_lock(&cx->serialize_lock);
717 if (cx18_init_on_first_open(cx)) {
718 CX18_ERR("Failed to initialize on minor %d\n", minor);
719 mutex_unlock(&cx->serialize_lock);
720 return -ENXIO;
721 }
722 res = cx18_serialized_open(s, filp);
723 mutex_unlock(&cx->serialize_lock);
724 return res;
725}
726
727void cx18_mute(struct cx18 *cx)
728{
Andy Wallsd3c5e702008-08-23 16:42:29 -0300729 u32 h;
730 if (atomic_read(&cx->ana_capturing)) {
731 h = cx18_find_handle(cx);
732 if (h != CX18_INVALID_TASK_HANDLE)
733 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
734 else
735 CX18_ERR("Can't find valid task handle for mute\n");
736 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300737 CX18_DEBUG_INFO("Mute\n");
738}
739
740void cx18_unmute(struct cx18 *cx)
741{
Andy Wallsd3c5e702008-08-23 16:42:29 -0300742 u32 h;
Hans Verkuil31554ae2008-05-25 11:21:27 -0300743 if (atomic_read(&cx->ana_capturing)) {
Andy Wallsd3c5e702008-08-23 16:42:29 -0300744 h = cx18_find_handle(cx);
745 if (h != CX18_INVALID_TASK_HANDLE) {
746 cx18_msleep_timeout(100, 0);
747 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
748 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
749 } else
750 CX18_ERR("Can't find valid task handle for unmute\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300751 }
752 CX18_DEBUG_INFO("Unmute\n");
753}