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