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