€•¾|Œsphinx.addnodes”Œdocument”“”)”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)”}”(hhh]”(hŒ pending_xref”“”)”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…””}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ%/translations/zh_CN/i2c/dev-interface”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)”}”(hhh]”hŒChinese (Traditional)”…””}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/zh_TW/i2c/dev-interface”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒItalian”…””}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/it_IT/i2c/dev-interface”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒJapanese”…””}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/ja_JP/i2c/dev-interface”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒKorean”…””}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/ko_KR/i2c/dev-interface”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒPortuguese (Brazilian)”…””}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/pt_BR/i2c/dev-interface”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒSpanish”…””}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/sp_SP/i2c/dev-interface”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒsection”“”)”}”(hhh]”(hŒtitle”“”)”}”(hŒ,Implementing I2C device drivers in userspace”h]”hŒ,Implementing I2C device drivers in userspace”…””}”(hh¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhh·h²hh³Œ?/var/lib/git/docbuild/linux/Documentation/i2c/dev-interface.rst”h´KubhŒ paragraph”“”)”}”(hŒÉUsually, I2C devices are controlled by a kernel driver. But it is also possible to access all devices on an adapter from userspace, through the /dev interface. You need to load module i2c-dev for this.”h]”hŒÉUsually, I2C devices are controlled by a kernel driver. But it is also possible to access all devices on an adapter from userspace, through the /dev interface. You need to load module i2c-dev for this.”…””}”(hhÍh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khh·h²hubhÌ)”}”(hX=Each registered I2C adapter gets a number, counting from 0. You can examine /sys/class/i2c-dev/ to see what number corresponds to which adapter. Alternatively, you can run "i2cdetect -l" to obtain a formatted list of all I2C adapters present on your system at a given time. i2cdetect is part of the i2c-tools package.”h]”hXAEach registered I2C adapter gets a number, counting from 0. You can examine /sys/class/i2c-dev/ to see what number corresponds to which adapter. Alternatively, you can run “i2cdetect -l†to obtain a formatted list of all I2C adapters present on your system at a given time. i2cdetect is part of the i2c-tools package.”…””}”(hhÛh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K hh·h²hubhÌ)”}”(hXI2C device files are character device files with major device number 89 and a minor device number corresponding to the number assigned as explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., i2c-10, ...). All 256 minor device numbers are reserved for I2C.”h]”hXI2C device files are character device files with major device number 89 and a minor device number corresponding to the number assigned as explained above. They should be called “i2c-%d†(i2c-0, i2c-1, ..., i2c-10, ...). All 256 minor device numbers are reserved for I2C.”…””}”(hhéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khh·h²hubh¶)”}”(hhh]”(h»)”}”(hŒ C example”h]”hŒ C example”…””}”(hhúh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhh÷h²hh³hÊh´KubhÌ)”}”(hŒoSo let's say you want to access an I2C adapter from a C program. First, you need to include these two headers::”h]”hŒpSo let’s say you want to access an I2C adapter from a C program. First, you need to include these two headers:”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khh÷h²hubhŒ literal_block”“”)”}”(hŒ1#include #include ”h]”hŒ1#include #include ”…””}”hjsbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1jh³hÊh´Khh÷h²hubhÌ)”}”(hXNow, you have to decide which adapter you want to access. You should inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this. Adapter numbers are assigned somewhat dynamically, so you can not assume much about them. They can even change from one boot to the next.”h]”hXNow, you have to decide which adapter you want to access. You should inspect /sys/class/i2c-dev/ or run “i2cdetect -l†to decide this. Adapter numbers are assigned somewhat dynamically, so you can not assume much about them. They can even change from one boot to the next.”…””}”(hj(h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khh÷h²hubhÌ)”}”(hŒ.Next thing, open the device file, as follows::”h]”hŒ-Next thing, open the device file, as follows:”…””}”(hj6h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K#hh÷h²hubj)”}”(hX int file; int adapter_nr = 2; /* probably dynamically determined */ char filename[20]; snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); file = open(filename, O_RDWR); if (file < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); }”h]”hX int file; int adapter_nr = 2; /* probably dynamically determined */ char filename[20]; snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); file = open(filename, O_RDWR); if (file < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); }”…””}”hjDsbah}”(h]”h ]”h"]”h$]”h&]”j&j'uh1jh³hÊh´K%hh÷h²hubhÌ)”}”(hŒdWhen you have opened the device, you must specify with what device address you want to communicate::”h]”hŒcWhen you have opened the device, you must specify with what device address you want to communicate:”…””}”(hjRh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K0hh÷h²hubj)”}”(hŒŸint addr = 0x40; /* The I2C address */ if (ioctl(file, I2C_SLAVE, addr) < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); }”h]”hŒŸint addr = 0x40; /* The I2C address */ if (ioctl(file, I2C_SLAVE, addr) < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); }”…””}”hj`sbah}”(h]”h ]”h"]”h$]”h&]”j&j'uh1jh³hÊh´K3hh÷h²hubhÌ)”}”(hŒÁWell, you are all set up now. You can now use SMBus commands or plain I2C to communicate with your device. SMBus commands are preferred if the device supports them. Both are illustrated below::”h]”hŒÀWell, you are all set up now. You can now use SMBus commands or plain I2C to communicate with your device. SMBus commands are preferred if the device supports them. Both are illustrated below:”…””}”(hjnh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K:hh÷h²hubj)”}”(hX‘__u8 reg = 0x10; /* Device register to access */ __s32 res; char buf[10]; /* Using SMBus commands */ res = i2c_smbus_read_word_data(file, reg); if (res < 0) { /* ERROR HANDLING: I2C transaction failed */ } else { /* res contains the read word */ } /* * Using I2C Write, equivalent of * i2c_smbus_write_word_data(file, reg, 0x6543) */ buf[0] = reg; buf[1] = 0x43; buf[2] = 0x65; if (write(file, buf, 3) != 3) { /* ERROR HANDLING: I2C transaction failed */ } /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ if (read(file, buf, 1) != 1) { /* ERROR HANDLING: I2C transaction failed */ } else { /* buf[0] contains the read byte */ }”h]”hX‘__u8 reg = 0x10; /* Device register to access */ __s32 res; char buf[10]; /* Using SMBus commands */ res = i2c_smbus_read_word_data(file, reg); if (res < 0) { /* ERROR HANDLING: I2C transaction failed */ } else { /* res contains the read word */ } /* * Using I2C Write, equivalent of * i2c_smbus_write_word_data(file, reg, 0x6543) */ buf[0] = reg; buf[1] = 0x43; buf[2] = 0x65; if (write(file, buf, 3) != 3) { /* ERROR HANDLING: I2C transaction failed */ } /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ if (read(file, buf, 1) != 1) { /* ERROR HANDLING: I2C transaction failed */ } else { /* buf[0] contains the read byte */ }”…””}”hj|sbah}”(h]”h ]”h"]”h$]”h&]”j&j'uh1jh³hÊh´K>hh÷h²hubhÌ)”}”(hX7Note that only a subset of the I2C and SMBus protocols can be achieved by the means of read() and write() calls. In particular, so-called combined transactions (mixing read and write messages in the same transaction) aren't supported. For this reason, this interface is almost never used by user-space programs.”h]”hX9Note that only a subset of the I2C and SMBus protocols can be achieved by the means of read() and write() calls. In particular, so-called combined transactions (mixing read and write messages in the same transaction) aren’t supported. For this reason, this interface is almost never used by user-space programs.”…””}”(hjŠh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K\hh÷h²hubhÌ)”}”(hŒzIMPORTANT: because of the use of inline functions, you *have* to use '-O' or some variation when you compile your program!”h]”(hŒ7IMPORTANT: because of the use of inline functions, you ”…””}”(hj˜h²hh³Nh´NubhŒemphasis”“”)”}”(hŒ*have*”h]”hŒhave”…””}”(hj¢h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj˜ubhŒA to use ‘-O’ or some variation when you compile your program!”…””}”(hj˜h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kbhh÷h²hubeh}”(h]”Œ c-example”ah ]”h"]”Œ c example”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kubh¶)”}”(hhh]”(h»)”}”(hŒFull interface description”h]”hŒFull interface description”…””}”(hjÅh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjÂh²hh³hÊh´KgubhÌ)”}”(hŒ!The following IOCTLs are defined:”h]”hŒ!The following IOCTLs are defined:”…””}”(hjÓh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KihjÂh²hubhŒdefinition_list”“”)”}”(hhh]”(hŒdefinition_list_item”“”)”}”(hŒ¿``ioctl(file, I2C_SLAVE, long addr)`` Change slave address. The address is passed in the 7 lower bits of the argument (except for 10 bit addresses, passed in the 10 lower bits in this case). ”h]”(hŒterm”“”)”}”(hŒ%``ioctl(file, I2C_SLAVE, long addr)``”h]”hŒliteral”“”)”}”(hjðh]”hŒ!ioctl(file, I2C_SLAVE, long addr)”…””}”(hjôh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhjîubah}”(h]”h ]”h"]”h$]”h&]”uh1jìh³hÊh´KnhjèubhŒ definition”“”)”}”(hhh]”hÌ)”}”(hŒ˜Change slave address. The address is passed in the 7 lower bits of the argument (except for 10 bit addresses, passed in the 10 lower bits in this case).”h]”hŒ˜Change slave address. The address is passed in the 7 lower bits of the argument (except for 10 bit addresses, passed in the 10 lower bits in this case).”…””}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Klhj ubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjèubeh}”(h]”h ]”h"]”h$]”h&]”uh1jæh³hÊh´Knhjãubjç)”}”(hŒÝ``ioctl(file, I2C_TENBIT, long select)`` Selects ten bit addresses if select not equals 0, selects normal 7 bit addresses if select equals 0. Default 0. This request is only valid if the adapter has I2C_FUNC_10BIT_ADDR. ”h]”(jí)”}”(hŒ(``ioctl(file, I2C_TENBIT, long select)``”h]”jó)”}”(hj,h]”hŒ$ioctl(file, I2C_TENBIT, long select)”…””}”(hj.h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhj*ubah}”(h]”h ]”h"]”h$]”h&]”uh1jìh³hÊh´Kshj&ubj)”}”(hhh]”hÌ)”}”(hŒ³Selects ten bit addresses if select not equals 0, selects normal 7 bit addresses if select equals 0. Default 0. This request is only valid if the adapter has I2C_FUNC_10BIT_ADDR.”h]”hŒ³Selects ten bit addresses if select not equals 0, selects normal 7 bit addresses if select equals 0. Default 0. This request is only valid if the adapter has I2C_FUNC_10BIT_ADDR.”…””}”(hjDh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KqhjAubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj&ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jæh³hÊh´Kshjãh²hubjç)”}”(hXT``ioctl(file, I2C_PEC, long select)`` Selects SMBus PEC (packet error checking) generation and verification if select not equals 0, disables if select equals 0. Default 0. Used only for SMBus transactions. This request only has an effect if the the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just doesn't have any effect. ”h]”(jí)”}”(hŒ%``ioctl(file, I2C_PEC, long select)``”h]”jó)”}”(hjdh]”hŒ!ioctl(file, I2C_PEC, long select)”…””}”(hjfh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhjbubah}”(h]”h ]”h"]”h$]”h&]”uh1jìh³hÊh´Kzhj^ubj)”}”(hhh]”hÌ)”}”(hX-Selects SMBus PEC (packet error checking) generation and verification if select not equals 0, disables if select equals 0. Default 0. Used only for SMBus transactions. This request only has an effect if the the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just doesn't have any effect.”h]”hX/Selects SMBus PEC (packet error checking) generation and verification if select not equals 0, disables if select equals 0. Default 0. Used only for SMBus transactions. This request only has an effect if the the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just doesn’t have any effect.”…””}”(hj|h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kvhjyubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj^ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jæh³hÊh´Kzhjãh²hubjç)”}”(hŒk``ioctl(file, I2C_FUNCS, unsigned long *funcs)`` Gets the adapter functionality and puts it in ``*funcs``. ”h]”(jí)”}”(hŒ0``ioctl(file, I2C_FUNCS, unsigned long *funcs)``”h]”jó)”}”(hjœh]”hŒ,ioctl(file, I2C_FUNCS, unsigned long *funcs)”…””}”(hjžh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhjšubah}”(h]”h ]”h"]”h$]”h&]”uh1jìh³hÊh´K}hj–ubj)”}”(hhh]”hÌ)”}”(hŒ9Gets the adapter functionality and puts it in ``*funcs``.”h]”(hŒ.Gets the adapter functionality and puts it in ”…””}”(hj´h²hh³Nh´Nubjó)”}”(hŒ ``*funcs``”h]”hŒ*funcs”…””}”(hj¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhj´ubhŒ.”…””}”(hj´h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K}hj±ubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj–ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jæh³hÊh´K}hjãh²hubjç)”}”(hXÆ``ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)`` Do combined read/write transaction without stop in between. Only valid if the adapter has I2C_FUNC_I2C. The argument is a pointer to a:: struct i2c_rdwr_ioctl_data { struct i2c_msg *msgs; /* ptr to array of simple messages */ int nmsgs; /* number of messages to exchange */ } The msgs[] themselves contain further pointers into data buffers. The function will write or read data to or from that buffers depending on whether the I2C_M_RD flag is set in a particular message or not. The slave address and whether to use ten bit address mode has to be set in each message, overriding the values set with the above ioctl's. ”h]”(jí)”}”(hŒ=``ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)``”h]”jó)”}”(hjæh]”hŒ9ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)”…””}”(hjèh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhjäubah}”(h]”h ]”h"]”h$]”h&]”uh1jìh³hÊh´Khjàubj)”}”(hhh]”(hÌ)”}”(hŒ‰Do combined read/write transaction without stop in between. Only valid if the adapter has I2C_FUNC_I2C. The argument is a pointer to a::”h]”hŒˆDo combined read/write transaction without stop in between. Only valid if the adapter has I2C_FUNC_I2C. The argument is a pointer to a:”…””}”(hjþh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K€hjûubj)”}”(hŒ›struct i2c_rdwr_ioctl_data { struct i2c_msg *msgs; /* ptr to array of simple messages */ int nmsgs; /* number of messages to exchange */ }”h]”hŒ›struct i2c_rdwr_ioctl_data { struct i2c_msg *msgs; /* ptr to array of simple messages */ int nmsgs; /* number of messages to exchange */ }”…””}”hj sbah}”(h]”h ]”h"]”h$]”h&]”j&j'uh1jh³hÊh´K„hjûubhÌ)”}”(hXWThe msgs[] themselves contain further pointers into data buffers. The function will write or read data to or from that buffers depending on whether the I2C_M_RD flag is set in a particular message or not. The slave address and whether to use ten bit address mode has to be set in each message, overriding the values set with the above ioctl's.”h]”hXYThe msgs[] themselves contain further pointers into data buffers. The function will write or read data to or from that buffers depending on whether the I2C_M_RD flag is set in a particular message or not. The slave address and whether to use ten bit address mode has to be set in each message, overriding the values set with the above ioctl’s.”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K‰hjûubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjàubeh}”(h]”h ]”h"]”h$]”h&]”uh1jæh³hÊh´Khjãh²hubjç)”}”(hŒ¦``ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)`` If possible, use the provided ``i2c_smbus_*`` methods described below instead of issuing direct ioctls. ”h]”(jí)”}”(hŒ=``ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)``”h]”jó)”}”(hj:h]”hŒ9ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)”…””}”(hj<h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhj8ubah}”(h]”h ]”h"]”h$]”h&]”uh1jìh³hÊh´K‘hj4ubj)”}”(hhh]”hÌ)”}”(hŒgIf possible, use the provided ``i2c_smbus_*`` methods described below instead of issuing direct ioctls.”h]”(hŒIf possible, use the provided ”…””}”(hjRh²hh³Nh´Nubjó)”}”(hŒ``i2c_smbus_*``”h]”hŒ i2c_smbus_*”…””}”(hjZh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jòhjRubhŒ: methods described below instead of issuing direct ioctls.”…””}”(hjRh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KhjOubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj4ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jæh³hÊh´K‘hjãh²hubeh}”(h]”h ]”h"]”h$]”h&]”uh1jáhjÂh²hh³hÊh´NubhÌ)”}”(hŒ½You can do plain I2C transactions by using read(2) and write(2) calls. You do not need to pass the address byte; instead, set it through ioctl I2C_SLAVE before you try to access the device.”h]”hŒ½You can do plain I2C transactions by using read(2) and write(2) calls. You do not need to pass the address byte; instead, set it through ioctl I2C_SLAVE before you try to access the device.”…””}”(hj„h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K“hjÂh²hubhÌ)”}”(hŒ}You can do SMBus level transactions (see documentation file smbus-protocol.rst for details) through the following functions::”h]”hŒ|You can do SMBus level transactions (see documentation file smbus-protocol.rst for details) through the following functions:”…””}”(hj’h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K—hjÂh²hubj)”}”(hXþ__s32 i2c_smbus_write_quick(int file, __u8 value); __s32 i2c_smbus_read_byte(int file); __s32 i2c_smbus_write_byte(int file, __u8 value); __s32 i2c_smbus_read_byte_data(int file, __u8 command); __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); __s32 i2c_smbus_read_word_data(int file, __u8 command); __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values); __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, __u8 *values);”h]”hXþ__s32 i2c_smbus_write_quick(int file, __u8 value); __s32 i2c_smbus_read_byte(int file); __s32 i2c_smbus_write_byte(int file, __u8 value); __s32 i2c_smbus_read_byte_data(int file, __u8 command); __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); __s32 i2c_smbus_read_word_data(int file, __u8 command); __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values); __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, __u8 *values);”…””}”hj sbah}”(h]”h ]”h"]”h$]”h&]”j&j'uh1jh³hÊh´KšhjÂh²hubhÌ)”}”(hX&All these transactions return -1 on failure; you can read errno to see what happened. The 'write' transactions return 0 on success; the 'read' transactions return the read value, except for read_block, which returns the number of values read. The block buffers need not be longer than 32 bytes.”h]”hX.All these transactions return -1 on failure; you can read errno to see what happened. The ‘write’ transactions return 0 on success; the ‘read’ transactions return the read value, except for read_block, which returns the number of values read. The block buffers need not be longer than 32 bytes.”…””}”(hj®h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K¨hjÂh²hubhÌ)”}”(hŒ¾The above functions are made available by linking against the libi2c library, which is provided by the i2c-tools project. See: https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/.”h]”(hŒ€The above functions are made available by linking against the libi2c library, which is provided by the i2c-tools project. See: ”…””}”(hj¼h²hh³Nh´NubhŒ reference”“”)”}”(hŒ=https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/”h]”hŒ=https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/”…””}”(hjÆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”jÈuh1jÄhj¼ubhŒ.”…””}”(hj¼h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K®hjÂh²hubeh}”(h]”Œfull-interface-description”ah ]”h"]”Œfull interface description”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kgubh¶)”}”(hhh]”(h»)”}”(hŒImplementation details”h]”hŒImplementation details”…””}”(hjêh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjçh²hh³hÊh´K´ubhÌ)”}”(hŒpFor the interested, here's the code flow which happens inside the kernel when you use the /dev interface to I2C:”h]”hŒrFor the interested, here’s the code flow which happens inside the kernel when you use the /dev interface to I2C:”…””}”(hjøh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K¶hjçh²hubhŒenumerated_list”“”)”}”(hhh]”(hŒ list_item”“”)”}”(hŒbYour program opens /dev/i2c-N and calls ioctl() on it, as described in section "C example" above. ”h]”hÌ)”}”(hŒaYour program opens /dev/i2c-N and calls ioctl() on it, as described in section "C example" above.”h]”hŒeYour program opens /dev/i2c-N and calls ioctl() on it, as described in section “C example†above.”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K¹hj ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÊh´Nubj )”}”(hŒïThese open() and ioctl() calls are handled by the i2c-dev kernel driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(), respectively. You can think of i2c-dev as a generic I2C chip driver that can be programmed from user-space. ”h]”hÌ)”}”(hŒîThese open() and ioctl() calls are handled by the i2c-dev kernel driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(), respectively. You can think of i2c-dev as a generic I2C chip driver that can be programmed from user-space.”h]”hŒîThese open() and ioctl() calls are handled by the i2c-dev kernel driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(), respectively. You can think of i2c-dev as a generic I2C chip driver that can be programmed from user-space.”…””}”(hj)h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K¼hj%ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÊh´Nubj )”}”(hŒðSome ioctl() calls are for administrative tasks and are handled by i2c-dev directly. Examples include I2C_SLAVE (set the address of the device you want to access) and I2C_PEC (enable or disable SMBus error checking on future transactions.) ”h]”hÌ)”}”(hŒïSome ioctl() calls are for administrative tasks and are handled by i2c-dev directly. Examples include I2C_SLAVE (set the address of the device you want to access) and I2C_PEC (enable or disable SMBus error checking on future transactions.)”h]”hŒïSome ioctl() calls are for administrative tasks and are handled by i2c-dev directly. Examples include I2C_SLAVE (set the address of the device you want to access) and I2C_PEC (enable or disable SMBus error checking on future transactions.)”…””}”(hjAh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KÁhj=ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÊh´Nubj )”}”(hXŸOther ioctl() calls are converted to in-kernel function calls by i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer(). The i2c-dev driver is responsible for checking all the parameters that come from user-space for validity. After this point, there is no difference between these calls that came from user-space through i2c-dev and calls that would have been performed by kernel I2C chip drivers directly. This means that I2C bus drivers don't need to implement anything special to support access from user-space. ”h]”(hÌ)”}”(hXOther ioctl() calls are converted to in-kernel function calls by i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer().”h]”hXOther ioctl() calls are converted to in-kernel function calls by i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer().”…””}”(hjYh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KÆhjUubhÌ)”}”(hXŠThe i2c-dev driver is responsible for checking all the parameters that come from user-space for validity. After this point, there is no difference between these calls that came from user-space through i2c-dev and calls that would have been performed by kernel I2C chip drivers directly. This means that I2C bus drivers don't need to implement anything special to support access from user-space.”h]”hXŒThe i2c-dev driver is responsible for checking all the parameters that come from user-space for validity. After this point, there is no difference between these calls that came from user-space through i2c-dev and calls that would have been performed by kernel I2C chip drivers directly. This means that I2C bus drivers don’t need to implement anything special to support access from user-space.”…””}”(hjgh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KËhjUubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÊh´Nubj )”}”(hXÀThese i2c.h functions are wrappers to the actual implementation of your I2C bus driver. Each adapter must declare callback functions implementing these standard calls. i2c.h:i2c_get_functionality() calls i2c_adapter.algo->functionality(), while i2c-core-smbus.c:i2c_smbus_xfer() calls either adapter.algo->smbus_xfer() if it is implemented, or if not, i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls i2c_adapter.algo->master_xfer(). ”h]”hÌ)”}”(hX¿These i2c.h functions are wrappers to the actual implementation of your I2C bus driver. Each adapter must declare callback functions implementing these standard calls. i2c.h:i2c_get_functionality() calls i2c_adapter.algo->functionality(), while i2c-core-smbus.c:i2c_smbus_xfer() calls either adapter.algo->smbus_xfer() if it is implemented, or if not, i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls i2c_adapter.algo->master_xfer().”h]”hX¿These i2c.h functions are wrappers to the actual implementation of your I2C bus driver. Each adapter must declare callback functions implementing these standard calls. i2c.h:i2c_get_functionality() calls i2c_adapter.algo->functionality(), while i2c-core-smbus.c:i2c_smbus_xfer() calls either adapter.algo->smbus_xfer() if it is implemented, or if not, i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls i2c_adapter.algo->master_xfer().”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KÒhj{ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œenumtype”Œarabic”Œprefix”hŒsuffix”Œ)”uh1jhjçh²hh³hÊh´K¹ubhÌ)”}”(hŒÓAfter your I2C bus driver has processed these requests, execution runs up the call chain, with almost no processing done, except by i2c-dev to package the returned data, if any, in suitable format for the ioctl.”h]”hŒÓAfter your I2C bus driver has processed these requests, execution runs up the call chain, with almost no processing done, except by i2c-dev to package the returned data, if any, in suitable format for the ioctl.”…””}”(hjžh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KÛhjçh²hubeh}”(h]”Œimplementation-details”ah ]”h"]”Œimplementation details”ah$]”h&]”uh1hµhh·h²hh³hÊh´K´ubeh}”(h]”Œ,implementing-i2c-device-drivers-in-userspace”ah ]”h"]”Œ,implementing i2c device drivers in userspace”ah$]”h&]”uh1hµhhh²hh³hÊh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÊuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)”}”(hºNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”jߌerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÊŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”Œnameids”}”(j¹j¶j¿j¼jäjáj±j®uŒ nametypes”}”(j¹‰j¿‰jä‰j±‰uh}”(j¶h·j¼h÷jájÂj®jçuŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.