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