asus-wmi.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  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. if (asus->backlight_device)
  1058. backlight_device_unregister(asus->backlight_device);
  1059. asus->backlight_device = NULL;
  1060. }
  1061. static int is_display_toggle(int code)
  1062. {
  1063. /* display toggle keys */
  1064. if ((code >= 0x61 && code <= 0x67) ||
  1065. (code >= 0x8c && code <= 0x93) ||
  1066. (code >= 0xa0 && code <= 0xa7) ||
  1067. (code >= 0xd0 && code <= 0xd5))
  1068. return 1;
  1069. return 0;
  1070. }
  1071. static void asus_wmi_notify(u32 value, void *context)
  1072. {
  1073. struct asus_wmi *asus = context;
  1074. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  1075. union acpi_object *obj;
  1076. acpi_status status;
  1077. int code;
  1078. int orig_code;
  1079. unsigned int key_value = 1;
  1080. bool autorelease = 1;
  1081. status = wmi_get_event_data(value, &response);
  1082. if (status != AE_OK) {
  1083. pr_err("bad event status 0x%x\n", status);
  1084. return;
  1085. }
  1086. obj = (union acpi_object *)response.pointer;
  1087. if (!obj || obj->type != ACPI_TYPE_INTEGER)
  1088. goto exit;
  1089. code = obj->integer.value;
  1090. orig_code = code;
  1091. if (asus->driver->key_filter) {
  1092. asus->driver->key_filter(asus->driver, &code, &key_value,
  1093. &autorelease);
  1094. if (code == ASUS_WMI_KEY_IGNORE)
  1095. goto exit;
  1096. }
  1097. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  1098. code = ASUS_WMI_BRN_UP;
  1099. else if (code >= NOTIFY_BRNDOWN_MIN &&
  1100. code <= NOTIFY_BRNDOWN_MAX)
  1101. code = ASUS_WMI_BRN_DOWN;
  1102. if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
  1103. if (!acpi_video_backlight_support()) {
  1104. asus_wmi_backlight_notify(asus, orig_code);
  1105. goto exit;
  1106. }
  1107. }
  1108. if (is_display_toggle(code) &&
  1109. asus->driver->quirks->no_display_toggle)
  1110. goto exit;
  1111. if (!sparse_keymap_report_event(asus->inputdev, code,
  1112. key_value, autorelease))
  1113. pr_info("Unknown key %x pressed\n", code);
  1114. exit:
  1115. kfree(obj);
  1116. }
  1117. /*
  1118. * Sys helpers
  1119. */
  1120. static int parse_arg(const char *buf, unsigned long count, int *val)
  1121. {
  1122. if (!count)
  1123. return 0;
  1124. if (sscanf(buf, "%i", val) != 1)
  1125. return -EINVAL;
  1126. return count;
  1127. }
  1128. static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
  1129. const char *buf, size_t count)
  1130. {
  1131. u32 retval;
  1132. int rv, err, value;
  1133. value = asus_wmi_get_devstate_simple(asus, devid);
  1134. if (value == -ENODEV) /* Check device presence */
  1135. return value;
  1136. rv = parse_arg(buf, count, &value);
  1137. err = asus_wmi_set_devstate(devid, value, &retval);
  1138. if (err < 0)
  1139. return err;
  1140. return rv;
  1141. }
  1142. static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
  1143. {
  1144. int value = asus_wmi_get_devstate_simple(asus, devid);
  1145. if (value < 0)
  1146. return value;
  1147. return sprintf(buf, "%d\n", value);
  1148. }
  1149. #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
  1150. static ssize_t show_##_name(struct device *dev, \
  1151. struct device_attribute *attr, \
  1152. char *buf) \
  1153. { \
  1154. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1155. \
  1156. return show_sys_wmi(asus, _cm, buf); \
  1157. } \
  1158. static ssize_t store_##_name(struct device *dev, \
  1159. struct device_attribute *attr, \
  1160. const char *buf, size_t count) \
  1161. { \
  1162. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1163. \
  1164. return store_sys_wmi(asus, _cm, buf, count); \
  1165. } \
  1166. static struct device_attribute dev_attr_##_name = { \
  1167. .attr = { \
  1168. .name = __stringify(_name), \
  1169. .mode = _mode }, \
  1170. .show = show_##_name, \
  1171. .store = store_##_name, \
  1172. }
  1173. ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
  1174. ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
  1175. ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
  1176. ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
  1177. static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
  1178. const char *buf, size_t count)
  1179. {
  1180. int value, rv;
  1181. if (!count || sscanf(buf, "%i", &value) != 1)
  1182. return -EINVAL;
  1183. if (value < 0 || value > 2)
  1184. return -EINVAL;
  1185. rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
  1186. if (rv < 0)
  1187. return rv;
  1188. return count;
  1189. }
  1190. static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
  1191. static struct attribute *platform_attributes[] = {
  1192. &dev_attr_cpufv.attr,
  1193. &dev_attr_camera.attr,
  1194. &dev_attr_cardr.attr,
  1195. &dev_attr_touchpad.attr,
  1196. &dev_attr_lid_resume.attr,
  1197. NULL
  1198. };
  1199. static umode_t asus_sysfs_is_visible(struct kobject *kobj,
  1200. struct attribute *attr, int idx)
  1201. {
  1202. struct device *dev = container_of(kobj, struct device, kobj);
  1203. struct platform_device *pdev = to_platform_device(dev);
  1204. struct asus_wmi *asus = platform_get_drvdata(pdev);
  1205. bool ok = true;
  1206. int devid = -1;
  1207. if (attr == &dev_attr_camera.attr)
  1208. devid = ASUS_WMI_DEVID_CAMERA;
  1209. else if (attr == &dev_attr_cardr.attr)
  1210. devid = ASUS_WMI_DEVID_CARDREADER;
  1211. else if (attr == &dev_attr_touchpad.attr)
  1212. devid = ASUS_WMI_DEVID_TOUCHPAD;
  1213. else if (attr == &dev_attr_lid_resume.attr)
  1214. devid = ASUS_WMI_DEVID_LID_RESUME;
  1215. if (devid != -1)
  1216. ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
  1217. return ok ? attr->mode : 0;
  1218. }
  1219. static struct attribute_group platform_attribute_group = {
  1220. .is_visible = asus_sysfs_is_visible,
  1221. .attrs = platform_attributes
  1222. };
  1223. static void asus_wmi_sysfs_exit(struct platform_device *device)
  1224. {
  1225. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  1226. }
  1227. static int asus_wmi_sysfs_init(struct platform_device *device)
  1228. {
  1229. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  1230. }
  1231. /*
  1232. * Platform device
  1233. */
  1234. static int asus_wmi_platform_init(struct asus_wmi *asus)
  1235. {
  1236. int rv;
  1237. /* INIT enable hotkeys on some models */
  1238. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
  1239. pr_info("Initialization: %#x\n", rv);
  1240. /* We don't know yet what to do with this version... */
  1241. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
  1242. pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
  1243. asus->spec = rv;
  1244. }
  1245. /*
  1246. * The SFUN method probably allows the original driver to get the list
  1247. * of features supported by a given model. For now, 0x0100 or 0x0800
  1248. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  1249. * The significance of others is yet to be found.
  1250. */
  1251. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
  1252. pr_info("SFUN value: %#x\n", rv);
  1253. asus->sfun = rv;
  1254. }
  1255. /*
  1256. * Eee PC and Notebooks seems to have different method_id for DSTS,
  1257. * but it may also be related to the BIOS's SPEC.
  1258. * Note, on most Eeepc, there is no way to check if a method exist
  1259. * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
  1260. * but once again, SPEC may probably be used for that kind of things.
  1261. */
  1262. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
  1263. asus->dsts_id = ASUS_WMI_METHODID_DSTS;
  1264. else
  1265. asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
  1266. /* CWAP allow to define the behavior of the Fn+F2 key,
  1267. * this method doesn't seems to be present on Eee PCs */
  1268. if (asus->driver->quirks->wapf >= 0)
  1269. asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
  1270. asus->driver->quirks->wapf, NULL);
  1271. return asus_wmi_sysfs_init(asus->platform_device);
  1272. }
  1273. static void asus_wmi_platform_exit(struct asus_wmi *asus)
  1274. {
  1275. asus_wmi_sysfs_exit(asus->platform_device);
  1276. }
  1277. /*
  1278. * debugfs
  1279. */
  1280. struct asus_wmi_debugfs_node {
  1281. struct asus_wmi *asus;
  1282. char *name;
  1283. int (*show) (struct seq_file *m, void *data);
  1284. };
  1285. static int show_dsts(struct seq_file *m, void *data)
  1286. {
  1287. struct asus_wmi *asus = m->private;
  1288. int err;
  1289. u32 retval = -1;
  1290. err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
  1291. if (err < 0)
  1292. return err;
  1293. seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
  1294. return 0;
  1295. }
  1296. static int show_devs(struct seq_file *m, void *data)
  1297. {
  1298. struct asus_wmi *asus = m->private;
  1299. int err;
  1300. u32 retval = -1;
  1301. err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
  1302. &retval);
  1303. if (err < 0)
  1304. return err;
  1305. seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
  1306. asus->debug.ctrl_param, retval);
  1307. return 0;
  1308. }
  1309. static int show_call(struct seq_file *m, void *data)
  1310. {
  1311. struct asus_wmi *asus = m->private;
  1312. struct bios_args args = {
  1313. .arg0 = asus->debug.dev_id,
  1314. .arg1 = asus->debug.ctrl_param,
  1315. };
  1316. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  1317. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  1318. union acpi_object *obj;
  1319. acpi_status status;
  1320. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
  1321. 1, asus->debug.method_id,
  1322. &input, &output);
  1323. if (ACPI_FAILURE(status))
  1324. return -EIO;
  1325. obj = (union acpi_object *)output.pointer;
  1326. if (obj && obj->type == ACPI_TYPE_INTEGER)
  1327. seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
  1328. asus->debug.dev_id, asus->debug.ctrl_param,
  1329. (u32) obj->integer.value);
  1330. else
  1331. seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
  1332. asus->debug.dev_id, asus->debug.ctrl_param,
  1333. obj ? obj->type : -1);
  1334. kfree(obj);
  1335. return 0;
  1336. }
  1337. static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
  1338. {NULL, "devs", show_devs},
  1339. {NULL, "dsts", show_dsts},
  1340. {NULL, "call", show_call},
  1341. };
  1342. static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
  1343. {
  1344. struct asus_wmi_debugfs_node *node = inode->i_private;
  1345. return single_open(file, node->show, node->asus);
  1346. }
  1347. static const struct file_operations asus_wmi_debugfs_io_ops = {
  1348. .owner = THIS_MODULE,
  1349. .open = asus_wmi_debugfs_open,
  1350. .read = seq_read,
  1351. .llseek = seq_lseek,
  1352. .release = single_release,
  1353. };
  1354. static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
  1355. {
  1356. debugfs_remove_recursive(asus->debug.root);
  1357. }
  1358. static int asus_wmi_debugfs_init(struct asus_wmi *asus)
  1359. {
  1360. struct dentry *dent;
  1361. int i;
  1362. asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
  1363. if (!asus->debug.root) {
  1364. pr_err("failed to create debugfs directory\n");
  1365. goto error_debugfs;
  1366. }
  1367. dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
  1368. asus->debug.root, &asus->debug.method_id);
  1369. if (!dent)
  1370. goto error_debugfs;
  1371. dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
  1372. asus->debug.root, &asus->debug.dev_id);
  1373. if (!dent)
  1374. goto error_debugfs;
  1375. dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
  1376. asus->debug.root, &asus->debug.ctrl_param);
  1377. if (!dent)
  1378. goto error_debugfs;
  1379. for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
  1380. struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
  1381. node->asus = asus;
  1382. dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  1383. asus->debug.root, node,
  1384. &asus_wmi_debugfs_io_ops);
  1385. if (!dent) {
  1386. pr_err("failed to create debug file: %s\n", node->name);
  1387. goto error_debugfs;
  1388. }
  1389. }
  1390. return 0;
  1391. error_debugfs:
  1392. asus_wmi_debugfs_exit(asus);
  1393. return -ENOMEM;
  1394. }
  1395. /*
  1396. * WMI Driver
  1397. */
  1398. static int asus_wmi_add(struct platform_device *pdev)
  1399. {
  1400. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  1401. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  1402. struct asus_wmi *asus;
  1403. const char *chassis_type;
  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. /* Some Asus desktop boards export an acpi-video backlight interface,
  1432. stop this from showing up */
  1433. chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
  1434. if (chassis_type && !strcmp(chassis_type, "3"))
  1435. acpi_video_dmi_promote_vendor();
  1436. if (asus->driver->quirks->wmi_backlight_power)
  1437. acpi_video_dmi_promote_vendor();
  1438. if (!acpi_video_backlight_support()) {
  1439. pr_info("Disabling ACPI video driver\n");
  1440. acpi_video_unregister();
  1441. err = asus_wmi_backlight_init(asus);
  1442. if (err && err != -ENODEV)
  1443. goto fail_backlight;
  1444. } else
  1445. pr_info("Backlight controlled by ACPI video driver\n");
  1446. status = wmi_install_notify_handler(asus->driver->event_guid,
  1447. asus_wmi_notify, asus);
  1448. if (ACPI_FAILURE(status)) {
  1449. pr_err("Unable to register notify handler - %d\n", status);
  1450. err = -ENODEV;
  1451. goto fail_wmi_handler;
  1452. }
  1453. err = asus_wmi_debugfs_init(asus);
  1454. if (err)
  1455. goto fail_debugfs;
  1456. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
  1457. if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
  1458. asus->driver->wlan_ctrl_by_user = 1;
  1459. return 0;
  1460. fail_debugfs:
  1461. wmi_remove_notify_handler(asus->driver->event_guid);
  1462. fail_wmi_handler:
  1463. asus_wmi_backlight_exit(asus);
  1464. fail_backlight:
  1465. asus_wmi_rfkill_exit(asus);
  1466. fail_rfkill:
  1467. asus_wmi_led_exit(asus);
  1468. fail_leds:
  1469. fail_hwmon:
  1470. asus_wmi_input_exit(asus);
  1471. fail_input:
  1472. asus_wmi_platform_exit(asus);
  1473. fail_platform:
  1474. kfree(asus);
  1475. return err;
  1476. }
  1477. static int asus_wmi_remove(struct platform_device *device)
  1478. {
  1479. struct asus_wmi *asus;
  1480. asus = platform_get_drvdata(device);
  1481. wmi_remove_notify_handler(asus->driver->event_guid);
  1482. asus_wmi_backlight_exit(asus);
  1483. asus_wmi_input_exit(asus);
  1484. asus_wmi_led_exit(asus);
  1485. asus_wmi_rfkill_exit(asus);
  1486. asus_wmi_debugfs_exit(asus);
  1487. asus_wmi_platform_exit(asus);
  1488. kfree(asus);
  1489. return 0;
  1490. }
  1491. /*
  1492. * Platform driver - hibernate/resume callbacks
  1493. */
  1494. static int asus_hotk_thaw(struct device *device)
  1495. {
  1496. struct asus_wmi *asus = dev_get_drvdata(device);
  1497. if (asus->wlan.rfkill) {
  1498. bool wlan;
  1499. /*
  1500. * Work around bios bug - acpi _PTS turns off the wireless led
  1501. * during suspend. Normally it restores it on resume, but
  1502. * we should kick it ourselves in case hibernation is aborted.
  1503. */
  1504. wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  1505. asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
  1506. }
  1507. return 0;
  1508. }
  1509. static int asus_hotk_restore(struct device *device)
  1510. {
  1511. struct asus_wmi *asus = dev_get_drvdata(device);
  1512. int bl;
  1513. /* Refresh both wlan rfkill state and pci hotplug */
  1514. if (asus->wlan.rfkill)
  1515. asus_rfkill_hotplug(asus);
  1516. if (asus->bluetooth.rfkill) {
  1517. bl = !asus_wmi_get_devstate_simple(asus,
  1518. ASUS_WMI_DEVID_BLUETOOTH);
  1519. rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
  1520. }
  1521. if (asus->wimax.rfkill) {
  1522. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
  1523. rfkill_set_sw_state(asus->wimax.rfkill, bl);
  1524. }
  1525. if (asus->wwan3g.rfkill) {
  1526. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
  1527. rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
  1528. }
  1529. if (asus->gps.rfkill) {
  1530. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
  1531. rfkill_set_sw_state(asus->gps.rfkill, bl);
  1532. }
  1533. if (asus->uwb.rfkill) {
  1534. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
  1535. rfkill_set_sw_state(asus->uwb.rfkill, bl);
  1536. }
  1537. return 0;
  1538. }
  1539. static const struct dev_pm_ops asus_pm_ops = {
  1540. .thaw = asus_hotk_thaw,
  1541. .restore = asus_hotk_restore,
  1542. };
  1543. static int asus_wmi_probe(struct platform_device *pdev)
  1544. {
  1545. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  1546. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  1547. int ret;
  1548. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  1549. pr_warn("Management GUID not found\n");
  1550. return -ENODEV;
  1551. }
  1552. if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
  1553. pr_warn("Event GUID not found\n");
  1554. return -ENODEV;
  1555. }
  1556. if (wdrv->probe) {
  1557. ret = wdrv->probe(pdev);
  1558. if (ret)
  1559. return ret;
  1560. }
  1561. return asus_wmi_add(pdev);
  1562. }
  1563. static bool used;
  1564. int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
  1565. {
  1566. struct platform_driver *platform_driver;
  1567. struct platform_device *platform_device;
  1568. if (used)
  1569. return -EBUSY;
  1570. platform_driver = &driver->platform_driver;
  1571. platform_driver->remove = asus_wmi_remove;
  1572. platform_driver->driver.owner = driver->owner;
  1573. platform_driver->driver.name = driver->name;
  1574. platform_driver->driver.pm = &asus_pm_ops;
  1575. platform_device = platform_create_bundle(platform_driver,
  1576. asus_wmi_probe,
  1577. NULL, 0, NULL, 0);
  1578. if (IS_ERR(platform_device))
  1579. return PTR_ERR(platform_device);
  1580. used = true;
  1581. return 0;
  1582. }
  1583. EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
  1584. void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
  1585. {
  1586. platform_device_unregister(driver->platform_device);
  1587. platform_driver_unregister(&driver->platform_driver);
  1588. used = false;
  1589. }
  1590. EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
  1591. static int __init asus_wmi_init(void)
  1592. {
  1593. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  1594. pr_info("Asus Management GUID not found\n");
  1595. return -ENODEV;
  1596. }
  1597. pr_info("ASUS WMI generic driver loaded\n");
  1598. return 0;
  1599. }
  1600. static void __exit asus_wmi_exit(void)
  1601. {
  1602. pr_info("ASUS WMI generic driver unloaded\n");
  1603. }
  1604. module_init(asus_wmi_init);
  1605. module_exit(asus_wmi_exit);