root/lm-sensors/trunk/kernel/chips/eeprom.c @ 1269

Revision 1269, 10.9 KB (checked in by mds, 11 years ago)

clean up some confusing (still untested) write code inside #if 0.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    eeprom.c - Part of lm_sensors, Linux kernel modules for hardware
3               monitoring
4    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and
5    Philip Edelbrock <phil@netroedge.com>
6
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.
11
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.
16
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.
20*/
21
22#include <linux/module.h>
23#include <linux/version.h>
24#include <linux/slab.h>
25#include <linux/i2c.h>
26#include "sensors.h"
27#include "version.h"
28#include <linux/init.h>
29
30#ifdef MODULE_LICENSE
31MODULE_LICENSE("GPL");
32#endif
33
34#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,2,18)) || \
35    (LINUX_VERSION_CODE == KERNEL_VERSION(2,3,0))
36#define init_MUTEX(s) do { *(s) = MUTEX; } while(0)
37#endif
38
39#ifndef THIS_MODULE
40#define THIS_MODULE NULL
41#endif
42
43/* Addresses to scan */
44static unsigned short normal_i2c[] = { SENSORS_I2C_END };
45static unsigned short normal_i2c_range[] = { 0x50, 0x57, SENSORS_I2C_END };
46static unsigned int normal_isa[] = { SENSORS_ISA_END };
47static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
48
49/* Insmod parameters */
50SENSORS_INSMOD_1(eeprom);
51
52static int checksum = 0;
53MODULE_PARM(checksum, "i");
54MODULE_PARM_DESC(checksum,
55                 "Only accept eeproms whose checksum is correct");
56
57
58/* Many constants specified below */
59
60/* EEPROM registers */
61#define EEPROM_REG_CHECKSUM 0x3f
62
63/* EEPROM memory types: */
64#define ONE_K           1
65#define TWO_K           2
66#define FOUR_K          3
67#define EIGHT_K         4
68#define SIXTEEN_K       5
69
70/* Conversions */
71/* Size of EEPROM in bytes */
72#define EEPROM_SIZE 128
73
74/* Each client has this additional data */
75struct eeprom_data {
76        int sysctl_id;
77
78        struct semaphore update_lock;
79        char valid;             /* !=0 if following fields are valid */
80        unsigned long last_updated;     /* In jiffies */
81
82        u8 data[EEPROM_SIZE];   /* Register values */
83        int memtype;
84};
85
86#ifdef MODULE
87extern int init_module(void);
88extern int cleanup_module(void);
89#endif                          /* MODULE */
90
91#ifdef MODULE
92static
93#else
94extern
95#endif
96int __init sensors_eeprom_init(void);
97static int __init eeprom_cleanup(void);
98
99static int eeprom_attach_adapter(struct i2c_adapter *adapter);
100static int eeprom_detect(struct i2c_adapter *adapter, int address,
101                         unsigned short flags, int kind);
102static int eeprom_detach_client(struct i2c_client *client);
103static int eeprom_command(struct i2c_client *client, unsigned int cmd,
104                          void *arg);
105
106static void eeprom_inc_use(struct i2c_client *client);
107static void eeprom_dec_use(struct i2c_client *client);
108
109#if 0
110static int eeprom_write_value(struct i2c_client *client, u8 reg,
111                              u8 value);
112#endif
113
114static void eeprom_contents(struct i2c_client *client, int operation,
115                            int ctl_name, int *nrels_mag, long *results);
116static void eeprom_update_client(struct i2c_client *client);
117
118
119/* This is the driver that will be inserted */
120static struct i2c_driver eeprom_driver = {
121        /* name */ "EEPROM READER",
122        /* id */ I2C_DRIVERID_EEPROM,
123        /* flags */ I2C_DF_NOTIFY,
124        /* attach_adapter */ &eeprom_attach_adapter,
125        /* detach_client */ &eeprom_detach_client,
126        /* command */ &eeprom_command,
127        /* inc_use */ &eeprom_inc_use,
128        /* dec_use */ &eeprom_dec_use
129};
130
131/* These files are created for each detected EEPROM. This is just a template;
132   though at first sight, you might think we could use a statically
133   allocated list, we need some way to get back to the parent - which
134   is done through one of the 'extra' fields which are initialized
135   when a new copy is allocated. */
136static ctl_table eeprom_dir_table_template[] = {
137        {EEPROM_SYSCTL1, "data0-15", NULL, 0, 0444, NULL, &i2c_proc_real,
138         &i2c_sysctl_real, NULL, &eeprom_contents},
139        {EEPROM_SYSCTL2, "data16-31", NULL, 0, 0444, NULL, &i2c_proc_real,
140         &i2c_sysctl_real, NULL, &eeprom_contents},
141        {EEPROM_SYSCTL3, "data32-47", NULL, 0, 0444, NULL, &i2c_proc_real,
142         &i2c_sysctl_real, NULL, &eeprom_contents},
143        {EEPROM_SYSCTL4, "data48-63", NULL, 0, 0444, NULL, &i2c_proc_real,
144         &i2c_sysctl_real, NULL, &eeprom_contents},
145        {EEPROM_SYSCTL5, "data64-79", NULL, 0, 0444, NULL, &i2c_proc_real,
146         &i2c_sysctl_real, NULL, &eeprom_contents},
147        {EEPROM_SYSCTL6, "data80-95", NULL, 0, 0444, NULL, &i2c_proc_real,
148         &i2c_sysctl_real, NULL, &eeprom_contents},
149        {EEPROM_SYSCTL7, "data96-111", NULL, 0, 0444, NULL, &i2c_proc_real,
150         &i2c_sysctl_real, NULL, &eeprom_contents},
151        {EEPROM_SYSCTL8, "data112-127", NULL, 0, 0444, NULL, &i2c_proc_real,
152         &i2c_sysctl_real, NULL, &eeprom_contents},
153        {0}
154};
155
156/* Used by init/cleanup */
157static int __initdata eeprom_initialized = 0;
158
159static int eeprom_id = 0;
160
161int eeprom_attach_adapter(struct i2c_adapter *adapter)
162{
163        return i2c_detect(adapter, &addr_data, eeprom_detect);
164}
165
166/* This function is called by i2c_detect */
167int eeprom_detect(struct i2c_adapter *adapter, int address,
168                  unsigned short flags, int kind)
169{
170        int i, cs;
171        struct i2c_client *new_client;
172        struct eeprom_data *data;
173        int err = 0;
174        const char *type_name, *client_name;
175
176        /* Make sure we aren't probing the ISA bus!! This is just a safety check
177           at this moment; i2c_detect really won't call us. */
178#ifdef DEBUG
179        if (i2c_is_isa_adapter(adapter)) {
180                printk
181                    ("eeprom.o: eeprom_detect called for an ISA bus adapter?!?\n");
182                return 0;
183        }
184#endif
185
186        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
187                    goto ERROR0;
188
189        /* OK. For now, we presume we have a valid client. We now create the
190           client structure, even though we cannot fill it completely yet.
191           But it allows us to access eeprom_{read,write}_value. */
192        if (!(new_client = kmalloc(sizeof(struct i2c_client) +
193                                   sizeof(struct eeprom_data),
194                                   GFP_KERNEL))) {
195                err = -ENOMEM;
196                goto ERROR0;
197        }
198
199        data = (struct eeprom_data *) (new_client + 1);
200        new_client->addr = address;
201        new_client->data = data;
202        new_client->adapter = adapter;
203        new_client->driver = &eeprom_driver;
204        new_client->flags = 0;
205
206        /* Now, we do the remaining detection. It is not there, unless you force
207           the checksum to work out. */
208        if (checksum) {
209                cs = 0;
210                for (i = 0; i <= 0x3e; i++)
211                        cs += i2c_smbus_read_byte_data(new_client, i);
212                cs &= 0xff;
213                if (i2c_smbus_read_byte_data
214                    (new_client, EEPROM_REG_CHECKSUM) != cs)
215                        goto ERROR1;
216        }
217
218        /* Determine the chip type - only one kind supported! */
219        if (kind <= 0)
220                kind = eeprom;
221
222        if (kind == eeprom) {
223                type_name = "eeprom";
224                client_name = "EEPROM chip";
225        } else {
226#ifdef DEBUG
227                printk("eeprom.o: Internal error: unknown kind (%d)?!?",
228                       kind);
229#endif
230                goto ERROR1;
231        }
232
233        /* Fill in the remaining client fields and put it into the global list */
234        strcpy(new_client->name, client_name);
235
236        new_client->id = eeprom_id++;
237        data->valid = 0;
238        init_MUTEX(&data->update_lock);
239
240        /* Tell the I2C layer a new client has arrived */
241        if ((err = i2c_attach_client(new_client)))
242                goto ERROR3;
243
244        /* Register a new directory entry with module sensors */
245        if ((i = i2c_register_entry(new_client, type_name,
246                                        eeprom_dir_table_template,
247                                        THIS_MODULE)) < 0) {
248                err = i;
249                goto ERROR4;
250        }
251        data->sysctl_id = i;
252
253        return 0;
254
255/* OK, this is not exactly good programming practice, usually. But it is
256   very code-efficient in this case. */
257
258      ERROR4:
259        i2c_detach_client(new_client);
260      ERROR3:
261      ERROR1:
262        kfree(new_client);
263      ERROR0:
264        return err;
265}
266
267int eeprom_detach_client(struct i2c_client *client)
268{
269        int err;
270
271        i2c_deregister_entry(((struct eeprom_data *) (client->data))->
272                                 sysctl_id);
273
274        if ((err = i2c_detach_client(client))) {
275                printk
276                    ("eeprom.o: Client deregistration failed, client not detached.\n");
277                return err;
278        }
279
280        kfree(client);
281
282        return 0;
283}
284
285
286/* No commands defined yet */
287int eeprom_command(struct i2c_client *client, unsigned int cmd, void *arg)
288{
289        return 0;
290}
291
292void eeprom_inc_use(struct i2c_client *client)
293{
294#ifdef MODULE
295        MOD_INC_USE_COUNT;
296#endif
297}
298
299void eeprom_dec_use(struct i2c_client *client)
300{
301#ifdef MODULE
302        MOD_DEC_USE_COUNT;
303#endif
304}
305
306#if 0
307/* No writes yet (PAE) */
308int eeprom_write_value(struct i2c_client *client, u8 reg, u8 value)
309{
310        return i2c_smbus_write_byte_data(client, reg, value);
311}
312#endif
313
314void eeprom_update_client(struct i2c_client *client)
315{
316        struct eeprom_data *data = client->data;
317        int i;
318
319        down(&data->update_lock);
320
321        if ((jiffies - data->last_updated > 300 * HZ) |
322            (jiffies < data->last_updated) || !data->valid) {
323
324#ifdef DEBUG
325                printk("Starting eeprom update\n");
326#endif
327
328                if (i2c_smbus_write_byte(client, 0)) {
329#ifdef DEBUG
330                        printk("eeprom read start has failed!\n");
331#endif
332                }
333                for (i = 0; i < EEPROM_SIZE; i++) {
334                        data->data[i] = (u8) i2c_smbus_read_byte(client);
335                }
336
337                data->last_updated = jiffies;
338                data->valid = 1;
339        }
340
341        up(&data->update_lock);
342}
343
344
345void eeprom_contents(struct i2c_client *client, int operation,
346                     int ctl_name, int *nrels_mag, long *results)
347{
348        int i;
349        int base = 0;
350        struct eeprom_data *data = client->data;
351
352        if (ctl_name == EEPROM_SYSCTL2) {
353                base = 16;
354        }
355        if (ctl_name == EEPROM_SYSCTL3) {
356                base = 32;
357        }
358        if (ctl_name == EEPROM_SYSCTL4) {
359                base = 48;
360        }
361        if (ctl_name == EEPROM_SYSCTL5) {
362                base = 64;
363        }
364        if (ctl_name == EEPROM_SYSCTL6) {
365                base = 80;
366        }
367        if (ctl_name == EEPROM_SYSCTL7) {
368                base = 96;
369        }
370        if (ctl_name == EEPROM_SYSCTL8) {
371                base = 112;
372        }
373
374        if (operation == SENSORS_PROC_REAL_INFO)
375                *nrels_mag = 0;
376        else if (operation == SENSORS_PROC_REAL_READ) {
377                eeprom_update_client(client);
378                for (i = 0; i < 16; i++) {
379                        results[i] = data->data[i + base];
380                }
381#ifdef DEBUG
382                printk("eeprom.o: 0x%X EEPROM Contents (base %d): ",
383                       client->addr, base);
384                for (i = 0; i < 16; i++) {
385                        printk(" 0x%X", data->data[i + base]);
386                }
387                printk(" .\n");
388#endif
389                *nrels_mag = 16;
390        } else if (operation == SENSORS_PROC_REAL_WRITE) {
391
392/* No writes to the EEPROM (yet, anyway) (PAE) */
393                printk("eeprom.o: No writes to EEPROMs supported!\n");
394        }
395}
396
397int __init sensors_eeprom_init(void)
398{
399        int res;
400
401        printk("eeprom.o version %s (%s)\n", LM_VERSION, LM_DATE);
402        eeprom_initialized = 0;
403        if ((res = i2c_add_driver(&eeprom_driver))) {
404                printk
405                    ("eeprom.o: Driver registration failed, module not inserted.\n");
406                eeprom_cleanup();
407                return res;
408        }
409        eeprom_initialized++;
410        return 0;
411}
412
413int __init eeprom_cleanup(void)
414{
415        int res;
416
417        if (eeprom_initialized >= 1) {
418                if ((res = i2c_del_driver(&eeprom_driver))) {
419                        printk
420                            ("eeprom.o: Driver deregistration failed, module not removed.\n");
421                        return res;
422                }
423        } else
424                eeprom_initialized--;
425
426        return 0;
427}
428
429EXPORT_NO_SYMBOLS;
430
431#ifdef MODULE
432
433MODULE_AUTHOR
434    ("Frodo Looijaard <frodol@dds.nl> and Philip Edelbrock <phil@netroedge.com>");
435MODULE_DESCRIPTION("EEPROM driver");
436
437int init_module(void)
438{
439        return sensors_eeprom_init();
440}
441
442int cleanup_module(void)
443{
444        return eeprom_cleanup();
445}
446
447#endif                          /* MODULE */
Note: See TracBrowser for help on using the browser.