pci-label.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Purpose: Export the firmware instance and label associated with
  3. * a pci device to sysfs
  4. * Copyright (C) 2010 Dell Inc.
  5. * by Narendra K <Narendra_K@dell.com>,
  6. * Jordan Hargrave <Jordan_Hargrave@dell.com>
  7. *
  8. * SMBIOS defines type 41 for onboard pci devices. This code retrieves
  9. * the instance number and string from the type 41 record and exports
  10. * it to sysfs.
  11. *
  12. * Please see http://linux.dell.com/wiki/index.php/Oss/libnetdevname for more
  13. * information.
  14. */
  15. #include <linux/dmi.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/pci.h>
  18. #include <linux/pci_ids.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include "pci.h"
  22. enum smbios_attr_enum {
  23. SMBIOS_ATTR_NONE = 0,
  24. SMBIOS_ATTR_LABEL_SHOW,
  25. SMBIOS_ATTR_INSTANCE_SHOW,
  26. };
  27. static mode_t
  28. find_smbios_instance_string(struct pci_dev *pdev, char *buf,
  29. enum smbios_attr_enum attribute)
  30. {
  31. const struct dmi_device *dmi;
  32. struct dmi_dev_onboard *donboard;
  33. int bus;
  34. int devfn;
  35. bus = pdev->bus->number;
  36. devfn = pdev->devfn;
  37. dmi = NULL;
  38. while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
  39. NULL, dmi)) != NULL) {
  40. donboard = dmi->device_data;
  41. if (donboard && donboard->bus == bus &&
  42. donboard->devfn == devfn) {
  43. if (buf) {
  44. if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
  45. return scnprintf(buf, PAGE_SIZE,
  46. "%d\n",
  47. donboard->instance);
  48. else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
  49. return scnprintf(buf, PAGE_SIZE,
  50. "%s\n",
  51. dmi->name);
  52. }
  53. return strlen(dmi->name);
  54. }
  55. }
  56. return 0;
  57. }
  58. static mode_t
  59. smbios_instance_string_exist(struct kobject *kobj, struct attribute *attr,
  60. int n)
  61. {
  62. struct device *dev;
  63. struct pci_dev *pdev;
  64. dev = container_of(kobj, struct device, kobj);
  65. pdev = to_pci_dev(dev);
  66. return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
  67. S_IRUGO : 0;
  68. }
  69. static ssize_t
  70. smbioslabel_show(struct device *dev, struct device_attribute *attr, char *buf)
  71. {
  72. struct pci_dev *pdev;
  73. pdev = to_pci_dev(dev);
  74. return find_smbios_instance_string(pdev, buf,
  75. SMBIOS_ATTR_LABEL_SHOW);
  76. }
  77. static ssize_t
  78. smbiosinstance_show(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct pci_dev *pdev;
  82. pdev = to_pci_dev(dev);
  83. return find_smbios_instance_string(pdev, buf,
  84. SMBIOS_ATTR_INSTANCE_SHOW);
  85. }
  86. static struct device_attribute smbios_attr_label = {
  87. .attr = {.name = "label", .mode = 0444},
  88. .show = smbioslabel_show,
  89. };
  90. static struct device_attribute smbios_attr_instance = {
  91. .attr = {.name = "index", .mode = 0444},
  92. .show = smbiosinstance_show,
  93. };
  94. static struct attribute *smbios_attributes[] = {
  95. &smbios_attr_label.attr,
  96. &smbios_attr_instance.attr,
  97. NULL,
  98. };
  99. static struct attribute_group smbios_attr_group = {
  100. .attrs = smbios_attributes,
  101. .is_visible = smbios_instance_string_exist,
  102. };
  103. static int
  104. pci_create_smbiosname_file(struct pci_dev *pdev)
  105. {
  106. if (!sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group))
  107. return 0;
  108. return -ENODEV;
  109. }
  110. static void
  111. pci_remove_smbiosname_file(struct pci_dev *pdev)
  112. {
  113. sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
  114. }
  115. void pci_create_firmware_label_files(struct pci_dev *pdev)
  116. {
  117. if (!pci_create_smbiosname_file(pdev))
  118. ;
  119. }
  120. void pci_remove_firmware_label_files(struct pci_dev *pdev)
  121. {
  122. pci_remove_smbiosname_file(pdev);
  123. }