dell-rbtn.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. /*
  174. * This driver can also handle the "DELLABC6" device that
  175. * appears on the XPS 13 9350, but that device is disabled
  176. * by the DSDT unless booted with acpi_osi="!Windows 2012"
  177. * acpi_osi="!Windows 2013". Even if we boot that and bind
  178. * the driver, we seem to have inconsistent behavior in
  179. * which NetworkManager can get out of sync with the rfkill
  180. * state.
  181. *
  182. * On the XPS 13 9350 and similar laptops, we're not supposed to
  183. * use DELLABC6 at all. Instead, we handle the rfkill button
  184. * via the intel-hid driver.
  185. */
  186. { "", 0 },
  187. };
  188. static struct acpi_driver rbtn_driver = {
  189. .name = "dell-rbtn",
  190. .ids = rbtn_ids,
  191. .ops = {
  192. .add = rbtn_add,
  193. .remove = rbtn_remove,
  194. .notify = rbtn_notify,
  195. },
  196. .owner = THIS_MODULE,
  197. };
  198. /*
  199. * notifier export functions
  200. */
  201. static bool auto_remove_rfkill = true;
  202. static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
  203. static int rbtn_inc_count(struct device *dev, void *data)
  204. {
  205. struct acpi_device *device = to_acpi_device(dev);
  206. struct rbtn_data *rbtn_data = device->driver_data;
  207. int *count = data;
  208. if (rbtn_data->type == RBTN_SLIDER)
  209. (*count)++;
  210. return 0;
  211. }
  212. static int rbtn_switch_dev(struct device *dev, void *data)
  213. {
  214. struct acpi_device *device = to_acpi_device(dev);
  215. struct rbtn_data *rbtn_data = device->driver_data;
  216. bool enable = data;
  217. if (rbtn_data->type != RBTN_SLIDER)
  218. return 0;
  219. if (enable)
  220. rbtn_rfkill_init(device);
  221. else
  222. rbtn_rfkill_exit(device);
  223. return 0;
  224. }
  225. int dell_rbtn_notifier_register(struct notifier_block *nb)
  226. {
  227. bool first;
  228. int count;
  229. int ret;
  230. count = 0;
  231. ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
  232. rbtn_inc_count);
  233. if (ret || count == 0)
  234. return -ENODEV;
  235. first = !rbtn_chain_head.head;
  236. ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
  237. if (ret != 0)
  238. return ret;
  239. if (auto_remove_rfkill && first)
  240. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  241. (void *)false, rbtn_switch_dev);
  242. return ret;
  243. }
  244. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
  245. int dell_rbtn_notifier_unregister(struct notifier_block *nb)
  246. {
  247. int ret;
  248. ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
  249. if (ret != 0)
  250. return ret;
  251. if (auto_remove_rfkill && !rbtn_chain_head.head)
  252. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  253. (void *)true, rbtn_switch_dev);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
  257. /*
  258. * acpi driver functions
  259. */
  260. static int rbtn_add(struct acpi_device *device)
  261. {
  262. struct rbtn_data *rbtn_data;
  263. enum rbtn_type type;
  264. int ret = 0;
  265. type = rbtn_check(device);
  266. if (type == RBTN_UNKNOWN) {
  267. dev_info(&device->dev, "Unknown device type\n");
  268. return -EINVAL;
  269. }
  270. ret = rbtn_acquire(device, true);
  271. if (ret < 0) {
  272. dev_err(&device->dev, "Cannot enable device\n");
  273. return ret;
  274. }
  275. rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
  276. if (!rbtn_data)
  277. return -ENOMEM;
  278. rbtn_data->type = type;
  279. device->driver_data = rbtn_data;
  280. switch (rbtn_data->type) {
  281. case RBTN_TOGGLE:
  282. ret = rbtn_input_init(rbtn_data);
  283. break;
  284. case RBTN_SLIDER:
  285. if (auto_remove_rfkill && rbtn_chain_head.head)
  286. ret = 0;
  287. else
  288. ret = rbtn_rfkill_init(device);
  289. break;
  290. default:
  291. ret = -EINVAL;
  292. }
  293. return ret;
  294. }
  295. static int rbtn_remove(struct acpi_device *device)
  296. {
  297. struct rbtn_data *rbtn_data = device->driver_data;
  298. switch (rbtn_data->type) {
  299. case RBTN_TOGGLE:
  300. rbtn_input_exit(rbtn_data);
  301. break;
  302. case RBTN_SLIDER:
  303. rbtn_rfkill_exit(device);
  304. break;
  305. default:
  306. break;
  307. }
  308. rbtn_acquire(device, false);
  309. device->driver_data = NULL;
  310. return 0;
  311. }
  312. static void rbtn_notify(struct acpi_device *device, u32 event)
  313. {
  314. struct rbtn_data *rbtn_data = device->driver_data;
  315. if (event != 0x80) {
  316. dev_info(&device->dev, "Received unknown event (0x%x)\n",
  317. event);
  318. return;
  319. }
  320. switch (rbtn_data->type) {
  321. case RBTN_TOGGLE:
  322. rbtn_input_event(rbtn_data);
  323. break;
  324. case RBTN_SLIDER:
  325. rbtn_rfkill_event(device);
  326. atomic_notifier_call_chain(&rbtn_chain_head, event, device);
  327. break;
  328. default:
  329. break;
  330. }
  331. }
  332. /*
  333. * module functions
  334. */
  335. module_acpi_driver(rbtn_driver);
  336. module_param(auto_remove_rfkill, bool, 0444);
  337. MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
  338. "other modules start receiving events "
  339. "from this module and re-add them when "
  340. "the last module stops receiving events "
  341. "(default true)");
  342. MODULE_DEVICE_TABLE(acpi, rbtn_ids);
  343. MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
  344. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  345. MODULE_LICENSE("GPL");