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