asus-wmi.c 37 KB

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