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 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
23 #define CTRL_READ_REQUEST 0xb8
24 #define CTRL_WRITE_REQUEST 0x38
26 #define REQTYPE_I2C_READ 0xb1
27 #define REQTYPE_I2C_WRITE 0xb0
28 #define REQTYPE_I2C_WRITE_STATT 0xd0
30 #define Z8F0811_IR_TX_I2C_ADDR 0x70
31 #define Z8F0811_IR_RX_I2C_ADDR 0x71
34 struct i2c_client
*hdpvr_register_ir_tx_i2c(struct hdpvr_device
*dev
)
36 struct IR_i2c_init_data
*init_data
= &dev
->ir_i2c_init_data
;
37 struct i2c_board_info hdpvr_ir_tx_i2c_board_info
= {
38 I2C_BOARD_INFO("ir_tx_z8f0811_hdpvr", Z8F0811_IR_TX_I2C_ADDR
),
41 init_data
->name
= "HD-PVR";
42 hdpvr_ir_tx_i2c_board_info
.platform_data
= init_data
;
44 return i2c_new_device(&dev
->i2c_adapter
, &hdpvr_ir_tx_i2c_board_info
);
47 struct i2c_client
*hdpvr_register_ir_rx_i2c(struct hdpvr_device
*dev
)
49 struct IR_i2c_init_data
*init_data
= &dev
->ir_i2c_init_data
;
50 struct i2c_board_info hdpvr_ir_rx_i2c_board_info
= {
51 I2C_BOARD_INFO("ir_rx_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR
),
54 /* Our default information for ir-kbd-i2c.c to use */
55 init_data
->ir_codes
= RC_MAP_HAUPPAUGE_NEW
;
56 init_data
->internal_get_key_func
= IR_KBD_GET_KEY_HAUP_XVR
;
57 init_data
->type
= RC_TYPE_RC5
;
58 init_data
->name
= "HD-PVR";
59 hdpvr_ir_rx_i2c_board_info
.platform_data
= init_data
;
61 return i2c_new_device(&dev
->i2c_adapter
, &hdpvr_ir_rx_i2c_board_info
);
64 static int hdpvr_i2c_read(struct hdpvr_device
*dev
, int bus
,
65 unsigned char addr
, char *data
, int len
)
69 if (len
> sizeof(dev
->i2c_buf
))
72 ret
= usb_control_msg(dev
->udev
,
73 usb_rcvctrlpipe(dev
->udev
, 0),
74 REQTYPE_I2C_READ
, CTRL_READ_REQUEST
,
75 (bus
<< 8) | addr
, 0, &dev
->i2c_buf
, len
, 1000);
78 memcpy(data
, &dev
->i2c_buf
, len
);
86 static int hdpvr_i2c_write(struct hdpvr_device
*dev
, int bus
,
87 unsigned char addr
, char *data
, int len
)
91 if (len
> sizeof(dev
->i2c_buf
))
94 memcpy(&dev
->i2c_buf
, data
, len
);
95 ret
= usb_control_msg(dev
->udev
,
96 usb_sndctrlpipe(dev
->udev
, 0),
97 REQTYPE_I2C_WRITE
, CTRL_WRITE_REQUEST
,
98 (bus
<< 8) | addr
, 0, &dev
->i2c_buf
, len
, 1000);
103 ret
= usb_control_msg(dev
->udev
,
104 usb_rcvctrlpipe(dev
->udev
, 0),
105 REQTYPE_I2C_WRITE_STATT
, CTRL_READ_REQUEST
,
106 0, 0, &dev
->i2c_buf
, 2, 1000);
108 if ((ret
== 2) && (dev
->i2c_buf
[1] == (len
- 1)))
116 static int hdpvr_transfer(struct i2c_adapter
*i2c_adapter
, struct i2c_msg
*msgs
,
119 struct hdpvr_device
*dev
= i2c_get_adapdata(i2c_adapter
);
120 int retval
= 0, i
, addr
;
125 mutex_lock(&dev
->i2c_mutex
);
127 for (i
= 0; i
< num
&& !retval
; i
++) {
128 addr
= msgs
[i
].addr
<< 1;
130 if (msgs
[i
].flags
& I2C_M_RD
)
131 retval
= hdpvr_i2c_read(dev
, 1, addr
, msgs
[i
].buf
,
134 retval
= hdpvr_i2c_write(dev
, 1, addr
, msgs
[i
].buf
,
138 mutex_unlock(&dev
->i2c_mutex
);
140 return retval
? retval
: num
;
143 static u32
hdpvr_functionality(struct i2c_adapter
*adapter
)
145 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
148 static struct i2c_algorithm hdpvr_algo
= {
149 .master_xfer
= hdpvr_transfer
,
150 .functionality
= hdpvr_functionality
,
153 static struct i2c_adapter hdpvr_i2c_adapter_template
= {
154 .name
= "Hauppage HD PVR I2C",
155 .owner
= THIS_MODULE
,
159 static int hdpvr_activate_ir(struct hdpvr_device
*dev
)
163 mutex_lock(&dev
->i2c_mutex
);
165 hdpvr_i2c_read(dev
, 0, 0x54, buffer
, 1);
169 hdpvr_i2c_write(dev
, 1, 0x54, buffer
, 2);
172 hdpvr_i2c_write(dev
, 1, 0x54, buffer
, 2);
174 mutex_unlock(&dev
->i2c_mutex
);
179 int hdpvr_register_i2c_adapter(struct hdpvr_device
*dev
)
181 int retval
= -ENOMEM
;
183 hdpvr_activate_ir(dev
);
185 memcpy(&dev
->i2c_adapter
, &hdpvr_i2c_adapter_template
,
186 sizeof(struct i2c_adapter
));
187 dev
->i2c_adapter
.dev
.parent
= &dev
->udev
->dev
;
189 i2c_set_adapdata(&dev
->i2c_adapter
, dev
);
191 retval
= i2c_add_adapter(&dev
->i2c_adapter
);