aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/chips/eeprom.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/i2c/chips/eeprom.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/i2c/chips/eeprom.c')
-rw-r--r--drivers/i2c/chips/eeprom.c264
1 files changed, 264 insertions, 0 deletions
diff --git a/drivers/i2c/chips/eeprom.c b/drivers/i2c/chips/eeprom.c
new file mode 100644
index 000000000000..cbdfa2db6f7c
--- /dev/null
+++ b/drivers/i2c/chips/eeprom.c
@@ -0,0 +1,264 @@
1/*
2 eeprom.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
6 Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 Copyright (C) 2003 IBM Corp.
8
9 2004-01-16 Jean Delvare <khali@linux-fr.org>
10 Divide the eeprom in 32-byte (arbitrary) slices. This significantly
11 speeds sensors up, as well as various scripts using the eeprom
12 module.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28
29#include <linux/config.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/slab.h>
34#include <linux/sched.h>
35#include <linux/jiffies.h>
36#include <linux/i2c.h>
37#include <linux/i2c-sensor.h>
38
39/* Addresses to scan */
40static unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
41 0x55, 0x56, 0x57, I2C_CLIENT_END };
42static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
43
44/* Insmod parameters */
45SENSORS_INSMOD_1(eeprom);
46
47
48/* Size of EEPROM in bytes */
49#define EEPROM_SIZE 256
50
51/* possible types of eeprom devices */
52enum eeprom_nature {
53 UNKNOWN,
54 VAIO,
55};
56
57/* Each client has this additional data */
58struct eeprom_data {
59 struct i2c_client client;
60 struct semaphore update_lock;
61 u8 valid; /* bitfield, bit!=0 if slice is valid */
62 unsigned long last_updated[8]; /* In jiffies, 8 slices */
63 u8 data[EEPROM_SIZE]; /* Register values */
64 enum eeprom_nature nature;
65};
66
67
68static int eeprom_attach_adapter(struct i2c_adapter *adapter);
69static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
70static int eeprom_detach_client(struct i2c_client *client);
71
72/* This is the driver that will be inserted */
73static struct i2c_driver eeprom_driver = {
74 .owner = THIS_MODULE,
75 .name = "eeprom",
76 .id = I2C_DRIVERID_EEPROM,
77 .flags = I2C_DF_NOTIFY,
78 .attach_adapter = eeprom_attach_adapter,
79 .detach_client = eeprom_detach_client,
80};
81
82static void eeprom_update_client(struct i2c_client *client, u8 slice)
83{
84 struct eeprom_data *data = i2c_get_clientdata(client);
85 int i, j;
86
87 down(&data->update_lock);
88
89 if (!(data->valid & (1 << slice)) ||
90 time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
91 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
92
93 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
94 for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_I2C_BLOCK_MAX)
95 if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_I2C_BLOCK_MAX)
96 goto exit;
97 } else {
98 if (i2c_smbus_write_byte(client, slice << 5)) {
99 dev_dbg(&client->dev, "eeprom read start has failed!\n");
100 goto exit;
101 }
102 for (i = slice << 5; i < (slice + 1) << 5; i++) {
103 j = i2c_smbus_read_byte(client);
104 if (j < 0)
105 goto exit;
106 data->data[i] = (u8) j;
107 }
108 }
109 data->last_updated[slice] = jiffies;
110 data->valid |= (1 << slice);
111 }
112exit:
113 up(&data->update_lock);
114}
115
116static ssize_t eeprom_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
117{
118 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
119 struct eeprom_data *data = i2c_get_clientdata(client);
120 u8 slice;
121
122 if (off > EEPROM_SIZE)
123 return 0;
124 if (off + count > EEPROM_SIZE)
125 count = EEPROM_SIZE - off;
126
127 /* Only refresh slices which contain requested bytes */
128 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
129 eeprom_update_client(client, slice);
130
131 /* Hide Vaio security settings to regular users (16 first bytes) */
132 if (data->nature == VAIO && off < 16 && !capable(CAP_SYS_ADMIN)) {
133 size_t in_row1 = 16 - off;
134 in_row1 = min(in_row1, count);
135 memset(buf, 0, in_row1);
136 if (count - in_row1 > 0)
137 memcpy(buf + in_row1, &data->data[16], count - in_row1);
138 } else {
139 memcpy(buf, &data->data[off], count);
140 }
141
142 return count;
143}
144
145static struct bin_attribute eeprom_attr = {
146 .attr = {
147 .name = "eeprom",
148 .mode = S_IRUGO,
149 .owner = THIS_MODULE,
150 },
151 .size = EEPROM_SIZE,
152 .read = eeprom_read,
153};
154
155static int eeprom_attach_adapter(struct i2c_adapter *adapter)
156{
157 return i2c_detect(adapter, &addr_data, eeprom_detect);
158}
159
160/* This function is called by i2c_detect */
161int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
162{
163 struct i2c_client *new_client;
164 struct eeprom_data *data;
165 int err = 0;
166
167 /* There are three ways we can read the EEPROM data:
168 (1) I2C block reads (faster, but unsupported by most adapters)
169 (2) Consecutive byte reads (100% overhead)
170 (3) Regular byte data reads (200% overhead)
171 The third method is not implemented by this driver because all
172 known adapters support at least the second. */
173 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA
174 | I2C_FUNC_SMBUS_BYTE))
175 goto exit;
176
177 /* OK. For now, we presume we have a valid client. We now create the
178 client structure, even though we cannot fill it completely yet.
179 But it allows us to access eeprom_{read,write}_value. */
180 if (!(data = kmalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
181 err = -ENOMEM;
182 goto exit;
183 }
184 memset(data, 0, sizeof(struct eeprom_data));
185
186 new_client = &data->client;
187 memset(data->data, 0xff, EEPROM_SIZE);
188 i2c_set_clientdata(new_client, data);
189 new_client->addr = address;
190 new_client->adapter = adapter;
191 new_client->driver = &eeprom_driver;
192 new_client->flags = 0;
193
194 /* prevent 24RF08 corruption */
195 i2c_smbus_write_quick(new_client, 0);
196
197 /* Fill in the remaining client fields */
198 strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
199 data->valid = 0;
200 init_MUTEX(&data->update_lock);
201 data->nature = UNKNOWN;
202
203 /* Tell the I2C layer a new client has arrived */
204 if ((err = i2c_attach_client(new_client)))
205 goto exit_kfree;
206
207 /* Detect the Vaio nature of EEPROMs.
208 We use the "PCG-" prefix as the signature. */
209 if (address == 0x57) {
210 if (i2c_smbus_read_byte_data(new_client, 0x80) == 'P'
211 && i2c_smbus_read_byte(new_client) == 'C'
212 && i2c_smbus_read_byte(new_client) == 'G'
213 && i2c_smbus_read_byte(new_client) == '-') {
214 dev_info(&new_client->dev, "Vaio EEPROM detected, "
215 "enabling password protection\n");
216 data->nature = VAIO;
217 }
218 }
219
220 /* create the sysfs eeprom file */
221 sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
222
223 return 0;
224
225exit_kfree:
226 kfree(data);
227exit:
228 return err;
229}
230
231static int eeprom_detach_client(struct i2c_client *client)
232{
233 int err;
234
235 err = i2c_detach_client(client);
236 if (err) {
237 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
238 return err;
239 }
240
241 kfree(i2c_get_clientdata(client));
242
243 return 0;
244}
245
246static int __init eeprom_init(void)
247{
248 return i2c_add_driver(&eeprom_driver);
249}
250
251static void __exit eeprom_exit(void)
252{
253 i2c_del_driver(&eeprom_driver);
254}
255
256
257MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
258 "Philip Edelbrock <phil@netroedge.com> and "
259 "Greg Kroah-Hartman <greg@kroah.com>");
260MODULE_DESCRIPTION("I2C EEPROM driver");
261MODULE_LICENSE("GPL");
262
263module_init(eeprom_init);
264module_exit(eeprom_exit);