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