xf86i2c.c revision 05b261ec
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/* Set which OSs have bad gettimeofday resolution. */ 39#if defined(SVR4) && !defined(sun) 40#define BAD_GETTIMEOFDAY_RESOLUTION 41#endif 42 43 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. 47 * 48 * Delay execution at least usec microseconds. 49 * All values 0 to 1e6 inclusive must be expected. 50 */ 51 52#ifdef BAD_GETTIMEOFDAY_RESOLUTION 53/* 54 * This is temporary until a better, portable 55 * way is found. Adjust bogo_usec to match CPU speed. 56 */ 57static int bogo_usec = 500; 58 59static void 60I2CUDelay(I2CBusPtr b, int usec) 61{ 62 volatile long i; 63 64 if (usec > 0) 65 for (i = usec * bogo_usec; i > 0; i--) 66 /* (perhaps hw delay action) */; 67} 68#else 69static void 70I2CUDelay(I2CBusPtr b, int usec) 71{ 72 struct timeval begin, cur; 73 long d_secs, d_usecs; 74 long diff; 75 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 ! 81 */ 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)); 87 } 88} 89#endif 90 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 * ================================================================ 95 * 96 * It is assumed that there is just one master on the I2C bus, therefore 97 * there is no explicit test for conflits. 98 */ 99 100#define RISEFALLTIME 2 /* usec, actually 300 to 1000 ns according to the i2c specs */ 101 102/* Some devices will hold SCL low to slow down the bus or until 103 * ready for transmission. 104 * 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. 108 */ 109 110static Bool 111I2CRaiseSCL(I2CBusPtr b, int sda, int timeout) 112{ 113 int i, scl; 114 115 b->I2CPutBits(b, 1, sda); 116 b->I2CUDelay(b, b->RiseFallTime); 117 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); 122 } 123 124 if (i <= 0) { 125 I2C_TIMEOUT(ErrorF("[I2CRaiseSCL(<%s>, %d, %d) timeout]", b->BusName, sda, timeout)); 126 return FALSE; 127 } 128 129 return TRUE; 130} 131 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. 134 * 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. 139 * 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. 146 * 147 * Note this is function does not implement a multiple master 148 * arbitration procedure. 149 */ 150 151static Bool 152I2CStart(I2CBusPtr b, int timeout) 153{ 154 if (!I2CRaiseSCL(b, 1, timeout)) 155 return FALSE; 156 157 b->I2CPutBits(b, 1, 0); 158 b->I2CUDelay(b, b->HoldTime); 159 b->I2CPutBits(b, 0, 0); 160 b->I2CUDelay(b, b->HoldTime); 161 162 I2C_TRACE(ErrorF("\ni2c: <")); 163 164 return TRUE; 165} 166 167/* This is the default I2CStop function if not supplied by the driver. 168 * 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. 172 */ 173 174static void 175I2CStop(I2CDevPtr d) 176{ 177 I2CBusPtr b = d->pI2CBus; 178 179 b->I2CPutBits(b, 0, 0); 180 b->I2CUDelay(b, b->RiseFallTime); 181 182 b->I2CPutBits(b, 1, 0); 183 b->I2CUDelay(b, b->HoldTime); 184 b->I2CPutBits(b, 1, 1); 185 b->I2CUDelay(b, b->HoldTime); 186 187 I2C_TRACE(ErrorF(">\n")); 188} 189 190/* Write/Read a single bit to/from a device. 191 * Return FALSE if a timeout occurs. 192 */ 193 194static Bool 195I2CWriteBit(I2CBusPtr b, int sda, int timeout) 196{ 197 Bool r; 198 199 b->I2CPutBits(b, 0, sda); 200 b->I2CUDelay(b, b->RiseFallTime); 201 202 r = I2CRaiseSCL(b, sda, timeout); 203 b->I2CUDelay(b, b->HoldTime); 204 205 b->I2CPutBits(b, 0, sda); 206 b->I2CUDelay(b, b->HoldTime); 207 208 return r; 209} 210 211static Bool 212I2CReadBit(I2CBusPtr b, int *psda, int timeout) 213{ 214 Bool r; 215 int scl; 216 217 r = I2CRaiseSCL(b, 1, timeout); 218 b->I2CUDelay(b, b->HoldTime); 219 220 b->I2CGetBits(b, &scl, psda); 221 222 b->I2CPutBits(b, 0, 1); 223 b->I2CUDelay(b, b->HoldTime); 224 225 return r; 226} 227 228/* This is the default I2CPutByte function if not supplied by the driver. 229 * 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. 233 * 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. 239 * 240 * AcknTimeout must be at least b->HoldTime, the other timeouts can be 241 * zero according to the comment on I2CRaiseSCL. 242 */ 243 244static Bool 245I2CPutByte(I2CDevPtr d, I2CByte data) 246{ 247 Bool r; 248 int i, scl, sda; 249 I2CBusPtr b = d->pI2CBus; 250 251 if (!I2CWriteBit(b, (data >> 7) & 1, d->ByteTimeout)) 252 return FALSE; 253 254 for (i = 6; i >= 0; i--) 255 if (!I2CWriteBit(b, (data >> i) & 1, d->BitTimeout)) 256 return FALSE; 257 258 b->I2CPutBits(b, 0, 1); 259 b->I2CUDelay(b, b->RiseFallTime); 260 261 r = I2CRaiseSCL(b, 1, b->HoldTime); 262 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; 268 } 269 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; 275 } 276 277 I2C_TRACE(ErrorF("W%02x%c ", (int) data, sda ? '-' : '+')); 278 } 279 280 b->I2CPutBits(b, 0, 1); 281 b->I2CUDelay(b, b->HoldTime); 282 283 return r; 284} 285 286/* This is the default I2CGetByte function if not supplied by the driver. 287 * 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. 291 * 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. 295 * 296 * ByteTimeout must be at least b->HoldTime, the other timeouts can be 297 * zero according to the comment on I2CRaiseSCL. 298 * 299 * For the <last> byte in a sequence the acknowledge bit NACK (1), 300 * otherwise ACK (0) will be sent. 301 */ 302 303static Bool 304I2CGetByte(I2CDevPtr d, I2CByte *data, Bool last) 305{ 306 int i, sda; 307 I2CBusPtr b = d->pI2CBus; 308 309 b->I2CPutBits(b, 0, 1); 310 b->I2CUDelay(b, b->RiseFallTime); 311 312 if (!I2CReadBit(b, &sda, d->ByteTimeout)) 313 return FALSE; 314 315 *data = (sda > 0) << 7; 316 317 for (i = 6; i >= 0; i--) 318 if (!I2CReadBit(b, &sda, d->BitTimeout)) 319 return FALSE; 320 else 321 *data |= (sda > 0) << i; 322 323 if (!I2CWriteBit(b, last ? 1 : 0, d->BitTimeout)) 324 return FALSE; 325 326 I2C_TRACE(ErrorF("R%02x%c ", (int) *data, last ? '+' : '-')); 327 328 return TRUE; 329} 330 331/* This is the default I2CAddress function if not supplied by the driver. 332 * 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. 337 * 338 * The same timeouts apply as with I2CPutByte and additional a 339 * StartTimeout, similar to the ByteTimeout but for the start 340 * condition. 341 * 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*. 344 * 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. 348 * 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. 354 */ 355 356static Bool 357I2CAddress(I2CDevPtr d, I2CSlaveAddr addr) 358{ 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; 364 365 if (I2CPutByte(d, (addr >> 8) & 0xFF)) 366 return TRUE; 367 } 368 369 I2CStop(d); 370 } 371 372 return FALSE; 373} 374 375/* These are the hardware independent I2C helper functions. 376 * ======================================================== 377 */ 378 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. 383 */ 384 385Bool 386xf86I2CProbeAddress(I2CBusPtr b, I2CSlaveAddr addr) 387{ 388 int r; 389 I2CDevRec d; 390 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; 399 400 r = b->I2CAddress(&d, addr); 401 402 if (r) b->I2CStop(&d); 403 404 return r; 405} 406 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). 410 */ 411 412/* General purpose read and write function. 413 * 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. 424 * 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. 428 */ 429 430static Bool 431I2CWriteRead(I2CDevPtr d, 432 I2CByte *WriteBuffer, int nWrite, 433 I2CByte *ReadBuffer, int nRead) 434{ 435 Bool r = TRUE; 436 I2CBusPtr b = d->pI2CBus; 437 int s = 0; 438 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++; 446 } 447 } 448 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++; 456 } 457 } 458 459 if (s) b->I2CStop(d); 460 461 return r; 462} 463 464/* wrapper - for compatibility and convinience */ 465 466Bool 467xf86I2CWriteRead(I2CDevPtr d, 468 I2CByte *WriteBuffer, int nWrite, 469 I2CByte *ReadBuffer, int nRead) 470{ 471 I2CBusPtr b = d->pI2CBus; 472 return b->I2CWriteRead(d,WriteBuffer,nWrite,ReadBuffer,nRead); 473} 474 475/* Read a byte, the only readable register of a device. 476 */ 477 478Bool 479xf86I2CReadStatus(I2CDevPtr d, I2CByte *pbyte) 480{ 481 return xf86I2CWriteRead(d, NULL, 0, pbyte, 1); 482} 483 484/* Read a byte from one of the registers determined by its sub-address. 485 */ 486 487Bool 488xf86I2CReadByte(I2CDevPtr d, I2CByte subaddr, I2CByte *pbyte) 489{ 490 return xf86I2CWriteRead(d, &subaddr, 1, pbyte, 1); 491} 492 493/* Read bytes from subsequent registers determined by the 494 * sub-address of the first register. 495 */ 496 497Bool 498xf86I2CReadBytes(I2CDevPtr d, I2CByte subaddr, I2CByte *pbyte, int n) 499{ 500 return xf86I2CWriteRead(d, &subaddr, 1, pbyte, n); 501} 502 503/* Read a word (high byte, then low byte) from one of the registers 504 * determined by its sub-address. 505 */ 506 507Bool 508xf86I2CReadWord(I2CDevPtr d, I2CByte subaddr, unsigned short *pword) 509{ 510 I2CByte rb[2]; 511 512 if (!xf86I2CWriteRead(d, &subaddr, 1, rb, 2)) return FALSE; 513 514 *pword = (rb[0] << 8) | rb[1]; 515 516 return TRUE; 517} 518 519/* Write a byte to one of the registers determined by its sub-address. 520 */ 521 522Bool 523xf86I2CWriteByte(I2CDevPtr d, I2CByte subaddr, I2CByte byte) 524{ 525 I2CByte wb[2]; 526 527 wb[0] = subaddr; 528 wb[1] = byte; 529 530 return xf86I2CWriteRead(d, wb, 2, NULL, 0); 531} 532 533/* Write bytes to subsequent registers determined by the 534 * sub-address of the first register. 535 */ 536 537Bool 538xf86I2CWriteBytes(I2CDevPtr d, I2CByte subaddr, 539 I2CByte *WriteBuffer, int nWrite) 540{ 541 I2CBusPtr b = d->pI2CBus; 542 Bool r = TRUE; 543 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; 551 552 b->I2CStop(d); 553 } 554 } 555 556 return r; 557} 558 559/* Write a word (high byte, then low byte) to one of the registers 560 * determined by its sub-address. 561 */ 562 563Bool 564xf86I2CWriteWord(I2CDevPtr d, I2CByte subaddr, unsigned short word) 565{ 566 I2CByte wb[3]; 567 568 wb[0] = subaddr; 569 wb[1] = word >> 8; 570 wb[2] = word & 0xFF; 571 572 return xf86I2CWriteRead(d, wb, 3, NULL, 0); 573} 574 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. 580 */ 581 582Bool 583xf86I2CWriteVec(I2CDevPtr d, I2CByte *vec, int nValues) 584{ 585 I2CBusPtr b = d->pI2CBus; 586 Bool r = TRUE; 587 int s = 0; 588 589 if (nValues > 0) { 590 for (; nValues > 0; nValues--, vec += 2) { 591 if (!(r = b->I2CAddress(d, d->SlaveAddr & ~1))) 592 break; 593 594 s++; 595 596 if (!(r = b->I2CPutByte(d, vec[0]))) 597 break; 598 599 if (!(r = b->I2CPutByte(d, vec[1]))) 600 break; 601 } 602 603 if (s > 0) b->I2CStop(d); 604 } 605 606 return r; 607} 608 609/* Administrative functions. 610 * ========================= 611 */ 612 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. 617 * 618 * See function I2CAddress for the slave address format. Always set 619 * the least significant bit, indicating a read or write access, to zero. 620 */ 621 622I2CDevPtr 623xf86CreateI2CDevRec(void) 624{ 625 return xcalloc(1, sizeof(I2CDevRec)); 626} 627 628/* Unlink an I2C device. If you got the I2CDevRec from xf86CreateI2CDevRec 629 * you should set <unalloc> to free it. 630 */ 631 632void 633xf86DestroyI2CDevRec(I2CDevPtr d, Bool unalloc) 634{ 635 if (d) { 636 I2CDevPtr *p; 637 638 /* Remove this from the list of active I2C devices. */ 639 640 for (p = &d->pI2CBus->FirstDev; *p != NULL; p = &(*p)->NextDev) 641 if (*p == d) { 642 *p = (*p)->NextDev; 643 break; 644 } 645 646 xf86DrvMsg(d->pI2CBus->scrnIndex, X_INFO, 647 "I2C device \"%s:%s\" removed.\n", 648 d->pI2CBus->BusName, d->DevName); 649 650 if (unalloc) xfree(d); 651 } 652} 653 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. 658 * 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. 662 * 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. 665 */ 666 667Bool 668xf86I2CDevInit(I2CDevPtr d) 669{ 670 I2CBusPtr b; 671 672 if (d == NULL || 673 (b = d->pI2CBus) == NULL || 674 (d->SlaveAddr & 1) || 675 xf86I2CFindDev(b, d->SlaveAddr) != NULL) 676 return FALSE; 677 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; 682 683 d->NextDev = b->FirstDev; 684 b->FirstDev = d; 685 686 xf86DrvMsg(b->scrnIndex, X_INFO, 687 "I2C device \"%s:%s\" registered at address 0x%02X.\n", 688 b->BusName, d->DevName, d->SlaveAddr); 689 690 return TRUE; 691} 692 693I2CDevPtr 694xf86I2CFindDev(I2CBusPtr b, I2CSlaveAddr addr) 695{ 696 I2CDevPtr d; 697 698 if (b) { 699 for (d = b->FirstDev; d != NULL; d = d->NextDev) 700 if (d->SlaveAddr == addr) 701 return d; 702 } 703 704 return NULL; 705} 706 707static I2CBusPtr I2CBusList; 708 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. 715 */ 716 717I2CBusPtr 718xf86CreateI2CBusRec(void) 719{ 720 I2CBusPtr b; 721 722 b = (I2CBusPtr) xcalloc(1, sizeof(I2CBusRec)); 723 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; 732 } 733 734 return b; 735} 736 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. 741 */ 742 743void 744xf86DestroyI2CBusRec(I2CBusPtr b, Bool unalloc, Bool devs_too) 745{ 746 if (b) { 747 I2CBusPtr *p; 748 749 /* Remove this from the list of active I2C buses */ 750 751 for (p = &I2CBusList; *p != NULL; p = &(*p)->NextBus) 752 if (*p == b) { 753 *p = (*p)->NextBus; 754 break; 755 } 756 757 if (b->FirstDev != NULL) { 758 if (devs_too) { 759 I2CDevPtr d; 760 761 while ((d = b->FirstDev) != NULL) { 762 b->FirstDev = d->NextDev; 763 xf86DestroyI2CDevRec(d, unalloc); 764 } 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; 771 } 772 } 773 } 774 775 xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" removed.\n", 776 b->BusName); 777 778 if (unalloc) xfree(b); 779 } 780} 781 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. 786 * 787 * At this point there won't be any traffic on the I2C bus. 788 */ 789 790Bool 791xf86I2CBusInit(I2CBusPtr b) 792{ 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. 796 */ 797 798 if (b->BusName == NULL || 799 xf86I2CFindBus(b->scrnIndex, b->BusName) != NULL) 800 return FALSE; 801 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. 806 */ 807 if (b->I2CWriteRead == NULL) 808 { 809 b->I2CWriteRead=I2CWriteRead; 810 811 if (b->I2CPutBits == NULL || 812 b->I2CGetBits == NULL) 813 { 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; 826 } 827 } 828 829 if (b->I2CUDelay == NULL) 830 b->I2CUDelay = I2CUDelay; 831 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; 837 838 /* Put new bus on list. */ 839 840 b->NextBus = I2CBusList; 841 I2CBusList = b; 842 843 xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" initialized.\n", 844 b->BusName); 845 846 return TRUE; 847} 848 849I2CBusPtr 850xf86I2CFindBus(int scrnIndex, char *name) 851{ 852 I2CBusPtr p; 853 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; 859 860 return NULL; 861} 862 863/* 864 * Return an array of I2CBusPtr's related to a screen. The caller is 865 * responsible for freeing the array. 866 */ 867int 868xf86I2CGetScreenBuses(int scrnIndex, I2CBusPtr **pppI2CBus) 869{ 870 I2CBusPtr pI2CBus; 871 int n = 0; 872 873 if (pppI2CBus) 874 *pppI2CBus = NULL; 875 876 for (pI2CBus = I2CBusList; pI2CBus; pI2CBus = pI2CBus->NextBus) { 877 if ((pI2CBus->scrnIndex >= 0) && (pI2CBus->scrnIndex != scrnIndex)) 878 continue; 879 880 n++; 881 882 if (!pppI2CBus) 883 continue; 884 885 *pppI2CBus = xnfrealloc(*pppI2CBus, n * sizeof(I2CBusPtr)); 886 *pppI2CBus[n - 1] = pI2CBus; 887 } 888 889 return n; 890} 891