V4L/DVB (4648): Hauppauge Nova-T 500 support added

This changeset finalizes the support of the Hauppauge Nova-T 500 (Dual DVB-T).
It adds correct AGC setting for the 3000P, correct firmware download state
detection.
Additionally it fixes the mt2060-driver to be able to be used with dvb_attach.

Signed-off-by: Patrick Boettcher <pb@linuxtv.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
diff --git a/drivers/media/dvb/dvb-usb/dib0700.h b/drivers/media/dvb/dvb-usb/dib0700.h
index a2cd697..ac84347 100644
--- a/drivers/media/dvb/dvb-usb/dib0700.h
+++ b/drivers/media/dvb/dvb-usb/dib0700.h
@@ -32,13 +32,15 @@
 
 struct dib0700_state {
 	u8 channel_state;
-	u8 mt2060_if1[2];
+	u16 mt2060_if1[2];
 };
 
 extern int dib0700_set_gpio(struct dvb_usb_device *, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val);
 extern int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw);
 extern int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff);
 extern struct i2c_algorithm dib0700_i2c_algo;
+extern int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
+			struct dvb_usb_device_description **desc, int *cold);
 
 extern int dib0700_device_count;
 extern struct dvb_usb_device_properties dib0700_devices[];
diff --git a/drivers/media/dvb/dvb-usb/dib0700_core.c b/drivers/media/dvb/dvb-usb/dib0700_core.c
index 1f444a4..75e8570 100644
--- a/drivers/media/dvb/dvb-usb/dib0700_core.c
+++ b/drivers/media/dvb/dvb-usb/dib0700_core.c
@@ -26,7 +26,7 @@
 		USB_CTRL_GET_TIMEOUT);
 
 	if (status != txlen)
-		err("ep 0 write error (status = %d, len: %d)",status,txlen);
+		deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
 
 	return status < 0 ? status : 0;
 }
@@ -65,7 +65,7 @@
 			USB_CTRL_GET_TIMEOUT);
 
 	if (status < 0)
-		err("ep 0 read error (status = %d)",status);
+		deb_info("ep 0 read error (status = %d)\n",status);
 
 	deb_data("<<< ");
 	debug_dump(rx,rxlen,deb_data);
@@ -130,6 +130,19 @@
 	.functionality = dib0700_i2c_func,
 };
 
+int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
+			struct dvb_usb_device_description **desc, int *cold)
+{
+	u8 buf[3] = { REQUEST_SET_GPIO, 4, (GPIO_IN << 7) | (0 << 6) }; // GPIO4 is save - used for I2C
+	*cold = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
+		buf[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, buf, 3, USB_CTRL_GET_TIMEOUT) != 3;
+
+	deb_info("cold: %d\n", *cold);
+
+	*cold = 0;
+	return 0;
+}
+
 static int dib0700_jumpram(struct usb_device *udev, u32 address)
 {
 	int ret, actlen;
@@ -182,8 +195,10 @@
 
 	if (ret == 0) {
 		/* start the firmware */
-		if ((ret = dib0700_jumpram(udev,0x70000000)) == 0)
+		if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
 			info("firmware started successfully.");
+			msleep(100);
+		}
 	} else
 		ret = -EIO;
 
