flexcop.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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@desy.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. * You should have received a copy of the GNU Lesser General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  32. */
  33. #include "flexcop.h"
  34. #define DRIVER_NAME "B2C2 FlexcopII/II(b)/III digital TV receiver chip"
  35. #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de"
  36. #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
  37. #define DEBSTATUS ""
  38. #else
  39. #define DEBSTATUS " (debugging is not enabled)"
  40. #endif
  41. int b2c2_flexcop_debug;
  42. EXPORT_SYMBOL_GPL(b2c2_flexcop_debug);
  43. module_param_named(debug, b2c2_flexcop_debug, int, 0644);
  44. MODULE_PARM_DESC(debug,
  45. "set debug level (1=info,2=tuner,4=i2c,8=ts,"
  46. "16=sram,32=reg (|-able))."
  47. DEBSTATUS);
  48. #undef DEBSTATUS
  49. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  50. /* global zero for ibi values */
  51. flexcop_ibi_value ibi_zero;
  52. static int flexcop_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  53. {
  54. struct flexcop_device *fc = dvbdmxfeed->demux->priv;
  55. return flexcop_pid_feed_control(fc, dvbdmxfeed, 1);
  56. }
  57. static int flexcop_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  58. {
  59. struct flexcop_device *fc = dvbdmxfeed->demux->priv;
  60. return flexcop_pid_feed_control(fc, dvbdmxfeed, 0);
  61. }
  62. static int flexcop_dvb_init(struct flexcop_device *fc)
  63. {
  64. int ret = dvb_register_adapter(&fc->dvb_adapter,
  65. "FlexCop Digital TV device", fc->owner,
  66. fc->dev, adapter_nr);
  67. if (ret < 0) {
  68. err("error registering DVB adapter");
  69. return ret;
  70. }
  71. fc->dvb_adapter.priv = fc;
  72. fc->demux.dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING
  73. | DMX_MEMORY_BASED_FILTERING);
  74. fc->demux.priv = fc;
  75. fc->demux.filternum = fc->demux.feednum = FC_MAX_FEED;
  76. fc->demux.start_feed = flexcop_dvb_start_feed;
  77. fc->demux.stop_feed = flexcop_dvb_stop_feed;
  78. fc->demux.write_to_decoder = NULL;
  79. ret = dvb_dmx_init(&fc->demux);
  80. if (ret < 0) {
  81. err("dvb_dmx failed: error %d", ret);
  82. goto err_dmx;
  83. }
  84. fc->hw_frontend.source = DMX_FRONTEND_0;
  85. fc->dmxdev.filternum = fc->demux.feednum;
  86. fc->dmxdev.demux = &fc->demux.dmx;
  87. fc->dmxdev.capabilities = 0;
  88. ret = dvb_dmxdev_init(&fc->dmxdev, &fc->dvb_adapter);
  89. if (ret < 0) {
  90. err("dvb_dmxdev_init failed: error %d", ret);
  91. goto err_dmx_dev;
  92. }
  93. ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->hw_frontend);
  94. if (ret < 0) {
  95. err("adding hw_frontend to dmx failed: error %d", ret);
  96. goto err_dmx_add_hw_frontend;
  97. }
  98. fc->mem_frontend.source = DMX_MEMORY_FE;
  99. ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->mem_frontend);
  100. if (ret < 0) {
  101. err("adding mem_frontend to dmx failed: error %d", ret);
  102. goto err_dmx_add_mem_frontend;
  103. }
  104. ret = fc->demux.dmx.connect_frontend(&fc->demux.dmx, &fc->hw_frontend);
  105. if (ret < 0) {
  106. err("connect frontend failed: error %d", ret);
  107. goto err_connect_frontend;
  108. }
  109. ret = dvb_net_init(&fc->dvb_adapter, &fc->dvbnet, &fc->demux.dmx);
  110. if (ret < 0) {
  111. err("dvb_net_init failed: error %d", ret);
  112. goto err_net;
  113. }
  114. fc->init_state |= FC_STATE_DVB_INIT;
  115. return 0;
  116. err_net:
  117. fc->demux.dmx.disconnect_frontend(&fc->demux.dmx);
  118. err_connect_frontend:
  119. fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->mem_frontend);
  120. err_dmx_add_mem_frontend:
  121. fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->hw_frontend);
  122. err_dmx_add_hw_frontend:
  123. dvb_dmxdev_release(&fc->dmxdev);
  124. err_dmx_dev:
  125. dvb_dmx_release(&fc->demux);
  126. err_dmx:
  127. dvb_unregister_adapter(&fc->dvb_adapter);
  128. return ret;
  129. }
  130. static void flexcop_dvb_exit(struct flexcop_device *fc)
  131. {
  132. if (fc->init_state & FC_STATE_DVB_INIT) {
  133. dvb_net_release(&fc->dvbnet);
  134. fc->demux.dmx.close(&fc->demux.dmx);
  135. fc->demux.dmx.remove_frontend(&fc->demux.dmx,
  136. &fc->mem_frontend);
  137. fc->demux.dmx.remove_frontend(&fc->demux.dmx,
  138. &fc->hw_frontend);
  139. dvb_dmxdev_release(&fc->dmxdev);
  140. dvb_dmx_release(&fc->demux);
  141. dvb_unregister_adapter(&fc->dvb_adapter);
  142. deb_info("deinitialized dvb stuff\n");
  143. }
  144. fc->init_state &= ~FC_STATE_DVB_INIT;
  145. }
  146. /* these methods are necessary to achieve the long-term-goal of hiding the
  147. * struct flexcop_device from the bus-parts */
  148. void flexcop_pass_dmx_data(struct flexcop_device *fc, u8 *buf, u32 len)
  149. {
  150. dvb_dmx_swfilter(&fc->demux, buf, len);
  151. }
  152. EXPORT_SYMBOL(flexcop_pass_dmx_data);
  153. void flexcop_pass_dmx_packets(struct flexcop_device *fc, u8 *buf, u32 no)
  154. {
  155. dvb_dmx_swfilter_packets(&fc->demux, buf, no);
  156. }
  157. EXPORT_SYMBOL(flexcop_pass_dmx_packets);
  158. static void flexcop_reset(struct flexcop_device *fc)
  159. {
  160. flexcop_ibi_value v210, v204;
  161. /* reset the flexcop itself */
  162. fc->write_ibi_reg(fc,ctrl_208,ibi_zero);
  163. v210.raw = 0;
  164. v210.sw_reset_210.reset_block_000 = 1;
  165. v210.sw_reset_210.reset_block_100 = 1;
  166. v210.sw_reset_210.reset_block_200 = 1;
  167. v210.sw_reset_210.reset_block_300 = 1;
  168. v210.sw_reset_210.reset_block_400 = 1;
  169. v210.sw_reset_210.reset_block_500 = 1;
  170. v210.sw_reset_210.reset_block_600 = 1;
  171. v210.sw_reset_210.reset_block_700 = 1;
  172. v210.sw_reset_210.Block_reset_enable = 0xb2;
  173. v210.sw_reset_210.Special_controls = 0xc259;
  174. fc->write_ibi_reg(fc,sw_reset_210,v210);
  175. msleep(1);
  176. /* reset the periphical devices */
  177. v204 = fc->read_ibi_reg(fc,misc_204);
  178. v204.misc_204.Per_reset_sig = 0;
  179. fc->write_ibi_reg(fc,misc_204,v204);
  180. msleep(1);
  181. v204.misc_204.Per_reset_sig = 1;
  182. fc->write_ibi_reg(fc,misc_204,v204);
  183. }
  184. void flexcop_reset_block_300(struct flexcop_device *fc)
  185. {
  186. flexcop_ibi_value v208_save = fc->read_ibi_reg(fc, ctrl_208),
  187. v210 = fc->read_ibi_reg(fc, sw_reset_210);
  188. deb_rdump("208: %08x, 210: %08x\n", v208_save.raw, v210.raw);
  189. fc->write_ibi_reg(fc,ctrl_208,ibi_zero);
  190. v210.sw_reset_210.reset_block_300 = 1;
  191. v210.sw_reset_210.Block_reset_enable = 0xb2;
  192. fc->write_ibi_reg(fc,sw_reset_210,v210);
  193. fc->write_ibi_reg(fc,ctrl_208,v208_save);
  194. }
  195. struct flexcop_device *flexcop_device_kmalloc(size_t bus_specific_len)
  196. {
  197. void *bus;
  198. struct flexcop_device *fc = kzalloc(sizeof(struct flexcop_device),
  199. GFP_KERNEL);
  200. if (!fc) {
  201. err("no memory");
  202. return NULL;
  203. }
  204. bus = kzalloc(bus_specific_len, GFP_KERNEL);
  205. if (!bus) {
  206. err("no memory");
  207. kfree(fc);
  208. return NULL;
  209. }
  210. fc->bus_specific = bus;
  211. return fc;
  212. }
  213. EXPORT_SYMBOL(flexcop_device_kmalloc);
  214. void flexcop_device_kfree(struct flexcop_device *fc)
  215. {
  216. kfree(fc->bus_specific);
  217. kfree(fc);
  218. }
  219. EXPORT_SYMBOL(flexcop_device_kfree);
  220. int flexcop_device_initialize(struct flexcop_device *fc)
  221. {
  222. int ret;
  223. ibi_zero.raw = 0;
  224. flexcop_reset(fc);
  225. flexcop_determine_revision(fc);
  226. flexcop_sram_init(fc);
  227. flexcop_hw_filter_init(fc);
  228. flexcop_smc_ctrl(fc, 0);
  229. ret = flexcop_dvb_init(fc);
  230. if (ret)
  231. goto error;
  232. /* i2c has to be done before doing EEProm stuff -
  233. * because the EEProm is accessed via i2c */
  234. ret = flexcop_i2c_init(fc);
  235. if (ret)
  236. goto error;
  237. /* do the MAC address reading after initializing the dvb_adapter */
  238. if (fc->get_mac_addr(fc, 0) == 0) {
  239. u8 *b = fc->dvb_adapter.proposed_mac;
  240. info("MAC address = %pM", b);
  241. flexcop_set_mac_filter(fc,b);
  242. flexcop_mac_filter_ctrl(fc,1);
  243. } else
  244. warn("reading of MAC address failed.\n");
  245. ret = flexcop_frontend_init(fc);
  246. if (ret)
  247. goto error;
  248. flexcop_device_name(fc,"initialization of","complete");
  249. return 0;
  250. error:
  251. flexcop_device_exit(fc);
  252. return ret;
  253. }
  254. EXPORT_SYMBOL(flexcop_device_initialize);
  255. void flexcop_device_exit(struct flexcop_device *fc)
  256. {
  257. flexcop_frontend_exit(fc);
  258. flexcop_i2c_exit(fc);
  259. flexcop_dvb_exit(fc);
  260. }
  261. EXPORT_SYMBOL(flexcop_device_exit);
  262. static int flexcop_module_init(void)
  263. {
  264. info(DRIVER_NAME " loaded successfully");
  265. return 0;
  266. }
  267. static void flexcop_module_cleanup(void)
  268. {
  269. info(DRIVER_NAME " unloaded successfully");
  270. }
  271. module_init(flexcop_module_init);
  272. module_exit(flexcop_module_cleanup);
  273. MODULE_AUTHOR(DRIVER_AUTHOR);
  274. MODULE_DESCRIPTION(DRIVER_NAME);
  275. MODULE_LICENSE("GPL");