cx25821-video-upstream-ch2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. *
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include "cx25821-video.h"
  24. #include "cx25821-video-upstream-ch2.h"
  25. #include <linux/fs.h>
  26. #include <linux/errno.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/file.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/slab.h>
  34. #include <linux/uaccess.h>
  35. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  36. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  37. MODULE_LICENSE("GPL");
  38. static int _intr_msk = FLD_VID_SRC_RISC1 | FLD_VID_SRC_UF | FLD_VID_SRC_SYNC |
  39. FLD_VID_SRC_OPC_ERR;
  40. static __le32 *cx25821_update_riscprogram_ch2(struct cx25821_dev *dev,
  41. __le32 *rp, unsigned int offset,
  42. unsigned int bpl, u32 sync_line,
  43. unsigned int lines,
  44. int fifo_enable, int field_type)
  45. {
  46. unsigned int line, i;
  47. int dist_betwn_starts = bpl * 2;
  48. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  49. if (USE_RISC_NOOP_VIDEO) {
  50. for (i = 0; i < NUM_NO_OPS; i++)
  51. *(rp++) = cpu_to_le32(RISC_NOOP);
  52. }
  53. /* scan lines */
  54. for (line = 0; line < lines; line++) {
  55. *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
  56. *(rp++) = cpu_to_le32(dev->_data_buf_phys_addr_ch2 + offset);
  57. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  58. if ((lines <= NTSC_FIELD_HEIGHT) ||
  59. (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC_ch2)) {
  60. offset += dist_betwn_starts;
  61. }
  62. }
  63. return rp;
  64. }
  65. static __le32 *cx25821_risc_field_upstream_ch2(struct cx25821_dev *dev,
  66. __le32 *rp,
  67. dma_addr_t databuf_phys_addr,
  68. unsigned int offset,
  69. u32 sync_line, unsigned int bpl,
  70. unsigned int lines,
  71. int fifo_enable, int field_type)
  72. {
  73. unsigned int line, i;
  74. struct sram_channel *sram_ch =
  75. dev->channels[dev->_channel2_upstream_select].sram_channels;
  76. int dist_betwn_starts = bpl * 2;
  77. /* sync instruction */
  78. if (sync_line != NO_SYNC_LINE)
  79. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  80. if (USE_RISC_NOOP_VIDEO) {
  81. for (i = 0; i < NUM_NO_OPS; i++)
  82. *(rp++) = cpu_to_le32(RISC_NOOP);
  83. }
  84. /* scan lines */
  85. for (line = 0; line < lines; line++) {
  86. *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
  87. *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
  88. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  89. if ((lines <= NTSC_FIELD_HEIGHT) ||
  90. (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC_ch2)) {
  91. offset += dist_betwn_starts;
  92. }
  93. /*
  94. check if we need to enable the FIFO after the first 4 lines
  95. For the upstream video channel, the risc engine will enable
  96. the FIFO.
  97. */
  98. if (fifo_enable && line == 3) {
  99. *(rp++) = RISC_WRITECR;
  100. *(rp++) = sram_ch->dma_ctl;
  101. *(rp++) = FLD_VID_FIFO_EN;
  102. *(rp++) = 0x00000001;
  103. }
  104. }
  105. return rp;
  106. }
  107. int cx25821_risc_buffer_upstream_ch2(struct cx25821_dev *dev,
  108. struct pci_dev *pci,
  109. unsigned int top_offset, unsigned int bpl,
  110. unsigned int lines)
  111. {
  112. __le32 *rp;
  113. int fifo_enable = 0;
  114. int singlefield_lines = lines >> 1; /*get line count for single field */
  115. int odd_num_lines = singlefield_lines;
  116. int frame = 0;
  117. int frame_size = 0;
  118. int databuf_offset = 0;
  119. int risc_program_size = 0;
  120. int risc_flag = RISC_CNT_RESET;
  121. unsigned int bottom_offset = bpl;
  122. dma_addr_t risc_phys_jump_addr;
  123. if (dev->_isNTSC_ch2) {
  124. odd_num_lines = singlefield_lines + 1;
  125. risc_program_size = FRAME1_VID_PROG_SIZE;
  126. if (bpl == Y411_LINE_SZ)
  127. frame_size = FRAME_SIZE_NTSC_Y411;
  128. else
  129. frame_size = FRAME_SIZE_NTSC_Y422;
  130. } else {
  131. risc_program_size = PAL_VID_PROG_SIZE;
  132. if (bpl == Y411_LINE_SZ)
  133. frame_size = FRAME_SIZE_PAL_Y411;
  134. else
  135. frame_size = FRAME_SIZE_PAL_Y422;
  136. }
  137. /* Virtual address of Risc buffer program */
  138. rp = dev->_dma_virt_addr_ch2;
  139. for (frame = 0; frame < NUM_FRAMES; frame++) {
  140. databuf_offset = frame_size * frame;
  141. if (UNSET != top_offset) {
  142. fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
  143. rp = cx25821_risc_field_upstream_ch2(dev, rp,
  144. dev->_data_buf_phys_addr_ch2 + databuf_offset,
  145. top_offset, 0, bpl, odd_num_lines, fifo_enable,
  146. ODD_FIELD);
  147. }
  148. fifo_enable = FIFO_DISABLE;
  149. /* Even field */
  150. rp = cx25821_risc_field_upstream_ch2(dev, rp,
  151. dev->_data_buf_phys_addr_ch2 + databuf_offset,
  152. bottom_offset, 0x200, bpl, singlefield_lines,
  153. fifo_enable, EVEN_FIELD);
  154. if (frame == 0) {
  155. risc_flag = RISC_CNT_RESET;
  156. risc_phys_jump_addr = dev->_dma_phys_start_addr_ch2 +
  157. risc_program_size;
  158. } else {
  159. risc_flag = RISC_CNT_INC;
  160. risc_phys_jump_addr = dev->_dma_phys_start_addr_ch2;
  161. }
  162. /*
  163. * Loop to 2ndFrameRISC or to Start of
  164. * Risc program & generate IRQ
  165. */
  166. *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
  167. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  168. *(rp++) = cpu_to_le32(0);
  169. }
  170. return 0;
  171. }
  172. void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev)
  173. {
  174. struct sram_channel *sram_ch =
  175. dev->channels[VID_UPSTREAM_SRAM_CHANNEL_J].sram_channels;
  176. u32 tmp = 0;
  177. if (!dev->_is_running_ch2) {
  178. pr_info("No video file is currently running so return!\n");
  179. return;
  180. }
  181. /* Disable RISC interrupts */
  182. tmp = cx_read(sram_ch->int_msk);
  183. cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
  184. /* Turn OFF risc and fifo */
  185. tmp = cx_read(sram_ch->dma_ctl);
  186. cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
  187. /* Clear data buffer memory */
  188. if (dev->_data_buf_virt_addr_ch2)
  189. memset(dev->_data_buf_virt_addr_ch2, 0,
  190. dev->_data_buf_size_ch2);
  191. dev->_is_running_ch2 = 0;
  192. dev->_is_first_frame_ch2 = 0;
  193. dev->_frame_count_ch2 = 0;
  194. dev->_file_status_ch2 = END_OF_FILE;
  195. kfree(dev->_irq_queues_ch2);
  196. dev->_irq_queues_ch2 = NULL;
  197. kfree(dev->_filename_ch2);
  198. tmp = cx_read(VID_CH_MODE_SEL);
  199. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  200. }
  201. void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev)
  202. {
  203. if (dev->_is_running_ch2)
  204. cx25821_stop_upstream_video_ch2(dev);
  205. if (dev->_dma_virt_addr_ch2) {
  206. pci_free_consistent(dev->pci, dev->_risc_size_ch2,
  207. dev->_dma_virt_addr_ch2,
  208. dev->_dma_phys_addr_ch2);
  209. dev->_dma_virt_addr_ch2 = NULL;
  210. }
  211. if (dev->_data_buf_virt_addr_ch2) {
  212. pci_free_consistent(dev->pci, dev->_data_buf_size_ch2,
  213. dev->_data_buf_virt_addr_ch2,
  214. dev->_data_buf_phys_addr_ch2);
  215. dev->_data_buf_virt_addr_ch2 = NULL;
  216. }
  217. }
  218. int cx25821_get_frame_ch2(struct cx25821_dev *dev, struct sram_channel *sram_ch)
  219. {
  220. struct file *myfile;
  221. int frame_index_temp = dev->_frame_index_ch2;
  222. int i = 0;
  223. int line_size = (dev->_pixel_format_ch2 == PIXEL_FRMT_411) ?
  224. Y411_LINE_SZ : Y422_LINE_SZ;
  225. int frame_size = 0;
  226. int frame_offset = 0;
  227. ssize_t vfs_read_retval = 0;
  228. char mybuf[line_size];
  229. loff_t file_offset;
  230. loff_t pos;
  231. mm_segment_t old_fs;
  232. if (dev->_file_status_ch2 == END_OF_FILE)
  233. return 0;
  234. if (dev->_isNTSC_ch2) {
  235. frame_size = (line_size == Y411_LINE_SZ) ?
  236. FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
  237. } else {
  238. frame_size = (line_size == Y411_LINE_SZ) ?
  239. FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
  240. }
  241. frame_offset = (frame_index_temp > 0) ? frame_size : 0;
  242. file_offset = dev->_frame_count_ch2 * frame_size;
  243. myfile = filp_open(dev->_filename_ch2, O_RDONLY | O_LARGEFILE, 0);
  244. if (IS_ERR(myfile)) {
  245. const int open_errno = -PTR_ERR(myfile);
  246. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  247. __func__, dev->_filename_ch2, open_errno);
  248. return PTR_ERR(myfile);
  249. } else {
  250. if (!(myfile->f_op)) {
  251. pr_err("%s(): File has no file operations registered!\n",
  252. __func__);
  253. filp_close(myfile, NULL);
  254. return -EIO;
  255. }
  256. if (!myfile->f_op->read) {
  257. pr_err("%s(): File has no READ operations registered!\n",
  258. __func__);
  259. filp_close(myfile, NULL);
  260. return -EIO;
  261. }
  262. pos = myfile->f_pos;
  263. old_fs = get_fs();
  264. set_fs(KERNEL_DS);
  265. for (i = 0; i < dev->_lines_count_ch2; i++) {
  266. pos = file_offset;
  267. vfs_read_retval = vfs_read(myfile, mybuf, line_size,
  268. &pos);
  269. if (vfs_read_retval > 0 && vfs_read_retval == line_size
  270. && dev->_data_buf_virt_addr_ch2 != NULL) {
  271. memcpy((void *)(dev->_data_buf_virt_addr_ch2 +
  272. frame_offset / 4), mybuf,
  273. vfs_read_retval);
  274. }
  275. file_offset += vfs_read_retval;
  276. frame_offset += vfs_read_retval;
  277. if (vfs_read_retval < line_size) {
  278. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  279. __func__);
  280. break;
  281. }
  282. }
  283. if (i > 0)
  284. dev->_frame_count_ch2++;
  285. dev->_file_status_ch2 = (vfs_read_retval == line_size) ?
  286. IN_PROGRESS : END_OF_FILE;
  287. set_fs(old_fs);
  288. filp_close(myfile, NULL);
  289. }
  290. return 0;
  291. }
  292. static void cx25821_vidups_handler_ch2(struct work_struct *work)
  293. {
  294. struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
  295. _irq_work_entry_ch2);
  296. if (!dev) {
  297. pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
  298. __func__);
  299. return;
  300. }
  301. cx25821_get_frame_ch2(dev, dev->channels[dev->
  302. _channel2_upstream_select].sram_channels);
  303. }
  304. int cx25821_openfile_ch2(struct cx25821_dev *dev, struct sram_channel *sram_ch)
  305. {
  306. struct file *myfile;
  307. int i = 0, j = 0;
  308. int line_size = (dev->_pixel_format_ch2 == PIXEL_FRMT_411) ?
  309. Y411_LINE_SZ : Y422_LINE_SZ;
  310. ssize_t vfs_read_retval = 0;
  311. char mybuf[line_size];
  312. loff_t pos;
  313. loff_t offset = (unsigned long)0;
  314. mm_segment_t old_fs;
  315. myfile = filp_open(dev->_filename_ch2, O_RDONLY | O_LARGEFILE, 0);
  316. if (IS_ERR(myfile)) {
  317. const int open_errno = -PTR_ERR(myfile);
  318. pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
  319. __func__, dev->_filename_ch2, open_errno);
  320. return PTR_ERR(myfile);
  321. } else {
  322. if (!(myfile->f_op)) {
  323. pr_err("%s(): File has no file operations registered!\n",
  324. __func__);
  325. filp_close(myfile, NULL);
  326. return -EIO;
  327. }
  328. if (!myfile->f_op->read) {
  329. pr_err("%s(): File has no READ operations registered! Returning\n",
  330. __func__);
  331. filp_close(myfile, NULL);
  332. return -EIO;
  333. }
  334. pos = myfile->f_pos;
  335. old_fs = get_fs();
  336. set_fs(KERNEL_DS);
  337. for (j = 0; j < NUM_FRAMES; j++) {
  338. for (i = 0; i < dev->_lines_count_ch2; i++) {
  339. pos = offset;
  340. vfs_read_retval = vfs_read(myfile, mybuf,
  341. line_size, &pos);
  342. if (vfs_read_retval > 0 &&
  343. vfs_read_retval == line_size &&
  344. dev->_data_buf_virt_addr_ch2 != NULL) {
  345. memcpy((void *)(dev->
  346. _data_buf_virt_addr_ch2
  347. + offset / 4), mybuf,
  348. vfs_read_retval);
  349. }
  350. offset += vfs_read_retval;
  351. if (vfs_read_retval < line_size) {
  352. pr_info("Done: exit %s() since no more bytes to read from Video file\n",
  353. __func__);
  354. break;
  355. }
  356. }
  357. if (i > 0)
  358. dev->_frame_count_ch2++;
  359. if (vfs_read_retval < line_size)
  360. break;
  361. }
  362. dev->_file_status_ch2 = (vfs_read_retval == line_size) ?
  363. IN_PROGRESS : END_OF_FILE;
  364. set_fs(old_fs);
  365. myfile->f_pos = 0;
  366. filp_close(myfile, NULL);
  367. }
  368. return 0;
  369. }
  370. static int cx25821_upstream_buffer_prepare_ch2(struct cx25821_dev *dev,
  371. struct sram_channel *sram_ch,
  372. int bpl)
  373. {
  374. int ret = 0;
  375. dma_addr_t dma_addr;
  376. dma_addr_t data_dma_addr;
  377. if (dev->_dma_virt_addr_ch2 != NULL) {
  378. pci_free_consistent(dev->pci, dev->upstream_riscbuf_size_ch2,
  379. dev->_dma_virt_addr_ch2,
  380. dev->_dma_phys_addr_ch2);
  381. }
  382. dev->_dma_virt_addr_ch2 = pci_alloc_consistent(dev->pci,
  383. dev->upstream_riscbuf_size_ch2, &dma_addr);
  384. dev->_dma_virt_start_addr_ch2 = dev->_dma_virt_addr_ch2;
  385. dev->_dma_phys_start_addr_ch2 = dma_addr;
  386. dev->_dma_phys_addr_ch2 = dma_addr;
  387. dev->_risc_size_ch2 = dev->upstream_riscbuf_size_ch2;
  388. if (!dev->_dma_virt_addr_ch2) {
  389. pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
  390. return -ENOMEM;
  391. }
  392. /* Iniitize at this address until n bytes to 0 */
  393. memset(dev->_dma_virt_addr_ch2, 0, dev->_risc_size_ch2);
  394. if (dev->_data_buf_virt_addr_ch2 != NULL) {
  395. pci_free_consistent(dev->pci, dev->upstream_databuf_size_ch2,
  396. dev->_data_buf_virt_addr_ch2,
  397. dev->_data_buf_phys_addr_ch2);
  398. }
  399. /* For Video Data buffer allocation */
  400. dev->_data_buf_virt_addr_ch2 = pci_alloc_consistent(dev->pci,
  401. dev->upstream_databuf_size_ch2, &data_dma_addr);
  402. dev->_data_buf_phys_addr_ch2 = data_dma_addr;
  403. dev->_data_buf_size_ch2 = dev->upstream_databuf_size_ch2;
  404. if (!dev->_data_buf_virt_addr_ch2) {
  405. pr_err("FAILED to allocate memory for data buffer! Returning\n");
  406. return -ENOMEM;
  407. }
  408. /* Initialize at this address until n bytes to 0 */
  409. memset(dev->_data_buf_virt_addr_ch2, 0, dev->_data_buf_size_ch2);
  410. ret = cx25821_openfile_ch2(dev, sram_ch);
  411. if (ret < 0)
  412. return ret;
  413. /* Creating RISC programs */
  414. ret = cx25821_risc_buffer_upstream_ch2(dev, dev->pci, 0, bpl,
  415. dev->_lines_count_ch2);
  416. if (ret < 0) {
  417. pr_info("Failed creating Video Upstream Risc programs!\n");
  418. goto error;
  419. }
  420. return 0;
  421. error:
  422. return ret;
  423. }
  424. int cx25821_video_upstream_irq_ch2(struct cx25821_dev *dev, int chan_num,
  425. u32 status)
  426. {
  427. u32 int_msk_tmp;
  428. struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  429. int singlefield_lines = NTSC_FIELD_HEIGHT;
  430. int line_size_in_bytes = Y422_LINE_SZ;
  431. int odd_risc_prog_size = 0;
  432. dma_addr_t risc_phys_jump_addr;
  433. __le32 *rp;
  434. if (status & FLD_VID_SRC_RISC1) {
  435. /* We should only process one program per call */
  436. u32 prog_cnt = cx_read(channel->gpcnt);
  437. /*
  438. * Since we've identified our IRQ, clear our bits from the
  439. * interrupt mask and interrupt status registers
  440. */
  441. int_msk_tmp = cx_read(channel->int_msk);
  442. cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
  443. cx_write(channel->int_stat, _intr_msk);
  444. spin_lock(&dev->slock);
  445. dev->_frame_index_ch2 = prog_cnt;
  446. queue_work(dev->_irq_queues_ch2, &dev->_irq_work_entry_ch2);
  447. if (dev->_is_first_frame_ch2) {
  448. dev->_is_first_frame_ch2 = 0;
  449. if (dev->_isNTSC_ch2) {
  450. singlefield_lines += 1;
  451. odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
  452. } else {
  453. singlefield_lines = PAL_FIELD_HEIGHT;
  454. odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
  455. }
  456. if (dev->_dma_virt_start_addr_ch2 != NULL) {
  457. if (dev->_pixel_format_ch2 == PIXEL_FRMT_411)
  458. line_size_in_bytes = Y411_LINE_SZ;
  459. else
  460. line_size_in_bytes = Y422_LINE_SZ;
  461. risc_phys_jump_addr =
  462. dev->_dma_phys_start_addr_ch2 +
  463. odd_risc_prog_size;
  464. rp = cx25821_update_riscprogram_ch2(dev,
  465. dev->_dma_virt_start_addr_ch2,
  466. TOP_OFFSET, line_size_in_bytes,
  467. 0x0, singlefield_lines,
  468. FIFO_DISABLE, ODD_FIELD);
  469. /* Jump to Even Risc program of 1st Frame */
  470. *(rp++) = cpu_to_le32(RISC_JUMP);
  471. *(rp++) = cpu_to_le32(risc_phys_jump_addr);
  472. *(rp++) = cpu_to_le32(0);
  473. }
  474. }
  475. spin_unlock(&dev->slock);
  476. }
  477. if (dev->_file_status_ch2 == END_OF_FILE) {
  478. pr_info("EOF Channel 2 Framecount = %d\n",
  479. dev->_frame_count_ch2);
  480. return -1;
  481. }
  482. /* ElSE, set the interrupt mask register, re-enable irq. */
  483. int_msk_tmp = cx_read(channel->int_msk);
  484. cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
  485. return 0;
  486. }
  487. static irqreturn_t cx25821_upstream_irq_ch2(int irq, void *dev_id)
  488. {
  489. struct cx25821_dev *dev = dev_id;
  490. u32 vid_status;
  491. int handled = 0;
  492. int channel_num = 0;
  493. struct sram_channel *sram_ch;
  494. if (!dev)
  495. return -1;
  496. channel_num = VID_UPSTREAM_SRAM_CHANNEL_J;
  497. sram_ch = dev->channels[channel_num].sram_channels;
  498. vid_status = cx_read(sram_ch->int_stat);
  499. /* Only deal with our interrupt */
  500. if (vid_status)
  501. handled = cx25821_video_upstream_irq_ch2(dev, channel_num,
  502. vid_status);
  503. if (handled < 0)
  504. cx25821_stop_upstream_video_ch2(dev);
  505. else
  506. handled += handled;
  507. return IRQ_RETVAL(handled);
  508. }
  509. static void cx25821_set_pixelengine_ch2(struct cx25821_dev *dev,
  510. struct sram_channel *ch, int pix_format)
  511. {
  512. int width = WIDTH_D1;
  513. int height = dev->_lines_count_ch2;
  514. int num_lines, odd_num_lines;
  515. u32 value;
  516. int vip_mode = PIXEL_ENGINE_VIP1;
  517. value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
  518. value &= 0xFFFFFFEF;
  519. value |= dev->_isNTSC_ch2 ? 0 : 0x10;
  520. cx_write(ch->vid_fmt_ctl, value);
  521. /*
  522. * set number of active pixels in each line. Default is 720
  523. * pixels in both NTSC and PAL format
  524. */
  525. cx_write(ch->vid_active_ctl1, width);
  526. num_lines = (height / 2) & 0x3FF;
  527. odd_num_lines = num_lines;
  528. if (dev->_isNTSC_ch2)
  529. odd_num_lines += 1;
  530. value = (num_lines << 16) | odd_num_lines;
  531. /* set number of active lines in field 0 (top) and field 1 (bottom) */
  532. cx_write(ch->vid_active_ctl2, value);
  533. cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
  534. }
  535. int cx25821_start_video_dma_upstream_ch2(struct cx25821_dev *dev,
  536. struct sram_channel *sram_ch)
  537. {
  538. u32 tmp = 0;
  539. int err = 0;
  540. /*
  541. * 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface
  542. * for channel A-C
  543. */
  544. tmp = cx_read(VID_CH_MODE_SEL);
  545. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  546. /*
  547. * Set the physical start address of the RISC program in the initial
  548. * program counter(IPC) member of the cmds.
  549. */
  550. cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr_ch2);
  551. cx_write(sram_ch->cmds_start + 4, 0); /* Risc IPC High 64 bits 63-32 */
  552. /* reset counter */
  553. cx_write(sram_ch->gpcnt_ctl, 3);
  554. /* Clear our bits from the interrupt status register. */
  555. cx_write(sram_ch->int_stat, _intr_msk);
  556. /* Set the interrupt mask register, enable irq. */
  557. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
  558. tmp = cx_read(sram_ch->int_msk);
  559. cx_write(sram_ch->int_msk, tmp |= _intr_msk);
  560. err = request_irq(dev->pci->irq, cx25821_upstream_irq_ch2,
  561. IRQF_SHARED, dev->name, dev);
  562. if (err < 0) {
  563. pr_err("%s: can't get upstream IRQ %d\n",
  564. dev->name, dev->pci->irq);
  565. goto fail_irq;
  566. }
  567. /* Start the DMA engine */
  568. tmp = cx_read(sram_ch->dma_ctl);
  569. cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
  570. dev->_is_running_ch2 = 1;
  571. dev->_is_first_frame_ch2 = 1;
  572. return 0;
  573. fail_irq:
  574. cx25821_dev_unregister(dev);
  575. return err;
  576. }
  577. int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev, int channel_select,
  578. int pixel_format)
  579. {
  580. struct sram_channel *sram_ch;
  581. u32 tmp;
  582. int retval = 0;
  583. int err = 0;
  584. int data_frame_size = 0;
  585. int risc_buffer_size = 0;
  586. int str_length = 0;
  587. if (dev->_is_running_ch2) {
  588. pr_info("Video Channel is still running so return!\n");
  589. return 0;
  590. }
  591. dev->_channel2_upstream_select = channel_select;
  592. sram_ch = dev->channels[channel_select].sram_channels;
  593. INIT_WORK(&dev->_irq_work_entry_ch2, cx25821_vidups_handler_ch2);
  594. dev->_irq_queues_ch2 =
  595. create_singlethread_workqueue("cx25821_workqueue2");
  596. if (!dev->_irq_queues_ch2) {
  597. pr_err("create_singlethread_workqueue() for Video FAILED!\n");
  598. return -ENOMEM;
  599. }
  600. /*
  601. * 656/VIP SRC Upstream Channel I & J and 7 -
  602. * Host Bus Interface for channel A-C
  603. */
  604. tmp = cx_read(VID_CH_MODE_SEL);
  605. cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
  606. dev->_is_running_ch2 = 0;
  607. dev->_frame_count_ch2 = 0;
  608. dev->_file_status_ch2 = RESET_STATUS;
  609. dev->_lines_count_ch2 = dev->_isNTSC_ch2 ? 480 : 576;
  610. dev->_pixel_format_ch2 = pixel_format;
  611. dev->_line_size_ch2 = (dev->_pixel_format_ch2 == PIXEL_FRMT_422) ?
  612. (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
  613. data_frame_size = dev->_isNTSC_ch2 ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
  614. risc_buffer_size = dev->_isNTSC_ch2 ?
  615. NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
  616. if (dev->input_filename_ch2) {
  617. str_length = strlen(dev->input_filename_ch2);
  618. dev->_filename_ch2 = kmemdup(dev->input_filename_ch2,
  619. str_length + 1, GFP_KERNEL);
  620. if (!dev->_filename_ch2)
  621. goto error;
  622. } else {
  623. str_length = strlen(dev->_defaultname_ch2);
  624. dev->_filename_ch2 = kmemdup(dev->_defaultname_ch2,
  625. str_length + 1, GFP_KERNEL);
  626. if (!dev->_filename_ch2)
  627. goto error;
  628. }
  629. /* Default if filename is empty string */
  630. if (strcmp(dev->_filename_ch2, "") == 0) {
  631. if (dev->_isNTSC_ch2) {
  632. dev->_filename_ch2 = (dev->_pixel_format_ch2 ==
  633. PIXEL_FRMT_411) ? "/root/vid411.yuv" :
  634. "/root/vidtest.yuv";
  635. } else {
  636. dev->_filename_ch2 = (dev->_pixel_format_ch2 ==
  637. PIXEL_FRMT_411) ? "/root/pal411.yuv" :
  638. "/root/pal422.yuv";
  639. }
  640. }
  641. retval = cx25821_sram_channel_setup_upstream(dev, sram_ch,
  642. dev->_line_size_ch2, 0);
  643. /* setup fifo + format */
  644. cx25821_set_pixelengine_ch2(dev, sram_ch, dev->_pixel_format_ch2);
  645. dev->upstream_riscbuf_size_ch2 = risc_buffer_size * 2;
  646. dev->upstream_databuf_size_ch2 = data_frame_size * 2;
  647. /* Allocating buffers and prepare RISC program */
  648. retval = cx25821_upstream_buffer_prepare_ch2(dev, sram_ch,
  649. dev->_line_size_ch2);
  650. if (retval < 0) {
  651. pr_err("%s: Failed to set up Video upstream buffers!\n",
  652. dev->name);
  653. goto error;
  654. }
  655. cx25821_start_video_dma_upstream_ch2(dev, sram_ch);
  656. return 0;
  657. error:
  658. cx25821_dev_unregister(dev);
  659. return err;
  660. }