asus-laptop.c 42 KB

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