DiBcom: protect the I2C bufer access
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / dvb / frontends / dibx000_common.c
blob774d507b66cc7e841b412efe4c06ea4a6c8c1be5
1 #include <linux/i2c.h>
2 #include <linux/mutex.h>
4 #include "dibx000_common.h"
6 static int debug;
7 module_param(debug, int, 0644);
8 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
10 #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiBX000: "); printk(args); printk("\n"); } } while (0)
12 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
14 int ret;
16 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
17 dprintk("could not acquire lock");
18 return -EINVAL;
21 mst->i2c_write_buffer[0] = (reg >> 8) & 0xff;
22 mst->i2c_write_buffer[1] = reg & 0xff;
23 mst->i2c_write_buffer[2] = (val >> 8) & 0xff;
24 mst->i2c_write_buffer[3] = val & 0xff;
26 memset(mst->msg, 0, sizeof(struct i2c_msg));
27 mst->msg[0].addr = mst->i2c_addr;
28 mst->msg[0].flags = 0;
29 mst->msg[0].buf = mst->i2c_write_buffer;
30 mst->msg[0].len = 4;
32 ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
33 mutex_unlock(&mst->i2c_buffer_lock);
35 return ret;
38 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
40 u16 ret;
42 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
43 dprintk("could not acquire lock");
44 return 0;
47 mst->i2c_write_buffer[0] = reg >> 8;
48 mst->i2c_write_buffer[1] = reg & 0xff;
50 memset(mst->msg, 0, 2 * sizeof(struct i2c_msg));
51 mst->msg[0].addr = mst->i2c_addr;
52 mst->msg[0].flags = 0;
53 mst->msg[0].buf = mst->i2c_write_buffer;
54 mst->msg[0].len = 2;
55 mst->msg[1].addr = mst->i2c_addr;
56 mst->msg[1].flags = I2C_M_RD;
57 mst->msg[1].buf = mst->i2c_read_buffer;
58 mst->msg[1].len = 2;
60 if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
61 dprintk("i2c read error on %d", reg);
63 ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
64 mutex_unlock(&mst->i2c_buffer_lock);
66 return ret;
69 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
71 int i = 100;
72 u16 status;
74 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
77 /* i2c timed out */
78 if (i == 0)
79 return -EREMOTEIO;
81 /* no acknowledge */
82 if ((status & 0x0080) == 0)
83 return -EREMOTEIO;
85 return 0;
88 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
90 u16 data;
91 u16 da;
92 u16 i;
93 u16 txlen = msg->len, len;
94 const u8 *b = msg->buf;
96 while (txlen) {
97 dibx000_read_word(mst, mst->base_reg + 2);
99 len = txlen > 8 ? 8 : txlen;
100 for (i = 0; i < len; i += 2) {
101 data = *b++ << 8;
102 if (i+1 < len)
103 data |= *b++;
104 dibx000_write_word(mst, mst->base_reg, data);
106 da = (((u8) (msg->addr)) << 9) |
107 (1 << 8) |
108 (1 << 7) |
109 (0 << 6) |
110 (0 << 5) |
111 ((len & 0x7) << 2) |
112 (0 << 1) |
113 (0 << 0);
115 if (txlen == msg->len)
116 da |= 1 << 5; /* start */
118 if (txlen-len == 0 && stop)
119 da |= 1 << 6; /* stop */
121 dibx000_write_word(mst, mst->base_reg+1, da);
123 if (dibx000_is_i2c_done(mst) != 0)
124 return -EREMOTEIO;
125 txlen -= len;
128 return 0;
131 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
133 u16 da;
134 u8 *b = msg->buf;
135 u16 rxlen = msg->len, len;
137 while (rxlen) {
138 len = rxlen > 8 ? 8 : rxlen;
139 da = (((u8) (msg->addr)) << 9) |
140 (1 << 8) |
141 (1 << 7) |
142 (0 << 6) |
143 (0 << 5) |
144 ((len & 0x7) << 2) |
145 (1 << 1) |
146 (0 << 0);
148 if (rxlen == msg->len)
149 da |= 1 << 5; /* start */
151 if (rxlen-len == 0)
152 da |= 1 << 6; /* stop */
153 dibx000_write_word(mst, mst->base_reg+1, da);
155 if (dibx000_is_i2c_done(mst) != 0)
156 return -EREMOTEIO;
158 rxlen -= len;
160 while (len) {
161 da = dibx000_read_word(mst, mst->base_reg);
162 *b++ = (da >> 8) & 0xff;
163 len--;
164 if (len >= 1) {
165 *b++ = da & 0xff;
166 len--;
171 return 0;
174 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
176 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
178 if (mst->device_rev < DIB7000MC && speed < 235)
179 speed = 235;
180 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
183 EXPORT_SYMBOL(dibx000_i2c_set_speed);
185 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
187 return I2C_FUNC_I2C;
190 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
191 enum dibx000_i2c_interface intf)
193 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
194 dprintk("selecting interface: %d", intf);
195 mst->selected_interface = intf;
196 return dibx000_write_word(mst, mst->base_reg + 4, intf);
198 return 0;
201 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
203 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
204 int msg_index;
205 int ret = 0;
207 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
208 for (msg_index = 0; msg_index < num; msg_index++) {
209 if (msg[msg_index].flags & I2C_M_RD) {
210 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
211 if (ret != 0)
212 return 0;
213 } else {
214 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
215 if (ret != 0)
216 return 0;
220 return num;
223 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
225 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
226 int msg_index;
227 int ret = 0;
229 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
230 for (msg_index = 0; msg_index < num; msg_index++) {
231 if (msg[msg_index].flags & I2C_M_RD) {
232 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
233 if (ret != 0)
234 return 0;
235 } else {
236 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
237 if (ret != 0)
238 return 0;
242 return num;
245 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
246 .master_xfer = dibx000_i2c_master_xfer_gpio12,
247 .functionality = dibx000_i2c_func,
250 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
251 .master_xfer = dibx000_i2c_master_xfer_gpio34,
252 .functionality = dibx000_i2c_func,
255 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
256 u8 addr, int onoff)
258 u16 val;
261 if (onoff)
262 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
263 else
264 val = 1 << 7;
266 if (mst->device_rev > DIB7000)
267 val <<= 1;
269 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
270 tx[1] = ((mst->base_reg + 1) & 0xff);
271 tx[2] = val >> 8;
272 tx[3] = val & 0xff;
274 return 0;
277 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
278 struct i2c_msg msg[], int num)
280 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
281 int ret;
283 if (num > 32) {
284 dprintk("%s: too much I2C message to be transmitted (%i).\
285 Maximum is 32", __func__, num);
286 return -ENOMEM;
289 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
291 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
292 dprintk("could not acquire lock");
293 return -EINVAL;
296 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
298 /* open the gate */
299 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
300 mst->msg[0].addr = mst->i2c_addr;
301 mst->msg[0].buf = &mst->i2c_write_buffer[0];
302 mst->msg[0].len = 4;
304 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
306 /* close the gate */
307 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
308 mst->msg[num + 1].addr = mst->i2c_addr;
309 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
310 mst->msg[num + 1].len = 4;
312 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
313 num : -EIO);
315 mutex_unlock(&mst->i2c_buffer_lock);
316 return ret;
319 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
320 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
321 .functionality = dibx000_i2c_func,
324 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
325 struct i2c_msg msg[], int num)
327 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
328 int ret;
330 if (num > 32) {
331 dprintk("%s: too much I2C message to be transmitted (%i).\
332 Maximum is 32", __func__, num);
333 return -ENOMEM;
336 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
338 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
339 dprintk("could not acquire lock");
340 return -EINVAL;
342 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
344 /* open the gate */
345 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
346 mst->msg[0].addr = mst->i2c_addr;
347 mst->msg[0].buf = &mst->i2c_write_buffer[0];
348 mst->msg[0].len = 4;
350 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
352 /* close the gate */
353 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
354 mst->msg[num + 1].addr = mst->i2c_addr;
355 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
356 mst->msg[num + 1].len = 4;
358 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
359 num : -EIO);
360 mutex_unlock(&mst->i2c_buffer_lock);
361 return ret;
364 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
365 .master_xfer = dibx000_i2c_gated_tuner_xfer,
366 .functionality = dibx000_i2c_func,
369 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
370 enum dibx000_i2c_interface intf,
371 int gating)
373 struct i2c_adapter *i2c = NULL;
375 switch (intf) {
376 case DIBX000_I2C_INTERFACE_TUNER:
377 if (gating)
378 i2c = &mst->gated_tuner_i2c_adap;
379 break;
380 case DIBX000_I2C_INTERFACE_GPIO_1_2:
381 if (!gating)
382 i2c = &mst->master_i2c_adap_gpio12;
383 break;
384 case DIBX000_I2C_INTERFACE_GPIO_3_4:
385 if (!gating)
386 i2c = &mst->master_i2c_adap_gpio34;
387 break;
388 case DIBX000_I2C_INTERFACE_GPIO_6_7:
389 if (gating)
390 i2c = &mst->master_i2c_adap_gpio67;
391 break;
392 default:
393 printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
394 break;
397 return i2c;
400 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
402 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
404 /* initialize the i2c-master by closing the gate */
405 u8 tx[4];
406 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
408 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
409 i2c_transfer(mst->i2c_adap, &m, 1);
410 mst->selected_interface = 0xff; // the first time force a select of the I2C
411 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
414 EXPORT_SYMBOL(dibx000_reset_i2c_master);
416 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
417 struct i2c_algorithm *algo, const char *name,
418 struct dibx000_i2c_master *mst)
420 strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
421 i2c_adap->algo = algo;
422 i2c_adap->algo_data = NULL;
423 i2c_set_adapdata(i2c_adap, mst);
424 if (i2c_add_adapter(i2c_adap) < 0)
425 return -ENODEV;
426 return 0;
429 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
430 struct i2c_adapter *i2c_adap, u8 i2c_addr)
432 int ret;
434 mutex_init(&mst->i2c_buffer_lock);
435 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
436 dprintk("could not acquire lock");
437 return -EINVAL;
439 memset(mst->msg, 0, sizeof(struct i2c_msg));
440 mst->msg[0].addr = i2c_addr >> 1;
441 mst->msg[0].flags = 0;
442 mst->msg[0].buf = mst->i2c_write_buffer;
443 mst->msg[0].len = 4;
445 mst->device_rev = device_rev;
446 mst->i2c_adap = i2c_adap;
447 mst->i2c_addr = i2c_addr >> 1;
449 if (device_rev == DIB7000P || device_rev == DIB8000)
450 mst->base_reg = 1024;
451 else
452 mst->base_reg = 768;
454 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
455 if (i2c_adapter_init
456 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
457 "DiBX000 tuner I2C bus", mst) != 0)
458 printk(KERN_ERR
459 "DiBX000: could not initialize the tuner i2c_adapter\n");
461 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
462 if (i2c_adapter_init
463 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
464 "DiBX000 master GPIO12 I2C bus", mst) != 0)
465 printk(KERN_ERR
466 "DiBX000: could not initialize the master i2c_adapter\n");
468 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
469 if (i2c_adapter_init
470 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
471 "DiBX000 master GPIO34 I2C bus", mst) != 0)
472 printk(KERN_ERR
473 "DiBX000: could not initialize the master i2c_adapter\n");
475 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
476 if (i2c_adapter_init
477 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
478 "DiBX000 master GPIO67 I2C bus", mst) != 0)
479 printk(KERN_ERR
480 "DiBX000: could not initialize the master i2c_adapter\n");
482 /* initialize the i2c-master by closing the gate */
483 dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0);
485 ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1);
486 mutex_unlock(&mst->i2c_buffer_lock);
488 return ret;
491 EXPORT_SYMBOL(dibx000_init_i2c_master);
493 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
495 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
496 i2c_del_adapter(&mst->master_i2c_adap_gpio12);
497 i2c_del_adapter(&mst->master_i2c_adap_gpio34);
498 i2c_del_adapter(&mst->master_i2c_adap_gpio67);
500 EXPORT_SYMBOL(dibx000_exit_i2c_master);
503 u32 systime(void)
505 struct timespec t;
507 t = current_kernel_time();
508 return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
510 EXPORT_SYMBOL(systime);
512 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
513 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
514 MODULE_LICENSE("GPL");