2 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
3 * Philip Edelbrock <phil@netroedge.com>
4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2003 IBM Corp.
6 * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
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>
27 /* Addresses to scan */
28 static const unsigned short normal_i2c
[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
29 0x55, 0x56, 0x57, I2C_CLIENT_END
};
32 /* Size of EEPROM in bytes */
33 #define EEPROM_SIZE 256
35 /* possible types of eeprom devices */
41 /* Each client has this additional data */
43 struct mutex update_lock
;
44 u8 valid
; /* bitfield, bit!=0 if slice is valid */
45 unsigned long last_updated
[8]; /* In jiffies, 8 slices */
46 u8 data
[EEPROM_SIZE
]; /* Register values */
47 enum eeprom_nature nature
;
51 static void eeprom_update_client(struct i2c_client
*client
, u8 slice
)
53 struct eeprom_data
*data
= i2c_get_clientdata(client
);
56 mutex_lock(&data
->update_lock
);
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
);
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
,
69 for (i
= slice
<< 5; i
< (slice
+ 1) << 5; i
+= 2) {
70 int word
= i2c_smbus_read_word_data(client
, i
);
73 data
->data
[i
] = word
& 0xff;
74 data
->data
[i
+ 1] = word
>> 8;
77 data
->last_updated
[slice
] = jiffies
;
78 data
->valid
|= (1 << slice
);
81 mutex_unlock(&data
->update_lock
);
84 static 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
)
88 struct i2c_client
*client
= to_i2c_client(kobj_to_dev(kobj
));
89 struct eeprom_data
*data
= i2c_get_clientdata(client
);
92 /* Only refresh slices which contain requested bytes */
93 for (slice
= off
>> 5; slice
<= (off
+ count
- 1) >> 5; slice
++)
94 eeprom_update_client(client
, slice
);
96 /* Hide Vaio private settings to regular users:
97 - BIOS passwords: bytes 0x00 to 0x0f
98 - UUID: bytes 0x10 to 0x1f
99 - Serial number: 0xc0 to 0xdf */
100 if (data
->nature
== VAIO
&& !capable(CAP_SYS_ADMIN
)) {
103 for (i
= 0; i
< count
; i
++) {
104 if ((off
+ i
<= 0x1f) ||
105 (off
+ i
>= 0xc0 && off
+ i
<= 0xdf))
108 buf
[i
] = data
->data
[off
+ i
];
111 memcpy(buf
, &data
->data
[off
], count
);
117 static const struct bin_attribute eeprom_attr
= {
126 /* Return 0 if detection is successful, -ENODEV otherwise */
127 static int eeprom_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
129 struct i2c_adapter
*adapter
= client
->adapter
;
131 /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
132 addresses 0x50-0x57, but we only care about 0x50. So decline
133 attaching to addresses >= 0x51 on DDC buses */
134 if (!(adapter
->class & I2C_CLASS_SPD
) && client
->addr
>= 0x51)
137 /* There are four ways we can read the EEPROM data:
138 (1) I2C block reads (faster, but unsupported by most adapters)
139 (2) Word reads (128% overhead)
140 (3) Consecutive byte reads (88% overhead, unsafe)
141 (4) Regular byte data reads (265% overhead)
142 The third and fourth methods are not implemented by this driver
143 because all known adapters support one of the first two. */
144 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_READ_WORD_DATA
)
145 && !i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_READ_I2C_BLOCK
))
148 strlcpy(info
->type
, "eeprom", I2C_NAME_SIZE
);
153 static int eeprom_probe(struct i2c_client
*client
,
154 const struct i2c_device_id
*id
)
156 struct i2c_adapter
*adapter
= client
->adapter
;
157 struct eeprom_data
*data
;
159 data
= devm_kzalloc(&client
->dev
, sizeof(struct eeprom_data
),
164 memset(data
->data
, 0xff, EEPROM_SIZE
);
165 i2c_set_clientdata(client
, data
);
166 mutex_init(&data
->update_lock
);
167 data
->nature
= UNKNOWN
;
169 /* Detect the Vaio nature of EEPROMs.
170 We use the "PCG-" or "VGN-" prefix as the signature. */
171 if (client
->addr
== 0x57
172 && i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_READ_BYTE_DATA
)) {
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);
180 if (!memcmp(name
, "PCG-", 4) || !memcmp(name
, "VGN-", 4)) {
181 dev_info(&client
->dev
, "Vaio EEPROM detected, "
182 "enabling privacy protection\n");
187 /* create the sysfs eeprom file */
188 return sysfs_create_bin_file(&client
->dev
.kobj
, &eeprom_attr
);
191 static int eeprom_remove(struct i2c_client
*client
)
193 sysfs_remove_bin_file(&client
->dev
.kobj
, &eeprom_attr
);
198 static const struct i2c_device_id eeprom_id
[] = {
203 static struct i2c_driver eeprom_driver
= {
207 .probe
= eeprom_probe
,
208 .remove
= eeprom_remove
,
209 .id_table
= eeprom_id
,
211 .class = I2C_CLASS_DDC
| I2C_CLASS_SPD
,
212 .detect
= eeprom_detect
,
213 .address_list
= normal_i2c
,
216 module_i2c_driver(eeprom_driver
);
218 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
219 "Philip Edelbrock <phil@netroedge.com> and "
220 "Greg Kroah-Hartman <greg@kroah.com>");
221 MODULE_DESCRIPTION("I2C EEPROM driver");
222 MODULE_LICENSE("GPL");