diff --git a/drivers/media/dvb/dvb-usb/dib0700_devices.c b/drivers/media/dvb/dvb-usb/dib0700_devices.c
index 74d98df..da177e7 100644
--- a/drivers/media/dvb/dvb-usb/dib0700_devices.c
+++ b/drivers/media/dvb/dvb-usb/dib0700_devices.c
@@ -25,24 +25,24 @@
 
 static struct dibx000_agc_config bristol_dib3000p_mt2060_agc_config = {
 	.band_caps = BAND_VHF | BAND_UHF,
-	.setup     = (0 << 15) | (0 << 14) | (1 << 13) | (1 << 12) | (29 << 0),
+	.setup     = (0 << 15) | (0 << 14) | (0 << 13) | (0 << 12) | (29 << 0),
 
-	.agc1_max = 48497,
-	.agc1_min = 23593,
-	.agc2_max = 46531,
-	.agc2_min = 24904,
+	.agc1_max = 42598,
+	.agc1_min = 17694,
+	.agc2_max = 45875,
+	.agc2_min = 0,
 
-	.agc1_pt1 = 0x65,
-	.agc1_pt2 = 0x69,
+	.agc1_pt1 = 0,
+	.agc1_pt2 = 59,
 
-	.agc1_slope1 = 0x51,
-	.agc1_slope2 = 0x27,
+	.agc1_slope1 = 0,
+	.agc1_slope2 = 69,
 
 	.agc2_pt1 = 0,
-	.agc2_pt2 = 0x33,
+	.agc2_pt2 = 59,
 
-	.agc2_slope1 = 0x35,
-	.agc2_slope2 = 0x37,
+	.agc2_slope1 = 111,
+	.agc2_slope2 = 28,
 };
 
 static struct dib3000mc_config bristol_dib3000mc_config[2] = {
@@ -60,25 +60,31 @@
 
 static int bristol_frontend_attach(struct dvb_usb_adapter *adap)
 {
+	struct dib0700_state *st = adap->dev->priv;
 	if (adap->id == 0) {
 		dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 0); msleep(10);
 		dib0700_set_gpio(adap->dev, GPIO6,  GPIO_OUT, 1); msleep(10);
 		dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 0); msleep(10);
 		dib0700_set_gpio(adap->dev, GPIO10, GPIO_OUT, 1); msleep(10);
 
+		dib0700_set_gpio(adap->dev, GPIO0, GPIO_OUT, 1); msleep(10); // LNA
+
 		if (dib3000mc_i2c_enumeration(&adap->dev->i2c_adap, 2, DEFAULT_DIB3000P_I2C_ADDRESS, bristol_dib3000mc_config) != 0) {
 			dib0700_set_gpio(adap->dev, GPIO6, GPIO_OUT, 0); msleep(10);
 			return -ENODEV;
 		}
 	}
-	return (adap->fe = dvb_attach(dib3000mc_attach, &adap->dev->i2c_adap, 10 + adap->id, &bristol_dib3000mc_config[adap->id])) == NULL ? -ENODEV : 0;
+	st->mt2060_if1[adap->id] = 1220;
+	return (adap->fe = dvb_attach(dib3000mc_attach, &adap->dev->i2c_adap,
+		(10 + adap->id) << 1, &bristol_dib3000mc_config[adap->id])) == NULL ? -ENODEV : 0;
 }
 
 static int bristol_tuner_attach(struct dvb_usb_adapter *adap)
 {
 	struct dib0700_state *st = adap->dev->priv;
 	struct i2c_adapter *tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe, 1);
-	return mt2060_attach(adap->fe, tun_i2c, &bristol_mt2060_config[adap->id], st->mt2060_if1[adap->id]);
+	return dvb_attach(mt2060_attach,adap->fe, tun_i2c, &bristol_mt2060_config[adap->id],
+		st->mt2060_if1[adap->id]) == NULL ? -ENODEV : 0;
 }
 
 /* STK7700P: Hauppauge Nova-T Stick, AVerMedia Volar */
@@ -119,8 +125,10 @@
 	.usb_ctrl          = DEVICE_SPECIFIC, \
 	.firmware          = "dvb-usb-dib0700-01.fw", \
 	.download_firmware = dib0700_download_firmware, \
+	.no_reconnect      = 1, \
 	.size_of_priv      = sizeof(struct dib0700_state), \
-	.i2c_algo          = &dib0700_i2c_algo
+	.i2c_algo          = &dib0700_i2c_algo, \
+	.identify_state    = dib0700_identify_state
 
 #define DIB0700_DEFAULT_STREAMING_CONFIG(ep) \
 	.streaming_ctrl   = dib0700_streaming_ctrl, \
diff --git a/drivers/media/dvb/dvb-usb/dvb-usb-firmware.c b/drivers/media/dvb/dvb-usb/dvb-usb-firmware.c
index e1112e3..4e986f1 100644
--- a/drivers/media/dvb/dvb-usb/dvb-usb-firmware.c
+++ b/drivers/media/dvb/dvb-usb/dvb-usb-firmware.c
@@ -80,6 +80,7 @@
 	int ret;
 	const struct firmware *fw = NULL;
 
