blob: 213a9f98decc8048c6cb684d207c9f853cb01755 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/jiffies.h>
34#include <linux/i2c.h>
Ingo Molnar5c085d32006-01-18 23:16:04 +010035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Addresses to scan */
Jean Delvare2cdddeb2008-01-27 18:14:47 +010038static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 0x55, 0x56, 0x57, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020042I2C_CLIENT_INSMOD_1(eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44
45/* Size of EEPROM in bytes */
46#define EEPROM_SIZE 256
47
48/* possible types of eeprom devices */
49enum eeprom_nature {
50 UNKNOWN,
51 VAIO,
52};
53
54/* Each client has this additional data */
55struct eeprom_data {
56 struct i2c_client client;
Ingo Molnar5c085d32006-01-18 23:16:04 +010057 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 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
65static int eeprom_attach_adapter(struct i2c_adapter *adapter);
66static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
67static int eeprom_detach_client(struct i2c_client *client);
68
69/* This is the driver that will be inserted */
70static struct i2c_driver eeprom_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010071 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010072 .name = "eeprom",
73 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .attach_adapter = eeprom_attach_adapter,
75 .detach_client = eeprom_detach_client,
76};
77
78static void eeprom_update_client(struct i2c_client *client, u8 slice)
79{
80 struct eeprom_data *data = i2c_get_clientdata(client);
81 int i, j;
82
Ingo Molnar5c085d32006-01-18 23:16:04 +010083 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
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)) {
Jean Delvare4b2643d2007-07-12 14:12:29 +020090 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 goto exit;
95 } else {
96 if (i2c_smbus_write_byte(client, slice << 5)) {
97 dev_dbg(&client->dev, "eeprom read start has failed!\n");
98 goto exit;
99 }
100 for (i = slice << 5; i < (slice + 1) << 5; i++) {
101 j = i2c_smbus_read_byte(client);
102 if (j < 0)
103 goto exit;
104 data->data[i] = (u8) j;
105 }
106 }
107 data->last_updated[slice] = jiffies;
108 data->valid |= (1 << slice);
109 }
110exit:
Ingo Molnar5c085d32006-01-18 23:16:04 +0100111 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Zhang Rui91a69022007-06-09 13:57:22 +0800114static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
115 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
118 struct eeprom_data *data = i2c_get_clientdata(client);
119 u8 slice;
120
121 if (off > EEPROM_SIZE)
122 return 0;
123 if (off + count > EEPROM_SIZE)
124 count = EEPROM_SIZE - off;
125
126 /* Only refresh slices which contain requested bytes */
127 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
128 eeprom_update_client(client, slice);
129
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100130 /* Hide Vaio private settings to regular users:
131 - BIOS passwords: bytes 0x00 to 0x0f
132 - UUID: bytes 0x10 to 0x1f
133 - Serial number: 0xc0 to 0xdf */
134 if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
135 int i;
136
137 for (i = 0; i < count; i++) {
138 if ((off + i <= 0x1f) ||
139 (off + i >= 0xc0 && off + i <= 0xdf))
140 buf[i] = 0;
141 else
142 buf[i] = data->data[off + i];
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 } else {
145 memcpy(buf, &data->data[off], count);
146 }
147
148 return count;
149}
150
151static struct bin_attribute eeprom_attr = {
152 .attr = {
153 .name = "eeprom",
154 .mode = S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 },
156 .size = EEPROM_SIZE,
157 .read = eeprom_read,
158};
159
160static int eeprom_attach_adapter(struct i2c_adapter *adapter)
161{
Jean Delvared4653bf2008-07-14 22:38:29 +0200162 if (!(adapter->class & (I2C_CLASS_DDC | I2C_CLASS_SPD)))
163 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200164 return i2c_probe(adapter, &addr_data, eeprom_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200167/* This function is called by i2c_probe */
Ben Dooks6536c492005-10-26 21:04:12 +0200168static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct i2c_client *new_client;
171 struct eeprom_data *data;
172 int err = 0;
173
Jean Delvared4653bf2008-07-14 22:38:29 +0200174 /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
175 addresses 0x50-0x57, but we only care about 0x50. So decline
176 attaching to addresses >= 0x51 on DDC buses */
177 if (!(adapter->class & I2C_CLASS_SPD) && address >= 0x51)
178 goto exit;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* There are three ways we can read the EEPROM data:
181 (1) I2C block reads (faster, but unsupported by most adapters)
182 (2) Consecutive byte reads (100% overhead)
183 (3) Regular byte data reads (200% overhead)
184 The third method is not implemented by this driver because all
185 known adapters support at least the second. */
186 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA
187 | I2C_FUNC_SMBUS_BYTE))
188 goto exit;
189
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200190 if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 err = -ENOMEM;
192 goto exit;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 new_client = &data->client;
196 memset(data->data, 0xff, EEPROM_SIZE);
197 i2c_set_clientdata(new_client, data);
198 new_client->addr = address;
199 new_client->adapter = adapter;
200 new_client->driver = &eeprom_driver;
201 new_client->flags = 0;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Fill in the remaining client fields */
204 strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
205 data->valid = 0;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100206 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 data->nature = UNKNOWN;
208
209 /* Tell the I2C layer a new client has arrived */
210 if ((err = i2c_attach_client(new_client)))
211 goto exit_kfree;
212
213 /* Detect the Vaio nature of EEPROMs.
Jean Delvare8b925a32007-11-15 19:24:03 +0100214 We use the "PCG-" or "VGN-" prefix as the signature. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (address == 0x57) {
Jean Delvare8b925a32007-11-15 19:24:03 +0100216 char name[4];
217
218 name[0] = i2c_smbus_read_byte_data(new_client, 0x80);
219 name[1] = i2c_smbus_read_byte(new_client);
220 name[2] = i2c_smbus_read_byte(new_client);
221 name[3] = i2c_smbus_read_byte(new_client);
222
223 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dev_info(&new_client->dev, "Vaio EEPROM detected, "
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100225 "enabling privacy protection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 data->nature = VAIO;
227 }
228 }
229
230 /* create the sysfs eeprom file */
Jean Delvare7d9db672006-09-03 22:20:24 +0200231 err = sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
232 if (err)
233 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 return 0;
236
Jean Delvare7d9db672006-09-03 22:20:24 +0200237exit_detach:
238 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239exit_kfree:
240 kfree(data);
241exit:
242 return err;
243}
244
245static int eeprom_detach_client(struct i2c_client *client)
246{
247 int err;
248
Jean Delvare7d9db672006-09-03 22:20:24 +0200249 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 err = i2c_detach_client(client);
Jean Delvare7bef5592005-07-27 22:14:49 +0200252 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 kfree(i2c_get_clientdata(client));
256
257 return 0;
258}
259
260static int __init eeprom_init(void)
261{
262 return i2c_add_driver(&eeprom_driver);
263}
264
265static void __exit eeprom_exit(void)
266{
267 i2c_del_driver(&eeprom_driver);
268}
269
270
271MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
272 "Philip Edelbrock <phil@netroedge.com> and "
273 "Greg Kroah-Hartman <greg@kroah.com>");
274MODULE_DESCRIPTION("I2C EEPROM driver");
275MODULE_LICENSE("GPL");
276
277module_init(eeprom_init);
278module_exit(eeprom_exit);