First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / i2c / xf86i2c.c
blob53382a859312685f9fa4e90048861325d8c273ce
1 /*
2 * Copyright (C) 1998 Itai Nahshon, Michael Schimek
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 */
10 #ifdef HAVE_XORG_CONFIG_H
11 #include <xorg-config.h>
12 #endif
14 #include <sys/time.h>
15 #include <string.h>
17 #include "misc.h"
18 #include "xf86.h"
19 #include "xf86_OSproc.h"
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"
33 #include "xf86i2c.h"
35 #define I2C_TIMEOUT(x) /*(x)*/ /* Report timeouts */
36 #define I2C_TRACE(x) /*(x)*/ /* Report progress */
38 /* Set which OSs have bad gettimeofday resolution. */
39 #if defined(SVR4) && !defined(sun)
40 #define BAD_GETTIMEOFDAY_RESOLUTION
41 #endif
44 /* This is the default I2CUDelay function if not supplied by the driver.
45 * High level I2C interfaces implementing the bus protocol in hardware
46 * should supply this function too.
48 * Delay execution at least usec microseconds.
49 * All values 0 to 1e6 inclusive must be expected.
52 #ifdef BAD_GETTIMEOFDAY_RESOLUTION
54 * This is temporary until a better, portable
55 * way is found. Adjust bogo_usec to match CPU speed.
57 static int bogo_usec = 500;
59 static void
60 I2CUDelay(I2CBusPtr b, int usec)
62 volatile long i;
64 if (usec > 0)
65 for (i = usec * bogo_usec; i > 0; i--)
66 /* (perhaps hw delay action) */;
68 #else
69 static void
70 I2CUDelay(I2CBusPtr b, int usec)
72 struct timeval begin, cur;
73 long d_secs, d_usecs;
74 long diff;
76 if (usec > 0) {
77 X_GETTIMEOFDAY(&begin);
78 do {
79 /* It would be nice to use {xf86}usleep,
80 * but usleep (1) takes >10000 usec !
82 X_GETTIMEOFDAY(&cur);
83 d_secs = (cur.tv_sec - begin.tv_sec);
84 d_usecs = (cur.tv_usec - begin.tv_usec);
85 diff = d_secs*1000000 + d_usecs;
86 } while (diff>=0 && diff< (usec + 1));
89 #endif
91 /* Most drivers will register just with GetBits/PutBits functions.
92 * The following functions implement a software I2C protocol
93 * by using the promitive functions given by the driver.
94 * ================================================================
96 * It is assumed that there is just one master on the I2C bus, therefore
97 * there is no explicit test for conflits.
100 #define RISEFALLTIME 2 /* usec, actually 300 to 1000 ns according to the i2c specs */
102 /* Some devices will hold SCL low to slow down the bus or until
103 * ready for transmission.
105 * This condition will be noticed when the master tries to raise
106 * the SCL line. You can set the timeout to zero if the slave device
107 * does not support this clock synchronization.
110 static Bool
111 I2CRaiseSCL(I2CBusPtr b, int sda, int timeout)
113 int i, scl;
115 b->I2CPutBits(b, 1, sda);
116 b->I2CUDelay(b, b->RiseFallTime);
118 for (i = timeout; i > 0; i -= b->RiseFallTime) {
119 b->I2CGetBits(b, &scl, &sda);
120 if (scl) break;
121 b->I2CUDelay(b, b->RiseFallTime);
124 if (i <= 0) {
125 I2C_TIMEOUT(ErrorF("[I2CRaiseSCL(<%s>, %d, %d) timeout]", b->BusName, sda, timeout));
126 return FALSE;
129 return TRUE;
132 /* Send a start signal on the I2C bus. The start signal notifies
133 * devices that a new transaction is initiated by the bus master.
135 * The start signal is always followed by a slave address.
136 * Slave addresses are 8+ bits. The first 7 bits identify the
137 * device and the last bit signals if this is a read (1) or
138 * write (0) operation.
140 * There may be more than one start signal on one transaction.
141 * This happens for example on some devices that allow reading
142 * of registers. First send a start bit followed by the device
143 * address (with the last bit 0) and the register number. Then send
144 * a new start bit with the device address (with the last bit 1)
145 * and then read the value from the device.
147 * Note this is function does not implement a multiple master
148 * arbitration procedure.
151 static Bool
152 I2CStart(I2CBusPtr b, int timeout)
154 if (!I2CRaiseSCL(b, 1, timeout))
155 return FALSE;
157 b->I2CPutBits(b, 1, 0);
158 b->I2CUDelay(b, b->HoldTime);
159 b->I2CPutBits(b, 0, 0);
160 b->I2CUDelay(b, b->HoldTime);
162 I2C_TRACE(ErrorF("\ni2c: <"));
164 return TRUE;
167 /* This is the default I2CStop function if not supplied by the driver.
169 * Signal devices on the I2C bus that a transaction on the
170 * bus has finished. There may be more than one start signal
171 * on a transaction but only one stop signal.
174 static void
175 I2CStop(I2CDevPtr d)
177 I2CBusPtr b = d->pI2CBus;
179 b->I2CPutBits(b, 0, 0);
180 b->I2CUDelay(b, b->RiseFallTime);
182 b->I2CPutBits(b, 1, 0);
183 b->I2CUDelay(b, b->HoldTime);
184 b->I2CPutBits(b, 1, 1);
185 b->I2CUDelay(b, b->HoldTime);
187 I2C_TRACE(ErrorF(">\n"));
190 /* Write/Read a single bit to/from a device.
191 * Return FALSE if a timeout occurs.
194 static Bool
195 I2CWriteBit(I2CBusPtr b, int sda, int timeout)
197 Bool r;
199 b->I2CPutBits(b, 0, sda);
200 b->I2CUDelay(b, b->RiseFallTime);
202 r = I2CRaiseSCL(b, sda, timeout);
203 b->I2CUDelay(b, b->HoldTime);
205 b->I2CPutBits(b, 0, sda);
206 b->I2CUDelay(b, b->HoldTime);
208 return r;
211 static Bool
212 I2CReadBit(I2CBusPtr b, int *psda, int timeout)
214 Bool r;
215 int scl;
217 r = I2CRaiseSCL(b, 1, timeout);
218 b->I2CUDelay(b, b->HoldTime);
220 b->I2CGetBits(b, &scl, psda);
222 b->I2CPutBits(b, 0, 1);
223 b->I2CUDelay(b, b->HoldTime);
225 return r;
228 /* This is the default I2CPutByte function if not supplied by the driver.
230 * A single byte is sent to the device.
231 * The function returns FALSE if a timeout occurs, you should send
232 * a stop condition afterwards to reset the bus.
234 * A timeout occurs,
235 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
236 * or slows down the bus for more than BitTimeout usecs for each bit,
237 * or does not send an ACK bit (0) to acknowledge the transmission within
238 * AcknTimeout usecs, but a NACK (1) bit.
240 * AcknTimeout must be at least b->HoldTime, the other timeouts can be
241 * zero according to the comment on I2CRaiseSCL.
244 static Bool
245 I2CPutByte(I2CDevPtr d, I2CByte data)
247 Bool r;
248 int i, scl, sda;
249 I2CBusPtr b = d->pI2CBus;
251 if (!I2CWriteBit(b, (data >> 7) & 1, d->ByteTimeout))
252 return FALSE;
254 for (i = 6; i >= 0; i--)
255 if (!I2CWriteBit(b, (data >> i) & 1, d->BitTimeout))
256 return FALSE;
258 b->I2CPutBits(b, 0, 1);
259 b->I2CUDelay(b, b->RiseFallTime);
261 r = I2CRaiseSCL(b, 1, b->HoldTime);
263 if (r) {
264 for (i = d->AcknTimeout; i > 0; i -= b->HoldTime) {
265 b->I2CUDelay(b, b->HoldTime);
266 b->I2CGetBits(b, &scl, &sda);
267 if (sda == 0) break;
270 if (i <= 0) {
271 I2C_TIMEOUT(ErrorF("[I2CPutByte(<%s>, 0x%02x, %d, %d, %d) timeout]",
272 b->BusName, data, d->BitTimeout,
273 d->ByteTimeout, d->AcknTimeout));
274 r = FALSE;
277 I2C_TRACE(ErrorF("W%02x%c ", (int) data, sda ? '-' : '+'));
280 b->I2CPutBits(b, 0, 1);
281 b->I2CUDelay(b, b->HoldTime);
283 return r;
286 /* This is the default I2CGetByte function if not supplied by the driver.
288 * A single byte is read from the device.
289 * The function returns FALSE if a timeout occurs, you should send
290 * a stop condition afterwards to reset the bus.
292 * A timeout occurs,
293 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
294 * or slows down the bus for more than b->BitTimeout usecs for each bit.
296 * ByteTimeout must be at least b->HoldTime, the other timeouts can be
297 * zero according to the comment on I2CRaiseSCL.
299 * For the <last> byte in a sequence the acknowledge bit NACK (1),
300 * otherwise ACK (0) will be sent.
303 static Bool
304 I2CGetByte(I2CDevPtr d, I2CByte *data, Bool last)
306 int i, sda;
307 I2CBusPtr b = d->pI2CBus;
309 b->I2CPutBits(b, 0, 1);
310 b->I2CUDelay(b, b->RiseFallTime);
312 if (!I2CReadBit(b, &sda, d->ByteTimeout))
313 return FALSE;
315 *data = (sda > 0) << 7;
317 for (i = 6; i >= 0; i--)
318 if (!I2CReadBit(b, &sda, d->BitTimeout))
319 return FALSE;
320 else
321 *data |= (sda > 0) << i;
323 if (!I2CWriteBit(b, last ? 1 : 0, d->BitTimeout))
324 return FALSE;
326 I2C_TRACE(ErrorF("R%02x%c ", (int) *data, last ? '+' : '-'));
328 return TRUE;
331 /* This is the default I2CAddress function if not supplied by the driver.
333 * It creates the start condition, followed by the d->SlaveAddr.
334 * Higher level functions must call this routine rather than
335 * I2CStart/PutByte because a hardware I2C master may not be able
336 * to send a slave address without a start condition.
338 * The same timeouts apply as with I2CPutByte and additional a
339 * StartTimeout, similar to the ByteTimeout but for the start
340 * condition.
342 * In case of a timeout, the bus is left in a clean idle condition.
343 * I. e. you *must not* send a Stop. If this function succeeds, you *must*.
345 * The slave address format is 16 bit, with the legacy _8_bit_ slave address
346 * in the least significant byte. This is, the slave address must include the
347 * R/_W flag as least significant bit.
349 * The most significant byte of the address will be sent _after_ the LSB,
350 * but only if the LSB indicates:
351 * a) an 11 bit address, this is LSB = 1111 0xxx.
352 * b) a 'general call address', this is LSB = 0000 000x - see the I2C specs
353 * for more.
356 static Bool
357 I2CAddress(I2CDevPtr d, I2CSlaveAddr addr)
359 if (I2CStart(d->pI2CBus, d->StartTimeout)) {
360 if (I2CPutByte(d, addr & 0xFF)) {
361 if ((addr & 0xF8) != 0xF0 &&
362 (addr & 0xFE) != 0x00)
363 return TRUE;
365 if (I2CPutByte(d, (addr >> 8) & 0xFF))
366 return TRUE;
369 I2CStop(d);
372 return FALSE;
375 /* These are the hardware independent I2C helper functions.
376 * ========================================================
379 /* Function for probing. Just send the slave address
380 * and return true if the device responds. The slave address
381 * must have the lsb set to reflect a read (1) or write (0) access.
382 * Don't expect a read- or write-only device will respond otherwise.
385 Bool
386 xf86I2CProbeAddress(I2CBusPtr b, I2CSlaveAddr addr)
388 int r;
389 I2CDevRec d;
391 d.DevName = "Probing";
392 d.BitTimeout = b->BitTimeout;
393 d.ByteTimeout = b->ByteTimeout;
394 d.AcknTimeout = b->AcknTimeout;
395 d.StartTimeout = b->StartTimeout;
396 d.SlaveAddr = addr;
397 d.pI2CBus = b;
398 d.NextDev = NULL;
400 r = b->I2CAddress(&d, addr);
402 if (r) b->I2CStop(&d);
404 return r;
407 /* All functions below are related to devices and take the
408 * slave address and timeout values from an I2CDevRec. They
409 * return FALSE in case of an error (presumably a timeout).
412 /* General purpose read and write function.
414 * 1st, if nWrite > 0
415 * Send a start condition
416 * Send the slave address (1 or 2 bytes) with write flag
417 * Write n bytes from WriteBuffer
418 * 2nd, if nRead > 0
419 * Send a start condition [again]
420 * Send the slave address (1 or 2 bytes) with read flag
421 * Read n bytes to ReadBuffer
422 * 3rd, if a Start condition has been successfully sent,
423 * Send a Stop condition.
425 * The functions exits immediately when an error occures,
426 * not proceeding any data left. However, step 3 will
427 * be executed anyway to leave the bus in clean idle state.
430 static Bool
431 I2CWriteRead(I2CDevPtr d,
432 I2CByte *WriteBuffer, int nWrite,
433 I2CByte *ReadBuffer, int nRead)
435 Bool r = TRUE;
436 I2CBusPtr b = d->pI2CBus;
437 int s = 0;
439 if (r && nWrite > 0) {
440 r = b->I2CAddress(d, d->SlaveAddr & ~1);
441 if (r) {
442 for (; nWrite > 0; WriteBuffer++, nWrite--)
443 if (!(r = b->I2CPutByte(d, *WriteBuffer)))
444 break;
445 s++;
449 if (r && nRead > 0) {
450 r = b->I2CAddress(d, d->SlaveAddr | 1);
451 if (r) {
452 for (; nRead > 0; ReadBuffer++, nRead--)
453 if (!(r = b->I2CGetByte(d, ReadBuffer, nRead == 1)))
454 break;
455 s++;
459 if (s) b->I2CStop(d);
461 return r;
464 /* wrapper - for compatibility and convinience */
466 Bool
467 xf86I2CWriteRead(I2CDevPtr d,
468 I2CByte *WriteBuffer, int nWrite,
469 I2CByte *ReadBuffer, int nRead)
471 I2CBusPtr b = d->pI2CBus;
472 return b->I2CWriteRead(d,WriteBuffer,nWrite,ReadBuffer,nRead);
475 /* Read a byte, the only readable register of a device.
478 Bool
479 xf86I2CReadStatus(I2CDevPtr d, I2CByte *pbyte)
481 return xf86I2CWriteRead(d, NULL, 0, pbyte, 1);
484 /* Read a byte from one of the registers determined by its sub-address.
487 Bool
488 xf86I2CReadByte(I2CDevPtr d, I2CByte subaddr, I2CByte *pbyte)
490 return xf86I2CWriteRead(d, &subaddr, 1, pbyte, 1);
493 /* Read bytes from subsequent registers determined by the
494 * sub-address of the first register.
497 Bool
498 xf86I2CReadBytes(I2CDevPtr d, I2CByte subaddr, I2CByte *pbyte, int n)
500 return xf86I2CWriteRead(d, &subaddr, 1, pbyte, n);
503 /* Read a word (high byte, then low byte) from one of the registers
504 * determined by its sub-address.
507 Bool
508 xf86I2CReadWord(I2CDevPtr d, I2CByte subaddr, unsigned short *pword)
510 I2CByte rb[2];
512 if (!xf86I2CWriteRead(d, &subaddr, 1, rb, 2)) return FALSE;
514 *pword = (rb[0] << 8) | rb[1];
516 return TRUE;
519 /* Write a byte to one of the registers determined by its sub-address.
522 Bool
523 xf86I2CWriteByte(I2CDevPtr d, I2CByte subaddr, I2CByte byte)
525 I2CByte wb[2];
527 wb[0] = subaddr;
528 wb[1] = byte;
530 return xf86I2CWriteRead(d, wb, 2, NULL, 0);
533 /* Write bytes to subsequent registers determined by the
534 * sub-address of the first register.
537 Bool
538 xf86I2CWriteBytes(I2CDevPtr d, I2CByte subaddr,
539 I2CByte *WriteBuffer, int nWrite)
541 I2CBusPtr b = d->pI2CBus;
542 Bool r = TRUE;
544 if (nWrite > 0) {
545 r = b->I2CAddress(d, d->SlaveAddr & ~1);
546 if (r){
547 if ((r = b->I2CPutByte(d, subaddr)))
548 for (; nWrite > 0; WriteBuffer++, nWrite--)
549 if (!(r = b->I2CPutByte(d, *WriteBuffer)))
550 break;
552 b->I2CStop(d);
556 return r;
559 /* Write a word (high byte, then low byte) to one of the registers
560 * determined by its sub-address.
563 Bool
564 xf86I2CWriteWord(I2CDevPtr d, I2CByte subaddr, unsigned short word)
566 I2CByte wb[3];
568 wb[0] = subaddr;
569 wb[1] = word >> 8;
570 wb[2] = word & 0xFF;
572 return xf86I2CWriteRead(d, wb, 3, NULL, 0);
575 /* Write a vector of bytes to not adjacent registers. This vector is,
576 * 1st byte sub-address, 2nd byte value, 3rd byte sub-address asf.
577 * This function is intended to initialize devices. Note this function
578 * exits immediately when an error occurs, some registers may
579 * remain uninitialized.
582 Bool
583 xf86I2CWriteVec(I2CDevPtr d, I2CByte *vec, int nValues)
585 I2CBusPtr b = d->pI2CBus;
586 Bool r = TRUE;
587 int s = 0;
589 if (nValues > 0) {
590 for (; nValues > 0; nValues--, vec += 2) {
591 if (!(r = b->I2CAddress(d, d->SlaveAddr & ~1)))
592 break;
594 s++;
596 if (!(r = b->I2CPutByte(d, vec[0])))
597 break;
599 if (!(r = b->I2CPutByte(d, vec[1])))
600 break;
603 if (s > 0) b->I2CStop(d);
606 return r;
609 /* Administrative functions.
610 * =========================
613 /* Allocates an I2CDevRec for you and initializes with propper defaults
614 * you may modify before calling xf86I2CDevInit. Your I2CDevRec must
615 * contain at least a SlaveAddr, and a pI2CBus pointer to the bus this
616 * device shall be linked to.
618 * See function I2CAddress for the slave address format. Always set
619 * the least significant bit, indicating a read or write access, to zero.
622 I2CDevPtr
623 xf86CreateI2CDevRec(void)
625 return xcalloc(1, sizeof(I2CDevRec));
628 /* Unlink an I2C device. If you got the I2CDevRec from xf86CreateI2CDevRec
629 * you should set <unalloc> to free it.
632 void
633 xf86DestroyI2CDevRec(I2CDevPtr d, Bool unalloc)
635 if (d) {
636 I2CDevPtr *p;
638 /* Remove this from the list of active I2C devices. */
640 for (p = &d->pI2CBus->FirstDev; *p != NULL; p = &(*p)->NextDev)
641 if (*p == d) {
642 *p = (*p)->NextDev;
643 break;
646 xf86DrvMsg(d->pI2CBus->scrnIndex, X_INFO,
647 "I2C device \"%s:%s\" removed.\n",
648 d->pI2CBus->BusName, d->DevName);
650 if (unalloc) xfree(d);
654 /* I2C transmissions are related to an I2CDevRec you must link to a
655 * previously registered bus (see xf86I2CBusInit) before attempting
656 * to read and write data. You may call xf86I2CProbeAddress first to
657 * see if the device in question is present on this bus.
659 * xf86I2CDevInit will not allocate an I2CBusRec for you, instead you
660 * may enter a pointer to a statically allocated I2CDevRec or the (modified)
661 * result of xf86CreateI2CDevRec.
663 * If you don't specify timeouts for the device (n <= 0), it will inherit
664 * the bus-wide defaults. The function returns TRUE on success.
667 Bool
668 xf86I2CDevInit(I2CDevPtr d)
670 I2CBusPtr b;
672 if (d == NULL ||
673 (b = d->pI2CBus) == NULL ||
674 (d->SlaveAddr & 1) ||
675 xf86I2CFindDev(b, d->SlaveAddr) != NULL)
676 return FALSE;
678 if (d->BitTimeout <= 0) d->BitTimeout = b->BitTimeout;
679 if (d->ByteTimeout <= 0) d->ByteTimeout = b->ByteTimeout;
680 if (d->AcknTimeout <= 0) d->AcknTimeout = b->AcknTimeout;
681 if (d->StartTimeout <= 0) d->StartTimeout = b->StartTimeout;
683 d->NextDev = b->FirstDev;
684 b->FirstDev = d;
686 xf86DrvMsg(b->scrnIndex, X_INFO,
687 "I2C device \"%s:%s\" registered at address 0x%02X.\n",
688 b->BusName, d->DevName, d->SlaveAddr);
690 return TRUE;
693 I2CDevPtr
694 xf86I2CFindDev(I2CBusPtr b, I2CSlaveAddr addr)
696 I2CDevPtr d;
698 if (b) {
699 for (d = b->FirstDev; d != NULL; d = d->NextDev)
700 if (d->SlaveAddr == addr)
701 return d;
704 return NULL;
707 static I2CBusPtr I2CBusList;
709 /* Allocates an I2CBusRec for you and initializes with propper defaults
710 * you may modify before calling xf86I2CBusInit. Your I2CBusRec must
711 * contain at least a BusName, a scrnIndex (or -1), and a complete set
712 * of either high or low level I2C function pointers. You may pass
713 * bus-wide timeouts, otherwise inplausible values will be replaced
714 * with safe defaults.
717 I2CBusPtr
718 xf86CreateI2CBusRec(void)
720 I2CBusPtr b;
722 b = (I2CBusPtr) xcalloc(1, sizeof(I2CBusRec));
724 if (b != NULL) {
725 b->scrnIndex = -1;
726 b->HoldTime = 5; /* 100 kHz bus */
727 b->BitTimeout = 5;
728 b->ByteTimeout = 5;
729 b->AcknTimeout = 5;
730 b->StartTimeout = 5;
731 b->RiseFallTime = RISEFALLTIME;
734 return b;
737 /* Unregister an I2C bus. If you got the I2CBusRec from xf86CreateI2CBusRec
738 * you should set <unalloc> to free it. If you set <devs_too>, the function
739 * xf86DestroyI2CDevRec will be called for all devices linked to the bus
740 * first, passing down the <unalloc> option.
743 void
744 xf86DestroyI2CBusRec(I2CBusPtr b, Bool unalloc, Bool devs_too)
746 if (b) {
747 I2CBusPtr *p;
749 /* Remove this from the list of active I2C buses */
751 for (p = &I2CBusList; *p != NULL; p = &(*p)->NextBus)
752 if (*p == b) {
753 *p = (*p)->NextBus;
754 break;
757 if (b->FirstDev != NULL) {
758 if (devs_too) {
759 I2CDevPtr d;
761 while ((d = b->FirstDev) != NULL) {
762 b->FirstDev = d->NextDev;
763 xf86DestroyI2CDevRec(d, unalloc);
765 } else {
766 if (unalloc) {
767 xf86Msg(X_ERROR, "i2c bug: Attempt to remove I2C bus \"%s\", "
768 "but device list is not empty.\n",
769 b->BusName);
770 return;
775 xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" removed.\n",
776 b->BusName);
778 if (unalloc) xfree(b);
782 /* I2C masters have to register themselves using this function.
783 * It will not allocate an I2CBusRec for you, instead you may enter
784 * a pointer to a statically allocated I2CBusRec or the (modified)
785 * result of xf86CreateI2CBusRec. Returns TRUE on success.
787 * At this point there won't be any traffic on the I2C bus.
790 Bool
791 xf86I2CBusInit(I2CBusPtr b)
793 /* I2C buses must be identified by a unique scrnIndex
794 * and name. If scrnIndex is unspecified (a negative value),
795 * then the name must be unique throughout the server.
798 if (b->BusName == NULL ||
799 xf86I2CFindBus(b->scrnIndex, b->BusName) != NULL)
800 return FALSE;
802 /* If the high level functions are not
803 * supplied, use the generic functions.
804 * In this case we need the low-level
805 * function.
807 if (b->I2CWriteRead == NULL)
809 b->I2CWriteRead=I2CWriteRead;
811 if (b->I2CPutBits == NULL ||
812 b->I2CGetBits == NULL)
814 if (b->I2CPutByte == NULL ||
815 b->I2CGetByte == NULL ||
816 b->I2CAddress == NULL ||
817 b->I2CStart == NULL ||
818 b->I2CStop == NULL)
819 return FALSE;
820 } else {
821 b->I2CPutByte = I2CPutByte;
822 b->I2CGetByte = I2CGetByte;
823 b->I2CAddress = I2CAddress;
824 b->I2CStop = I2CStop;
825 b->I2CStart = I2CStart;
829 if (b->I2CUDelay == NULL)
830 b->I2CUDelay = I2CUDelay;
832 if (b->HoldTime < 2) b->HoldTime = 5;
833 if (b->BitTimeout <= 0) b->BitTimeout = b->HoldTime;
834 if (b->ByteTimeout <= 0) b->ByteTimeout = b->HoldTime;
835 if (b->AcknTimeout <= 0) b->AcknTimeout = b->HoldTime;
836 if (b->StartTimeout <= 0) b->StartTimeout = b->HoldTime;
838 /* Put new bus on list. */
840 b->NextBus = I2CBusList;
841 I2CBusList = b;
843 xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" initialized.\n",
844 b->BusName);
846 return TRUE;
849 I2CBusPtr
850 xf86I2CFindBus(int scrnIndex, char *name)
852 I2CBusPtr p;
854 if (name != NULL)
855 for (p = I2CBusList; p != NULL; p = p->NextBus)
856 if (scrnIndex < 0 || p->scrnIndex == scrnIndex)
857 if (!strcmp(p->BusName, name))
858 return p;
860 return NULL;
864 * Return an array of I2CBusPtr's related to a screen. The caller is
865 * responsible for freeing the array.
868 xf86I2CGetScreenBuses(int scrnIndex, I2CBusPtr **pppI2CBus)
870 I2CBusPtr pI2CBus;
871 int n = 0;
873 if (pppI2CBus)
874 *pppI2CBus = NULL;
876 for (pI2CBus = I2CBusList; pI2CBus; pI2CBus = pI2CBus->NextBus) {
877 if ((pI2CBus->scrnIndex >= 0) && (pI2CBus->scrnIndex != scrnIndex))
878 continue;
880 n++;
882 if (!pppI2CBus)
883 continue;
885 *pppI2CBus = xnfrealloc(*pppI2CBus, n * sizeof(I2CBusPtr));
886 *pppI2CBus[n - 1] = pI2CBus;
889 return n;