| 517 | | } |
| 518 | | |
| 519 | | /* |
| 520 | | * query functions for other modules |
| 521 | | */ |
| 522 | | |
| 523 | | |
| 524 | | /* this function returns the handle for the first i2c_bus available, or |
| 525 | | -1 if no bus is present at all */ |
| 526 | | i2c_bus_handle i2c_get_first_adapter(void) |
| 527 | | { |
| 528 | | int i = 0; |
| 529 | | |
| 530 | | for (i = 0; i < I2C_ADAP_MAX; i++) |
| 531 | | if ( NULL != adapters[(int)i] ) |
| 532 | | return (i2c_bus_handle)i; |
| 533 | | |
| 534 | | return -1; |
| 535 | | } |
| 536 | | |
| 537 | | /* this function returns the handle for the next i2c_bus available, or |
| 538 | | NULL if there is not any other */ |
| 539 | | i2c_bus_handle i2c_get_next_adapter(i2c_bus_handle i) |
| 540 | | { |
| 541 | | int j = (int)i; |
| 542 | | |
| 543 | | j++; |
| 544 | | |
| 545 | | for(; j < I2C_ADAP_MAX; j++) |
| 546 | | if ( NULL != adapters[j] ) |
| 547 | | break; |
| 548 | | |
| 549 | | if ( I2C_ADAP_MAX >= j) |
| 550 | | return -1; |
| 551 | | |
| 552 | | return j; |
| 553 | | } |
| 554 | | |
| 555 | | /* this function returns the type of the i2c_bus 'i' */ |
| 556 | | int i2c_get_adapter_type(i2c_bus_handle i) |
| 557 | | { |
| 558 | | if ( i < 0 || i >= I2C_ADAP_MAX ) |
| 559 | | return -1; |
| 560 | | |
| 561 | | if ( NULL == adapters[(int)i] ) |
| 562 | | return -1; |
| 563 | | |
| 564 | | return adapters[(int)i]->id; |
| 565 | | } |
| 566 | | |
| 567 | | /* */ |
| 568 | | void* i2c_get_adapter_data(i2c_bus_handle i) |
| 569 | | { |
| 570 | | if ( i < 0 || i >= I2C_ADAP_MAX ) |
| 571 | | return NULL; |
| 572 | | |
| 573 | | if ( NULL == adapters[(int)i] ) |
| 574 | | return NULL; |
| 575 | | |
| 576 | | return adapters[(int)i]->data; |
| 577 | | } |
| 578 | | |
| 579 | | /* this function returns the number of available clients on the i2c_bus 'i' |
| 580 | | or -1 if the bus-handle is invalid */ |
| 581 | | int i2c_get_client_count(i2c_bus_handle i) |
| 582 | | { |
| 583 | | if ( i < 0 || i >= I2C_ADAP_MAX ) |
| 584 | | return -1; |
| 585 | | |
| 586 | | if ( NULL == adapters[(int)i] ) |
| 587 | | return -1; |
| 588 | | |
| 589 | | return adapters[(int)i]->client_count; |
| 590 | | } |
| 591 | | |
| 592 | | /* this function returns the type (client->id) for the client 'c' |
| 593 | | on i2c_bus 'i'. the function returns -1 if the i2c_bus-nr, -2 if the client is invalid |
| 594 | | |
| 595 | | this function is useful for checking for specific devices on an i2c-bus. simply loop |
| 596 | | through all available clients and see if they fit your needs */ |
| 597 | | int i2c_get_client_type(i2c_bus_handle i, int c) |
| 598 | | { |
| 599 | | if ( i < 0 || i >= I2C_ADAP_MAX ) |
| 600 | | return -1; |
| 601 | | |
| 602 | | if ( NULL == adapters[(int)i] ) |
| 603 | | return -1; |
| 604 | | |
| 605 | | if ( NULL == adapters[(int)i]->clients[c] ) |
| 606 | | return -2; |
| 607 | | |
| 608 | | return adapters[(int)i]->clients[c]->id; |
| 609 | | |
| 610 | | } |
| 611 | | |
| 612 | | /* this function sends the command 'cmd' to the client 'c' specified by its id on |
| 613 | | the i2c_bus 'i' supplying the argument 'arg'. |
| 614 | | it returns the return code of the specific function or -2 if the i2c_client could |
| 615 | | not be found on that bus */ |
| 616 | | int i2c_client_command(i2c_bus_handle i, int c, unsigned int cmd, void *arg) |
| 617 | | { |
| 618 | | int j = 0; |
| 619 | | |
| 620 | | if ( i < 0 || i >= I2C_ADAP_MAX ) |
| 621 | | return -1; |
| 622 | | |
| 623 | | if ( NULL == adapters[(int)i] ) |
| 624 | | return -1; |
| 625 | | |
| 626 | | for (j = 0; j < adapters[(int)i]->client_count; j++) { |
| 627 | | if( c == adapters[(int)i]->clients[j]->id ) |
| 628 | | return adapters[(int)i]->clients[j]->driver->command(adapters[(int)i]->clients[j],cmd,arg); |
| 629 | | } |
| 630 | | |
| 631 | | DEB2(printk("i2c_client_command: didn't find that device!\n")); |
| 632 | | |
| 633 | | printk("i2c_client_command: didn't find that device!\n"); |
| 634 | | |
| 635 | | return -2; |
| 636 | | } |
| 637 | | |
| 638 | | /* this function tells the client 'c' on the i2c_bus 'i' that it is used by another module */ |
| 639 | | int i2c_inc_client_use(i2c_bus_handle i, int c) |
| 640 | | { |
| 641 | | int j = 0; |
| 642 | | |
| 643 | | if ( i < 0 || i >= I2C_ADAP_MAX ) |
| 644 | | return -1; |
| 645 | | |
| 646 | | if ( NULL == adapters[(int)i] ) |
| 647 | | return -1; |
| 648 | | |
| 649 | | for (j = 0; j < adapters[(int)i]->client_count; j++) { |
| 650 | | if( c == adapters[(int)i]->clients[j]->id ) |
| 651 | | if ( NULL != adapters[(int)i]->clients[j]->driver->inc_use ){ |
| 652 | | adapters[(int)i]->clients[j]->driver->inc_use(adapters[(int)i]->clients[j]); |
| 653 | | return 0; |
| 654 | | } |
| 655 | | } |
| 656 | | |
| 657 | | printk("i2c_inc_client_use: didn't find that device!\n"); |
| 658 | | |
| 659 | | return -2; |
| 660 | | } |
| 661 | | |
| 662 | | /* this function tells the client 'c' on the i2c_bus 'i' that it is no longer uesd by a module */ |
| 663 | | int i2c_dec_client_use(i2c_bus_handle i, int c) |
| 664 | | { |
| 665 | | int j = 0; |
| 666 | | |
| 667 | | if ( i < 0 || i >= I2C_ADAP_MAX ) |
| 668 | | return -1; |
| 669 | | |
| 670 | | if ( NULL == adapters[(int)i] ) |
| 671 | | return -1; |
| 672 | | |
| 673 | | for (j = 0; j < adapters[(int)i]->client_count; j++) { |
| 674 | | if( c == adapters[(int)i]->clients[j]->id ) |
| 675 | | if ( NULL != adapters[(int)i]->clients[j]->driver->dec_use ) { |
| 676 | | adapters[(int)i]->clients[j]->driver->dec_use(adapters[(int)i]->clients[j]); |
| 677 | | return 0; |
| 678 | | } |
| 679 | | } |
| 680 | | |
| 681 | | printk("i2c_dec_use_client: didn't find that device!\n"); |
| 682 | | return -2; |