sysfs: add parameter "struct bin_attribute *" in .read/.write methods for sysfs binar...
[deliverable/linux.git] / drivers / i2c / chips / eeprom.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/slab.h>
1da177e4
LT
33#include <linux/jiffies.h>
34#include <linux/i2c.h>
5c085d36 35#include <linux/mutex.h>
1da177e4
LT
36
37/* Addresses to scan */
38static unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
39 0x55, 0x56, 0x57, I2C_CLIENT_END };
1da177e4
LT
40
41/* Insmod parameters */
f4b50261 42I2C_CLIENT_INSMOD_1(eeprom);
1da177e4
LT
43
44
45/* Size of EEPROM in bytes */
46#define EEPROM_SIZE 256
47
48/* possible types of eeprom devices */
49enum eeprom_nature {
50 UNKNOWN,
51 VAIO,
52};
53
54/* Each client has this additional data */
55struct eeprom_data {
56 struct i2c_client client;
5c085d36 57 struct mutex update_lock;
1da177e4
LT
58 u8 valid; /* bitfield, bit!=0 if slice is valid */
59 unsigned long last_updated[8]; /* In jiffies, 8 slices */
60 u8 data[EEPROM_SIZE]; /* Register values */
61 enum eeprom_nature nature;
62};
63
64
65static int eeprom_attach_adapter(struct i2c_adapter *adapter);
66static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
67static int eeprom_detach_client(struct i2c_client *client);
68
69/* This is the driver that will be inserted */
70static struct i2c_driver eeprom_driver = {
a9718b0c 71 .driver = {
a9718b0c
LR
72 .name = "eeprom",
73 },
1da177e4 74 .id = I2C_DRIVERID_EEPROM,
1da177e4
LT
75 .attach_adapter = eeprom_attach_adapter,
76 .detach_client = eeprom_detach_client,
77};
78
79static void eeprom_update_client(struct i2c_client *client, u8 slice)
80{
81 struct eeprom_data *data = i2c_get_clientdata(client);
82 int i, j;
83
5c085d36 84 mutex_lock(&data->update_lock);
1da177e4
LT
85
86 if (!(data->valid & (1 << slice)) ||
87 time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
88 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
89
90 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
30dac746
JD
91 for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_BLOCK_MAX)
92 if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_BLOCK_MAX)
1da177e4
LT
93 goto exit;
94 } else {
95 if (i2c_smbus_write_byte(client, slice << 5)) {
96 dev_dbg(&client->dev, "eeprom read start has failed!\n");
97 goto exit;
98 }
99 for (i = slice << 5; i < (slice + 1) << 5; i++) {
100 j = i2c_smbus_read_byte(client);
101 if (j < 0)
102 goto exit;
103 data->data[i] = (u8) j;
104 }
105 }
106 data->last_updated[slice] = jiffies;
107 data->valid |= (1 << slice);
108 }
109exit:
5c085d36 110 mutex_unlock(&data->update_lock);
1da177e4
LT
111}
112
91a69029
ZR
113static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
114 char *buf, loff_t off, size_t count)
1da177e4
LT
115{
116 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
117 struct eeprom_data *data = i2c_get_clientdata(client);
118 u8 slice;
119
120 if (off > EEPROM_SIZE)
121 return 0;
122 if (off + count > EEPROM_SIZE)
123 count = EEPROM_SIZE - off;
124
125 /* Only refresh slices which contain requested bytes */
126 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
127 eeprom_update_client(client, slice);
128
129 /* Hide Vaio security settings to regular users (16 first bytes) */
130 if (data->nature == VAIO && off < 16 && !capable(CAP_SYS_ADMIN)) {
131 size_t in_row1 = 16 - off;
132 in_row1 = min(in_row1, count);
133 memset(buf, 0, in_row1);
134 if (count - in_row1 > 0)
135 memcpy(buf + in_row1, &data->data[16], count - in_row1);
136 } else {
137 memcpy(buf, &data->data[off], count);
138 }
139
140 return count;
141}
142
143static struct bin_attribute eeprom_attr = {
144 .attr = {
145 .name = "eeprom",
146 .mode = S_IRUGO,
1da177e4
LT
147 },
148 .size = EEPROM_SIZE,
149 .read = eeprom_read,
150};
151
152static int eeprom_attach_adapter(struct i2c_adapter *adapter)
153{
2ed2dc3c 154 return i2c_probe(adapter, &addr_data, eeprom_detect);
1da177e4
LT
155}
156
2ed2dc3c 157/* This function is called by i2c_probe */
6536c49a 158static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
1da177e4
LT
159{
160 struct i2c_client *new_client;
161 struct eeprom_data *data;
162 int err = 0;
163
164 /* There are three ways we can read the EEPROM data:
165 (1) I2C block reads (faster, but unsupported by most adapters)
166 (2) Consecutive byte reads (100% overhead)
167 (3) Regular byte data reads (200% overhead)
168 The third method is not implemented by this driver because all
169 known adapters support at least the second. */
170 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA
171 | I2C_FUNC_SMBUS_BYTE))
172 goto exit;
173
5263ebb5 174 if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
1da177e4
LT
175 err = -ENOMEM;
176 goto exit;
177 }
1da177e4
LT
178
179 new_client = &data->client;
180 memset(data->data, 0xff, EEPROM_SIZE);
181 i2c_set_clientdata(new_client, data);
182 new_client->addr = address;
183 new_client->adapter = adapter;
184 new_client->driver = &eeprom_driver;
185 new_client->flags = 0;
186
1da177e4
LT
187 /* Fill in the remaining client fields */
188 strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
189 data->valid = 0;
5c085d36 190 mutex_init(&data->update_lock);
1da177e4
LT
191 data->nature = UNKNOWN;
192
193 /* Tell the I2C layer a new client has arrived */
194 if ((err = i2c_attach_client(new_client)))
195 goto exit_kfree;
196
197 /* Detect the Vaio nature of EEPROMs.
198 We use the "PCG-" prefix as the signature. */
199 if (address == 0x57) {
200 if (i2c_smbus_read_byte_data(new_client, 0x80) == 'P'
201 && i2c_smbus_read_byte(new_client) == 'C'
202 && i2c_smbus_read_byte(new_client) == 'G'
203 && i2c_smbus_read_byte(new_client) == '-') {
204 dev_info(&new_client->dev, "Vaio EEPROM detected, "
205 "enabling password protection\n");
206 data->nature = VAIO;
207 }
208 }
209
210 /* create the sysfs eeprom file */
7d9db67f
JD
211 err = sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
212 if (err)
213 goto exit_detach;
1da177e4
LT
214
215 return 0;
216
7d9db67f
JD
217exit_detach:
218 i2c_detach_client(new_client);
1da177e4
LT
219exit_kfree:
220 kfree(data);
221exit:
222 return err;
223}
224
225static int eeprom_detach_client(struct i2c_client *client)
226{
227 int err;
228
7d9db67f
JD
229 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
230
1da177e4 231 err = i2c_detach_client(client);
7bef5594 232 if (err)
1da177e4 233 return err;
1da177e4
LT
234
235 kfree(i2c_get_clientdata(client));
236
237 return 0;
238}
239
240static int __init eeprom_init(void)
241{
242 return i2c_add_driver(&eeprom_driver);
243}
244
245static void __exit eeprom_exit(void)
246{
247 i2c_del_driver(&eeprom_driver);
248}
249
250
251MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
252 "Philip Edelbrock <phil@netroedge.com> and "
253 "Greg Kroah-Hartman <greg@kroah.com>");
254MODULE_DESCRIPTION("I2C EEPROM driver");
255MODULE_LICENSE("GPL");
256
257module_init(eeprom_init);
258module_exit(eeprom_exit);
This page took 0.235013 seconds and 5 git commands to generate.