pvpanic.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * pvpanic.c - pvpanic Device Support
  3. *
  4. * Copyright (C) 2013 Fujitsu.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <acpi/acpi_bus.h>
  26. #include <acpi/acpi_drivers.h>
  27. MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
  28. MODULE_DESCRIPTION("pvpanic device driver");
  29. MODULE_LICENSE("GPL");
  30. static int pvpanic_add(struct acpi_device *device);
  31. static int pvpanic_remove(struct acpi_device *device);
  32. static const struct acpi_device_id pvpanic_device_ids[] = {
  33. { "QEMU0001", 0 },
  34. { "", 0 },
  35. };
  36. MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
  37. #define PVPANIC_PANICKED (1 << 0)
  38. static u16 port;
  39. static struct acpi_driver pvpanic_driver = {
  40. .name = "pvpanic",
  41. .class = "QEMU",
  42. .ids = pvpanic_device_ids,
  43. .ops = {
  44. .add = pvpanic_add,
  45. .remove = pvpanic_remove,
  46. },
  47. .owner = THIS_MODULE,
  48. };
  49. static void
  50. pvpanic_send_event(unsigned int event)
  51. {
  52. outb(event, port);
  53. }
  54. static int
  55. pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
  56. void *unused)
  57. {
  58. pvpanic_send_event(PVPANIC_PANICKED);
  59. return NOTIFY_DONE;
  60. }
  61. static struct notifier_block pvpanic_panic_nb = {
  62. .notifier_call = pvpanic_panic_notify,
  63. };
  64. static acpi_status
  65. pvpanic_walk_resources(struct acpi_resource *res, void *context)
  66. {
  67. switch (res->type) {
  68. case ACPI_RESOURCE_TYPE_END_TAG:
  69. return AE_OK;
  70. case ACPI_RESOURCE_TYPE_IO:
  71. port = res->data.io.minimum;
  72. return AE_OK;
  73. default:
  74. return AE_ERROR;
  75. }
  76. }
  77. static int pvpanic_add(struct acpi_device *device)
  78. {
  79. acpi_status status;
  80. u64 ret;
  81. status = acpi_evaluate_integer(device->handle, "_STA", NULL,
  82. &ret);
  83. if (ACPI_FAILURE(status) || (ret & 0x0B) != 0x0B)
  84. return -ENODEV;
  85. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  86. pvpanic_walk_resources, NULL);
  87. if (!port)
  88. return -ENODEV;
  89. atomic_notifier_chain_register(&panic_notifier_list,
  90. &pvpanic_panic_nb);
  91. return 0;
  92. }
  93. static int pvpanic_remove(struct acpi_device *device)
  94. {
  95. atomic_notifier_chain_unregister(&panic_notifier_list,
  96. &pvpanic_panic_nb);
  97. return 0;
  98. }
  99. module_acpi_driver(pvpanic_driver);