asus-laptop.c 53 KB

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