writing-clients 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. This is a small guide for those who want to write kernel drivers for I2C
  2. or SMBus devices.
  3. To set up a driver, you need to do several things. Some are optional, and
  4. some things can be done slightly or completely different. Use this as a
  5. guide, not as a rule book!
  6. General remarks
  7. ===============
  8. Try to keep the kernel namespace as clean as possible. The best way to
  9. do this is to use a unique prefix for all global symbols. This is
  10. especially important for exported symbols, but it is a good idea to do
  11. it for non-exported symbols too. We will use the prefix `foo_' in this
  12. tutorial, and `FOO_' for preprocessor variables.
  13. The driver structure
  14. ====================
  15. Usually, you will implement a single driver structure, and instantiate
  16. all clients from it. Remember, a driver structure contains general access
  17. routines, a client structure specific information like the actual I2C
  18. address.
  19. static struct i2c_driver foo_driver = {
  20. .owner = THIS_MODULE,
  21. .name = "Foo version 2.3 driver",
  22. .flags = I2C_DF_NOTIFY,
  23. .attach_adapter = &foo_attach_adapter,
  24. .detach_client = &foo_detach_client,
  25. .command = &foo_command /* may be NULL */
  26. }
  27. The name can be chosen freely, and may be upto 40 characters long. Please
  28. use something descriptive here.
  29. Don't worry about the flags field; just put I2C_DF_NOTIFY into it. This
  30. means that your driver will be notified when new adapters are found.
  31. This is almost always what you want.
  32. All other fields are for call-back functions which will be explained
  33. below.
  34. There use to be two additional fields in this structure, inc_use et dec_use,
  35. for module usage count, but these fields were obsoleted and removed.
  36. Extra client data
  37. =================
  38. The client structure has a special `data' field that can point to any
  39. structure at all. You can use this to keep client-specific data. You
  40. do not always need this, but especially for `sensors' drivers, it can
  41. be very useful.
  42. An example structure is below.
  43. struct foo_data {
  44. struct semaphore lock; /* For ISA access in `sensors' drivers. */
  45. int sysctl_id; /* To keep the /proc directory entry for
  46. `sensors' drivers. */
  47. enum chips type; /* To keep the chips type for `sensors' drivers. */
  48. /* Because the i2c bus is slow, it is often useful to cache the read
  49. information of a chip for some time (for example, 1 or 2 seconds).
  50. It depends of course on the device whether this is really worthwhile
  51. or even sensible. */
  52. struct semaphore update_lock; /* When we are reading lots of information,
  53. another process should not update the
  54. below information */
  55. char valid; /* != 0 if the following fields are valid. */
  56. unsigned long last_updated; /* In jiffies */
  57. /* Add the read information here too */
  58. };
  59. Accessing the client
  60. ====================
  61. Let's say we have a valid client structure. At some time, we will need
  62. to gather information from the client, or write new information to the
  63. client. How we will export this information to user-space is less
  64. important at this moment (perhaps we do not need to do this at all for
  65. some obscure clients). But we need generic reading and writing routines.
  66. I have found it useful to define foo_read and foo_write function for this.
  67. For some cases, it will be easier to call the i2c functions directly,
  68. but many chips have some kind of register-value idea that can easily
  69. be encapsulated. Also, some chips have both ISA and I2C interfaces, and
  70. it useful to abstract from this (only for `sensors' drivers).
  71. The below functions are simple examples, and should not be copied
  72. literally.
  73. int foo_read_value(struct i2c_client *client, u8 reg)
  74. {
  75. if (reg < 0x10) /* byte-sized register */
  76. return i2c_smbus_read_byte_data(client,reg);
  77. else /* word-sized register */
  78. return i2c_smbus_read_word_data(client,reg);
  79. }
  80. int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
  81. {
  82. if (reg == 0x10) /* Impossible to write - driver error! */ {
  83. return -1;
  84. else if (reg < 0x10) /* byte-sized register */
  85. return i2c_smbus_write_byte_data(client,reg,value);
  86. else /* word-sized register */
  87. return i2c_smbus_write_word_data(client,reg,value);
  88. }
  89. For sensors code, you may have to cope with ISA registers too. Something
  90. like the below often works. Note the locking!
  91. int foo_read_value(struct i2c_client *client, u8 reg)
  92. {
  93. int res;
  94. if (i2c_is_isa_client(client)) {
  95. down(&(((struct foo_data *) (client->data)) -> lock));
  96. outb_p(reg,client->addr + FOO_ADDR_REG_OFFSET);
  97. res = inb_p(client->addr + FOO_DATA_REG_OFFSET);
  98. up(&(((struct foo_data *) (client->data)) -> lock));
  99. return res;
  100. } else
  101. return i2c_smbus_read_byte_data(client,reg);
  102. }
  103. Writing is done the same way.
  104. Probing and attaching
  105. =====================
  106. Most i2c devices can be present on several i2c addresses; for some this
  107. is determined in hardware (by soldering some chip pins to Vcc or Ground),
  108. for others this can be changed in software (by writing to specific client
  109. registers). Some devices are usually on a specific address, but not always;
  110. and some are even more tricky. So you will probably need to scan several
  111. i2c addresses for your clients, and do some sort of detection to see
  112. whether it is actually a device supported by your driver.
  113. To give the user a maximum of possibilities, some default module parameters
  114. are defined to help determine what addresses are scanned. Several macros
  115. are defined in i2c.h to help you support them, as well as a generic
  116. detection algorithm.
  117. You do not have to use this parameter interface; but don't try to use
  118. function i2c_probe() if you don't.
  119. NOTE: If you want to write a `sensors' driver, the interface is slightly
  120. different! See below.
  121. Probing classes (i2c)
  122. ---------------------
  123. All parameters are given as lists of unsigned 16-bit integers. Lists are
  124. terminated by I2C_CLIENT_END.
  125. The following lists are used internally:
  126. normal_i2c: filled in by the module writer.
  127. A list of I2C addresses which should normally be examined.
  128. probe: insmod parameter.
  129. A list of pairs. The first value is a bus number (-1 for any I2C bus),
  130. the second is the address. These addresses are also probed, as if they
  131. were in the 'normal' list.
  132. ignore: insmod parameter.
  133. A list of pairs. The first value is a bus number (-1 for any I2C bus),
  134. the second is the I2C address. These addresses are never probed.
  135. This parameter overrules 'normal' and 'probe', but not the 'force' lists.
  136. force: insmod parameter.
  137. A list of pairs. The first value is a bus number (-1 for any I2C bus),
  138. the second is the I2C address. A device is blindly assumed to be on
  139. the given address, no probing is done.
  140. Fortunately, as a module writer, you just have to define the `normal_i2c'
  141. parameter. The complete declaration could look like this:
  142. /* Scan 0x37, and 0x48 to 0x4f */
  143. static unsigned short normal_i2c[] = { 0x37, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  144. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  145. /* Magic definition of all other variables and things */
  146. I2C_CLIENT_INSMOD;
  147. Note that you *have* to call the defined variable `normal_i2c',
  148. without any prefix!
  149. Probing classes (sensors)
  150. -------------------------
  151. If you write a `sensors' driver, you use a slightly different interface.
  152. Also, we use a enum of chip types. Don't forget to include `sensors.h'.
  153. The following lists are used internally. They are all lists of integers.
  154. normal_i2c: filled in by the module writer. Terminated by I2C_CLIENT_END.
  155. A list of I2C addresses which should normally be examined.
  156. probe: insmod parameter. Initialize this list with I2C_CLIENT_END values.
  157. A list of pairs. The first value is a bus number (ANY_I2C_BUS for any
  158. I2C bus), the second is the address. These addresses are also probed,
  159. as if they were in the 'normal' list.
  160. ignore: insmod parameter. Initialize this list with I2C_CLIENT_END values.
  161. A list of pairs. The first value is a bus number (ANY_I2C_BUS for any
  162. I2C bus), the second is the I2C address. These addresses are never
  163. probed. This parameter overrules 'normal' and 'probe', but not the
  164. 'force' lists.
  165. Also used is a list of pointers to sensors_force_data structures:
  166. force_data: insmod parameters. A list, ending with an element of which
  167. the force field is NULL.
  168. Each element contains the type of chip and a list of pairs.
  169. The first value is a bus number (ANY_I2C_BUS for any I2C bus), the
  170. second is the address.
  171. These are automatically translated to insmod variables of the form
  172. force_foo.
  173. So we have a generic insmod variabled `force', and chip-specific variables
  174. `force_CHIPNAME'.
  175. Fortunately, as a module writer, you just have to define the `normal_i2c'
  176. parameter, and define what chip names are used. The complete declaration
  177. could look like this:
  178. /* Scan i2c addresses 0x37, and 0x48 to 0x4f */
  179. static unsigned short normal_i2c[] = { 0x37, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  180. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  181. /* Define chips foo and bar, as well as all module parameters and things */
  182. SENSORS_INSMOD_2(foo,bar);
  183. If you have one chip, you use macro SENSORS_INSMOD_1(chip), if you have 2
  184. you use macro SENSORS_INSMOD_2(chip1,chip2), etc. If you do not want to
  185. bother with chip types, you can use SENSORS_INSMOD_0.
  186. A enum is automatically defined as follows:
  187. enum chips { any_chip, chip1, chip2, ... }
  188. Attaching to an adapter
  189. -----------------------
  190. Whenever a new adapter is inserted, or for all adapters if the driver is
  191. being registered, the callback attach_adapter() is called. Now is the
  192. time to determine what devices are present on the adapter, and to register
  193. a client for each of them.
  194. The attach_adapter callback is really easy: we just call the generic
  195. detection function. This function will scan the bus for us, using the
  196. information as defined in the lists explained above. If a device is
  197. detected at a specific address, another callback is called.
  198. int foo_attach_adapter(struct i2c_adapter *adapter)
  199. {
  200. return i2c_probe(adapter,&addr_data,&foo_detect_client);
  201. }
  202. Remember, structure `addr_data' is defined by the macros explained above,
  203. so you do not have to define it yourself.
  204. The i2c_probe function will call the foo_detect_client
  205. function only for those i2c addresses that actually have a device on
  206. them (unless a `force' parameter was used). In addition, addresses that
  207. are already in use (by some other registered client) are skipped.
  208. The detect client function
  209. --------------------------
  210. The detect client function is called by i2c_probe. The `kind' parameter
  211. contains -1 for a probed detection, 0 for a forced detection, or a positive
  212. number for a forced detection with a chip type forced.
  213. Below, some things are only needed if this is a `sensors' driver. Those
  214. parts are between /* SENSORS ONLY START */ and /* SENSORS ONLY END */
  215. markers.
  216. This function should only return an error (any value != 0) if there is
  217. some reason why no more detection should be done anymore. If the
  218. detection just fails for this address, return 0.
  219. For now, you can ignore the `flags' parameter. It is there for future use.
  220. int foo_detect_client(struct i2c_adapter *adapter, int address,
  221. unsigned short flags, int kind)
  222. {
  223. int err = 0;
  224. int i;
  225. struct i2c_client *new_client;
  226. struct foo_data *data;
  227. const char *client_name = ""; /* For non-`sensors' drivers, put the real
  228. name here! */
  229. /* Let's see whether this adapter can support what we need.
  230. Please substitute the things you need here!
  231. For `sensors' drivers, add `! is_isa &&' to the if statement */
  232. if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA |
  233. I2C_FUNC_SMBUS_WRITE_BYTE))
  234. goto ERROR0;
  235. /* SENSORS ONLY START */
  236. const char *type_name = "";
  237. int is_isa = i2c_is_isa_adapter(adapter);
  238. /* Do this only if the chip can additionally be found on the ISA bus
  239. (hybrid chip). */
  240. if (is_isa) {
  241. /* Discard immediately if this ISA range is already used */
  242. if (check_region(address,FOO_EXTENT))
  243. goto ERROR0;
  244. /* Probe whether there is anything on this address.
  245. Some example code is below, but you will have to adapt this
  246. for your own driver */
  247. if (kind < 0) /* Only if no force parameter was used */ {
  248. /* We may need long timeouts at least for some chips. */
  249. #define REALLY_SLOW_IO
  250. i = inb_p(address + 1);
  251. if (inb_p(address + 2) != i)
  252. goto ERROR0;
  253. if (inb_p(address + 3) != i)
  254. goto ERROR0;
  255. if (inb_p(address + 7) != i)
  256. goto ERROR0;
  257. #undef REALLY_SLOW_IO
  258. /* Let's just hope nothing breaks here */
  259. i = inb_p(address + 5) & 0x7f;
  260. outb_p(~i & 0x7f,address+5);
  261. if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
  262. outb_p(i,address+5);
  263. return 0;
  264. }
  265. }
  266. }
  267. /* SENSORS ONLY END */
  268. /* OK. For now, we presume we have a valid client. We now create the
  269. client structure, even though we cannot fill it completely yet.
  270. But it allows us to access several i2c functions safely */
  271. /* Note that we reserve some space for foo_data too. If you don't
  272. need it, remove it. We do it here to help to lessen memory
  273. fragmentation. */
  274. if (! (new_client = kmalloc(sizeof(struct i2c_client) +
  275. sizeof(struct foo_data),
  276. GFP_KERNEL))) {
  277. err = -ENOMEM;
  278. goto ERROR0;
  279. }
  280. /* This is tricky, but it will set the data to the right value. */
  281. client->data = new_client + 1;
  282. data = (struct foo_data *) (client->data);
  283. new_client->addr = address;
  284. new_client->data = data;
  285. new_client->adapter = adapter;
  286. new_client->driver = &foo_driver;
  287. new_client->flags = 0;
  288. /* Now, we do the remaining detection. If no `force' parameter is used. */
  289. /* First, the generic detection (if any), that is skipped if any force
  290. parameter was used. */
  291. if (kind < 0) {
  292. /* The below is of course bogus */
  293. if (foo_read(new_client,FOO_REG_GENERIC) != FOO_GENERIC_VALUE)
  294. goto ERROR1;
  295. }
  296. /* SENSORS ONLY START */
  297. /* Next, specific detection. This is especially important for `sensors'
  298. devices. */
  299. /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter
  300. was used. */
  301. if (kind <= 0) {
  302. i = foo_read(new_client,FOO_REG_CHIPTYPE);
  303. if (i == FOO_TYPE_1)
  304. kind = chip1; /* As defined in the enum */
  305. else if (i == FOO_TYPE_2)
  306. kind = chip2;
  307. else {
  308. printk("foo: Ignoring 'force' parameter for unknown chip at "
  309. "adapter %d, address 0x%02x\n",i2c_adapter_id(adapter),address);
  310. goto ERROR1;
  311. }
  312. }
  313. /* Now set the type and chip names */
  314. if (kind == chip1) {
  315. type_name = "chip1"; /* For /proc entry */
  316. client_name = "CHIP 1";
  317. } else if (kind == chip2) {
  318. type_name = "chip2"; /* For /proc entry */
  319. client_name = "CHIP 2";
  320. }
  321. /* Reserve the ISA region */
  322. if (is_isa)
  323. request_region(address,FOO_EXTENT,type_name);
  324. /* SENSORS ONLY END */
  325. /* Fill in the remaining client fields. */
  326. strcpy(new_client->name,client_name);
  327. /* SENSORS ONLY BEGIN */
  328. data->type = kind;
  329. /* SENSORS ONLY END */
  330. data->valid = 0; /* Only if you use this field */
  331. init_MUTEX(&data->update_lock); /* Only if you use this field */
  332. /* Any other initializations in data must be done here too. */
  333. /* Tell the i2c layer a new client has arrived */
  334. if ((err = i2c_attach_client(new_client)))
  335. goto ERROR3;
  336. /* SENSORS ONLY BEGIN */
  337. /* Register a new directory entry with module sensors. See below for
  338. the `template' structure. */
  339. if ((i = i2c_register_entry(new_client, type_name,
  340. foo_dir_table_template,THIS_MODULE)) < 0) {
  341. err = i;
  342. goto ERROR4;
  343. }
  344. data->sysctl_id = i;
  345. /* SENSORS ONLY END */
  346. /* This function can write default values to the client registers, if
  347. needed. */
  348. foo_init_client(new_client);
  349. return 0;
  350. /* OK, this is not exactly good programming practice, usually. But it is
  351. very code-efficient in this case. */
  352. ERROR4:
  353. i2c_detach_client(new_client);
  354. ERROR3:
  355. ERROR2:
  356. /* SENSORS ONLY START */
  357. if (is_isa)
  358. release_region(address,FOO_EXTENT);
  359. /* SENSORS ONLY END */
  360. ERROR1:
  361. kfree(new_client);
  362. ERROR0:
  363. return err;
  364. }
  365. Removing the client
  366. ===================
  367. The detach_client call back function is called when a client should be
  368. removed. It may actually fail, but only when panicking. This code is
  369. much simpler than the attachment code, fortunately!
  370. int foo_detach_client(struct i2c_client *client)
  371. {
  372. int err,i;
  373. /* SENSORS ONLY START */
  374. /* Deregister with the `i2c-proc' module. */
  375. i2c_deregister_entry(((struct lm78_data *)(client->data))->sysctl_id);
  376. /* SENSORS ONLY END */
  377. /* Try to detach the client from i2c space */
  378. if ((err = i2c_detach_client(client)))
  379. return err;
  380. /* HYBRID SENSORS CHIP ONLY START */
  381. if i2c_is_isa_client(client)
  382. release_region(client->addr,LM78_EXTENT);
  383. /* HYBRID SENSORS CHIP ONLY END */
  384. kfree(client); /* Frees client data too, if allocated at the same time */
  385. return 0;
  386. }
  387. Initializing the module or kernel
  388. =================================
  389. When the kernel is booted, or when your foo driver module is inserted,
  390. you have to do some initializing. Fortunately, just attaching (registering)
  391. the driver module is usually enough.
  392. /* Keep track of how far we got in the initialization process. If several
  393. things have to initialized, and we fail halfway, only those things
  394. have to be cleaned up! */
  395. static int __initdata foo_initialized = 0;
  396. static int __init foo_init(void)
  397. {
  398. int res;
  399. printk("foo version %s (%s)\n",FOO_VERSION,FOO_DATE);
  400. if ((res = i2c_add_driver(&foo_driver))) {
  401. printk("foo: Driver registration failed, module not inserted.\n");
  402. foo_cleanup();
  403. return res;
  404. }
  405. foo_initialized ++;
  406. return 0;
  407. }
  408. void foo_cleanup(void)
  409. {
  410. if (foo_initialized == 1) {
  411. if ((res = i2c_del_driver(&foo_driver))) {
  412. printk("foo: Driver registration failed, module not removed.\n");
  413. return;
  414. }
  415. foo_initialized --;
  416. }
  417. }
  418. /* Substitute your own name and email address */
  419. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
  420. MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
  421. module_init(foo_init);
  422. module_exit(foo_cleanup);
  423. Note that some functions are marked by `__init', and some data structures
  424. by `__init_data'. Hose functions and structures can be removed after
  425. kernel booting (or module loading) is completed.
  426. Command function
  427. ================
  428. A generic ioctl-like function call back is supported. You will seldom
  429. need this. You may even set it to NULL.
  430. /* No commands defined */
  431. int foo_command(struct i2c_client *client, unsigned int cmd, void *arg)
  432. {
  433. return 0;
  434. }
  435. Sending and receiving
  436. =====================
  437. If you want to communicate with your device, there are several functions
  438. to do this. You can find all of them in i2c.h.
  439. If you can choose between plain i2c communication and SMBus level
  440. communication, please use the last. All adapters understand SMBus level
  441. commands, but only some of them understand plain i2c!
  442. Plain i2c communication
  443. -----------------------
  444. extern int i2c_master_send(struct i2c_client *,const char* ,int);
  445. extern int i2c_master_recv(struct i2c_client *,char* ,int);
  446. These routines read and write some bytes from/to a client. The client
  447. contains the i2c address, so you do not have to include it. The second
  448. parameter contains the bytes the read/write, the third the length of the
  449. buffer. Returned is the actual number of bytes read/written.
  450. extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  451. int num);
  452. This sends a series of messages. Each message can be a read or write,
  453. and they can be mixed in any way. The transactions are combined: no
  454. stop bit is sent between transaction. The i2c_msg structure contains
  455. for each message the client address, the number of bytes of the message
  456. and the message data itself.
  457. You can read the file `i2c-protocol' for more information about the
  458. actual i2c protocol.
  459. SMBus communication
  460. -------------------
  461. extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr,
  462. unsigned short flags,
  463. char read_write, u8 command, int size,
  464. union i2c_smbus_data * data);
  465. This is the generic SMBus function. All functions below are implemented
  466. in terms of it. Never use this function directly!
  467. extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
  468. extern s32 i2c_smbus_read_byte(struct i2c_client * client);
  469. extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value);
  470. extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command);
  471. extern s32 i2c_smbus_write_byte_data(struct i2c_client * client,
  472. u8 command, u8 value);
  473. extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
  474. extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
  475. u8 command, u16 value);
  476. extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
  477. u8 command, u8 length,
  478. u8 *values);
  479. These ones were removed in Linux 2.6.10 because they had no users, but could
  480. be added back later if needed:
  481. extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
  482. u8 command, u8 *values);
  483. extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
  484. u8 command, u8 *values);
  485. extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
  486. u8 command, u8 length,
  487. u8 *values);
  488. extern s32 i2c_smbus_process_call(struct i2c_client * client,
  489. u8 command, u16 value);
  490. extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
  491. u8 command, u8 length,
  492. u8 *values)
  493. All these transactions return -1 on failure. The 'write' transactions
  494. return 0 on success; the 'read' transactions return the read value, except
  495. for read_block, which returns the number of values read. The block buffers
  496. need not be longer than 32 bytes.
  497. You can read the file `smbus-protocol' for more information about the
  498. actual SMBus protocol.
  499. General purpose routines
  500. ========================
  501. Below all general purpose routines are listed, that were not mentioned
  502. before.
  503. /* This call returns a unique low identifier for each registered adapter,
  504. * or -1 if the adapter was not registered.
  505. */
  506. extern int i2c_adapter_id(struct i2c_adapter *adap);
  507. The sensors sysctl/proc interface
  508. =================================
  509. This section only applies if you write `sensors' drivers.
  510. Each sensors driver creates a directory in /proc/sys/dev/sensors for each
  511. registered client. The directory is called something like foo-i2c-4-65.
  512. The sensors module helps you to do this as easily as possible.
  513. The template
  514. ------------
  515. You will need to define a ctl_table template. This template will automatically
  516. be copied to a newly allocated structure and filled in where necessary when
  517. you call sensors_register_entry.
  518. First, I will give an example definition.
  519. static ctl_table foo_dir_table_template[] = {
  520. { FOO_SYSCTL_FUNC1, "func1", NULL, 0, 0644, NULL, &i2c_proc_real,
  521. &i2c_sysctl_real,NULL,&foo_func },
  522. { FOO_SYSCTL_FUNC2, "func2", NULL, 0, 0644, NULL, &i2c_proc_real,
  523. &i2c_sysctl_real,NULL,&foo_func },
  524. { FOO_SYSCTL_DATA, "data", NULL, 0, 0644, NULL, &i2c_proc_real,
  525. &i2c_sysctl_real,NULL,&foo_data },
  526. { 0 }
  527. };
  528. In the above example, three entries are defined. They can either be
  529. accessed through the /proc interface, in the /proc/sys/dev/sensors/*
  530. directories, as files named func1, func2 and data, or alternatively
  531. through the sysctl interface, in the appropriate table, with identifiers
  532. FOO_SYSCTL_FUNC1, FOO_SYSCTL_FUNC2 and FOO_SYSCTL_DATA.
  533. The third, sixth and ninth parameters should always be NULL, and the
  534. fourth should always be 0. The fifth is the mode of the /proc file;
  535. 0644 is safe, as the file will be owned by root:root.
  536. The seventh and eighth parameters should be &i2c_proc_real and
  537. &i2c_sysctl_real if you want to export lists of reals (scaled
  538. integers). You can also use your own function for them, as usual.
  539. Finally, the last parameter is the call-back to gather the data
  540. (see below) if you use the *_proc_real functions.
  541. Gathering the data
  542. ------------------
  543. The call back functions (foo_func and foo_data in the above example)
  544. can be called in several ways; the operation parameter determines
  545. what should be done:
  546. * If operation == SENSORS_PROC_REAL_INFO, you must return the
  547. magnitude (scaling) in nrels_mag;
  548. * If operation == SENSORS_PROC_REAL_READ, you must read information
  549. from the chip and return it in results. The number of integers
  550. to display should be put in nrels_mag;
  551. * If operation == SENSORS_PROC_REAL_WRITE, you must write the
  552. supplied information to the chip. nrels_mag will contain the number
  553. of integers, results the integers themselves.
  554. The *_proc_real functions will display the elements as reals for the
  555. /proc interface. If you set the magnitude to 2, and supply 345 for
  556. SENSORS_PROC_REAL_READ, it would display 3.45; and if the user would
  557. write 45.6 to the /proc file, it would be returned as 4560 for
  558. SENSORS_PROC_REAL_WRITE. A magnitude may even be negative!
  559. An example function:
  560. /* FOO_FROM_REG and FOO_TO_REG translate between scaled values and
  561. register values. Note the use of the read cache. */
  562. void foo_in(struct i2c_client *client, int operation, int ctl_name,
  563. int *nrels_mag, long *results)
  564. {
  565. struct foo_data *data = client->data;
  566. int nr = ctl_name - FOO_SYSCTL_FUNC1; /* reduce to 0 upwards */
  567. if (operation == SENSORS_PROC_REAL_INFO)
  568. *nrels_mag = 2;
  569. else if (operation == SENSORS_PROC_REAL_READ) {
  570. /* Update the readings cache (if necessary) */
  571. foo_update_client(client);
  572. /* Get the readings from the cache */
  573. results[0] = FOO_FROM_REG(data->foo_func_base[nr]);
  574. results[1] = FOO_FROM_REG(data->foo_func_more[nr]);
  575. results[2] = FOO_FROM_REG(data->foo_func_readonly[nr]);
  576. *nrels_mag = 2;
  577. } else if (operation == SENSORS_PROC_REAL_WRITE) {
  578. if (*nrels_mag >= 1) {
  579. /* Update the cache */
  580. data->foo_base[nr] = FOO_TO_REG(results[0]);
  581. /* Update the chip */
  582. foo_write_value(client,FOO_REG_FUNC_BASE(nr),data->foo_base[nr]);
  583. }
  584. if (*nrels_mag >= 2) {
  585. /* Update the cache */
  586. data->foo_more[nr] = FOO_TO_REG(results[1]);
  587. /* Update the chip */
  588. foo_write_value(client,FOO_REG_FUNC_MORE(nr),data->foo_more[nr]);
  589. }
  590. }
  591. }