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