Commit | Line | Data |
---|---|---|
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 | 43 | static 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 | 90 | static 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 | ||
128 | static 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; | |
d3c5e707 | 135 | u32 h; |
1c1e45d1 HV |
136 | |
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 | ||
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 | ||
169 | static 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) { | |
179 | ||
180 | if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) { | |
181 | cx->dualwatch_jiffies = jiffies; | |
182 | cx18_dualwatch(cx); | |
183 | } | |
184 | if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) && | |
185 | !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) { | |
186 | while ((buf = cx18_dequeue(s_vbi, &s_vbi->q_full))) { | |
187 | /* byteswap and process VBI data */ | |
af009cf6 AW |
188 | cx18_process_vbi_data(cx, buf, |
189 | s_vbi->dma_pts, | |
190 | s_vbi->type); | |
66c2a6b0 | 191 | cx18_stream_put_buf_fw(s_vbi, buf); |
1c1e45d1 HV |
192 | } |
193 | } | |
194 | buf = &cx->vbi.sliced_mpeg_buf; | |
195 | if (buf->readpos != buf->bytesused) | |
196 | return buf; | |
197 | } | |
198 | ||
1c1e45d1 HV |
199 | /* do we have new data? */ |
200 | buf = cx18_dequeue(s, &s->q_full); | |
201 | if (buf) { | |
202 | if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP, | |
203 | &buf->b_flags)) | |
204 | return buf; | |
205 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) | |
206 | /* byteswap MPG data */ | |
207 | cx18_buf_swap(buf); | |
208 | else { | |
209 | /* byteswap and process VBI data */ | |
210 | cx18_process_vbi_data(cx, buf, | |
211 | s->dma_pts, s->type); | |
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 | ||
244 | static 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 | ||
253 | static 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) { |
1c1e45d1 HV |
263 | const char *start = buf->buf + buf->readpos; |
264 | const char *p = start + 1; | |
265 | const u8 *q; | |
266 | u8 ch = cx->search_pack_header ? 0xba : 0xe0; | |
267 | int stuffing, i; | |
268 | ||
269 | while (start + len > p) { | |
270 | q = memchr(p, 0, start + len - p); | |
271 | if (q == NULL) | |
272 | break; | |
273 | p = q + 1; | |
274 | if ((char *)q + 15 >= buf->buf + buf->bytesused || | |
275 | q[1] != 0 || q[2] != 1 || q[3] != ch) | |
276 | continue; | |
277 | if (!cx->search_pack_header) { | |
278 | if ((q[6] & 0xc0) != 0x80) | |
279 | continue; | |
280 | if (((q[7] & 0xc0) == 0x80 && | |
281 | (q[9] & 0xf0) == 0x20) || | |
282 | ((q[7] & 0xc0) == 0xc0 && | |
283 | (q[9] & 0xf0) == 0x30)) { | |
284 | ch = 0xba; | |
285 | cx->search_pack_header = 1; | |
286 | p = q + 9; | |
287 | } | |
288 | continue; | |
289 | } | |
290 | stuffing = q[13] & 7; | |
291 | /* all stuffing bytes must be 0xff */ | |
292 | for (i = 0; i < stuffing; i++) | |
293 | if (q[14 + i] != 0xff) | |
294 | break; | |
295 | if (i == stuffing && | |
296 | (q[4] & 0xc4) == 0x44 && | |
297 | (q[12] & 3) == 3 && | |
298 | q[14 + stuffing] == 0 && | |
299 | q[15 + stuffing] == 0 && | |
300 | q[16 + stuffing] == 1) { | |
301 | cx->search_pack_header = 0; | |
302 | len = (char *)q - start; | |
303 | cx18_setup_sliced_vbi_buf(cx); | |
304 | break; | |
305 | } | |
306 | } | |
307 | } | |
308 | if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) { | |
309 | CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n", | |
310 | len, s->name); | |
311 | return -EFAULT; | |
312 | } | |
313 | buf->readpos += len; | |
314 | if (s->type == CX18_ENC_STREAM_TYPE_MPG && | |
315 | buf != &cx->vbi.sliced_mpeg_buf) | |
316 | cx->mpg_data_received += len; | |
317 | return len; | |
318 | } | |
319 | ||
320 | static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf, | |
321 | size_t tot_count, int non_block) | |
322 | { | |
323 | struct cx18 *cx = s->cx; | |
324 | size_t tot_written = 0; | |
325 | int single_frame = 0; | |
326 | ||
31554ae5 | 327 | if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) { |
1c1e45d1 HV |
328 | /* shouldn't happen */ |
329 | CX18_DEBUG_WARN("Stream %s not initialized before read\n", | |
330 | s->name); | |
331 | return -EIO; | |
332 | } | |
333 | ||
334 | /* Each VBI buffer is one frame, the v4l2 API says that for VBI the | |
335 | frames should arrive one-by-one, so make sure we never output more | |
336 | than one VBI frame at a time */ | |
dd073434 | 337 | if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx)) |
1c1e45d1 HV |
338 | single_frame = 1; |
339 | ||
340 | for (;;) { | |
341 | struct cx18_buffer *buf; | |
342 | int rc; | |
343 | ||
344 | buf = cx18_get_buffer(s, non_block, &rc); | |
345 | /* if there is no data available... */ | |
346 | if (buf == NULL) { | |
347 | /* if we got data, then return that regardless */ | |
348 | if (tot_written) | |
349 | break; | |
350 | /* EOS condition */ | |
351 | if (rc == 0) { | |
352 | clear_bit(CX18_F_S_STREAMOFF, &s->s_flags); | |
353 | clear_bit(CX18_F_S_APPL_IO, &s->s_flags); | |
354 | cx18_release_stream(s); | |
355 | } | |
356 | /* set errno */ | |
357 | return rc; | |
358 | } | |
359 | ||
360 | rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written, | |
361 | tot_count - tot_written); | |
362 | ||
363 | if (buf != &cx->vbi.sliced_mpeg_buf) { | |
66c2a6b0 AW |
364 | if (buf->readpos == buf->bytesused) |
365 | cx18_stream_put_buf_fw(s, buf); | |
366 | else | |
b80e1074 | 367 | cx18_push(s, buf, &s->q_full); |
1c1e45d1 HV |
368 | } else if (buf->readpos == buf->bytesused) { |
369 | int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES; | |
370 | ||
371 | cx->vbi.sliced_mpeg_size[idx] = 0; | |
372 | cx->vbi.inserted_frame++; | |
373 | cx->vbi_data_inserted += buf->bytesused; | |
374 | } | |
375 | if (rc < 0) | |
376 | return rc; | |
377 | tot_written += rc; | |
378 | ||
379 | if (tot_written == tot_count || single_frame) | |
380 | break; | |
381 | } | |
382 | return tot_written; | |
383 | } | |
384 | ||
385 | static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf, | |
386 | size_t count, loff_t *pos, int non_block) | |
387 | { | |
388 | ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0; | |
389 | struct cx18 *cx = s->cx; | |
390 | ||
391 | CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc); | |
392 | if (rc > 0) | |
393 | pos += rc; | |
394 | return rc; | |
395 | } | |
396 | ||
397 | int cx18_start_capture(struct cx18_open_id *id) | |
398 | { | |
399 | struct cx18 *cx = id->cx; | |
400 | struct cx18_stream *s = &cx->streams[id->type]; | |
401 | struct cx18_stream *s_vbi; | |
402 | ||
403 | if (s->type == CX18_ENC_STREAM_TYPE_RAD) { | |
404 | /* you cannot read from these stream types. */ | |
405 | return -EPERM; | |
406 | } | |
407 | ||
408 | /* Try to claim this stream. */ | |
409 | if (cx18_claim_stream(id, s->type)) | |
410 | return -EBUSY; | |
411 | ||
412 | /* If capture is already in progress, then we also have to | |
413 | do nothing extra. */ | |
414 | if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) || | |
415 | test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) { | |
416 | set_bit(CX18_F_S_APPL_IO, &s->s_flags); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | /* Start VBI capture if required */ | |
421 | s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI]; | |
422 | if (s->type == CX18_ENC_STREAM_TYPE_MPG && | |
423 | test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) && | |
424 | !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) { | |
425 | /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed | |
426 | automatically when the MPG stream is claimed. | |
427 | We only need to start the VBI capturing. */ | |
428 | if (cx18_start_v4l2_encode_stream(s_vbi)) { | |
429 | CX18_DEBUG_WARN("VBI capture start failed\n"); | |
430 | ||
431 | /* Failure, clean up and return an error */ | |
432 | clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags); | |
433 | clear_bit(CX18_F_S_STREAMING, &s->s_flags); | |
434 | /* also releases the associated VBI stream */ | |
435 | cx18_release_stream(s); | |
436 | return -EIO; | |
437 | } | |
438 | CX18_DEBUG_INFO("VBI insertion started\n"); | |
439 | } | |
440 | ||
441 | /* Tell the card to start capturing */ | |
442 | if (!cx18_start_v4l2_encode_stream(s)) { | |
443 | /* We're done */ | |
444 | set_bit(CX18_F_S_APPL_IO, &s->s_flags); | |
445 | /* Resume a possibly paused encoder */ | |
446 | if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags)) | |
447 | cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | /* failure, clean up */ | |
452 | CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name); | |
453 | ||
454 | /* Note: the CX18_ENC_STREAM_TYPE_VBI is released | |
455 | automatically when the MPG stream is released. | |
456 | We only need to stop the VBI capturing. */ | |
457 | if (s->type == CX18_ENC_STREAM_TYPE_MPG && | |
458 | test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) { | |
459 | cx18_stop_v4l2_encode_stream(s_vbi, 0); | |
460 | clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags); | |
461 | } | |
462 | clear_bit(CX18_F_S_STREAMING, &s->s_flags); | |
463 | cx18_release_stream(s); | |
464 | return -EIO; | |
465 | } | |
466 | ||
467 | ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count, | |
468 | loff_t *pos) | |
469 | { | |
470 | struct cx18_open_id *id = filp->private_data; | |
471 | struct cx18 *cx = id->cx; | |
472 | struct cx18_stream *s = &cx->streams[id->type]; | |
473 | int rc; | |
474 | ||
475 | CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name); | |
476 | ||
477 | mutex_lock(&cx->serialize_lock); | |
478 | rc = cx18_start_capture(id); | |
479 | mutex_unlock(&cx->serialize_lock); | |
480 | if (rc) | |
481 | return rc; | |
482 | return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK); | |
483 | } | |
484 | ||
485 | unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait) | |
486 | { | |
487 | struct cx18_open_id *id = filp->private_data; | |
488 | struct cx18 *cx = id->cx; | |
489 | struct cx18_stream *s = &cx->streams[id->type]; | |
490 | int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags); | |
491 | ||
492 | /* Start a capture if there is none */ | |
493 | if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) { | |
494 | int rc; | |
495 | ||
496 | mutex_lock(&cx->serialize_lock); | |
497 | rc = cx18_start_capture(id); | |
498 | mutex_unlock(&cx->serialize_lock); | |
499 | if (rc) { | |
500 | CX18_DEBUG_INFO("Could not start capture for %s (%d)\n", | |
501 | s->name, rc); | |
502 | return POLLERR; | |
503 | } | |
504 | CX18_DEBUG_FILE("Encoder poll started capture\n"); | |
505 | } | |
506 | ||
507 | /* add stream's waitq to the poll list */ | |
508 | CX18_DEBUG_HI_FILE("Encoder poll\n"); | |
509 | poll_wait(filp, &s->waitq, wait); | |
510 | ||
b80e1074 | 511 | if (atomic_read(&s->q_full.buffers)) |
1c1e45d1 HV |
512 | return POLLIN | POLLRDNORM; |
513 | if (eof) | |
514 | return POLLHUP; | |
515 | return 0; | |
516 | } | |
517 | ||
518 | void cx18_stop_capture(struct cx18_open_id *id, int gop_end) | |
519 | { | |
520 | struct cx18 *cx = id->cx; | |
521 | struct cx18_stream *s = &cx->streams[id->type]; | |
522 | ||
523 | CX18_DEBUG_IOCTL("close() of %s\n", s->name); | |
524 | ||
525 | /* 'Unclaim' this stream */ | |
526 | ||
527 | /* Stop capturing */ | |
528 | if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) { | |
529 | struct cx18_stream *s_vbi = | |
530 | &cx->streams[CX18_ENC_STREAM_TYPE_VBI]; | |
531 | ||
532 | CX18_DEBUG_INFO("close stopping capture\n"); | |
533 | /* Special case: a running VBI capture for VBI insertion | |
534 | in the mpeg stream. Need to stop that too. */ | |
535 | if (id->type == CX18_ENC_STREAM_TYPE_MPG && | |
536 | test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) && | |
537 | !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) { | |
538 | CX18_DEBUG_INFO("close stopping embedded VBI capture\n"); | |
539 | cx18_stop_v4l2_encode_stream(s_vbi, 0); | |
540 | } | |
541 | if (id->type == CX18_ENC_STREAM_TYPE_VBI && | |
542 | test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) | |
543 | /* Also used internally, don't stop capturing */ | |
544 | s->id = -1; | |
545 | else | |
546 | cx18_stop_v4l2_encode_stream(s, gop_end); | |
547 | } | |
548 | if (!gop_end) { | |
549 | clear_bit(CX18_F_S_APPL_IO, &s->s_flags); | |
550 | clear_bit(CX18_F_S_STREAMOFF, &s->s_flags); | |
551 | cx18_release_stream(s); | |
552 | } | |
553 | } | |
554 | ||
bec43661 | 555 | int cx18_v4l2_close(struct file *filp) |
1c1e45d1 HV |
556 | { |
557 | struct cx18_open_id *id = filp->private_data; | |
558 | struct cx18 *cx = id->cx; | |
559 | struct cx18_stream *s = &cx->streams[id->type]; | |
560 | ||
561 | CX18_DEBUG_IOCTL("close() of %s\n", s->name); | |
562 | ||
563 | v4l2_prio_close(&cx->prio, &id->prio); | |
564 | ||
565 | /* Easy case first: this stream was never claimed by us */ | |
566 | if (s->id != id->open_id) { | |
567 | kfree(id); | |
568 | return 0; | |
569 | } | |
570 | ||
571 | /* 'Unclaim' this stream */ | |
572 | ||
573 | /* Stop radio */ | |
574 | mutex_lock(&cx->serialize_lock); | |
575 | if (id->type == CX18_ENC_STREAM_TYPE_RAD) { | |
576 | /* Closing radio device, return to TV mode */ | |
577 | cx18_mute(cx); | |
578 | /* Mark that the radio is no longer in use */ | |
579 | clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags); | |
580 | /* Switch tuner to TV */ | |
581 | cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std); | |
582 | /* Select correct audio input (i.e. TV tuner or Line in) */ | |
583 | cx18_audio_set_io(cx); | |
31554ae5 | 584 | if (atomic_read(&cx->ana_capturing) > 0) { |
1c1e45d1 HV |
585 | /* Undo video mute */ |
586 | cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle, | |
587 | cx->params.video_mute | | |
588 | (cx->params.video_mute_yuv << 8)); | |
589 | } | |
590 | /* Done! Unmute and continue. */ | |
591 | cx18_unmute(cx); | |
592 | cx18_release_stream(s); | |
593 | } else { | |
594 | cx18_stop_capture(id, 0); | |
595 | } | |
596 | kfree(id); | |
597 | mutex_unlock(&cx->serialize_lock); | |
598 | return 0; | |
599 | } | |
600 | ||
601 | static int cx18_serialized_open(struct cx18_stream *s, struct file *filp) | |
602 | { | |
603 | struct cx18 *cx = s->cx; | |
604 | struct cx18_open_id *item; | |
605 | ||
606 | CX18_DEBUG_FILE("open %s\n", s->name); | |
607 | ||
608 | /* Allocate memory */ | |
609 | item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL); | |
610 | if (NULL == item) { | |
611 | CX18_DEBUG_WARN("nomem on v4l2 open\n"); | |
612 | return -ENOMEM; | |
613 | } | |
614 | item->cx = cx; | |
615 | item->type = s->type; | |
616 | v4l2_prio_open(&cx->prio, &item->prio); | |
617 | ||
618 | item->open_id = cx->open_id++; | |
619 | filp->private_data = item; | |
620 | ||
621 | if (item->type == CX18_ENC_STREAM_TYPE_RAD) { | |
622 | /* Try to claim this stream */ | |
623 | if (cx18_claim_stream(item, item->type)) { | |
624 | /* No, it's already in use */ | |
625 | kfree(item); | |
626 | return -EBUSY; | |
627 | } | |
628 | ||
629 | if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) { | |
31554ae5 | 630 | if (atomic_read(&cx->ana_capturing) > 0) { |
1c1e45d1 HV |
631 | /* switching to radio while capture is |
632 | in progress is not polite */ | |
633 | cx18_release_stream(s); | |
634 | kfree(item); | |
635 | return -EBUSY; | |
636 | } | |
637 | } | |
638 | ||
639 | /* Mark that the radio is being used. */ | |
640 | set_bit(CX18_F_I_RADIO_USER, &cx->i_flags); | |
641 | /* We have the radio */ | |
642 | cx18_mute(cx); | |
643 | /* Switch tuner to radio */ | |
644 | cx18_call_i2c_clients(cx, AUDC_SET_RADIO, NULL); | |
645 | /* Select the correct audio input (i.e. radio tuner) */ | |
646 | cx18_audio_set_io(cx); | |
647 | /* Done! Unmute and continue. */ | |
648 | cx18_unmute(cx); | |
649 | } | |
650 | return 0; | |
651 | } | |
652 | ||
bec43661 | 653 | int cx18_v4l2_open(struct file *filp) |
1c1e45d1 HV |
654 | { |
655 | int res, x, y = 0; | |
656 | struct cx18 *cx = NULL; | |
657 | struct cx18_stream *s = NULL; | |
bec43661 | 658 | int minor = video_devdata(filp)->minor; |
1c1e45d1 HV |
659 | |
660 | /* Find which card this open was on */ | |
661 | spin_lock(&cx18_cards_lock); | |
662 | for (x = 0; cx == NULL && x < cx18_cards_active; x++) { | |
663 | /* find out which stream this open was on */ | |
664 | for (y = 0; y < CX18_MAX_STREAMS; y++) { | |
07c87a83 AW |
665 | if (cx18_cards[x] == NULL) |
666 | continue; | |
1c1e45d1 HV |
667 | s = &cx18_cards[x]->streams[y]; |
668 | if (s->v4l2dev && s->v4l2dev->minor == minor) { | |
669 | cx = cx18_cards[x]; | |
670 | break; | |
671 | } | |
672 | } | |
673 | } | |
674 | spin_unlock(&cx18_cards_lock); | |
675 | ||
676 | if (cx == NULL) { | |
677 | /* Couldn't find a device registered | |
678 | on that minor, shouldn't happen! */ | |
679 | printk(KERN_WARNING "No cx18 device found on minor %d\n", | |
680 | minor); | |
681 | return -ENXIO; | |
682 | } | |
683 | ||
684 | mutex_lock(&cx->serialize_lock); | |
685 | if (cx18_init_on_first_open(cx)) { | |
686 | CX18_ERR("Failed to initialize on minor %d\n", minor); | |
687 | mutex_unlock(&cx->serialize_lock); | |
688 | return -ENXIO; | |
689 | } | |
690 | res = cx18_serialized_open(s, filp); | |
691 | mutex_unlock(&cx->serialize_lock); | |
692 | return res; | |
693 | } | |
694 | ||
695 | void cx18_mute(struct cx18 *cx) | |
696 | { | |
d3c5e707 AW |
697 | u32 h; |
698 | if (atomic_read(&cx->ana_capturing)) { | |
699 | h = cx18_find_handle(cx); | |
700 | if (h != CX18_INVALID_TASK_HANDLE) | |
701 | cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1); | |
702 | else | |
703 | CX18_ERR("Can't find valid task handle for mute\n"); | |
704 | } | |
1c1e45d1 HV |
705 | CX18_DEBUG_INFO("Mute\n"); |
706 | } | |
707 | ||
708 | void cx18_unmute(struct cx18 *cx) | |
709 | { | |
d3c5e707 | 710 | u32 h; |
31554ae5 | 711 | if (atomic_read(&cx->ana_capturing)) { |
d3c5e707 AW |
712 | h = cx18_find_handle(cx); |
713 | if (h != CX18_INVALID_TASK_HANDLE) { | |
714 | cx18_msleep_timeout(100, 0); | |
715 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12); | |
716 | cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0); | |
717 | } else | |
718 | CX18_ERR("Can't find valid task handle for unmute\n"); | |
1c1e45d1 HV |
719 | } |
720 | CX18_DEBUG_INFO("Unmute\n"); | |
721 | } |