asus-wmi.c 43 KB

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