2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
|
|
|
|
Philip Edelbrock <phil@netroedge.com>
|
|
|
|
Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
|
|
|
|
Copyright (C) 2003 IBM Corp.
|
2008-07-14 20:38:34 +00:00
|
|
|
Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/i2c.h>
|
2006-01-18 22:16:04 +00:00
|
|
|
#include <linux/mutex.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Addresses to scan */
|
2008-01-27 17:14:47 +00:00
|
|
|
static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
|
2005-04-16 22:20:36 +00:00
|
|
|
0x55, 0x56, 0x57, I2C_CLIENT_END };
|
|
|
|
|
|
|
|
|
|
|
|
/* Size of EEPROM in bytes */
|
|
|
|
#define EEPROM_SIZE 256
|
|
|
|
|
|
|
|
/* possible types of eeprom devices */
|
|
|
|
enum eeprom_nature {
|
|
|
|
UNKNOWN,
|
|
|
|
VAIO,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Each client has this additional data */
|
|
|
|
struct eeprom_data {
|
2006-01-18 22:16:04 +00:00
|
|
|
struct mutex update_lock;
|
2005-04-16 22:20:36 +00:00
|
|
|
u8 valid; /* bitfield, bit!=0 if slice is valid */
|
|
|
|
unsigned long last_updated[8]; /* In jiffies, 8 slices */
|
|
|
|
u8 data[EEPROM_SIZE]; /* Register values */
|
|
|
|
enum eeprom_nature nature;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void eeprom_update_client(struct i2c_client *client, u8 slice)
|
|
|
|
{
|
|
|
|
struct eeprom_data *data = i2c_get_clientdata(client);
|
2008-07-14 20:38:29 +00:00
|
|
|
int i;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-01-18 22:16:04 +00:00
|
|
|
mutex_lock(&data->update_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (!(data->valid & (1 << slice)) ||
|
|
|
|
time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
|
|
|
|
dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
|
|
|
|
|
|
|
|
if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
|
i2c: Fix the i2c_smbus_read_i2c_block_data() prototype
Let the drivers specify how many bytes they want to read with
i2c_smbus_read_i2c_block_data(). So far, the block count was
hard-coded to I2C_SMBUS_BLOCK_MAX (32), which did not make much sense.
Many driver authors complained about this before, and I believe it's
about time to fix it. Right now, authors have to do technically stupid
things, such as individual byte reads or full-fledged I2C messaging,
to work around the problem. We do not want to encourage that.
I even found that some bus drivers (e.g. i2c-amd8111) already
implemented I2C block read the "right" way, that is, they didn't
follow the old, broken standard. The fact that it was never noticed
before just shows how little i2c_smbus_read_i2c_block_data() was used,
which isn't that surprising given how broken its prototype was so far.
There are some obvious compatiblity considerations:
* This changes the i2c_smbus_read_i2c_block_data() prototype. Users
outside the kernel tree will notice at compilation time, and will
have to update their code.
* User-space has access to i2c_smbus_xfer() directly using i2c-dev, so
the changed expectations would affect tools such as i2cdump. In order
to preserve binary compatibility, we give I2C_SMBUS_I2C_BLOCK_DATA
a new numeric value, and define I2C_SMBUS_I2C_BLOCK_BROKEN with the
old numeric value. When i2c-dev receives a transaction with the
old value, it can convert it to the new format on the fly.
Signed-off-by: Jean Delvare <khali@linux-fr.org>
2007-07-12 12:12:29 +00:00
|
|
|
for (i = slice << 5; i < (slice + 1) << 5; i += 32)
|
|
|
|
if (i2c_smbus_read_i2c_block_data(client, i,
|
|
|
|
32, data->data + i)
|
|
|
|
!= 32)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto exit;
|
|
|
|
} else {
|
2008-07-14 20:38:29 +00:00
|
|
|
for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
|
|
|
|
int word = i2c_smbus_read_word_data(client, i);
|
|
|
|
if (word < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto exit;
|
2008-07-14 20:38:29 +00:00
|
|
|
data->data[i] = word & 0xff;
|
|
|
|
data->data[i + 1] = word >> 8;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data->last_updated[slice] = jiffies;
|
|
|
|
data->valid |= (1 << slice);
|
|
|
|
}
|
|
|
|
exit:
|
2006-01-18 22:16:04 +00:00
|
|
|
mutex_unlock(&data->update_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-06-09 05:57:22 +00:00
|
|
|
static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t off, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
|
|
|
|
struct eeprom_data *data = i2c_get_clientdata(client);
|
|
|
|
u8 slice;
|
|
|
|
|
|
|
|
if (off > EEPROM_SIZE)
|
|
|
|
return 0;
|
|
|
|
if (off + count > EEPROM_SIZE)
|
|
|
|
count = EEPROM_SIZE - off;
|
|
|
|
|
|
|
|
/* Only refresh slices which contain requested bytes */
|
|
|
|
for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
|
|
|
|
eeprom_update_client(client, slice);
|
|
|
|
|
2007-11-15 18:24:03 +00:00
|
|
|
/* Hide Vaio private settings to regular users:
|
|
|
|
- BIOS passwords: bytes 0x00 to 0x0f
|
|
|
|
- UUID: bytes 0x10 to 0x1f
|
|
|
|
- Serial number: 0xc0 to 0xdf */
|
|
|
|
if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if ((off + i <= 0x1f) ||
|
|
|
|
(off + i >= 0xc0 && off + i <= 0xdf))
|
|
|
|
buf[i] = 0;
|
|
|
|
else
|
|
|
|
buf[i] = data->data[off + i];
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
memcpy(buf, &data->data[off], count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bin_attribute eeprom_attr = {
|
|
|
|
.attr = {
|
|
|
|
.name = "eeprom",
|
|
|
|
.mode = S_IRUGO,
|
|
|
|
},
|
|
|
|
.size = EEPROM_SIZE,
|
|
|
|
.read = eeprom_read,
|
|
|
|
};
|
|
|
|
|
2008-07-16 17:30:05 +00:00
|
|
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
2009-12-14 20:17:23 +00:00
|
|
|
static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-07-16 17:30:05 +00:00
|
|
|
struct i2c_adapter *adapter = client->adapter;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-07-14 20:38:29 +00:00
|
|
|
/* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
|
|
|
|
addresses 0x50-0x57, but we only care about 0x50. So decline
|
|
|
|
attaching to addresses >= 0x51 on DDC buses */
|
2008-07-16 17:30:05 +00:00
|
|
|
if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
|
|
|
|
return -ENODEV;
|
2008-07-14 20:38:29 +00:00
|
|
|
|
2008-07-14 20:38:29 +00:00
|
|
|
/* There are four ways we can read the EEPROM data:
|
2005-04-16 22:20:36 +00:00
|
|
|
(1) I2C block reads (faster, but unsupported by most adapters)
|
2008-07-14 20:38:29 +00:00
|
|
|
(2) Word reads (128% overhead)
|
|
|
|
(3) Consecutive byte reads (88% overhead, unsafe)
|
|
|
|
(4) Regular byte data reads (265% overhead)
|
|
|
|
The third and fourth methods are not implemented by this driver
|
|
|
|
because all known adapters support one of the first two. */
|
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
|
|
|
|
&& !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
|
2008-07-16 17:30:05 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eeprom_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
|
|
|
struct i2c_adapter *adapter = client->adapter;
|
|
|
|
struct eeprom_data *data;
|
|
|
|
int err;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-10-17 21:09:43 +00:00
|
|
|
if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
|
2005-04-16 22:20:36 +00:00
|
|
|
err = -ENOMEM;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(data->data, 0xff, EEPROM_SIZE);
|
2008-07-14 20:38:36 +00:00
|
|
|
i2c_set_clientdata(client, data);
|
2006-01-18 22:16:04 +00:00
|
|
|
mutex_init(&data->update_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
data->nature = UNKNOWN;
|
|
|
|
|
|
|
|
/* Detect the Vaio nature of EEPROMs.
|
2007-11-15 18:24:03 +00:00
|
|
|
We use the "PCG-" or "VGN-" prefix as the signature. */
|
2008-07-16 17:30:05 +00:00
|
|
|
if (client->addr == 0x57
|
2008-07-14 20:38:29 +00:00
|
|
|
&& i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
|
2007-11-15 18:24:03 +00:00
|
|
|
char name[4];
|
|
|
|
|
2008-07-14 20:38:36 +00:00
|
|
|
name[0] = i2c_smbus_read_byte_data(client, 0x80);
|
|
|
|
name[1] = i2c_smbus_read_byte_data(client, 0x81);
|
|
|
|
name[2] = i2c_smbus_read_byte_data(client, 0x82);
|
|
|
|
name[3] = i2c_smbus_read_byte_data(client, 0x83);
|
2007-11-15 18:24:03 +00:00
|
|
|
|
|
|
|
if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
|
2008-07-14 20:38:36 +00:00
|
|
|
dev_info(&client->dev, "Vaio EEPROM detected, "
|
2007-11-15 18:24:03 +00:00
|
|
|
"enabling privacy protection\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
data->nature = VAIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the sysfs eeprom file */
|
2008-07-14 20:38:36 +00:00
|
|
|
err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
|
2006-09-03 20:20:24 +00:00
|
|
|
if (err)
|
2008-07-16 17:30:05 +00:00
|
|
|
goto exit_kfree;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
exit_kfree:
|
|
|
|
kfree(data);
|
|
|
|
exit:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-07-16 17:30:05 +00:00
|
|
|
static int eeprom_remove(struct i2c_client *client)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-09-03 20:20:24 +00:00
|
|
|
sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(i2c_get_clientdata(client));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-16 17:30:05 +00:00
|
|
|
static const struct i2c_device_id eeprom_id[] = {
|
|
|
|
{ "eeprom", 0 },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct i2c_driver eeprom_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "eeprom",
|
|
|
|
},
|
|
|
|
.probe = eeprom_probe,
|
|
|
|
.remove = eeprom_remove,
|
|
|
|
.id_table = eeprom_id,
|
|
|
|
|
|
|
|
.class = I2C_CLASS_DDC | I2C_CLASS_SPD,
|
|
|
|
.detect = eeprom_detect,
|
2009-12-14 20:17:25 +00:00
|
|
|
.address_list = normal_i2c,
|
2008-07-16 17:30:05 +00:00
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int __init eeprom_init(void)
|
|
|
|
{
|
|
|
|
return i2c_add_driver(&eeprom_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit eeprom_exit(void)
|
|
|
|
{
|
|
|
|
i2c_del_driver(&eeprom_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
|
|
|
|
"Philip Edelbrock <phil@netroedge.com> and "
|
|
|
|
"Greg Kroah-Hartman <greg@kroah.com>");
|
|
|
|
MODULE_DESCRIPTION("I2C EEPROM driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
module_init(eeprom_init);
|
|
|
|
module_exit(eeprom_exit);
|