asus-wmi.c 40 KB

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