asus-wmi.c 59 KB

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