2 i2c-proc.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl> and
5 Mark D. Studebaker <mdsxyz123@yahoo.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 This driver puts entries in /proc/sys/dev/sensors for each I2C device
26 #include <linux/version.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sysctl.h>
32 #include <linux/proc_fs.h>
33 #include <linux/ioport.h>
34 #include <asm/uaccess.h>
36 #include <linux/i2c.h>
37 #include <linux/i2c-proc.h>
39 #include <linux/init.h>
41 /* FIXME need i2c versioning */
42 #define LM_DATE "20010825"
43 #define LM_VERSION "2.6.1"
46 #define THIS_MODULE NULL
49 static int i2c_create_name(char **name, const char *prefix,
50 struct i2c_adapter *adapter, int addr);
51 static int i2c_parse_reals(int *nrels, void *buffer, int bufsize,
52 long *results, int magnitude);
53 static int i2c_write_reals(int nrels, void *buffer, int *bufsize,
54 long *results, int magnitude);
55 static int i2c_proc_chips(ctl_table * ctl, int write,
56 struct file *filp, void *buffer,
58 static int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
59 void *oldval, size_t * oldlenp,
60 void *newval, size_t newlen,
63 int __init sensors_init(void);
65 #define SENSORS_ENTRY_MAX 20
66 static struct ctl_table_header *i2c_entries[SENSORS_ENTRY_MAX];
68 static struct i2c_client *i2c_clients[SENSORS_ENTRY_MAX];
69 static unsigned short i2c_inodes[SENSORS_ENTRY_MAX];
70 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1)
71 static void i2c_fill_inode(struct inode *inode, int fill);
72 static void i2c_dir_fill_inode(struct inode *inode, int fill);
73 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1) */
75 static ctl_table sysctl_table[] = {
76 {CTL_DEV, "dev", NULL, 0, 0555},
78 {DEV_SENSORS, "sensors", NULL, 0, 0555},
80 {0, NULL, NULL, 0, 0555},
84 static ctl_table i2c_proc_dev_sensors[] = {
85 {SENSORS_CHIPS, "chips", NULL, 0, 0644, NULL, &i2c_proc_chips,
90 static ctl_table i2c_proc_dev[] = {
91 {DEV_SENSORS, "sensors", NULL, 0, 0555, i2c_proc_dev_sensors},
96 static ctl_table i2c_proc[] = {
97 {CTL_DEV, "dev", NULL, 0, 0555, i2c_proc_dev},
102 static struct ctl_table_header *i2c_proc_header;
103 static int i2c_initialized;
105 /* This returns a nice name for a new directory; for example lm78-isa-0310
106 (for a LM78 chip on the ISA bus at port 0x310), or lm75-i2c-3-4e (for
107 a LM75 chip on the third i2c bus at address 0x4e).
108 name is allocated first. */
109 int i2c_create_name(char **name, const char *prefix,
110 struct i2c_adapter *adapter, int addr)
112 char name_buffer[50];
114 if (i2c_is_isa_adapter(adapter))
115 sprintf(name_buffer, "%s-isa-%04x", prefix, addr);
117 if ((id = i2c_adapter_id(adapter)) < 0)
119 sprintf(name_buffer, "%s-i2c-%d-%02x", prefix, id, addr);
121 *name = kmalloc(strlen(name_buffer) + 1, GFP_KERNEL);
123 printk (KERN_WARNING "i2c_create_name: not enough memory\n");
126 strcpy(*name, name_buffer);
130 /* This rather complex function must be called when you want to add an entry
131 to /proc/sys/dev/sensors/chips. It also creates a new directory within
132 /proc/sys/dev/sensors/.
133 ctl_template should be a template of the newly created directory. It is
134 copied in memory. The extra2 field of each file is set to point to client.
135 If any driver wants subdirectories within the newly created directory,
136 this function must be updated!
137 controlling_mod is the controlling module. It should usually be
138 THIS_MODULE when calling. Note that this symbol is not defined in
139 kernels before 2.3.13; define it to NULL in that case. We will not use it
140 for anything older than 2.3.27 anyway. */
141 int i2c_register_entry(struct i2c_client *client, const char *prefix,
142 ctl_table * ctl_template,
143 struct module *controlling_mod)
146 ctl_table *new_table;
148 struct ctl_table_header *new_header;
150 if ((res = i2c_create_name(&name, prefix, client->adapter,
151 client->addr))) return res;
153 for (id = 0; id < SENSORS_ENTRY_MAX; id++)
154 if (!i2c_entries[id]) {
157 if (id == SENSORS_ENTRY_MAX) {
164 while (ctl_template[len].procname)
167 if (!(new_table = kmalloc(sizeof(ctl_table) * len, GFP_KERNEL))) {
172 memcpy(new_table, sysctl_table, 6 * sizeof(ctl_table));
173 new_table[0].child = &new_table[2];
174 new_table[2].child = &new_table[4];
175 new_table[4].child = &new_table[6];
176 new_table[4].procname = name;
177 new_table[4].ctl_name = id;
178 memcpy(new_table + 6, ctl_template, (len - 6) * sizeof(ctl_table));
179 for (i = 6; i < len; i++)
180 new_table[i].extra2 = client;
182 if (!(new_header = register_sysctl_table(new_table, 0))) {
188 i2c_entries[id - 256] = new_header;
190 i2c_clients[id - 256] = client;
192 if (!new_header || !new_header->ctl_table ||
193 !new_header->ctl_table->child ||
194 !new_header->ctl_table->child->child ||
195 !new_header->ctl_table->child->child->de) {
197 ("i2c-proc.o: NULL pointer when trying to install fill_inode fix!\n");
201 i2c_inodes[id - 256] =
202 new_header->ctl_table->child->child->de->low_ino;
203 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27))
204 new_header->ctl_table->child->child->de->owner = controlling_mod;
206 new_header->ctl_table->child->child->de->fill_inode =
208 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27)) */
213 void i2c_deregister_entry(int id)
218 if (i2c_entries[id]) {
219 table = i2c_entries[id]->ctl_table;
220 unregister_sysctl_table(i2c_entries[id]);
221 /* 2-step kfree needed to keep gcc happy about const points */
222 (const char *) temp = table[4].procname;
225 i2c_entries[id] = NULL;
226 i2c_clients[id] = NULL;
230 /* Monitor access for /proc/sys/dev/sensors; make unloading i2c-proc.o
231 impossible if some process still uses it or some file in it */
232 void i2c_fill_inode(struct inode *inode, int fill)
240 /* Monitor access for /proc/sys/dev/sensors/ directories; make unloading
241 the corresponding module impossible if some process still uses it or
243 void i2c_dir_fill_inode(struct inode *inode, int fill)
246 struct i2c_client *client;
250 printk("i2c-proc.o: Warning: inode NULL in fill_inode()\n");
253 #endif /* def DEBUG */
255 for (i = 0; i < SENSORS_ENTRY_MAX; i++)
257 && (i2c_inodes[i] == inode->i_ino)) break;
259 if (i == SENSORS_ENTRY_MAX) {
261 ("i2c-proc.o: Warning: inode (%ld) not found in fill_inode()\n",
265 #endif /* def DEBUG */
266 client = i2c_clients[i];
268 client->driver->inc_use(client);
270 client->driver->dec_use(client);
273 int i2c_proc_chips(ctl_table * ctl, int write, struct file *filp,
274 void *buffer, size_t * lenp)
276 char BUF[SENSORS_PREFIX_MAX + 30];
277 int buflen, curbufsize, i;
278 struct ctl_table *client_tbl;
283 /* If buffer is size 0, or we try to read when not at the start, we
284 return nothing. Note that I think writing when not at the start
285 does not work either, but anyway, this is straight from the kernel
287 if (!*lenp || (filp->f_pos && !write)) {
292 for (i = 0; i < SENSORS_ENTRY_MAX; i++)
293 if (i2c_entries[i]) {
295 i2c_entries[i]->ctl_table->child->child;
297 sprintf(BUF, "%d\t%s\n", client_tbl->ctl_name,
298 client_tbl->procname);
299 if (buflen + curbufsize > *lenp)
300 buflen = *lenp - curbufsize;
301 if(copy_to_user(buffer, BUF, buflen))
303 curbufsize += buflen;
304 (char *) buffer += buflen;
307 filp->f_pos += curbufsize;
311 int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
312 void *oldval, size_t * oldlenp, void *newval,
313 size_t newlen, void **context)
315 struct i2c_chips_data data;
316 int i, oldlen, nrels, maxels,ret=0;
317 struct ctl_table *client_tbl;
319 if (oldval && oldlenp && !((ret = get_user(oldlen, oldlenp))) &&
321 maxels = oldlen / sizeof(struct i2c_chips_data);
323 for (i = 0; (i < SENSORS_ENTRY_MAX) && (nrels < maxels);
325 if (i2c_entries[i]) {
327 i2c_entries[i]->ctl_table->child->
329 data.sysctl_id = client_tbl->ctl_name;
330 strcpy(data.name, client_tbl->procname);
331 if(copy_to_user(oldval, &data,
336 sizeof(struct i2c_chips_data);
339 oldlen = nrels * sizeof(struct i2c_chips_data);
340 if(put_user(oldlen, oldlenp))
347 /* This funcion reads or writes a 'real' value (encoded by the combination
348 of an integer and a magnitude, the last is the power of ten the value
349 should be divided with) to a /proc/sys directory. To use this function,
350 you must (before registering the ctl_table) set the extra2 field to the
351 client, and the extra1 field to a function of the form:
352 void func(struct i2c_client *client, int operation, int ctl_name,
353 int *nrels_mag, long *results)
354 This function can be called for three values of operation. If operation
355 equals SENSORS_PROC_REAL_INFO, the magnitude should be returned in
356 nrels_mag. If operation equals SENSORS_PROC_REAL_READ, values should
357 be read into results. nrels_mag should return the number of elements
358 read; the maximum number is put in it on entry. Finally, if operation
359 equals SENSORS_PROC_REAL_WRITE, the values in results should be
360 written to the chip. nrels_mag contains on entry the number of elements
362 In all cases, client points to the client we wish to interact with,
363 and ctl_name is the SYSCTL id of the file we are accessing. */
364 int i2c_proc_real(ctl_table * ctl, int write, struct file *filp,
365 void *buffer, size_t * lenp)
367 #define MAX_RESULTS 32
368 int mag, nrels = MAX_RESULTS;
369 long results[MAX_RESULTS];
370 i2c_real_callback callback = ctl->extra1;
371 struct i2c_client *client = ctl->extra2;
374 /* If buffer is size 0, or we try to read when not at the start, we
375 return nothing. Note that I think writing when not at the start
376 does not work either, but anyway, this is straight from the kernel
378 if (!*lenp || (filp->f_pos && !write)) {
383 /* Get the magnitude */
384 callback(client, SENSORS_PROC_REAL_INFO, ctl->ctl_name, &mag,
388 /* Read the complete input into results, converting to longs */
389 res = i2c_parse_reals(&nrels, buffer, *lenp, results, mag);
396 /* Now feed this information back to the client */
397 callback(client, SENSORS_PROC_REAL_WRITE, ctl->ctl_name,
400 filp->f_pos += *lenp;
403 /* Get the information from the client into results */
404 callback(client, SENSORS_PROC_REAL_READ, ctl->ctl_name,
407 /* And write them to buffer, converting to reals */
408 res = i2c_write_reals(nrels, buffer, lenp, results, mag);
411 filp->f_pos += *lenp;
416 /* This function is equivalent to i2c_proc_real, only it interacts with
417 the sysctl(2) syscall, and returns no reals, but integers */
418 int i2c_sysctl_real(ctl_table * table, int *name, int nlen,
419 void *oldval, size_t * oldlenp, void *newval,
420 size_t newlen, void **context)
422 long results[MAX_RESULTS];
423 int oldlen, nrels = MAX_RESULTS,ret=0;
424 i2c_real_callback callback = table->extra1;
425 struct i2c_client *client = table->extra2;
427 /* Check if we need to output the old values */
428 if (oldval && oldlenp && !((ret=get_user(oldlen, oldlenp))) && oldlen) {
429 callback(client, SENSORS_PROC_REAL_READ, table->ctl_name,
432 /* Note the rounding factor! */
433 if (nrels * sizeof(long) < oldlen)
434 oldlen = nrels * sizeof(long);
435 oldlen = (oldlen / sizeof(long)) * sizeof(long);
436 if(copy_to_user(oldval, results, oldlen))
438 if(put_user(oldlen, oldlenp))
442 if (newval && newlen) {
443 /* Note the rounding factor! */
444 newlen -= newlen % sizeof(long);
445 nrels = newlen / sizeof(long);
446 if(copy_from_user(results, newval, newlen))
449 /* Get the new values back to the client */
450 callback(client, SENSORS_PROC_REAL_WRITE, table->ctl_name,
457 /* nrels contains initially the maximum number of elements which can be
458 put in results, and finally the number of elements actually put there.
459 A magnitude of 1 will multiply everything with 10; etc.
460 buffer, bufsize is the character buffer we read from and its length.
461 results will finally contain the parsed integers.
463 Buffer should contain several reals, separated by whitespace. A real
464 has the following syntax:
465 [ Minus ] Digit* [ Dot Digit* ]
466 (everything between [] is optional; * means zero or more).
467 When the next character is unparsable, everything is skipped until the
470 WARNING! This is tricky code. I have tested it, but there may still be
471 hidden bugs in it, even leading to crashes and things!
473 int i2c_parse_reals(int *nrels, void *buffer, int bufsize,
474 long *results, int magnitude)
476 int maxels, min, mag;
483 while (bufsize && (*nrels < maxels)) {
485 /* Skip spaces at the start */
487 !((ret=get_user(nextchar, (char *) buffer))) &&
488 isspace((int) nextchar)) {
495 /* Well, we may be done now */
499 /* New defaults for our result */
504 /* Check for a minus */
505 if (!((ret=get_user(nextchar, (char *) buffer)))
506 && (nextchar == '-')) {
514 /* Digits before a decimal dot */
516 !((ret=get_user(nextchar, (char *) buffer))) &&
517 isdigit((int) nextchar)) {
518 res = res * 10 + nextchar - '0';
525 /* If mag < 0, we must actually divide here! */
531 if (bufsize && (nextchar == '.')) {
536 /* Read digits while they are significant */
537 while (bufsize && (mag > 0) &&
538 !((ret=get_user(nextchar, (char *) buffer))) &&
539 isdigit((int) nextchar)) {
540 res = res * 10 + nextchar - '0';
548 /* If we are out of data, but mag > 0, we need to scale here */
554 /* Skip everything until we hit whitespace */
556 !((ret=get_user(nextchar, (char *) buffer))) &&
557 isspace((int) nextchar)) {
564 /* Put res in results */
565 results[*nrels] = (min ? -1 : 1) * res;
569 /* Well, there may be more in the buffer, but we need no more data.
570 Ignore anything that is left. */
574 int i2c_write_reals(int nrels, void *buffer, int *bufsize,
575 long *results, int magnitude)
578 char BUF[BUFLEN + 1]; /* An individual representation should fit! */
581 int buflen, mag, times;
584 while ((nr < nrels) && (curbufsize < *bufsize)) {
588 if(put_user(' ', (char *) buffer))
594 /* Fill BUF with the representation of the next string */
596 buflen = sprintf(BUF, "%ld", results[nr]);
597 if (buflen < 0) { /* Oops, a sprintf error! */
601 while ((mag < 0) && (buflen < BUFLEN)) {
608 for (times = 1; mag-- > 0; times *= 10);
609 if (results[nr] < 0) {
614 strcpy(printfstr, "%ld.%0Xld");
615 printfstr[6] = magnitude + '0';
617 sprintf(BUF + buflen, printfstr,
618 abs(results[nr]) / times,
619 abs(results[nr]) % times);
620 if (buflen < 0) { /* Oops, a sprintf error! */
626 /* Now copy it to the user-space buffer */
627 if (buflen + curbufsize > *bufsize)
628 buflen = *bufsize - curbufsize;
629 if(copy_to_user(buffer, BUF, buflen))
631 curbufsize += buflen;
632 (char *) buffer += buflen;
636 if (curbufsize < *bufsize) {
637 if(put_user('\n', (char *) buffer))
641 *bufsize = curbufsize;
646 /* Very inefficient for ISA detects, and won't work for 10-bit addresses! */
647 int i2c_detect(struct i2c_adapter *adapter,
648 struct i2c_address_data *address_data,
649 i2c_found_addr_proc * found_proc)
651 int addr, i, found, j, err;
652 struct i2c_force_data *this_force;
653 int is_isa = i2c_is_isa_adapter(adapter);
655 is_isa ? SENSORS_ISA_BUS : i2c_adapter_id(adapter);
657 /* Forget it if we can't probe using SMBUS_QUICK */
659 && !i2c_check_functionality(adapter,
660 I2C_FUNC_SMBUS_QUICK)) return -1;
662 for (addr = 0x00; addr <= (is_isa ? 0xffff : 0x7f); addr++) {
663 if ((is_isa && check_region(addr, 1)) ||
664 (!is_isa && i2c_check_addr(adapter, addr)))
667 /* If it is in one of the force entries, we don't do any
673 address_data->forces + i, this_force->force); i++) {
676 && (this_force->force[j] != SENSORS_I2C_END);
679 ((adapter_id == this_force->force[j])
682 force[j] == SENSORS_ANY_I2C_BUS)
684 && (addr == this_force->force[j + 1])) {
687 ("i2c-proc.o: found force parameter for adapter %d, addr %04x\n",
692 found_proc(adapter, addr, 0,
702 /* If this address is in one of the ignores, we can forget about it
706 && (address_data->ignore[i] != SENSORS_I2C_END);
709 ((adapter_id == address_data->ignore[i])
712 ignore[i] == SENSORS_ANY_I2C_BUS)
714 && (addr == address_data->ignore[i + 1])) {
717 ("i2c-proc.o: found ignore parameter for adapter %d, "
718 "addr %04x\n", adapter_id, addr);
725 && (address_data->ignore_range[i] != SENSORS_I2C_END);
728 ((adapter_id == address_data->ignore_range[i])
732 SENSORS_ANY_I2C_BUS) & !is_isa))
733 && (addr >= address_data->ignore_range[i + 1])
734 && (addr <= address_data->ignore_range[i + 2])) {
737 ("i2c-proc.o: found ignore_range parameter for adapter %d, "
738 "addr %04x\n", adapter_id, addr);
746 /* Now, we will do a detection, but only if it is in the normal or
751 && (address_data->normal_isa[i] !=
752 SENSORS_ISA_END); i += 1) {
753 if (addr == address_data->normal_isa[i]) {
756 ("i2c-proc.o: found normal isa entry for adapter %d, "
757 "addr %04x\n", adapter_id,
765 && (address_data->normal_isa_range[i] !=
766 SENSORS_ISA_END); i += 3) {
768 address_data->normal_isa_range[i])
770 address_data->normal_isa_range[i + 1])
773 address_data->normal_isa_range[i]) %
774 address_data->normal_isa_range[i + 2] ==
778 ("i2c-proc.o: found normal isa_range entry for adapter %d, "
779 "addr %04x", adapter_id, addr);
786 !found && (address_data->normal_i2c[i] !=
787 SENSORS_I2C_END); i += 1) {
788 if (addr == address_data->normal_i2c[i]) {
792 ("i2c-proc.o: found normal i2c entry for adapter %d, "
793 "addr %02x", adapter_id, addr);
799 && (address_data->normal_i2c_range[i] !=
800 SENSORS_I2C_END); i += 2) {
802 address_data->normal_i2c_range[i])
804 address_data->normal_i2c_range[i + 1]))
808 ("i2c-proc.o: found normal i2c_range entry for adapter %d, "
809 "addr %04x\n", adapter_id, addr);
817 !found && (address_data->probe[i] != SENSORS_I2C_END);
819 if (((adapter_id == address_data->probe[i]) ||
821 probe[i] == SENSORS_ANY_I2C_BUS) & !is_isa))
822 && (addr == address_data->probe[i + 1])) {
825 ("i2c-proc.o: found probe parameter for adapter %d, "
826 "addr %04x\n", adapter_id, addr);
831 for (i = 0; !found &&
832 (address_data->probe_range[i] != SENSORS_I2C_END);
835 ((adapter_id == address_data->probe_range[i])
837 ((address_data->probe_range[i] ==
838 SENSORS_ANY_I2C_BUS) & !is_isa))
839 && (addr >= address_data->probe_range[i + 1])
840 && (addr <= address_data->probe_range[i + 2])) {
844 ("i2c-proc.o: found probe_range parameter for adapter %d, "
845 "addr %04x\n", adapter_id, addr);
852 /* OK, so we really should examine this address. First check
853 whether there is some client here at all! */
856 (adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0))
857 if ((err = found_proc(adapter, addr, 0, -1)))
863 int __init sensors_init(void)
865 printk("i2c-proc.o version %s (%s)\n", LM_VERSION, LM_DATE);
869 register_sysctl_table(i2c_proc, 0))) return -ENOMEM;
870 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,1))
871 i2c_proc_header->ctl_table->child->de->owner = THIS_MODULE;
873 i2c_proc_header->ctl_table->child->de->fill_inode =
875 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,1)) */
880 EXPORT_SYMBOL(i2c_deregister_entry);
881 EXPORT_SYMBOL(i2c_detect);
882 EXPORT_SYMBOL(i2c_proc_real);
883 EXPORT_SYMBOL(i2c_register_entry);
884 EXPORT_SYMBOL(i2c_sysctl_real);
888 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
889 MODULE_DESCRIPTION("i2c-proc driver");
890 MODULE_LICENSE("GPL");
892 int i2c_cleanup(void)
894 if (i2c_initialized >= 1) {
895 unregister_sysctl_table(i2c_proc_header);
901 int init_module(void)
903 return sensors_init();
906 int cleanup_module(void)
908 return i2c_cleanup();