xo15-ebook.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OLPC XO-1.5 ebook switch driver
  4. * (based on generic ACPI button driver)
  5. *
  6. * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
  7. * Copyright (C) 2010 One Laptop per Child
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/input.h>
  15. #include <linux/acpi.h>
  16. #define MODULE_NAME "xo15-ebook"
  17. #define XO15_EBOOK_CLASS MODULE_NAME
  18. #define XO15_EBOOK_TYPE_UNKNOWN 0x00
  19. #define XO15_EBOOK_NOTIFY_STATUS 0x80
  20. #define XO15_EBOOK_SUBCLASS "ebook"
  21. #define XO15_EBOOK_HID "XO15EBK"
  22. #define XO15_EBOOK_DEVICE_NAME "EBook Switch"
  23. ACPI_MODULE_NAME(MODULE_NAME);
  24. MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
  25. MODULE_LICENSE("GPL");
  26. static const struct acpi_device_id ebook_device_ids[] = {
  27. { XO15_EBOOK_HID, 0 },
  28. { "", 0 },
  29. };
  30. MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
  31. struct ebook_switch {
  32. struct input_dev *input;
  33. char phys[32]; /* for input device */
  34. };
  35. static int ebook_send_state(struct acpi_device *device)
  36. {
  37. struct ebook_switch *button = acpi_driver_data(device);
  38. unsigned long long state;
  39. acpi_status status;
  40. status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
  41. if (ACPI_FAILURE(status))
  42. return -EIO;
  43. /* input layer checks if event is redundant */
  44. input_report_switch(button->input, SW_TABLET_MODE, !state);
  45. input_sync(button->input);
  46. return 0;
  47. }
  48. static void ebook_switch_notify(struct acpi_device *device, u32 event)
  49. {
  50. switch (event) {
  51. case ACPI_FIXED_HARDWARE_EVENT:
  52. case XO15_EBOOK_NOTIFY_STATUS:
  53. ebook_send_state(device);
  54. break;
  55. default:
  56. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  57. "Unsupported event [0x%x]\n", event));
  58. break;
  59. }
  60. }
  61. #ifdef CONFIG_PM_SLEEP
  62. static int ebook_switch_resume(struct device *dev)
  63. {
  64. return ebook_send_state(to_acpi_device(dev));
  65. }
  66. #endif
  67. static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
  68. static int ebook_switch_add(struct acpi_device *device)
  69. {
  70. struct ebook_switch *button;
  71. struct input_dev *input;
  72. const char *hid = acpi_device_hid(device);
  73. char *name, *class;
  74. int error;
  75. button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
  76. if (!button)
  77. return -ENOMEM;
  78. device->driver_data = button;
  79. button->input = input = input_allocate_device();
  80. if (!input) {
  81. error = -ENOMEM;
  82. goto err_free_button;
  83. }
  84. name = acpi_device_name(device);
  85. class = acpi_device_class(device);
  86. if (strcmp(hid, XO15_EBOOK_HID)) {
  87. pr_err("Unsupported hid [%s]\n", hid);
  88. error = -ENODEV;
  89. goto err_free_input;
  90. }
  91. strcpy(name, XO15_EBOOK_DEVICE_NAME);
  92. sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
  93. snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
  94. input->name = name;
  95. input->phys = button->phys;
  96. input->id.bustype = BUS_HOST;
  97. input->dev.parent = &device->dev;
  98. input->evbit[0] = BIT_MASK(EV_SW);
  99. set_bit(SW_TABLET_MODE, input->swbit);
  100. error = input_register_device(input);
  101. if (error)
  102. goto err_free_input;
  103. ebook_send_state(device);
  104. if (device->wakeup.flags.valid) {
  105. /* Button's GPE is run-wake GPE */
  106. acpi_enable_gpe(device->wakeup.gpe_device,
  107. device->wakeup.gpe_number);
  108. device_set_wakeup_enable(&device->dev, true);
  109. }
  110. return 0;
  111. err_free_input:
  112. input_free_device(input);
  113. err_free_button:
  114. kfree(button);
  115. return error;
  116. }
  117. static int ebook_switch_remove(struct acpi_device *device)
  118. {
  119. struct ebook_switch *button = acpi_driver_data(device);
  120. input_unregister_device(button->input);
  121. kfree(button);
  122. return 0;
  123. }
  124. static struct acpi_driver xo15_ebook_driver = {
  125. .name = MODULE_NAME,
  126. .class = XO15_EBOOK_CLASS,
  127. .ids = ebook_device_ids,
  128. .ops = {
  129. .add = ebook_switch_add,
  130. .remove = ebook_switch_remove,
  131. .notify = ebook_switch_notify,
  132. },
  133. .drv.pm = &ebook_switch_pm,
  134. };
  135. module_acpi_driver(xo15_ebook_driver);