1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "saa7164.h"
23 
24 #include "tda10048.h"
25 #include "tda18271.h"
26 #include "s5h1411.h"
27 #include "si2157.h"
28 #include "si2168.h"
29 #include "lgdt3306a.h"
30 
31 #define DRIVER_NAME "saa7164"
32 
33 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
34 
35 /* addr is in the card struct, get it from there */
36 static struct tda10048_config hauppauge_hvr2200_1_config = {
37 	.demod_address    = 0x10 >> 1,
38 	.output_mode      = TDA10048_SERIAL_OUTPUT,
39 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
40 	.inversion        = TDA10048_INVERSION_ON,
41 	.dtv6_if_freq_khz = TDA10048_IF_3300,
42 	.dtv7_if_freq_khz = TDA10048_IF_3500,
43 	.dtv8_if_freq_khz = TDA10048_IF_4000,
44 	.clk_freq_khz     = TDA10048_CLK_16000,
45 };
46 static struct tda10048_config hauppauge_hvr2200_2_config = {
47 	.demod_address    = 0x12 >> 1,
48 	.output_mode      = TDA10048_SERIAL_OUTPUT,
49 	.fwbulkwritelen   = TDA10048_BULKWRITE_200,
50 	.inversion        = TDA10048_INVERSION_ON,
51 	.dtv6_if_freq_khz = TDA10048_IF_3300,
52 	.dtv7_if_freq_khz = TDA10048_IF_3500,
53 	.dtv8_if_freq_khz = TDA10048_IF_4000,
54 	.clk_freq_khz     = TDA10048_CLK_16000,
55 };
56 
57 static struct tda18271_std_map hauppauge_tda18271_std_map = {
58 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 3,
59 		      .if_lvl = 6, .rfagc_top = 0x37 },
60 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 0,
61 		      .if_lvl = 6, .rfagc_top = 0x37 },
62 };
63 
64 static struct tda18271_config hauppauge_hvr22x0_tuner_config = {
65 	.std_map	= &hauppauge_tda18271_std_map,
66 	.gate		= TDA18271_GATE_ANALOG,
67 	.role		= TDA18271_MASTER,
68 };
69 
70 static struct tda18271_config hauppauge_hvr22x0s_tuner_config = {
71 	.std_map	= &hauppauge_tda18271_std_map,
72 	.gate		= TDA18271_GATE_ANALOG,
73 	.role		= TDA18271_SLAVE,
74 	.output_opt     = TDA18271_OUTPUT_LT_OFF,
75 	.rf_cal_on_startup = 1
76 };
77 
78 static struct s5h1411_config hauppauge_s5h1411_config = {
79 	.output_mode   = S5H1411_SERIAL_OUTPUT,
80 	.gpio          = S5H1411_GPIO_ON,
81 	.qam_if        = S5H1411_IF_4000,
82 	.vsb_if        = S5H1411_IF_3250,
83 	.inversion     = S5H1411_INVERSION_ON,
84 	.status_mode   = S5H1411_DEMODLOCKING,
85 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
86 };
87 
88 static struct lgdt3306a_config hauppauge_hvr2255a_config = {
89 	.i2c_addr               = 0xb2 >> 1,
90 	.qam_if_khz             = 4000,
91 	.vsb_if_khz             = 3250,
92 	.deny_i2c_rptr          = 1, /* Disabled */
93 	.spectral_inversion     = 0, /* Disabled */
94 	.mpeg_mode              = LGDT3306A_MPEG_SERIAL,
95 	.tpclk_edge             = LGDT3306A_TPCLK_RISING_EDGE,
96 	.tpvalid_polarity       = LGDT3306A_TP_VALID_HIGH,
97 	.xtalMHz                = 25, /* 24 or 25 */
98 };
99 
100 static struct lgdt3306a_config hauppauge_hvr2255b_config = {
101 	.i2c_addr               = 0x1c >> 1,
102 	.qam_if_khz             = 4000,
103 	.vsb_if_khz             = 3250,
104 	.deny_i2c_rptr          = 1, /* Disabled */
105 	.spectral_inversion     = 0, /* Disabled */
106 	.mpeg_mode              = LGDT3306A_MPEG_SERIAL,
107 	.tpclk_edge             = LGDT3306A_TPCLK_RISING_EDGE,
108 	.tpvalid_polarity       = LGDT3306A_TP_VALID_HIGH,
109 	.xtalMHz                = 25, /* 24 or 25 */
110 };
111 
112 static struct si2157_config hauppauge_hvr2255_tuner_config = {
113 	.inversion = 1,
114 	.if_port = 1,
115 };
116 
117 static int si2157_attach(struct saa7164_port *port, struct i2c_adapter *adapter,
118 	struct dvb_frontend *fe, u8 addr8bit, struct si2157_config *cfg)
119 {
120 	struct i2c_board_info bi;
121 	struct i2c_client *tuner;
122 
123 	cfg->fe = fe;
124 
125 	memset(&bi, 0, sizeof(bi));
126 
127 	strlcpy(bi.type, "si2157", I2C_NAME_SIZE);
128 	bi.platform_data = cfg;
129 	bi.addr = addr8bit >> 1;
130 
131 	request_module(bi.type);
132 
133 	tuner = i2c_new_device(adapter, &bi);
134 	if (tuner == NULL || tuner->dev.driver == NULL)
135 		return -ENODEV;
136 
137 	if (!try_module_get(tuner->dev.driver->owner)) {
138 		i2c_unregister_device(tuner);
139 		return -ENODEV;
140 	}
141 
142 	port->i2c_client_tuner = tuner;
143 
144 	return 0;
145 }
146 
147 static int saa7164_dvb_stop_port(struct saa7164_port *port)
148 {
149 	struct saa7164_dev *dev = port->dev;
150 	int ret;
151 
152 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
153 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
154 		printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
155 			__func__, ret);
156 		ret = -EIO;
157 	} else {
158 		dprintk(DBGLVL_DVB, "%s()    Stopped\n", __func__);
159 		ret = 0;
160 	}
161 
162 	return ret;
163 }
164 
165 static int saa7164_dvb_acquire_port(struct saa7164_port *port)
166 {
167 	struct saa7164_dev *dev = port->dev;
168 	int ret;
169 
170 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
171 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
172 		printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
173 			__func__, ret);
174 		ret = -EIO;
175 	} else {
176 		dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
177 		ret = 0;
178 	}
179 
180 	return ret;
181 }
182 
183 static int saa7164_dvb_pause_port(struct saa7164_port *port)
184 {
185 	struct saa7164_dev *dev = port->dev;
186 	int ret;
187 
188 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
189 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
190 		printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
191 			__func__, ret);
192 		ret = -EIO;
193 	} else {
194 		dprintk(DBGLVL_DVB, "%s()   Paused\n", __func__);
195 		ret = 0;
196 	}
197 
198 	return ret;
199 }
200 
201 /* Firmware is very windows centric, meaning you have to transition
202  * the part through AVStream / KS Windows stages, forwards or backwards.
203  * States are: stopped, acquired (h/w), paused, started.
204  */
205 static int saa7164_dvb_stop_streaming(struct saa7164_port *port)
206 {
207 	struct saa7164_dev *dev = port->dev;
208 	struct saa7164_buffer *buf;
209 	struct list_head *p, *q;
210 	int ret;
211 
212 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
213 
214 	ret = saa7164_dvb_pause_port(port);
215 	ret = saa7164_dvb_acquire_port(port);
216 	ret = saa7164_dvb_stop_port(port);
217 
218 	/* Mark the hardware buffers as free */
219 	mutex_lock(&port->dmaqueue_lock);
220 	list_for_each_safe(p, q, &port->dmaqueue.list) {
221 		buf = list_entry(p, struct saa7164_buffer, list);
222 		buf->flags = SAA7164_BUFFER_FREE;
223 	}
224 	mutex_unlock(&port->dmaqueue_lock);
225 
226 	return ret;
227 }
228 
229 static int saa7164_dvb_start_port(struct saa7164_port *port)
230 {
231 	struct saa7164_dev *dev = port->dev;
232 	int ret = 0, result;
233 
234 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
235 
236 	saa7164_buffer_cfg_port(port);
237 
238 	/* Acquire the hardware */
239 	result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
240 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
241 		printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
242 			__func__, result);
243 
244 		/* Stop the hardware, regardless */
245 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
246 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
247 			printk(KERN_ERR "%s() acquire/forced stop transition "
248 				"failed, res = 0x%x\n", __func__, result);
249 		}
250 		ret = -EIO;
251 		goto out;
252 	} else
253 		dprintk(DBGLVL_DVB, "%s()   Acquired\n", __func__);
254 
255 	/* Pause the hardware */
256 	result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
257 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
258 		printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
259 				__func__, result);
260 
261 		/* Stop the hardware, regardless */
262 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
263 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
264 			printk(KERN_ERR "%s() pause/forced stop transition "
265 				"failed, res = 0x%x\n", __func__, result);
266 		}
267 
268 		ret = -EIO;
269 		goto out;
270 	} else
271 		dprintk(DBGLVL_DVB, "%s()   Paused\n", __func__);
272 
273 	/* Start the hardware */
274 	result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
275 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
276 		printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
277 				__func__, result);
278 
279 		/* Stop the hardware, regardless */
280 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
281 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
282 			printk(KERN_ERR "%s() run/forced stop transition "
283 				"failed, res = 0x%x\n", __func__, result);
284 		}
285 
286 		ret = -EIO;
287 	} else
288 		dprintk(DBGLVL_DVB, "%s()   Running\n", __func__);
289 
290 out:
291 	return ret;
292 }
293 
294 static int saa7164_dvb_start_feed(struct dvb_demux_feed *feed)
295 {
296 	struct dvb_demux *demux = feed->demux;
297 	struct saa7164_port *port = (struct saa7164_port *) demux->priv;
298 	struct saa7164_dvb *dvb = &port->dvb;
299 	struct saa7164_dev *dev = port->dev;
300 	int ret = 0;
301 
302 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
303 
304 	if (!demux->dmx.frontend)
305 		return -EINVAL;
306 
307 	if (dvb) {
308 		mutex_lock(&dvb->lock);
309 		if (dvb->feeding++ == 0) {
310 			/* Start transport */
311 			ret = saa7164_dvb_start_port(port);
312 		}
313 		mutex_unlock(&dvb->lock);
314 		dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
315 			__func__, port->nr, dvb->feeding);
316 	}
317 
318 	return ret;
319 }
320 
321 static int saa7164_dvb_stop_feed(struct dvb_demux_feed *feed)
322 {
323 	struct dvb_demux *demux = feed->demux;
324 	struct saa7164_port *port = (struct saa7164_port *) demux->priv;
325 	struct saa7164_dvb *dvb = &port->dvb;
326 	struct saa7164_dev *dev = port->dev;
327 	int ret = 0;
328 
329 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
330 
331 	if (dvb) {
332 		mutex_lock(&dvb->lock);
333 		if (--dvb->feeding == 0) {
334 			/* Stop transport */
335 			ret = saa7164_dvb_stop_streaming(port);
336 		}
337 		mutex_unlock(&dvb->lock);
338 		dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
339 			__func__, port->nr, dvb->feeding);
340 	}
341 
342 	return ret;
343 }
344 
345 static int dvb_register(struct saa7164_port *port)
346 {
347 	struct saa7164_dvb *dvb = &port->dvb;
348 	struct saa7164_dev *dev = port->dev;
349 	struct saa7164_buffer *buf;
350 	int result, i;
351 
352 	dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
353 
354 	if (port->type != SAA7164_MPEG_DVB)
355 		BUG();
356 
357 	/* Sanity check that the PCI configuration space is active */
358 	if (port->hwcfg.BARLocation == 0) {
359 		result = -ENOMEM;
360 		printk(KERN_ERR "%s: dvb_register_adapter failed "
361 		       "(errno = %d), NO PCI configuration\n",
362 			DRIVER_NAME, result);
363 		goto fail_adapter;
364 	}
365 
366 	/* Init and establish defaults */
367 	port->hw_streamingparams.bitspersample = 8;
368 	port->hw_streamingparams.samplesperline = 188;
369 	port->hw_streamingparams.numberoflines =
370 		(SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
371 
372 	port->hw_streamingparams.pitch = 188;
373 	port->hw_streamingparams.linethreshold = 0;
374 	port->hw_streamingparams.pagetablelistvirt = NULL;
375 	port->hw_streamingparams.pagetablelistphys = NULL;
376 	port->hw_streamingparams.numpagetables = 2 +
377 		((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
378 
379 	port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
380 
381 	/* Allocate the PCI resources */
382 	for (i = 0; i < port->hwcfg.buffercount; i++) {
383 		buf = saa7164_buffer_alloc(port,
384 			port->hw_streamingparams.numberoflines *
385 			port->hw_streamingparams.pitch);
386 
387 		if (!buf) {
388 			result = -ENOMEM;
389 			printk(KERN_ERR "%s: dvb_register_adapter failed "
390 			       "(errno = %d), unable to allocate buffers\n",
391 				DRIVER_NAME, result);
392 			goto fail_adapter;
393 		}
394 
395 		mutex_lock(&port->dmaqueue_lock);
396 		list_add_tail(&buf->list, &port->dmaqueue.list);
397 		mutex_unlock(&port->dmaqueue_lock);
398 	}
399 
400 	/* register adapter */
401 	result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
402 			&dev->pci->dev, adapter_nr);
403 	if (result < 0) {
404 		printk(KERN_ERR "%s: dvb_register_adapter failed "
405 		       "(errno = %d)\n", DRIVER_NAME, result);
406 		goto fail_adapter;
407 	}
408 	dvb->adapter.priv = port;
409 
410 	/* register frontend */
411 	result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
412 	if (result < 0) {
413 		printk(KERN_ERR "%s: dvb_register_frontend failed "
414 		       "(errno = %d)\n", DRIVER_NAME, result);
415 		goto fail_frontend;
416 	}
417 
418 	/* register demux stuff */
419 	dvb->demux.dmx.capabilities =
420 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
421 		DMX_MEMORY_BASED_FILTERING;
422 	dvb->demux.priv       = port;
423 	dvb->demux.filternum  = 256;
424 	dvb->demux.feednum    = 256;
425 	dvb->demux.start_feed = saa7164_dvb_start_feed;
426 	dvb->demux.stop_feed  = saa7164_dvb_stop_feed;
427 	result = dvb_dmx_init(&dvb->demux);
428 	if (result < 0) {
429 		printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
430 		       DRIVER_NAME, result);
431 		goto fail_dmx;
432 	}
433 
434 	dvb->dmxdev.filternum    = 256;
435 	dvb->dmxdev.demux        = &dvb->demux.dmx;
436 	dvb->dmxdev.capabilities = 0;
437 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
438 	if (result < 0) {
439 		printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
440 		       DRIVER_NAME, result);
441 		goto fail_dmxdev;
442 	}
443 
444 	dvb->fe_hw.source = DMX_FRONTEND_0;
445 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
446 	if (result < 0) {
447 		printk(KERN_ERR "%s: add_frontend failed "
448 		       "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
449 		goto fail_fe_hw;
450 	}
451 
452 	dvb->fe_mem.source = DMX_MEMORY_FE;
453 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
454 	if (result < 0) {
455 		printk(KERN_ERR "%s: add_frontend failed "
456 		       "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
457 		goto fail_fe_mem;
458 	}
459 
460 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
461 	if (result < 0) {
462 		printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
463 		       DRIVER_NAME, result);
464 		goto fail_fe_conn;
465 	}
466 
467 	/* register network adapter */
468 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
469 	return 0;
470 
471 fail_fe_conn:
472 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
473 fail_fe_mem:
474 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
475 fail_fe_hw:
476 	dvb_dmxdev_release(&dvb->dmxdev);
477 fail_dmxdev:
478 	dvb_dmx_release(&dvb->demux);
479 fail_dmx:
480 	dvb_unregister_frontend(dvb->frontend);
481 fail_frontend:
482 	dvb_frontend_detach(dvb->frontend);
483 	dvb_unregister_adapter(&dvb->adapter);
484 fail_adapter:
485 	return result;
486 }
487 
488 int saa7164_dvb_unregister(struct saa7164_port *port)
489 {
490 	struct saa7164_dvb *dvb = &port->dvb;
491 	struct saa7164_dev *dev = port->dev;
492 	struct saa7164_buffer *b;
493 	struct list_head *c, *n;
494 	struct i2c_client *client;
495 
496 	dprintk(DBGLVL_DVB, "%s()\n", __func__);
497 
498 	if (port->type != SAA7164_MPEG_DVB)
499 		BUG();
500 
501 	/* Remove any allocated buffers */
502 	mutex_lock(&port->dmaqueue_lock);
503 	list_for_each_safe(c, n, &port->dmaqueue.list) {
504 		b = list_entry(c, struct saa7164_buffer, list);
505 		list_del(c);
506 		saa7164_buffer_dealloc(b);
507 	}
508 	mutex_unlock(&port->dmaqueue_lock);
509 
510 	if (dvb->frontend == NULL)
511 		return 0;
512 
513 	/* remove I2C client for tuner */
514 	client = port->i2c_client_tuner;
515 	if (client) {
516 		module_put(client->dev.driver->owner);
517 		i2c_unregister_device(client);
518 	}
519 
520 	/* remove I2C client for demodulator */
521 	client = port->i2c_client_demod;
522 	if (client) {
523 		module_put(client->dev.driver->owner);
524 		i2c_unregister_device(client);
525 	}
526 
527 	dvb_net_release(&dvb->net);
528 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
529 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
530 	dvb_dmxdev_release(&dvb->dmxdev);
531 	dvb_dmx_release(&dvb->demux);
532 	dvb_unregister_frontend(dvb->frontend);
533 	dvb_frontend_detach(dvb->frontend);
534 	dvb_unregister_adapter(&dvb->adapter);
535 	return 0;
536 }
537 
538 /* All the DVB attach calls go here, this function get's modified
539  * for each new card.
540  */
541 int saa7164_dvb_register(struct saa7164_port *port)
542 {
543 	struct saa7164_dev *dev = port->dev;
544 	struct saa7164_dvb *dvb = &port->dvb;
545 	struct saa7164_i2c *i2c_bus = NULL;
546 	struct si2168_config si2168_config;
547 	struct si2157_config si2157_config;
548 	struct i2c_adapter *adapter;
549 	struct i2c_board_info info;
550 	struct i2c_client *client_demod;
551 	struct i2c_client *client_tuner;
552 	int ret;
553 
554 	dprintk(DBGLVL_DVB, "%s()\n", __func__);
555 
556 	/* init frontend */
557 	switch (dev->board) {
558 	case SAA7164_BOARD_HAUPPAUGE_HVR2200:
559 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
560 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
561 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
562 	case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
563 		i2c_bus = &dev->i2c_bus[port->nr + 1];
564 		switch (port->nr) {
565 		case 0:
566 			port->dvb.frontend = dvb_attach(tda10048_attach,
567 				&hauppauge_hvr2200_1_config,
568 				&i2c_bus->i2c_adap);
569 
570 			if (port->dvb.frontend != NULL) {
571 				/* TODO: addr is in the card struct */
572 				dvb_attach(tda18271_attach, port->dvb.frontend,
573 					0xc0 >> 1, &i2c_bus->i2c_adap,
574 					&hauppauge_hvr22x0_tuner_config);
575 			}
576 
577 			break;
578 		case 1:
579 			port->dvb.frontend = dvb_attach(tda10048_attach,
580 				&hauppauge_hvr2200_2_config,
581 				&i2c_bus->i2c_adap);
582 
583 			if (port->dvb.frontend != NULL) {
584 				/* TODO: addr is in the card struct */
585 				dvb_attach(tda18271_attach, port->dvb.frontend,
586 					0xc0 >> 1, &i2c_bus->i2c_adap,
587 					&hauppauge_hvr22x0s_tuner_config);
588 			}
589 
590 			break;
591 		}
592 		break;
593 	case SAA7164_BOARD_HAUPPAUGE_HVR2250:
594 	case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
595 	case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
596 		i2c_bus = &dev->i2c_bus[port->nr + 1];
597 
598 		port->dvb.frontend = dvb_attach(s5h1411_attach,
599 			&hauppauge_s5h1411_config,
600 			&i2c_bus->i2c_adap);
601 
602 		if (port->dvb.frontend != NULL) {
603 			if (port->nr == 0) {
604 				/* Master TDA18271 */
605 				/* TODO: addr is in the card struct */
606 				dvb_attach(tda18271_attach, port->dvb.frontend,
607 					0xc0 >> 1, &i2c_bus->i2c_adap,
608 					&hauppauge_hvr22x0_tuner_config);
609 			} else {
610 				/* Slave TDA18271 */
611 				dvb_attach(tda18271_attach, port->dvb.frontend,
612 					0xc0 >> 1, &i2c_bus->i2c_adap,
613 					&hauppauge_hvr22x0s_tuner_config);
614 			}
615 		}
616 
617 		break;
618 	case SAA7164_BOARD_HAUPPAUGE_HVR2255proto:
619 	case SAA7164_BOARD_HAUPPAUGE_HVR2255:
620 		i2c_bus = &dev->i2c_bus[2];
621 
622 		if (port->nr == 0) {
623 			port->dvb.frontend = dvb_attach(lgdt3306a_attach,
624 				&hauppauge_hvr2255a_config, &i2c_bus->i2c_adap);
625 		} else {
626 			port->dvb.frontend = dvb_attach(lgdt3306a_attach,
627 				&hauppauge_hvr2255b_config, &i2c_bus->i2c_adap);
628 		}
629 
630 		if (port->dvb.frontend != NULL) {
631 
632 			if (port->nr == 0) {
633 				si2157_attach(port, &dev->i2c_bus[0].i2c_adap,
634 					      port->dvb.frontend, 0xc0,
635 					      &hauppauge_hvr2255_tuner_config);
636 			} else {
637 				si2157_attach(port, &dev->i2c_bus[1].i2c_adap,
638 					      port->dvb.frontend, 0xc0,
639 					      &hauppauge_hvr2255_tuner_config);
640 			}
641 		}
642 		break;
643 	case SAA7164_BOARD_HAUPPAUGE_HVR2205:
644 
645 		if (port->nr == 0) {
646 			/* attach frontend */
647 			memset(&si2168_config, 0, sizeof(si2168_config));
648 			si2168_config.i2c_adapter = &adapter;
649 			si2168_config.fe = &port->dvb.frontend;
650 			si2168_config.ts_mode = SI2168_TS_SERIAL;
651 			memset(&info, 0, sizeof(struct i2c_board_info));
652 			strlcpy(info.type, "si2168", I2C_NAME_SIZE);
653 			info.addr = 0xc8 >> 1;
654 			info.platform_data = &si2168_config;
655 			request_module(info.type);
656 			client_demod = i2c_new_device(&dev->i2c_bus[2].i2c_adap,
657 						      &info);
658 			if (!client_demod || !client_demod->dev.driver)
659 				goto frontend_detach;
660 
661 			if (!try_module_get(client_demod->dev.driver->owner)) {
662 				i2c_unregister_device(client_demod);
663 				goto frontend_detach;
664 			}
665 			port->i2c_client_demod = client_demod;
666 
667 			/* attach tuner */
668 			memset(&si2157_config, 0, sizeof(si2157_config));
669 			si2157_config.if_port = 1;
670 			si2157_config.fe = port->dvb.frontend;
671 			memset(&info, 0, sizeof(struct i2c_board_info));
672 			strlcpy(info.type, "si2157", I2C_NAME_SIZE);
673 			info.addr = 0xc0 >> 1;
674 			info.platform_data = &si2157_config;
675 			request_module(info.type);
676 			client_tuner = i2c_new_device(&dev->i2c_bus[0].i2c_adap,
677 						      &info);
678 			if (!client_tuner || !client_tuner->dev.driver) {
679 				module_put(client_demod->dev.driver->owner);
680 				i2c_unregister_device(client_demod);
681 				goto frontend_detach;
682 			}
683 			if (!try_module_get(client_tuner->dev.driver->owner)) {
684 				i2c_unregister_device(client_tuner);
685 				module_put(client_demod->dev.driver->owner);
686 				i2c_unregister_device(client_demod);
687 				goto frontend_detach;
688 			}
689 			port->i2c_client_tuner = client_tuner;
690 		} else {
691 			/* attach frontend */
692 			memset(&si2168_config, 0, sizeof(si2168_config));
693 			si2168_config.i2c_adapter = &adapter;
694 			si2168_config.fe = &port->dvb.frontend;
695 			si2168_config.ts_mode = SI2168_TS_SERIAL;
696 			memset(&info, 0, sizeof(struct i2c_board_info));
697 			strlcpy(info.type, "si2168", I2C_NAME_SIZE);
698 			info.addr = 0xcc >> 1;
699 			info.platform_data = &si2168_config;
700 			request_module(info.type);
701 			client_demod = i2c_new_device(&dev->i2c_bus[2].i2c_adap,
702 						      &info);
703 			if (!client_demod || !client_demod->dev.driver)
704 				goto frontend_detach;
705 
706 			if (!try_module_get(client_demod->dev.driver->owner)) {
707 				i2c_unregister_device(client_demod);
708 				goto frontend_detach;
709 			}
710 			port->i2c_client_demod = client_demod;
711 
712 			/* attach tuner */
713 			memset(&si2157_config, 0, sizeof(si2157_config));
714 			si2157_config.fe = port->dvb.frontend;
715 			si2157_config.if_port = 1;
716 			memset(&info, 0, sizeof(struct i2c_board_info));
717 			strlcpy(info.type, "si2157", I2C_NAME_SIZE);
718 			info.addr = 0xc0 >> 1;
719 			info.platform_data = &si2157_config;
720 			request_module(info.type);
721 			client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap,
722 						      &info);
723 			if (!client_tuner || !client_tuner->dev.driver) {
724 				module_put(client_demod->dev.driver->owner);
725 				i2c_unregister_device(client_demod);
726 				goto frontend_detach;
727 			}
728 			if (!try_module_get(client_tuner->dev.driver->owner)) {
729 				i2c_unregister_device(client_tuner);
730 				module_put(client_demod->dev.driver->owner);
731 				i2c_unregister_device(client_demod);
732 				goto frontend_detach;
733 			}
734 			port->i2c_client_tuner = client_tuner;
735 		}
736 
737 		break;
738 	default:
739 		printk(KERN_ERR "%s: The frontend isn't supported\n",
740 		       dev->name);
741 		break;
742 	}
743 	if (NULL == dvb->frontend) {
744 		printk(KERN_ERR "%s() Frontend initialization failed\n",
745 		       __func__);
746 		return -1;
747 	}
748 
749 	/* register everything */
750 	ret = dvb_register(port);
751 	if (ret < 0) {
752 		if (dvb->frontend->ops.release)
753 			dvb->frontend->ops.release(dvb->frontend);
754 		return ret;
755 	}
756 
757 	return 0;
758 
759 frontend_detach:
760 	printk(KERN_ERR "%s() Frontend/I2C initialization failed\n", __func__);
761 	return -1;
762 }
763 
764