eeprom.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. 2004-01-16 Jean Delvare <khali@linux-fr.org>
  9. Divide the eeprom in 32-byte (arbitrary) slices. This significantly
  10. speeds sensors up, as well as various scripts using the eeprom
  11. module.
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/i2c.h>
  31. /* Addresses to scan */
  32. static unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
  33. 0x55, 0x56, 0x57, I2C_CLIENT_END };
  34. /* Insmod parameters */
  35. I2C_CLIENT_INSMOD_1(eeprom);
  36. /* Size of EEPROM in bytes */
  37. #define EEPROM_SIZE 256
  38. /* possible types of eeprom devices */
  39. enum eeprom_nature {
  40. UNKNOWN,
  41. VAIO,
  42. };
  43. /* Each client has this additional data */
  44. struct eeprom_data {
  45. struct i2c_client client;
  46. struct semaphore update_lock;
  47. u8 valid; /* bitfield, bit!=0 if slice is valid */
  48. unsigned long last_updated[8]; /* In jiffies, 8 slices */
  49. u8 data[EEPROM_SIZE]; /* Register values */
  50. enum eeprom_nature nature;
  51. };
  52. static int eeprom_attach_adapter(struct i2c_adapter *adapter);
  53. static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
  54. static int eeprom_detach_client(struct i2c_client *client);
  55. /* This is the driver that will be inserted */
  56. static struct i2c_driver eeprom_driver = {
  57. .driver = {
  58. .name = "eeprom",
  59. },
  60. .id = I2C_DRIVERID_EEPROM,
  61. .attach_adapter = eeprom_attach_adapter,
  62. .detach_client = eeprom_detach_client,
  63. };
  64. static void eeprom_update_client(struct i2c_client *client, u8 slice)
  65. {
  66. struct eeprom_data *data = i2c_get_clientdata(client);
  67. int i, j;
  68. down(&data->update_lock);
  69. if (!(data->valid & (1 << slice)) ||
  70. time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
  71. dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
  72. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  73. for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_BLOCK_MAX)
  74. if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_BLOCK_MAX)
  75. goto exit;
  76. } else {
  77. if (i2c_smbus_write_byte(client, slice << 5)) {
  78. dev_dbg(&client->dev, "eeprom read start has failed!\n");
  79. goto exit;
  80. }
  81. for (i = slice << 5; i < (slice + 1) << 5; i++) {
  82. j = i2c_smbus_read_byte(client);
  83. if (j < 0)
  84. goto exit;
  85. data->data[i] = (u8) j;
  86. }
  87. }
  88. data->last_updated[slice] = jiffies;
  89. data->valid |= (1 << slice);
  90. }
  91. exit:
  92. up(&data->update_lock);
  93. }
  94. static ssize_t eeprom_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
  95. {
  96. struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
  97. struct eeprom_data *data = i2c_get_clientdata(client);
  98. u8 slice;
  99. if (off > EEPROM_SIZE)
  100. return 0;
  101. if (off + count > EEPROM_SIZE)
  102. count = EEPROM_SIZE - off;
  103. /* Only refresh slices which contain requested bytes */
  104. for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
  105. eeprom_update_client(client, slice);
  106. /* Hide Vaio security settings to regular users (16 first bytes) */
  107. if (data->nature == VAIO && off < 16 && !capable(CAP_SYS_ADMIN)) {
  108. size_t in_row1 = 16 - off;
  109. in_row1 = min(in_row1, count);
  110. memset(buf, 0, in_row1);
  111. if (count - in_row1 > 0)
  112. memcpy(buf + in_row1, &data->data[16], count - in_row1);
  113. } else {
  114. memcpy(buf, &data->data[off], count);
  115. }
  116. return count;
  117. }
  118. static struct bin_attribute eeprom_attr = {
  119. .attr = {
  120. .name = "eeprom",
  121. .mode = S_IRUGO,
  122. .owner = THIS_MODULE,
  123. },
  124. .size = EEPROM_SIZE,
  125. .read = eeprom_read,
  126. };
  127. static int eeprom_attach_adapter(struct i2c_adapter *adapter)
  128. {
  129. return i2c_probe(adapter, &addr_data, eeprom_detect);
  130. }
  131. /* This function is called by i2c_probe */
  132. static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
  133. {
  134. struct i2c_client *new_client;
  135. struct eeprom_data *data;
  136. int err = 0;
  137. /* There are three ways we can read the EEPROM data:
  138. (1) I2C block reads (faster, but unsupported by most adapters)
  139. (2) Consecutive byte reads (100% overhead)
  140. (3) Regular byte data reads (200% overhead)
  141. The third method is not implemented by this driver because all
  142. known adapters support at least the second. */
  143. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA
  144. | I2C_FUNC_SMBUS_BYTE))
  145. goto exit;
  146. if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
  147. err = -ENOMEM;
  148. goto exit;
  149. }
  150. new_client = &data->client;
  151. memset(data->data, 0xff, EEPROM_SIZE);
  152. i2c_set_clientdata(new_client, data);
  153. new_client->addr = address;
  154. new_client->adapter = adapter;
  155. new_client->driver = &eeprom_driver;
  156. new_client->flags = 0;
  157. /* Fill in the remaining client fields */
  158. strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
  159. data->valid = 0;
  160. init_MUTEX(&data->update_lock);
  161. data->nature = UNKNOWN;
  162. /* Tell the I2C layer a new client has arrived */
  163. if ((err = i2c_attach_client(new_client)))
  164. goto exit_kfree;
  165. /* Detect the Vaio nature of EEPROMs.
  166. We use the "PCG-" prefix as the signature. */
  167. if (address == 0x57) {
  168. if (i2c_smbus_read_byte_data(new_client, 0x80) == 'P'
  169. && i2c_smbus_read_byte(new_client) == 'C'
  170. && i2c_smbus_read_byte(new_client) == 'G'
  171. && i2c_smbus_read_byte(new_client) == '-') {
  172. dev_info(&new_client->dev, "Vaio EEPROM detected, "
  173. "enabling password protection\n");
  174. data->nature = VAIO;
  175. }
  176. }
  177. /* create the sysfs eeprom file */
  178. sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
  179. return 0;
  180. exit_kfree:
  181. kfree(data);
  182. exit:
  183. return err;
  184. }
  185. static int eeprom_detach_client(struct i2c_client *client)
  186. {
  187. int err;
  188. err = i2c_detach_client(client);
  189. if (err)
  190. return err;
  191. kfree(i2c_get_clientdata(client));
  192. return 0;
  193. }
  194. static int __init eeprom_init(void)
  195. {
  196. return i2c_add_driver(&eeprom_driver);
  197. }
  198. static void __exit eeprom_exit(void)
  199. {
  200. i2c_del_driver(&eeprom_driver);
  201. }
  202. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  203. "Philip Edelbrock <phil@netroedge.com> and "
  204. "Greg Kroah-Hartman <greg@kroah.com>");
  205. MODULE_DESCRIPTION("I2C EEPROM driver");
  206. MODULE_LICENSE("GPL");
  207. module_init(eeprom_init);
  208. module_exit(eeprom_exit);