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