]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/misc/eeprom/eeprom.c
i2c: Drop the kind parameter from detect callbacks
[net-next-2.6.git] / drivers / misc / eeprom / eeprom.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 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.
954a9930 6 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
1da177e4
LT
7
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
1da177e4
LT
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
1da177e4
LT
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
5c085d36 29#include <linux/mutex.h>
1da177e4
LT
30
31/* Addresses to scan */
2cdddeb8 32static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
1da177e4 33 0x55, 0x56, 0x57, I2C_CLIENT_END };
1da177e4
LT
34
35/* Insmod parameters */
f4b50261 36I2C_CLIENT_INSMOD_1(eeprom);
1da177e4
LT
37
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 {
5c085d36 50 struct mutex update_lock;
1da177e4
LT
51 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
1da177e4
LT
58static void eeprom_update_client(struct i2c_client *client, u8 slice)
59{
60 struct eeprom_data *data = i2c_get_clientdata(client);
1b4dff9c 61 int i;
1da177e4 62
5c085d36 63 mutex_lock(&data->update_lock);
1da177e4
LT
64
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)) {
4b2643d7
JD
70 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)
1da177e4
LT
74 goto exit;
75 } else {
1b4dff9c
JD
76 for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
77 int word = i2c_smbus_read_word_data(client, i);
78 if (word < 0)
1da177e4 79 goto exit;
1b4dff9c
JD
80 data->data[i] = word & 0xff;
81 data->data[i + 1] = word >> 8;
1da177e4
LT
82 }
83 }
84 data->last_updated[slice] = jiffies;
85 data->valid |= (1 << slice);
86 }
87exit:
5c085d36 88 mutex_unlock(&data->update_lock);
1da177e4
LT
89}
90
91a69029
ZR
91static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
92 char *buf, loff_t off, size_t count)
1da177e4
LT
93{
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
0f2cbd38
JD
107 /* 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 }
1da177e4
LT
121 } 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,
1da177e4
LT
132 },
133 .size = EEPROM_SIZE,
134 .read = eeprom_read,
135};
136
68be3363 137/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 138static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
1da177e4 139{
68be3363 140 struct i2c_adapter *adapter = client->adapter;
1da177e4 141
d4653bf9
JD
142 /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
143 addresses 0x50-0x57, but we only care about 0x50. So decline
144 attaching to addresses >= 0x51 on DDC buses */
68be3363
JD
145 if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
146 return -ENODEV;
d4653bf9 147
1b4dff9c 148 /* There are four ways we can read the EEPROM data:
1da177e4 149 (1) I2C block reads (faster, but unsupported by most adapters)
1b4dff9c
JD
150 (2) Word reads (128% overhead)
151 (3) Consecutive byte reads (88% overhead, unsafe)
152 (4) Regular byte data reads (265% overhead)
153 The third and fourth methods are not implemented by this driver
154 because all known adapters support one of the first two. */
155 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
156 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
68be3363
JD
157 return -ENODEV;
158
159 strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
160
161 return 0;
162}
163
164static int eeprom_probe(struct i2c_client *client,
165 const struct i2c_device_id *id)
166{
167 struct i2c_adapter *adapter = client->adapter;
168 struct eeprom_data *data;
169 int err;
1da177e4 170
5263ebb5 171 if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
1da177e4
LT
172 err = -ENOMEM;
173 goto exit;
174 }
1da177e4 175
1da177e4 176 memset(data->data, 0xff, EEPROM_SIZE);
f741f673 177 i2c_set_clientdata(client, data);
5c085d36 178 mutex_init(&data->update_lock);
1da177e4
LT
179 data->nature = UNKNOWN;
180
1da177e4 181 /* Detect the Vaio nature of EEPROMs.
8b925a3d 182 We use the "PCG-" or "VGN-" prefix as the signature. */
68be3363 183 if (client->addr == 0x57
1b4dff9c 184 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
8b925a3d
JD
185 char name[4];
186
f741f673
JD
187 name[0] = i2c_smbus_read_byte_data(client, 0x80);
188 name[1] = i2c_smbus_read_byte_data(client, 0x81);
189 name[2] = i2c_smbus_read_byte_data(client, 0x82);
190 name[3] = i2c_smbus_read_byte_data(client, 0x83);
8b925a3d
JD
191
192 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
f741f673 193 dev_info(&client->dev, "Vaio EEPROM detected, "
0f2cbd38 194 "enabling privacy protection\n");
1da177e4
LT
195 data->nature = VAIO;
196 }
197 }
198
199 /* create the sysfs eeprom file */
f741f673 200 err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
7d9db67f 201 if (err)
68be3363 202 goto exit_kfree;
1da177e4
LT
203
204 return 0;
205
206exit_kfree:
207 kfree(data);
208exit:
209 return err;
210}
211
68be3363 212static int eeprom_remove(struct i2c_client *client)
1da177e4 213{
7d9db67f 214 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
1da177e4
LT
215 kfree(i2c_get_clientdata(client));
216
217 return 0;
218}
219
68be3363
JD
220static const struct i2c_device_id eeprom_id[] = {
221 { "eeprom", 0 },
222 { }
223};
224
225static struct i2c_driver eeprom_driver = {
226 .driver = {
227 .name = "eeprom",
228 },
229 .probe = eeprom_probe,
230 .remove = eeprom_remove,
231 .id_table = eeprom_id,
232
233 .class = I2C_CLASS_DDC | I2C_CLASS_SPD,
234 .detect = eeprom_detect,
235 .address_data = &addr_data,
236};
237
1da177e4
LT
238static int __init eeprom_init(void)
239{
240 return i2c_add_driver(&eeprom_driver);
241}
242
243static void __exit eeprom_exit(void)
244{
245 i2c_del_driver(&eeprom_driver);
246}
247
248
249MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
250 "Philip Edelbrock <phil@netroedge.com> and "
251 "Greg Kroah-Hartman <greg@kroah.com>");
252MODULE_DESCRIPTION("I2C EEPROM driver");
253MODULE_LICENSE("GPL");
254
255module_init(eeprom_init);
256module_exit(eeprom_exit);