asus-wmi.c 58 KB

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