Jesse Keating 7a32965
diff --git a/drivers/media/video/hdpvr/Makefile b/drivers/media/video/hdpvr/Makefile
Jesse Keating 7a32965
index e0230fc..3baa9f6 100644
Jesse Keating 7a32965
--- a/drivers/media/video/hdpvr/Makefile
Jesse Keating 7a32965
+++ b/drivers/media/video/hdpvr/Makefile
Jesse Keating 7a32965
@@ -1,6 +1,4 @@
Jesse Keating 7a32965
-hdpvr-objs	:= hdpvr-control.o hdpvr-core.o hdpvr-video.o
Jesse Keating 7a32965
-
Jesse Keating 7a32965
-hdpvr-$(CONFIG_I2C) += hdpvr-i2c.o
Jesse Keating 7a32965
+hdpvr-objs	:= hdpvr-control.o hdpvr-core.o hdpvr-video.o hdpvr-i2c.o
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 obj-$(CONFIG_VIDEO_HDPVR) += hdpvr.o
Jesse Keating 7a32965
 
Jesse Keating 7a32965
diff --git a/drivers/media/video/hdpvr/hdpvr-core.c b/drivers/media/video/hdpvr/hdpvr-core.c
Jesse Keating 7a32965
index 830d47b..70cfdc8 100644
Jesse Keating 7a32965
--- a/drivers/media/video/hdpvr/hdpvr-core.c
Jesse Keating 7a32965
+++ b/drivers/media/video/hdpvr/hdpvr-core.c
Jesse Keating 7a32965
@@ -364,14 +364,13 @@ static int hdpvr_probe(struct usb_interface *interface,
Jesse Keating 7a32965
 		goto error;
Jesse Keating 7a32965
 	}
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-#ifdef CONFIG_I2C
Jesse Keating 7a32965
-	/* until i2c is working properly */
Jesse Keating 7a32965
-	retval = 0; /* hdpvr_register_i2c_adapter(dev); */
Jesse Keating 7a32965
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jesse Keating 7a32965
+	retval = hdpvr_register_i2c_adapter(dev);
Jesse Keating 7a32965
 	if (retval < 0) {
Jesse Keating 7a32965
 		v4l2_err(&dev->v4l2_dev, "registering i2c adapter failed\n");
Jesse Keating 7a32965
 		goto error;
Jesse Keating 7a32965
 	}
Jesse Keating 7a32965
-#endif /* CONFIG_I2C */
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	/* let the user know what node this device is now attached to */
Jesse Keating 7a32965
 	v4l2_info(&dev->v4l2_dev, "device now attached to %s\n",
Jesse Keating 7a32965
diff --git a/drivers/media/video/hdpvr/hdpvr-i2c.c b/drivers/media/video/hdpvr/hdpvr-i2c.c
Jesse Keating 7a32965
index 463b81b..60cdc06 100644
Jesse Keating 7a32965
--- a/drivers/media/video/hdpvr/hdpvr-i2c.c
Jesse Keating 7a32965
+++ b/drivers/media/video/hdpvr/hdpvr-i2c.c
Jesse Keating 7a32965
@@ -10,6 +10,8 @@
Jesse Keating 7a32965
  *
Jesse Keating 7a32965
  */
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 #include <linux/i2c.h>
Jesse Keating 7a32965
 #include <linux/slab.h>
Jesse Keating 7a32965
 
Jesse Keating 7a32965
@@ -22,8 +24,11 @@
Jesse Keating 7a32965
 #define REQTYPE_I2C_WRITE	0xb0
Jesse Keating 7a32965
 #define REQTYPE_I2C_WRITE_STATT	0xd0
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-static int hdpvr_i2c_read(struct hdpvr_device *dev, unsigned char addr,
Jesse Keating 7a32965
-			  char *data, int len)
Jesse Keating 7a32965
+#define HDPVR_HW_Z8F0811_IR_TX_I2C_ADDR	0x70
Jesse Keating 7a32965
+#define HDPVR_HW_Z8F0811_IR_RX_I2C_ADDR	0x71
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
Jesse Keating 7a32965
+			  unsigned char addr, char *data, int len)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
 	int ret;
Jesse Keating 7a32965
 	char *buf = kmalloc(len, GFP_KERNEL);
