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