xo15-ebook.c 4.2 KB

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