asus-laptop.c 42 KB

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