blob: fde297b21ad72b8dc36eddcc2d2e65ae0f5b4954 [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 .id = I2C_DRIVERID_EEPROM,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .attach_adapter = eeprom_attach_adapter,
76 .detach_client = eeprom_detach_client,
77};
78
79static void eeprom_update_client(struct i2c_client *client, u8 slice)
80{
81 struct eeprom_data *data = i2c_get_clientdata(client);
82 int i, j;
83
Ingo Molnar5c085d32006-01-18 23:16:04 +010084 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (!(data->valid & (1 << slice)) ||
87 time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
88 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
89
90 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
Jean Delvare4b2643d2007-07-12 14:12:29 +020091 for (i = slice << 5; i < (slice + 1) << 5; i += 32)
92 if (i2c_smbus_read_i2c_block_data(client, i,
93 32, data->data + i)
94 != 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto exit;
96 } else {
97 if (i2c_smbus_write_byte(client, slice << 5)) {
98 dev_dbg(&client->dev, "eeprom read start has failed!\n");
99 goto exit;
100 }
101 for (i = slice << 5; i < (slice + 1) << 5; i++) {
102 j = i2c_smbus_read_byte(client);
103 if (j < 0)
104 goto exit;
105 data->data[i] = (u8) j;
106 }
107 }
108 data->last_updated[slice] = jiffies;
109 data->valid |= (1 << slice);
110 }
111exit:
Ingo Molnar5c085d32006-01-18 23:16:04 +0100112 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Zhang Rui91a69022007-06-09 13:57:22 +0800115static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
116 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
119 struct eeprom_data *data = i2c_get_clientdata(client);
120 u8 slice;
121
122 if (off > EEPROM_SIZE)
123 return 0;
124 if (off + count > EEPROM_SIZE)
125 count = EEPROM_SIZE - off;
126
127 /* Only refresh slices which contain requested bytes */
128 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
129 eeprom_update_client(client, slice);
130
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100131 /* Hide Vaio private settings to regular users:
132 - BIOS passwords: bytes 0x00 to 0x0f
133 - UUID: bytes 0x10 to 0x1f
134 - Serial number: 0xc0 to 0xdf */
135 if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
136 int i;
137
138 for (i = 0; i < count; i++) {
139 if ((off + i <= 0x1f) ||
140 (off + i >= 0xc0 && off + i <= 0xdf))
141 buf[i] = 0;
142 else
143 buf[i] = data->data[off + i];
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 } else {
146 memcpy(buf, &data->data[off], count);
147 }
148
149 return count;
150}
151
152static struct bin_attribute eeprom_attr = {
153 .attr = {
154 .name = "eeprom",
155 .mode = S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 },
157 .size = EEPROM_SIZE,
158 .read = eeprom_read,
159};
160
161static int eeprom_attach_adapter(struct i2c_adapter *adapter)
162{
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200163 return i2c_probe(adapter, &addr_data, eeprom_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200166/* This function is called by i2c_probe */
Ben Dooks6536c492005-10-26 21:04:12 +0200167static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct i2c_client *new_client;
170 struct eeprom_data *data;
171 int err = 0;
172
173 /* There are three ways we can read the EEPROM data:
174 (1) I2C block reads (faster, but unsupported by most adapters)
175 (2) Consecutive byte reads (100% overhead)
176 (3) Regular byte data reads (200% overhead)
177 The third method is not implemented by this driver because all
178 known adapters support at least the second. */
179 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA
180 | I2C_FUNC_SMBUS_BYTE))
181 goto exit;
182
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200183 if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 err = -ENOMEM;
185 goto exit;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 new_client = &data->client;
189 memset(data->data, 0xff, EEPROM_SIZE);
190 i2c_set_clientdata(new_client, data);
191 new_client->addr = address;
192 new_client->adapter = adapter;
193 new_client->driver = &eeprom_driver;
194 new_client->flags = 0;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Fill in the remaining client fields */
197 strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
198 data->valid = 0;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100199 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 data->nature = UNKNOWN;
201
202 /* Tell the I2C layer a new client has arrived */
203 if ((err = i2c_attach_client(new_client)))
204 goto exit_kfree;
205
206 /* Detect the Vaio nature of EEPROMs.
Jean Delvare8b925a32007-11-15 19:24:03 +0100207 We use the "PCG-" or "VGN-" prefix as the signature. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (address == 0x57) {
Jean Delvare8b925a32007-11-15 19:24:03 +0100209 char name[4];
210
211 name[0] = i2c_smbus_read_byte_data(new_client, 0x80);
212 name[1] = i2c_smbus_read_byte(new_client);
213 name[2] = i2c_smbus_read_byte(new_client);
214 name[3] = i2c_smbus_read_byte(new_client);
215
216 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dev_info(&new_client->dev, "Vaio EEPROM detected, "
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100218 "enabling privacy protection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 data->nature = VAIO;
220 }
221 }
222
223 /* create the sysfs eeprom file */
Jean Delvare7d9db672006-09-03 22:20:24 +0200224 err = sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
225 if (err)
226 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 return 0;
229
Jean Delvare7d9db672006-09-03 22:20:24 +0200230exit_detach:
231 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232exit_kfree:
233 kfree(data);
234exit:
235 return err;
236}
237
238static int eeprom_detach_client(struct i2c_client *client)
239{
240 int err;
241
Jean Delvare7d9db672006-09-03 22:20:24 +0200242 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 err = i2c_detach_client(client);
Jean Delvare7bef5592005-07-27 22:14:49 +0200245 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 kfree(i2c_get_clientdata(client));
249
250 return 0;
251}
252
253static int __init eeprom_init(void)
254{
255 return i2c_add_driver(&eeprom_driver);
256}
257
258static void __exit eeprom_exit(void)
259{
260 i2c_del_driver(&eeprom_driver);
261}
262
263
264MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
265 "Philip Edelbrock <phil@netroedge.com> and "
266 "Greg Kroah-Hartman <greg@kroah.com>");
267MODULE_DESCRIPTION("I2C EEPROM driver");
268MODULE_LICENSE("GPL");
269
270module_init(eeprom_init);
271module_exit(eeprom_exit);