cx25821-video-upstream-ch2.c 21 KB

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