asus-wmi.c 48 KB

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