xf86i2c.c revision 6747b715
1/*
2 * Copyright (C) 1998 Itai Nahshon, Michael Schimek
3 *
4 * The original code was derived from and inspired by
5 * the I2C driver from the Linux kernel.
6 *      (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
7 */
8
9
10#ifdef HAVE_XORG_CONFIG_H
11#include <xorg-config.h>
12#endif
13
14#include <sys/time.h>
15#include <string.h>
16
17#include "misc.h"
18#include "xf86.h"
19#include "xf86_OSproc.h"
20
21#include <X11/X.h>
22#include <X11/Xos.h>
23#include <X11/Xproto.h>
24#include "scrnintstr.h"
25#include "regionstr.h"
26#include "windowstr.h"
27#include "pixmapstr.h"
28#include "validate.h"
29#include "resource.h"
30#include "gcstruct.h"
31#include "dixstruct.h"
32
33#include "xf86i2c.h"
34
35#define I2C_TIMEOUT(x)	/*(x)*/  /* Report timeouts */
36#define I2C_TRACE(x)    /*(x)*/  /* Report progress */
37
38/* This is the default I2CUDelay function if not supplied by the driver.
39 * High level I2C interfaces implementing the bus protocol in hardware
40 * should supply this function too.
41 *
42 * Delay execution at least usec microseconds.
43 * All values 0 to 1e6 inclusive must be expected.
44 */
45
46static void
47I2CUDelay(I2CBusPtr b, int usec)
48{
49  struct timeval begin, cur;
50  long d_secs, d_usecs;
51  long diff;
52
53  if (usec > 0) {
54    X_GETTIMEOFDAY(&begin);
55    do {
56      /* It would be nice to use {xf86}usleep,
57       * but usleep (1) takes >10000 usec !
58       */
59      X_GETTIMEOFDAY(&cur);
60      d_secs  = (cur.tv_sec - begin.tv_sec);
61      d_usecs = (cur.tv_usec - begin.tv_usec);
62      diff = d_secs*1000000 + d_usecs;
63    } while (diff>=0 && diff< (usec + 1));
64  }
65}
66
67/* Most drivers will register just with GetBits/PutBits functions.
68 * The following functions implement a software I2C protocol
69 * by using the promitive functions given by the driver.
70 * ================================================================
71 *
72 * It is assumed that there is just one master on the I2C bus, therefore
73 * there is no explicit test for conflits.
74 */
75
76#define RISEFALLTIME 2 /* usec, actually 300 to 1000 ns according to the i2c specs */
77
78/* Some devices will hold SCL low to slow down the bus or until
79 * ready for transmission.
80 *
81 * This condition will be noticed when the master tries to raise
82 * the SCL line. You can set the timeout to zero if the slave device
83 * does not support this clock synchronization.
84 */
85
86static Bool
87I2CRaiseSCL(I2CBusPtr b, int sda, int timeout)
88{
89    int i, scl;
90
91    b->I2CPutBits(b, 1, sda);
92    b->I2CUDelay(b, b->RiseFallTime);
93
94    for (i = timeout; i > 0; i -= b->RiseFallTime) {
95	b->I2CGetBits(b, &scl, &sda);
96	if (scl) break;
97	b->I2CUDelay(b, b->RiseFallTime);
98    }
99
100    if (i <= 0) {
101	I2C_TIMEOUT(ErrorF("[I2CRaiseSCL(<%s>, %d, %d) timeout]", b->BusName, sda, timeout));
102	return FALSE;
103    }
104
105    return TRUE;
106}
107
108/* Send a start signal on the I2C bus. The start signal notifies
109 * devices that a new transaction is initiated by the bus master.
110 *
111 * The start signal is always followed by a slave address.
112 * Slave addresses are 8+ bits. The first 7 bits identify the
113 * device and the last bit signals if this is a read (1) or
114 * write (0) operation.
115 *
116 * There may be more than one start signal on one transaction.
117 * This happens for example on some devices that allow reading
118 * of registers. First send a start bit followed by the device
119 * address (with the last bit 0) and the register number. Then send
120 * a new start bit with the device address (with the last bit 1)
121 * and then read the value from the device.
122 *
123 * Note this is function does not implement a multiple master
124 * arbitration procedure.
125 */
126
127static Bool
128I2CStart(I2CBusPtr b, int timeout)
129{
130    if (!I2CRaiseSCL(b, 1, timeout))
131	return FALSE;
132
133    b->I2CPutBits(b, 1, 0);
134    b->I2CUDelay(b, b->HoldTime);
135    b->I2CPutBits(b, 0, 0);
136    b->I2CUDelay(b, b->HoldTime);
137
138    I2C_TRACE(ErrorF("\ni2c: <"));
139
140    return TRUE;
141}
142
143/* This is the default I2CStop function if not supplied by the driver.
144 *
145 * Signal devices on the I2C bus that a transaction on the
146 * bus has finished. There may be more than one start signal
147 * on a transaction but only one stop signal.
148 */
149
150static void
151I2CStop(I2CDevPtr d)
152{
153    I2CBusPtr b = d->pI2CBus;
154
155    b->I2CPutBits(b, 0, 0);
156    b->I2CUDelay(b, b->RiseFallTime);
157
158    b->I2CPutBits(b, 1, 0);
159    b->I2CUDelay(b, b->HoldTime);
160    b->I2CPutBits(b, 1, 1);
161    b->I2CUDelay(b, b->HoldTime);
162
163    I2C_TRACE(ErrorF(">\n"));
164}
165
166/* Write/Read a single bit to/from a device.
167 * Return FALSE if a timeout occurs.
168 */
169
170static Bool
171I2CWriteBit(I2CBusPtr b, int sda, int timeout)
172{
173    Bool r;
174
175    b->I2CPutBits(b, 0, sda);
176    b->I2CUDelay(b, b->RiseFallTime);
177
178    r = I2CRaiseSCL(b, sda, timeout);
179    b->I2CUDelay(b, b->HoldTime);
180
181    b->I2CPutBits(b, 0, sda);
182    b->I2CUDelay(b, b->HoldTime);
183
184    return r;
185}
186
187static Bool
188I2CReadBit(I2CBusPtr b, int *psda, int timeout)
189{
190    Bool r;
191    int scl;
192
193    r = I2CRaiseSCL(b, 1, timeout);
194    b->I2CUDelay(b, b->HoldTime);
195
196    b->I2CGetBits(b, &scl, psda);
197
198    b->I2CPutBits(b, 0, 1);
199    b->I2CUDelay(b, b->HoldTime);
200
201    return r;
202}
203
204/* This is the default I2CPutByte function if not supplied by the driver.
205 *
206 * A single byte is sent to the device.
207 * The function returns FALSE if a timeout occurs, you should send
208 * a stop condition afterwards to reset the bus.
209 *
210 * A timeout occurs,
211 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
212 * or slows down the bus for more than BitTimeout usecs for each bit,
213 * or does not send an ACK bit (0) to acknowledge the transmission within
214 * AcknTimeout usecs, but a NACK (1) bit.
215 *
216 * AcknTimeout must be at least b->HoldTime, the other timeouts can be
217 * zero according to the comment on I2CRaiseSCL.
218 */
219
220static Bool
221I2CPutByte(I2CDevPtr d, I2CByte data)
222{
223    Bool r;
224    int i, scl, sda;
225    I2CBusPtr b = d->pI2CBus;
226
227    if (!I2CWriteBit(b, (data >> 7) & 1, d->ByteTimeout))
228	return FALSE;
229
230    for (i = 6; i >= 0; i--)
231	if (!I2CWriteBit(b, (data >> i) & 1, d->BitTimeout))
232	    return FALSE;
233
234    b->I2CPutBits(b, 0, 1);
235    b->I2CUDelay(b, b->RiseFallTime);
236
237    r = I2CRaiseSCL(b, 1, b->HoldTime);
238
239    if (r) {
240    	for (i = d->AcknTimeout; i > 0; i -= b->HoldTime) {
241	    b->I2CUDelay(b, b->HoldTime);
242	    b->I2CGetBits(b, &scl, &sda);
243	    if (sda == 0) break;
244	}
245
246	if (i <= 0) {
247	    I2C_TIMEOUT(ErrorF("[I2CPutByte(<%s>, 0x%02x, %d, %d, %d) timeout]",
248				       b->BusName, data, d->BitTimeout,
249				       d->ByteTimeout, d->AcknTimeout));
250	    r = FALSE;
251	}
252
253	I2C_TRACE(ErrorF("W%02x%c ", (int) data, sda ? '-' : '+'));
254    }
255
256    b->I2CPutBits(b, 0, 1);
257    b->I2CUDelay(b, b->HoldTime);
258
259    return r;
260}
261
262/* This is the default I2CGetByte function if not supplied by the driver.
263 *
264 * A single byte is read from the device.
265 * The function returns FALSE if a timeout occurs, you should send
266 * a stop condition afterwards to reset the bus.
267 *
268 * A timeout occurs,
269 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
270 * or slows down the bus for more than b->BitTimeout usecs for each bit.
271 *
272 * ByteTimeout must be at least b->HoldTime, the other timeouts can be
273 * zero according to the comment on I2CRaiseSCL.
274 *
275 * For the <last> byte in a sequence the acknowledge bit NACK (1),
276 * otherwise ACK (0) will be sent.
277 */
278
279static Bool
280I2CGetByte(I2CDevPtr d, I2CByte *data, Bool last)
281{
282    int i, sda;
283    I2CBusPtr b = d->pI2CBus;
284
285    b->I2CPutBits(b, 0, 1);
286    b->I2CUDelay(b, b->RiseFallTime);
287
288    if (!I2CReadBit(b, &sda, d->ByteTimeout))
289	return FALSE;
290
291    *data = (sda > 0) << 7;
292
293    for (i = 6; i >= 0; i--)
294	if (!I2CReadBit(b, &sda, d->BitTimeout))
295	    return FALSE;
296	else
297	    *data |= (sda > 0) << i;
298
299    if (!I2CWriteBit(b, last ? 1 : 0, d->BitTimeout))
300	return FALSE;
301
302    I2C_TRACE(ErrorF("R%02x%c ", (int) *data, last ? '+' : '-'));
303
304    return TRUE;
305}
306
307/* This is the default I2CAddress function if not supplied by the driver.
308 *
309 * It creates the start condition, followed by the d->SlaveAddr.
310 * Higher level functions must call this routine rather than
311 * I2CStart/PutByte because a hardware I2C master may not be able
312 * to send a slave address without a start condition.
313 *
314 * The same timeouts apply as with I2CPutByte and additional a
315 * StartTimeout, similar to the ByteTimeout but for the start
316 * condition.
317 *
318 * In case of a timeout, the bus is left in a clean idle condition.
319 * I. e. you *must not* send a Stop. If this function succeeds, you *must*.
320 *
321 * The slave address format is 16 bit, with the legacy _8_bit_ slave address
322 * in the least significant byte. This is, the slave address must include the
323 * R/_W flag as least significant bit.
324 *
325 * The most significant byte of the address will be sent _after_ the LSB,
326 * but only if the LSB indicates:
327 * a) an 11 bit address, this is LSB = 1111 0xxx.
328 * b) a 'general call address', this is LSB = 0000 000x - see the I2C specs
329 *    for more.
330 */
331
332static Bool
333I2CAddress(I2CDevPtr d, I2CSlaveAddr addr)
334{
335    if (I2CStart(d->pI2CBus, d->StartTimeout)) {
336	if (I2CPutByte(d, addr & 0xFF)) {
337	    if ((addr & 0xF8) != 0xF0 &&
338		(addr & 0xFE) != 0x00)
339		return TRUE;
340
341	    if (I2CPutByte(d, (addr >> 8) & 0xFF))
342		return TRUE;
343	}
344
345	I2CStop(d);
346    }
347
348    return FALSE;
349}
350
351/* These are the hardware independent I2C helper functions.
352 * ========================================================
353 */
354
355/* Function for probing. Just send the slave address
356 * and return true if the device responds. The slave address
357 * must have the lsb set to reflect a read (1) or write (0) access.
358 * Don't expect a read- or write-only device will respond otherwise.
359 */
360
361Bool
362xf86I2CProbeAddress(I2CBusPtr b, I2CSlaveAddr addr)
363{
364    int r;
365    I2CDevRec d;
366
367    d.DevName = "Probing";
368    d.BitTimeout = b->BitTimeout;
369    d.ByteTimeout = b->ByteTimeout;
370    d.AcknTimeout = b->AcknTimeout;
371    d.StartTimeout = b->StartTimeout;
372    d.SlaveAddr = addr;
373    d.pI2CBus = b;
374    d.NextDev = NULL;
375
376    r = b->I2CAddress(&d, addr);
377
378    if (r) b->I2CStop(&d);
379
380    return r;
381}
382
383/* All functions below are related to devices and take the
384 * slave address and timeout values from an I2CDevRec. They
385 * return FALSE in case of an error (presumably a timeout).
386 */
387
388/* General purpose read and write function.
389 *
390 * 1st, if nWrite > 0
391 *   Send a start condition
392 *   Send the slave address (1 or 2 bytes) with write flag
393 *   Write n bytes from WriteBuffer
394 * 2nd, if nRead > 0
395 *   Send a start condition [again]
396 *   Send the slave address (1 or 2 bytes) with read flag
397 *   Read n bytes to ReadBuffer
398 * 3rd, if a Start condition has been successfully sent,
399 *   Send a Stop condition.
400 *
401 * The functions exits immediately when an error occures,
402 * not proceeding any data left. However, step 3 will
403 * be executed anyway to leave the bus in clean idle state.
404 */
405
406static Bool
407I2CWriteRead(I2CDevPtr d,
408		 I2CByte *WriteBuffer, int nWrite,
409		 I2CByte *ReadBuffer,  int nRead)
410{
411    Bool r = TRUE;
412    I2CBusPtr b = d->pI2CBus;
413    int s = 0;
414
415    if (r && nWrite > 0) {
416	r = b->I2CAddress(d, d->SlaveAddr & ~1);
417	if (r) {
418	    for (; nWrite > 0; WriteBuffer++, nWrite--)
419		if (!(r = b->I2CPutByte(d, *WriteBuffer)))
420		    break;
421	    s++;
422	}
423    }
424
425    if (r && nRead > 0) {
426	r = b->I2CAddress(d, d->SlaveAddr | 1);
427	if (r) {
428	    for (; nRead > 0; ReadBuffer++, nRead--)
429		if (!(r = b->I2CGetByte(d, ReadBuffer, nRead == 1)))
430		    break;
431	    s++;
432	}
433    }
434
435    if (s) b->I2CStop(d);
436
437    return r;
438}
439
440/* wrapper - for compatibility and convinience */
441
442Bool
443xf86I2CWriteRead(I2CDevPtr d,
444		 I2CByte *WriteBuffer, int nWrite,
445		 I2CByte *ReadBuffer,  int nRead)
446{
447    I2CBusPtr b = d->pI2CBus;
448    return b->I2CWriteRead(d,WriteBuffer,nWrite,ReadBuffer,nRead);
449}
450
451/* Read a byte, the only readable register of a device.
452 */
453
454Bool
455xf86I2CReadStatus(I2CDevPtr d, I2CByte *pbyte)
456{
457    return xf86I2CWriteRead(d, NULL, 0, pbyte, 1);
458}
459
460/* Read a byte from one of the registers determined by its sub-address.
461 */
462
463Bool
464xf86I2CReadByte(I2CDevPtr d, I2CByte subaddr, I2CByte *pbyte)
465{
466    return xf86I2CWriteRead(d, &subaddr, 1, pbyte, 1);
467}
468
469/* Read bytes from subsequent registers determined by the
470 * sub-address of the first register.
471 */
472
473Bool
474xf86I2CReadBytes(I2CDevPtr d, I2CByte subaddr, I2CByte *pbyte, int n)
475{
476    return xf86I2CWriteRead(d, &subaddr, 1, pbyte, n);
477}
478
479/* Read a word (high byte, then low byte) from one of the registers
480 * determined by its sub-address.
481 */
482
483Bool
484xf86I2CReadWord(I2CDevPtr d, I2CByte subaddr, unsigned short *pword)
485{
486    I2CByte rb[2];
487
488    if (!xf86I2CWriteRead(d, &subaddr, 1, rb, 2)) return FALSE;
489
490    *pword = (rb[0] << 8) | rb[1];
491
492    return TRUE;
493}
494
495/* Write a byte to one of the registers determined by its sub-address.
496 */
497
498Bool
499xf86I2CWriteByte(I2CDevPtr d, I2CByte subaddr, I2CByte byte)
500{
501    I2CByte wb[2];
502
503    wb[0] = subaddr;
504    wb[1] = byte;
505
506    return xf86I2CWriteRead(d, wb, 2, NULL, 0);
507}
508
509/* Write bytes to subsequent registers determined by the
510 * sub-address of the first register.
511 */
512
513Bool
514xf86I2CWriteBytes(I2CDevPtr d, I2CByte subaddr,
515		  I2CByte *WriteBuffer, int nWrite)
516{
517    I2CBusPtr b = d->pI2CBus;
518    Bool r = TRUE;
519
520    if (nWrite > 0) {
521	r = b->I2CAddress(d, d->SlaveAddr & ~1);
522	if (r){
523	    if ((r = b->I2CPutByte(d, subaddr)))
524		for (; nWrite > 0; WriteBuffer++, nWrite--)
525		    if (!(r = b->I2CPutByte(d, *WriteBuffer)))
526			break;
527
528	    b->I2CStop(d);
529	}
530    }
531
532    return r;
533}
534
535/* Write a word (high byte, then low byte) to one of the registers
536 * determined by its sub-address.
537 */
538
539Bool
540xf86I2CWriteWord(I2CDevPtr d, I2CByte subaddr, unsigned short word)
541{
542    I2CByte wb[3];
543
544    wb[0] = subaddr;
545    wb[1] = word >> 8;
546    wb[2] = word & 0xFF;
547
548    return xf86I2CWriteRead(d, wb, 3, NULL, 0);
549}
550
551/* Write a vector of bytes to not adjacent registers. This vector is,
552 * 1st byte sub-address, 2nd byte value, 3rd byte sub-address asf.
553 * This function is intended to initialize devices. Note this function
554 * exits immediately when an error occurs, some registers may
555 * remain uninitialized.
556 */
557
558Bool
559xf86I2CWriteVec(I2CDevPtr d, I2CByte *vec, int nValues)
560{
561    I2CBusPtr b = d->pI2CBus;
562    Bool r = TRUE;
563    int s = 0;
564
565    if (nValues > 0) {
566	for (; nValues > 0; nValues--, vec += 2) {
567	    if (!(r = b->I2CAddress(d, d->SlaveAddr & ~1)))
568	    	break;
569
570	    s++;
571
572	    if (!(r = b->I2CPutByte(d, vec[0])))
573		break;
574
575	    if (!(r = b->I2CPutByte(d, vec[1])))
576		break;
577	}
578
579	if (s > 0) b->I2CStop(d);
580    }
581
582    return r;
583}
584
585/* Administrative functions.
586 * =========================
587 */
588
589/* Allocates an I2CDevRec for you and initializes with propper defaults
590 * you may modify before calling xf86I2CDevInit. Your I2CDevRec must
591 * contain at least a SlaveAddr, and a pI2CBus pointer to the bus this
592 * device shall be linked to.
593 *
594 * See function I2CAddress for the slave address format. Always set
595 * the least significant bit, indicating a read or write access, to zero.
596 */
597
598I2CDevPtr
599xf86CreateI2CDevRec(void)
600{
601    return calloc(1, sizeof(I2CDevRec));
602}
603
604/* Unlink an I2C device. If you got the I2CDevRec from xf86CreateI2CDevRec
605 * you should set <unalloc> to free it.
606 */
607
608void
609xf86DestroyI2CDevRec(I2CDevPtr d, Bool unalloc)
610{
611    if (d) {
612	I2CDevPtr *p;
613
614	/* Remove this from the list of active I2C devices. */
615
616	for (p = &d->pI2CBus->FirstDev; *p != NULL; p = &(*p)->NextDev)
617	    if (*p == d) {
618		*p = (*p)->NextDev;
619		break;
620	    }
621
622	xf86DrvMsg(d->pI2CBus->scrnIndex, X_INFO,
623		   "I2C device \"%s:%s\" removed.\n",
624		   d->pI2CBus->BusName, d->DevName);
625
626	if (unalloc) free(d);
627    }
628}
629
630/* I2C transmissions are related to an I2CDevRec you must link to a
631 * previously registered bus (see xf86I2CBusInit) before attempting
632 * to read and write data. You may call xf86I2CProbeAddress first to
633 * see if the device in question is present on this bus.
634 *
635 * xf86I2CDevInit will not allocate an I2CBusRec for you, instead you
636 * may enter a pointer to a statically allocated I2CDevRec or the (modified)
637 * result of xf86CreateI2CDevRec.
638 *
639 * If you don't specify timeouts for the device (n <= 0), it will inherit
640 * the bus-wide defaults. The function returns TRUE on success.
641 */
642
643Bool
644xf86I2CDevInit(I2CDevPtr d)
645{
646    I2CBusPtr b;
647
648    if (d == NULL ||
649	(b = d->pI2CBus) == NULL ||
650        (d->SlaveAddr & 1) ||
651        xf86I2CFindDev(b, d->SlaveAddr) != NULL)
652	return FALSE;
653
654    if (d->BitTimeout <= 0) d->BitTimeout = b->BitTimeout;
655    if (d->ByteTimeout <= 0) d->ByteTimeout = b->ByteTimeout;
656    if (d->AcknTimeout <= 0) d->AcknTimeout = b->AcknTimeout;
657    if (d->StartTimeout <= 0) d->StartTimeout = b->StartTimeout;
658
659    d->NextDev = b->FirstDev;
660    b->FirstDev = d;
661
662    xf86DrvMsg(b->scrnIndex, X_INFO,
663	       "I2C device \"%s:%s\" registered at address 0x%02X.\n",
664	       b->BusName, d->DevName, d->SlaveAddr);
665
666    return TRUE;
667}
668
669I2CDevPtr
670xf86I2CFindDev(I2CBusPtr b, I2CSlaveAddr addr)
671{
672    I2CDevPtr d;
673
674    if (b) {
675         for (d = b->FirstDev; d != NULL; d = d->NextDev)
676	    if (d->SlaveAddr == addr)
677		return d;
678    }
679
680    return NULL;
681}
682
683static I2CBusPtr I2CBusList;
684
685/* Allocates an I2CBusRec for you and initializes with propper defaults
686 * you may modify before calling xf86I2CBusInit. Your I2CBusRec must
687 * contain at least a BusName, a scrnIndex (or -1), and a complete set
688 * of either high or low level I2C function pointers. You may pass
689 * bus-wide timeouts, otherwise inplausible values will be replaced
690 * with safe defaults.
691 */
692
693I2CBusPtr
694xf86CreateI2CBusRec(void)
695{
696    I2CBusPtr b;
697
698    b = (I2CBusPtr) calloc(1, sizeof(I2CBusRec));
699
700    if (b != NULL) {
701	b->scrnIndex = -1;
702	b->HoldTime = 5; /* 100 kHz bus */
703	b->BitTimeout = 5;
704	b->ByteTimeout = 5;
705	b->AcknTimeout = 5;
706	b->StartTimeout = 5;
707	b->RiseFallTime = RISEFALLTIME;
708    }
709
710    return b;
711}
712
713/* Unregister an I2C bus. If you got the I2CBusRec from xf86CreateI2CBusRec
714 * you should set <unalloc> to free it. If you set <devs_too>, the function
715 * xf86DestroyI2CDevRec will be called for all devices linked to the bus
716 * first, passing down the <unalloc> option.
717 */
718
719void
720xf86DestroyI2CBusRec(I2CBusPtr b, Bool unalloc, Bool devs_too)
721{
722    if (b) {
723	I2CBusPtr *p;
724
725	/* Remove this from the list of active I2C buses */
726
727	for (p = &I2CBusList; *p != NULL; p = &(*p)->NextBus)
728	    if (*p == b) {
729		*p = (*p)->NextBus;
730		break;
731	    }
732
733	if (b->FirstDev != NULL) {
734	    if (devs_too) {
735		I2CDevPtr d;
736
737		while ((d = b->FirstDev) != NULL) {
738		    b->FirstDev = d->NextDev;
739		    xf86DestroyI2CDevRec(d, unalloc);
740		}
741	    } else {
742		if (unalloc) {
743		    xf86Msg(X_ERROR, "i2c bug: Attempt to remove I2C bus \"%s\", "
744			    "but device list is not empty.\n",
745			    b->BusName);
746		    return;
747		}
748	    }
749	}
750
751	xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" removed.\n",
752		   b->BusName);
753
754	if (unalloc) free(b);
755    }
756}
757
758/* I2C masters have to register themselves using this function.
759 * It will not allocate an I2CBusRec for you, instead you may enter
760 * a pointer to a statically allocated I2CBusRec or the (modified)
761 * result of xf86CreateI2CBusRec. Returns TRUE on success.
762 *
763 * At this point there won't be any traffic on the I2C bus.
764 */
765
766Bool
767xf86I2CBusInit(I2CBusPtr b)
768{
769    /* I2C buses must be identified by a unique scrnIndex
770     * and name. If scrnIndex is unspecified (a negative value),
771     * then the name must be unique throughout the server.
772     */
773
774    if (b->BusName == NULL ||
775	xf86I2CFindBus(b->scrnIndex, b->BusName) != NULL)
776	return FALSE;
777
778    /* If the high level functions are not
779     * supplied, use the generic functions.
780     * In this case we need the low-level
781     * function.
782     */
783    if (b->I2CWriteRead == NULL)
784    {
785        b->I2CWriteRead=I2CWriteRead;
786
787        if (b->I2CPutBits == NULL ||
788	    b->I2CGetBits == NULL)
789        {
790	    if (b->I2CPutByte == NULL ||
791	        b->I2CGetByte == NULL ||
792	        b->I2CAddress == NULL ||
793	        b->I2CStart   == NULL ||
794	        b->I2CStop    == NULL)
795	        return FALSE;
796        } else {
797	    b->I2CPutByte = I2CPutByte;
798	    b->I2CGetByte = I2CGetByte;
799	    b->I2CAddress = I2CAddress;
800	    b->I2CStop    = I2CStop;
801	    b->I2CStart   = I2CStart;
802        }
803     }
804
805    if (b->I2CUDelay == NULL)
806	b->I2CUDelay = I2CUDelay;
807
808    if (b->HoldTime < 2) b->HoldTime = 5;
809    if (b->BitTimeout <= 0) b->BitTimeout = b->HoldTime;
810    if (b->ByteTimeout <= 0) b->ByteTimeout = b->HoldTime;
811    if (b->AcknTimeout <= 0) b->AcknTimeout = b->HoldTime;
812    if (b->StartTimeout <= 0) b->StartTimeout = b->HoldTime;
813
814    /* Put new bus on list. */
815
816    b->NextBus = I2CBusList;
817    I2CBusList = b;
818
819    xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" initialized.\n",
820	       b->BusName);
821
822    return TRUE;
823}
824
825I2CBusPtr
826xf86I2CFindBus(int scrnIndex, char *name)
827{
828    I2CBusPtr p;
829
830    if (name != NULL)
831	for (p = I2CBusList; p != NULL; p = p->NextBus)
832	    if (scrnIndex < 0 || p->scrnIndex == scrnIndex)
833		if (!strcmp(p->BusName, name))
834		    return p;
835
836    return NULL;
837}
838
839/*
840 * Return an array of I2CBusPtr's related to a screen.  The caller is
841 * responsible for freeing the array.
842 */
843int
844xf86I2CGetScreenBuses(int scrnIndex, I2CBusPtr **pppI2CBus)
845{
846    I2CBusPtr pI2CBus;
847    int n = 0;
848
849    if (pppI2CBus)
850	*pppI2CBus = NULL;
851
852    for (pI2CBus = I2CBusList;  pI2CBus;  pI2CBus = pI2CBus->NextBus) {
853	if ((pI2CBus->scrnIndex >= 0) && (pI2CBus->scrnIndex != scrnIndex))
854	    continue;
855
856	n++;
857
858	if (!pppI2CBus)
859	    continue;
860
861        *pppI2CBus = xnfrealloc(*pppI2CBus, n * sizeof(I2CBusPtr));
862	(*pppI2CBus)[n - 1] = pI2CBus;
863    }
864
865    return n;
866}
867