1 /*
2  * ngene-cards.c: nGene PCIe bridge driver - card specific info
3  *
4  * Copyright (C) 2005-2007 Micronas
5  *
6  * Copyright (C) 2008-2009 Ralph Metzler <rjkm@metzlerbros.de>
7  *                         Modifications for new nGene firmware,
8  *                         support for EEPROM-copying,
9  *                         support for new dual DVB-S2 card prototype
10  *
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2 only, as published by the Free Software Foundation.
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * To obtain the license, point your browser to
23  * http://www.gnu.org/copyleft/gpl.html
24  */
25 
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/pci.h>
31 #include <linux/pci_ids.h>
32 
33 #include "ngene.h"
34 
35 /* demods/tuners */
36 #include "stv6110x.h"
37 #include "stv090x.h"
38 #include "lnbh24.h"
39 #include "lgdt330x.h"
40 #include "mt2131.h"
41 #include "tda18271c2dd.h"
42 #include "drxk.h"
43 #include "drxd.h"
44 #include "dvb-pll.h"
45 #include "stv0367.h"
46 #include "stv0367_priv.h"
47 #include "tda18212.h"
48 #include "cxd2841er.h"
49 #include "stv0910.h"
50 #include "stv6111.h"
51 #include "lnbh25.h"
52 
53 /****************************************************************************/
54 /* I2C transfer functions used for demod/tuner probing***********************/
55 /****************************************************************************/
56 
57 static int i2c_io(struct i2c_adapter *adapter, u8 adr,
58 		  u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen)
59 {
60 	struct i2c_msg msgs[2] = {{.addr = adr,  .flags = 0,
61 				   .buf  = wbuf, .len   = wlen },
62 				  {.addr = adr,  .flags = I2C_M_RD,
63 				   .buf  = rbuf,  .len   = rlen } };
64 	return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
65 }
66 
67 static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
68 {
69 	struct i2c_msg msg = {.addr = adr, .flags = 0,
70 			      .buf = data, .len = len};
71 
72 	return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1;
73 }
74 
75 static int i2c_write_reg(struct i2c_adapter *adap, u8 adr,
76 			 u8 reg, u8 val)
77 {
78 	u8 msg[2] = {reg, val};
79 
80 	return i2c_write(adap, adr, msg, 2);
81 }
82 
83 static int i2c_read(struct i2c_adapter *adapter, u8 adr, u8 *val)
84 {
85 	struct i2c_msg msgs[1] = {{.addr = adr,  .flags = I2C_M_RD,
86 				   .buf  = val,  .len   = 1 } };
87 	return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1;
88 }
89 
90 static int i2c_read_reg16(struct i2c_adapter *adapter, u8 adr,
91 			  u16 reg, u8 *val)
92 {
93 	u8 msg[2] = {reg >> 8, reg & 0xff};
94 	struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
95 				   .buf  = msg, .len   = 2},
96 				  {.addr = adr, .flags = I2C_M_RD,
97 				   .buf  = val, .len   = 1} };
98 	return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
99 }
100 
101 static int i2c_read_regs(struct i2c_adapter *adapter,
102 			 u8 adr, u8 reg, u8 *val, u8 len)
103 {
104 	struct i2c_msg msgs[2] = {{.addr = adr,  .flags = 0,
105 				   .buf  = &reg, .len   = 1},
106 				  {.addr = adr,  .flags = I2C_M_RD,
107 				   .buf  = val,  .len   = len} };
108 
109 	return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
110 }
111 
112 static int i2c_read_reg(struct i2c_adapter *adapter, u8 adr, u8 reg, u8 *val)
113 {
114 	return i2c_read_regs(adapter, adr, reg, val, 1);
115 }
116 
117 /****************************************************************************/
118 /* Demod/tuner attachment ***************************************************/
119 /****************************************************************************/
120 
121 static struct i2c_adapter *i2c_adapter_from_chan(struct ngene_channel *chan)
122 {
123 	/* tuner 1+2: i2c adapter #0, tuner 3+4: i2c adapter #1 */
124 	if (chan->number < 2)
125 		return &chan->dev->channel[0].i2c_adapter;
126 
127 	return &chan->dev->channel[1].i2c_adapter;
128 }
129 
130 static int tuner_attach_stv6110(struct ngene_channel *chan)
131 {
132 	struct device *pdev = &chan->dev->pci_dev->dev;
133 	struct i2c_adapter *i2c = i2c_adapter_from_chan(chan);
134 	struct stv090x_config *feconf = (struct stv090x_config *)
135 		chan->dev->card_info->fe_config[chan->number];
136 	struct stv6110x_config *tunerconf = (struct stv6110x_config *)
137 		chan->dev->card_info->tuner_config[chan->number];
138 	const struct stv6110x_devctl *ctl;
139 
140 	if (chan->number < 2)
141 		i2c = &chan->dev->channel[0].i2c_adapter;
142 	else
143 		i2c = &chan->dev->channel[1].i2c_adapter;
144 
145 	ctl = dvb_attach(stv6110x_attach, chan->fe, tunerconf, i2c);
146 	if (ctl == NULL) {
147 		dev_err(pdev, "No STV6110X found!\n");
148 		return -ENODEV;
149 	}
150 
151 	feconf->tuner_init          = ctl->tuner_init;
152 	feconf->tuner_sleep         = ctl->tuner_sleep;
153 	feconf->tuner_set_mode      = ctl->tuner_set_mode;
154 	feconf->tuner_set_frequency = ctl->tuner_set_frequency;
155 	feconf->tuner_get_frequency = ctl->tuner_get_frequency;
156 	feconf->tuner_set_bandwidth = ctl->tuner_set_bandwidth;
157 	feconf->tuner_get_bandwidth = ctl->tuner_get_bandwidth;
158 	feconf->tuner_set_bbgain    = ctl->tuner_set_bbgain;
159 	feconf->tuner_get_bbgain    = ctl->tuner_get_bbgain;
160 	feconf->tuner_set_refclk    = ctl->tuner_set_refclk;
161 	feconf->tuner_get_status    = ctl->tuner_get_status;
162 
163 	return 0;
164 }
165 
166 static int tuner_attach_stv6111(struct ngene_channel *chan)
167 {
168 	struct device *pdev = &chan->dev->pci_dev->dev;
169 	struct i2c_adapter *i2c = i2c_adapter_from_chan(chan);
170 	struct dvb_frontend *fe;
171 	u8 adr = 4 + ((chan->number & 1) ? 0x63 : 0x60);
172 
173 	fe = dvb_attach(stv6111_attach, chan->fe, i2c, adr);
174 	if (!fe) {
175 		fe = dvb_attach(stv6111_attach, chan->fe, i2c, adr & ~4);
176 		if (!fe) {
177 			dev_err(pdev, "stv6111_attach() failed!\n");
178 			return -ENODEV;
179 		}
180 	}
181 	return 0;
182 }
183 
184 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
185 {
186 	struct ngene_channel *chan = fe->sec_priv;
187 	int status;
188 
189 	if (enable) {
190 		down(&chan->dev->pll_mutex);
191 		status = chan->gate_ctrl(fe, 1);
192 	} else {
193 		status = chan->gate_ctrl(fe, 0);
194 		up(&chan->dev->pll_mutex);
195 	}
196 	return status;
197 }
198 
199 static int tuner_attach_tda18271(struct ngene_channel *chan)
200 {
201 	struct device *pdev = &chan->dev->pci_dev->dev;
202 	struct i2c_adapter *i2c = i2c_adapter_from_chan(chan);
203 	struct dvb_frontend *fe;
204 
205 	if (chan->fe->ops.i2c_gate_ctrl)
206 		chan->fe->ops.i2c_gate_ctrl(chan->fe, 1);
207 	fe = dvb_attach(tda18271c2dd_attach, chan->fe, i2c, 0x60);
208 	if (chan->fe->ops.i2c_gate_ctrl)
209 		chan->fe->ops.i2c_gate_ctrl(chan->fe, 0);
210 	if (!fe) {
211 		dev_err(pdev, "No TDA18271 found!\n");
212 		return -ENODEV;
213 	}
214 
215 	return 0;
216 }
217 
218 static int tuner_tda18212_ping(struct ngene_channel *chan,
219 			       struct i2c_adapter *i2c,
220 			       unsigned short adr)
221 {
222 	struct device *pdev = &chan->dev->pci_dev->dev;
223 	u8 tda_id[2];
224 	u8 subaddr = 0x00;
225 
226 	dev_dbg(pdev, "stv0367-tda18212 tuner ping\n");
227 	if (chan->fe->ops.i2c_gate_ctrl)
228 		chan->fe->ops.i2c_gate_ctrl(chan->fe, 1);
229 
230 	if (i2c_read_regs(i2c, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
231 		dev_dbg(pdev, "tda18212 ping 1 fail\n");
232 	if (i2c_read_regs(i2c, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
233 		dev_warn(pdev, "tda18212 ping failed, expect problems\n");
234 
235 	if (chan->fe->ops.i2c_gate_ctrl)
236 		chan->fe->ops.i2c_gate_ctrl(chan->fe, 0);
237 
238 	return 0;
239 }
240 
241 static int tuner_attach_tda18212(struct ngene_channel *chan, u32 dmdtype)
242 {
243 	struct device *pdev = &chan->dev->pci_dev->dev;
244 	struct i2c_adapter *i2c = i2c_adapter_from_chan(chan);
245 	struct i2c_client *client;
246 	struct tda18212_config config = {
247 		.fe = chan->fe,
248 		.if_dvbt_6 = 3550,
249 		.if_dvbt_7 = 3700,
250 		.if_dvbt_8 = 4150,
251 		.if_dvbt2_6 = 3250,
252 		.if_dvbt2_7 = 4000,
253 		.if_dvbt2_8 = 4000,
254 		.if_dvbc = 5000,
255 	};
256 	u8 addr = (chan->number & 1) ? 0x63 : 0x60;
257 
258 	/*
259 	 * due to a hardware quirk with the I2C gate on the stv0367+tda18212
260 	 * combo, the tda18212 must be probed by reading it's id _twice_ when
261 	 * cold started, or it very likely will fail.
262 	 */
263 	if (dmdtype == DEMOD_TYPE_STV0367)
264 		tuner_tda18212_ping(chan, i2c, addr);
265 
266 	/* perform tuner probe/init/attach */
267 	client = dvb_module_probe("tda18212", NULL, i2c, addr, &config);
268 	if (!client)
269 		goto err;
270 
271 	chan->i2c_client[0] = client;
272 	chan->i2c_client_fe = 1;
273 
274 	return 0;
275 err:
276 	dev_err(pdev, "TDA18212 tuner not found. Device is not fully operational.\n");
277 	return -ENODEV;
278 }
279 
280 static int tuner_attach_probe(struct ngene_channel *chan)
281 {
282 	switch (chan->demod_type) {
283 	case DEMOD_TYPE_STV090X:
284 		return tuner_attach_stv6110(chan);
285 	case DEMOD_TYPE_DRXK:
286 		return tuner_attach_tda18271(chan);
287 	case DEMOD_TYPE_STV0367:
288 	case DEMOD_TYPE_SONY_CT2:
289 	case DEMOD_TYPE_SONY_ISDBT:
290 	case DEMOD_TYPE_SONY_C2T2:
291 	case DEMOD_TYPE_SONY_C2T2I:
292 		return tuner_attach_tda18212(chan, chan->demod_type);
293 	case DEMOD_TYPE_STV0910:
294 		return tuner_attach_stv6111(chan);
295 	}
296 
297 	return -EINVAL;
298 }
299 
300 static int demod_attach_stv0900(struct ngene_channel *chan)
301 {
302 	struct device *pdev = &chan->dev->pci_dev->dev;
303 	struct i2c_adapter *i2c = i2c_adapter_from_chan(chan);
304 	struct stv090x_config *feconf = (struct stv090x_config *)
305 		chan->dev->card_info->fe_config[chan->number];
306 
307 	/* tuner 1+2: i2c adapter #0, tuner 3+4: i2c adapter #1 */
308 	/* Note: Both adapters share the same i2c bus, but the demod     */
309 	/*       driver requires that each demod has its own i2c adapter */
310 	if (chan->number < 2)
311 		i2c = &chan->dev->channel[0].i2c_adapter;
312 	else
313 		i2c = &chan->dev->channel[1].i2c_adapter;
314 
315 	chan->fe = dvb_attach(stv090x_attach, feconf, i2c,
316 			(chan->number & 1) == 0 ? STV090x_DEMODULATOR_0
317 						: STV090x_DEMODULATOR_1);
318 	if (chan->fe == NULL) {
319 		dev_err(pdev, "No STV0900 found!\n");
320 		return -ENODEV;
321 	}
322 
323 	/* store channel info */
324 	if (feconf->tuner_i2c_lock)
325 		chan->fe->analog_demod_priv = chan;
326 
327 	if (!dvb_attach(lnbh24_attach, chan->fe, i2c, 0,
328 			0, chan->dev->card_info->lnb[chan->number])) {
329 		dev_err(pdev, "No LNBH24 found!\n");
330 		dvb_frontend_detach(chan->fe);
331 		chan->fe = NULL;
332 		return -ENODEV;
333 	}
334 
335 	return 0;
336 }
337 
338 static struct stv0910_cfg stv0910_p = {
339 	.adr      = 0x68,
340 	.parallel = 1,
341 	.rptlvl   = 4,
342 	.clk      = 30000000,
343 };
344 
345 static struct lnbh25_config lnbh25_cfg = {
346 	.i2c_address = 0x0c << 1,
347 	.data2_config = LNBH25_TEN
348 };
349 
350 static int demod_attach_stv0910(struct ngene_channel *chan,
351 				struct i2c_adapter *i2c)
352 {
353 	struct device *pdev = &chan->dev->pci_dev->dev;
354 	struct stv0910_cfg cfg = stv0910_p;
355 	struct lnbh25_config lnbcfg = lnbh25_cfg;
356 
357 	chan->fe = dvb_attach(stv0910_attach, i2c, &cfg, (chan->number & 1));
358 	if (!chan->fe) {
359 		cfg.adr = 0x6c;
360 		chan->fe = dvb_attach(stv0910_attach, i2c,
361 				      &cfg, (chan->number & 1));
362 	}
363 	if (!chan->fe) {
364 		dev_err(pdev, "stv0910_attach() failed!\n");
365 		return -ENODEV;
366 	}
367 
368 	/*
369 	 * attach lnbh25 - leftshift by one as the lnbh25 driver expects 8bit
370 	 * i2c addresses
371 	 */
372 	lnbcfg.i2c_address = (((chan->number & 1) ? 0x0d : 0x0c) << 1);
373 	if (!dvb_attach(lnbh25_attach, chan->fe, &lnbcfg, i2c)) {
374 		lnbcfg.i2c_address = (((chan->number & 1) ? 0x09 : 0x08) << 1);
375 		if (!dvb_attach(lnbh25_attach, chan->fe, &lnbcfg, i2c)) {
376 			dev_err(pdev, "lnbh25_attach() failed!\n");
377 			dvb_frontend_detach(chan->fe);
378 			chan->fe = NULL;
379 			return -ENODEV;
380 		}
381 	}
382 
383 	return 0;
384 }
385 
386 static struct stv0367_config ddb_stv0367_config[] = {
387 	{
388 		.demod_address = 0x1f,
389 		.xtal = 27000000,
390 		.if_khz = 0,
391 		.if_iq_mode = FE_TER_NORMAL_IF_TUNER,
392 		.ts_mode = STV0367_SERIAL_PUNCT_CLOCK,
393 		.clk_pol = STV0367_CLOCKPOLARITY_DEFAULT,
394 	}, {
395 		.demod_address = 0x1e,
396 		.xtal = 27000000,
397 		.if_khz = 0,
398 		.if_iq_mode = FE_TER_NORMAL_IF_TUNER,
399 		.ts_mode = STV0367_SERIAL_PUNCT_CLOCK,
400 		.clk_pol = STV0367_CLOCKPOLARITY_DEFAULT,
401 	},
402 };
403 
404 static int demod_attach_stv0367(struct ngene_channel *chan,
405 				struct i2c_adapter *i2c)
406 {
407 	struct device *pdev = &chan->dev->pci_dev->dev;
408 
409 	chan->fe = dvb_attach(stv0367ddb_attach,
410 			      &ddb_stv0367_config[(chan->number & 1)], i2c);
411 
412 	if (!chan->fe) {
413 		dev_err(pdev, "stv0367ddb_attach() failed!\n");
414 		return -ENODEV;
415 	}
416 
417 	chan->fe->sec_priv = chan;
418 	chan->gate_ctrl = chan->fe->ops.i2c_gate_ctrl;
419 	chan->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
420 	return 0;
421 }
422 
423 static int demod_attach_cxd28xx(struct ngene_channel *chan,
424 				struct i2c_adapter *i2c, int osc24)
425 {
426 	struct device *pdev = &chan->dev->pci_dev->dev;
427 	struct cxd2841er_config cfg;
428 
429 	/* the cxd2841er driver expects 8bit/shifted I2C addresses */
430 	cfg.i2c_addr = ((chan->number & 1) ? 0x6d : 0x6c) << 1;
431 
432 	cfg.xtal = osc24 ? SONY_XTAL_24000 : SONY_XTAL_20500;
433 	cfg.flags = CXD2841ER_AUTO_IFHZ | CXD2841ER_EARLY_TUNE |
434 		CXD2841ER_NO_WAIT_LOCK | CXD2841ER_NO_AGCNEG |
435 		CXD2841ER_TSBITS | CXD2841ER_TS_SERIAL;
436 
437 	/* attach frontend */
438 	chan->fe = dvb_attach(cxd2841er_attach_t_c, &cfg, i2c);
439 
440 	if (!chan->fe) {
441 		dev_err(pdev, "CXD28XX attach failed!\n");
442 		return -ENODEV;
443 	}
444 
445 	chan->fe->sec_priv = chan;
446 	chan->gate_ctrl = chan->fe->ops.i2c_gate_ctrl;
447 	chan->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
448 	return 0;
449 }
450 
451 static void cineS2_tuner_i2c_lock(struct dvb_frontend *fe, int lock)
452 {
453 	struct ngene_channel *chan = fe->analog_demod_priv;
454 
455 	if (lock)
456 		down(&chan->dev->pll_mutex);
457 	else
458 		up(&chan->dev->pll_mutex);
459 }
460 
461 static int port_has_stv0900(struct i2c_adapter *i2c, int port)
462 {
463 	u8 val;
464 	if (i2c_read_reg16(i2c, 0x68+port/2, 0xf100, &val) < 0)
465 		return 0;
466 	return 1;
467 }
468 
469 static int port_has_drxk(struct i2c_adapter *i2c, int port)
470 {
471 	u8 val;
472 
473 	if (i2c_read(i2c, 0x29+port, &val) < 0)
474 		return 0;
475 	return 1;
476 }
477 
478 static int port_has_stv0367(struct i2c_adapter *i2c)
479 {
480 	u8 val;
481 
482 	if (i2c_read_reg16(i2c, 0x1e, 0xf000, &val) < 0)
483 		return 0;
484 	if (val != 0x60)
485 		return 0;
486 	if (i2c_read_reg16(i2c, 0x1f, 0xf000, &val) < 0)
487 		return 0;
488 	if (val != 0x60)
489 		return 0;
490 	return 1;
491 }
492 
493 int ngene_port_has_cxd2099(struct i2c_adapter *i2c, u8 *type)
494 {
495 	u8 val;
496 	u8 probe[4] = { 0xe0, 0x00, 0x00, 0x00 }, data[4];
497 	struct i2c_msg msgs[2] = {{ .addr = 0x40,  .flags = 0,
498 				    .buf  = probe, .len   = 4 },
499 				  { .addr = 0x40,  .flags = I2C_M_RD,
500 				    .buf  = data,  .len   = 4 } };
501 	val = i2c_transfer(i2c, msgs, 2);
502 	if (val != 2)
503 		return 0;
504 
505 	if (data[0] == 0x02 && data[1] == 0x2b && data[3] == 0x43)
506 		*type = 2;
507 	else
508 		*type = 1;
509 	return 1;
510 }
511 
512 static int demod_attach_drxk(struct ngene_channel *chan,
513 			     struct i2c_adapter *i2c)
514 {
515 	struct device *pdev = &chan->dev->pci_dev->dev;
516 	struct drxk_config config;
517 
518 	memset(&config, 0, sizeof(config));
519 	config.microcode_name = "drxk_a3.mc";
520 	config.qam_demod_parameter_count = 4;
521 	config.adr = 0x29 + (chan->number ^ 2);
522 
523 	chan->fe = dvb_attach(drxk_attach, &config, i2c);
524 	if (!chan->fe) {
525 		dev_err(pdev, "No DRXK found!\n");
526 		return -ENODEV;
527 	}
528 	chan->fe->sec_priv = chan;
529 	chan->gate_ctrl = chan->fe->ops.i2c_gate_ctrl;
530 	chan->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
531 	return 0;
532 }
533 
534 /****************************************************************************/
535 /* XO2 related lists and functions ******************************************/
536 /****************************************************************************/
537 
538 static char *xo2names[] = {
539 	"DUAL DVB-S2",
540 	"DUAL DVB-C/T/T2",
541 	"DUAL DVB-ISDBT",
542 	"DUAL DVB-C/C2/T/T2",
543 	"DUAL ATSC",
544 	"DUAL DVB-C/C2/T/T2/I",
545 };
546 
547 static int init_xo2(struct ngene_channel *chan, struct i2c_adapter *i2c)
548 {
549 	struct device *pdev = &chan->dev->pci_dev->dev;
550 	u8 addr = 0x10;
551 	u8 val, data[2];
552 	int res;
553 
554 	res = i2c_read_regs(i2c, addr, 0x04, data, 2);
555 	if (res < 0)
556 		return res;
557 
558 	if (data[0] != 0x01)  {
559 		dev_info(pdev, "Invalid XO2 on channel %d\n", chan->number);
560 		return -1;
561 	}
562 
563 	i2c_read_reg(i2c, addr, 0x08, &val);
564 	if (val != 0) {
565 		i2c_write_reg(i2c, addr, 0x08, 0x00);
566 		msleep(100);
567 	}
568 	/* Enable tuner power, disable pll, reset demods */
569 	i2c_write_reg(i2c, addr, 0x08, 0x04);
570 	usleep_range(2000, 3000);
571 	/* Release demod resets */
572 	i2c_write_reg(i2c, addr, 0x08, 0x07);
573 
574 	/*
575 	 * speed: 0=55,1=75,2=90,3=104 MBit/s
576 	 * Note: The ngene hardware must be run at 75 MBit/s compared
577 	 * to more modern ddbridge hardware which runs at 90 MBit/s,
578 	 * else there will be issues with the data transport and non-
579 	 * working secondary/slave demods/tuners.
580 	 */
581 	i2c_write_reg(i2c, addr, 0x09, 1);
582 
583 	i2c_write_reg(i2c, addr, 0x0a, 0x01);
584 	i2c_write_reg(i2c, addr, 0x0b, 0x01);
585 
586 	usleep_range(2000, 3000);
587 	/* Start XO2 PLL */
588 	i2c_write_reg(i2c, addr, 0x08, 0x87);
589 
590 	return 0;
591 }
592 
593 static int port_has_xo2(struct i2c_adapter *i2c, u8 *type, u8 *id)
594 {
595 	u8 probe[1] = { 0x00 }, data[4];
596 	u8 addr = 0x10;
597 
598 	*type = NGENE_XO2_TYPE_NONE;
599 
600 	if (i2c_io(i2c, addr, probe, 1, data, 4))
601 		return 0;
602 	if (data[0] == 'D' && data[1] == 'F') {
603 		*id = data[2];
604 		*type = NGENE_XO2_TYPE_DUOFLEX;
605 		return 1;
606 	}
607 	if (data[0] == 'C' && data[1] == 'I') {
608 		*id = data[2];
609 		*type = NGENE_XO2_TYPE_CI;
610 		return 1;
611 	}
612 	return 0;
613 }
614 
615 /****************************************************************************/
616 /* Probing and port/channel handling ****************************************/
617 /****************************************************************************/
618 
619 static int cineS2_probe(struct ngene_channel *chan)
620 {
621 	struct device *pdev = &chan->dev->pci_dev->dev;
622 	struct i2c_adapter *i2c = i2c_adapter_from_chan(chan);
623 	struct stv090x_config *fe_conf;
624 	u8 buf[3];
625 	u8 xo2_type, xo2_id, xo2_demodtype;
626 	u8 sony_osc24 = 0;
627 	struct i2c_msg i2c_msg = { .flags = 0, .buf = buf };
628 	int rc;
629 
630 	if (port_has_xo2(i2c, &xo2_type, &xo2_id)) {
631 		xo2_id >>= 2;
632 		dev_dbg(pdev, "XO2 on channel %d (type %d, id %d)\n",
633 			chan->number, xo2_type, xo2_id);
634 
635 		switch (xo2_type) {
636 		case NGENE_XO2_TYPE_DUOFLEX:
637 			if (chan->number & 1)
638 				dev_dbg(pdev,
639 					"skipping XO2 init on odd channel %d",
640 					chan->number);
641 			else
642 				init_xo2(chan, i2c);
643 
644 			xo2_demodtype = DEMOD_TYPE_XO2 + xo2_id;
645 
646 			switch (xo2_demodtype) {
647 			case DEMOD_TYPE_SONY_CT2:
648 			case DEMOD_TYPE_SONY_ISDBT:
649 			case DEMOD_TYPE_SONY_C2T2:
650 			case DEMOD_TYPE_SONY_C2T2I:
651 				dev_info(pdev, "%s (XO2) on channel %d\n",
652 					 xo2names[xo2_id], chan->number);
653 				chan->demod_type = xo2_demodtype;
654 				if (xo2_demodtype == DEMOD_TYPE_SONY_C2T2I)
655 					sony_osc24 = 1;
656 
657 				demod_attach_cxd28xx(chan, i2c, sony_osc24);
658 				break;
659 			case DEMOD_TYPE_STV0910:
660 				dev_info(pdev, "%s (XO2) on channel %d\n",
661 					 xo2names[xo2_id], chan->number);
662 				chan->demod_type = xo2_demodtype;
663 				demod_attach_stv0910(chan, i2c);
664 				break;
665 			default:
666 				dev_warn(pdev,
667 					 "Unsupported XO2 module on channel %d\n",
668 					 chan->number);
669 				return -ENODEV;
670 			}
671 			break;
672 		case NGENE_XO2_TYPE_CI:
673 			dev_info(pdev, "DuoFlex CI modules not supported\n");
674 			return -ENODEV;
675 		default:
676 			dev_info(pdev, "Unsupported XO2 module type\n");
677 			return -ENODEV;
678 		}
679 	} else if (port_has_stv0900(i2c, chan->number)) {
680 		chan->demod_type = DEMOD_TYPE_STV090X;
681 		fe_conf = chan->dev->card_info->fe_config[chan->number];
682 		/* demod found, attach it */
683 		rc = demod_attach_stv0900(chan);
684 		if (rc < 0 || chan->number < 2)
685 			return rc;
686 
687 		/* demod #2: reprogram outputs DPN1 & DPN2 */
688 		i2c_msg.addr = fe_conf->address;
689 		i2c_msg.len = 3;
690 		buf[0] = 0xf1;
691 		switch (chan->number) {
692 		case 2:
693 			buf[1] = 0x5c;
694 			buf[2] = 0xc2;
695 			break;
696 		case 3:
697 			buf[1] = 0x61;
698 			buf[2] = 0xcc;
699 			break;
700 		default:
701 			return -ENODEV;
702 		}
703 		rc = i2c_transfer(i2c, &i2c_msg, 1);
704 		if (rc != 1) {
705 			dev_err(pdev, "Could not setup DPNx\n");
706 			return -EIO;
707 		}
708 	} else if (port_has_drxk(i2c, chan->number^2)) {
709 		chan->demod_type = DEMOD_TYPE_DRXK;
710 		demod_attach_drxk(chan, i2c);
711 	} else if (port_has_stv0367(i2c)) {
712 		chan->demod_type = DEMOD_TYPE_STV0367;
713 		dev_info(pdev, "STV0367 on channel %d\n", chan->number);
714 		demod_attach_stv0367(chan, i2c);
715 	} else {
716 		dev_info(pdev, "No demod found on chan %d\n", chan->number);
717 		return -ENODEV;
718 	}
719 	return 0;
720 }
721 
722 
723 static struct lgdt330x_config aver_m780 = {
724 	.demod_address = 0xb2 >> 1,
725 	.demod_chip    = LGDT3303,
726 	.serial_mpeg   = 0x00, /* PARALLEL */
727 	.clock_polarity_flip = 1,
728 };
729 
730 static struct mt2131_config m780_tunerconfig = {
731 	0xc0 >> 1
732 };
733 
734 /* A single func to attach the demo and tuner, rather than
735  * use two sep funcs like the current design mandates.
736  */
737 static int demod_attach_lg330x(struct ngene_channel *chan)
738 {
739 	struct device *pdev = &chan->dev->pci_dev->dev;
740 
741 	chan->fe = dvb_attach(lgdt330x_attach, &aver_m780, &chan->i2c_adapter);
742 	if (chan->fe == NULL) {
743 		dev_err(pdev, "No LGDT330x found!\n");
744 		return -ENODEV;
745 	}
746 
747 	dvb_attach(mt2131_attach, chan->fe, &chan->i2c_adapter,
748 		   &m780_tunerconfig, 0);
749 
750 	return (chan->fe) ? 0 : -ENODEV;
751 }
752 
753 static int demod_attach_drxd(struct ngene_channel *chan)
754 {
755 	struct device *pdev = &chan->dev->pci_dev->dev;
756 	struct drxd_config *feconf;
757 
758 	feconf = chan->dev->card_info->fe_config[chan->number];
759 
760 	chan->fe = dvb_attach(drxd_attach, feconf, chan,
761 			&chan->i2c_adapter, &chan->dev->pci_dev->dev);
762 	if (!chan->fe) {
763 		dev_err(pdev, "No DRXD found!\n");
764 		return -ENODEV;
765 	}
766 	return 0;
767 }
768 
769 static int tuner_attach_dtt7520x(struct ngene_channel *chan)
770 {
771 	struct device *pdev = &chan->dev->pci_dev->dev;
772 	struct drxd_config *feconf;
773 
774 	feconf = chan->dev->card_info->fe_config[chan->number];
775 
776 	if (!dvb_attach(dvb_pll_attach, chan->fe, feconf->pll_address,
777 			&chan->i2c_adapter,
778 			feconf->pll_type)) {
779 		dev_err(pdev, "No pll(%d) found!\n", feconf->pll_type);
780 		return -ENODEV;
781 	}
782 	return 0;
783 }
784 
785 /****************************************************************************/
786 /* EEPROM TAGS **************************************************************/
787 /****************************************************************************/
788 
789 #define MICNG_EE_START      0x0100
790 #define MICNG_EE_END        0x0FF0
791 
792 #define MICNG_EETAG_END0    0x0000
793 #define MICNG_EETAG_END1    0xFFFF
794 
795 /* 0x0001 - 0x000F reserved for housekeeping */
796 /* 0xFFFF - 0xFFFE reserved for housekeeping */
797 
798 /* Micronas assigned tags
799    EEProm tags for hardware support */
800 
801 #define MICNG_EETAG_DRXD1_OSCDEVIATION  0x1000  /* 2 Bytes data */
802 #define MICNG_EETAG_DRXD2_OSCDEVIATION  0x1001  /* 2 Bytes data */
803 
804 #define MICNG_EETAG_MT2060_1_1STIF      0x1100  /* 2 Bytes data */
805 #define MICNG_EETAG_MT2060_2_1STIF      0x1101  /* 2 Bytes data */
806 
807 /* Tag range for OEMs */
808 
809 #define MICNG_EETAG_OEM_FIRST  0xC000
810 #define MICNG_EETAG_OEM_LAST   0xFFEF
811 
812 static int i2c_write_eeprom(struct i2c_adapter *adapter,
813 			    u8 adr, u16 reg, u8 data)
814 {
815 	struct device *pdev = adapter->dev.parent;
816 	u8 m[3] = {(reg >> 8), (reg & 0xff), data};
817 	struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = m,
818 			      .len = sizeof(m)};
819 
820 	if (i2c_transfer(adapter, &msg, 1) != 1) {
821 		dev_err(pdev, "Error writing EEPROM!\n");
822 		return -EIO;
823 	}
824 	return 0;
825 }
826 
827 static int i2c_read_eeprom(struct i2c_adapter *adapter,
828 			   u8 adr, u16 reg, u8 *data, int len)
829 {
830 	struct device *pdev = adapter->dev.parent;
831 	u8 msg[2] = {(reg >> 8), (reg & 0xff)};
832 	struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
833 				   .buf = msg, .len = 2 },
834 				  {.addr = adr, .flags = I2C_M_RD,
835 				   .buf = data, .len = len} };
836 
837 	if (i2c_transfer(adapter, msgs, 2) != 2) {
838 		dev_err(pdev, "Error reading EEPROM\n");
839 		return -EIO;
840 	}
841 	return 0;
842 }
843 
844 static int ReadEEProm(struct i2c_adapter *adapter,
845 		      u16 Tag, u32 MaxLen, u8 *data, u32 *pLength)
846 {
847 	struct device *pdev = adapter->dev.parent;
848 	int status = 0;
849 	u16 Addr = MICNG_EE_START, Length, tag = 0;
850 	u8  EETag[3];
851 
852 	while (Addr + sizeof(u16) + 1 < MICNG_EE_END) {
853 		if (i2c_read_eeprom(adapter, 0x50, Addr, EETag, sizeof(EETag)))
854 			return -1;
855 		tag = (EETag[0] << 8) | EETag[1];
856 		if (tag == MICNG_EETAG_END0 || tag == MICNG_EETAG_END1)
857 			return -1;
858 		if (tag == Tag)
859 			break;
860 		Addr += sizeof(u16) + 1 + EETag[2];
861 	}
862 	if (Addr + sizeof(u16) + 1 + EETag[2] > MICNG_EE_END) {
863 		dev_err(pdev, "Reached EOEE @ Tag = %04x Length = %3d\n",
864 			tag, EETag[2]);
865 		return -1;
866 	}
867 	Length = EETag[2];
868 	if (Length > MaxLen)
869 		Length = (u16) MaxLen;
870 	if (Length > 0) {
871 		Addr += sizeof(u16) + 1;
872 		status = i2c_read_eeprom(adapter, 0x50, Addr, data, Length);
873 		if (!status) {
874 			*pLength = EETag[2];
875 #if 0
876 			if (Length < EETag[2])
877 				status = STATUS_BUFFER_OVERFLOW;
878 #endif
879 		}
880 	}
881 	return status;
882 }
883 
884 static int WriteEEProm(struct i2c_adapter *adapter,
885 		       u16 Tag, u32 Length, u8 *data)
886 {
887 	struct device *pdev = adapter->dev.parent;
888 	int status = 0;
889 	u16 Addr = MICNG_EE_START;
890 	u8 EETag[3];
891 	u16 tag = 0;
892 	int retry, i;
893 
894 	while (Addr + sizeof(u16) + 1 < MICNG_EE_END) {
895 		if (i2c_read_eeprom(adapter, 0x50, Addr, EETag, sizeof(EETag)))
896 			return -1;
897 		tag = (EETag[0] << 8) | EETag[1];
898 		if (tag == MICNG_EETAG_END0 || tag == MICNG_EETAG_END1)
899 			return -1;
900 		if (tag == Tag)
901 			break;
902 		Addr += sizeof(u16) + 1 + EETag[2];
903 	}
904 	if (Addr + sizeof(u16) + 1 + EETag[2] > MICNG_EE_END) {
905 		dev_err(pdev, "Reached EOEE @ Tag = %04x Length = %3d\n",
906 			tag, EETag[2]);
907 		return -1;
908 	}
909 
910 	if (Length > EETag[2])
911 		return -EINVAL;
912 	/* Note: We write the data one byte at a time to avoid
913 	   issues with page sizes. (which are different for
914 	   each manufacture and eeprom size)
915 	 */
916 	Addr += sizeof(u16) + 1;
917 	for (i = 0; i < Length; i++, Addr++) {
918 		status = i2c_write_eeprom(adapter, 0x50, Addr, data[i]);
919 
920 		if (status)
921 			break;
922 
923 		/* Poll for finishing write cycle */
924 		retry = 10;
925 		while (retry) {
926 			u8 Tmp;
927 
928 			msleep(50);
929 			status = i2c_read_eeprom(adapter, 0x50, Addr, &Tmp, 1);
930 			if (status)
931 				break;
932 			if (Tmp != data[i])
933 				dev_err(pdev, "eeprom write error\n");
934 			retry -= 1;
935 		}
936 		if (status) {
937 			dev_err(pdev, "Timeout polling eeprom\n");
938 			break;
939 		}
940 	}
941 	return status;
942 }
943 
944 static int eeprom_read_ushort(struct i2c_adapter *adapter, u16 tag, u16 *data)
945 {
946 	int stat;
947 	u8 buf[2];
948 	u32 len = 0;
949 
950 	stat = ReadEEProm(adapter, tag, 2, buf, &len);
951 	if (stat)
952 		return stat;
953 	if (len != 2)
954 		return -EINVAL;
955 
956 	*data = (buf[0] << 8) | buf[1];
957 	return 0;
958 }
959 
960 static int eeprom_write_ushort(struct i2c_adapter *adapter, u16 tag, u16 data)
961 {
962 	int stat;
963 	u8 buf[2];
964 
965 	buf[0] = data >> 8;
966 	buf[1] = data & 0xff;
967 	stat = WriteEEProm(adapter, tag, 2, buf);
968 	if (stat)
969 		return stat;
970 	return 0;
971 }
972 
973 static s16 osc_deviation(void *priv, s16 deviation, int flag)
974 {
975 	struct ngene_channel *chan = priv;
976 	struct device *pdev = &chan->dev->pci_dev->dev;
977 	struct i2c_adapter *adap = &chan->i2c_adapter;
978 	u16 data = 0;
979 
980 	if (flag) {
981 		data = (u16) deviation;
982 		dev_info(pdev, "write deviation %d\n",
983 			 deviation);
984 		eeprom_write_ushort(adap, 0x1000 + chan->number, data);
985 	} else {
986 		if (eeprom_read_ushort(adap, 0x1000 + chan->number, &data))
987 			data = 0;
988 		dev_info(pdev, "read deviation %d\n",
989 			 (s16)data);
990 	}
991 
992 	return (s16) data;
993 }
994 
995 /****************************************************************************/
996 /* Switch control (I2C gates, etc.) *****************************************/
997 /****************************************************************************/
998 
999 
1000 static struct stv090x_config fe_cineS2 = {
1001 	.device         = STV0900,
1002 	.demod_mode     = STV090x_DUAL,
1003 	.clk_mode       = STV090x_CLK_EXT,
1004 
1005 	.xtal           = 27000000,
1006 	.address        = 0x68,
1007 
1008 	.ts1_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,
1009 	.ts2_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,
1010 
1011 	.repeater_level = STV090x_RPTLEVEL_16,
1012 
1013 	.adc1_range	= STV090x_ADC_1Vpp,
1014 	.adc2_range	= STV090x_ADC_1Vpp,
1015 
1016 	.diseqc_envelope_mode = true,
1017 
1018 	.tuner_i2c_lock = cineS2_tuner_i2c_lock,
1019 };
1020 
1021 static struct stv090x_config fe_cineS2_2 = {
1022 	.device         = STV0900,
1023 	.demod_mode     = STV090x_DUAL,
1024 	.clk_mode       = STV090x_CLK_EXT,
1025 
1026 	.xtal           = 27000000,
1027 	.address        = 0x69,
1028 
1029 	.ts1_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,
1030 	.ts2_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,
1031 
1032 	.repeater_level = STV090x_RPTLEVEL_16,
1033 
1034 	.adc1_range	= STV090x_ADC_1Vpp,
1035 	.adc2_range	= STV090x_ADC_1Vpp,
1036 
1037 	.diseqc_envelope_mode = true,
1038 
1039 	.tuner_i2c_lock = cineS2_tuner_i2c_lock,
1040 };
1041 
1042 static struct stv6110x_config tuner_cineS2_0 = {
1043 	.addr	= 0x60,
1044 	.refclk	= 27000000,
1045 	.clk_div = 1,
1046 };
1047 
1048 static struct stv6110x_config tuner_cineS2_1 = {
1049 	.addr	= 0x63,
1050 	.refclk	= 27000000,
1051 	.clk_div = 1,
1052 };
1053 
1054 static const struct ngene_info ngene_info_cineS2 = {
1055 	.type		= NGENE_SIDEWINDER,
1056 	.name		= "Linux4Media cineS2 DVB-S2 Twin Tuner",
1057 	.io_type	= {NGENE_IO_TSIN, NGENE_IO_TSIN},
1058 	.demod_attach	= {demod_attach_stv0900, demod_attach_stv0900},
1059 	.tuner_attach	= {tuner_attach_stv6110, tuner_attach_stv6110},
1060 	.fe_config	= {&fe_cineS2, &fe_cineS2},
1061 	.tuner_config	= {&tuner_cineS2_0, &tuner_cineS2_1},
1062 	.lnb		= {0x0b, 0x08},
1063 	.tsf		= {3, 3},
1064 	.fw_version	= 18,
1065 	.msi_supported	= true,
1066 };
1067 
1068 static const struct ngene_info ngene_info_satixS2 = {
1069 	.type		= NGENE_SIDEWINDER,
1070 	.name		= "Mystique SaTiX-S2 Dual",
1071 	.io_type	= {NGENE_IO_TSIN, NGENE_IO_TSIN},
1072 	.demod_attach	= {demod_attach_stv0900, demod_attach_stv0900},
1073 	.tuner_attach	= {tuner_attach_stv6110, tuner_attach_stv6110},
1074 	.fe_config	= {&fe_cineS2, &fe_cineS2},
1075 	.tuner_config	= {&tuner_cineS2_0, &tuner_cineS2_1},
1076 	.lnb		= {0x0b, 0x08},
1077 	.tsf		= {3, 3},
1078 	.fw_version	= 18,
1079 	.msi_supported	= true,
1080 };
1081 
1082 static const struct ngene_info ngene_info_satixS2v2 = {
1083 	.type		= NGENE_SIDEWINDER,
1084 	.name		= "Mystique SaTiX-S2 Dual (v2)",
1085 	.io_type	= {NGENE_IO_TSIN, NGENE_IO_TSIN, NGENE_IO_TSIN, NGENE_IO_TSIN,
1086 			   NGENE_IO_TSOUT},
1087 	.demod_attach	= {demod_attach_stv0900, demod_attach_stv0900, cineS2_probe, cineS2_probe},
1088 	.tuner_attach	= {tuner_attach_stv6110, tuner_attach_stv6110, tuner_attach_probe, tuner_attach_probe},
1089 	.fe_config	= {&fe_cineS2, &fe_cineS2, &fe_cineS2_2, &fe_cineS2_2},
1090 	.tuner_config	= {&tuner_cineS2_0, &tuner_cineS2_1, &tuner_cineS2_0, &tuner_cineS2_1},
1091 	.lnb		= {0x0a, 0x08, 0x0b, 0x09},
1092 	.tsf		= {3, 3},
1093 	.fw_version	= 18,
1094 	.msi_supported	= true,
1095 };
1096 
1097 static const struct ngene_info ngene_info_cineS2v5 = {
1098 	.type		= NGENE_SIDEWINDER,
1099 	.name		= "Linux4Media cineS2 DVB-S2 Twin Tuner (v5)",
1100 	.io_type	= {NGENE_IO_TSIN, NGENE_IO_TSIN, NGENE_IO_TSIN, NGENE_IO_TSIN,
1101 			   NGENE_IO_TSOUT},
1102 	.demod_attach	= {demod_attach_stv0900, demod_attach_stv0900, cineS2_probe, cineS2_probe},
1103 	.tuner_attach	= {tuner_attach_stv6110, tuner_attach_stv6110, tuner_attach_probe, tuner_attach_probe},
1104 	.fe_config	= {&fe_cineS2, &fe_cineS2, &fe_cineS2_2, &fe_cineS2_2},
1105 	.tuner_config	= {&tuner_cineS2_0, &tuner_cineS2_1, &tuner_cineS2_0, &tuner_cineS2_1},
1106 	.lnb		= {0x0a, 0x08, 0x0b, 0x09},
1107 	.tsf		= {3, 3},
1108 	.fw_version	= 18,
1109 	.msi_supported	= true,
1110 };
1111 
1112 
1113 static const struct ngene_info ngene_info_duoFlex = {
1114 	.type           = NGENE_SIDEWINDER,
1115 	.name           = "Digital Devices DuoFlex PCIe or miniPCIe",
1116 	.io_type        = {NGENE_IO_TSIN, NGENE_IO_TSIN, NGENE_IO_TSIN, NGENE_IO_TSIN,
1117 			   NGENE_IO_TSOUT},
1118 	.demod_attach   = {cineS2_probe, cineS2_probe, cineS2_probe, cineS2_probe},
1119 	.tuner_attach   = {tuner_attach_probe, tuner_attach_probe, tuner_attach_probe, tuner_attach_probe},
1120 	.fe_config      = {&fe_cineS2, &fe_cineS2, &fe_cineS2_2, &fe_cineS2_2},
1121 	.tuner_config   = {&tuner_cineS2_0, &tuner_cineS2_1, &tuner_cineS2_0, &tuner_cineS2_1},
1122 	.lnb            = {0x0a, 0x08, 0x0b, 0x09},
1123 	.tsf            = {3, 3},
1124 	.fw_version     = 18,
1125 	.msi_supported	= true,
1126 };
1127 
1128 static const struct ngene_info ngene_info_m780 = {
1129 	.type           = NGENE_APP,
1130 	.name           = "Aver M780 ATSC/QAM-B",
1131 
1132 	/* Channel 0 is analog, which is currently unsupported */
1133 	.io_type        = { NGENE_IO_NONE, NGENE_IO_TSIN },
1134 	.demod_attach   = { NULL, demod_attach_lg330x },
1135 
1136 	/* Ensure these are NULL else the frame will call them (as funcs) */
1137 	.tuner_attach   = { NULL, NULL, NULL, NULL },
1138 	.fe_config      = { NULL, &aver_m780 },
1139 	.avf            = { 0 },
1140 
1141 	/* A custom electrical interface config for the demod to bridge */
1142 	.tsf		= { 4, 4 },
1143 	.fw_version	= 15,
1144 };
1145 
1146 static struct drxd_config fe_terratec_dvbt_0 = {
1147 	.index          = 0,
1148 	.demod_address  = 0x70,
1149 	.demod_revision = 0xa2,
1150 	.demoda_address = 0x00,
1151 	.pll_address    = 0x60,
1152 	.pll_type       = DVB_PLL_THOMSON_DTT7520X,
1153 	.clock          = 20000,
1154 	.osc_deviation  = osc_deviation,
1155 };
1156 
1157 static struct drxd_config fe_terratec_dvbt_1 = {
1158 	.index          = 1,
1159 	.demod_address  = 0x71,
1160 	.demod_revision = 0xa2,
1161 	.demoda_address = 0x00,
1162 	.pll_address    = 0x60,
1163 	.pll_type       = DVB_PLL_THOMSON_DTT7520X,
1164 	.clock          = 20000,
1165 	.osc_deviation  = osc_deviation,
1166 };
1167 
1168 static const struct ngene_info ngene_info_terratec = {
1169 	.type           = NGENE_TERRATEC,
1170 	.name           = "Terratec Integra/Cinergy2400i Dual DVB-T",
1171 	.io_type        = {NGENE_IO_TSIN, NGENE_IO_TSIN},
1172 	.demod_attach   = {demod_attach_drxd, demod_attach_drxd},
1173 	.tuner_attach	= {tuner_attach_dtt7520x, tuner_attach_dtt7520x},
1174 	.fe_config      = {&fe_terratec_dvbt_0, &fe_terratec_dvbt_1},
1175 	.i2c_access     = 1,
1176 };
1177 
1178 /****************************************************************************/
1179 
1180 
1181 
1182 /****************************************************************************/
1183 /* PCI Subsystem ID *********************************************************/
1184 /****************************************************************************/
1185 
1186 #define NGENE_ID(_subvend, _subdev, _driverdata) { \
1187 	.vendor = NGENE_VID, .device = NGENE_PID, \
1188 	.subvendor = _subvend, .subdevice = _subdev, \
1189 	.driver_data = (unsigned long) &_driverdata }
1190 
1191 /****************************************************************************/
1192 
1193 static const struct pci_device_id ngene_id_tbl[] = {
1194 	NGENE_ID(0x18c3, 0xab04, ngene_info_cineS2),
1195 	NGENE_ID(0x18c3, 0xab05, ngene_info_cineS2v5),
1196 	NGENE_ID(0x18c3, 0xabc3, ngene_info_cineS2),
1197 	NGENE_ID(0x18c3, 0xabc4, ngene_info_cineS2),
1198 	NGENE_ID(0x18c3, 0xdb01, ngene_info_satixS2),
1199 	NGENE_ID(0x18c3, 0xdb02, ngene_info_satixS2v2),
1200 	NGENE_ID(0x18c3, 0xdd00, ngene_info_cineS2v5),
1201 	NGENE_ID(0x18c3, 0xdd10, ngene_info_duoFlex),
1202 	NGENE_ID(0x18c3, 0xdd20, ngene_info_duoFlex),
1203 	NGENE_ID(0x1461, 0x062e, ngene_info_m780),
1204 	NGENE_ID(0x153b, 0x1167, ngene_info_terratec),
1205 	{0}
1206 };
1207 MODULE_DEVICE_TABLE(pci, ngene_id_tbl);
1208 
1209 /****************************************************************************/
1210 /* Init/Exit ****************************************************************/
1211 /****************************************************************************/
1212 
1213 static pci_ers_result_t ngene_error_detected(struct pci_dev *dev,
1214 					     enum pci_channel_state state)
1215 {
1216 	dev_err(&dev->dev, "PCI error\n");
1217 	if (state == pci_channel_io_perm_failure)
1218 		return PCI_ERS_RESULT_DISCONNECT;
1219 	if (state == pci_channel_io_frozen)
1220 		return PCI_ERS_RESULT_NEED_RESET;
1221 	return PCI_ERS_RESULT_CAN_RECOVER;
1222 }
1223 
1224 static pci_ers_result_t ngene_slot_reset(struct pci_dev *dev)
1225 {
1226 	dev_info(&dev->dev, "slot reset\n");
1227 	return 0;
1228 }
1229 
1230 static void ngene_resume(struct pci_dev *dev)
1231 {
1232 	dev_info(&dev->dev, "resume\n");
1233 }
1234 
1235 static const struct pci_error_handlers ngene_errors = {
1236 	.error_detected = ngene_error_detected,
1237 	.slot_reset = ngene_slot_reset,
1238 	.resume = ngene_resume,
1239 };
1240 
1241 static struct pci_driver ngene_pci_driver = {
1242 	.name        = "ngene",
1243 	.id_table    = ngene_id_tbl,
1244 	.probe       = ngene_probe,
1245 	.remove      = ngene_remove,
1246 	.err_handler = &ngene_errors,
1247 	.shutdown    = ngene_shutdown,
1248 };
1249 
1250 static __init int module_init_ngene(void)
1251 {
1252 	/* pr_*() since we don't have a device to use with dev_*() yet */
1253 	pr_info("nGene PCIE bridge driver, Copyright (C) 2005-2007 Micronas\n");
1254 
1255 	return pci_register_driver(&ngene_pci_driver);
1256 }
1257 
1258 static __exit void module_exit_ngene(void)
1259 {
1260 	pci_unregister_driver(&ngene_pci_driver);
1261 }
1262 
1263 module_init(module_init_ngene);
1264 module_exit(module_exit_ngene);
1265 
1266 MODULE_DESCRIPTION("nGene");
1267 MODULE_AUTHOR("Micronas, Ralph Metzler, Manfred Voelkel");
1268 MODULE_LICENSE("GPL");
1269