dell-rbtn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 const 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 by
  177. * the DSDT unless booted with acpi_osi="!Windows 2012"
  178. * acpi_osi="!Windows 2013".
  179. *
  180. * According to Mario at Dell:
  181. *
  182. * DELLABC6 is a custom interface that was created solely to
  183. * have airplane mode support for Windows 7. For Windows 10
  184. * the proper interface is to use that which is handled by
  185. * intel-hid. A OEM airplane mode driver is not used.
  186. *
  187. * Since the kernel doesn't identify as Windows 7 it would be
  188. * incorrect to do attempt to use that interface.
  189. *
  190. * Even if we override _OSI and bind to DELLABC6, we end up with
  191. * inconsistent behavior in which userspace can get out of sync
  192. * with the rfkill state as it conflicts with events from
  193. * intel-hid.
  194. *
  195. * The upshot is that it is better to just ignore DELLABC6
  196. * devices.
  197. */
  198. { "", 0 },
  199. };
  200. #ifdef CONFIG_PM_SLEEP
  201. static void ACPI_SYSTEM_XFACE rbtn_clear_suspended_flag(void *context)
  202. {
  203. struct rbtn_data *rbtn_data = context;
  204. rbtn_data->suspended = false;
  205. }
  206. static int rbtn_suspend(struct device *dev)
  207. {
  208. struct acpi_device *device = to_acpi_device(dev);
  209. struct rbtn_data *rbtn_data = acpi_driver_data(device);
  210. rbtn_data->suspended = true;
  211. return 0;
  212. }
  213. static int rbtn_resume(struct device *dev)
  214. {
  215. struct acpi_device *device = to_acpi_device(dev);
  216. struct rbtn_data *rbtn_data = acpi_driver_data(device);
  217. acpi_status status;
  218. /*
  219. * Upon resume, some BIOSes send an ACPI notification thet triggers
  220. * an unwanted input event. In order to ignore it, we use a flag
  221. * that we set at suspend and clear once we have received the extra
  222. * ACPI notification. Since ACPI notifications are delivered
  223. * asynchronously to drivers, we clear the flag from the workqueue
  224. * used to deliver the notifications. This should be enough
  225. * to have the flag cleared only after we received the extra
  226. * notification, if any.
  227. */
  228. status = acpi_os_execute(OSL_NOTIFY_HANDLER,
  229. rbtn_clear_suspended_flag, rbtn_data);
  230. if (ACPI_FAILURE(status))
  231. rbtn_clear_suspended_flag(rbtn_data);
  232. return 0;
  233. }
  234. #endif
  235. static SIMPLE_DEV_PM_OPS(rbtn_pm_ops, rbtn_suspend, rbtn_resume);
  236. static struct acpi_driver rbtn_driver = {
  237. .name = "dell-rbtn",
  238. .ids = rbtn_ids,
  239. .drv.pm = &rbtn_pm_ops,
  240. .ops = {
  241. .add = rbtn_add,
  242. .remove = rbtn_remove,
  243. .notify = rbtn_notify,
  244. },
  245. .owner = THIS_MODULE,
  246. };
  247. /*
  248. * notifier export functions
  249. */
  250. static bool auto_remove_rfkill = true;
  251. static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
  252. static int rbtn_inc_count(struct device *dev, void *data)
  253. {
  254. struct acpi_device *device = to_acpi_device(dev);
  255. struct rbtn_data *rbtn_data = device->driver_data;
  256. int *count = data;
  257. if (rbtn_data->type == RBTN_SLIDER)
  258. (*count)++;
  259. return 0;
  260. }
  261. static int rbtn_switch_dev(struct device *dev, void *data)
  262. {
  263. struct acpi_device *device = to_acpi_device(dev);
  264. struct rbtn_data *rbtn_data = device->driver_data;
  265. bool enable = data;
  266. if (rbtn_data->type != RBTN_SLIDER)
  267. return 0;
  268. if (enable)
  269. rbtn_rfkill_init(device);
  270. else
  271. rbtn_rfkill_exit(device);
  272. return 0;
  273. }
  274. int dell_rbtn_notifier_register(struct notifier_block *nb)
  275. {
  276. bool first;
  277. int count;
  278. int ret;
  279. count = 0;
  280. ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
  281. rbtn_inc_count);
  282. if (ret || count == 0)
  283. return -ENODEV;
  284. first = !rbtn_chain_head.head;
  285. ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
  286. if (ret != 0)
  287. return ret;
  288. if (auto_remove_rfkill && first)
  289. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  290. (void *)false, rbtn_switch_dev);
  291. return ret;
  292. }
  293. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
  294. int dell_rbtn_notifier_unregister(struct notifier_block *nb)
  295. {
  296. int ret;
  297. ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
  298. if (ret != 0)
  299. return ret;
  300. if (auto_remove_rfkill && !rbtn_chain_head.head)
  301. ret = driver_for_each_device(&rbtn_driver.drv, NULL,
  302. (void *)true, rbtn_switch_dev);
  303. return ret;
  304. }
  305. EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
  306. /*
  307. * acpi driver functions
  308. */
  309. static int rbtn_add(struct acpi_device *device)
  310. {
  311. struct rbtn_data *rbtn_data;
  312. enum rbtn_type type;
  313. int ret = 0;
  314. type = rbtn_check(device);
  315. if (type == RBTN_UNKNOWN) {
  316. dev_info(&device->dev, "Unknown device type\n");
  317. return -EINVAL;
  318. }
  319. ret = rbtn_acquire(device, true);
  320. if (ret < 0) {
  321. dev_err(&device->dev, "Cannot enable device\n");
  322. return ret;
  323. }
  324. rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
  325. if (!rbtn_data)
  326. return -ENOMEM;
  327. rbtn_data->type = type;
  328. device->driver_data = rbtn_data;
  329. switch (rbtn_data->type) {
  330. case RBTN_TOGGLE:
  331. ret = rbtn_input_init(rbtn_data);
  332. break;
  333. case RBTN_SLIDER:
  334. if (auto_remove_rfkill && rbtn_chain_head.head)
  335. ret = 0;
  336. else
  337. ret = rbtn_rfkill_init(device);
  338. break;
  339. default:
  340. ret = -EINVAL;
  341. }
  342. return ret;
  343. }
  344. static int rbtn_remove(struct acpi_device *device)
  345. {
  346. struct rbtn_data *rbtn_data = device->driver_data;
  347. switch (rbtn_data->type) {
  348. case RBTN_TOGGLE:
  349. rbtn_input_exit(rbtn_data);
  350. break;
  351. case RBTN_SLIDER:
  352. rbtn_rfkill_exit(device);
  353. break;
  354. default:
  355. break;
  356. }
  357. rbtn_acquire(device, false);
  358. device->driver_data = NULL;
  359. return 0;
  360. }
  361. static void rbtn_notify(struct acpi_device *device, u32 event)
  362. {
  363. struct rbtn_data *rbtn_data = device->driver_data;
  364. /*
  365. * Some BIOSes send a notification at resume.
  366. * Ignore it to prevent unwanted input events.
  367. */
  368. if (rbtn_data->suspended) {
  369. dev_dbg(&device->dev, "ACPI notification ignored\n");
  370. return;
  371. }
  372. if (event != 0x80) {
  373. dev_info(&device->dev, "Received unknown event (0x%x)\n",
  374. event);
  375. return;
  376. }
  377. switch (rbtn_data->type) {
  378. case RBTN_TOGGLE:
  379. rbtn_input_event(rbtn_data);
  380. break;
  381. case RBTN_SLIDER:
  382. rbtn_rfkill_event(device);
  383. atomic_notifier_call_chain(&rbtn_chain_head, event, device);
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. /*
  390. * module functions
  391. */
  392. module_acpi_driver(rbtn_driver);
  393. module_param(auto_remove_rfkill, bool, 0444);
  394. MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
  395. "other modules start receiving events "
  396. "from this module and re-add them when "
  397. "the last module stops receiving events "
  398. "(default true)");
  399. MODULE_DEVICE_TABLE(acpi, rbtn_ids);
  400. MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
  401. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  402. MODULE_LICENSE("GPL");