eeprom.c 6.3 KB

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