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