asus-wmi.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  1. /*
  2. * Asus PC WMI hotkey driver
  3. *
  4. * Copyright(C) 2010 Intel Corporation.
  5. * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
  6. *
  7. * Portions based on wistron_btns.c:
  8. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  9. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  10. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/slab.h>
  32. #include <linux/input.h>
  33. #include <linux/input/sparse-keymap.h>
  34. #include <linux/fb.h>
  35. #include <linux/backlight.h>
  36. #include <linux/leds.h>
  37. #include <linux/rfkill.h>
  38. #include <linux/pci.h>
  39. #include <linux/pci_hotplug.h>
  40. #include <linux/hwmon.h>
  41. #include <linux/hwmon-sysfs.h>
  42. #include <linux/debugfs.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/thermal.h>
  46. #include <linux/acpi.h>
  47. #include <linux/dmi.h>
  48. #include <acpi/video.h>
  49. #include "asus-wmi.h"
  50. MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
  51. "Yong Wang <yong.y.wang@intel.com>");
  52. MODULE_DESCRIPTION("Asus Generic WMI Driver");
  53. MODULE_LICENSE("GPL");
  54. #define to_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. #define NOTIFY_KBD_BRTUP 0xc4
  62. #define NOTIFY_KBD_BRTDWN 0xc5
  63. /* WMI Methods */
  64. #define ASUS_WMI_METHODID_SPEC 0x43455053 /* BIOS SPECification */
  65. #define ASUS_WMI_METHODID_SFBD 0x44424653 /* Set First Boot Device */
  66. #define ASUS_WMI_METHODID_GLCD 0x44434C47 /* Get LCD status */
  67. #define ASUS_WMI_METHODID_GPID 0x44495047 /* Get Panel ID?? (Resol) */
  68. #define ASUS_WMI_METHODID_QMOD 0x444F4D51 /* Quiet MODe */
  69. #define ASUS_WMI_METHODID_SPLV 0x4C425053 /* Set Panel Light Value */
  70. #define ASUS_WMI_METHODID_AGFN 0x4E464741 /* FaN? */
  71. #define ASUS_WMI_METHODID_SFUN 0x4E554653 /* FUNCtionalities */
  72. #define ASUS_WMI_METHODID_SDSP 0x50534453 /* Set DiSPlay output */
  73. #define ASUS_WMI_METHODID_GDSP 0x50534447 /* Get DiSPlay output */
  74. #define ASUS_WMI_METHODID_DEVP 0x50564544 /* DEVice Policy */
  75. #define ASUS_WMI_METHODID_OSVR 0x5256534F /* OS VeRsion */
  76. #define ASUS_WMI_METHODID_DSTS 0x53544344 /* Device STatuS */
  77. #define ASUS_WMI_METHODID_DSTS2 0x53545344 /* Device STatuS #2*/
  78. #define ASUS_WMI_METHODID_BSTS 0x53545342 /* Bios STatuS ? */
  79. #define ASUS_WMI_METHODID_DEVS 0x53564544 /* DEVice Set */
  80. #define ASUS_WMI_METHODID_CFVS 0x53564643 /* CPU Frequency Volt Set */
  81. #define ASUS_WMI_METHODID_KBFT 0x5446424B /* KeyBoard FilTer */
  82. #define ASUS_WMI_METHODID_INIT 0x54494E49 /* INITialize */
  83. #define ASUS_WMI_METHODID_HKEY 0x59454B48 /* Hot KEY ?? */
  84. #define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
  85. /* Wireless */
  86. #define ASUS_WMI_DEVID_HW_SWITCH 0x00010001
  87. #define ASUS_WMI_DEVID_WIRELESS_LED 0x00010002
  88. #define ASUS_WMI_DEVID_CWAP 0x00010003
  89. #define ASUS_WMI_DEVID_WLAN 0x00010011
  90. #define ASUS_WMI_DEVID_WLAN_LED 0x00010012
  91. #define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
  92. #define ASUS_WMI_DEVID_GPS 0x00010015
  93. #define ASUS_WMI_DEVID_WIMAX 0x00010017
  94. #define ASUS_WMI_DEVID_WWAN3G 0x00010019
  95. #define ASUS_WMI_DEVID_UWB 0x00010021
  96. /* Leds */
  97. /* 0x000200XX and 0x000400XX */
  98. #define ASUS_WMI_DEVID_LED1 0x00020011
  99. #define ASUS_WMI_DEVID_LED2 0x00020012
  100. #define ASUS_WMI_DEVID_LED3 0x00020013
  101. #define ASUS_WMI_DEVID_LED4 0x00020014
  102. #define ASUS_WMI_DEVID_LED5 0x00020015
  103. #define ASUS_WMI_DEVID_LED6 0x00020016
  104. /* Backlight and Brightness */
  105. #define ASUS_WMI_DEVID_ALS_ENABLE 0x00050001 /* Ambient Light Sensor */
  106. #define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
  107. #define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
  108. #define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
  109. #define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */
  110. #define ASUS_WMI_DEVID_LIGHTBAR 0x00050025
  111. /* Misc */
  112. #define ASUS_WMI_DEVID_CAMERA 0x00060013
  113. /* Storage */
  114. #define ASUS_WMI_DEVID_CARDREADER 0x00080013
  115. /* Input */
  116. #define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
  117. #define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
  118. /* Fan, Thermal */
  119. #define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011
  120. #define ASUS_WMI_DEVID_FAN_CTRL 0x00110012
  121. /* Power */
  122. #define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
  123. /* Deep S3 / Resume on LID open */
  124. #define ASUS_WMI_DEVID_LID_RESUME 0x00120031
  125. /* DSTS masks */
  126. #define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
  127. #define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
  128. #define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
  129. #define ASUS_WMI_DSTS_USER_BIT 0x00020000
  130. #define ASUS_WMI_DSTS_BIOS_BIT 0x00040000
  131. #define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
  132. #define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
  133. #define ASUS_WMI_DSTS_LIGHTBAR_MASK 0x0000000F
  134. #define ASUS_FAN_DESC "cpu_fan"
  135. #define ASUS_FAN_MFUN 0x13
  136. #define ASUS_FAN_SFUN_READ 0x06
  137. #define ASUS_FAN_SFUN_WRITE 0x07
  138. #define ASUS_FAN_CTRL_MANUAL 1
  139. #define ASUS_FAN_CTRL_AUTO 2
  140. #define USB_INTEL_XUSB2PR 0xD0
  141. #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
  142. static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
  143. struct bios_args {
  144. u32 arg0;
  145. u32 arg1;
  146. } __packed;
  147. /*
  148. * Struct that's used for all methods called via AGFN. Naming is
  149. * identically to the AML code.
  150. */
  151. struct agfn_args {
  152. u16 mfun; /* probably "Multi-function" to be called */
  153. u16 sfun; /* probably "Sub-function" to be called */
  154. u16 len; /* size of the hole struct, including subfunction fields */
  155. u8 stas; /* not used by now */
  156. u8 err; /* zero on success */
  157. } __packed;
  158. /* struct used for calling fan read and write methods */
  159. struct fan_args {
  160. struct agfn_args agfn; /* common fields */
  161. u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
  162. u32 speed; /* read: RPM/100 - write: 0-255 */
  163. } __packed;
  164. /*
  165. * <platform>/ - debugfs root directory
  166. * dev_id - current dev_id
  167. * ctrl_param - current ctrl_param
  168. * method_id - current method_id
  169. * devs - call DEVS(dev_id, ctrl_param) and print result
  170. * dsts - call DSTS(dev_id) and print result
  171. * call - call method_id(dev_id, ctrl_param) and print result
  172. */
  173. struct asus_wmi_debug {
  174. struct dentry *root;
  175. u32 method_id;
  176. u32 dev_id;
  177. u32 ctrl_param;
  178. };
  179. struct asus_rfkill {
  180. struct asus_wmi *asus;
  181. struct rfkill *rfkill;
  182. u32 dev_id;
  183. };
  184. struct asus_wmi {
  185. int dsts_id;
  186. int spec;
  187. int sfun;
  188. struct input_dev *inputdev;
  189. struct backlight_device *backlight_device;
  190. struct platform_device *platform_device;
  191. struct led_classdev wlan_led;
  192. int wlan_led_wk;
  193. struct led_classdev tpd_led;
  194. int tpd_led_wk;
  195. struct led_classdev kbd_led;
  196. int kbd_led_wk;
  197. struct led_classdev lightbar_led;
  198. int lightbar_led_wk;
  199. struct workqueue_struct *led_workqueue;
  200. struct work_struct tpd_led_work;
  201. struct work_struct kbd_led_work;
  202. struct work_struct wlan_led_work;
  203. struct work_struct lightbar_led_work;
  204. struct asus_rfkill wlan;
  205. struct asus_rfkill bluetooth;
  206. struct asus_rfkill wimax;
  207. struct asus_rfkill wwan3g;
  208. struct asus_rfkill gps;
  209. struct asus_rfkill uwb;
  210. bool asus_hwmon_fan_manual_mode;
  211. int asus_hwmon_num_fans;
  212. int asus_hwmon_pwm;
  213. struct hotplug_slot *hotplug_slot;
  214. struct mutex hotplug_lock;
  215. struct mutex wmi_lock;
  216. struct workqueue_struct *hotplug_workqueue;
  217. struct work_struct hotplug_work;
  218. struct asus_wmi_debug debug;
  219. struct asus_wmi_driver *driver;
  220. };
  221. static int asus_wmi_input_init(struct asus_wmi *asus)
  222. {
  223. int err;
  224. asus->inputdev = input_allocate_device();
  225. if (!asus->inputdev)
  226. return -ENOMEM;
  227. asus->inputdev->name = asus->driver->input_name;
  228. asus->inputdev->phys = asus->driver->input_phys;
  229. asus->inputdev->id.bustype = BUS_HOST;
  230. asus->inputdev->dev.parent = &asus->platform_device->dev;
  231. set_bit(EV_REP, asus->inputdev->evbit);
  232. err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
  233. if (err)
  234. goto err_free_dev;
  235. err = input_register_device(asus->inputdev);
  236. if (err)
  237. goto err_free_dev;
  238. return 0;
  239. err_free_dev:
  240. input_free_device(asus->inputdev);
  241. return err;
  242. }
  243. static void asus_wmi_input_exit(struct asus_wmi *asus)
  244. {
  245. if (asus->inputdev)
  246. input_unregister_device(asus->inputdev);
  247. asus->inputdev = NULL;
  248. }
  249. static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
  250. u32 *retval)
  251. {
  252. struct bios_args args = {
  253. .arg0 = arg0,
  254. .arg1 = arg1,
  255. };
  256. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  257. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  258. acpi_status status;
  259. union acpi_object *obj;
  260. u32 tmp = 0;
  261. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
  262. &input, &output);
  263. if (ACPI_FAILURE(status))
  264. goto exit;
  265. obj = (union acpi_object *)output.pointer;
  266. if (obj && obj->type == ACPI_TYPE_INTEGER)
  267. tmp = (u32) obj->integer.value;
  268. if (retval)
  269. *retval = tmp;
  270. kfree(obj);
  271. exit:
  272. if (ACPI_FAILURE(status))
  273. return -EIO;
  274. if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
  275. return -ENODEV;
  276. return 0;
  277. }
  278. static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
  279. {
  280. struct acpi_buffer input;
  281. u64 phys_addr;
  282. u32 retval;
  283. u32 status = -1;
  284. /*
  285. * Copy to dma capable address otherwise memory corruption occurs as
  286. * bios has to be able to access it.
  287. */
  288. input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
  289. input.length = args.length;
  290. if (!input.pointer)
  291. return -ENOMEM;
  292. phys_addr = virt_to_phys(input.pointer);
  293. memcpy(input.pointer, args.pointer, args.length);
  294. status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
  295. phys_addr, 0, &retval);
  296. if (!status)
  297. memcpy(args.pointer, input.pointer, args.length);
  298. kfree(input.pointer);
  299. if (status)
  300. return -ENXIO;
  301. return retval;
  302. }
  303. static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
  304. {
  305. return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
  306. }
  307. static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
  308. u32 *retval)
  309. {
  310. return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
  311. ctrl_param, retval);
  312. }
  313. /* Helper for special devices with magic return codes */
  314. static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
  315. u32 dev_id, u32 mask)
  316. {
  317. u32 retval = 0;
  318. int err;
  319. err = asus_wmi_get_devstate(asus, dev_id, &retval);
  320. if (err < 0)
  321. return err;
  322. if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
  323. return -ENODEV;
  324. if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
  325. if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
  326. return -ENODEV;
  327. }
  328. return retval & mask;
  329. }
  330. static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
  331. {
  332. return asus_wmi_get_devstate_bits(asus, dev_id,
  333. ASUS_WMI_DSTS_STATUS_BIT);
  334. }
  335. /*
  336. * LEDs
  337. */
  338. /*
  339. * These functions actually update the LED's, and are called from a
  340. * workqueue. By doing this as separate work rather than when the LED
  341. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  342. * potentially bad time, such as a timer interrupt.
  343. */
  344. static void tpd_led_update(struct work_struct *work)
  345. {
  346. int ctrl_param;
  347. struct asus_wmi *asus;
  348. asus = container_of(work, struct asus_wmi, tpd_led_work);
  349. ctrl_param = asus->tpd_led_wk;
  350. asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
  351. }
  352. static void tpd_led_set(struct led_classdev *led_cdev,
  353. enum led_brightness value)
  354. {
  355. struct asus_wmi *asus;
  356. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  357. asus->tpd_led_wk = !!value;
  358. queue_work(asus->led_workqueue, &asus->tpd_led_work);
  359. }
  360. static int read_tpd_led_state(struct asus_wmi *asus)
  361. {
  362. return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
  363. }
  364. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  365. {
  366. struct asus_wmi *asus;
  367. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  368. return read_tpd_led_state(asus);
  369. }
  370. static void kbd_led_update(struct work_struct *work)
  371. {
  372. int ctrl_param = 0;
  373. struct asus_wmi *asus;
  374. asus = container_of(work, struct asus_wmi, kbd_led_work);
  375. /*
  376. * bits 0-2: level
  377. * bit 7: light on/off
  378. */
  379. if (asus->kbd_led_wk > 0)
  380. ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
  381. asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
  382. }
  383. static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
  384. {
  385. int retval;
  386. /*
  387. * bits 0-2: level
  388. * bit 7: light on/off
  389. * bit 8-10: environment (0: dark, 1: normal, 2: light)
  390. * bit 17: status unknown
  391. */
  392. retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
  393. 0xFFFF);
  394. /* Unknown status is considered as off */
  395. if (retval == 0x8000)
  396. retval = 0;
  397. if (retval >= 0) {
  398. if (level)
  399. *level = retval & 0x7F;
  400. if (env)
  401. *env = (retval >> 8) & 0x7F;
  402. retval = 0;
  403. }
  404. return retval;
  405. }
  406. static void kbd_led_set(struct led_classdev *led_cdev,
  407. enum led_brightness value)
  408. {
  409. struct asus_wmi *asus;
  410. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  411. if (value > asus->kbd_led.max_brightness)
  412. value = asus->kbd_led.max_brightness;
  413. else if (value < 0)
  414. value = 0;
  415. asus->kbd_led_wk = value;
  416. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  417. }
  418. static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
  419. {
  420. struct asus_wmi *asus;
  421. int retval, value;
  422. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  423. retval = kbd_led_read(asus, &value, NULL);
  424. if (retval < 0)
  425. return retval;
  426. return value;
  427. }
  428. static int wlan_led_unknown_state(struct asus_wmi *asus)
  429. {
  430. u32 result;
  431. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  432. return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
  433. }
  434. static int wlan_led_presence(struct asus_wmi *asus)
  435. {
  436. u32 result;
  437. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  438. return result & ASUS_WMI_DSTS_PRESENCE_BIT;
  439. }
  440. static void wlan_led_update(struct work_struct *work)
  441. {
  442. int ctrl_param;
  443. struct asus_wmi *asus;
  444. asus = container_of(work, struct asus_wmi, wlan_led_work);
  445. ctrl_param = asus->wlan_led_wk;
  446. asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
  447. }
  448. static void wlan_led_set(struct led_classdev *led_cdev,
  449. enum led_brightness value)
  450. {
  451. struct asus_wmi *asus;
  452. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  453. asus->wlan_led_wk = !!value;
  454. queue_work(asus->led_workqueue, &asus->wlan_led_work);
  455. }
  456. static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
  457. {
  458. struct asus_wmi *asus;
  459. u32 result;
  460. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  461. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  462. return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  463. }
  464. static void lightbar_led_update(struct work_struct *work)
  465. {
  466. struct asus_wmi *asus;
  467. int ctrl_param;
  468. asus = container_of(work, struct asus_wmi, lightbar_led_work);
  469. ctrl_param = asus->lightbar_led_wk;
  470. asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
  471. }
  472. static void lightbar_led_set(struct led_classdev *led_cdev,
  473. enum led_brightness value)
  474. {
  475. struct asus_wmi *asus;
  476. asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
  477. asus->lightbar_led_wk = !!value;
  478. queue_work(asus->led_workqueue, &asus->lightbar_led_work);
  479. }
  480. static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
  481. {
  482. struct asus_wmi *asus;
  483. u32 result;
  484. asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
  485. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
  486. return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
  487. }
  488. static int lightbar_led_presence(struct asus_wmi *asus)
  489. {
  490. u32 result;
  491. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
  492. return result & ASUS_WMI_DSTS_PRESENCE_BIT;
  493. }
  494. static void asus_wmi_led_exit(struct asus_wmi *asus)
  495. {
  496. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  497. led_classdev_unregister(&asus->kbd_led);
  498. if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
  499. led_classdev_unregister(&asus->tpd_led);
  500. if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
  501. led_classdev_unregister(&asus->wlan_led);
  502. if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
  503. led_classdev_unregister(&asus->lightbar_led);
  504. if (asus->led_workqueue)
  505. destroy_workqueue(asus->led_workqueue);
  506. }
  507. static int asus_wmi_led_init(struct asus_wmi *asus)
  508. {
  509. int rv = 0, led_val;
  510. asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
  511. if (!asus->led_workqueue)
  512. return -ENOMEM;
  513. if (read_tpd_led_state(asus) >= 0) {
  514. INIT_WORK(&asus->tpd_led_work, tpd_led_update);
  515. asus->tpd_led.name = "asus::touchpad";
  516. asus->tpd_led.brightness_set = tpd_led_set;
  517. asus->tpd_led.brightness_get = tpd_led_get;
  518. asus->tpd_led.max_brightness = 1;
  519. rv = led_classdev_register(&asus->platform_device->dev,
  520. &asus->tpd_led);
  521. if (rv)
  522. goto error;
  523. }
  524. led_val = kbd_led_read(asus, NULL, NULL);
  525. if (led_val >= 0) {
  526. INIT_WORK(&asus->kbd_led_work, kbd_led_update);
  527. asus->kbd_led_wk = led_val;
  528. asus->kbd_led.name = "asus::kbd_backlight";
  529. asus->kbd_led.brightness_set = kbd_led_set;
  530. asus->kbd_led.brightness_get = kbd_led_get;
  531. asus->kbd_led.max_brightness = 3;
  532. rv = led_classdev_register(&asus->platform_device->dev,
  533. &asus->kbd_led);
  534. if (rv)
  535. goto error;
  536. }
  537. if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
  538. INIT_WORK(&asus->wlan_led_work, wlan_led_update);
  539. asus->wlan_led.name = "asus::wlan";
  540. asus->wlan_led.brightness_set = wlan_led_set;
  541. if (!wlan_led_unknown_state(asus))
  542. asus->wlan_led.brightness_get = wlan_led_get;
  543. asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
  544. asus->wlan_led.max_brightness = 1;
  545. asus->wlan_led.default_trigger = "asus-wlan";
  546. rv = led_classdev_register(&asus->platform_device->dev,
  547. &asus->wlan_led);
  548. if (rv)
  549. goto error;
  550. }
  551. if (lightbar_led_presence(asus)) {
  552. INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
  553. asus->lightbar_led.name = "asus::lightbar";
  554. asus->lightbar_led.brightness_set = lightbar_led_set;
  555. asus->lightbar_led.brightness_get = lightbar_led_get;
  556. asus->lightbar_led.max_brightness = 1;
  557. rv = led_classdev_register(&asus->platform_device->dev,
  558. &asus->lightbar_led);
  559. }
  560. error:
  561. if (rv)
  562. asus_wmi_led_exit(asus);
  563. return rv;
  564. }
  565. /*
  566. * PCI hotplug (for wlan rfkill)
  567. */
  568. static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
  569. {
  570. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  571. if (result < 0)
  572. return false;
  573. return !result;
  574. }
  575. static void asus_rfkill_hotplug(struct asus_wmi *asus)
  576. {
  577. struct pci_dev *dev;
  578. struct pci_bus *bus;
  579. bool blocked;
  580. bool absent;
  581. u32 l;
  582. mutex_lock(&asus->wmi_lock);
  583. blocked = asus_wlan_rfkill_blocked(asus);
  584. mutex_unlock(&asus->wmi_lock);
  585. mutex_lock(&asus->hotplug_lock);
  586. pci_lock_rescan_remove();
  587. if (asus->wlan.rfkill)
  588. rfkill_set_sw_state(asus->wlan.rfkill, blocked);
  589. if (asus->hotplug_slot) {
  590. bus = pci_find_bus(0, 1);
  591. if (!bus) {
  592. pr_warn("Unable to find PCI bus 1?\n");
  593. goto out_unlock;
  594. }
  595. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  596. pr_err("Unable to read PCI config space?\n");
  597. goto out_unlock;
  598. }
  599. absent = (l == 0xffffffff);
  600. if (blocked != absent) {
  601. pr_warn("BIOS says wireless lan is %s, "
  602. "but the pci device is %s\n",
  603. blocked ? "blocked" : "unblocked",
  604. absent ? "absent" : "present");
  605. pr_warn("skipped wireless hotplug as probably "
  606. "inappropriate for this model\n");
  607. goto out_unlock;
  608. }
  609. if (!blocked) {
  610. dev = pci_get_slot(bus, 0);
  611. if (dev) {
  612. /* Device already present */
  613. pci_dev_put(dev);
  614. goto out_unlock;
  615. }
  616. dev = pci_scan_single_device(bus, 0);
  617. if (dev) {
  618. pci_bus_assign_resources(bus);
  619. pci_bus_add_device(dev);
  620. }
  621. } else {
  622. dev = pci_get_slot(bus, 0);
  623. if (dev) {
  624. pci_stop_and_remove_bus_device(dev);
  625. pci_dev_put(dev);
  626. }
  627. }
  628. }
  629. out_unlock:
  630. pci_unlock_rescan_remove();
  631. mutex_unlock(&asus->hotplug_lock);
  632. }
  633. static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
  634. {
  635. struct asus_wmi *asus = data;
  636. if (event != ACPI_NOTIFY_BUS_CHECK)
  637. return;
  638. /*
  639. * We can't call directly asus_rfkill_hotplug because most
  640. * of the time WMBC is still being executed and not reetrant.
  641. * There is currently no way to tell ACPICA that we want this
  642. * method to be serialized, we schedule a asus_rfkill_hotplug
  643. * call later, in a safer context.
  644. */
  645. queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
  646. }
  647. static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
  648. {
  649. acpi_status status;
  650. acpi_handle handle;
  651. status = acpi_get_handle(NULL, node, &handle);
  652. if (ACPI_SUCCESS(status)) {
  653. status = acpi_install_notify_handler(handle,
  654. ACPI_SYSTEM_NOTIFY,
  655. asus_rfkill_notify, asus);
  656. if (ACPI_FAILURE(status))
  657. pr_warn("Failed to register notify on %s\n", node);
  658. } else
  659. return -ENODEV;
  660. return 0;
  661. }
  662. static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
  663. {
  664. acpi_status status = AE_OK;
  665. acpi_handle handle;
  666. status = acpi_get_handle(NULL, node, &handle);
  667. if (ACPI_SUCCESS(status)) {
  668. status = acpi_remove_notify_handler(handle,
  669. ACPI_SYSTEM_NOTIFY,
  670. asus_rfkill_notify);
  671. if (ACPI_FAILURE(status))
  672. pr_err("Error removing rfkill notify handler %s\n",
  673. node);
  674. }
  675. }
  676. static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
  677. u8 *value)
  678. {
  679. struct asus_wmi *asus = hotplug_slot->private;
  680. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  681. if (result < 0)
  682. return result;
  683. *value = !!result;
  684. return 0;
  685. }
  686. static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  687. {
  688. kfree(hotplug_slot->info);
  689. kfree(hotplug_slot);
  690. }
  691. static struct hotplug_slot_ops asus_hotplug_slot_ops = {
  692. .owner = THIS_MODULE,
  693. .get_adapter_status = asus_get_adapter_status,
  694. .get_power_status = asus_get_adapter_status,
  695. };
  696. static void asus_hotplug_work(struct work_struct *work)
  697. {
  698. struct asus_wmi *asus;
  699. asus = container_of(work, struct asus_wmi, hotplug_work);
  700. asus_rfkill_hotplug(asus);
  701. }
  702. static int asus_setup_pci_hotplug(struct asus_wmi *asus)
  703. {
  704. int ret = -ENOMEM;
  705. struct pci_bus *bus = pci_find_bus(0, 1);
  706. if (!bus) {
  707. pr_err("Unable to find wifi PCI bus\n");
  708. return -ENODEV;
  709. }
  710. asus->hotplug_workqueue =
  711. create_singlethread_workqueue("hotplug_workqueue");
  712. if (!asus->hotplug_workqueue)
  713. goto error_workqueue;
  714. INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
  715. asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  716. if (!asus->hotplug_slot)
  717. goto error_slot;
  718. asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  719. GFP_KERNEL);
  720. if (!asus->hotplug_slot->info)
  721. goto error_info;
  722. asus->hotplug_slot->private = asus;
  723. asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
  724. asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
  725. asus_get_adapter_status(asus->hotplug_slot,
  726. &asus->hotplug_slot->info->adapter_status);
  727. ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
  728. if (ret) {
  729. pr_err("Unable to register hotplug slot - %d\n", ret);
  730. goto error_register;
  731. }
  732. return 0;
  733. error_register:
  734. kfree(asus->hotplug_slot->info);
  735. error_info:
  736. kfree(asus->hotplug_slot);
  737. asus->hotplug_slot = NULL;
  738. error_slot:
  739. destroy_workqueue(asus->hotplug_workqueue);
  740. error_workqueue:
  741. return ret;
  742. }
  743. /*
  744. * Rfkill devices
  745. */
  746. static int asus_rfkill_set(void *data, bool blocked)
  747. {
  748. struct asus_rfkill *priv = data;
  749. u32 ctrl_param = !blocked;
  750. u32 dev_id = priv->dev_id;
  751. /*
  752. * If the user bit is set, BIOS can't set and record the wlan status,
  753. * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
  754. * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
  755. * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
  756. * while setting the wlan status through WMI.
  757. * This is also the behavior that windows app will do.
  758. */
  759. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  760. priv->asus->driver->wlan_ctrl_by_user)
  761. dev_id = ASUS_WMI_DEVID_WLAN_LED;
  762. return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
  763. }
  764. static void asus_rfkill_query(struct rfkill *rfkill, void *data)
  765. {
  766. struct asus_rfkill *priv = data;
  767. int result;
  768. result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
  769. if (result < 0)
  770. return;
  771. rfkill_set_sw_state(priv->rfkill, !result);
  772. }
  773. static int asus_rfkill_wlan_set(void *data, bool blocked)
  774. {
  775. struct asus_rfkill *priv = data;
  776. struct asus_wmi *asus = priv->asus;
  777. int ret;
  778. /*
  779. * This handler is enabled only if hotplug is enabled.
  780. * In this case, the asus_wmi_set_devstate() will
  781. * trigger a wmi notification and we need to wait
  782. * this call to finish before being able to call
  783. * any wmi method
  784. */
  785. mutex_lock(&asus->wmi_lock);
  786. ret = asus_rfkill_set(data, blocked);
  787. mutex_unlock(&asus->wmi_lock);
  788. return ret;
  789. }
  790. static const struct rfkill_ops asus_rfkill_wlan_ops = {
  791. .set_block = asus_rfkill_wlan_set,
  792. .query = asus_rfkill_query,
  793. };
  794. static const struct rfkill_ops asus_rfkill_ops = {
  795. .set_block = asus_rfkill_set,
  796. .query = asus_rfkill_query,
  797. };
  798. static int asus_new_rfkill(struct asus_wmi *asus,
  799. struct asus_rfkill *arfkill,
  800. const char *name, enum rfkill_type type, int dev_id)
  801. {
  802. int result = asus_wmi_get_devstate_simple(asus, dev_id);
  803. struct rfkill **rfkill = &arfkill->rfkill;
  804. if (result < 0)
  805. return result;
  806. arfkill->dev_id = dev_id;
  807. arfkill->asus = asus;
  808. if (dev_id == ASUS_WMI_DEVID_WLAN &&
  809. asus->driver->quirks->hotplug_wireless)
  810. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  811. &asus_rfkill_wlan_ops, arfkill);
  812. else
  813. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  814. &asus_rfkill_ops, arfkill);
  815. if (!*rfkill)
  816. return -EINVAL;
  817. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  818. (asus->driver->quirks->wapf > 0))
  819. rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
  820. rfkill_init_sw_state(*rfkill, !result);
  821. result = rfkill_register(*rfkill);
  822. if (result) {
  823. rfkill_destroy(*rfkill);
  824. *rfkill = NULL;
  825. return result;
  826. }
  827. return 0;
  828. }
  829. static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
  830. {
  831. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  832. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  833. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  834. if (asus->wlan.rfkill) {
  835. rfkill_unregister(asus->wlan.rfkill);
  836. rfkill_destroy(asus->wlan.rfkill);
  837. asus->wlan.rfkill = NULL;
  838. }
  839. /*
  840. * Refresh pci hotplug in case the rfkill state was changed after
  841. * asus_unregister_rfkill_notifier()
  842. */
  843. asus_rfkill_hotplug(asus);
  844. if (asus->hotplug_slot)
  845. pci_hp_deregister(asus->hotplug_slot);
  846. if (asus->hotplug_workqueue)
  847. destroy_workqueue(asus->hotplug_workqueue);
  848. if (asus->bluetooth.rfkill) {
  849. rfkill_unregister(asus->bluetooth.rfkill);
  850. rfkill_destroy(asus->bluetooth.rfkill);
  851. asus->bluetooth.rfkill = NULL;
  852. }
  853. if (asus->wimax.rfkill) {
  854. rfkill_unregister(asus->wimax.rfkill);
  855. rfkill_destroy(asus->wimax.rfkill);
  856. asus->wimax.rfkill = NULL;
  857. }
  858. if (asus->wwan3g.rfkill) {
  859. rfkill_unregister(asus->wwan3g.rfkill);
  860. rfkill_destroy(asus->wwan3g.rfkill);
  861. asus->wwan3g.rfkill = NULL;
  862. }
  863. if (asus->gps.rfkill) {
  864. rfkill_unregister(asus->gps.rfkill);
  865. rfkill_destroy(asus->gps.rfkill);
  866. asus->gps.rfkill = NULL;
  867. }
  868. if (asus->uwb.rfkill) {
  869. rfkill_unregister(asus->uwb.rfkill);
  870. rfkill_destroy(asus->uwb.rfkill);
  871. asus->uwb.rfkill = NULL;
  872. }
  873. }
  874. static int asus_wmi_rfkill_init(struct asus_wmi *asus)
  875. {
  876. int result = 0;
  877. mutex_init(&asus->hotplug_lock);
  878. mutex_init(&asus->wmi_lock);
  879. result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
  880. RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
  881. if (result && result != -ENODEV)
  882. goto exit;
  883. result = asus_new_rfkill(asus, &asus->bluetooth,
  884. "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
  885. ASUS_WMI_DEVID_BLUETOOTH);
  886. if (result && result != -ENODEV)
  887. goto exit;
  888. result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
  889. RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
  890. if (result && result != -ENODEV)
  891. goto exit;
  892. result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
  893. RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
  894. if (result && result != -ENODEV)
  895. goto exit;
  896. result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
  897. RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
  898. if (result && result != -ENODEV)
  899. goto exit;
  900. result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
  901. RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
  902. if (result && result != -ENODEV)
  903. goto exit;
  904. if (!asus->driver->quirks->hotplug_wireless)
  905. goto exit;
  906. result = asus_setup_pci_hotplug(asus);
  907. /*
  908. * If we get -EBUSY then something else is handling the PCI hotplug -
  909. * don't fail in this case
  910. */
  911. if (result == -EBUSY)
  912. result = 0;
  913. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  914. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  915. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  916. /*
  917. * Refresh pci hotplug in case the rfkill state was changed during
  918. * setup.
  919. */
  920. asus_rfkill_hotplug(asus);
  921. exit:
  922. if (result && result != -ENODEV)
  923. asus_wmi_rfkill_exit(asus);
  924. if (result == -ENODEV)
  925. result = 0;
  926. return result;
  927. }
  928. static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
  929. {
  930. struct pci_dev *xhci_pdev;
  931. u32 orig_ports_available;
  932. u32 ports_available = asus->driver->quirks->xusb2pr;
  933. xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  934. PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
  935. NULL);
  936. if (!xhci_pdev)
  937. return;
  938. pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  939. &orig_ports_available);
  940. pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  941. cpu_to_le32(ports_available));
  942. pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
  943. orig_ports_available, ports_available);
  944. }
  945. /*
  946. * Some devices dont support or have borcken get_als method
  947. * but still support set method.
  948. */
  949. static void asus_wmi_set_als(void)
  950. {
  951. asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
  952. }
  953. /*
  954. * Hwmon device
  955. */
  956. static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
  957. int *speed)
  958. {
  959. struct fan_args args = {
  960. .agfn.len = sizeof(args),
  961. .agfn.mfun = ASUS_FAN_MFUN,
  962. .agfn.sfun = ASUS_FAN_SFUN_READ,
  963. .fan = fan,
  964. .speed = 0,
  965. };
  966. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  967. int status;
  968. if (fan != 1)
  969. return -EINVAL;
  970. status = asus_wmi_evaluate_method_agfn(input);
  971. if (status || args.agfn.err)
  972. return -ENXIO;
  973. if (speed)
  974. *speed = args.speed;
  975. return 0;
  976. }
  977. static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
  978. int *speed)
  979. {
  980. struct fan_args args = {
  981. .agfn.len = sizeof(args),
  982. .agfn.mfun = ASUS_FAN_MFUN,
  983. .agfn.sfun = ASUS_FAN_SFUN_WRITE,
  984. .fan = fan,
  985. .speed = speed ? *speed : 0,
  986. };
  987. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  988. int status;
  989. /* 1: for setting 1st fan's speed 0: setting auto mode */
  990. if (fan != 1 && fan != 0)
  991. return -EINVAL;
  992. status = asus_wmi_evaluate_method_agfn(input);
  993. if (status || args.agfn.err)
  994. return -ENXIO;
  995. if (speed && fan == 1)
  996. asus->asus_hwmon_pwm = *speed;
  997. return 0;
  998. }
  999. /*
  1000. * Check if we can read the speed of one fan. If true we assume we can also
  1001. * control it.
  1002. */
  1003. static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
  1004. {
  1005. int status;
  1006. int speed = 0;
  1007. *num_fans = 0;
  1008. status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
  1009. if (!status)
  1010. *num_fans = 1;
  1011. return 0;
  1012. }
  1013. static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
  1014. {
  1015. int status;
  1016. status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
  1017. if (status)
  1018. return -ENXIO;
  1019. asus->asus_hwmon_fan_manual_mode = false;
  1020. return 0;
  1021. }
  1022. static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
  1023. {
  1024. struct asus_wmi *asus = dev_get_drvdata(dev);
  1025. int value;
  1026. int ret;
  1027. /* no speed readable on manual mode */
  1028. if (asus->asus_hwmon_fan_manual_mode)
  1029. return -ENXIO;
  1030. ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
  1031. if (ret) {
  1032. pr_warn("reading fan speed failed: %d\n", ret);
  1033. return -ENXIO;
  1034. }
  1035. return value;
  1036. }
  1037. static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
  1038. {
  1039. int err;
  1040. if (asus->asus_hwmon_pwm >= 0) {
  1041. *value = asus->asus_hwmon_pwm;
  1042. return;
  1043. }
  1044. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
  1045. if (err < 0)
  1046. return;
  1047. *value &= 0xFF;
  1048. if (*value == 1) /* Low Speed */
  1049. *value = 85;
  1050. else if (*value == 2)
  1051. *value = 170;
  1052. else if (*value == 3)
  1053. *value = 255;
  1054. else if (*value) {
  1055. pr_err("Unknown fan speed %#x\n", *value);
  1056. *value = -1;
  1057. }
  1058. }
  1059. static ssize_t pwm1_show(struct device *dev,
  1060. struct device_attribute *attr,
  1061. char *buf)
  1062. {
  1063. struct asus_wmi *asus = dev_get_drvdata(dev);
  1064. int value;
  1065. asus_hwmon_pwm_show(asus, 0, &value);
  1066. return sprintf(buf, "%d\n", value);
  1067. }
  1068. static ssize_t pwm1_store(struct device *dev,
  1069. struct device_attribute *attr,
  1070. const char *buf, size_t count) {
  1071. struct asus_wmi *asus = dev_get_drvdata(dev);
  1072. int value;
  1073. int state;
  1074. int ret;
  1075. ret = kstrtouint(buf, 10, &value);
  1076. if (ret)
  1077. return ret;
  1078. value = clamp(value, 0, 255);
  1079. state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
  1080. if (state)
  1081. pr_warn("Setting fan speed failed: %d\n", state);
  1082. else
  1083. asus->asus_hwmon_fan_manual_mode = true;
  1084. return count;
  1085. }
  1086. static ssize_t fan1_input_show(struct device *dev,
  1087. struct device_attribute *attr,
  1088. char *buf)
  1089. {
  1090. int value = asus_hwmon_fan_rpm_show(dev, 0);
  1091. return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
  1092. }
  1093. static ssize_t pwm1_enable_show(struct device *dev,
  1094. struct device_attribute *attr,
  1095. char *buf)
  1096. {
  1097. struct asus_wmi *asus = dev_get_drvdata(dev);
  1098. if (asus->asus_hwmon_fan_manual_mode)
  1099. return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
  1100. return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
  1101. }
  1102. static ssize_t pwm1_enable_store(struct device *dev,
  1103. struct device_attribute *attr,
  1104. const char *buf, size_t count)
  1105. {
  1106. struct asus_wmi *asus = dev_get_drvdata(dev);
  1107. int status = 0;
  1108. int state;
  1109. int ret;
  1110. ret = kstrtouint(buf, 10, &state);
  1111. if (ret)
  1112. return ret;
  1113. if (state == ASUS_FAN_CTRL_MANUAL)
  1114. asus->asus_hwmon_fan_manual_mode = true;
  1115. else
  1116. status = asus_hwmon_fan_set_auto(asus);
  1117. if (status)
  1118. return status;
  1119. return count;
  1120. }
  1121. static ssize_t fan1_label_show(struct device *dev,
  1122. struct device_attribute *attr,
  1123. char *buf)
  1124. {
  1125. return sprintf(buf, "%s\n", ASUS_FAN_DESC);
  1126. }
  1127. static ssize_t asus_hwmon_temp1(struct device *dev,
  1128. struct device_attribute *attr,
  1129. char *buf)
  1130. {
  1131. struct asus_wmi *asus = dev_get_drvdata(dev);
  1132. u32 value;
  1133. int err;
  1134. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
  1135. if (err < 0)
  1136. return err;
  1137. value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
  1138. return sprintf(buf, "%d\n", value);
  1139. }
  1140. /* Fan1 */
  1141. static DEVICE_ATTR_RW(pwm1);
  1142. static DEVICE_ATTR_RW(pwm1_enable);
  1143. static DEVICE_ATTR_RO(fan1_input);
  1144. static DEVICE_ATTR_RO(fan1_label);
  1145. /* Temperature */
  1146. static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
  1147. static struct attribute *hwmon_attributes[] = {
  1148. &dev_attr_pwm1.attr,
  1149. &dev_attr_pwm1_enable.attr,
  1150. &dev_attr_fan1_input.attr,
  1151. &dev_attr_fan1_label.attr,
  1152. &dev_attr_temp1_input.attr,
  1153. NULL
  1154. };
  1155. static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
  1156. struct attribute *attr, int idx)
  1157. {
  1158. struct device *dev = container_of(kobj, struct device, kobj);
  1159. struct platform_device *pdev = to_platform_device(dev->parent);
  1160. struct asus_wmi *asus = platform_get_drvdata(pdev);
  1161. int dev_id = -1;
  1162. int fan_attr = -1;
  1163. u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
  1164. bool ok = true;
  1165. if (attr == &dev_attr_pwm1.attr)
  1166. dev_id = ASUS_WMI_DEVID_FAN_CTRL;
  1167. else if (attr == &dev_attr_temp1_input.attr)
  1168. dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
  1169. if (attr == &dev_attr_fan1_input.attr
  1170. || attr == &dev_attr_fan1_label.attr
  1171. || attr == &dev_attr_pwm1.attr
  1172. || attr == &dev_attr_pwm1_enable.attr) {
  1173. fan_attr = 1;
  1174. }
  1175. if (dev_id != -1) {
  1176. int err = asus_wmi_get_devstate(asus, dev_id, &value);
  1177. if (err < 0 && fan_attr == -1)
  1178. return 0; /* can't return negative here */
  1179. }
  1180. if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
  1181. /*
  1182. * We need to find a better way, probably using sfun,
  1183. * bits or spec ...
  1184. * Currently we disable it if:
  1185. * - ASUS_WMI_UNSUPPORTED_METHOD is returned
  1186. * - reverved bits are non-zero
  1187. * - sfun and presence bit are not set
  1188. */
  1189. if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
  1190. || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
  1191. ok = false;
  1192. else
  1193. ok = fan_attr <= asus->asus_hwmon_num_fans;
  1194. } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
  1195. /* If value is zero, something is clearly wrong */
  1196. if (!value)
  1197. ok = false;
  1198. } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
  1199. ok = true;
  1200. } else {
  1201. ok = false;
  1202. }
  1203. return ok ? attr->mode : 0;
  1204. }
  1205. static const struct attribute_group hwmon_attribute_group = {
  1206. .is_visible = asus_hwmon_sysfs_is_visible,
  1207. .attrs = hwmon_attributes
  1208. };
  1209. __ATTRIBUTE_GROUPS(hwmon_attribute);
  1210. static int asus_wmi_hwmon_init(struct asus_wmi *asus)
  1211. {
  1212. struct device *hwmon;
  1213. hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
  1214. "asus", asus,
  1215. hwmon_attribute_groups);
  1216. if (IS_ERR(hwmon)) {
  1217. pr_err("Could not register asus hwmon device\n");
  1218. return PTR_ERR(hwmon);
  1219. }
  1220. return 0;
  1221. }
  1222. /*
  1223. * Backlight
  1224. */
  1225. static int read_backlight_power(struct asus_wmi *asus)
  1226. {
  1227. int ret;
  1228. if (asus->driver->quirks->store_backlight_power)
  1229. ret = !asus->driver->panel_power;
  1230. else
  1231. ret = asus_wmi_get_devstate_simple(asus,
  1232. ASUS_WMI_DEVID_BACKLIGHT);
  1233. if (ret < 0)
  1234. return ret;
  1235. return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  1236. }
  1237. static int read_brightness_max(struct asus_wmi *asus)
  1238. {
  1239. u32 retval;
  1240. int err;
  1241. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  1242. if (err < 0)
  1243. return err;
  1244. retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
  1245. retval >>= 8;
  1246. if (!retval)
  1247. return -ENODEV;
  1248. return retval;
  1249. }
  1250. static int read_brightness(struct backlight_device *bd)
  1251. {
  1252. struct asus_wmi *asus = bl_get_data(bd);
  1253. u32 retval;
  1254. int err;
  1255. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  1256. if (err < 0)
  1257. return err;
  1258. return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  1259. }
  1260. static u32 get_scalar_command(struct backlight_device *bd)
  1261. {
  1262. struct asus_wmi *asus = bl_get_data(bd);
  1263. u32 ctrl_param = 0;
  1264. if ((asus->driver->brightness < bd->props.brightness) ||
  1265. bd->props.brightness == bd->props.max_brightness)
  1266. ctrl_param = 0x00008001;
  1267. else if ((asus->driver->brightness > bd->props.brightness) ||
  1268. bd->props.brightness == 0)
  1269. ctrl_param = 0x00008000;
  1270. asus->driver->brightness = bd->props.brightness;
  1271. return ctrl_param;
  1272. }
  1273. static int update_bl_status(struct backlight_device *bd)
  1274. {
  1275. struct asus_wmi *asus = bl_get_data(bd);
  1276. u32 ctrl_param;
  1277. int power, err = 0;
  1278. power = read_backlight_power(asus);
  1279. if (power != -ENODEV && bd->props.power != power) {
  1280. ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
  1281. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
  1282. ctrl_param, NULL);
  1283. if (asus->driver->quirks->store_backlight_power)
  1284. asus->driver->panel_power = bd->props.power;
  1285. /* When using scalar brightness, updating the brightness
  1286. * will mess with the backlight power */
  1287. if (asus->driver->quirks->scalar_panel_brightness)
  1288. return err;
  1289. }
  1290. if (asus->driver->quirks->scalar_panel_brightness)
  1291. ctrl_param = get_scalar_command(bd);
  1292. else
  1293. ctrl_param = bd->props.brightness;
  1294. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
  1295. ctrl_param, NULL);
  1296. return err;
  1297. }
  1298. static const struct backlight_ops asus_wmi_bl_ops = {
  1299. .get_brightness = read_brightness,
  1300. .update_status = update_bl_status,
  1301. };
  1302. static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
  1303. {
  1304. struct backlight_device *bd = asus->backlight_device;
  1305. int old = bd->props.brightness;
  1306. int new = old;
  1307. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  1308. new = code - NOTIFY_BRNUP_MIN + 1;
  1309. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  1310. new = code - NOTIFY_BRNDOWN_MIN;
  1311. bd->props.brightness = new;
  1312. backlight_update_status(bd);
  1313. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  1314. return old;
  1315. }
  1316. static int asus_wmi_backlight_init(struct asus_wmi *asus)
  1317. {
  1318. struct backlight_device *bd;
  1319. struct backlight_properties props;
  1320. int max;
  1321. int power;
  1322. max = read_brightness_max(asus);
  1323. if (max < 0)
  1324. return max;
  1325. power = read_backlight_power(asus);
  1326. if (power == -ENODEV)
  1327. power = FB_BLANK_UNBLANK;
  1328. else if (power < 0)
  1329. return power;
  1330. memset(&props, 0, sizeof(struct backlight_properties));
  1331. props.type = BACKLIGHT_PLATFORM;
  1332. props.max_brightness = max;
  1333. bd = backlight_device_register(asus->driver->name,
  1334. &asus->platform_device->dev, asus,
  1335. &asus_wmi_bl_ops, &props);
  1336. if (IS_ERR(bd)) {
  1337. pr_err("Could not register backlight device\n");
  1338. return PTR_ERR(bd);
  1339. }
  1340. asus->backlight_device = bd;
  1341. if (asus->driver->quirks->store_backlight_power)
  1342. asus->driver->panel_power = power;
  1343. bd->props.brightness = read_brightness(bd);
  1344. bd->props.power = power;
  1345. backlight_update_status(bd);
  1346. asus->driver->brightness = bd->props.brightness;
  1347. return 0;
  1348. }
  1349. static void asus_wmi_backlight_exit(struct asus_wmi *asus)
  1350. {
  1351. backlight_device_unregister(asus->backlight_device);
  1352. asus->backlight_device = NULL;
  1353. }
  1354. static int is_display_toggle(int code)
  1355. {
  1356. /* display toggle keys */
  1357. if ((code >= 0x61 && code <= 0x67) ||
  1358. (code >= 0x8c && code <= 0x93) ||
  1359. (code >= 0xa0 && code <= 0xa7) ||
  1360. (code >= 0xd0 && code <= 0xd5))
  1361. return 1;
  1362. return 0;
  1363. }
  1364. static void asus_wmi_notify(u32 value, void *context)
  1365. {
  1366. struct asus_wmi *asus = context;
  1367. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  1368. union acpi_object *obj;
  1369. acpi_status status;
  1370. int code;
  1371. int orig_code;
  1372. unsigned int key_value = 1;
  1373. bool autorelease = 1;
  1374. status = wmi_get_event_data(value, &response);
  1375. if (status != AE_OK) {
  1376. pr_err("bad event status 0x%x\n", status);
  1377. return;
  1378. }
  1379. obj = (union acpi_object *)response.pointer;
  1380. if (!obj || obj->type != ACPI_TYPE_INTEGER)
  1381. goto exit;
  1382. code = obj->integer.value;
  1383. orig_code = code;
  1384. if (asus->driver->key_filter) {
  1385. asus->driver->key_filter(asus->driver, &code, &key_value,
  1386. &autorelease);
  1387. if (code == ASUS_WMI_KEY_IGNORE)
  1388. goto exit;
  1389. }
  1390. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  1391. code = ASUS_WMI_BRN_UP;
  1392. else if (code >= NOTIFY_BRNDOWN_MIN &&
  1393. code <= NOTIFY_BRNDOWN_MAX)
  1394. code = ASUS_WMI_BRN_DOWN;
  1395. if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
  1396. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  1397. asus_wmi_backlight_notify(asus, orig_code);
  1398. goto exit;
  1399. }
  1400. }
  1401. if (is_display_toggle(code) &&
  1402. asus->driver->quirks->no_display_toggle)
  1403. goto exit;
  1404. if (!sparse_keymap_report_event(asus->inputdev, code,
  1405. key_value, autorelease))
  1406. pr_info("Unknown key %x pressed\n", code);
  1407. exit:
  1408. kfree(obj);
  1409. }
  1410. /*
  1411. * Sys helpers
  1412. */
  1413. static int parse_arg(const char *buf, unsigned long count, int *val)
  1414. {
  1415. if (!count)
  1416. return 0;
  1417. if (sscanf(buf, "%i", val) != 1)
  1418. return -EINVAL;
  1419. return count;
  1420. }
  1421. static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
  1422. const char *buf, size_t count)
  1423. {
  1424. u32 retval;
  1425. int rv, err, value;
  1426. value = asus_wmi_get_devstate_simple(asus, devid);
  1427. if (value < 0)
  1428. return value;
  1429. rv = parse_arg(buf, count, &value);
  1430. err = asus_wmi_set_devstate(devid, value, &retval);
  1431. if (err < 0)
  1432. return err;
  1433. return rv;
  1434. }
  1435. static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
  1436. {
  1437. int value = asus_wmi_get_devstate_simple(asus, devid);
  1438. if (value < 0)
  1439. return value;
  1440. return sprintf(buf, "%d\n", value);
  1441. }
  1442. #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
  1443. static ssize_t show_##_name(struct device *dev, \
  1444. struct device_attribute *attr, \
  1445. char *buf) \
  1446. { \
  1447. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1448. \
  1449. return show_sys_wmi(asus, _cm, buf); \
  1450. } \
  1451. static ssize_t store_##_name(struct device *dev, \
  1452. struct device_attribute *attr, \
  1453. const char *buf, size_t count) \
  1454. { \
  1455. struct asus_wmi *asus = dev_get_drvdata(dev); \
  1456. \
  1457. return store_sys_wmi(asus, _cm, buf, count); \
  1458. } \
  1459. static struct device_attribute dev_attr_##_name = { \
  1460. .attr = { \
  1461. .name = __stringify(_name), \
  1462. .mode = _mode }, \
  1463. .show = show_##_name, \
  1464. .store = store_##_name, \
  1465. }
  1466. ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
  1467. ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
  1468. ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
  1469. ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
  1470. ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
  1471. static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
  1472. const char *buf, size_t count)
  1473. {
  1474. int value, rv;
  1475. if (!count || sscanf(buf, "%i", &value) != 1)
  1476. return -EINVAL;
  1477. if (value < 0 || value > 2)
  1478. return -EINVAL;
  1479. rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
  1480. if (rv < 0)
  1481. return rv;
  1482. return count;
  1483. }
  1484. static DEVICE_ATTR_WO(cpufv);
  1485. static struct attribute *platform_attributes[] = {
  1486. &dev_attr_cpufv.attr,
  1487. &dev_attr_camera.attr,
  1488. &dev_attr_cardr.attr,
  1489. &dev_attr_touchpad.attr,
  1490. &dev_attr_lid_resume.attr,
  1491. &dev_attr_als_enable.attr,
  1492. NULL
  1493. };
  1494. static umode_t asus_sysfs_is_visible(struct kobject *kobj,
  1495. struct attribute *attr, int idx)
  1496. {
  1497. struct device *dev = container_of(kobj, struct device, kobj);
  1498. struct platform_device *pdev = to_platform_device(dev);
  1499. struct asus_wmi *asus = platform_get_drvdata(pdev);
  1500. bool ok = true;
  1501. int devid = -1;
  1502. if (attr == &dev_attr_camera.attr)
  1503. devid = ASUS_WMI_DEVID_CAMERA;
  1504. else if (attr == &dev_attr_cardr.attr)
  1505. devid = ASUS_WMI_DEVID_CARDREADER;
  1506. else if (attr == &dev_attr_touchpad.attr)
  1507. devid = ASUS_WMI_DEVID_TOUCHPAD;
  1508. else if (attr == &dev_attr_lid_resume.attr)
  1509. devid = ASUS_WMI_DEVID_LID_RESUME;
  1510. else if (attr == &dev_attr_als_enable.attr)
  1511. devid = ASUS_WMI_DEVID_ALS_ENABLE;
  1512. if (devid != -1)
  1513. ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
  1514. return ok ? attr->mode : 0;
  1515. }
  1516. static const struct attribute_group platform_attribute_group = {
  1517. .is_visible = asus_sysfs_is_visible,
  1518. .attrs = platform_attributes
  1519. };
  1520. static void asus_wmi_sysfs_exit(struct platform_device *device)
  1521. {
  1522. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  1523. }
  1524. static int asus_wmi_sysfs_init(struct platform_device *device)
  1525. {
  1526. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  1527. }
  1528. /*
  1529. * Platform device
  1530. */
  1531. static int asus_wmi_platform_init(struct asus_wmi *asus)
  1532. {
  1533. int rv;
  1534. /* INIT enable hotkeys on some models */
  1535. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
  1536. pr_info("Initialization: %#x\n", rv);
  1537. /* We don't know yet what to do with this version... */
  1538. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
  1539. pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
  1540. asus->spec = rv;
  1541. }
  1542. /*
  1543. * The SFUN method probably allows the original driver to get the list
  1544. * of features supported by a given model. For now, 0x0100 or 0x0800
  1545. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  1546. * The significance of others is yet to be found.
  1547. */
  1548. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
  1549. pr_info("SFUN value: %#x\n", rv);
  1550. asus->sfun = rv;
  1551. }
  1552. /*
  1553. * Eee PC and Notebooks seems to have different method_id for DSTS,
  1554. * but it may also be related to the BIOS's SPEC.
  1555. * Note, on most Eeepc, there is no way to check if a method exist
  1556. * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
  1557. * but once again, SPEC may probably be used for that kind of things.
  1558. */
  1559. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
  1560. asus->dsts_id = ASUS_WMI_METHODID_DSTS;
  1561. else
  1562. asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
  1563. /* CWAP allow to define the behavior of the Fn+F2 key,
  1564. * this method doesn't seems to be present on Eee PCs */
  1565. if (asus->driver->quirks->wapf >= 0)
  1566. asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
  1567. asus->driver->quirks->wapf, NULL);
  1568. return asus_wmi_sysfs_init(asus->platform_device);
  1569. }
  1570. static void asus_wmi_platform_exit(struct asus_wmi *asus)
  1571. {
  1572. asus_wmi_sysfs_exit(asus->platform_device);
  1573. }
  1574. /*
  1575. * debugfs
  1576. */
  1577. struct asus_wmi_debugfs_node {
  1578. struct asus_wmi *asus;
  1579. char *name;
  1580. int (*show) (struct seq_file *m, void *data);
  1581. };
  1582. static int show_dsts(struct seq_file *m, void *data)
  1583. {
  1584. struct asus_wmi *asus = m->private;
  1585. int err;
  1586. u32 retval = -1;
  1587. err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
  1588. if (err < 0)
  1589. return err;
  1590. seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
  1591. return 0;
  1592. }
  1593. static int show_devs(struct seq_file *m, void *data)
  1594. {
  1595. struct asus_wmi *asus = m->private;
  1596. int err;
  1597. u32 retval = -1;
  1598. err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
  1599. &retval);
  1600. if (err < 0)
  1601. return err;
  1602. seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
  1603. asus->debug.ctrl_param, retval);
  1604. return 0;
  1605. }
  1606. static int show_call(struct seq_file *m, void *data)
  1607. {
  1608. struct asus_wmi *asus = m->private;
  1609. struct bios_args args = {
  1610. .arg0 = asus->debug.dev_id,
  1611. .arg1 = asus->debug.ctrl_param,
  1612. };
  1613. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  1614. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  1615. union acpi_object *obj;
  1616. acpi_status status;
  1617. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
  1618. 0, asus->debug.method_id,
  1619. &input, &output);
  1620. if (ACPI_FAILURE(status))
  1621. return -EIO;
  1622. obj = (union acpi_object *)output.pointer;
  1623. if (obj && obj->type == ACPI_TYPE_INTEGER)
  1624. seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
  1625. asus->debug.dev_id, asus->debug.ctrl_param,
  1626. (u32) obj->integer.value);
  1627. else
  1628. seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
  1629. asus->debug.dev_id, asus->debug.ctrl_param,
  1630. obj ? obj->type : -1);
  1631. kfree(obj);
  1632. return 0;
  1633. }
  1634. static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
  1635. {NULL, "devs", show_devs},
  1636. {NULL, "dsts", show_dsts},
  1637. {NULL, "call", show_call},
  1638. };
  1639. static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
  1640. {
  1641. struct asus_wmi_debugfs_node *node = inode->i_private;
  1642. return single_open(file, node->show, node->asus);
  1643. }
  1644. static const struct file_operations asus_wmi_debugfs_io_ops = {
  1645. .owner = THIS_MODULE,
  1646. .open = asus_wmi_debugfs_open,
  1647. .read = seq_read,
  1648. .llseek = seq_lseek,
  1649. .release = single_release,
  1650. };
  1651. static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
  1652. {
  1653. debugfs_remove_recursive(asus->debug.root);
  1654. }
  1655. static int asus_wmi_debugfs_init(struct asus_wmi *asus)
  1656. {
  1657. struct dentry *dent;
  1658. int i;
  1659. asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
  1660. if (!asus->debug.root) {
  1661. pr_err("failed to create debugfs directory\n");
  1662. goto error_debugfs;
  1663. }
  1664. dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
  1665. asus->debug.root, &asus->debug.method_id);
  1666. if (!dent)
  1667. goto error_debugfs;
  1668. dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
  1669. asus->debug.root, &asus->debug.dev_id);
  1670. if (!dent)
  1671. goto error_debugfs;
  1672. dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
  1673. asus->debug.root, &asus->debug.ctrl_param);
  1674. if (!dent)
  1675. goto error_debugfs;
  1676. for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
  1677. struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
  1678. node->asus = asus;
  1679. dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  1680. asus->debug.root, node,
  1681. &asus_wmi_debugfs_io_ops);
  1682. if (!dent) {
  1683. pr_err("failed to create debug file: %s\n", node->name);
  1684. goto error_debugfs;
  1685. }
  1686. }
  1687. return 0;
  1688. error_debugfs:
  1689. asus_wmi_debugfs_exit(asus);
  1690. return -ENOMEM;
  1691. }
  1692. static int asus_wmi_fan_init(struct asus_wmi *asus)
  1693. {
  1694. int status;
  1695. asus->asus_hwmon_pwm = -1;
  1696. asus->asus_hwmon_num_fans = -1;
  1697. asus->asus_hwmon_fan_manual_mode = false;
  1698. status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
  1699. if (status) {
  1700. asus->asus_hwmon_num_fans = 0;
  1701. pr_warn("Could not determine number of fans: %d\n", status);
  1702. return -ENXIO;
  1703. }
  1704. pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
  1705. return 0;
  1706. }
  1707. static bool ashs_present(void)
  1708. {
  1709. int i = 0;
  1710. while (ashs_ids[i]) {
  1711. if (acpi_dev_found(ashs_ids[i++]))
  1712. return true;
  1713. }
  1714. return false;
  1715. }
  1716. /*
  1717. * WMI Driver
  1718. */
  1719. static int asus_wmi_add(struct platform_device *pdev)
  1720. {
  1721. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  1722. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  1723. struct asus_wmi *asus;
  1724. const char *chassis_type;
  1725. acpi_status status;
  1726. int err;
  1727. u32 result;
  1728. asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
  1729. if (!asus)
  1730. return -ENOMEM;
  1731. asus->driver = wdrv;
  1732. asus->platform_device = pdev;
  1733. wdrv->platform_device = pdev;
  1734. platform_set_drvdata(asus->platform_device, asus);
  1735. if (wdrv->detect_quirks)
  1736. wdrv->detect_quirks(asus->driver);
  1737. err = asus_wmi_platform_init(asus);
  1738. if (err)
  1739. goto fail_platform;
  1740. err = asus_wmi_input_init(asus);
  1741. if (err)
  1742. goto fail_input;
  1743. err = asus_wmi_fan_init(asus); /* probably no problems on error */
  1744. asus_hwmon_fan_set_auto(asus);
  1745. err = asus_wmi_hwmon_init(asus);
  1746. if (err)
  1747. goto fail_hwmon;
  1748. err = asus_wmi_led_init(asus);
  1749. if (err)
  1750. goto fail_leds;
  1751. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
  1752. if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
  1753. asus->driver->wlan_ctrl_by_user = 1;
  1754. if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
  1755. err = asus_wmi_rfkill_init(asus);
  1756. if (err)
  1757. goto fail_rfkill;
  1758. }
  1759. if (asus->driver->quirks->wmi_force_als_set)
  1760. asus_wmi_set_als();
  1761. /* Some Asus desktop boards export an acpi-video backlight interface,
  1762. stop this from showing up */
  1763. chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
  1764. if (chassis_type && !strcmp(chassis_type, "3"))
  1765. acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
  1766. if (asus->driver->quirks->wmi_backlight_power)
  1767. acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
  1768. if (asus->driver->quirks->wmi_backlight_native)
  1769. acpi_video_set_dmi_backlight_type(acpi_backlight_native);
  1770. if (asus->driver->quirks->xusb2pr)
  1771. asus_wmi_set_xusb2pr(asus);
  1772. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  1773. err = asus_wmi_backlight_init(asus);
  1774. if (err && err != -ENODEV)
  1775. goto fail_backlight;
  1776. }
  1777. status = wmi_install_notify_handler(asus->driver->event_guid,
  1778. asus_wmi_notify, asus);
  1779. if (ACPI_FAILURE(status)) {
  1780. pr_err("Unable to register notify handler - %d\n", status);
  1781. err = -ENODEV;
  1782. goto fail_wmi_handler;
  1783. }
  1784. err = asus_wmi_debugfs_init(asus);
  1785. if (err)
  1786. goto fail_debugfs;
  1787. return 0;
  1788. fail_debugfs:
  1789. wmi_remove_notify_handler(asus->driver->event_guid);
  1790. fail_wmi_handler:
  1791. asus_wmi_backlight_exit(asus);
  1792. fail_backlight:
  1793. asus_wmi_rfkill_exit(asus);
  1794. fail_rfkill:
  1795. asus_wmi_led_exit(asus);
  1796. fail_leds:
  1797. fail_hwmon:
  1798. asus_wmi_input_exit(asus);
  1799. fail_input:
  1800. asus_wmi_platform_exit(asus);
  1801. fail_platform:
  1802. kfree(asus);
  1803. return err;
  1804. }
  1805. static int asus_wmi_remove(struct platform_device *device)
  1806. {
  1807. struct asus_wmi *asus;
  1808. asus = platform_get_drvdata(device);
  1809. wmi_remove_notify_handler(asus->driver->event_guid);
  1810. asus_wmi_backlight_exit(asus);
  1811. asus_wmi_input_exit(asus);
  1812. asus_wmi_led_exit(asus);
  1813. asus_wmi_rfkill_exit(asus);
  1814. asus_wmi_debugfs_exit(asus);
  1815. asus_wmi_platform_exit(asus);
  1816. asus_hwmon_fan_set_auto(asus);
  1817. kfree(asus);
  1818. return 0;
  1819. }
  1820. /*
  1821. * Platform driver - hibernate/resume callbacks
  1822. */
  1823. static int asus_hotk_thaw(struct device *device)
  1824. {
  1825. struct asus_wmi *asus = dev_get_drvdata(device);
  1826. if (asus->wlan.rfkill) {
  1827. bool wlan;
  1828. /*
  1829. * Work around bios bug - acpi _PTS turns off the wireless led
  1830. * during suspend. Normally it restores it on resume, but
  1831. * we should kick it ourselves in case hibernation is aborted.
  1832. */
  1833. wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  1834. asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
  1835. }
  1836. return 0;
  1837. }
  1838. static int asus_hotk_resume(struct device *device)
  1839. {
  1840. struct asus_wmi *asus = dev_get_drvdata(device);
  1841. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  1842. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  1843. return 0;
  1844. }
  1845. static int asus_hotk_restore(struct device *device)
  1846. {
  1847. struct asus_wmi *asus = dev_get_drvdata(device);
  1848. int bl;
  1849. /* Refresh both wlan rfkill state and pci hotplug */
  1850. if (asus->wlan.rfkill)
  1851. asus_rfkill_hotplug(asus);
  1852. if (asus->bluetooth.rfkill) {
  1853. bl = !asus_wmi_get_devstate_simple(asus,
  1854. ASUS_WMI_DEVID_BLUETOOTH);
  1855. rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
  1856. }
  1857. if (asus->wimax.rfkill) {
  1858. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
  1859. rfkill_set_sw_state(asus->wimax.rfkill, bl);
  1860. }
  1861. if (asus->wwan3g.rfkill) {
  1862. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
  1863. rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
  1864. }
  1865. if (asus->gps.rfkill) {
  1866. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
  1867. rfkill_set_sw_state(asus->gps.rfkill, bl);
  1868. }
  1869. if (asus->uwb.rfkill) {
  1870. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
  1871. rfkill_set_sw_state(asus->uwb.rfkill, bl);
  1872. }
  1873. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  1874. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  1875. return 0;
  1876. }
  1877. static const struct dev_pm_ops asus_pm_ops = {
  1878. .thaw = asus_hotk_thaw,
  1879. .restore = asus_hotk_restore,
  1880. .resume = asus_hotk_resume,
  1881. };
  1882. static int asus_wmi_probe(struct platform_device *pdev)
  1883. {
  1884. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  1885. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  1886. int ret;
  1887. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  1888. pr_warn("Management GUID not found\n");
  1889. return -ENODEV;
  1890. }
  1891. if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
  1892. pr_warn("Event GUID not found\n");
  1893. return -ENODEV;
  1894. }
  1895. if (wdrv->probe) {
  1896. ret = wdrv->probe(pdev);
  1897. if (ret)
  1898. return ret;
  1899. }
  1900. return asus_wmi_add(pdev);
  1901. }
  1902. static bool used;
  1903. int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
  1904. {
  1905. struct platform_driver *platform_driver;
  1906. struct platform_device *platform_device;
  1907. if (used)
  1908. return -EBUSY;
  1909. platform_driver = &driver->platform_driver;
  1910. platform_driver->remove = asus_wmi_remove;
  1911. platform_driver->driver.owner = driver->owner;
  1912. platform_driver->driver.name = driver->name;
  1913. platform_driver->driver.pm = &asus_pm_ops;
  1914. platform_device = platform_create_bundle(platform_driver,
  1915. asus_wmi_probe,
  1916. NULL, 0, NULL, 0);
  1917. if (IS_ERR(platform_device))
  1918. return PTR_ERR(platform_device);
  1919. used = true;
  1920. return 0;
  1921. }
  1922. EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
  1923. void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
  1924. {
  1925. platform_device_unregister(driver->platform_device);
  1926. platform_driver_unregister(&driver->platform_driver);
  1927. used = false;
  1928. }
  1929. EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
  1930. static int __init asus_wmi_init(void)
  1931. {
  1932. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  1933. pr_info("Asus Management GUID not found\n");
  1934. return -ENODEV;
  1935. }
  1936. pr_info("ASUS WMI generic driver loaded\n");
  1937. return 0;
  1938. }
  1939. static void __exit asus_wmi_exit(void)
  1940. {
  1941. pr_info("ASUS WMI generic driver unloaded\n");
  1942. }
  1943. module_init(asus_wmi_init);
  1944. module_exit(asus_wmi_exit);