dell-rbtn.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. Dell Airplane Mode Switch driver
  3. Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/acpi.h>
  15. #include <linux/rfkill.h>
  16. #include <linux/input.h>
  17. enum rbtn_type {
  18. RBTN_UNKNOWN,
  19. RBTN_TOGGLE,
  20. RBTN_SLIDER,
  21. };
  22. struct rbtn_data {
  23. enum rbtn_type type;
  24. struct rfkill *rfkill;
  25. struct input_dev *input_dev;
  26. };
  27. /*
  28. * acpi functions
  29. */
  30. static enum rbtn_type rbtn_check(struct acpi_device *device)
  31. {
  32. unsigned long long output;
  33. acpi_status status;
  34. status = acpi_evaluate_integer(device->handle, "CRBT", NULL, &output);
  35. if (ACPI_FAILURE(status))
  36. return RBTN_UNKNOWN;
  37. switch (output) {
  38. case 0:
  39. case 1:
  40. return RBTN_TOGGLE;
  41. case 2:
  42. case 3:
  43. return RBTN_SLIDER;
  44. default:
  45. return RBTN_UNKNOWN;
  46. }
  47. }
  48. static int rbtn_get(struct acpi_device *device)
  49. {
  50. unsigned long long output;
  51. acpi_status status;
  52. status = acpi_evaluate_integer(device->handle, "GRBT", NULL, &output);
  53. if (ACPI_FAILURE(status))
  54. return -EINVAL;
  55. return !output;
  56. }
  57. static int rbtn_acquire(struct acpi_device *device, bool enable)
  58. {
  59. struct acpi_object_list input;
  60. union acpi_object param;
  61. acpi_status status;
  62. param.type = ACPI_TYPE_INTEGER;
  63. param.integer.value = enable;
  64. input.count = 1;
  65. input.pointer = &param;
  66. status = acpi_evaluate_object(device->handle, "ARBT", &input, NULL);
  67. if (ACPI_FAILURE(status))
  68. return -EINVAL;
  69. return 0;
  70. }
  71. /*
  72. * rfkill device
  73. */
  74. static void rbtn_rfkill_query(struct rfkill *rfkill, void *data)
  75. {
  76. struct acpi_device *device = data;
  77. int state;
  78. state = rbtn_get(device);
  79. if (state < 0)
  80. return;
  81. rfkill_set_states(rfkill, state, state);
  82. }
  83. static int rbtn_rfkill_set_block(void *data, bool blocked)
  84. {
  85. /* NOTE: setting soft rfkill state is not supported */
  86. return -EINVAL;
  87. }
  88. static struct rfkill_ops rbtn_ops = {
  89. .query = rbtn_rfkill_query,
  90. .set_block = rbtn_rfkill_set_block,
  91. };
  92. static int rbtn_rfkill_init(struct acpi_device *device)
  93. {
  94. struct rbtn_data *rbtn_data = device->driver_data;
  95. int ret;
  96. if (rbtn_data->rfkill)
  97. return 0;
  98. /*
  99. * NOTE: rbtn controls all radio devices, not only WLAN
  100. * but rfkill interface does not support "ANY" type
  101. * so "WLAN" type is used
  102. */
  103. rbtn_data->rfkill = rfkill_alloc("dell-rbtn", &device->dev,
  104. RFKILL_TYPE_WLAN, &rbtn_ops, device);
  105. if (!rbtn_data->rfkill)
  106. return -ENOMEM;
  107. ret = rfkill_register(rbtn_data->rfkill);
  108. if (ret) {
  109. rfkill_destroy(rbtn_data->rfkill);
  110. rbtn_data->rfkill = NULL;
  111. return ret;
  112. }
  113. return 0;
  114. }
  115. static void rbtn_rfkill_exit(struct acpi_device *device)
  116. {
  117. struct rbtn_data *rbtn_data = device->driver_data;
  118. if (!rbtn_data->rfkill)
  119. return;
  120. rfkill_unregister(rbtn_data->rfkill);
  121. rfkill_destroy(rbtn_data->rfkill);
  122. rbtn_data->rfkill = NULL;
  123. }
  124. static void rbtn_rfkill_event(struct acpi_device *device)
  125. {
  126. struct rbtn_data *rbtn_data = device->driver_data;
  127. if (rbtn_data->rfkill)
  128. rbtn_rfkill_query(rbtn_data->rfkill, device);
  129. }
  130. /*
  131. * input device
  132. */
  133. static int rbtn_input_init(struct rbtn_data *rbtn_data)
  134. {
  135. int ret;
  136. rbtn_data->input_dev = input_allocate_device();
  137. if (!rbtn_data->input_dev)
  138. return -ENOMEM;
  139. rbtn_data->input_dev->name = "DELL Wireless hotkeys";
  140. rbtn_data->input_dev->phys = "dellabce/input0";
  141. rbtn_data->input_dev->id.bustype = BUS_HOST;
  142. rbtn_data->input_dev->evbit[0] = BIT(EV_KEY);
  143. set_bit(KEY_RFKILL, rbtn_data->input_dev->keybit);
  144. ret = input_register_device(rbtn_data->input_dev);
  145. if (ret) {
  146. input_free_device(rbtn_data->input_dev);
  147. rbtn_data->input_dev = NULL;
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static void rbtn_input_exit(struct rbtn_data *rbtn_data)
  153. {
  154. input_unregister_device(rbtn_data->input_dev);
  155. rbtn_data->input_dev = NULL;
  156. }
  157. static void rbtn_input_event(struct rbtn_data *rbtn_data)
  158. {
  159. input_report_key(rbtn_data->input_dev, KEY_RFKILL, 1);
  160. input_sync(rbtn_data->input_dev);
  161. input_report_key(rbtn_data->input_dev, KEY_RFKILL, 0);
  162. input_sync(rbtn_data->input_dev);
  163. }
  164. /*
  165. * acpi driver
  166. */
  167. static int rbtn_add(struct acpi_device *device);
  168. static int rbtn_remove(struct acpi_device *device);
  169. static void rbtn_notify(struct acpi_device *device, u32 event);
  170. static const struct acpi_device_id rbtn_ids[] = {
  171. { "DELRBTN", 0 },
  172. { "DELLABCE", 0 },
  173. { "", 0 },
  174. };
  175. static struct acpi_driver rbtn_driver = {
  176. .name = "dell-rbtn",
  177. .ids = rbtn_ids,
  178. .ops = {
  179. .add = rbtn_add,
  180. .remove = rbtn_remove,
  181. .notify = rbtn_notify,
  182. },
  183. .owner = THIS_MODULE,
  184. };
  185. /*
  186. * notifier export functions
  187. */
  188. static bool auto_remove_rfkill = true;
  189. static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
  190. static int rbtn_inc_count(struct device *dev, void *data)
  191. {
  192. struct acpi_device *device = to_acpi_device(dev);
  193. struct rbtn_data *rbtn_data = device->driver_data;
  194. int *count = data;
  195. if (rbtn_data->type == RBTN_SLIDER)
  196. (*count)++;
  197. return 0;
  198. }
  199. static int rbtn_switch_dev(struct device *dev, void *data)
  200. {
  201. struct acpi_device *device = to_acpi_device(dev);
  202. struct rbtn_data *rbtn_data = device->driver_data;
  203. bool enable = data;
  204. if (rbtn_data->type != RBTN_SLIDER)
  205. return 0;
  206. if (enable)
  207. rbtn_rfkill_init(device);
  208. else
  209. rbtn_rfkill_exit(device);
  210. return 0;
  211. }
  212. int dell_rbtn_notifier_register(struct notifier_block *nb)
  213. {
  214. bool first;
  215. int count;
  216. int ret;
  217. count = 0;
  218. ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
  219. rbtn_inc_count);
  220. if (ret || count == 0)
  221. return -ENODEV;
  222. first = !rbtn_chain_head.head;
  223. ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
  224. if (ret != 0)
  225. return ret;
  226. if (auto_remove_rfkill && first)
  227. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  228. (void *)false, rbtn_switch_dev);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
  232. int dell_rbtn_notifier_unregister(struct notifier_block *nb)
  233. {
  234. int ret;
  235. ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
  236. if (ret != 0)
  237. return ret;
  238. if (auto_remove_rfkill && !rbtn_chain_head.head)
  239. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  240. (void *)true, rbtn_switch_dev);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
  244. /*
  245. * acpi driver functions
  246. */
  247. static int rbtn_add(struct acpi_device *device)
  248. {
  249. struct rbtn_data *rbtn_data;
  250. enum rbtn_type type;
  251. int ret = 0;
  252. type = rbtn_check(device);
  253. if (type == RBTN_UNKNOWN) {
  254. dev_info(&device->dev, "Unknown device type\n");
  255. return -EINVAL;
  256. }
  257. ret = rbtn_acquire(device, true);
  258. if (ret < 0) {
  259. dev_err(&device->dev, "Cannot enable device\n");
  260. return ret;
  261. }
  262. rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
  263. if (!rbtn_data)
  264. return -ENOMEM;
  265. rbtn_data->type = type;
  266. device->driver_data = rbtn_data;
  267. switch (rbtn_data->type) {
  268. case RBTN_TOGGLE:
  269. ret = rbtn_input_init(rbtn_data);
  270. break;
  271. case RBTN_SLIDER:
  272. if (auto_remove_rfkill && rbtn_chain_head.head)
  273. ret = 0;
  274. else
  275. ret = rbtn_rfkill_init(device);
  276. break;
  277. default:
  278. ret = -EINVAL;
  279. }
  280. return ret;
  281. }
  282. static int rbtn_remove(struct acpi_device *device)
  283. {
  284. struct rbtn_data *rbtn_data = device->driver_data;
  285. switch (rbtn_data->type) {
  286. case RBTN_TOGGLE:
  287. rbtn_input_exit(rbtn_data);
  288. break;
  289. case RBTN_SLIDER:
  290. rbtn_rfkill_exit(device);
  291. break;
  292. default:
  293. break;
  294. }
  295. rbtn_acquire(device, false);
  296. device->driver_data = NULL;
  297. return 0;
  298. }
  299. static void rbtn_notify(struct acpi_device *device, u32 event)
  300. {
  301. struct rbtn_data *rbtn_data = device->driver_data;
  302. if (event != 0x80) {
  303. dev_info(&device->dev, "Received unknown event (0x%x)\n",
  304. event);
  305. return;
  306. }
  307. switch (rbtn_data->type) {
  308. case RBTN_TOGGLE:
  309. rbtn_input_event(rbtn_data);
  310. break;
  311. case RBTN_SLIDER:
  312. rbtn_rfkill_event(device);
  313. atomic_notifier_call_chain(&rbtn_chain_head, event, device);
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. /*
  320. * module functions
  321. */
  322. module_acpi_driver(rbtn_driver);
  323. module_param(auto_remove_rfkill, bool, 0444);
  324. MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
  325. "other modules start receiving events "
  326. "from this module and re-add them when "
  327. "the last module stops receiving events "
  328. "(default true)");
  329. MODULE_DEVICE_TABLE(acpi, rbtn_ids);
  330. MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
  331. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  332. MODULE_LICENSE("GPL");