dell-rbtn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. bool suspended;
  27. };
  28. /*
  29. * acpi functions
  30. */
  31. static enum rbtn_type rbtn_check(struct acpi_device *device)
  32. {
  33. unsigned long long output;
  34. acpi_status status;
  35. status = acpi_evaluate_integer(device->handle, "CRBT", NULL, &output);
  36. if (ACPI_FAILURE(status))
  37. return RBTN_UNKNOWN;
  38. switch (output) {
  39. case 0:
  40. case 1:
  41. return RBTN_TOGGLE;
  42. case 2:
  43. case 3:
  44. return RBTN_SLIDER;
  45. default:
  46. return RBTN_UNKNOWN;
  47. }
  48. }
  49. static int rbtn_get(struct acpi_device *device)
  50. {
  51. unsigned long long output;
  52. acpi_status status;
  53. status = acpi_evaluate_integer(device->handle, "GRBT", NULL, &output);
  54. if (ACPI_FAILURE(status))
  55. return -EINVAL;
  56. return !output;
  57. }
  58. static int rbtn_acquire(struct acpi_device *device, bool enable)
  59. {
  60. struct acpi_object_list input;
  61. union acpi_object param;
  62. acpi_status status;
  63. param.type = ACPI_TYPE_INTEGER;
  64. param.integer.value = enable;
  65. input.count = 1;
  66. input.pointer = &param;
  67. status = acpi_evaluate_object(device->handle, "ARBT", &input, NULL);
  68. if (ACPI_FAILURE(status))
  69. return -EINVAL;
  70. return 0;
  71. }
  72. /*
  73. * rfkill device
  74. */
  75. static void rbtn_rfkill_query(struct rfkill *rfkill, void *data)
  76. {
  77. struct acpi_device *device = data;
  78. int state;
  79. state = rbtn_get(device);
  80. if (state < 0)
  81. return;
  82. rfkill_set_states(rfkill, state, state);
  83. }
  84. static int rbtn_rfkill_set_block(void *data, bool blocked)
  85. {
  86. /* NOTE: setting soft rfkill state is not supported */
  87. return -EINVAL;
  88. }
  89. static struct rfkill_ops rbtn_ops = {
  90. .query = rbtn_rfkill_query,
  91. .set_block = rbtn_rfkill_set_block,
  92. };
  93. static int rbtn_rfkill_init(struct acpi_device *device)
  94. {
  95. struct rbtn_data *rbtn_data = device->driver_data;
  96. int ret;
  97. if (rbtn_data->rfkill)
  98. return 0;
  99. /*
  100. * NOTE: rbtn controls all radio devices, not only WLAN
  101. * but rfkill interface does not support "ANY" type
  102. * so "WLAN" type is used
  103. */
  104. rbtn_data->rfkill = rfkill_alloc("dell-rbtn", &device->dev,
  105. RFKILL_TYPE_WLAN, &rbtn_ops, device);
  106. if (!rbtn_data->rfkill)
  107. return -ENOMEM;
  108. ret = rfkill_register(rbtn_data->rfkill);
  109. if (ret) {
  110. rfkill_destroy(rbtn_data->rfkill);
  111. rbtn_data->rfkill = NULL;
  112. return ret;
  113. }
  114. return 0;
  115. }
  116. static void rbtn_rfkill_exit(struct acpi_device *device)
  117. {
  118. struct rbtn_data *rbtn_data = device->driver_data;
  119. if (!rbtn_data->rfkill)
  120. return;
  121. rfkill_unregister(rbtn_data->rfkill);
  122. rfkill_destroy(rbtn_data->rfkill);
  123. rbtn_data->rfkill = NULL;
  124. }
  125. static void rbtn_rfkill_event(struct acpi_device *device)
  126. {
  127. struct rbtn_data *rbtn_data = device->driver_data;
  128. if (rbtn_data->rfkill)
  129. rbtn_rfkill_query(rbtn_data->rfkill, device);
  130. }
  131. /*
  132. * input device
  133. */
  134. static int rbtn_input_init(struct rbtn_data *rbtn_data)
  135. {
  136. int ret;
  137. rbtn_data->input_dev = input_allocate_device();
  138. if (!rbtn_data->input_dev)
  139. return -ENOMEM;
  140. rbtn_data->input_dev->name = "DELL Wireless hotkeys";
  141. rbtn_data->input_dev->phys = "dellabce/input0";
  142. rbtn_data->input_dev->id.bustype = BUS_HOST;
  143. rbtn_data->input_dev->evbit[0] = BIT(EV_KEY);
  144. set_bit(KEY_RFKILL, rbtn_data->input_dev->keybit);
  145. ret = input_register_device(rbtn_data->input_dev);
  146. if (ret) {
  147. input_free_device(rbtn_data->input_dev);
  148. rbtn_data->input_dev = NULL;
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static void rbtn_input_exit(struct rbtn_data *rbtn_data)
  154. {
  155. input_unregister_device(rbtn_data->input_dev);
  156. rbtn_data->input_dev = NULL;
  157. }
  158. static void rbtn_input_event(struct rbtn_data *rbtn_data)
  159. {
  160. input_report_key(rbtn_data->input_dev, KEY_RFKILL, 1);
  161. input_sync(rbtn_data->input_dev);
  162. input_report_key(rbtn_data->input_dev, KEY_RFKILL, 0);
  163. input_sync(rbtn_data->input_dev);
  164. }
  165. /*
  166. * acpi driver
  167. */
  168. static int rbtn_add(struct acpi_device *device);
  169. static int rbtn_remove(struct acpi_device *device);
  170. static void rbtn_notify(struct acpi_device *device, u32 event);
  171. static const struct acpi_device_id rbtn_ids[] = {
  172. { "DELRBTN", 0 },
  173. { "DELLABCE", 0 },
  174. /*
  175. * This driver can also handle the "DELLABC6" device that
  176. * appears on the XPS 13 9350, but that device is disabled
  177. * by the DSDT unless booted with acpi_osi="!Windows 2012"
  178. * acpi_osi="!Windows 2013". Even if we boot that and bind
  179. * the driver, we seem to have inconsistent behavior in
  180. * which NetworkManager can get out of sync with the rfkill
  181. * state.
  182. *
  183. * On the XPS 13 9350 and similar laptops, we're not supposed to
  184. * use DELLABC6 at all. Instead, we handle the rfkill button
  185. * via the intel-hid driver.
  186. */
  187. { "", 0 },
  188. };
  189. #ifdef CONFIG_PM_SLEEP
  190. static void ACPI_SYSTEM_XFACE rbtn_clear_suspended_flag(void *context)
  191. {
  192. struct rbtn_data *rbtn_data = context;
  193. rbtn_data->suspended = false;
  194. }
  195. static int rbtn_suspend(struct device *dev)
  196. {
  197. struct acpi_device *device = to_acpi_device(dev);
  198. struct rbtn_data *rbtn_data = acpi_driver_data(device);
  199. rbtn_data->suspended = true;
  200. return 0;
  201. }
  202. static int rbtn_resume(struct device *dev)
  203. {
  204. struct acpi_device *device = to_acpi_device(dev);
  205. struct rbtn_data *rbtn_data = acpi_driver_data(device);
  206. acpi_status status;
  207. /*
  208. * Upon resume, some BIOSes send an ACPI notification thet triggers
  209. * an unwanted input event. In order to ignore it, we use a flag
  210. * that we set at suspend and clear once we have received the extra
  211. * ACPI notification. Since ACPI notifications are delivered
  212. * asynchronously to drivers, we clear the flag from the workqueue
  213. * used to deliver the notifications. This should be enough
  214. * to have the flag cleared only after we received the extra
  215. * notification, if any.
  216. */
  217. status = acpi_os_execute(OSL_NOTIFY_HANDLER,
  218. rbtn_clear_suspended_flag, rbtn_data);
  219. if (ACPI_FAILURE(status))
  220. rbtn_clear_suspended_flag(rbtn_data);
  221. return 0;
  222. }
  223. #endif
  224. static SIMPLE_DEV_PM_OPS(rbtn_pm_ops, rbtn_suspend, rbtn_resume);
  225. static struct acpi_driver rbtn_driver = {
  226. .name = "dell-rbtn",
  227. .ids = rbtn_ids,
  228. .drv.pm = &rbtn_pm_ops,
  229. .ops = {
  230. .add = rbtn_add,
  231. .remove = rbtn_remove,
  232. .notify = rbtn_notify,
  233. },
  234. .owner = THIS_MODULE,
  235. };
  236. /*
  237. * notifier export functions
  238. */
  239. static bool auto_remove_rfkill = true;
  240. static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
  241. static int rbtn_inc_count(struct device *dev, void *data)
  242. {
  243. struct acpi_device *device = to_acpi_device(dev);
  244. struct rbtn_data *rbtn_data = device->driver_data;
  245. int *count = data;
  246. if (rbtn_data->type == RBTN_SLIDER)
  247. (*count)++;
  248. return 0;
  249. }
  250. static int rbtn_switch_dev(struct device *dev, void *data)
  251. {
  252. struct acpi_device *device = to_acpi_device(dev);
  253. struct rbtn_data *rbtn_data = device->driver_data;
  254. bool enable = data;
  255. if (rbtn_data->type != RBTN_SLIDER)
  256. return 0;
  257. if (enable)
  258. rbtn_rfkill_init(device);
  259. else
  260. rbtn_rfkill_exit(device);
  261. return 0;
  262. }
  263. int dell_rbtn_notifier_register(struct notifier_block *nb)
  264. {
  265. bool first;
  266. int count;
  267. int ret;
  268. count = 0;
  269. ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
  270. rbtn_inc_count);
  271. if (ret || count == 0)
  272. return -ENODEV;
  273. first = !rbtn_chain_head.head;
  274. ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
  275. if (ret != 0)
  276. return ret;
  277. if (auto_remove_rfkill && first)
  278. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  279. (void *)false, rbtn_switch_dev);
  280. return ret;
  281. }
  282. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
  283. int dell_rbtn_notifier_unregister(struct notifier_block *nb)
  284. {
  285. int ret;
  286. ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
  287. if (ret != 0)
  288. return ret;
  289. if (auto_remove_rfkill && !rbtn_chain_head.head)
  290. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  291. (void *)true, rbtn_switch_dev);
  292. return ret;
  293. }
  294. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
  295. /*
  296. * acpi driver functions
  297. */
  298. static int rbtn_add(struct acpi_device *device)
  299. {
  300. struct rbtn_data *rbtn_data;
  301. enum rbtn_type type;
  302. int ret = 0;
  303. type = rbtn_check(device);
  304. if (type == RBTN_UNKNOWN) {
  305. dev_info(&device->dev, "Unknown device type\n");
  306. return -EINVAL;
  307. }
  308. ret = rbtn_acquire(device, true);
  309. if (ret < 0) {
  310. dev_err(&device->dev, "Cannot enable device\n");
  311. return ret;
  312. }
  313. rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
  314. if (!rbtn_data)
  315. return -ENOMEM;
  316. rbtn_data->type = type;
  317. device->driver_data = rbtn_data;
  318. switch (rbtn_data->type) {
  319. case RBTN_TOGGLE:
  320. ret = rbtn_input_init(rbtn_data);
  321. break;
  322. case RBTN_SLIDER:
  323. if (auto_remove_rfkill && rbtn_chain_head.head)
  324. ret = 0;
  325. else
  326. ret = rbtn_rfkill_init(device);
  327. break;
  328. default:
  329. ret = -EINVAL;
  330. }
  331. return ret;
  332. }
  333. static int rbtn_remove(struct acpi_device *device)
  334. {
  335. struct rbtn_data *rbtn_data = device->driver_data;
  336. switch (rbtn_data->type) {
  337. case RBTN_TOGGLE:
  338. rbtn_input_exit(rbtn_data);
  339. break;
  340. case RBTN_SLIDER:
  341. rbtn_rfkill_exit(device);
  342. break;
  343. default:
  344. break;
  345. }
  346. rbtn_acquire(device, false);
  347. device->driver_data = NULL;
  348. return 0;
  349. }
  350. static void rbtn_notify(struct acpi_device *device, u32 event)
  351. {
  352. struct rbtn_data *rbtn_data = device->driver_data;
  353. /*
  354. * Some BIOSes send a notification at resume.
  355. * Ignore it to prevent unwanted input events.
  356. */
  357. if (rbtn_data->suspended) {
  358. dev_dbg(&device->dev, "ACPI notification ignored\n");
  359. return;
  360. }
  361. if (event != 0x80) {
  362. dev_info(&device->dev, "Received unknown event (0x%x)\n",
  363. event);
  364. return;
  365. }
  366. switch (rbtn_data->type) {
  367. case RBTN_TOGGLE:
  368. rbtn_input_event(rbtn_data);
  369. break;
  370. case RBTN_SLIDER:
  371. rbtn_rfkill_event(device);
  372. atomic_notifier_call_chain(&rbtn_chain_head, event, device);
  373. break;
  374. default:
  375. break;
  376. }
  377. }
  378. /*
  379. * module functions
  380. */
  381. module_acpi_driver(rbtn_driver);
  382. module_param(auto_remove_rfkill, bool, 0444);
  383. MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
  384. "other modules start receiving events "
  385. "from this module and re-add them when "
  386. "the last module stops receiving events "
  387. "(default true)");
  388. MODULE_DEVICE_TABLE(acpi, rbtn_ids);
  389. MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
  390. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  391. MODULE_LICENSE("GPL");