asus-laptop.c 40 KB

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