eeprom.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 i2c_client client;
  41. struct mutex update_lock;
  42. u8 valid; /* bitfield, bit!=0 if slice is valid */
  43. unsigned long last_updated[8]; /* In jiffies, 8 slices */
  44. u8 data[EEPROM_SIZE]; /* Register values */
  45. enum eeprom_nature nature;
  46. };
  47. static int eeprom_attach_adapter(struct i2c_adapter *adapter);
  48. static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
  49. static int eeprom_detach_client(struct i2c_client *client);
  50. /* This is the driver that will be inserted */
  51. static struct i2c_driver eeprom_driver = {
  52. .driver = {
  53. .name = "eeprom",
  54. },
  55. .attach_adapter = eeprom_attach_adapter,
  56. .detach_client = eeprom_detach_client,
  57. };
  58. static void eeprom_update_client(struct i2c_client *client, u8 slice)
  59. {
  60. struct eeprom_data *data = i2c_get_clientdata(client);
  61. int i;
  62. mutex_lock(&data->update_lock);
  63. if (!(data->valid & (1 << slice)) ||
  64. time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
  65. dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
  66. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  67. for (i = slice << 5; i < (slice + 1) << 5; i += 32)
  68. if (i2c_smbus_read_i2c_block_data(client, i,
  69. 32, data->data + i)
  70. != 32)
  71. goto exit;
  72. } else {
  73. for (i = slice << 5; i < (slice + 1) << 5; i += 2) {
  74. int word = i2c_smbus_read_word_data(client, i);
  75. if (word < 0)
  76. goto exit;
  77. data->data[i] = word & 0xff;
  78. data->data[i + 1] = word >> 8;
  79. }
  80. }
  81. data->last_updated[slice] = jiffies;
  82. data->valid |= (1 << slice);
  83. }
  84. exit:
  85. mutex_unlock(&data->update_lock);
  86. }
  87. static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
  88. char *buf, loff_t off, size_t count)
  89. {
  90. struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
  91. struct eeprom_data *data = i2c_get_clientdata(client);
  92. u8 slice;
  93. if (off > EEPROM_SIZE)
  94. return 0;
  95. if (off + count > EEPROM_SIZE)
  96. count = EEPROM_SIZE - off;
  97. /* Only refresh slices which contain requested bytes */
  98. for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
  99. eeprom_update_client(client, slice);
  100. /* Hide Vaio private settings to regular users:
  101. - BIOS passwords: bytes 0x00 to 0x0f
  102. - UUID: bytes 0x10 to 0x1f
  103. - Serial number: 0xc0 to 0xdf */
  104. if (data->nature == VAIO && !capable(CAP_SYS_ADMIN)) {
  105. int i;
  106. for (i = 0; i < count; i++) {
  107. if ((off + i <= 0x1f) ||
  108. (off + i >= 0xc0 && off + i <= 0xdf))
  109. buf[i] = 0;
  110. else
  111. buf[i] = data->data[off + i];
  112. }
  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. },
  123. .size = EEPROM_SIZE,
  124. .read = eeprom_read,
  125. };
  126. static int eeprom_attach_adapter(struct i2c_adapter *adapter)
  127. {
  128. if (!(adapter->class & (I2C_CLASS_DDC | I2C_CLASS_SPD)))
  129. return 0;
  130. return i2c_probe(adapter, &addr_data, eeprom_detect);
  131. }
  132. /* This function is called by i2c_probe */
  133. static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
  134. {
  135. struct i2c_client *client;
  136. struct eeprom_data *data;
  137. int err = 0;
  138. /* EDID EEPROMs are often 24C00 EEPROMs, which answer to all
  139. addresses 0x50-0x57, but we only care about 0x50. So decline
  140. attaching to addresses >= 0x51 on DDC buses */
  141. if (!(adapter->class & I2C_CLASS_SPD) && address >= 0x51)
  142. goto exit;
  143. /* There are four ways we can read the EEPROM data:
  144. (1) I2C block reads (faster, but unsupported by most adapters)
  145. (2) Word reads (128% overhead)
  146. (3) Consecutive byte reads (88% overhead, unsafe)
  147. (4) Regular byte data reads (265% overhead)
  148. The third and fourth methods are not implemented by this driver
  149. because all known adapters support one of the first two. */
  150. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)
  151. && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  152. goto exit;
  153. if (!(data = kzalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
  154. err = -ENOMEM;
  155. goto exit;
  156. }
  157. client = &data->client;
  158. memset(data->data, 0xff, EEPROM_SIZE);
  159. i2c_set_clientdata(client, data);
  160. client->addr = address;
  161. client->adapter = adapter;
  162. client->driver = &eeprom_driver;
  163. /* Fill in the remaining client fields */
  164. strlcpy(client->name, "eeprom", I2C_NAME_SIZE);
  165. mutex_init(&data->update_lock);
  166. data->nature = UNKNOWN;
  167. /* Tell the I2C layer a new client has arrived */
  168. if ((err = i2c_attach_client(client)))
  169. goto exit_kfree;
  170. /* Detect the Vaio nature of EEPROMs.
  171. We use the "PCG-" or "VGN-" prefix as the signature. */
  172. if (address == 0x57
  173. && i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  174. char name[4];
  175. name[0] = i2c_smbus_read_byte_data(client, 0x80);
  176. name[1] = i2c_smbus_read_byte_data(client, 0x81);
  177. name[2] = i2c_smbus_read_byte_data(client, 0x82);
  178. name[3] = i2c_smbus_read_byte_data(client, 0x83);
  179. if (!memcmp(name, "PCG-", 4) || !memcmp(name, "VGN-", 4)) {
  180. dev_info(&client->dev, "Vaio EEPROM detected, "
  181. "enabling privacy protection\n");
  182. data->nature = VAIO;
  183. }
  184. }
  185. /* create the sysfs eeprom file */
  186. err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
  187. if (err)
  188. goto exit_detach;
  189. return 0;
  190. exit_detach:
  191. i2c_detach_client(client);
  192. exit_kfree:
  193. kfree(data);
  194. exit:
  195. return err;
  196. }
  197. static int eeprom_detach_client(struct i2c_client *client)
  198. {
  199. int err;
  200. sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
  201. err = i2c_detach_client(client);
  202. if (err)
  203. return err;
  204. kfree(i2c_get_clientdata(client));
  205. return 0;
  206. }
  207. static int __init eeprom_init(void)
  208. {
  209. return i2c_add_driver(&eeprom_driver);
  210. }
  211. static void __exit eeprom_exit(void)
  212. {
  213. i2c_del_driver(&eeprom_driver);
  214. }
  215. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  216. "Philip Edelbrock <phil@netroedge.com> and "
  217. "Greg Kroah-Hartman <greg@kroah.com>");
  218. MODULE_DESCRIPTION("I2C EEPROM driver");
  219. MODULE_LICENSE("GPL");
  220. module_init(eeprom_init);
  221. module_exit(eeprom_exit);