eeepc-wmi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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 eeepc_wmi_debug debug;
  121. };
  122. /* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
  123. static struct platform_device *platform_device;
  124. static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
  125. {
  126. int err;
  127. eeepc->inputdev = input_allocate_device();
  128. if (!eeepc->inputdev)
  129. return -ENOMEM;
  130. eeepc->inputdev->name = "Eee PC WMI hotkeys";
  131. eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
  132. eeepc->inputdev->id.bustype = BUS_HOST;
  133. eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
  134. err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
  135. if (err)
  136. goto err_free_dev;
  137. err = input_register_device(eeepc->inputdev);
  138. if (err)
  139. goto err_free_keymap;
  140. return 0;
  141. err_free_keymap:
  142. sparse_keymap_free(eeepc->inputdev);
  143. err_free_dev:
  144. input_free_device(eeepc->inputdev);
  145. return err;
  146. }
  147. static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
  148. {
  149. if (eeepc->inputdev) {
  150. sparse_keymap_free(eeepc->inputdev);
  151. input_unregister_device(eeepc->inputdev);
  152. }
  153. eeepc->inputdev = NULL;
  154. }
  155. static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
  156. {
  157. struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
  158. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  159. union acpi_object *obj;
  160. acpi_status status;
  161. u32 tmp;
  162. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  163. 1, EEEPC_WMI_METHODID_DSTS,
  164. &input, &output);
  165. if (ACPI_FAILURE(status))
  166. return status;
  167. obj = (union acpi_object *)output.pointer;
  168. if (obj && obj->type == ACPI_TYPE_INTEGER)
  169. tmp = (u32)obj->integer.value;
  170. else
  171. tmp = 0;
  172. if (retval)
  173. *retval = tmp;
  174. kfree(obj);
  175. return status;
  176. }
  177. static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
  178. u32 *retval)
  179. {
  180. struct bios_args args = {
  181. .dev_id = dev_id,
  182. .ctrl_param = ctrl_param,
  183. };
  184. struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
  185. acpi_status status;
  186. if (!retval) {
  187. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
  188. EEEPC_WMI_METHODID_DEVS,
  189. &input, NULL);
  190. } else {
  191. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  192. union acpi_object *obj;
  193. u32 tmp;
  194. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
  195. EEEPC_WMI_METHODID_DEVS,
  196. &input, &output);
  197. if (ACPI_FAILURE(status))
  198. return status;
  199. obj = (union acpi_object *)output.pointer;
  200. if (obj && obj->type == ACPI_TYPE_INTEGER)
  201. tmp = (u32)obj->integer.value;
  202. else
  203. tmp = 0;
  204. *retval = tmp;
  205. kfree(obj);
  206. }
  207. return status;
  208. }
  209. /*
  210. * LEDs
  211. */
  212. /*
  213. * These functions actually update the LED's, and are called from a
  214. * workqueue. By doing this as separate work rather than when the LED
  215. * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
  216. * potentially bad time, such as a timer interrupt.
  217. */
  218. static void tpd_led_update(struct work_struct *work)
  219. {
  220. int ctrl_param;
  221. struct eeepc_wmi *eeepc;
  222. eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
  223. ctrl_param = eeepc->tpd_led_wk;
  224. eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
  225. }
  226. static void tpd_led_set(struct led_classdev *led_cdev,
  227. enum led_brightness value)
  228. {
  229. struct eeepc_wmi *eeepc;
  230. eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
  231. eeepc->tpd_led_wk = !!value;
  232. queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
  233. }
  234. static int read_tpd_state(struct eeepc_wmi *eeepc)
  235. {
  236. u32 retval;
  237. acpi_status status;
  238. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_TPDLED, &retval);
  239. if (ACPI_FAILURE(status))
  240. return -1;
  241. else if (!retval || retval == 0x00060000)
  242. /*
  243. * if touchpad led is present, DSTS will set some bits,
  244. * usually 0x00020000.
  245. * 0x00060000 means that the device is not supported
  246. */
  247. return -ENODEV;
  248. else
  249. /* Status is stored in the first bit */
  250. return retval & 0x1;
  251. }
  252. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  253. {
  254. struct eeepc_wmi *eeepc;
  255. eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
  256. return read_tpd_state(eeepc);
  257. }
  258. static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
  259. {
  260. int rv;
  261. if (read_tpd_state(eeepc) < 0)
  262. return 0;
  263. eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
  264. if (!eeepc->led_workqueue)
  265. return -ENOMEM;
  266. INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
  267. eeepc->tpd_led.name = "eeepc::touchpad";
  268. eeepc->tpd_led.brightness_set = tpd_led_set;
  269. eeepc->tpd_led.brightness_get = tpd_led_get;
  270. eeepc->tpd_led.max_brightness = 1;
  271. rv = led_classdev_register(&eeepc->platform_device->dev,
  272. &eeepc->tpd_led);
  273. if (rv) {
  274. destroy_workqueue(eeepc->led_workqueue);
  275. return rv;
  276. }
  277. return 0;
  278. }
  279. static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
  280. {
  281. if (eeepc->tpd_led.dev)
  282. led_classdev_unregister(&eeepc->tpd_led);
  283. if (eeepc->led_workqueue)
  284. destroy_workqueue(eeepc->led_workqueue);
  285. }
  286. /*
  287. * PCI hotplug (for wlan rfkill)
  288. */
  289. static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
  290. {
  291. u32 retval;
  292. acpi_status status;
  293. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_WLAN, &retval);
  294. if (ACPI_FAILURE(status))
  295. return false;
  296. return !(retval & 0x1);
  297. }
  298. static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
  299. {
  300. struct pci_dev *dev;
  301. struct pci_bus *bus;
  302. bool blocked = eeepc_wlan_rfkill_blocked(eeepc);
  303. bool absent;
  304. u32 l;
  305. if (eeepc->wlan_rfkill)
  306. rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
  307. mutex_lock(&eeepc->hotplug_lock);
  308. if (eeepc->hotplug_slot) {
  309. bus = pci_find_bus(0, 1);
  310. if (!bus) {
  311. pr_warning("Unable to find PCI bus 1?\n");
  312. goto out_unlock;
  313. }
  314. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  315. pr_err("Unable to read PCI config space?\n");
  316. goto out_unlock;
  317. }
  318. absent = (l == 0xffffffff);
  319. if (blocked != absent) {
  320. pr_warning("BIOS says wireless lan is %s, "
  321. "but the pci device is %s\n",
  322. blocked ? "blocked" : "unblocked",
  323. absent ? "absent" : "present");
  324. pr_warning("skipped wireless hotplug as probably "
  325. "inappropriate for this model\n");
  326. goto out_unlock;
  327. }
  328. if (!blocked) {
  329. dev = pci_get_slot(bus, 0);
  330. if (dev) {
  331. /* Device already present */
  332. pci_dev_put(dev);
  333. goto out_unlock;
  334. }
  335. dev = pci_scan_single_device(bus, 0);
  336. if (dev) {
  337. pci_bus_assign_resources(bus);
  338. if (pci_bus_add_device(dev))
  339. pr_err("Unable to hotplug wifi\n");
  340. }
  341. } else {
  342. dev = pci_get_slot(bus, 0);
  343. if (dev) {
  344. pci_remove_bus_device(dev);
  345. pci_dev_put(dev);
  346. }
  347. }
  348. }
  349. out_unlock:
  350. mutex_unlock(&eeepc->hotplug_lock);
  351. }
  352. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  353. {
  354. struct eeepc_wmi *eeepc = data;
  355. if (event != ACPI_NOTIFY_BUS_CHECK)
  356. return;
  357. eeepc_rfkill_hotplug(eeepc);
  358. }
  359. static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
  360. char *node)
  361. {
  362. acpi_status status;
  363. acpi_handle handle;
  364. status = acpi_get_handle(NULL, node, &handle);
  365. if (ACPI_SUCCESS(status)) {
  366. status = acpi_install_notify_handler(handle,
  367. ACPI_SYSTEM_NOTIFY,
  368. eeepc_rfkill_notify,
  369. eeepc);
  370. if (ACPI_FAILURE(status))
  371. pr_warning("Failed to register notify on %s\n", node);
  372. } else
  373. return -ENODEV;
  374. return 0;
  375. }
  376. static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
  377. char *node)
  378. {
  379. acpi_status status = AE_OK;
  380. acpi_handle handle;
  381. status = acpi_get_handle(NULL, node, &handle);
  382. if (ACPI_SUCCESS(status)) {
  383. status = acpi_remove_notify_handler(handle,
  384. ACPI_SYSTEM_NOTIFY,
  385. eeepc_rfkill_notify);
  386. if (ACPI_FAILURE(status))
  387. pr_err("Error removing rfkill notify handler %s\n",
  388. node);
  389. }
  390. }
  391. static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
  392. u8 *value)
  393. {
  394. u32 retval;
  395. acpi_status status;
  396. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_WLAN, &retval);
  397. if (ACPI_FAILURE(status))
  398. return -EIO;
  399. if (!retval || retval == 0x00060000)
  400. return -ENODEV;
  401. else
  402. *value = (retval & 0x1);
  403. return 0;
  404. }
  405. static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  406. {
  407. kfree(hotplug_slot->info);
  408. kfree(hotplug_slot);
  409. }
  410. static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
  411. .owner = THIS_MODULE,
  412. .get_adapter_status = eeepc_get_adapter_status,
  413. .get_power_status = eeepc_get_adapter_status,
  414. };
  415. static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
  416. {
  417. int ret = -ENOMEM;
  418. struct pci_bus *bus = pci_find_bus(0, 1);
  419. if (!bus) {
  420. pr_err("Unable to find wifi PCI bus\n");
  421. return -ENODEV;
  422. }
  423. eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  424. if (!eeepc->hotplug_slot)
  425. goto error_slot;
  426. eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  427. GFP_KERNEL);
  428. if (!eeepc->hotplug_slot->info)
  429. goto error_info;
  430. eeepc->hotplug_slot->private = eeepc;
  431. eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
  432. eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
  433. eeepc_get_adapter_status(eeepc->hotplug_slot,
  434. &eeepc->hotplug_slot->info->adapter_status);
  435. ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
  436. if (ret) {
  437. pr_err("Unable to register hotplug slot - %d\n", ret);
  438. goto error_register;
  439. }
  440. return 0;
  441. error_register:
  442. kfree(eeepc->hotplug_slot->info);
  443. error_info:
  444. kfree(eeepc->hotplug_slot);
  445. eeepc->hotplug_slot = NULL;
  446. error_slot:
  447. return ret;
  448. }
  449. /*
  450. * Rfkill devices
  451. */
  452. static int eeepc_rfkill_set(void *data, bool blocked)
  453. {
  454. int dev_id = (unsigned long)data;
  455. u32 ctrl_param = !blocked;
  456. return eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
  457. }
  458. static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
  459. {
  460. int dev_id = (unsigned long)data;
  461. u32 retval;
  462. acpi_status status;
  463. status = eeepc_wmi_get_devstate(dev_id, &retval);
  464. if (ACPI_FAILURE(status))
  465. return ;
  466. rfkill_set_sw_state(rfkill, !(retval & 0x1));
  467. }
  468. static const struct rfkill_ops eeepc_rfkill_ops = {
  469. .set_block = eeepc_rfkill_set,
  470. .query = eeepc_rfkill_query,
  471. };
  472. static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
  473. struct rfkill **rfkill,
  474. const char *name,
  475. enum rfkill_type type, int dev_id)
  476. {
  477. int result;
  478. u32 retval;
  479. acpi_status status;
  480. status = eeepc_wmi_get_devstate(dev_id, &retval);
  481. if (ACPI_FAILURE(status))
  482. return -1;
  483. /* If the device is present, DSTS will always set some bits
  484. * 0x00070000 - 1110000000000000000 - device supported
  485. * 0x00060000 - 1100000000000000000 - not supported
  486. * 0x00020000 - 0100000000000000000 - device supported
  487. * 0x00010000 - 0010000000000000000 - not supported / special mode ?
  488. */
  489. if (!retval || retval == 0x00060000)
  490. return -ENODEV;
  491. *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
  492. &eeepc_rfkill_ops, (void *)(long)dev_id);
  493. if (!*rfkill)
  494. return -EINVAL;
  495. rfkill_init_sw_state(*rfkill, !(retval & 0x1));
  496. result = rfkill_register(*rfkill);
  497. if (result) {
  498. rfkill_destroy(*rfkill);
  499. *rfkill = NULL;
  500. return result;
  501. }
  502. return 0;
  503. }
  504. static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
  505. {
  506. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  507. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  508. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  509. if (eeepc->wlan_rfkill) {
  510. rfkill_unregister(eeepc->wlan_rfkill);
  511. rfkill_destroy(eeepc->wlan_rfkill);
  512. eeepc->wlan_rfkill = NULL;
  513. }
  514. /*
  515. * Refresh pci hotplug in case the rfkill state was changed after
  516. * eeepc_unregister_rfkill_notifier()
  517. */
  518. eeepc_rfkill_hotplug(eeepc);
  519. if (eeepc->hotplug_slot)
  520. pci_hp_deregister(eeepc->hotplug_slot);
  521. if (eeepc->bluetooth_rfkill) {
  522. rfkill_unregister(eeepc->bluetooth_rfkill);
  523. rfkill_destroy(eeepc->bluetooth_rfkill);
  524. eeepc->bluetooth_rfkill = NULL;
  525. }
  526. if (eeepc->wwan3g_rfkill) {
  527. rfkill_unregister(eeepc->wwan3g_rfkill);
  528. rfkill_destroy(eeepc->wwan3g_rfkill);
  529. eeepc->wwan3g_rfkill = NULL;
  530. }
  531. }
  532. static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
  533. {
  534. int result = 0;
  535. mutex_init(&eeepc->hotplug_lock);
  536. result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
  537. "eeepc-wlan", RFKILL_TYPE_WLAN,
  538. EEEPC_WMI_DEVID_WLAN);
  539. if (result && result != -ENODEV)
  540. goto exit;
  541. result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
  542. "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
  543. EEEPC_WMI_DEVID_BLUETOOTH);
  544. if (result && result != -ENODEV)
  545. goto exit;
  546. result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
  547. "eeepc-wwan3g", RFKILL_TYPE_WWAN,
  548. EEEPC_WMI_DEVID_WWAN3G);
  549. if (result && result != -ENODEV)
  550. goto exit;
  551. result = eeepc_setup_pci_hotplug(eeepc);
  552. /*
  553. * If we get -EBUSY then something else is handling the PCI hotplug -
  554. * don't fail in this case
  555. */
  556. if (result == -EBUSY)
  557. result = 0;
  558. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  559. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  560. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  561. /*
  562. * Refresh pci hotplug in case the rfkill state was changed during
  563. * setup.
  564. */
  565. eeepc_rfkill_hotplug(eeepc);
  566. exit:
  567. if (result && result != -ENODEV)
  568. eeepc_wmi_rfkill_exit(eeepc);
  569. if (result == -ENODEV)
  570. result = 0;
  571. return result;
  572. }
  573. /*
  574. * Backlight
  575. */
  576. static int read_brightness(struct backlight_device *bd)
  577. {
  578. u32 retval;
  579. acpi_status status;
  580. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
  581. if (ACPI_FAILURE(status))
  582. return -1;
  583. else
  584. return retval & 0xFF;
  585. }
  586. static int update_bl_status(struct backlight_device *bd)
  587. {
  588. u32 ctrl_param;
  589. acpi_status status;
  590. ctrl_param = bd->props.brightness;
  591. status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
  592. ctrl_param, NULL);
  593. if (ACPI_FAILURE(status))
  594. return -1;
  595. else
  596. return 0;
  597. }
  598. static const struct backlight_ops eeepc_wmi_bl_ops = {
  599. .get_brightness = read_brightness,
  600. .update_status = update_bl_status,
  601. };
  602. static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
  603. {
  604. struct backlight_device *bd = eeepc->backlight_device;
  605. int old = bd->props.brightness;
  606. int new = old;
  607. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  608. new = code - NOTIFY_BRNUP_MIN + 1;
  609. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  610. new = code - NOTIFY_BRNDOWN_MIN;
  611. bd->props.brightness = new;
  612. backlight_update_status(bd);
  613. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  614. return old;
  615. }
  616. static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
  617. {
  618. struct backlight_device *bd;
  619. struct backlight_properties props;
  620. memset(&props, 0, sizeof(struct backlight_properties));
  621. props.max_brightness = 15;
  622. bd = backlight_device_register(EEEPC_WMI_FILE,
  623. &eeepc->platform_device->dev, eeepc,
  624. &eeepc_wmi_bl_ops, &props);
  625. if (IS_ERR(bd)) {
  626. pr_err("Could not register backlight device\n");
  627. return PTR_ERR(bd);
  628. }
  629. eeepc->backlight_device = bd;
  630. bd->props.brightness = read_brightness(bd);
  631. bd->props.power = FB_BLANK_UNBLANK;
  632. backlight_update_status(bd);
  633. return 0;
  634. }
  635. static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
  636. {
  637. if (eeepc->backlight_device)
  638. backlight_device_unregister(eeepc->backlight_device);
  639. eeepc->backlight_device = NULL;
  640. }
  641. static void eeepc_wmi_notify(u32 value, void *context)
  642. {
  643. struct eeepc_wmi *eeepc = context;
  644. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  645. union acpi_object *obj;
  646. acpi_status status;
  647. int code;
  648. int orig_code;
  649. status = wmi_get_event_data(value, &response);
  650. if (status != AE_OK) {
  651. pr_err("bad event status 0x%x\n", status);
  652. return;
  653. }
  654. obj = (union acpi_object *)response.pointer;
  655. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  656. code = obj->integer.value;
  657. orig_code = code;
  658. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  659. code = NOTIFY_BRNUP_MIN;
  660. else if (code >= NOTIFY_BRNDOWN_MIN &&
  661. code <= NOTIFY_BRNDOWN_MAX)
  662. code = NOTIFY_BRNDOWN_MIN;
  663. if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
  664. if (!acpi_video_backlight_support())
  665. eeepc_wmi_backlight_notify(eeepc, orig_code);
  666. }
  667. if (!sparse_keymap_report_event(eeepc->inputdev,
  668. code, 1, true))
  669. pr_info("Unknown key %x pressed\n", code);
  670. }
  671. kfree(obj);
  672. }
  673. static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
  674. const char *buf, size_t count)
  675. {
  676. int value;
  677. struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
  678. acpi_status status;
  679. if (!count || sscanf(buf, "%i", &value) != 1)
  680. return -EINVAL;
  681. if (value < 0 || value > 2)
  682. return -EINVAL;
  683. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  684. 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
  685. if (ACPI_FAILURE(status))
  686. return -EIO;
  687. else
  688. return count;
  689. }
  690. static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
  691. static struct attribute *platform_attributes[] = {
  692. &dev_attr_cpufv.attr,
  693. NULL
  694. };
  695. static struct attribute_group platform_attribute_group = {
  696. .attrs = platform_attributes
  697. };
  698. static void eeepc_wmi_sysfs_exit(struct platform_device *device)
  699. {
  700. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  701. }
  702. static int eeepc_wmi_sysfs_init(struct platform_device *device)
  703. {
  704. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  705. }
  706. /*
  707. * Platform device
  708. */
  709. static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
  710. {
  711. int err;
  712. eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
  713. if (!eeepc->platform_device)
  714. return -ENOMEM;
  715. platform_set_drvdata(eeepc->platform_device, eeepc);
  716. err = platform_device_add(eeepc->platform_device);
  717. if (err)
  718. goto fail_platform_device;
  719. err = eeepc_wmi_sysfs_init(eeepc->platform_device);
  720. if (err)
  721. goto fail_sysfs;
  722. return 0;
  723. fail_sysfs:
  724. platform_device_del(eeepc->platform_device);
  725. fail_platform_device:
  726. platform_device_put(eeepc->platform_device);
  727. return err;
  728. }
  729. static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
  730. {
  731. eeepc_wmi_sysfs_exit(eeepc->platform_device);
  732. platform_device_unregister(eeepc->platform_device);
  733. }
  734. /*
  735. * debugfs
  736. */
  737. struct eeepc_wmi_debugfs_node {
  738. struct eeepc_wmi *eeepc;
  739. char *name;
  740. int (*show)(struct seq_file *m, void *data);
  741. };
  742. static int show_dsts(struct seq_file *m, void *data)
  743. {
  744. struct eeepc_wmi *eeepc = m->private;
  745. acpi_status status;
  746. u32 retval = -1;
  747. status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
  748. if (ACPI_FAILURE(status))
  749. return -EIO;
  750. seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
  751. return 0;
  752. }
  753. static int show_devs(struct seq_file *m, void *data)
  754. {
  755. struct eeepc_wmi *eeepc = m->private;
  756. acpi_status status;
  757. u32 retval = -1;
  758. status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
  759. eeepc->debug.ctrl_param, &retval);
  760. if (ACPI_FAILURE(status))
  761. return -EIO;
  762. seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
  763. eeepc->debug.ctrl_param, retval);
  764. return 0;
  765. }
  766. static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
  767. { NULL, "devs", show_devs },
  768. { NULL, "dsts", show_dsts },
  769. };
  770. static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
  771. {
  772. struct eeepc_wmi_debugfs_node *node = inode->i_private;
  773. return single_open(file, node->show, node->eeepc);
  774. }
  775. static const struct file_operations eeepc_wmi_debugfs_io_ops = {
  776. .owner = THIS_MODULE,
  777. .open = eeepc_wmi_debugfs_open,
  778. .read = seq_read,
  779. .llseek = seq_lseek,
  780. .release = single_release,
  781. };
  782. static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
  783. {
  784. debugfs_remove_recursive(eeepc->debug.root);
  785. }
  786. static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
  787. {
  788. struct dentry *dent;
  789. int i;
  790. eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
  791. if (!eeepc->debug.root) {
  792. pr_err("failed to create debugfs directory");
  793. goto error_debugfs;
  794. }
  795. dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
  796. eeepc->debug.root, &eeepc->debug.dev_id);
  797. if (!dent)
  798. goto error_debugfs;
  799. dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
  800. eeepc->debug.root, &eeepc->debug.ctrl_param);
  801. if (!dent)
  802. goto error_debugfs;
  803. for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
  804. struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
  805. node->eeepc = eeepc;
  806. dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  807. eeepc->debug.root, node,
  808. &eeepc_wmi_debugfs_io_ops);
  809. if (!dent) {
  810. pr_err("failed to create debug file: %s\n", node->name);
  811. goto error_debugfs;
  812. }
  813. }
  814. return 0;
  815. error_debugfs:
  816. eeepc_wmi_debugfs_exit(eeepc);
  817. return -ENOMEM;
  818. }
  819. /*
  820. * WMI Driver
  821. */
  822. static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
  823. {
  824. const char *model;
  825. model = dmi_get_system_info(DMI_PRODUCT_NAME);
  826. if (!model)
  827. return;
  828. /*
  829. * Whitelist for wlan hotplug
  830. *
  831. * Eeepc 1000H needs the current hotplug code to handle
  832. * Fn+F2 correctly. We may add other Eeepc here later, but
  833. * it seems that most of the laptops supported by eeepc-wmi
  834. * don't need to be on this list
  835. */
  836. if (strcmp(model, "1000H") == 0) {
  837. eeepc->hotplug_wireless = true;
  838. pr_info("wlan hotplug enabled\n");
  839. }
  840. }
  841. static struct platform_device * __init eeepc_wmi_add(void)
  842. {
  843. struct eeepc_wmi *eeepc;
  844. acpi_status status;
  845. int err;
  846. eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
  847. if (!eeepc)
  848. return ERR_PTR(-ENOMEM);
  849. eeepc->hotplug_wireless = hotplug_wireless;
  850. eeepc_dmi_check(eeepc);
  851. /*
  852. * Register the platform device first. It is used as a parent for the
  853. * sub-devices below.
  854. */
  855. err = eeepc_wmi_platform_init(eeepc);
  856. if (err)
  857. goto fail_platform;
  858. err = eeepc_wmi_input_init(eeepc);
  859. if (err)
  860. goto fail_input;
  861. err = eeepc_wmi_led_init(eeepc);
  862. if (err)
  863. goto fail_leds;
  864. err = eeepc_wmi_rfkill_init(eeepc);
  865. if (err)
  866. goto fail_rfkill;
  867. if (!acpi_video_backlight_support()) {
  868. err = eeepc_wmi_backlight_init(eeepc);
  869. if (err)
  870. goto fail_backlight;
  871. } else
  872. pr_info("Backlight controlled by ACPI video driver\n");
  873. status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
  874. eeepc_wmi_notify, eeepc);
  875. if (ACPI_FAILURE(status)) {
  876. pr_err("Unable to register notify handler - %d\n",
  877. status);
  878. err = -ENODEV;
  879. goto fail_wmi_handler;
  880. }
  881. err = eeepc_wmi_debugfs_init(eeepc);
  882. if (err)
  883. goto fail_debugfs;
  884. return eeepc->platform_device;
  885. fail_debugfs:
  886. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  887. fail_wmi_handler:
  888. eeepc_wmi_backlight_exit(eeepc);
  889. fail_backlight:
  890. eeepc_wmi_rfkill_exit(eeepc);
  891. fail_rfkill:
  892. eeepc_wmi_led_exit(eeepc);
  893. fail_leds:
  894. eeepc_wmi_input_exit(eeepc);
  895. fail_input:
  896. eeepc_wmi_platform_exit(eeepc);
  897. fail_platform:
  898. kfree(eeepc);
  899. return ERR_PTR(err);
  900. }
  901. static int eeepc_wmi_remove(struct platform_device *device)
  902. {
  903. struct eeepc_wmi *eeepc;
  904. eeepc = platform_get_drvdata(device);
  905. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  906. eeepc_wmi_backlight_exit(eeepc);
  907. eeepc_wmi_input_exit(eeepc);
  908. eeepc_wmi_led_exit(eeepc);
  909. eeepc_wmi_rfkill_exit(eeepc);
  910. eeepc_wmi_debugfs_exit(eeepc);
  911. eeepc_wmi_platform_exit(eeepc);
  912. kfree(eeepc);
  913. return 0;
  914. }
  915. static struct platform_driver platform_driver = {
  916. .driver = {
  917. .name = EEEPC_WMI_FILE,
  918. .owner = THIS_MODULE,
  919. },
  920. };
  921. static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
  922. void *context, void **retval)
  923. {
  924. pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
  925. *(bool *)context = true;
  926. return AE_CTRL_TERMINATE;
  927. }
  928. static int __init eeepc_wmi_check_atkd(void)
  929. {
  930. acpi_status status;
  931. bool found = false;
  932. status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
  933. &found, NULL);
  934. if (ACPI_FAILURE(status) || !found)
  935. return 0;
  936. return -1;
  937. }
  938. static int __init eeepc_wmi_init(void)
  939. {
  940. int err;
  941. if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
  942. !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
  943. pr_warning("No known WMI GUID found\n");
  944. return -ENODEV;
  945. }
  946. if (eeepc_wmi_check_atkd()) {
  947. pr_warning("WMI device present, but legacy ATKD device is also "
  948. "present and enabled.");
  949. pr_warning("You probably booted with acpi_osi=\"Linux\" or "
  950. "acpi_osi=\"!Windows 2009\"");
  951. pr_warning("Can't load eeepc-wmi, use default acpi_osi "
  952. "(preferred) or eeepc-laptop");
  953. return -ENODEV;
  954. }
  955. platform_device = eeepc_wmi_add();
  956. if (IS_ERR(platform_device)) {
  957. err = PTR_ERR(platform_device);
  958. goto fail_eeepc_wmi;
  959. }
  960. err = platform_driver_register(&platform_driver);
  961. if (err) {
  962. pr_warning("Unable to register platform driver\n");
  963. goto fail_platform_driver;
  964. }
  965. return 0;
  966. fail_platform_driver:
  967. eeepc_wmi_remove(platform_device);
  968. fail_eeepc_wmi:
  969. return err;
  970. }
  971. static void __exit eeepc_wmi_exit(void)
  972. {
  973. eeepc_wmi_remove(platform_device);
  974. platform_driver_unregister(&platform_driver);
  975. }
  976. module_init(eeepc_wmi_init);
  977. module_exit(eeepc_wmi_exit);