1 /*
2  * Copyright (C) 2010 Michael Krufky (mkrufky@kernellabs.com)
3  *
4  *   This program is free software; you can redistribute it and/or modify it
5  *   under the terms of the GNU General Public License as published by the Free
6  *   Software Foundation, version 2.
7  *
8  * see Documentation/dvb/README.dvb-usb for more information
9  */
10 
11 #include <linux/vmalloc.h>
12 #include <linux/i2c.h>
13 
14 #include "mxl111sf.h"
15 #include "mxl111sf-reg.h"
16 #include "mxl111sf-phy.h"
17 #include "mxl111sf-i2c.h"
18 #include "mxl111sf-gpio.h"
19 
20 #include "mxl111sf-demod.h"
21 #include "mxl111sf-tuner.h"
22 
23 #include "lgdt3305.h"
24 #include "lg2160.h"
25 
26 int dvb_usb_mxl111sf_debug;
27 module_param_named(debug, dvb_usb_mxl111sf_debug, int, 0644);
28 MODULE_PARM_DESC(debug, "set debugging level "
29 		 "(1=info, 2=xfer, 4=i2c, 8=reg, 16=adv (or-able)).");
30 
31 int dvb_usb_mxl111sf_isoc;
32 module_param_named(isoc, dvb_usb_mxl111sf_isoc, int, 0644);
33 MODULE_PARM_DESC(isoc, "enable usb isoc xfer (0=bulk, 1=isoc).");
34 
35 int dvb_usb_mxl111sf_spi;
36 module_param_named(spi, dvb_usb_mxl111sf_spi, int, 0644);
37 MODULE_PARM_DESC(spi, "use spi rather than tp for data xfer (0=tp, 1=spi).");
38 
39 #define ANT_PATH_AUTO 0
40 #define ANT_PATH_EXTERNAL 1
41 #define ANT_PATH_INTERNAL 2
42 
43 int dvb_usb_mxl111sf_rfswitch =
44 #if 0
45 		ANT_PATH_AUTO;
46 #else
47 		ANT_PATH_EXTERNAL;
48 #endif
49 
50 module_param_named(rfswitch, dvb_usb_mxl111sf_rfswitch, int, 0644);
51 MODULE_PARM_DESC(rfswitch, "force rf switch position (0=auto, 1=ext, 2=int).");
52 
53 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
54 
55 int mxl111sf_ctrl_msg(struct dvb_usb_device *d,
56 		      u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
57 {
58 	int wo = (rbuf == NULL || rlen == 0); /* write-only */
59 	int ret;
60 	u8 sndbuf[1+wlen];
61 
62 	pr_debug("%s(wlen = %d, rlen = %d)\n", __func__, wlen, rlen);
63 
64 	memset(sndbuf, 0, 1+wlen);
65 
66 	sndbuf[0] = cmd;
67 	memcpy(&sndbuf[1], wbuf, wlen);
68 
69 	ret = (wo) ? dvb_usbv2_generic_write(d, sndbuf, 1+wlen) :
70 		dvb_usbv2_generic_rw(d, sndbuf, 1+wlen, rbuf, rlen);
71 	mxl_fail(ret);
72 
73 	return ret;
74 }
75 
76 /* ------------------------------------------------------------------------ */
77 
78 #define MXL_CMD_REG_READ	0xaa
79 #define MXL_CMD_REG_WRITE	0x55
80 
81 int mxl111sf_read_reg(struct mxl111sf_state *state, u8 addr, u8 *data)
82 {
83 	u8 buf[2];
84 	int ret;
85 
86 	ret = mxl111sf_ctrl_msg(state->d, MXL_CMD_REG_READ, &addr, 1, buf, 2);
87 	if (mxl_fail(ret)) {
88 		mxl_debug("error reading reg: 0x%02x", addr);
89 		goto fail;
90 	}
91 
92 	if (buf[0] == addr)
93 		*data = buf[1];
94 	else {
95 		pr_err("invalid response reading reg: 0x%02x != 0x%02x, 0x%02x",
96 		    addr, buf[0], buf[1]);
97 		ret = -EINVAL;
98 	}
99 
100 	pr_debug("R: (0x%02x, 0x%02x)\n", addr, *data);
101 fail:
102 	return ret;
103 }
104 
105 int mxl111sf_write_reg(struct mxl111sf_state *state, u8 addr, u8 data)
106 {
107 	u8 buf[] = { addr, data };
108 	int ret;
109 
110 	pr_debug("W: (0x%02x, 0x%02x)\n", addr, data);
111 
112 	ret = mxl111sf_ctrl_msg(state->d, MXL_CMD_REG_WRITE, buf, 2, NULL, 0);
113 	if (mxl_fail(ret))
114 		pr_err("error writing reg: 0x%02x, val: 0x%02x", addr, data);
115 	return ret;
116 }
117 
118 /* ------------------------------------------------------------------------ */
119 
120 int mxl111sf_write_reg_mask(struct mxl111sf_state *state,
121 				   u8 addr, u8 mask, u8 data)
122 {
123 	int ret;
124 	u8 val;
125 
126 	if (mask != 0xff) {
127 		ret = mxl111sf_read_reg(state, addr, &val);
128 #if 1
129 		/* dont know why this usually errors out on the first try */
130 		if (mxl_fail(ret))
131 			pr_err("error writing addr: 0x%02x, mask: 0x%02x, "
132 			    "data: 0x%02x, retrying...", addr, mask, data);
133 
134 		ret = mxl111sf_read_reg(state, addr, &val);
135 #endif
136 		if (mxl_fail(ret))
137 			goto fail;
138 	}
139 	val &= ~mask;
140 	val |= data;
141 
142 	ret = mxl111sf_write_reg(state, addr, val);
143 	mxl_fail(ret);
144 fail:
145 	return ret;
146 }
147 
148 /* ------------------------------------------------------------------------ */
149 
150 int mxl111sf_ctrl_program_regs(struct mxl111sf_state *state,
151 			       struct mxl111sf_reg_ctrl_info *ctrl_reg_info)
152 {
153 	int i, ret = 0;
154 
155 	for (i = 0;  ctrl_reg_info[i].addr |
156 		     ctrl_reg_info[i].mask |
157 		     ctrl_reg_info[i].data;  i++) {
158 
159 		ret = mxl111sf_write_reg_mask(state,
160 					      ctrl_reg_info[i].addr,
161 					      ctrl_reg_info[i].mask,
162 					      ctrl_reg_info[i].data);
163 		if (mxl_fail(ret)) {
164 			pr_err("failed on reg #%d (0x%02x)", i,
165 			    ctrl_reg_info[i].addr);
166 			break;
167 		}
168 	}
169 	return ret;
170 }
171 
172 /* ------------------------------------------------------------------------ */
173 
174 static int mxl1x1sf_get_chip_info(struct mxl111sf_state *state)
175 {
176 	int ret;
177 	u8 id, ver;
178 	char *mxl_chip, *mxl_rev;
179 
180 	if ((state->chip_id) && (state->chip_ver))
181 		return 0;
182 
183 	ret = mxl111sf_read_reg(state, CHIP_ID_REG, &id);
184 	if (mxl_fail(ret))
185 		goto fail;
186 	state->chip_id = id;
187 
188 	ret = mxl111sf_read_reg(state, TOP_CHIP_REV_ID_REG, &ver);
189 	if (mxl_fail(ret))
190 		goto fail;
191 	state->chip_ver = ver;
192 
193 	switch (id) {
194 	case 0x61:
195 		mxl_chip = "MxL101SF";
196 		break;
197 	case 0x63:
198 		mxl_chip = "MxL111SF";
199 		break;
200 	default:
201 		mxl_chip = "UNKNOWN MxL1X1";
202 		break;
203 	}
204 	switch (ver) {
205 	case 0x36:
206 		state->chip_rev = MXL111SF_V6;
207 		mxl_rev = "v6";
208 		break;
209 	case 0x08:
210 		state->chip_rev = MXL111SF_V8_100;
211 		mxl_rev = "v8_100";
212 		break;
213 	case 0x18:
214 		state->chip_rev = MXL111SF_V8_200;
215 		mxl_rev = "v8_200";
216 		break;
217 	default:
218 		state->chip_rev = 0;
219 		mxl_rev = "UNKNOWN REVISION";
220 		break;
221 	}
222 	pr_info("%s detected, %s (0x%x)", mxl_chip, mxl_rev, ver);
223 fail:
224 	return ret;
225 }
226 
227 #define get_chip_info(state)						\
228 ({									\
229 	int ___ret;							\
230 	___ret = mxl1x1sf_get_chip_info(state);				\
231 	if (mxl_fail(___ret)) {						\
232 		mxl_debug("failed to get chip info"			\
233 			  " on first probe attempt");			\
234 		___ret = mxl1x1sf_get_chip_info(state);			\
235 		if (mxl_fail(___ret))					\
236 			pr_err("failed to get chip info during probe");	\
237 		else							\
238 			mxl_debug("probe needed a retry "		\
239 				  "in order to succeed.");		\
240 	}								\
241 	___ret;								\
242 })
243 
244 /* ------------------------------------------------------------------------ */
245 #if 0
246 static int mxl111sf_power_ctrl(struct dvb_usb_device *d, int onoff)
247 {
248 	/* power control depends on which adapter is being woken:
249 	 * save this for init, instead, via mxl111sf_adap_fe_init */
250 	return 0;
251 }
252 #endif
253 
254 static int mxl111sf_adap_fe_init(struct dvb_frontend *fe)
255 {
256 	struct dvb_usb_device *d = fe_to_d(fe);
257 	struct mxl111sf_state *state = fe_to_priv(fe);
258 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
259 	int err;
260 
261 	/* exit if we didnt initialize the driver yet */
262 	if (!state->chip_id) {
263 		mxl_debug("driver not yet initialized, exit.");
264 		goto fail;
265 	}
266 
267 	pr_debug("%s()\n", __func__);
268 
269 	mutex_lock(&state->fe_lock);
270 
271 	state->alt_mode = adap_state->alt_mode;
272 
273 	if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
274 		pr_err("set interface failed");
275 
276 	err = mxl1x1sf_soft_reset(state);
277 	mxl_fail(err);
278 	err = mxl111sf_init_tuner_demod(state);
279 	mxl_fail(err);
280 	err = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
281 
282 	mxl_fail(err);
283 	mxl111sf_enable_usb_output(state);
284 	mxl_fail(err);
285 	mxl1x1sf_top_master_ctrl(state, 1);
286 	mxl_fail(err);
287 
288 	if ((MXL111SF_GPIO_MOD_DVBT != adap_state->gpio_mode) &&
289 	    (state->chip_rev > MXL111SF_V6)) {
290 		mxl111sf_config_pin_mux_modes(state,
291 					      PIN_MUX_TS_SPI_IN_MODE_1);
292 		mxl_fail(err);
293 	}
294 	err = mxl111sf_init_port_expander(state);
295 	if (!mxl_fail(err)) {
296 		state->gpio_mode = adap_state->gpio_mode;
297 		err = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
298 		mxl_fail(err);
299 #if 0
300 		err = fe->ops.init(fe);
301 #endif
302 		msleep(100); /* add short delay after enabling
303 			      * the demod before touching it */
304 	}
305 
306 	return (adap_state->fe_init) ? adap_state->fe_init(fe) : 0;
307 fail:
308 	return -ENODEV;
309 }
310 
311 static int mxl111sf_adap_fe_sleep(struct dvb_frontend *fe)
312 {
313 	struct mxl111sf_state *state = fe_to_priv(fe);
314 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
315 	int err;
316 
317 	/* exit if we didnt initialize the driver yet */
318 	if (!state->chip_id) {
319 		mxl_debug("driver not yet initialized, exit.");
320 		goto fail;
321 	}
322 
323 	pr_debug("%s()\n", __func__);
324 
325 	err = (adap_state->fe_sleep) ? adap_state->fe_sleep(fe) : 0;
326 
327 	mutex_unlock(&state->fe_lock);
328 
329 	return err;
330 fail:
331 	return -ENODEV;
332 }
333 
334 
335 static int mxl111sf_ep6_streaming_ctrl(struct dvb_frontend *fe, int onoff)
336 {
337 	struct mxl111sf_state *state = fe_to_priv(fe);
338 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
339 	int ret = 0;
340 
341 	pr_debug("%s(%d)\n", __func__, onoff);
342 
343 	if (onoff) {
344 		ret = mxl111sf_enable_usb_output(state);
345 		mxl_fail(ret);
346 		ret = mxl111sf_config_mpeg_in(state, 1, 1,
347 					      adap_state->ep6_clockphase,
348 					      0, 0);
349 		mxl_fail(ret);
350 #if 0
351 	} else {
352 		ret = mxl111sf_disable_656_port(state);
353 		mxl_fail(ret);
354 #endif
355 	}
356 
357 	return ret;
358 }
359 
360 static int mxl111sf_ep5_streaming_ctrl(struct dvb_frontend *fe, int onoff)
361 {
362 	struct mxl111sf_state *state = fe_to_priv(fe);
363 	int ret = 0;
364 
365 	pr_debug("%s(%d)\n", __func__, onoff);
366 
367 	if (onoff) {
368 		ret = mxl111sf_enable_usb_output(state);
369 		mxl_fail(ret);
370 
371 		ret = mxl111sf_init_i2s_port(state, 200);
372 		mxl_fail(ret);
373 		ret = mxl111sf_config_i2s(state, 0, 15);
374 		mxl_fail(ret);
375 	} else {
376 		ret = mxl111sf_disable_i2s_port(state);
377 		mxl_fail(ret);
378 	}
379 	if (state->chip_rev > MXL111SF_V6)
380 		ret = mxl111sf_config_spi(state, onoff);
381 	mxl_fail(ret);
382 
383 	return ret;
384 }
385 
386 static int mxl111sf_ep4_streaming_ctrl(struct dvb_frontend *fe, int onoff)
387 {
388 	struct mxl111sf_state *state = fe_to_priv(fe);
389 	int ret = 0;
390 
391 	pr_debug("%s(%d)\n", __func__, onoff);
392 
393 	if (onoff) {
394 		ret = mxl111sf_enable_usb_output(state);
395 		mxl_fail(ret);
396 	}
397 
398 	return ret;
399 }
400 
401 /* ------------------------------------------------------------------------ */
402 
403 static struct lgdt3305_config hauppauge_lgdt3305_config = {
404 	.i2c_addr           = 0xb2 >> 1,
405 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
406 	.tpclk_edge         = LGDT3305_TPCLK_RISING_EDGE,
407 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
408 	.deny_i2c_rptr      = 1,
409 	.spectral_inversion = 0,
410 	.qam_if_khz         = 6000,
411 	.vsb_if_khz         = 6000,
412 };
413 
414 static int mxl111sf_lgdt3305_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
415 {
416 	struct dvb_usb_device *d = adap_to_d(adap);
417 	struct mxl111sf_state *state = d_to_priv(d);
418 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
419 	int ret;
420 
421 	pr_debug("%s()\n", __func__);
422 
423 	/* save a pointer to the dvb_usb_device in device state */
424 	state->d = d;
425 	adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
426 	state->alt_mode = adap_state->alt_mode;
427 
428 	if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
429 		pr_err("set interface failed");
430 
431 	state->gpio_mode = MXL111SF_GPIO_MOD_ATSC;
432 	adap_state->gpio_mode = state->gpio_mode;
433 	adap_state->device_mode = MXL_TUNER_MODE;
434 	adap_state->ep6_clockphase = 1;
435 
436 	ret = mxl1x1sf_soft_reset(state);
437 	if (mxl_fail(ret))
438 		goto fail;
439 	ret = mxl111sf_init_tuner_demod(state);
440 	if (mxl_fail(ret))
441 		goto fail;
442 
443 	ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
444 	if (mxl_fail(ret))
445 		goto fail;
446 
447 	ret = mxl111sf_enable_usb_output(state);
448 	if (mxl_fail(ret))
449 		goto fail;
450 	ret = mxl1x1sf_top_master_ctrl(state, 1);
451 	if (mxl_fail(ret))
452 		goto fail;
453 
454 	ret = mxl111sf_init_port_expander(state);
455 	if (mxl_fail(ret))
456 		goto fail;
457 	ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
458 	if (mxl_fail(ret))
459 		goto fail;
460 
461 	adap->fe[fe_id] = dvb_attach(lgdt3305_attach,
462 				 &hauppauge_lgdt3305_config,
463 				 &d->i2c_adap);
464 	if (adap->fe[fe_id]) {
465 		state->num_frontends++;
466 		adap_state->fe_init = adap->fe[fe_id]->ops.init;
467 		adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
468 		adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
469 		adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
470 		return 0;
471 	}
472 	ret = -EIO;
473 fail:
474 	return ret;
475 }
476 
477 static struct lg2160_config hauppauge_lg2160_config = {
478 	.lg_chip            = LG2160,
479 	.i2c_addr           = 0x1c >> 1,
480 	.deny_i2c_rptr      = 1,
481 	.spectral_inversion = 0,
482 	.if_khz             = 6000,
483 };
484 
485 static int mxl111sf_lg2160_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
486 {
487 	struct dvb_usb_device *d = adap_to_d(adap);
488 	struct mxl111sf_state *state = d_to_priv(d);
489 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
490 	int ret;
491 
492 	pr_debug("%s()\n", __func__);
493 
494 	/* save a pointer to the dvb_usb_device in device state */
495 	state->d = d;
496 	adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
497 	state->alt_mode = adap_state->alt_mode;
498 
499 	if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
500 		pr_err("set interface failed");
501 
502 	state->gpio_mode = MXL111SF_GPIO_MOD_MH;
503 	adap_state->gpio_mode = state->gpio_mode;
504 	adap_state->device_mode = MXL_TUNER_MODE;
505 	adap_state->ep6_clockphase = 1;
506 
507 	ret = mxl1x1sf_soft_reset(state);
508 	if (mxl_fail(ret))
509 		goto fail;
510 	ret = mxl111sf_init_tuner_demod(state);
511 	if (mxl_fail(ret))
512 		goto fail;
513 
514 	ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
515 	if (mxl_fail(ret))
516 		goto fail;
517 
518 	ret = mxl111sf_enable_usb_output(state);
519 	if (mxl_fail(ret))
520 		goto fail;
521 	ret = mxl1x1sf_top_master_ctrl(state, 1);
522 	if (mxl_fail(ret))
523 		goto fail;
524 
525 	ret = mxl111sf_init_port_expander(state);
526 	if (mxl_fail(ret))
527 		goto fail;
528 	ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
529 	if (mxl_fail(ret))
530 		goto fail;
531 
532 	ret = get_chip_info(state);
533 	if (mxl_fail(ret))
534 		goto fail;
535 
536 	adap->fe[fe_id] = dvb_attach(lg2160_attach,
537 			      &hauppauge_lg2160_config,
538 			      &d->i2c_adap);
539 	if (adap->fe[fe_id]) {
540 		state->num_frontends++;
541 		adap_state->fe_init = adap->fe[fe_id]->ops.init;
542 		adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
543 		adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
544 		adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
545 		return 0;
546 	}
547 	ret = -EIO;
548 fail:
549 	return ret;
550 }
551 
552 static struct lg2160_config hauppauge_lg2161_1019_config = {
553 	.lg_chip            = LG2161_1019,
554 	.i2c_addr           = 0x1c >> 1,
555 	.deny_i2c_rptr      = 1,
556 	.spectral_inversion = 0,
557 	.if_khz             = 6000,
558 	.output_if          = 2, /* LG2161_OIF_SPI_MAS */
559 };
560 
561 static struct lg2160_config hauppauge_lg2161_1040_config = {
562 	.lg_chip            = LG2161_1040,
563 	.i2c_addr           = 0x1c >> 1,
564 	.deny_i2c_rptr      = 1,
565 	.spectral_inversion = 0,
566 	.if_khz             = 6000,
567 	.output_if          = 4, /* LG2161_OIF_SPI_MAS */
568 };
569 
570 static int mxl111sf_lg2161_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
571 {
572 	struct dvb_usb_device *d = adap_to_d(adap);
573 	struct mxl111sf_state *state = d_to_priv(d);
574 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
575 	int ret;
576 
577 	pr_debug("%s()\n", __func__);
578 
579 	/* save a pointer to the dvb_usb_device in device state */
580 	state->d = d;
581 	adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
582 	state->alt_mode = adap_state->alt_mode;
583 
584 	if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
585 		pr_err("set interface failed");
586 
587 	state->gpio_mode = MXL111SF_GPIO_MOD_MH;
588 	adap_state->gpio_mode = state->gpio_mode;
589 	adap_state->device_mode = MXL_TUNER_MODE;
590 	adap_state->ep6_clockphase = 1;
591 
592 	ret = mxl1x1sf_soft_reset(state);
593 	if (mxl_fail(ret))
594 		goto fail;
595 	ret = mxl111sf_init_tuner_demod(state);
596 	if (mxl_fail(ret))
597 		goto fail;
598 
599 	ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
600 	if (mxl_fail(ret))
601 		goto fail;
602 
603 	ret = mxl111sf_enable_usb_output(state);
604 	if (mxl_fail(ret))
605 		goto fail;
606 	ret = mxl1x1sf_top_master_ctrl(state, 1);
607 	if (mxl_fail(ret))
608 		goto fail;
609 
610 	ret = mxl111sf_init_port_expander(state);
611 	if (mxl_fail(ret))
612 		goto fail;
613 	ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
614 	if (mxl_fail(ret))
615 		goto fail;
616 
617 	ret = get_chip_info(state);
618 	if (mxl_fail(ret))
619 		goto fail;
620 
621 	adap->fe[fe_id] = dvb_attach(lg2160_attach,
622 			      (MXL111SF_V8_200 == state->chip_rev) ?
623 			      &hauppauge_lg2161_1040_config :
624 			      &hauppauge_lg2161_1019_config,
625 			      &d->i2c_adap);
626 	if (adap->fe[fe_id]) {
627 		state->num_frontends++;
628 		adap_state->fe_init = adap->fe[fe_id]->ops.init;
629 		adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
630 		adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
631 		adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
632 		return 0;
633 	}
634 	ret = -EIO;
635 fail:
636 	return ret;
637 }
638 
639 static struct lg2160_config hauppauge_lg2161_1019_ep6_config = {
640 	.lg_chip            = LG2161_1019,
641 	.i2c_addr           = 0x1c >> 1,
642 	.deny_i2c_rptr      = 1,
643 	.spectral_inversion = 0,
644 	.if_khz             = 6000,
645 	.output_if          = 1, /* LG2161_OIF_SERIAL_TS */
646 };
647 
648 static struct lg2160_config hauppauge_lg2161_1040_ep6_config = {
649 	.lg_chip            = LG2161_1040,
650 	.i2c_addr           = 0x1c >> 1,
651 	.deny_i2c_rptr      = 1,
652 	.spectral_inversion = 0,
653 	.if_khz             = 6000,
654 	.output_if          = 7, /* LG2161_OIF_SERIAL_TS */
655 };
656 
657 static int mxl111sf_lg2161_ep6_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
658 {
659 	struct dvb_usb_device *d = adap_to_d(adap);
660 	struct mxl111sf_state *state = d_to_priv(d);
661 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
662 	int ret;
663 
664 	pr_debug("%s()\n", __func__);
665 
666 	/* save a pointer to the dvb_usb_device in device state */
667 	state->d = d;
668 	adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
669 	state->alt_mode = adap_state->alt_mode;
670 
671 	if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
672 		pr_err("set interface failed");
673 
674 	state->gpio_mode = MXL111SF_GPIO_MOD_MH;
675 	adap_state->gpio_mode = state->gpio_mode;
676 	adap_state->device_mode = MXL_TUNER_MODE;
677 	adap_state->ep6_clockphase = 0;
678 
679 	ret = mxl1x1sf_soft_reset(state);
680 	if (mxl_fail(ret))
681 		goto fail;
682 	ret = mxl111sf_init_tuner_demod(state);
683 	if (mxl_fail(ret))
684 		goto fail;
685 
686 	ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
687 	if (mxl_fail(ret))
688 		goto fail;
689 
690 	ret = mxl111sf_enable_usb_output(state);
691 	if (mxl_fail(ret))
692 		goto fail;
693 	ret = mxl1x1sf_top_master_ctrl(state, 1);
694 	if (mxl_fail(ret))
695 		goto fail;
696 
697 	ret = mxl111sf_init_port_expander(state);
698 	if (mxl_fail(ret))
699 		goto fail;
700 	ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
701 	if (mxl_fail(ret))
702 		goto fail;
703 
704 	ret = get_chip_info(state);
705 	if (mxl_fail(ret))
706 		goto fail;
707 
708 	adap->fe[fe_id] = dvb_attach(lg2160_attach,
709 			      (MXL111SF_V8_200 == state->chip_rev) ?
710 			      &hauppauge_lg2161_1040_ep6_config :
711 			      &hauppauge_lg2161_1019_ep6_config,
712 			      &d->i2c_adap);
713 	if (adap->fe[fe_id]) {
714 		state->num_frontends++;
715 		adap_state->fe_init = adap->fe[fe_id]->ops.init;
716 		adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
717 		adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
718 		adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
719 		return 0;
720 	}
721 	ret = -EIO;
722 fail:
723 	return ret;
724 }
725 
726 static struct mxl111sf_demod_config mxl_demod_config = {
727 	.read_reg        = mxl111sf_read_reg,
728 	.write_reg       = mxl111sf_write_reg,
729 	.program_regs    = mxl111sf_ctrl_program_regs,
730 };
731 
732 static int mxl111sf_attach_demod(struct dvb_usb_adapter *adap, u8 fe_id)
733 {
734 	struct dvb_usb_device *d = adap_to_d(adap);
735 	struct mxl111sf_state *state = d_to_priv(d);
736 	struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
737 	int ret;
738 
739 	pr_debug("%s()\n", __func__);
740 
741 	/* save a pointer to the dvb_usb_device in device state */
742 	state->d = d;
743 	adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 1 : 2;
744 	state->alt_mode = adap_state->alt_mode;
745 
746 	if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
747 		pr_err("set interface failed");
748 
749 	state->gpio_mode = MXL111SF_GPIO_MOD_DVBT;
750 	adap_state->gpio_mode = state->gpio_mode;
751 	adap_state->device_mode = MXL_SOC_MODE;
752 	adap_state->ep6_clockphase = 1;
753 
754 	ret = mxl1x1sf_soft_reset(state);
755 	if (mxl_fail(ret))
756 		goto fail;
757 	ret = mxl111sf_init_tuner_demod(state);
758 	if (mxl_fail(ret))
759 		goto fail;
760 
761 	ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
762 	if (mxl_fail(ret))
763 		goto fail;
764 
765 	ret = mxl111sf_enable_usb_output(state);
766 	if (mxl_fail(ret))
767 		goto fail;
768 	ret = mxl1x1sf_top_master_ctrl(state, 1);
769 	if (mxl_fail(ret))
770 		goto fail;
771 
772 	/* dont care if this fails */
773 	mxl111sf_init_port_expander(state);
774 
775 	adap->fe[fe_id] = dvb_attach(mxl111sf_demod_attach, state,
776 			      &mxl_demod_config);
777 	if (adap->fe[fe_id]) {
778 		state->num_frontends++;
779 		adap_state->fe_init = adap->fe[fe_id]->ops.init;
780 		adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
781 		adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
782 		adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
783 		return 0;
784 	}
785 	ret = -EIO;
786 fail:
787 	return ret;
788 }
789 
790 static inline int mxl111sf_set_ant_path(struct mxl111sf_state *state,
791 					int antpath)
792 {
793 	return mxl111sf_idac_config(state, 1, 1,
794 				    (antpath == ANT_PATH_INTERNAL) ?
795 				    0x3f : 0x00, 0);
796 }
797 
798 #define DbgAntHunt(x, pwr0, pwr1, pwr2, pwr3) \
799 	pr_err("%s(%d) FINAL input set to %s rxPwr:%d|%d|%d|%d\n", \
800 	    __func__, __LINE__, \
801 	    (ANT_PATH_EXTERNAL == x) ? "EXTERNAL" : "INTERNAL", \
802 	    pwr0, pwr1, pwr2, pwr3)
803 
804 #define ANT_HUNT_SLEEP 90
805 #define ANT_EXT_TWEAK 0
806 
807 static int mxl111sf_ant_hunt(struct dvb_frontend *fe)
808 {
809 	struct mxl111sf_state *state = fe_to_priv(fe);
810 	int antctrl = dvb_usb_mxl111sf_rfswitch;
811 
812 	u16 rxPwrA, rxPwr0, rxPwr1, rxPwr2;
813 
814 	/* FIXME: must force EXTERNAL for QAM - done elsewhere */
815 	mxl111sf_set_ant_path(state, antctrl == ANT_PATH_AUTO ?
816 			      ANT_PATH_EXTERNAL : antctrl);
817 
818 	if (antctrl == ANT_PATH_AUTO) {
819 #if 0
820 		msleep(ANT_HUNT_SLEEP);
821 #endif
822 		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwrA);
823 
824 		mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
825 		msleep(ANT_HUNT_SLEEP);
826 		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr0);
827 
828 		mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
829 		msleep(ANT_HUNT_SLEEP);
830 		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr1);
831 
832 		mxl111sf_set_ant_path(state, ANT_PATH_INTERNAL);
833 		msleep(ANT_HUNT_SLEEP);
834 		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr2);
835 
836 		if (rxPwr1+ANT_EXT_TWEAK >= rxPwr2) {
837 			/* return with EXTERNAL enabled */
838 			mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
839 			DbgAntHunt(ANT_PATH_EXTERNAL, rxPwrA,
840 				   rxPwr0, rxPwr1, rxPwr2);
841 		} else {
842 			/* return with INTERNAL enabled */
843 			DbgAntHunt(ANT_PATH_INTERNAL, rxPwrA,
844 				   rxPwr0, rxPwr1, rxPwr2);
845 		}
846 	}
847 	return 0;
848 }
849 
850 static struct mxl111sf_tuner_config mxl_tuner_config = {
851 	.if_freq         = MXL_IF_6_0, /* applies to external IF output, only */
852 	.invert_spectrum = 0,
853 	.read_reg        = mxl111sf_read_reg,
854 	.write_reg       = mxl111sf_write_reg,
855 	.program_regs    = mxl111sf_ctrl_program_regs,
856 	.top_master_ctrl = mxl1x1sf_top_master_ctrl,
857 	.ant_hunt        = mxl111sf_ant_hunt,
858 };
859 
860 static int mxl111sf_attach_tuner(struct dvb_usb_adapter *adap)
861 {
862 	struct mxl111sf_state *state = adap_to_priv(adap);
863 	int i;
864 
865 	pr_debug("%s()\n", __func__);
866 
867 	for (i = 0; i < state->num_frontends; i++) {
868 		if (dvb_attach(mxl111sf_tuner_attach, adap->fe[i], state,
869 				&mxl_tuner_config) == NULL)
870 			return -EIO;
871 		adap->fe[i]->ops.read_signal_strength = adap->fe[i]->ops.tuner_ops.get_rf_strength;
872 	}
873 
874 	return 0;
875 }
876 
877 static u32 mxl111sf_i2c_func(struct i2c_adapter *adapter)
878 {
879 	return I2C_FUNC_I2C;
880 }
881 
882 struct i2c_algorithm mxl111sf_i2c_algo = {
883 	.master_xfer   = mxl111sf_i2c_xfer,
884 	.functionality = mxl111sf_i2c_func,
885 #ifdef NEED_ALGO_CONTROL
886 	.algo_control = dummy_algo_control,
887 #endif
888 };
889 
890 static int mxl111sf_init(struct dvb_usb_device *d)
891 {
892 	struct mxl111sf_state *state = d_to_priv(d);
893 	int ret;
894 	static u8 eeprom[256];
895 	struct i2c_client c;
896 
897 	ret = get_chip_info(state);
898 	if (mxl_fail(ret))
899 		pr_err("failed to get chip info during probe");
900 
901 	mutex_init(&state->fe_lock);
902 
903 	if (state->chip_rev > MXL111SF_V6)
904 		mxl111sf_config_pin_mux_modes(state, PIN_MUX_TS_SPI_IN_MODE_1);
905 
906 	c.adapter = &d->i2c_adap;
907 	c.addr = 0xa0 >> 1;
908 
909 	ret = tveeprom_read(&c, eeprom, sizeof(eeprom));
910 	if (mxl_fail(ret))
911 		return 0;
912 	tveeprom_hauppauge_analog(&c, &state->tv, (0x84 == eeprom[0xa0]) ?
913 			eeprom + 0xa0 : eeprom + 0x80);
914 #if 0
915 	switch (state->tv.model) {
916 	case 117001:
917 	case 126001:
918 	case 138001:
919 		break;
920 	default:
921 		printk(KERN_WARNING "%s: warning: "
922 		       "unknown hauppauge model #%d\n",
923 		       __func__, state->tv.model);
924 	}
925 #endif
926 	return 0;
927 }
928 
929 static int mxl111sf_frontend_attach_dvbt(struct dvb_usb_adapter *adap)
930 {
931 	return mxl111sf_attach_demod(adap, 0);
932 }
933 
934 static int mxl111sf_frontend_attach_atsc(struct dvb_usb_adapter *adap)
935 {
936 	return mxl111sf_lgdt3305_frontend_attach(adap, 0);
937 }
938 
939 static int mxl111sf_frontend_attach_mh(struct dvb_usb_adapter *adap)
940 {
941 	return mxl111sf_lg2160_frontend_attach(adap, 0);
942 }
943 
944 static int mxl111sf_frontend_attach_atsc_mh(struct dvb_usb_adapter *adap)
945 {
946 	int ret;
947 	pr_debug("%s\n", __func__);
948 
949 	ret = mxl111sf_lgdt3305_frontend_attach(adap, 0);
950 	if (ret < 0)
951 		return ret;
952 
953 	ret = mxl111sf_attach_demod(adap, 1);
954 	if (ret < 0)
955 		return ret;
956 
957 	ret = mxl111sf_lg2160_frontend_attach(adap, 2);
958 	if (ret < 0)
959 		return ret;
960 
961 	return ret;
962 }
963 
964 static int mxl111sf_frontend_attach_mercury(struct dvb_usb_adapter *adap)
965 {
966 	int ret;
967 	pr_debug("%s\n", __func__);
968 
969 	ret = mxl111sf_lgdt3305_frontend_attach(adap, 0);
970 	if (ret < 0)
971 		return ret;
972 
973 	ret = mxl111sf_attach_demod(adap, 1);
974 	if (ret < 0)
975 		return ret;
976 
977 	ret = mxl111sf_lg2161_ep6_frontend_attach(adap, 2);
978 	if (ret < 0)
979 		return ret;
980 
981 	return ret;
982 }
983 
984 static int mxl111sf_frontend_attach_mercury_mh(struct dvb_usb_adapter *adap)
985 {
986 	int ret;
987 	pr_debug("%s\n", __func__);
988 
989 	ret = mxl111sf_attach_demod(adap, 0);
990 	if (ret < 0)
991 		return ret;
992 
993 	if (dvb_usb_mxl111sf_spi)
994 		ret = mxl111sf_lg2161_frontend_attach(adap, 1);
995 	else
996 		ret = mxl111sf_lg2161_ep6_frontend_attach(adap, 1);
997 
998 	return ret;
999 }
1000 
1001 static void mxl111sf_stream_config_bulk(struct usb_data_stream_properties *stream, u8 endpoint)
1002 {
1003 	pr_debug("%s: endpoint=%d size=8192\n", __func__, endpoint);
1004 	stream->type = USB_BULK;
1005 	stream->count = 5;
1006 	stream->endpoint = endpoint;
1007 	stream->u.bulk.buffersize = 8192;
1008 }
1009 
1010 static void mxl111sf_stream_config_isoc(struct usb_data_stream_properties *stream,
1011 		u8 endpoint, int framesperurb, int framesize)
1012 {
1013 	pr_debug("%s: endpoint=%d size=%d\n", __func__, endpoint,
1014 			framesperurb * framesize);
1015 	stream->type = USB_ISOC;
1016 	stream->count = 5;
1017 	stream->endpoint = endpoint;
1018 	stream->u.isoc.framesperurb = framesperurb;
1019 	stream->u.isoc.framesize = framesize;
1020 	stream->u.isoc.interval = 1;
1021 }
1022 
1023 /* DVB USB Driver stuff */
1024 
1025 /* dvbt       mxl111sf
1026  * bulk       EP4/BULK/5/8192
1027  * isoc       EP4/ISOC/5/96/564
1028  */
1029 static int mxl111sf_get_stream_config_dvbt(struct dvb_frontend *fe,
1030 		u8 *ts_type, struct usb_data_stream_properties *stream)
1031 {
1032 	pr_debug("%s: fe=%d\n", __func__, fe->id);
1033 
1034 	*ts_type = DVB_USB_FE_TS_TYPE_188;
1035 	if (dvb_usb_mxl111sf_isoc)
1036 		mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1037 	else
1038 		mxl111sf_stream_config_bulk(stream, 4);
1039 	return 0;
1040 }
1041 
1042 static struct dvb_usb_device_properties mxl111sf_props_dvbt = {
1043 	.driver_name = KBUILD_MODNAME,
1044 	.owner = THIS_MODULE,
1045 	.adapter_nr = adapter_nr,
1046 	.size_of_priv = sizeof(struct mxl111sf_state),
1047 
1048 	.generic_bulk_ctrl_endpoint = 0x02,
1049 	.generic_bulk_ctrl_endpoint_response = 0x81,
1050 
1051 	.i2c_algo          = &mxl111sf_i2c_algo,
1052 	.frontend_attach   = mxl111sf_frontend_attach_dvbt,
1053 	.tuner_attach      = mxl111sf_attach_tuner,
1054 	.init              = mxl111sf_init,
1055 	.streaming_ctrl    = mxl111sf_ep4_streaming_ctrl,
1056 	.get_stream_config = mxl111sf_get_stream_config_dvbt,
1057 
1058 	.num_adapters = 1,
1059 	.adapter = {
1060 		{
1061 			.stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1062 		}
1063 	}
1064 };
1065 
1066 /* atsc       lgdt3305
1067  * bulk       EP6/BULK/5/8192
1068  * isoc       EP6/ISOC/5/24/3072
1069  */
1070 static int mxl111sf_get_stream_config_atsc(struct dvb_frontend *fe,
1071 		u8 *ts_type, struct usb_data_stream_properties *stream)
1072 {
1073 	pr_debug("%s: fe=%d\n", __func__, fe->id);
1074 
1075 	*ts_type = DVB_USB_FE_TS_TYPE_188;
1076 	if (dvb_usb_mxl111sf_isoc)
1077 		mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1078 	else
1079 		mxl111sf_stream_config_bulk(stream, 6);
1080 	return 0;
1081 }
1082 
1083 static struct dvb_usb_device_properties mxl111sf_props_atsc = {
1084 	.driver_name = KBUILD_MODNAME,
1085 	.owner = THIS_MODULE,
1086 	.adapter_nr = adapter_nr,
1087 	.size_of_priv = sizeof(struct mxl111sf_state),
1088 
1089 	.generic_bulk_ctrl_endpoint = 0x02,
1090 	.generic_bulk_ctrl_endpoint_response = 0x81,
1091 
1092 	.i2c_algo          = &mxl111sf_i2c_algo,
1093 	.frontend_attach   = mxl111sf_frontend_attach_atsc,
1094 	.tuner_attach      = mxl111sf_attach_tuner,
1095 	.init              = mxl111sf_init,
1096 	.streaming_ctrl    = mxl111sf_ep6_streaming_ctrl,
1097 	.get_stream_config = mxl111sf_get_stream_config_atsc,
1098 
1099 	.num_adapters = 1,
1100 	.adapter = {
1101 		{
1102 			.stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1103 		}
1104 	}
1105 };
1106 
1107 /* mh         lg2160
1108  * bulk       EP5/BULK/5/8192/RAW
1109  * isoc       EP5/ISOC/5/96/200/RAW
1110  */
1111 static int mxl111sf_get_stream_config_mh(struct dvb_frontend *fe,
1112 		u8 *ts_type, struct usb_data_stream_properties *stream)
1113 {
1114 	pr_debug("%s: fe=%d\n", __func__, fe->id);
1115 
1116 	*ts_type = DVB_USB_FE_TS_TYPE_RAW;
1117 	if (dvb_usb_mxl111sf_isoc)
1118 		mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1119 	else
1120 		mxl111sf_stream_config_bulk(stream, 5);
1121 	return 0;
1122 }
1123 
1124 static struct dvb_usb_device_properties mxl111sf_props_mh = {
1125 	.driver_name = KBUILD_MODNAME,
1126 	.owner = THIS_MODULE,
1127 	.adapter_nr = adapter_nr,
1128 	.size_of_priv = sizeof(struct mxl111sf_state),
1129 
1130 	.generic_bulk_ctrl_endpoint = 0x02,
1131 	.generic_bulk_ctrl_endpoint_response = 0x81,
1132 
1133 	.i2c_algo          = &mxl111sf_i2c_algo,
1134 	.frontend_attach   = mxl111sf_frontend_attach_mh,
1135 	.tuner_attach      = mxl111sf_attach_tuner,
1136 	.init              = mxl111sf_init,
1137 	.streaming_ctrl    = mxl111sf_ep5_streaming_ctrl,
1138 	.get_stream_config = mxl111sf_get_stream_config_mh,
1139 
1140 	.num_adapters = 1,
1141 	.adapter = {
1142 		{
1143 			.stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1144 		}
1145 	}
1146 };
1147 
1148 /* atsc mh    lgdt3305           mxl111sf          lg2160
1149  * bulk       EP6/BULK/5/8192    EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1150  * isoc       EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1151  */
1152 static int mxl111sf_get_stream_config_atsc_mh(struct dvb_frontend *fe,
1153 		u8 *ts_type, struct usb_data_stream_properties *stream)
1154 {
1155 	pr_debug("%s: fe=%d\n", __func__, fe->id);
1156 
1157 	if (fe->id == 0) {
1158 		*ts_type = DVB_USB_FE_TS_TYPE_188;
1159 		if (dvb_usb_mxl111sf_isoc)
1160 			mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1161 		else
1162 			mxl111sf_stream_config_bulk(stream, 6);
1163 	} else if (fe->id == 1) {
1164 		*ts_type = DVB_USB_FE_TS_TYPE_188;
1165 		if (dvb_usb_mxl111sf_isoc)
1166 			mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1167 		else
1168 			mxl111sf_stream_config_bulk(stream, 4);
1169 	} else if (fe->id == 2) {
1170 		*ts_type = DVB_USB_FE_TS_TYPE_RAW;
1171 		if (dvb_usb_mxl111sf_isoc)
1172 			mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1173 		else
1174 			mxl111sf_stream_config_bulk(stream, 5);
1175 	}
1176 	return 0;
1177 }
1178 
1179 static int mxl111sf_streaming_ctrl_atsc_mh(struct dvb_frontend *fe, int onoff)
1180 {
1181 	pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1182 
1183 	if (fe->id == 0)
1184 		return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1185 	else if (fe->id == 1)
1186 		return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1187 	else if (fe->id == 2)
1188 		return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1189 	return 0;
1190 }
1191 
1192 static struct dvb_usb_device_properties mxl111sf_props_atsc_mh = {
1193 	.driver_name = KBUILD_MODNAME,
1194 	.owner = THIS_MODULE,
1195 	.adapter_nr = adapter_nr,
1196 	.size_of_priv = sizeof(struct mxl111sf_state),
1197 
1198 	.generic_bulk_ctrl_endpoint = 0x02,
1199 	.generic_bulk_ctrl_endpoint_response = 0x81,
1200 
1201 	.i2c_algo          = &mxl111sf_i2c_algo,
1202 	.frontend_attach   = mxl111sf_frontend_attach_atsc_mh,
1203 	.tuner_attach      = mxl111sf_attach_tuner,
1204 	.init              = mxl111sf_init,
1205 	.streaming_ctrl    = mxl111sf_streaming_ctrl_atsc_mh,
1206 	.get_stream_config = mxl111sf_get_stream_config_atsc_mh,
1207 
1208 	.num_adapters = 1,
1209 	.adapter = {
1210 		{
1211 			.stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1212 		}
1213 	}
1214 };
1215 
1216 /* mercury    lgdt3305           mxl111sf          lg2161
1217  * tp bulk    EP6/BULK/5/8192    EP4/BULK/5/8192   EP6/BULK/5/8192/RAW
1218  * tp isoc    EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP6/ISOC/5/24/3072/RAW
1219  * spi bulk   EP6/BULK/5/8192    EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1220  * spi isoc   EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1221  */
1222 static int mxl111sf_get_stream_config_mercury(struct dvb_frontend *fe,
1223 		u8 *ts_type, struct usb_data_stream_properties *stream)
1224 {
1225 	pr_debug("%s: fe=%d\n", __func__, fe->id);
1226 
1227 	if (fe->id == 0) {
1228 		*ts_type = DVB_USB_FE_TS_TYPE_188;
1229 		if (dvb_usb_mxl111sf_isoc)
1230 			mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1231 		else
1232 			mxl111sf_stream_config_bulk(stream, 6);
1233 	} else if (fe->id == 1) {
1234 		*ts_type = DVB_USB_FE_TS_TYPE_188;
1235 		if (dvb_usb_mxl111sf_isoc)
1236 			mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1237 		else
1238 			mxl111sf_stream_config_bulk(stream, 4);
1239 	} else if (fe->id == 2 && dvb_usb_mxl111sf_spi) {
1240 		*ts_type = DVB_USB_FE_TS_TYPE_RAW;
1241 		if (dvb_usb_mxl111sf_isoc)
1242 			mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1243 		else
1244 			mxl111sf_stream_config_bulk(stream, 5);
1245 	} else if (fe->id == 2 && !dvb_usb_mxl111sf_spi) {
1246 		*ts_type = DVB_USB_FE_TS_TYPE_RAW;
1247 		if (dvb_usb_mxl111sf_isoc)
1248 			mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1249 		else
1250 			mxl111sf_stream_config_bulk(stream, 6);
1251 	}
1252 	return 0;
1253 }
1254 
1255 static int mxl111sf_streaming_ctrl_mercury(struct dvb_frontend *fe, int onoff)
1256 {
1257 	pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1258 
1259 	if (fe->id == 0)
1260 		return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1261 	else if (fe->id == 1)
1262 		return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1263 	else if (fe->id == 2 && dvb_usb_mxl111sf_spi)
1264 		return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1265 	else if (fe->id == 2 && !dvb_usb_mxl111sf_spi)
1266 		return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1267 	return 0;
1268 }
1269 
1270 static struct dvb_usb_device_properties mxl111sf_props_mercury = {
1271 	.driver_name = KBUILD_MODNAME,
1272 	.owner = THIS_MODULE,
1273 	.adapter_nr = adapter_nr,
1274 	.size_of_priv = sizeof(struct mxl111sf_state),
1275 
1276 	.generic_bulk_ctrl_endpoint = 0x02,
1277 	.generic_bulk_ctrl_endpoint_response = 0x81,
1278 
1279 	.i2c_algo          = &mxl111sf_i2c_algo,
1280 	.frontend_attach   = mxl111sf_frontend_attach_mercury,
1281 	.tuner_attach      = mxl111sf_attach_tuner,
1282 	.init              = mxl111sf_init,
1283 	.streaming_ctrl    = mxl111sf_streaming_ctrl_mercury,
1284 	.get_stream_config = mxl111sf_get_stream_config_mercury,
1285 
1286 	.num_adapters = 1,
1287 	.adapter = {
1288 		{
1289 			.stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1290 		}
1291 	}
1292 };
1293 
1294 /* mercury mh mxl111sf          lg2161
1295  * tp bulk    EP4/BULK/5/8192   EP6/BULK/5/8192/RAW
1296  * tp isoc    EP4/ISOC/5/96/564 EP6/ISOC/5/24/3072/RAW
1297  * spi bulk   EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1298  * spi isoc   EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1299  */
1300 static int mxl111sf_get_stream_config_mercury_mh(struct dvb_frontend *fe,
1301 		u8 *ts_type, struct usb_data_stream_properties *stream)
1302 {
1303 	pr_debug("%s: fe=%d\n", __func__, fe->id);
1304 
1305 	if (fe->id == 0) {
1306 		*ts_type = DVB_USB_FE_TS_TYPE_188;
1307 		if (dvb_usb_mxl111sf_isoc)
1308 			mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1309 		else
1310 			mxl111sf_stream_config_bulk(stream, 4);
1311 	} else if (fe->id == 1 && dvb_usb_mxl111sf_spi) {
1312 		*ts_type = DVB_USB_FE_TS_TYPE_RAW;
1313 		if (dvb_usb_mxl111sf_isoc)
1314 			mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1315 		else
1316 			mxl111sf_stream_config_bulk(stream, 5);
1317 	} else if (fe->id == 1 && !dvb_usb_mxl111sf_spi) {
1318 		*ts_type = DVB_USB_FE_TS_TYPE_RAW;
1319 		if (dvb_usb_mxl111sf_isoc)
1320 			mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1321 		else
1322 			mxl111sf_stream_config_bulk(stream, 6);
1323 	}
1324 	return 0;
1325 }
1326 
1327 static int mxl111sf_streaming_ctrl_mercury_mh(struct dvb_frontend *fe, int onoff)
1328 {
1329 	pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1330 
1331 	if (fe->id == 0)
1332 		return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1333 	else if (fe->id == 1  && dvb_usb_mxl111sf_spi)
1334 		return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1335 	else if (fe->id == 1 && !dvb_usb_mxl111sf_spi)
1336 		return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1337 	return 0;
1338 }
1339 
1340 static struct dvb_usb_device_properties mxl111sf_props_mercury_mh = {
1341 	.driver_name = KBUILD_MODNAME,
1342 	.owner = THIS_MODULE,
1343 	.adapter_nr = adapter_nr,
1344 	.size_of_priv = sizeof(struct mxl111sf_state),
1345 
1346 	.generic_bulk_ctrl_endpoint = 0x02,
1347 	.generic_bulk_ctrl_endpoint_response = 0x81,
1348 
1349 	.i2c_algo          = &mxl111sf_i2c_algo,
1350 	.frontend_attach   = mxl111sf_frontend_attach_mercury_mh,
1351 	.tuner_attach      = mxl111sf_attach_tuner,
1352 	.init              = mxl111sf_init,
1353 	.streaming_ctrl    = mxl111sf_streaming_ctrl_mercury_mh,
1354 	.get_stream_config = mxl111sf_get_stream_config_mercury_mh,
1355 
1356 	.num_adapters = 1,
1357 	.adapter = {
1358 		{
1359 			.stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1360 		}
1361 	}
1362 };
1363 
1364 static const struct usb_device_id mxl111sf_id_table[] = {
1365 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc600, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1366 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc601, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1367 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc602, &mxl111sf_props_mh, "HCW 126xxx", NULL) },
1368 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc603, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1369 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc604, &mxl111sf_props_dvbt, "Hauppauge 126xxx DVBT", NULL) },
1370 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc609, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1371 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60a, &mxl111sf_props_mh, "HCW 126xxx", NULL) },
1372 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60b, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1373 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60c, &mxl111sf_props_dvbt, "Hauppauge 126xxx DVBT", NULL) },
1374 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc653, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1375 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc65b, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1376 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb700, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1377 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb701, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1378 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb702, &mxl111sf_props_mh, "HCW 117xxx", NULL) },
1379 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb703, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1380 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb704, &mxl111sf_props_dvbt, "Hauppauge 117xxx DVBT", NULL) },
1381 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb753, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1382 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb763, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1383 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb764, &mxl111sf_props_dvbt, "Hauppauge 117xxx DVBT", NULL) },
1384 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd853, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1385 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd854, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1386 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd863, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1387 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd864, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1388 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d3, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1389 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d4, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1390 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e3, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1391 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e4, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1392 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8ff, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1393 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc612, &mxl111sf_props_mercury_mh, "Hauppauge 126xxx", NULL) },
1394 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc613, &mxl111sf_props_mercury, "Hauppauge WinTV-Aero-M", NULL) },
1395 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61a, &mxl111sf_props_mercury_mh, "Hauppauge 126xxx", NULL) },
1396 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61b, &mxl111sf_props_mercury, "Hauppauge WinTV-Aero-M", NULL) },
1397 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb757, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1398 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb767, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1399 	{ }
1400 };
1401 MODULE_DEVICE_TABLE(usb, mxl111sf_id_table);
1402 
1403 static struct usb_driver mxl111sf_usb_driver = {
1404 	.name = KBUILD_MODNAME,
1405 	.id_table = mxl111sf_id_table,
1406 	.probe = dvb_usbv2_probe,
1407 	.disconnect = dvb_usbv2_disconnect,
1408 	.suspend = dvb_usbv2_suspend,
1409 	.resume = dvb_usbv2_resume,
1410 	.no_dynamic_id = 1,
1411 	.soft_unbind = 1,
1412 };
1413 
1414 module_usb_driver(mxl111sf_usb_driver);
1415 
1416 MODULE_AUTHOR("Michael Krufky <mkrufky@kernellabs.com>");
1417 MODULE_DESCRIPTION("Driver for MaxLinear MxL111SF");
1418 MODULE_VERSION("1.0");
1419 MODULE_LICENSE("GPL");
1420 
1421 /*
1422  * Local variables:
1423  * c-basic-offset: 8
1424  * End:
1425  */
1426