blob: 412c5daf2b48c4208f2fb0bd60ed50fb2f4acbef [file] [log] [blame]
Johannes Stezenbach2add87a2005-05-16 21:54:10 -07001/*
Uwe Bugla1589a992009-03-29 07:46:58 -03002 * 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
Johannes Stezenbach2add87a2005-05-16 21:54:10 -07006 *
7 * Acknowledgements:
Uwe Bugla1589a992009-03-29 07:46:58 -03008 * 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)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070011 *
12 * Contributions to the skystar2-driver have been done by
Uwe Bugla1589a992009-03-29 07:46:58 -030013 * 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)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070018 *
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
34#include "flexcop.h"
35
36#define DRIVER_NAME "B2C2 FlexcopII/II(b)/III digital TV receiver chip"
37#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de"
38
39#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
40#define DEBSTATUS ""
41#else
42#define DEBSTATUS " (debugging is not enabled)"
43#endif
44
45int b2c2_flexcop_debug;
Mauro Carvalho Chehab9b78c5a2012-08-17 11:09:19 -030046EXPORT_SYMBOL_GPL(b2c2_flexcop_debug);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070047module_param_named(debug, b2c2_flexcop_debug, int, 0644);
Uwe Bugla1589a992009-03-29 07:46:58 -030048MODULE_PARM_DESC(debug,
49 "set debug level (1=info,2=tuner,4=i2c,8=ts,"
50 "16=sram,32=reg (|-able))."
51 DEBSTATUS);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070052#undef DEBSTATUS
53
Janne Grunau78e92002008-04-09 19:13:13 -030054DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
55
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070056/* global zero for ibi values */
57flexcop_ibi_value ibi_zero;
58
59static int flexcop_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
60{
61 struct flexcop_device *fc = dvbdmxfeed->demux->priv;
Uwe Bugla1589a992009-03-29 07:46:58 -030062 return flexcop_pid_feed_control(fc, dvbdmxfeed, 1);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070063}
64
65static int flexcop_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
66{
67 struct flexcop_device *fc = dvbdmxfeed->demux->priv;
Uwe Bugla1589a992009-03-29 07:46:58 -030068 return flexcop_pid_feed_control(fc, dvbdmxfeed, 0);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070069}
70
71static int flexcop_dvb_init(struct flexcop_device *fc)
72{
Janne Grunau78e92002008-04-09 19:13:13 -030073 int ret = dvb_register_adapter(&fc->dvb_adapter,
Uwe Bugla1589a992009-03-29 07:46:58 -030074 "FlexCop Digital TV device", fc->owner,
75 fc->dev, adapter_nr);
Janne Grunau78e92002008-04-09 19:13:13 -030076 if (ret < 0) {
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070077 err("error registering DVB adapter");
78 return ret;
79 }
80 fc->dvb_adapter.priv = fc;
81
Uwe Bugla1589a992009-03-29 07:46:58 -030082 fc->demux.dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING
83 | DMX_MEMORY_BASED_FILTERING);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070084 fc->demux.priv = fc;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070085 fc->demux.filternum = fc->demux.feednum = FC_MAX_FEED;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070086 fc->demux.start_feed = flexcop_dvb_start_feed;
87 fc->demux.stop_feed = flexcop_dvb_stop_feed;
88 fc->demux.write_to_decoder = NULL;
89
Jonathan Niederefd279e2012-01-06 12:57:56 -030090 ret = dvb_dmx_init(&fc->demux);
91 if (ret < 0) {
Uwe Bugla1589a992009-03-29 07:46:58 -030092 err("dvb_dmx failed: error %d", ret);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070093 goto err_dmx;
94 }
95
96 fc->hw_frontend.source = DMX_FRONTEND_0;
97
98 fc->dmxdev.filternum = fc->demux.feednum;
99 fc->dmxdev.demux = &fc->demux.dmx;
100 fc->dmxdev.capabilities = 0;
Jonathan Niederefd279e2012-01-06 12:57:56 -0300101 ret = dvb_dmxdev_init(&fc->dmxdev, &fc->dvb_adapter);
102 if (ret < 0) {
Uwe Bugla1589a992009-03-29 07:46:58 -0300103 err("dvb_dmxdev_init failed: error %d", ret);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700104 goto err_dmx_dev;
105 }
106
Jonathan Niederefd279e2012-01-06 12:57:56 -0300107 ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->hw_frontend);
108 if (ret < 0) {
Uwe Bugla1589a992009-03-29 07:46:58 -0300109 err("adding hw_frontend to dmx failed: error %d", ret);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700110 goto err_dmx_add_hw_frontend;
111 }
112
113 fc->mem_frontend.source = DMX_MEMORY_FE;
Jonathan Niederefd279e2012-01-06 12:57:56 -0300114 ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->mem_frontend);
115 if (ret < 0) {
Uwe Bugla1589a992009-03-29 07:46:58 -0300116 err("adding mem_frontend to dmx failed: error %d", ret);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700117 goto err_dmx_add_mem_frontend;
118 }
119
Jonathan Niederefd279e2012-01-06 12:57:56 -0300120 ret = fc->demux.dmx.connect_frontend(&fc->demux.dmx, &fc->hw_frontend);
121 if (ret < 0) {
Uwe Bugla1589a992009-03-29 07:46:58 -0300122 err("connect frontend failed: error %d", ret);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700123 goto err_connect_frontend;
124 }
125
Jonathan Nieder01732eb2011-12-31 08:04:25 -0300126 ret = dvb_net_init(&fc->dvb_adapter, &fc->dvbnet, &fc->demux.dmx);
127 if (ret < 0) {
128 err("dvb_net_init failed: error %d", ret);
129 goto err_net;
130 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700131
132 fc->init_state |= FC_STATE_DVB_INIT;
Trent Piepho83977032006-05-12 20:36:24 -0300133 return 0;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700134
Jonathan Nieder01732eb2011-12-31 08:04:25 -0300135err_net:
136 fc->demux.dmx.disconnect_frontend(&fc->demux.dmx);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700137err_connect_frontend:
Uwe Bugla1589a992009-03-29 07:46:58 -0300138 fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->mem_frontend);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700139err_dmx_add_mem_frontend:
Uwe Bugla1589a992009-03-29 07:46:58 -0300140 fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->hw_frontend);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700141err_dmx_add_hw_frontend:
142 dvb_dmxdev_release(&fc->dmxdev);
143err_dmx_dev:
144 dvb_dmx_release(&fc->demux);
145err_dmx:
146 dvb_unregister_adapter(&fc->dvb_adapter);
147 return ret;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700148}
149
150static void flexcop_dvb_exit(struct flexcop_device *fc)
151{
152 if (fc->init_state & FC_STATE_DVB_INIT) {
153 dvb_net_release(&fc->dvbnet);
154
155 fc->demux.dmx.close(&fc->demux.dmx);
Uwe Bugla1589a992009-03-29 07:46:58 -0300156 fc->demux.dmx.remove_frontend(&fc->demux.dmx,
157 &fc->mem_frontend);
158 fc->demux.dmx.remove_frontend(&fc->demux.dmx,
159 &fc->hw_frontend);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700160 dvb_dmxdev_release(&fc->dmxdev);
161 dvb_dmx_release(&fc->demux);
162 dvb_unregister_adapter(&fc->dvb_adapter);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700163 deb_info("deinitialized dvb stuff\n");
164 }
165 fc->init_state &= ~FC_STATE_DVB_INIT;
166}
167
168/* these methods are necessary to achieve the long-term-goal of hiding the
169 * struct flexcop_device from the bus-parts */
170void flexcop_pass_dmx_data(struct flexcop_device *fc, u8 *buf, u32 len)
171{
172 dvb_dmx_swfilter(&fc->demux, buf, len);
173}
174EXPORT_SYMBOL(flexcop_pass_dmx_data);
175
176void flexcop_pass_dmx_packets(struct flexcop_device *fc, u8 *buf, u32 no)
177{
178 dvb_dmx_swfilter_packets(&fc->demux, buf, no);
179}
180EXPORT_SYMBOL(flexcop_pass_dmx_packets);
181
182static void flexcop_reset(struct flexcop_device *fc)
183{
Uwe Bugla1589a992009-03-29 07:46:58 -0300184 flexcop_ibi_value v210, v204;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700185
Uwe Bugla1589a992009-03-29 07:46:58 -0300186 /* reset the flexcop itself */
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700187 fc->write_ibi_reg(fc,ctrl_208,ibi_zero);
188
189 v210.raw = 0;
Patrick Boettcher64221be2005-07-07 17:57:49 -0700190 v210.sw_reset_210.reset_block_000 = 1;
191 v210.sw_reset_210.reset_block_100 = 1;
192 v210.sw_reset_210.reset_block_200 = 1;
193 v210.sw_reset_210.reset_block_300 = 1;
194 v210.sw_reset_210.reset_block_400 = 1;
195 v210.sw_reset_210.reset_block_500 = 1;
196 v210.sw_reset_210.reset_block_600 = 1;
197 v210.sw_reset_210.reset_block_700 = 1;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700198 v210.sw_reset_210.Block_reset_enable = 0xb2;
Patrick Boettcher64221be2005-07-07 17:57:49 -0700199 v210.sw_reset_210.Special_controls = 0xc259;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700200 fc->write_ibi_reg(fc,sw_reset_210,v210);
Patrick Boettcher64221be2005-07-07 17:57:49 -0700201 msleep(1);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700202
Uwe Bugla1589a992009-03-29 07:46:58 -0300203 /* reset the periphical devices */
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700204
205 v204 = fc->read_ibi_reg(fc,misc_204);
206 v204.misc_204.Per_reset_sig = 0;
207 fc->write_ibi_reg(fc,misc_204,v204);
Michael Krufkyc0b11b92005-11-08 21:35:32 -0800208 msleep(1);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700209 v204.misc_204.Per_reset_sig = 1;
210 fc->write_ibi_reg(fc,misc_204,v204);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700211}
212
Patrick Boettcher64221be2005-07-07 17:57:49 -0700213void flexcop_reset_block_300(struct flexcop_device *fc)
214{
Uwe Bugla1589a992009-03-29 07:46:58 -0300215 flexcop_ibi_value v208_save = fc->read_ibi_reg(fc, ctrl_208),
216 v210 = fc->read_ibi_reg(fc, sw_reset_210);
Patrick Boettcher64221be2005-07-07 17:57:49 -0700217
Uwe Bugla1589a992009-03-29 07:46:58 -0300218 deb_rdump("208: %08x, 210: %08x\n", v208_save.raw, v210.raw);
Patrick Boettcher64221be2005-07-07 17:57:49 -0700219 fc->write_ibi_reg(fc,ctrl_208,ibi_zero);
220
221 v210.sw_reset_210.reset_block_300 = 1;
222 v210.sw_reset_210.Block_reset_enable = 0xb2;
223
224 fc->write_ibi_reg(fc,sw_reset_210,v210);
Patrick Boettcher64221be2005-07-07 17:57:49 -0700225 fc->write_ibi_reg(fc,ctrl_208,v208_save);
226}
Patrick Boettcher64221be2005-07-07 17:57:49 -0700227
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700228struct flexcop_device *flexcop_device_kmalloc(size_t bus_specific_len)
229{
230 void *bus;
Uwe Bugla1589a992009-03-29 07:46:58 -0300231 struct flexcop_device *fc = kzalloc(sizeof(struct flexcop_device),
232 GFP_KERNEL);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700233 if (!fc) {
234 err("no memory");
235 return NULL;
236 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700237
Panagiotis Issaris74081872006-01-11 19:40:56 -0200238 bus = kzalloc(bus_specific_len, GFP_KERNEL);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700239 if (!bus) {
240 err("no memory");
241 kfree(fc);
242 return NULL;
243 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700244
245 fc->bus_specific = bus;
246
247 return fc;
248}
249EXPORT_SYMBOL(flexcop_device_kmalloc);
250
251void flexcop_device_kfree(struct flexcop_device *fc)
252{
253 kfree(fc->bus_specific);
254 kfree(fc);
255}
256EXPORT_SYMBOL(flexcop_device_kfree);
257
258int flexcop_device_initialize(struct flexcop_device *fc)
259{
260 int ret;
261 ibi_zero.raw = 0;
262
263 flexcop_reset(fc);
264 flexcop_determine_revision(fc);
265 flexcop_sram_init(fc);
266 flexcop_hw_filter_init(fc);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700267 flexcop_smc_ctrl(fc, 0);
268
Jonathan Niederefd279e2012-01-06 12:57:56 -0300269 ret = flexcop_dvb_init(fc);
270 if (ret)
Johannes Stezenbach77824132005-05-16 21:54:14 -0700271 goto error;
272
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300273 /* i2c has to be done before doing EEProm stuff -
274 * because the EEProm is accessed via i2c */
275 ret = flexcop_i2c_init(fc);
276 if (ret)
277 goto error;
278
Johannes Stezenbach77824132005-05-16 21:54:14 -0700279 /* do the MAC address reading after initializing the dvb_adapter */
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700280 if (fc->get_mac_addr(fc, 0) == 0) {
Johannes Stezenbach77824132005-05-16 21:54:14 -0700281 u8 *b = fc->dvb_adapter.proposed_mac;
Johannes Berg7c510e42008-10-27 17:47:26 -0700282 info("MAC address = %pM", b);
Johannes Stezenbach77824132005-05-16 21:54:14 -0700283 flexcop_set_mac_filter(fc,b);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700284 flexcop_mac_filter_ctrl(fc,1);
285 } else
286 warn("reading of MAC address failed.\n");
287
Jonathan Niederefd279e2012-01-06 12:57:56 -0300288 ret = flexcop_frontend_init(fc);
289 if (ret)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700290 goto error;
291
292 flexcop_device_name(fc,"initialization of","complete");
Trent Piepho83977032006-05-12 20:36:24 -0300293 return 0;
294
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700295error:
296 flexcop_device_exit(fc);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700297 return ret;
298}
299EXPORT_SYMBOL(flexcop_device_initialize);
300
301void flexcop_device_exit(struct flexcop_device *fc)
302{
303 flexcop_frontend_exit(fc);
304 flexcop_i2c_exit(fc);
305 flexcop_dvb_exit(fc);
306}
307EXPORT_SYMBOL(flexcop_device_exit);
308
309static int flexcop_module_init(void)
310{
311 info(DRIVER_NAME " loaded successfully");
312 return 0;
313}
314
315static void flexcop_module_cleanup(void)
316{
317 info(DRIVER_NAME " unloaded successfully");
318}
319
320module_init(flexcop_module_init);
321module_exit(flexcop_module_cleanup);
322
323MODULE_AUTHOR(DRIVER_AUTHOR);
324MODULE_DESCRIPTION(DRIVER_NAME);
325MODULE_LICENSE("GPL");