asus-wmi.c 49 KB

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