eeprom.c 6.5 KB

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