i2c-proc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:24k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.     i2c-proc.c - Part of lm_sensors, Linux kernel modules for hardware
  3.                 monitoring
  4.     Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl> and
  5.     Mark D. Studebaker <mdsxyz123@yahoo.com>
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19.     This driver puts entries in /proc/sys/dev/sensors for each I2C device
  20. */
  21. #include <linux/version.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/ctype.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/ioport.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/i2c.h>
  31. #include <linux/i2c-proc.h>
  32. #include <linux/init.h>
  33. /* FIXME need i2c versioning */
  34. #define LM_DATE "20010825"
  35. #define LM_VERSION "2.6.1"
  36. #ifndef THIS_MODULE
  37. #define THIS_MODULE NULL
  38. #endif
  39. static int i2c_create_name(char **name, const char *prefix,
  40.        struct i2c_adapter *adapter, int addr);
  41. static int i2c_parse_reals(int *nrels, void *buffer, int bufsize,
  42.        long *results, int magnitude);
  43. static int i2c_write_reals(int nrels, void *buffer, int *bufsize,
  44.        long *results, int magnitude);
  45. static int i2c_proc_chips(ctl_table * ctl, int write,
  46.       struct file *filp, void *buffer,
  47.       size_t * lenp);
  48. static int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
  49. void *oldval, size_t * oldlenp,
  50. void *newval, size_t newlen,
  51. void **context);
  52. int __init sensors_init(void);
  53. #define SENSORS_ENTRY_MAX 20
  54. static struct ctl_table_header *i2c_entries[SENSORS_ENTRY_MAX];
  55. static struct i2c_client *i2c_clients[SENSORS_ENTRY_MAX];
  56. static unsigned short i2c_inodes[SENSORS_ENTRY_MAX];
  57. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1)
  58. static void i2c_fill_inode(struct inode *inode, int fill);
  59. static void i2c_dir_fill_inode(struct inode *inode, int fill);
  60. #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1) */
  61. static ctl_table sysctl_table[] = {
  62. {CTL_DEV, "dev", NULL, 0, 0555},
  63. {0},
  64. {DEV_SENSORS, "sensors", NULL, 0, 0555},
  65. {0},
  66. {0, NULL, NULL, 0, 0555},
  67. {0}
  68. };
  69. static ctl_table i2c_proc_dev_sensors[] = {
  70. {SENSORS_CHIPS, "chips", NULL, 0, 0644, NULL, &i2c_proc_chips,
  71.  &i2c_sysctl_chips},
  72. {0}
  73. };
  74. static ctl_table i2c_proc_dev[] = {
  75. {DEV_SENSORS, "sensors", NULL, 0, 0555, i2c_proc_dev_sensors},
  76. {0},
  77. };
  78. static ctl_table i2c_proc[] = {
  79. {CTL_DEV, "dev", NULL, 0, 0555, i2c_proc_dev},
  80. {0}
  81. };
  82. static struct ctl_table_header *i2c_proc_header;
  83. static int i2c_initialized;
  84. /* This returns a nice name for a new directory; for example lm78-isa-0310
  85.    (for a LM78 chip on the ISA bus at port 0x310), or lm75-i2c-3-4e (for
  86.    a LM75 chip on the third i2c bus at address 0x4e).  
  87.    name is allocated first. */
  88. int i2c_create_name(char **name, const char *prefix,
  89. struct i2c_adapter *adapter, int addr)
  90. {
  91. char name_buffer[50];
  92. int id;
  93. if (i2c_is_isa_adapter(adapter))
  94. sprintf(name_buffer, "%s-isa-%04x", prefix, addr);
  95. else {
  96. if ((id = i2c_adapter_id(adapter)) < 0)
  97. return -ENOENT;
  98. sprintf(name_buffer, "%s-i2c-%d-%02x", prefix, id, addr);
  99. }
  100. *name = kmalloc(strlen(name_buffer) + 1, GFP_KERNEL);
  101. if (!*name) {
  102. printk (KERN_WARNING "i2c_create_name: not enough memoryn");
  103. return -ENOMEM;
  104. }
  105. strcpy(*name, name_buffer);
  106. return 0;
  107. }
  108. /* This rather complex function must be called when you want to add an entry
  109.    to /proc/sys/dev/sensors/chips. It also creates a new directory within 
  110.    /proc/sys/dev/sensors/.
  111.    ctl_template should be a template of the newly created directory. It is
  112.    copied in memory. The extra2 field of each file is set to point to client.
  113.    If any driver wants subdirectories within the newly created directory,
  114.    this function must be updated! 
  115.    controlling_mod is the controlling module. It should usually be
  116.    THIS_MODULE when calling. Note that this symbol is not defined in
  117.    kernels before 2.3.13; define it to NULL in that case. We will not use it
  118.    for anything older than 2.3.27 anyway. */
  119. int i2c_register_entry(struct i2c_client *client, const char *prefix,
  120.    ctl_table * ctl_template,
  121.    struct module *controlling_mod)
  122. {
  123. int i, res, len, id;
  124. ctl_table *new_table;
  125. char *name;
  126. struct ctl_table_header *new_header;
  127. if ((res = i2c_create_name(&name, prefix, client->adapter,
  128.        client->addr))) return res;
  129. for (id = 0; id < SENSORS_ENTRY_MAX; id++)
  130. if (!i2c_entries[id]) {
  131. break;
  132. }
  133. if (id == SENSORS_ENTRY_MAX) {
  134. kfree(name);
  135. return -ENOMEM;
  136. }
  137. id += 256;
  138. len = 0;
  139. while (ctl_template[len].procname)
  140. len++;
  141. len += 7;
  142. if (!(new_table = kmalloc(sizeof(ctl_table) * len, GFP_KERNEL))) {
  143. kfree(name);
  144. return -ENOMEM;
  145. }
  146. memcpy(new_table, sysctl_table, 6 * sizeof(ctl_table));
  147. new_table[0].child = &new_table[2];
  148. new_table[2].child = &new_table[4];
  149. new_table[4].child = &new_table[6];
  150. new_table[4].procname = name;
  151. new_table[4].ctl_name = id;
  152. memcpy(new_table + 6, ctl_template, (len - 6) * sizeof(ctl_table));
  153. for (i = 6; i < len; i++)
  154. new_table[i].extra2 = client;
  155. if (!(new_header = register_sysctl_table(new_table, 0))) {
  156. kfree(new_table);
  157. kfree(name);
  158. return -ENOMEM;
  159. }
  160. i2c_entries[id - 256] = new_header;
  161. i2c_clients[id - 256] = client;
  162. #ifdef DEBUG
  163. if (!new_header || !new_header->ctl_table ||
  164.     !new_header->ctl_table->child ||
  165.     !new_header->ctl_table->child->child ||
  166.     !new_header->ctl_table->child->child->de) {
  167. printk
  168.     ("i2c-proc.o: NULL pointer when trying to install fill_inode fix!n");
  169. return id;
  170. }
  171. #endif /* DEBUG */
  172. i2c_inodes[id - 256] =
  173.     new_header->ctl_table->child->child->de->low_ino;
  174. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27))
  175. new_header->ctl_table->child->child->de->owner = controlling_mod;
  176. #else
  177. new_header->ctl_table->child->child->de->fill_inode =
  178.     &i2c_dir_fill_inode;
  179. #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27)) */
  180. return id;
  181. }
  182. void i2c_deregister_entry(int id)
  183. {
  184. ctl_table *table;
  185. char *temp;
  186. id -= 256;
  187. if (i2c_entries[id]) {
  188. table = i2c_entries[id]->ctl_table;
  189. unregister_sysctl_table(i2c_entries[id]);
  190. /* 2-step kfree needed to keep gcc happy about const points */
  191. (const char *) temp = table[4].procname;
  192. kfree(temp);
  193. kfree(table);
  194. i2c_entries[id] = NULL;
  195. i2c_clients[id] = NULL;
  196. }
  197. }
  198. /* Monitor access for /proc/sys/dev/sensors; make unloading i2c-proc.o 
  199.    impossible if some process still uses it or some file in it */
  200. void i2c_fill_inode(struct inode *inode, int fill)
  201. {
  202. if (fill)
  203. MOD_INC_USE_COUNT;
  204. else
  205. MOD_DEC_USE_COUNT;
  206. }
  207. /* Monitor access for /proc/sys/dev/sensors/ directories; make unloading
  208.    the corresponding module impossible if some process still uses it or
  209.    some file in it */
  210. void i2c_dir_fill_inode(struct inode *inode, int fill)
  211. {
  212. int i;
  213. struct i2c_client *client;
  214. #ifdef DEBUG
  215. if (!inode) {
  216. printk("i2c-proc.o: Warning: inode NULL in fill_inode()n");
  217. return;
  218. }
  219. #endif /* def DEBUG */
  220. for (i = 0; i < SENSORS_ENTRY_MAX; i++)
  221. if (i2c_clients[i]
  222.     && (i2c_inodes[i] == inode->i_ino)) break;
  223. #ifdef DEBUG
  224. if (i == SENSORS_ENTRY_MAX) {
  225. printk
  226.     ("i2c-proc.o: Warning: inode (%ld) not found in fill_inode()n",
  227.      inode->i_ino);
  228. return;
  229. }
  230. #endif /* def DEBUG */
  231. client = i2c_clients[i];
  232. if (fill)
  233. client->driver->inc_use(client);
  234. else
  235. client->driver->dec_use(client);
  236. }
  237. int i2c_proc_chips(ctl_table * ctl, int write, struct file *filp,
  238.        void *buffer, size_t * lenp)
  239. {
  240. char BUF[SENSORS_PREFIX_MAX + 30];
  241. int buflen, curbufsize, i;
  242. struct ctl_table *client_tbl;
  243. if (write)
  244. return 0;
  245. /* If buffer is size 0, or we try to read when not at the start, we
  246.    return nothing. Note that I think writing when not at the start
  247.    does not work either, but anyway, this is straight from the kernel
  248.    sources. */
  249. if (!*lenp || (filp->f_pos && !write)) {
  250. *lenp = 0;
  251. return 0;
  252. }
  253. curbufsize = 0;
  254. for (i = 0; i < SENSORS_ENTRY_MAX; i++)
  255. if (i2c_entries[i]) {
  256. client_tbl =
  257.     i2c_entries[i]->ctl_table->child->child;
  258. buflen =
  259.     sprintf(BUF, "%dt%sn", client_tbl->ctl_name,
  260.     client_tbl->procname);
  261. if (buflen + curbufsize > *lenp)
  262. buflen = *lenp - curbufsize;
  263. if(copy_to_user(buffer, BUF, buflen))
  264. return -EFAULT;
  265. curbufsize += buflen;
  266. (char *) buffer += buflen;
  267. }
  268. *lenp = curbufsize;
  269. filp->f_pos += curbufsize;
  270. return 0;
  271. }
  272. int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
  273.  void *oldval, size_t * oldlenp, void *newval,
  274.  size_t newlen, void **context)
  275. {
  276. struct i2c_chips_data data;
  277. int i, oldlen, nrels, maxels,ret=0;
  278. struct ctl_table *client_tbl;
  279. if (oldval && oldlenp && !((ret = get_user(oldlen, oldlenp))) && 
  280.     oldlen) {
  281. maxels = oldlen / sizeof(struct i2c_chips_data);
  282. nrels = 0;
  283. for (i = 0; (i < SENSORS_ENTRY_MAX) && (nrels < maxels);
  284.      i++)
  285. if (i2c_entries[i]) {
  286. client_tbl =
  287.     i2c_entries[i]->ctl_table->child->
  288.     child;
  289. data.sysctl_id = client_tbl->ctl_name;
  290. strcpy(data.name, client_tbl->procname);
  291. if(copy_to_user(oldval, &data,
  292.      sizeof(struct
  293.     i2c_chips_data)))
  294. return -EFAULT;
  295. (char *) oldval +=
  296.     sizeof(struct i2c_chips_data);
  297. nrels++;
  298. }
  299. oldlen = nrels * sizeof(struct i2c_chips_data);
  300. if(put_user(oldlen, oldlenp))
  301. return -EFAULT;
  302. }
  303. return ret;
  304. }
  305. /* This funcion reads or writes a 'real' value (encoded by the combination
  306.    of an integer and a magnitude, the last is the power of ten the value
  307.    should be divided with) to a /proc/sys directory. To use this function,
  308.    you must (before registering the ctl_table) set the extra2 field to the
  309.    client, and the extra1 field to a function of the form:
  310.       void func(struct i2c_client *client, int operation, int ctl_name,
  311.                 int *nrels_mag, long *results)
  312.    This function can be called for three values of operation. If operation
  313.    equals SENSORS_PROC_REAL_INFO, the magnitude should be returned in 
  314.    nrels_mag. If operation equals SENSORS_PROC_REAL_READ, values should
  315.    be read into results. nrels_mag should return the number of elements
  316.    read; the maximum number is put in it on entry. Finally, if operation
  317.    equals SENSORS_PROC_REAL_WRITE, the values in results should be
  318.    written to the chip. nrels_mag contains on entry the number of elements
  319.    found.
  320.    In all cases, client points to the client we wish to interact with,
  321.    and ctl_name is the SYSCTL id of the file we are accessing. */
  322. int i2c_proc_real(ctl_table * ctl, int write, struct file *filp,
  323.       void *buffer, size_t * lenp)
  324. {
  325. #define MAX_RESULTS 32
  326. int mag, nrels = MAX_RESULTS;
  327. long results[MAX_RESULTS];
  328. i2c_real_callback callback = ctl->extra1;
  329. struct i2c_client *client = ctl->extra2;
  330. int res;
  331. /* If buffer is size 0, or we try to read when not at the start, we
  332.    return nothing. Note that I think writing when not at the start
  333.    does not work either, but anyway, this is straight from the kernel
  334.    sources. */
  335. if (!*lenp || (filp->f_pos && !write)) {
  336. *lenp = 0;
  337. return 0;
  338. }
  339. /* Get the magnitude */
  340. callback(client, SENSORS_PROC_REAL_INFO, ctl->ctl_name, &mag,
  341.  NULL);
  342. if (write) {
  343. /* Read the complete input into results, converting to longs */
  344. res = i2c_parse_reals(&nrels, buffer, *lenp, results, mag);
  345. if (res)
  346. return res;
  347. if (!nrels)
  348. return 0;
  349. /* Now feed this information back to the client */
  350. callback(client, SENSORS_PROC_REAL_WRITE, ctl->ctl_name,
  351.  &nrels, results);
  352. filp->f_pos += *lenp;
  353. return 0;
  354. } else { /* read */
  355. /* Get the information from the client into results */
  356. callback(client, SENSORS_PROC_REAL_READ, ctl->ctl_name,
  357.  &nrels, results);
  358. /* And write them to buffer, converting to reals */
  359. res = i2c_write_reals(nrels, buffer, lenp, results, mag);
  360. if (res)
  361. return res;
  362. filp->f_pos += *lenp;
  363. return 0;
  364. }
  365. }
  366. /* This function is equivalent to i2c_proc_real, only it interacts with
  367.    the sysctl(2) syscall, and returns no reals, but integers */
  368. int i2c_sysctl_real(ctl_table * table, int *name, int nlen,
  369. void *oldval, size_t * oldlenp, void *newval,
  370. size_t newlen, void **context)
  371. {
  372. long results[MAX_RESULTS];
  373. int oldlen, nrels = MAX_RESULTS,ret=0;
  374. i2c_real_callback callback = table->extra1;
  375. struct i2c_client *client = table->extra2;
  376. /* Check if we need to output the old values */
  377. if (oldval && oldlenp && !((ret=get_user(oldlen, oldlenp))) && oldlen) {
  378. callback(client, SENSORS_PROC_REAL_READ, table->ctl_name,
  379.  &nrels, results);
  380. /* Note the rounding factor! */
  381. if (nrels * sizeof(long) < oldlen)
  382. oldlen = nrels * sizeof(long);
  383. oldlen = (oldlen / sizeof(long)) * sizeof(long);
  384. if(copy_to_user(oldval, results, oldlen))
  385. return -EFAULT;
  386. if(put_user(oldlen, oldlenp))
  387. return -EFAULT;
  388. }
  389. if (newval && newlen) {
  390. /* Note the rounding factor! */
  391. newlen -= newlen % sizeof(long);
  392. nrels = newlen / sizeof(long);
  393. if(copy_from_user(results, newval, newlen))
  394. return -EFAULT;
  395. /* Get the new values back to the client */
  396. callback(client, SENSORS_PROC_REAL_WRITE, table->ctl_name,
  397.  &nrels, results);
  398. }
  399. return ret;
  400. }
  401. /* nrels contains initially the maximum number of elements which can be
  402.    put in results, and finally the number of elements actually put there.
  403.    A magnitude of 1 will multiply everything with 10; etc.
  404.    buffer, bufsize is the character buffer we read from and its length.
  405.    results will finally contain the parsed integers. 
  406.    Buffer should contain several reals, separated by whitespace. A real
  407.    has the following syntax:
  408.      [ Minus ] Digit* [ Dot Digit* ] 
  409.    (everything between [] is optional; * means zero or more).
  410.    When the next character is unparsable, everything is skipped until the
  411.    next whitespace.
  412.    WARNING! This is tricky code. I have tested it, but there may still be
  413.             hidden bugs in it, even leading to crashes and things!
  414. */
  415. int i2c_parse_reals(int *nrels, void *buffer, int bufsize,
  416.  long *results, int magnitude)
  417. {
  418. int maxels, min, mag;
  419. long res,ret=0;
  420. char nextchar = 0;
  421. maxels = *nrels;
  422. *nrels = 0;
  423. while (bufsize && (*nrels < maxels)) {
  424. /* Skip spaces at the start */
  425. while (bufsize && 
  426.        !((ret=get_user(nextchar, (char *) buffer))) &&
  427.        isspace((int) nextchar)) {
  428. bufsize--;
  429. ((char *) buffer)++;
  430. }
  431. if (ret)
  432. return -EFAULT;
  433. /* Well, we may be done now */
  434. if (!bufsize)
  435. return 0;
  436. /* New defaults for our result */
  437. min = 0;
  438. res = 0;
  439. mag = magnitude;
  440. /* Check for a minus */
  441. if (!((ret=get_user(nextchar, (char *) buffer)))
  442.     && (nextchar == '-')) {
  443. min = 1;
  444. bufsize--;
  445. ((char *) buffer)++;
  446. }
  447. if (ret)
  448. return -EFAULT;
  449. /* Digits before a decimal dot */
  450. while (bufsize && 
  451.        !((ret=get_user(nextchar, (char *) buffer))) &&
  452.        isdigit((int) nextchar)) {
  453. res = res * 10 + nextchar - '0';
  454. bufsize--;
  455. ((char *) buffer)++;
  456. }
  457. if (ret)
  458. return -EFAULT;
  459. /* If mag < 0, we must actually divide here! */
  460. while (mag < 0) {
  461. res = res / 10;
  462. mag++;
  463. }
  464. if (bufsize && (nextchar == '.')) {
  465. /* Skip the dot */
  466. bufsize--;
  467. ((char *) buffer)++;
  468. /* Read digits while they are significant */
  469. while (bufsize && (mag > 0) &&
  470.        !((ret=get_user(nextchar, (char *) buffer))) &&
  471.        isdigit((int) nextchar)) {
  472. res = res * 10 + nextchar - '0';
  473. mag--;
  474. bufsize--;
  475. ((char *) buffer)++;
  476. }
  477. if (ret)
  478. return -EFAULT;
  479. }
  480. /* If we are out of data, but mag > 0, we need to scale here */
  481. while (mag > 0) {
  482. res = res * 10;
  483. mag--;
  484. }
  485. /* Skip everything until we hit whitespace */
  486. while (bufsize && 
  487.        !((ret=get_user(nextchar, (char *) buffer))) &&
  488.        isspace((int) nextchar)) {
  489. bufsize--;
  490. ((char *) buffer)++;
  491. }
  492. if (ret)
  493. return -EFAULT;
  494. /* Put res in results */
  495. results[*nrels] = (min ? -1 : 1) * res;
  496. (*nrels)++;
  497. }
  498. /* Well, there may be more in the buffer, but we need no more data. 
  499.    Ignore anything that is left. */
  500. return 0;
  501. }
  502. int i2c_write_reals(int nrels, void *buffer, int *bufsize,
  503.  long *results, int magnitude)
  504. {
  505. #define BUFLEN 20
  506. char BUF[BUFLEN + 1]; /* An individual representation should fit! */
  507. char printfstr[10];
  508. int nr = 0;
  509. int buflen, mag, times;
  510. int curbufsize = 0;
  511. while ((nr < nrels) && (curbufsize < *bufsize)) {
  512. mag = magnitude;
  513. if (nr != 0) {
  514. if(put_user(' ', (char *) buffer))
  515. return -EFAULT;
  516. curbufsize++;
  517. ((char *) buffer)++;
  518. }
  519. /* Fill BUF with the representation of the next string */
  520. if (mag <= 0) {
  521. buflen = sprintf(BUF, "%ld", results[nr]);
  522. if (buflen < 0) { /* Oops, a sprintf error! */
  523. *bufsize = 0;
  524. return -EINVAL;
  525. }
  526. while ((mag < 0) && (buflen < BUFLEN)) {
  527. BUF[buflen++] = '0';
  528. mag++;
  529. }
  530. BUF[buflen] = 0;
  531. } else {
  532. times = 1;
  533. for (times = 1; mag-- > 0; times *= 10);
  534. if (results[nr] < 0) {
  535. BUF[0] = '-';
  536. buflen = 1;
  537. } else
  538. buflen = 0;
  539. strcpy(printfstr, "%ld.%0Xld");
  540. printfstr[6] = magnitude + '0';
  541. buflen +=
  542.     sprintf(BUF + buflen, printfstr,
  543.     abs(results[nr]) / times,
  544.     abs(results[nr]) % times);
  545. if (buflen < 0) { /* Oops, a sprintf error! */
  546. *bufsize = 0;
  547. return -EINVAL;
  548. }
  549. }
  550. /* Now copy it to the user-space buffer */
  551. if (buflen + curbufsize > *bufsize)
  552. buflen = *bufsize - curbufsize;
  553. if(copy_to_user(buffer, BUF, buflen))
  554. return -EFAULT;
  555. curbufsize += buflen;
  556. (char *) buffer += buflen;
  557. nr++;
  558. }
  559. if (curbufsize < *bufsize) {
  560. if(put_user('n', (char *) buffer))
  561. return -EFAULT;
  562. curbufsize++;
  563. }
  564. *bufsize = curbufsize;
  565. return 0;
  566. }
  567. /* Very inefficient for ISA detects, and won't work for 10-bit addresses! */
  568. int i2c_detect(struct i2c_adapter *adapter,
  569.    struct i2c_address_data *address_data,
  570.    i2c_found_addr_proc * found_proc)
  571. {
  572. int addr, i, found, j, err;
  573. struct i2c_force_data *this_force;
  574. int is_isa = i2c_is_isa_adapter(adapter);
  575. int adapter_id =
  576.     is_isa ? SENSORS_ISA_BUS : i2c_adapter_id(adapter);
  577. /* Forget it if we can't probe using SMBUS_QUICK */
  578. if ((!is_isa)
  579.     && !i2c_check_functionality(adapter,
  580. I2C_FUNC_SMBUS_QUICK)) return -1;
  581. for (addr = 0x00; addr <= (is_isa ? 0xffff : 0x7f); addr++) {
  582. if ((is_isa && check_region(addr, 1)) ||
  583.     (!is_isa && i2c_check_addr(adapter, addr)))
  584. continue;
  585. /* If it is in one of the force entries, we don't do any
  586.    detection at all */
  587. found = 0;
  588. for (i = 0;
  589.      !found
  590.      && (this_force =
  591.  address_data->forces + i, this_force->force); i++) {
  592. for (j = 0;
  593.      !found
  594.      && (this_force->force[j] != SENSORS_I2C_END);
  595.      j += 2) {
  596. if (
  597.     ((adapter_id == this_force->force[j])
  598.      ||
  599.      ((this_force->
  600.        force[j] == SENSORS_ANY_I2C_BUS)
  601.       && !is_isa))
  602.     && (addr == this_force->force[j + 1])) {
  603. #ifdef DEBUG
  604. printk
  605.     ("i2c-proc.o: found force parameter for adapter %d, addr %04xn",
  606.      adapter_id, addr);
  607. #endif
  608. if (
  609.     (err =
  610.      found_proc(adapter, addr, 0,
  611. this_force->
  612. kind))) return err;
  613. found = 1;
  614. }
  615. }
  616. }
  617. if (found)
  618. continue;
  619. /* If this address is in one of the ignores, we can forget about it
  620.    right now */
  621. for (i = 0;
  622.      !found
  623.      && (address_data->ignore[i] != SENSORS_I2C_END);
  624.      i += 2) {
  625. if (
  626.     ((adapter_id == address_data->ignore[i])
  627.      ||
  628.      ((address_data->
  629.        ignore[i] == SENSORS_ANY_I2C_BUS)
  630.       && !is_isa))
  631.     && (addr == address_data->ignore[i + 1])) {
  632. #ifdef DEBUG
  633. printk
  634.     ("i2c-proc.o: found ignore parameter for adapter %d, "
  635.      "addr %04xn", adapter_id, addr);
  636. #endif
  637. found = 1;
  638. }
  639. }
  640. for (i = 0;
  641.      !found
  642.      && (address_data->ignore_range[i] != SENSORS_I2C_END);
  643.      i += 3) {
  644. if (
  645.     ((adapter_id == address_data->ignore_range[i])
  646.      ||
  647.      ((address_data->
  648.        ignore_range[i] ==
  649.        SENSORS_ANY_I2C_BUS) & !is_isa))
  650.     && (addr >= address_data->ignore_range[i + 1])
  651.     && (addr <= address_data->ignore_range[i + 2])) {
  652. #ifdef DEBUG
  653. printk
  654.     ("i2c-proc.o: found ignore_range parameter for adapter %d, "
  655.      "addr %04xn", adapter_id, addr);
  656. #endif
  657. found = 1;
  658. }
  659. }
  660. if (found)
  661. continue;
  662. /* Now, we will do a detection, but only if it is in the normal or 
  663.    probe entries */
  664. if (is_isa) {
  665. for (i = 0;
  666.      !found
  667.      && (address_data->normal_isa[i] !=
  668.  SENSORS_ISA_END); i += 1) {
  669. if (addr == address_data->normal_isa[i]) {
  670. #ifdef DEBUG
  671. printk
  672.     ("i2c-proc.o: found normal isa entry for adapter %d, "
  673.      "addr %04xn", adapter_id,
  674.      addr);
  675. #endif
  676. found = 1;
  677. }
  678. }
  679. for (i = 0;
  680.      !found
  681.      && (address_data->normal_isa_range[i] !=
  682.  SENSORS_ISA_END); i += 3) {
  683. if ((addr >=
  684.      address_data->normal_isa_range[i])
  685.     && (addr <=
  686. address_data->normal_isa_range[i + 1])
  687.     &&
  688.     ((addr -
  689.       address_data->normal_isa_range[i]) %
  690.      address_data->normal_isa_range[i + 2] ==
  691.      0)) {
  692. #ifdef DEBUG
  693. printk
  694.     ("i2c-proc.o: found normal isa_range entry for adapter %d, "
  695.      "addr %04x", adapter_id, addr);
  696. #endif
  697. found = 1;
  698. }
  699. }
  700. } else {
  701. for (i = 0;
  702.      !found && (address_data->normal_i2c[i] !=
  703.  SENSORS_I2C_END); i += 1) {
  704. if (addr == address_data->normal_i2c[i]) {
  705. found = 1;
  706. #ifdef DEBUG
  707. printk
  708.     ("i2c-proc.o: found normal i2c entry for adapter %d, "
  709.      "addr %02x", adapter_id, addr);
  710. #endif
  711. }
  712. }
  713. for (i = 0;
  714.      !found
  715.      && (address_data->normal_i2c_range[i] !=
  716.  SENSORS_I2C_END); i += 2) {
  717. if ((addr >=
  718.      address_data->normal_i2c_range[i])
  719.     && (addr <=
  720. address_data->normal_i2c_range[i + 1]))
  721. {
  722. #ifdef DEBUG
  723. printk
  724.     ("i2c-proc.o: found normal i2c_range entry for adapter %d, "
  725.      "addr %04xn", adapter_id, addr);
  726. #endif
  727. found = 1;
  728. }
  729. }
  730. }
  731. for (i = 0;
  732.      !found && (address_data->probe[i] != SENSORS_I2C_END);
  733.      i += 2) {
  734. if (((adapter_id == address_data->probe[i]) ||
  735.      ((address_data->
  736.        probe[i] == SENSORS_ANY_I2C_BUS) & !is_isa))
  737.     && (addr == address_data->probe[i + 1])) {
  738. #ifdef DEBUG
  739. printk
  740.     ("i2c-proc.o: found probe parameter for adapter %d, "
  741.      "addr %04xn", adapter_id, addr);
  742. #endif
  743. found = 1;
  744. }
  745. }
  746. for (i = 0; !found &&
  747.            (address_data->probe_range[i] != SENSORS_I2C_END);
  748.      i += 3) {
  749. if (
  750.     ((adapter_id == address_data->probe_range[i])
  751.      ||
  752.      ((address_data->probe_range[i] ==
  753.        SENSORS_ANY_I2C_BUS) & !is_isa))
  754.     && (addr >= address_data->probe_range[i + 1])
  755.     && (addr <= address_data->probe_range[i + 2])) {
  756. found = 1;
  757. #ifdef DEBUG
  758. printk
  759.     ("i2c-proc.o: found probe_range parameter for adapter %d, "
  760.      "addr %04xn", adapter_id, addr);
  761. #endif
  762. }
  763. }
  764. if (!found)
  765. continue;
  766. /* OK, so we really should examine this address. First check
  767.    whether there is some client here at all! */
  768. if (is_isa ||
  769.     (i2c_smbus_xfer
  770.      (adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0))
  771. if ((err = found_proc(adapter, addr, 0, -1)))
  772. return err;
  773. }
  774. return 0;
  775. }
  776. int __init sensors_init(void)
  777. {
  778. printk("i2c-proc.o version %s (%s)n", LM_VERSION, LM_DATE);
  779. i2c_initialized = 0;
  780. if (!
  781.     (i2c_proc_header =
  782.      register_sysctl_table(i2c_proc, 0))) return -ENOMEM;
  783. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,1))
  784. i2c_proc_header->ctl_table->child->de->owner = THIS_MODULE;
  785. #else
  786. i2c_proc_header->ctl_table->child->de->fill_inode =
  787.     &i2c_fill_inode;
  788. #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,1)) */
  789. i2c_initialized++;
  790. return 0;
  791. }
  792. EXPORT_SYMBOL(i2c_deregister_entry);
  793. EXPORT_SYMBOL(i2c_detect);
  794. EXPORT_SYMBOL(i2c_proc_real);
  795. EXPORT_SYMBOL(i2c_register_entry);
  796. EXPORT_SYMBOL(i2c_sysctl_real);
  797. #ifdef MODULE
  798. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
  799. MODULE_DESCRIPTION("i2c-proc driver");
  800. MODULE_LICENSE("GPL");
  801. int i2c_cleanup(void)
  802. {
  803. if (i2c_initialized >= 1) {
  804. unregister_sysctl_table(i2c_proc_header);
  805. i2c_initialized--;
  806. }
  807. return 0;
  808. }
  809. int init_module(void)
  810. {
  811. return sensors_init();
  812. }
  813. int cleanup_module(void)
  814. {
  815. return i2c_cleanup();
  816. }
  817. #endif /* MODULE */