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/module.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/ctype.h>
30 #include <linux/sysctl.h>
31 #include <linux/proc_fs.h>
32 #include <linux/init.h>
33 #include <linux/ioport.h>
34 #include <linux/i2c.h>
35 #include <linux/i2c-proc.h>
36 #include <asm/uaccess.h>
39 #define THIS_MODULE NULL
42 static int i2c_parse_reals(int *nrels, char *buffer, int bufsize,
43 long *results, int magnitude);
44 static int i2c_write_reals(int nrels, char *buffer, size_t *bufsize,
45 long *results, int magnitude);
46 static int i2c_proc_chips(ctl_table * ctl, int write,
47 struct file *filp, void *buffer,
49 static int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
50 void *oldval, size_t * oldlenp,
51 void *newval, size_t newlen,
54 int __init sensors_init(void);
56 #define SENSORS_ENTRY_MAX 20
57 static struct ctl_table_header *i2c_entries[SENSORS_ENTRY_MAX];
59 static struct i2c_client *i2c_clients[SENSORS_ENTRY_MAX];
60 static unsigned short i2c_inodes[SENSORS_ENTRY_MAX];
62 static ctl_table sysctl_table[] = {
63 {CTL_DEV, "dev", NULL, 0, 0555},
65 {DEV_SENSORS, "sensors", NULL, 0, 0555},
67 {0, NULL, NULL, 0, 0555},
71 static ctl_table i2c_proc_dev_sensors[] = {
72 {SENSORS_CHIPS, "chips", NULL, 0, 0644, NULL, &i2c_proc_chips,
77 static ctl_table i2c_proc_dev[] = {
78 {DEV_SENSORS, "sensors", NULL, 0, 0555, i2c_proc_dev_sensors},
83 static ctl_table i2c_proc[] = {
84 {CTL_DEV, "dev", NULL, 0, 0555, i2c_proc_dev},
89 static struct ctl_table_header *i2c_proc_header;
90 static int i2c_initialized;
92 /* This returns a nice name for a new directory; for example lm78-isa-0310
93 (for a LM78 chip on the ISA bus at port 0x310), or lm75-i2c-3-4e (for
94 a LM75 chip on the third i2c bus at address 0x4e).
95 name is allocated first. */
96 int i2c_create_name(char **name, const char *prefix,
97 struct i2c_adapter *adapter, int addr)
101 if (i2c_is_isa_adapter(adapter))
102 sprintf(name_buffer, "%s-isa-%04x", prefix, addr);
104 if ((id = i2c_adapter_id(adapter)) < 0)
106 sprintf(name_buffer, "%s-i2c-%d-%02x", prefix, id, addr);
108 *name = kmalloc(strlen(name_buffer) + 1, GFP_KERNEL);
110 printk (KERN_WARNING "i2c_create_name: not enough memory\n");
113 strcpy(*name, name_buffer);
117 /* This rather complex function must be called when you want to add an entry
118 to /proc/sys/dev/sensors/chips. It also creates a new directory within
119 /proc/sys/dev/sensors/.
120 ctl_template should be a template of the newly created directory. It is
121 copied in memory. The extra2 field of each file is set to point to client.
122 If any driver wants subdirectories within the newly created directory,
123 this function must be updated!
124 controlling_mod is the controlling module. It should usually be
125 THIS_MODULE when calling. */
126 int i2c_register_entry(struct i2c_client *client, const char *prefix,
127 ctl_table *ctl_template,
128 struct module *controlling_mod)
131 ctl_table *new_table;
133 struct ctl_table_header *new_header;
135 if ((res = i2c_create_name(&name, prefix, client->adapter,
136 client->addr))) return res;
138 for (id = 0; id < SENSORS_ENTRY_MAX; id++)
139 if (!i2c_entries[id]) {
142 if (id == SENSORS_ENTRY_MAX) {
149 while (ctl_template[len].ctl_name)
152 if (!(new_table = kmalloc(sizeof(ctl_table) * len, GFP_KERNEL))) {
157 memcpy(new_table, sysctl_table, 6 * sizeof(ctl_table));
158 new_table[0].child = &new_table[2];
159 new_table[2].child = &new_table[4];
160 new_table[4].child = &new_table[6];
161 new_table[4].procname = name;
162 new_table[4].ctl_name = id;
163 memcpy(new_table + 6, ctl_template, (len - 6) * sizeof(ctl_table));
164 for (i = 6; i < len; i++)
165 new_table[i].extra2 = client;
167 if (!(new_header = register_sysctl_table(new_table, 0))) {
173 i2c_entries[id - 256] = new_header;
175 i2c_clients[id - 256] = client;
177 if (!new_header || !new_header->ctl_table ||
178 !new_header->ctl_table->child ||
179 !new_header->ctl_table->child->child ||
180 !new_header->ctl_table->child->child->de) {
182 ("i2c-proc.o: NULL pointer when trying to install fill_inode fix!\n");
186 i2c_inodes[id - 256] =
187 new_header->ctl_table->child->child->de->low_ino;
188 new_header->ctl_table->child->child->de->owner = controlling_mod;
193 void i2c_deregister_entry(int id)
198 if (i2c_entries[id]) {
199 table = i2c_entries[id]->ctl_table;
200 unregister_sysctl_table(i2c_entries[id]);
201 /* 2-step kfree needed to keep gcc happy about const points */
202 temp = (char *) table[4].procname;
205 i2c_entries[id] = NULL;
206 i2c_clients[id] = NULL;
210 /* Monitor access for /proc/sys/dev/sensors; make unloading i2c-proc.o
211 impossible if some process still uses it or some file in it */
212 void i2c_fill_inode(struct inode *inode, int fill)
220 /* Monitor access for /proc/sys/dev/sensors/ directories; make unloading
221 the corresponding module impossible if some process still uses it or
223 void i2c_dir_fill_inode(struct inode *inode, int fill)
226 struct i2c_client *client;
230 printk("i2c-proc.o: Warning: inode NULL in fill_inode()\n");
233 #endif /* def DEBUG */
235 for (i = 0; i < SENSORS_ENTRY_MAX; i++)
237 && (i2c_inodes[i] == inode->i_ino)) break;
239 if (i == SENSORS_ENTRY_MAX) {
241 ("i2c-proc.o: Warning: inode (%ld) not found in fill_inode()\n",
245 #endif /* def DEBUG */
246 client = i2c_clients[i];
248 client->driver->inc_use(client);
250 client->driver->dec_use(client);
253 int i2c_proc_chips(ctl_table * ctl, int write, struct file *filp,
254 void *buffer, size_t * lenp)
256 char BUF[SENSORS_PREFIX_MAX + 30];
257 int buflen, curbufsize, i;
258 struct ctl_table *client_tbl;
263 /* If buffer is size 0, or we try to read when not at the start, we
264 return nothing. Note that I think writing when not at the start
265 does not work either, but anyway, this is straight from the kernel
267 if (!*lenp || (filp->f_pos && !write)) {
272 for (i = 0; i < SENSORS_ENTRY_MAX; i++)
273 if (i2c_entries[i]) {
275 i2c_entries[i]->ctl_table->child->child;
277 sprintf(BUF, "%d\t%s\n", client_tbl->ctl_name,
278 client_tbl->procname);
279 if (buflen + curbufsize > *lenp)
280 buflen = *lenp - curbufsize;
281 if(copy_to_user(buffer, BUF, buflen))
283 curbufsize += buflen;
284 buffer = (char *) buffer + buflen;
287 filp->f_pos += curbufsize;
291 int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
292 void *oldval, size_t * oldlenp, void *newval,
293 size_t newlen, void **context)
295 struct i2c_chips_data data;
296 int i, oldlen, nrels, maxels,ret=0;
297 struct ctl_table *client_tbl;
299 if (oldval && oldlenp && !((ret = get_user(oldlen, oldlenp))) &&
301 maxels = oldlen / sizeof(struct i2c_chips_data);
303 for (i = 0; (i < SENSORS_ENTRY_MAX) && (nrels < maxels);
305 if (i2c_entries[i]) {
307 i2c_entries[i]->ctl_table->child->
309 data.sysctl_id = client_tbl->ctl_name;
310 strcpy(data.name, client_tbl->procname);
311 if(copy_to_user(oldval, &data,
315 oldval = (char *) oldval +
316 sizeof(struct i2c_chips_data);
319 oldlen = nrels * sizeof(struct i2c_chips_data);
320 if(put_user(oldlen, oldlenp))
327 /* This funcion reads or writes a 'real' value (encoded by the combination
328 of an integer and a magnitude, the last is the power of ten the value
329 should be divided with) to a /proc/sys directory. To use this function,
330 you must (before registering the ctl_table) set the extra2 field to the
331 client, and the extra1 field to a function of the form:
332 void func(struct i2c_client *client, int operation, int ctl_name,
333 int *nrels_mag, long *results)
334 This function can be called for three values of operation. If operation
335 equals SENSORS_PROC_REAL_INFO, the magnitude should be returned in
336 nrels_mag. If operation equals SENSORS_PROC_REAL_READ, values should
337 be read into results. nrels_mag should return the number of elements
338 read; the maximum number is put in it on entry. Finally, if operation
339 equals SENSORS_PROC_REAL_WRITE, the values in results should be
340 written to the chip. nrels_mag contains on entry the number of elements
342 In all cases, client points to the client we wish to interact with,
343 and ctl_name is the SYSCTL id of the file we are accessing. */
344 int i2c_proc_real(ctl_table * ctl, int write, struct file *filp,
345 void *buffer, size_t * lenp)
347 #define MAX_RESULTS 32
348 int mag, nrels = MAX_RESULTS;
349 long results[MAX_RESULTS];
350 i2c_real_callback callback = ctl->extra1;
351 struct i2c_client *client = ctl->extra2;
354 /* If buffer is size 0, or we try to read when not at the start, we
355 return nothing. Note that I think writing when not at the start
356 does not work either, but anyway, this is straight from the kernel
358 if (!*lenp || (filp->f_pos && !write)) {
363 /* Get the magnitude */
364 callback(client, SENSORS_PROC_REAL_INFO, ctl->ctl_name, &mag,
368 /* Read the complete input into results, converting to longs */
369 res = i2c_parse_reals(&nrels, buffer, *lenp, results, mag);
376 /* Now feed this information back to the client */
377 callback(client, SENSORS_PROC_REAL_WRITE, ctl->ctl_name,
380 filp->f_pos += *lenp;
383 /* Get the information from the client into results */
384 callback(client, SENSORS_PROC_REAL_READ, ctl->ctl_name,
387 /* And write them to buffer, converting to reals */
388 res = i2c_write_reals(nrels, buffer, lenp, results, mag);
391 filp->f_pos += *lenp;
396 /* This function is equivalent to i2c_proc_real, only it interacts with
397 the sysctl(2) syscall, and returns no reals, but integers */
398 int i2c_sysctl_real(ctl_table * table, int *name, int nlen,
399 void *oldval, size_t * oldlenp, void *newval,
400 size_t newlen, void **context)
402 long results[MAX_RESULTS];
403 int oldlen, nrels = MAX_RESULTS,ret=0;
404 i2c_real_callback callback = table->extra1;
405 struct i2c_client *client = table->extra2;
407 /* Check if we need to output the old values */
408 if (oldval && oldlenp && !((ret=get_user(oldlen, oldlenp))) && oldlen) {
409 callback(client, SENSORS_PROC_REAL_READ, table->ctl_name,
412 /* Note the rounding factor! */
413 if (nrels * sizeof(long) < oldlen)
414 oldlen = nrels * sizeof(long);
415 oldlen = (oldlen / sizeof(long)) * sizeof(long);
416 if(copy_to_user(oldval, results, oldlen))
418 if(put_user(oldlen, oldlenp))
422 if (newval && newlen) {
423 /* Note the rounding factor! */
424 newlen -= newlen % sizeof(long);
425 nrels = newlen / sizeof(long);
426 if(copy_from_user(results, newval, newlen))
429 /* Get the new values back to the client */
430 callback(client, SENSORS_PROC_REAL_WRITE, table->ctl_name,
437 /* nrels contains initially the maximum number of elements which can be
438 put in results, and finally the number of elements actually put there.
439 A magnitude of 1 will multiply everything with 10; etc.
440 buffer, bufsize is the character buffer we read from and its length.
441 results will finally contain the parsed integers.
443 Buffer should contain several reals, separated by whitespace. A real
444 has the following syntax:
445 [ Minus ] Digit* [ Dot Digit* ]
446 (everything between [] is optional; * means zero or more).
447 When the next character is unparsable, everything is skipped until the
450 WARNING! This is tricky code. I have tested it, but there may still be
451 hidden bugs in it, even leading to crashes and things!
453 static int i2c_parse_reals(int *nrels, char *buffer, int bufsize,
454 long *results, int magnitude)
456 int maxels, min, mag;
463 while (bufsize && (*nrels < maxels)) {
465 /* Skip spaces at the start */
467 !((ret=get_user(nextchar, buffer))) &&
468 isspace((int) nextchar)) {
475 /* Well, we may be done now */
479 /* New defaults for our result */
484 /* Check for a minus */
485 if (!((ret=get_user(nextchar, buffer)))
486 && (nextchar == '-')) {
494 /* Digits before a decimal dot */
496 !((ret=get_user(nextchar, buffer))) &&
497 isdigit((int) nextchar)) {
498 res = res * 10 + nextchar - '0';
505 /* If mag < 0, we must actually divide here! */
511 if (bufsize && (nextchar == '.')) {
516 /* Read digits while they are significant */
517 while (bufsize && (mag > 0) &&
518 !((ret=get_user(nextchar, buffer))) &&
519 isdigit((int) nextchar)) {
520 res = res * 10 + nextchar - '0';
528 /* If we are out of data, but mag > 0, we need to scale here */
534 /* Skip everything until we hit whitespace */
536 !((ret=get_user(nextchar, buffer))) &&
537 !isspace((int) nextchar)) {
544 /* Put res in results */
545 results[*nrels] = (min ? -1 : 1) * res;
549 /* Well, there may be more in the buffer, but we need no more data.
550 Ignore anything that is left. */
554 static int i2c_write_reals(int nrels, char *buffer, size_t *bufsize,
555 long *results, int magnitude)
558 char BUF[BUFLEN + 1]; /* An individual representation should fit! */
561 int buflen, mag, times;
564 while ((nr < nrels) && (curbufsize < *bufsize)) {
568 if(put_user(' ', buffer))
574 /* Fill BUF with the representation of the next string */
576 buflen = sprintf(BUF, "%ld", results[nr]);
577 if (buflen < 0) { /* Oops, a sprintf error! */
581 while ((mag < 0) && (buflen < BUFLEN)) {
588 for (times = 1; mag-- > 0; times *= 10);
589 if (results[nr] < 0) {
594 strcpy(printfstr, "%ld.%0Xld");
595 printfstr[6] = magnitude + '0';
597 sprintf(BUF + buflen, printfstr,
598 abs(results[nr]) / times,
599 abs(results[nr]) % times);
600 if (buflen < 0) { /* Oops, a sprintf error! */
606 /* Now copy it to the user-space buffer */
607 if (buflen + curbufsize > *bufsize)
608 buflen = *bufsize - curbufsize;
609 if(copy_to_user(buffer, BUF, buflen))
611 curbufsize += buflen;
616 if (curbufsize < *bufsize) {
617 if(put_user('\n', buffer))
621 *bufsize = curbufsize;
626 /* Very inefficient for ISA detects, and won't work for 10-bit addresses! */
627 int i2c_detect(struct i2c_adapter *adapter,
628 struct i2c_address_data *address_data,
629 i2c_found_addr_proc * found_proc)
631 int addr, i, found, j, err;
632 struct i2c_force_data *this_force;
633 int is_isa = i2c_is_isa_adapter(adapter);
635 is_isa ? SENSORS_ISA_BUS : i2c_adapter_id(adapter);
637 /* Forget it if we can't probe using SMBUS_QUICK */
639 && !i2c_check_functionality(adapter,
640 I2C_FUNC_SMBUS_QUICK)) return -1;
642 for (addr = 0x00; addr <= (is_isa ? 0xffff : 0x7f); addr++) {
643 if ((is_isa && check_region(addr, 1)) ||
644 (!is_isa && i2c_check_addr(adapter, addr)))
647 /* If it is in one of the force entries, we don't do any
653 address_data->forces + i, this_force->force); i++) {
656 && (this_force->force[j] != SENSORS_I2C_END);
659 ((adapter_id == this_force->force[j])
662 force[j] == SENSORS_ANY_I2C_BUS)
664 && (addr == this_force->force[j + 1])) {
667 (KERN_DEBUG "i2c-proc.o: found force parameter for adapter %d, addr %04x\n",
672 found_proc(adapter, addr, 0,
682 /* If this address is in one of the ignores, we can forget about it
686 && (address_data->ignore[i] != SENSORS_I2C_END);
689 ((adapter_id == address_data->ignore[i])
692 ignore[i] == SENSORS_ANY_I2C_BUS)
694 && (addr == address_data->ignore[i + 1])) {
697 (KERN_DEBUG "i2c-proc.o: found ignore parameter for adapter %d, "
698 "addr %04x\n", adapter_id, addr);
705 && (address_data->ignore_range[i] != SENSORS_I2C_END);
708 ((adapter_id == address_data->ignore_range[i])
712 SENSORS_ANY_I2C_BUS) && !is_isa))
713 && (addr >= address_data->ignore_range[i + 1])
714 && (addr <= address_data->ignore_range[i + 2])) {
717 (KERN_DEBUG "i2c-proc.o: found ignore_range parameter for adapter %d, "
718 "addr %04x\n", adapter_id, addr);
726 /* Now, we will do a detection, but only if it is in the normal or
731 && (address_data->normal_isa[i] !=
732 SENSORS_ISA_END); i += 1) {
733 if (addr == address_data->normal_isa[i]) {
736 (KERN_DEBUG "i2c-proc.o: found normal isa entry for adapter %d, "
737 "addr %04x\n", adapter_id,
745 && (address_data->normal_isa_range[i] !=
746 SENSORS_ISA_END); i += 3) {
748 address_data->normal_isa_range[i])
750 address_data->normal_isa_range[i + 1])
753 address_data->normal_isa_range[i]) %
754 address_data->normal_isa_range[i + 2] ==
758 (KERN_DEBUG "i2c-proc.o: found normal isa_range entry for adapter %d, "
759 "addr %04x\n", adapter_id, addr);
766 !found && (address_data->normal_i2c[i] !=
767 SENSORS_I2C_END); i += 1) {
768 if (addr == address_data->normal_i2c[i]) {
772 (KERN_DEBUG "i2c-proc.o: found normal i2c entry for adapter %d, "
773 "addr %02x\n", adapter_id, addr);
779 && (address_data->normal_i2c_range[i] !=
780 SENSORS_I2C_END); i += 2) {
782 address_data->normal_i2c_range[i])
784 address_data->normal_i2c_range[i + 1]))
788 (KERN_DEBUG "i2c-proc.o: found normal i2c_range entry for adapter %d, "
789 "addr %04x\n", adapter_id, addr);
797 !found && (address_data->probe[i] != SENSORS_I2C_END);
799 if (((adapter_id == address_data->probe[i]) ||
801 probe[i] == SENSORS_ANY_I2C_BUS) && !is_isa))
802 && (addr == address_data->probe[i + 1])) {
805 (KERN_DEBUG "i2c-proc.o: found probe parameter for adapter %d, "
806 "addr %04x\n", adapter_id, addr);
811 for (i = 0; !found &&
812 (address_data->probe_range[i] != SENSORS_I2C_END);
815 ((adapter_id == address_data->probe_range[i])
817 ((address_data->probe_range[i] ==
818 SENSORS_ANY_I2C_BUS) && !is_isa))
819 && (addr >= address_data->probe_range[i + 1])
820 && (addr <= address_data->probe_range[i + 2])) {
824 (KERN_DEBUG "i2c-proc.o: found probe_range parameter for adapter %d, "
825 "addr %04x\n", adapter_id, addr);
832 /* OK, so we really should examine this address. First check
833 whether there is some client here at all! */
836 (adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0))
837 if ((err = found_proc(adapter, addr, 0, -1)))
843 int __init sensors_init(void)
845 printk(KERN_INFO "i2c-proc.o version %s (%s)\n", I2C_VERSION, I2C_DATE);
849 register_sysctl_table(i2c_proc, 0))) return -ENOMEM;
850 i2c_proc_header->ctl_table->child->de->owner = THIS_MODULE;
855 EXPORT_SYMBOL(i2c_deregister_entry);
856 EXPORT_SYMBOL(i2c_detect);
857 EXPORT_SYMBOL(i2c_proc_real);
858 EXPORT_SYMBOL(i2c_register_entry);
859 EXPORT_SYMBOL(i2c_sysctl_real);
863 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
864 MODULE_DESCRIPTION("i2c-proc driver");
865 MODULE_LICENSE("GPL");
867 int i2c_cleanup(void)
869 if (i2c_initialized >= 1) {
870 unregister_sysctl_table(i2c_proc_header);
876 int init_module(void)
878 return sensors_init();
881 int cleanup_module(void)
883 return i2c_cleanup();