patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / eeprom.c
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     Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7     Copyright (C) 2003 IBM Corp.
8
9     2004-01-16  Jean Delvare <khali@linux-fr.org>
10     Divide the eeprom in 32-byte (arbitrary) slices. This significantly
11     speeds sensors up, as well as various scripts using the eeprom
12     module.
13
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License as published by
16     the Free Software Foundation; either version 2 of the License, or
17     (at your option) any later version.
18
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     GNU General Public License for more details.
23
24     You should have received a copy of the GNU General Public License
25     along with this program; if not, write to the Free Software
26     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29 #include <linux/config.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/sched.h>
35 #include <linux/i2c.h>
36 #include <linux/i2c-sensor.h>
37
38 /* Addresses to scan */
39 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
40 static unsigned short normal_i2c_range[] = { 0x50, 0x57, I2C_CLIENT_END };
41 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
42 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
43
44 /* Insmod parameters */
45 SENSORS_INSMOD_1(eeprom);
46
47 static int checksum = 0;
48 MODULE_PARM(checksum, "i");
49 MODULE_PARM_DESC(checksum, "Only accept eeproms whose checksum is correct");
50
51
52 /* EEPROM registers */
53 #define EEPROM_REG_CHECKSUM     0x3f
54
55 /* Size of EEPROM in bytes */
56 #define EEPROM_SIZE             256
57
58 /* possible types of eeprom devices */
59 enum eeprom_nature {
60         UNKNOWN,
61         VAIO,
62 };
63
64 /* Each client has this additional data */
65 struct eeprom_data {
66         struct i2c_client client;
67         struct semaphore update_lock;
68         u8 valid;                       /* bitfield, bit!=0 if slice is valid */
69         unsigned long last_updated[8];  /* In jiffies, 8 slices */
70         u8 data[EEPROM_SIZE];           /* Register values */
71         enum eeprom_nature nature;
72 };
73
74
75 static int eeprom_attach_adapter(struct i2c_adapter *adapter);
76 static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
77 static int eeprom_detach_client(struct i2c_client *client);
78
79 /* This is the driver that will be inserted */
80 static struct i2c_driver eeprom_driver = {
81         .owner          = THIS_MODULE,
82         .name           = "eeprom",
83         .id             = I2C_DRIVERID_EEPROM,
84         .flags          = I2C_DF_NOTIFY,
85         .attach_adapter = eeprom_attach_adapter,
86         .detach_client  = eeprom_detach_client,
87 };
88
89 static int eeprom_id = 0;
90
91 static void eeprom_update_client(struct i2c_client *client, u8 slice)
92 {
93         struct eeprom_data *data = i2c_get_clientdata(client);
94         int i, j;
95
96         down(&data->update_lock);
97
98         if (!(data->valid & (1 << slice)) ||
99             (jiffies - data->last_updated[slice] > 300 * HZ) ||
100             (jiffies < data->last_updated[slice])) {
101                 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
102
103                 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
104                         for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_I2C_BLOCK_MAX)
105                                 if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_I2C_BLOCK_MAX)
106                                         goto exit;
107                 } else {
108                         if (i2c_smbus_write_byte(client, slice << 5)) {
109                                 dev_dbg(&client->dev, "eeprom read start has failed!\n");
110                                 goto exit;
111                         }
112                         for (i = slice << 5; i < (slice + 1) << 5; i++) {
113                                 j = i2c_smbus_read_byte(client);
114                                 if (j < 0)
115                                         goto exit;
116                                 data->data[i] = (u8) j;
117                         }
118                 }
119                 data->last_updated[slice] = jiffies;
120                 data->valid |= (1 << slice);
121         }
122 exit:
123         up(&data->update_lock);
124 }
125
126 static ssize_t eeprom_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
127 {
128         struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
129         struct eeprom_data *data = i2c_get_clientdata(client);
130         u8 slice;
131
132         if (off > EEPROM_SIZE)
133                 return 0;
134         if (off + count > EEPROM_SIZE)
135                 count = EEPROM_SIZE - off;
136
137         /* Only refresh slices which contain requested bytes */
138         for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
139                 eeprom_update_client(client, slice);
140
141         /* Hide Vaio security settings to regular users (16 first bytes) */
142         if (data->nature == VAIO && off < 16 && !capable(CAP_SYS_ADMIN)) {
143                 int in_row1 = 16 - off;
144                 memset(buf, 0, in_row1);
145                 if (count - in_row1 > 0)
146                         memcpy(buf + in_row1, &data->data[16], count - in_row1);
147         } else {
148                 memcpy(buf, &data->data[off], count);
149         }
150
151         return count;
152 }
153
154 static struct bin_attribute eeprom_attr = {
155         .attr = {
156                 .name = "eeprom",
157                 .mode = S_IRUGO,
158                 .owner = THIS_MODULE,
159         },
160         .size = EEPROM_SIZE,
161         .read = eeprom_read,
162 };
163
164 static int eeprom_attach_adapter(struct i2c_adapter *adapter)
165 {
166         return i2c_detect(adapter, &addr_data, eeprom_detect);
167 }
168
169 /* This function is called by i2c_detect */
170 int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
171 {
172         int i, cs;
173         struct i2c_client *new_client;
174         struct eeprom_data *data;
175         int err = 0;
176
177         /* Make sure we aren't probing the ISA bus!! This is just a safety check
178            at this moment; i2c_detect really won't call us. */
179 #ifdef DEBUG
180         if (i2c_is_isa_adapter(adapter)) {
181                 dev_dbg(&adapter->dev, " 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 exit;
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 (!(data = kmalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
193                 err = -ENOMEM;
194                 goto exit;
195         }
196         memset(data, 0, sizeof(struct eeprom_data));
197
198         new_client = &data->client;
199         memset(data->data, 0xff, EEPROM_SIZE);
200         i2c_set_clientdata(new_client, data);
201         new_client->addr = address;
202         new_client->adapter = adapter;
203         new_client->driver = &eeprom_driver;
204         new_client->flags = 0;
205
206         /* prevent 24RF08 corruption */
207         i2c_smbus_write_quick(new_client, 0);
208
209         /* Now, we do the remaining detection. It is not there, unless you force
210            the checksum to work out. */
211         if (checksum) {
212                 cs = 0;
213                 for (i = 0; i <= 0x3e; i++)
214                         cs += i2c_smbus_read_byte_data(new_client, i);
215                 cs &= 0xff;
216                 if (i2c_smbus_read_byte_data (new_client, EEPROM_REG_CHECKSUM) != cs)
217                         goto exit_kfree;
218         }
219
220         data->nature = UNKNOWN;
221         /* Detect the Vaio nature of EEPROMs.
222            We use the "PCG-" prefix as the signature. */
223         if (address == 0x57) {
224                 if (i2c_smbus_read_byte_data(new_client, 0x80) == 'P' && 
225                     i2c_smbus_read_byte_data(new_client, 0x81) == 'C' && 
226                     i2c_smbus_read_byte_data(new_client, 0x82) == 'G' &&
227                     i2c_smbus_read_byte_data(new_client, 0x83) == '-')
228                         data->nature = VAIO;
229         }
230
231         /* Fill in the remaining client fields */
232         strncpy(new_client->name, "eeprom", I2C_NAME_SIZE);
233         new_client->id = eeprom_id++;
234         data->valid = 0;
235         init_MUTEX(&data->update_lock);
236
237         /* Tell the I2C layer a new client has arrived */
238         if ((err = i2c_attach_client(new_client)))
239                 goto exit_kfree;
240
241         /* create the sysfs eeprom file */
242         sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
243
244         return 0;
245
246 exit_kfree:
247         kfree(data);
248 exit:
249         return err;
250 }
251
252 static int eeprom_detach_client(struct i2c_client *client)
253 {
254         int err;
255
256         err = i2c_detach_client(client);
257         if (err) {
258                 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
259                 return err;
260         }
261
262         kfree(i2c_get_clientdata(client));
263
264         return 0;
265 }
266
267 static int __init eeprom_init(void)
268 {
269         return i2c_add_driver(&eeprom_driver);
270 }
271
272 static void __exit eeprom_exit(void)
273 {
274         i2c_del_driver(&eeprom_driver);
275 }
276
277
278 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
279                 "Philip Edelbrock <phil@netroedge.com> and "
280                 "Greg Kroah-Hartman <greg@kroah.com>");
281 MODULE_DESCRIPTION("I2C EEPROM driver");
282 MODULE_LICENSE("GPL");
283
284 module_init(eeprom_init);
285 module_exit(eeprom_exit);