]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/eeprom.c
9a81252a72181e8389d7c848d41c4a1d74c6c210
[linux-2.6-omap-h63xx.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/kernel.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/jiffies.h>
34 #include <linux/i2c.h>
35 #include <linux/mutex.h>
36
37 /* Addresses to scan */
38 static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
39                                         0x55, 0x56, 0x57, I2C_CLIENT_END };
40
41 /* Insmod parameters */
42 I2C_CLIENT_INSMOD_1(eeprom);
43
44
45 /* Size of EEPROM in bytes */
46 #define EEPROM_SIZE             256
47
48 /* possible types of eeprom devices */
49 enum eeprom_nature {
50         UNKNOWN,
51         VAIO,
52 };
53
54 /* Each client has this additional data */
55 struct eeprom_data {
56         struct i2c_client client;
57         struct mutex update_lock;
58         u8 valid;                       /* bitfield, bit!=0 if slice is valid */
59         unsigned long last_updated[8];  /* In jiffies, 8 slices */
60         u8 data[EEPROM_SIZE];           /* Register values */
61         enum eeprom_nature nature;
62 };
63
64
65 static int eeprom_attach_adapter(struct i2c_adapter *adapter);
66 static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
67 static int eeprom_detach_client(struct i2c_client *client);
68
69 /* This is the driver that will be inserted */
70 static struct i2c_driver eeprom_driver = {
71         .driver = {
72                 .name   = "eeprom",
73         },
74         .attach_adapter = eeprom_attach_adapter,
75         .detach_client  = eeprom_detach_client,
76 };
77
78 static void eeprom_update_client(struct i2c_client *client, u8 slice)
79 {
80         struct eeprom_data *data = i2c_get_clientdata(client);
81         int i;
82
83         mutex_lock(&data->update_lock);
84
85         if (!(data->valid & (1 << slice)) ||
86             time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
87                 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
88
89                 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
90                         for (i = slice << 5; i < (slice + 1) << 5; i += 32)
91                                 if (i2c_smbus_read_i2c_block_data(client, i,
92                                                         32, data->data + i)
93                                                         != 32)
94                                         goto exit;
95                 } else {
96                         for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
97                                 int word = i2c_smbus_read_word_data(client, i);
98                                 if (word < 0)
99                                         goto exit;
100                                 data->data[i] = word & 0xff;
101                                 data->data[i + 1] = word >> 8;
102                         }
103                 }
104                 data->last_updated[slice] = jiffies;
105                 data->valid |= (1 << slice);
106         }
107 exit:
108         mutex_unlock(&data->update_lock);
109 }
110
111 static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
112                            char *buf, loff_t off, size_t count)
113 {
114         struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
115         struct eeprom_data *data = i2c_get_clientdata(client);
116         u8 slice;
117
118         if (off > EEPROM_SIZE)
119                 return 0;
120         if (off + count > EEPROM_SIZE)
121                 count = EEPROM_SIZE - off;
122
123         /* Only refresh slices which contain requested bytes */
124         for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
125                 eeprom_update_client(client, slice);
126
127         /* Hide Vaio private settings to regular users:
128            - BIOS passwords: bytes 0x00 to 0x0f
129            - UUID: bytes 0x10 to 0x1f
130            - Serial number: 0xc0 to 0xdf */
131         if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
132                 int i;
133
134                 for (i = 0; i < count; i++) {
135                         if ((off + i <= 0x1f) ||
136                             (off + i >= 0xc0 && off + i <= 0xdf))
137                                 buf[i] = 0;
138                         else
139                                 buf[i] = data->data[off + i];
140                 }
141         } else {
142                 memcpy(buf, &data->data[off], count);
143         }
144
145         return count;
146 }
147
148 static struct bin_attribute eeprom_attr = {
149         .attr = {
150                 .name = "eeprom",
151                 .mode = S_IRUGO,
152         },
153         .size = EEPROM_SIZE,
154         .read = eeprom_read,
155 };
156
157 static int eeprom_attach_adapter(struct i2c_adapter *adapter)
158 {
159         if (!(adapter->class & (I2C_CLASS_DDC | I2C_CLASS_SPD)))
160                 return 0;
161         return i2c_probe(adapter, &addr_data, eeprom_detect);
162 }
163
164 /* This function is called by i2c_probe */
165 static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
166 {
167         struct i2c_client *new_client;
168         struct eeprom_data *data;
169         int err = 0;
170
171         /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
172            addresses 0x50-0x57, but we only care about 0x50. So decline
173            attaching to addresses >= 0x51 on DDC buses */
174         if (!(adapter->class & I2C_CLASS_SPD) && address >= 0x51)
175                 goto exit;
176
177         /* There are four ways we can read the EEPROM data:
178            (1) I2C block reads (faster, but unsupported by most adapters)
179            (2) Word reads (128% overhead)
180            (3) Consecutive byte reads (88% overhead, unsafe)
181            (4) Regular byte data reads (265% overhead)
182            The third and fourth methods are not implemented by this driver
183            because all known adapters support one of the first two. */
184         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
185          && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
186                 goto exit;
187
188         if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
189                 err = -ENOMEM;
190                 goto exit;
191         }
192
193         new_client = &data->client;
194         memset(data->data, 0xff, EEPROM_SIZE);
195         i2c_set_clientdata(new_client, data);
196         new_client->addr = address;
197         new_client->adapter = adapter;
198         new_client->driver = &eeprom_driver;
199         new_client->flags = 0;
200
201         /* Fill in the remaining client fields */
202         strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
203         data->valid = 0;
204         mutex_init(&data->update_lock);
205         data->nature = UNKNOWN;
206
207         /* Tell the I2C layer a new client has arrived */
208         if ((err = i2c_attach_client(new_client)))
209                 goto exit_kfree;
210
211         /* Detect the Vaio nature of EEPROMs.
212            We use the "PCG-" or "VGN-" prefix as the signature. */
213         if (address == 0x57
214          && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
215                 char name[4];
216
217                 name[0] = i2c_smbus_read_byte_data(new_client, 0x80);
218                 name[1] = i2c_smbus_read_byte_data(new_client, 0x81);
219                 name[2] = i2c_smbus_read_byte_data(new_client, 0x82);
220                 name[3] = i2c_smbus_read_byte_data(new_client, 0x83);
221
222                 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
223                         dev_info(&new_client->dev, "Vaio EEPROM detected, "
224                                  "enabling privacy protection\n");
225                         data->nature = VAIO;
226                 }
227         }
228
229         /* create the sysfs eeprom file */
230         err = sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
231         if (err)
232                 goto exit_detach;
233
234         return 0;
235
236 exit_detach:
237         i2c_detach_client(new_client);
238 exit_kfree:
239         kfree(data);
240 exit:
241         return err;
242 }
243
244 static int eeprom_detach_client(struct i2c_client *client)
245 {
246         int err;
247
248         sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
249
250         err = i2c_detach_client(client);
251         if (err)
252                 return err;
253
254         kfree(i2c_get_clientdata(client));
255
256         return 0;
257 }
258
259 static int __init eeprom_init(void)
260 {
261         return i2c_add_driver(&eeprom_driver);
262 }
263
264 static void __exit eeprom_exit(void)
265 {
266         i2c_del_driver(&eeprom_driver);
267 }
268
269
270 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
271                 "Philip Edelbrock <phil@netroedge.com> and "
272                 "Greg Kroah-Hartman <greg@kroah.com>");
273 MODULE_DESCRIPTION("I2C EEPROM driver");
274 MODULE_LICENSE("GPL");
275
276 module_init(eeprom_init);
277 module_exit(eeprom_exit);