Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  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 <khali@linux-fr.org>
  7 *
  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.
 12 *
 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.
 17 */
 18
 19#include <linux/kernel.h>
 20#include <linux/init.h>
 21#include <linux/module.h>
 22#include <linux/slab.h>
 
 23#include <linux/jiffies.h>
 24#include <linux/i2c.h>
 25#include <linux/mutex.h>
 26
 27/* Addresses to scan */
 28static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
 29					0x55, 0x56, 0x57, I2C_CLIENT_END };
 30
 31
 32/* Size of EEPROM in bytes */
 33#define EEPROM_SIZE		256
 34
 35/* possible types of eeprom devices */
 36enum eeprom_nature {
 37	UNKNOWN,
 38	VAIO,
 39};
 40
 41/* Each client has this additional data */
 42struct eeprom_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;
 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(container_of(kobj, struct device, kobj));
 89	struct eeprom_data *data = i2c_get_clientdata(client);
 90	u8 slice;
 91
 92	if (off > EEPROM_SIZE)
 93		return 0;
 94	if (off + count > EEPROM_SIZE)
 95		count = EEPROM_SIZE - off;
 96
 97	/* Only refresh slices which contain requested bytes */
 98	for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
 99		eeprom_update_client(client, slice);
100
101	/* Hide Vaio private settings to regular users:
102	   - BIOS passwords: bytes 0x00 to 0x0f
103	   - UUID: bytes 0x10 to 0x1f
104	   - Serial number: 0xc0 to 0xdf */
105	if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
106		int i;
107
108		for (i = 0; i < count; i++) {
109			if ((off + i <= 0x1f) ||
110			    (off + i >= 0xc0 && off + i <= 0xdf))
111				buf[i] = 0;
112			else
113				buf[i] = data->data[off + i];
114		}
115	} else {
116		memcpy(buf, &data->data[off], count);
117	}
118
119	return count;
120}
121
122static struct bin_attribute eeprom_attr = {
123	.attr = {
124		.name = "eeprom",
125		.mode = S_IRUGO,
126	},
127	.size = EEPROM_SIZE,
128	.read = eeprom_read,
129};
130
131/* Return 0 if detection is successful, -ENODEV otherwise */
132static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
133{
134	struct i2c_adapter *adapter = client->adapter;
135
136	/* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
137	   addresses 0x50-0x57, but we only care about 0x50. So decline
138	   attaching to addresses >= 0x51 on DDC buses */
139	if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
140		return -ENODEV;
141
142	/* There are four ways we can read the EEPROM data:
143	   (1) I2C block reads (faster, but unsupported by most adapters)
144	   (2) Word reads (128% overhead)
145	   (3) Consecutive byte reads (88% overhead, unsafe)
146	   (4) Regular byte data reads (265% overhead)
147	   The third and fourth methods are not implemented by this driver
148	   because all known adapters support one of the first two. */
149	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
150	 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
151		return -ENODEV;
152
153	strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
154
155	return 0;
156}
157
158static int eeprom_probe(struct i2c_client *client,
159			const struct i2c_device_id *id)
160{
161	struct i2c_adapter *adapter = client->adapter;
162	struct eeprom_data *data;
163	int err;
164
165	if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
166		err = -ENOMEM;
167		goto exit;
168	}
169
170	memset(data->data, 0xff, EEPROM_SIZE);
171	i2c_set_clientdata(client, data);
172	mutex_init(&data->update_lock);
173	data->nature = UNKNOWN;
174
175	/* Detect the Vaio nature of EEPROMs.
176	   We use the "PCG-" or "VGN-" prefix as the signature. */
177	if (client->addr == 0x57
178	 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
179		char name[4];
180
181		name[0] = i2c_smbus_read_byte_data(client, 0x80);
182		name[1] = i2c_smbus_read_byte_data(client, 0x81);
183		name[2] = i2c_smbus_read_byte_data(client, 0x82);
184		name[3] = i2c_smbus_read_byte_data(client, 0x83);
185
186		if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
187			dev_info(&client->dev, "Vaio EEPROM detected, "
188				 "enabling privacy protection\n");
189			data->nature = VAIO;
190		}
191	}
192
193	/* create the sysfs eeprom file */
194	err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
195	if (err)
196		goto exit_kfree;
197
198	return 0;
199
200exit_kfree:
201	kfree(data);
202exit:
203	return err;
204}
205
206static int eeprom_remove(struct i2c_client *client)
207{
208	sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
209	kfree(i2c_get_clientdata(client));
210
211	return 0;
212}
213
214static const struct i2c_device_id eeprom_id[] = {
215	{ "eeprom", 0 },
216	{ }
217};
218
219static struct i2c_driver eeprom_driver = {
220	.driver = {
221		.name	= "eeprom",
222	},
223	.probe		= eeprom_probe,
224	.remove		= eeprom_remove,
225	.id_table	= eeprom_id,
226
227	.class		= I2C_CLASS_DDC | I2C_CLASS_SPD,
228	.detect		= eeprom_detect,
229	.address_list	= normal_i2c,
230};
231
232static int __init eeprom_init(void)
233{
234	return i2c_add_driver(&eeprom_driver);
235}
236
237static void __exit eeprom_exit(void)
238{
239	i2c_del_driver(&eeprom_driver);
240}
241
242
243MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
244		"Philip Edelbrock <phil@netroedge.com> and "
245		"Greg Kroah-Hartman <greg@kroah.com>");
246MODULE_DESCRIPTION("I2C EEPROM driver");
247MODULE_LICENSE("GPL");
248
249module_init(eeprom_init);
250module_exit(eeprom_exit);
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * 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.
  7 * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de>
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/kernel.h>
 
 11#include <linux/module.h>
 12#include <linux/device.h>
 13#include <linux/capability.h>
 14#include <linux/jiffies.h>
 15#include <linux/i2c.h>
 16#include <linux/mutex.h>
 17
 18/* Addresses to scan */
 19static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
 20					0x55, 0x56, 0x57, I2C_CLIENT_END };
 21
 22
 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 {
 34	struct mutex update_lock;
 35	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
 42static void eeprom_update_client(struct i2c_client *client, u8 slice)
 43{
 44	struct eeprom_data *data = i2c_get_clientdata(client);
 45	int i;
 46
 47	mutex_lock(&data->update_lock);
 48
 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)) {
 54			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)
 58					goto exit;
 59		} else {
 60			for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
 61				int word = i2c_smbus_read_word_data(client, i);
 62				if (word < 0)
 63					goto exit;
 64				data->data[i] = word & 0xff;
 65				data->data[i + 1] = word >> 8;
 66			}
 67		}
 68		data->last_updated[slice] = jiffies;
 69		data->valid |= (1 << slice);
 70	}
 71exit:
 72	mutex_unlock(&data->update_lock);
 73}
 74
 75static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
 76			   struct bin_attribute *bin_attr,
 77			   char *buf, loff_t off, size_t count)
 78{
 79	struct i2c_client *client = kobj_to_i2c_client(kobj);
 80	struct eeprom_data *data = i2c_get_clientdata(client);
 81	u8 slice;
 82
 
 
 
 
 
 83	/* 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
 87	/* 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		}
101	} else {
102		memcpy(buf, &data->data[off], count);
103	}
104
105	return count;
106}
107
108static const struct bin_attribute eeprom_attr = {
109	.attr = {
110		.name = "eeprom",
111		.mode = S_IRUGO,
112	},
113	.size = EEPROM_SIZE,
114	.read = eeprom_read,
115};
116
117/* Return 0 if detection is successful, -ENODEV otherwise */
118static int eeprom_detect(struct i2c_client *client, struct i2c_board_info *info)
119{
120	struct i2c_adapter *adapter = client->adapter;
121
122	/* 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 */
125	if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
126		return -ENODEV;
127
128	/* There are four ways we can read the EEPROM data:
129	   (1) I2C block reads (faster, but unsupported by most adapters)
130	   (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))
137		return -ENODEV;
138
139	strscpy(info->type, "eeprom", I2C_NAME_SIZE);
140
141	return 0;
142}
143
144static int eeprom_probe(struct i2c_client *client)
 
145{
146	struct i2c_adapter *adapter = client->adapter;
147	struct eeprom_data *data;
 
148
149	data = devm_kzalloc(&client->dev, sizeof(struct eeprom_data),
150			    GFP_KERNEL);
151	if (!data)
152		return -ENOMEM;
153
154	memset(data->data, 0xff, EEPROM_SIZE);
155	i2c_set_clientdata(client, data);
156	mutex_init(&data->update_lock);
157	data->nature = UNKNOWN;
158
159	/* Detect the Vaio nature of EEPROMs.
160	   We use the "PCG-" or "VGN-" prefix as the signature. */
161	if (client->addr == 0x57
162	 && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
163		char name[4];
164
165		name[0] = i2c_smbus_read_byte_data(client, 0x80);
166		name[1] = i2c_smbus_read_byte_data(client, 0x81);
167		name[2] = i2c_smbus_read_byte_data(client, 0x82);
168		name[3] = i2c_smbus_read_byte_data(client, 0x83);
169
170		if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
171			dev_info(&client->dev, "Vaio EEPROM detected, "
172				 "enabling privacy protection\n");
173			data->nature = VAIO;
174		}
175	}
176
177	/* Let the users know they are using deprecated driver */
178	dev_notice(&client->dev,
179		   "eeprom driver is deprecated, please use at24 instead\n");
 
 
 
180
181	/* create the sysfs eeprom file */
182	return sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
 
 
183}
184
185static void eeprom_remove(struct i2c_client *client)
186{
187	sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
 
 
 
188}
189
190static const struct i2c_device_id eeprom_id[] = {
191	{ "eeprom", 0 },
192	{ }
193};
194
195static struct i2c_driver eeprom_driver = {
196	.driver = {
197		.name	= "eeprom",
198	},
199	.probe_new	= eeprom_probe,
200	.remove		= eeprom_remove,
201	.id_table	= eeprom_id,
202
203	.class		= I2C_CLASS_DDC | I2C_CLASS_SPD,
204	.detect		= eeprom_detect,
205	.address_list	= normal_i2c,
206};
207
208module_i2c_driver(eeprom_driver);
 
 
 
 
 
 
 
 
 
209
210MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
211		"Philip Edelbrock <phil@netroedge.com> and "
212		"Greg Kroah-Hartman <greg@kroah.com>");
213MODULE_DESCRIPTION("I2C EEPROM driver");
214MODULE_LICENSE("GPL");