asus-wmi.c 45 KB

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