asus-wmi.c 57 KB

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