1 /*
2  *
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20 
21 #include <linux/i2c.h>
22 #include <linux/module.h>
23 #include <media/i2c/ir-kbd-i2c.h>
24 #include "pvrusb2-i2c-core.h"
25 #include "pvrusb2-hdw-internal.h"
26 #include "pvrusb2-debug.h"
27 #include "pvrusb2-fx2-cmd.h"
28 #include "pvrusb2.h"
29 
30 #define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
31 
32 /*
33 
34   This module attempts to implement a compliant I2C adapter for the pvrusb2
35   device.
36 
37 */
38 
39 static unsigned int i2c_scan;
40 module_param(i2c_scan, int, S_IRUGO|S_IWUSR);
41 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
42 
43 static int ir_mode[PVR_NUM] = { [0 ... PVR_NUM-1] = 1 };
44 module_param_array(ir_mode, int, NULL, 0444);
45 MODULE_PARM_DESC(ir_mode,"specify: 0=disable IR reception, 1=normal IR");
46 
47 static int pvr2_disable_ir_video;
48 module_param_named(disable_autoload_ir_video, pvr2_disable_ir_video,
49 		   int, S_IRUGO|S_IWUSR);
50 MODULE_PARM_DESC(disable_autoload_ir_video,
51 		 "1=do not try to autoload ir_video IR receiver");
52 
53 static int pvr2_i2c_write(struct pvr2_hdw *hdw, /* Context */
54 			  u8 i2c_addr,      /* I2C address we're talking to */
55 			  u8 *data,         /* Data to write */
56 			  u16 length)       /* Size of data to write */
57 {
58 	/* Return value - default 0 means success */
59 	int ret;
60 
61 
62 	if (!data) length = 0;
63 	if (length > (sizeof(hdw->cmd_buffer) - 3)) {
64 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
65 			   "Killing an I2C write to %u that is too large (desired=%u limit=%u)",
66 			   i2c_addr,
67 			   length,(unsigned int)(sizeof(hdw->cmd_buffer) - 3));
68 		return -ENOTSUPP;
69 	}
70 
71 	LOCK_TAKE(hdw->ctl_lock);
72 
73 	/* Clear the command buffer (likely to be paranoia) */
74 	memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
75 
76 	/* Set up command buffer for an I2C write */
77 	hdw->cmd_buffer[0] = FX2CMD_I2C_WRITE;      /* write prefix */
78 	hdw->cmd_buffer[1] = i2c_addr;  /* i2c addr of chip */
79 	hdw->cmd_buffer[2] = length;    /* length of what follows */
80 	if (length) memcpy(hdw->cmd_buffer + 3, data, length);
81 
82 	/* Do the operation */
83 	ret = pvr2_send_request(hdw,
84 				hdw->cmd_buffer,
85 				length + 3,
86 				hdw->cmd_buffer,
87 				1);
88 	if (!ret) {
89 		if (hdw->cmd_buffer[0] != 8) {
90 			ret = -EIO;
91 			if (hdw->cmd_buffer[0] != 7) {
92 				trace_i2c("unexpected status from i2_write[%d]: %d",
93 					  i2c_addr,hdw->cmd_buffer[0]);
94 			}
95 		}
96 	}
97 
98 	LOCK_GIVE(hdw->ctl_lock);
99 
100 	return ret;
101 }
102 
103 static int pvr2_i2c_read(struct pvr2_hdw *hdw, /* Context */
104 			 u8 i2c_addr,       /* I2C address we're talking to */
105 			 u8 *data,          /* Data to write */
106 			 u16 dlen,          /* Size of data to write */
107 			 u8 *res,           /* Where to put data we read */
108 			 u16 rlen)          /* Amount of data to read */
109 {
110 	/* Return value - default 0 means success */
111 	int ret;
112 
113 
114 	if (!data) dlen = 0;
115 	if (dlen > (sizeof(hdw->cmd_buffer) - 4)) {
116 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
117 			   "Killing an I2C read to %u that has wlen too large (desired=%u limit=%u)",
118 			   i2c_addr,
119 			   dlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 4));
120 		return -ENOTSUPP;
121 	}
122 	if (res && (rlen > (sizeof(hdw->cmd_buffer) - 1))) {
123 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
124 			   "Killing an I2C read to %u that has rlen too large (desired=%u limit=%u)",
125 			   i2c_addr,
126 			   rlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 1));
127 		return -ENOTSUPP;
128 	}
129 
130 	LOCK_TAKE(hdw->ctl_lock);
131 
132 	/* Clear the command buffer (likely to be paranoia) */
133 	memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
134 
135 	/* Set up command buffer for an I2C write followed by a read */
136 	hdw->cmd_buffer[0] = FX2CMD_I2C_READ;  /* read prefix */
137 	hdw->cmd_buffer[1] = dlen;  /* arg length */
138 	hdw->cmd_buffer[2] = rlen;  /* answer length. Device will send one
139 				       more byte (status). */
140 	hdw->cmd_buffer[3] = i2c_addr;  /* i2c addr of chip */
141 	if (dlen) memcpy(hdw->cmd_buffer + 4, data, dlen);
142 
143 	/* Do the operation */
144 	ret = pvr2_send_request(hdw,
145 				hdw->cmd_buffer,
146 				4 + dlen,
147 				hdw->cmd_buffer,
148 				rlen + 1);
149 	if (!ret) {
150 		if (hdw->cmd_buffer[0] != 8) {
151 			ret = -EIO;
152 			if (hdw->cmd_buffer[0] != 7) {
153 				trace_i2c("unexpected status from i2_read[%d]: %d",
154 					  i2c_addr,hdw->cmd_buffer[0]);
155 			}
156 		}
157 	}
158 
159 	/* Copy back the result */
160 	if (res && rlen) {
161 		if (ret) {
162 			/* Error, just blank out the return buffer */
163 			memset(res, 0, rlen);
164 		} else {
165 			memcpy(res, hdw->cmd_buffer + 1, rlen);
166 		}
167 	}
168 
169 	LOCK_GIVE(hdw->ctl_lock);
170 
171 	return ret;
172 }
173 
174 /* This is the common low level entry point for doing I2C operations to the
175    hardware. */
176 static int pvr2_i2c_basic_op(struct pvr2_hdw *hdw,
177 			     u8 i2c_addr,
178 			     u8 *wdata,
179 			     u16 wlen,
180 			     u8 *rdata,
181 			     u16 rlen)
182 {
183 	if (!rdata) rlen = 0;
184 	if (!wdata) wlen = 0;
185 	if (rlen || !wlen) {
186 		return pvr2_i2c_read(hdw,i2c_addr,wdata,wlen,rdata,rlen);
187 	} else {
188 		return pvr2_i2c_write(hdw,i2c_addr,wdata,wlen);
189 	}
190 }
191 
192 
193 /* This is a special entry point for cases of I2C transaction attempts to
194    the IR receiver.  The implementation here simulates the IR receiver by
195    issuing a command to the FX2 firmware and using that response to return
196    what the real I2C receiver would have returned.  We use this for 24xxx
197    devices, where the IR receiver chip has been removed and replaced with
198    FX2 related logic. */
199 static int i2c_24xxx_ir(struct pvr2_hdw *hdw,
200 			u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
201 {
202 	u8 dat[4];
203 	unsigned int stat;
204 
205 	if (!(rlen || wlen)) {
206 		/* This is a probe attempt.  Just let it succeed. */
207 		return 0;
208 	}
209 
210 	/* We don't understand this kind of transaction */
211 	if ((wlen != 0) || (rlen == 0)) return -EIO;
212 
213 	if (rlen < 3) {
214 		/* Mike Isely <isely@pobox.com> Appears to be a probe
215 		   attempt from lirc.  Just fill in zeroes and return.  If
216 		   we try instead to do the full transaction here, then bad
217 		   things seem to happen within the lirc driver module
218 		   (version 0.8.0-7 sources from Debian, when run under
219 		   vanilla 2.6.17.6 kernel) - and I don't have the patience
220 		   to chase it down. */
221 		if (rlen > 0) rdata[0] = 0;
222 		if (rlen > 1) rdata[1] = 0;
223 		return 0;
224 	}
225 
226 	/* Issue a command to the FX2 to read the IR receiver. */
227 	LOCK_TAKE(hdw->ctl_lock); do {
228 		hdw->cmd_buffer[0] = FX2CMD_GET_IR_CODE;
229 		stat = pvr2_send_request(hdw,
230 					 hdw->cmd_buffer,1,
231 					 hdw->cmd_buffer,4);
232 		dat[0] = hdw->cmd_buffer[0];
233 		dat[1] = hdw->cmd_buffer[1];
234 		dat[2] = hdw->cmd_buffer[2];
235 		dat[3] = hdw->cmd_buffer[3];
236 	} while (0); LOCK_GIVE(hdw->ctl_lock);
237 
238 	/* Give up if that operation failed. */
239 	if (stat != 0) return stat;
240 
241 	/* Mangle the results into something that looks like the real IR
242 	   receiver. */
243 	rdata[2] = 0xc1;
244 	if (dat[0] != 1) {
245 		/* No code received. */
246 		rdata[0] = 0;
247 		rdata[1] = 0;
248 	} else {
249 		u16 val;
250 		/* Mash the FX2 firmware-provided IR code into something
251 		   that the normal i2c chip-level driver expects. */
252 		val = dat[1];
253 		val <<= 8;
254 		val |= dat[2];
255 		val >>= 1;
256 		val &= ~0x0003;
257 		val |= 0x8000;
258 		rdata[0] = (val >> 8) & 0xffu;
259 		rdata[1] = val & 0xffu;
260 	}
261 
262 	return 0;
263 }
264 
265 /* This is a special entry point that is entered if an I2C operation is
266    attempted to a wm8775 chip on model 24xxx hardware.  Autodetect of this
267    part doesn't work, but we know it is really there.  So let's look for
268    the autodetect attempt and just return success if we see that. */
269 static int i2c_hack_wm8775(struct pvr2_hdw *hdw,
270 			   u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
271 {
272 	if (!(rlen || wlen)) {
273 		// This is a probe attempt.  Just let it succeed.
274 		return 0;
275 	}
276 	return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
277 }
278 
279 /* This is an entry point designed to always fail any attempt to perform a
280    transfer.  We use this to cause certain I2C addresses to not be
281    probed. */
282 static int i2c_black_hole(struct pvr2_hdw *hdw,
283 			   u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
284 {
285 	return -EIO;
286 }
287 
288 /* This is a special entry point that is entered if an I2C operation is
289    attempted to a cx25840 chip on model 24xxx hardware.  This chip can
290    sometimes wedge itself.  Worse still, when this happens msp3400 can
291    falsely detect this part and then the system gets hosed up after msp3400
292    gets confused and dies.  What we want to do here is try to keep msp3400
293    away and also try to notice if the chip is wedged and send a warning to
294    the system log. */
295 static int i2c_hack_cx25840(struct pvr2_hdw *hdw,
296 			    u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
297 {
298 	int ret;
299 	unsigned int subaddr;
300 	u8 wbuf[2];
301 	int state = hdw->i2c_cx25840_hack_state;
302 
303 	if (!(rlen || wlen)) {
304 		// Probe attempt - always just succeed and don't bother the
305 		// hardware (this helps to make the state machine further
306 		// down somewhat easier).
307 		return 0;
308 	}
309 
310 	if (state == 3) {
311 		return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
312 	}
313 
314 	/* We're looking for the exact pattern where the revision register
315 	   is being read.  The cx25840 module will always look at the
316 	   revision register first.  Any other pattern of access therefore
317 	   has to be a probe attempt from somebody else so we'll reject it.
318 	   Normally we could just let each client just probe the part
319 	   anyway, but when the cx25840 is wedged, msp3400 will get a false
320 	   positive and that just screws things up... */
321 
322 	if (wlen == 0) {
323 		switch (state) {
324 		case 1: subaddr = 0x0100; break;
325 		case 2: subaddr = 0x0101; break;
326 		default: goto fail;
327 		}
328 	} else if (wlen == 2) {
329 		subaddr = (wdata[0] << 8) | wdata[1];
330 		switch (subaddr) {
331 		case 0x0100: state = 1; break;
332 		case 0x0101: state = 2; break;
333 		default: goto fail;
334 		}
335 	} else {
336 		goto fail;
337 	}
338 	if (!rlen) goto success;
339 	state = 0;
340 	if (rlen != 1) goto fail;
341 
342 	/* If we get to here then we have a legitimate read for one of the
343 	   two revision bytes, so pass it through. */
344 	wbuf[0] = subaddr >> 8;
345 	wbuf[1] = subaddr;
346 	ret = pvr2_i2c_basic_op(hdw,i2c_addr,wbuf,2,rdata,rlen);
347 
348 	if ((ret != 0) || (*rdata == 0x04) || (*rdata == 0x0a)) {
349 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
350 			   "WARNING: Detected a wedged cx25840 chip; the device will not work.");
351 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
352 			   "WARNING: Try power cycling the pvrusb2 device.");
353 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
354 			   "WARNING: Disabling further access to the device to prevent other foul-ups.");
355 		// This blocks all further communication with the part.
356 		hdw->i2c_func[0x44] = NULL;
357 		pvr2_hdw_render_useless(hdw);
358 		goto fail;
359 	}
360 
361 	/* Success! */
362 	pvr2_trace(PVR2_TRACE_CHIPS,"cx25840 appears to be OK.");
363 	state = 3;
364 
365  success:
366 	hdw->i2c_cx25840_hack_state = state;
367 	return 0;
368 
369  fail:
370 	hdw->i2c_cx25840_hack_state = state;
371 	return -EIO;
372 }
373 
374 /* This is a very, very limited I2C adapter implementation.  We can only
375    support what we actually know will work on the device... */
376 static int pvr2_i2c_xfer(struct i2c_adapter *i2c_adap,
377 			 struct i2c_msg msgs[],
378 			 int num)
379 {
380 	int ret = -ENOTSUPP;
381 	pvr2_i2c_func funcp = NULL;
382 	struct pvr2_hdw *hdw = (struct pvr2_hdw *)(i2c_adap->algo_data);
383 
384 	if (!num) {
385 		ret = -EINVAL;
386 		goto done;
387 	}
388 	if (msgs[0].addr < PVR2_I2C_FUNC_CNT) {
389 		funcp = hdw->i2c_func[msgs[0].addr];
390 	}
391 	if (!funcp) {
392 		ret = -EIO;
393 		goto done;
394 	}
395 
396 	if (num == 1) {
397 		if (msgs[0].flags & I2C_M_RD) {
398 			/* Simple read */
399 			u16 tcnt,bcnt,offs;
400 			if (!msgs[0].len) {
401 				/* Length == 0 read.  This is a probe. */
402 				if (funcp(hdw,msgs[0].addr,NULL,0,NULL,0)) {
403 					ret = -EIO;
404 					goto done;
405 				}
406 				ret = 1;
407 				goto done;
408 			}
409 			/* If the read is short enough we'll do the whole
410 			   thing atomically.  Otherwise we have no choice
411 			   but to break apart the reads. */
412 			tcnt = msgs[0].len;
413 			offs = 0;
414 			while (tcnt) {
415 				bcnt = tcnt;
416 				if (bcnt > sizeof(hdw->cmd_buffer)-1) {
417 					bcnt = sizeof(hdw->cmd_buffer)-1;
418 				}
419 				if (funcp(hdw,msgs[0].addr,NULL,0,
420 					  msgs[0].buf+offs,bcnt)) {
421 					ret = -EIO;
422 					goto done;
423 				}
424 				offs += bcnt;
425 				tcnt -= bcnt;
426 			}
427 			ret = 1;
428 			goto done;
429 		} else {
430 			/* Simple write */
431 			ret = 1;
432 			if (funcp(hdw,msgs[0].addr,
433 				  msgs[0].buf,msgs[0].len,NULL,0)) {
434 				ret = -EIO;
435 			}
436 			goto done;
437 		}
438 	} else if (num == 2) {
439 		if (msgs[0].addr != msgs[1].addr) {
440 			trace_i2c("i2c refusing 2 phase transfer with conflicting target addresses");
441 			ret = -ENOTSUPP;
442 			goto done;
443 		}
444 		if ((!((msgs[0].flags & I2C_M_RD))) &&
445 		    (msgs[1].flags & I2C_M_RD)) {
446 			u16 tcnt,bcnt,wcnt,offs;
447 			/* Write followed by atomic read.  If the read
448 			   portion is short enough we'll do the whole thing
449 			   atomically.  Otherwise we have no choice but to
450 			   break apart the reads. */
451 			tcnt = msgs[1].len;
452 			wcnt = msgs[0].len;
453 			offs = 0;
454 			while (tcnt || wcnt) {
455 				bcnt = tcnt;
456 				if (bcnt > sizeof(hdw->cmd_buffer)-1) {
457 					bcnt = sizeof(hdw->cmd_buffer)-1;
458 				}
459 				if (funcp(hdw,msgs[0].addr,
460 					  msgs[0].buf,wcnt,
461 					  msgs[1].buf+offs,bcnt)) {
462 					ret = -EIO;
463 					goto done;
464 				}
465 				offs += bcnt;
466 				tcnt -= bcnt;
467 				wcnt = 0;
468 			}
469 			ret = 2;
470 			goto done;
471 		} else {
472 			trace_i2c("i2c refusing complex transfer read0=%d read1=%d",
473 				  (msgs[0].flags & I2C_M_RD),
474 				  (msgs[1].flags & I2C_M_RD));
475 		}
476 	} else {
477 		trace_i2c("i2c refusing %d phase transfer",num);
478 	}
479 
480  done:
481 	if (pvrusb2_debug & PVR2_TRACE_I2C_TRAF) {
482 		unsigned int idx,offs,cnt;
483 		for (idx = 0; idx < num; idx++) {
484 			cnt = msgs[idx].len;
485 			printk(KERN_INFO
486 			       "pvrusb2 i2c xfer %u/%u: addr=0x%x len=%d %s",
487 			       idx+1,num,
488 			       msgs[idx].addr,
489 			       cnt,
490 			       (msgs[idx].flags & I2C_M_RD ?
491 				"read" : "write"));
492 			if ((ret > 0) || !(msgs[idx].flags & I2C_M_RD)) {
493 				if (cnt > 8) cnt = 8;
494 				printk(KERN_CONT " [");
495 				for (offs = 0; offs < (cnt>8?8:cnt); offs++) {
496 					if (offs) printk(KERN_CONT " ");
497 					printk(KERN_CONT "%02x",msgs[idx].buf[offs]);
498 				}
499 				if (offs < cnt) printk(KERN_CONT " ...");
500 				printk(KERN_CONT "]");
501 			}
502 			if (idx+1 == num) {
503 				printk(KERN_CONT " result=%d",ret);
504 			}
505 			printk(KERN_CONT "\n");
506 		}
507 		if (!num) {
508 			printk(KERN_INFO
509 			       "pvrusb2 i2c xfer null transfer result=%d\n",
510 			       ret);
511 		}
512 	}
513 	return ret;
514 }
515 
516 static u32 pvr2_i2c_functionality(struct i2c_adapter *adap)
517 {
518 	return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
519 }
520 
521 static struct i2c_algorithm pvr2_i2c_algo_template = {
522 	.master_xfer   = pvr2_i2c_xfer,
523 	.functionality = pvr2_i2c_functionality,
524 };
525 
526 static struct i2c_adapter pvr2_i2c_adap_template = {
527 	.owner         = THIS_MODULE,
528 	.class	       = 0,
529 };
530 
531 
532 /* Return true if device exists at given address */
533 static int do_i2c_probe(struct pvr2_hdw *hdw, int addr)
534 {
535 	struct i2c_msg msg[1];
536 	int rc;
537 	msg[0].addr = 0;
538 	msg[0].flags = I2C_M_RD;
539 	msg[0].len = 0;
540 	msg[0].buf = NULL;
541 	msg[0].addr = addr;
542 	rc = i2c_transfer(&hdw->i2c_adap, msg, ARRAY_SIZE(msg));
543 	return rc == 1;
544 }
545 
546 static void do_i2c_scan(struct pvr2_hdw *hdw)
547 {
548 	int i;
549 	printk(KERN_INFO "%s: i2c scan beginning\n", hdw->name);
550 	for (i = 0; i < 128; i++) {
551 		if (do_i2c_probe(hdw, i)) {
552 			printk(KERN_INFO "%s: i2c scan: found device @ 0x%x\n",
553 			       hdw->name, i);
554 		}
555 	}
556 	printk(KERN_INFO "%s: i2c scan done.\n", hdw->name);
557 }
558 
559 static void pvr2_i2c_register_ir(struct pvr2_hdw *hdw)
560 {
561 	struct i2c_board_info info;
562 	struct IR_i2c_init_data *init_data = &hdw->ir_init_data;
563 	if (pvr2_disable_ir_video) {
564 		pvr2_trace(PVR2_TRACE_INFO,
565 			   "Automatic binding of ir_video has been disabled.");
566 		return;
567 	}
568 	memset(&info, 0, sizeof(struct i2c_board_info));
569 	switch (hdw->ir_scheme_active) {
570 	case PVR2_IR_SCHEME_24XXX: /* FX2-controlled IR */
571 	case PVR2_IR_SCHEME_29XXX: /* Original 29xxx device */
572 		init_data->ir_codes              = RC_MAP_HAUPPAUGE;
573 		init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP;
574 		init_data->type                  = RC_BIT_RC5;
575 		init_data->name                  = hdw->hdw_desc->description;
576 		init_data->polling_interval      = 100; /* ms From ir-kbd-i2c */
577 		/* IR Receiver */
578 		info.addr          = 0x18;
579 		info.platform_data = init_data;
580 		strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
581 		pvr2_trace(PVR2_TRACE_INFO, "Binding %s to i2c address 0x%02x.",
582 			   info.type, info.addr);
583 		i2c_new_device(&hdw->i2c_adap, &info);
584 		break;
585 	case PVR2_IR_SCHEME_ZILOG:     /* HVR-1950 style */
586 	case PVR2_IR_SCHEME_24XXX_MCE: /* 24xxx MCE device */
587 		init_data->ir_codes              = RC_MAP_HAUPPAUGE;
588 		init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
589 		init_data->type                  = RC_BIT_RC5 | RC_BIT_RC6_MCE |
590 							RC_BIT_RC6_6A_32;
591 		init_data->name                  = hdw->hdw_desc->description;
592 		/* IR Receiver */
593 		info.addr          = 0x71;
594 		info.platform_data = init_data;
595 		strlcpy(info.type, "ir_rx_z8f0811_haup", I2C_NAME_SIZE);
596 		pvr2_trace(PVR2_TRACE_INFO, "Binding %s to i2c address 0x%02x.",
597 			   info.type, info.addr);
598 		i2c_new_device(&hdw->i2c_adap, &info);
599 		/* IR Trasmitter */
600 		info.addr          = 0x70;
601 		info.platform_data = init_data;
602 		strlcpy(info.type, "ir_tx_z8f0811_haup", I2C_NAME_SIZE);
603 		pvr2_trace(PVR2_TRACE_INFO, "Binding %s to i2c address 0x%02x.",
604 			   info.type, info.addr);
605 		i2c_new_device(&hdw->i2c_adap, &info);
606 		break;
607 	default:
608 		/* The device either doesn't support I2C-based IR or we
609 		   don't know (yet) how to operate IR on the device. */
610 		break;
611 	}
612 }
613 
614 void pvr2_i2c_core_init(struct pvr2_hdw *hdw)
615 {
616 	unsigned int idx;
617 
618 	/* The default action for all possible I2C addresses is just to do
619 	   the transfer normally. */
620 	for (idx = 0; idx < PVR2_I2C_FUNC_CNT; idx++) {
621 		hdw->i2c_func[idx] = pvr2_i2c_basic_op;
622 	}
623 
624 	/* However, deal with various special cases for 24xxx hardware. */
625 	if (ir_mode[hdw->unit_number] == 0) {
626 		printk(KERN_INFO "%s: IR disabled\n",hdw->name);
627 		hdw->i2c_func[0x18] = i2c_black_hole;
628 	} else if (ir_mode[hdw->unit_number] == 1) {
629 		if (hdw->ir_scheme_active == PVR2_IR_SCHEME_24XXX) {
630 			/* Set up translation so that our IR looks like a
631 			   29xxx device */
632 			hdw->i2c_func[0x18] = i2c_24xxx_ir;
633 		}
634 	}
635 	if (hdw->hdw_desc->flag_has_cx25840) {
636 		hdw->i2c_func[0x44] = i2c_hack_cx25840;
637 	}
638 	if (hdw->hdw_desc->flag_has_wm8775) {
639 		hdw->i2c_func[0x1b] = i2c_hack_wm8775;
640 	}
641 
642 	// Configure the adapter and set up everything else related to it.
643 	hdw->i2c_adap = pvr2_i2c_adap_template;
644 	hdw->i2c_algo = pvr2_i2c_algo_template;
645 	strlcpy(hdw->i2c_adap.name,hdw->name,sizeof(hdw->i2c_adap.name));
646 	hdw->i2c_adap.dev.parent = &hdw->usb_dev->dev;
647 	hdw->i2c_adap.algo = &hdw->i2c_algo;
648 	hdw->i2c_adap.algo_data = hdw;
649 	hdw->i2c_linked = !0;
650 	i2c_set_adapdata(&hdw->i2c_adap, &hdw->v4l2_dev);
651 	i2c_add_adapter(&hdw->i2c_adap);
652 	if (hdw->i2c_func[0x18] == i2c_24xxx_ir) {
653 		/* Probe for a different type of IR receiver on this
654 		   device.  This is really the only way to differentiate
655 		   older 24xxx devices from 24xxx variants that include an
656 		   IR blaster.  If the IR blaster is present, the IR
657 		   receiver is part of that chip and thus we must disable
658 		   the emulated IR receiver. */
659 		if (do_i2c_probe(hdw, 0x71)) {
660 			pvr2_trace(PVR2_TRACE_INFO,
661 				   "Device has newer IR hardware; disabling unneeded virtual IR device");
662 			hdw->i2c_func[0x18] = NULL;
663 			/* Remember that this is a different device... */
664 			hdw->ir_scheme_active = PVR2_IR_SCHEME_24XXX_MCE;
665 		}
666 	}
667 	if (i2c_scan) do_i2c_scan(hdw);
668 
669 	pvr2_i2c_register_ir(hdw);
670 }
671 
672 void pvr2_i2c_core_done(struct pvr2_hdw *hdw)
673 {
674 	if (hdw->i2c_linked) {
675 		i2c_del_adapter(&hdw->i2c_adap);
676 		hdw->i2c_linked = 0;
677 	}
678 }
679