functionality 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. INTRODUCTION
  2. ------------
  3. Because not every I2C or SMBus adapter implements everything in the
  4. I2C specifications, a client can not trust that everything it needs
  5. is implemented when it is given the option to attach to an adapter:
  6. the client needs some way to check whether an adapter has the needed
  7. functionality.
  8. FUNCTIONALITY CONSTANTS
  9. -----------------------
  10. For the most up-to-date list of functionality constants, please check
  11. <linux/i2c.h>!
  12. I2C_FUNC_I2C Plain i2c-level commands (Pure SMBus
  13. adapters typically can not do these)
  14. I2C_FUNC_10BIT_ADDR Handles the 10-bit address extensions
  15. I2C_FUNC_PROTOCOL_MANGLING Knows about the I2C_M_IGNORE_NAK,
  16. I2C_M_REV_DIR_ADDR, I2C_M_NOSTART and
  17. I2C_M_NO_RD_ACK flags (which modify the
  18. I2C protocol!)
  19. I2C_FUNC_SMBUS_QUICK Handles the SMBus write_quick command
  20. I2C_FUNC_SMBUS_READ_BYTE Handles the SMBus read_byte command
  21. I2C_FUNC_SMBUS_WRITE_BYTE Handles the SMBus write_byte command
  22. I2C_FUNC_SMBUS_READ_BYTE_DATA Handles the SMBus read_byte_data command
  23. I2C_FUNC_SMBUS_WRITE_BYTE_DATA Handles the SMBus write_byte_data command
  24. I2C_FUNC_SMBUS_READ_WORD_DATA Handles the SMBus read_word_data command
  25. I2C_FUNC_SMBUS_WRITE_WORD_DATA Handles the SMBus write_byte_data command
  26. I2C_FUNC_SMBUS_PROC_CALL Handles the SMBus process_call command
  27. I2C_FUNC_SMBUS_READ_BLOCK_DATA Handles the SMBus read_block_data command
  28. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA Handles the SMBus write_block_data command
  29. I2C_FUNC_SMBUS_READ_I2C_BLOCK Handles the SMBus read_i2c_block_data command
  30. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK Handles the SMBus write_i2c_block_data command
  31. A few combinations of the above flags are also defined for your convenience:
  32. I2C_FUNC_SMBUS_BYTE Handles the SMBus read_byte
  33. and write_byte commands
  34. I2C_FUNC_SMBUS_BYTE_DATA Handles the SMBus read_byte_data
  35. and write_byte_data commands
  36. I2C_FUNC_SMBUS_WORD_DATA Handles the SMBus read_word_data
  37. and write_word_data commands
  38. I2C_FUNC_SMBUS_BLOCK_DATA Handles the SMBus read_block_data
  39. and write_block_data commands
  40. I2C_FUNC_SMBUS_I2C_BLOCK Handles the SMBus read_i2c_block_data
  41. and write_i2c_block_data commands
  42. I2C_FUNC_SMBUS_EMUL Handles all SMBus commands than can be
  43. emulated by a real I2C adapter (using
  44. the transparent emulation layer)
  45. ALGORITHM/ADAPTER IMPLEMENTATION
  46. --------------------------------
  47. When you write a new algorithm driver, you will have to implement a
  48. function callback `functionality', that gets an i2c_adapter structure
  49. pointer as its only parameter:
  50. struct i2c_algorithm {
  51. /* Many other things of course; check <linux/i2c.h>! */
  52. u32 (*functionality) (struct i2c_adapter *);
  53. }
  54. A typically implementation is given below, from i2c-algo-bit.c:
  55. static u32 bit_func(struct i2c_adapter *adap)
  56. {
  57. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
  58. I2C_FUNC_PROTOCOL_MANGLING;
  59. }
  60. CLIENT CHECKING
  61. ---------------
  62. Before a client tries to attach to an adapter, or even do tests to check
  63. whether one of the devices it supports is present on an adapter, it should
  64. check whether the needed functionality is present. There are two functions
  65. defined which should be used instead of calling the functionality hook
  66. in the algorithm structure directly:
  67. /* Return the functionality mask */
  68. extern u32 i2c_get_functionality (struct i2c_adapter *adap);
  69. /* Return 1 if adapter supports everything we need, 0 if not. */
  70. extern int i2c_check_functionality (struct i2c_adapter *adap, u32 func);
  71. This is a typical way to use these functions (from the writing-clients
  72. document):
  73. int foo_detect_client(struct i2c_adapter *adapter, int address,
  74. unsigned short flags, int kind)
  75. {
  76. /* Define needed variables */
  77. /* As the very first action, we check whether the adapter has the
  78. needed functionality: we need the SMBus read_word_data,
  79. write_word_data and write_byte functions in this example. */
  80. if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA |
  81. I2C_FUNC_SMBUS_WRITE_BYTE))
  82. goto ERROR0;
  83. /* Now we can do the real detection */
  84. ERROR0:
  85. /* Return an error */
  86. }
  87. CHECKING THROUGH /DEV
  88. ---------------------
  89. If you try to access an adapter from a userspace program, you will have
  90. to use the /dev interface. You will still have to check whether the
  91. functionality you need is supported, of course. This is done using
  92. the I2C_FUNCS ioctl. An example, adapted from the lm_sensors i2cdetect
  93. program, is below:
  94. int file;
  95. if (file = open("/dev/i2c-0",O_RDWR) < 0) {
  96. /* Some kind of error handling */
  97. exit(1);
  98. }
  99. if (ioctl(file,I2C_FUNCS,&funcs) < 0) {
  100. /* Some kind of error handling */
  101. exit(1);
  102. }
  103. if (! (funcs & I2C_FUNC_SMBUS_QUICK)) {
  104. /* Oops, the needed functionality (SMBus write_quick function) is
  105. not available! */
  106. exit(1);
  107. }
  108. /* Now it is safe to use the SMBus write_quick command */