asus-wireless.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Asus Wireless Radio Control Driver
  3. *
  4. * Copyright (C) 2015-2016 Endless Mobile, Inc.
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/acpi.h>
  15. #include <linux/input.h>
  16. #include <linux/pci_ids.h>
  17. #include <linux/leds.h>
  18. #define ASUS_WIRELESS_LED_STATUS 0x2
  19. #define ASUS_WIRELESS_LED_OFF 0x4
  20. #define ASUS_WIRELESS_LED_ON 0x5
  21. struct asus_wireless_data {
  22. struct input_dev *idev;
  23. struct acpi_device *adev;
  24. struct workqueue_struct *wq;
  25. struct work_struct led_work;
  26. struct led_classdev led;
  27. int led_state;
  28. };
  29. static u64 asus_wireless_method(acpi_handle handle, const char *method,
  30. int param)
  31. {
  32. struct acpi_object_list p;
  33. union acpi_object obj;
  34. acpi_status s;
  35. u64 ret;
  36. acpi_handle_debug(handle, "Evaluating method %s, parameter %#x\n",
  37. method, param);
  38. obj.type = ACPI_TYPE_INTEGER;
  39. obj.integer.value = param;
  40. p.count = 1;
  41. p.pointer = &obj;
  42. s = acpi_evaluate_integer(handle, (acpi_string) method, &p, &ret);
  43. if (ACPI_FAILURE(s))
  44. acpi_handle_err(handle,
  45. "Failed to eval method %s, param %#x (%d)\n",
  46. method, param, s);
  47. acpi_handle_debug(handle, "%s returned %#x\n", method, (uint) ret);
  48. return ret;
  49. }
  50. static enum led_brightness led_state_get(struct led_classdev *led)
  51. {
  52. struct asus_wireless_data *data;
  53. int s;
  54. data = container_of(led, struct asus_wireless_data, led);
  55. s = asus_wireless_method(acpi_device_handle(data->adev), "HSWC",
  56. ASUS_WIRELESS_LED_STATUS);
  57. if (s == ASUS_WIRELESS_LED_ON)
  58. return LED_FULL;
  59. return LED_OFF;
  60. }
  61. static void led_state_update(struct work_struct *work)
  62. {
  63. struct asus_wireless_data *data;
  64. data = container_of(work, struct asus_wireless_data, led_work);
  65. asus_wireless_method(acpi_device_handle(data->adev), "HSWC",
  66. data->led_state);
  67. }
  68. static void led_state_set(struct led_classdev *led,
  69. enum led_brightness value)
  70. {
  71. struct asus_wireless_data *data;
  72. data = container_of(led, struct asus_wireless_data, led);
  73. data->led_state = value == LED_OFF ? ASUS_WIRELESS_LED_OFF :
  74. ASUS_WIRELESS_LED_ON;
  75. queue_work(data->wq, &data->led_work);
  76. }
  77. static void asus_wireless_notify(struct acpi_device *adev, u32 event)
  78. {
  79. struct asus_wireless_data *data = acpi_driver_data(adev);
  80. dev_dbg(&adev->dev, "event=%#x\n", event);
  81. if (event != 0x88) {
  82. dev_notice(&adev->dev, "Unknown ASHS event: %#x\n", event);
  83. return;
  84. }
  85. input_report_key(data->idev, KEY_RFKILL, 1);
  86. input_sync(data->idev);
  87. input_report_key(data->idev, KEY_RFKILL, 0);
  88. input_sync(data->idev);
  89. }
  90. static int asus_wireless_add(struct acpi_device *adev)
  91. {
  92. struct asus_wireless_data *data;
  93. int err;
  94. data = devm_kzalloc(&adev->dev, sizeof(*data), GFP_KERNEL);
  95. if (!data)
  96. return -ENOMEM;
  97. adev->driver_data = data;
  98. data->idev = devm_input_allocate_device(&adev->dev);
  99. if (!data->idev)
  100. return -ENOMEM;
  101. data->idev->name = "Asus Wireless Radio Control";
  102. data->idev->phys = "asus-wireless/input0";
  103. data->idev->id.bustype = BUS_HOST;
  104. data->idev->id.vendor = PCI_VENDOR_ID_ASUSTEK;
  105. set_bit(EV_KEY, data->idev->evbit);
  106. set_bit(KEY_RFKILL, data->idev->keybit);
  107. err = input_register_device(data->idev);
  108. if (err)
  109. return err;
  110. data->adev = adev;
  111. data->wq = create_singlethread_workqueue("asus_wireless_workqueue");
  112. if (!data->wq)
  113. return -ENOMEM;
  114. INIT_WORK(&data->led_work, led_state_update);
  115. data->led.name = "asus-wireless::airplane";
  116. data->led.brightness_set = led_state_set;
  117. data->led.brightness_get = led_state_get;
  118. data->led.flags = LED_CORE_SUSPENDRESUME;
  119. data->led.max_brightness = 1;
  120. err = devm_led_classdev_register(&adev->dev, &data->led);
  121. if (err)
  122. destroy_workqueue(data->wq);
  123. return err;
  124. }
  125. static int asus_wireless_remove(struct acpi_device *adev)
  126. {
  127. struct asus_wireless_data *data = acpi_driver_data(adev);
  128. if (data->wq) {
  129. devm_led_classdev_unregister(&adev->dev, &data->led);
  130. destroy_workqueue(data->wq);
  131. }
  132. return 0;
  133. }
  134. static const struct acpi_device_id device_ids[] = {
  135. {"ATK4001", 0},
  136. {"ATK4002", 0},
  137. {"", 0},
  138. };
  139. MODULE_DEVICE_TABLE(acpi, device_ids);
  140. static struct acpi_driver asus_wireless_driver = {
  141. .name = "Asus Wireless Radio Control Driver",
  142. .class = "hotkey",
  143. .ids = device_ids,
  144. .ops = {
  145. .add = asus_wireless_add,
  146. .remove = asus_wireless_remove,
  147. .notify = asus_wireless_notify,
  148. },
  149. };
  150. module_acpi_driver(asus_wireless_driver);
  151. MODULE_DESCRIPTION("Asus Wireless Radio Control Driver");
  152. MODULE_AUTHOR("João Paulo Rechi Vita <jprvita@gmail.com>");
  153. MODULE_LICENSE("GPL");