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