]>
Commit | Line | Data |
---|---|---|
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 | 32 | static 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 | 36 | I2C_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 */ | |
43 | enum eeprom_nature { | |
44 | UNKNOWN, | |
45 | VAIO, | |
46 | }; | |
47 | ||
48 | /* Each client has this additional data */ | |
49 | struct eeprom_data { | |
50 | struct i2c_client client; | |
5c085d36 | 51 | struct mutex update_lock; |
1da177e4 LT |
52 | 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 | ||
59 | static int eeprom_attach_adapter(struct i2c_adapter *adapter); | |
60 | static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind); | |
61 | static int eeprom_detach_client(struct i2c_client *client); | |
62 | ||
63 | /* This is the driver that will be inserted */ | |
64 | static struct i2c_driver eeprom_driver = { | |
a9718b0c | 65 | .driver = { |
a9718b0c LR |
66 | .name = "eeprom", |
67 | }, | |
1da177e4 LT |
68 | .attach_adapter = eeprom_attach_adapter, |
69 | .detach_client = eeprom_detach_client, | |
70 | }; | |
71 | ||
72 | static void eeprom_update_client(struct i2c_client *client, u8 slice) | |
73 | { | |
74 | struct eeprom_data *data = i2c_get_clientdata(client); | |
1b4dff9c | 75 | int i; |
1da177e4 | 76 | |
5c085d36 | 77 | mutex_lock(&data->update_lock); |
1da177e4 LT |
78 | |
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)) { | |
4b2643d7 JD |
84 | 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) | |
1da177e4 LT |
88 | goto exit; |
89 | } else { | |
1b4dff9c JD |
90 | for (i = slice << 5; i < (slice + 1) << 5; i += 2) { |
91 | int word = i2c_smbus_read_word_data(client, i); | |
92 | if (word < 0) | |
1da177e4 | 93 | goto exit; |
1b4dff9c JD |
94 | data->data[i] = word & 0xff; |
95 | data->data[i + 1] = word >> 8; | |
1da177e4 LT |
96 | } |
97 | } | |
98 | data->last_updated[slice] = jiffies; | |
99 | data->valid |= (1 << slice); | |
100 | } | |
101 | exit: | |
5c085d36 | 102 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
103 | } |
104 | ||
91a69029 ZR |
105 | static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr, |
106 | char *buf, loff_t off, size_t count) | |
1da177e4 LT |
107 | { |
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 | ||
0f2cbd38 JD |
121 | /* 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 | } | |
1da177e4 LT |
135 | } else { |
136 | memcpy(buf, &data->data[off], count); | |
137 | } | |
138 | ||
139 | return count; | |
140 | } | |
141 | ||
142 | static struct bin_attribute eeprom_attr = { | |
143 | .attr = { | |
144 | .name = "eeprom", | |
145 | .mode = S_IRUGO, | |
1da177e4 LT |
146 | }, |
147 | .size = EEPROM_SIZE, | |
148 | .read = eeprom_read, | |
149 | }; | |
150 | ||
151 | static int eeprom_attach_adapter(struct i2c_adapter *adapter) | |
152 | { | |
d4653bf9 JD |
153 | if (!(adapter->class & (I2C_CLASS_DDC | I2C_CLASS_SPD))) |
154 | return 0; | |
2ed2dc3c | 155 | return i2c_probe(adapter, &addr_data, eeprom_detect); |
1da177e4 LT |
156 | } |
157 | ||
2ed2dc3c | 158 | /* This function is called by i2c_probe */ |
6536c49a | 159 | static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind) |
1da177e4 | 160 | { |
f741f673 | 161 | struct i2c_client *client; |
1da177e4 LT |
162 | struct eeprom_data *data; |
163 | int err = 0; | |
164 | ||
d4653bf9 JD |
165 | /* 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 | ||
1b4dff9c | 171 | /* There are four ways we can read the EEPROM data: |
1da177e4 | 172 | (1) I2C block reads (faster, but unsupported by most adapters) |
1b4dff9c JD |
173 | (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)) | |
1da177e4 LT |
180 | goto exit; |
181 | ||
5263ebb5 | 182 | if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) { |
1da177e4 LT |
183 | err = -ENOMEM; |
184 | goto exit; | |
185 | } | |
1da177e4 | 186 | |
f741f673 | 187 | client = &data->client; |
1da177e4 | 188 | memset(data->data, 0xff, EEPROM_SIZE); |
f741f673 JD |
189 | i2c_set_clientdata(client, data); |
190 | client->addr = address; | |
191 | client->adapter = adapter; | |
192 | client->driver = &eeprom_driver; | |
1da177e4 | 193 | |
1da177e4 | 194 | /* Fill in the remaining client fields */ |
f741f673 | 195 | strlcpy(client->name, "eeprom", I2C_NAME_SIZE); |
5c085d36 | 196 | mutex_init(&data->update_lock); |
1da177e4 LT |
197 | data->nature = UNKNOWN; |
198 | ||
199 | /* Tell the I2C layer a new client has arrived */ | |
f741f673 | 200 | if ((err = i2c_attach_client(client))) |
1da177e4 LT |
201 | goto exit_kfree; |
202 | ||
203 | /* Detect the Vaio nature of EEPROMs. | |
8b925a3d | 204 | We use the "PCG-" or "VGN-" prefix as the signature. */ |
1b4dff9c JD |
205 | if (address == 0x57 |
206 | && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) { | |
8b925a3d JD |
207 | char name[4]; |
208 | ||
f741f673 JD |
209 | name[0] = i2c_smbus_read_byte_data(client, 0x80); |
210 | name[1] = i2c_smbus_read_byte_data(client, 0x81); | |
211 | name[2] = i2c_smbus_read_byte_data(client, 0x82); | |
212 | name[3] = i2c_smbus_read_byte_data(client, 0x83); | |
8b925a3d JD |
213 | |
214 | if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) { | |
f741f673 | 215 | dev_info(&client->dev, "Vaio EEPROM detected, " |
0f2cbd38 | 216 | "enabling privacy protection\n"); |
1da177e4 LT |
217 | data->nature = VAIO; |
218 | } | |
219 | } | |
220 | ||
221 | /* create the sysfs eeprom file */ | |
f741f673 | 222 | err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr); |
7d9db67f JD |
223 | if (err) |
224 | goto exit_detach; | |
1da177e4 LT |
225 | |
226 | return 0; | |
227 | ||
7d9db67f | 228 | exit_detach: |
f741f673 | 229 | i2c_detach_client(client); |
1da177e4 LT |
230 | exit_kfree: |
231 | kfree(data); | |
232 | exit: | |
233 | return err; | |
234 | } | |
235 | ||
236 | static int eeprom_detach_client(struct i2c_client *client) | |
237 | { | |
238 | int err; | |
239 | ||
7d9db67f JD |
240 | sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr); |
241 | ||
1da177e4 | 242 | err = i2c_detach_client(client); |
7bef5594 | 243 | if (err) |
1da177e4 | 244 | return err; |
1da177e4 LT |
245 | |
246 | kfree(i2c_get_clientdata(client)); | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | static int __init eeprom_init(void) | |
252 | { | |
253 | return i2c_add_driver(&eeprom_driver); | |
254 | } | |
255 | ||
256 | static void __exit eeprom_exit(void) | |
257 | { | |
258 | i2c_del_driver(&eeprom_driver); | |
259 | } | |
260 | ||
261 | ||
262 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " | |
263 | "Philip Edelbrock <phil@netroedge.com> and " | |
264 | "Greg Kroah-Hartman <greg@kroah.com>"); | |
265 | MODULE_DESCRIPTION("I2C EEPROM driver"); | |
266 | MODULE_LICENSE("GPL"); | |
267 | ||
268 | module_init(eeprom_init); | |
269 | module_exit(eeprom_exit); |