| 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 | |
|---|
| 7 | This program is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | This program is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with this program; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include <linux/module.h> |
|---|
| 23 | #include <linux/version.h> |
|---|
| 24 | #include <linux/slab.h> |
|---|
| 25 | #include <linux/i2c.h> |
|---|
| 26 | #include "sensors.h" |
|---|
| 27 | #include "version.h" |
|---|
| 28 | #include <linux/init.h> |
|---|
| 29 | |
|---|
| 30 | #ifdef MODULE_LICENSE |
|---|
| 31 | MODULE_LICENSE("GPL"); |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,2,18)) || \ |
|---|
| 35 | (LINUX_VERSION_CODE == KERNEL_VERSION(2,3,0)) |
|---|
| 36 | #define init_MUTEX(s) do { *(s) = MUTEX; } while(0) |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | #ifndef THIS_MODULE |
|---|
| 40 | #define THIS_MODULE NULL |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | /* Addresses to scan */ |
|---|
| 44 | static unsigned short normal_i2c[] = { SENSORS_I2C_END }; |
|---|
| 45 | static unsigned short normal_i2c_range[] = { 0x50, 0x57, SENSORS_I2C_END }; |
|---|
| 46 | static unsigned int normal_isa[] = { SENSORS_ISA_END }; |
|---|
| 47 | static unsigned int normal_isa_range[] = { SENSORS_ISA_END }; |
|---|
| 48 | |
|---|
| 49 | /* Insmod parameters */ |
|---|
| 50 | SENSORS_INSMOD_1(eeprom); |
|---|
| 51 | |
|---|
| 52 | static int checksum = 0; |
|---|
| 53 | MODULE_PARM(checksum, "i"); |
|---|
| 54 | MODULE_PARM_DESC(checksum, |
|---|
| 55 | "Only accept eeproms whose checksum is correct"); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /* Many constants specified below */ |
|---|
| 59 | |
|---|
| 60 | /* EEPROM registers */ |
|---|
| 61 | #define EEPROM_REG_CHECKSUM 0x3f |
|---|
| 62 | |
|---|
| 63 | /* EEPROM memory types: */ |
|---|
| 64 | #define ONE_K 1 |
|---|
| 65 | #define TWO_K 2 |
|---|
| 66 | #define FOUR_K 3 |
|---|
| 67 | #define EIGHT_K 4 |
|---|
| 68 | #define SIXTEEN_K 5 |
|---|
| 69 | |
|---|
| 70 | /* Conversions */ |
|---|
| 71 | /* Size of EEPROM in bytes */ |
|---|
| 72 | #define EEPROM_SIZE 128 |
|---|
| 73 | |
|---|
| 74 | /* Each client has this additional data */ |
|---|
| 75 | struct eeprom_data { |
|---|
| 76 | int sysctl_id; |
|---|
| 77 | |
|---|
| 78 | struct semaphore update_lock; |
|---|
| 79 | char valid; /* !=0 if following fields are valid */ |
|---|
| 80 | unsigned long last_updated; /* In jiffies */ |
|---|
| 81 | |
|---|
| 82 | u8 data[EEPROM_SIZE]; /* Register values */ |
|---|
| 83 | int memtype; |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | #ifdef MODULE |
|---|
| 87 | extern int init_module(void); |
|---|
| 88 | extern int cleanup_module(void); |
|---|
| 89 | #endif /* MODULE */ |
|---|
| 90 | |
|---|
| 91 | #ifdef MODULE |
|---|
| 92 | static |
|---|
| 93 | #else |
|---|
| 94 | extern |
|---|
| 95 | #endif |
|---|
| 96 | int __init sensors_eeprom_init(void); |
|---|
| 97 | static int __init eeprom_cleanup(void); |
|---|
| 98 | |
|---|
| 99 | static int eeprom_attach_adapter(struct i2c_adapter *adapter); |
|---|
| 100 | static int eeprom_detect(struct i2c_adapter *adapter, int address, |
|---|
| 101 | unsigned short flags, int kind); |
|---|
| 102 | static int eeprom_detach_client(struct i2c_client *client); |
|---|
| 103 | static int eeprom_command(struct i2c_client *client, unsigned int cmd, |
|---|
| 104 | void *arg); |
|---|
| 105 | |
|---|
| 106 | static void eeprom_inc_use(struct i2c_client *client); |
|---|
| 107 | static void eeprom_dec_use(struct i2c_client *client); |
|---|
| 108 | |
|---|
| 109 | #if 0 |
|---|
| 110 | static int eeprom_write_value(struct i2c_client *client, u8 reg, |
|---|
| 111 | u16 value); |
|---|
| 112 | #endif |
|---|
| 113 | |
|---|
| 114 | static void eeprom_contents(struct i2c_client *client, int operation, |
|---|
| 115 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 116 | static void eeprom_update_client(struct i2c_client *client); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | /* This is the driver that will be inserted */ |
|---|
| 120 | static struct i2c_driver eeprom_driver = { |
|---|
| 121 | /* name */ "EEPROM READER", |
|---|
| 122 | /* id */ I2C_DRIVERID_EEPROM, |
|---|
| 123 | /* flags */ I2C_DF_NOTIFY, |
|---|
| 124 | /* attach_adapter */ &eeprom_attach_adapter, |
|---|
| 125 | /* detach_client */ &eeprom_detach_client, |
|---|
| 126 | /* command */ &eeprom_command, |
|---|
| 127 | /* inc_use */ &eeprom_inc_use, |
|---|
| 128 | /* dec_use */ &eeprom_dec_use |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | /* These files are created for each detected EEPROM. This is just a template; |
|---|
| 132 | though at first sight, you might think we could use a statically |
|---|
| 133 | allocated list, we need some way to get back to the parent - which |
|---|
| 134 | is done through one of the 'extra' fields which are initialized |
|---|
| 135 | when a new copy is allocated. */ |
|---|
| 136 | static ctl_table eeprom_dir_table_template[] = { |
|---|
| 137 | {EEPROM_SYSCTL1, "data0-15", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 138 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 139 | {EEPROM_SYSCTL2, "data16-31", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 140 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 141 | {EEPROM_SYSCTL3, "data32-47", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 142 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 143 | {EEPROM_SYSCTL4, "data48-63", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 144 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 145 | {EEPROM_SYSCTL5, "data64-79", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 146 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 147 | {EEPROM_SYSCTL6, "data80-95", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 148 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 149 | {EEPROM_SYSCTL7, "data96-111", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 150 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 151 | {EEPROM_SYSCTL8, "data112-127", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 152 | &i2c_sysctl_real, NULL, &eeprom_contents}, |
|---|
| 153 | {0} |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | /* Used by init/cleanup */ |
|---|
| 157 | static int __initdata eeprom_initialized = 0; |
|---|
| 158 | |
|---|
| 159 | static int eeprom_id = 0; |
|---|
| 160 | |
|---|
| 161 | int eeprom_attach_adapter(struct i2c_adapter *adapter) |
|---|
| 162 | { |
|---|
| 163 | return i2c_detect(adapter, &addr_data, eeprom_detect); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /* This function is called by i2c_detect */ |
|---|
| 167 | int eeprom_detect(struct i2c_adapter *adapter, int address, |
|---|
| 168 | unsigned short flags, int kind) |
|---|
| 169 | { |
|---|
| 170 | int i, cs; |
|---|
| 171 | struct i2c_client *new_client; |
|---|
| 172 | struct eeprom_data *data; |
|---|
| 173 | int err = 0; |
|---|
| 174 | const char *type_name, *client_name; |
|---|
| 175 | |
|---|
| 176 | /* Make sure we aren't probing the ISA bus!! This is just a safety check |
|---|
| 177 | at this moment; i2c_detect really won't call us. */ |
|---|
| 178 | #ifdef DEBUG |
|---|
| 179 | if (i2c_is_isa_adapter(adapter)) { |
|---|
| 180 | printk |
|---|
| 181 | ("eeprom.o: eeprom_detect called for an ISA bus adapter?!?\n"); |
|---|
| 182 | return 0; |
|---|
| 183 | } |
|---|
| 184 | #endif |
|---|
| 185 | |
|---|
| 186 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
|---|
| 187 | goto ERROR0; |
|---|
| 188 | |
|---|
| 189 | /* OK. For now, we presume we have a valid client. We now create the |
|---|
| 190 | client structure, even though we cannot fill it completely yet. |
|---|
| 191 | But it allows us to access eeprom_{read,write}_value. */ |
|---|
| 192 | if (!(new_client = kmalloc(sizeof(struct i2c_client) + |
|---|
| 193 | sizeof(struct eeprom_data), |
|---|
| 194 | GFP_KERNEL))) { |
|---|
| 195 | err = -ENOMEM; |
|---|
| 196 | goto ERROR0; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | data = (struct eeprom_data *) (new_client + 1); |
|---|
| 200 | new_client->addr = address; |
|---|
| 201 | new_client->data = data; |
|---|
| 202 | new_client->adapter = adapter; |
|---|
| 203 | new_client->driver = &eeprom_driver; |
|---|
| 204 | new_client->flags = 0; |
|---|
| 205 | |
|---|
| 206 | /* Now, we do the remaining detection. It is not there, unless you force |
|---|
| 207 | the checksum to work out. */ |
|---|
| 208 | if (checksum) { |
|---|
| 209 | cs = 0; |
|---|
| 210 | for (i = 0; i <= 0x3e; i++) |
|---|
| 211 | cs += i2c_smbus_read_byte_data(new_client, i); |
|---|
| 212 | cs &= 0xff; |
|---|
| 213 | if (i2c_smbus_read_byte_data |
|---|
| 214 | (new_client, EEPROM_REG_CHECKSUM) != cs) |
|---|
| 215 | goto ERROR1; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /* Determine the chip type - only one kind supported! */ |
|---|
| 219 | if (kind <= 0) |
|---|
| 220 | kind = eeprom; |
|---|
| 221 | |
|---|
| 222 | if (kind == eeprom) { |
|---|
| 223 | type_name = "eeprom"; |
|---|
| 224 | client_name = "EEPROM chip"; |
|---|
| 225 | } else { |
|---|
| 226 | #ifdef DEBUG |
|---|
| 227 | printk("eeprom.o: Internal error: unknown kind (%d)?!?", |
|---|
| 228 | kind); |
|---|
| 229 | #endif |
|---|
| 230 | goto ERROR1; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /* Fill in the remaining client fields and put it into the global list */ |
|---|
| 234 | strcpy(new_client->name, client_name); |
|---|
| 235 | |
|---|
| 236 | new_client->id = eeprom_id++; |
|---|
| 237 | data->valid = 0; |
|---|
| 238 | init_MUTEX(&data->update_lock); |
|---|
| 239 | |
|---|
| 240 | /* Tell the I2C layer a new client has arrived */ |
|---|
| 241 | if ((err = i2c_attach_client(new_client))) |
|---|
| 242 | goto ERROR3; |
|---|
| 243 | |
|---|
| 244 | /* Register a new directory entry with module sensors */ |
|---|
| 245 | if ((i = i2c_register_entry(new_client, type_name, |
|---|
| 246 | eeprom_dir_table_template, |
|---|
| 247 | THIS_MODULE)) < 0) { |
|---|
| 248 | err = i; |
|---|
| 249 | goto ERROR4; |
|---|
| 250 | } |
|---|
| 251 | data->sysctl_id = i; |
|---|
| 252 | |
|---|
| 253 | return 0; |
|---|
| 254 | |
|---|
| 255 | /* OK, this is not exactly good programming practice, usually. But it is |
|---|
| 256 | very code-efficient in this case. */ |
|---|
| 257 | |
|---|
| 258 | ERROR4: |
|---|
| 259 | i2c_detach_client(new_client); |
|---|
| 260 | ERROR3: |
|---|
| 261 | ERROR1: |
|---|
| 262 | kfree(new_client); |
|---|
| 263 | ERROR0: |
|---|
| 264 | return err; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | int eeprom_detach_client(struct i2c_client *client) |
|---|
| 268 | { |
|---|
| 269 | int err; |
|---|
| 270 | |
|---|
| 271 | i2c_deregister_entry(((struct eeprom_data *) (client->data))-> |
|---|
| 272 | sysctl_id); |
|---|
| 273 | |
|---|
| 274 | if ((err = i2c_detach_client(client))) { |
|---|
| 275 | printk |
|---|
| 276 | ("eeprom.o: Client deregistration failed, client not detached.\n"); |
|---|
| 277 | return err; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | kfree(client); |
|---|
| 281 | |
|---|
| 282 | return 0; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | /* No commands defined yet */ |
|---|
| 287 | int eeprom_command(struct i2c_client *client, unsigned int cmd, void *arg) |
|---|
| 288 | { |
|---|
| 289 | return 0; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | void eeprom_inc_use(struct i2c_client *client) |
|---|
| 293 | { |
|---|
| 294 | #ifdef MODULE |
|---|
| 295 | MOD_INC_USE_COUNT; |
|---|
| 296 | #endif |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | void eeprom_dec_use(struct i2c_client *client) |
|---|
| 300 | { |
|---|
| 301 | #ifdef MODULE |
|---|
| 302 | MOD_DEC_USE_COUNT; |
|---|
| 303 | #endif |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | #if 0 |
|---|
| 307 | /* No writes yet (PAE) */ |
|---|
| 308 | int eeprom_write_value(struct i2c_client *client, u8 reg, u16 value) |
|---|
| 309 | { |
|---|
| 310 | if (reg == EEPROM_REG_CONF) |
|---|
| 311 | return i2c_smbus_write_byte_data(client, reg, value); |
|---|
| 312 | else |
|---|
| 313 | return i2c_smbus_write_word_data(client, reg, value); |
|---|
| 314 | */return 0; |
|---|
| 315 | } |
|---|
| 316 | #endif |
|---|
| 317 | |
|---|
| 318 | void eeprom_update_client(struct i2c_client *client) |
|---|
| 319 | { |
|---|
| 320 | struct eeprom_data *data = client->data; |
|---|
| 321 | int i; |
|---|
| 322 | |
|---|
| 323 | down(&data->update_lock); |
|---|
| 324 | |
|---|
| 325 | if ((jiffies - data->last_updated > 300 * HZ) | |
|---|
| 326 | (jiffies < data->last_updated) || !data->valid) { |
|---|
| 327 | |
|---|
| 328 | #ifdef DEBUG |
|---|
| 329 | printk("Starting eeprom update\n"); |
|---|
| 330 | #endif |
|---|
| 331 | |
|---|
| 332 | if (i2c_smbus_write_byte(client, 0)) { |
|---|
| 333 | #ifdef DEBUG |
|---|
| 334 | printk("eeprom read start has failed!\n"); |
|---|
| 335 | #endif |
|---|
| 336 | } |
|---|
| 337 | for (i = 0; i < EEPROM_SIZE; i++) { |
|---|
| 338 | data->data[i] = (u8) i2c_smbus_read_byte(client); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | data->last_updated = jiffies; |
|---|
| 342 | data->valid = 1; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | up(&data->update_lock); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | void eeprom_contents(struct i2c_client *client, int operation, |
|---|
| 350 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 351 | { |
|---|
| 352 | int i; |
|---|
| 353 | int base = 0; |
|---|
| 354 | struct eeprom_data *data = client->data; |
|---|
| 355 | |
|---|
| 356 | if (ctl_name == EEPROM_SYSCTL2) { |
|---|
| 357 | base = 16; |
|---|
| 358 | } |
|---|
| 359 | if (ctl_name == EEPROM_SYSCTL3) { |
|---|
| 360 | base = 32; |
|---|
| 361 | } |
|---|
| 362 | if (ctl_name == EEPROM_SYSCTL4) { |
|---|
| 363 | base = 48; |
|---|
| 364 | } |
|---|
| 365 | if (ctl_name == EEPROM_SYSCTL5) { |
|---|
| 366 | base = 64; |
|---|
| 367 | } |
|---|
| 368 | if (ctl_name == EEPROM_SYSCTL6) { |
|---|
| 369 | base = 80; |
|---|
| 370 | } |
|---|
| 371 | if (ctl_name == EEPROM_SYSCTL7) { |
|---|
| 372 | base = 96; |
|---|
| 373 | } |
|---|
| 374 | if (ctl_name == EEPROM_SYSCTL8) { |
|---|
| 375 | base = 112; |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 379 | *nrels_mag = 0; |
|---|
| 380 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 381 | eeprom_update_client(client); |
|---|
| 382 | for (i = 0; i < 16; i++) { |
|---|
| 383 | results[i] = data->data[i + base]; |
|---|
| 384 | } |
|---|
| 385 | #ifdef DEBUG |
|---|
| 386 | printk("eeprom.o: 0x%X EEPROM Contents (base %d): ", |
|---|
| 387 | client->addr, base); |
|---|
| 388 | for (i = 0; i < 16; i++) { |
|---|
| 389 | printk(" 0x%X", data->data[i + base]); |
|---|
| 390 | } |
|---|
| 391 | printk(" .\n"); |
|---|
| 392 | #endif |
|---|
| 393 | *nrels_mag = 16; |
|---|
| 394 | } else if (operation == SENSORS_PROC_REAL_WRITE) { |
|---|
| 395 | |
|---|
| 396 | /* No writes to the EEPROM (yet, anyway) (PAE) */ |
|---|
| 397 | printk("eeprom.o: No writes to EEPROMs supported!\n"); |
|---|
| 398 | } |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | int __init sensors_eeprom_init(void) |
|---|
| 402 | { |
|---|
| 403 | int res; |
|---|
| 404 | |
|---|
| 405 | printk("eeprom.o version %s (%s)\n", LM_VERSION, LM_DATE); |
|---|
| 406 | eeprom_initialized = 0; |
|---|
| 407 | if ((res = i2c_add_driver(&eeprom_driver))) { |
|---|
| 408 | printk |
|---|
| 409 | ("eeprom.o: Driver registration failed, module not inserted.\n"); |
|---|
| 410 | eeprom_cleanup(); |
|---|
| 411 | return res; |
|---|
| 412 | } |
|---|
| 413 | eeprom_initialized++; |
|---|
| 414 | return 0; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | int __init eeprom_cleanup(void) |
|---|
| 418 | { |
|---|
| 419 | int res; |
|---|
| 420 | |
|---|
| 421 | if (eeprom_initialized >= 1) { |
|---|
| 422 | if ((res = i2c_del_driver(&eeprom_driver))) { |
|---|
| 423 | printk |
|---|
| 424 | ("eeprom.o: Driver deregistration failed, module not removed.\n"); |
|---|
| 425 | return res; |
|---|
| 426 | } |
|---|
| 427 | } else |
|---|
| 428 | eeprom_initialized--; |
|---|
| 429 | |
|---|
| 430 | return 0; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | EXPORT_NO_SYMBOLS; |
|---|
| 434 | |
|---|
| 435 | #ifdef MODULE |
|---|
| 436 | |
|---|
| 437 | MODULE_AUTHOR |
|---|
| 438 | ("Frodo Looijaard <frodol@dds.nl> and Philip Edelbrock <phil@netroedge.com>"); |
|---|
| 439 | MODULE_DESCRIPTION("EEPROM driver"); |
|---|
| 440 | |
|---|
| 441 | int init_module(void) |
|---|
| 442 | { |
|---|
| 443 | return sensors_eeprom_init(); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | int cleanup_module(void) |
|---|
| 447 | { |
|---|
| 448 | return eeprom_cleanup(); |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | #endif /* MODULE */ |
|---|