asus-wmi.c 49 KB

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