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> |
| 33 | #include <linux/sched.h> |
| 34 | #include <linux/jiffies.h> |
| 35 | #include <linux/i2c.h> |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* Addresses to scan */ |
| 39 | static unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54, |
| 40 | 0x55, 0x56, 0x57, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 43 | I2C_CLIENT_INSMOD_1(eeprom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | |
| 46 | /* Size of EEPROM in bytes */ |
| 47 | #define EEPROM_SIZE 256 |
| 48 | |
| 49 | /* possible types of eeprom devices */ |
| 50 | enum eeprom_nature { |
| 51 | UNKNOWN, |
| 52 | VAIO, |
| 53 | }; |
| 54 | |
| 55 | /* Each client has this additional data */ |
| 56 | struct eeprom_data { |
| 57 | struct i2c_client client; |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 58 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | u8 valid; /* bitfield, bit!=0 if slice is valid */ |
| 60 | unsigned long last_updated[8]; /* In jiffies, 8 slices */ |
| 61 | u8 data[EEPROM_SIZE]; /* Register values */ |
| 62 | enum eeprom_nature nature; |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | static int eeprom_attach_adapter(struct i2c_adapter *adapter); |
| 67 | static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind); |
| 68 | static int eeprom_detach_client(struct i2c_client *client); |
| 69 | |
| 70 | /* This is the driver that will be inserted */ |
| 71 | static struct i2c_driver eeprom_driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 72 | .driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 73 | .name = "eeprom", |
| 74 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | .id = I2C_DRIVERID_EEPROM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | .attach_adapter = eeprom_attach_adapter, |
| 77 | .detach_client = eeprom_detach_client, |
| 78 | }; |
| 79 | |
| 80 | static void eeprom_update_client(struct i2c_client *client, u8 slice) |
| 81 | { |
| 82 | struct eeprom_data *data = i2c_get_clientdata(client); |
| 83 | int i, j; |
| 84 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 85 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | if (!(data->valid & (1 << slice)) || |
| 88 | time_after(jiffies, data->last_updated[slice] + 300 * HZ)) { |
| 89 | dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice); |
| 90 | |
| 91 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 92 | for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_BLOCK_MAX) |
| 93 | if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_BLOCK_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | goto exit; |
| 95 | } else { |
| 96 | if (i2c_smbus_write_byte(client, slice << 5)) { |
| 97 | dev_dbg(&client->dev, "eeprom read start has failed!\n"); |
| 98 | goto exit; |
| 99 | } |
| 100 | for (i = slice << 5; i < (slice + 1) << 5; i++) { |
| 101 | j = i2c_smbus_read_byte(client); |
| 102 | if (j < 0) |
| 103 | goto exit; |
| 104 | data->data[i] = (u8) j; |
| 105 | } |
| 106 | } |
| 107 | data->last_updated[slice] = jiffies; |
| 108 | data->valid |= (1 << slice); |
| 109 | } |
| 110 | exit: |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 111 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static ssize_t eeprom_read(struct kobject *kobj, char *buf, loff_t off, size_t count) |
| 115 | { |
| 116 | struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj)); |
| 117 | struct eeprom_data *data = i2c_get_clientdata(client); |
| 118 | u8 slice; |
| 119 | |
| 120 | if (off > EEPROM_SIZE) |
| 121 | return 0; |
| 122 | if (off + count > EEPROM_SIZE) |
| 123 | count = EEPROM_SIZE - off; |
| 124 | |
| 125 | /* Only refresh slices which contain requested bytes */ |
| 126 | for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++) |
| 127 | eeprom_update_client(client, slice); |
| 128 | |
| 129 | /* Hide Vaio security settings to regular users (16 first bytes) */ |
| 130 | if (data->nature == VAIO && off < 16 && !capable(CAP_SYS_ADMIN)) { |
| 131 | size_t in_row1 = 16 - off; |
| 132 | in_row1 = min(in_row1, count); |
| 133 | memset(buf, 0, in_row1); |
| 134 | if (count - in_row1 > 0) |
| 135 | memcpy(buf + in_row1, &data->data[16], count - in_row1); |
| 136 | } else { |
| 137 | memcpy(buf, &data->data[off], count); |
| 138 | } |
| 139 | |
| 140 | return count; |
| 141 | } |
| 142 | |
| 143 | static struct bin_attribute eeprom_attr = { |
| 144 | .attr = { |
| 145 | .name = "eeprom", |
| 146 | .mode = S_IRUGO, |
| 147 | .owner = THIS_MODULE, |
| 148 | }, |
| 149 | .size = EEPROM_SIZE, |
| 150 | .read = eeprom_read, |
| 151 | }; |
| 152 | |
| 153 | static int eeprom_attach_adapter(struct i2c_adapter *adapter) |
| 154 | { |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 155 | return i2c_probe(adapter, &addr_data, eeprom_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 158 | /* This function is called by i2c_probe */ |
Ben Dooks | 6536c49 | 2005-10-26 21:04:12 +0200 | [diff] [blame] | 159 | static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | struct i2c_client *new_client; |
| 162 | struct eeprom_data *data; |
| 163 | int err = 0; |
| 164 | |
| 165 | /* There are three ways we can read the EEPROM data: |
| 166 | (1) I2C block reads (faster, but unsupported by most adapters) |
| 167 | (2) Consecutive byte reads (100% overhead) |
| 168 | (3) Regular byte data reads (200% overhead) |
| 169 | The third method is not implemented by this driver because all |
| 170 | known adapters support at least the second. */ |
| 171 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA |
| 172 | | I2C_FUNC_SMBUS_BYTE)) |
| 173 | goto exit; |
| 174 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 175 | if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | err = -ENOMEM; |
| 177 | goto exit; |
| 178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | new_client = &data->client; |
| 181 | memset(data->data, 0xff, EEPROM_SIZE); |
| 182 | i2c_set_clientdata(new_client, data); |
| 183 | new_client->addr = address; |
| 184 | new_client->adapter = adapter; |
| 185 | new_client->driver = &eeprom_driver; |
| 186 | new_client->flags = 0; |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | /* Fill in the remaining client fields */ |
| 189 | strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE); |
| 190 | data->valid = 0; |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 191 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | data->nature = UNKNOWN; |
| 193 | |
| 194 | /* Tell the I2C layer a new client has arrived */ |
| 195 | if ((err = i2c_attach_client(new_client))) |
| 196 | goto exit_kfree; |
| 197 | |
| 198 | /* Detect the Vaio nature of EEPROMs. |
| 199 | We use the "PCG-" prefix as the signature. */ |
| 200 | if (address == 0x57) { |
| 201 | if (i2c_smbus_read_byte_data(new_client, 0x80) == 'P' |
| 202 | && i2c_smbus_read_byte(new_client) == 'C' |
| 203 | && i2c_smbus_read_byte(new_client) == 'G' |
| 204 | && i2c_smbus_read_byte(new_client) == '-') { |
| 205 | dev_info(&new_client->dev, "Vaio EEPROM detected, " |
| 206 | "enabling password protection\n"); |
| 207 | data->nature = VAIO; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* create the sysfs eeprom file */ |
| 212 | sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr); |
| 213 | |
| 214 | return 0; |
| 215 | |
| 216 | exit_kfree: |
| 217 | kfree(data); |
| 218 | exit: |
| 219 | return err; |
| 220 | } |
| 221 | |
| 222 | static int eeprom_detach_client(struct i2c_client *client) |
| 223 | { |
| 224 | int err; |
| 225 | |
| 226 | err = i2c_detach_client(client); |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 227 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | kfree(i2c_get_clientdata(client)); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 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); |