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