asus-wmi.c 45 KB

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