18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and 48c2ecf20Sopenharmony_ci * Philip Edelbrock <phil@netroedge.com> 58c2ecf20Sopenharmony_ci * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> 68c2ecf20Sopenharmony_ci * Copyright (C) 2003 IBM Corp. 78c2ecf20Sopenharmony_ci * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/device.h> 138c2ecf20Sopenharmony_ci#include <linux/capability.h> 148c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 158c2ecf20Sopenharmony_ci#include <linux/i2c.h> 168c2ecf20Sopenharmony_ci#include <linux/mutex.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* Addresses to scan */ 198c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54, 208c2ecf20Sopenharmony_ci 0x55, 0x56, 0x57, I2C_CLIENT_END }; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* Size of EEPROM in bytes */ 248c2ecf20Sopenharmony_ci#define EEPROM_SIZE 256 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* possible types of eeprom devices */ 278c2ecf20Sopenharmony_cienum eeprom_nature { 288c2ecf20Sopenharmony_ci UNKNOWN, 298c2ecf20Sopenharmony_ci VAIO, 308c2ecf20Sopenharmony_ci}; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* Each client has this additional data */ 338c2ecf20Sopenharmony_cistruct eeprom_data { 348c2ecf20Sopenharmony_ci struct mutex update_lock; 358c2ecf20Sopenharmony_ci u8 valid; /* bitfield, bit!=0 if slice is valid */ 368c2ecf20Sopenharmony_ci unsigned long last_updated[8]; /* In jiffies, 8 slices */ 378c2ecf20Sopenharmony_ci u8 data[EEPROM_SIZE]; /* Register values */ 388c2ecf20Sopenharmony_ci enum eeprom_nature nature; 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic void eeprom_update_client(struct i2c_client *client, u8 slice) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci struct eeprom_data *data = i2c_get_clientdata(client); 458c2ecf20Sopenharmony_ci int i; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci if (!(data->valid & (1 << slice)) || 508c2ecf20Sopenharmony_ci time_after(jiffies, data->last_updated[slice] + 300 * HZ)) { 518c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 548c2ecf20Sopenharmony_ci for (i = slice << 5; i < (slice + 1) << 5; i += 32) 558c2ecf20Sopenharmony_ci if (i2c_smbus_read_i2c_block_data(client, i, 568c2ecf20Sopenharmony_ci 32, data->data + i) 578c2ecf20Sopenharmony_ci != 32) 588c2ecf20Sopenharmony_ci goto exit; 598c2ecf20Sopenharmony_ci } else { 608c2ecf20Sopenharmony_ci for (i = slice << 5; i < (slice + 1) << 5; i += 2) { 618c2ecf20Sopenharmony_ci int word = i2c_smbus_read_word_data(client, i); 628c2ecf20Sopenharmony_ci if (word < 0) 638c2ecf20Sopenharmony_ci goto exit; 648c2ecf20Sopenharmony_ci data->data[i] = word & 0xff; 658c2ecf20Sopenharmony_ci data->data[i + 1] = word >> 8; 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci data->last_updated[slice] = jiffies; 698c2ecf20Sopenharmony_ci data->valid |= (1 << slice); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ciexit: 728c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic ssize_t eeprom_read(struct file *filp, struct kobject *kobj, 768c2ecf20Sopenharmony_ci struct bin_attribute *bin_attr, 778c2ecf20Sopenharmony_ci char *buf, loff_t off, size_t count) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci struct i2c_client *client = kobj_to_i2c_client(kobj); 808c2ecf20Sopenharmony_ci struct eeprom_data *data = i2c_get_clientdata(client); 818c2ecf20Sopenharmony_ci u8 slice; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci /* Only refresh slices which contain requested bytes */ 848c2ecf20Sopenharmony_ci for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++) 858c2ecf20Sopenharmony_ci eeprom_update_client(client, slice); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* Hide Vaio private settings to regular users: 888c2ecf20Sopenharmony_ci - BIOS passwords: bytes 0x00 to 0x0f 898c2ecf20Sopenharmony_ci - UUID: bytes 0x10 to 0x1f 908c2ecf20Sopenharmony_ci - Serial number: 0xc0 to 0xdf */ 918c2ecf20Sopenharmony_ci if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) { 928c2ecf20Sopenharmony_ci int i; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) { 958c2ecf20Sopenharmony_ci if ((off + i <= 0x1f) || 968c2ecf20Sopenharmony_ci (off + i >= 0xc0 && off + i <= 0xdf)) 978c2ecf20Sopenharmony_ci buf[i] = 0; 988c2ecf20Sopenharmony_ci else 998c2ecf20Sopenharmony_ci buf[i] = data->data[off + i]; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci } else { 1028c2ecf20Sopenharmony_ci memcpy(buf, &data->data[off], count); 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci return count; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic const struct bin_attribute eeprom_attr = { 1098c2ecf20Sopenharmony_ci .attr = { 1108c2ecf20Sopenharmony_ci .name = "eeprom", 1118c2ecf20Sopenharmony_ci .mode = S_IRUGO, 1128c2ecf20Sopenharmony_ci }, 1138c2ecf20Sopenharmony_ci .size = EEPROM_SIZE, 1148c2ecf20Sopenharmony_ci .read = eeprom_read, 1158c2ecf20Sopenharmony_ci}; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */ 1188c2ecf20Sopenharmony_cistatic int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all 1238c2ecf20Sopenharmony_ci addresses 0x50-0x57, but we only care about 0x50. So decline 1248c2ecf20Sopenharmony_ci attaching to addresses >= 0x51 on DDC buses */ 1258c2ecf20Sopenharmony_ci if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51) 1268c2ecf20Sopenharmony_ci return -ENODEV; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* There are four ways we can read the EEPROM data: 1298c2ecf20Sopenharmony_ci (1) I2C block reads (faster, but unsupported by most adapters) 1308c2ecf20Sopenharmony_ci (2) Word reads (128% overhead) 1318c2ecf20Sopenharmony_ci (3) Consecutive byte reads (88% overhead, unsafe) 1328c2ecf20Sopenharmony_ci (4) Regular byte data reads (265% overhead) 1338c2ecf20Sopenharmony_ci The third and fourth methods are not implemented by this driver 1348c2ecf20Sopenharmony_ci because all known adapters support one of the first two. */ 1358c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA) 1368c2ecf20Sopenharmony_ci && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 1378c2ecf20Sopenharmony_ci return -ENODEV; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci strlcpy(info->type, "eeprom", I2C_NAME_SIZE); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci return 0; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic int eeprom_probe(struct i2c_client *client, 1458c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 1488c2ecf20Sopenharmony_ci struct eeprom_data *data; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci data = devm_kzalloc(&client->dev, sizeof(struct eeprom_data), 1518c2ecf20Sopenharmony_ci GFP_KERNEL); 1528c2ecf20Sopenharmony_ci if (!data) 1538c2ecf20Sopenharmony_ci return -ENOMEM; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci memset(data->data, 0xff, EEPROM_SIZE); 1568c2ecf20Sopenharmony_ci i2c_set_clientdata(client, data); 1578c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 1588c2ecf20Sopenharmony_ci data->nature = UNKNOWN; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* Detect the Vaio nature of EEPROMs. 1618c2ecf20Sopenharmony_ci We use the "PCG-" or "VGN-" prefix as the signature. */ 1628c2ecf20Sopenharmony_ci if (client->addr == 0x57 1638c2ecf20Sopenharmony_ci && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) { 1648c2ecf20Sopenharmony_ci char name[4]; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci name[0] = i2c_smbus_read_byte_data(client, 0x80); 1678c2ecf20Sopenharmony_ci name[1] = i2c_smbus_read_byte_data(client, 0x81); 1688c2ecf20Sopenharmony_ci name[2] = i2c_smbus_read_byte_data(client, 0x82); 1698c2ecf20Sopenharmony_ci name[3] = i2c_smbus_read_byte_data(client, 0x83); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) { 1728c2ecf20Sopenharmony_ci dev_info(&client->dev, "Vaio EEPROM detected, " 1738c2ecf20Sopenharmony_ci "enabling privacy protection\n"); 1748c2ecf20Sopenharmony_ci data->nature = VAIO; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci /* Let the users know they are using deprecated driver */ 1798c2ecf20Sopenharmony_ci dev_notice(&client->dev, 1808c2ecf20Sopenharmony_ci "eeprom driver is deprecated, please use at24 instead\n"); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* create the sysfs eeprom file */ 1838c2ecf20Sopenharmony_ci return sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr); 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic int eeprom_remove(struct i2c_client *client) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci return 0; 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic const struct i2c_device_id eeprom_id[] = { 1948c2ecf20Sopenharmony_ci { "eeprom", 0 }, 1958c2ecf20Sopenharmony_ci { } 1968c2ecf20Sopenharmony_ci}; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic struct i2c_driver eeprom_driver = { 1998c2ecf20Sopenharmony_ci .driver = { 2008c2ecf20Sopenharmony_ci .name = "eeprom", 2018c2ecf20Sopenharmony_ci }, 2028c2ecf20Sopenharmony_ci .probe = eeprom_probe, 2038c2ecf20Sopenharmony_ci .remove = eeprom_remove, 2048c2ecf20Sopenharmony_ci .id_table = eeprom_id, 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci .class = I2C_CLASS_DDC | I2C_CLASS_SPD, 2078c2ecf20Sopenharmony_ci .detect = eeprom_detect, 2088c2ecf20Sopenharmony_ci .address_list = normal_i2c, 2098c2ecf20Sopenharmony_ci}; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cimodule_i2c_driver(eeprom_driver); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciMODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " 2148c2ecf20Sopenharmony_ci "Philip Edelbrock <phil@netroedge.com> and " 2158c2ecf20Sopenharmony_ci "Greg Kroah-Hartman <greg@kroah.com>"); 2168c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("I2C EEPROM driver"); 2178c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 218