Merge branch 'upstream-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-i2c-core.c
1 /*
2  *
3  *  $Id$
4  *
5  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "pvrusb2-i2c-core.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include "pvrusb2-debug.h"
25 #include "pvrusb2-fx2-cmd.h"
26 #include "pvrusb2.h"
27
28 #define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
29
30 /*
31
32   This module attempts to implement a compliant I2C adapter for the pvrusb2
33   device.  By doing this we can then make use of existing functionality in
34   V4L (e.g. tuner.c) rather than rolling our own.
35
36 */
37
38 static unsigned int i2c_scan = 0;
39 module_param(i2c_scan, int, S_IRUGO|S_IWUSR);
40 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
41
42 static int ir_mode[PVR_NUM] = { [0 ... PVR_NUM-1] = 1 };
43 module_param_array(ir_mode, int, NULL, 0444);
44 MODULE_PARM_DESC(ir_mode,"specify: 0=disable IR reception, 1=normal IR");
45
46 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client *cp,
47                                              unsigned int detail,
48                                              char *buf,unsigned int maxlen);
49
50 static int pvr2_i2c_write(struct pvr2_hdw *hdw, /* Context */
51                           u8 i2c_addr,      /* I2C address we're talking to */
52                           u8 *data,         /* Data to write */
53                           u16 length)       /* Size of data to write */
54 {
55         /* Return value - default 0 means success */
56         int ret;
57
58
59         if (!data) length = 0;
60         if (length > (sizeof(hdw->cmd_buffer) - 3)) {
61                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
62                            "Killing an I2C write to %u that is too large"
63                            " (desired=%u limit=%u)",
64                            i2c_addr,
65                            length,(unsigned int)(sizeof(hdw->cmd_buffer) - 3));
66                 return -ENOTSUPP;
67         }
68
69         LOCK_TAKE(hdw->ctl_lock);
70
71         /* Clear the command buffer (likely to be paranoia) */
72         memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
73
74         /* Set up command buffer for an I2C write */
75         hdw->cmd_buffer[0] = FX2CMD_I2C_WRITE;      /* write prefix */
76         hdw->cmd_buffer[1] = i2c_addr;  /* i2c addr of chip */
77         hdw->cmd_buffer[2] = length;    /* length of what follows */
78         if (length) memcpy(hdw->cmd_buffer + 3, data, length);
79
80         /* Do the operation */
81         ret = pvr2_send_request(hdw,
82                                 hdw->cmd_buffer,
83                                 length + 3,
84                                 hdw->cmd_buffer,
85                                 1);
86         if (!ret) {
87                 if (hdw->cmd_buffer[0] != 8) {
88                         ret = -EIO;
89                         if (hdw->cmd_buffer[0] != 7) {
90                                 trace_i2c("unexpected status"
91                                           " from i2_write[%d]: %d",
92                                           i2c_addr,hdw->cmd_buffer[0]);
93                         }
94                 }
95         }
96
97         LOCK_GIVE(hdw->ctl_lock);
98
99         return ret;
100 }
101
102 static int pvr2_i2c_read(struct pvr2_hdw *hdw, /* Context */
103                          u8 i2c_addr,       /* I2C address we're talking to */
104                          u8 *data,          /* Data to write */
105                          u16 dlen,          /* Size of data to write */
106                          u8 *res,           /* Where to put data we read */
107                          u16 rlen)          /* Amount of data to read */
108 {
109         /* Return value - default 0 means success */
110         int ret;
111
112
113         if (!data) dlen = 0;
114         if (dlen > (sizeof(hdw->cmd_buffer) - 4)) {
115                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
116                            "Killing an I2C read to %u that has wlen too large"
117                            " (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"
125                            " (desired=%u limit=%u)",
126                            i2c_addr,
127                            rlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 1));
128                 return -ENOTSUPP;
129         }
130
131         LOCK_TAKE(hdw->ctl_lock);
132
133         /* Clear the command buffer (likely to be paranoia) */
134         memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
135
136         /* Set up command buffer for an I2C write followed by a read */
137         hdw->cmd_buffer[0] = FX2CMD_I2C_READ;  /* read prefix */
138         hdw->cmd_buffer[1] = dlen;  /* arg length */
139         hdw->cmd_buffer[2] = rlen;  /* answer length. Device will send one
140                                        more byte (status). */
141         hdw->cmd_buffer[3] = i2c_addr;  /* i2c addr of chip */
142         if (dlen) memcpy(hdw->cmd_buffer + 4, data, dlen);
143
144         /* Do the operation */
145         ret = pvr2_send_request(hdw,
146                                 hdw->cmd_buffer,
147                                 4 + dlen,
148                                 hdw->cmd_buffer,
149                                 rlen + 1);
150         if (!ret) {
151                 if (hdw->cmd_buffer[0] != 8) {
152                         ret = -EIO;
153                         if (hdw->cmd_buffer[0] != 7) {
154                                 trace_i2c("unexpected status"
155                                           " from i2_read[%d]: %d",
156                                           i2c_addr,hdw->cmd_buffer[0]);
157                         }
158                 }
159         }
160
161         /* Copy back the result */
162         if (res && rlen) {
163                 if (ret) {
164                         /* Error, just blank out the return buffer */
165                         memset(res, 0, rlen);
166                 } else {
167                         memcpy(res, hdw->cmd_buffer + 1, rlen);
168                 }
169         }
170
171         LOCK_GIVE(hdw->ctl_lock);
172
173         return ret;
174 }
175
176 /* This is the common low level entry point for doing I2C operations to the
177    hardware. */
178 static int pvr2_i2c_basic_op(struct pvr2_hdw *hdw,
179                              u8 i2c_addr,
180                              u8 *wdata,
181                              u16 wlen,
182                              u8 *rdata,
183                              u16 rlen)
184 {
185         if (!rdata) rlen = 0;
186         if (!wdata) wlen = 0;
187         if (rlen || !wlen) {
188                 return pvr2_i2c_read(hdw,i2c_addr,wdata,wlen,rdata,rlen);
189         } else {
190                 return pvr2_i2c_write(hdw,i2c_addr,wdata,wlen);
191         }
192 }
193
194
195 /* This is a special entry point for cases of I2C transaction attempts to
196    the IR receiver.  The implementation here simulates the IR receiver by
197    issuing a command to the FX2 firmware and using that response to return
198    what the real I2C receiver would have returned.  We use this for 24xxx
199    devices, where the IR receiver chip has been removed and replaced with
200    FX2 related logic. */
201 static int i2c_24xxx_ir(struct pvr2_hdw *hdw,
202                         u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
203 {
204         u8 dat[4];
205         unsigned int stat;
206
207         if (!(rlen || wlen)) {
208                 /* This is a probe attempt.  Just let it succeed. */
209                 return 0;
210         }
211
212         /* We don't understand this kind of transaction */
213         if ((wlen != 0) || (rlen == 0)) return -EIO;
214
215         if (rlen < 3) {
216                 /* Mike Isely <isely@pobox.com> Appears to be a probe
217                    attempt from lirc.  Just fill in zeroes and return.  If
218                    we try instead to do the full transaction here, then bad
219                    things seem to happen within the lirc driver module
220                    (version 0.8.0-7 sources from Debian, when run under
221                    vanilla 2.6.17.6 kernel) - and I don't have the patience
222                    to chase it down. */
223                 if (rlen > 0) rdata[0] = 0;
224                 if (rlen > 1) rdata[1] = 0;
225                 return 0;
226         }
227
228         /* Issue a command to the FX2 to read the IR receiver. */
229         LOCK_TAKE(hdw->ctl_lock); do {
230                 hdw->cmd_buffer[0] = FX2CMD_GET_IR_CODE;
231                 stat = pvr2_send_request(hdw,
232                                          hdw->cmd_buffer,1,
233                                          hdw->cmd_buffer,4);
234                 dat[0] = hdw->cmd_buffer[0];
235                 dat[1] = hdw->cmd_buffer[1];
236                 dat[2] = hdw->cmd_buffer[2];
237                 dat[3] = hdw->cmd_buffer[3];
238         } while (0); LOCK_GIVE(hdw->ctl_lock);
239
240         /* Give up if that operation failed. */
241         if (stat != 0) return stat;
242
243         /* Mangle the results into something that looks like the real IR
244            receiver. */
245         rdata[2] = 0xc1;
246         if (dat[0] != 1) {
247                 /* No code received. */
248                 rdata[0] = 0;
249                 rdata[1] = 0;
250         } else {
251                 u16 val;
252                 /* Mash the FX2 firmware-provided IR code into something
253                    that the normal i2c chip-level driver expects. */
254                 val = dat[1];
255                 val <<= 8;
256                 val |= dat[2];
257                 val >>= 1;
258                 val &= ~0x0003;
259                 val |= 0x8000;
260                 rdata[0] = (val >> 8) & 0xffu;
261                 rdata[1] = val & 0xffu;
262         }
263
264         return 0;
265 }
266
267 /* This is a special entry point that is entered if an I2C operation is
268    attempted to a wm8775 chip on model 24xxx hardware.  Autodetect of this
269    part doesn't work, but we know it is really there.  So let's look for
270    the autodetect attempt and just return success if we see that. */
271 static int i2c_hack_wm8775(struct pvr2_hdw *hdw,
272                            u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
273 {
274         if (!(rlen || wlen)) {
275                 // This is a probe attempt.  Just let it succeed.
276                 return 0;
277         }
278         return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
279 }
280
281 /* This is an entry point designed to always fail any attempt to perform a
282    transfer.  We use this to cause certain I2C addresses to not be
283    probed. */
284 static int i2c_black_hole(struct pvr2_hdw *hdw,
285                            u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
286 {
287         return -EIO;
288 }
289
290 /* This is a special entry point that is entered if an I2C operation is
291    attempted to a cx25840 chip on model 24xxx hardware.  This chip can
292    sometimes wedge itself.  Worse still, when this happens msp3400 can
293    falsely detect this part and then the system gets hosed up after msp3400
294    gets confused and dies.  What we want to do here is try to keep msp3400
295    away and also try to notice if the chip is wedged and send a warning to
296    the system log. */
297 static int i2c_hack_cx25840(struct pvr2_hdw *hdw,
298                             u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
299 {
300         int ret;
301         unsigned int subaddr;
302         u8 wbuf[2];
303         int state = hdw->i2c_cx25840_hack_state;
304
305         if (!(rlen || wlen)) {
306                 // Probe attempt - always just succeed and don't bother the
307                 // hardware (this helps to make the state machine further
308                 // down somewhat easier).
309                 return 0;
310         }
311
312         if (state == 3) {
313                 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
314         }
315
316         /* We're looking for the exact pattern where the revision register
317            is being read.  The cx25840 module will always look at the
318            revision register first.  Any other pattern of access therefore
319            has to be a probe attempt from somebody else so we'll reject it.
320            Normally we could just let each client just probe the part
321            anyway, but when the cx25840 is wedged, msp3400 will get a false
322            positive and that just screws things up... */
323
324         if (wlen == 0) {
325                 switch (state) {
326                 case 1: subaddr = 0x0100; break;
327                 case 2: subaddr = 0x0101; break;
328                 default: goto fail;
329                 }
330         } else if (wlen == 2) {
331                 subaddr = (wdata[0] << 8) | wdata[1];
332                 switch (subaddr) {
333                 case 0x0100: state = 1; break;
334                 case 0x0101: state = 2; break;
335                 default: goto fail;
336                 }
337         } else {
338                 goto fail;
339         }
340         if (!rlen) goto success;
341         state = 0;
342         if (rlen != 1) goto fail;
343
344         /* If we get to here then we have a legitimate read for one of the
345            two revision bytes, so pass it through. */
346         wbuf[0] = subaddr >> 8;
347         wbuf[1] = subaddr;
348         ret = pvr2_i2c_basic_op(hdw,i2c_addr,wbuf,2,rdata,rlen);
349
350         if ((ret != 0) || (*rdata == 0x04) || (*rdata == 0x0a)) {
351                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
352                            "WARNING: Detected a wedged cx25840 chip;"
353                            " the device will not work.");
354                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
355                            "WARNING: Try power cycling the pvrusb2 device.");
356                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
357                            "WARNING: Disabling further access to the device"
358                            " to prevent other foul-ups.");
359                 // This blocks all further communication with the part.
360                 hdw->i2c_func[0x44] = NULL;
361                 pvr2_hdw_render_useless(hdw);
362                 goto fail;
363         }
364
365         /* Success! */
366         pvr2_trace(PVR2_TRACE_CHIPS,"cx25840 appears to be OK.");
367         state = 3;
368
369  success:
370         hdw->i2c_cx25840_hack_state = state;
371         return 0;
372
373  fail:
374         hdw->i2c_cx25840_hack_state = state;
375         return -EIO;
376 }
377
378 /* This is a very, very limited I2C adapter implementation.  We can only
379    support what we actually know will work on the device... */
380 static int pvr2_i2c_xfer(struct i2c_adapter *i2c_adap,
381                          struct i2c_msg msgs[],
382                          int num)
383 {
384         int ret = -ENOTSUPP;
385         pvr2_i2c_func funcp = NULL;
386         struct pvr2_hdw *hdw = (struct pvr2_hdw *)(i2c_adap->algo_data);
387
388         if (!num) {
389                 ret = -EINVAL;
390                 goto done;
391         }
392         if (msgs[0].addr < PVR2_I2C_FUNC_CNT) {
393                 funcp = hdw->i2c_func[msgs[0].addr];
394         }
395         if (!funcp) {
396                 ret = -EIO;
397                 goto done;
398         }
399
400         if (num == 1) {
401                 if (msgs[0].flags & I2C_M_RD) {
402                         /* Simple read */
403                         u16 tcnt,bcnt,offs;
404                         if (!msgs[0].len) {
405                                 /* Length == 0 read.  This is a probe. */
406                                 if (funcp(hdw,msgs[0].addr,NULL,0,NULL,0)) {
407                                         ret = -EIO;
408                                         goto done;
409                                 }
410                                 ret = 1;
411                                 goto done;
412                         }
413                         /* If the read is short enough we'll do the whole
414                            thing atomically.  Otherwise we have no choice
415                            but to break apart the reads. */
416                         tcnt = msgs[0].len;
417                         offs = 0;
418                         while (tcnt) {
419                                 bcnt = tcnt;
420                                 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
421                                         bcnt = sizeof(hdw->cmd_buffer)-1;
422                                 }
423                                 if (funcp(hdw,msgs[0].addr,NULL,0,
424                                           msgs[0].buf+offs,bcnt)) {
425                                         ret = -EIO;
426                                         goto done;
427                                 }
428                                 offs += bcnt;
429                                 tcnt -= bcnt;
430                         }
431                         ret = 1;
432                         goto done;
433                 } else {
434                         /* Simple write */
435                         ret = 1;
436                         if (funcp(hdw,msgs[0].addr,
437                                   msgs[0].buf,msgs[0].len,NULL,0)) {
438                                 ret = -EIO;
439                         }
440                         goto done;
441                 }
442         } else if (num == 2) {
443                 if (msgs[0].addr != msgs[1].addr) {
444                         trace_i2c("i2c refusing 2 phase transfer with"
445                                   " conflicting target addresses");
446                         ret = -ENOTSUPP;
447                         goto done;
448                 }
449                 if ((!((msgs[0].flags & I2C_M_RD))) &&
450                     (msgs[1].flags & I2C_M_RD)) {
451                         u16 tcnt,bcnt,wcnt,offs;
452                         /* Write followed by atomic read.  If the read
453                            portion is short enough we'll do the whole thing
454                            atomically.  Otherwise we have no choice but to
455                            break apart the reads. */
456                         tcnt = msgs[1].len;
457                         wcnt = msgs[0].len;
458                         offs = 0;
459                         while (tcnt || wcnt) {
460                                 bcnt = tcnt;
461                                 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
462                                         bcnt = sizeof(hdw->cmd_buffer)-1;
463                                 }
464                                 if (funcp(hdw,msgs[0].addr,
465                                           msgs[0].buf,wcnt,
466                                           msgs[1].buf+offs,bcnt)) {
467                                         ret = -EIO;
468                                         goto done;
469                                 }
470                                 offs += bcnt;
471                                 tcnt -= bcnt;
472                                 wcnt = 0;
473                         }
474                         ret = 2;
475                         goto done;
476                 } else {
477                         trace_i2c("i2c refusing complex transfer"
478                                   " read0=%d read1=%d",
479                                   (msgs[0].flags & I2C_M_RD),
480                                   (msgs[1].flags & I2C_M_RD));
481                 }
482         } else {
483                 trace_i2c("i2c refusing %d phase transfer",num);
484         }
485
486  done:
487         if (pvrusb2_debug & PVR2_TRACE_I2C_TRAF) {
488                 unsigned int idx,offs,cnt;
489                 for (idx = 0; idx < num; idx++) {
490                         cnt = msgs[idx].len;
491                         printk(KERN_INFO
492                                "pvrusb2 i2c xfer %u/%u:"
493                                " addr=0x%x len=%d %s",
494                                idx+1,num,
495                                msgs[idx].addr,
496                                cnt,
497                                (msgs[idx].flags & I2C_M_RD ?
498                                 "read" : "write"));
499                         if ((ret > 0) || !(msgs[idx].flags & I2C_M_RD)) {
500                                 if (cnt > 8) cnt = 8;
501                                 printk(" [");
502                                 for (offs = 0; offs < (cnt>8?8:cnt); offs++) {
503                                         if (offs) printk(" ");
504                                         printk("%02x",msgs[idx].buf[offs]);
505                                 }
506                                 if (offs < cnt) printk(" ...");
507                                 printk("]");
508                         }
509                         if (idx+1 == num) {
510                                 printk(" result=%d",ret);
511                         }
512                         printk("\n");
513                 }
514                 if (!num) {
515                         printk(KERN_INFO
516                                "pvrusb2 i2c xfer null transfer result=%d\n",
517                                ret);
518                 }
519         }
520         return ret;
521 }
522
523 static int pvr2_i2c_control(struct i2c_adapter *adapter,
524                             unsigned int cmd, unsigned long arg)
525 {
526         return 0;
527 }
528
529 static u32 pvr2_i2c_functionality(struct i2c_adapter *adap)
530 {
531         return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
532 }
533
534 static int pvr2_i2c_core_singleton(struct i2c_client *cp,
535                                    unsigned int cmd,void *arg)
536 {
537         int stat;
538         if (!cp) return -EINVAL;
539         if (!(cp->driver)) return -EINVAL;
540         if (!(cp->driver->command)) return -EINVAL;
541         if (!try_module_get(cp->driver->driver.owner)) return -EAGAIN;
542         stat = cp->driver->command(cp,cmd,arg);
543         module_put(cp->driver->driver.owner);
544         return stat;
545 }
546
547 int pvr2_i2c_client_cmd(struct pvr2_i2c_client *cp,unsigned int cmd,void *arg)
548 {
549         int stat;
550         if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
551                 char buf[100];
552                 unsigned int cnt;
553                 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
554                                                buf,sizeof(buf));
555                 pvr2_trace(PVR2_TRACE_I2C_CMD,
556                            "i2c COMMAND (code=%u 0x%x) to %.*s",
557                            cmd,cmd,cnt,buf);
558         }
559         stat = pvr2_i2c_core_singleton(cp->client,cmd,arg);
560         if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
561                 char buf[100];
562                 unsigned int cnt;
563                 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
564                                                buf,sizeof(buf));
565                 pvr2_trace(PVR2_TRACE_I2C_CMD,
566                            "i2c COMMAND to %.*s (ret=%d)",cnt,buf,stat);
567         }
568         return stat;
569 }
570
571 int pvr2_i2c_core_cmd(struct pvr2_hdw *hdw,unsigned int cmd,void *arg)
572 {
573         struct pvr2_i2c_client *cp, *ncp;
574         int stat = -EINVAL;
575
576         if (!hdw) return stat;
577
578         mutex_lock(&hdw->i2c_list_lock);
579         list_for_each_entry_safe(cp, ncp, &hdw->i2c_clients, list) {
580                 if (!cp->recv_enable) continue;
581                 mutex_unlock(&hdw->i2c_list_lock);
582                 stat = pvr2_i2c_client_cmd(cp,cmd,arg);
583                 mutex_lock(&hdw->i2c_list_lock);
584         }
585         mutex_unlock(&hdw->i2c_list_lock);
586         return stat;
587 }
588
589
590 static int handler_check(struct pvr2_i2c_client *cp)
591 {
592         struct pvr2_i2c_handler *hp = cp->handler;
593         if (!hp) return 0;
594         if (!hp->func_table->check) return 0;
595         return hp->func_table->check(hp->func_data) != 0;
596 }
597
598 #define BUFSIZE 500
599
600
601 void pvr2_i2c_core_status_poll(struct pvr2_hdw *hdw)
602 {
603         struct pvr2_i2c_client *cp;
604         mutex_lock(&hdw->i2c_list_lock); do {
605                 struct v4l2_tuner *vtp = &hdw->tuner_signal_info;
606                 memset(vtp,0,sizeof(*vtp));
607                 list_for_each_entry(cp, &hdw->i2c_clients, list) {
608                         if (!cp->detected_flag) continue;
609                         if (!cp->status_poll) continue;
610                         cp->status_poll(cp);
611                 }
612                 hdw->tuner_signal_stale = 0;
613                 pvr2_trace(PVR2_TRACE_CHIPS,"i2c status poll"
614                            " type=%u strength=%u audio=0x%x cap=0x%x"
615                            " low=%u hi=%u",
616                            vtp->type,
617                            vtp->signal,vtp->rxsubchans,vtp->capability,
618                            vtp->rangelow,vtp->rangehigh);
619         } while (0); mutex_unlock(&hdw->i2c_list_lock);
620 }
621
622
623 /* Issue various I2C operations to bring chip-level drivers into sync with
624    state stored in this driver. */
625 void pvr2_i2c_core_sync(struct pvr2_hdw *hdw)
626 {
627         unsigned long msk;
628         unsigned int idx;
629         struct pvr2_i2c_client *cp, *ncp;
630
631         if (!hdw->i2c_linked) return;
632         if (!(hdw->i2c_pend_types & PVR2_I2C_PEND_ALL)) {
633                 return;
634         }
635         mutex_lock(&hdw->i2c_list_lock); do {
636                 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: core_sync BEGIN");
637                 if (hdw->i2c_pend_types & PVR2_I2C_PEND_DETECT) {
638                         /* One or more I2C clients have attached since we
639                            last synced.  So scan the list and identify the
640                            new clients. */
641                         char *buf;
642                         unsigned int cnt;
643                         unsigned long amask = 0;
644                         buf = kmalloc(BUFSIZE,GFP_KERNEL);
645                         pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_DETECT");
646                         hdw->i2c_pend_types &= ~PVR2_I2C_PEND_DETECT;
647                         list_for_each_entry(cp, &hdw->i2c_clients, list) {
648                                 if (!cp->detected_flag) {
649                                         cp->ctl_mask = 0;
650                                         pvr2_i2c_probe(hdw,cp);
651                                         cp->detected_flag = !0;
652                                         msk = cp->ctl_mask;
653                                         cnt = 0;
654                                         if (buf) {
655                                                 cnt = pvr2_i2c_client_describe(
656                                                         cp,
657                                                         PVR2_I2C_DETAIL_ALL,
658                                                         buf,BUFSIZE);
659                                         }
660                                         trace_i2c("Probed: %.*s",cnt,buf);
661                                         if (handler_check(cp)) {
662                                                 hdw->i2c_pend_types |=
663                                                         PVR2_I2C_PEND_CLIENT;
664                                         }
665                                         cp->pend_mask = msk;
666                                         hdw->i2c_pend_mask |= msk;
667                                         hdw->i2c_pend_types |=
668                                                 PVR2_I2C_PEND_REFRESH;
669                                 }
670                                 amask |= cp->ctl_mask;
671                         }
672                         hdw->i2c_active_mask = amask;
673                         if (buf) kfree(buf);
674                 }
675                 if (hdw->i2c_pend_types & PVR2_I2C_PEND_STALE) {
676                         /* Need to do one or more global updates.  Arrange
677                            for this to happen. */
678                         unsigned long m2;
679                         pvr2_trace(PVR2_TRACE_I2C_CORE,
680                                    "i2c: PEND_STALE (0x%lx)",
681                                    hdw->i2c_stale_mask);
682                         hdw->i2c_pend_types &= ~PVR2_I2C_PEND_STALE;
683                         list_for_each_entry(cp, &hdw->i2c_clients, list) {
684                                 m2 = hdw->i2c_stale_mask;
685                                 m2 &= cp->ctl_mask;
686                                 m2 &= ~cp->pend_mask;
687                                 if (m2) {
688                                         pvr2_trace(PVR2_TRACE_I2C_CORE,
689                                                    "i2c: cp=%p setting 0x%lx",
690                                                    cp,m2);
691                                         cp->pend_mask |= m2;
692                                 }
693                         }
694                         hdw->i2c_pend_mask |= hdw->i2c_stale_mask;
695                         hdw->i2c_stale_mask = 0;
696                         hdw->i2c_pend_types |= PVR2_I2C_PEND_REFRESH;
697                 }
698                 if (hdw->i2c_pend_types & PVR2_I2C_PEND_CLIENT) {
699                         /* One or more client handlers are asking for an
700                            update.  Run through the list of known clients
701                            and update each one. */
702                         pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_CLIENT");
703                         hdw->i2c_pend_types &= ~PVR2_I2C_PEND_CLIENT;
704                         list_for_each_entry_safe(cp, ncp, &hdw->i2c_clients,
705                                                  list) {
706                                 if (!cp->handler) continue;
707                                 if (!cp->handler->func_table->update) continue;
708                                 pvr2_trace(PVR2_TRACE_I2C_CORE,
709                                            "i2c: cp=%p update",cp);
710                                 mutex_unlock(&hdw->i2c_list_lock);
711                                 cp->handler->func_table->update(
712                                         cp->handler->func_data);
713                                 mutex_lock(&hdw->i2c_list_lock);
714                                 /* If client's update function set some
715                                    additional pending bits, account for that
716                                    here. */
717                                 if (cp->pend_mask & ~hdw->i2c_pend_mask) {
718                                         hdw->i2c_pend_mask |= cp->pend_mask;
719                                         hdw->i2c_pend_types |=
720                                                 PVR2_I2C_PEND_REFRESH;
721                                 }
722                         }
723                 }
724                 if (hdw->i2c_pend_types & PVR2_I2C_PEND_REFRESH) {
725                         const struct pvr2_i2c_op *opf;
726                         unsigned long pm;
727                         /* Some actual updates are pending.  Walk through
728                            each update type and perform it. */
729                         pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_REFRESH"
730                                    " (0x%lx)",hdw->i2c_pend_mask);
731                         hdw->i2c_pend_types &= ~PVR2_I2C_PEND_REFRESH;
732                         pm = hdw->i2c_pend_mask;
733                         hdw->i2c_pend_mask = 0;
734                         for (idx = 0, msk = 1; pm; idx++, msk <<= 1) {
735                                 if (!(pm & msk)) continue;
736                                 pm &= ~msk;
737                                 list_for_each_entry(cp, &hdw->i2c_clients,
738                                                     list) {
739                                         if (cp->pend_mask & msk) {
740                                                 cp->pend_mask &= ~msk;
741                                                 cp->recv_enable = !0;
742                                         } else {
743                                                 cp->recv_enable = 0;
744                                         }
745                                 }
746                                 opf = pvr2_i2c_get_op(idx);
747                                 if (!opf) continue;
748                                 mutex_unlock(&hdw->i2c_list_lock);
749                                 opf->update(hdw);
750                                 mutex_lock(&hdw->i2c_list_lock);
751                         }
752                 }
753                 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: core_sync END");
754         } while (0); mutex_unlock(&hdw->i2c_list_lock);
755 }
756
757 int pvr2_i2c_core_check_stale(struct pvr2_hdw *hdw)
758 {
759         unsigned long msk,sm,pm;
760         unsigned int idx;
761         const struct pvr2_i2c_op *opf;
762         struct pvr2_i2c_client *cp;
763         unsigned int pt = 0;
764
765         pvr2_trace(PVR2_TRACE_I2C_CORE,"pvr2_i2c_core_check_stale BEGIN");
766
767         pm = hdw->i2c_active_mask;
768         sm = 0;
769         for (idx = 0, msk = 1; pm; idx++, msk <<= 1) {
770                 if (!(msk & pm)) continue;
771                 pm &= ~msk;
772                 opf = pvr2_i2c_get_op(idx);
773                 if (!opf) continue;
774                 if (opf->check(hdw)) {
775                         sm |= msk;
776                 }
777         }
778         if (sm) pt |= PVR2_I2C_PEND_STALE;
779
780         list_for_each_entry(cp, &hdw->i2c_clients, list)
781                 if (handler_check(cp))
782                         pt |= PVR2_I2C_PEND_CLIENT;
783
784         if (pt) {
785                 mutex_lock(&hdw->i2c_list_lock); do {
786                         hdw->i2c_pend_types |= pt;
787                         hdw->i2c_stale_mask |= sm;
788                         hdw->i2c_pend_mask |= hdw->i2c_stale_mask;
789                 } while (0); mutex_unlock(&hdw->i2c_list_lock);
790         }
791
792         pvr2_trace(PVR2_TRACE_I2C_CORE,
793                    "i2c: types=0x%x stale=0x%lx pend=0x%lx",
794                    hdw->i2c_pend_types,
795                    hdw->i2c_stale_mask,
796                    hdw->i2c_pend_mask);
797         pvr2_trace(PVR2_TRACE_I2C_CORE,"pvr2_i2c_core_check_stale END");
798
799         return (hdw->i2c_pend_types & PVR2_I2C_PEND_ALL) != 0;
800 }
801
802 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client *cp,
803                                              unsigned int detail,
804                                              char *buf,unsigned int maxlen)
805 {
806         unsigned int ccnt,bcnt;
807         int spcfl = 0;
808         const struct pvr2_i2c_op *opf;
809
810         ccnt = 0;
811         if (detail & PVR2_I2C_DETAIL_DEBUG) {
812                 bcnt = scnprintf(buf,maxlen,
813                                  "ctxt=%p ctl_mask=0x%lx",
814                                  cp,cp->ctl_mask);
815                 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
816                 spcfl = !0;
817         }
818         bcnt = scnprintf(buf,maxlen,
819                          "%s%s @ 0x%x",
820                          (spcfl ? " " : ""),
821                          cp->client->name,
822                          cp->client->addr);
823         ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
824         if ((detail & PVR2_I2C_DETAIL_HANDLER) &&
825             cp->handler && cp->handler->func_table->describe) {
826                 bcnt = scnprintf(buf,maxlen," (");
827                 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
828                 bcnt = cp->handler->func_table->describe(
829                         cp->handler->func_data,buf,maxlen);
830                 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
831                 bcnt = scnprintf(buf,maxlen,")");
832                 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
833         }
834         if ((detail & PVR2_I2C_DETAIL_CTLMASK) && cp->ctl_mask) {
835                 unsigned int idx;
836                 unsigned long msk,sm;
837                 int spcfl;
838                 bcnt = scnprintf(buf,maxlen," [");
839                 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
840                 sm = 0;
841                 spcfl = 0;
842                 for (idx = 0, msk = 1; msk; idx++, msk <<= 1) {
843                         if (!(cp->ctl_mask & msk)) continue;
844                         opf = pvr2_i2c_get_op(idx);
845                         if (opf) {
846                                 bcnt = scnprintf(buf,maxlen,"%s%s",
847                                                  spcfl ? " " : "",
848                                                  opf->name);
849                                 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
850                                 spcfl = !0;
851                         } else {
852                                 sm |= msk;
853                         }
854                 }
855                 if (sm) {
856                         bcnt = scnprintf(buf,maxlen,"%s%lx",
857                                          idx != 0 ? " " : "",sm);
858                         ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
859                 }
860                 bcnt = scnprintf(buf,maxlen,"]");
861                 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
862         }
863         return ccnt;
864 }
865
866 unsigned int pvr2_i2c_report(struct pvr2_hdw *hdw,
867                              char *buf,unsigned int maxlen)
868 {
869         unsigned int ccnt,bcnt;
870         struct pvr2_i2c_client *cp;
871         ccnt = 0;
872         mutex_lock(&hdw->i2c_list_lock); do {
873                 list_for_each_entry(cp, &hdw->i2c_clients, list) {
874                         bcnt = pvr2_i2c_client_describe(
875                                 cp,
876                                 (PVR2_I2C_DETAIL_HANDLER|
877                                  PVR2_I2C_DETAIL_CTLMASK),
878                                 buf,maxlen);
879                         ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
880                         bcnt = scnprintf(buf,maxlen,"\n");
881                         ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
882                 }
883         } while (0); mutex_unlock(&hdw->i2c_list_lock);
884         return ccnt;
885 }
886
887 static int pvr2_i2c_attach_inform(struct i2c_client *client)
888 {
889         struct pvr2_hdw *hdw = (struct pvr2_hdw *)(client->adapter->algo_data);
890         struct pvr2_i2c_client *cp;
891         int fl = !(hdw->i2c_pend_types & PVR2_I2C_PEND_ALL);
892         cp = kzalloc(sizeof(*cp),GFP_KERNEL);
893         trace_i2c("i2c_attach [client=%s @ 0x%x ctxt=%p]",
894                   client->name,
895                   client->addr,cp);
896         if (!cp) return -ENOMEM;
897         cp->hdw = hdw;
898         INIT_LIST_HEAD(&cp->list);
899         cp->client = client;
900         mutex_lock(&hdw->i2c_list_lock); do {
901                 list_add_tail(&cp->list,&hdw->i2c_clients);
902                 hdw->i2c_pend_types |= PVR2_I2C_PEND_DETECT;
903         } while (0); mutex_unlock(&hdw->i2c_list_lock);
904         if (fl) pvr2_hdw_poll_trigger_unlocked(hdw);
905         return 0;
906 }
907
908 static int pvr2_i2c_detach_inform(struct i2c_client *client)
909 {
910         struct pvr2_hdw *hdw = (struct pvr2_hdw *)(client->adapter->algo_data);
911         struct pvr2_i2c_client *cp, *ncp;
912         unsigned long amask = 0;
913         int foundfl = 0;
914         mutex_lock(&hdw->i2c_list_lock); do {
915                 list_for_each_entry_safe(cp, ncp, &hdw->i2c_clients, list) {
916                         if (cp->client == client) {
917                                 trace_i2c("pvr2_i2c_detach"
918                                           " [client=%s @ 0x%x ctxt=%p]",
919                                           client->name,
920                                           client->addr,cp);
921                                 if (cp->handler &&
922                                     cp->handler->func_table->detach) {
923                                         cp->handler->func_table->detach(
924                                                 cp->handler->func_data);
925                                 }
926                                 list_del(&cp->list);
927                                 kfree(cp);
928                                 foundfl = !0;
929                                 continue;
930                         }
931                         amask |= cp->ctl_mask;
932                 }
933                 hdw->i2c_active_mask = amask;
934         } while (0); mutex_unlock(&hdw->i2c_list_lock);
935         if (!foundfl) {
936                 trace_i2c("pvr2_i2c_detach [client=%s @ 0x%x ctxt=<unknown>]",
937                           client->name,
938                           client->addr);
939         }
940         return 0;
941 }
942
943 static struct i2c_algorithm pvr2_i2c_algo_template = {
944         .master_xfer   = pvr2_i2c_xfer,
945         .algo_control  = pvr2_i2c_control,
946         .functionality = pvr2_i2c_functionality,
947 };
948
949 static struct i2c_adapter pvr2_i2c_adap_template = {
950         .owner         = THIS_MODULE,
951         .class     = I2C_CLASS_TV_ANALOG,
952         .id            = I2C_HW_B_BT848,
953         .client_register = pvr2_i2c_attach_inform,
954         .client_unregister = pvr2_i2c_detach_inform,
955 };
956
957 static void do_i2c_scan(struct pvr2_hdw *hdw)
958 {
959         struct i2c_msg msg[1];
960         int i,rc;
961         msg[0].addr = 0;
962         msg[0].flags = I2C_M_RD;
963         msg[0].len = 0;
964         msg[0].buf = NULL;
965         printk("%s: i2c scan beginning\n",hdw->name);
966         for (i = 0; i < 128; i++) {
967                 msg[0].addr = i;
968                 rc = i2c_transfer(&hdw->i2c_adap,msg, ARRAY_SIZE(msg));
969                 if (rc != 1) continue;
970                 printk("%s: i2c scan: found device @ 0x%x\n",hdw->name,i);
971         }
972         printk("%s: i2c scan done.\n",hdw->name);
973 }
974
975 void pvr2_i2c_core_init(struct pvr2_hdw *hdw)
976 {
977         unsigned int idx;
978
979         /* The default action for all possible I2C addresses is just to do
980            the transfer normally. */
981         for (idx = 0; idx < PVR2_I2C_FUNC_CNT; idx++) {
982                 hdw->i2c_func[idx] = pvr2_i2c_basic_op;
983         }
984
985         /* However, deal with various special cases for 24xxx hardware. */
986         if (ir_mode[hdw->unit_number] == 0) {
987                 printk(KERN_INFO "%s: IR disabled\n",hdw->name);
988                 hdw->i2c_func[0x18] = i2c_black_hole;
989         } else if (ir_mode[hdw->unit_number] == 1) {
990                 if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) {
991                         hdw->i2c_func[0x18] = i2c_24xxx_ir;
992                 }
993         }
994         if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) {
995                 hdw->i2c_func[0x1b] = i2c_hack_wm8775;
996                 hdw->i2c_func[0x44] = i2c_hack_cx25840;
997         }
998
999         // Configure the adapter and set up everything else related to it.
1000         memcpy(&hdw->i2c_adap,&pvr2_i2c_adap_template,sizeof(hdw->i2c_adap));
1001         memcpy(&hdw->i2c_algo,&pvr2_i2c_algo_template,sizeof(hdw->i2c_algo));
1002         strlcpy(hdw->i2c_adap.name,hdw->name,sizeof(hdw->i2c_adap.name));
1003         hdw->i2c_adap.dev.parent = &hdw->usb_dev->dev;
1004         hdw->i2c_adap.algo = &hdw->i2c_algo;
1005         hdw->i2c_adap.algo_data = hdw;
1006         hdw->i2c_pend_mask = 0;
1007         hdw->i2c_stale_mask = 0;
1008         hdw->i2c_active_mask = 0;
1009         INIT_LIST_HEAD(&hdw->i2c_clients);
1010         mutex_init(&hdw->i2c_list_lock);
1011         hdw->i2c_linked = !0;
1012         i2c_add_adapter(&hdw->i2c_adap);
1013         if (i2c_scan) do_i2c_scan(hdw);
1014 }
1015
1016 void pvr2_i2c_core_done(struct pvr2_hdw *hdw)
1017 {
1018         if (hdw->i2c_linked) {
1019                 i2c_del_adapter(&hdw->i2c_adap);
1020                 hdw->i2c_linked = 0;
1021         }
1022 }
1023
1024 /*
1025   Stuff for Emacs to see, in order to encourage consistent editing style:
1026   *** Local Variables: ***
1027   *** mode: c ***
1028   *** fill-column: 75 ***
1029   *** tab-width: 8 ***
1030   *** c-basic-offset: 8 ***
1031   *** End: ***
1032   */