asus-wmi.c 57 KB

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