asus-wmi.c 55 KB

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