eeprom.c 5.9 KB

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