asus-wmi.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Asus PC WMI hotkey driver
  4. *
  5. * Copyright(C) 2010 Intel Corporation.
  6. * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
  7. *
  8. * Portions based on wistron_btns.c:
  9. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  10. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  11. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/input.h>
  20. #include <linux/input/sparse-keymap.h>
  21. #include <linux/fb.h>
  22. #include <linux/backlight.h>
  23. #include <linux/leds.h>
  24. #include <linux/rfkill.h>
  25. #include <linux/pci.h>
  26. #include <linux/pci_hotplug.h>
  27. #include <linux/power_supply.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/platform_data/x86/asus-wmi.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/acpi.h>
  35. #include <linux/dmi.h>
  36. #include <linux/units.h>
  37. #include <acpi/battery.h>
  38. #include <acpi/video.h>
  39. #include "asus-wmi.h"
  40. MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
  41. "Yong Wang <yong.y.wang@intel.com>");
  42. MODULE_DESCRIPTION("Asus Generic WMI Driver");
  43. MODULE_LICENSE("GPL");
  44. #define to_asus_wmi_driver(pdrv) \
  45. (container_of((pdrv), struct asus_wmi_driver, platform_driver))
  46. #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
  47. #define NOTIFY_BRNUP_MIN 0x11
  48. #define NOTIFY_BRNUP_MAX 0x1f
  49. #define NOTIFY_BRNDOWN_MIN 0x20
  50. #define NOTIFY_BRNDOWN_MAX 0x2e
  51. #define NOTIFY_FNLOCK_TOGGLE 0x4e
  52. #define NOTIFY_KBD_DOCK_CHANGE 0x75
  53. #define NOTIFY_KBD_BRTUP 0xc4
  54. #define NOTIFY_KBD_BRTDWN 0xc5
  55. #define NOTIFY_KBD_BRTTOGGLE 0xc7
  56. #define NOTIFY_KBD_FBM 0x99
  57. #define NOTIFY_KBD_TTP 0xae
  58. #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
  59. #define ASUS_FAN_DESC "cpu_fan"
  60. #define ASUS_FAN_MFUN 0x13
  61. #define ASUS_FAN_SFUN_READ 0x06
  62. #define ASUS_FAN_SFUN_WRITE 0x07
  63. /* Based on standard hwmon pwmX_enable values */
  64. #define ASUS_FAN_CTRL_FULLSPEED 0
  65. #define ASUS_FAN_CTRL_MANUAL 1
  66. #define ASUS_FAN_CTRL_AUTO 2
  67. #define ASUS_FAN_BOOST_MODE_NORMAL 0
  68. #define ASUS_FAN_BOOST_MODE_OVERBOOST 1
  69. #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
  70. #define ASUS_FAN_BOOST_MODE_SILENT 2
  71. #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
  72. #define ASUS_FAN_BOOST_MODES_MASK 0x03
  73. #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
  74. #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
  75. #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
  76. #define USB_INTEL_XUSB2PR 0xD0
  77. #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
  78. #define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
  79. #define ASUS_ACPI_UID_ATK "ATK"
  80. #define WMI_EVENT_QUEUE_SIZE 0x10
  81. #define WMI_EVENT_QUEUE_END 0x1
  82. #define WMI_EVENT_MASK 0xFFFF
  83. /* The WMI hotkey event value is always the same. */
  84. #define WMI_EVENT_VALUE_ATK 0xFF
  85. #define WMI_EVENT_MASK 0xFFFF
  86. static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
  87. static bool ashs_present(void)
  88. {
  89. int i = 0;
  90. while (ashs_ids[i]) {
  91. if (acpi_dev_found(ashs_ids[i++]))
  92. return true;
  93. }
  94. return false;
  95. }
  96. struct bios_args {
  97. u32 arg0;
  98. u32 arg1;
  99. u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
  100. u32 arg4;
  101. u32 arg5;
  102. } __packed;
  103. /*
  104. * Struct that's used for all methods called via AGFN. Naming is
  105. * identically to the AML code.
  106. */
  107. struct agfn_args {
  108. u16 mfun; /* probably "Multi-function" to be called */
  109. u16 sfun; /* probably "Sub-function" to be called */
  110. u16 len; /* size of the hole struct, including subfunction fields */
  111. u8 stas; /* not used by now */
  112. u8 err; /* zero on success */
  113. } __packed;
  114. /* struct used for calling fan read and write methods */
  115. struct agfn_fan_args {
  116. struct agfn_args agfn; /* common fields */
  117. u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
  118. u32 speed; /* read: RPM/100 - write: 0-255 */
  119. } __packed;
  120. /*
  121. * <platform>/ - debugfs root directory
  122. * dev_id - current dev_id
  123. * ctrl_param - current ctrl_param
  124. * method_id - current method_id
  125. * devs - call DEVS(dev_id, ctrl_param) and print result
  126. * dsts - call DSTS(dev_id) and print result
  127. * call - call method_id(dev_id, ctrl_param) and print result
  128. */
  129. struct asus_wmi_debug {
  130. struct dentry *root;
  131. u32 method_id;
  132. u32 dev_id;
  133. u32 ctrl_param;
  134. };
  135. struct asus_rfkill {
  136. struct asus_wmi *asus;
  137. struct rfkill *rfkill;
  138. u32 dev_id;
  139. };
  140. enum fan_type {
  141. FAN_TYPE_NONE = 0,
  142. FAN_TYPE_AGFN, /* deprecated on newer platforms */
  143. FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
  144. };
  145. struct asus_wmi {
  146. int dsts_id;
  147. int spec;
  148. int sfun;
  149. bool wmi_event_queue;
  150. struct input_dev *inputdev;
  151. struct backlight_device *backlight_device;
  152. struct platform_device *platform_device;
  153. struct led_classdev wlan_led;
  154. int wlan_led_wk;
  155. struct led_classdev tpd_led;
  156. int tpd_led_wk;
  157. struct led_classdev kbd_led;
  158. int kbd_led_wk;
  159. struct led_classdev lightbar_led;
  160. int lightbar_led_wk;
  161. struct workqueue_struct *led_workqueue;
  162. struct work_struct tpd_led_work;
  163. struct work_struct wlan_led_work;
  164. struct work_struct lightbar_led_work;
  165. struct asus_rfkill wlan;
  166. struct asus_rfkill bluetooth;
  167. struct asus_rfkill wimax;
  168. struct asus_rfkill wwan3g;
  169. struct asus_rfkill gps;
  170. struct asus_rfkill uwb;
  171. enum fan_type fan_type;
  172. int fan_pwm_mode;
  173. int agfn_pwm;
  174. bool fan_boost_mode_available;
  175. u8 fan_boost_mode_mask;
  176. u8 fan_boost_mode;
  177. bool throttle_thermal_policy_available;
  178. u8 throttle_thermal_policy_mode;
  179. // The RSOC controls the maximum charging percentage.
  180. bool battery_rsoc_available;
  181. struct hotplug_slot hotplug_slot;
  182. struct mutex hotplug_lock;
  183. struct mutex wmi_lock;
  184. struct workqueue_struct *hotplug_workqueue;
  185. struct work_struct hotplug_work;
  186. bool fnlock_locked;
  187. struct asus_wmi_debug debug;
  188. struct asus_wmi_driver *driver;
  189. };
  190. /* WMI ************************************************************************/
  191. static int asus_wmi_evaluate_method3(u32 method_id,
  192. u32 arg0, u32 arg1, u32 arg2, u32 *retval)
  193. {
  194. struct bios_args args = {
  195. .arg0 = arg0,
  196. .arg1 = arg1,
  197. .arg2 = arg2,
  198. };
  199. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  200. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  201. acpi_status status;
  202. union acpi_object *obj;
  203. u32 tmp = 0;
  204. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
  205. &input, &output);
  206. if (ACPI_FAILURE(status))
  207. return -EIO;
  208. obj = (union acpi_object *)output.pointer;
  209. if (obj && obj->type == ACPI_TYPE_INTEGER)
  210. tmp = (u32) obj->integer.value;
  211. if (retval)
  212. *retval = tmp;
  213. kfree(obj);
  214. if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
  215. return -ENODEV;
  216. return 0;
  217. }
  218. int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
  219. {
  220. return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
  221. }
  222. EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
  223. static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
  224. {
  225. struct acpi_buffer input;
  226. u64 phys_addr;
  227. u32 retval;
  228. u32 status;
  229. /*
  230. * Copy to dma capable address otherwise memory corruption occurs as
  231. * bios has to be able to access it.
  232. */
  233. input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
  234. input.length = args.length;
  235. if (!input.pointer)
  236. return -ENOMEM;
  237. phys_addr = virt_to_phys(input.pointer);
  238. status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
  239. phys_addr, 0, &retval);
  240. if (!status)
  241. memcpy(args.pointer, input.pointer, args.length);
  242. kfree(input.pointer);
  243. if (status)
  244. return -ENXIO;
  245. return retval;
  246. }
  247. static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
  248. {
  249. return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
  250. }
  251. static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
  252. u32 *retval)
  253. {
  254. return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
  255. ctrl_param, retval);
  256. }
  257. /* Helper for special devices with magic return codes */
  258. static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
  259. u32 dev_id, u32 mask)
  260. {
  261. u32 retval = 0;
  262. int err;
  263. err = asus_wmi_get_devstate(asus, dev_id, &retval);
  264. if (err < 0)
  265. return err;
  266. if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
  267. return -ENODEV;
  268. if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
  269. if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
  270. return -ENODEV;
  271. }
  272. return retval & mask;
  273. }
  274. static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
  275. {
  276. return asus_wmi_get_devstate_bits(asus, dev_id,
  277. ASUS_WMI_DSTS_STATUS_BIT);
  278. }
  279. static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
  280. {
  281. u32 retval;
  282. int status = asus_wmi_get_devstate(asus, dev_id, &retval);
  283. return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
  284. }
  285. /* Input **********************************************************************/
  286. static int asus_wmi_input_init(struct asus_wmi *asus)
  287. {
  288. int err, result;
  289. asus->inputdev = input_allocate_device();
  290. if (!asus->inputdev)
  291. return -ENOMEM;
  292. asus->inputdev->name = asus->driver->input_name;
  293. asus->inputdev->phys = asus->driver->input_phys;
  294. asus->inputdev->id.bustype = BUS_HOST;
  295. asus->inputdev->dev.parent = &asus->platform_device->dev;
  296. set_bit(EV_REP, asus->inputdev->evbit);
  297. err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
  298. if (err)
  299. goto err_free_dev;
  300. if (asus->driver->quirks->use_kbd_dock_devid) {
  301. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
  302. if (result >= 0) {
  303. input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
  304. input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
  305. } else if (result != -ENODEV) {
  306. pr_err("Error checking for keyboard-dock: %d\n", result);
  307. }
  308. }
  309. err = input_register_device(asus->inputdev);
  310. if (err)
  311. goto err_free_dev;
  312. return 0;
  313. err_free_dev:
  314. input_free_device(asus->inputdev);
  315. return err;
  316. }
  317. static void asus_wmi_input_exit(struct asus_wmi *asus)
  318. {
  319. if (asus->inputdev)
  320. input_unregister_device(asus->inputdev);
  321. asus->inputdev = NULL;
  322. }
  323. /* Battery ********************************************************************/
  324. /* The battery maximum charging percentage */
  325. static int charge_end_threshold;
  326. static ssize_t charge_control_end_threshold_store(struct device *dev,
  327. struct device_attribute *attr,
  328. const char *buf, size_t count)
  329. {
  330. int value, ret, rv;
  331. ret = kstrtouint(buf, 10, &value);
  332. if (ret)
  333. return ret;
  334. if (value < 0 || value > 100)
  335. return -EINVAL;
  336. ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
  337. if (ret)
  338. return ret;
  339. if (rv != 1)
  340. return -EIO;
  341. /* There isn't any method in the DSDT to read the threshold, so we
  342. * save the threshold.
  343. */
  344. charge_end_threshold = value;
  345. return count;
  346. }
  347. static ssize_t charge_control_end_threshold_show(struct device *device,
  348. struct device_attribute *attr,
  349. char *buf)
  350. {
  351. return sprintf(buf, "%d\n", charge_end_threshold);
  352. }
  353. static DEVICE_ATTR_RW(charge_control_end_threshold);
  354. static int asus_wmi_battery_add(struct power_supply *battery)
  355. {
  356. /* The WMI method does not provide a way to specific a battery, so we
  357. * just assume it is the first battery.
  358. * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
  359. * battery is named BATT.
  360. */
  361. if (strcmp(battery->desc->name, "BAT0") != 0 &&
  362. strcmp(battery->desc->name, "BAT1") != 0 &&
  363. strcmp(battery->desc->name, "BATC") != 0 &&
  364. strcmp(battery->desc->name, "BATT") != 0)
  365. return -ENODEV;
  366. if (device_create_file(&battery->dev,
  367. &dev_attr_charge_control_end_threshold))
  368. return -ENODEV;
  369. /* The charge threshold is only reset when the system is power cycled,
  370. * and we can't get the current threshold so let set it to 100% when
  371. * a battery is added.
  372. */
  373. asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
  374. charge_end_threshold = 100;
  375. return 0;
  376. }
  377. static int asus_wmi_battery_remove(struct power_supply *battery)
  378. {
  379. device_remove_file(&battery->dev,
  380. &dev_attr_charge_control_end_threshold);
  381. return 0;
  382. }
  383. static struct acpi_battery_hook battery_hook = {
  384. .add_battery = asus_wmi_battery_add,
  385. .remove_battery = asus_wmi_battery_remove,
  386. .name = "ASUS Battery Extension",
  387. };
  388. static void asus_wmi_battery_init(struct asus_wmi *asus)
  389. {
  390. asus->battery_rsoc_available = false;
  391. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
  392. asus->battery_rsoc_available = true;
  393. battery_hook_register(&battery_hook);
  394. }
  395. }
  396. static void asus_wmi_battery_exit(struct asus_wmi *asus)
  397. {
  398. if (asus->battery_rsoc_available)
  399. battery_hook_unregister(&battery_hook);
  400. }
  401. /* LEDs ***********************************************************************/
  402. /*
  403. * These functions actually update the LED's, and are called from a
  404. * workqueue. By doing this as separate work rather than when the LED
  405. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  406. * potentially bad time, such as a timer interrupt.
  407. */
  408. static void tpd_led_update(struct work_struct *work)
  409. {
  410. int ctrl_param;
  411. struct asus_wmi *asus;
  412. asus = container_of(work, struct asus_wmi, tpd_led_work);
  413. ctrl_param = asus->tpd_led_wk;
  414. asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
  415. }
  416. static void tpd_led_set(struct led_classdev *led_cdev,
  417. enum led_brightness value)
  418. {
  419. struct asus_wmi *asus;
  420. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  421. asus->tpd_led_wk = !!value;
  422. queue_work(asus->led_workqueue, &asus->tpd_led_work);
  423. }
  424. static int read_tpd_led_state(struct asus_wmi *asus)
  425. {
  426. return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
  427. }
  428. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  429. {
  430. struct asus_wmi *asus;
  431. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  432. return read_tpd_led_state(asus);
  433. }
  434. static void kbd_led_update(struct asus_wmi *asus)
  435. {
  436. int ctrl_param = 0;
  437. ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
  438. asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
  439. }
  440. static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
  441. {
  442. int retval;
  443. /*
  444. * bits 0-2: level
  445. * bit 7: light on/off
  446. * bit 8-10: environment (0: dark, 1: normal, 2: light)
  447. * bit 17: status unknown
  448. */
  449. retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
  450. 0xFFFF);
  451. /* Unknown status is considered as off */
  452. if (retval == 0x8000)
  453. retval = 0;
  454. if (retval < 0)
  455. return retval;
  456. if (level)
  457. *level = retval & 0x7F;
  458. if (env)
  459. *env = (retval >> 8) & 0x7F;
  460. return 0;
  461. }
  462. static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
  463. {
  464. struct asus_wmi *asus;
  465. int max_level;
  466. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  467. max_level = asus->kbd_led.max_brightness;
  468. asus->kbd_led_wk = clamp_val(value, 0, max_level);
  469. kbd_led_update(asus);
  470. }
  471. static void kbd_led_set(struct led_classdev *led_cdev,
  472. enum led_brightness value)
  473. {
  474. /* Prevent disabling keyboard backlight on module unregister */
  475. if (led_cdev->flags & LED_UNREGISTERING)
  476. return;
  477. do_kbd_led_set(led_cdev, value);
  478. }
  479. static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
  480. {
  481. struct led_classdev *led_cdev = &asus->kbd_led;
  482. do_kbd_led_set(led_cdev, value);
  483. led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
  484. }
  485. static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
  486. {
  487. struct asus_wmi *asus;
  488. int retval, value;
  489. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  490. retval = kbd_led_read(asus, &value, NULL);
  491. if (retval < 0)
  492. return retval;
  493. return value;
  494. }
  495. static int wlan_led_unknown_state(struct asus_wmi *asus)
  496. {
  497. u32 result;
  498. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  499. return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
  500. }
  501. static void wlan_led_update(struct work_struct *work)
  502. {
  503. int ctrl_param;
  504. struct asus_wmi *asus;
  505. asus = container_of(work, struct asus_wmi, wlan_led_work);
  506. ctrl_param = asus->wlan_led_wk;
  507. asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
  508. }
  509. static void wlan_led_set(struct led_classdev *led_cdev,
  510. enum led_brightness value)
  511. {
  512. struct asus_wmi *asus;
  513. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  514. asus->wlan_led_wk = !!value;
  515. queue_work(asus->led_workqueue, &asus->wlan_led_work);
  516. }
  517. static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
  518. {
  519. struct asus_wmi *asus;
  520. u32 result;
  521. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  522. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  523. return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  524. }
  525. static void lightbar_led_update(struct work_struct *work)
  526. {
  527. struct asus_wmi *asus;
  528. int ctrl_param;
  529. asus = container_of(work, struct asus_wmi, lightbar_led_work);
  530. ctrl_param = asus->lightbar_led_wk;
  531. asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
  532. }
  533. static void lightbar_led_set(struct led_classdev *led_cdev,
  534. enum led_brightness value)
  535. {
  536. struct asus_wmi *asus;
  537. asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
  538. asus->lightbar_led_wk = !!value;
  539. queue_work(asus->led_workqueue, &asus->lightbar_led_work);
  540. }
  541. static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
  542. {
  543. struct asus_wmi *asus;
  544. u32 result;
  545. asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
  546. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
  547. return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
  548. }
  549. static void asus_wmi_led_exit(struct asus_wmi *asus)
  550. {
  551. led_classdev_unregister(&asus->kbd_led);
  552. led_classdev_unregister(&asus->tpd_led);
  553. led_classdev_unregister(&asus->wlan_led);
  554. led_classdev_unregister(&asus->lightbar_led);
  555. if (asus->led_workqueue)
  556. destroy_workqueue(asus->led_workqueue);
  557. }
  558. static int asus_wmi_led_init(struct asus_wmi *asus)
  559. {
  560. int rv = 0, led_val;
  561. asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
  562. if (!asus->led_workqueue)
  563. return -ENOMEM;
  564. if (read_tpd_led_state(asus) >= 0) {
  565. INIT_WORK(&asus->tpd_led_work, tpd_led_update);
  566. asus->tpd_led.name = "asus::touchpad";
  567. asus->tpd_led.brightness_set = tpd_led_set;
  568. asus->tpd_led.brightness_get = tpd_led_get;
  569. asus->tpd_led.max_brightness = 1;
  570. rv = led_classdev_register(&asus->platform_device->dev,
  571. &asus->tpd_led);
  572. if (rv)
  573. goto error;
  574. }
  575. if (!kbd_led_read(asus, &led_val, NULL)) {
  576. asus->kbd_led_wk = led_val;
  577. asus->kbd_led.name = "asus::kbd_backlight";
  578. asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
  579. asus->kbd_led.brightness_set = kbd_led_set;
  580. asus->kbd_led.brightness_get = kbd_led_get;
  581. asus->kbd_led.max_brightness = 3;
  582. rv = led_classdev_register(&asus->platform_device->dev,
  583. &asus->kbd_led);
  584. if (rv)
  585. goto error;
  586. }
  587. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
  588. && (asus->driver->quirks->wapf > 0)) {
  589. INIT_WORK(&asus->wlan_led_work, wlan_led_update);
  590. asus->wlan_led.name = "asus::wlan";
  591. asus->wlan_led.brightness_set = wlan_led_set;
  592. if (!wlan_led_unknown_state(asus))
  593. asus->wlan_led.brightness_get = wlan_led_get;
  594. asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
  595. asus->wlan_led.max_brightness = 1;
  596. asus->wlan_led.default_trigger = "asus-wlan";
  597. rv = led_classdev_register(&asus->platform_device->dev,
  598. &asus->wlan_led);
  599. if (rv)
  600. goto error;
  601. }
  602. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
  603. INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
  604. asus->lightbar_led.name = "asus::lightbar";
  605. asus->lightbar_led.brightness_set = lightbar_led_set;
  606. asus->lightbar_led.brightness_get = lightbar_led_get;
  607. asus->lightbar_led.max_brightness = 1;
  608. rv = led_classdev_register(&asus->platform_device->dev,
  609. &asus->lightbar_led);
  610. }
  611. error:
  612. if (rv)
  613. asus_wmi_led_exit(asus);
  614. return rv;
  615. }
  616. /* RF *************************************************************************/
  617. /*
  618. * PCI hotplug (for wlan rfkill)
  619. */
  620. static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
  621. {
  622. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  623. if (result < 0)
  624. return false;
  625. return !result;
  626. }
  627. static void asus_rfkill_hotplug(struct asus_wmi *asus)
  628. {
  629. struct pci_dev *dev;
  630. struct pci_bus *bus;
  631. bool blocked;
  632. bool absent;
  633. u32 l;
  634. mutex_lock(&asus->wmi_lock);
  635. blocked = asus_wlan_rfkill_blocked(asus);
  636. mutex_unlock(&asus->wmi_lock);
  637. mutex_lock(&asus->hotplug_lock);
  638. pci_lock_rescan_remove();
  639. if (asus->wlan.rfkill)
  640. rfkill_set_sw_state(asus->wlan.rfkill, blocked);
  641. if (asus->hotplug_slot.ops) {
  642. bus = pci_find_bus(0, 1);
  643. if (!bus) {
  644. pr_warn("Unable to find PCI bus 1?\n");
  645. goto out_unlock;
  646. }
  647. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  648. pr_err("Unable to read PCI config space?\n");
  649. goto out_unlock;
  650. }
  651. absent = (l == 0xffffffff);
  652. if (blocked != absent) {
  653. pr_warn("BIOS says wireless lan is %s, "
  654. "but the pci device is %s\n",
  655. blocked ? "blocked" : "unblocked",
  656. absent ? "absent" : "present");
  657. pr_warn("skipped wireless hotplug as probably "
  658. "inappropriate for this model\n");
  659. goto out_unlock;
  660. }
  661. if (!blocked) {
  662. dev = pci_get_slot(bus, 0);
  663. if (dev) {
  664. /* Device already present */
  665. pci_dev_put(dev);
  666. goto out_unlock;
  667. }
  668. dev = pci_scan_single_device(bus, 0);
  669. if (dev) {
  670. pci_bus_assign_resources(bus);
  671. pci_bus_add_device(dev);
  672. }
  673. } else {
  674. dev = pci_get_slot(bus, 0);
  675. if (dev) {
  676. pci_stop_and_remove_bus_device(dev);
  677. pci_dev_put(dev);
  678. }
  679. }
  680. }
  681. out_unlock:
  682. pci_unlock_rescan_remove();
  683. mutex_unlock(&asus->hotplug_lock);
  684. }
  685. static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
  686. {
  687. struct asus_wmi *asus = data;
  688. if (event != ACPI_NOTIFY_BUS_CHECK)
  689. return;
  690. /*
  691. * We can't call directly asus_rfkill_hotplug because most
  692. * of the time WMBC is still being executed and not reetrant.
  693. * There is currently no way to tell ACPICA that we want this
  694. * method to be serialized, we schedule a asus_rfkill_hotplug
  695. * call later, in a safer context.
  696. */
  697. queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
  698. }
  699. static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
  700. {
  701. acpi_status status;
  702. acpi_handle handle;
  703. status = acpi_get_handle(NULL, node, &handle);
  704. if (ACPI_FAILURE(status))
  705. return -ENODEV;
  706. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  707. asus_rfkill_notify, asus);
  708. if (ACPI_FAILURE(status))
  709. pr_warn("Failed to register notify on %s\n", node);
  710. return 0;
  711. }
  712. static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
  713. {
  714. acpi_status status = AE_OK;
  715. acpi_handle handle;
  716. status = acpi_get_handle(NULL, node, &handle);
  717. if (ACPI_FAILURE(status))
  718. return;
  719. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  720. asus_rfkill_notify);
  721. if (ACPI_FAILURE(status))
  722. pr_err("Error removing rfkill notify handler %s\n", node);
  723. }
  724. static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
  725. u8 *value)
  726. {
  727. struct asus_wmi *asus = container_of(hotplug_slot,
  728. struct asus_wmi, hotplug_slot);
  729. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  730. if (result < 0)
  731. return result;
  732. *value = !!result;
  733. return 0;
  734. }
  735. static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
  736. .get_adapter_status = asus_get_adapter_status,
  737. .get_power_status = asus_get_adapter_status,
  738. };
  739. static void asus_hotplug_work(struct work_struct *work)
  740. {
  741. struct asus_wmi *asus;
  742. asus = container_of(work, struct asus_wmi, hotplug_work);
  743. asus_rfkill_hotplug(asus);
  744. }
  745. static int asus_setup_pci_hotplug(struct asus_wmi *asus)
  746. {
  747. int ret = -ENOMEM;
  748. struct pci_bus *bus = pci_find_bus(0, 1);
  749. if (!bus) {
  750. pr_err("Unable to find wifi PCI bus\n");
  751. return -ENODEV;
  752. }
  753. asus->hotplug_workqueue =
  754. create_singlethread_workqueue("hotplug_workqueue");
  755. if (!asus->hotplug_workqueue)
  756. goto error_workqueue;
  757. INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
  758. asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
  759. ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
  760. if (ret) {
  761. pr_err("Unable to register hotplug slot - %d\n", ret);
  762. goto error_register;
  763. }
  764. return 0;
  765. error_register:
  766. asus->hotplug_slot.ops = NULL;
  767. destroy_workqueue(asus->hotplug_workqueue);
  768. error_workqueue:
  769. return ret;
  770. }
  771. /*
  772. * Rfkill devices
  773. */
  774. static int asus_rfkill_set(void *data, bool blocked)
  775. {
  776. struct asus_rfkill *priv = data;
  777. u32 ctrl_param = !blocked;
  778. u32 dev_id = priv->dev_id;
  779. /*
  780. * If the user bit is set, BIOS can't set and record the wlan status,
  781. * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
  782. * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
  783. * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
  784. * while setting the wlan status through WMI.
  785. * This is also the behavior that windows app will do.
  786. */
  787. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  788. priv->asus->driver->wlan_ctrl_by_user)
  789. dev_id = ASUS_WMI_DEVID_WLAN_LED;
  790. return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
  791. }
  792. static void asus_rfkill_query(struct rfkill *rfkill, void *data)
  793. {
  794. struct asus_rfkill *priv = data;
  795. int result;
  796. result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
  797. if (result < 0)
  798. return;
  799. rfkill_set_sw_state(priv->rfkill, !result);
  800. }
  801. static int asus_rfkill_wlan_set(void *data, bool blocked)
  802. {
  803. struct asus_rfkill *priv = data;
  804. struct asus_wmi *asus = priv->asus;
  805. int ret;
  806. /*
  807. * This handler is enabled only if hotplug is enabled.
  808. * In this case, the asus_wmi_set_devstate() will
  809. * trigger a wmi notification and we need to wait
  810. * this call to finish before being able to call
  811. * any wmi method
  812. */
  813. mutex_lock(&asus->wmi_lock);
  814. ret = asus_rfkill_set(data, blocked);
  815. mutex_unlock(&asus->wmi_lock);
  816. return ret;
  817. }
  818. static const struct rfkill_ops asus_rfkill_wlan_ops = {
  819. .set_block = asus_rfkill_wlan_set,
  820. .query = asus_rfkill_query,
  821. };
  822. static const struct rfkill_ops asus_rfkill_ops = {
  823. .set_block = asus_rfkill_set,
  824. .query = asus_rfkill_query,
  825. };
  826. static int asus_new_rfkill(struct asus_wmi *asus,
  827. struct asus_rfkill *arfkill,
  828. const char *name, enum rfkill_type type, int dev_id)
  829. {
  830. int result = asus_wmi_get_devstate_simple(asus, dev_id);
  831. struct rfkill **rfkill = &arfkill->rfkill;
  832. if (result < 0)
  833. return result;
  834. arfkill->dev_id = dev_id;
  835. arfkill->asus = asus;
  836. if (dev_id == ASUS_WMI_DEVID_WLAN &&
  837. asus->driver->quirks->hotplug_wireless)
  838. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  839. &asus_rfkill_wlan_ops, arfkill);
  840. else
  841. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  842. &asus_rfkill_ops, arfkill);
  843. if (!*rfkill)
  844. return -EINVAL;
  845. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  846. (asus->driver->quirks->wapf > 0))
  847. rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
  848. rfkill_init_sw_state(*rfkill, !result);
  849. result = rfkill_register(*rfkill);
  850. if (result) {
  851. rfkill_destroy(*rfkill);
  852. *rfkill = NULL;
  853. return result;
  854. }
  855. return 0;
  856. }
  857. static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
  858. {
  859. if (asus->driver->wlan_ctrl_by_user && ashs_present())
  860. return;
  861. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  862. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  863. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  864. if (asus->wlan.rfkill) {
  865. rfkill_unregister(asus->wlan.rfkill);
  866. rfkill_destroy(asus->wlan.rfkill);
  867. asus->wlan.rfkill = NULL;
  868. }
  869. /*
  870. * Refresh pci hotplug in case the rfkill state was changed after
  871. * asus_unregister_rfkill_notifier()
  872. */
  873. asus_rfkill_hotplug(asus);
  874. if (asus->hotplug_slot.ops)
  875. pci_hp_deregister(&asus->hotplug_slot);
  876. if (asus->hotplug_workqueue)
  877. destroy_workqueue(asus->hotplug_workqueue);
  878. if (asus->bluetooth.rfkill) {
  879. rfkill_unregister(asus->bluetooth.rfkill);
  880. rfkill_destroy(asus->bluetooth.rfkill);
  881. asus->bluetooth.rfkill = NULL;
  882. }
  883. if (asus->wimax.rfkill) {
  884. rfkill_unregister(asus->wimax.rfkill);
  885. rfkill_destroy(asus->wimax.rfkill);
  886. asus->wimax.rfkill = NULL;
  887. }
  888. if (asus->wwan3g.rfkill) {
  889. rfkill_unregister(asus->wwan3g.rfkill);
  890. rfkill_destroy(asus->wwan3g.rfkill);
  891. asus->wwan3g.rfkill = NULL;
  892. }
  893. if (asus->gps.rfkill) {
  894. rfkill_unregister(asus->gps.rfkill);
  895. rfkill_destroy(asus->gps.rfkill);
  896. asus->gps.rfkill = NULL;
  897. }
  898. if (asus->uwb.rfkill) {
  899. rfkill_unregister(asus->uwb.rfkill);
  900. rfkill_destroy(asus->uwb.rfkill);
  901. asus->uwb.rfkill = NULL;
  902. }
  903. }
  904. static int asus_wmi_rfkill_init(struct asus_wmi *asus)
  905. {
  906. int result = 0;
  907. mutex_init(&asus->hotplug_lock);
  908. mutex_init(&asus->wmi_lock);
  909. result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
  910. RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
  911. if (result && result != -ENODEV)
  912. goto exit;
  913. result = asus_new_rfkill(asus, &asus->bluetooth,
  914. "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
  915. ASUS_WMI_DEVID_BLUETOOTH);
  916. if (result && result != -ENODEV)
  917. goto exit;
  918. result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
  919. RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
  920. if (result && result != -ENODEV)
  921. goto exit;
  922. result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
  923. RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
  924. if (result && result != -ENODEV)
  925. goto exit;
  926. result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
  927. RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
  928. if (result && result != -ENODEV)
  929. goto exit;
  930. result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
  931. RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
  932. if (result && result != -ENODEV)
  933. goto exit;
  934. if (!asus->driver->quirks->hotplug_wireless)
  935. goto exit;
  936. result = asus_setup_pci_hotplug(asus);
  937. /*
  938. * If we get -EBUSY then something else is handling the PCI hotplug -
  939. * don't fail in this case
  940. */
  941. if (result == -EBUSY)
  942. result = 0;
  943. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  944. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  945. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  946. /*
  947. * Refresh pci hotplug in case the rfkill state was changed during
  948. * setup.
  949. */
  950. asus_rfkill_hotplug(asus);
  951. exit:
  952. if (result && result != -ENODEV)
  953. asus_wmi_rfkill_exit(asus);
  954. if (result == -ENODEV)
  955. result = 0;
  956. return result;
  957. }
  958. /* Quirks *********************************************************************/
  959. static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
  960. {
  961. struct pci_dev *xhci_pdev;
  962. u32 orig_ports_available;
  963. u32 ports_available = asus->driver->quirks->xusb2pr;
  964. xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  965. PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
  966. NULL);
  967. if (!xhci_pdev)
  968. return;
  969. pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  970. &orig_ports_available);
  971. pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  972. cpu_to_le32(ports_available));
  973. pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
  974. orig_ports_available, ports_available);
  975. }
  976. /*
  977. * Some devices dont support or have borcken get_als method
  978. * but still support set method.
  979. */
  980. static void asus_wmi_set_als(void)
  981. {
  982. asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
  983. }
  984. /* Hwmon device ***************************************************************/
  985. static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
  986. int *speed)
  987. {
  988. struct agfn_fan_args args = {
  989. .agfn.len = sizeof(args),
  990. .agfn.mfun = ASUS_FAN_MFUN,
  991. .agfn.sfun = ASUS_FAN_SFUN_READ,
  992. .fan = fan,
  993. .speed = 0,
  994. };
  995. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  996. int status;
  997. if (fan != 1)
  998. return -EINVAL;
  999. status = asus_wmi_evaluate_method_agfn(input);
  1000. if (status || args.agfn.err)
  1001. return -ENXIO;
  1002. if (speed)
  1003. *speed = args.speed;
  1004. return 0;
  1005. }
  1006. static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
  1007. int *speed)
  1008. {
  1009. struct agfn_fan_args args = {
  1010. .agfn.len = sizeof(args),
  1011. .agfn.mfun = ASUS_FAN_MFUN,
  1012. .agfn.sfun = ASUS_FAN_SFUN_WRITE,
  1013. .fan = fan,
  1014. .speed = speed ? *speed : 0,
  1015. };
  1016. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  1017. int status;
  1018. /* 1: for setting 1st fan's speed 0: setting auto mode */
  1019. if (fan != 1 && fan != 0)
  1020. return -EINVAL;
  1021. status = asus_wmi_evaluate_method_agfn(input);
  1022. if (status || args.agfn.err)
  1023. return -ENXIO;
  1024. if (speed && fan == 1)
  1025. asus->agfn_pwm = *speed;
  1026. return 0;
  1027. }
  1028. /*
  1029. * Check if we can read the speed of one fan. If true we assume we can also
  1030. * control it.
  1031. */
  1032. static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
  1033. {
  1034. int status;
  1035. int speed;
  1036. u32 value;
  1037. status = asus_agfn_fan_speed_read(asus, 1, &speed);
  1038. if (status != 0)
  1039. return false;
  1040. status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
  1041. if (status != 0)
  1042. return false;
  1043. /*
  1044. * We need to find a better way, probably using sfun,
  1045. * bits or spec ...
  1046. * Currently we disable it if:
  1047. * - ASUS_WMI_UNSUPPORTED_METHOD is returned
  1048. * - reverved bits are non-zero
  1049. * - sfun and presence bit are not set
  1050. */
  1051. return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
  1052. || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
  1053. }
  1054. static int asus_fan_set_auto(struct asus_wmi *asus)
  1055. {
  1056. int status;
  1057. u32 retval;
  1058. switch (asus->fan_type) {
  1059. case FAN_TYPE_SPEC83:
  1060. status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
  1061. 0, &retval);
  1062. if (status)
  1063. return status;
  1064. if (retval != 1)
  1065. return -EIO;
  1066. break;
  1067. case FAN_TYPE_AGFN:
  1068. status = asus_agfn_fan_speed_write(asus, 0, NULL);
  1069. if (status)
  1070. return -ENXIO;
  1071. break;
  1072. default:
  1073. return -ENXIO;
  1074. }
  1075. return 0;
  1076. }
  1077. static ssize_t pwm1_show(struct device *dev,
  1078. struct device_attribute *attr,
  1079. char *buf)
  1080. {
  1081. struct asus_wmi *asus = dev_get_drvdata(dev);
  1082. int err;
  1083. int value;
  1084. /* If we already set a value then just return it */
  1085. if (asus->agfn_pwm >= 0)
  1086. return sprintf(buf, "%d\n", asus->agfn_pwm);
  1087. /*
  1088. * If we haven't set already set a value through the AGFN interface,
  1089. * we read a current value through the (now-deprecated) FAN_CTRL device.
  1090. */
  1091. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
  1092. if (err < 0)
  1093. return err;
  1094. value &= 0xFF;
  1095. if (value == 1) /* Low Speed */
  1096. value = 85;
  1097. else if (value == 2)
  1098. value = 170;
  1099. else if (value == 3)
  1100. value = 255;
  1101. else if (value) {
  1102. pr_err("Unknown fan speed %#x\n", value);
  1103. value = -1;
  1104. }
  1105. return sprintf(buf, "%d\n", value);
  1106. }
  1107. static ssize_t pwm1_store(struct device *dev,
  1108. struct device_attribute *attr,
  1109. const char *buf, size_t count) {
  1110. struct asus_wmi *asus = dev_get_drvdata(dev);
  1111. int value;
  1112. int state;
  1113. int ret;
  1114. ret = kstrtouint(buf, 10, &value);
  1115. if (ret)
  1116. return ret;
  1117. value = clamp(value, 0, 255);
  1118. state = asus_agfn_fan_speed_write(asus, 1, &value);
  1119. if (state)
  1120. pr_warn("Setting fan speed failed: %d\n", state);
  1121. else
  1122. asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
  1123. return count;
  1124. }
  1125. static ssize_t fan1_input_show(struct device *dev,
  1126. struct device_attribute *attr,
  1127. char *buf)
  1128. {
  1129. struct asus_wmi *asus = dev_get_drvdata(dev);
  1130. int value;
  1131. int ret;
  1132. switch (asus->fan_type) {
  1133. case FAN_TYPE_SPEC83:
  1134. ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
  1135. &value);
  1136. if (ret < 0)
  1137. return ret;
  1138. value &= 0xffff;
  1139. break;
  1140. case FAN_TYPE_AGFN:
  1141. /* no speed readable on manual mode */
  1142. if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
  1143. return -ENXIO;
  1144. ret = asus_agfn_fan_speed_read(asus, 1, &value);
  1145. if (ret) {
  1146. pr_warn("reading fan speed failed: %d\n", ret);
  1147. return -ENXIO;
  1148. }
  1149. break;
  1150. default:
  1151. return -ENXIO;
  1152. }
  1153. return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
  1154. }
  1155. static ssize_t pwm1_enable_show(struct device *dev,
  1156. struct device_attribute *attr,
  1157. char *buf)
  1158. {
  1159. struct asus_wmi *asus = dev_get_drvdata(dev);
  1160. /*
  1161. * Just read back the cached pwm mode.
  1162. *
  1163. * For the CPU_FAN device, the spec indicates that we should be
  1164. * able to read the device status and consult bit 19 to see if we
  1165. * are in Full On or Automatic mode. However, this does not work
  1166. * in practice on X532FL at least (the bit is always 0) and there's
  1167. * also nothing in the DSDT to indicate that this behaviour exists.
  1168. */
  1169. return sprintf(buf, "%d\n", asus->fan_pwm_mode);
  1170. }
  1171. static ssize_t pwm1_enable_store(struct device *dev,
  1172. struct device_attribute *attr,
  1173. const char *buf, size_t count)
  1174. {
  1175. struct asus_wmi *asus = dev_get_drvdata(dev);
  1176. int status = 0;
  1177. int state;
  1178. int value;
  1179. int ret;
  1180. u32 retval;
  1181. ret = kstrtouint(buf, 10, &state);
  1182. if (ret)
  1183. return ret;
  1184. if (asus->fan_type == FAN_TYPE_SPEC83) {
  1185. switch (state) { /* standard documented hwmon values */
  1186. case ASUS_FAN_CTRL_FULLSPEED:
  1187. value = 1;
  1188. break;
  1189. case ASUS_FAN_CTRL_AUTO:
  1190. value = 0;
  1191. break;
  1192. default:
  1193. return -EINVAL;
  1194. }
  1195. ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
  1196. value, &retval);
  1197. if (ret)
  1198. return ret;
  1199. if (retval != 1)
  1200. return -EIO;
  1201. } else if (asus->fan_type == FAN_TYPE_AGFN) {
  1202. switch (state) {
  1203. case ASUS_FAN_CTRL_MANUAL:
  1204. break;
  1205. case ASUS_FAN_CTRL_AUTO:
  1206. status = asus_fan_set_auto(asus);
  1207. if (status)
  1208. return status;
  1209. break;
  1210. default:
  1211. return -EINVAL;
  1212. }
  1213. }
  1214. asus->fan_pwm_mode = state;
  1215. return count;
  1216. }
  1217. static ssize_t fan1_label_show(struct device *dev,
  1218. struct device_attribute *attr,
  1219. char *buf)
  1220. {
  1221. return sprintf(buf, "%s\n", ASUS_FAN_DESC);
  1222. }
  1223. static ssize_t asus_hwmon_temp1(struct device *dev,
  1224. struct device_attribute *attr,
  1225. char *buf)
  1226. {
  1227. struct asus_wmi *asus = dev_get_drvdata(dev);
  1228. u32 value;
  1229. int err;
  1230. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
  1231. if (err < 0)
  1232. return err;
  1233. return sprintf(buf, "%ld\n",
  1234. deci_kelvin_to_millicelsius(value & 0xFFFF));
  1235. }
  1236. /* Fan1 */
  1237. static DEVICE_ATTR_RW(pwm1);
  1238. static DEVICE_ATTR_RW(pwm1_enable);
  1239. static DEVICE_ATTR_RO(fan1_input);
  1240. static DEVICE_ATTR_RO(fan1_label);
  1241. /* Temperature */
  1242. static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
  1243. static struct attribute *hwmon_attributes[] = {
  1244. &dev_attr_pwm1.attr,
  1245. &dev_attr_pwm1_enable.attr,
  1246. &dev_attr_fan1_input.attr,
  1247. &dev_attr_fan1_label.attr,
  1248. &dev_attr_temp1_input.attr,
  1249. NULL
  1250. };
  1251. static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
  1252. struct attribute *attr, int idx)
  1253. {
  1254. struct device *dev = container_of(kobj, struct device, kobj);
  1255. struct asus_wmi *asus = dev_get_drvdata(dev->parent);
  1256. u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
  1257. if (attr == &dev_attr_pwm1.attr) {
  1258. if (asus->fan_type != FAN_TYPE_AGFN)
  1259. return 0;
  1260. } else if (attr == &dev_attr_fan1_input.attr
  1261. || attr == &dev_attr_fan1_label.attr
  1262. || attr == &dev_attr_pwm1_enable.attr) {
  1263. if (asus->fan_type == FAN_TYPE_NONE)
  1264. return 0;
  1265. } else if (attr == &dev_attr_temp1_input.attr) {
  1266. int err = asus_wmi_get_devstate(asus,
  1267. ASUS_WMI_DEVID_THERMAL_CTRL,
  1268. &value);
  1269. if (err < 0)
  1270. return 0; /* can't return negative here */
  1271. /*
  1272. * If the temperature value in deci-Kelvin is near the absolute
  1273. * zero temperature, something is clearly wrong
  1274. */
  1275. if (value == 0 || value == 1)
  1276. return 0;
  1277. }
  1278. return attr->mode;
  1279. }
  1280. static const struct attribute_group hwmon_attribute_group = {
  1281. .is_visible = asus_hwmon_sysfs_is_visible,
  1282. .attrs = hwmon_attributes
  1283. };
  1284. __ATTRIBUTE_GROUPS(hwmon_attribute);
  1285. static int asus_wmi_hwmon_init(struct asus_wmi *asus)
  1286. {
  1287. struct device *dev = &asus->platform_device->dev;
  1288. struct device *hwmon;
  1289. hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
  1290. hwmon_attribute_groups);
  1291. if (IS_ERR(hwmon)) {
  1292. pr_err("Could not register asus hwmon device\n");
  1293. return PTR_ERR(hwmon);
  1294. }
  1295. return 0;
  1296. }
  1297. static int asus_wmi_fan_init(struct asus_wmi *asus)
  1298. {
  1299. asus->fan_type = FAN_TYPE_NONE;
  1300. asus->agfn_pwm = -1;
  1301. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
  1302. asus->fan_type = FAN_TYPE_SPEC83;
  1303. else if (asus_wmi_has_agfn_fan(asus))
  1304. asus->fan_type = FAN_TYPE_AGFN;
  1305. if (asus->fan_type == FAN_TYPE_NONE)
  1306. return -ENODEV;
  1307. asus_fan_set_auto(asus);
  1308. asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
  1309. return 0;
  1310. }
  1311. /* Fan mode *******************************************************************/
  1312. static int fan_boost_mode_check_present(struct asus_wmi *asus)
  1313. {
  1314. u32 result;
  1315. int err;
  1316. asus->fan_boost_mode_available = false;
  1317. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
  1318. &result);
  1319. if (err) {
  1320. if (err == -ENODEV)
  1321. return 0;
  1322. else
  1323. return err;
  1324. }
  1325. if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
  1326. (result & ASUS_FAN_BOOST_MODES_MASK)) {
  1327. asus->fan_boost_mode_available = true;
  1328. asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
  1329. }
  1330. return 0;
  1331. }
  1332. static int fan_boost_mode_write(struct asus_wmi *asus)
  1333. {
  1334. int err;
  1335. u8 value;
  1336. u32 retval;
  1337. value = asus->fan_boost_mode;
  1338. pr_info("Set fan boost mode: %u\n", value);
  1339. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
  1340. &retval);
  1341. if (err) {
  1342. pr_warn("Failed to set fan boost mode: %d\n", err);
  1343. return err;
  1344. }
  1345. if (retval != 1) {
  1346. pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
  1347. retval);
  1348. return -EIO;
  1349. }
  1350. return 0;
  1351. }
  1352. static int fan_boost_mode_switch_next(struct asus_wmi *asus)
  1353. {
  1354. u8 mask = asus->fan_boost_mode_mask;
  1355. if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
  1356. if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
  1357. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
  1358. else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
  1359. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
  1360. } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
  1361. if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
  1362. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
  1363. else
  1364. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
  1365. } else {
  1366. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
  1367. }
  1368. return fan_boost_mode_write(asus);
  1369. }
  1370. static ssize_t fan_boost_mode_show(struct device *dev,
  1371. struct device_attribute *attr, char *buf)
  1372. {
  1373. struct asus_wmi *asus = dev_get_drvdata(dev);
  1374. return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
  1375. }
  1376. static ssize_t fan_boost_mode_store(struct device *dev,
  1377. struct device_attribute *attr,
  1378. const char *buf, size_t count)
  1379. {
  1380. int result;
  1381. u8 new_mode;
  1382. struct asus_wmi *asus = dev_get_drvdata(dev);
  1383. u8 mask = asus->fan_boost_mode_mask;
  1384. result = kstrtou8(buf, 10, &new_mode);
  1385. if (result < 0) {
  1386. pr_warn("Trying to store invalid value\n");
  1387. return result;
  1388. }
  1389. if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
  1390. if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
  1391. return -EINVAL;
  1392. } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
  1393. if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
  1394. return -EINVAL;
  1395. } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
  1396. return -EINVAL;
  1397. }
  1398. asus->fan_boost_mode = new_mode;
  1399. fan_boost_mode_write(asus);
  1400. return count;
  1401. }
  1402. // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
  1403. static DEVICE_ATTR_RW(fan_boost_mode);
  1404. /* Throttle thermal policy ****************************************************/
  1405. static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
  1406. {
  1407. u32 result;
  1408. int err;
  1409. asus->throttle_thermal_policy_available = false;
  1410. err = asus_wmi_get_devstate(asus,
  1411. ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
  1412. &result);
  1413. if (err) {
  1414. if (err == -ENODEV)
  1415. return 0;
  1416. return err;
  1417. }
  1418. if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
  1419. asus->throttle_thermal_policy_available = true;
  1420. return 0;
  1421. }
  1422. static int throttle_thermal_policy_write(struct asus_wmi *asus)
  1423. {
  1424. int err;
  1425. u8 value;
  1426. u32 retval;
  1427. value = asus->throttle_thermal_policy_mode;
  1428. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
  1429. value, &retval);
  1430. if (err) {
  1431. pr_warn("Failed to set throttle thermal policy: %d\n", err);
  1432. return err;
  1433. }
  1434. if (retval != 1) {
  1435. pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
  1436. retval);
  1437. return -EIO;
  1438. }
  1439. return 0;
  1440. }
  1441. static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
  1442. {
  1443. if (!asus->throttle_thermal_policy_available)
  1444. return 0;
  1445. asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
  1446. return throttle_thermal_policy_write(asus);
  1447. }
  1448. static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
  1449. {
  1450. u8 new_mode = asus->throttle_thermal_policy_mode + 1;
  1451. if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
  1452. new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
  1453. asus->throttle_thermal_policy_mode = new_mode;
  1454. return throttle_thermal_policy_write(asus);
  1455. }
  1456. static ssize_t throttle_thermal_policy_show(struct device *dev,
  1457. struct device_attribute *attr, char *buf)
  1458. {
  1459. struct asus_wmi *asus = dev_get_drvdata(dev);
  1460. u8 mode = asus->throttle_thermal_policy_mode;
  1461. return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
  1462. }
  1463. static ssize_t throttle_thermal_policy_store(struct device *dev,
  1464. struct device_attribute *attr,
  1465. const char *buf, size_t count)
  1466. {
  1467. int result;
  1468. u8 new_mode;
  1469. struct asus_wmi *asus = dev_get_drvdata(dev);
  1470. result = kstrtou8(buf, 10, &new_mode);
  1471. if (result < 0)
  1472. return result;
  1473. if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
  1474. return -EINVAL;
  1475. asus->throttle_thermal_policy_mode = new_mode;
  1476. throttle_thermal_policy_write(asus);
  1477. return count;
  1478. }
  1479. // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
  1480. static DEVICE_ATTR_RW(throttle_thermal_policy);
  1481. /* Backlight ******************************************************************/
  1482. static int read_backlight_power(struct asus_wmi *asus)
  1483. {
  1484. int ret;
  1485. if (asus->driver->quirks->store_backlight_power)
  1486. ret = !asus->driver->panel_power;
  1487. else
  1488. ret = asus_wmi_get_devstate_simple(asus,
  1489. ASUS_WMI_DEVID_BACKLIGHT);
  1490. if (ret < 0)
  1491. return ret;
  1492. return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  1493. }
  1494. static int read_brightness_max(struct asus_wmi *asus)
  1495. {
  1496. u32 retval;
  1497. int err;
  1498. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  1499. if (err < 0)
  1500. return err;
  1501. retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
  1502. retval >>= 8;
  1503. if (!retval)
  1504. return -ENODEV;
  1505. return retval;
  1506. }
  1507. static int read_brightness(struct backlight_device *bd)
  1508. {
  1509. struct asus_wmi *asus = bl_get_data(bd);
  1510. u32 retval;
  1511. int err;
  1512. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  1513. if (err < 0)
  1514. return err;
  1515. return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  1516. }
  1517. static u32 get_scalar_command(struct backlight_device *bd)
  1518. {
  1519. struct asus_wmi *asus = bl_get_data(bd);
  1520. u32 ctrl_param = 0;
  1521. if ((asus->driver->brightness < bd->props.brightness) ||
  1522. bd->props.brightness == bd->props.max_brightness)
  1523. ctrl_param = 0x00008001;
  1524. else if ((asus->driver->brightness > bd->props.brightness) ||
  1525. bd->props.brightness == 0)
  1526. ctrl_param = 0x00008000;
  1527. asus->driver->brightness = bd->props.brightness;
  1528. return ctrl_param;
  1529. }
  1530. static int update_bl_status(struct backlight_device *bd)
  1531. {
  1532. struct asus_wmi *asus = bl_get_data(bd);
  1533. u32 ctrl_param;
  1534. int power, err = 0;
  1535. power = read_backlight_power(asus);
  1536. if (power != -ENODEV && bd->props.power != power) {
  1537. ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
  1538. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
  1539. ctrl_param, NULL);
  1540. if (asus->driver->quirks->store_backlight_power)
  1541. asus->driver->panel_power = bd->props.power;
  1542. /* When using scalar brightness, updating the brightness
  1543. * will mess with the backlight power */
  1544. if (asus->driver->quirks->scalar_panel_brightness)
  1545. return err;
  1546. }
  1547. if (asus->driver->quirks->scalar_panel_brightness)
  1548. ctrl_param = get_scalar_command(bd);
  1549. else
  1550. ctrl_param = bd->props.brightness;
  1551. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
  1552. ctrl_param, NULL);
  1553. return err;
  1554. }
  1555. static const struct backlight_ops asus_wmi_bl_ops = {
  1556. .get_brightness = read_brightness,
  1557. .update_status = update_bl_status,
  1558. };
  1559. static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
  1560. {
  1561. struct backlight_device *bd = asus->backlight_device;
  1562. int old = bd->props.brightness;
  1563. int new = old;
  1564. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  1565. new = code - NOTIFY_BRNUP_MIN + 1;
  1566. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  1567. new = code - NOTIFY_BRNDOWN_MIN;
  1568. bd->props.brightness = new;
  1569. backlight_update_status(bd);
  1570. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  1571. return old;
  1572. }
  1573. static int asus_wmi_backlight_init(struct asus_wmi *asus)
  1574. {
  1575. struct backlight_device *bd;
  1576. struct backlight_properties props;
  1577. int max;
  1578. int power;
  1579. max = read_brightness_max(asus);
  1580. if (max < 0)
  1581. return max;
  1582. power = read_backlight_power(asus);
  1583. if (power == -ENODEV)
  1584. power = FB_BLANK_UNBLANK;
  1585. else if (power < 0)
  1586. return power;
  1587. memset(&props, 0, sizeof(struct backlight_properties));
  1588. props.type = BACKLIGHT_PLATFORM;
  1589. props.max_brightness = max;
  1590. bd = backlight_device_register(asus->driver->name,
  1591. &asus->platform_device->dev, asus,
  1592. &asus_wmi_bl_ops, &props);
  1593. if (IS_ERR(bd)) {
  1594. pr_err("Could not register backlight device\n");
  1595. return PTR_ERR(bd);
  1596. }
  1597. asus->backlight_device = bd;
  1598. if (asus->driver->quirks->store_backlight_power)
  1599. asus->driver->panel_power = power;
  1600. bd->props.brightness = read_brightness(bd);
  1601. bd->props.power = power;
  1602. backlight_update_status(bd);
  1603. asus->driver->brightness = bd->props.brightness;
  1604. return 0;
  1605. }
  1606. static void asus_wmi_backlight_exit(struct asus_wmi *asus)
  1607. {
  1608. backlight_device_unregister(asus->backlight_device);
  1609. asus->backlight_device = NULL;
  1610. }
  1611. static int is_display_toggle(int code)
  1612. {
  1613. /* display toggle keys */
  1614. if ((code >= 0x61 && code <= 0x67) ||
  1615. (code >= 0x8c && code <= 0x93) ||
  1616. (code >= 0xa0 && code <= 0xa7) ||
  1617. (code >= 0xd0 && code <= 0xd5))
  1618. return 1;
  1619. return 0;
  1620. }
  1621. /* Fn-lock ********************************************************************/
  1622. static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
  1623. {
  1624. u32 result;
  1625. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
  1626. return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
  1627. !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
  1628. }
  1629. static void asus_wmi_fnlock_update(struct asus_wmi *asus)
  1630. {
  1631. int mode = asus->fnlock_locked;
  1632. asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
  1633. }
  1634. /* WMI events *****************************************************************/
  1635. static int asus_wmi_get_event_code(u32 value)
  1636. {
  1637. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  1638. union acpi_object *obj;
  1639. acpi_status status;
  1640. int code;
  1641. status = wmi_get_event_data(value, &response);
  1642. if (ACPI_FAILURE(status)) {
  1643. pr_warn("Failed to get WMI notify code: %s\n",
  1644. acpi_format_exception(status));
  1645. return -EIO;
  1646. }
  1647. obj = (union acpi_object *)response.pointer;
  1648. if (obj && obj->type == ACPI_TYPE_INTEGER)
  1649. code = (int)(obj->integer.value & WMI_EVENT_MASK);
  1650. else
  1651. code = -EIO;
  1652. kfree(obj);
  1653. return code;
  1654. }
  1655. static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
  1656. {
  1657. unsigned int key_value = 1;
  1658. bool autorelease = 1;
  1659. int result, orig_code;
  1660. orig_code = code;
  1661. if (asus->driver->key_filter) {
  1662. asus->driver->key_filter(asus->driver, &code, &key_value,
  1663. &autorelease);
  1664. if (code == ASUS_WMI_KEY_IGNORE)
  1665. return;
  1666. }
  1667. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  1668. code = ASUS_WMI_BRN_UP;
  1669. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  1670. code = ASUS_WMI_BRN_DOWN;
  1671. if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
  1672. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  1673. asus_wmi_backlight_notify(asus, orig_code);
  1674. return;
  1675. }
  1676. }
  1677. if (code == NOTIFY_KBD_BRTUP) {
  1678. kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
  1679. return;
  1680. }
  1681. if (code == NOTIFY_KBD_BRTDWN) {
  1682. kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
  1683. return;
  1684. }
  1685. if (code == NOTIFY_KBD_BRTTOGGLE) {
  1686. if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
  1687. kbd_led_set_by_kbd(asus, 0);
  1688. else
  1689. kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
  1690. return;
  1691. }
  1692. if (code == NOTIFY_FNLOCK_TOGGLE) {
  1693. asus->fnlock_locked = !asus->fnlock_locked;
  1694. asus_wmi_fnlock_update(asus);
  1695. return;
  1696. }
  1697. if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
  1698. result = asus_wmi_get_devstate_simple(asus,
  1699. ASUS_WMI_DEVID_KBD_DOCK);
  1700. if (result >= 0) {
  1701. input_report_switch(asus->inputdev, SW_TABLET_MODE,
  1702. !result);
  1703. input_sync(asus->inputdev);
  1704. }
  1705. return;
  1706. }
  1707. if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
  1708. fan_boost_mode_switch_next(asus);
  1709. return;
  1710. }
  1711. if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
  1712. throttle_thermal_policy_switch_next(asus);
  1713. return;
  1714. }
  1715. if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
  1716. return;
  1717. if (!sparse_keymap_report_event(asus->inputdev, code,
  1718. key_value, autorelease))
  1719. pr_info("Unknown key %x pressed\n", code);
  1720. }
  1721. static void asus_wmi_notify(u32 value, void *context)
  1722. {
  1723. struct asus_wmi *asus = context;
  1724. int code;
  1725. int i;
  1726. for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
  1727. code = asus_wmi_get_event_code(value);
  1728. if (code < 0) {
  1729. pr_warn("Failed to get notify code: %d\n", code);
  1730. return;
  1731. }
  1732. if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
  1733. return;
  1734. asus_wmi_handle_event_code(code, asus);
  1735. /*
  1736. * Double check that queue is present:
  1737. * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
  1738. */
  1739. if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
  1740. return;
  1741. }
  1742. pr_warn("Failed to process event queue, last code: 0x%x\n", code);
  1743. }
  1744. static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
  1745. {
  1746. int code;
  1747. int i;
  1748. for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
  1749. code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
  1750. if (code < 0) {
  1751. pr_warn("Failed to get event during flush: %d\n", code);
  1752. return code;
  1753. }
  1754. if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
  1755. return 0;
  1756. }
  1757. pr_warn("Failed to flush event queue\n");
  1758. return -EIO;
  1759. }
  1760. /* Sysfs **********************************************************************/
  1761. static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
  1762. const char *buf, size_t count)
  1763. {
  1764. u32 retval;
  1765. int err, value;
  1766. value = asus_wmi_get_devstate_simple(asus, devid);
  1767. if (value < 0)
  1768. return value;
  1769. err = kstrtoint(buf, 0, &value);
  1770. if (err)
  1771. return err;
  1772. err = asus_wmi_set_devstate(devid, value, &retval);
  1773. if (err < 0)
  1774. return err;
  1775. return count;
  1776. }
  1777. static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
  1778. {
  1779. int value = asus_wmi_get_devstate_simple(asus, devid);
  1780. if (value < 0)
  1781. return value;
  1782. return sprintf(buf, "%d\n", value);
  1783. }
  1784. #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
  1785. static ssize_t show_##_name(struct device *dev, \
  1786. struct device_attribute *attr, \
  1787. char *buf) \
  1788. { \
  1789. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1790. \
  1791. return show_sys_wmi(asus, _cm, buf); \
  1792. } \
  1793. static ssize_t store_##_name(struct device *dev, \
  1794. struct device_attribute *attr, \
  1795. const char *buf, size_t count) \
  1796. { \
  1797. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1798. \
  1799. return store_sys_wmi(asus, _cm, buf, count); \
  1800. } \
  1801. static struct device_attribute dev_attr_##_name = { \
  1802. .attr = { \
  1803. .name = __stringify(_name), \
  1804. .mode = _mode }, \
  1805. .show = show_##_name, \
  1806. .store = store_##_name, \
  1807. }
  1808. ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
  1809. ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
  1810. ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
  1811. ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
  1812. ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
  1813. static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
  1814. const char *buf, size_t count)
  1815. {
  1816. int value, rv;
  1817. rv = kstrtoint(buf, 0, &value);
  1818. if (rv)
  1819. return rv;
  1820. if (value < 0 || value > 2)
  1821. return -EINVAL;
  1822. rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
  1823. if (rv < 0)
  1824. return rv;
  1825. return count;
  1826. }
  1827. static DEVICE_ATTR_WO(cpufv);
  1828. static struct attribute *platform_attributes[] = {
  1829. &dev_attr_cpufv.attr,
  1830. &dev_attr_camera.attr,
  1831. &dev_attr_cardr.attr,
  1832. &dev_attr_touchpad.attr,
  1833. &dev_attr_lid_resume.attr,
  1834. &dev_attr_als_enable.attr,
  1835. &dev_attr_fan_boost_mode.attr,
  1836. &dev_attr_throttle_thermal_policy.attr,
  1837. NULL
  1838. };
  1839. static umode_t asus_sysfs_is_visible(struct kobject *kobj,
  1840. struct attribute *attr, int idx)
  1841. {
  1842. struct device *dev = container_of(kobj, struct device, kobj);
  1843. struct asus_wmi *asus = dev_get_drvdata(dev);
  1844. bool ok = true;
  1845. int devid = -1;
  1846. if (attr == &dev_attr_camera.attr)
  1847. devid = ASUS_WMI_DEVID_CAMERA;
  1848. else if (attr == &dev_attr_cardr.attr)
  1849. devid = ASUS_WMI_DEVID_CARDREADER;
  1850. else if (attr == &dev_attr_touchpad.attr)
  1851. devid = ASUS_WMI_DEVID_TOUCHPAD;
  1852. else if (attr == &dev_attr_lid_resume.attr)
  1853. devid = ASUS_WMI_DEVID_LID_RESUME;
  1854. else if (attr == &dev_attr_als_enable.attr)
  1855. devid = ASUS_WMI_DEVID_ALS_ENABLE;
  1856. else if (attr == &dev_attr_fan_boost_mode.attr)
  1857. ok = asus->fan_boost_mode_available;
  1858. else if (attr == &dev_attr_throttle_thermal_policy.attr)
  1859. ok = asus->throttle_thermal_policy_available;
  1860. if (devid != -1)
  1861. ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
  1862. return ok ? attr->mode : 0;
  1863. }
  1864. static const struct attribute_group platform_attribute_group = {
  1865. .is_visible = asus_sysfs_is_visible,
  1866. .attrs = platform_attributes
  1867. };
  1868. static void asus_wmi_sysfs_exit(struct platform_device *device)
  1869. {
  1870. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  1871. }
  1872. static int asus_wmi_sysfs_init(struct platform_device *device)
  1873. {
  1874. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  1875. }
  1876. /* Platform device ************************************************************/
  1877. static int asus_wmi_platform_init(struct asus_wmi *asus)
  1878. {
  1879. struct device *dev = &asus->platform_device->dev;
  1880. char *wmi_uid;
  1881. int rv;
  1882. /* INIT enable hotkeys on some models */
  1883. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
  1884. pr_info("Initialization: %#x\n", rv);
  1885. /* We don't know yet what to do with this version... */
  1886. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
  1887. pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
  1888. asus->spec = rv;
  1889. }
  1890. /*
  1891. * The SFUN method probably allows the original driver to get the list
  1892. * of features supported by a given model. For now, 0x0100 or 0x0800
  1893. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  1894. * The significance of others is yet to be found.
  1895. */
  1896. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
  1897. pr_info("SFUN value: %#x\n", rv);
  1898. asus->sfun = rv;
  1899. }
  1900. /*
  1901. * Eee PC and Notebooks seems to have different method_id for DSTS,
  1902. * but it may also be related to the BIOS's SPEC.
  1903. * Note, on most Eeepc, there is no way to check if a method exist
  1904. * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
  1905. * but once again, SPEC may probably be used for that kind of things.
  1906. *
  1907. * Additionally at least TUF Gaming series laptops return nothing for
  1908. * unknown methods, so the detection in this way is not possible.
  1909. *
  1910. * There is strong indication that only ACPI WMI devices that have _UID
  1911. * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
  1912. */
  1913. wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
  1914. if (!wmi_uid)
  1915. return -ENODEV;
  1916. if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
  1917. dev_info(dev, "Detected ASUSWMI, use DCTS\n");
  1918. asus->dsts_id = ASUS_WMI_METHODID_DCTS;
  1919. } else {
  1920. dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
  1921. asus->dsts_id = ASUS_WMI_METHODID_DSTS;
  1922. }
  1923. /*
  1924. * Some devices can have multiple event codes stored in a queue before
  1925. * the module load if it was unloaded intermittently after calling
  1926. * the INIT method (enables event handling). The WMI notify handler is
  1927. * expected to retrieve all event codes until a retrieved code equals
  1928. * queue end marker (One or Ones). Old codes are flushed from the queue
  1929. * upon module load. Not enabling this when it should be has minimal
  1930. * visible impact so fall back if anything goes wrong.
  1931. */
  1932. wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
  1933. if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
  1934. dev_info(dev, "Detected ATK, enable event queue\n");
  1935. if (!asus_wmi_notify_queue_flush(asus))
  1936. asus->wmi_event_queue = true;
  1937. }
  1938. /* CWAP allow to define the behavior of the Fn+F2 key,
  1939. * this method doesn't seems to be present on Eee PCs */
  1940. if (asus->driver->quirks->wapf >= 0)
  1941. asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
  1942. asus->driver->quirks->wapf, NULL);
  1943. return 0;
  1944. }
  1945. /* debugfs ********************************************************************/
  1946. struct asus_wmi_debugfs_node {
  1947. struct asus_wmi *asus;
  1948. char *name;
  1949. int (*show) (struct seq_file *m, void *data);
  1950. };
  1951. static int show_dsts(struct seq_file *m, void *data)
  1952. {
  1953. struct asus_wmi *asus = m->private;
  1954. int err;
  1955. u32 retval = -1;
  1956. err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
  1957. if (err < 0)
  1958. return err;
  1959. seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
  1960. return 0;
  1961. }
  1962. static int show_devs(struct seq_file *m, void *data)
  1963. {
  1964. struct asus_wmi *asus = m->private;
  1965. int err;
  1966. u32 retval = -1;
  1967. err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
  1968. &retval);
  1969. if (err < 0)
  1970. return err;
  1971. seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
  1972. asus->debug.ctrl_param, retval);
  1973. return 0;
  1974. }
  1975. static int show_call(struct seq_file *m, void *data)
  1976. {
  1977. struct asus_wmi *asus = m->private;
  1978. struct bios_args args = {
  1979. .arg0 = asus->debug.dev_id,
  1980. .arg1 = asus->debug.ctrl_param,
  1981. };
  1982. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  1983. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  1984. union acpi_object *obj;
  1985. acpi_status status;
  1986. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
  1987. 0, asus->debug.method_id,
  1988. &input, &output);
  1989. if (ACPI_FAILURE(status))
  1990. return -EIO;
  1991. obj = (union acpi_object *)output.pointer;
  1992. if (obj && obj->type == ACPI_TYPE_INTEGER)
  1993. seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
  1994. asus->debug.dev_id, asus->debug.ctrl_param,
  1995. (u32) obj->integer.value);
  1996. else
  1997. seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
  1998. asus->debug.dev_id, asus->debug.ctrl_param,
  1999. obj ? obj->type : -1);
  2000. kfree(obj);
  2001. return 0;
  2002. }
  2003. static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
  2004. {NULL, "devs", show_devs},
  2005. {NULL, "dsts", show_dsts},
  2006. {NULL, "call", show_call},
  2007. };
  2008. static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
  2009. {
  2010. struct asus_wmi_debugfs_node *node = inode->i_private;
  2011. return single_open(file, node->show, node->asus);
  2012. }
  2013. static const struct file_operations asus_wmi_debugfs_io_ops = {
  2014. .owner = THIS_MODULE,
  2015. .open = asus_wmi_debugfs_open,
  2016. .read = seq_read,
  2017. .llseek = seq_lseek,
  2018. .release = single_release,
  2019. };
  2020. static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
  2021. {
  2022. debugfs_remove_recursive(asus->debug.root);
  2023. }
  2024. static void asus_wmi_debugfs_init(struct asus_wmi *asus)
  2025. {
  2026. int i;
  2027. asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
  2028. debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
  2029. &asus->debug.method_id);
  2030. debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
  2031. &asus->debug.dev_id);
  2032. debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
  2033. &asus->debug.ctrl_param);
  2034. for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
  2035. struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
  2036. node->asus = asus;
  2037. debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  2038. asus->debug.root, node,
  2039. &asus_wmi_debugfs_io_ops);
  2040. }
  2041. }
  2042. /* Init / exit ****************************************************************/
  2043. static int asus_wmi_add(struct platform_device *pdev)
  2044. {
  2045. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  2046. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  2047. struct asus_wmi *asus;
  2048. const char *chassis_type;
  2049. acpi_status status;
  2050. int err;
  2051. u32 result;
  2052. asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
  2053. if (!asus)
  2054. return -ENOMEM;
  2055. asus->driver = wdrv;
  2056. asus->platform_device = pdev;
  2057. wdrv->platform_device = pdev;
  2058. platform_set_drvdata(asus->platform_device, asus);
  2059. if (wdrv->detect_quirks)
  2060. wdrv->detect_quirks(asus->driver);
  2061. err = asus_wmi_platform_init(asus);
  2062. if (err)
  2063. goto fail_platform;
  2064. err = fan_boost_mode_check_present(asus);
  2065. if (err)
  2066. goto fail_fan_boost_mode;
  2067. err = throttle_thermal_policy_check_present(asus);
  2068. if (err)
  2069. goto fail_throttle_thermal_policy;
  2070. else
  2071. throttle_thermal_policy_set_default(asus);
  2072. err = asus_wmi_sysfs_init(asus->platform_device);
  2073. if (err)
  2074. goto fail_sysfs;
  2075. err = asus_wmi_input_init(asus);
  2076. if (err)
  2077. goto fail_input;
  2078. err = asus_wmi_fan_init(asus); /* probably no problems on error */
  2079. err = asus_wmi_hwmon_init(asus);
  2080. if (err)
  2081. goto fail_hwmon;
  2082. err = asus_wmi_led_init(asus);
  2083. if (err)
  2084. goto fail_leds;
  2085. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
  2086. if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
  2087. asus->driver->wlan_ctrl_by_user = 1;
  2088. if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
  2089. err = asus_wmi_rfkill_init(asus);
  2090. if (err)
  2091. goto fail_rfkill;
  2092. }
  2093. if (asus->driver->quirks->wmi_force_als_set)
  2094. asus_wmi_set_als();
  2095. /* Some Asus desktop boards export an acpi-video backlight interface,
  2096. stop this from showing up */
  2097. chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
  2098. if (chassis_type && !strcmp(chassis_type, "3"))
  2099. acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
  2100. if (asus->driver->quirks->wmi_backlight_power)
  2101. acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
  2102. if (asus->driver->quirks->wmi_backlight_native)
  2103. acpi_video_set_dmi_backlight_type(acpi_backlight_native);
  2104. if (asus->driver->quirks->xusb2pr)
  2105. asus_wmi_set_xusb2pr(asus);
  2106. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  2107. err = asus_wmi_backlight_init(asus);
  2108. if (err && err != -ENODEV)
  2109. goto fail_backlight;
  2110. } else if (asus->driver->quirks->wmi_backlight_set_devstate)
  2111. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
  2112. if (asus_wmi_has_fnlock_key(asus)) {
  2113. asus->fnlock_locked = true;
  2114. asus_wmi_fnlock_update(asus);
  2115. }
  2116. status = wmi_install_notify_handler(asus->driver->event_guid,
  2117. asus_wmi_notify, asus);
  2118. if (ACPI_FAILURE(status)) {
  2119. pr_err("Unable to register notify handler - %d\n", status);
  2120. err = -ENODEV;
  2121. goto fail_wmi_handler;
  2122. }
  2123. asus_wmi_battery_init(asus);
  2124. asus_wmi_debugfs_init(asus);
  2125. return 0;
  2126. fail_wmi_handler:
  2127. asus_wmi_backlight_exit(asus);
  2128. fail_backlight:
  2129. asus_wmi_rfkill_exit(asus);
  2130. fail_rfkill:
  2131. asus_wmi_led_exit(asus);
  2132. fail_leds:
  2133. fail_hwmon:
  2134. asus_wmi_input_exit(asus);
  2135. fail_input:
  2136. asus_wmi_sysfs_exit(asus->platform_device);
  2137. fail_sysfs:
  2138. fail_throttle_thermal_policy:
  2139. fail_fan_boost_mode:
  2140. fail_platform:
  2141. kfree(asus);
  2142. return err;
  2143. }
  2144. static int asus_wmi_remove(struct platform_device *device)
  2145. {
  2146. struct asus_wmi *asus;
  2147. asus = platform_get_drvdata(device);
  2148. wmi_remove_notify_handler(asus->driver->event_guid);
  2149. asus_wmi_backlight_exit(asus);
  2150. asus_wmi_input_exit(asus);
  2151. asus_wmi_led_exit(asus);
  2152. asus_wmi_rfkill_exit(asus);
  2153. asus_wmi_debugfs_exit(asus);
  2154. asus_wmi_sysfs_exit(asus->platform_device);
  2155. asus_fan_set_auto(asus);
  2156. asus_wmi_battery_exit(asus);
  2157. kfree(asus);
  2158. return 0;
  2159. }
  2160. /* Platform driver - hibernate/resume callbacks *******************************/
  2161. static int asus_hotk_thaw(struct device *device)
  2162. {
  2163. struct asus_wmi *asus = dev_get_drvdata(device);
  2164. if (asus->wlan.rfkill) {
  2165. bool wlan;
  2166. /*
  2167. * Work around bios bug - acpi _PTS turns off the wireless led
  2168. * during suspend. Normally it restores it on resume, but
  2169. * we should kick it ourselves in case hibernation is aborted.
  2170. */
  2171. wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  2172. asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
  2173. }
  2174. return 0;
  2175. }
  2176. static int asus_hotk_resume(struct device *device)
  2177. {
  2178. struct asus_wmi *asus = dev_get_drvdata(device);
  2179. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  2180. kbd_led_update(asus);
  2181. if (asus_wmi_has_fnlock_key(asus))
  2182. asus_wmi_fnlock_update(asus);
  2183. return 0;
  2184. }
  2185. static int asus_hotk_restore(struct device *device)
  2186. {
  2187. struct asus_wmi *asus = dev_get_drvdata(device);
  2188. int bl;
  2189. /* Refresh both wlan rfkill state and pci hotplug */
  2190. if (asus->wlan.rfkill)
  2191. asus_rfkill_hotplug(asus);
  2192. if (asus->bluetooth.rfkill) {
  2193. bl = !asus_wmi_get_devstate_simple(asus,
  2194. ASUS_WMI_DEVID_BLUETOOTH);
  2195. rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
  2196. }
  2197. if (asus->wimax.rfkill) {
  2198. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
  2199. rfkill_set_sw_state(asus->wimax.rfkill, bl);
  2200. }
  2201. if (asus->wwan3g.rfkill) {
  2202. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
  2203. rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
  2204. }
  2205. if (asus->gps.rfkill) {
  2206. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
  2207. rfkill_set_sw_state(asus->gps.rfkill, bl);
  2208. }
  2209. if (asus->uwb.rfkill) {
  2210. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
  2211. rfkill_set_sw_state(asus->uwb.rfkill, bl);
  2212. }
  2213. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  2214. kbd_led_update(asus);
  2215. if (asus_wmi_has_fnlock_key(asus))
  2216. asus_wmi_fnlock_update(asus);
  2217. return 0;
  2218. }
  2219. static const struct dev_pm_ops asus_pm_ops = {
  2220. .thaw = asus_hotk_thaw,
  2221. .restore = asus_hotk_restore,
  2222. .resume = asus_hotk_resume,
  2223. };
  2224. /* Registration ***************************************************************/
  2225. static int asus_wmi_probe(struct platform_device *pdev)
  2226. {
  2227. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  2228. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  2229. int ret;
  2230. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  2231. pr_warn("ASUS Management GUID not found\n");
  2232. return -ENODEV;
  2233. }
  2234. if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
  2235. pr_warn("ASUS Event GUID not found\n");
  2236. return -ENODEV;
  2237. }
  2238. if (wdrv->probe) {
  2239. ret = wdrv->probe(pdev);
  2240. if (ret)
  2241. return ret;
  2242. }
  2243. return asus_wmi_add(pdev);
  2244. }
  2245. static bool used;
  2246. int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
  2247. {
  2248. struct platform_driver *platform_driver;
  2249. struct platform_device *platform_device;
  2250. if (used)
  2251. return -EBUSY;
  2252. platform_driver = &driver->platform_driver;
  2253. platform_driver->remove = asus_wmi_remove;
  2254. platform_driver->driver.owner = driver->owner;
  2255. platform_driver->driver.name = driver->name;
  2256. platform_driver->driver.pm = &asus_pm_ops;
  2257. platform_device = platform_create_bundle(platform_driver,
  2258. asus_wmi_probe,
  2259. NULL, 0, NULL, 0);
  2260. if (IS_ERR(platform_device))
  2261. return PTR_ERR(platform_device);
  2262. used = true;
  2263. return 0;
  2264. }
  2265. EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
  2266. void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
  2267. {
  2268. platform_device_unregister(driver->platform_device);
  2269. platform_driver_unregister(&driver->platform_driver);
  2270. used = false;
  2271. }
  2272. EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
  2273. static int __init asus_wmi_init(void)
  2274. {
  2275. pr_info("ASUS WMI generic driver loaded\n");
  2276. return 0;
  2277. }
  2278. static void __exit asus_wmi_exit(void)
  2279. {
  2280. pr_info("ASUS WMI generic driver unloaded\n");
  2281. }
  2282. module_init(asus_wmi_init);
  2283. module_exit(asus_wmi_exit);