1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Copyright (C) 2003 IBM Corp.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/capability.h>
14 #include <linux/jiffies.h>
15 #include <linux/i2c.h>
16 #include <linux/mutex.h>
18 /* Addresses to scan */
19 static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
20 0x55, 0x56, 0x57, I2C_CLIENT_END };
23 /* Size of EEPROM in bytes */
24 #define EEPROM_SIZE 256
26 /* possible types of eeprom devices */
32 /* Each client has this additional data */
34 struct mutex update_lock;
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;
42 static void eeprom_update_client(struct i2c_client *client, u8 slice)
44 struct eeprom_data *data = i2c_get_clientdata(client);
47 mutex_lock(&data->update_lock);
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);
53 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
54 for (i = slice << 5; i < (slice + 1) << 5; i += 32)
55 if (i2c_smbus_read_i2c_block_data(client, i,
60 for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
61 int word = i2c_smbus_read_word_data(client, i);
64 data->data[i] = word & 0xff;
65 data->data[i + 1] = word >> 8;
68 data->last_updated[slice] = jiffies;
69 data->valid |= (1 << slice);
72 mutex_unlock(&data->update_lock);
75 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
76 struct bin_attribute *bin_attr,
77 char *buf, loff_t off, size_t count)
79 struct i2c_client *client = to_i2c_client(kobj_to_dev(kobj));
80 struct eeprom_data *data = i2c_get_clientdata(client);
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);
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)) {
94 for (i = 0; i < count; i++) {
95 if ((off + i <= 0x1f) ||
96 (off + i >= 0xc0 && off + i <= 0xdf))
99 buf[i] = data->data[off + i];
102 memcpy(buf, &data->data[off], count);
108 static const struct bin_attribute eeprom_attr = {
117 /* Return 0 if detection is successful, -ENODEV otherwise */
118 static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
120 struct i2c_adapter *adapter = client->adapter;
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 */
125 if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
128 /* There are four ways we can read the EEPROM data:
129 (1) I2C block reads (faster, but unsupported by most adapters)
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))
139 strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
144 static int eeprom_probe(struct i2c_client *client,
145 const struct i2c_device_id *id)
147 struct i2c_adapter *adapter = client->adapter;
148 struct eeprom_data *data;
150 data = devm_kzalloc(&client->dev, sizeof(struct eeprom_data),
155 memset(data->data, 0xff, EEPROM_SIZE);
156 i2c_set_clientdata(client, data);
157 mutex_init(&data->update_lock);
158 data->nature = UNKNOWN;
160 /* Detect the Vaio nature of EEPROMs.
161 We use the "PCG-" or "VGN-" prefix as the signature. */
162 if (client->addr == 0x57
163 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
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);
171 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
172 dev_info(&client->dev, "Vaio EEPROM detected, "
173 "enabling privacy protection\n");
178 /* create the sysfs eeprom file */
179 return sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
182 static int eeprom_remove(struct i2c_client *client)
184 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
189 static const struct i2c_device_id eeprom_id[] = {
194 static struct i2c_driver eeprom_driver = {
198 .probe = eeprom_probe,
199 .remove = eeprom_remove,
200 .id_table = eeprom_id,
202 .class = I2C_CLASS_DDC | I2C_CLASS_SPD,
203 .detect = eeprom_detect,
204 .address_list = normal_i2c,
207 module_i2c_driver(eeprom_driver);
212 MODULE_DESCRIPTION("I2C EEPROM driver");
213 MODULE_LICENSE("GPL");