3 * Hauppauge HD PVR USB driver
5 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
13 #include <linux/i2c.h>
17 #define CTRL_READ_REQUEST 0xb8
18 #define CTRL_WRITE_REQUEST 0x38
20 #define REQTYPE_I2C_READ 0xb1
21 #define REQTYPE_I2C_WRITE 0xb0
22 #define REQTYPE_I2C_WRITE_STATT 0xd0
24 static int hdpvr_i2c_read(struct hdpvr_device *dev, unsigned char addr,
28 char *buf = kmalloc(len, GFP_KERNEL);
32 ret = usb_control_msg(dev->udev,
33 usb_rcvctrlpipe(dev->udev, 0),
34 REQTYPE_I2C_READ, CTRL_READ_REQUEST,
35 0x100|addr, 0, buf, len, 1000);
38 memcpy(data, buf, len);
48 static int hdpvr_i2c_write(struct hdpvr_device *dev, unsigned char addr,
52 char *buf = kmalloc(len, GFP_KERNEL);
56 memcpy(buf, data, len);
57 ret = usb_control_msg(dev->udev,
58 usb_sndctrlpipe(dev->udev, 0),
59 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
60 0x100|addr, 0, buf, len, 1000);
65 ret = usb_control_msg(dev->udev,
66 usb_rcvctrlpipe(dev->udev, 0),
67 REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
80 static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
83 struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
84 int retval = 0, i, addr;
89 mutex_lock(&dev->i2c_mutex);
91 for (i = 0; i < num && !retval; i++) {
92 addr = msgs[i].addr << 1;
94 if (msgs[i].flags & I2C_M_RD)
95 retval = hdpvr_i2c_read(dev, addr, msgs[i].buf,
98 retval = hdpvr_i2c_write(dev, addr, msgs[i].buf,
102 mutex_unlock(&dev->i2c_mutex);
104 return retval ? retval : num;
107 static u32 hdpvr_functionality(struct i2c_adapter *adapter)
109 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
112 static struct i2c_algorithm hdpvr_algo = {
113 .master_xfer = hdpvr_transfer,
114 .functionality = hdpvr_functionality,
117 int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
119 struct i2c_adapter *i2c_adap;
120 int retval = -ENOMEM;
122 i2c_adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
123 if (i2c_adap == NULL)
126 strlcpy(i2c_adap->name, "Hauppauge HD PVR I2C",
127 sizeof(i2c_adap->name));
128 i2c_adap->algo = &hdpvr_algo;
129 i2c_adap->class = I2C_CLASS_TV_ANALOG;
130 i2c_adap->id = I2C_HW_B_HDPVR;
131 i2c_adap->owner = THIS_MODULE;
132 i2c_adap->dev.parent = &dev->udev->dev;
134 i2c_set_adapdata(i2c_adap, dev);
136 retval = i2c_add_adapter(i2c_adap);
139 dev->i2c_adapter = i2c_adap;