asus-laptop.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  1. /*
  2. * asus-laptop.c - Asus Laptop Support
  3. *
  4. *
  5. * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
  6. * Copyright (C) 2006-2007 Corentin Chary
  7. * Copyright (C) 2011 Wind River Systems
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. *
  24. * The development page for this driver is located at
  25. * http://sourceforge.net/projects/acpi4asus/
  26. *
  27. * Credits:
  28. * Pontus Fuchs - Helper functions, cleanup
  29. * Johann Wiesner - Small compile fixes
  30. * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
  31. * Eric Burghard - LED display support for W1N
  32. * Josh Green - Light Sens support
  33. * Thomas Tuttle - His first patch for led support was very helpful
  34. * Sam Lin - GPS support
  35. */
  36. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/types.h>
  41. #include <linux/err.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/backlight.h>
  44. #include <linux/fb.h>
  45. #include <linux/leds.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/uaccess.h>
  48. #include <linux/input.h>
  49. #include <linux/input/sparse-keymap.h>
  50. #include <linux/input-polldev.h>
  51. #include <linux/rfkill.h>
  52. #include <linux/slab.h>
  53. #include <linux/dmi.h>
  54. #include <acpi/acpi_drivers.h>
  55. #include <acpi/acpi_bus.h>
  56. #define ASUS_LAPTOP_VERSION "0.42"
  57. #define ASUS_LAPTOP_NAME "Asus Laptop Support"
  58. #define ASUS_LAPTOP_CLASS "hotkey"
  59. #define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
  60. #define ASUS_LAPTOP_FILE KBUILD_MODNAME
  61. #define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
  62. MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
  63. MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
  64. MODULE_LICENSE("GPL");
  65. /*
  66. * WAPF defines the behavior of the Fn+Fx wlan key
  67. * The significance of values is yet to be found, but
  68. * most of the time:
  69. * Bit | Bluetooth | WLAN
  70. * 0 | Hardware | Hardware
  71. * 1 | Hardware | Software
  72. * 4 | Software | Software
  73. */
  74. static uint wapf = 1;
  75. module_param(wapf, uint, 0444);
  76. MODULE_PARM_DESC(wapf, "WAPF value");
  77. static char *wled_type = "unknown";
  78. static char *bled_type = "unknown";
  79. module_param(wled_type, charp, 0444);
  80. MODULE_PARM_DESC(wled_type, "Set the wled type on boot "
  81. "(unknown, led or rfkill). "
  82. "default is unknown");
  83. module_param(bled_type, charp, 0444);
  84. MODULE_PARM_DESC(bled_type, "Set the bled type on boot "
  85. "(unknown, led or rfkill). "
  86. "default is unknown");
  87. static int wlan_status = 1;
  88. static int bluetooth_status = 1;
  89. static int wimax_status = -1;
  90. static int wwan_status = -1;
  91. static int als_status;
  92. module_param(wlan_status, int, 0444);
  93. MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
  94. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  95. "default is -1");
  96. module_param(bluetooth_status, int, 0444);
  97. MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
  98. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  99. "default is -1");
  100. module_param(wimax_status, int, 0444);
  101. MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
  102. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  103. "default is -1");
  104. module_param(wwan_status, int, 0444);
  105. MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
  106. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  107. "default is -1");
  108. module_param(als_status, int, 0444);
  109. MODULE_PARM_DESC(als_status, "Set the ALS status on boot "
  110. "(0 = disabled, 1 = enabled). "
  111. "default is 0");
  112. /*
  113. * Some events we use, same for all Asus
  114. */
  115. #define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
  116. #define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
  117. #define ATKD_BR_MIN ATKD_BR_UP
  118. #define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
  119. #define ATKD_LCD_ON 0x33
  120. #define ATKD_LCD_OFF 0x34
  121. /*
  122. * Known bits returned by \_SB.ATKD.HWRS
  123. */
  124. #define WL_HWRS 0x80
  125. #define BT_HWRS 0x100
  126. /*
  127. * Flags for hotk status
  128. * WL_ON and BT_ON are also used for wireless_status()
  129. */
  130. #define WL_RSTS 0x01 /* internal Wifi */
  131. #define BT_RSTS 0x02 /* internal Bluetooth */
  132. #define WM_RSTS 0x08 /* internal wimax */
  133. #define WW_RSTS 0x20 /* internal wwan */
  134. /* WLED and BLED type */
  135. #define TYPE_UNKNOWN 0
  136. #define TYPE_LED 1
  137. #define TYPE_RFKILL 2
  138. /* LED */
  139. #define METHOD_MLED "MLED"
  140. #define METHOD_TLED "TLED"
  141. #define METHOD_RLED "RLED" /* W1JC */
  142. #define METHOD_PLED "PLED" /* A7J */
  143. #define METHOD_GLED "GLED" /* G1, G2 (probably) */
  144. /* LEDD */
  145. #define METHOD_LEDD "SLCM"
  146. /*
  147. * Bluetooth and WLAN
  148. * WLED and BLED are not handled like other XLED, because in some dsdt
  149. * they also control the WLAN/Bluetooth device.
  150. */
  151. #define METHOD_WLAN "WLED"
  152. #define METHOD_BLUETOOTH "BLED"
  153. /* WWAN and WIMAX */
  154. #define METHOD_WWAN "GSMC"
  155. #define METHOD_WIMAX "WMXC"
  156. #define METHOD_WL_STATUS "RSTS"
  157. /* Brightness */
  158. #define METHOD_BRIGHTNESS_SET "SPLV"
  159. #define METHOD_BRIGHTNESS_GET "GPLV"
  160. /* Display */
  161. #define METHOD_SWITCH_DISPLAY "SDSP"
  162. #define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
  163. #define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
  164. /* GPS */
  165. /* R2H use different handle for GPS on/off */
  166. #define METHOD_GPS_ON "SDON"
  167. #define METHOD_GPS_OFF "SDOF"
  168. #define METHOD_GPS_STATUS "GPST"
  169. /* Keyboard light */
  170. #define METHOD_KBD_LIGHT_SET "SLKB"
  171. #define METHOD_KBD_LIGHT_GET "GLKB"
  172. /* For Pegatron Lucid tablet */
  173. #define DEVICE_NAME_PEGA "Lucid"
  174. #define METHOD_PEGA_ENABLE "ENPR"
  175. #define METHOD_PEGA_DISABLE "DAPR"
  176. #define PEGA_WLAN 0x00
  177. #define PEGA_BLUETOOTH 0x01
  178. #define PEGA_WWAN 0x02
  179. #define PEGA_ALS 0x04
  180. #define PEGA_ALS_POWER 0x05
  181. #define METHOD_PEGA_READ "RDLN"
  182. #define PEGA_READ_ALS_H 0x02
  183. #define PEGA_READ_ALS_L 0x03
  184. #define PEGA_ACCEL_NAME "pega_accel"
  185. #define PEGA_ACCEL_DESC "Pegatron Lucid Tablet Accelerometer"
  186. #define METHOD_XLRX "XLRX"
  187. #define METHOD_XLRY "XLRY"
  188. #define METHOD_XLRZ "XLRZ"
  189. #define PEGA_ACC_CLAMP 512 /* 1G accel is reported as ~256, so clamp to 2G */
  190. #define PEGA_ACC_RETRIES 3
  191. /*
  192. * Define a specific led structure to keep the main structure clean
  193. */
  194. struct asus_led {
  195. int wk;
  196. struct work_struct work;
  197. struct led_classdev led;
  198. struct asus_laptop *asus;
  199. const char *method;
  200. };
  201. /*
  202. * Same thing for rfkill
  203. */
  204. struct asus_rfkill {
  205. /* type of control. Maps to PEGA_* values or *_RSTS */
  206. int control_id;
  207. struct rfkill *rfkill;
  208. struct asus_laptop *asus;
  209. };
  210. /*
  211. * This is the main structure, we can use it to store anything interesting
  212. * about the hotk device
  213. */
  214. struct asus_laptop {
  215. char *name; /* laptop name */
  216. struct acpi_table_header *dsdt_info;
  217. struct platform_device *platform_device;
  218. struct acpi_device *device; /* the device we are in */
  219. struct backlight_device *backlight_device;
  220. struct input_dev *inputdev;
  221. struct key_entry *keymap;
  222. struct input_polled_dev *pega_accel_poll;
  223. struct asus_led wled;
  224. struct asus_led bled;
  225. struct asus_led mled;
  226. struct asus_led tled;
  227. struct asus_led rled;
  228. struct asus_led pled;
  229. struct asus_led gled;
  230. struct asus_led kled;
  231. struct workqueue_struct *led_workqueue;
  232. int wled_type;
  233. int bled_type;
  234. int wireless_status;
  235. bool have_rsts;
  236. bool is_pega_lucid;
  237. bool pega_acc_live;
  238. int pega_acc_x;
  239. int pega_acc_y;
  240. int pega_acc_z;
  241. struct asus_rfkill wlan;
  242. struct asus_rfkill bluetooth;
  243. struct asus_rfkill wwan;
  244. struct asus_rfkill wimax;
  245. struct asus_rfkill gps;
  246. acpi_handle handle; /* the handle of the hotk device */
  247. u32 ledd_status; /* status of the LED display */
  248. u8 light_level; /* light sensor level */
  249. u8 light_switch; /* light sensor switch value */
  250. u16 event_count[128]; /* count for each event TODO make this better */
  251. };
  252. static const struct key_entry asus_keymap[] = {
  253. /* Lenovo SL Specific keycodes */
  254. {KE_KEY, 0x02, { KEY_SCREENLOCK } },
  255. {KE_KEY, 0x05, { KEY_WLAN } },
  256. {KE_KEY, 0x08, { KEY_F13 } },
  257. {KE_KEY, 0x09, { KEY_PROG2 } }, /* Dock */
  258. {KE_KEY, 0x17, { KEY_ZOOM } },
  259. {KE_KEY, 0x1f, { KEY_BATTERY } },
  260. /* End of Lenovo SL Specific keycodes */
  261. {KE_KEY, 0x30, { KEY_VOLUMEUP } },
  262. {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
  263. {KE_KEY, 0x32, { KEY_MUTE } },
  264. {KE_KEY, 0x33, { KEY_DISPLAYTOGGLE } }, /* LCD on */
  265. {KE_KEY, 0x34, { KEY_DISPLAY_OFF } }, /* LCD off */
  266. {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
  267. {KE_KEY, 0x41, { KEY_NEXTSONG } },
  268. {KE_KEY, 0x43, { KEY_STOPCD } }, /* Stop/Eject */
  269. {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
  270. {KE_KEY, 0x4c, { KEY_MEDIA } }, /* WMP Key */
  271. {KE_KEY, 0x50, { KEY_EMAIL } },
  272. {KE_KEY, 0x51, { KEY_WWW } },
  273. {KE_KEY, 0x55, { KEY_CALC } },
  274. {KE_IGNORE, 0x57, }, /* Battery mode */
  275. {KE_IGNORE, 0x58, }, /* AC mode */
  276. {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
  277. {KE_KEY, 0x5D, { KEY_WLAN } }, /* WLAN Toggle */
  278. {KE_KEY, 0x5E, { KEY_WLAN } }, /* WLAN Enable */
  279. {KE_KEY, 0x5F, { KEY_WLAN } }, /* WLAN Disable */
  280. {KE_KEY, 0x60, { KEY_TOUCHPAD_ON } },
  281. {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD only */
  282. {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT only */
  283. {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT */
  284. {KE_KEY, 0x64, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV */
  285. {KE_KEY, 0x65, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV */
  286. {KE_KEY, 0x66, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV */
  287. {KE_KEY, 0x67, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV */
  288. {KE_KEY, 0x6B, { KEY_TOUCHPAD_TOGGLE } }, /* Lock Touchpad */
  289. {KE_KEY, 0x6C, { KEY_SLEEP } }, /* Suspend */
  290. {KE_KEY, 0x6D, { KEY_SLEEP } }, /* Hibernate */
  291. {KE_IGNORE, 0x6E, }, /* Low Battery notification */
  292. {KE_KEY, 0x7D, { KEY_BLUETOOTH } }, /* Bluetooth Enable */
  293. {KE_KEY, 0x7E, { KEY_BLUETOOTH } }, /* Bluetooth Disable */
  294. {KE_KEY, 0x82, { KEY_CAMERA } },
  295. {KE_KEY, 0x88, { KEY_RFKILL } }, /* Radio Toggle Key */
  296. {KE_KEY, 0x8A, { KEY_PROG1 } }, /* Color enhancement mode */
  297. {KE_KEY, 0x8C, { KEY_SWITCHVIDEOMODE } }, /* SDSP DVI only */
  298. {KE_KEY, 0x8D, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + DVI */
  299. {KE_KEY, 0x8E, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + DVI */
  300. {KE_KEY, 0x8F, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV + DVI */
  301. {KE_KEY, 0x90, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + DVI */
  302. {KE_KEY, 0x91, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV + DVI */
  303. {KE_KEY, 0x92, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV + DVI */
  304. {KE_KEY, 0x93, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV + DVI */
  305. {KE_KEY, 0x95, { KEY_MEDIA } },
  306. {KE_KEY, 0x99, { KEY_PHONE } },
  307. {KE_KEY, 0xA0, { KEY_SWITCHVIDEOMODE } }, /* SDSP HDMI only */
  308. {KE_KEY, 0xA1, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + HDMI */
  309. {KE_KEY, 0xA2, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + HDMI */
  310. {KE_KEY, 0xA3, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV + HDMI */
  311. {KE_KEY, 0xA4, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + HDMI */
  312. {KE_KEY, 0xA5, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV + HDMI */
  313. {KE_KEY, 0xA6, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV + HDMI */
  314. {KE_KEY, 0xA7, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV + HDMI */
  315. {KE_KEY, 0xB5, { KEY_CALC } },
  316. {KE_KEY, 0xC4, { KEY_KBDILLUMUP } },
  317. {KE_KEY, 0xC5, { KEY_KBDILLUMDOWN } },
  318. {KE_END, 0},
  319. };
  320. /*
  321. * This function evaluates an ACPI method, given an int as parameter, the
  322. * method is searched within the scope of the handle, can be NULL. The output
  323. * of the method is written is output, which can also be NULL
  324. *
  325. * returns 0 if write is successful, -1 else.
  326. */
  327. static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
  328. struct acpi_buffer *output)
  329. {
  330. struct acpi_object_list params; /* list of input parameters (an int) */
  331. union acpi_object in_obj; /* the only param we use */
  332. acpi_status status;
  333. if (!handle)
  334. return -1;
  335. params.count = 1;
  336. params.pointer = &in_obj;
  337. in_obj.type = ACPI_TYPE_INTEGER;
  338. in_obj.integer.value = val;
  339. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  340. if (status == AE_OK)
  341. return 0;
  342. else
  343. return -1;
  344. }
  345. static int write_acpi_int(acpi_handle handle, const char *method, int val)
  346. {
  347. return write_acpi_int_ret(handle, method, val, NULL);
  348. }
  349. static int acpi_check_handle(acpi_handle handle, const char *method,
  350. acpi_handle *ret)
  351. {
  352. acpi_status status;
  353. if (method == NULL)
  354. return -ENODEV;
  355. if (ret)
  356. status = acpi_get_handle(handle, (char *)method,
  357. ret);
  358. else {
  359. acpi_handle dummy;
  360. status = acpi_get_handle(handle, (char *)method,
  361. &dummy);
  362. }
  363. if (status != AE_OK) {
  364. if (ret)
  365. pr_warn("Error finding %s\n", method);
  366. return -ENODEV;
  367. }
  368. return 0;
  369. }
  370. static bool asus_check_pega_lucid(struct asus_laptop *asus)
  371. {
  372. return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
  373. !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
  374. !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
  375. !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
  376. }
  377. static int asus_pega_lucid_set(struct asus_laptop *asus, int unit, bool enable)
  378. {
  379. char *method = enable ? METHOD_PEGA_ENABLE : METHOD_PEGA_DISABLE;
  380. return write_acpi_int(asus->handle, method, unit);
  381. }
  382. static int pega_acc_axis(struct asus_laptop *asus, int curr, char *method)
  383. {
  384. int i, delta;
  385. unsigned long long val;
  386. for (i = 0; i < PEGA_ACC_RETRIES; i++) {
  387. acpi_evaluate_integer(asus->handle, method, NULL, &val);
  388. /* The output is noisy. From reading the ASL
  389. * dissassembly, timeout errors are returned with 1's
  390. * in the high word, and the lack of locking around
  391. * thei hi/lo byte reads means that a transition
  392. * between (for example) -1 and 0 could be read as
  393. * 0xff00 or 0x00ff. */
  394. delta = abs(curr - (short)val);
  395. if (delta < 128 && !(val & ~0xffff))
  396. break;
  397. }
  398. return clamp_val((short)val, -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP);
  399. }
  400. static void pega_accel_poll(struct input_polled_dev *ipd)
  401. {
  402. struct device *parent = ipd->input->dev.parent;
  403. struct asus_laptop *asus = dev_get_drvdata(parent);
  404. /* In some cases, the very first call to poll causes a
  405. * recursive fault under the polldev worker. This is
  406. * apparently related to very early userspace access to the
  407. * device, and perhaps a firmware bug. Fake the first report. */
  408. if (!asus->pega_acc_live) {
  409. asus->pega_acc_live = true;
  410. input_report_abs(ipd->input, ABS_X, 0);
  411. input_report_abs(ipd->input, ABS_Y, 0);
  412. input_report_abs(ipd->input, ABS_Z, 0);
  413. input_sync(ipd->input);
  414. return;
  415. }
  416. asus->pega_acc_x = pega_acc_axis(asus, asus->pega_acc_x, METHOD_XLRX);
  417. asus->pega_acc_y = pega_acc_axis(asus, asus->pega_acc_y, METHOD_XLRY);
  418. asus->pega_acc_z = pega_acc_axis(asus, asus->pega_acc_z, METHOD_XLRZ);
  419. /* Note transform, convert to "right/up/out" in the native
  420. * landscape orientation (i.e. the vector is the direction of
  421. * "real up" in the device's cartiesian coordinates). */
  422. input_report_abs(ipd->input, ABS_X, -asus->pega_acc_x);
  423. input_report_abs(ipd->input, ABS_Y, -asus->pega_acc_y);
  424. input_report_abs(ipd->input, ABS_Z, asus->pega_acc_z);
  425. input_sync(ipd->input);
  426. }
  427. static void pega_accel_exit(struct asus_laptop *asus)
  428. {
  429. if (asus->pega_accel_poll) {
  430. input_unregister_polled_device(asus->pega_accel_poll);
  431. input_free_polled_device(asus->pega_accel_poll);
  432. }
  433. asus->pega_accel_poll = NULL;
  434. }
  435. static int pega_accel_init(struct asus_laptop *asus)
  436. {
  437. int err;
  438. struct input_polled_dev *ipd;
  439. if (!asus->is_pega_lucid)
  440. return -ENODEV;
  441. if (acpi_check_handle(asus->handle, METHOD_XLRX, NULL) ||
  442. acpi_check_handle(asus->handle, METHOD_XLRY, NULL) ||
  443. acpi_check_handle(asus->handle, METHOD_XLRZ, NULL))
  444. return -ENODEV;
  445. ipd = input_allocate_polled_device();
  446. if (!ipd)
  447. return -ENOMEM;
  448. ipd->poll = pega_accel_poll;
  449. ipd->poll_interval = 125;
  450. ipd->poll_interval_min = 50;
  451. ipd->poll_interval_max = 2000;
  452. ipd->input->name = PEGA_ACCEL_DESC;
  453. ipd->input->phys = PEGA_ACCEL_NAME "/input0";
  454. ipd->input->dev.parent = &asus->platform_device->dev;
  455. ipd->input->id.bustype = BUS_HOST;
  456. set_bit(EV_ABS, ipd->input->evbit);
  457. input_set_abs_params(ipd->input, ABS_X,
  458. -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
  459. input_set_abs_params(ipd->input, ABS_Y,
  460. -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
  461. input_set_abs_params(ipd->input, ABS_Z,
  462. -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
  463. err = input_register_polled_device(ipd);
  464. if (err)
  465. goto exit;
  466. asus->pega_accel_poll = ipd;
  467. return 0;
  468. exit:
  469. input_free_polled_device(ipd);
  470. return err;
  471. }
  472. /* Generic LED function */
  473. static int asus_led_set(struct asus_laptop *asus, const char *method,
  474. int value)
  475. {
  476. if (!strcmp(method, METHOD_MLED))
  477. value = !value;
  478. else if (!strcmp(method, METHOD_GLED))
  479. value = !value + 1;
  480. else
  481. value = !!value;
  482. return write_acpi_int(asus->handle, method, value);
  483. }
  484. /*
  485. * LEDs
  486. */
  487. /* /sys/class/led handlers */
  488. static void asus_led_cdev_set(struct led_classdev *led_cdev,
  489. enum led_brightness value)
  490. {
  491. struct asus_led *led = container_of(led_cdev, struct asus_led, led);
  492. struct asus_laptop *asus = led->asus;
  493. led->wk = !!value;
  494. queue_work(asus->led_workqueue, &led->work);
  495. }
  496. static void asus_led_cdev_update(struct work_struct *work)
  497. {
  498. struct asus_led *led = container_of(work, struct asus_led, work);
  499. struct asus_laptop *asus = led->asus;
  500. asus_led_set(asus, led->method, led->wk);
  501. }
  502. static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
  503. {
  504. return led_cdev->brightness;
  505. }
  506. /*
  507. * Keyboard backlight (also a LED)
  508. */
  509. static int asus_kled_lvl(struct asus_laptop *asus)
  510. {
  511. unsigned long long kblv;
  512. struct acpi_object_list params;
  513. union acpi_object in_obj;
  514. acpi_status rv;
  515. params.count = 1;
  516. params.pointer = &in_obj;
  517. in_obj.type = ACPI_TYPE_INTEGER;
  518. in_obj.integer.value = 2;
  519. rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
  520. &params, &kblv);
  521. if (ACPI_FAILURE(rv)) {
  522. pr_warn("Error reading kled level\n");
  523. return -ENODEV;
  524. }
  525. return kblv;
  526. }
  527. static int asus_kled_set(struct asus_laptop *asus, int kblv)
  528. {
  529. if (kblv > 0)
  530. kblv = (1 << 7) | (kblv & 0x7F);
  531. else
  532. kblv = 0;
  533. if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
  534. pr_warn("Keyboard LED display write failed\n");
  535. return -EINVAL;
  536. }
  537. return 0;
  538. }
  539. static void asus_kled_cdev_set(struct led_classdev *led_cdev,
  540. enum led_brightness value)
  541. {
  542. struct asus_led *led = container_of(led_cdev, struct asus_led, led);
  543. struct asus_laptop *asus = led->asus;
  544. led->wk = value;
  545. queue_work(asus->led_workqueue, &led->work);
  546. }
  547. static void asus_kled_cdev_update(struct work_struct *work)
  548. {
  549. struct asus_led *led = container_of(work, struct asus_led, work);
  550. struct asus_laptop *asus = led->asus;
  551. asus_kled_set(asus, led->wk);
  552. }
  553. static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
  554. {
  555. struct asus_led *led = container_of(led_cdev, struct asus_led, led);
  556. struct asus_laptop *asus = led->asus;
  557. return asus_kled_lvl(asus);
  558. }
  559. static void asus_led_exit(struct asus_laptop *asus)
  560. {
  561. if (!IS_ERR_OR_NULL(asus->wled.led.dev))
  562. led_classdev_unregister(&asus->wled.led);
  563. if (!IS_ERR_OR_NULL(asus->bled.led.dev))
  564. led_classdev_unregister(&asus->bled.led);
  565. if (!IS_ERR_OR_NULL(asus->mled.led.dev))
  566. led_classdev_unregister(&asus->mled.led);
  567. if (!IS_ERR_OR_NULL(asus->tled.led.dev))
  568. led_classdev_unregister(&asus->tled.led);
  569. if (!IS_ERR_OR_NULL(asus->pled.led.dev))
  570. led_classdev_unregister(&asus->pled.led);
  571. if (!IS_ERR_OR_NULL(asus->rled.led.dev))
  572. led_classdev_unregister(&asus->rled.led);
  573. if (!IS_ERR_OR_NULL(asus->gled.led.dev))
  574. led_classdev_unregister(&asus->gled.led);
  575. if (!IS_ERR_OR_NULL(asus->kled.led.dev))
  576. led_classdev_unregister(&asus->kled.led);
  577. if (asus->led_workqueue) {
  578. destroy_workqueue(asus->led_workqueue);
  579. asus->led_workqueue = NULL;
  580. }
  581. }
  582. /* Ugly macro, need to fix that later */
  583. static int asus_led_register(struct asus_laptop *asus,
  584. struct asus_led *led,
  585. const char *name, const char *method)
  586. {
  587. struct led_classdev *led_cdev = &led->led;
  588. if (!method || acpi_check_handle(asus->handle, method, NULL))
  589. return 0; /* Led not present */
  590. led->asus = asus;
  591. led->method = method;
  592. INIT_WORK(&led->work, asus_led_cdev_update);
  593. led_cdev->name = name;
  594. led_cdev->brightness_set = asus_led_cdev_set;
  595. led_cdev->brightness_get = asus_led_cdev_get;
  596. led_cdev->max_brightness = 1;
  597. return led_classdev_register(&asus->platform_device->dev, led_cdev);
  598. }
  599. static int asus_led_init(struct asus_laptop *asus)
  600. {
  601. int r = 0;
  602. /*
  603. * The Pegatron Lucid has no physical leds, but all methods are
  604. * available in the DSDT...
  605. */
  606. if (asus->is_pega_lucid)
  607. return 0;
  608. /*
  609. * Functions that actually update the LED's are called from a
  610. * workqueue. By doing this as separate work rather than when the LED
  611. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  612. * potentially bad time, such as a timer interrupt.
  613. */
  614. asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
  615. if (!asus->led_workqueue)
  616. return -ENOMEM;
  617. if (asus->wled_type == TYPE_LED)
  618. r = asus_led_register(asus, &asus->wled, "asus::wlan",
  619. METHOD_WLAN);
  620. if (r)
  621. goto error;
  622. if (asus->bled_type == TYPE_LED)
  623. r = asus_led_register(asus, &asus->bled, "asus::bluetooth",
  624. METHOD_BLUETOOTH);
  625. if (r)
  626. goto error;
  627. r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
  628. if (r)
  629. goto error;
  630. r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
  631. if (r)
  632. goto error;
  633. r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
  634. if (r)
  635. goto error;
  636. r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
  637. if (r)
  638. goto error;
  639. r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
  640. if (r)
  641. goto error;
  642. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
  643. !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
  644. struct asus_led *led = &asus->kled;
  645. struct led_classdev *cdev = &led->led;
  646. led->asus = asus;
  647. INIT_WORK(&led->work, asus_kled_cdev_update);
  648. cdev->name = "asus::kbd_backlight";
  649. cdev->brightness_set = asus_kled_cdev_set;
  650. cdev->brightness_get = asus_kled_cdev_get;
  651. cdev->max_brightness = 3;
  652. r = led_classdev_register(&asus->platform_device->dev, cdev);
  653. }
  654. error:
  655. if (r)
  656. asus_led_exit(asus);
  657. return r;
  658. }
  659. /*
  660. * Backlight device
  661. */
  662. static int asus_read_brightness(struct backlight_device *bd)
  663. {
  664. struct asus_laptop *asus = bl_get_data(bd);
  665. unsigned long long value;
  666. acpi_status rv = AE_OK;
  667. rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
  668. NULL, &value);
  669. if (ACPI_FAILURE(rv))
  670. pr_warn("Error reading brightness\n");
  671. return value;
  672. }
  673. static int asus_set_brightness(struct backlight_device *bd, int value)
  674. {
  675. struct asus_laptop *asus = bl_get_data(bd);
  676. if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
  677. pr_warn("Error changing brightness\n");
  678. return -EIO;
  679. }
  680. return 0;
  681. }
  682. static int update_bl_status(struct backlight_device *bd)
  683. {
  684. int value = bd->props.brightness;
  685. return asus_set_brightness(bd, value);
  686. }
  687. static const struct backlight_ops asusbl_ops = {
  688. .get_brightness = asus_read_brightness,
  689. .update_status = update_bl_status,
  690. };
  691. static int asus_backlight_notify(struct asus_laptop *asus)
  692. {
  693. struct backlight_device *bd = asus->backlight_device;
  694. int old = bd->props.brightness;
  695. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  696. return old;
  697. }
  698. static int asus_backlight_init(struct asus_laptop *asus)
  699. {
  700. struct backlight_device *bd;
  701. struct backlight_properties props;
  702. if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
  703. acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
  704. return 0;
  705. memset(&props, 0, sizeof(struct backlight_properties));
  706. props.max_brightness = 15;
  707. props.type = BACKLIGHT_PLATFORM;
  708. bd = backlight_device_register(ASUS_LAPTOP_FILE,
  709. &asus->platform_device->dev, asus,
  710. &asusbl_ops, &props);
  711. if (IS_ERR(bd)) {
  712. pr_err("Could not register asus backlight device\n");
  713. asus->backlight_device = NULL;
  714. return PTR_ERR(bd);
  715. }
  716. asus->backlight_device = bd;
  717. bd->props.brightness = asus_read_brightness(bd);
  718. bd->props.power = FB_BLANK_UNBLANK;
  719. backlight_update_status(bd);
  720. return 0;
  721. }
  722. static void asus_backlight_exit(struct asus_laptop *asus)
  723. {
  724. if (asus->backlight_device)
  725. backlight_device_unregister(asus->backlight_device);
  726. asus->backlight_device = NULL;
  727. }
  728. /*
  729. * Platform device handlers
  730. */
  731. /*
  732. * We write our info in page, we begin at offset off and cannot write more
  733. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  734. * number of bytes written in page
  735. */
  736. static ssize_t show_infos(struct device *dev,
  737. struct device_attribute *attr, char *page)
  738. {
  739. struct asus_laptop *asus = dev_get_drvdata(dev);
  740. int len = 0;
  741. unsigned long long temp;
  742. char buf[16]; /* enough for all info */
  743. acpi_status rv = AE_OK;
  744. /*
  745. * We use the easy way, we don't care of off and count,
  746. * so we don't set eof to 1
  747. */
  748. len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
  749. len += sprintf(page + len, "Model reference : %s\n", asus->name);
  750. /*
  751. * The SFUN method probably allows the original driver to get the list
  752. * of features supported by a given model. For now, 0x0100 or 0x0800
  753. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  754. * The significance of others is yet to be found.
  755. */
  756. rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
  757. if (!ACPI_FAILURE(rv))
  758. len += sprintf(page + len, "SFUN value : %#x\n",
  759. (uint) temp);
  760. /*
  761. * The HWRS method return informations about the hardware.
  762. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  763. * 0x40 for WWAN, 0x10 for WIMAX.
  764. * The significance of others is yet to be found.
  765. * We don't currently use this for device detection, and it
  766. * takes several seconds to run on some systems.
  767. */
  768. rv = acpi_evaluate_integer(asus->handle, "HWRS", NULL, &temp);
  769. if (!ACPI_FAILURE(rv))
  770. len += sprintf(page + len, "HWRS value : %#x\n",
  771. (uint) temp);
  772. /*
  773. * Another value for userspace: the ASYM method returns 0x02 for
  774. * battery low and 0x04 for battery critical, its readings tend to be
  775. * more accurate than those provided by _BST.
  776. * Note: since not all the laptops provide this method, errors are
  777. * silently ignored.
  778. */
  779. rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
  780. if (!ACPI_FAILURE(rv))
  781. len += sprintf(page + len, "ASYM value : %#x\n",
  782. (uint) temp);
  783. if (asus->dsdt_info) {
  784. snprintf(buf, 16, "%d", asus->dsdt_info->length);
  785. len += sprintf(page + len, "DSDT length : %s\n", buf);
  786. snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
  787. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  788. snprintf(buf, 16, "%d", asus->dsdt_info->revision);
  789. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  790. snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
  791. len += sprintf(page + len, "OEM id : %s\n", buf);
  792. snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
  793. len += sprintf(page + len, "OEM table id : %s\n", buf);
  794. snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
  795. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  796. snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
  797. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  798. snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
  799. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  800. }
  801. return len;
  802. }
  803. static int parse_arg(const char *buf, unsigned long count, int *val)
  804. {
  805. if (!count)
  806. return 0;
  807. if (count > 31)
  808. return -EINVAL;
  809. if (sscanf(buf, "%i", val) != 1)
  810. return -EINVAL;
  811. return count;
  812. }
  813. static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
  814. const char *buf, size_t count,
  815. const char *method)
  816. {
  817. int rv, value;
  818. int out = 0;
  819. rv = parse_arg(buf, count, &value);
  820. if (rv > 0)
  821. out = value ? 1 : 0;
  822. if (write_acpi_int(asus->handle, method, value))
  823. return -ENODEV;
  824. return rv;
  825. }
  826. /*
  827. * LEDD display
  828. */
  829. static ssize_t show_ledd(struct device *dev,
  830. struct device_attribute *attr, char *buf)
  831. {
  832. struct asus_laptop *asus = dev_get_drvdata(dev);
  833. return sprintf(buf, "0x%08x\n", asus->ledd_status);
  834. }
  835. static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
  836. const char *buf, size_t count)
  837. {
  838. struct asus_laptop *asus = dev_get_drvdata(dev);
  839. int rv, value;
  840. rv = parse_arg(buf, count, &value);
  841. if (rv > 0) {
  842. if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
  843. pr_warn("LED display write failed\n");
  844. return -ENODEV;
  845. }
  846. asus->ledd_status = (u32) value;
  847. }
  848. return rv;
  849. }
  850. /*
  851. * Wireless
  852. */
  853. static int asus_wireless_status(struct asus_laptop *asus, int mask)
  854. {
  855. unsigned long long status;
  856. acpi_status rv = AE_OK;
  857. if (!asus->have_rsts)
  858. return (asus->wireless_status & mask) ? 1 : 0;
  859. rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
  860. NULL, &status);
  861. if (ACPI_FAILURE(rv)) {
  862. pr_warn("Error reading Wireless status\n");
  863. return -EINVAL;
  864. }
  865. return !!(status & mask);
  866. }
  867. /*
  868. * WLAN
  869. */
  870. static int asus_wlan_set(struct asus_laptop *asus, int status)
  871. {
  872. if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
  873. pr_warn("Error setting wlan status to %d\n", status);
  874. return -EIO;
  875. }
  876. return 0;
  877. }
  878. static ssize_t show_wlan(struct device *dev,
  879. struct device_attribute *attr, char *buf)
  880. {
  881. struct asus_laptop *asus = dev_get_drvdata(dev);
  882. return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
  883. }
  884. static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
  885. const char *buf, size_t count)
  886. {
  887. struct asus_laptop *asus = dev_get_drvdata(dev);
  888. return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
  889. }
  890. /*e
  891. * Bluetooth
  892. */
  893. static int asus_bluetooth_set(struct asus_laptop *asus, int status)
  894. {
  895. if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
  896. pr_warn("Error setting bluetooth status to %d\n", status);
  897. return -EIO;
  898. }
  899. return 0;
  900. }
  901. static ssize_t show_bluetooth(struct device *dev,
  902. struct device_attribute *attr, char *buf)
  903. {
  904. struct asus_laptop *asus = dev_get_drvdata(dev);
  905. return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
  906. }
  907. static ssize_t store_bluetooth(struct device *dev,
  908. struct device_attribute *attr, const char *buf,
  909. size_t count)
  910. {
  911. struct asus_laptop *asus = dev_get_drvdata(dev);
  912. return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
  913. }
  914. /*
  915. * Wimax
  916. */
  917. static int asus_wimax_set(struct asus_laptop *asus, int status)
  918. {
  919. if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
  920. pr_warn("Error setting wimax status to %d\n", status);
  921. return -EIO;
  922. }
  923. return 0;
  924. }
  925. static ssize_t show_wimax(struct device *dev,
  926. struct device_attribute *attr, char *buf)
  927. {
  928. struct asus_laptop *asus = dev_get_drvdata(dev);
  929. return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
  930. }
  931. static ssize_t store_wimax(struct device *dev,
  932. struct device_attribute *attr, const char *buf,
  933. size_t count)
  934. {
  935. struct asus_laptop *asus = dev_get_drvdata(dev);
  936. return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
  937. }
  938. /*
  939. * Wwan
  940. */
  941. static int asus_wwan_set(struct asus_laptop *asus, int status)
  942. {
  943. if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
  944. pr_warn("Error setting wwan status to %d\n", status);
  945. return -EIO;
  946. }
  947. return 0;
  948. }
  949. static ssize_t show_wwan(struct device *dev,
  950. struct device_attribute *attr, char *buf)
  951. {
  952. struct asus_laptop *asus = dev_get_drvdata(dev);
  953. return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
  954. }
  955. static ssize_t store_wwan(struct device *dev,
  956. struct device_attribute *attr, const char *buf,
  957. size_t count)
  958. {
  959. struct asus_laptop *asus = dev_get_drvdata(dev);
  960. return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
  961. }
  962. /*
  963. * Display
  964. */
  965. static void asus_set_display(struct asus_laptop *asus, int value)
  966. {
  967. /* no sanity check needed for now */
  968. if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
  969. pr_warn("Error setting display\n");
  970. return;
  971. }
  972. /*
  973. * Experimental support for display switching. As of now: 1 should activate
  974. * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
  975. * Any combination (bitwise) of these will suffice. I never actually tested 4
  976. * displays hooked up simultaneously, so be warned. See the acpi4asus README
  977. * for more info.
  978. */
  979. static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
  980. const char *buf, size_t count)
  981. {
  982. struct asus_laptop *asus = dev_get_drvdata(dev);
  983. int rv, value;
  984. rv = parse_arg(buf, count, &value);
  985. if (rv > 0)
  986. asus_set_display(asus, value);
  987. return rv;
  988. }
  989. /*
  990. * Light Sens
  991. */
  992. static void asus_als_switch(struct asus_laptop *asus, int value)
  993. {
  994. int ret;
  995. if (asus->is_pega_lucid) {
  996. ret = asus_pega_lucid_set(asus, PEGA_ALS, value);
  997. if (!ret)
  998. ret = asus_pega_lucid_set(asus, PEGA_ALS_POWER, value);
  999. } else {
  1000. ret = write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value);
  1001. }
  1002. if (ret)
  1003. pr_warning("Error setting light sensor switch\n");
  1004. asus->light_switch = value;
  1005. }
  1006. static ssize_t show_lssw(struct device *dev,
  1007. struct device_attribute *attr, char *buf)
  1008. {
  1009. struct asus_laptop *asus = dev_get_drvdata(dev);
  1010. return sprintf(buf, "%d\n", asus->light_switch);
  1011. }
  1012. static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
  1013. const char *buf, size_t count)
  1014. {
  1015. struct asus_laptop *asus = dev_get_drvdata(dev);
  1016. int rv, value;
  1017. rv = parse_arg(buf, count, &value);
  1018. if (rv > 0)
  1019. asus_als_switch(asus, value ? 1 : 0);
  1020. return rv;
  1021. }
  1022. static void asus_als_level(struct asus_laptop *asus, int value)
  1023. {
  1024. if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
  1025. pr_warn("Error setting light sensor level\n");
  1026. asus->light_level = value;
  1027. }
  1028. static ssize_t show_lslvl(struct device *dev,
  1029. struct device_attribute *attr, char *buf)
  1030. {
  1031. struct asus_laptop *asus = dev_get_drvdata(dev);
  1032. return sprintf(buf, "%d\n", asus->light_level);
  1033. }
  1034. static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
  1035. const char *buf, size_t count)
  1036. {
  1037. struct asus_laptop *asus = dev_get_drvdata(dev);
  1038. int rv, value;
  1039. rv = parse_arg(buf, count, &value);
  1040. if (rv > 0) {
  1041. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  1042. /* 0 <= value <= 15 */
  1043. asus_als_level(asus, value);
  1044. }
  1045. return rv;
  1046. }
  1047. static int pega_int_read(struct asus_laptop *asus, int arg, int *result)
  1048. {
  1049. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1050. int err = write_acpi_int_ret(asus->handle, METHOD_PEGA_READ, arg,
  1051. &buffer);
  1052. if (!err) {
  1053. union acpi_object *obj = buffer.pointer;
  1054. if (obj && obj->type == ACPI_TYPE_INTEGER)
  1055. *result = obj->integer.value;
  1056. else
  1057. err = -EIO;
  1058. }
  1059. return err;
  1060. }
  1061. static ssize_t show_lsvalue(struct device *dev,
  1062. struct device_attribute *attr, char *buf)
  1063. {
  1064. struct asus_laptop *asus = dev_get_drvdata(dev);
  1065. int err, hi, lo;
  1066. err = pega_int_read(asus, PEGA_READ_ALS_H, &hi);
  1067. if (!err)
  1068. err = pega_int_read(asus, PEGA_READ_ALS_L, &lo);
  1069. if (!err)
  1070. return sprintf(buf, "%d\n", 10 * hi + lo);
  1071. return err;
  1072. }
  1073. /*
  1074. * GPS
  1075. */
  1076. static int asus_gps_status(struct asus_laptop *asus)
  1077. {
  1078. unsigned long long status;
  1079. acpi_status rv = AE_OK;
  1080. rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
  1081. NULL, &status);
  1082. if (ACPI_FAILURE(rv)) {
  1083. pr_warn("Error reading GPS status\n");
  1084. return -ENODEV;
  1085. }
  1086. return !!status;
  1087. }
  1088. static int asus_gps_switch(struct asus_laptop *asus, int status)
  1089. {
  1090. const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
  1091. if (write_acpi_int(asus->handle, meth, 0x02))
  1092. return -ENODEV;
  1093. return 0;
  1094. }
  1095. static ssize_t show_gps(struct device *dev,
  1096. struct device_attribute *attr, char *buf)
  1097. {
  1098. struct asus_laptop *asus = dev_get_drvdata(dev);
  1099. return sprintf(buf, "%d\n", asus_gps_status(asus));
  1100. }
  1101. static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
  1102. const char *buf, size_t count)
  1103. {
  1104. struct asus_laptop *asus = dev_get_drvdata(dev);
  1105. int rv, value;
  1106. int ret;
  1107. rv = parse_arg(buf, count, &value);
  1108. if (rv <= 0)
  1109. return -EINVAL;
  1110. ret = asus_gps_switch(asus, !!value);
  1111. if (ret)
  1112. return ret;
  1113. rfkill_set_sw_state(asus->gps.rfkill, !value);
  1114. return rv;
  1115. }
  1116. /*
  1117. * rfkill
  1118. */
  1119. static int asus_gps_rfkill_set(void *data, bool blocked)
  1120. {
  1121. struct asus_laptop *asus = data;
  1122. return asus_gps_switch(asus, !blocked);
  1123. }
  1124. static const struct rfkill_ops asus_gps_rfkill_ops = {
  1125. .set_block = asus_gps_rfkill_set,
  1126. };
  1127. static int asus_rfkill_set(void *data, bool blocked)
  1128. {
  1129. struct asus_rfkill *rfk = data;
  1130. struct asus_laptop *asus = rfk->asus;
  1131. if (rfk->control_id == WL_RSTS)
  1132. return asus_wlan_set(asus, !blocked);
  1133. else if (rfk->control_id == BT_RSTS)
  1134. return asus_bluetooth_set(asus, !blocked);
  1135. else if (rfk->control_id == WM_RSTS)
  1136. return asus_wimax_set(asus, !blocked);
  1137. else if (rfk->control_id == WW_RSTS)
  1138. return asus_wwan_set(asus, !blocked);
  1139. return -EINVAL;
  1140. }
  1141. static const struct rfkill_ops asus_rfkill_ops = {
  1142. .set_block = asus_rfkill_set,
  1143. };
  1144. static void asus_rfkill_terminate(struct asus_rfkill *rfk)
  1145. {
  1146. if (!rfk->rfkill)
  1147. return ;
  1148. rfkill_unregister(rfk->rfkill);
  1149. rfkill_destroy(rfk->rfkill);
  1150. rfk->rfkill = NULL;
  1151. }
  1152. static void asus_rfkill_exit(struct asus_laptop *asus)
  1153. {
  1154. asus_rfkill_terminate(&asus->wwan);
  1155. asus_rfkill_terminate(&asus->bluetooth);
  1156. asus_rfkill_terminate(&asus->wlan);
  1157. asus_rfkill_terminate(&asus->gps);
  1158. }
  1159. static int asus_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
  1160. const char *name, int control_id, int type,
  1161. const struct rfkill_ops *ops)
  1162. {
  1163. int result;
  1164. rfk->control_id = control_id;
  1165. rfk->asus = asus;
  1166. rfk->rfkill = rfkill_alloc(name, &asus->platform_device->dev,
  1167. type, ops, rfk);
  1168. if (!rfk->rfkill)
  1169. return -EINVAL;
  1170. result = rfkill_register(rfk->rfkill);
  1171. if (result) {
  1172. rfkill_destroy(rfk->rfkill);
  1173. rfk->rfkill = NULL;
  1174. }
  1175. return result;
  1176. }
  1177. static int asus_rfkill_init(struct asus_laptop *asus)
  1178. {
  1179. int result = 0;
  1180. if (asus->is_pega_lucid)
  1181. return -ENODEV;
  1182. if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
  1183. !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
  1184. !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
  1185. result = asus_rfkill_setup(asus, &asus->gps, "asus-gps",
  1186. -1, RFKILL_TYPE_GPS,
  1187. &asus_gps_rfkill_ops);
  1188. if (result)
  1189. goto exit;
  1190. if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL) &&
  1191. asus->wled_type == TYPE_RFKILL)
  1192. result = asus_rfkill_setup(asus, &asus->wlan, "asus-wlan",
  1193. WL_RSTS, RFKILL_TYPE_WLAN,
  1194. &asus_rfkill_ops);
  1195. if (result)
  1196. goto exit;
  1197. if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL) &&
  1198. asus->bled_type == TYPE_RFKILL)
  1199. result = asus_rfkill_setup(asus, &asus->bluetooth,
  1200. "asus-bluetooth", BT_RSTS,
  1201. RFKILL_TYPE_BLUETOOTH,
  1202. &asus_rfkill_ops);
  1203. if (result)
  1204. goto exit;
  1205. if (!acpi_check_handle(asus->handle, METHOD_WWAN, NULL))
  1206. result = asus_rfkill_setup(asus, &asus->wwan, "asus-wwan",
  1207. WW_RSTS, RFKILL_TYPE_WWAN,
  1208. &asus_rfkill_ops);
  1209. if (result)
  1210. goto exit;
  1211. if (!acpi_check_handle(asus->handle, METHOD_WIMAX, NULL))
  1212. result = asus_rfkill_setup(asus, &asus->wimax, "asus-wimax",
  1213. WM_RSTS, RFKILL_TYPE_WIMAX,
  1214. &asus_rfkill_ops);
  1215. if (result)
  1216. goto exit;
  1217. exit:
  1218. if (result)
  1219. asus_rfkill_exit(asus);
  1220. return result;
  1221. }
  1222. static int pega_rfkill_set(void *data, bool blocked)
  1223. {
  1224. struct asus_rfkill *rfk = data;
  1225. int ret = asus_pega_lucid_set(rfk->asus, rfk->control_id, !blocked);
  1226. return ret;
  1227. }
  1228. static const struct rfkill_ops pega_rfkill_ops = {
  1229. .set_block = pega_rfkill_set,
  1230. };
  1231. static int pega_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
  1232. const char *name, int controlid, int rfkill_type)
  1233. {
  1234. return asus_rfkill_setup(asus, rfk, name, controlid, rfkill_type,
  1235. &pega_rfkill_ops);
  1236. }
  1237. static int pega_rfkill_init(struct asus_laptop *asus)
  1238. {
  1239. int ret = 0;
  1240. if(!asus->is_pega_lucid)
  1241. return -ENODEV;
  1242. ret = pega_rfkill_setup(asus, &asus->wlan, "pega-wlan",
  1243. PEGA_WLAN, RFKILL_TYPE_WLAN);
  1244. if(ret)
  1245. goto exit;
  1246. ret = pega_rfkill_setup(asus, &asus->bluetooth, "pega-bt",
  1247. PEGA_BLUETOOTH, RFKILL_TYPE_BLUETOOTH);
  1248. if(ret)
  1249. goto exit;
  1250. ret = pega_rfkill_setup(asus, &asus->wwan, "pega-wwan",
  1251. PEGA_WWAN, RFKILL_TYPE_WWAN);
  1252. exit:
  1253. if (ret)
  1254. asus_rfkill_exit(asus);
  1255. return ret;
  1256. }
  1257. /*
  1258. * Input device (i.e. hotkeys)
  1259. */
  1260. static void asus_input_notify(struct asus_laptop *asus, int event)
  1261. {
  1262. if (!asus->inputdev)
  1263. return ;
  1264. if (!sparse_keymap_report_event(asus->inputdev, event, 1, true))
  1265. pr_info("Unknown key %x pressed\n", event);
  1266. }
  1267. static int asus_input_init(struct asus_laptop *asus)
  1268. {
  1269. struct input_dev *input;
  1270. int error;
  1271. input = input_allocate_device();
  1272. if (!input) {
  1273. pr_warn("Unable to allocate input device\n");
  1274. return -ENOMEM;
  1275. }
  1276. input->name = "Asus Laptop extra buttons";
  1277. input->phys = ASUS_LAPTOP_FILE "/input0";
  1278. input->id.bustype = BUS_HOST;
  1279. input->dev.parent = &asus->platform_device->dev;
  1280. error = sparse_keymap_setup(input, asus_keymap, NULL);
  1281. if (error) {
  1282. pr_err("Unable to setup input device keymap\n");
  1283. goto err_free_dev;
  1284. }
  1285. error = input_register_device(input);
  1286. if (error) {
  1287. pr_warn("Unable to register input device\n");
  1288. goto err_free_keymap;
  1289. }
  1290. asus->inputdev = input;
  1291. return 0;
  1292. err_free_keymap:
  1293. sparse_keymap_free(input);
  1294. err_free_dev:
  1295. input_free_device(input);
  1296. return error;
  1297. }
  1298. static void asus_input_exit(struct asus_laptop *asus)
  1299. {
  1300. if (asus->inputdev) {
  1301. sparse_keymap_free(asus->inputdev);
  1302. input_unregister_device(asus->inputdev);
  1303. }
  1304. asus->inputdev = NULL;
  1305. }
  1306. /*
  1307. * ACPI driver
  1308. */
  1309. static void asus_acpi_notify(struct acpi_device *device, u32 event)
  1310. {
  1311. struct asus_laptop *asus = acpi_driver_data(device);
  1312. u16 count;
  1313. /* TODO Find a better way to handle events count. */
  1314. count = asus->event_count[event % 128]++;
  1315. acpi_bus_generate_proc_event(asus->device, event, count);
  1316. acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
  1317. dev_name(&asus->device->dev), event,
  1318. count);
  1319. /* Brightness events are special */
  1320. if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
  1321. /* Ignore them completely if the acpi video driver is used */
  1322. if (asus->backlight_device != NULL) {
  1323. /* Update the backlight device. */
  1324. asus_backlight_notify(asus);
  1325. }
  1326. return ;
  1327. }
  1328. /* Accelerometer "coarse orientation change" event */
  1329. if (asus->pega_accel_poll && event == 0xEA) {
  1330. kobject_uevent(&asus->pega_accel_poll->input->dev.kobj,
  1331. KOBJ_CHANGE);
  1332. return ;
  1333. }
  1334. asus_input_notify(asus, event);
  1335. }
  1336. static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
  1337. static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
  1338. static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
  1339. show_bluetooth, store_bluetooth);
  1340. static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
  1341. static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
  1342. static DEVICE_ATTR(display, S_IWUSR, NULL, store_disp);
  1343. static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
  1344. static DEVICE_ATTR(ls_value, S_IRUGO, show_lsvalue, NULL);
  1345. static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
  1346. static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
  1347. static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
  1348. static struct attribute *asus_attributes[] = {
  1349. &dev_attr_infos.attr,
  1350. &dev_attr_wlan.attr,
  1351. &dev_attr_bluetooth.attr,
  1352. &dev_attr_wimax.attr,
  1353. &dev_attr_wwan.attr,
  1354. &dev_attr_display.attr,
  1355. &dev_attr_ledd.attr,
  1356. &dev_attr_ls_value.attr,
  1357. &dev_attr_ls_level.attr,
  1358. &dev_attr_ls_switch.attr,
  1359. &dev_attr_gps.attr,
  1360. NULL
  1361. };
  1362. static umode_t asus_sysfs_is_visible(struct kobject *kobj,
  1363. struct attribute *attr,
  1364. int idx)
  1365. {
  1366. struct device *dev = container_of(kobj, struct device, kobj);
  1367. struct platform_device *pdev = to_platform_device(dev);
  1368. struct asus_laptop *asus = platform_get_drvdata(pdev);
  1369. acpi_handle handle = asus->handle;
  1370. bool supported;
  1371. if (asus->is_pega_lucid) {
  1372. /* no ls_level interface on the Lucid */
  1373. if (attr == &dev_attr_ls_switch.attr)
  1374. supported = true;
  1375. else if (attr == &dev_attr_ls_level.attr)
  1376. supported = false;
  1377. else
  1378. goto normal;
  1379. return supported;
  1380. }
  1381. normal:
  1382. if (attr == &dev_attr_wlan.attr) {
  1383. supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
  1384. } else if (attr == &dev_attr_bluetooth.attr) {
  1385. supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
  1386. } else if (attr == &dev_attr_display.attr) {
  1387. supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
  1388. } else if (attr == &dev_attr_wimax.attr) {
  1389. supported =
  1390. !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
  1391. } else if (attr == &dev_attr_wwan.attr) {
  1392. supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
  1393. } else if (attr == &dev_attr_ledd.attr) {
  1394. supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
  1395. } else if (attr == &dev_attr_ls_switch.attr ||
  1396. attr == &dev_attr_ls_level.attr) {
  1397. supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
  1398. !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
  1399. } else if (attr == &dev_attr_ls_value.attr) {
  1400. supported = asus->is_pega_lucid;
  1401. } else if (attr == &dev_attr_gps.attr) {
  1402. supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
  1403. !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
  1404. !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
  1405. } else {
  1406. supported = true;
  1407. }
  1408. return supported ? attr->mode : 0;
  1409. }
  1410. static const struct attribute_group asus_attr_group = {
  1411. .is_visible = asus_sysfs_is_visible,
  1412. .attrs = asus_attributes,
  1413. };
  1414. static int asus_platform_init(struct asus_laptop *asus)
  1415. {
  1416. int result;
  1417. asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
  1418. if (!asus->platform_device)
  1419. return -ENOMEM;
  1420. platform_set_drvdata(asus->platform_device, asus);
  1421. result = platform_device_add(asus->platform_device);
  1422. if (result)
  1423. goto fail_platform_device;
  1424. result = sysfs_create_group(&asus->platform_device->dev.kobj,
  1425. &asus_attr_group);
  1426. if (result)
  1427. goto fail_sysfs;
  1428. return 0;
  1429. fail_sysfs:
  1430. platform_device_del(asus->platform_device);
  1431. fail_platform_device:
  1432. platform_device_put(asus->platform_device);
  1433. return result;
  1434. }
  1435. static void asus_platform_exit(struct asus_laptop *asus)
  1436. {
  1437. sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
  1438. platform_device_unregister(asus->platform_device);
  1439. }
  1440. static struct platform_driver platform_driver = {
  1441. .driver = {
  1442. .name = ASUS_LAPTOP_FILE,
  1443. .owner = THIS_MODULE,
  1444. },
  1445. };
  1446. /*
  1447. * This function is used to initialize the context with right values. In this
  1448. * method, we can make all the detection we want, and modify the asus_laptop
  1449. * struct
  1450. */
  1451. static int asus_laptop_get_info(struct asus_laptop *asus)
  1452. {
  1453. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1454. union acpi_object *model = NULL;
  1455. unsigned long long bsts_result;
  1456. char *string = NULL;
  1457. acpi_status status;
  1458. /*
  1459. * Get DSDT headers early enough to allow for differentiating between
  1460. * models, but late enough to allow acpi_bus_register_driver() to fail
  1461. * before doing anything ACPI-specific. Should we encounter a machine,
  1462. * which needs special handling (i.e. its hotkey device has a different
  1463. * HID), this bit will be moved.
  1464. */
  1465. status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
  1466. if (ACPI_FAILURE(status))
  1467. pr_warn("Couldn't get the DSDT table header\n");
  1468. /* We have to write 0 on init this far for all ASUS models */
  1469. if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
  1470. pr_err("Hotkey initialization failed\n");
  1471. return -ENODEV;
  1472. }
  1473. /* This needs to be called for some laptops to init properly */
  1474. status =
  1475. acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
  1476. if (ACPI_FAILURE(status))
  1477. pr_warn("Error calling BSTS\n");
  1478. else if (bsts_result)
  1479. pr_notice("BSTS called, 0x%02x returned\n",
  1480. (uint) bsts_result);
  1481. /* This too ... */
  1482. if (write_acpi_int(asus->handle, "CWAP", wapf))
  1483. pr_err("Error calling CWAP(%d)\n", wapf);
  1484. /*
  1485. * Try to match the object returned by INIT to the specific model.
  1486. * Handle every possible object (or the lack of thereof) the DSDT
  1487. * writers might throw at us. When in trouble, we pass NULL to
  1488. * asus_model_match() and try something completely different.
  1489. */
  1490. if (buffer.pointer) {
  1491. model = buffer.pointer;
  1492. switch (model->type) {
  1493. case ACPI_TYPE_STRING:
  1494. string = model->string.pointer;
  1495. break;
  1496. case ACPI_TYPE_BUFFER:
  1497. string = model->buffer.pointer;
  1498. break;
  1499. default:
  1500. string = "";
  1501. break;
  1502. }
  1503. }
  1504. asus->name = kstrdup(string, GFP_KERNEL);
  1505. if (!asus->name) {
  1506. kfree(buffer.pointer);
  1507. return -ENOMEM;
  1508. }
  1509. if (string)
  1510. pr_notice(" %s model detected\n", string);
  1511. if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
  1512. asus->have_rsts = true;
  1513. kfree(model);
  1514. return AE_OK;
  1515. }
  1516. static int asus_acpi_init(struct asus_laptop *asus)
  1517. {
  1518. int result = 0;
  1519. result = acpi_bus_get_status(asus->device);
  1520. if (result)
  1521. return result;
  1522. if (!asus->device->status.present) {
  1523. pr_err("Hotkey device not present, aborting\n");
  1524. return -ENODEV;
  1525. }
  1526. result = asus_laptop_get_info(asus);
  1527. if (result)
  1528. return result;
  1529. if (!strcmp(bled_type, "led"))
  1530. asus->bled_type = TYPE_LED;
  1531. else if (!strcmp(bled_type, "rfkill"))
  1532. asus->bled_type = TYPE_RFKILL;
  1533. if (!strcmp(wled_type, "led"))
  1534. asus->wled_type = TYPE_LED;
  1535. else if (!strcmp(wled_type, "rfkill"))
  1536. asus->wled_type = TYPE_RFKILL;
  1537. if (bluetooth_status >= 0)
  1538. asus_bluetooth_set(asus, !!bluetooth_status);
  1539. if (wlan_status >= 0)
  1540. asus_wlan_set(asus, !!wlan_status);
  1541. if (wimax_status >= 0)
  1542. asus_wimax_set(asus, !!wimax_status);
  1543. if (wwan_status >= 0)
  1544. asus_wwan_set(asus, !!wwan_status);
  1545. /* Keyboard Backlight is on by default */
  1546. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
  1547. asus_kled_set(asus, 1);
  1548. /* LED display is off by default */
  1549. asus->ledd_status = 0xFFF;
  1550. /* Set initial values of light sensor and level */
  1551. asus->light_switch = !!als_status;
  1552. asus->light_level = 5; /* level 5 for sensor sensitivity */
  1553. if (asus->is_pega_lucid) {
  1554. asus_als_switch(asus, asus->light_switch);
  1555. } else if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1556. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1557. asus_als_switch(asus, asus->light_switch);
  1558. asus_als_level(asus, asus->light_level);
  1559. }
  1560. return result;
  1561. }
  1562. static void asus_dmi_check(void)
  1563. {
  1564. const char *model;
  1565. model = dmi_get_system_info(DMI_PRODUCT_NAME);
  1566. if (!model)
  1567. return;
  1568. /* On L1400B WLED control the sound card, don't mess with it ... */
  1569. if (strncmp(model, "L1400B", 6) == 0) {
  1570. wlan_status = -1;
  1571. }
  1572. }
  1573. static bool asus_device_present;
  1574. static int asus_acpi_add(struct acpi_device *device)
  1575. {
  1576. struct asus_laptop *asus;
  1577. int result;
  1578. pr_notice("Asus Laptop Support version %s\n",
  1579. ASUS_LAPTOP_VERSION);
  1580. asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
  1581. if (!asus)
  1582. return -ENOMEM;
  1583. asus->handle = device->handle;
  1584. strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
  1585. strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
  1586. device->driver_data = asus;
  1587. asus->device = device;
  1588. asus_dmi_check();
  1589. result = asus_acpi_init(asus);
  1590. if (result)
  1591. goto fail_platform;
  1592. /*
  1593. * Need platform type detection first, then the platform
  1594. * device. It is used as a parent for the sub-devices below.
  1595. */
  1596. asus->is_pega_lucid = asus_check_pega_lucid(asus);
  1597. result = asus_platform_init(asus);
  1598. if (result)
  1599. goto fail_platform;
  1600. if (!acpi_video_backlight_support()) {
  1601. result = asus_backlight_init(asus);
  1602. if (result)
  1603. goto fail_backlight;
  1604. } else
  1605. pr_info("Backlight controlled by ACPI video driver\n");
  1606. result = asus_input_init(asus);
  1607. if (result)
  1608. goto fail_input;
  1609. result = asus_led_init(asus);
  1610. if (result)
  1611. goto fail_led;
  1612. result = asus_rfkill_init(asus);
  1613. if (result && result != -ENODEV)
  1614. goto fail_rfkill;
  1615. result = pega_accel_init(asus);
  1616. if (result && result != -ENODEV)
  1617. goto fail_pega_accel;
  1618. result = pega_rfkill_init(asus);
  1619. if (result && result != -ENODEV)
  1620. goto fail_pega_rfkill;
  1621. asus_device_present = true;
  1622. return 0;
  1623. fail_pega_rfkill:
  1624. pega_accel_exit(asus);
  1625. fail_pega_accel:
  1626. asus_rfkill_exit(asus);
  1627. fail_rfkill:
  1628. asus_led_exit(asus);
  1629. fail_led:
  1630. asus_input_exit(asus);
  1631. fail_input:
  1632. asus_backlight_exit(asus);
  1633. fail_backlight:
  1634. asus_platform_exit(asus);
  1635. fail_platform:
  1636. kfree(asus->name);
  1637. kfree(asus);
  1638. return result;
  1639. }
  1640. static int asus_acpi_remove(struct acpi_device *device)
  1641. {
  1642. struct asus_laptop *asus = acpi_driver_data(device);
  1643. asus_backlight_exit(asus);
  1644. asus_rfkill_exit(asus);
  1645. asus_led_exit(asus);
  1646. asus_input_exit(asus);
  1647. pega_accel_exit(asus);
  1648. asus_platform_exit(asus);
  1649. kfree(asus->name);
  1650. kfree(asus);
  1651. return 0;
  1652. }
  1653. static const struct acpi_device_id asus_device_ids[] = {
  1654. {"ATK0100", 0},
  1655. {"ATK0101", 0},
  1656. {"", 0},
  1657. };
  1658. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  1659. static struct acpi_driver asus_acpi_driver = {
  1660. .name = ASUS_LAPTOP_NAME,
  1661. .class = ASUS_LAPTOP_CLASS,
  1662. .owner = THIS_MODULE,
  1663. .ids = asus_device_ids,
  1664. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1665. .ops = {
  1666. .add = asus_acpi_add,
  1667. .remove = asus_acpi_remove,
  1668. .notify = asus_acpi_notify,
  1669. },
  1670. };
  1671. static int __init asus_laptop_init(void)
  1672. {
  1673. int result;
  1674. result = platform_driver_register(&platform_driver);
  1675. if (result < 0)
  1676. return result;
  1677. result = acpi_bus_register_driver(&asus_acpi_driver);
  1678. if (result < 0)
  1679. goto fail_acpi_driver;
  1680. if (!asus_device_present) {
  1681. result = -ENODEV;
  1682. goto fail_no_device;
  1683. }
  1684. return 0;
  1685. fail_no_device:
  1686. acpi_bus_unregister_driver(&asus_acpi_driver);
  1687. fail_acpi_driver:
  1688. platform_driver_unregister(&platform_driver);
  1689. return result;
  1690. }
  1691. static void __exit asus_laptop_exit(void)
  1692. {
  1693. acpi_bus_unregister_driver(&asus_acpi_driver);
  1694. platform_driver_unregister(&platform_driver);
  1695. }
  1696. module_init(asus_laptop_init);
  1697. module_exit(asus_laptop_exit);