blob: 1e537fe04a2372196254aa35e6846131f136c02e [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>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24#include "cx18-driver.h"
25#include "cx18-fileops.h"
26#include "cx18-i2c.h"
27#include "cx18-queue.h"
28#include "cx18-vbi.h"
29#include "cx18-audio.h"
30#include "cx18-mailbox.h"
31#include "cx18-scb.h"
32#include "cx18-streams.h"
33#include "cx18-controls.h"
34#include "cx18-ioctl.h"
35#include "cx18-cards.h"
36
37/* This function tries to claim the stream for a specific file descriptor.
38 If no one else is using this stream then the stream is claimed and
39 associated VBI streams are also automatically claimed.
40 Possible error returns: -EBUSY if someone else has claimed
41 the stream or 0 on success. */
Adrian Bunk50510992008-05-05 18:25:22 -030042static int cx18_claim_stream(struct cx18_open_id *id, int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030043{
44 struct cx18 *cx = id->cx;
45 struct cx18_stream *s = &cx->streams[type];
46 struct cx18_stream *s_vbi;
47 int vbi_type;
48
49 if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
50 /* someone already claimed this stream */
51 if (s->id == id->open_id) {
52 /* yes, this file descriptor did. So that's OK. */
53 return 0;
54 }
55 if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
56 /* VBI is handled already internally, now also assign
57 the file descriptor to this stream for external
58 reading of the stream. */
59 s->id = id->open_id;
60 CX18_DEBUG_INFO("Start Read VBI\n");
61 return 0;
62 }
63 /* someone else is using this stream already */
64 CX18_DEBUG_INFO("Stream %d is busy\n", type);
65 return -EBUSY;
66 }
67 s->id = id->open_id;
68
69 /* CX18_DEC_STREAM_TYPE_MPG needs to claim CX18_DEC_STREAM_TYPE_VBI,
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 && cx->vbi.sliced_in->service_set) {
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. */
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;
131 u16 new_bitmap;
132 u16 new_stereo_mode;
133 const u16 stereo_mask = 0x0300;
134 const u16 dual = 0x0200;
135
136 new_stereo_mode = cx->params.audio_properties & stereo_mask;
137 memset(&vt, 0, sizeof(vt));
138 cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, &vt);
139 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
140 (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
141 new_stereo_mode = dual;
142
143 if (new_stereo_mode == cx->dualwatch_stereo_mode)
144 return;
145
146 new_bitmap = new_stereo_mode | (cx->params.audio_properties & ~stereo_mask);
147
148 CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n",
149 cx->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
150
151 if (cx18_vapi(cx, CX18_CPU_SET_AUDIO_PARAMETERS, 2,
152 cx18_find_handle(cx), new_bitmap) == 0) {
153 cx->dualwatch_stereo_mode = new_stereo_mode;
154 return;
155 }
156 CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
157}
158
159
160static struct cx18_buffer *cx18_get_buffer(struct cx18_stream *s, int non_block, int *err)
161{
162 struct cx18 *cx = s->cx;
163 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
164 struct cx18_buffer *buf;
165 DEFINE_WAIT(wait);
166
167 *err = 0;
168 while (1) {
169 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
170
171 if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
172 cx->dualwatch_jiffies = jiffies;
173 cx18_dualwatch(cx);
174 }
175 if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
176 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
177 while ((buf = cx18_dequeue(s_vbi, &s_vbi->q_full))) {
178 /* byteswap and process VBI data */
179/* cx18_process_vbi_data(cx, buf, s_vbi->dma_pts, s_vbi->type); */
180 cx18_enqueue(s_vbi, buf, &s_vbi->q_free);
181 }
182 }
183 buf = &cx->vbi.sliced_mpeg_buf;
184 if (buf->readpos != buf->bytesused)
185 return buf;
186 }
187
188 /* do we have leftover data? */
189 buf = cx18_dequeue(s, &s->q_io);
190 if (buf)
191 return buf;
192
193 /* do we have new data? */
194 buf = cx18_dequeue(s, &s->q_full);
195 if (buf) {
196 if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
197 &buf->b_flags))
198 return buf;
199 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
200 /* byteswap MPG data */
201 cx18_buf_swap(buf);
202 else {
203 /* byteswap and process VBI data */
204 cx18_process_vbi_data(cx, buf,
205 s->dma_pts, s->type);
206 }
207 return buf;
208 }
209
210 /* return if end of stream */
211 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
212 CX18_DEBUG_INFO("EOS %s\n", s->name);
213 return NULL;
214 }
215
216 /* return if file was opened with O_NONBLOCK */
217 if (non_block) {
218 *err = -EAGAIN;
219 return NULL;
220 }
221
222 /* wait for more data to arrive */
223 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
224 /* New buffers might have become available before we were added
225 to the waitqueue */
226 if (!s->q_full.buffers)
227 schedule();
228 finish_wait(&s->waitq, &wait);
229 if (signal_pending(current)) {
230 /* return if a signal was received */
231 CX18_DEBUG_INFO("User stopped %s\n", s->name);
232 *err = -EINTR;
233 return NULL;
234 }
235 }
236}
237
238static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
239{
240 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
241
242 cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
243 cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
244 cx->vbi.sliced_mpeg_buf.readpos = 0;
245}
246
247static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
248 struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
249{
250 struct cx18 *cx = s->cx;
251 size_t len = buf->bytesused - buf->readpos;
252
253 if (len > ucount)
254 len = ucount;
255 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
256 cx->vbi.sliced_in->service_set && buf != &cx->vbi.sliced_mpeg_buf) {
257 const char *start = buf->buf + buf->readpos;
258 const char *p = start + 1;
259 const u8 *q;
260 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
261 int stuffing, i;
262
263 while (start + len > p) {
264 q = memchr(p, 0, start + len - p);
265 if (q == NULL)
266 break;
267 p = q + 1;
268 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
269 q[1] != 0 || q[2] != 1 || q[3] != ch)
270 continue;
271 if (!cx->search_pack_header) {
272 if ((q[6] & 0xc0) != 0x80)
273 continue;
274 if (((q[7] & 0xc0) == 0x80 &&
275 (q[9] & 0xf0) == 0x20) ||
276 ((q[7] & 0xc0) == 0xc0 &&
277 (q[9] & 0xf0) == 0x30)) {
278 ch = 0xba;
279 cx->search_pack_header = 1;
280 p = q + 9;
281 }
282 continue;
283 }
284 stuffing = q[13] & 7;
285 /* all stuffing bytes must be 0xff */
286 for (i = 0; i < stuffing; i++)
287 if (q[14 + i] != 0xff)
288 break;
289 if (i == stuffing &&
290 (q[4] & 0xc4) == 0x44 &&
291 (q[12] & 3) == 3 &&
292 q[14 + stuffing] == 0 &&
293 q[15 + stuffing] == 0 &&
294 q[16 + stuffing] == 1) {
295 cx->search_pack_header = 0;
296 len = (char *)q - start;
297 cx18_setup_sliced_vbi_buf(cx);
298 break;
299 }
300 }
301 }
302 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
303 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
304 len, s->name);
305 return -EFAULT;
306 }
307 buf->readpos += len;
308 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
309 buf != &cx->vbi.sliced_mpeg_buf)
310 cx->mpg_data_received += len;
311 return len;
312}
313
314static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
315 size_t tot_count, int non_block)
316{
317 struct cx18 *cx = s->cx;
318 size_t tot_written = 0;
319 int single_frame = 0;
320
Hans Verkuil31554ae2008-05-25 11:21:27 -0300321 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300322 /* shouldn't happen */
323 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
324 s->name);
325 return -EIO;
326 }
327
328 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
329 frames should arrive one-by-one, so make sure we never output more
330 than one VBI frame at a time */
331 if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
332 cx->vbi.sliced_in->service_set)
333 single_frame = 1;
334
335 for (;;) {
336 struct cx18_buffer *buf;
337 int rc;
338
339 buf = cx18_get_buffer(s, non_block, &rc);
340 /* if there is no data available... */
341 if (buf == NULL) {
342 /* if we got data, then return that regardless */
343 if (tot_written)
344 break;
345 /* EOS condition */
346 if (rc == 0) {
347 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
348 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
349 cx18_release_stream(s);
350 }
351 /* set errno */
352 return rc;
353 }
354
355 rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
356 tot_count - tot_written);
357
358 if (buf != &cx->vbi.sliced_mpeg_buf) {
359 if (buf->readpos == buf->bytesused) {
360 cx18_buf_sync_for_device(s, buf);
361 cx18_enqueue(s, buf, &s->q_free);
362 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5,
363 s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300364 (void __iomem *)&cx->scb->cpu_mdl[buf->id] -
365 cx->enc_mem,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300366 1, buf->id, s->buf_size);
367 } else
368 cx18_enqueue(s, buf, &s->q_io);
369 } else if (buf->readpos == buf->bytesused) {
370 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
371
372 cx->vbi.sliced_mpeg_size[idx] = 0;
373 cx->vbi.inserted_frame++;
374 cx->vbi_data_inserted += buf->bytesused;
375 }
376 if (rc < 0)
377 return rc;
378 tot_written += rc;
379
380 if (tot_written == tot_count || single_frame)
381 break;
382 }
383 return tot_written;
384}
385
386static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
387 size_t count, loff_t *pos, int non_block)
388{
389 ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
390 struct cx18 *cx = s->cx;
391
392 CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
393 if (rc > 0)
394 pos += rc;
395 return rc;
396}
397
398int cx18_start_capture(struct cx18_open_id *id)
399{
400 struct cx18 *cx = id->cx;
401 struct cx18_stream *s = &cx->streams[id->type];
402 struct cx18_stream *s_vbi;
403
404 if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
405 /* you cannot read from these stream types. */
406 return -EPERM;
407 }
408
409 /* Try to claim this stream. */
410 if (cx18_claim_stream(id, s->type))
411 return -EBUSY;
412
413 /* If capture is already in progress, then we also have to
414 do nothing extra. */
415 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
416 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
417 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
418 return 0;
419 }
420
421 /* Start VBI capture if required */
422 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
423 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
424 test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
425 !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
426 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
427 automatically when the MPG stream is claimed.
428 We only need to start the VBI capturing. */
429 if (cx18_start_v4l2_encode_stream(s_vbi)) {
430 CX18_DEBUG_WARN("VBI capture start failed\n");
431
432 /* Failure, clean up and return an error */
433 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
434 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
435 /* also releases the associated VBI stream */
436 cx18_release_stream(s);
437 return -EIO;
438 }
439 CX18_DEBUG_INFO("VBI insertion started\n");
440 }
441
442 /* Tell the card to start capturing */
443 if (!cx18_start_v4l2_encode_stream(s)) {
444 /* We're done */
445 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
446 /* Resume a possibly paused encoder */
447 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
448 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
449 return 0;
450 }
451
452 /* failure, clean up */
453 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
454
455 /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
456 automatically when the MPG stream is released.
457 We only need to stop the VBI capturing. */
458 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
459 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
460 cx18_stop_v4l2_encode_stream(s_vbi, 0);
461 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
462 }
463 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
464 cx18_release_stream(s);
465 return -EIO;
466}
467
468ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
469 loff_t *pos)
470{
471 struct cx18_open_id *id = filp->private_data;
472 struct cx18 *cx = id->cx;
473 struct cx18_stream *s = &cx->streams[id->type];
474 int rc;
475
476 CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
477
478 mutex_lock(&cx->serialize_lock);
479 rc = cx18_start_capture(id);
480 mutex_unlock(&cx->serialize_lock);
481 if (rc)
482 return rc;
483 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
484}
485
486unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
487{
488 struct cx18_open_id *id = filp->private_data;
489 struct cx18 *cx = id->cx;
490 struct cx18_stream *s = &cx->streams[id->type];
491 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
492
493 /* Start a capture if there is none */
494 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
495 int rc;
496
497 mutex_lock(&cx->serialize_lock);
498 rc = cx18_start_capture(id);
499 mutex_unlock(&cx->serialize_lock);
500 if (rc) {
501 CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
502 s->name, rc);
503 return POLLERR;
504 }
505 CX18_DEBUG_FILE("Encoder poll started capture\n");
506 }
507
508 /* add stream's waitq to the poll list */
509 CX18_DEBUG_HI_FILE("Encoder poll\n");
510 poll_wait(filp, &s->waitq, wait);
511
512 if (s->q_full.length || s->q_io.length)
513 return POLLIN | POLLRDNORM;
514 if (eof)
515 return POLLHUP;
516 return 0;
517}
518
519void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
520{
521 struct cx18 *cx = id->cx;
522 struct cx18_stream *s = &cx->streams[id->type];
523
524 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
525
526 /* 'Unclaim' this stream */
527
528 /* Stop capturing */
529 if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
530 struct cx18_stream *s_vbi =
531 &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
532
533 CX18_DEBUG_INFO("close stopping capture\n");
534 /* Special case: a running VBI capture for VBI insertion
535 in the mpeg stream. Need to stop that too. */
536 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
537 test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
538 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
539 CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
540 cx18_stop_v4l2_encode_stream(s_vbi, 0);
541 }
542 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
543 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
544 /* Also used internally, don't stop capturing */
545 s->id = -1;
546 else
547 cx18_stop_v4l2_encode_stream(s, gop_end);
548 }
549 if (!gop_end) {
550 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
551 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
552 cx18_release_stream(s);
553 }
554}
555
556int cx18_v4l2_close(struct inode *inode, struct file *filp)
557{
558 struct cx18_open_id *id = filp->private_data;
559 struct cx18 *cx = id->cx;
560 struct cx18_stream *s = &cx->streams[id->type];
561
562 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
563
564 v4l2_prio_close(&cx->prio, &id->prio);
565
566 /* Easy case first: this stream was never claimed by us */
567 if (s->id != id->open_id) {
568 kfree(id);
569 return 0;
570 }
571
572 /* 'Unclaim' this stream */
573
574 /* Stop radio */
575 mutex_lock(&cx->serialize_lock);
576 if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
577 /* Closing radio device, return to TV mode */
578 cx18_mute(cx);
579 /* Mark that the radio is no longer in use */
580 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
581 /* Switch tuner to TV */
582 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
583 /* Select correct audio input (i.e. TV tuner or Line in) */
584 cx18_audio_set_io(cx);
Hans Verkuil31554ae2008-05-25 11:21:27 -0300585 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300586 /* Undo video mute */
587 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
588 cx->params.video_mute |
589 (cx->params.video_mute_yuv << 8));
590 }
591 /* Done! Unmute and continue. */
592 cx18_unmute(cx);
593 cx18_release_stream(s);
594 } else {
595 cx18_stop_capture(id, 0);
596 }
597 kfree(id);
598 mutex_unlock(&cx->serialize_lock);
599 return 0;
600}
601
602static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
603{
604 struct cx18 *cx = s->cx;
605 struct cx18_open_id *item;
606
607 CX18_DEBUG_FILE("open %s\n", s->name);
608
609 /* Allocate memory */
610 item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
611 if (NULL == item) {
612 CX18_DEBUG_WARN("nomem on v4l2 open\n");
613 return -ENOMEM;
614 }
615 item->cx = cx;
616 item->type = s->type;
617 v4l2_prio_open(&cx->prio, &item->prio);
618
619 item->open_id = cx->open_id++;
620 filp->private_data = item;
621
622 if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
623 /* Try to claim this stream */
624 if (cx18_claim_stream(item, item->type)) {
625 /* No, it's already in use */
626 kfree(item);
627 return -EBUSY;
628 }
629
630 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
Hans Verkuil31554ae2008-05-25 11:21:27 -0300631 if (atomic_read(&cx->ana_capturing) > 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300632 /* switching to radio while capture is
633 in progress is not polite */
634 cx18_release_stream(s);
635 kfree(item);
636 return -EBUSY;
637 }
638 }
639
640 /* Mark that the radio is being used. */
641 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
642 /* We have the radio */
643 cx18_mute(cx);
644 /* Switch tuner to radio */
645 cx18_call_i2c_clients(cx, AUDC_SET_RADIO, NULL);
646 /* Select the correct audio input (i.e. radio tuner) */
647 cx18_audio_set_io(cx);
648 /* Done! Unmute and continue. */
649 cx18_unmute(cx);
650 }
651 return 0;
652}
653
654int cx18_v4l2_open(struct inode *inode, struct file *filp)
655{
656 int res, x, y = 0;
657 struct cx18 *cx = NULL;
658 struct cx18_stream *s = NULL;
659 int minor = iminor(inode);
660
661 /* Find which card this open was on */
662 spin_lock(&cx18_cards_lock);
663 for (x = 0; cx == NULL && x < cx18_cards_active; x++) {
664 /* find out which stream this open was on */
665 for (y = 0; y < CX18_MAX_STREAMS; y++) {
Andy Walls07c87a82008-05-12 15:01:27 -0300666 if (cx18_cards[x] == NULL)
667 continue;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300668 s = &cx18_cards[x]->streams[y];
669 if (s->v4l2dev && s->v4l2dev->minor == minor) {
670 cx = cx18_cards[x];
671 break;
672 }
673 }
674 }
675 spin_unlock(&cx18_cards_lock);
676
677 if (cx == NULL) {
678 /* Couldn't find a device registered
679 on that minor, shouldn't happen! */
680 printk(KERN_WARNING "No cx18 device found on minor %d\n",
681 minor);
682 return -ENXIO;
683 }
684
685 mutex_lock(&cx->serialize_lock);
686 if (cx18_init_on_first_open(cx)) {
687 CX18_ERR("Failed to initialize on minor %d\n", minor);
688 mutex_unlock(&cx->serialize_lock);
689 return -ENXIO;
690 }
691 res = cx18_serialized_open(s, filp);
692 mutex_unlock(&cx->serialize_lock);
693 return res;
694}
695
696void cx18_mute(struct cx18 *cx)
697{
Hans Verkuil31554ae2008-05-25 11:21:27 -0300698 if (atomic_read(&cx->ana_capturing))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300699 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
700 cx18_find_handle(cx), 1);
701 CX18_DEBUG_INFO("Mute\n");
702}
703
704void cx18_unmute(struct cx18 *cx)
705{
Hans Verkuil31554ae2008-05-25 11:21:27 -0300706 if (atomic_read(&cx->ana_capturing)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300707 cx18_msleep_timeout(100, 0);
708 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
709 cx18_find_handle(cx), 12);
710 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
711 cx18_find_handle(cx), 0);
712 }
713 CX18_DEBUG_INFO("Unmute\n");
714}