ivtv-irq.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /* interrupt handling
  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>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "ivtv-driver.h"
  18. #include "ivtv-queue.h"
  19. #include "ivtv-udma.h"
  20. #include "ivtv-irq.h"
  21. #include "ivtv-mailbox.h"
  22. #include "ivtv-vbi.h"
  23. #include "ivtv-yuv.h"
  24. #include <media/v4l2-event.h>
  25. #define DMA_MAGIC_COOKIE 0x000001fe
  26. static void ivtv_dma_dec_start(struct ivtv_stream *s);
  27. static const int ivtv_stream_map[] = {
  28. IVTV_ENC_STREAM_TYPE_MPG,
  29. IVTV_ENC_STREAM_TYPE_YUV,
  30. IVTV_ENC_STREAM_TYPE_PCM,
  31. IVTV_ENC_STREAM_TYPE_VBI,
  32. };
  33. static void ivtv_pcm_work_handler(struct ivtv *itv)
  34. {
  35. struct ivtv_stream *s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  36. struct ivtv_buffer *buf;
  37. /* Pass the PCM data to ivtv-alsa */
  38. while (1) {
  39. /*
  40. * Users should not be using both the ALSA and V4L2 PCM audio
  41. * capture interfaces at the same time. If the user is doing
  42. * this, there maybe a buffer in q_io to grab, use, and put
  43. * back in rotation.
  44. */
  45. buf = ivtv_dequeue(s, &s->q_io);
  46. if (buf == NULL)
  47. buf = ivtv_dequeue(s, &s->q_full);
  48. if (buf == NULL)
  49. break;
  50. if (buf->readpos < buf->bytesused)
  51. itv->pcm_announce_callback(itv->alsa,
  52. (u8 *)(buf->buf + buf->readpos),
  53. (size_t)(buf->bytesused - buf->readpos));
  54. ivtv_enqueue(s, buf, &s->q_free);
  55. }
  56. }
  57. static void ivtv_pio_work_handler(struct ivtv *itv)
  58. {
  59. struct ivtv_stream *s = &itv->streams[itv->cur_pio_stream];
  60. struct ivtv_buffer *buf;
  61. int i = 0;
  62. IVTV_DEBUG_HI_DMA("ivtv_pio_work_handler\n");
  63. if (itv->cur_pio_stream < 0 || itv->cur_pio_stream >= IVTV_MAX_STREAMS ||
  64. s->vdev.v4l2_dev == NULL || !ivtv_use_pio(s)) {
  65. itv->cur_pio_stream = -1;
  66. /* trigger PIO complete user interrupt */
  67. write_reg(IVTV_IRQ_ENC_PIO_COMPLETE, 0x44);
  68. return;
  69. }
  70. IVTV_DEBUG_HI_DMA("Process PIO %s\n", s->name);
  71. list_for_each_entry(buf, &s->q_dma.list, list) {
  72. u32 size = s->sg_processing[i].size & 0x3ffff;
  73. /* Copy the data from the card to the buffer */
  74. if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
  75. memcpy_fromio(buf->buf, itv->dec_mem + s->sg_processing[i].src - IVTV_DECODER_OFFSET, size);
  76. }
  77. else {
  78. memcpy_fromio(buf->buf, itv->enc_mem + s->sg_processing[i].src, size);
  79. }
  80. i++;
  81. if (i == s->sg_processing_size)
  82. break;
  83. }
  84. write_reg(IVTV_IRQ_ENC_PIO_COMPLETE, 0x44);
  85. }
  86. void ivtv_irq_work_handler(struct kthread_work *work)
  87. {
  88. struct ivtv *itv = container_of(work, struct ivtv, irq_work);
  89. if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_PIO, &itv->i_flags))
  90. ivtv_pio_work_handler(itv);
  91. if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_VBI, &itv->i_flags))
  92. ivtv_vbi_work_handler(itv);
  93. if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_YUV, &itv->i_flags))
  94. ivtv_yuv_work_handler(itv);
  95. if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_PCM, &itv->i_flags))
  96. ivtv_pcm_work_handler(itv);
  97. }
  98. /* Determine the required DMA size, setup enough buffers in the predma queue and
  99. actually copy the data from the card to the buffers in case a PIO transfer is
  100. required for this stream.
  101. */
  102. static int stream_enc_dma_append(struct ivtv_stream *s, u32 data[CX2341X_MBOX_MAX_DATA])
  103. {
  104. struct ivtv *itv = s->itv;
  105. struct ivtv_buffer *buf;
  106. u32 bytes_needed = 0;
  107. u32 offset, size;
  108. u32 UVoffset = 0, UVsize = 0;
  109. int skip_bufs = s->q_predma.buffers;
  110. int idx = s->sg_pending_size;
  111. int rc;
  112. /* sanity checks */
  113. if (s->vdev.v4l2_dev == NULL) {
  114. IVTV_DEBUG_WARN("Stream %s not started\n", s->name);
  115. return -1;
  116. }
  117. if (!test_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
  118. IVTV_DEBUG_WARN("Stream %s not open\n", s->name);
  119. return -1;
  120. }
  121. /* determine offset, size and PTS for the various streams */
  122. switch (s->type) {
  123. case IVTV_ENC_STREAM_TYPE_MPG:
  124. offset = data[1];
  125. size = data[2];
  126. s->pending_pts = 0;
  127. break;
  128. case IVTV_ENC_STREAM_TYPE_YUV:
  129. offset = data[1];
  130. size = data[2];
  131. UVoffset = data[3];
  132. UVsize = data[4];
  133. s->pending_pts = ((u64) data[5] << 32) | data[6];
  134. break;
  135. case IVTV_ENC_STREAM_TYPE_PCM:
  136. offset = data[1] + 12;
  137. size = data[2] - 12;
  138. s->pending_pts = read_dec(offset - 8) |
  139. ((u64)(read_dec(offset - 12)) << 32);
  140. if (itv->has_cx23415)
  141. offset += IVTV_DECODER_OFFSET;
  142. break;
  143. case IVTV_ENC_STREAM_TYPE_VBI:
  144. size = itv->vbi.enc_size * itv->vbi.fpi;
  145. offset = read_enc(itv->vbi.enc_start - 4) + 12;
  146. if (offset == 12) {
  147. IVTV_DEBUG_INFO("VBI offset == 0\n");
  148. return -1;
  149. }
  150. s->pending_pts = read_enc(offset - 4) | ((u64)read_enc(offset - 8) << 32);
  151. break;
  152. case IVTV_DEC_STREAM_TYPE_VBI:
  153. size = read_dec(itv->vbi.dec_start + 4) + 8;
  154. offset = read_dec(itv->vbi.dec_start) + itv->vbi.dec_start;
  155. s->pending_pts = 0;
  156. offset += IVTV_DECODER_OFFSET;
  157. break;
  158. default:
  159. /* shouldn't happen */
  160. return -1;
  161. }
  162. /* if this is the start of the DMA then fill in the magic cookie */
  163. if (s->sg_pending_size == 0 && ivtv_use_dma(s)) {
  164. if (itv->has_cx23415 && (s->type == IVTV_ENC_STREAM_TYPE_PCM ||
  165. s->type == IVTV_DEC_STREAM_TYPE_VBI)) {
  166. s->pending_backup = read_dec(offset - IVTV_DECODER_OFFSET);
  167. write_dec_sync(DMA_MAGIC_COOKIE, offset - IVTV_DECODER_OFFSET);
  168. }
  169. else {
  170. s->pending_backup = read_enc(offset);
  171. write_enc_sync(DMA_MAGIC_COOKIE, offset);
  172. }
  173. s->pending_offset = offset;
  174. }
  175. bytes_needed = size;
  176. if (s->type == IVTV_ENC_STREAM_TYPE_YUV) {
  177. /* The size for the Y samples needs to be rounded upwards to a
  178. multiple of the buf_size. The UV samples then start in the
  179. next buffer. */
  180. bytes_needed = s->buf_size * ((bytes_needed + s->buf_size - 1) / s->buf_size);
  181. bytes_needed += UVsize;
  182. }
  183. IVTV_DEBUG_HI_DMA("%s %s: 0x%08x bytes at 0x%08x\n",
  184. ivtv_use_pio(s) ? "PIO" : "DMA", s->name, bytes_needed, offset);
  185. rc = ivtv_queue_move(s, &s->q_free, &s->q_full, &s->q_predma, bytes_needed);
  186. if (rc < 0) { /* Insufficient buffers */
  187. IVTV_DEBUG_WARN("Cannot obtain %d bytes for %s data transfer\n",
  188. bytes_needed, s->name);
  189. return -1;
  190. }
  191. if (rc && !s->buffers_stolen && test_bit(IVTV_F_S_APPL_IO, &s->s_flags)) {
  192. IVTV_WARN("All %s stream buffers are full. Dropping data.\n", s->name);
  193. IVTV_WARN("Cause: the application is not reading fast enough.\n");
  194. }
  195. s->buffers_stolen = rc;
  196. /* got the buffers, now fill in sg_pending */
  197. buf = list_entry(s->q_predma.list.next, struct ivtv_buffer, list);
  198. memset(buf->buf, 0, 128);
  199. list_for_each_entry(buf, &s->q_predma.list, list) {
  200. if (skip_bufs-- > 0)
  201. continue;
  202. s->sg_pending[idx].dst = buf->dma_handle;
  203. s->sg_pending[idx].src = offset;
  204. s->sg_pending[idx].size = s->buf_size;
  205. buf->bytesused = min(size, s->buf_size);
  206. buf->dma_xfer_cnt = s->dma_xfer_cnt;
  207. s->q_predma.bytesused += buf->bytesused;
  208. size -= buf->bytesused;
  209. offset += s->buf_size;
  210. /* Sync SG buffers */
  211. ivtv_buf_sync_for_device(s, buf);
  212. if (size == 0) { /* YUV */
  213. /* process the UV section */
  214. offset = UVoffset;
  215. size = UVsize;
  216. }
  217. idx++;
  218. }
  219. s->sg_pending_size = idx;
  220. return 0;
  221. }
  222. static void dma_post(struct ivtv_stream *s)
  223. {
  224. struct ivtv *itv = s->itv;
  225. struct ivtv_buffer *buf = NULL;
  226. struct list_head *p;
  227. u32 offset;
  228. __le32 *u32buf;
  229. int x = 0;
  230. IVTV_DEBUG_HI_DMA("%s %s completed (%x)\n", ivtv_use_pio(s) ? "PIO" : "DMA",
  231. s->name, s->dma_offset);
  232. list_for_each(p, &s->q_dma.list) {
  233. buf = list_entry(p, struct ivtv_buffer, list);
  234. u32buf = (__le32 *)buf->buf;
  235. /* Sync Buffer */
  236. ivtv_buf_sync_for_cpu(s, buf);
  237. if (x == 0 && ivtv_use_dma(s)) {
  238. offset = s->dma_last_offset;
  239. if (le32_to_cpu(u32buf[offset / 4]) != DMA_MAGIC_COOKIE)
  240. {
  241. for (offset = 0; offset < 64; offset++)
  242. if (le32_to_cpu(u32buf[offset]) == DMA_MAGIC_COOKIE)
  243. break;
  244. offset *= 4;
  245. if (offset == 256) {
  246. IVTV_DEBUG_WARN("%s: Couldn't find start of buffer within the first 256 bytes\n", s->name);
  247. offset = s->dma_last_offset;
  248. }
  249. if (s->dma_last_offset != offset)
  250. IVTV_DEBUG_WARN("%s: offset %d -> %d\n", s->name, s->dma_last_offset, offset);
  251. s->dma_last_offset = offset;
  252. }
  253. if (itv->has_cx23415 && (s->type == IVTV_ENC_STREAM_TYPE_PCM ||
  254. s->type == IVTV_DEC_STREAM_TYPE_VBI)) {
  255. write_dec_sync(0, s->dma_offset - IVTV_DECODER_OFFSET);
  256. }
  257. else {
  258. write_enc_sync(0, s->dma_offset);
  259. }
  260. if (offset) {
  261. buf->bytesused -= offset;
  262. memcpy(buf->buf, buf->buf + offset, buf->bytesused + offset);
  263. }
  264. *u32buf = cpu_to_le32(s->dma_backup);
  265. }
  266. x++;
  267. /* flag byteswap ABCD -> DCBA for MPG & VBI data outside irq */
  268. if (s->type == IVTV_ENC_STREAM_TYPE_MPG ||
  269. s->type == IVTV_ENC_STREAM_TYPE_VBI)
  270. buf->b_flags |= IVTV_F_B_NEED_BUF_SWAP;
  271. }
  272. if (buf)
  273. buf->bytesused += s->dma_last_offset;
  274. if (buf && s->type == IVTV_DEC_STREAM_TYPE_VBI) {
  275. list_for_each_entry(buf, &s->q_dma.list, list) {
  276. /* Parse and Groom VBI Data */
  277. s->q_dma.bytesused -= buf->bytesused;
  278. ivtv_process_vbi_data(itv, buf, 0, s->type);
  279. s->q_dma.bytesused += buf->bytesused;
  280. }
  281. if (s->fh == NULL) {
  282. ivtv_queue_move(s, &s->q_dma, NULL, &s->q_free, 0);
  283. return;
  284. }
  285. }
  286. ivtv_queue_move(s, &s->q_dma, NULL, &s->q_full, s->q_dma.bytesused);
  287. if (s->type == IVTV_ENC_STREAM_TYPE_PCM &&
  288. itv->pcm_announce_callback != NULL) {
  289. /*
  290. * Set up the work handler to pass the data to ivtv-alsa.
  291. *
  292. * We just use q_full and let the work handler race with users
  293. * making ivtv-fileops.c calls on the PCM device node.
  294. *
  295. * Users should not be using both the ALSA and V4L2 PCM audio
  296. * capture interfaces at the same time. If the user does this,
  297. * fragments of data will just go out each interface as they
  298. * race for PCM data.
  299. */
  300. set_bit(IVTV_F_I_WORK_HANDLER_PCM, &itv->i_flags);
  301. set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
  302. }
  303. if (s->fh)
  304. wake_up(&s->waitq);
  305. }
  306. void ivtv_dma_stream_dec_prepare(struct ivtv_stream *s, u32 offset, int lock)
  307. {
  308. struct ivtv *itv = s->itv;
  309. struct yuv_playback_info *yi = &itv->yuv_info;
  310. u8 frame = yi->draw_frame;
  311. struct yuv_frame_info *f = &yi->new_frame_info[frame];
  312. struct ivtv_buffer *buf;
  313. u32 y_size = 720 * ((f->src_h + 31) & ~31);
  314. u32 uv_offset = offset + IVTV_YUV_BUFFER_UV_OFFSET;
  315. int y_done = 0;
  316. int bytes_written = 0;
  317. int idx = 0;
  318. IVTV_DEBUG_HI_DMA("DEC PREPARE DMA %s: %08x %08x\n", s->name, s->q_predma.bytesused, offset);
  319. /* Insert buffer block for YUV if needed */
  320. if (s->type == IVTV_DEC_STREAM_TYPE_YUV && f->offset_y) {
  321. if (yi->blanking_dmaptr) {
  322. s->sg_pending[idx].src = yi->blanking_dmaptr;
  323. s->sg_pending[idx].dst = offset;
  324. s->sg_pending[idx].size = 720 * 16;
  325. }
  326. offset += 720 * 16;
  327. idx++;
  328. }
  329. list_for_each_entry(buf, &s->q_predma.list, list) {
  330. /* YUV UV Offset from Y Buffer */
  331. if (s->type == IVTV_DEC_STREAM_TYPE_YUV && !y_done &&
  332. (bytes_written + buf->bytesused) >= y_size) {
  333. s->sg_pending[idx].src = buf->dma_handle;
  334. s->sg_pending[idx].dst = offset;
  335. s->sg_pending[idx].size = y_size - bytes_written;
  336. offset = uv_offset;
  337. if (s->sg_pending[idx].size != buf->bytesused) {
  338. idx++;
  339. s->sg_pending[idx].src =
  340. buf->dma_handle + s->sg_pending[idx - 1].size;
  341. s->sg_pending[idx].dst = offset;
  342. s->sg_pending[idx].size =
  343. buf->bytesused - s->sg_pending[idx - 1].size;
  344. offset += s->sg_pending[idx].size;
  345. }
  346. y_done = 1;
  347. } else {
  348. s->sg_pending[idx].src = buf->dma_handle;
  349. s->sg_pending[idx].dst = offset;
  350. s->sg_pending[idx].size = buf->bytesused;
  351. offset += buf->bytesused;
  352. }
  353. bytes_written += buf->bytesused;
  354. /* Sync SG buffers */
  355. ivtv_buf_sync_for_device(s, buf);
  356. idx++;
  357. }
  358. s->sg_pending_size = idx;
  359. /* Sync Hardware SG List of buffers */
  360. ivtv_stream_sync_for_device(s);
  361. if (lock) {
  362. unsigned long flags = 0;
  363. spin_lock_irqsave(&itv->dma_reg_lock, flags);
  364. if (!test_bit(IVTV_F_I_DMA, &itv->i_flags))
  365. ivtv_dma_dec_start(s);
  366. else
  367. set_bit(IVTV_F_S_DMA_PENDING, &s->s_flags);
  368. spin_unlock_irqrestore(&itv->dma_reg_lock, flags);
  369. } else {
  370. if (!test_bit(IVTV_F_I_DMA, &itv->i_flags))
  371. ivtv_dma_dec_start(s);
  372. else
  373. set_bit(IVTV_F_S_DMA_PENDING, &s->s_flags);
  374. }
  375. }
  376. static void ivtv_dma_enc_start_xfer(struct ivtv_stream *s)
  377. {
  378. struct ivtv *itv = s->itv;
  379. s->sg_dma->src = cpu_to_le32(s->sg_processing[s->sg_processed].src);
  380. s->sg_dma->dst = cpu_to_le32(s->sg_processing[s->sg_processed].dst);
  381. s->sg_dma->size = cpu_to_le32(s->sg_processing[s->sg_processed].size | 0x80000000);
  382. s->sg_processed++;
  383. /* Sync Hardware SG List of buffers */
  384. ivtv_stream_sync_for_device(s);
  385. write_reg(s->sg_handle, IVTV_REG_ENCDMAADDR);
  386. write_reg_sync(read_reg(IVTV_REG_DMAXFER) | 0x02, IVTV_REG_DMAXFER);
  387. itv->dma_timer.expires = jiffies + msecs_to_jiffies(300);
  388. add_timer(&itv->dma_timer);
  389. }
  390. static void ivtv_dma_dec_start_xfer(struct ivtv_stream *s)
  391. {
  392. struct ivtv *itv = s->itv;
  393. s->sg_dma->src = cpu_to_le32(s->sg_processing[s->sg_processed].src);
  394. s->sg_dma->dst = cpu_to_le32(s->sg_processing[s->sg_processed].dst);
  395. s->sg_dma->size = cpu_to_le32(s->sg_processing[s->sg_processed].size | 0x80000000);
  396. s->sg_processed++;
  397. /* Sync Hardware SG List of buffers */
  398. ivtv_stream_sync_for_device(s);
  399. write_reg(s->sg_handle, IVTV_REG_DECDMAADDR);
  400. write_reg_sync(read_reg(IVTV_REG_DMAXFER) | 0x01, IVTV_REG_DMAXFER);
  401. itv->dma_timer.expires = jiffies + msecs_to_jiffies(300);
  402. add_timer(&itv->dma_timer);
  403. }
  404. /* start the encoder DMA */
  405. static void ivtv_dma_enc_start(struct ivtv_stream *s)
  406. {
  407. struct ivtv *itv = s->itv;
  408. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  409. int i;
  410. IVTV_DEBUG_HI_DMA("start %s for %s\n", ivtv_use_dma(s) ? "DMA" : "PIO", s->name);
  411. if (s->q_predma.bytesused)
  412. ivtv_queue_move(s, &s->q_predma, NULL, &s->q_dma, s->q_predma.bytesused);
  413. if (ivtv_use_dma(s))
  414. s->sg_pending[s->sg_pending_size - 1].size += 256;
  415. /* If this is an MPEG stream, and VBI data is also pending, then append the
  416. VBI DMA to the MPEG DMA and transfer both sets of data at once.
  417. VBI DMA is a second class citizen compared to MPEG and mixing them together
  418. will confuse the firmware (the end of a VBI DMA is seen as the end of a
  419. MPEG DMA, thus effectively dropping an MPEG frame). So instead we make
  420. sure we only use the MPEG DMA to transfer the VBI DMA if both are in
  421. use. This way no conflicts occur. */
  422. clear_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags);
  423. if (s->type == IVTV_ENC_STREAM_TYPE_MPG && s_vbi->sg_pending_size &&
  424. s->sg_pending_size + s_vbi->sg_pending_size <= s->buffers) {
  425. ivtv_queue_move(s_vbi, &s_vbi->q_predma, NULL, &s_vbi->q_dma, s_vbi->q_predma.bytesused);
  426. if (ivtv_use_dma(s_vbi))
  427. s_vbi->sg_pending[s_vbi->sg_pending_size - 1].size += 256;
  428. for (i = 0; i < s_vbi->sg_pending_size; i++) {
  429. s->sg_pending[s->sg_pending_size++] = s_vbi->sg_pending[i];
  430. }
  431. s_vbi->dma_offset = s_vbi->pending_offset;
  432. s_vbi->sg_pending_size = 0;
  433. s_vbi->dma_xfer_cnt++;
  434. set_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags);
  435. IVTV_DEBUG_HI_DMA("include DMA for %s\n", s_vbi->name);
  436. }
  437. s->dma_xfer_cnt++;
  438. memcpy(s->sg_processing, s->sg_pending, sizeof(struct ivtv_sg_host_element) * s->sg_pending_size);
  439. s->sg_processing_size = s->sg_pending_size;
  440. s->sg_pending_size = 0;
  441. s->sg_processed = 0;
  442. s->dma_offset = s->pending_offset;
  443. s->dma_backup = s->pending_backup;
  444. s->dma_pts = s->pending_pts;
  445. if (ivtv_use_pio(s)) {
  446. set_bit(IVTV_F_I_WORK_HANDLER_PIO, &itv->i_flags);
  447. set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
  448. set_bit(IVTV_F_I_PIO, &itv->i_flags);
  449. itv->cur_pio_stream = s->type;
  450. }
  451. else {
  452. itv->dma_retries = 0;
  453. ivtv_dma_enc_start_xfer(s);
  454. set_bit(IVTV_F_I_DMA, &itv->i_flags);
  455. itv->cur_dma_stream = s->type;
  456. }
  457. }
  458. static void ivtv_dma_dec_start(struct ivtv_stream *s)
  459. {
  460. struct ivtv *itv = s->itv;
  461. if (s->q_predma.bytesused)
  462. ivtv_queue_move(s, &s->q_predma, NULL, &s->q_dma, s->q_predma.bytesused);
  463. s->dma_xfer_cnt++;
  464. memcpy(s->sg_processing, s->sg_pending, sizeof(struct ivtv_sg_host_element) * s->sg_pending_size);
  465. s->sg_processing_size = s->sg_pending_size;
  466. s->sg_pending_size = 0;
  467. s->sg_processed = 0;
  468. IVTV_DEBUG_HI_DMA("start DMA for %s\n", s->name);
  469. itv->dma_retries = 0;
  470. ivtv_dma_dec_start_xfer(s);
  471. set_bit(IVTV_F_I_DMA, &itv->i_flags);
  472. itv->cur_dma_stream = s->type;
  473. }
  474. static void ivtv_irq_dma_read(struct ivtv *itv)
  475. {
  476. struct ivtv_stream *s = NULL;
  477. struct ivtv_buffer *buf;
  478. int hw_stream_type = 0;
  479. IVTV_DEBUG_HI_IRQ("DEC DMA READ\n");
  480. del_timer(&itv->dma_timer);
  481. if (!test_bit(IVTV_F_I_UDMA, &itv->i_flags) && itv->cur_dma_stream < 0)
  482. return;
  483. if (!test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
  484. s = &itv->streams[itv->cur_dma_stream];
  485. ivtv_stream_sync_for_cpu(s);
  486. if (read_reg(IVTV_REG_DMASTATUS) & 0x14) {
  487. IVTV_DEBUG_WARN("DEC DMA ERROR %x (xfer %d of %d, retry %d)\n",
  488. read_reg(IVTV_REG_DMASTATUS),
  489. s->sg_processed, s->sg_processing_size, itv->dma_retries);
  490. write_reg(read_reg(IVTV_REG_DMASTATUS) & 3, IVTV_REG_DMASTATUS);
  491. if (itv->dma_retries == 3) {
  492. /* Too many retries, give up on this frame */
  493. itv->dma_retries = 0;
  494. s->sg_processed = s->sg_processing_size;
  495. }
  496. else {
  497. /* Retry, starting with the first xfer segment.
  498. Just retrying the current segment is not sufficient. */
  499. s->sg_processed = 0;
  500. itv->dma_retries++;
  501. }
  502. }
  503. if (s->sg_processed < s->sg_processing_size) {
  504. /* DMA next buffer */
  505. ivtv_dma_dec_start_xfer(s);
  506. return;
  507. }
  508. if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
  509. hw_stream_type = 2;
  510. IVTV_DEBUG_HI_DMA("DEC DATA READ %s: %d\n", s->name, s->q_dma.bytesused);
  511. /* For some reason must kick the firmware, like PIO mode,
  512. I think this tells the firmware we are done and the size
  513. of the xfer so it can calculate what we need next.
  514. I think we can do this part ourselves but would have to
  515. fully calculate xfer info ourselves and not use interrupts
  516. */
  517. ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, s->q_dma.bytesused,
  518. hw_stream_type);
  519. /* Free last DMA call */
  520. while ((buf = ivtv_dequeue(s, &s->q_dma)) != NULL) {
  521. ivtv_buf_sync_for_cpu(s, buf);
  522. ivtv_enqueue(s, buf, &s->q_free);
  523. }
  524. wake_up(&s->waitq);
  525. }
  526. clear_bit(IVTV_F_I_UDMA, &itv->i_flags);
  527. clear_bit(IVTV_F_I_DMA, &itv->i_flags);
  528. itv->cur_dma_stream = -1;
  529. wake_up(&itv->dma_waitq);
  530. }
  531. static void ivtv_irq_enc_dma_complete(struct ivtv *itv)
  532. {
  533. u32 data[CX2341X_MBOX_MAX_DATA];
  534. struct ivtv_stream *s;
  535. ivtv_api_get_data(&itv->enc_mbox, IVTV_MBOX_DMA_END, 2, data);
  536. IVTV_DEBUG_HI_IRQ("ENC DMA COMPLETE %x %d (%d)\n", data[0], data[1], itv->cur_dma_stream);
  537. del_timer(&itv->dma_timer);
  538. if (itv->cur_dma_stream < 0)
  539. return;
  540. s = &itv->streams[itv->cur_dma_stream];
  541. ivtv_stream_sync_for_cpu(s);
  542. if (data[0] & 0x18) {
  543. IVTV_DEBUG_WARN("ENC DMA ERROR %x (offset %08x, xfer %d of %d, retry %d)\n", data[0],
  544. s->dma_offset, s->sg_processed, s->sg_processing_size, itv->dma_retries);
  545. write_reg(read_reg(IVTV_REG_DMASTATUS) & 3, IVTV_REG_DMASTATUS);
  546. if (itv->dma_retries == 3) {
  547. /* Too many retries, give up on this frame */
  548. itv->dma_retries = 0;
  549. s->sg_processed = s->sg_processing_size;
  550. }
  551. else {
  552. /* Retry, starting with the first xfer segment.
  553. Just retrying the current segment is not sufficient. */
  554. s->sg_processed = 0;
  555. itv->dma_retries++;
  556. }
  557. }
  558. if (s->sg_processed < s->sg_processing_size) {
  559. /* DMA next buffer */
  560. ivtv_dma_enc_start_xfer(s);
  561. return;
  562. }
  563. clear_bit(IVTV_F_I_DMA, &itv->i_flags);
  564. itv->cur_dma_stream = -1;
  565. dma_post(s);
  566. if (test_and_clear_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags)) {
  567. s = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  568. dma_post(s);
  569. }
  570. s->sg_processing_size = 0;
  571. s->sg_processed = 0;
  572. wake_up(&itv->dma_waitq);
  573. }
  574. static void ivtv_irq_enc_pio_complete(struct ivtv *itv)
  575. {
  576. struct ivtv_stream *s;
  577. if (itv->cur_pio_stream < 0 || itv->cur_pio_stream >= IVTV_MAX_STREAMS) {
  578. itv->cur_pio_stream = -1;
  579. return;
  580. }
  581. s = &itv->streams[itv->cur_pio_stream];
  582. IVTV_DEBUG_HI_IRQ("ENC PIO COMPLETE %s\n", s->name);
  583. clear_bit(IVTV_F_I_PIO, &itv->i_flags);
  584. itv->cur_pio_stream = -1;
  585. dma_post(s);
  586. if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
  587. ivtv_vapi(itv, CX2341X_ENC_SCHED_DMA_TO_HOST, 3, 0, 0, 0);
  588. else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
  589. ivtv_vapi(itv, CX2341X_ENC_SCHED_DMA_TO_HOST, 3, 0, 0, 1);
  590. else if (s->type == IVTV_ENC_STREAM_TYPE_PCM)
  591. ivtv_vapi(itv, CX2341X_ENC_SCHED_DMA_TO_HOST, 3, 0, 0, 2);
  592. clear_bit(IVTV_F_I_PIO, &itv->i_flags);
  593. if (test_and_clear_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags)) {
  594. s = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  595. dma_post(s);
  596. }
  597. wake_up(&itv->dma_waitq);
  598. }
  599. static void ivtv_irq_dma_err(struct ivtv *itv)
  600. {
  601. u32 data[CX2341X_MBOX_MAX_DATA];
  602. u32 status;
  603. del_timer(&itv->dma_timer);
  604. ivtv_api_get_data(&itv->enc_mbox, IVTV_MBOX_DMA_END, 2, data);
  605. status = read_reg(IVTV_REG_DMASTATUS);
  606. IVTV_DEBUG_WARN("DMA ERROR %08x %08x %08x %d\n", data[0], data[1],
  607. status, itv->cur_dma_stream);
  608. /*
  609. * We do *not* write back to the IVTV_REG_DMASTATUS register to
  610. * clear the error status, if either the encoder write (0x02) or
  611. * decoder read (0x01) bus master DMA operation do not indicate
  612. * completed. We can race with the DMA engine, which may have
  613. * transitioned to completed status *after* we read the register.
  614. * Setting a IVTV_REG_DMASTATUS flag back to "busy" status, after the
  615. * DMA engine has completed, will cause the DMA engine to stop working.
  616. */
  617. status &= 0x3;
  618. if (status == 0x3)
  619. write_reg(status, IVTV_REG_DMASTATUS);
  620. if (!test_bit(IVTV_F_I_UDMA, &itv->i_flags) &&
  621. itv->cur_dma_stream >= 0 && itv->cur_dma_stream < IVTV_MAX_STREAMS) {
  622. struct ivtv_stream *s = &itv->streams[itv->cur_dma_stream];
  623. if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
  624. /* retry */
  625. /*
  626. * FIXME - handle cases of DMA error similar to
  627. * encoder below, except conditioned on status & 0x1
  628. */
  629. ivtv_dma_dec_start(s);
  630. return;
  631. } else {
  632. if ((status & 0x2) == 0) {
  633. /*
  634. * CX2341x Bus Master DMA write is ongoing.
  635. * Reset the timer and let it complete.
  636. */
  637. itv->dma_timer.expires =
  638. jiffies + msecs_to_jiffies(600);
  639. add_timer(&itv->dma_timer);
  640. return;
  641. }
  642. if (itv->dma_retries < 3) {
  643. /*
  644. * CX2341x Bus Master DMA write has ended.
  645. * Retry the write, starting with the first
  646. * xfer segment. Just retrying the current
  647. * segment is not sufficient.
  648. */
  649. s->sg_processed = 0;
  650. itv->dma_retries++;
  651. ivtv_dma_enc_start_xfer(s);
  652. return;
  653. }
  654. /* Too many retries, give up on this one */
  655. }
  656. }
  657. if (test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
  658. ivtv_udma_start(itv);
  659. return;
  660. }
  661. clear_bit(IVTV_F_I_UDMA, &itv->i_flags);
  662. clear_bit(IVTV_F_I_DMA, &itv->i_flags);
  663. itv->cur_dma_stream = -1;
  664. wake_up(&itv->dma_waitq);
  665. }
  666. static void ivtv_irq_enc_start_cap(struct ivtv *itv)
  667. {
  668. u32 data[CX2341X_MBOX_MAX_DATA];
  669. struct ivtv_stream *s;
  670. /* Get DMA destination and size arguments from card */
  671. ivtv_api_get_data(&itv->enc_mbox, IVTV_MBOX_DMA, 7, data);
  672. IVTV_DEBUG_HI_IRQ("ENC START CAP %d: %08x %08x\n", data[0], data[1], data[2]);
  673. if (data[0] > 2 || data[1] == 0 || data[2] == 0) {
  674. IVTV_DEBUG_WARN("Unknown input: %08x %08x %08x\n",
  675. data[0], data[1], data[2]);
  676. return;
  677. }
  678. s = &itv->streams[ivtv_stream_map[data[0]]];
  679. if (!stream_enc_dma_append(s, data)) {
  680. set_bit(ivtv_use_pio(s) ? IVTV_F_S_PIO_PENDING : IVTV_F_S_DMA_PENDING, &s->s_flags);
  681. }
  682. }
  683. static void ivtv_irq_enc_vbi_cap(struct ivtv *itv)
  684. {
  685. u32 data[CX2341X_MBOX_MAX_DATA];
  686. struct ivtv_stream *s;
  687. IVTV_DEBUG_HI_IRQ("ENC START VBI CAP\n");
  688. s = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  689. if (!stream_enc_dma_append(s, data))
  690. set_bit(ivtv_use_pio(s) ? IVTV_F_S_PIO_PENDING : IVTV_F_S_DMA_PENDING, &s->s_flags);
  691. }
  692. static void ivtv_irq_dec_vbi_reinsert(struct ivtv *itv)
  693. {
  694. u32 data[CX2341X_MBOX_MAX_DATA];
  695. struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
  696. IVTV_DEBUG_HI_IRQ("DEC VBI REINSERT\n");
  697. if (test_bit(IVTV_F_S_CLAIMED, &s->s_flags) &&
  698. !stream_enc_dma_append(s, data)) {
  699. set_bit(IVTV_F_S_PIO_PENDING, &s->s_flags);
  700. }
  701. }
  702. static void ivtv_irq_dec_data_req(struct ivtv *itv)
  703. {
  704. u32 data[CX2341X_MBOX_MAX_DATA];
  705. struct ivtv_stream *s;
  706. /* YUV or MPG */
  707. if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags)) {
  708. ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 2, data);
  709. itv->dma_data_req_size =
  710. 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
  711. itv->dma_data_req_offset = data[1];
  712. if (atomic_read(&itv->yuv_info.next_dma_frame) >= 0)
  713. ivtv_yuv_frame_complete(itv);
  714. s = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
  715. }
  716. else {
  717. ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 3, data);
  718. itv->dma_data_req_size = min_t(u32, data[2], 0x10000);
  719. itv->dma_data_req_offset = data[1];
  720. s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
  721. }
  722. IVTV_DEBUG_HI_IRQ("DEC DATA REQ %s: %d %08x %u\n", s->name, s->q_full.bytesused,
  723. itv->dma_data_req_offset, itv->dma_data_req_size);
  724. if (itv->dma_data_req_size == 0 || s->q_full.bytesused < itv->dma_data_req_size) {
  725. set_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
  726. }
  727. else {
  728. if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
  729. ivtv_yuv_setup_stream_frame(itv);
  730. clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
  731. ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
  732. ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 0);
  733. }
  734. }
  735. static void ivtv_irq_vsync(struct ivtv *itv)
  736. {
  737. /* The vsync interrupt is unusual in that it won't clear until
  738. * the end of the first line for the current field, at which
  739. * point it clears itself. This can result in repeated vsync
  740. * interrupts, or a missed vsync. Read some of the registers
  741. * to determine the line being displayed and ensure we handle
  742. * one vsync per frame.
  743. */
  744. unsigned int frame = read_reg(IVTV_REG_DEC_LINE_FIELD) & 1;
  745. struct yuv_playback_info *yi = &itv->yuv_info;
  746. int last_dma_frame = atomic_read(&yi->next_dma_frame);
  747. struct yuv_frame_info *f = &yi->new_frame_info[last_dma_frame];
  748. if (0) IVTV_DEBUG_IRQ("DEC VSYNC\n");
  749. if (((frame ^ f->sync_field) == 0 &&
  750. ((itv->last_vsync_field & 1) ^ f->sync_field)) ||
  751. (frame != (itv->last_vsync_field & 1) && !f->interlaced)) {
  752. int next_dma_frame = last_dma_frame;
  753. if (!(f->interlaced && f->delay && yi->fields_lapsed < 1)) {
  754. if (next_dma_frame >= 0 && next_dma_frame != atomic_read(&yi->next_fill_frame)) {
  755. write_reg(yuv_offset[next_dma_frame] >> 4, 0x82c);
  756. write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x830);
  757. write_reg(yuv_offset[next_dma_frame] >> 4, 0x834);
  758. write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x838);
  759. next_dma_frame = (next_dma_frame + 1) % IVTV_YUV_BUFFERS;
  760. atomic_set(&yi->next_dma_frame, next_dma_frame);
  761. yi->fields_lapsed = -1;
  762. yi->running = 1;
  763. }
  764. }
  765. }
  766. if (frame != (itv->last_vsync_field & 1)) {
  767. static const struct v4l2_event evtop = {
  768. .type = V4L2_EVENT_VSYNC,
  769. .u.vsync.field = V4L2_FIELD_TOP,
  770. };
  771. static const struct v4l2_event evbottom = {
  772. .type = V4L2_EVENT_VSYNC,
  773. .u.vsync.field = V4L2_FIELD_BOTTOM,
  774. };
  775. struct ivtv_stream *s = ivtv_get_output_stream(itv);
  776. itv->last_vsync_field += 1;
  777. if (frame == 0) {
  778. clear_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
  779. clear_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags);
  780. }
  781. else {
  782. set_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags);
  783. }
  784. if (test_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags)) {
  785. set_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags);
  786. wake_up(&itv->event_waitq);
  787. if (s)
  788. wake_up(&s->waitq);
  789. }
  790. if (s && s->vdev.v4l2_dev)
  791. v4l2_event_queue(&s->vdev, frame ? &evtop : &evbottom);
  792. wake_up(&itv->vsync_waitq);
  793. /* Send VBI to saa7127 */
  794. if (frame && (itv->output_mode == OUT_PASSTHROUGH ||
  795. test_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags) ||
  796. test_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags) ||
  797. test_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags))) {
  798. set_bit(IVTV_F_I_WORK_HANDLER_VBI, &itv->i_flags);
  799. set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
  800. }
  801. /* Check if we need to update the yuv registers */
  802. if (yi->running && (yi->yuv_forced_update || f->update)) {
  803. if (!f->update) {
  804. last_dma_frame =
  805. (u8)(atomic_read(&yi->next_dma_frame) -
  806. 1) % IVTV_YUV_BUFFERS;
  807. f = &yi->new_frame_info[last_dma_frame];
  808. }
  809. if (f->src_w) {
  810. yi->update_frame = last_dma_frame;
  811. f->update = 0;
  812. yi->yuv_forced_update = 0;
  813. set_bit(IVTV_F_I_WORK_HANDLER_YUV, &itv->i_flags);
  814. set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
  815. }
  816. }
  817. yi->fields_lapsed++;
  818. }
  819. }
  820. #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 | IVTV_IRQ_DEC_VBI_RE_INSERT)
  821. irqreturn_t ivtv_irq_handler(int irq, void *dev_id)
  822. {
  823. struct ivtv *itv = (struct ivtv *)dev_id;
  824. u32 combo;
  825. u32 stat;
  826. int i;
  827. u8 vsync_force = 0;
  828. spin_lock(&itv->dma_reg_lock);
  829. /* get contents of irq status register */
  830. stat = read_reg(IVTV_REG_IRQSTATUS);
  831. combo = ~itv->irqmask & stat;
  832. /* Clear out IRQ */
  833. if (combo) write_reg(combo, IVTV_REG_IRQSTATUS);
  834. if (0 == combo) {
  835. /* The vsync interrupt is unusual and clears itself. If we
  836. * took too long, we may have missed it. Do some checks
  837. */
  838. if (~itv->irqmask & IVTV_IRQ_DEC_VSYNC) {
  839. /* vsync is enabled, see if we're in a new field */
  840. if ((itv->last_vsync_field & 1) !=
  841. (read_reg(IVTV_REG_DEC_LINE_FIELD) & 1)) {
  842. /* New field, looks like we missed it */
  843. IVTV_DEBUG_YUV("VSync interrupt missed %d\n",
  844. read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16);
  845. vsync_force = 1;
  846. }
  847. }
  848. if (!vsync_force) {
  849. /* No Vsync expected, wasn't for us */
  850. spin_unlock(&itv->dma_reg_lock);
  851. return IRQ_NONE;
  852. }
  853. }
  854. /* Exclude interrupts noted below from the output, otherwise the log is flooded with
  855. these messages */
  856. if (combo & ~0xff6d0400)
  857. IVTV_DEBUG_HI_IRQ("======= valid IRQ bits: 0x%08x ======\n", combo);
  858. if (combo & IVTV_IRQ_DEC_DMA_COMPLETE) {
  859. IVTV_DEBUG_HI_IRQ("DEC DMA COMPLETE\n");
  860. }
  861. if (combo & IVTV_IRQ_DMA_READ) {
  862. ivtv_irq_dma_read(itv);
  863. }
  864. if (combo & IVTV_IRQ_ENC_DMA_COMPLETE) {
  865. ivtv_irq_enc_dma_complete(itv);
  866. }
  867. if (combo & IVTV_IRQ_ENC_PIO_COMPLETE) {
  868. ivtv_irq_enc_pio_complete(itv);
  869. }
  870. if (combo & IVTV_IRQ_DMA_ERR) {
  871. ivtv_irq_dma_err(itv);
  872. }
  873. if (combo & IVTV_IRQ_ENC_START_CAP) {
  874. ivtv_irq_enc_start_cap(itv);
  875. }
  876. if (combo & IVTV_IRQ_ENC_VBI_CAP) {
  877. ivtv_irq_enc_vbi_cap(itv);
  878. }
  879. if (combo & IVTV_IRQ_DEC_VBI_RE_INSERT) {
  880. ivtv_irq_dec_vbi_reinsert(itv);
  881. }
  882. if (combo & IVTV_IRQ_ENC_EOS) {
  883. IVTV_DEBUG_IRQ("ENC EOS\n");
  884. set_bit(IVTV_F_I_EOS, &itv->i_flags);
  885. wake_up(&itv->eos_waitq);
  886. }
  887. if (combo & IVTV_IRQ_DEC_DATA_REQ) {
  888. ivtv_irq_dec_data_req(itv);
  889. }
  890. /* Decoder Vertical Sync - We can't rely on 'combo', so check if vsync enabled */
  891. if (~itv->irqmask & IVTV_IRQ_DEC_VSYNC) {
  892. ivtv_irq_vsync(itv);
  893. }
  894. if (combo & IVTV_IRQ_ENC_VIM_RST) {
  895. IVTV_DEBUG_IRQ("VIM RST\n");
  896. /*ivtv_vapi(itv, CX2341X_ENC_REFRESH_INPUT, 0); */
  897. }
  898. if (combo & IVTV_IRQ_DEC_AUD_MODE_CHG) {
  899. IVTV_DEBUG_INFO("Stereo mode changed\n");
  900. }
  901. if ((combo & IVTV_IRQ_DMA) && !test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
  902. itv->irq_rr_idx++;
  903. for (i = 0; i < IVTV_MAX_STREAMS; i++) {
  904. int idx = (i + itv->irq_rr_idx) % IVTV_MAX_STREAMS;
  905. struct ivtv_stream *s = &itv->streams[idx];
  906. if (!test_and_clear_bit(IVTV_F_S_DMA_PENDING, &s->s_flags))
  907. continue;
  908. if (s->type >= IVTV_DEC_STREAM_TYPE_MPG)
  909. ivtv_dma_dec_start(s);
  910. else
  911. ivtv_dma_enc_start(s);
  912. break;
  913. }
  914. if (i == IVTV_MAX_STREAMS &&
  915. test_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
  916. ivtv_udma_start(itv);
  917. }
  918. if ((combo & IVTV_IRQ_DMA) && !test_bit(IVTV_F_I_PIO, &itv->i_flags)) {
  919. itv->irq_rr_idx++;
  920. for (i = 0; i < IVTV_MAX_STREAMS; i++) {
  921. int idx = (i + itv->irq_rr_idx) % IVTV_MAX_STREAMS;
  922. struct ivtv_stream *s = &itv->streams[idx];
  923. if (!test_and_clear_bit(IVTV_F_S_PIO_PENDING, &s->s_flags))
  924. continue;
  925. if (s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type < IVTV_DEC_STREAM_TYPE_MPG)
  926. ivtv_dma_enc_start(s);
  927. break;
  928. }
  929. }
  930. if (test_and_clear_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags)) {
  931. queue_kthread_work(&itv->irq_worker, &itv->irq_work);
  932. }
  933. spin_unlock(&itv->dma_reg_lock);
  934. /* If we've just handled a 'forced' vsync, it's safest to say it
  935. * wasn't ours. Another device may have triggered it at just
  936. * the right time.
  937. */
  938. return vsync_force ? IRQ_NONE : IRQ_HANDLED;
  939. }
  940. void ivtv_unfinished_dma(unsigned long arg)
  941. {
  942. struct ivtv *itv = (struct ivtv *)arg;
  943. if (!test_bit(IVTV_F_I_DMA, &itv->i_flags))
  944. return;
  945. IVTV_ERR("DMA TIMEOUT %08x %d\n", read_reg(IVTV_REG_DMASTATUS), itv->cur_dma_stream);
  946. write_reg(read_reg(IVTV_REG_DMASTATUS) & 3, IVTV_REG_DMASTATUS);
  947. clear_bit(IVTV_F_I_UDMA, &itv->i_flags);
  948. clear_bit(IVTV_F_I_DMA, &itv->i_flags);
  949. itv->cur_dma_stream = -1;
  950. wake_up(&itv->dma_waitq);
  951. }