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