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