+
 	if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
 		err("did not find the firmware file. (%s) "
 			"Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
diff --git a/drivers/media/dvb/dvb-usb/dvb-usb-ids.h b/drivers/media/dvb/dvb-usb/dvb-usb-ids.h
index feb0987..415a989 100644
--- a/drivers/media/dvb/dvb-usb/dvb-usb-ids.h
+++ b/drivers/media/dvb/dvb-usb/dvb-usb-ids.h
@@ -94,8 +94,8 @@
 #define USB_PID_WT220U_ZL0353_WARM			0x022b
 #define USB_PID_WINTV_NOVA_T_USB2_COLD			0x9300
 #define USB_PID_WINTV_NOVA_T_USB2_WARM			0x9301
-#define USB_PID_HAUPPAUGE_NOVA_T_500			0x1234
-#define USB_PID_HAUPPAUGE_NOVA_T_STICK			0x1234
+#define USB_PID_HAUPPAUGE_NOVA_T_500			0x9941
+#define USB_PID_HAUPPAUGE_NOVA_T_STICK			0x7050
 #define USB_PID_AVERMEDIA_VOLAR				0x1234
 #define USB_PID_NEBULA_DIGITV				0x0201
 #define USB_PID_DVICO_BLUEBIRD_LGDT			0xd820
diff --git a/drivers/media/dvb/frontends/dib3000mc.c b/drivers/media/dvb/frontends/dib3000mc.c
index d7c4a98..60e10db 100644
--- a/drivers/media/dvb/frontends/dib3000mc.c
+++ b/drivers/media/dvb/frontends/dib3000mc.c
@@ -830,6 +830,7 @@
 
 	st->cfg = cfg;
 	st->i2c_adap = i2c_adap;
+	st->i2c_addr = i2c_addr;
 
 	demod                   = &st->demod;
 	demod->demodulator_priv = st;
diff --git a/drivers/media/dvb/frontends/mt2060.c b/drivers/media/dvb/frontends/mt2060.c
index 19bd66a..450fad8 100644
--- a/drivers/media/dvb/frontends/mt2060.c
+++ b/drivers/media/dvb/frontends/mt2060.c
@@ -332,14 +332,14 @@
 };
 
 /* This functions tries to identify a MT2060 tuner by reading the PART/REV register. This is hasty. */
-int mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1)
+struct dvb_frontend * mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1)
 {
 	struct mt2060_priv *priv = NULL;
 	u8 id = 0;
 
 	priv = kzalloc(sizeof(struct mt2060_priv), GFP_KERNEL);
 	if (priv == NULL)
-		return -ENOMEM;
+		return NULL;
 
 	priv->cfg      = cfg;
 	priv->i2c      = i2c;
@@ -347,12 +347,12 @@
 
 	if (mt2060_readreg(priv,REG_PART_REV,&id) != 0) {
 		kfree(priv);
-		return -ENODEV;
+		return NULL;
 	}
 
 	if (id != PART_REV) {
 		kfree(priv);
-		return -ENODEV;
+		return NULL;
 	}
 	printk(KERN_INFO "MT2060: successfully identified (IF1 = %d)\n", if1);
 	memcpy(&fe->ops.tuner_ops, &mt2060_tuner_ops, sizeof(struct dvb_tuner_ops));
@@ -361,7 +361,7 @@
 
 	mt2060_calibrate(priv);
 
-	return 0;
+	return fe;
 }
 EXPORT_SYMBOL(mt2060_attach);
 
diff --git a/drivers/media/dvb/frontends/mt2060.h b/drivers/media/dvb/frontends/mt2060.h
index 471d332..fc12c79 100644
--- a/drivers/media/dvb/frontends/mt2060.h
+++ b/drivers/media/dvb/frontends/mt2060.h
@@ -30,6 +30,6 @@
 	u8 clock_out; /* 0 = off, 1 = CLK/1, 2 = CLK/2, 3 = CLK/4 */
 };
 
-extern int mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1);
+extern struct dvb_frontend * mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1);
 
 #endif