cx25821-video-upstream-ch2.c 21 KB

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