asus-laptop.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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->name);
  1645. kfree(asus);
  1646. return result;
  1647. }
  1648. static int asus_acpi_remove(struct acpi_device *device)
  1649. {
  1650. struct asus_laptop *asus = acpi_driver_data(device);
  1651. asus_backlight_exit(asus);
  1652. asus_rfkill_exit(asus);
  1653. asus_led_exit(asus);
  1654. asus_input_exit(asus);
  1655. pega_accel_exit(asus);
  1656. asus_platform_exit(asus);
  1657. kfree(asus->name);
  1658. kfree(asus);
  1659. return 0;
  1660. }
  1661. static const struct acpi_device_id asus_device_ids[] = {
  1662. {"ATK0100", 0},
  1663. {"ATK0101", 0},
  1664. {"", 0},
  1665. };
  1666. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  1667. static struct acpi_driver asus_acpi_driver = {
  1668. .name = ASUS_LAPTOP_NAME,
  1669. .class = ASUS_LAPTOP_CLASS,
  1670. .owner = THIS_MODULE,
  1671. .ids = asus_device_ids,
  1672. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1673. .ops = {
  1674. .add = asus_acpi_add,
  1675. .remove = asus_acpi_remove,
  1676. .notify = asus_acpi_notify,
  1677. },
  1678. };
  1679. static int __init asus_laptop_init(void)
  1680. {
  1681. int result;
  1682. result = platform_driver_register(&platform_driver);
  1683. if (result < 0)
  1684. return result;
  1685. result = acpi_bus_register_driver(&asus_acpi_driver);
  1686. if (result < 0)
  1687. goto fail_acpi_driver;
  1688. if (!asus_device_present) {
  1689. result = -ENODEV;
  1690. goto fail_no_device;
  1691. }
  1692. return 0;
  1693. fail_no_device:
  1694. acpi_bus_unregister_driver(&asus_acpi_driver);
  1695. fail_acpi_driver:
  1696. platform_driver_unregister(&platform_driver);
  1697. return result;
  1698. }
  1699. static void __exit asus_laptop_exit(void)
  1700. {
  1701. acpi_bus_unregister_driver(&asus_acpi_driver);
  1702. platform_driver_unregister(&platform_driver);
  1703. }
  1704. module_init(asus_laptop_init);
  1705. module_exit(asus_laptop_exit);