eeprom.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
  3. Philip Edelbrock <phil@netroedge.com>
  4. Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
  5. Copyright (C) 2003 IBM Corp.
  6. Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
  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. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/mutex.h>
  26. /* Addresses to scan */
  27. static const unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
  28. 0x55, 0x56, 0x57, I2C_CLIENT_END };
  29. /* Insmod parameters */
  30. I2C_CLIENT_INSMOD_1(eeprom);
  31. /* Size of EEPROM in bytes */
  32. #define EEPROM_SIZE 256
  33. /* possible types of eeprom devices */
  34. enum eeprom_nature {
  35. UNKNOWN,
  36. VAIO,
  37. };
  38. /* Each client has this additional data */
  39. struct eeprom_data {
  40. struct mutex update_lock;
  41. u8 valid; /* bitfield, bit!=0 if slice is valid */
  42. unsigned long last_updated[8]; /* In jiffies, 8 slices */
  43. u8 data[EEPROM_SIZE]; /* Register values */
  44. enum eeprom_nature nature;
  45. };
  46. static void eeprom_update_client(struct i2c_client *client, u8 slice)
  47. {
  48. struct eeprom_data *data = i2c_get_clientdata(client);
  49. int i;
  50. mutex_lock(&data->update_lock);
  51. if (!(data->valid & (1 << slice)) ||
  52. time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
  53. dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
  54. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  55. for (i = slice << 5; i < (slice + 1) << 5; i += 32)
  56. if (i2c_smbus_read_i2c_block_data(client, i,
  57. 32, data->data + i)
  58. != 32)
  59. goto exit;
  60. } else {
  61. for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
  62. int word = i2c_smbus_read_word_data(client, i);
  63. if (word < 0)
  64. goto exit;
  65. data->data[i] = word & 0xff;
  66. data->data[i + 1] = word >> 8;
  67. }
  68. }
  69. data->last_updated[slice] = jiffies;
  70. data->valid |= (1 << slice);
  71. }
  72. exit:
  73. mutex_unlock(&data->update_lock);
  74. }
  75. static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
  76. char *buf, loff_t off, size_t count)
  77. {
  78. struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
  79. struct eeprom_data *data = i2c_get_clientdata(client);
  80. u8 slice;
  81. if (off > EEPROM_SIZE)
  82. return 0;
  83. if (off + count > EEPROM_SIZE)
  84. count = EEPROM_SIZE - off;
  85. /* Only refresh slices which contain requested bytes */
  86. for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
  87. eeprom_update_client(client, slice);
  88. /* Hide Vaio private settings to regular users:
  89. - BIOS passwords: bytes 0x00 to 0x0f
  90. - UUID: bytes 0x10 to 0x1f
  91. - Serial number: 0xc0 to 0xdf */
  92. if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
  93. int i;
  94. for (i = 0; i < count; i++) {
  95. if ((off + i <= 0x1f) ||
  96. (off + i >= 0xc0 && off + i <= 0xdf))
  97. buf[i] = 0;
  98. else
  99. buf[i] = data->data[off + i];
  100. }
  101. } else {
  102. memcpy(buf, &data->data[off], count);
  103. }
  104. return count;
  105. }
  106. static struct bin_attribute eeprom_attr = {
  107. .attr = {
  108. .name = "eeprom",
  109. .mode = S_IRUGO,
  110. },
  111. .size = EEPROM_SIZE,
  112. .read = eeprom_read,
  113. };
  114. /* Return 0 if detection is successful, -ENODEV otherwise */
  115. static int eeprom_detect(struct i2c_client *client, int kind,
  116. struct i2c_board_info *info)
  117. {
  118. struct i2c_adapter *adapter = client->adapter;
  119. /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
  120. addresses 0x50-0x57, but we only care about 0x50. So decline
  121. attaching to addresses >= 0x51 on DDC buses */
  122. if (!(adapter->class & I2C_CLASS_SPD) && client->addr >= 0x51)
  123. return -ENODEV;
  124. /* There are four ways we can read the EEPROM data:
  125. (1) I2C block reads (faster, but unsupported by most adapters)
  126. (2) Word reads (128% overhead)
  127. (3) Consecutive byte reads (88% overhead, unsafe)
  128. (4) Regular byte data reads (265% overhead)
  129. The third and fourth methods are not implemented by this driver
  130. because all known adapters support one of the first two. */
  131. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
  132. && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  133. return -ENODEV;
  134. strlcpy(info->type, "eeprom", I2C_NAME_SIZE);
  135. return 0;
  136. }
  137. static int eeprom_probe(struct i2c_client *client,
  138. const struct i2c_device_id *id)
  139. {
  140. struct i2c_adapter *adapter = client->adapter;
  141. struct eeprom_data *data;
  142. int err;
  143. if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
  144. err = -ENOMEM;
  145. goto exit;
  146. }
  147. memset(data->data, 0xff, EEPROM_SIZE);
  148. i2c_set_clientdata(client, data);
  149. mutex_init(&data->update_lock);
  150. data->nature = UNKNOWN;
  151. /* Detect the Vaio nature of EEPROMs.
  152. We use the "PCG-" or "VGN-" prefix as the signature. */
  153. if (client->addr == 0x57
  154. && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  155. char name[4];
  156. name[0] = i2c_smbus_read_byte_data(client, 0x80);
  157. name[1] = i2c_smbus_read_byte_data(client, 0x81);
  158. name[2] = i2c_smbus_read_byte_data(client, 0x82);
  159. name[3] = i2c_smbus_read_byte_data(client, 0x83);
  160. if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
  161. dev_info(&client->dev, "Vaio EEPROM detected, "
  162. "enabling privacy protection\n");
  163. data->nature = VAIO;
  164. }
  165. }
  166. /* create the sysfs eeprom file */
  167. err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
  168. if (err)
  169. goto exit_kfree;
  170. return 0;
  171. exit_kfree:
  172. kfree(data);
  173. exit:
  174. return err;
  175. }
  176. static int eeprom_remove(struct i2c_client *client)
  177. {
  178. sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
  179. kfree(i2c_get_clientdata(client));
  180. return 0;
  181. }
  182. static const struct i2c_device_id eeprom_id[] = {
  183. { "eeprom", 0 },
  184. { }
  185. };
  186. static struct i2c_driver eeprom_driver = {
  187. .driver = {
  188. .name = "eeprom",
  189. },
  190. .probe = eeprom_probe,
  191. .remove = eeprom_remove,
  192. .id_table = eeprom_id,
  193. .class = I2C_CLASS_DDC | I2C_CLASS_SPD,
  194. .detect = eeprom_detect,
  195. .address_data = &addr_data,
  196. };
  197. static int __init eeprom_init(void)
  198. {
  199. return i2c_add_driver(&eeprom_driver);
  200. }
  201. static void __exit eeprom_exit(void)
  202. {
  203. i2c_del_driver(&eeprom_driver);
  204. }
  205. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  206. "Philip Edelbrock <phil@netroedge.com> and "
  207. "Greg Kroah-Hartman <greg@kroah.com>");
  208. MODULE_DESCRIPTION("I2C EEPROM driver");
  209. MODULE_LICENSE("GPL");
  210. module_init(eeprom_init);
  211. module_exit(eeprom_exit);