Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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