asus-wmi.c 57 KB

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