asus-wmi.c 55 KB

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