Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
110f4229 AA |
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. | |
6 | * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org> | |
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 | */ | |
1da177e4 | 18 | |
1da177e4 LT |
19 | #include <linux/kernel.h> |
20 | #include <linux/init.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/slab.h> | |
1da177e4 LT |
23 | #include <linux/jiffies.h> |
24 | #include <linux/i2c.h> | |
5c085d36 | 25 | #include <linux/mutex.h> |
1da177e4 LT |
26 | |
27 | /* Addresses to scan */ | |
2cdddeb8 | 28 | static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54, |
1da177e4 | 29 | 0x55, 0x56, 0x57, I2C_CLIENT_END }; |
1da177e4 | 30 | |
1da177e4 LT |
31 | |
32 | /* Size of EEPROM in bytes */ | |
33 | #define EEPROM_SIZE 256 | |
34 | ||
35 | /* possible types of eeprom devices */ | |
36 | enum eeprom_nature { | |
37 | UNKNOWN, | |
38 | VAIO, | |
39 | }; | |
40 | ||
41 | /* Each client has this additional data */ | |
42 | struct eeprom_data { | |
5c085d36 | 43 | struct mutex update_lock; |
1da177e4 LT |
44 | u8 valid; /* bitfield, bit!=0 if slice is valid */ |
45 | unsigned long last_updated[8]; /* In jiffies, 8 slices */ | |
46 | u8 data[EEPROM_SIZE]; /* Register values */ | |
47 | enum eeprom_nature nature; | |
48 | }; | |
49 | ||
50 | ||
1da177e4 LT |
51 | static void eeprom_update_client(struct i2c_client *client, u8 slice) |
52 | { | |
53 | struct eeprom_data *data = i2c_get_clientdata(client); | |
1b4dff9c | 54 | int i; |
1da177e4 | 55 | |
5c085d36 | 56 | mutex_lock(&data->update_lock); |
1da177e4 LT |
57 | |
58 | if (!(data->valid & (1 << slice)) || | |
59 | time_after(jiffies, data->last_updated[slice] + 300 * HZ)) { | |
60 | dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice); | |
61 | ||
62 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { | |
4b2643d7 JD |
63 | for (i = slice << 5; i < (slice + 1) << 5; i += 32) |
64 | if (i2c_smbus_read_i2c_block_data(client, i, | |
65 | 32, data->data + i) | |
66 | != 32) | |
1da177e4 LT |
67 | goto exit; |
68 | } else { | |
1b4dff9c JD |
69 | for (i = slice << 5; i < (slice + 1) << 5; i += 2) { |
70 | int word = i2c_smbus_read_word_data(client, i); | |
71 | if (word < 0) | |
1da177e4 | 72 | goto exit; |
1b4dff9c JD |
73 | data->data[i] = word & 0xff; |
74 | data->data[i + 1] = word >> 8; | |
1da177e4 LT |
75 | } |
76 | } | |
77 | data->last_updated[slice] = jiffies; | |
78 | data->valid |= (1 << slice); | |
79 | } | |
80 | exit: | |
5c085d36 | 81 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
82 | } |
83 | ||
91a69029 ZR |
84 | static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr, |
85 | char *buf, loff_t off, size_t count) | |
1da177e4 LT |
86 | { |
87 | struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj)); | |
88 | struct eeprom_data *data = i2c_get_clientdata(client); | |
89 | u8 slice; | |
90 | ||
91 | if (off > EEPROM_SIZE) | |
92 | return 0; | |
93 | if (off + count > EEPROM_SIZE) | |
94 | count = EEPROM_SIZE - off; | |
95 | ||
96 | /* Only refresh slices which contain requested bytes */ | |
97 | for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++) | |
98 | eeprom_update_client(client, slice); | |
99 | ||
0f2cbd38 JD |
100 | /* Hide Vaio private settings to regular users: |
101 | - BIOS passwords: bytes 0x00 to 0x0f | |
102 | - UUID: bytes 0x10 to 0x1f | |
103 | - Serial number: 0xc0 to 0xdf */ | |
104 | if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) { | |
105 | int i; | |
106 | ||
107 | for (i = 0; i < count; i++) { | |
108 | if ((off + i <= 0x1f) || | |
109 | (off + i >= 0xc0 && off + i <= 0xdf)) | |
110 | buf[i] = 0; | |
111 | else | |
112 | buf[i] = data->data[off + i]; | |
113 | } | |
1da177e4 LT |
114 | } else { |
115 | memcpy(buf, &data->data[off], count); | |
116 | } | |
117 | ||
118 | return count; | |
119 | } | |
120 | ||
121 | static struct bin_attribute eeprom_attr = { | |
122 | .attr = { | |
123 | .name = "eeprom", | |
124 | .mode = S_IRUGO, | |
1da177e4 LT |
125 | }, |
126 | .size = EEPROM_SIZE, | |
127 | .read = eeprom_read, | |
128 | }; | |
129 | ||
68be3363 | 130 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 131 | static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info) |
1da177e4 | 132 | { |
68be3363 | 133 | struct i2c_adapter *adapter = client->adapter; |
1da177e4 | 134 | |
d4653bf9 JD |
135 | /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all |
136 | addresses 0x50-0x57, but we only care about 0x50. So decline | |
137 | attaching to addresses >= 0x51 on DDC buses */ | |
68be3363 JD |
138 | if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51) |
139 | return -ENODEV; | |
d4653bf9 | 140 | |
1b4dff9c | 141 | /* There are four ways we can read the EEPROM data: |
1da177e4 | 142 | (1) I2C block reads (faster, but unsupported by most adapters) |
1b4dff9c JD |
143 | (2) Word reads (128% overhead) |
144 | (3) Consecutive byte reads (88% overhead, unsafe) | |
145 | (4) Regular byte data reads (265% overhead) | |
146 | The third and fourth methods are not implemented by this driver | |
147 | because all known adapters support one of the first two. */ | |
148 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA) | |
149 | && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) | |
68be3363 JD |
150 | return -ENODEV; |
151 | ||
152 | strlcpy(info->type, "eeprom", I2C_NAME_SIZE); | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
157 | static int eeprom_probe(struct i2c_client *client, | |
158 | const struct i2c_device_id *id) | |
159 | { | |
160 | struct i2c_adapter *adapter = client->adapter; | |
161 | struct eeprom_data *data; | |
162 | int err; | |
1da177e4 | 163 | |
5263ebb5 | 164 | if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) { |
1da177e4 LT |
165 | err = -ENOMEM; |
166 | goto exit; | |
167 | } | |
1da177e4 | 168 | |
1da177e4 | 169 | memset(data->data, 0xff, EEPROM_SIZE); |
f741f673 | 170 | i2c_set_clientdata(client, data); |
5c085d36 | 171 | mutex_init(&data->update_lock); |
1da177e4 LT |
172 | data->nature = UNKNOWN; |
173 | ||
1da177e4 | 174 | /* Detect the Vaio nature of EEPROMs. |
8b925a3d | 175 | We use the "PCG-" or "VGN-" prefix as the signature. */ |
68be3363 | 176 | if (client->addr == 0x57 |
1b4dff9c | 177 | && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) { |
8b925a3d JD |
178 | char name[4]; |
179 | ||
f741f673 JD |
180 | name[0] = i2c_smbus_read_byte_data(client, 0x80); |
181 | name[1] = i2c_smbus_read_byte_data(client, 0x81); | |
182 | name[2] = i2c_smbus_read_byte_data(client, 0x82); | |
183 | name[3] = i2c_smbus_read_byte_data(client, 0x83); | |
8b925a3d JD |
184 | |
185 | if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) { | |
f741f673 | 186 | dev_info(&client->dev, "Vaio EEPROM detected, " |
0f2cbd38 | 187 | "enabling privacy protection\n"); |
1da177e4 LT |
188 | data->nature = VAIO; |
189 | } | |
190 | } | |
191 | ||
192 | /* create the sysfs eeprom file */ | |
f741f673 | 193 | err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr); |
7d9db67f | 194 | if (err) |
68be3363 | 195 | goto exit_kfree; |
1da177e4 LT |
196 | |
197 | return 0; | |
198 | ||
199 | exit_kfree: | |
200 | kfree(data); | |
201 | exit: | |
202 | return err; | |
203 | } | |
204 | ||
68be3363 | 205 | static int eeprom_remove(struct i2c_client *client) |
1da177e4 | 206 | { |
7d9db67f | 207 | sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr); |
1da177e4 LT |
208 | kfree(i2c_get_clientdata(client)); |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
68be3363 JD |
213 | static const struct i2c_device_id eeprom_id[] = { |
214 | { "eeprom", 0 }, | |
215 | { } | |
216 | }; | |
217 | ||
218 | static struct i2c_driver eeprom_driver = { | |
219 | .driver = { | |
220 | .name = "eeprom", | |
221 | }, | |
222 | .probe = eeprom_probe, | |
223 | .remove = eeprom_remove, | |
224 | .id_table = eeprom_id, | |
225 | ||
226 | .class = I2C_CLASS_DDC | I2C_CLASS_SPD, | |
227 | .detect = eeprom_detect, | |
c3813d6a | 228 | .address_list = normal_i2c, |
68be3363 JD |
229 | }; |
230 | ||
1da177e4 LT |
231 | static int __init eeprom_init(void) |
232 | { | |
233 | return i2c_add_driver(&eeprom_driver); | |
234 | } | |
235 | ||
236 | static void __exit eeprom_exit(void) | |
237 | { | |
238 | i2c_del_driver(&eeprom_driver); | |
239 | } | |
240 | ||
241 | ||
242 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " | |
243 | "Philip Edelbrock <phil@netroedge.com> and " | |
244 | "Greg Kroah-Hartman <greg@kroah.com>"); | |
245 | MODULE_DESCRIPTION("I2C EEPROM driver"); | |
246 | MODULE_LICENSE("GPL"); | |
247 | ||
248 | module_init(eeprom_init); | |
249 | module_exit(eeprom_exit); |