cx25821-video-upstream-ch2.c 21 KB

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