asus-wmi.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /*
  2. * Asus PC WMI hotkey driver
  3. *
  4. * Copyright(C) 2010 Intel Corporation.
  5. * Copyright(C) 2010-2011 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/hwmon.h>
  41. #include <linux/hwmon-sysfs.h>
  42. #include <linux/debugfs.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/thermal.h>
  46. #include <linux/acpi.h>
  47. #include <linux/dmi.h>
  48. #include <acpi/video.h>
  49. #include "asus-wmi.h"
  50. MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
  51. "Yong Wang <yong.y.wang@intel.com>");
  52. MODULE_DESCRIPTION("Asus Generic WMI Driver");
  53. MODULE_LICENSE("GPL");
  54. #define to_platform_driver(drv) \
  55. (container_of((drv), struct platform_driver, driver))
  56. #define to_asus_wmi_driver(pdrv) \
  57. (container_of((pdrv), struct asus_wmi_driver, platform_driver))
  58. #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
  59. #define NOTIFY_BRNUP_MIN 0x11
  60. #define NOTIFY_BRNUP_MAX 0x1f
  61. #define NOTIFY_BRNDOWN_MIN 0x20
  62. #define NOTIFY_BRNDOWN_MAX 0x2e
  63. #define NOTIFY_KBD_BRTUP 0xc4
  64. #define NOTIFY_KBD_BRTDWN 0xc5
  65. /* WMI Methods */
  66. #define ASUS_WMI_METHODID_SPEC 0x43455053 /* BIOS SPECification */
  67. #define ASUS_WMI_METHODID_SFBD 0x44424653 /* Set First Boot Device */
  68. #define ASUS_WMI_METHODID_GLCD 0x44434C47 /* Get LCD status */
  69. #define ASUS_WMI_METHODID_GPID 0x44495047 /* Get Panel ID?? (Resol) */
  70. #define ASUS_WMI_METHODID_QMOD 0x444F4D51 /* Quiet MODe */
  71. #define ASUS_WMI_METHODID_SPLV 0x4C425053 /* Set Panel Light Value */
  72. #define ASUS_WMI_METHODID_SFUN 0x4E554653 /* FUNCtionalities */
  73. #define ASUS_WMI_METHODID_SDSP 0x50534453 /* Set DiSPlay output */
  74. #define ASUS_WMI_METHODID_GDSP 0x50534447 /* Get DiSPlay output */
  75. #define ASUS_WMI_METHODID_DEVP 0x50564544 /* DEVice Policy */
  76. #define ASUS_WMI_METHODID_OSVR 0x5256534F /* OS VeRsion */
  77. #define ASUS_WMI_METHODID_DSTS 0x53544344 /* Device STatuS */
  78. #define ASUS_WMI_METHODID_DSTS2 0x53545344 /* Device STatuS #2*/
  79. #define ASUS_WMI_METHODID_BSTS 0x53545342 /* Bios STatuS ? */
  80. #define ASUS_WMI_METHODID_DEVS 0x53564544 /* DEVice Set */
  81. #define ASUS_WMI_METHODID_CFVS 0x53564643 /* CPU Frequency Volt Set */
  82. #define ASUS_WMI_METHODID_KBFT 0x5446424B /* KeyBoard FilTer */
  83. #define ASUS_WMI_METHODID_INIT 0x54494E49 /* INITialize */
  84. #define ASUS_WMI_METHODID_HKEY 0x59454B48 /* Hot KEY ?? */
  85. #define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
  86. /* Wireless */
  87. #define ASUS_WMI_DEVID_HW_SWITCH 0x00010001
  88. #define ASUS_WMI_DEVID_WIRELESS_LED 0x00010002
  89. #define ASUS_WMI_DEVID_CWAP 0x00010003
  90. #define ASUS_WMI_DEVID_WLAN 0x00010011
  91. #define ASUS_WMI_DEVID_WLAN_LED 0x00010012
  92. #define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
  93. #define ASUS_WMI_DEVID_GPS 0x00010015
  94. #define ASUS_WMI_DEVID_WIMAX 0x00010017
  95. #define ASUS_WMI_DEVID_WWAN3G 0x00010019
  96. #define ASUS_WMI_DEVID_UWB 0x00010021
  97. /* Leds */
  98. /* 0x000200XX and 0x000400XX */
  99. #define ASUS_WMI_DEVID_LED1 0x00020011
  100. #define ASUS_WMI_DEVID_LED2 0x00020012
  101. #define ASUS_WMI_DEVID_LED3 0x00020013
  102. #define ASUS_WMI_DEVID_LED4 0x00020014
  103. #define ASUS_WMI_DEVID_LED5 0x00020015
  104. #define ASUS_WMI_DEVID_LED6 0x00020016
  105. /* Backlight and Brightness */
  106. #define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
  107. #define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
  108. #define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
  109. #define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */
  110. /* Misc */
  111. #define ASUS_WMI_DEVID_CAMERA 0x00060013
  112. /* Storage */
  113. #define ASUS_WMI_DEVID_CARDREADER 0x00080013
  114. /* Input */
  115. #define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
  116. #define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
  117. /* Fan, Thermal */
  118. #define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011
  119. #define ASUS_WMI_DEVID_FAN_CTRL 0x00110012
  120. /* Power */
  121. #define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
  122. /* Deep S3 / Resume on LID open */
  123. #define ASUS_WMI_DEVID_LID_RESUME 0x00120031
  124. /* DSTS masks */
  125. #define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
  126. #define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
  127. #define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
  128. #define ASUS_WMI_DSTS_USER_BIT 0x00020000
  129. #define ASUS_WMI_DSTS_BIOS_BIT 0x00040000
  130. #define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
  131. #define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
  132. struct bios_args {
  133. u32 arg0;
  134. u32 arg1;
  135. } __packed;
  136. /*
  137. * <platform>/ - debugfs root directory
  138. * dev_id - current dev_id
  139. * ctrl_param - current ctrl_param
  140. * method_id - current method_id
  141. * devs - call DEVS(dev_id, ctrl_param) and print result
  142. * dsts - call DSTS(dev_id) and print result
  143. * call - call method_id(dev_id, ctrl_param) and print result
  144. */
  145. struct asus_wmi_debug {
  146. struct dentry *root;
  147. u32 method_id;
  148. u32 dev_id;
  149. u32 ctrl_param;
  150. };
  151. struct asus_rfkill {
  152. struct asus_wmi *asus;
  153. struct rfkill *rfkill;
  154. u32 dev_id;
  155. };
  156. struct asus_wmi {
  157. int dsts_id;
  158. int spec;
  159. int sfun;
  160. struct input_dev *inputdev;
  161. struct backlight_device *backlight_device;
  162. struct platform_device *platform_device;
  163. struct led_classdev wlan_led;
  164. int wlan_led_wk;
  165. struct led_classdev tpd_led;
  166. int tpd_led_wk;
  167. struct led_classdev kbd_led;
  168. int kbd_led_wk;
  169. struct workqueue_struct *led_workqueue;
  170. struct work_struct tpd_led_work;
  171. struct work_struct kbd_led_work;
  172. struct work_struct wlan_led_work;
  173. struct asus_rfkill wlan;
  174. struct asus_rfkill bluetooth;
  175. struct asus_rfkill wimax;
  176. struct asus_rfkill wwan3g;
  177. struct asus_rfkill gps;
  178. struct asus_rfkill uwb;
  179. struct hotplug_slot *hotplug_slot;
  180. struct mutex hotplug_lock;
  181. struct mutex wmi_lock;
  182. struct workqueue_struct *hotplug_workqueue;
  183. struct work_struct hotplug_work;
  184. struct asus_wmi_debug debug;
  185. struct asus_wmi_driver *driver;
  186. };
  187. static int asus_wmi_input_init(struct asus_wmi *asus)
  188. {
  189. int err;
  190. asus->inputdev = input_allocate_device();
  191. if (!asus->inputdev)
  192. return -ENOMEM;
  193. asus->inputdev->name = asus->driver->input_name;
  194. asus->inputdev->phys = asus->driver->input_phys;
  195. asus->inputdev->id.bustype = BUS_HOST;
  196. asus->inputdev->dev.parent = &asus->platform_device->dev;
  197. set_bit(EV_REP, asus->inputdev->evbit);
  198. err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
  199. if (err)
  200. goto err_free_dev;
  201. err = input_register_device(asus->inputdev);
  202. if (err)
  203. goto err_free_keymap;
  204. return 0;
  205. err_free_keymap:
  206. sparse_keymap_free(asus->inputdev);
  207. err_free_dev:
  208. input_free_device(asus->inputdev);
  209. return err;
  210. }
  211. static void asus_wmi_input_exit(struct asus_wmi *asus)
  212. {
  213. if (asus->inputdev) {
  214. sparse_keymap_free(asus->inputdev);
  215. input_unregister_device(asus->inputdev);
  216. }
  217. asus->inputdev = NULL;
  218. }
  219. static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
  220. u32 *retval)
  221. {
  222. struct bios_args args = {
  223. .arg0 = arg0,
  224. .arg1 = arg1,
  225. };
  226. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  227. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  228. acpi_status status;
  229. union acpi_object *obj;
  230. u32 tmp = 0;
  231. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1, method_id,
  232. &input, &output);
  233. if (ACPI_FAILURE(status))
  234. goto exit;
  235. obj = (union acpi_object *)output.pointer;
  236. if (obj && obj->type == ACPI_TYPE_INTEGER)
  237. tmp = (u32) obj->integer.value;
  238. if (retval)
  239. *retval = tmp;
  240. kfree(obj);
  241. exit:
  242. if (ACPI_FAILURE(status))
  243. return -EIO;
  244. if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
  245. return -ENODEV;
  246. return 0;
  247. }
  248. static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
  249. {
  250. return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
  251. }
  252. static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
  253. u32 *retval)
  254. {
  255. return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
  256. ctrl_param, retval);
  257. }
  258. /* Helper for special devices with magic return codes */
  259. static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
  260. u32 dev_id, u32 mask)
  261. {
  262. u32 retval = 0;
  263. int err;
  264. err = asus_wmi_get_devstate(asus, dev_id, &retval);
  265. if (err < 0)
  266. return err;
  267. if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
  268. return -ENODEV;
  269. if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
  270. if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
  271. return -ENODEV;
  272. }
  273. return retval & mask;
  274. }
  275. static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
  276. {
  277. return asus_wmi_get_devstate_bits(asus, dev_id,
  278. ASUS_WMI_DSTS_STATUS_BIT);
  279. }
  280. /*
  281. * LEDs
  282. */
  283. /*
  284. * These functions actually update the LED's, and are called from a
  285. * workqueue. By doing this as separate work rather than when the LED
  286. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  287. * potentially bad time, such as a timer interrupt.
  288. */
  289. static void tpd_led_update(struct work_struct *work)
  290. {
  291. int ctrl_param;
  292. struct asus_wmi *asus;
  293. asus = container_of(work, struct asus_wmi, tpd_led_work);
  294. ctrl_param = asus->tpd_led_wk;
  295. asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
  296. }
  297. static void tpd_led_set(struct led_classdev *led_cdev,
  298. enum led_brightness value)
  299. {
  300. struct asus_wmi *asus;
  301. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  302. asus->tpd_led_wk = !!value;
  303. queue_work(asus->led_workqueue, &asus->tpd_led_work);
  304. }
  305. static int read_tpd_led_state(struct asus_wmi *asus)
  306. {
  307. return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
  308. }
  309. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  310. {
  311. struct asus_wmi *asus;
  312. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  313. return read_tpd_led_state(asus);
  314. }
  315. static void kbd_led_update(struct work_struct *work)
  316. {
  317. int ctrl_param = 0;
  318. struct asus_wmi *asus;
  319. asus = container_of(work, struct asus_wmi, kbd_led_work);
  320. /*
  321. * bits 0-2: level
  322. * bit 7: light on/off
  323. */
  324. if (asus->kbd_led_wk > 0)
  325. ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
  326. asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
  327. }
  328. static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
  329. {
  330. int retval;
  331. /*
  332. * bits 0-2: level
  333. * bit 7: light on/off
  334. * bit 8-10: environment (0: dark, 1: normal, 2: light)
  335. * bit 17: status unknown
  336. */
  337. retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
  338. 0xFFFF);
  339. /* Unknown status is considered as off */
  340. if (retval == 0x8000)
  341. retval = 0;
  342. if (retval >= 0) {
  343. if (level)
  344. *level = retval & 0x7F;
  345. if (env)
  346. *env = (retval >> 8) & 0x7F;
  347. retval = 0;
  348. }
  349. return retval;
  350. }
  351. static void kbd_led_set(struct led_classdev *led_cdev,
  352. enum led_brightness value)
  353. {
  354. struct asus_wmi *asus;
  355. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  356. if (value > asus->kbd_led.max_brightness)
  357. value = asus->kbd_led.max_brightness;
  358. else if (value < 0)
  359. value = 0;
  360. asus->kbd_led_wk = value;
  361. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  362. }
  363. static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
  364. {
  365. struct asus_wmi *asus;
  366. int retval, value;
  367. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  368. retval = kbd_led_read(asus, &value, NULL);
  369. if (retval < 0)
  370. return retval;
  371. return value;
  372. }
  373. static int wlan_led_unknown_state(struct asus_wmi *asus)
  374. {
  375. u32 result;
  376. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  377. return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
  378. }
  379. static int wlan_led_presence(struct asus_wmi *asus)
  380. {
  381. u32 result;
  382. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  383. return result & ASUS_WMI_DSTS_PRESENCE_BIT;
  384. }
  385. static void wlan_led_update(struct work_struct *work)
  386. {
  387. int ctrl_param;
  388. struct asus_wmi *asus;
  389. asus = container_of(work, struct asus_wmi, wlan_led_work);
  390. ctrl_param = asus->wlan_led_wk;
  391. asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
  392. }
  393. static void wlan_led_set(struct led_classdev *led_cdev,
  394. enum led_brightness value)
  395. {
  396. struct asus_wmi *asus;
  397. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  398. asus->wlan_led_wk = !!value;
  399. queue_work(asus->led_workqueue, &asus->wlan_led_work);
  400. }
  401. static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
  402. {
  403. struct asus_wmi *asus;
  404. u32 result;
  405. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  406. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  407. return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  408. }
  409. static void asus_wmi_led_exit(struct asus_wmi *asus)
  410. {
  411. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  412. led_classdev_unregister(&asus->kbd_led);
  413. if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
  414. led_classdev_unregister(&asus->tpd_led);
  415. if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
  416. led_classdev_unregister(&asus->wlan_led);
  417. if (asus->led_workqueue)
  418. destroy_workqueue(asus->led_workqueue);
  419. }
  420. static int asus_wmi_led_init(struct asus_wmi *asus)
  421. {
  422. int rv = 0;
  423. asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
  424. if (!asus->led_workqueue)
  425. return -ENOMEM;
  426. if (read_tpd_led_state(asus) >= 0) {
  427. INIT_WORK(&asus->tpd_led_work, tpd_led_update);
  428. asus->tpd_led.name = "asus::touchpad";
  429. asus->tpd_led.brightness_set = tpd_led_set;
  430. asus->tpd_led.brightness_get = tpd_led_get;
  431. asus->tpd_led.max_brightness = 1;
  432. rv = led_classdev_register(&asus->platform_device->dev,
  433. &asus->tpd_led);
  434. if (rv)
  435. goto error;
  436. }
  437. if (kbd_led_read(asus, NULL, NULL) >= 0) {
  438. INIT_WORK(&asus->kbd_led_work, kbd_led_update);
  439. asus->kbd_led.name = "asus::kbd_backlight";
  440. asus->kbd_led.brightness_set = kbd_led_set;
  441. asus->kbd_led.brightness_get = kbd_led_get;
  442. asus->kbd_led.max_brightness = 3;
  443. rv = led_classdev_register(&asus->platform_device->dev,
  444. &asus->kbd_led);
  445. if (rv)
  446. goto error;
  447. }
  448. if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
  449. INIT_WORK(&asus->wlan_led_work, wlan_led_update);
  450. asus->wlan_led.name = "asus::wlan";
  451. asus->wlan_led.brightness_set = wlan_led_set;
  452. if (!wlan_led_unknown_state(asus))
  453. asus->wlan_led.brightness_get = wlan_led_get;
  454. asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
  455. asus->wlan_led.max_brightness = 1;
  456. asus->wlan_led.default_trigger = "asus-wlan";
  457. rv = led_classdev_register(&asus->platform_device->dev,
  458. &asus->wlan_led);
  459. }
  460. error:
  461. if (rv)
  462. asus_wmi_led_exit(asus);
  463. return rv;
  464. }
  465. /*
  466. * PCI hotplug (for wlan rfkill)
  467. */
  468. static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
  469. {
  470. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  471. if (result < 0)
  472. return false;
  473. return !result;
  474. }
  475. static void asus_rfkill_hotplug(struct asus_wmi *asus)
  476. {
  477. struct pci_dev *dev;
  478. struct pci_bus *bus;
  479. bool blocked;
  480. bool absent;
  481. u32 l;
  482. mutex_lock(&asus->wmi_lock);
  483. blocked = asus_wlan_rfkill_blocked(asus);
  484. mutex_unlock(&asus->wmi_lock);
  485. mutex_lock(&asus->hotplug_lock);
  486. pci_lock_rescan_remove();
  487. if (asus->wlan.rfkill)
  488. rfkill_set_sw_state(asus->wlan.rfkill, blocked);
  489. if (asus->hotplug_slot) {
  490. bus = pci_find_bus(0, 1);
  491. if (!bus) {
  492. pr_warn("Unable to find PCI bus 1?\n");
  493. goto out_unlock;
  494. }
  495. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  496. pr_err("Unable to read PCI config space?\n");
  497. goto out_unlock;
  498. }
  499. absent = (l == 0xffffffff);
  500. if (blocked != absent) {
  501. pr_warn("BIOS says wireless lan is %s, "
  502. "but the pci device is %s\n",
  503. blocked ? "blocked" : "unblocked",
  504. absent ? "absent" : "present");
  505. pr_warn("skipped wireless hotplug as probably "
  506. "inappropriate for this model\n");
  507. goto out_unlock;
  508. }
  509. if (!blocked) {
  510. dev = pci_get_slot(bus, 0);
  511. if (dev) {
  512. /* Device already present */
  513. pci_dev_put(dev);
  514. goto out_unlock;
  515. }
  516. dev = pci_scan_single_device(bus, 0);
  517. if (dev) {
  518. pci_bus_assign_resources(bus);
  519. pci_bus_add_device(dev);
  520. }
  521. } else {
  522. dev = pci_get_slot(bus, 0);
  523. if (dev) {
  524. pci_stop_and_remove_bus_device(dev);
  525. pci_dev_put(dev);
  526. }
  527. }
  528. }
  529. out_unlock:
  530. pci_unlock_rescan_remove();
  531. mutex_unlock(&asus->hotplug_lock);
  532. }
  533. static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
  534. {
  535. struct asus_wmi *asus = data;
  536. if (event != ACPI_NOTIFY_BUS_CHECK)
  537. return;
  538. /*
  539. * We can't call directly asus_rfkill_hotplug because most
  540. * of the time WMBC is still being executed and not reetrant.
  541. * There is currently no way to tell ACPICA that we want this
  542. * method to be serialized, we schedule a asus_rfkill_hotplug
  543. * call later, in a safer context.
  544. */
  545. queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
  546. }
  547. static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
  548. {
  549. acpi_status status;
  550. acpi_handle handle;
  551. status = acpi_get_handle(NULL, node, &handle);
  552. if (ACPI_SUCCESS(status)) {
  553. status = acpi_install_notify_handler(handle,
  554. ACPI_SYSTEM_NOTIFY,
  555. asus_rfkill_notify, asus);
  556. if (ACPI_FAILURE(status))
  557. pr_warn("Failed to register notify on %s\n", node);
  558. } else
  559. return -ENODEV;
  560. return 0;
  561. }
  562. static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
  563. {
  564. acpi_status status = AE_OK;
  565. acpi_handle handle;
  566. status = acpi_get_handle(NULL, node, &handle);
  567. if (ACPI_SUCCESS(status)) {
  568. status = acpi_remove_notify_handler(handle,
  569. ACPI_SYSTEM_NOTIFY,
  570. asus_rfkill_notify);
  571. if (ACPI_FAILURE(status))
  572. pr_err("Error removing rfkill notify handler %s\n",
  573. node);
  574. }
  575. }
  576. static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
  577. u8 *value)
  578. {
  579. struct asus_wmi *asus = hotplug_slot->private;
  580. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  581. if (result < 0)
  582. return result;
  583. *value = !!result;
  584. return 0;
  585. }
  586. static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  587. {
  588. kfree(hotplug_slot->info);
  589. kfree(hotplug_slot);
  590. }
  591. static struct hotplug_slot_ops asus_hotplug_slot_ops = {
  592. .owner = THIS_MODULE,
  593. .get_adapter_status = asus_get_adapter_status,
  594. .get_power_status = asus_get_adapter_status,
  595. };
  596. static void asus_hotplug_work(struct work_struct *work)
  597. {
  598. struct asus_wmi *asus;
  599. asus = container_of(work, struct asus_wmi, hotplug_work);
  600. asus_rfkill_hotplug(asus);
  601. }
  602. static int asus_setup_pci_hotplug(struct asus_wmi *asus)
  603. {
  604. int ret = -ENOMEM;
  605. struct pci_bus *bus = pci_find_bus(0, 1);
  606. if (!bus) {
  607. pr_err("Unable to find wifi PCI bus\n");
  608. return -ENODEV;
  609. }
  610. asus->hotplug_workqueue =
  611. create_singlethread_workqueue("hotplug_workqueue");
  612. if (!asus->hotplug_workqueue)
  613. goto error_workqueue;
  614. INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
  615. asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  616. if (!asus->hotplug_slot)
  617. goto error_slot;
  618. asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  619. GFP_KERNEL);
  620. if (!asus->hotplug_slot->info)
  621. goto error_info;
  622. asus->hotplug_slot->private = asus;
  623. asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
  624. asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
  625. asus_get_adapter_status(asus->hotplug_slot,
  626. &asus->hotplug_slot->info->adapter_status);
  627. ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
  628. if (ret) {
  629. pr_err("Unable to register hotplug slot - %d\n", ret);
  630. goto error_register;
  631. }
  632. return 0;
  633. error_register:
  634. kfree(asus->hotplug_slot->info);
  635. error_info:
  636. kfree(asus->hotplug_slot);
  637. asus->hotplug_slot = NULL;
  638. error_slot:
  639. destroy_workqueue(asus->hotplug_workqueue);
  640. error_workqueue:
  641. return ret;
  642. }
  643. /*
  644. * Rfkill devices
  645. */
  646. static int asus_rfkill_set(void *data, bool blocked)
  647. {
  648. struct asus_rfkill *priv = data;
  649. u32 ctrl_param = !blocked;
  650. u32 dev_id = priv->dev_id;
  651. /*
  652. * If the user bit is set, BIOS can't set and record the wlan status,
  653. * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
  654. * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
  655. * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
  656. * while setting the wlan status through WMI.
  657. * This is also the behavior that windows app will do.
  658. */
  659. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  660. priv->asus->driver->wlan_ctrl_by_user)
  661. dev_id = ASUS_WMI_DEVID_WLAN_LED;
  662. return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
  663. }
  664. static void asus_rfkill_query(struct rfkill *rfkill, void *data)
  665. {
  666. struct asus_rfkill *priv = data;
  667. int result;
  668. result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
  669. if (result < 0)
  670. return;
  671. rfkill_set_sw_state(priv->rfkill, !result);
  672. }
  673. static int asus_rfkill_wlan_set(void *data, bool blocked)
  674. {
  675. struct asus_rfkill *priv = data;
  676. struct asus_wmi *asus = priv->asus;
  677. int ret;
  678. /*
  679. * This handler is enabled only if hotplug is enabled.
  680. * In this case, the asus_wmi_set_devstate() will
  681. * trigger a wmi notification and we need to wait
  682. * this call to finish before being able to call
  683. * any wmi method
  684. */
  685. mutex_lock(&asus->wmi_lock);
  686. ret = asus_rfkill_set(data, blocked);
  687. mutex_unlock(&asus->wmi_lock);
  688. return ret;
  689. }
  690. static const struct rfkill_ops asus_rfkill_wlan_ops = {
  691. .set_block = asus_rfkill_wlan_set,
  692. .query = asus_rfkill_query,
  693. };
  694. static const struct rfkill_ops asus_rfkill_ops = {
  695. .set_block = asus_rfkill_set,
  696. .query = asus_rfkill_query,
  697. };
  698. static int asus_new_rfkill(struct asus_wmi *asus,
  699. struct asus_rfkill *arfkill,
  700. const char *name, enum rfkill_type type, int dev_id)
  701. {
  702. int result = asus_wmi_get_devstate_simple(asus, dev_id);
  703. struct rfkill **rfkill = &arfkill->rfkill;
  704. if (result < 0)
  705. return result;
  706. arfkill->dev_id = dev_id;
  707. arfkill->asus = asus;
  708. if (dev_id == ASUS_WMI_DEVID_WLAN &&
  709. asus->driver->quirks->hotplug_wireless)
  710. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  711. &asus_rfkill_wlan_ops, arfkill);
  712. else
  713. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  714. &asus_rfkill_ops, arfkill);
  715. if (!*rfkill)
  716. return -EINVAL;
  717. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  718. (asus->driver->quirks->wapf > 0))
  719. rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
  720. rfkill_init_sw_state(*rfkill, !result);
  721. result = rfkill_register(*rfkill);
  722. if (result) {
  723. rfkill_destroy(*rfkill);
  724. *rfkill = NULL;
  725. return result;
  726. }
  727. return 0;
  728. }
  729. static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
  730. {
  731. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  732. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  733. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  734. if (asus->wlan.rfkill) {
  735. rfkill_unregister(asus->wlan.rfkill);
  736. rfkill_destroy(asus->wlan.rfkill);
  737. asus->wlan.rfkill = NULL;
  738. }
  739. /*
  740. * Refresh pci hotplug in case the rfkill state was changed after
  741. * asus_unregister_rfkill_notifier()
  742. */
  743. asus_rfkill_hotplug(asus);
  744. if (asus->hotplug_slot)
  745. pci_hp_deregister(asus->hotplug_slot);
  746. if (asus->hotplug_workqueue)
  747. destroy_workqueue(asus->hotplug_workqueue);
  748. if (asus->bluetooth.rfkill) {
  749. rfkill_unregister(asus->bluetooth.rfkill);
  750. rfkill_destroy(asus->bluetooth.rfkill);
  751. asus->bluetooth.rfkill = NULL;
  752. }
  753. if (asus->wimax.rfkill) {
  754. rfkill_unregister(asus->wimax.rfkill);
  755. rfkill_destroy(asus->wimax.rfkill);
  756. asus->wimax.rfkill = NULL;
  757. }
  758. if (asus->wwan3g.rfkill) {
  759. rfkill_unregister(asus->wwan3g.rfkill);
  760. rfkill_destroy(asus->wwan3g.rfkill);
  761. asus->wwan3g.rfkill = NULL;
  762. }
  763. if (asus->gps.rfkill) {
  764. rfkill_unregister(asus->gps.rfkill);
  765. rfkill_destroy(asus->gps.rfkill);
  766. asus->gps.rfkill = NULL;
  767. }
  768. if (asus->uwb.rfkill) {
  769. rfkill_unregister(asus->uwb.rfkill);
  770. rfkill_destroy(asus->uwb.rfkill);
  771. asus->uwb.rfkill = NULL;
  772. }
  773. }
  774. static int asus_wmi_rfkill_init(struct asus_wmi *asus)
  775. {
  776. int result = 0;
  777. mutex_init(&asus->hotplug_lock);
  778. mutex_init(&asus->wmi_lock);
  779. result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
  780. RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
  781. if (result && result != -ENODEV)
  782. goto exit;
  783. result = asus_new_rfkill(asus, &asus->bluetooth,
  784. "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
  785. ASUS_WMI_DEVID_BLUETOOTH);
  786. if (result && result != -ENODEV)
  787. goto exit;
  788. result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
  789. RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
  790. if (result && result != -ENODEV)
  791. goto exit;
  792. result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
  793. RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
  794. if (result && result != -ENODEV)
  795. goto exit;
  796. result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
  797. RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
  798. if (result && result != -ENODEV)
  799. goto exit;
  800. result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
  801. RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
  802. if (result && result != -ENODEV)
  803. goto exit;
  804. if (!asus->driver->quirks->hotplug_wireless)
  805. goto exit;
  806. result = asus_setup_pci_hotplug(asus);
  807. /*
  808. * If we get -EBUSY then something else is handling the PCI hotplug -
  809. * don't fail in this case
  810. */
  811. if (result == -EBUSY)
  812. result = 0;
  813. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  814. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  815. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  816. /*
  817. * Refresh pci hotplug in case the rfkill state was changed during
  818. * setup.
  819. */
  820. asus_rfkill_hotplug(asus);
  821. exit:
  822. if (result && result != -ENODEV)
  823. asus_wmi_rfkill_exit(asus);
  824. if (result == -ENODEV)
  825. result = 0;
  826. return result;
  827. }
  828. /*
  829. * Hwmon device
  830. */
  831. static ssize_t asus_hwmon_pwm1(struct device *dev,
  832. struct device_attribute *attr,
  833. char *buf)
  834. {
  835. struct asus_wmi *asus = dev_get_drvdata(dev);
  836. u32 value;
  837. int err;
  838. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
  839. if (err < 0)
  840. return err;
  841. value &= 0xFF;
  842. if (value == 1) /* Low Speed */
  843. value = 85;
  844. else if (value == 2)
  845. value = 170;
  846. else if (value == 3)
  847. value = 255;
  848. else if (value != 0) {
  849. pr_err("Unknown fan speed %#x\n", value);
  850. value = -1;
  851. }
  852. return sprintf(buf, "%d\n", value);
  853. }
  854. static ssize_t asus_hwmon_temp1(struct device *dev,
  855. struct device_attribute *attr,
  856. char *buf)
  857. {
  858. struct asus_wmi *asus = dev_get_drvdata(dev);
  859. u32 value;
  860. int err;
  861. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
  862. if (err < 0)
  863. return err;
  864. value = KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
  865. return sprintf(buf, "%d\n", value);
  866. }
  867. static DEVICE_ATTR(pwm1, S_IRUGO, asus_hwmon_pwm1, NULL);
  868. static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
  869. static struct attribute *hwmon_attributes[] = {
  870. &dev_attr_pwm1.attr,
  871. &dev_attr_temp1_input.attr,
  872. NULL
  873. };
  874. static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
  875. struct attribute *attr, int idx)
  876. {
  877. struct device *dev = container_of(kobj, struct device, kobj);
  878. struct platform_device *pdev = to_platform_device(dev->parent);
  879. struct asus_wmi *asus = platform_get_drvdata(pdev);
  880. bool ok = true;
  881. int dev_id = -1;
  882. u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
  883. if (attr == &dev_attr_pwm1.attr)
  884. dev_id = ASUS_WMI_DEVID_FAN_CTRL;
  885. else if (attr == &dev_attr_temp1_input.attr)
  886. dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
  887. if (dev_id != -1) {
  888. int err = asus_wmi_get_devstate(asus, dev_id, &value);
  889. if (err < 0)
  890. return 0; /* can't return negative here */
  891. }
  892. if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
  893. /*
  894. * We need to find a better way, probably using sfun,
  895. * bits or spec ...
  896. * Currently we disable it if:
  897. * - ASUS_WMI_UNSUPPORTED_METHOD is returned
  898. * - reverved bits are non-zero
  899. * - sfun and presence bit are not set
  900. */
  901. if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
  902. || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
  903. ok = false;
  904. } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
  905. /* If value is zero, something is clearly wrong */
  906. if (value == 0)
  907. ok = false;
  908. }
  909. return ok ? attr->mode : 0;
  910. }
  911. static struct attribute_group hwmon_attribute_group = {
  912. .is_visible = asus_hwmon_sysfs_is_visible,
  913. .attrs = hwmon_attributes
  914. };
  915. __ATTRIBUTE_GROUPS(hwmon_attribute);
  916. static int asus_wmi_hwmon_init(struct asus_wmi *asus)
  917. {
  918. struct device *hwmon;
  919. hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
  920. "asus", asus,
  921. hwmon_attribute_groups);
  922. if (IS_ERR(hwmon)) {
  923. pr_err("Could not register asus hwmon device\n");
  924. return PTR_ERR(hwmon);
  925. }
  926. return 0;
  927. }
  928. /*
  929. * Backlight
  930. */
  931. static int read_backlight_power(struct asus_wmi *asus)
  932. {
  933. int ret;
  934. if (asus->driver->quirks->store_backlight_power)
  935. ret = !asus->driver->panel_power;
  936. else
  937. ret = asus_wmi_get_devstate_simple(asus,
  938. ASUS_WMI_DEVID_BACKLIGHT);
  939. if (ret < 0)
  940. return ret;
  941. return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  942. }
  943. static int read_brightness_max(struct asus_wmi *asus)
  944. {
  945. u32 retval;
  946. int err;
  947. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  948. if (err < 0)
  949. return err;
  950. retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
  951. retval >>= 8;
  952. if (!retval)
  953. return -ENODEV;
  954. return retval;
  955. }
  956. static int read_brightness(struct backlight_device *bd)
  957. {
  958. struct asus_wmi *asus = bl_get_data(bd);
  959. u32 retval;
  960. int err;
  961. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  962. if (err < 0)
  963. return err;
  964. return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  965. }
  966. static u32 get_scalar_command(struct backlight_device *bd)
  967. {
  968. struct asus_wmi *asus = bl_get_data(bd);
  969. u32 ctrl_param = 0;
  970. if ((asus->driver->brightness < bd->props.brightness) ||
  971. bd->props.brightness == bd->props.max_brightness)
  972. ctrl_param = 0x00008001;
  973. else if ((asus->driver->brightness > bd->props.brightness) ||
  974. bd->props.brightness == 0)
  975. ctrl_param = 0x00008000;
  976. asus->driver->brightness = bd->props.brightness;
  977. return ctrl_param;
  978. }
  979. static int update_bl_status(struct backlight_device *bd)
  980. {
  981. struct asus_wmi *asus = bl_get_data(bd);
  982. u32 ctrl_param;
  983. int power, err = 0;
  984. power = read_backlight_power(asus);
  985. if (power != -ENODEV && bd->props.power != power) {
  986. ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
  987. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
  988. ctrl_param, NULL);
  989. if (asus->driver->quirks->store_backlight_power)
  990. asus->driver->panel_power = bd->props.power;
  991. /* When using scalar brightness, updating the brightness
  992. * will mess with the backlight power */
  993. if (asus->driver->quirks->scalar_panel_brightness)
  994. return err;
  995. }
  996. if (asus->driver->quirks->scalar_panel_brightness)
  997. ctrl_param = get_scalar_command(bd);
  998. else
  999. ctrl_param = bd->props.brightness;
  1000. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
  1001. ctrl_param, NULL);
  1002. return err;
  1003. }
  1004. static const struct backlight_ops asus_wmi_bl_ops = {
  1005. .get_brightness = read_brightness,
  1006. .update_status = update_bl_status,
  1007. };
  1008. static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
  1009. {
  1010. struct backlight_device *bd = asus->backlight_device;
  1011. int old = bd->props.brightness;
  1012. int new = old;
  1013. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  1014. new = code - NOTIFY_BRNUP_MIN + 1;
  1015. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  1016. new = code - NOTIFY_BRNDOWN_MIN;
  1017. bd->props.brightness = new;
  1018. backlight_update_status(bd);
  1019. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  1020. return old;
  1021. }
  1022. static int asus_wmi_backlight_init(struct asus_wmi *asus)
  1023. {
  1024. struct backlight_device *bd;
  1025. struct backlight_properties props;
  1026. int max;
  1027. int power;
  1028. max = read_brightness_max(asus);
  1029. if (max < 0)
  1030. return max;
  1031. power = read_backlight_power(asus);
  1032. if (power == -ENODEV)
  1033. power = FB_BLANK_UNBLANK;
  1034. else if (power < 0)
  1035. return power;
  1036. memset(&props, 0, sizeof(struct backlight_properties));
  1037. props.type = BACKLIGHT_PLATFORM;
  1038. props.max_brightness = max;
  1039. bd = backlight_device_register(asus->driver->name,
  1040. &asus->platform_device->dev, asus,
  1041. &asus_wmi_bl_ops, &props);
  1042. if (IS_ERR(bd)) {
  1043. pr_err("Could not register backlight device\n");
  1044. return PTR_ERR(bd);
  1045. }
  1046. asus->backlight_device = bd;
  1047. if (asus->driver->quirks->store_backlight_power)
  1048. asus->driver->panel_power = power;
  1049. bd->props.brightness = read_brightness(bd);
  1050. bd->props.power = power;
  1051. backlight_update_status(bd);
  1052. asus->driver->brightness = bd->props.brightness;
  1053. return 0;
  1054. }
  1055. static void asus_wmi_backlight_exit(struct asus_wmi *asus)
  1056. {
  1057. backlight_device_unregister(asus->backlight_device);
  1058. asus->backlight_device = NULL;
  1059. }
  1060. static int is_display_toggle(int code)
  1061. {
  1062. /* display toggle keys */
  1063. if ((code >= 0x61 && code <= 0x67) ||
  1064. (code >= 0x8c && code <= 0x93) ||
  1065. (code >= 0xa0 && code <= 0xa7) ||
  1066. (code >= 0xd0 && code <= 0xd5))
  1067. return 1;
  1068. return 0;
  1069. }
  1070. static void asus_wmi_notify(u32 value, void *context)
  1071. {
  1072. struct asus_wmi *asus = context;
  1073. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  1074. union acpi_object *obj;
  1075. acpi_status status;
  1076. int code;
  1077. int orig_code;
  1078. unsigned int key_value = 1;
  1079. bool autorelease = 1;
  1080. status = wmi_get_event_data(value, &response);
  1081. if (status != AE_OK) {
  1082. pr_err("bad event status 0x%x\n", status);
  1083. return;
  1084. }
  1085. obj = (union acpi_object *)response.pointer;
  1086. if (!obj || obj->type != ACPI_TYPE_INTEGER)
  1087. goto exit;
  1088. code = obj->integer.value;
  1089. orig_code = code;
  1090. if (asus->driver->key_filter) {
  1091. asus->driver->key_filter(asus->driver, &code, &key_value,
  1092. &autorelease);
  1093. if (code == ASUS_WMI_KEY_IGNORE)
  1094. goto exit;
  1095. }
  1096. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  1097. code = ASUS_WMI_BRN_UP;
  1098. else if (code >= NOTIFY_BRNDOWN_MIN &&
  1099. code <= NOTIFY_BRNDOWN_MAX)
  1100. code = ASUS_WMI_BRN_DOWN;
  1101. if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
  1102. if (!acpi_video_backlight_support()) {
  1103. asus_wmi_backlight_notify(asus, orig_code);
  1104. goto exit;
  1105. }
  1106. }
  1107. if (is_display_toggle(code) &&
  1108. asus->driver->quirks->no_display_toggle)
  1109. goto exit;
  1110. if (!sparse_keymap_report_event(asus->inputdev, code,
  1111. key_value, autorelease))
  1112. pr_info("Unknown key %x pressed\n", code);
  1113. exit:
  1114. kfree(obj);
  1115. }
  1116. /*
  1117. * Sys helpers
  1118. */
  1119. static int parse_arg(const char *buf, unsigned long count, int *val)
  1120. {
  1121. if (!count)
  1122. return 0;
  1123. if (sscanf(buf, "%i", val) != 1)
  1124. return -EINVAL;
  1125. return count;
  1126. }
  1127. static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
  1128. const char *buf, size_t count)
  1129. {
  1130. u32 retval;
  1131. int rv, err, value;
  1132. value = asus_wmi_get_devstate_simple(asus, devid);
  1133. if (value == -ENODEV) /* Check device presence */
  1134. return value;
  1135. rv = parse_arg(buf, count, &value);
  1136. err = asus_wmi_set_devstate(devid, value, &retval);
  1137. if (err < 0)
  1138. return err;
  1139. return rv;
  1140. }
  1141. static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
  1142. {
  1143. int value = asus_wmi_get_devstate_simple(asus, devid);
  1144. if (value < 0)
  1145. return value;
  1146. return sprintf(buf, "%d\n", value);
  1147. }
  1148. #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
  1149. static ssize_t show_##_name(struct device *dev, \
  1150. struct device_attribute *attr, \
  1151. char *buf) \
  1152. { \
  1153. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1154. \
  1155. return show_sys_wmi(asus, _cm, buf); \
  1156. } \
  1157. static ssize_t store_##_name(struct device *dev, \
  1158. struct device_attribute *attr, \
  1159. const char *buf, size_t count) \
  1160. { \
  1161. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1162. \
  1163. return store_sys_wmi(asus, _cm, buf, count); \
  1164. } \
  1165. static struct device_attribute dev_attr_##_name = { \
  1166. .attr = { \
  1167. .name = __stringify(_name), \
  1168. .mode = _mode }, \
  1169. .show = show_##_name, \
  1170. .store = store_##_name, \
  1171. }
  1172. ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
  1173. ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
  1174. ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
  1175. ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
  1176. static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
  1177. const char *buf, size_t count)
  1178. {
  1179. int value, rv;
  1180. if (!count || sscanf(buf, "%i", &value) != 1)
  1181. return -EINVAL;
  1182. if (value < 0 || value > 2)
  1183. return -EINVAL;
  1184. rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
  1185. if (rv < 0)
  1186. return rv;
  1187. return count;
  1188. }
  1189. static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
  1190. static struct attribute *platform_attributes[] = {
  1191. &dev_attr_cpufv.attr,
  1192. &dev_attr_camera.attr,
  1193. &dev_attr_cardr.attr,
  1194. &dev_attr_touchpad.attr,
  1195. &dev_attr_lid_resume.attr,
  1196. NULL
  1197. };
  1198. static umode_t asus_sysfs_is_visible(struct kobject *kobj,
  1199. struct attribute *attr, int idx)
  1200. {
  1201. struct device *dev = container_of(kobj, struct device, kobj);
  1202. struct platform_device *pdev = to_platform_device(dev);
  1203. struct asus_wmi *asus = platform_get_drvdata(pdev);
  1204. bool ok = true;
  1205. int devid = -1;
  1206. if (attr == &dev_attr_camera.attr)
  1207. devid = ASUS_WMI_DEVID_CAMERA;
  1208. else if (attr == &dev_attr_cardr.attr)
  1209. devid = ASUS_WMI_DEVID_CARDREADER;
  1210. else if (attr == &dev_attr_touchpad.attr)
  1211. devid = ASUS_WMI_DEVID_TOUCHPAD;
  1212. else if (attr == &dev_attr_lid_resume.attr)
  1213. devid = ASUS_WMI_DEVID_LID_RESUME;
  1214. if (devid != -1)
  1215. ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
  1216. return ok ? attr->mode : 0;
  1217. }
  1218. static struct attribute_group platform_attribute_group = {
  1219. .is_visible = asus_sysfs_is_visible,
  1220. .attrs = platform_attributes
  1221. };
  1222. static void asus_wmi_sysfs_exit(struct platform_device *device)
  1223. {
  1224. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  1225. }
  1226. static int asus_wmi_sysfs_init(struct platform_device *device)
  1227. {
  1228. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  1229. }
  1230. /*
  1231. * Platform device
  1232. */
  1233. static int asus_wmi_platform_init(struct asus_wmi *asus)
  1234. {
  1235. int rv;
  1236. /* INIT enable hotkeys on some models */
  1237. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
  1238. pr_info("Initialization: %#x\n", rv);
  1239. /* We don't know yet what to do with this version... */
  1240. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
  1241. pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
  1242. asus->spec = rv;
  1243. }
  1244. /*
  1245. * The SFUN method probably allows the original driver to get the list
  1246. * of features supported by a given model. For now, 0x0100 or 0x0800
  1247. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  1248. * The significance of others is yet to be found.
  1249. */
  1250. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
  1251. pr_info("SFUN value: %#x\n", rv);
  1252. asus->sfun = rv;
  1253. }
  1254. /*
  1255. * Eee PC and Notebooks seems to have different method_id for DSTS,
  1256. * but it may also be related to the BIOS's SPEC.
  1257. * Note, on most Eeepc, there is no way to check if a method exist
  1258. * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
  1259. * but once again, SPEC may probably be used for that kind of things.
  1260. */
  1261. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
  1262. asus->dsts_id = ASUS_WMI_METHODID_DSTS;
  1263. else
  1264. asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
  1265. /* CWAP allow to define the behavior of the Fn+F2 key,
  1266. * this method doesn't seems to be present on Eee PCs */
  1267. if (asus->driver->quirks->wapf >= 0)
  1268. asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
  1269. asus->driver->quirks->wapf, NULL);
  1270. return asus_wmi_sysfs_init(asus->platform_device);
  1271. }
  1272. static void asus_wmi_platform_exit(struct asus_wmi *asus)
  1273. {
  1274. asus_wmi_sysfs_exit(asus->platform_device);
  1275. }
  1276. /*
  1277. * debugfs
  1278. */
  1279. struct asus_wmi_debugfs_node {
  1280. struct asus_wmi *asus;
  1281. char *name;
  1282. int (*show) (struct seq_file *m, void *data);
  1283. };
  1284. static int show_dsts(struct seq_file *m, void *data)
  1285. {
  1286. struct asus_wmi *asus = m->private;
  1287. int err;
  1288. u32 retval = -1;
  1289. err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
  1290. if (err < 0)
  1291. return err;
  1292. seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
  1293. return 0;
  1294. }
  1295. static int show_devs(struct seq_file *m, void *data)
  1296. {
  1297. struct asus_wmi *asus = m->private;
  1298. int err;
  1299. u32 retval = -1;
  1300. err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
  1301. &retval);
  1302. if (err < 0)
  1303. return err;
  1304. seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
  1305. asus->debug.ctrl_param, retval);
  1306. return 0;
  1307. }
  1308. static int show_call(struct seq_file *m, void *data)
  1309. {
  1310. struct asus_wmi *asus = m->private;
  1311. struct bios_args args = {
  1312. .arg0 = asus->debug.dev_id,
  1313. .arg1 = asus->debug.ctrl_param,
  1314. };
  1315. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  1316. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  1317. union acpi_object *obj;
  1318. acpi_status status;
  1319. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
  1320. 1, asus->debug.method_id,
  1321. &input, &output);
  1322. if (ACPI_FAILURE(status))
  1323. return -EIO;
  1324. obj = (union acpi_object *)output.pointer;
  1325. if (obj && obj->type == ACPI_TYPE_INTEGER)
  1326. seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
  1327. asus->debug.dev_id, asus->debug.ctrl_param,
  1328. (u32) obj->integer.value);
  1329. else
  1330. seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
  1331. asus->debug.dev_id, asus->debug.ctrl_param,
  1332. obj ? obj->type : -1);
  1333. kfree(obj);
  1334. return 0;
  1335. }
  1336. static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
  1337. {NULL, "devs", show_devs},
  1338. {NULL, "dsts", show_dsts},
  1339. {NULL, "call", show_call},
  1340. };
  1341. static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
  1342. {
  1343. struct asus_wmi_debugfs_node *node = inode->i_private;
  1344. return single_open(file, node->show, node->asus);
  1345. }
  1346. static const struct file_operations asus_wmi_debugfs_io_ops = {
  1347. .owner = THIS_MODULE,
  1348. .open = asus_wmi_debugfs_open,
  1349. .read = seq_read,
  1350. .llseek = seq_lseek,
  1351. .release = single_release,
  1352. };
  1353. static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
  1354. {
  1355. debugfs_remove_recursive(asus->debug.root);
  1356. }
  1357. static int asus_wmi_debugfs_init(struct asus_wmi *asus)
  1358. {
  1359. struct dentry *dent;
  1360. int i;
  1361. asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
  1362. if (!asus->debug.root) {
  1363. pr_err("failed to create debugfs directory\n");
  1364. goto error_debugfs;
  1365. }
  1366. dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
  1367. asus->debug.root, &asus->debug.method_id);
  1368. if (!dent)
  1369. goto error_debugfs;
  1370. dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
  1371. asus->debug.root, &asus->debug.dev_id);
  1372. if (!dent)
  1373. goto error_debugfs;
  1374. dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
  1375. asus->debug.root, &asus->debug.ctrl_param);
  1376. if (!dent)
  1377. goto error_debugfs;
  1378. for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
  1379. struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
  1380. node->asus = asus;
  1381. dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  1382. asus->debug.root, node,
  1383. &asus_wmi_debugfs_io_ops);
  1384. if (!dent) {
  1385. pr_err("failed to create debug file: %s\n", node->name);
  1386. goto error_debugfs;
  1387. }
  1388. }
  1389. return 0;
  1390. error_debugfs:
  1391. asus_wmi_debugfs_exit(asus);
  1392. return -ENOMEM;
  1393. }
  1394. /*
  1395. * WMI Driver
  1396. */
  1397. static int asus_wmi_add(struct platform_device *pdev)
  1398. {
  1399. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  1400. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  1401. struct asus_wmi *asus;
  1402. const char *chassis_type;
  1403. acpi_status status;
  1404. int err;
  1405. u32 result;
  1406. asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
  1407. if (!asus)
  1408. return -ENOMEM;
  1409. asus->driver = wdrv;
  1410. asus->platform_device = pdev;
  1411. wdrv->platform_device = pdev;
  1412. platform_set_drvdata(asus->platform_device, asus);
  1413. if (wdrv->detect_quirks)
  1414. wdrv->detect_quirks(asus->driver);
  1415. err = asus_wmi_platform_init(asus);
  1416. if (err)
  1417. goto fail_platform;
  1418. err = asus_wmi_input_init(asus);
  1419. if (err)
  1420. goto fail_input;
  1421. err = asus_wmi_hwmon_init(asus);
  1422. if (err)
  1423. goto fail_hwmon;
  1424. err = asus_wmi_led_init(asus);
  1425. if (err)
  1426. goto fail_leds;
  1427. err = asus_wmi_rfkill_init(asus);
  1428. if (err)
  1429. goto fail_rfkill;
  1430. /* Some Asus desktop boards export an acpi-video backlight interface,
  1431. stop this from showing up */
  1432. chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
  1433. if (chassis_type && !strcmp(chassis_type, "3"))
  1434. acpi_video_dmi_promote_vendor();
  1435. if (asus->driver->quirks->wmi_backlight_power)
  1436. acpi_video_dmi_promote_vendor();
  1437. if (!acpi_video_backlight_support()) {
  1438. pr_info("Disabling ACPI video driver\n");
  1439. acpi_video_unregister();
  1440. err = asus_wmi_backlight_init(asus);
  1441. if (err && err != -ENODEV)
  1442. goto fail_backlight;
  1443. } else
  1444. pr_info("Backlight controlled by ACPI video driver\n");
  1445. status = wmi_install_notify_handler(asus->driver->event_guid,
  1446. asus_wmi_notify, asus);
  1447. if (ACPI_FAILURE(status)) {
  1448. pr_err("Unable to register notify handler - %d\n", status);
  1449. err = -ENODEV;
  1450. goto fail_wmi_handler;
  1451. }
  1452. err = asus_wmi_debugfs_init(asus);
  1453. if (err)
  1454. goto fail_debugfs;
  1455. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
  1456. if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
  1457. asus->driver->wlan_ctrl_by_user = 1;
  1458. return 0;
  1459. fail_debugfs:
  1460. wmi_remove_notify_handler(asus->driver->event_guid);
  1461. fail_wmi_handler:
  1462. asus_wmi_backlight_exit(asus);
  1463. fail_backlight:
  1464. asus_wmi_rfkill_exit(asus);
  1465. fail_rfkill:
  1466. asus_wmi_led_exit(asus);
  1467. fail_leds:
  1468. fail_hwmon:
  1469. asus_wmi_input_exit(asus);
  1470. fail_input:
  1471. asus_wmi_platform_exit(asus);
  1472. fail_platform:
  1473. kfree(asus);
  1474. return err;
  1475. }
  1476. static int asus_wmi_remove(struct platform_device *device)
  1477. {
  1478. struct asus_wmi *asus;
  1479. asus = platform_get_drvdata(device);
  1480. wmi_remove_notify_handler(asus->driver->event_guid);
  1481. asus_wmi_backlight_exit(asus);
  1482. asus_wmi_input_exit(asus);
  1483. asus_wmi_led_exit(asus);
  1484. asus_wmi_rfkill_exit(asus);
  1485. asus_wmi_debugfs_exit(asus);
  1486. asus_wmi_platform_exit(asus);
  1487. kfree(asus);
  1488. return 0;
  1489. }
  1490. /*
  1491. * Platform driver - hibernate/resume callbacks
  1492. */
  1493. static int asus_hotk_thaw(struct device *device)
  1494. {
  1495. struct asus_wmi *asus = dev_get_drvdata(device);
  1496. if (asus->wlan.rfkill) {
  1497. bool wlan;
  1498. /*
  1499. * Work around bios bug - acpi _PTS turns off the wireless led
  1500. * during suspend. Normally it restores it on resume, but
  1501. * we should kick it ourselves in case hibernation is aborted.
  1502. */
  1503. wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  1504. asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
  1505. }
  1506. return 0;
  1507. }
  1508. static int asus_hotk_restore(struct device *device)
  1509. {
  1510. struct asus_wmi *asus = dev_get_drvdata(device);
  1511. int bl;
  1512. /* Refresh both wlan rfkill state and pci hotplug */
  1513. if (asus->wlan.rfkill)
  1514. asus_rfkill_hotplug(asus);
  1515. if (asus->bluetooth.rfkill) {
  1516. bl = !asus_wmi_get_devstate_simple(asus,
  1517. ASUS_WMI_DEVID_BLUETOOTH);
  1518. rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
  1519. }
  1520. if (asus->wimax.rfkill) {
  1521. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
  1522. rfkill_set_sw_state(asus->wimax.rfkill, bl);
  1523. }
  1524. if (asus->wwan3g.rfkill) {
  1525. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
  1526. rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
  1527. }
  1528. if (asus->gps.rfkill) {
  1529. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
  1530. rfkill_set_sw_state(asus->gps.rfkill, bl);
  1531. }
  1532. if (asus->uwb.rfkill) {
  1533. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
  1534. rfkill_set_sw_state(asus->uwb.rfkill, bl);
  1535. }
  1536. return 0;
  1537. }
  1538. static const struct dev_pm_ops asus_pm_ops = {
  1539. .thaw = asus_hotk_thaw,
  1540. .restore = asus_hotk_restore,
  1541. };
  1542. static int asus_wmi_probe(struct platform_device *pdev)
  1543. {
  1544. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  1545. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  1546. int ret;
  1547. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  1548. pr_warn("Management GUID not found\n");
  1549. return -ENODEV;
  1550. }
  1551. if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
  1552. pr_warn("Event GUID not found\n");
  1553. return -ENODEV;
  1554. }
  1555. if (wdrv->probe) {
  1556. ret = wdrv->probe(pdev);
  1557. if (ret)
  1558. return ret;
  1559. }
  1560. return asus_wmi_add(pdev);
  1561. }
  1562. static bool used;
  1563. int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
  1564. {
  1565. struct platform_driver *platform_driver;
  1566. struct platform_device *platform_device;
  1567. if (used)
  1568. return -EBUSY;
  1569. platform_driver = &driver->platform_driver;
  1570. platform_driver->remove = asus_wmi_remove;
  1571. platform_driver->driver.owner = driver->owner;
  1572. platform_driver->driver.name = driver->name;
  1573. platform_driver->driver.pm = &asus_pm_ops;
  1574. platform_device = platform_create_bundle(platform_driver,
  1575. asus_wmi_probe,
  1576. NULL, 0, NULL, 0);
  1577. if (IS_ERR(platform_device))
  1578. return PTR_ERR(platform_device);
  1579. used = true;
  1580. return 0;
  1581. }
  1582. EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
  1583. void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
  1584. {
  1585. platform_device_unregister(driver->platform_device);
  1586. platform_driver_unregister(&driver->platform_driver);
  1587. used = false;
  1588. }
  1589. EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
  1590. static int __init asus_wmi_init(void)
  1591. {
  1592. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  1593. pr_info("Asus Management GUID not found\n");
  1594. return -ENODEV;
  1595. }
  1596. pr_info("ASUS WMI generic driver loaded\n");
  1597. return 0;
  1598. }
  1599. static void __exit asus_wmi_exit(void)
  1600. {
  1601. pr_info("ASUS WMI generic driver unloaded\n");
  1602. }
  1603. module_init(asus_wmi_init);
  1604. module_exit(asus_wmi_exit);