2 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
3 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ivtv-driver.h"
22 #include "ivtv-firmware.h"
23 #include "ivtv-fileops.h"
24 #include "ivtv-queue.h"
25 #include "ivtv-udma.h"
27 #include "ivtv-ioctl.h"
28 #include "ivtv-mailbox.h"
32 #define DMA_MAGIC_COOKIE 0x000001fe
34 #define SLICED_VBI_PIO 1
36 static void ivtv_dma_dec_start(struct ivtv_stream
*s
);
38 static const int ivtv_stream_map
[] = {
39 IVTV_ENC_STREAM_TYPE_MPG
,
40 IVTV_ENC_STREAM_TYPE_YUV
,
41 IVTV_ENC_STREAM_TYPE_PCM
,
42 IVTV_ENC_STREAM_TYPE_VBI
,
45 static inline int ivtv_use_pio(struct ivtv_stream
*s
)
47 struct ivtv
*itv
= s
->itv
;
49 return s
->dma
== PCI_DMA_NONE
||
50 (SLICED_VBI_PIO
&& s
->type
== IVTV_ENC_STREAM_TYPE_VBI
&& itv
->vbi
.sliced_in
->service_set
);
53 void ivtv_irq_work_handler(struct work_struct
*work
)
55 struct ivtv
*itv
= container_of(work
, struct ivtv
, irq_work_queue
);
59 if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_VBI
, &itv
->i_flags
))
60 vbi_work_handler(itv
);
62 if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_YUV
, &itv
->i_flags
))
63 ivtv_yuv_work_handler(itv
);
66 /* Determine the required DMA size, setup enough buffers in the predma queue and
67 actually copy the data from the card to the buffers in case a PIO transfer is
68 required for this stream.
70 static int stream_enc_dma_append(struct ivtv_stream
*s
, u32 data
[CX2341X_MBOX_MAX_DATA
])
72 struct ivtv
*itv
= s
->itv
;
73 struct ivtv_buffer
*buf
;
77 u32 UVoffset
= 0, UVsize
= 0;
78 int skip_bufs
= s
->q_predma
.buffers
;
79 int idx
= s
->SG_length
;
83 if (s
->v4l2dev
== NULL
) {
84 IVTV_DEBUG_WARN("Stream %s not started\n", s
->name
);
87 if (!test_bit(IVTV_F_S_CLAIMED
, &s
->s_flags
)) {
88 IVTV_DEBUG_WARN("Stream %s not open\n", s
->name
);
92 /* determine offset, size and PTS for the various streams */
94 case IVTV_ENC_STREAM_TYPE_MPG
:
100 case IVTV_ENC_STREAM_TYPE_YUV
:
105 s
->dma_pts
= ((u64
) data
[5] << 32) | data
[6];
108 case IVTV_ENC_STREAM_TYPE_PCM
:
109 offset
= data
[1] + 12;
111 s
->dma_pts
= read_dec(offset
- 8) |
112 ((u64
)(read_dec(offset
- 12)) << 32);
113 if (itv
->has_cx23415
)
114 offset
+= IVTV_DECODER_OFFSET
;
117 case IVTV_ENC_STREAM_TYPE_VBI
:
118 size
= itv
->vbi
.enc_size
* itv
->vbi
.fpi
;
119 offset
= read_enc(itv
->vbi
.enc_start
- 4) + 12;
121 IVTV_DEBUG_INFO("VBI offset == 0\n");
124 s
->dma_pts
= read_enc(offset
- 4) | ((u64
)read_enc(offset
- 8) << 32);
127 case IVTV_DEC_STREAM_TYPE_VBI
:
128 size
= read_dec(itv
->vbi
.dec_start
+ 4) + 8;
129 offset
= read_dec(itv
->vbi
.dec_start
) + itv
->vbi
.dec_start
;
131 offset
+= IVTV_DECODER_OFFSET
;
134 /* shouldn't happen */
138 /* if this is the start of the DMA then fill in the magic cookie */
139 if (s
->SG_length
== 0) {
140 if (itv
->has_cx23415
&& (s
->type
== IVTV_ENC_STREAM_TYPE_PCM
||
141 s
->type
== IVTV_DEC_STREAM_TYPE_VBI
)) {
142 s
->dma_backup
= read_dec(offset
- IVTV_DECODER_OFFSET
);
143 write_dec_sync(cpu_to_le32(DMA_MAGIC_COOKIE
), offset
- IVTV_DECODER_OFFSET
);
146 s
->dma_backup
= read_enc(offset
);
147 write_enc_sync(cpu_to_le32(DMA_MAGIC_COOKIE
), offset
);
149 s
->dma_offset
= offset
;
153 if (s
->type
== IVTV_ENC_STREAM_TYPE_YUV
) {
154 /* The size for the Y samples needs to be rounded upwards to a
155 multiple of the buf_size. The UV samples then start in the
157 bytes_needed
= s
->buf_size
* ((bytes_needed
+ s
->buf_size
- 1) / s
->buf_size
);
158 bytes_needed
+= UVsize
;
161 IVTV_DEBUG_DMA("%s %s: 0x%08x bytes at 0x%08x\n",
162 ivtv_use_pio(s
) ? "PIO" : "DMA", s
->name
, bytes_needed
, offset
);
164 rc
= ivtv_queue_move(s
, &s
->q_free
, &s
->q_full
, &s
->q_predma
, bytes_needed
);
165 if (rc
< 0) { /* Insufficient buffers */
166 IVTV_DEBUG_WARN("Cannot obtain %d bytes for %s data transfer\n",
167 bytes_needed
, s
->name
);
170 if (rc
&& !s
->buffers_stolen
&& (s
->s_flags
& IVTV_F_S_APPL_IO
)) {
171 IVTV_WARN("All %s stream buffers are full. Dropping data.\n", s
->name
);
172 IVTV_WARN("Cause: the application is not reading fast enough.\n");
174 s
->buffers_stolen
= rc
;
176 /* got the buffers, now fill in SGarray (DMA) or copy the data from the card
177 to the buffers (PIO). */
178 buf
= list_entry(s
->q_predma
.list
.next
, struct ivtv_buffer
, list
);
179 memset(buf
->buf
, 0, 128);
180 list_for_each(p
, &s
->q_predma
.list
) {
181 struct ivtv_buffer
*buf
= list_entry(p
, struct ivtv_buffer
, list
);
185 if (!ivtv_use_pio(s
)) {
186 s
->SGarray
[idx
].dst
= cpu_to_le32(buf
->dma_handle
);
187 s
->SGarray
[idx
].src
= cpu_to_le32(offset
);
188 s
->SGarray
[idx
].size
= cpu_to_le32(s
->buf_size
);
190 buf
->bytesused
= (size
< s
->buf_size
) ? size
: s
->buf_size
;
192 /* If PIO, then copy the data from the card to the buffer */
193 if (s
->type
== IVTV_DEC_STREAM_TYPE_VBI
) {
194 memcpy_fromio(buf
->buf
, itv
->dec_mem
+ offset
- IVTV_DECODER_OFFSET
, buf
->bytesused
);
196 else if (ivtv_use_pio(s
)) {
197 memcpy_fromio(buf
->buf
, itv
->enc_mem
+ offset
, buf
->bytesused
);
200 s
->q_predma
.bytesused
+= buf
->bytesused
;
201 size
-= buf
->bytesused
;
202 offset
+= s
->buf_size
;
204 /* Sync SG buffers */
205 ivtv_buf_sync_for_device(s
, buf
);
207 if (size
== 0) { /* YUV */
208 /* process the UV section */
218 static void dma_post(struct ivtv_stream
*s
)
220 struct ivtv
*itv
= s
->itv
;
221 struct ivtv_buffer
*buf
= NULL
;
227 if (ivtv_use_pio(s
)) {
228 if (s
->q_predma
.bytesused
)
229 ivtv_queue_move(s
, &s
->q_predma
, NULL
, &s
->q_dma
, s
->q_predma
.bytesused
);
232 IVTV_DEBUG_DMA("%s %s completed (%x)\n", ivtv_use_pio(s
) ? "PIO" : "DMA",
233 s
->name
, s
->dma_offset
);
234 list_for_each(p
, &s
->q_dma
.list
) {
235 buf
= list_entry(p
, struct ivtv_buffer
, list
);
236 u32buf
= (u32
*)buf
->buf
;
239 ivtv_buf_sync_for_cpu(s
, buf
);
242 offset
= s
->dma_last_offset
;
243 if (u32buf
[offset
/ 4] != DMA_MAGIC_COOKIE
)
245 for (offset
= 0; offset
< 64; offset
++) {
246 if (u32buf
[offset
] == DMA_MAGIC_COOKIE
) {
252 IVTV_DEBUG_WARN("%s: Couldn't find start of buffer within the first 256 bytes\n", s
->name
);
253 offset
= s
->dma_last_offset
;
255 if (s
->dma_last_offset
!= offset
)
256 IVTV_DEBUG_WARN("%s: offset %d -> %d\n", s
->name
, s
->dma_last_offset
, offset
);
257 s
->dma_last_offset
= offset
;
259 if (itv
->has_cx23415
&& (s
->type
== IVTV_ENC_STREAM_TYPE_PCM
||
260 s
->type
== IVTV_DEC_STREAM_TYPE_VBI
)) {
261 write_dec_sync(0, s
->dma_offset
- IVTV_DECODER_OFFSET
);
264 write_enc_sync(0, s
->dma_offset
);
267 buf
->bytesused
-= offset
;
268 memcpy(buf
->buf
, buf
->buf
+ offset
, buf
->bytesused
+ offset
);
270 *u32buf
= cpu_to_le32(s
->dma_backup
);
273 /* flag byteswap ABCD -> DCBA for MPG & VBI data outside irq */
274 if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
||
275 s
->type
== IVTV_ENC_STREAM_TYPE_VBI
)
276 set_bit(IVTV_F_B_NEED_BUF_SWAP
, &buf
->b_flags
);
279 buf
->bytesused
+= s
->dma_last_offset
;
280 if (buf
&& s
->type
== IVTV_DEC_STREAM_TYPE_VBI
) {
281 /* Parse and Groom VBI Data */
282 s
->q_dma
.bytesused
-= buf
->bytesused
;
283 ivtv_process_vbi_data(itv
, buf
, 0, s
->type
);
284 s
->q_dma
.bytesused
+= buf
->bytesused
;
286 ivtv_queue_move(s
, &s
->q_dma
, NULL
, &s
->q_free
, 0);
290 ivtv_queue_move(s
, &s
->q_dma
, NULL
, &s
->q_full
, s
->q_dma
.bytesused
);
295 void ivtv_dma_stream_dec_prepare(struct ivtv_stream
*s
, u32 offset
, int lock
)
297 struct ivtv
*itv
= s
->itv
;
298 struct ivtv_buffer
*buf
;
300 u32 y_size
= itv
->params
.height
* itv
->params
.width
;
301 u32 uv_offset
= offset
+ IVTV_YUV_BUFFER_UV_OFFSET
;
303 int bytes_written
= 0;
304 unsigned long flags
= 0;
307 IVTV_DEBUG_DMA("DEC PREPARE DMA %s: %08x %08x\n", s
->name
, s
->q_predma
.bytesused
, offset
);
308 buf
= list_entry(s
->q_predma
.list
.next
, struct ivtv_buffer
, list
);
309 list_for_each(p
, &s
->q_predma
.list
) {
310 struct ivtv_buffer
*buf
= list_entry(p
, struct ivtv_buffer
, list
);
312 /* YUV UV Offset from Y Buffer */
313 if (s
->type
== IVTV_DEC_STREAM_TYPE_YUV
&& !y_done
&& bytes_written
>= y_size
) {
317 s
->SGarray
[idx
].src
= cpu_to_le32(buf
->dma_handle
);
318 s
->SGarray
[idx
].dst
= cpu_to_le32(offset
);
319 s
->SGarray
[idx
].size
= cpu_to_le32(buf
->bytesused
);
321 offset
+= buf
->bytesused
;
322 bytes_written
+= buf
->bytesused
;
324 /* Sync SG buffers */
325 ivtv_buf_sync_for_device(s
, buf
);
330 /* Mark last buffer size for Interrupt flag */
331 s
->SGarray
[s
->SG_length
- 1].size
|= cpu_to_le32(0x80000000);
333 /* Sync Hardware SG List of buffers */
334 ivtv_stream_sync_for_device(s
);
336 spin_lock_irqsave(&itv
->dma_reg_lock
, flags
);
337 if (!test_bit(IVTV_F_I_DMA
, &itv
->i_flags
)) {
338 ivtv_dma_dec_start(s
);
341 set_bit(IVTV_F_S_DMA_PENDING
, &s
->s_flags
);
344 spin_unlock_irqrestore(&itv
->dma_reg_lock
, flags
);
347 /* start the encoder DMA */
348 static void ivtv_dma_enc_start(struct ivtv_stream
*s
)
350 struct ivtv
*itv
= s
->itv
;
351 struct ivtv_stream
*s_vbi
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_VBI
];
354 if (s
->q_predma
.bytesused
)
355 ivtv_queue_move(s
, &s
->q_predma
, NULL
, &s
->q_dma
, s
->q_predma
.bytesused
);
356 IVTV_DEBUG_DMA("start DMA for %s\n", s
->name
);
357 s
->SGarray
[s
->SG_length
- 1].size
= cpu_to_le32(le32_to_cpu(s
->SGarray
[s
->SG_length
- 1].size
) + 256);
359 /* If this is an MPEG stream, and VBI data is also pending, then append the
360 VBI DMA to the MPEG DMA and transfer both sets of data at once.
362 VBI DMA is a second class citizen compared to MPEG and mixing them together
363 will confuse the firmware (the end of a VBI DMA is seen as the end of a
364 MPEG DMA, thus effectively dropping an MPEG frame). So instead we make
365 sure we only use the MPEG DMA to transfer the VBI DMA if both are in
366 use. This way no conflicts occur. */
367 clear_bit(IVTV_F_S_DMA_HAS_VBI
, &s
->s_flags
);
368 if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
&& s_vbi
->SG_length
&&
369 s
->SG_length
+ s_vbi
->SG_length
<= s
->buffers
) {
370 ivtv_queue_move(s_vbi
, &s_vbi
->q_predma
, NULL
, &s_vbi
->q_dma
, s_vbi
->q_predma
.bytesused
);
371 s_vbi
->SGarray
[s_vbi
->SG_length
- 1].size
= cpu_to_le32(le32_to_cpu(s_vbi
->SGarray
[s
->SG_length
- 1].size
) + 256);
372 for (i
= 0; i
< s_vbi
->SG_length
; i
++) {
373 s
->SGarray
[s
->SG_length
++] = s_vbi
->SGarray
[i
];
375 itv
->vbi
.dma_offset
= s_vbi
->dma_offset
;
376 s_vbi
->SG_length
= 0;
377 set_bit(IVTV_F_S_DMA_HAS_VBI
, &s
->s_flags
);
378 IVTV_DEBUG_DMA("include DMA for %s\n", s
->name
);
381 /* Mark last buffer size for Interrupt flag */
382 s
->SGarray
[s
->SG_length
- 1].size
|= cpu_to_le32(0x80000000);
384 /* Sync Hardware SG List of buffers */
385 ivtv_stream_sync_for_device(s
);
386 write_reg(s
->SG_handle
, IVTV_REG_ENCDMAADDR
);
387 write_reg_sync(read_reg(IVTV_REG_DMAXFER
) | 0x02, IVTV_REG_DMAXFER
);
388 set_bit(IVTV_F_I_DMA
, &itv
->i_flags
);
389 itv
->cur_dma_stream
= s
->type
;
390 itv
->dma_timer
.expires
= jiffies
+ HZ
/ 10;
391 add_timer(&itv
->dma_timer
);
394 static void ivtv_dma_dec_start(struct ivtv_stream
*s
)
396 struct ivtv
*itv
= s
->itv
;
398 if (s
->q_predma
.bytesused
)
399 ivtv_queue_move(s
, &s
->q_predma
, NULL
, &s
->q_dma
, s
->q_predma
.bytesused
);
400 IVTV_DEBUG_DMA("start DMA for %s\n", s
->name
);
401 /* put SG Handle into register 0x0c */
402 write_reg(s
->SG_handle
, IVTV_REG_DECDMAADDR
);
403 write_reg_sync(read_reg(IVTV_REG_DMAXFER
) | 0x01, IVTV_REG_DMAXFER
);
404 set_bit(IVTV_F_I_DMA
, &itv
->i_flags
);
405 itv
->cur_dma_stream
= s
->type
;
406 itv
->dma_timer
.expires
= jiffies
+ HZ
/ 10;
407 add_timer(&itv
->dma_timer
);
410 static void ivtv_irq_dma_read(struct ivtv
*itv
)
412 struct ivtv_stream
*s
= NULL
;
413 struct ivtv_buffer
*buf
;
416 IVTV_DEBUG_IRQ("DEC DMA READ\n");
417 del_timer(&itv
->dma_timer
);
418 if (read_reg(IVTV_REG_DMASTATUS
) & 0x14) {
419 IVTV_DEBUG_WARN("DEC DMA ERROR %x\n", read_reg(IVTV_REG_DMASTATUS
));
420 write_reg(read_reg(IVTV_REG_DMASTATUS
) & 3, IVTV_REG_DMASTATUS
);
422 if (!test_bit(IVTV_F_I_UDMA
, &itv
->i_flags
)) {
423 if (test_bit(IVTV_F_I_DEC_YUV
, &itv
->i_flags
)) {
424 s
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_YUV
];
428 s
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_MPG
];
431 IVTV_DEBUG_DMA("DEC DATA READ %s: %d\n", s
->name
, s
->q_dma
.bytesused
);
433 ivtv_stream_sync_for_cpu(s
);
435 /* For some reason must kick the firmware, like PIO mode,
436 I think this tells the firmware we are done and the size
437 of the xfer so it can calculate what we need next.
438 I think we can do this part ourselves but would have to
439 fully calculate xfer info ourselves and not use interrupts
441 ivtv_vapi(itv
, CX2341X_DEC_SCHED_DMA_FROM_HOST
, 3, 0, s
->q_dma
.bytesused
,
444 /* Free last DMA call */
445 while ((buf
= ivtv_dequeue(s
, &s
->q_dma
)) != NULL
) {
446 ivtv_buf_sync_for_cpu(s
, buf
);
447 ivtv_enqueue(s
, buf
, &s
->q_free
);
451 clear_bit(IVTV_F_I_UDMA
, &itv
->i_flags
);
452 clear_bit(IVTV_F_I_DMA
, &itv
->i_flags
);
453 itv
->cur_dma_stream
= -1;
454 wake_up(&itv
->dma_waitq
);
457 static void ivtv_irq_enc_dma_complete(struct ivtv
*itv
)
459 u32 data
[CX2341X_MBOX_MAX_DATA
];
460 struct ivtv_stream
*s
;
462 del_timer(&itv
->dma_timer
);
463 ivtv_api_get_data(&itv
->enc_mbox
, IVTV_MBOX_DMA_END
, data
);
464 IVTV_DEBUG_IRQ("ENC DMA COMPLETE %x %d\n", data
[0], data
[1]);
465 if (test_and_clear_bit(IVTV_F_I_ENC_VBI
, &itv
->i_flags
))
467 else if (data
[1] > 2)
469 s
= &itv
->streams
[ivtv_stream_map
[data
[1]]];
470 if (data
[0] & 0x18) {
471 IVTV_DEBUG_WARN("ENC DMA ERROR %x\n", data
[0]);
472 write_reg(read_reg(IVTV_REG_DMASTATUS
) & 3, IVTV_REG_DMASTATUS
);
473 ivtv_vapi(itv
, CX2341X_ENC_SCHED_DMA_TO_HOST
, 3, 0, 0, data
[1]);
476 clear_bit(IVTV_F_I_DMA
, &itv
->i_flags
);
477 itv
->cur_dma_stream
= -1;
479 ivtv_stream_sync_for_cpu(s
);
480 if (test_and_clear_bit(IVTV_F_S_DMA_HAS_VBI
, &s
->s_flags
)) {
483 s
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_VBI
];
485 s
->dma_offset
= itv
->vbi
.dma_offset
;
489 wake_up(&itv
->dma_waitq
);
492 static void ivtv_irq_dma_err(struct ivtv
*itv
)
494 u32 data
[CX2341X_MBOX_MAX_DATA
];
496 del_timer(&itv
->dma_timer
);
497 ivtv_api_get_data(&itv
->enc_mbox
, IVTV_MBOX_DMA_END
, data
);
498 IVTV_DEBUG_WARN("DMA ERROR %08x %08x %08x %d\n", data
[0], data
[1],
499 read_reg(IVTV_REG_DMASTATUS
), itv
->cur_dma_stream
);
500 if (!test_bit(IVTV_F_I_UDMA
, &itv
->i_flags
) &&
501 itv
->cur_dma_stream
>= 0 && itv
->cur_dma_stream
< IVTV_MAX_STREAMS
) {
502 struct ivtv_stream
*s
= &itv
->streams
[itv
->cur_dma_stream
];
505 write_reg(read_reg(IVTV_REG_DMASTATUS
) & 3, IVTV_REG_DMASTATUS
);
506 if (s
->type
>= IVTV_DEC_STREAM_TYPE_MPG
)
507 ivtv_dma_dec_start(s
);
509 ivtv_dma_enc_start(s
);
512 clear_bit(IVTV_F_I_UDMA
, &itv
->i_flags
);
513 clear_bit(IVTV_F_I_DMA
, &itv
->i_flags
);
514 itv
->cur_dma_stream
= -1;
515 wake_up(&itv
->dma_waitq
);
518 static void ivtv_irq_enc_start_cap(struct ivtv
*itv
)
520 u32 data
[CX2341X_MBOX_MAX_DATA
];
521 struct ivtv_stream
*s
;
523 /* Get DMA destination and size arguments from card */
524 ivtv_api_get_data(&itv
->enc_mbox
, IVTV_MBOX_DMA
, data
);
525 IVTV_DEBUG_IRQ("ENC START CAP %d: %08x %08x\n", data
[0], data
[1], data
[2]);
527 if (data
[0] > 2 || data
[1] == 0 || data
[2] == 0) {
528 IVTV_DEBUG_WARN("Unknown input: %08x %08x %08x\n",
529 data
[0], data
[1], data
[2]);
532 clear_bit(IVTV_F_I_ENC_VBI
, &itv
->i_flags
);
533 s
= &itv
->streams
[ivtv_stream_map
[data
[0]]];
534 if (!stream_enc_dma_append(s
, data
)) {
535 if (ivtv_use_pio(s
)) {
537 ivtv_vapi(itv
, CX2341X_ENC_SCHED_DMA_TO_HOST
, 3, 0, 0, data
[0]);
540 set_bit(IVTV_F_S_DMA_PENDING
, &s
->s_flags
);
545 static void ivtv_irq_enc_vbi_cap(struct ivtv
*itv
)
547 struct ivtv_stream
*s_mpg
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_MPG
];
548 u32 data
[CX2341X_MBOX_MAX_DATA
];
549 struct ivtv_stream
*s
;
551 IVTV_DEBUG_IRQ("ENC START VBI CAP\n");
552 s
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_VBI
];
554 if (ivtv_use_pio(s
)) {
555 if (stream_enc_dma_append(s
, data
))
557 if (s
->q_predma
.bytesused
)
558 ivtv_queue_move(s
, &s
->q_predma
, NULL
, &s
->q_dma
, s
->q_predma
.bytesused
);
563 /* If more than two VBI buffers are pending, then
564 clear the old ones and start with this new one.
565 This can happen during transition stages when MPEG capturing is
566 started, but the first interrupts haven't arrived yet. During
567 that period VBI requests can accumulate without being able to
568 DMA the data. Since at most four VBI DMA buffers are available,
569 we just drop the old requests when there are already three
571 if (s
->SG_length
> 2) {
573 list_for_each(p
, &s
->q_predma
.list
) {
574 struct ivtv_buffer
*buf
= list_entry(p
, struct ivtv_buffer
, list
);
575 ivtv_buf_sync_for_cpu(s
, buf
);
577 ivtv_queue_move(s
, &s
->q_predma
, NULL
, &s
->q_free
, 0);
580 /* if we can append the data, and the MPEG stream isn't capturing,
581 then start a DMA request for just the VBI data. */
582 if (!stream_enc_dma_append(s
, data
) &&
583 !test_bit(IVTV_F_S_STREAMING
, &s_mpg
->s_flags
)) {
584 set_bit(IVTV_F_I_ENC_VBI
, &itv
->i_flags
);
585 set_bit(IVTV_F_S_DMA_PENDING
, &s
->s_flags
);
589 static void ivtv_irq_dev_vbi_reinsert(struct ivtv
*itv
)
591 u32 data
[CX2341X_MBOX_MAX_DATA
];
592 struct ivtv_stream
*s
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_VBI
];
594 IVTV_DEBUG_IRQ("DEC VBI REINSERT\n");
595 if (test_bit(IVTV_F_S_CLAIMED
, &s
->s_flags
) &&
596 !stream_enc_dma_append(s
, data
)) {
601 static void ivtv_irq_dec_data_req(struct ivtv
*itv
)
603 u32 data
[CX2341X_MBOX_MAX_DATA
];
604 struct ivtv_stream
*s
;
607 ivtv_api_get_data(&itv
->dec_mbox
, IVTV_MBOX_DMA
, data
);
609 if (test_bit(IVTV_F_I_DEC_YUV
, &itv
->i_flags
)) {
610 itv
->dma_data_req_size
= itv
->params
.width
* itv
->params
.height
* 3 / 2;
611 itv
->dma_data_req_offset
= data
[1] ? data
[1] : yuv_offset
[0];
612 s
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_YUV
];
615 itv
->dma_data_req_size
= data
[2] >= 0x10000 ? 0x10000 : data
[2];
616 itv
->dma_data_req_offset
= data
[1];
617 s
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_MPG
];
619 IVTV_DEBUG_IRQ("DEC DATA REQ %s: %d %08x %u\n", s
->name
, s
->q_full
.bytesused
,
620 itv
->dma_data_req_offset
, itv
->dma_data_req_size
);
621 if (itv
->dma_data_req_size
== 0 || s
->q_full
.bytesused
< itv
->dma_data_req_size
) {
622 set_bit(IVTV_F_S_NEEDS_DATA
, &s
->s_flags
);
625 clear_bit(IVTV_F_S_NEEDS_DATA
, &s
->s_flags
);
626 ivtv_queue_move(s
, &s
->q_full
, NULL
, &s
->q_predma
, itv
->dma_data_req_size
);
627 ivtv_dma_stream_dec_prepare(s
, itv
->dma_data_req_offset
+ IVTV_DECODER_OFFSET
, 0);
631 static void ivtv_irq_vsync(struct ivtv
*itv
)
633 /* The vsync interrupt is unusual in that it won't clear until
634 * the end of the first line for the current field, at which
635 * point it clears itself. This can result in repeated vsync
636 * interrupts, or a missed vsync. Read some of the registers
637 * to determine the line being displayed and ensure we handle
638 * one vsync per frame.
640 unsigned int frame
= read_reg(0x28c0) & 1;
641 int last_dma_frame
= atomic_read(&itv
->yuv_info
.next_dma_frame
);
643 if (0) IVTV_DEBUG_IRQ("DEC VSYNC\n");
645 if (((frame
^ itv
->yuv_info
.lace_sync_field
) == 0 && ((itv
->lastVsyncFrame
& 1) ^ itv
->yuv_info
.lace_sync_field
)) ||
646 (frame
!= (itv
->lastVsyncFrame
& 1) && !itv
->yuv_info
.frame_interlaced
)) {
647 int next_dma_frame
= last_dma_frame
;
649 if (next_dma_frame
>= 0 && next_dma_frame
!= atomic_read(&itv
->yuv_info
.next_fill_frame
)) {
650 write_reg(yuv_offset
[next_dma_frame
] >> 4, 0x82c);
651 write_reg((yuv_offset
[next_dma_frame
] + IVTV_YUV_BUFFER_UV_OFFSET
) >> 4, 0x830);
652 write_reg(yuv_offset
[next_dma_frame
] >> 4, 0x834);
653 write_reg((yuv_offset
[next_dma_frame
] + IVTV_YUV_BUFFER_UV_OFFSET
) >> 4, 0x838);
654 next_dma_frame
= (next_dma_frame
+ 1) & 0x3;
655 atomic_set(&itv
->yuv_info
.next_dma_frame
, next_dma_frame
);
658 if (frame
!= (itv
->lastVsyncFrame
& 1)) {
659 struct ivtv_stream
*s
= ivtv_get_output_stream(itv
);
662 itv
->lastVsyncFrame
+= 1;
664 clear_bit(IVTV_F_I_VALID_DEC_TIMINGS
, &itv
->i_flags
);
665 clear_bit(IVTV_F_I_EV_VSYNC_FIELD
, &itv
->i_flags
);
668 set_bit(IVTV_F_I_EV_VSYNC_FIELD
, &itv
->i_flags
);
670 if (test_bit(IVTV_F_I_EV_VSYNC_ENABLED
, &itv
->i_flags
)) {
671 set_bit(IVTV_F_I_EV_VSYNC
, &itv
->i_flags
);
672 wake_up(&itv
->event_waitq
);
674 wake_up(&itv
->vsync_waitq
);
678 /* Send VBI to saa7127 */
680 set_bit(IVTV_F_I_WORK_HANDLER_VBI
, &itv
->i_flags
);
684 /* Check if we need to update the yuv registers */
685 if ((itv
->yuv_info
.yuv_forced_update
|| itv
->yuv_info
.new_frame_info
[last_dma_frame
].update
) && last_dma_frame
!= -1) {
686 if (!itv
->yuv_info
.new_frame_info
[last_dma_frame
].update
)
687 last_dma_frame
= (last_dma_frame
- 1) & 3;
689 if (itv
->yuv_info
.new_frame_info
[last_dma_frame
].src_w
) {
690 itv
->yuv_info
.update_frame
= last_dma_frame
;
691 itv
->yuv_info
.new_frame_info
[last_dma_frame
].update
= 0;
692 itv
->yuv_info
.yuv_forced_update
= 0;
693 set_bit(IVTV_F_I_WORK_HANDLER_YUV
, &itv
->i_flags
);
698 queue_work(itv
->irq_work_queues
, &itv
->irq_work_queue
);
702 #define IVTV_IRQ_DMA (IVTV_IRQ_DMA_READ | IVTV_IRQ_ENC_DMA_COMPLETE | IVTV_IRQ_DMA_ERR | IVTV_IRQ_ENC_START_CAP | IVTV_IRQ_ENC_VBI_CAP | IVTV_IRQ_DEC_DATA_REQ)
704 irqreturn_t
ivtv_irq_handler(int irq
, void *dev_id
)
706 struct ivtv
*itv
= (struct ivtv
*)dev_id
;
712 spin_lock(&itv
->dma_reg_lock
);
713 /* get contents of irq status register */
714 stat
= read_reg(IVTV_REG_IRQSTATUS
);
716 combo
= ~itv
->irqmask
& stat
;
719 if (combo
) write_reg(combo
, IVTV_REG_IRQSTATUS
);
722 /* The vsync interrupt is unusual and clears itself. If we
723 * took too long, we may have missed it. Do some checks
725 if (~itv
->irqmask
& IVTV_IRQ_DEC_VSYNC
) {
726 /* vsync is enabled, see if we're in a new field */
727 if ((itv
->lastVsyncFrame
& 1) != (read_reg(0x28c0) & 1)) {
728 /* New field, looks like we missed it */
729 IVTV_DEBUG_YUV("VSync interrupt missed %d\n",read_reg(0x28c0)>>16);
735 /* No Vsync expected, wasn't for us */
736 spin_unlock(&itv
->dma_reg_lock
);
741 /* Exclude interrupts noted below from the output, otherwise the log is flooded with
743 if (combo
& ~0xff6d0400)
744 IVTV_DEBUG_IRQ("======= valid IRQ bits: 0x%08x ======\n", combo
);
746 if (combo
& IVTV_IRQ_DEC_DMA_COMPLETE
) {
747 IVTV_DEBUG_IRQ("DEC DMA COMPLETE\n");
750 if (combo
& IVTV_IRQ_DMA_READ
) {
751 ivtv_irq_dma_read(itv
);
754 if (combo
& IVTV_IRQ_ENC_DMA_COMPLETE
) {
755 ivtv_irq_enc_dma_complete(itv
);
758 if (combo
& IVTV_IRQ_DMA_ERR
) {
759 ivtv_irq_dma_err(itv
);
762 if (combo
& IVTV_IRQ_ENC_START_CAP
) {
763 ivtv_irq_enc_start_cap(itv
);
766 if (combo
& IVTV_IRQ_ENC_VBI_CAP
) {
767 ivtv_irq_enc_vbi_cap(itv
);
770 if (combo
& IVTV_IRQ_DEC_VBI_RE_INSERT
) {
771 ivtv_irq_dev_vbi_reinsert(itv
);
774 if (combo
& IVTV_IRQ_ENC_EOS
) {
775 IVTV_DEBUG_IRQ("ENC EOS\n");
776 set_bit(IVTV_F_I_EOS
, &itv
->i_flags
);
777 wake_up(&itv
->cap_w
);
780 if (combo
& IVTV_IRQ_DEC_DATA_REQ
) {
781 ivtv_irq_dec_data_req(itv
);
784 /* Decoder Vertical Sync - We can't rely on 'combo', so check if vsync enabled */
785 if (~itv
->irqmask
& IVTV_IRQ_DEC_VSYNC
) {
789 if (combo
& IVTV_IRQ_ENC_VIM_RST
) {
790 IVTV_DEBUG_IRQ("VIM RST\n");
791 /*ivtv_vapi(itv, CX2341X_ENC_REFRESH_INPUT, 0); */
794 if (combo
& IVTV_IRQ_DEC_AUD_MODE_CHG
) {
795 IVTV_DEBUG_INFO("Stereo mode changed\n");
798 if ((combo
& IVTV_IRQ_DMA
) && !test_bit(IVTV_F_I_DMA
, &itv
->i_flags
)) {
799 for (i
= 0; i
< IVTV_MAX_STREAMS
; i
++) {
800 int idx
= (i
+ itv
->irq_rr_idx
++) % IVTV_MAX_STREAMS
;
801 struct ivtv_stream
*s
= &itv
->streams
[idx
];
803 if (!test_and_clear_bit(IVTV_F_S_DMA_PENDING
, &s
->s_flags
))
805 if (s
->type
>= IVTV_DEC_STREAM_TYPE_MPG
)
806 ivtv_dma_dec_start(s
);
808 ivtv_dma_enc_start(s
);
811 if (i
== IVTV_MAX_STREAMS
&& test_and_clear_bit(IVTV_F_I_UDMA_PENDING
, &itv
->i_flags
)) {
812 ivtv_udma_start(itv
);
816 spin_unlock(&itv
->dma_reg_lock
);
818 /* If we've just handled a 'forced' vsync, it's safest to say it
819 * wasn't ours. Another device may have triggered it at just
822 return vsync_force
? IRQ_NONE
: IRQ_HANDLED
;
825 void ivtv_unfinished_dma(unsigned long arg
)
827 struct ivtv
*itv
= (struct ivtv
*)arg
;
829 if (!test_bit(IVTV_F_I_DMA
, &itv
->i_flags
))
831 IVTV_ERR("DMA TIMEOUT %08x %d\n", read_reg(IVTV_REG_DMASTATUS
), itv
->cur_dma_stream
);
833 write_reg(read_reg(IVTV_REG_DMASTATUS
) & 3, IVTV_REG_DMASTATUS
);
834 clear_bit(IVTV_F_I_UDMA
, &itv
->i_flags
);
835 clear_bit(IVTV_F_I_DMA
, &itv
->i_flags
);
836 itv
->cur_dma_stream
= -1;
837 wake_up(&itv
->dma_waitq
);