1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/device.h>
22#include <linux/capability.h>
23#include <linux/jiffies.h>
24#include <linux/i2c.h>
25#include <linux/mutex.h>
26
27
28static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
29 0x55, 0x56, 0x57, I2C_CLIENT_END };
30
31
32
33#define EEPROM_SIZE 256
34
35
36enum eeprom_nature {
37 UNKNOWN,
38 VAIO,
39};
40
41
42struct eeprom_data {
43 struct mutex update_lock;
44 u8 valid;
45 unsigned long last_updated[8];
46 u8 data[EEPROM_SIZE];
47 enum eeprom_nature nature;
48};
49
50
51static void eeprom_update_client(struct i2c_client *client, u8 slice)
52{
53 struct eeprom_data *data = i2c_get_clientdata(client);
54 int i;
55
56 mutex_lock(&data->update_lock);
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)) {
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)
67 goto exit;
68 } else {
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)
72 goto exit;
73 data->data[i] = word & 0xff;
74 data->data[i + 1] = word >> 8;
75 }
76 }
77 data->last_updated[slice] = jiffies;
78 data->valid |= (1 << slice);
79 }
80exit:
81 mutex_unlock(&data->update_lock);
82}
83
84static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
85 struct bin_attribute *bin_attr,
86 char *buf, loff_t off, size_t count)
87{
88 struct i2c_client *client = to_i2c_client(kobj_to_dev(kobj));
89 struct eeprom_data *data = i2c_get_clientdata(client);
90 u8 slice;
91
92
93 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
94 eeprom_update_client(client, slice);
95
96
97
98
99
100 if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
101 int i;
102
103 for (i = 0; i < count; i++) {
104 if ((off + i <= 0x1f) ||
105 (off + i >= 0xc0 && off + i <= 0xdf))
106 buf[i] = 0;
107 else
108 buf[i] = data->data[off + i];
109 }
110 } else {
111 memcpy(buf, &data->data[off], count);
112 }
113
114 return count;
115}
116
117static const struct bin_attribute eeprom_attr = {
118 .attr = {
119 .name = "eeprom",
120 .mode = S_IRUGO,
121 },
122 .size = EEPROM_SIZE,
123 .read = eeprom_read,
124};
125
126
127static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
128{
129 struct i2c_adapter *adapter = client->adapter;
130
131
132
133
134 if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
135 return -ENODEV;
136
137
138
139
140
141
142
143
144 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
145 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
146 return -ENODEV;
147
148 strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
149
150 return 0;
151}
152
153static int eeprom_probe(struct i2c_client *client,
154 const struct i2c_device_id *id)
155{
156 struct i2c_adapter *adapter = client->adapter;
157 struct eeprom_data *data;
158
159 data = devm_kzalloc(&client->dev, sizeof(struct eeprom_data),
160 GFP_KERNEL);
161 if (!data)
162 return -ENOMEM;
163
164 memset(data->data, 0xff, EEPROM_SIZE);
165 i2c_set_clientdata(client, data);
166 mutex_init(&data->update_lock);
167 data->nature = UNKNOWN;
168
169
170
171 if (client->addr == 0x57
172 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
173 char name[4];
174
175 name[0] = i2c_smbus_read_byte_data(client, 0x80);
176 name[1] = i2c_smbus_read_byte_data(client, 0x81);
177 name[2] = i2c_smbus_read_byte_data(client, 0x82);
178 name[3] = i2c_smbus_read_byte_data(client, 0x83);
179
180 if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
181 dev_info(&client->dev, "Vaio EEPROM detected, "
182 "enabling privacy protection\n");
183 data->nature = VAIO;
184 }
185 }
186
187
188 return sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
189}
190
191static int eeprom_remove(struct i2c_client *client)
192{
193 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
194
195 return 0;
196}
197
198static const struct i2c_device_id eeprom_id[] = {
199 { "eeprom", 0 },
200 { }
201};
202
203static struct i2c_driver eeprom_driver = {
204 .driver = {
205 .name = "eeprom",
206 },
207 .probe = eeprom_probe,
208 .remove = eeprom_remove,
209 .id_table = eeprom_id,
210
211 .class = I2C_CLASS_DDC | I2C_CLASS_SPD,
212 .detect = eeprom_detect,
213 .address_list = normal_i2c,
214};
215
216module_i2c_driver(eeprom_driver);
217
218MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
219 "Philip Edelbrock <phil@netroedge.com> and "
220 "Greg Kroah-Hartman <greg@kroah.com>");
221MODULE_DESCRIPTION("I2C EEPROM driver");
222MODULE_LICENSE("GPL");
223