blob: 2cfe3d4ae144b67e877b5f547b360156f9d75eb6 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Arce, Abraham110f4222010-05-13 15:04:30 -05003 * 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 Delvare7c81c60f2014-01-29 20:40:08 +01007 * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de>
Arce, Abraham110f4222010-05-13 15:04:30 -05008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
Himangi Saraogi1698da22014-08-02 00:34:23 +053012#include <linux/device.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/jiffies.h>
15#include <linux/i2c.h>
Ingo Molnar5c085d32006-01-18 23:16:04 +010016#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/* Addresses to scan */
Jean Delvare2cdddeb2008-01-27 18:14:47 +010019static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 0x55, 0x56, 0x57, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* Size of EEPROM in bytes */
24#define EEPROM_SIZE 256
25
26/* possible types of eeprom devices */
27enum eeprom_nature {
28 UNKNOWN,
29 VAIO,
30};
31
32/* Each client has this additional data */
33struct eeprom_data {
Ingo Molnar5c085d32006-01-18 23:16:04 +010034 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 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 Torvalds1da177e2005-04-16 15:20:36 -070042static void eeprom_update_client(struct i2c_client *client, u8 slice)
43{
44 struct eeprom_data *data = i2c_get_clientdata(client);
Jean Delvare1b4dff92008-07-14 22:38:29 +020045 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Ingo Molnar5c085d32006-01-18 23:16:04 +010047 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
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 Delvare4b2643d2007-07-12 14:12:29 +020054 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 Torvalds1da177e2005-04-16 15:20:36 -070058 goto exit;
59 } else {
Jean Delvare1b4dff92008-07-14 22:38:29 +020060 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 Torvalds1da177e2005-04-16 15:20:36 -070063 goto exit;
Jean Delvare1b4dff92008-07-14 22:38:29 +020064 data->data[i] = word & 0xff;
65 data->data[i + 1] = word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67 }
68 data->last_updated[slice] = jiffies;
69 data->valid |= (1 << slice);
70 }
71exit:
Ingo Molnar5c085d32006-01-18 23:16:04 +010072 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Chris Wright2c3c8be2010-05-12 18:28:57 -070075static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
76 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +080077 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Geliang Tang092462c2016-01-13 23:30:11 +080079 struct i2c_client *client = to_i2c_client(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct eeprom_data *data = i2c_get_clientdata(client);
81 u8 slice;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* 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 Delvare0f2cbd32007-11-15 19:24:03 +010087 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700101 } else {
102 memcpy(buf, &data->data[off], count);
103 }
104
105 return count;
106}
107
Bhumika Goyal57daedf2017-08-02 14:40:06 +0530108static const struct bin_attribute eeprom_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .attr = {
110 .name = "eeprom",
111 .mode = S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 },
113 .size = EEPROM_SIZE,
114 .read = eeprom_read,
115};
116
Jean Delvare68be3362008-07-16 19:30:05 +0200117/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100118static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Jean Delvare68be3362008-07-16 19:30:05 +0200120 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jean Delvared4653bf2008-07-14 22:38:29 +0200122 /* 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 Delvare68be3362008-07-16 19:30:05 +0200125 if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
126 return -ENODEV;
Jean Delvared4653bf2008-07-14 22:38:29 +0200127
Jean Delvare1b4dff92008-07-14 22:38:29 +0200128 /* There are four ways we can read the EEPROM data:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 (1) I2C block reads (faster, but unsupported by most adapters)
Jean Delvare1b4dff92008-07-14 22:38:29 +0200130 (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 Delvare68be3362008-07-16 19:30:05 +0200137 return -ENODEV;
138
139 strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
140
141 return 0;
142}
143
144static 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 Torvalds1da177e2005-04-16 15:20:36 -0700149
Himangi Saraogi1698da22014-08-02 00:34:23 +0530150 data = devm_kzalloc(&client->dev, sizeof(struct eeprom_data),
151 GFP_KERNEL);
152 if (!data)
153 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 memset(data->data, 0xff, EEPROM_SIZE);
Jean Delvaref741f672008-07-14 22:38:36 +0200156 i2c_set_clientdata(client, data);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100157 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 data->nature = UNKNOWN;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* Detect the Vaio nature of EEPROMs.
Jean Delvare8b925a32007-11-15 19:24:03 +0100161 We use the "PCG-" or "VGN-" prefix as the signature. */
Jean Delvare68be3362008-07-16 19:30:05 +0200162 if (client->addr == 0x57
Jean Delvare1b4dff92008-07-14 22:38:29 +0200163 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
Jean Delvare8b925a32007-11-15 19:24:03 +0100164 char name[4];
165
Jean Delvaref741f672008-07-14 22:38:36 +0200166 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 Delvare8b925a32007-11-15 19:24:03 +0100170
171 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
Jean Delvaref741f672008-07-14 22:38:36 +0200172 dev_info(&client->dev, "Vaio EEPROM detected, "
Jean Delvare0f2cbd32007-11-15 19:24:03 +0100173 "enabling privacy protection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 data->nature = VAIO;
175 }
176 }
177
178 /* create the sysfs eeprom file */
Himangi Saraogi1698da22014-08-02 00:34:23 +0530179 return sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Jean Delvare68be3362008-07-16 19:30:05 +0200182static int eeprom_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jean Delvare7d9db672006-09-03 22:20:24 +0200184 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 return 0;
187}
188
Jean Delvare68be3362008-07-16 19:30:05 +0200189static const struct i2c_device_id eeprom_id[] = {
190 { "eeprom", 0 },
191 { }
192};
193
194static 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 Delvarec3813d62009-12-14 21:17:25 +0100204 .address_list = normal_i2c,
Jean Delvare68be3362008-07-16 19:30:05 +0200205};
206
Axel Lina64fe2e2012-01-22 15:36:45 +0800207module_i2c_driver(eeprom_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
210 "Philip Edelbrock <phil@netroedge.com> and "
211 "Greg Kroah-Hartman <greg@kroah.com>");
212MODULE_DESCRIPTION("I2C EEPROM driver");
213MODULE_LICENSE("GPL");