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