3 * Hauppauge HD PVR USB driver
5 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
7 * IR device registration code is
8 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
21 #define CTRL_READ_REQUEST 0xb8
22 #define CTRL_WRITE_REQUEST 0x38
24 #define REQTYPE_I2C_READ 0xb1
25 #define REQTYPE_I2C_WRITE 0xb0
26 #define REQTYPE_I2C_WRITE_STATT 0xd0
28 #define Z8F0811_IR_TX_I2C_ADDR 0x70
29 #define Z8F0811_IR_RX_I2C_ADDR 0x71
31 static const u8 ir_i2c_addrs[] = {
32 Z8F0811_IR_TX_I2C_ADDR,
33 Z8F0811_IR_RX_I2C_ADDR,
36 static const char * const ir_devicenames[] = {
37 "ir_tx_z8f0811_hdpvr",
38 "ir_rx_z8f0811_hdpvr",
41 static int hdpvr_new_i2c_ir(struct hdpvr_device *dev, struct i2c_adapter *adap,
42 const char *type, u8 addr)
44 struct i2c_board_info info;
45 struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
46 unsigned short addr_list[2] = { addr, I2C_CLIENT_END };
48 memset(&info, 0, sizeof(struct i2c_board_info));
49 strlcpy(info.type, type, I2C_NAME_SIZE);
51 /* Our default information for ir-kbd-i2c.c to use */
53 case Z8F0811_IR_RX_I2C_ADDR:
54 init_data->ir_codes = RC_MAP_HAUPPAUGE_NEW;
55 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
56 init_data->type = RC_TYPE_RC5;
57 init_data->name = "HD PVR";
58 info.platform_data = init_data;
62 return i2c_new_probed_device(adap, &info, addr_list, NULL) == NULL ?
66 int hdpvr_register_i2c_ir(struct hdpvr_device *dev)
71 for (i = 0; i < ARRAY_SIZE(ir_i2c_addrs); i++)
72 ret += hdpvr_new_i2c_ir(dev, dev->i2c_adapter,
73 ir_devicenames[i], ir_i2c_addrs[i]);
78 static int hdpvr_i2c_read(struct hdpvr_device *dev, unsigned char addr,
82 char *buf = kmalloc(len, GFP_KERNEL);
86 ret = usb_control_msg(dev->udev,
87 usb_rcvctrlpipe(dev->udev, 0),
88 REQTYPE_I2C_READ, CTRL_READ_REQUEST,
89 0x100|addr, 0, buf, len, 1000);
92 memcpy(data, buf, len);
102 static int hdpvr_i2c_write(struct hdpvr_device *dev, unsigned char addr,
106 char *buf = kmalloc(len, GFP_KERNEL);
110 memcpy(buf, data, len);
111 ret = usb_control_msg(dev->udev,
112 usb_sndctrlpipe(dev->udev, 0),
113 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
114 0x100|addr, 0, buf, len, 1000);
119 ret = usb_control_msg(dev->udev,
120 usb_rcvctrlpipe(dev->udev, 0),
121 REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
134 static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
137 struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
138 int retval = 0, i, addr;
143 mutex_lock(&dev->i2c_mutex);
145 for (i = 0; i < num && !retval; i++) {
146 addr = msgs[i].addr << 1;
148 if (msgs[i].flags & I2C_M_RD)
149 retval = hdpvr_i2c_read(dev, addr, msgs[i].buf,
152 retval = hdpvr_i2c_write(dev, addr, msgs[i].buf,
156 mutex_unlock(&dev->i2c_mutex);
158 return retval ? retval : num;
161 static u32 hdpvr_functionality(struct i2c_adapter *adapter)
163 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
166 static struct i2c_algorithm hdpvr_algo = {
167 .master_xfer = hdpvr_transfer,
168 .functionality = hdpvr_functionality,
171 int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
173 struct i2c_adapter *i2c_adap;
174 int retval = -ENOMEM;
176 i2c_adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
177 if (i2c_adap == NULL)
180 strlcpy(i2c_adap->name, "Hauppauge HD PVR I2C",
181 sizeof(i2c_adap->name));
182 i2c_adap->algo = &hdpvr_algo;
183 i2c_adap->owner = THIS_MODULE;
184 i2c_adap->dev.parent = &dev->udev->dev;
186 i2c_set_adapdata(i2c_adap, dev);
188 retval = i2c_add_adapter(i2c_adap);
191 dev->i2c_adapter = i2c_adap;