asus-wmi.c 56 KB

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