asus-wmi.c 57 KB

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