PageRenderTime 61ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/media/dvb/b2c2/flexcop-pci.c

https://github.com/TeamHeroC/htc-kernel-2.6.35
C | 452 lines | 351 code | 77 blank | 24 comment | 58 complexity | 5cc0cf799fea0be03fdc51de1e1e3e4c MD5 | raw file
  1. /*
  2. * Linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
  3. * flexcop-pci.c - covers the PCI part including DMA transfers
  4. * see flexcop.c for copyright information
  5. */
  6. #define FC_LOG_PREFIX "flexcop-pci"
  7. #include "flexcop-common.h"
  8. static int enable_pid_filtering = 1;
  9. module_param(enable_pid_filtering, int, 0444);
  10. MODULE_PARM_DESC(enable_pid_filtering,
  11. "enable hardware pid filtering: supported values: 0 (fullts), 1");
  12. static int irq_chk_intv = 100;
  13. module_param(irq_chk_intv, int, 0644);
  14. MODULE_PARM_DESC(irq_chk_intv, "set the interval for IRQ streaming watchdog.");
  15. #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
  16. #define dprintk(level,args...) \
  17. do { if ((debug & level)) printk(args); } while (0)
  18. #define DEBSTATUS ""
  19. #else
  20. #define dprintk(level,args...)
  21. #define DEBSTATUS " (debugging is not enabled)"
  22. #endif
  23. #define deb_info(args...) dprintk(0x01, args)
  24. #define deb_reg(args...) dprintk(0x02, args)
  25. #define deb_ts(args...) dprintk(0x04, args)
  26. #define deb_irq(args...) dprintk(0x08, args)
  27. #define deb_chk(args...) dprintk(0x10, args)
  28. static int debug;
  29. module_param(debug, int, 0644);
  30. MODULE_PARM_DESC(debug,
  31. "set debug level (1=info,2=regs,4=TS,8=irqdma,16=check (|-able))."
  32. DEBSTATUS);
  33. #define DRIVER_VERSION "0.1"
  34. #define DRIVER_NAME "flexcop-pci"
  35. #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de>"
  36. struct flexcop_pci {
  37. struct pci_dev *pdev;
  38. #define FC_PCI_INIT 0x01
  39. #define FC_PCI_DMA_INIT 0x02
  40. int init_state;
  41. void __iomem *io_mem;
  42. u32 irq;
  43. /* buffersize (at least for DMA1, need to be % 188 == 0,
  44. * this logic is required */
  45. #define FC_DEFAULT_DMA1_BUFSIZE (1280 * 188)
  46. #define FC_DEFAULT_DMA2_BUFSIZE (10 * 188)
  47. struct flexcop_dma dma[2];
  48. int active_dma1_addr; /* 0 = addr0 of dma1; 1 = addr1 of dma1 */
  49. u32 last_dma1_cur_pos;
  50. /* position of the pointer last time the timer/packet irq occured */
  51. int count;
  52. int count_prev;
  53. int stream_problem;
  54. spinlock_t irq_lock;
  55. unsigned long last_irq;
  56. struct delayed_work irq_check_work;
  57. struct flexcop_device *fc_dev;
  58. };
  59. static int lastwreg, lastwval, lastrreg, lastrval;
  60. static flexcop_ibi_value flexcop_pci_read_ibi_reg(struct flexcop_device *fc,
  61. flexcop_ibi_register r)
  62. {
  63. struct flexcop_pci *fc_pci = fc->bus_specific;
  64. flexcop_ibi_value v;
  65. v.raw = readl(fc_pci->io_mem + r);
  66. if (lastrreg != r || lastrval != v.raw) {
  67. lastrreg = r; lastrval = v.raw;
  68. deb_reg("new rd: %3x: %08x\n", r, v.raw);
  69. }
  70. return v;
  71. }
  72. static int flexcop_pci_write_ibi_reg(struct flexcop_device *fc,
  73. flexcop_ibi_register r, flexcop_ibi_value v)
  74. {
  75. struct flexcop_pci *fc_pci = fc->bus_specific;
  76. if (lastwreg != r || lastwval != v.raw) {
  77. lastwreg = r; lastwval = v.raw;
  78. deb_reg("new wr: %3x: %08x\n", r, v.raw);
  79. }
  80. writel(v.raw, fc_pci->io_mem + r);
  81. return 0;
  82. }
  83. static void flexcop_pci_irq_check_work(struct work_struct *work)
  84. {
  85. struct flexcop_pci *fc_pci =
  86. container_of(work, struct flexcop_pci, irq_check_work.work);
  87. struct flexcop_device *fc = fc_pci->fc_dev;
  88. if (fc->feedcount) {
  89. if (fc_pci->count == fc_pci->count_prev) {
  90. deb_chk("no IRQ since the last check\n");
  91. if (fc_pci->stream_problem++ == 3) {
  92. struct dvb_demux_feed *feed;
  93. deb_info("flexcop-pci: stream problem, resetting pid filter\n");
  94. spin_lock_irq(&fc->demux.lock);
  95. list_for_each_entry(feed, &fc->demux.feed_list,
  96. list_head) {
  97. flexcop_pid_feed_control(fc, feed, 0);
  98. }
  99. list_for_each_entry(feed, &fc->demux.feed_list,
  100. list_head) {
  101. flexcop_pid_feed_control(fc, feed, 1);
  102. }
  103. spin_unlock_irq(&fc->demux.lock);
  104. fc_pci->stream_problem = 0;
  105. }
  106. } else {
  107. fc_pci->stream_problem = 0;
  108. fc_pci->count_prev = fc_pci->count;
  109. }
  110. }
  111. schedule_delayed_work(&fc_pci->irq_check_work,
  112. msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
  113. }
  114. /* When PID filtering is turned on, we use the timer IRQ, because small amounts
  115. * of data need to be passed to the user space instantly as well. When PID
  116. * filtering is turned off, we use the page-change-IRQ */
  117. static irqreturn_t flexcop_pci_isr(int irq, void *dev_id)
  118. {
  119. struct flexcop_pci *fc_pci = dev_id;
  120. struct flexcop_device *fc = fc_pci->fc_dev;
  121. unsigned long flags;
  122. flexcop_ibi_value v;
  123. irqreturn_t ret = IRQ_HANDLED;
  124. spin_lock_irqsave(&fc_pci->irq_lock, flags);
  125. v = fc->read_ibi_reg(fc, irq_20c);
  126. /* errors */
  127. if (v.irq_20c.Data_receiver_error)
  128. deb_chk("data receiver error\n");
  129. if (v.irq_20c.Continuity_error_flag)
  130. deb_chk("Contunuity error flag is set\n");
  131. if (v.irq_20c.LLC_SNAP_FLAG_set)
  132. deb_chk("LLC_SNAP_FLAG_set is set\n");
  133. if (v.irq_20c.Transport_Error)
  134. deb_chk("Transport error\n");
  135. if ((fc_pci->count % 1000) == 0)
  136. deb_chk("%d valid irq took place so far\n", fc_pci->count);
  137. if (v.irq_20c.DMA1_IRQ_Status == 1) {
  138. if (fc_pci->active_dma1_addr == 0)
  139. flexcop_pass_dmx_packets(fc_pci->fc_dev,
  140. fc_pci->dma[0].cpu_addr0,
  141. fc_pci->dma[0].size / 188);
  142. else
  143. flexcop_pass_dmx_packets(fc_pci->fc_dev,
  144. fc_pci->dma[0].cpu_addr1,
  145. fc_pci->dma[0].size / 188);
  146. deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
  147. fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
  148. /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
  149. * complete TS packets when reading from the DMA memory */
  150. } else if (v.irq_20c.DMA1_Timer_Status == 1) {
  151. dma_addr_t cur_addr =
  152. fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
  153. u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
  154. deb_irq("%u irq: %08x cur_addr: %llx: cur_pos: %08x, "
  155. "last_cur_pos: %08x ",
  156. jiffies_to_usecs(jiffies - fc_pci->last_irq),
  157. v.raw, (unsigned long long)cur_addr, cur_pos,
  158. fc_pci->last_dma1_cur_pos);
  159. fc_pci->last_irq = jiffies;
  160. /* buffer end was reached, restarted from the beginning
  161. * pass the data from last_cur_pos to the buffer end to the demux
  162. */
  163. if (cur_pos < fc_pci->last_dma1_cur_pos) {
  164. deb_irq(" end was reached: passing %d bytes ",
  165. (fc_pci->dma[0].size*2 - 1) -
  166. fc_pci->last_dma1_cur_pos);
  167. flexcop_pass_dmx_data(fc_pci->fc_dev,
  168. fc_pci->dma[0].cpu_addr0 +
  169. fc_pci->last_dma1_cur_pos,
  170. (fc_pci->dma[0].size*2) -
  171. fc_pci->last_dma1_cur_pos);
  172. fc_pci->last_dma1_cur_pos = 0;
  173. }
  174. if (cur_pos > fc_pci->last_dma1_cur_pos) {
  175. deb_irq(" passing %d bytes ",
  176. cur_pos - fc_pci->last_dma1_cur_pos);
  177. flexcop_pass_dmx_data(fc_pci->fc_dev,
  178. fc_pci->dma[0].cpu_addr0 +
  179. fc_pci->last_dma1_cur_pos,
  180. cur_pos - fc_pci->last_dma1_cur_pos);
  181. }
  182. deb_irq("\n");
  183. fc_pci->last_dma1_cur_pos = cur_pos;
  184. fc_pci->count++;
  185. } else {
  186. deb_irq("isr for flexcop called, "
  187. "apparently without reason (%08x)\n", v.raw);
  188. ret = IRQ_NONE;
  189. }
  190. spin_unlock_irqrestore(&fc_pci->irq_lock, flags);
  191. return ret;
  192. }
  193. static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
  194. {
  195. struct flexcop_pci *fc_pci = fc->bus_specific;
  196. if (onoff) {
  197. flexcop_dma_config(fc, &fc_pci->dma[0], FC_DMA_1);
  198. flexcop_dma_config(fc, &fc_pci->dma[1], FC_DMA_2);
  199. flexcop_dma_config_timer(fc, FC_DMA_1, 0);
  200. flexcop_dma_xfer_control(fc, FC_DMA_1,
  201. FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 1);
  202. deb_irq("DMA xfer enabled\n");
  203. fc_pci->last_dma1_cur_pos = 0;
  204. flexcop_dma_control_timer_irq(fc, FC_DMA_1, 1);
  205. deb_irq("IRQ enabled\n");
  206. fc_pci->count_prev = fc_pci->count;
  207. } else {
  208. flexcop_dma_control_timer_irq(fc, FC_DMA_1, 0);
  209. deb_irq("IRQ disabled\n");
  210. flexcop_dma_xfer_control(fc, FC_DMA_1,
  211. FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 0);
  212. deb_irq("DMA xfer disabled\n");
  213. }
  214. return 0;
  215. }
  216. static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
  217. {
  218. int ret;
  219. ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[0],
  220. FC_DEFAULT_DMA1_BUFSIZE);
  221. if (ret != 0)
  222. return ret;
  223. ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[1],
  224. FC_DEFAULT_DMA2_BUFSIZE);
  225. if (ret != 0) {
  226. flexcop_dma_free(&fc_pci->dma[0]);
  227. return ret;
  228. }
  229. flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_MEDIA |
  230. FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
  231. flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_CAO |
  232. FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
  233. fc_pci->init_state |= FC_PCI_DMA_INIT;
  234. return ret;
  235. }
  236. static void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
  237. {
  238. if (fc_pci->init_state & FC_PCI_DMA_INIT) {
  239. flexcop_dma_free(&fc_pci->dma[0]);
  240. flexcop_dma_free(&fc_pci->dma[1]);
  241. }
  242. fc_pci->init_state &= ~FC_PCI_DMA_INIT;
  243. }
  244. static int flexcop_pci_init(struct flexcop_pci *fc_pci)
  245. {
  246. int ret;
  247. u8 card_rev;
  248. pci_read_config_byte(fc_pci->pdev, PCI_CLASS_REVISION, &card_rev);
  249. info("card revision %x", card_rev);
  250. if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
  251. return ret;
  252. pci_set_master(fc_pci->pdev);
  253. if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
  254. goto err_pci_disable_device;
  255. fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
  256. if (!fc_pci->io_mem) {
  257. err("cannot map io memory\n");
  258. ret = -EIO;
  259. goto err_pci_release_regions;
  260. }
  261. pci_set_drvdata(fc_pci->pdev, fc_pci);
  262. spin_lock_init(&fc_pci->irq_lock);
  263. if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_isr,
  264. IRQF_SHARED, DRIVER_NAME, fc_pci)) != 0)
  265. goto err_pci_iounmap;
  266. fc_pci->init_state |= FC_PCI_INIT;
  267. return ret;
  268. err_pci_iounmap:
  269. pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
  270. pci_set_drvdata(fc_pci->pdev, NULL);
  271. err_pci_release_regions:
  272. pci_release_regions(fc_pci->pdev);
  273. err_pci_disable_device:
  274. pci_disable_device(fc_pci->pdev);
  275. return ret;
  276. }
  277. static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
  278. {
  279. if (fc_pci->init_state & FC_PCI_INIT) {
  280. free_irq(fc_pci->pdev->irq, fc_pci);
  281. pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
  282. pci_set_drvdata(fc_pci->pdev, NULL);
  283. pci_release_regions(fc_pci->pdev);
  284. pci_disable_device(fc_pci->pdev);
  285. }
  286. fc_pci->init_state &= ~FC_PCI_INIT;
  287. }
  288. static int flexcop_pci_probe(struct pci_dev *pdev,
  289. const struct pci_device_id *ent)
  290. {
  291. struct flexcop_device *fc;
  292. struct flexcop_pci *fc_pci;
  293. int ret = -ENOMEM;
  294. if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
  295. err("out of memory\n");
  296. return -ENOMEM;
  297. }
  298. /* general flexcop init */
  299. fc_pci = fc->bus_specific;
  300. fc_pci->fc_dev = fc;
  301. fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
  302. fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
  303. fc->i2c_request = flexcop_i2c_request;
  304. fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
  305. fc->stream_control = flexcop_pci_stream_control;
  306. if (enable_pid_filtering)
  307. info("will use the HW PID filter.");
  308. else
  309. info("will pass the complete TS to the demuxer.");
  310. fc->pid_filtering = enable_pid_filtering;
  311. fc->bus_type = FC_PCI;
  312. fc->dev = &pdev->dev;
  313. fc->owner = THIS_MODULE;
  314. /* bus specific part */
  315. fc_pci->pdev = pdev;
  316. if ((ret = flexcop_pci_init(fc_pci)) != 0)
  317. goto err_kfree;
  318. /* init flexcop */
  319. if ((ret = flexcop_device_initialize(fc)) != 0)
  320. goto err_pci_exit;
  321. /* init dma */
  322. if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
  323. goto err_fc_exit;
  324. INIT_DELAYED_WORK(&fc_pci->irq_check_work, flexcop_pci_irq_check_work);
  325. if (irq_chk_intv > 0)
  326. schedule_delayed_work(&fc_pci->irq_check_work,
  327. msecs_to_jiffies(irq_chk_intv < 100 ?
  328. 100 :
  329. irq_chk_intv));
  330. return ret;
  331. err_fc_exit:
  332. flexcop_device_exit(fc);
  333. err_pci_exit:
  334. flexcop_pci_exit(fc_pci);
  335. err_kfree:
  336. flexcop_device_kfree(fc);
  337. return ret;
  338. }
  339. /* in theory every _exit function should be called exactly two times,
  340. * here and in the bail-out-part of the _init-function
  341. */
  342. static void flexcop_pci_remove(struct pci_dev *pdev)
  343. {
  344. struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
  345. if (irq_chk_intv > 0)
  346. cancel_delayed_work(&fc_pci->irq_check_work);
  347. flexcop_pci_dma_exit(fc_pci);
  348. flexcop_device_exit(fc_pci->fc_dev);
  349. flexcop_pci_exit(fc_pci);
  350. flexcop_device_kfree(fc_pci->fc_dev);
  351. }
  352. static struct pci_device_id flexcop_pci_tbl[] = {
  353. { PCI_DEVICE(0x13d0, 0x2103) },
  354. { },
  355. };
  356. MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
  357. static struct pci_driver flexcop_pci_driver = {
  358. .name = "b2c2_flexcop_pci",
  359. .id_table = flexcop_pci_tbl,
  360. .probe = flexcop_pci_probe,
  361. .remove = flexcop_pci_remove,
  362. };
  363. static int __init flexcop_pci_module_init(void)
  364. {
  365. return pci_register_driver(&flexcop_pci_driver);
  366. }
  367. static void __exit flexcop_pci_module_exit(void)
  368. {
  369. pci_unregister_driver(&flexcop_pci_driver);
  370. }
  371. module_init(flexcop_pci_module_init);
  372. module_exit(flexcop_pci_module_exit);
  373. MODULE_AUTHOR(DRIVER_AUTHOR);
  374. MODULE_DESCRIPTION(DRIVER_NAME);
  375. MODULE_LICENSE("GPL");