asus-wmi.c 59 KB

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