Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awilliam
GitHub Repository: awilliam/linux-vfio
Path: blob/master/drivers/media/video/hdpvr/hdpvr-i2c.c
17680 views
1
2
/*
3
* Hauppauge HD PVR USB driver
4
*
5
* Copyright (C) 2008 Janne Grunau ([email protected])
6
*
7
* IR device registration code is
8
* Copyright (C) 2010 Andy Walls <[email protected]>
9
*
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.
13
*
14
*/
15
16
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
17
18
#include <linux/i2c.h>
19
#include <linux/slab.h>
20
21
#include "hdpvr.h"
22
23
#define CTRL_READ_REQUEST 0xb8
24
#define CTRL_WRITE_REQUEST 0x38
25
26
#define REQTYPE_I2C_READ 0xb1
27
#define REQTYPE_I2C_WRITE 0xb0
28
#define REQTYPE_I2C_WRITE_STATT 0xd0
29
30
#define Z8F0811_IR_TX_I2C_ADDR 0x70
31
#define Z8F0811_IR_RX_I2C_ADDR 0x71
32
33
34
struct i2c_client *hdpvr_register_ir_tx_i2c(struct hdpvr_device *dev)
35
{
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),
39
};
40
41
init_data->name = "HD-PVR";
42
hdpvr_ir_tx_i2c_board_info.platform_data = init_data;
43
44
return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_tx_i2c_board_info);
45
}
46
47
struct i2c_client *hdpvr_register_ir_rx_i2c(struct hdpvr_device *dev)
48
{
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),
52
};
53
54
/* Our default information for ir-kbd-i2c.c to use */
55
init_data->ir_codes = RC_MAP_HAUPPAUGE;
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
init_data->polling_interval = 405; /* ms, duplicated from Windows */
60
hdpvr_ir_rx_i2c_board_info.platform_data = init_data;
61
62
return i2c_new_device(&dev->i2c_adapter, &hdpvr_ir_rx_i2c_board_info);
63
}
64
65
static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
66
unsigned char addr, char *wdata, int wlen,
67
char *data, int len)
68
{
69
int ret;
70
71
if ((len > sizeof(dev->i2c_buf)) || (wlen > sizeof(dev->i2c_buf)))
72
return -EINVAL;
73
74
if (wlen) {
75
memcpy(&dev->i2c_buf, wdata, wlen);
76
ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
77
REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
78
(bus << 8) | addr, 0, &dev->i2c_buf,
79
wlen, 1000);
80
if (ret < 0)
81
return ret;
82
}
83
84
ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
85
REQTYPE_I2C_READ, CTRL_READ_REQUEST,
86
(bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
87
88
if (ret == len) {
89
memcpy(data, &dev->i2c_buf, len);
90
ret = 0;
91
} else if (ret >= 0)
92
ret = -EIO;
93
94
return ret;
95
}
96
97
static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
98
unsigned char addr, char *data, int len)
99
{
100
int ret;
101
102
if (len > sizeof(dev->i2c_buf))
103
return -EINVAL;
104
105
memcpy(&dev->i2c_buf, data, len);
106
ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
107
REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
108
(bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
109
110
if (ret < 0)
111
return ret;
112
113
ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
114
REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
115
0, 0, &dev->i2c_buf, 2, 1000);
116
117
if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
118
ret = 0;
119
else if (ret >= 0)
120
ret = -EIO;
121
122
return ret;
123
}
124
125
static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
126
int num)
127
{
128
struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
129
int retval = 0, addr;
130
131
if (num <= 0)
132
return 0;
133
134
mutex_lock(&dev->i2c_mutex);
135
136
addr = msgs[0].addr << 1;
137
138
if (num == 1) {
139
if (msgs[0].flags & I2C_M_RD)
140
retval = hdpvr_i2c_read(dev, 1, addr, NULL, 0,
141
msgs[0].buf, msgs[0].len);
142
else
143
retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,
144
msgs[0].len);
145
} else if (num == 2) {
146
if (msgs[0].addr != msgs[1].addr) {
147
v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer "
148
"with conflicting target addresses\n");
149
retval = -EINVAL;
150
goto out;
151
}
152
153
if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {
154
v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with "
155
"r0=%d, r1=%d\n", msgs[0].flags & I2C_M_RD,
156
msgs[1].flags & I2C_M_RD);
157
retval = -EINVAL;
158
goto out;
159
}
160
161
/*
162
* Write followed by atomic read is the only complex xfer that
163
* we actually support here.
164
*/
165
retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,
166
msgs[1].buf, msgs[1].len);
167
} else {
168
v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);
169
}
170
171
out:
172
mutex_unlock(&dev->i2c_mutex);
173
174
return retval ? retval : num;
175
}
176
177
static u32 hdpvr_functionality(struct i2c_adapter *adapter)
178
{
179
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
180
}
181
182
static struct i2c_algorithm hdpvr_algo = {
183
.master_xfer = hdpvr_transfer,
184
.functionality = hdpvr_functionality,
185
};
186
187
static struct i2c_adapter hdpvr_i2c_adapter_template = {
188
.name = "Hauppage HD PVR I2C",
189
.owner = THIS_MODULE,
190
.algo = &hdpvr_algo,
191
};
192
193
static int hdpvr_activate_ir(struct hdpvr_device *dev)
194
{
195
char buffer[2];
196
197
mutex_lock(&dev->i2c_mutex);
198
199
hdpvr_i2c_read(dev, 0, 0x54, NULL, 0, buffer, 1);
200
201
buffer[0] = 0;
202
buffer[1] = 0x8;
203
hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
204
205
buffer[1] = 0x18;
206
hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
207
208
mutex_unlock(&dev->i2c_mutex);
209
210
return 0;
211
}
212
213
int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
214
{
215
int retval = -ENOMEM;
216
217
hdpvr_activate_ir(dev);
218
219
memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template,
220
sizeof(struct i2c_adapter));
221
dev->i2c_adapter.dev.parent = &dev->udev->dev;
222
223
i2c_set_adapdata(&dev->i2c_adapter, dev);
224
225
retval = i2c_add_adapter(&dev->i2c_adapter);
226
227
return retval;
228
}
229
230
#endif
231
232