asus-laptop.c 53 KB

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