Jesse Keating 7a32965
@@ -33,7 +38,7 @@ static int hdpvr_i2c_read(struct hdpvr_device *dev, unsigned char addr,
Jesse Keating 7a32965
 	ret = usb_control_msg(dev->udev,
Jesse Keating 7a32965
 			      usb_rcvctrlpipe(dev->udev, 0),
Jesse Keating 7a32965
 			      REQTYPE_I2C_READ, CTRL_READ_REQUEST,
Jesse Keating 7a32965
-			      0x100|addr, 0, buf, len, 1000);
Jesse Keating 7a32965
+			      (bus << 8) | addr, 0, buf, len, 1000);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	if (ret == len) {
Jesse Keating 7a32965
 		memcpy(data, buf, len);
Jesse Keating 7a32965
@@ -46,8 +51,8 @@ static int hdpvr_i2c_read(struct hdpvr_device *dev, unsigned char addr,
Jesse Keating 7a32965
 	return ret;
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-static int hdpvr_i2c_write(struct hdpvr_device *dev, unsigned char addr,
Jesse Keating 7a32965
-			   char *data, int len)
Jesse Keating 7a32965
+static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
Jesse Keating 7a32965
+			   unsigned char addr, char *data, int len)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
 	int ret;
Jesse Keating 7a32965
 	char *buf = kmalloc(len, GFP_KERNEL);
Jesse Keating 7a32965
@@ -58,7 +63,7 @@ static int hdpvr_i2c_write(struct hdpvr_device *dev, unsigned char addr,
Jesse Keating 7a32965
 	ret = usb_control_msg(dev->udev,
Jesse Keating 7a32965
 			      usb_sndctrlpipe(dev->udev, 0),
Jesse Keating 7a32965
 			      REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
Jesse Keating 7a32965
-			      0x100|addr, 0, buf, len, 1000);
Jesse Keating 7a32965
+			      (bus << 8) | addr, 0, buf, len, 1000);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	if (ret < 0)
Jesse Keating 7a32965
 		goto error;
Jesse Keating 7a32965
@@ -68,7 +73,7 @@ static int hdpvr_i2c_write(struct hdpvr_device *dev, unsigned char addr,
Jesse Keating 7a32965
 			      REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
Jesse Keating 7a32965
 			      0, 0, buf, 2, 1000);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	if (ret == 2)
Jesse Keating 7a32965
+	if ((ret == 2) && (buf[1] == (len - 1)))
Jesse Keating 7a32965
 		ret = 0;
Jesse Keating 7a32965
 	else if (ret >= 0)
Jesse Keating 7a32965
 		ret = -EIO;
Jesse Keating 7a32965
@@ -93,10 +98,10 @@ static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
Jesse Keating 7a32965
 		addr = msgs[i].addr << 1;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 		if (msgs[i].flags & I2C_M_RD)
Jesse Keating 7a32965
-			retval = hdpvr_i2c_read(dev, addr, msgs[i].buf,
Jesse Keating 7a32965
+			retval = hdpvr_i2c_read(dev, 1, addr, msgs[i].buf,
Jesse Keating 7a32965
 						msgs[i].len);
Jesse Keating 7a32965
 		else
Jesse Keating 7a32965
-			retval = hdpvr_i2c_write(dev, addr, msgs[i].buf,
Jesse Keating 7a32965
+			retval = hdpvr_i2c_write(dev, 1, addr, msgs[i].buf,
Jesse Keating 7a32965
 						 msgs[i].len);
Jesse Keating 7a32965
 	}
Jesse Keating 7a32965
 
Jesse Keating 7a32965
@@ -115,31 +120,59 @@ static struct i2c_algorithm hdpvr_algo = {
Jesse Keating 7a32965
 	.functionality = hdpvr_functionality,
Jesse Keating 7a32965
 };
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+static struct i2c_adapter hdpvr_i2c_adapter_template = {
Jesse Keating 7a32965
+	.name 	= "Hauppage HD PVR I2C",
Jesse Keating 7a32965
+	.owner 	= THIS_MODULE,
Jesse Keating 7a32965
+	.id 	= I2C_HW_B_HDPVR,
Jesse Keating 7a32965
+	.algo 	= &hdpvr_algo,
Jesse Keating 7a32965
+	.class 	= I2C_CLASS_TV_ANALOG,
Jesse Keating 7a32965
+};
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static struct i2c_board_info hdpvr_i2c_board_info = {
Jesse Keating 7a32965
+	I2C_BOARD_INFO("ir_tx_z8f0811_haup", HDPVR_HW_Z8F0811_IR_TX_I2C_ADDR),
Jesse Keating 7a32965
+	I2C_BOARD_INFO("ir_rx_z8f0811_haup", HDPVR_HW_Z8F0811_IR_RX_I2C_ADDR),
Jesse Keating 7a32965
+};
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static int hdpvr_activate_ir(struct hdpvr_device *dev)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	char buffer[8];
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	mutex_lock(&dev->i2c_mutex);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	hdpvr_i2c_read(dev, 0, 0x54, buffer, 1);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	buffer[0] = 0;
Jesse Keating 7a32965
+	buffer[1] = 0x8;
Jesse Keating 7a32965
+	hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	buffer[1] = 0x18;
Jesse Keating 7a32965
+	hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	mutex_unlock(&dev->i2c_mutex);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	return 0;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
Jesse Keating 7a32965
 {
Jesse Keating 7a32965
-	struct i2c_adapter *i2c_adap;
Jesse Keating 7a32965
 	int retval = -ENOMEM;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	i2c_adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
Jesse Keating 7a32965
-	if (i2c_adap == NULL)
Jesse Keating 7a32965
-		goto error;
Jesse Keating 7a32965
+	hdpvr_activate_ir(dev);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	strlcpy(i2c_adap->name, "Hauppauge HD PVR I2C",
Jesse Keating 7a32965
-		sizeof(i2c_adap->name));
Jesse Keating 7a32965
-	i2c_adap->algo  = &hdpvr_algo;
Jesse Keating 7a32965
-	i2c_adap->class = I2C_CLASS_TV_ANALOG;
Jesse Keating 7a32965
-	i2c_adap->owner = THIS_MODULE;
Jesse Keating 7a32965
-	i2c_adap->dev.parent = &dev->udev->dev;
Jesse Keating 7a32965
+	memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template,
Jesse Keating 7a32965
+		sizeof(struct i2c_adapter));
Jesse Keating 7a32965
+	dev->i2c_adapter.dev.parent = &dev->udev->dev;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	i2c_set_adapdata(i2c_adap, dev);
Jesse Keating 7a32965
+	i2c_set_adapdata(&dev->i2c_adapter, dev);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	retval = i2c_add_adapter(i2c_adap);
Jesse Keating 7a32965
+	retval = i2c_add_adapter(&dev->i2c_adapter);
Jesse Keating 7a32965
+	if (retval)
Jesse Keating 7a32965
+		goto error;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
-	if (!retval)
Jesse Keating 7a32965
-		dev->i2c_adapter = i2c_adap;
Jesse Keating 7a32965
-	else
Jesse Keating 7a32965
-		kfree(i2c_adap);
Jesse Keating 7a32965
+	i2c_new_device(&dev->i2c_adapter, &hdpvr_i2c_board_info);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 error:
Jesse Keating 7a32965
 	return retval;
Jesse Keating 7a32965
 }
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#endif
Jesse Keating 7a32965
diff --git a/drivers/media/video/hdpvr/hdpvr-video.c b/drivers/media/video/hdpvr/hdpvr-video.c
Jesse Keating 7a32965
index c338f3f..26fd9bf 100644
Jesse Keating 7a32965
--- a/drivers/media/video/hdpvr/hdpvr-video.c
Jesse Keating 7a32965
+++ b/drivers/media/video/hdpvr/hdpvr-video.c
Jesse Keating 7a32965
@@ -1221,12 +1221,9 @@ static void hdpvr_device_release(struct video_device *vdev)
Jesse Keating 7a32965
 	v4l2_device_unregister(&dev->v4l2_dev);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	/* deregister I2C adapter */
Jesse Keating 7a32965
-#ifdef CONFIG_I2C
Jesse Keating 7a32965
+#if defined(CONFIG_I2C) || (CONFIG_I2C_MODULE)
Jesse Keating 7a32965
 	mutex_lock(&dev->i2c_mutex);
Jesse Keating 7a32965
-	if (dev->i2c_adapter)
Jesse Keating 7a32965
-		i2c_del_adapter(dev->i2c_adapter);
Jesse Keating 7a32965
-	kfree(dev->i2c_adapter);
Jesse Keating 7a32965
-	dev->i2c_adapter = NULL;
Jesse Keating 7a32965
+	i2c_del_adapter(&dev->i2c_adapter);
Jesse Keating 7a32965
 	mutex_unlock(&dev->i2c_mutex);
Jesse Keating 7a32965
 #endif /* CONFIG_I2C */
Jesse Keating 7a32965
 
Jesse Keating 7a32965
diff --git a/drivers/media/video/hdpvr/hdpvr.h b/drivers/media/video/hdpvr/hdpvr.h
Jesse Keating 7a32965
index b0f046d..2107055 100644
Jesse Keating 7a32965
--- a/drivers/media/video/hdpvr/hdpvr.h
Jesse Keating 7a32965
+++ b/drivers/media/video/hdpvr/hdpvr.h
Jesse Keating 7a32965
@@ -102,7 +102,7 @@ struct hdpvr_device {
Jesse Keating 7a32965
 	struct work_struct	worker;
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 	/* I2C adapter */
Jesse Keating 7a32965
-	struct i2c_adapter	*i2c_adapter;
Jesse Keating 7a32965
+	struct i2c_adapter	i2c_adapter;
Jesse Keating 7a32965
 	/* I2C lock */
Jesse Keating 7a32965
 	struct mutex		i2c_mutex;
Jesse Keating 7a32965