asus-wmi.c 56 KB

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