flexcop.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
  3. * flexcop.c - main module part
  4. * Copyright (C) 2004-9 Patrick Boettcher <patrick.boettcher@posteo.de>
  5. * based on skystar2-driver Copyright (C) 2003 Vadim Catana, skystar@moldova.cc
  6. *
  7. * Acknowledgements:
  8. * John Jurrius from BBTI, Inc. for extensive support
  9. * with code examples and data books
  10. * Bjarne Steinsbo, bjarne at steinsbo.com (some ideas for rewriting)
  11. *
  12. * Contributions to the skystar2-driver have been done by
  13. * Vincenzo Di Massa, hawk.it at tiscalinet.it (several DiSEqC fixes)
  14. * Roberto Ragusa, r.ragusa at libero.it (polishing, restyling the code)
  15. * Uwe Bugla, uwe.bugla at gmx.de (doing tests, restyling code, writing docu)
  16. * Niklas Peinecke, peinecke at gdv.uni-hannover.de (hardware pid/mac
  17. * filtering)
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public License
  21. * as published by the Free Software Foundation; either version 2.1
  22. * of the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. */
  29. #include "flexcop.h"
  30. #define DRIVER_NAME "B2C2 FlexcopII/II(b)/III digital TV receiver chip"
  31. #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@posteo.de"
  32. #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
  33. #define DEBSTATUS ""
  34. #else
  35. #define DEBSTATUS " (debugging is not enabled)"
  36. #endif
  37. int b2c2_flexcop_debug;
  38. EXPORT_SYMBOL_GPL(b2c2_flexcop_debug);
  39. module_param_named(debug, b2c2_flexcop_debug, int, 0644);
  40. MODULE_PARM_DESC(debug,
  41. "set debug level (1=info,2=tuner,4=i2c,8=ts,16=sram,32=reg,64=i2cdump (|-able))."
  42. DEBSTATUS);
  43. #undef DEBSTATUS
  44. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  45. /* global zero for ibi values */
  46. flexcop_ibi_value ibi_zero;
  47. static int flexcop_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  48. {
  49. struct flexcop_device *fc = dvbdmxfeed->demux->priv;
  50. return flexcop_pid_feed_control(fc, dvbdmxfeed, 1);
  51. }
  52. static int flexcop_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  53. {
  54. struct flexcop_device *fc = dvbdmxfeed->demux->priv;
  55. return flexcop_pid_feed_control(fc, dvbdmxfeed, 0);
  56. }
  57. static int flexcop_dvb_init(struct flexcop_device *fc)
  58. {
  59. int ret = dvb_register_adapter(&fc->dvb_adapter,
  60. "FlexCop Digital TV device", fc->owner,
  61. fc->dev, adapter_nr);
  62. if (ret < 0) {
  63. err("error registering DVB adapter");
  64. return ret;
  65. }
  66. fc->dvb_adapter.priv = fc;
  67. fc->demux.dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING
  68. | DMX_MEMORY_BASED_FILTERING);
  69. fc->demux.priv = fc;
  70. fc->demux.filternum = fc->demux.feednum = FC_MAX_FEED;
  71. fc->demux.start_feed = flexcop_dvb_start_feed;
  72. fc->demux.stop_feed = flexcop_dvb_stop_feed;
  73. fc->demux.write_to_decoder = NULL;
  74. ret = dvb_dmx_init(&fc->demux);
  75. if (ret < 0) {
  76. err("dvb_dmx failed: error %d", ret);
  77. goto err_dmx;
  78. }
  79. fc->hw_frontend.source = DMX_FRONTEND_0;
  80. fc->dmxdev.filternum = fc->demux.feednum;
  81. fc->dmxdev.demux = &fc->demux.dmx;
  82. fc->dmxdev.capabilities = 0;
  83. ret = dvb_dmxdev_init(&fc->dmxdev, &fc->dvb_adapter);
  84. if (ret < 0) {
  85. err("dvb_dmxdev_init failed: error %d", ret);
  86. goto err_dmx_dev;
  87. }
  88. ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->hw_frontend);
  89. if (ret < 0) {
  90. err("adding hw_frontend to dmx failed: error %d", ret);
  91. goto err_dmx_add_hw_frontend;
  92. }
  93. fc->mem_frontend.source = DMX_MEMORY_FE;
  94. ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->mem_frontend);
  95. if (ret < 0) {
  96. err("adding mem_frontend to dmx failed: error %d", ret);
  97. goto err_dmx_add_mem_frontend;
  98. }
  99. ret = fc->demux.dmx.connect_frontend(&fc->demux.dmx, &fc->hw_frontend);
  100. if (ret < 0) {
  101. err("connect frontend failed: error %d", ret);
  102. goto err_connect_frontend;
  103. }
  104. ret = dvb_net_init(&fc->dvb_adapter, &fc->dvbnet, &fc->demux.dmx);
  105. if (ret < 0) {
  106. err("dvb_net_init failed: error %d", ret);
  107. goto err_net;
  108. }
  109. fc->init_state |= FC_STATE_DVB_INIT;
  110. return 0;
  111. err_net:
  112. fc->demux.dmx.disconnect_frontend(&fc->demux.dmx);
  113. err_connect_frontend:
  114. fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->mem_frontend);
  115. err_dmx_add_mem_frontend:
  116. fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->hw_frontend);
  117. err_dmx_add_hw_frontend:
  118. dvb_dmxdev_release(&fc->dmxdev);
  119. err_dmx_dev:
  120. dvb_dmx_release(&fc->demux);
  121. err_dmx:
  122. dvb_unregister_adapter(&fc->dvb_adapter);
  123. return ret;
  124. }
  125. static void flexcop_dvb_exit(struct flexcop_device *fc)
  126. {
  127. if (fc->init_state & FC_STATE_DVB_INIT) {
  128. dvb_net_release(&fc->dvbnet);
  129. fc->demux.dmx.close(&fc->demux.dmx);
  130. fc->demux.dmx.remove_frontend(&fc->demux.dmx,
  131. &fc->mem_frontend);
  132. fc->demux.dmx.remove_frontend(&fc->demux.dmx,
  133. &fc->hw_frontend);
  134. dvb_dmxdev_release(&fc->dmxdev);
  135. dvb_dmx_release(&fc->demux);
  136. dvb_unregister_adapter(&fc->dvb_adapter);
  137. deb_info("deinitialized dvb stuff\n");
  138. }
  139. fc->init_state &= ~FC_STATE_DVB_INIT;
  140. }
  141. /* these methods are necessary to achieve the long-term-goal of hiding the
  142. * struct flexcop_device from the bus-parts */
  143. void flexcop_pass_dmx_data(struct flexcop_device *fc, u8 *buf, u32 len)
  144. {
  145. dvb_dmx_swfilter(&fc->demux, buf, len);
  146. }
  147. EXPORT_SYMBOL(flexcop_pass_dmx_data);
  148. void flexcop_pass_dmx_packets(struct flexcop_device *fc, u8 *buf, u32 no)
  149. {
  150. dvb_dmx_swfilter_packets(&fc->demux, buf, no);
  151. }
  152. EXPORT_SYMBOL(flexcop_pass_dmx_packets);
  153. static void flexcop_reset(struct flexcop_device *fc)
  154. {
  155. flexcop_ibi_value v210, v204;
  156. /* reset the flexcop itself */
  157. fc->write_ibi_reg(fc,ctrl_208,ibi_zero);
  158. v210.raw = 0;
  159. v210.sw_reset_210.reset_block_000 = 1;
  160. v210.sw_reset_210.reset_block_100 = 1;
  161. v210.sw_reset_210.reset_block_200 = 1;
  162. v210.sw_reset_210.reset_block_300 = 1;
  163. v210.sw_reset_210.reset_block_400 = 1;
  164. v210.sw_reset_210.reset_block_500 = 1;
  165. v210.sw_reset_210.reset_block_600 = 1;
  166. v210.sw_reset_210.reset_block_700 = 1;
  167. v210.sw_reset_210.Block_reset_enable = 0xb2;
  168. v210.sw_reset_210.Special_controls = 0xc259;
  169. fc->write_ibi_reg(fc,sw_reset_210,v210);
  170. msleep(1);
  171. /* reset the periphical devices */
  172. v204 = fc->read_ibi_reg(fc,misc_204);
  173. v204.misc_204.Per_reset_sig = 0;
  174. fc->write_ibi_reg(fc,misc_204,v204);
  175. msleep(1);
  176. v204.misc_204.Per_reset_sig = 1;
  177. fc->write_ibi_reg(fc,misc_204,v204);
  178. }
  179. void flexcop_reset_block_300(struct flexcop_device *fc)
  180. {
  181. flexcop_ibi_value v208_save = fc->read_ibi_reg(fc, ctrl_208),
  182. v210 = fc->read_ibi_reg(fc, sw_reset_210);
  183. deb_rdump("208: %08x, 210: %08x\n", v208_save.raw, v210.raw);
  184. fc->write_ibi_reg(fc,ctrl_208,ibi_zero);
  185. v210.sw_reset_210.reset_block_300 = 1;
  186. v210.sw_reset_210.Block_reset_enable = 0xb2;
  187. fc->write_ibi_reg(fc,sw_reset_210,v210);
  188. fc->write_ibi_reg(fc,ctrl_208,v208_save);
  189. }
  190. struct flexcop_device *flexcop_device_kmalloc(size_t bus_specific_len)
  191. {
  192. void *bus;
  193. struct flexcop_device *fc = kzalloc(sizeof(struct flexcop_device),
  194. GFP_KERNEL);
  195. if (!fc) {
  196. err("no memory");
  197. return NULL;
  198. }
  199. bus = kzalloc(bus_specific_len, GFP_KERNEL);
  200. if (!bus) {
  201. err("no memory");
  202. kfree(fc);
  203. return NULL;
  204. }
  205. fc->bus_specific = bus;
  206. return fc;
  207. }
  208. EXPORT_SYMBOL(flexcop_device_kmalloc);
  209. void flexcop_device_kfree(struct flexcop_device *fc)
  210. {
  211. kfree(fc->bus_specific);
  212. kfree(fc);
  213. }
  214. EXPORT_SYMBOL(flexcop_device_kfree);
  215. int flexcop_device_initialize(struct flexcop_device *fc)
  216. {
  217. int ret;
  218. ibi_zero.raw = 0;
  219. flexcop_reset(fc);
  220. flexcop_determine_revision(fc);
  221. flexcop_sram_init(fc);
  222. flexcop_hw_filter_init(fc);
  223. flexcop_smc_ctrl(fc, 0);
  224. ret = flexcop_dvb_init(fc);
  225. if (ret)
  226. goto error;
  227. /* i2c has to be done before doing EEProm stuff -
  228. * because the EEProm is accessed via i2c */
  229. ret = flexcop_i2c_init(fc);
  230. if (ret)
  231. goto error;
  232. /* do the MAC address reading after initializing the dvb_adapter */
  233. if (fc->get_mac_addr(fc, 0) == 0) {
  234. u8 *b = fc->dvb_adapter.proposed_mac;
  235. info("MAC address = %pM", b);
  236. flexcop_set_mac_filter(fc,b);
  237. flexcop_mac_filter_ctrl(fc,1);
  238. } else
  239. warn("reading of MAC address failed.\n");
  240. ret = flexcop_frontend_init(fc);
  241. if (ret)
  242. goto error;
  243. flexcop_device_name(fc,"initialization of","complete");
  244. return 0;
  245. error:
  246. flexcop_device_exit(fc);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL(flexcop_device_initialize);
  250. void flexcop_device_exit(struct flexcop_device *fc)
  251. {
  252. flexcop_frontend_exit(fc);
  253. flexcop_i2c_exit(fc);
  254. flexcop_dvb_exit(fc);
  255. }
  256. EXPORT_SYMBOL(flexcop_device_exit);
  257. static int flexcop_module_init(void)
  258. {
  259. info(DRIVER_NAME " loaded successfully");
  260. return 0;
  261. }
  262. static void flexcop_module_cleanup(void)
  263. {
  264. info(DRIVER_NAME " unloaded successfully");
  265. }
  266. module_init(flexcop_module_init);
  267. module_exit(flexcop_module_cleanup);
  268. MODULE_AUTHOR(DRIVER_AUTHOR);
  269. MODULE_DESCRIPTION(DRIVER_NAME);
  270. MODULE_LICENSE("GPL");