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