blob: 2c8117510fb4effde5d705a5e2a0ed66f4dfc60c [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
70 /* CX18_DEC_STREAM_TYPE_MPG needs to claim CX18_DEC_STREAM_TYPE_VBI,
71 CX18_ENC_STREAM_TYPE_MPG needs to claim CX18_ENC_STREAM_TYPE_VBI
72 (provided VBI insertion is on and sliced VBI is selected), for all
73 other streams we're done */
74 if (type == CX18_ENC_STREAM_TYPE_MPG &&
75 cx->vbi.insert_mpeg && cx->vbi.sliced_in->service_set) {
76 vbi_type = CX18_ENC_STREAM_TYPE_VBI;
77 } else {
78 return 0;
79 }
80 s_vbi = &cx->streams[vbi_type];
81
82 set_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
83
84 /* mark that it is used internally */
85 set_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags);
86 return 0;
87}
88
89/* This function releases a previously claimed stream. It will take into
90 account associated VBI streams. */
Adrian Bunk50510992008-05-05 18:25:22 -030091static void cx18_release_stream(struct cx18_stream *s)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030092{
93 struct cx18 *cx = s->cx;
94 struct cx18_stream *s_vbi;
95
96 s->id = -1;
97 if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
98 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
99 /* this stream is still in use internally */
100 return;
101 }
102 if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
103 CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
104 return;
105 }
106
107 cx18_flush_queues(s);
108
109 /* CX18_ENC_STREAM_TYPE_MPG needs to release CX18_ENC_STREAM_TYPE_VBI,
110 for all other streams we're done */
111 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
112 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
113 else
114 return;
115
116 /* clear internal use flag */
117 if (!test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
118 /* was already cleared */
119 return;
120 }
121 if (s_vbi->id != -1) {
122 /* VBI stream still claimed by a file descriptor */
123 return;
124 }
125 clear_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
126 cx18_flush_queues(s_vbi);
127}
128
129static void cx18_dualwatch(struct cx18 *cx)
130{
131 struct v4l2_tuner vt;
132 u16 new_bitmap;
133 u16 new_stereo_mode;
134 const u16 stereo_mask = 0x0300;
135 const u16 dual = 0x0200;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300136 u32 h;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300137
138 new_stereo_mode = cx->params.audio_properties & stereo_mask;
139 memset(&vt, 0, sizeof(vt));
140 cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, &vt);
141 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
142 (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
143 new_stereo_mode = dual;
144
145 if (new_stereo_mode == cx->dualwatch_stereo_mode)
146 return;
147
Andy Wallsd3c5e702008-08-23 16:42:29 -0300148 new_bitmap = new_stereo_mode
149 | (cx->params.audio_properties & ~stereo_mask);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300150
Andy Wallsd3c5e702008-08-23 16:42:29 -0300151 CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. "
152 "new audio_bitmask=0x%ux\n",
153 cx->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300154
Andy Wallsd3c5e702008-08-23 16:42:29 -0300155 h = cx18_find_handle(cx);
156 if (h == CX18_INVALID_TASK_HANDLE) {
157 CX18_DEBUG_INFO("dualwatch: can't find valid task handle\n");
158 return;
159 }
160
161 if (cx18_vapi(cx,
162 CX18_CPU_SET_AUDIO_PARAMETERS, 2, h, new_bitmap) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300163 cx->dualwatch_stereo_mode = new_stereo_mode;
164 return;
165 }
166 CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
167}
168
169
170static struct cx18_buffer *cx18_get_buffer(struct cx18_stream *s, int non_block, int *err)
171{
172 struct cx18 *cx = s->cx;
173 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
174 struct cx18_buffer *buf;
175 DEFINE_WAIT(wait);
176
177 *err = 0;
178 while (1) {
179 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
180
181 if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
182 cx->dualwatch_jiffies = jiffies;
183 cx18_dualwatch(cx);
184 }
185 if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
186 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
187 while ((buf = cx18_dequeue(s_vbi, &s_vbi->q_full))) {
188 /* byteswap and process VBI data */
189/* cx18_process_vbi_data(cx, buf, s_vbi->dma_pts, s_vbi->type); */
190 cx18_enqueue(s_vbi, buf, &s_vbi->q_free);
191 }
192 }
193 buf = &cx->vbi.sliced_mpeg_buf;
194 if (buf->readpos != buf->bytesused)
195 return buf;
196 }
197
198 /* do we have leftover data? */
199 buf = cx18_dequeue(s, &s->q_io);
200 if (buf)
201 return buf;
202
203 /* do we have new data? */
204 buf = cx18_dequeue(s, &s->q_full);
205 if (buf) {
206 if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
207 &buf->b_flags))
208 return buf;
209 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
210 /* byteswap MPG data */
211 cx18_buf_swap(buf);
212 else {
213 /* byteswap and process VBI data */
214 cx18_process_vbi_data(cx, buf,
215 s->dma_pts, s->type);
216 }
217 return buf;
218 }
219
220 /* return if end of stream */
221 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
222 CX18_DEBUG_INFO("EOS %s\n", s->name);
223 return NULL;
224 }
225
226 /* return if file was opened with O_NONBLOCK */
227 if (non_block) {
228 *err = -EAGAIN;
229 return NULL;
230 }
231
232 /* wait for more data to arrive */
233 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
234 /* New buffers might have become available before we were added
235 to the waitqueue */
Andy Wallsb04bce42008-08-22 21:03:11 -0300236 if (!atomic_read(&s->q_full.buffers))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300237 schedule();
238 finish_wait(&s->waitq, &wait);
239 if (signal_pending(current)) {
240 /* return if a signal was received */
241 CX18_DEBUG_INFO("User stopped %s\n", s->name);
242 *err = -EINTR;
243 return NULL;
244 }
245 }
246}
247
248static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
249{
250 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
251
252 cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
253 cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
254 cx->vbi.sliced_mpeg_buf.readpos = 0;
255}
256
257static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
258 struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
259{
260 struct cx18 *cx = s->cx;
261 size_t len = buf->bytesused - buf->readpos;
262
263 if (len > ucount)
264 len = ucount;
265 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
266 cx->vbi.sliced_in->service_set && buf != &cx->vbi.sliced_mpeg_buf) {
267 const char *start = buf->buf + buf->readpos;
268 const char *p = start + 1;
269 const u8 *q;
270 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
271 int stuffing, i;
272
273 while (start + len > p) {
274 q = memchr(p, 0, start + len - p);
275 if (q == NULL)
276 break;
277 p = q + 1;
278 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
279 q[1] != 0 || q[2] != 1 || q[3] != ch)
280 continue;
281 if (!cx->search_pack_header) {
282 if ((q[6] & 0xc0) != 0x80)
283 continue;
284 if (((q[7] & 0xc0) == 0x80 &&
285 (q[9] & 0xf0) == 0x20) ||
286 ((q[7] & 0xc0) == 0xc0 &&
287 (q[9] & 0xf0) == 0x30)) {
288 ch = 0xba;
289 cx->search_pack_header = 1;
290 p = q + 9;
291 }
292 continue;
293 }
294 stuffing = q[13] & 7;
295 /* all stuffing bytes must be 0xff */
296 for (i = 0; i < stuffing; i++)
297 if (q[14 + i] != 0xff)
298 break;
299 if (i == stuffing &&
300 (q[4] & 0xc4) == 0x44 &&
301 (q[12] & 3) == 3 &&
302 q[14 + stuffing] == 0 &&
303 q[15 + stuffing] == 0 &&
304 q[16 + stuffing] == 1) {
305 cx->search_pack_header = 0;
306 len = (char *)q - start;
307 cx18_setup_sliced_vbi_buf(cx);
308 break;
309 }
310 }
311 }
312 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
313 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
314 len, s->name);
315 return -EFAULT;
316 }
317 buf->readpos += len;
318 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
319 buf != &cx->vbi.sliced_mpeg_buf)
320 cx->mpg_data_received += len;
321 return len;
322}
323
324static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
325 size_t tot_count, int non_block)
326{
327 struct cx18 *cx = s->cx;
328 size_t tot_written = 0;
329 int single_frame = 0;
330
Hans Verkuil31554ae2008-05-25 11:21:27 -0300331 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300332 /* shouldn't happen */
333 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
334 s->name);
335 return -EIO;
336 }
337
338 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
339 frames should arrive one-by-one, so make sure we never output more
340 than one VBI frame at a time */
341 if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
342 cx->vbi.sliced_in->service_set)
343 single_frame = 1;
344
345 for (;;) {
346 struct cx18_buffer *buf;
347 int rc;
348
349 buf = cx18_get_buffer(s, non_block, &rc);
350 /* if there is no data available... */
351 if (buf == NULL) {
352 /* if we got data, then return that regardless */
353 if (tot_written)
354 break;
355 /* EOS condition */
356 if (rc == 0) {
357 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
358 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
359 cx18_release_stream(s);
360 }
361 /* set errno */
362 return rc;
363 }
364
365 rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
366 tot_count - tot_written);
367
368 if (buf != &cx->vbi.sliced_mpeg_buf) {
369 if (buf->readpos == buf->bytesused) {
370 cx18_buf_sync_for_device(s, buf);
371 cx18_enqueue(s, buf, &s->q_free);
372 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5,
373 s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300374 (void __iomem *)&cx->scb->cpu_mdl[buf->id] -
375 cx->enc_mem,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300376 1, buf->id, s->buf_size);
377 } else
378 cx18_enqueue(s, buf, &s->q_io);
379 } else if (buf->readpos == buf->bytesused) {
380 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
381
382 cx->vbi.sliced_mpeg_size[idx] = 0;
383 cx->vbi.inserted_frame++;
384 cx->vbi_data_inserted += buf->bytesused;
385 }
386 if (rc < 0)
387 return rc;
388 tot_written += rc;
389
390 if (tot_written == tot_count || single_frame)
391 break;
392 }
393 return tot_written;
394}
395
396static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
397 size_t count, loff_t *pos, int non_block)
398{
399 ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
400 struct cx18 *cx = s->cx;
401
402 CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
403 if (rc > 0)
404 pos += rc;
405 return rc;
406}
407
408int cx18_start_capture(struct cx18_open_id *id)
409{
410 struct cx18 *cx = id->cx;
411 struct cx18_stream *s = &cx->streams[id->type];
412 struct cx18_stream *s_vbi;
413
414 if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
415 /* you cannot read from these stream types. */
416 return -EPERM;
417 }
418
419 /* Try to claim this stream. */
420 if (cx18_claim_stream(id, s->type))
421 return -EBUSY;
422
423 /* If capture is already in progress, then we also have to
424 do nothing extra. */
425 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
426 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
427 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
428 return 0;
429 }
430
431 /* Start VBI capture if required */
432 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
433 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
434 test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
435 !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
436 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
437 automatically when the MPG stream is claimed.
438 We only need to start the VBI capturing. */
439 if (cx18_start_v4l2_encode_stream(s_vbi)) {
440 CX18_DEBUG_WARN("VBI capture start failed\n");
441
442 /* Failure, clean up and return an error */
443 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
444 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
445 /* also releases the associated VBI stream */
446 cx18_release_stream(s);
447 return -EIO;
448 }
449 CX18_DEBUG_INFO("VBI insertion started\n");
450 }
451
452 /* Tell the card to start capturing */
453 if (!cx18_start_v4l2_encode_stream(s)) {
454 /* We're done */
455 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
456 /* Resume a possibly paused encoder */
457 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
458 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
459 return 0;
460 }
461
462 /* failure, clean up */
463 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
464
465 /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
466 automatically when the MPG stream is released.
467 We only need to stop the VBI capturing. */
468 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
469 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
470 cx18_stop_v4l2_encode_stream(s_vbi, 0);
471 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
472 }
473 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
474 cx18_release_stream(s);
475 return -EIO;
476}
477
478ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
479 loff_t *pos)
480{
481 struct cx18_open_id *id = filp->private_data;
482 struct cx18 *cx = id->cx;
483 struct cx18_stream *s = &cx->streams[id->type];
484 int rc;
485
486 CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
487
488 mutex_lock(&cx->serialize_lock);
489 rc = cx18_start_capture(id);
490 mutex_unlock(&cx->serialize_lock);
491 if (rc)
492 return rc;
493 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
494}
495
496unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
497{
498 struct cx18_open_id *id = filp->private_data;
499 struct cx18 *cx = id->cx;
500 struct cx18_stream *s = &cx->streams[id->type];
501 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
502
503 /* Start a capture if there is none */
504 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
505 int rc;
506
507 mutex_lock(&cx->serialize_lock);
508 rc = cx18_start_capture(id);
509 mutex_unlock(&cx->serialize_lock);
510 if (rc) {
511 CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
512 s->name, rc);
513 return POLLERR;
514 }
515 CX18_DEBUG_FILE("Encoder poll started capture\n");
516 }
517
518 /* add stream's waitq to the poll list */
519 CX18_DEBUG_HI_FILE("Encoder poll\n");
520 poll_wait(filp, &s->waitq, wait);
521
Andy Wallsb04bce42008-08-22 21:03:11 -0300522 if (atomic_read(&s->q_full.buffers) || atomic_read(&s->q_io.buffers))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300523 return POLLIN | POLLRDNORM;
524 if (eof)
525 return POLLHUP;
526 return 0;
527}
528
529void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
530{
531 struct cx18 *cx = id->cx;
532 struct cx18_stream *s = &cx->streams[id->type];
533
534 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
535
536 /* 'Unclaim' this stream */
537
538 /* Stop capturing */
539 if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
540 struct cx18_stream *s_vbi =
541 &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
542
543 CX18_DEBUG_INFO("close stopping capture\n");
544 /* Special case: a running VBI capture for VBI insertion
545 in the mpeg stream. Need to stop that too. */
546 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
547 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
548 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
549 CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
550 cx18_stop_v4l2_encode_stream(s_vbi, 0);
551 }
552 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
553 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
554 /* Also used internally, don't stop capturing */
555 s->id = -1;
556 else
557 cx18_stop_v4l2_encode_stream(s, gop_end);
558 }
559 if (!gop_end) {
560 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
561 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
562 cx18_release_stream(s);
563 }
564}
565
566int cx18_v4l2_close(struct inode *inode, struct file *filp)
567{
568 struct cx18_open_id *id = filp->private_data;
569 struct cx18 *cx = id->cx;
570 struct cx18_stream *s = &cx->streams[id->type];
571
572 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
573
574 v4l2_prio_close(&cx->prio, &id->prio);
575
576 /* Easy case first: this stream was never claimed by us */
577 if (s->id != id->open_id) {
578 kfree(id);
579 return 0;
580 }
581
582 /* 'Unclaim' this stream */
583
584 /* Stop radio */
585 mutex_lock(&cx->serialize_lock);
586 if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
587 /* Closing radio device, return to TV mode */
588 cx18_mute(cx);
589 /* Mark that the radio is no longer in use */
590 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
591 /* Switch tuner to TV */
592 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
593 /* Select correct audio input (i.e. TV tuner or Line in) */
594 cx18_audio_set_io(cx);
Hans Verkuil31554ae2008-05-25 11:21:27 -0300595 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300596 /* Undo video mute */
597 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
598 cx->params.video_mute |
599 (cx->params.video_mute_yuv << 8));
600 }
601 /* Done! Unmute and continue. */
602 cx18_unmute(cx);
603 cx18_release_stream(s);
604 } else {
605 cx18_stop_capture(id, 0);
606 }
607 kfree(id);
608 mutex_unlock(&cx->serialize_lock);
609 return 0;
610}
611
612static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
613{
614 struct cx18 *cx = s->cx;
615 struct cx18_open_id *item;
616
617 CX18_DEBUG_FILE("open %s\n", s->name);
618
619 /* Allocate memory */
620 item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
621 if (NULL == item) {
622 CX18_DEBUG_WARN("nomem on v4l2 open\n");
623 return -ENOMEM;
624 }
625 item->cx = cx;
626 item->type = s->type;
627 v4l2_prio_open(&cx->prio, &item->prio);
628
629 item->open_id = cx->open_id++;
630 filp->private_data = item;
631
632 if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
633 /* Try to claim this stream */
634 if (cx18_claim_stream(item, item->type)) {
635 /* No, it's already in use */
636 kfree(item);
637 return -EBUSY;
638 }
639
640 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
Hans Verkuil31554ae2008-05-25 11:21:27 -0300641 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300642 /* switching to radio while capture is
643 in progress is not polite */
644 cx18_release_stream(s);
645 kfree(item);
646 return -EBUSY;
647 }
648 }
649
650 /* Mark that the radio is being used. */
651 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
652 /* We have the radio */
653 cx18_mute(cx);
654 /* Switch tuner to radio */
655 cx18_call_i2c_clients(cx, AUDC_SET_RADIO, NULL);
656 /* Select the correct audio input (i.e. radio tuner) */
657 cx18_audio_set_io(cx);
658 /* Done! Unmute and continue. */
659 cx18_unmute(cx);
660 }
661 return 0;
662}
663
664int cx18_v4l2_open(struct inode *inode, struct file *filp)
665{
666 int res, x, y = 0;
667 struct cx18 *cx = NULL;
668 struct cx18_stream *s = NULL;
669 int minor = iminor(inode);
670
671 /* Find which card this open was on */
672 spin_lock(&cx18_cards_lock);
673 for (x = 0; cx == NULL && x < cx18_cards_active; x++) {
674 /* find out which stream this open was on */
675 for (y = 0; y < CX18_MAX_STREAMS; y++) {
Andy Walls07c87a82008-05-12 15:01:27 -0300676 if (cx18_cards[x] == NULL)
677 continue;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300678 s = &cx18_cards[x]->streams[y];
679 if (s->v4l2dev && s->v4l2dev->minor == minor) {
680 cx = cx18_cards[x];
681 break;
682 }
683 }
684 }
685 spin_unlock(&cx18_cards_lock);
686
687 if (cx == NULL) {
688 /* Couldn't find a device registered
689 on that minor, shouldn't happen! */
690 printk(KERN_WARNING "No cx18 device found on minor %d\n",
691 minor);
692 return -ENXIO;
693 }
694
695 mutex_lock(&cx->serialize_lock);
696 if (cx18_init_on_first_open(cx)) {
697 CX18_ERR("Failed to initialize on minor %d\n", minor);
698 mutex_unlock(&cx->serialize_lock);
699 return -ENXIO;
700 }
701 res = cx18_serialized_open(s, filp);
702 mutex_unlock(&cx->serialize_lock);
703 return res;
704}
705
706void cx18_mute(struct cx18 *cx)
707{
Andy Wallsd3c5e702008-08-23 16:42:29 -0300708 u32 h;
709 if (atomic_read(&cx->ana_capturing)) {
710 h = cx18_find_handle(cx);
711 if (h != CX18_INVALID_TASK_HANDLE)
712 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
713 else
714 CX18_ERR("Can't find valid task handle for mute\n");
715 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300716 CX18_DEBUG_INFO("Mute\n");
717}
718
719void cx18_unmute(struct cx18 *cx)
720{
Andy Wallsd3c5e702008-08-23 16:42:29 -0300721 u32 h;
Hans Verkuil31554ae2008-05-25 11:21:27 -0300722 if (atomic_read(&cx->ana_capturing)) {
Andy Wallsd3c5e702008-08-23 16:42:29 -0300723 h = cx18_find_handle(cx);
724 if (h != CX18_INVALID_TASK_HANDLE) {
725 cx18_msleep_timeout(100, 0);
726 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
727 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
728 } else
729 CX18_ERR("Can't find valid task handle for unmute\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300730 }
731 CX18_DEBUG_INFO("Unmute\n");
732}