asus-laptop.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * The development page for this driver is located at
  24. * http://sourceforge.net/projects/acpi4asus/
  25. *
  26. * Credits:
  27. * Pontus Fuchs - Helper functions, cleanup
  28. * Johann Wiesner - Small compile fixes
  29. * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
  30. * Eric Burghard - LED display support for W1N
  31. * Josh Green - Light Sens support
  32. * Thomas Tuttle - His first patch for led support was very helpfull
  33. * Sam Lin - GPS support
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/err.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/backlight.h>
  43. #include <linux/fb.h>
  44. #include <linux/leds.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/uaccess.h>
  47. #include <linux/input.h>
  48. #include <linux/input/sparse-keymap.h>
  49. #include <linux/rfkill.h>
  50. #include <linux/slab.h>
  51. #include <acpi/acpi_drivers.h>
  52. #include <acpi/acpi_bus.h>
  53. #define ASUS_LAPTOP_VERSION "0.42"
  54. #define ASUS_LAPTOP_NAME "Asus Laptop Support"
  55. #define ASUS_LAPTOP_CLASS "hotkey"
  56. #define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
  57. #define ASUS_LAPTOP_FILE KBUILD_MODNAME
  58. #define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
  59. MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
  60. MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
  61. MODULE_LICENSE("GPL");
  62. /*
  63. * WAPF defines the behavior of the Fn+Fx wlan key
  64. * The significance of values is yet to be found, but
  65. * most of the time:
  66. * 0x0 will do nothing
  67. * 0x1 will allow to control the device with Fn+Fx key.
  68. * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
  69. * 0x5 like 0x1 or 0x4
  70. * So, if something doesn't work as you want, just try other values =)
  71. */
  72. static uint wapf = 1;
  73. module_param(wapf, uint, 0444);
  74. MODULE_PARM_DESC(wapf, "WAPF value");
  75. static int wlan_status = 1;
  76. static int bluetooth_status = 1;
  77. static int wimax_status = -1;
  78. static int wwan_status = -1;
  79. module_param(wlan_status, int, 0444);
  80. MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
  81. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  82. "default is 1");
  83. module_param(bluetooth_status, int, 0444);
  84. MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
  85. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  86. "default is 1");
  87. module_param(wimax_status, int, 0444);
  88. MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
  89. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  90. "default is 1");
  91. module_param(wwan_status, int, 0444);
  92. MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
  93. "(0 = disabled, 1 = enabled, -1 = don't do anything). "
  94. "default is 1");
  95. /*
  96. * Some events we use, same for all Asus
  97. */
  98. #define ATKD_BR_UP 0x10 /* (event & ~ATKD_BR_UP) = brightness level */
  99. #define ATKD_BR_DOWN 0x20 /* (event & ~ATKD_BR_DOWN) = britghness level */
  100. #define ATKD_BR_MIN ATKD_BR_UP
  101. #define ATKD_BR_MAX (ATKD_BR_DOWN | 0xF) /* 0x2f */
  102. #define ATKD_LCD_ON 0x33
  103. #define ATKD_LCD_OFF 0x34
  104. /*
  105. * Known bits returned by \_SB.ATKD.HWRS
  106. */
  107. #define WL_HWRS 0x80
  108. #define BT_HWRS 0x100
  109. /*
  110. * Flags for hotk status
  111. * WL_ON and BT_ON are also used for wireless_status()
  112. */
  113. #define WL_RSTS 0x01 /* internal Wifi */
  114. #define BT_RSTS 0x02 /* internal Bluetooth */
  115. #define WM_RSTS 0x08 /* internal wimax */
  116. #define WW_RSTS 0x20 /* internal wwan */
  117. /* LED */
  118. #define METHOD_MLED "MLED"
  119. #define METHOD_TLED "TLED"
  120. #define METHOD_RLED "RLED" /* W1JC */
  121. #define METHOD_PLED "PLED" /* A7J */
  122. #define METHOD_GLED "GLED" /* G1, G2 (probably) */
  123. /* LEDD */
  124. #define METHOD_LEDD "SLCM"
  125. /*
  126. * Bluetooth and WLAN
  127. * WLED and BLED are not handled like other XLED, because in some dsdt
  128. * they also control the WLAN/Bluetooth device.
  129. */
  130. #define METHOD_WLAN "WLED"
  131. #define METHOD_BLUETOOTH "BLED"
  132. /* WWAN and WIMAX */
  133. #define METHOD_WWAN "GSMC"
  134. #define METHOD_WIMAX "WMXC"
  135. #define METHOD_WL_STATUS "RSTS"
  136. /* Brightness */
  137. #define METHOD_BRIGHTNESS_SET "SPLV"
  138. #define METHOD_BRIGHTNESS_GET "GPLV"
  139. /* Backlight */
  140. static acpi_handle lcd_switch_handle;
  141. static char *lcd_switch_paths[] = {
  142. "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
  143. "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
  144. "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
  145. "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
  146. "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
  147. "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
  148. "\\_SB.PCI0.PX40.Q10", /* S1x */
  149. "\\Q10"}; /* A2x, L2D, L3D, M2E */
  150. /* Display */
  151. #define METHOD_SWITCH_DISPLAY "SDSP"
  152. static acpi_handle display_get_handle;
  153. static char *display_get_paths[] = {
  154. /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
  155. "\\_SB.PCI0.P0P1.VGA.GETD",
  156. /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
  157. "\\_SB.PCI0.P0P2.VGA.GETD",
  158. /* A6V A6Q */
  159. "\\_SB.PCI0.P0P3.VGA.GETD",
  160. /* A6T, A6M */
  161. "\\_SB.PCI0.P0PA.VGA.GETD",
  162. /* L3C */
  163. "\\_SB.PCI0.PCI1.VGAC.NMAP",
  164. /* Z96F */
  165. "\\_SB.PCI0.VGA.GETD",
  166. /* A2D */
  167. "\\ACTD",
  168. /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
  169. "\\ADVG",
  170. /* P30 */
  171. "\\DNXT",
  172. /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
  173. "\\INFB",
  174. /* A3F A6F A3N A3L M6N W3N W6A */
  175. "\\SSTE"};
  176. #define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
  177. #define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
  178. /* GPS */
  179. /* R2H use different handle for GPS on/off */
  180. #define METHOD_GPS_ON "SDON"
  181. #define METHOD_GPS_OFF "SDOF"
  182. #define METHOD_GPS_STATUS "GPST"
  183. /* Keyboard light */
  184. #define METHOD_KBD_LIGHT_SET "SLKB"
  185. #define METHOD_KBD_LIGHT_GET "GLKB"
  186. /*
  187. * Define a specific led structure to keep the main structure clean
  188. */
  189. struct asus_led {
  190. int wk;
  191. struct work_struct work;
  192. struct led_classdev led;
  193. struct asus_laptop *asus;
  194. const char *method;
  195. };
  196. /*
  197. * This is the main structure, we can use it to store anything interesting
  198. * about the hotk device
  199. */
  200. struct asus_laptop {
  201. char *name; /* laptop name */
  202. struct acpi_table_header *dsdt_info;
  203. struct platform_device *platform_device;
  204. struct acpi_device *device; /* the device we are in */
  205. struct backlight_device *backlight_device;
  206. struct input_dev *inputdev;
  207. struct key_entry *keymap;
  208. struct asus_led mled;
  209. struct asus_led tled;
  210. struct asus_led rled;
  211. struct asus_led pled;
  212. struct asus_led gled;
  213. struct asus_led kled;
  214. struct workqueue_struct *led_workqueue;
  215. int wireless_status;
  216. bool have_rsts;
  217. int lcd_state;
  218. struct rfkill *gps_rfkill;
  219. acpi_handle handle; /* the handle of the hotk device */
  220. u32 ledd_status; /* status of the LED display */
  221. u8 light_level; /* light sensor level */
  222. u8 light_switch; /* light sensor switch value */
  223. u16 event_count[128]; /* count for each event TODO make this better */
  224. };
  225. static const struct key_entry asus_keymap[] = {
  226. /* Lenovo SL Specific keycodes */
  227. {KE_KEY, 0x02, { KEY_SCREENLOCK } },
  228. {KE_KEY, 0x05, { KEY_WLAN } },
  229. {KE_KEY, 0x08, { KEY_F13 } },
  230. {KE_KEY, 0x17, { KEY_ZOOM } },
  231. {KE_KEY, 0x1f, { KEY_BATTERY } },
  232. /* End of Lenovo SL Specific keycodes */
  233. {KE_KEY, 0x30, { KEY_VOLUMEUP } },
  234. {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
  235. {KE_KEY, 0x32, { KEY_MUTE } },
  236. {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
  237. {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
  238. {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
  239. {KE_KEY, 0x41, { KEY_NEXTSONG } },
  240. {KE_KEY, 0x43, { KEY_STOPCD } },
  241. {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
  242. {KE_KEY, 0x4c, { KEY_MEDIA } },
  243. {KE_KEY, 0x50, { KEY_EMAIL } },
  244. {KE_KEY, 0x51, { KEY_WWW } },
  245. {KE_KEY, 0x55, { KEY_CALC } },
  246. {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
  247. {KE_KEY, 0x5D, { KEY_WLAN } },
  248. {KE_KEY, 0x5E, { KEY_WLAN } },
  249. {KE_KEY, 0x5F, { KEY_WLAN } },
  250. {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
  251. {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
  252. {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
  253. {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
  254. {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
  255. {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
  256. {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
  257. {KE_KEY, 0x82, { KEY_CAMERA } },
  258. {KE_KEY, 0x88, { KEY_WLAN } },
  259. {KE_KEY, 0x8A, { KEY_PROG1 } },
  260. {KE_KEY, 0x95, { KEY_MEDIA } },
  261. {KE_KEY, 0x99, { KEY_PHONE } },
  262. {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
  263. {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
  264. {KE_KEY, 0xb5, { KEY_CALC } },
  265. {KE_END, 0},
  266. };
  267. /*
  268. * This function evaluates an ACPI method, given an int as parameter, the
  269. * method is searched within the scope of the handle, can be NULL. The output
  270. * of the method is written is output, which can also be NULL
  271. *
  272. * returns 0 if write is successful, -1 else.
  273. */
  274. static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
  275. struct acpi_buffer *output)
  276. {
  277. struct acpi_object_list params; /* list of input parameters (an int) */
  278. union acpi_object in_obj; /* the only param we use */
  279. acpi_status status;
  280. if (!handle)
  281. return -1;
  282. params.count = 1;
  283. params.pointer = &in_obj;
  284. in_obj.type = ACPI_TYPE_INTEGER;
  285. in_obj.integer.value = val;
  286. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  287. if (status == AE_OK)
  288. return 0;
  289. else
  290. return -1;
  291. }
  292. static int write_acpi_int(acpi_handle handle, const char *method, int val)
  293. {
  294. return write_acpi_int_ret(handle, method, val, NULL);
  295. }
  296. static int acpi_check_handle(acpi_handle handle, const char *method,
  297. acpi_handle *ret)
  298. {
  299. acpi_status status;
  300. if (method == NULL)
  301. return -ENODEV;
  302. if (ret)
  303. status = acpi_get_handle(handle, (char *)method,
  304. ret);
  305. else {
  306. acpi_handle dummy;
  307. status = acpi_get_handle(handle, (char *)method,
  308. &dummy);
  309. }
  310. if (status != AE_OK) {
  311. if (ret)
  312. pr_warning("Error finding %s\n", method);
  313. return -ENODEV;
  314. }
  315. return 0;
  316. }
  317. /* Generic LED function */
  318. static int asus_led_set(struct asus_laptop *asus, const char *method,
  319. int value)
  320. {
  321. if (!strcmp(method, METHOD_MLED))
  322. value = !value;
  323. else if (!strcmp(method, METHOD_GLED))
  324. value = !value + 1;
  325. else
  326. value = !!value;
  327. return write_acpi_int(asus->handle, method, value);
  328. }
  329. /*
  330. * LEDs
  331. */
  332. /* /sys/class/led handlers */
  333. static void asus_led_cdev_set(struct led_classdev *led_cdev,
  334. enum led_brightness value)
  335. {
  336. struct asus_led *led = container_of(led_cdev, struct asus_led, led);
  337. struct asus_laptop *asus = led->asus;
  338. led->wk = !!value;
  339. queue_work(asus->led_workqueue, &led->work);
  340. }
  341. static void asus_led_cdev_update(struct work_struct *work)
  342. {
  343. struct asus_led *led = container_of(work, struct asus_led, work);
  344. struct asus_laptop *asus = led->asus;
  345. asus_led_set(asus, led->method, led->wk);
  346. }
  347. static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
  348. {
  349. return led_cdev->brightness;
  350. }
  351. /*
  352. * Keyboard backlight (also a LED)
  353. */
  354. static int asus_kled_lvl(struct asus_laptop *asus)
  355. {
  356. unsigned long long kblv;
  357. struct acpi_object_list params;
  358. union acpi_object in_obj;
  359. acpi_status rv;
  360. params.count = 1;
  361. params.pointer = &in_obj;
  362. in_obj.type = ACPI_TYPE_INTEGER;
  363. in_obj.integer.value = 2;
  364. rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
  365. &params, &kblv);
  366. if (ACPI_FAILURE(rv)) {
  367. pr_warning("Error reading kled level\n");
  368. return -ENODEV;
  369. }
  370. return kblv;
  371. }
  372. static int asus_kled_set(struct asus_laptop *asus, int kblv)
  373. {
  374. if (kblv > 0)
  375. kblv = (1 << 7) | (kblv & 0x7F);
  376. else
  377. kblv = 0;
  378. if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
  379. pr_warning("Keyboard LED display write failed\n");
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. static void asus_kled_cdev_set(struct led_classdev *led_cdev,
  385. enum led_brightness value)
  386. {
  387. struct asus_led *led = container_of(led_cdev, struct asus_led, led);
  388. struct asus_laptop *asus = led->asus;
  389. led->wk = value;
  390. queue_work(asus->led_workqueue, &led->work);
  391. }
  392. static void asus_kled_cdev_update(struct work_struct *work)
  393. {
  394. struct asus_led *led = container_of(work, struct asus_led, work);
  395. struct asus_laptop *asus = led->asus;
  396. asus_kled_set(asus, led->wk);
  397. }
  398. static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
  399. {
  400. struct asus_led *led = container_of(led_cdev, struct asus_led, led);
  401. struct asus_laptop *asus = led->asus;
  402. return asus_kled_lvl(asus);
  403. }
  404. static void asus_led_exit(struct asus_laptop *asus)
  405. {
  406. if (asus->mled.led.dev)
  407. led_classdev_unregister(&asus->mled.led);
  408. if (asus->tled.led.dev)
  409. led_classdev_unregister(&asus->tled.led);
  410. if (asus->pled.led.dev)
  411. led_classdev_unregister(&asus->pled.led);
  412. if (asus->rled.led.dev)
  413. led_classdev_unregister(&asus->rled.led);
  414. if (asus->gled.led.dev)
  415. led_classdev_unregister(&asus->gled.led);
  416. if (asus->kled.led.dev)
  417. led_classdev_unregister(&asus->kled.led);
  418. if (asus->led_workqueue) {
  419. destroy_workqueue(asus->led_workqueue);
  420. asus->led_workqueue = NULL;
  421. }
  422. }
  423. /* Ugly macro, need to fix that later */
  424. static int asus_led_register(struct asus_laptop *asus,
  425. struct asus_led *led,
  426. const char *name, const char *method)
  427. {
  428. struct led_classdev *led_cdev = &led->led;
  429. if (!method || acpi_check_handle(asus->handle, method, NULL))
  430. return 0; /* Led not present */
  431. led->asus = asus;
  432. led->method = method;
  433. INIT_WORK(&led->work, asus_led_cdev_update);
  434. led_cdev->name = name;
  435. led_cdev->brightness_set = asus_led_cdev_set;
  436. led_cdev->brightness_get = asus_led_cdev_get;
  437. led_cdev->max_brightness = 1;
  438. return led_classdev_register(&asus->platform_device->dev, led_cdev);
  439. }
  440. static int asus_led_init(struct asus_laptop *asus)
  441. {
  442. int r;
  443. /*
  444. * Functions that actually update the LED's are called from a
  445. * workqueue. By doing this as separate work rather than when the LED
  446. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  447. * potentially bad time, such as a timer interrupt.
  448. */
  449. asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
  450. if (!asus->led_workqueue)
  451. return -ENOMEM;
  452. r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
  453. if (r)
  454. goto error;
  455. r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
  456. if (r)
  457. goto error;
  458. r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
  459. if (r)
  460. goto error;
  461. r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
  462. if (r)
  463. goto error;
  464. r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
  465. if (r)
  466. goto error;
  467. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
  468. !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
  469. struct asus_led *led = &asus->kled;
  470. struct led_classdev *cdev = &led->led;
  471. led->asus = asus;
  472. INIT_WORK(&led->work, asus_kled_cdev_update);
  473. cdev->name = "asus::kbd_backlight";
  474. cdev->brightness_set = asus_kled_cdev_set;
  475. cdev->brightness_get = asus_kled_cdev_get;
  476. cdev->max_brightness = 3;
  477. r = led_classdev_register(&asus->platform_device->dev, cdev);
  478. }
  479. error:
  480. if (r)
  481. asus_led_exit(asus);
  482. return r;
  483. }
  484. /*
  485. * Backlight device
  486. */
  487. static int asus_lcd_status(struct asus_laptop *asus)
  488. {
  489. return asus->lcd_state;
  490. }
  491. static int asus_lcd_set(struct asus_laptop *asus, int value)
  492. {
  493. int lcd = 0;
  494. acpi_status status = 0;
  495. lcd = !!value;
  496. if (lcd == asus_lcd_status(asus))
  497. return 0;
  498. if (!lcd_switch_handle)
  499. return -ENODEV;
  500. status = acpi_evaluate_object(lcd_switch_handle,
  501. NULL, NULL, NULL);
  502. if (ACPI_FAILURE(status)) {
  503. pr_warning("Error switching LCD\n");
  504. return -ENODEV;
  505. }
  506. asus->lcd_state = lcd;
  507. return 0;
  508. }
  509. static void lcd_blank(struct asus_laptop *asus, int blank)
  510. {
  511. struct backlight_device *bd = asus->backlight_device;
  512. asus->lcd_state = (blank == FB_BLANK_UNBLANK);
  513. if (bd) {
  514. bd->props.power = blank;
  515. backlight_update_status(bd);
  516. }
  517. }
  518. static int asus_read_brightness(struct backlight_device *bd)
  519. {
  520. struct asus_laptop *asus = bl_get_data(bd);
  521. unsigned long long value;
  522. acpi_status rv = AE_OK;
  523. rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
  524. NULL, &value);
  525. if (ACPI_FAILURE(rv))
  526. pr_warning("Error reading brightness\n");
  527. return value;
  528. }
  529. static int asus_set_brightness(struct backlight_device *bd, int value)
  530. {
  531. struct asus_laptop *asus = bl_get_data(bd);
  532. if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
  533. pr_warning("Error changing brightness\n");
  534. return -EIO;
  535. }
  536. return 0;
  537. }
  538. static int update_bl_status(struct backlight_device *bd)
  539. {
  540. struct asus_laptop *asus = bl_get_data(bd);
  541. int rv;
  542. int value = bd->props.brightness;
  543. rv = asus_set_brightness(bd, value);
  544. if (rv)
  545. return rv;
  546. value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
  547. return asus_lcd_set(asus, value);
  548. }
  549. static const struct backlight_ops asusbl_ops = {
  550. .get_brightness = asus_read_brightness,
  551. .update_status = update_bl_status,
  552. };
  553. static int asus_backlight_notify(struct asus_laptop *asus)
  554. {
  555. struct backlight_device *bd = asus->backlight_device;
  556. int old = bd->props.brightness;
  557. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  558. return old;
  559. }
  560. static int asus_backlight_init(struct asus_laptop *asus)
  561. {
  562. struct backlight_device *bd;
  563. struct backlight_properties props;
  564. if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
  565. acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) ||
  566. !lcd_switch_handle)
  567. return 0;
  568. memset(&props, 0, sizeof(struct backlight_properties));
  569. props.max_brightness = 15;
  570. props.type = BACKLIGHT_PLATFORM;
  571. bd = backlight_device_register(ASUS_LAPTOP_FILE,
  572. &asus->platform_device->dev, asus,
  573. &asusbl_ops, &props);
  574. if (IS_ERR(bd)) {
  575. pr_err("Could not register asus backlight device\n");
  576. asus->backlight_device = NULL;
  577. return PTR_ERR(bd);
  578. }
  579. asus->backlight_device = bd;
  580. bd->props.brightness = asus_read_brightness(bd);
  581. bd->props.power = FB_BLANK_UNBLANK;
  582. backlight_update_status(bd);
  583. return 0;
  584. }
  585. static void asus_backlight_exit(struct asus_laptop *asus)
  586. {
  587. if (asus->backlight_device)
  588. backlight_device_unregister(asus->backlight_device);
  589. asus->backlight_device = NULL;
  590. }
  591. /*
  592. * Platform device handlers
  593. */
  594. /*
  595. * We write our info in page, we begin at offset off and cannot write more
  596. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  597. * number of bytes written in page
  598. */
  599. static ssize_t show_infos(struct device *dev,
  600. struct device_attribute *attr, char *page)
  601. {
  602. struct asus_laptop *asus = dev_get_drvdata(dev);
  603. int len = 0;
  604. unsigned long long temp;
  605. char buf[16]; /* enough for all info */
  606. acpi_status rv = AE_OK;
  607. /*
  608. * We use the easy way, we don't care of off and count,
  609. * so we don't set eof to 1
  610. */
  611. len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
  612. len += sprintf(page + len, "Model reference : %s\n", asus->name);
  613. /*
  614. * The SFUN method probably allows the original driver to get the list
  615. * of features supported by a given model. For now, 0x0100 or 0x0800
  616. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  617. * The significance of others is yet to be found.
  618. */
  619. rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
  620. if (!ACPI_FAILURE(rv))
  621. len += sprintf(page + len, "SFUN value : %#x\n",
  622. (uint) temp);
  623. /*
  624. * The HWRS method return informations about the hardware.
  625. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  626. * The significance of others is yet to be found.
  627. * If we don't find the method, we assume the device are present.
  628. */
  629. rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
  630. if (!ACPI_FAILURE(rv))
  631. len += sprintf(page + len, "HRWS value : %#x\n",
  632. (uint) temp);
  633. /*
  634. * Another value for userspace: the ASYM method returns 0x02 for
  635. * battery low and 0x04 for battery critical, its readings tend to be
  636. * more accurate than those provided by _BST.
  637. * Note: since not all the laptops provide this method, errors are
  638. * silently ignored.
  639. */
  640. rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
  641. if (!ACPI_FAILURE(rv))
  642. len += sprintf(page + len, "ASYM value : %#x\n",
  643. (uint) temp);
  644. if (asus->dsdt_info) {
  645. snprintf(buf, 16, "%d", asus->dsdt_info->length);
  646. len += sprintf(page + len, "DSDT length : %s\n", buf);
  647. snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
  648. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  649. snprintf(buf, 16, "%d", asus->dsdt_info->revision);
  650. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  651. snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
  652. len += sprintf(page + len, "OEM id : %s\n", buf);
  653. snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
  654. len += sprintf(page + len, "OEM table id : %s\n", buf);
  655. snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
  656. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  657. snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
  658. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  659. snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
  660. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  661. }
  662. return len;
  663. }
  664. static int parse_arg(const char *buf, unsigned long count, int *val)
  665. {
  666. if (!count)
  667. return 0;
  668. if (count > 31)
  669. return -EINVAL;
  670. if (sscanf(buf, "%i", val) != 1)
  671. return -EINVAL;
  672. return count;
  673. }
  674. static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
  675. const char *buf, size_t count,
  676. const char *method)
  677. {
  678. int rv, value;
  679. int out = 0;
  680. rv = parse_arg(buf, count, &value);
  681. if (rv > 0)
  682. out = value ? 1 : 0;
  683. if (write_acpi_int(asus->handle, method, value))
  684. return -ENODEV;
  685. return rv;
  686. }
  687. /*
  688. * LEDD display
  689. */
  690. static ssize_t show_ledd(struct device *dev,
  691. struct device_attribute *attr, char *buf)
  692. {
  693. struct asus_laptop *asus = dev_get_drvdata(dev);
  694. return sprintf(buf, "0x%08x\n", asus->ledd_status);
  695. }
  696. static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
  697. const char *buf, size_t count)
  698. {
  699. struct asus_laptop *asus = dev_get_drvdata(dev);
  700. int rv, value;
  701. rv = parse_arg(buf, count, &value);
  702. if (rv > 0) {
  703. if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
  704. pr_warning("LED display write failed\n");
  705. return -ENODEV;
  706. }
  707. asus->ledd_status = (u32) value;
  708. }
  709. return rv;
  710. }
  711. /*
  712. * Wireless
  713. */
  714. static int asus_wireless_status(struct asus_laptop *asus, int mask)
  715. {
  716. unsigned long long status;
  717. acpi_status rv = AE_OK;
  718. if (!asus->have_rsts)
  719. return (asus->wireless_status & mask) ? 1 : 0;
  720. rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
  721. NULL, &status);
  722. if (ACPI_FAILURE(rv)) {
  723. pr_warning("Error reading Wireless status\n");
  724. return -EINVAL;
  725. }
  726. return !!(status & mask);
  727. }
  728. /*
  729. * WLAN
  730. */
  731. static int asus_wlan_set(struct asus_laptop *asus, int status)
  732. {
  733. if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
  734. pr_warning("Error setting wlan status to %d", status);
  735. return -EIO;
  736. }
  737. return 0;
  738. }
  739. static ssize_t show_wlan(struct device *dev,
  740. struct device_attribute *attr, char *buf)
  741. {
  742. struct asus_laptop *asus = dev_get_drvdata(dev);
  743. return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
  744. }
  745. static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
  746. const char *buf, size_t count)
  747. {
  748. struct asus_laptop *asus = dev_get_drvdata(dev);
  749. return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
  750. }
  751. /*
  752. * Bluetooth
  753. */
  754. static int asus_bluetooth_set(struct asus_laptop *asus, int status)
  755. {
  756. if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
  757. pr_warning("Error setting bluetooth status to %d", status);
  758. return -EIO;
  759. }
  760. return 0;
  761. }
  762. static ssize_t show_bluetooth(struct device *dev,
  763. struct device_attribute *attr, char *buf)
  764. {
  765. struct asus_laptop *asus = dev_get_drvdata(dev);
  766. return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
  767. }
  768. static ssize_t store_bluetooth(struct device *dev,
  769. struct device_attribute *attr, const char *buf,
  770. size_t count)
  771. {
  772. struct asus_laptop *asus = dev_get_drvdata(dev);
  773. return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
  774. }
  775. /*
  776. * Wimax
  777. */
  778. static int asus_wimax_set(struct asus_laptop *asus, int status)
  779. {
  780. if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
  781. pr_warning("Error setting wimax status to %d", status);
  782. return -EIO;
  783. }
  784. return 0;
  785. }
  786. static ssize_t show_wimax(struct device *dev,
  787. struct device_attribute *attr, char *buf)
  788. {
  789. struct asus_laptop *asus = dev_get_drvdata(dev);
  790. return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
  791. }
  792. static ssize_t store_wimax(struct device *dev,
  793. struct device_attribute *attr, const char *buf,
  794. size_t count)
  795. {
  796. struct asus_laptop *asus = dev_get_drvdata(dev);
  797. return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
  798. }
  799. /*
  800. * Wwan
  801. */
  802. static int asus_wwan_set(struct asus_laptop *asus, int status)
  803. {
  804. if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
  805. pr_warning("Error setting wwan status to %d", status);
  806. return -EIO;
  807. }
  808. return 0;
  809. }
  810. static ssize_t show_wwan(struct device *dev,
  811. struct device_attribute *attr, char *buf)
  812. {
  813. struct asus_laptop *asus = dev_get_drvdata(dev);
  814. return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
  815. }
  816. static ssize_t store_wwan(struct device *dev,
  817. struct device_attribute *attr, const char *buf,
  818. size_t count)
  819. {
  820. struct asus_laptop *asus = dev_get_drvdata(dev);
  821. return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
  822. }
  823. /*
  824. * Display
  825. */
  826. static void asus_set_display(struct asus_laptop *asus, int value)
  827. {
  828. /* no sanity check needed for now */
  829. if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
  830. pr_warning("Error setting display\n");
  831. return;
  832. }
  833. static int read_display(struct asus_laptop *asus)
  834. {
  835. unsigned long long value = 0;
  836. acpi_status rv = AE_OK;
  837. /*
  838. * In most of the case, we know how to set the display, but sometime
  839. * we can't read it
  840. */
  841. if (display_get_handle) {
  842. rv = acpi_evaluate_integer(display_get_handle, NULL,
  843. NULL, &value);
  844. if (ACPI_FAILURE(rv))
  845. pr_warning("Error reading display status\n");
  846. }
  847. value &= 0x0F; /* needed for some models, shouldn't hurt others */
  848. return value;
  849. }
  850. /*
  851. * Now, *this* one could be more user-friendly, but so far, no-one has
  852. * complained. The significance of bits is the same as in store_disp()
  853. */
  854. static ssize_t show_disp(struct device *dev,
  855. struct device_attribute *attr, char *buf)
  856. {
  857. struct asus_laptop *asus = dev_get_drvdata(dev);
  858. if (!display_get_handle)
  859. return -ENODEV;
  860. return sprintf(buf, "%d\n", read_display(asus));
  861. }
  862. /*
  863. * Experimental support for display switching. As of now: 1 should activate
  864. * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
  865. * Any combination (bitwise) of these will suffice. I never actually tested 4
  866. * displays hooked up simultaneously, so be warned. See the acpi4asus README
  867. * for more info.
  868. */
  869. static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
  870. const char *buf, size_t count)
  871. {
  872. struct asus_laptop *asus = dev_get_drvdata(dev);
  873. int rv, value;
  874. rv = parse_arg(buf, count, &value);
  875. if (rv > 0)
  876. asus_set_display(asus, value);
  877. return rv;
  878. }
  879. /*
  880. * Light Sens
  881. */
  882. static void asus_als_switch(struct asus_laptop *asus, int value)
  883. {
  884. if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
  885. pr_warning("Error setting light sensor switch\n");
  886. asus->light_switch = value;
  887. }
  888. static ssize_t show_lssw(struct device *dev,
  889. struct device_attribute *attr, char *buf)
  890. {
  891. struct asus_laptop *asus = dev_get_drvdata(dev);
  892. return sprintf(buf, "%d\n", asus->light_switch);
  893. }
  894. static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
  895. const char *buf, size_t count)
  896. {
  897. struct asus_laptop *asus = dev_get_drvdata(dev);
  898. int rv, value;
  899. rv = parse_arg(buf, count, &value);
  900. if (rv > 0)
  901. asus_als_switch(asus, value ? 1 : 0);
  902. return rv;
  903. }
  904. static void asus_als_level(struct asus_laptop *asus, int value)
  905. {
  906. if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
  907. pr_warning("Error setting light sensor level\n");
  908. asus->light_level = value;
  909. }
  910. static ssize_t show_lslvl(struct device *dev,
  911. struct device_attribute *attr, char *buf)
  912. {
  913. struct asus_laptop *asus = dev_get_drvdata(dev);
  914. return sprintf(buf, "%d\n", asus->light_level);
  915. }
  916. static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
  917. const char *buf, size_t count)
  918. {
  919. struct asus_laptop *asus = dev_get_drvdata(dev);
  920. int rv, value;
  921. rv = parse_arg(buf, count, &value);
  922. if (rv > 0) {
  923. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  924. /* 0 <= value <= 15 */
  925. asus_als_level(asus, value);
  926. }
  927. return rv;
  928. }
  929. /*
  930. * GPS
  931. */
  932. static int asus_gps_status(struct asus_laptop *asus)
  933. {
  934. unsigned long long status;
  935. acpi_status rv = AE_OK;
  936. rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
  937. NULL, &status);
  938. if (ACPI_FAILURE(rv)) {
  939. pr_warning("Error reading GPS status\n");
  940. return -ENODEV;
  941. }
  942. return !!status;
  943. }
  944. static int asus_gps_switch(struct asus_laptop *asus, int status)
  945. {
  946. const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
  947. if (write_acpi_int(asus->handle, meth, 0x02))
  948. return -ENODEV;
  949. return 0;
  950. }
  951. static ssize_t show_gps(struct device *dev,
  952. struct device_attribute *attr, char *buf)
  953. {
  954. struct asus_laptop *asus = dev_get_drvdata(dev);
  955. return sprintf(buf, "%d\n", asus_gps_status(asus));
  956. }
  957. static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
  958. const char *buf, size_t count)
  959. {
  960. struct asus_laptop *asus = dev_get_drvdata(dev);
  961. int rv, value;
  962. int ret;
  963. rv = parse_arg(buf, count, &value);
  964. if (rv <= 0)
  965. return -EINVAL;
  966. ret = asus_gps_switch(asus, !!value);
  967. if (ret)
  968. return ret;
  969. rfkill_set_sw_state(asus->gps_rfkill, !value);
  970. return rv;
  971. }
  972. /*
  973. * rfkill
  974. */
  975. static int asus_gps_rfkill_set(void *data, bool blocked)
  976. {
  977. struct asus_laptop *asus = data;
  978. return asus_gps_switch(asus, !blocked);
  979. }
  980. static const struct rfkill_ops asus_gps_rfkill_ops = {
  981. .set_block = asus_gps_rfkill_set,
  982. };
  983. static void asus_rfkill_exit(struct asus_laptop *asus)
  984. {
  985. if (asus->gps_rfkill) {
  986. rfkill_unregister(asus->gps_rfkill);
  987. rfkill_destroy(asus->gps_rfkill);
  988. asus->gps_rfkill = NULL;
  989. }
  990. }
  991. static int asus_rfkill_init(struct asus_laptop *asus)
  992. {
  993. int result;
  994. if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
  995. acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
  996. acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
  997. return 0;
  998. asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
  999. RFKILL_TYPE_GPS,
  1000. &asus_gps_rfkill_ops, asus);
  1001. if (!asus->gps_rfkill)
  1002. return -EINVAL;
  1003. result = rfkill_register(asus->gps_rfkill);
  1004. if (result) {
  1005. rfkill_destroy(asus->gps_rfkill);
  1006. asus->gps_rfkill = NULL;
  1007. }
  1008. return result;
  1009. }
  1010. /*
  1011. * Input device (i.e. hotkeys)
  1012. */
  1013. static void asus_input_notify(struct asus_laptop *asus, int event)
  1014. {
  1015. if (asus->inputdev)
  1016. sparse_keymap_report_event(asus->inputdev, event, 1, true);
  1017. }
  1018. static int asus_input_init(struct asus_laptop *asus)
  1019. {
  1020. struct input_dev *input;
  1021. int error;
  1022. input = input_allocate_device();
  1023. if (!input) {
  1024. pr_info("Unable to allocate input device\n");
  1025. return -ENOMEM;
  1026. }
  1027. input->name = "Asus Laptop extra buttons";
  1028. input->phys = ASUS_LAPTOP_FILE "/input0";
  1029. input->id.bustype = BUS_HOST;
  1030. input->dev.parent = &asus->platform_device->dev;
  1031. error = sparse_keymap_setup(input, asus_keymap, NULL);
  1032. if (error) {
  1033. pr_err("Unable to setup input device keymap\n");
  1034. goto err_free_dev;
  1035. }
  1036. error = input_register_device(input);
  1037. if (error) {
  1038. pr_info("Unable to register input device\n");
  1039. goto err_free_keymap;
  1040. }
  1041. asus->inputdev = input;
  1042. return 0;
  1043. err_free_keymap:
  1044. sparse_keymap_free(input);
  1045. err_free_dev:
  1046. input_free_device(input);
  1047. return error;
  1048. }
  1049. static void asus_input_exit(struct asus_laptop *asus)
  1050. {
  1051. if (asus->inputdev) {
  1052. sparse_keymap_free(asus->inputdev);
  1053. input_unregister_device(asus->inputdev);
  1054. }
  1055. asus->inputdev = NULL;
  1056. }
  1057. /*
  1058. * ACPI driver
  1059. */
  1060. static void asus_acpi_notify(struct acpi_device *device, u32 event)
  1061. {
  1062. struct asus_laptop *asus = acpi_driver_data(device);
  1063. u16 count;
  1064. /*
  1065. * We need to tell the backlight device when the backlight power is
  1066. * switched
  1067. */
  1068. if (event == ATKD_LCD_ON)
  1069. lcd_blank(asus, FB_BLANK_UNBLANK);
  1070. else if (event == ATKD_LCD_OFF)
  1071. lcd_blank(asus, FB_BLANK_POWERDOWN);
  1072. /* TODO Find a better way to handle events count. */
  1073. count = asus->event_count[event % 128]++;
  1074. acpi_bus_generate_proc_event(asus->device, event, count);
  1075. acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
  1076. dev_name(&asus->device->dev), event,
  1077. count);
  1078. /* Brightness events are special */
  1079. if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
  1080. /* Ignore them completely if the acpi video driver is used */
  1081. if (asus->backlight_device != NULL) {
  1082. /* Update the backlight device. */
  1083. asus_backlight_notify(asus);
  1084. }
  1085. return ;
  1086. }
  1087. asus_input_notify(asus, event);
  1088. }
  1089. static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
  1090. static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
  1091. static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR,
  1092. show_bluetooth, store_bluetooth);
  1093. static DEVICE_ATTR(wimax, S_IRUGO | S_IWUSR, show_wimax, store_wimax);
  1094. static DEVICE_ATTR(wwan, S_IRUGO | S_IWUSR, show_wwan, store_wwan);
  1095. static DEVICE_ATTR(display, S_IRUGO | S_IWUSR, show_disp, store_disp);
  1096. static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
  1097. static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
  1098. static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
  1099. static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
  1100. static struct attribute *asus_attributes[] = {
  1101. &dev_attr_infos.attr,
  1102. &dev_attr_wlan.attr,
  1103. &dev_attr_bluetooth.attr,
  1104. &dev_attr_wimax.attr,
  1105. &dev_attr_wwan.attr,
  1106. &dev_attr_display.attr,
  1107. &dev_attr_ledd.attr,
  1108. &dev_attr_ls_level.attr,
  1109. &dev_attr_ls_switch.attr,
  1110. &dev_attr_gps.attr,
  1111. NULL
  1112. };
  1113. static mode_t asus_sysfs_is_visible(struct kobject *kobj,
  1114. struct attribute *attr,
  1115. int idx)
  1116. {
  1117. struct device *dev = container_of(kobj, struct device, kobj);
  1118. struct platform_device *pdev = to_platform_device(dev);
  1119. struct asus_laptop *asus = platform_get_drvdata(pdev);
  1120. acpi_handle handle = asus->handle;
  1121. bool supported;
  1122. if (attr == &dev_attr_wlan.attr) {
  1123. supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
  1124. } else if (attr == &dev_attr_bluetooth.attr) {
  1125. supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
  1126. } else if (attr == &dev_attr_display.attr) {
  1127. supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
  1128. } else if (attr == &dev_attr_wimax.attr) {
  1129. supported =
  1130. !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
  1131. } else if (attr == &dev_attr_wwan.attr) {
  1132. supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
  1133. } else if (attr == &dev_attr_ledd.attr) {
  1134. supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
  1135. } else if (attr == &dev_attr_ls_switch.attr ||
  1136. attr == &dev_attr_ls_level.attr) {
  1137. supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
  1138. !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
  1139. } else if (attr == &dev_attr_gps.attr) {
  1140. supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
  1141. !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
  1142. !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
  1143. } else {
  1144. supported = true;
  1145. }
  1146. return supported ? attr->mode : 0;
  1147. }
  1148. static const struct attribute_group asus_attr_group = {
  1149. .is_visible = asus_sysfs_is_visible,
  1150. .attrs = asus_attributes,
  1151. };
  1152. static int asus_platform_init(struct asus_laptop *asus)
  1153. {
  1154. int result;
  1155. asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
  1156. if (!asus->platform_device)
  1157. return -ENOMEM;
  1158. platform_set_drvdata(asus->platform_device, asus);
  1159. result = platform_device_add(asus->platform_device);
  1160. if (result)
  1161. goto fail_platform_device;
  1162. result = sysfs_create_group(&asus->platform_device->dev.kobj,
  1163. &asus_attr_group);
  1164. if (result)
  1165. goto fail_sysfs;
  1166. return 0;
  1167. fail_sysfs:
  1168. platform_device_del(asus->platform_device);
  1169. fail_platform_device:
  1170. platform_device_put(asus->platform_device);
  1171. return result;
  1172. }
  1173. static void asus_platform_exit(struct asus_laptop *asus)
  1174. {
  1175. sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
  1176. platform_device_unregister(asus->platform_device);
  1177. }
  1178. static struct platform_driver platform_driver = {
  1179. .driver = {
  1180. .name = ASUS_LAPTOP_FILE,
  1181. .owner = THIS_MODULE,
  1182. }
  1183. };
  1184. static int asus_handle_init(char *name, acpi_handle * handle,
  1185. char **paths, int num_paths)
  1186. {
  1187. int i;
  1188. acpi_status status;
  1189. for (i = 0; i < num_paths; i++) {
  1190. status = acpi_get_handle(NULL, paths[i], handle);
  1191. if (ACPI_SUCCESS(status))
  1192. return 0;
  1193. }
  1194. *handle = NULL;
  1195. return -ENODEV;
  1196. }
  1197. #define ASUS_HANDLE_INIT(object) \
  1198. asus_handle_init(#object, &object##_handle, object##_paths, \
  1199. ARRAY_SIZE(object##_paths))
  1200. /*
  1201. * This function is used to initialize the context with right values. In this
  1202. * method, we can make all the detection we want, and modify the asus_laptop
  1203. * struct
  1204. */
  1205. static int asus_laptop_get_info(struct asus_laptop *asus)
  1206. {
  1207. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1208. union acpi_object *model = NULL;
  1209. unsigned long long bsts_result, hwrs_result;
  1210. char *string = NULL;
  1211. acpi_status status;
  1212. /*
  1213. * Get DSDT headers early enough to allow for differentiating between
  1214. * models, but late enough to allow acpi_bus_register_driver() to fail
  1215. * before doing anything ACPI-specific. Should we encounter a machine,
  1216. * which needs special handling (i.e. its hotkey device has a different
  1217. * HID), this bit will be moved.
  1218. */
  1219. status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
  1220. if (ACPI_FAILURE(status))
  1221. pr_warning("Couldn't get the DSDT table header\n");
  1222. /* We have to write 0 on init this far for all ASUS models */
  1223. if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
  1224. pr_err("Hotkey initialization failed\n");
  1225. return -ENODEV;
  1226. }
  1227. /* This needs to be called for some laptops to init properly */
  1228. status =
  1229. acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
  1230. if (ACPI_FAILURE(status))
  1231. pr_warning("Error calling BSTS\n");
  1232. else if (bsts_result)
  1233. pr_notice("BSTS called, 0x%02x returned\n",
  1234. (uint) bsts_result);
  1235. /* This too ... */
  1236. if (write_acpi_int(asus->handle, "CWAP", wapf))
  1237. pr_err("Error calling CWAP(%d)\n", wapf);
  1238. /*
  1239. * Try to match the object returned by INIT to the specific model.
  1240. * Handle every possible object (or the lack of thereof) the DSDT
  1241. * writers might throw at us. When in trouble, we pass NULL to
  1242. * asus_model_match() and try something completely different.
  1243. */
  1244. if (buffer.pointer) {
  1245. model = buffer.pointer;
  1246. switch (model->type) {
  1247. case ACPI_TYPE_STRING:
  1248. string = model->string.pointer;
  1249. break;
  1250. case ACPI_TYPE_BUFFER:
  1251. string = model->buffer.pointer;
  1252. break;
  1253. default:
  1254. string = "";
  1255. break;
  1256. }
  1257. }
  1258. asus->name = kstrdup(string, GFP_KERNEL);
  1259. if (!asus->name) {
  1260. kfree(buffer.pointer);
  1261. return -ENOMEM;
  1262. }
  1263. if (*string)
  1264. pr_notice(" %s model detected\n", string);
  1265. /*
  1266. * The HWRS method return informations about the hardware.
  1267. * 0x80 bit is for WLAN, 0x100 for Bluetooth,
  1268. * 0x40 for WWAN, 0x10 for WIMAX.
  1269. * The significance of others is yet to be found.
  1270. */
  1271. status =
  1272. acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
  1273. if (!ACPI_FAILURE(status))
  1274. pr_notice(" HRWS returned %x", (int)hwrs_result);
  1275. if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
  1276. asus->have_rsts = true;
  1277. /* Scheduled for removal */
  1278. ASUS_HANDLE_INIT(lcd_switch);
  1279. ASUS_HANDLE_INIT(display_get);
  1280. kfree(model);
  1281. return AE_OK;
  1282. }
  1283. static int __devinit asus_acpi_init(struct asus_laptop *asus)
  1284. {
  1285. int result = 0;
  1286. result = acpi_bus_get_status(asus->device);
  1287. if (result)
  1288. return result;
  1289. if (!asus->device->status.present) {
  1290. pr_err("Hotkey device not present, aborting\n");
  1291. return -ENODEV;
  1292. }
  1293. result = asus_laptop_get_info(asus);
  1294. if (result)
  1295. return result;
  1296. /* WLED and BLED are on by default */
  1297. if (bluetooth_status >= 0)
  1298. asus_bluetooth_set(asus, !!bluetooth_status);
  1299. if (wlan_status >= 0)
  1300. asus_wlan_set(asus, !!wlan_status);
  1301. if (wimax_status >= 0)
  1302. asus_wimax_set(asus, !!wimax_status);
  1303. if (wwan_status >= 0)
  1304. asus_wwan_set(asus, !!wwan_status);
  1305. /* Keyboard Backlight is on by default */
  1306. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
  1307. asus_kled_set(asus, 1);
  1308. /* LED display is off by default */
  1309. asus->ledd_status = 0xFFF;
  1310. /* Set initial values of light sensor and level */
  1311. asus->light_switch = 0; /* Default to light sensor disabled */
  1312. asus->light_level = 5; /* level 5 for sensor sensitivity */
  1313. if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1314. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1315. asus_als_switch(asus, asus->light_switch);
  1316. asus_als_level(asus, asus->light_level);
  1317. }
  1318. asus->lcd_state = 1; /* LCD should be on when the module load */
  1319. return result;
  1320. }
  1321. static bool asus_device_present;
  1322. static int __devinit asus_acpi_add(struct acpi_device *device)
  1323. {
  1324. struct asus_laptop *asus;
  1325. int result;
  1326. pr_notice("Asus Laptop Support version %s\n",
  1327. ASUS_LAPTOP_VERSION);
  1328. asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
  1329. if (!asus)
  1330. return -ENOMEM;
  1331. asus->handle = device->handle;
  1332. strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
  1333. strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
  1334. device->driver_data = asus;
  1335. asus->device = device;
  1336. result = asus_acpi_init(asus);
  1337. if (result)
  1338. goto fail_platform;
  1339. /*
  1340. * Register the platform device first. It is used as a parent for the
  1341. * sub-devices below.
  1342. */
  1343. result = asus_platform_init(asus);
  1344. if (result)
  1345. goto fail_platform;
  1346. if (!acpi_video_backlight_support()) {
  1347. result = asus_backlight_init(asus);
  1348. if (result)
  1349. goto fail_backlight;
  1350. } else
  1351. pr_info("Backlight controlled by ACPI video driver\n");
  1352. result = asus_input_init(asus);
  1353. if (result)
  1354. goto fail_input;
  1355. result = asus_led_init(asus);
  1356. if (result)
  1357. goto fail_led;
  1358. result = asus_rfkill_init(asus);
  1359. if (result)
  1360. goto fail_rfkill;
  1361. asus_device_present = true;
  1362. return 0;
  1363. fail_rfkill:
  1364. asus_led_exit(asus);
  1365. fail_led:
  1366. asus_input_exit(asus);
  1367. fail_input:
  1368. asus_backlight_exit(asus);
  1369. fail_backlight:
  1370. asus_platform_exit(asus);
  1371. fail_platform:
  1372. kfree(asus->name);
  1373. kfree(asus);
  1374. return result;
  1375. }
  1376. static int asus_acpi_remove(struct acpi_device *device, int type)
  1377. {
  1378. struct asus_laptop *asus = acpi_driver_data(device);
  1379. asus_backlight_exit(asus);
  1380. asus_rfkill_exit(asus);
  1381. asus_led_exit(asus);
  1382. asus_input_exit(asus);
  1383. asus_platform_exit(asus);
  1384. kfree(asus->name);
  1385. kfree(asus);
  1386. return 0;
  1387. }
  1388. static const struct acpi_device_id asus_device_ids[] = {
  1389. {"ATK0100", 0},
  1390. {"ATK0101", 0},
  1391. {"", 0},
  1392. };
  1393. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  1394. static struct acpi_driver asus_acpi_driver = {
  1395. .name = ASUS_LAPTOP_NAME,
  1396. .class = ASUS_LAPTOP_CLASS,
  1397. .owner = THIS_MODULE,
  1398. .ids = asus_device_ids,
  1399. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1400. .ops = {
  1401. .add = asus_acpi_add,
  1402. .remove = asus_acpi_remove,
  1403. .notify = asus_acpi_notify,
  1404. },
  1405. };
  1406. static int __init asus_laptop_init(void)
  1407. {
  1408. int result;
  1409. result = platform_driver_register(&platform_driver);
  1410. if (result < 0)
  1411. return result;
  1412. result = acpi_bus_register_driver(&asus_acpi_driver);
  1413. if (result < 0)
  1414. goto fail_acpi_driver;
  1415. if (!asus_device_present) {
  1416. result = -ENODEV;
  1417. goto fail_no_device;
  1418. }
  1419. return 0;
  1420. fail_no_device:
  1421. acpi_bus_unregister_driver(&asus_acpi_driver);
  1422. fail_acpi_driver:
  1423. platform_driver_unregister(&platform_driver);
  1424. return result;
  1425. }
  1426. static void __exit asus_laptop_exit(void)
  1427. {
  1428. acpi_bus_unregister_driver(&asus_acpi_driver);
  1429. platform_driver_unregister(&platform_driver);
  1430. }
  1431. module_init(asus_laptop_init);
  1432. module_exit(asus_laptop_exit);