Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | eeprom.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | monitoring |
| 4 | Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and |
| 5 | Philip Edelbrock <phil@netroedge.com> |
| 6 | Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> |
| 7 | Copyright (C) 2003 IBM Corp. |
| 8 | |
| 9 | 2004-01-16 Jean Delvare <khali@linux-fr.org> |
| 10 | Divide the eeprom in 32-byte (arbitrary) slices. This significantly |
| 11 | speeds sensors up, as well as various scripts using the eeprom |
| 12 | module. |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License as published by |
| 16 | the Free Software Foundation; either version 2 of the License, or |
| 17 | (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | */ |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kernel.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/jiffies.h> |
| 34 | #include <linux/i2c.h> |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 35 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Addresses to scan */ |
| 38 | static unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54, |
| 39 | 0x55, 0x56, 0x57, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 42 | I2C_CLIENT_INSMOD_1(eeprom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | /* Size of EEPROM in bytes */ |
| 46 | #define EEPROM_SIZE 256 |
| 47 | |
| 48 | /* possible types of eeprom devices */ |
| 49 | enum eeprom_nature { |
| 50 | UNKNOWN, |
| 51 | VAIO, |
| 52 | }; |
| 53 | |
| 54 | /* Each client has this additional data */ |
| 55 | struct eeprom_data { |
| 56 | struct i2c_client client; |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 57 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | u8 valid; /* bitfield, bit!=0 if slice is valid */ |
| 59 | unsigned long last_updated[8]; /* In jiffies, 8 slices */ |
| 60 | u8 data[EEPROM_SIZE]; /* Register values */ |
| 61 | enum eeprom_nature nature; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | static int eeprom_attach_adapter(struct i2c_adapter *adapter); |
| 66 | static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind); |
| 67 | static int eeprom_detach_client(struct i2c_client *client); |
| 68 | |
| 69 | /* This is the driver that will be inserted */ |
| 70 | static struct i2c_driver eeprom_driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 71 | .driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 72 | .name = "eeprom", |
| 73 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | .id = I2C_DRIVERID_EEPROM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | .attach_adapter = eeprom_attach_adapter, |
| 76 | .detach_client = eeprom_detach_client, |
| 77 | }; |
| 78 | |
| 79 | static void eeprom_update_client(struct i2c_client *client, u8 slice) |
| 80 | { |
| 81 | struct eeprom_data *data = i2c_get_clientdata(client); |
| 82 | int i, j; |
| 83 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 84 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | if (!(data->valid & (1 << slice)) || |
| 87 | time_after(jiffies, data->last_updated[slice] + 300 * HZ)) { |
| 88 | dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice); |
| 89 | |
| 90 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 91 | for (i = slice << 5; i < (slice + 1) << 5; i += 32) |
| 92 | if (i2c_smbus_read_i2c_block_data(client, i, |
| 93 | 32, data->data + i) |
| 94 | != 32) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | goto exit; |
| 96 | } else { |
| 97 | if (i2c_smbus_write_byte(client, slice << 5)) { |
| 98 | dev_dbg(&client->dev, "eeprom read start has failed!\n"); |
| 99 | goto exit; |
| 100 | } |
| 101 | for (i = slice << 5; i < (slice + 1) << 5; i++) { |
| 102 | j = i2c_smbus_read_byte(client); |
| 103 | if (j < 0) |
| 104 | goto exit; |
| 105 | data->data[i] = (u8) j; |
| 106 | } |
| 107 | } |
| 108 | data->last_updated[slice] = jiffies; |
| 109 | data->valid |= (1 << slice); |
| 110 | } |
| 111 | exit: |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 112 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 115 | static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 116 | char *buf, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj)); |
| 119 | struct eeprom_data *data = i2c_get_clientdata(client); |
| 120 | u8 slice; |
| 121 | |
| 122 | if (off > EEPROM_SIZE) |
| 123 | return 0; |
| 124 | if (off + count > EEPROM_SIZE) |
| 125 | count = EEPROM_SIZE - off; |
| 126 | |
| 127 | /* Only refresh slices which contain requested bytes */ |
| 128 | for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++) |
| 129 | eeprom_update_client(client, slice); |
| 130 | |
| 131 | /* Hide Vaio security settings to regular users (16 first bytes) */ |
| 132 | if (data->nature == VAIO && off < 16 && !capable(CAP_SYS_ADMIN)) { |
| 133 | size_t in_row1 = 16 - off; |
| 134 | in_row1 = min(in_row1, count); |
| 135 | memset(buf, 0, in_row1); |
| 136 | if (count - in_row1 > 0) |
| 137 | memcpy(buf + in_row1, &data->data[16], count - in_row1); |
| 138 | } else { |
| 139 | memcpy(buf, &data->data[off], count); |
| 140 | } |
| 141 | |
| 142 | return count; |
| 143 | } |
| 144 | |
| 145 | static struct bin_attribute eeprom_attr = { |
| 146 | .attr = { |
| 147 | .name = "eeprom", |
| 148 | .mode = S_IRUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | }, |
| 150 | .size = EEPROM_SIZE, |
| 151 | .read = eeprom_read, |
| 152 | }; |
| 153 | |
| 154 | static int eeprom_attach_adapter(struct i2c_adapter *adapter) |
| 155 | { |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 156 | return i2c_probe(adapter, &addr_data, eeprom_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 159 | /* This function is called by i2c_probe */ |
Ben Dooks | 6536c49 | 2005-10-26 21:04:12 +0200 | [diff] [blame] | 160 | static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct i2c_client *new_client; |
| 163 | struct eeprom_data *data; |
| 164 | int err = 0; |
| 165 | |
| 166 | /* There are three ways we can read the EEPROM data: |
| 167 | (1) I2C block reads (faster, but unsupported by most adapters) |
| 168 | (2) Consecutive byte reads (100% overhead) |
| 169 | (3) Regular byte data reads (200% overhead) |
| 170 | The third method is not implemented by this driver because all |
| 171 | known adapters support at least the second. */ |
| 172 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA |
| 173 | | I2C_FUNC_SMBUS_BYTE)) |
| 174 | goto exit; |
| 175 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 176 | if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | err = -ENOMEM; |
| 178 | goto exit; |
| 179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | new_client = &data->client; |
| 182 | memset(data->data, 0xff, EEPROM_SIZE); |
| 183 | i2c_set_clientdata(new_client, data); |
| 184 | new_client->addr = address; |
| 185 | new_client->adapter = adapter; |
| 186 | new_client->driver = &eeprom_driver; |
| 187 | new_client->flags = 0; |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* Fill in the remaining client fields */ |
| 190 | strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE); |
| 191 | data->valid = 0; |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 192 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | data->nature = UNKNOWN; |
| 194 | |
| 195 | /* Tell the I2C layer a new client has arrived */ |
| 196 | if ((err = i2c_attach_client(new_client))) |
| 197 | goto exit_kfree; |
| 198 | |
| 199 | /* Detect the Vaio nature of EEPROMs. |
| 200 | We use the "PCG-" prefix as the signature. */ |
| 201 | if (address == 0x57) { |
| 202 | if (i2c_smbus_read_byte_data(new_client, 0x80) == 'P' |
| 203 | && i2c_smbus_read_byte(new_client) == 'C' |
| 204 | && i2c_smbus_read_byte(new_client) == 'G' |
| 205 | && i2c_smbus_read_byte(new_client) == '-') { |
| 206 | dev_info(&new_client->dev, "Vaio EEPROM detected, " |
| 207 | "enabling password protection\n"); |
| 208 | data->nature = VAIO; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* create the sysfs eeprom file */ |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 213 | err = sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr); |
| 214 | if (err) |
| 215 | goto exit_detach; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 219 | exit_detach: |
| 220 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | exit_kfree: |
| 222 | kfree(data); |
| 223 | exit: |
| 224 | return err; |
| 225 | } |
| 226 | |
| 227 | static int eeprom_detach_client(struct i2c_client *client) |
| 228 | { |
| 229 | int err; |
| 230 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 231 | sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr); |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | err = i2c_detach_client(client); |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 234 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | kfree(i2c_get_clientdata(client)); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static int __init eeprom_init(void) |
| 243 | { |
| 244 | return i2c_add_driver(&eeprom_driver); |
| 245 | } |
| 246 | |
| 247 | static void __exit eeprom_exit(void) |
| 248 | { |
| 249 | i2c_del_driver(&eeprom_driver); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " |
| 254 | "Philip Edelbrock <phil@netroedge.com> and " |
| 255 | "Greg Kroah-Hartman <greg@kroah.com>"); |
| 256 | MODULE_DESCRIPTION("I2C EEPROM driver"); |
| 257 | MODULE_LICENSE("GPL"); |
| 258 | |
| 259 | module_init(eeprom_init); |
| 260 | module_exit(eeprom_exit); |