eeprom.c 7.5 KB

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