blob: 2c27193aeaa04721ee3159a279e4925abf28edd5 [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 {
Ingo Molnar5c085d32006-01-18 23:16:04 +010050 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 u8 valid; /* bitfield, bit!=0 if slice is valid */
52 unsigned long last_updated[8]; /* In jiffies, 8 slices */
53 u8 data[EEPROM_SIZE]; /* Register values */
54 enum eeprom_nature nature;
55};
56
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void eeprom_update_client(struct i2c_client *client, u8 slice)
59{
60 struct eeprom_data *data = i2c_get_clientdata(client);
Jean Delvare1b4dff92008-07-14 22:38:29 +020061 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ingo Molnar5c085d32006-01-18 23:16:04 +010063 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (!(data->valid & (1 << slice)) ||
66 time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
67 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
68
69 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
Jean Delvare4b2643d2007-07-12 14:12:29 +020070 for (i = slice << 5; i < (slice + 1) << 5; i += 32)
71 if (i2c_smbus_read_i2c_block_data(client, i,
72 32, data->data + i)
73 != 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 goto exit;
75 } else {
Jean Delvare1b4dff92008-07-14 22:38:29 +020076 for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
77 int word = i2c_smbus_read_word_data(client, i);
78 if (word < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 goto exit;
Jean Delvare1b4dff92008-07-14 22:38:29 +020080 data->data[i] = word & 0xff;
81 data->data[i + 1] = word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83 }
84 data->last_updated[slice] = jiffies;
85 data->valid |= (1 << slice);
86 }
87exit:
Ingo Molnar5c085d32006-01-18 23:16:04 +010088 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Zhang Rui91a69022007-06-09 13:57:22 +080091static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
92 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
95 struct eeprom_data *data = i2c_get_clientdata(client);
96 u8 slice;
97
98 if (off > EEPROM_SIZE)
99 return 0;
100 if (off + count > EEPROM_SIZE)
101 count = EEPROM_SIZE - off;
102
103 /* Only refresh slices which contain requested bytes */
104 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
105 eeprom_update_client(client, slice);
106
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100107 /* Hide Vaio private settings to regular users:
108 - BIOS passwords: bytes 0x00 to 0x0f
109 - UUID: bytes 0x10 to 0x1f
110 - Serial number: 0xc0 to 0xdf */
111 if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
112 int i;
113
114 for (i = 0; i < count; i++) {
115 if ((off + i <= 0x1f) ||
116 (off + i >= 0xc0 && off + i <= 0xdf))
117 buf[i] = 0;
118 else
119 buf[i] = data->data[off + i];
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 } else {
122 memcpy(buf, &data->data[off], count);
123 }
124
125 return count;
126}
127
128static struct bin_attribute eeprom_attr = {
129 .attr = {
130 .name = "eeprom",
131 .mode = S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 },
133 .size = EEPROM_SIZE,
134 .read = eeprom_read,
135};
136
Jean Delvare68be3362008-07-16 19:30:05 +0200137/* Return 0 if detection is successful, -ENODEV otherwise */
138static int eeprom_detect(struct i2c_client *client, int kind,
139 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Jean Delvare68be3362008-07-16 19:30:05 +0200141 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jean Delvared4653bf2008-07-14 22:38:29 +0200143 /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
144 addresses 0x50-0x57, but we only care about 0x50. So decline
145 attaching to addresses >= 0x51 on DDC buses */
Jean Delvare68be3362008-07-16 19:30:05 +0200146 if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
147 return -ENODEV;
Jean Delvared4653bf2008-07-14 22:38:29 +0200148
Jean Delvare1b4dff92008-07-14 22:38:29 +0200149 /* There are four ways we can read the EEPROM data:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 (1) I2C block reads (faster, but unsupported by most adapters)
Jean Delvare1b4dff92008-07-14 22:38:29 +0200151 (2) Word reads (128% overhead)
152 (3) Consecutive byte reads (88% overhead, unsafe)
153 (4) Regular byte data reads (265% overhead)
154 The third and fourth methods are not implemented by this driver
155 because all known adapters support one of the first two. */
156 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
157 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
Jean Delvare68be3362008-07-16 19:30:05 +0200158 return -ENODEV;
159
160 strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
161
162 return 0;
163}
164
165static int eeprom_probe(struct i2c_client *client,
166 const struct i2c_device_id *id)
167{
168 struct i2c_adapter *adapter = client->adapter;
169 struct eeprom_data *data;
170 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200172 if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 err = -ENOMEM;
174 goto exit;
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 memset(data->data, 0xff, EEPROM_SIZE);
Jean Delvaref741f672008-07-14 22:38:36 +0200178 i2c_set_clientdata(client, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100179 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 data->nature = UNKNOWN;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* Detect the Vaio nature of EEPROMs.
Jean Delvare8b925a32007-11-15 19:24:03 +0100183 We use the "PCG-" or "VGN-" prefix as the signature. */
Jean Delvare68be3362008-07-16 19:30:05 +0200184 if (client->addr == 0x57
Jean Delvare1b4dff92008-07-14 22:38:29 +0200185 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
Jean Delvare8b925a32007-11-15 19:24:03 +0100186 char name[4];
187
Jean Delvaref741f672008-07-14 22:38:36 +0200188 name[0] = i2c_smbus_read_byte_data(client, 0x80);
189 name[1] = i2c_smbus_read_byte_data(client, 0x81);
190 name[2] = i2c_smbus_read_byte_data(client, 0x82);
191 name[3] = i2c_smbus_read_byte_data(client, 0x83);
Jean Delvare8b925a32007-11-15 19:24:03 +0100192
193 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
Jean Delvaref741f672008-07-14 22:38:36 +0200194 dev_info(&client->dev, "Vaio EEPROM detected, "
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100195 "enabling privacy protection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 data->nature = VAIO;
197 }
198 }
199
200 /* create the sysfs eeprom file */
Jean Delvaref741f672008-07-14 22:38:36 +0200201 err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
Jean Delvare7d9db672006-09-03 22:20:24 +0200202 if (err)
Jean Delvare68be3362008-07-16 19:30:05 +0200203 goto exit_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 return 0;
206
207exit_kfree:
208 kfree(data);
209exit:
210 return err;
211}
212
Jean Delvare68be3362008-07-16 19:30:05 +0200213static int eeprom_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Jean Delvare7d9db672006-09-03 22:20:24 +0200215 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 kfree(i2c_get_clientdata(client));
217
218 return 0;
219}
220
Jean Delvare68be3362008-07-16 19:30:05 +0200221static const struct i2c_device_id eeprom_id[] = {
222 { "eeprom", 0 },
223 { }
224};
225
226static struct i2c_driver eeprom_driver = {
227 .driver = {
228 .name = "eeprom",
229 },
230 .probe = eeprom_probe,
231 .remove = eeprom_remove,
232 .id_table = eeprom_id,
233
234 .class = I2C_CLASS_DDC | I2C_CLASS_SPD,
235 .detect = eeprom_detect,
236 .address_data = &addr_data,
237};
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static int __init eeprom_init(void)
240{
241 return i2c_add_driver(&eeprom_driver);
242}
243
244static void __exit eeprom_exit(void)
245{
246 i2c_del_driver(&eeprom_driver);
247}
248
249
250MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
251 "Philip Edelbrock <phil@netroedge.com> and "
252 "Greg Kroah-Hartman <greg@kroah.com>");
253MODULE_DESCRIPTION("I2C EEPROM driver");
254MODULE_LICENSE("GPL");
255
256module_init(eeprom_init);
257module_exit(eeprom_exit);