bgrt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2012 Red Hat, Inc <mjg@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/io.h>
  14. #include <acpi/acpi.h>
  15. #include <acpi/acpi_bus.h>
  16. static struct acpi_table_bgrt *bgrt_tab;
  17. static struct kobject *bgrt_kobj;
  18. struct bmp_header {
  19. u16 id;
  20. u32 size;
  21. } __attribute ((packed));
  22. static struct bmp_header bmp_header;
  23. static ssize_t show_version(struct device *dev,
  24. struct device_attribute *attr, char *buf)
  25. {
  26. return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab->version);
  27. }
  28. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  29. static ssize_t show_status(struct device *dev,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab->status);
  33. }
  34. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  35. static ssize_t show_type(struct device *dev,
  36. struct device_attribute *attr, char *buf)
  37. {
  38. return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab->image_type);
  39. }
  40. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  41. static ssize_t show_xoffset(struct device *dev,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab->image_offset_x);
  45. }
  46. static DEVICE_ATTR(xoffset, S_IRUGO, show_xoffset, NULL);
  47. static ssize_t show_yoffset(struct device *dev,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab->image_offset_y);
  51. }
  52. static DEVICE_ATTR(yoffset, S_IRUGO, show_yoffset, NULL);
  53. static ssize_t show_image(struct file *file, struct kobject *kobj,
  54. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  55. {
  56. int size = attr->size;
  57. void __iomem *image = attr->private;
  58. if (off >= size) {
  59. count = 0;
  60. } else {
  61. if (off + count > size)
  62. count = size - off;
  63. memcpy_fromio(buf, image+off, count);
  64. }
  65. return count;
  66. }
  67. static struct bin_attribute image_attr = {
  68. .attr = {
  69. .name = "image",
  70. .mode = S_IRUGO,
  71. },
  72. .read = show_image,
  73. };
  74. static struct attribute *bgrt_attributes[] = {
  75. &dev_attr_version.attr,
  76. &dev_attr_status.attr,
  77. &dev_attr_type.attr,
  78. &dev_attr_xoffset.attr,
  79. &dev_attr_yoffset.attr,
  80. NULL,
  81. };
  82. static struct attribute_group bgrt_attribute_group = {
  83. .attrs = bgrt_attributes,
  84. };
  85. static int __init bgrt_init(void)
  86. {
  87. acpi_status status;
  88. int ret;
  89. void __iomem *bgrt;
  90. if (acpi_disabled)
  91. return -ENODEV;
  92. status = acpi_get_table("BGRT", 0,
  93. (struct acpi_table_header **)&bgrt_tab);
  94. if (ACPI_FAILURE(status))
  95. return -ENODEV;
  96. sysfs_bin_attr_init(&image_attr);
  97. bgrt = ioremap(bgrt_tab->image_address, sizeof(struct bmp_header));
  98. if (!bgrt) {
  99. ret = -EINVAL;
  100. goto out_err;
  101. }
  102. memcpy_fromio(&bmp_header, bgrt, sizeof(bmp_header));
  103. image_attr.size = bmp_header.size;
  104. iounmap(bgrt);
  105. image_attr.private = ioremap(bgrt_tab->image_address, image_attr.size);
  106. if (!image_attr.private) {
  107. ret = -EINVAL;
  108. goto out_err;
  109. }
  110. bgrt_kobj = kobject_create_and_add("bgrt", acpi_kobj);
  111. if (!bgrt_kobj) {
  112. ret = -EINVAL;
  113. goto out_iounmap;
  114. }
  115. ret = sysfs_create_group(bgrt_kobj, &bgrt_attribute_group);
  116. if (ret)
  117. goto out_kobject;
  118. ret = sysfs_create_bin_file(bgrt_kobj, &image_attr);
  119. if (ret)
  120. goto out_group;
  121. return 0;
  122. out_group:
  123. sysfs_remove_group(bgrt_kobj, &bgrt_attribute_group);
  124. out_kobject:
  125. kobject_put(bgrt_kobj);
  126. out_iounmap:
  127. iounmap(image_attr.private);
  128. out_err:
  129. return ret;
  130. }
  131. static void __exit bgrt_exit(void)
  132. {
  133. iounmap(image_attr.private);
  134. sysfs_remove_group(bgrt_kobj, &bgrt_attribute_group);
  135. sysfs_remove_bin_file(bgrt_kobj, &image_attr);
  136. }
  137. module_init(bgrt_init);
  138. module_exit(bgrt_exit);
  139. MODULE_AUTHOR("Matthew Garrett");
  140. MODULE_DESCRIPTION("BGRT boot graphic support");
  141. MODULE_LICENSE("GPL");