cx25821-video-upstream-ch2.c 21 KB

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