blob: e22ec3b3aedf9f2f2e6adf98d2896b838ddaa58f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
3 Philip Edelbrock <phil@netroedge.com>
4 Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 Copyright (C) 2003 IBM Corp.
Jean Delvare954a9932008-07-14 22:38:34 +02006 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/jiffies.h>
28#include <linux/i2c.h>
Ingo Molnar5c085d32006-01-18 23:16:04 +010029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* Addresses to scan */
Jean Delvare2cdddeb2008-01-27 18:14:47 +010032static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 0x55, 0x56, 0x57, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020036I2C_CLIENT_INSMOD_1(eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
39/* Size of EEPROM in bytes */
40#define EEPROM_SIZE 256
41
42/* possible types of eeprom devices */
43enum eeprom_nature {
44 UNKNOWN,
45 VAIO,
46};
47
48/* Each client has this additional data */
49struct eeprom_data {
50 struct i2c_client client;
Ingo Molnar5c085d32006-01-18 23:16:04 +010051 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 u8 valid; /* bitfield, bit!=0 if slice is valid */
53 unsigned long last_updated[8]; /* In jiffies, 8 slices */
54 u8 data[EEPROM_SIZE]; /* Register values */
55 enum eeprom_nature nature;
56};
57
58
59static int eeprom_attach_adapter(struct i2c_adapter *adapter);
60static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
61static int eeprom_detach_client(struct i2c_client *client);
62
63/* This is the driver that will be inserted */
64static struct i2c_driver eeprom_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010065 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010066 .name = "eeprom",
67 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .attach_adapter = eeprom_attach_adapter,
69 .detach_client = eeprom_detach_client,
70};
71
72static void eeprom_update_client(struct i2c_client *client, u8 slice)
73{
74 struct eeprom_data *data = i2c_get_clientdata(client);
Jean Delvare1b4dff92008-07-14 22:38:29 +020075 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Ingo Molnar5c085d32006-01-18 23:16:04 +010077 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (!(data->valid & (1 << slice)) ||
80 time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
81 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
82
83 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
Jean Delvare4b2643d2007-07-12 14:12:29 +020084 for (i = slice << 5; i < (slice + 1) << 5; i += 32)
85 if (i2c_smbus_read_i2c_block_data(client, i,
86 32, data->data + i)
87 != 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 goto exit;
89 } else {
Jean Delvare1b4dff92008-07-14 22:38:29 +020090 for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
91 int word = i2c_smbus_read_word_data(client, i);
92 if (word < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 goto exit;
Jean Delvare1b4dff92008-07-14 22:38:29 +020094 data->data[i] = word & 0xff;
95 data->data[i + 1] = word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97 }
98 data->last_updated[slice] = jiffies;
99 data->valid |= (1 << slice);
100 }
101exit:
Ingo Molnar5c085d32006-01-18 23:16:04 +0100102 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Zhang Rui91a69022007-06-09 13:57:22 +0800105static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
106 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
109 struct eeprom_data *data = i2c_get_clientdata(client);
110 u8 slice;
111
112 if (off > EEPROM_SIZE)
113 return 0;
114 if (off + count > EEPROM_SIZE)
115 count = EEPROM_SIZE - off;
116
117 /* Only refresh slices which contain requested bytes */
118 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
119 eeprom_update_client(client, slice);
120
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100121 /* Hide Vaio private settings to regular users:
122 - BIOS passwords: bytes 0x00 to 0x0f
123 - UUID: bytes 0x10 to 0x1f
124 - Serial number: 0xc0 to 0xdf */
125 if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
126 int i;
127
128 for (i = 0; i < count; i++) {
129 if ((off + i <= 0x1f) ||
130 (off + i >= 0xc0 && off + i <= 0xdf))
131 buf[i] = 0;
132 else
133 buf[i] = data->data[off + i];
134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 } else {
136 memcpy(buf, &data->data[off], count);
137 }
138
139 return count;
140}
141
142static struct bin_attribute eeprom_attr = {
143 .attr = {
144 .name = "eeprom",
145 .mode = S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 },
147 .size = EEPROM_SIZE,
148 .read = eeprom_read,
149};
150
151static int eeprom_attach_adapter(struct i2c_adapter *adapter)
152{
Jean Delvared4653bf2008-07-14 22:38:29 +0200153 if (!(adapter->class & (I2C_CLASS_DDC | I2C_CLASS_SPD)))
154 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200155 return i2c_probe(adapter, &addr_data, eeprom_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200158/* This function is called by i2c_probe */
Ben Dooks6536c492005-10-26 21:04:12 +0200159static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct i2c_client *new_client;
162 struct eeprom_data *data;
163 int err = 0;
164
Jean Delvared4653bf2008-07-14 22:38:29 +0200165 /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
166 addresses 0x50-0x57, but we only care about 0x50. So decline
167 attaching to addresses >= 0x51 on DDC buses */
168 if (!(adapter->class & I2C_CLASS_SPD) && address >= 0x51)
169 goto exit;
170
Jean Delvare1b4dff92008-07-14 22:38:29 +0200171 /* There are four ways we can read the EEPROM data:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 (1) I2C block reads (faster, but unsupported by most adapters)
Jean Delvare1b4dff92008-07-14 22:38:29 +0200173 (2) Word reads (128% overhead)
174 (3) Consecutive byte reads (88% overhead, unsafe)
175 (4) Regular byte data reads (265% overhead)
176 The third and fourth methods are not implemented by this driver
177 because all known adapters support one of the first two. */
178 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
179 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto exit;
181
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200182 if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 err = -ENOMEM;
184 goto exit;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 new_client = &data->client;
188 memset(data->data, 0xff, EEPROM_SIZE);
189 i2c_set_clientdata(new_client, data);
190 new_client->addr = address;
191 new_client->adapter = adapter;
192 new_client->driver = &eeprom_driver;
193 new_client->flags = 0;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Fill in the remaining client fields */
196 strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
197 data->valid = 0;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100198 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 data->nature = UNKNOWN;
200
201 /* Tell the I2C layer a new client has arrived */
202 if ((err = i2c_attach_client(new_client)))
203 goto exit_kfree;
204
205 /* Detect the Vaio nature of EEPROMs.
Jean Delvare8b925a32007-11-15 19:24:03 +0100206 We use the "PCG-" or "VGN-" prefix as the signature. */
Jean Delvare1b4dff92008-07-14 22:38:29 +0200207 if (address == 0x57
208 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
Jean Delvare8b925a32007-11-15 19:24:03 +0100209 char name[4];
210
211 name[0] = i2c_smbus_read_byte_data(new_client, 0x80);
Jean Delvare1b4dff92008-07-14 22:38:29 +0200212 name[1] = i2c_smbus_read_byte_data(new_client, 0x81);
213 name[2] = i2c_smbus_read_byte_data(new_client, 0x82);
214 name[3] = i2c_smbus_read_byte_data(new_client, 0x83);
Jean Delvare8b925a32007-11-15 19:24:03 +0100215
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);