eeepc-wmi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * Eee PC WMI hotkey driver
  3. *
  4. * Copyright(C) 2010 Intel Corporation.
  5. * Copyright(C) 2010 Corentin Chary <corentin.chary@gmail.com>
  6. *
  7. * Portions based on wistron_btns.c:
  8. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  9. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  10. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/slab.h>
  32. #include <linux/input.h>
  33. #include <linux/input/sparse-keymap.h>
  34. #include <linux/fb.h>
  35. #include <linux/backlight.h>
  36. #include <linux/leds.h>
  37. #include <linux/rfkill.h>
  38. #include <linux/pci.h>
  39. #include <linux/pci_hotplug.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/dmi.h>
  44. #include <acpi/acpi_bus.h>
  45. #include <acpi/acpi_drivers.h>
  46. #define EEEPC_WMI_FILE "eeepc-wmi"
  47. MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
  48. MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
  49. MODULE_LICENSE("GPL");
  50. #define EEEPC_ACPI_HID "ASUS010" /* old _HID used in eeepc-laptop */
  51. #define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
  52. #define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
  53. MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
  54. MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
  55. #define NOTIFY_BRNUP_MIN 0x11
  56. #define NOTIFY_BRNUP_MAX 0x1f
  57. #define NOTIFY_BRNDOWN_MIN 0x20
  58. #define NOTIFY_BRNDOWN_MAX 0x2e
  59. #define EEEPC_WMI_METHODID_DEVS 0x53564544
  60. #define EEEPC_WMI_METHODID_DSTS 0x53544344
  61. #define EEEPC_WMI_METHODID_CFVS 0x53564643
  62. #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
  63. #define EEEPC_WMI_DEVID_TPDLED 0x00100011
  64. #define EEEPC_WMI_DEVID_WLAN 0x00010011
  65. #define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
  66. #define EEEPC_WMI_DEVID_WWAN3G 0x00010019
  67. static bool hotplug_wireless;
  68. module_param(hotplug_wireless, bool, 0444);
  69. MODULE_PARM_DESC(hotplug_wireless,
  70. "Enable hotplug for wireless device. "
  71. "If your laptop needs that, please report to "
  72. "acpi4asus-user@lists.sourceforge.net.");
  73. static const struct key_entry eeepc_wmi_keymap[] = {
  74. /* Sleep already handled via generic ACPI code */
  75. { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
  76. { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
  77. { KE_KEY, 0x30, { KEY_VOLUMEUP } },
  78. { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
  79. { KE_KEY, 0x32, { KEY_MUTE } },
  80. { KE_KEY, 0x5c, { KEY_F15 } },
  81. { KE_KEY, 0x5d, { KEY_WLAN } },
  82. { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
  83. { KE_KEY, 0x88, { KEY_WLAN } },
  84. { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
  85. { KE_KEY, 0xe0, { KEY_PROG1 } },
  86. { KE_KEY, 0xe1, { KEY_F14 } },
  87. { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
  88. { KE_END, 0},
  89. };
  90. struct bios_args {
  91. u32 dev_id;
  92. u32 ctrl_param;
  93. };
  94. /*
  95. * eeepc-wmi/ - debugfs root directory
  96. * dev_id - current dev_id
  97. * ctrl_param - current ctrl_param
  98. * devs - call DEVS(dev_id, ctrl_param) and print result
  99. * dsts - call DSTS(dev_id) and print result
  100. */
  101. struct eeepc_wmi_debug {
  102. struct dentry *root;
  103. u32 dev_id;
  104. u32 ctrl_param;
  105. };
  106. struct eeepc_wmi {
  107. bool hotplug_wireless;
  108. struct input_dev *inputdev;
  109. struct backlight_device *backlight_device;
  110. struct platform_device *platform_device;
  111. struct led_classdev tpd_led;
  112. int tpd_led_wk;
  113. struct workqueue_struct *led_workqueue;
  114. struct work_struct tpd_led_work;
  115. struct rfkill *wlan_rfkill;
  116. struct rfkill *bluetooth_rfkill;
  117. struct rfkill *wwan3g_rfkill;
  118. struct hotplug_slot *hotplug_slot;
  119. struct mutex hotplug_lock;
  120. struct mutex wmi_lock;
  121. struct workqueue_struct *hotplug_workqueue;
  122. struct work_struct hotplug_work;
  123. struct eeepc_wmi_debug debug;
  124. };
  125. /* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
  126. static struct platform_device *platform_device;
  127. static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
  128. {
  129. int err;
  130. eeepc->inputdev = input_allocate_device();
  131. if (!eeepc->inputdev)
  132. return -ENOMEM;
  133. eeepc->inputdev->name = "Eee PC WMI hotkeys";
  134. eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
  135. eeepc->inputdev->id.bustype = BUS_HOST;
  136. eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
  137. err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
  138. if (err)
  139. goto err_free_dev;
  140. err = input_register_device(eeepc->inputdev);
  141. if (err)
  142. goto err_free_keymap;
  143. return 0;
  144. err_free_keymap:
  145. sparse_keymap_free(eeepc->inputdev);
  146. err_free_dev:
  147. input_free_device(eeepc->inputdev);
  148. return err;
  149. }
  150. static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
  151. {
  152. if (eeepc->inputdev) {
  153. sparse_keymap_free(eeepc->inputdev);
  154. input_unregister_device(eeepc->inputdev);
  155. }
  156. eeepc->inputdev = NULL;
  157. }
  158. static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
  159. {
  160. struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
  161. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  162. union acpi_object *obj;
  163. acpi_status status;
  164. u32 tmp;
  165. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  166. 1, EEEPC_WMI_METHODID_DSTS,
  167. &input, &output);
  168. if (ACPI_FAILURE(status))
  169. return status;
  170. obj = (union acpi_object *)output.pointer;
  171. if (obj && obj->type == ACPI_TYPE_INTEGER)
  172. tmp = (u32)obj->integer.value;
  173. else
  174. tmp = 0;
  175. if (retval)
  176. *retval = tmp;
  177. kfree(obj);
  178. return status;
  179. }
  180. static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
  181. u32 *retval)
  182. {
  183. struct bios_args args = {
  184. .dev_id = dev_id,
  185. .ctrl_param = ctrl_param,
  186. };
  187. struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
  188. acpi_status status;
  189. if (!retval) {
  190. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
  191. EEEPC_WMI_METHODID_DEVS,
  192. &input, NULL);
  193. } else {
  194. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  195. union acpi_object *obj;
  196. u32 tmp;
  197. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
  198. EEEPC_WMI_METHODID_DEVS,
  199. &input, &output);
  200. if (ACPI_FAILURE(status))
  201. return status;
  202. obj = (union acpi_object *)output.pointer;
  203. if (obj && obj->type == ACPI_TYPE_INTEGER)
  204. tmp = (u32)obj->integer.value;
  205. else
  206. tmp = 0;
  207. *retval = tmp;
  208. kfree(obj);
  209. }
  210. return status;
  211. }
  212. /* Helper for special devices with magic return codes */
  213. static int eeepc_wmi_get_devstate_simple(u32 dev_id)
  214. {
  215. u32 retval = 0;
  216. acpi_status status;
  217. status = eeepc_wmi_get_devstate(dev_id, &retval);
  218. if (ACPI_FAILURE(status))
  219. return -EINVAL;
  220. /* If the device is present, DSTS will always set some bits
  221. * 0x00070000 - 1110000000000000000 - device supported
  222. * 0x00060000 - 1100000000000000000 - not supported
  223. * 0x00020000 - 0100000000000000000 - device supported
  224. * 0x00010000 - 0010000000000000000 - not supported / special mode ?
  225. */
  226. if (!retval || retval == 0x00060000)
  227. return -ENODEV;
  228. return retval & 0x1;
  229. }
  230. /*
  231. * LEDs
  232. */
  233. /*
  234. * These functions actually update the LED's, and are called from a
  235. * workqueue. By doing this as separate work rather than when the LED
  236. * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
  237. * potentially bad time, such as a timer interrupt.
  238. */
  239. static void tpd_led_update(struct work_struct *work)
  240. {
  241. int ctrl_param;
  242. struct eeepc_wmi *eeepc;
  243. eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
  244. ctrl_param = eeepc->tpd_led_wk;
  245. eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
  246. }
  247. static void tpd_led_set(struct led_classdev *led_cdev,
  248. enum led_brightness value)
  249. {
  250. struct eeepc_wmi *eeepc;
  251. eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
  252. eeepc->tpd_led_wk = !!value;
  253. queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
  254. }
  255. static int read_tpd_state(struct eeepc_wmi *eeepc)
  256. {
  257. return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TPDLED);
  258. }
  259. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  260. {
  261. struct eeepc_wmi *eeepc;
  262. eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
  263. return read_tpd_state(eeepc);
  264. }
  265. static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
  266. {
  267. int rv;
  268. if (read_tpd_state(eeepc) < 0)
  269. return 0;
  270. eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
  271. if (!eeepc->led_workqueue)
  272. return -ENOMEM;
  273. INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
  274. eeepc->tpd_led.name = "eeepc::touchpad";
  275. eeepc->tpd_led.brightness_set = tpd_led_set;
  276. eeepc->tpd_led.brightness_get = tpd_led_get;
  277. eeepc->tpd_led.max_brightness = 1;
  278. rv = led_classdev_register(&eeepc->platform_device->dev,
  279. &eeepc->tpd_led);
  280. if (rv) {
  281. destroy_workqueue(eeepc->led_workqueue);
  282. return rv;
  283. }
  284. return 0;
  285. }
  286. static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
  287. {
  288. if (eeepc->tpd_led.dev)
  289. led_classdev_unregister(&eeepc->tpd_led);
  290. if (eeepc->led_workqueue)
  291. destroy_workqueue(eeepc->led_workqueue);
  292. }
  293. /*
  294. * PCI hotplug (for wlan rfkill)
  295. */
  296. static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
  297. {
  298. int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
  299. if (result < 0)
  300. return false;
  301. return !result;
  302. }
  303. static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
  304. {
  305. struct pci_dev *dev;
  306. struct pci_bus *bus;
  307. bool blocked;
  308. bool absent;
  309. u32 l;
  310. mutex_lock(&eeepc->wmi_lock);
  311. blocked = eeepc_wlan_rfkill_blocked(eeepc);
  312. mutex_unlock(&eeepc->wmi_lock);
  313. mutex_lock(&eeepc->hotplug_lock);
  314. if (eeepc->wlan_rfkill)
  315. rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
  316. if (eeepc->hotplug_slot) {
  317. bus = pci_find_bus(0, 1);
  318. if (!bus) {
  319. pr_warning("Unable to find PCI bus 1?\n");
  320. goto out_unlock;
  321. }
  322. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  323. pr_err("Unable to read PCI config space?\n");
  324. goto out_unlock;
  325. }
  326. absent = (l == 0xffffffff);
  327. if (blocked != absent) {
  328. pr_warning("BIOS says wireless lan is %s, "
  329. "but the pci device is %s\n",
  330. blocked ? "blocked" : "unblocked",
  331. absent ? "absent" : "present");
  332. pr_warning("skipped wireless hotplug as probably "
  333. "inappropriate for this model\n");
  334. goto out_unlock;
  335. }
  336. if (!blocked) {
  337. dev = pci_get_slot(bus, 0);
  338. if (dev) {
  339. /* Device already present */
  340. pci_dev_put(dev);
  341. goto out_unlock;
  342. }
  343. dev = pci_scan_single_device(bus, 0);
  344. if (dev) {
  345. pci_bus_assign_resources(bus);
  346. if (pci_bus_add_device(dev))
  347. pr_err("Unable to hotplug wifi\n");
  348. }
  349. } else {
  350. dev = pci_get_slot(bus, 0);
  351. if (dev) {
  352. pci_remove_bus_device(dev);
  353. pci_dev_put(dev);
  354. }
  355. }
  356. }
  357. out_unlock:
  358. mutex_unlock(&eeepc->hotplug_lock);
  359. }
  360. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  361. {
  362. struct eeepc_wmi *eeepc = data;
  363. if (event != ACPI_NOTIFY_BUS_CHECK)
  364. return;
  365. /*
  366. * We can't call directly eeepc_rfkill_hotplug because most
  367. * of the time WMBC is still being executed and not reetrant.
  368. * There is currently no way to tell ACPICA that we want this
  369. * method to be serialized, we schedule a eeepc_rfkill_hotplug
  370. * call later, in a safer context.
  371. */
  372. queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
  373. }
  374. static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
  375. char *node)
  376. {
  377. acpi_status status;
  378. acpi_handle handle;
  379. status = acpi_get_handle(NULL, node, &handle);
  380. if (ACPI_SUCCESS(status)) {
  381. status = acpi_install_notify_handler(handle,
  382. ACPI_SYSTEM_NOTIFY,
  383. eeepc_rfkill_notify,
  384. eeepc);
  385. if (ACPI_FAILURE(status))
  386. pr_warning("Failed to register notify on %s\n", node);
  387. } else
  388. return -ENODEV;
  389. return 0;
  390. }
  391. static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
  392. char *node)
  393. {
  394. acpi_status status = AE_OK;
  395. acpi_handle handle;
  396. status = acpi_get_handle(NULL, node, &handle);
  397. if (ACPI_SUCCESS(status)) {
  398. status = acpi_remove_notify_handler(handle,
  399. ACPI_SYSTEM_NOTIFY,
  400. eeepc_rfkill_notify);
  401. if (ACPI_FAILURE(status))
  402. pr_err("Error removing rfkill notify handler %s\n",
  403. node);
  404. }
  405. }
  406. static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
  407. u8 *value)
  408. {
  409. int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
  410. if (result < 0)
  411. return result;
  412. *value = !!result;
  413. return 0;
  414. }
  415. static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  416. {
  417. kfree(hotplug_slot->info);
  418. kfree(hotplug_slot);
  419. }
  420. static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
  421. .owner = THIS_MODULE,
  422. .get_adapter_status = eeepc_get_adapter_status,
  423. .get_power_status = eeepc_get_adapter_status,
  424. };
  425. static void eeepc_hotplug_work(struct work_struct *work)
  426. {
  427. struct eeepc_wmi *eeepc;
  428. eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
  429. eeepc_rfkill_hotplug(eeepc);
  430. }
  431. static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
  432. {
  433. int ret = -ENOMEM;
  434. struct pci_bus *bus = pci_find_bus(0, 1);
  435. if (!bus) {
  436. pr_err("Unable to find wifi PCI bus\n");
  437. return -ENODEV;
  438. }
  439. eeepc->hotplug_workqueue =
  440. create_singlethread_workqueue("hotplug_workqueue");
  441. if (!eeepc->hotplug_workqueue)
  442. goto error_workqueue;
  443. INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
  444. eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  445. if (!eeepc->hotplug_slot)
  446. goto error_slot;
  447. eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  448. GFP_KERNEL);
  449. if (!eeepc->hotplug_slot->info)
  450. goto error_info;
  451. eeepc->hotplug_slot->private = eeepc;
  452. eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
  453. eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
  454. eeepc_get_adapter_status(eeepc->hotplug_slot,
  455. &eeepc->hotplug_slot->info->adapter_status);
  456. ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
  457. if (ret) {
  458. pr_err("Unable to register hotplug slot - %d\n", ret);
  459. goto error_register;
  460. }
  461. return 0;
  462. error_register:
  463. kfree(eeepc->hotplug_slot->info);
  464. error_info:
  465. kfree(eeepc->hotplug_slot);
  466. eeepc->hotplug_slot = NULL;
  467. error_slot:
  468. destroy_workqueue(eeepc->hotplug_workqueue);
  469. error_workqueue:
  470. return ret;
  471. }
  472. /*
  473. * Rfkill devices
  474. */
  475. static int eeepc_rfkill_set(void *data, bool blocked)
  476. {
  477. int dev_id = (unsigned long)data;
  478. u32 ctrl_param = !blocked;
  479. acpi_status status;
  480. status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
  481. if (ACPI_FAILURE(status))
  482. return -EIO;
  483. return 0;
  484. }
  485. static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
  486. {
  487. int dev_id = (unsigned long)data;
  488. int result;
  489. result = eeepc_wmi_get_devstate_simple(dev_id);
  490. if (result < 0)
  491. return ;
  492. rfkill_set_sw_state(rfkill, !result);
  493. }
  494. static int eeepc_rfkill_wlan_set(void *data, bool blocked)
  495. {
  496. struct eeepc_wmi *eeepc = data;
  497. int ret;
  498. /*
  499. * This handler is enabled only if hotplug is enabled.
  500. * In this case, the eeepc_wmi_set_devstate() will
  501. * trigger a wmi notification and we need to wait
  502. * this call to finish before being able to call
  503. * any wmi method
  504. */
  505. mutex_lock(&eeepc->wmi_lock);
  506. ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
  507. mutex_unlock(&eeepc->wmi_lock);
  508. return ret;
  509. }
  510. static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
  511. {
  512. eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
  513. }
  514. static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
  515. .set_block = eeepc_rfkill_wlan_set,
  516. .query = eeepc_rfkill_wlan_query,
  517. };
  518. static const struct rfkill_ops eeepc_rfkill_ops = {
  519. .set_block = eeepc_rfkill_set,
  520. .query = eeepc_rfkill_query,
  521. };
  522. static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
  523. struct rfkill **rfkill,
  524. const char *name,
  525. enum rfkill_type type, int dev_id)
  526. {
  527. int result = eeepc_wmi_get_devstate_simple(dev_id);
  528. if (result < 0)
  529. return result;
  530. if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
  531. *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
  532. &eeepc_rfkill_wlan_ops, eeepc);
  533. else
  534. *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
  535. &eeepc_rfkill_ops, (void *)(long)dev_id);
  536. if (!*rfkill)
  537. return -EINVAL;
  538. rfkill_init_sw_state(*rfkill, !result);
  539. result = rfkill_register(*rfkill);
  540. if (result) {
  541. rfkill_destroy(*rfkill);
  542. *rfkill = NULL;
  543. return result;
  544. }
  545. return 0;
  546. }
  547. static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
  548. {
  549. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  550. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  551. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  552. if (eeepc->wlan_rfkill) {
  553. rfkill_unregister(eeepc->wlan_rfkill);
  554. rfkill_destroy(eeepc->wlan_rfkill);
  555. eeepc->wlan_rfkill = NULL;
  556. }
  557. /*
  558. * Refresh pci hotplug in case the rfkill state was changed after
  559. * eeepc_unregister_rfkill_notifier()
  560. */
  561. eeepc_rfkill_hotplug(eeepc);
  562. if (eeepc->hotplug_slot)
  563. pci_hp_deregister(eeepc->hotplug_slot);
  564. if (eeepc->hotplug_workqueue)
  565. destroy_workqueue(eeepc->hotplug_workqueue);
  566. if (eeepc->bluetooth_rfkill) {
  567. rfkill_unregister(eeepc->bluetooth_rfkill);
  568. rfkill_destroy(eeepc->bluetooth_rfkill);
  569. eeepc->bluetooth_rfkill = NULL;
  570. }
  571. if (eeepc->wwan3g_rfkill) {
  572. rfkill_unregister(eeepc->wwan3g_rfkill);
  573. rfkill_destroy(eeepc->wwan3g_rfkill);
  574. eeepc->wwan3g_rfkill = NULL;
  575. }
  576. }
  577. static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
  578. {
  579. int result = 0;
  580. mutex_init(&eeepc->hotplug_lock);
  581. mutex_init(&eeepc->wmi_lock);
  582. result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
  583. "eeepc-wlan", RFKILL_TYPE_WLAN,
  584. EEEPC_WMI_DEVID_WLAN);
  585. if (result && result != -ENODEV)
  586. goto exit;
  587. result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
  588. "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
  589. EEEPC_WMI_DEVID_BLUETOOTH);
  590. if (result && result != -ENODEV)
  591. goto exit;
  592. result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
  593. "eeepc-wwan3g", RFKILL_TYPE_WWAN,
  594. EEEPC_WMI_DEVID_WWAN3G);
  595. if (result && result != -ENODEV)
  596. goto exit;
  597. result = eeepc_setup_pci_hotplug(eeepc);
  598. /*
  599. * If we get -EBUSY then something else is handling the PCI hotplug -
  600. * don't fail in this case
  601. */
  602. if (result == -EBUSY)
  603. result = 0;
  604. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  605. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  606. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  607. /*
  608. * Refresh pci hotplug in case the rfkill state was changed during
  609. * setup.
  610. */
  611. eeepc_rfkill_hotplug(eeepc);
  612. exit:
  613. if (result && result != -ENODEV)
  614. eeepc_wmi_rfkill_exit(eeepc);
  615. if (result == -ENODEV)
  616. result = 0;
  617. return result;
  618. }
  619. /*
  620. * Backlight
  621. */
  622. static int read_brightness(struct backlight_device *bd)
  623. {
  624. u32 retval;
  625. acpi_status status;
  626. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
  627. if (ACPI_FAILURE(status))
  628. return -1;
  629. else
  630. return retval & 0xFF;
  631. }
  632. static int update_bl_status(struct backlight_device *bd)
  633. {
  634. u32 ctrl_param;
  635. acpi_status status;
  636. ctrl_param = bd->props.brightness;
  637. status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
  638. ctrl_param, NULL);
  639. if (ACPI_FAILURE(status))
  640. return -1;
  641. else
  642. return 0;
  643. }
  644. static const struct backlight_ops eeepc_wmi_bl_ops = {
  645. .get_brightness = read_brightness,
  646. .update_status = update_bl_status,
  647. };
  648. static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
  649. {
  650. struct backlight_device *bd = eeepc->backlight_device;
  651. int old = bd->props.brightness;
  652. int new = old;
  653. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  654. new = code - NOTIFY_BRNUP_MIN + 1;
  655. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  656. new = code - NOTIFY_BRNDOWN_MIN;
  657. bd->props.brightness = new;
  658. backlight_update_status(bd);
  659. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  660. return old;
  661. }
  662. static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
  663. {
  664. struct backlight_device *bd;
  665. struct backlight_properties props;
  666. memset(&props, 0, sizeof(struct backlight_properties));
  667. props.max_brightness = 15;
  668. bd = backlight_device_register(EEEPC_WMI_FILE,
  669. &eeepc->platform_device->dev, eeepc,
  670. &eeepc_wmi_bl_ops, &props);
  671. if (IS_ERR(bd)) {
  672. pr_err("Could not register backlight device\n");
  673. return PTR_ERR(bd);
  674. }
  675. eeepc->backlight_device = bd;
  676. bd->props.brightness = read_brightness(bd);
  677. bd->props.power = FB_BLANK_UNBLANK;
  678. backlight_update_status(bd);
  679. return 0;
  680. }
  681. static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
  682. {
  683. if (eeepc->backlight_device)
  684. backlight_device_unregister(eeepc->backlight_device);
  685. eeepc->backlight_device = NULL;
  686. }
  687. static void eeepc_wmi_notify(u32 value, void *context)
  688. {
  689. struct eeepc_wmi *eeepc = context;
  690. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  691. union acpi_object *obj;
  692. acpi_status status;
  693. int code;
  694. int orig_code;
  695. status = wmi_get_event_data(value, &response);
  696. if (status != AE_OK) {
  697. pr_err("bad event status 0x%x\n", status);
  698. return;
  699. }
  700. obj = (union acpi_object *)response.pointer;
  701. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  702. code = obj->integer.value;
  703. orig_code = code;
  704. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  705. code = NOTIFY_BRNUP_MIN;
  706. else if (code >= NOTIFY_BRNDOWN_MIN &&
  707. code <= NOTIFY_BRNDOWN_MAX)
  708. code = NOTIFY_BRNDOWN_MIN;
  709. if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
  710. if (!acpi_video_backlight_support())
  711. eeepc_wmi_backlight_notify(eeepc, orig_code);
  712. }
  713. if (!sparse_keymap_report_event(eeepc->inputdev,
  714. code, 1, true))
  715. pr_info("Unknown key %x pressed\n", code);
  716. }
  717. kfree(obj);
  718. }
  719. static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
  720. const char *buf, size_t count)
  721. {
  722. int value;
  723. struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
  724. acpi_status status;
  725. if (!count || sscanf(buf, "%i", &value) != 1)
  726. return -EINVAL;
  727. if (value < 0 || value > 2)
  728. return -EINVAL;
  729. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  730. 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
  731. if (ACPI_FAILURE(status))
  732. return -EIO;
  733. else
  734. return count;
  735. }
  736. static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
  737. static struct attribute *platform_attributes[] = {
  738. &dev_attr_cpufv.attr,
  739. NULL
  740. };
  741. static struct attribute_group platform_attribute_group = {
  742. .attrs = platform_attributes
  743. };
  744. static void eeepc_wmi_sysfs_exit(struct platform_device *device)
  745. {
  746. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  747. }
  748. static int eeepc_wmi_sysfs_init(struct platform_device *device)
  749. {
  750. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  751. }
  752. /*
  753. * Platform device
  754. */
  755. static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
  756. {
  757. int err;
  758. eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
  759. if (!eeepc->platform_device)
  760. return -ENOMEM;
  761. platform_set_drvdata(eeepc->platform_device, eeepc);
  762. err = platform_device_add(eeepc->platform_device);
  763. if (err)
  764. goto fail_platform_device;
  765. err = eeepc_wmi_sysfs_init(eeepc->platform_device);
  766. if (err)
  767. goto fail_sysfs;
  768. return 0;
  769. fail_sysfs:
  770. platform_device_del(eeepc->platform_device);
  771. fail_platform_device:
  772. platform_device_put(eeepc->platform_device);
  773. return err;
  774. }
  775. static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
  776. {
  777. eeepc_wmi_sysfs_exit(eeepc->platform_device);
  778. platform_device_unregister(eeepc->platform_device);
  779. }
  780. /*
  781. * debugfs
  782. */
  783. struct eeepc_wmi_debugfs_node {
  784. struct eeepc_wmi *eeepc;
  785. char *name;
  786. int (*show)(struct seq_file *m, void *data);
  787. };
  788. static int show_dsts(struct seq_file *m, void *data)
  789. {
  790. struct eeepc_wmi *eeepc = m->private;
  791. acpi_status status;
  792. u32 retval = -1;
  793. status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
  794. if (ACPI_FAILURE(status))
  795. return -EIO;
  796. seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
  797. return 0;
  798. }
  799. static int show_devs(struct seq_file *m, void *data)
  800. {
  801. struct eeepc_wmi *eeepc = m->private;
  802. acpi_status status;
  803. u32 retval = -1;
  804. status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
  805. eeepc->debug.ctrl_param, &retval);
  806. if (ACPI_FAILURE(status))
  807. return -EIO;
  808. seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
  809. eeepc->debug.ctrl_param, retval);
  810. return 0;
  811. }
  812. static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
  813. { NULL, "devs", show_devs },
  814. { NULL, "dsts", show_dsts },
  815. };
  816. static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
  817. {
  818. struct eeepc_wmi_debugfs_node *node = inode->i_private;
  819. return single_open(file, node->show, node->eeepc);
  820. }
  821. static const struct file_operations eeepc_wmi_debugfs_io_ops = {
  822. .owner = THIS_MODULE,
  823. .open = eeepc_wmi_debugfs_open,
  824. .read = seq_read,
  825. .llseek = seq_lseek,
  826. .release = single_release,
  827. };
  828. static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
  829. {
  830. debugfs_remove_recursive(eeepc->debug.root);
  831. }
  832. static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
  833. {
  834. struct dentry *dent;
  835. int i;
  836. eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
  837. if (!eeepc->debug.root) {
  838. pr_err("failed to create debugfs directory");
  839. goto error_debugfs;
  840. }
  841. dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
  842. eeepc->debug.root, &eeepc->debug.dev_id);
  843. if (!dent)
  844. goto error_debugfs;
  845. dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
  846. eeepc->debug.root, &eeepc->debug.ctrl_param);
  847. if (!dent)
  848. goto error_debugfs;
  849. for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
  850. struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
  851. node->eeepc = eeepc;
  852. dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  853. eeepc->debug.root, node,
  854. &eeepc_wmi_debugfs_io_ops);
  855. if (!dent) {
  856. pr_err("failed to create debug file: %s\n", node->name);
  857. goto error_debugfs;
  858. }
  859. }
  860. return 0;
  861. error_debugfs:
  862. eeepc_wmi_debugfs_exit(eeepc);
  863. return -ENOMEM;
  864. }
  865. /*
  866. * WMI Driver
  867. */
  868. static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
  869. {
  870. const char *model;
  871. model = dmi_get_system_info(DMI_PRODUCT_NAME);
  872. if (!model)
  873. return;
  874. /*
  875. * Whitelist for wlan hotplug
  876. *
  877. * Eeepc 1000H needs the current hotplug code to handle
  878. * Fn+F2 correctly. We may add other Eeepc here later, but
  879. * it seems that most of the laptops supported by eeepc-wmi
  880. * don't need to be on this list
  881. */
  882. if (strcmp(model, "1000H") == 0) {
  883. eeepc->hotplug_wireless = true;
  884. pr_info("wlan hotplug enabled\n");
  885. }
  886. }
  887. static struct platform_device * __init eeepc_wmi_add(void)
  888. {
  889. struct eeepc_wmi *eeepc;
  890. acpi_status status;
  891. int err;
  892. eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
  893. if (!eeepc)
  894. return ERR_PTR(-ENOMEM);
  895. eeepc->hotplug_wireless = hotplug_wireless;
  896. eeepc_dmi_check(eeepc);
  897. /*
  898. * Register the platform device first. It is used as a parent for the
  899. * sub-devices below.
  900. */
  901. err = eeepc_wmi_platform_init(eeepc);
  902. if (err)
  903. goto fail_platform;
  904. err = eeepc_wmi_input_init(eeepc);
  905. if (err)
  906. goto fail_input;
  907. err = eeepc_wmi_led_init(eeepc);
  908. if (err)
  909. goto fail_leds;
  910. err = eeepc_wmi_rfkill_init(eeepc);
  911. if (err)
  912. goto fail_rfkill;
  913. if (!acpi_video_backlight_support()) {
  914. err = eeepc_wmi_backlight_init(eeepc);
  915. if (err)
  916. goto fail_backlight;
  917. } else
  918. pr_info("Backlight controlled by ACPI video driver\n");
  919. status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
  920. eeepc_wmi_notify, eeepc);
  921. if (ACPI_FAILURE(status)) {
  922. pr_err("Unable to register notify handler - %d\n",
  923. status);
  924. err = -ENODEV;
  925. goto fail_wmi_handler;
  926. }
  927. err = eeepc_wmi_debugfs_init(eeepc);
  928. if (err)
  929. goto fail_debugfs;
  930. return eeepc->platform_device;
  931. fail_debugfs:
  932. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  933. fail_wmi_handler:
  934. eeepc_wmi_backlight_exit(eeepc);
  935. fail_backlight:
  936. eeepc_wmi_rfkill_exit(eeepc);
  937. fail_rfkill:
  938. eeepc_wmi_led_exit(eeepc);
  939. fail_leds:
  940. eeepc_wmi_input_exit(eeepc);
  941. fail_input:
  942. eeepc_wmi_platform_exit(eeepc);
  943. fail_platform:
  944. kfree(eeepc);
  945. return ERR_PTR(err);
  946. }
  947. static int eeepc_wmi_remove(struct platform_device *device)
  948. {
  949. struct eeepc_wmi *eeepc;
  950. eeepc = platform_get_drvdata(device);
  951. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  952. eeepc_wmi_backlight_exit(eeepc);
  953. eeepc_wmi_input_exit(eeepc);
  954. eeepc_wmi_led_exit(eeepc);
  955. eeepc_wmi_rfkill_exit(eeepc);
  956. eeepc_wmi_debugfs_exit(eeepc);
  957. eeepc_wmi_platform_exit(eeepc);
  958. kfree(eeepc);
  959. return 0;
  960. }
  961. static struct platform_driver platform_driver = {
  962. .driver = {
  963. .name = EEEPC_WMI_FILE,
  964. .owner = THIS_MODULE,
  965. },
  966. };
  967. static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
  968. void *context, void **retval)
  969. {
  970. pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
  971. *(bool *)context = true;
  972. return AE_CTRL_TERMINATE;
  973. }
  974. static int __init eeepc_wmi_check_atkd(void)
  975. {
  976. acpi_status status;
  977. bool found = false;
  978. status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
  979. &found, NULL);
  980. if (ACPI_FAILURE(status) || !found)
  981. return 0;
  982. return -1;
  983. }
  984. static int __init eeepc_wmi_init(void)
  985. {
  986. int err;
  987. if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
  988. !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
  989. pr_warning("No known WMI GUID found\n");
  990. return -ENODEV;
  991. }
  992. if (eeepc_wmi_check_atkd()) {
  993. pr_warning("WMI device present, but legacy ATKD device is also "
  994. "present and enabled.");
  995. pr_warning("You probably booted with acpi_osi=\"Linux\" or "
  996. "acpi_osi=\"!Windows 2009\"");
  997. pr_warning("Can't load eeepc-wmi, use default acpi_osi "
  998. "(preferred) or eeepc-laptop");
  999. return -ENODEV;
  1000. }
  1001. platform_device = eeepc_wmi_add();
  1002. if (IS_ERR(platform_device)) {
  1003. err = PTR_ERR(platform_device);
  1004. goto fail_eeepc_wmi;
  1005. }
  1006. err = platform_driver_register(&platform_driver);
  1007. if (err) {
  1008. pr_warning("Unable to register platform driver\n");
  1009. goto fail_platform_driver;
  1010. }
  1011. return 0;
  1012. fail_platform_driver:
  1013. eeepc_wmi_remove(platform_device);
  1014. fail_eeepc_wmi:
  1015. return err;
  1016. }
  1017. static void __exit eeepc_wmi_exit(void)
  1018. {
  1019. eeepc_wmi_remove(platform_device);
  1020. platform_driver_unregister(&platform_driver);
  1021. }
  1022. module_init(eeepc_wmi_init);
  1023. module_exit(eeepc_wmi_exit);