asus-laptop.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. /*
  2. * asus-laptop.c - Asus Laptop Support
  3. *
  4. *
  5. * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
  6. * Copyright (C) 2006-2007 Corentin Chary
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * The development page for this driver is located at
  24. * http://sourceforge.net/projects/acpi4asus/
  25. *
  26. * Credits:
  27. * Pontus Fuchs - Helper functions, cleanup
  28. * Johann Wiesner - Small compile fixes
  29. * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
  30. * Eric Burghard - LED display support for W1N
  31. * Josh Green - Light Sens support
  32. * Thomas Tuttle - His first patch for led support was very helpfull
  33. * Sam Lin - GPS support
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/err.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/backlight.h>
  43. #include <linux/fb.h>
  44. #include <linux/leds.h>
  45. #include <linux/platform_device.h>
  46. #include <acpi/acpi_drivers.h>
  47. #include <acpi/acpi_bus.h>
  48. #include <asm/uaccess.h>
  49. #include <linux/input.h>
  50. #define ASUS_LAPTOP_VERSION "0.42"
  51. #define ASUS_HOTK_NAME "Asus Laptop Support"
  52. #define ASUS_HOTK_CLASS "hotkey"
  53. #define ASUS_HOTK_DEVICE_NAME "Hotkey"
  54. #define ASUS_HOTK_FILE KBUILD_MODNAME
  55. #define ASUS_HOTK_PREFIX "\\_SB.ATKD."
  56. /*
  57. * Some events we use, same for all Asus
  58. */
  59. #define ATKD_BR_UP 0x10
  60. #define ATKD_BR_DOWN 0x20
  61. #define ATKD_LCD_ON 0x33
  62. #define ATKD_LCD_OFF 0x34
  63. /*
  64. * Known bits returned by \_SB.ATKD.HWRS
  65. */
  66. #define WL_HWRS 0x80
  67. #define BT_HWRS 0x100
  68. /*
  69. * Flags for hotk status
  70. * WL_ON and BT_ON are also used for wireless_status()
  71. */
  72. #define WL_ON 0x01 //internal Wifi
  73. #define BT_ON 0x02 //internal Bluetooth
  74. #define MLED_ON 0x04 //mail LED
  75. #define TLED_ON 0x08 //touchpad LED
  76. #define RLED_ON 0x10 //Record LED
  77. #define PLED_ON 0x20 //Phone LED
  78. #define GLED_ON 0x40 //Gaming LED
  79. #define LCD_ON 0x80 //LCD backlight
  80. #define GPS_ON 0x100 //GPS
  81. #define KEY_ON 0x200 //Keyboard backlight
  82. #define ASUS_LOG ASUS_HOTK_FILE ": "
  83. #define ASUS_ERR KERN_ERR ASUS_LOG
  84. #define ASUS_WARNING KERN_WARNING ASUS_LOG
  85. #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
  86. #define ASUS_INFO KERN_INFO ASUS_LOG
  87. #define ASUS_DEBUG KERN_DEBUG ASUS_LOG
  88. MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
  89. MODULE_DESCRIPTION(ASUS_HOTK_NAME);
  90. MODULE_LICENSE("GPL");
  91. /* WAPF defines the behavior of the Fn+Fx wlan key
  92. * The significance of values is yet to be found, but
  93. * most of the time:
  94. * 0x0 will do nothing
  95. * 0x1 will allow to control the device with Fn+Fx key.
  96. * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
  97. * 0x5 like 0x1 or 0x4
  98. * So, if something doesn't work as you want, just try other values =)
  99. */
  100. static uint wapf = 1;
  101. module_param(wapf, uint, 0644);
  102. MODULE_PARM_DESC(wapf, "WAPF value");
  103. #define ASUS_HANDLE(object, paths...) \
  104. static acpi_handle object##_handle = NULL; \
  105. static char *object##_paths[] = { paths }
  106. /* LED */
  107. ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
  108. ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
  109. ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
  110. ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
  111. ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
  112. /* LEDD */
  113. ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
  114. /* Bluetooth and WLAN
  115. * WLED and BLED are not handled like other XLED, because in some dsdt
  116. * they also control the WLAN/Bluetooth device.
  117. */
  118. ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
  119. ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
  120. ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
  121. /* Brightness */
  122. ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
  123. ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
  124. /* Backlight */
  125. ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
  126. "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
  127. "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
  128. "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
  129. "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
  130. "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
  131. "\\_SB.PCI0.PX40.Q10", /* S1x */
  132. "\\Q10"); /* A2x, L2D, L3D, M2E */
  133. /* Display */
  134. ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
  135. ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
  136. M6A M6V VX-1 V6J V6V W3Z */
  137. "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
  138. S5A M5A z33A W1Jc W2V G1 */
  139. "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
  140. "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
  141. "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
  142. "\\_SB.PCI0.VGA.GETD", /* Z96F */
  143. "\\ACTD", /* A2D */
  144. "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
  145. "\\DNXT", /* P30 */
  146. "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
  147. "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
  148. ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
  149. ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
  150. /* GPS */
  151. /* R2H use different handle for GPS on/off */
  152. ASUS_HANDLE(gps_on, ASUS_HOTK_PREFIX "SDON"); /* R2H */
  153. ASUS_HANDLE(gps_off, ASUS_HOTK_PREFIX "SDOF"); /* R2H */
  154. ASUS_HANDLE(gps_status, ASUS_HOTK_PREFIX "GPST");
  155. /* Keyboard light */
  156. ASUS_HANDLE(kled_set, ASUS_HOTK_PREFIX "SLKB");
  157. ASUS_HANDLE(kled_get, ASUS_HOTK_PREFIX "GLKB");
  158. /*
  159. * This is the main structure, we can use it to store anything interesting
  160. * about the hotk device
  161. */
  162. struct asus_hotk {
  163. char *name; //laptop name
  164. struct acpi_device *device; //the device we are in
  165. acpi_handle handle; //the handle of the hotk device
  166. char status; //status of the hotk, for LEDs, ...
  167. u32 ledd_status; //status of the LED display
  168. u8 light_level; //light sensor level
  169. u8 light_switch; //light sensor switch value
  170. u16 event_count[128]; //count for each event TODO make this better
  171. struct input_dev *inputdev;
  172. u16 *keycode_map;
  173. };
  174. /*
  175. * This header is made available to allow proper configuration given model,
  176. * revision number , ... this info cannot go in struct asus_hotk because it is
  177. * available before the hotk
  178. */
  179. static struct acpi_table_header *asus_info;
  180. /* The actual device the driver binds to */
  181. static struct asus_hotk *hotk;
  182. /*
  183. * The hotkey driver declaration
  184. */
  185. static const struct acpi_device_id asus_device_ids[] = {
  186. {"ATK0100", 0},
  187. {"", 0},
  188. };
  189. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  190. static int asus_hotk_add(struct acpi_device *device);
  191. static int asus_hotk_remove(struct acpi_device *device, int type);
  192. static void asus_hotk_notify(struct acpi_device *device, u32 event);
  193. static struct acpi_driver asus_hotk_driver = {
  194. .name = ASUS_HOTK_NAME,
  195. .class = ASUS_HOTK_CLASS,
  196. .ids = asus_device_ids,
  197. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  198. .ops = {
  199. .add = asus_hotk_add,
  200. .remove = asus_hotk_remove,
  201. .notify = asus_hotk_notify,
  202. },
  203. };
  204. /* The backlight device /sys/class/backlight */
  205. static struct backlight_device *asus_backlight_device;
  206. /*
  207. * The backlight class declaration
  208. */
  209. static int read_brightness(struct backlight_device *bd);
  210. static int update_bl_status(struct backlight_device *bd);
  211. static struct backlight_ops asusbl_ops = {
  212. .get_brightness = read_brightness,
  213. .update_status = update_bl_status,
  214. };
  215. /* These functions actually update the LED's, and are called from a
  216. * workqueue. By doing this as separate work rather than when the LED
  217. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  218. * potentially bad time, such as a timer interrupt. */
  219. static struct workqueue_struct *led_workqueue;
  220. #define ASUS_LED(object, ledname, max) \
  221. static void object##_led_set(struct led_classdev *led_cdev, \
  222. enum led_brightness value); \
  223. static enum led_brightness object##_led_get( \
  224. struct led_classdev *led_cdev); \
  225. static void object##_led_update(struct work_struct *ignored); \
  226. static int object##_led_wk; \
  227. static DECLARE_WORK(object##_led_work, object##_led_update); \
  228. static struct led_classdev object##_led = { \
  229. .name = "asus::" ledname, \
  230. .brightness_set = object##_led_set, \
  231. .brightness_get = object##_led_get, \
  232. .max_brightness = max \
  233. }
  234. ASUS_LED(mled, "mail", 1);
  235. ASUS_LED(tled, "touchpad", 1);
  236. ASUS_LED(rled, "record", 1);
  237. ASUS_LED(pled, "phone", 1);
  238. ASUS_LED(gled, "gaming", 1);
  239. ASUS_LED(kled, "kbd_backlight", 3);
  240. struct key_entry {
  241. char type;
  242. u8 code;
  243. u16 keycode;
  244. };
  245. enum { KE_KEY, KE_END };
  246. static struct key_entry asus_keymap[] = {
  247. {KE_KEY, 0x30, KEY_VOLUMEUP},
  248. {KE_KEY, 0x31, KEY_VOLUMEDOWN},
  249. {KE_KEY, 0x32, KEY_MUTE},
  250. {KE_KEY, 0x33, KEY_SWITCHVIDEOMODE},
  251. {KE_KEY, 0x34, KEY_SWITCHVIDEOMODE},
  252. {KE_KEY, 0x40, KEY_PREVIOUSSONG},
  253. {KE_KEY, 0x41, KEY_NEXTSONG},
  254. {KE_KEY, 0x43, KEY_STOPCD},
  255. {KE_KEY, 0x45, KEY_PLAYPAUSE},
  256. {KE_KEY, 0x4c, KEY_MEDIA},
  257. {KE_KEY, 0x50, KEY_EMAIL},
  258. {KE_KEY, 0x51, KEY_WWW},
  259. {KE_KEY, 0x5C, KEY_SCREENLOCK}, /* Screenlock */
  260. {KE_KEY, 0x5D, KEY_WLAN},
  261. {KE_KEY, 0x5E, KEY_WLAN},
  262. {KE_KEY, 0x5F, KEY_WLAN},
  263. {KE_KEY, 0x60, KEY_SWITCHVIDEOMODE},
  264. {KE_KEY, 0x61, KEY_SWITCHVIDEOMODE},
  265. {KE_KEY, 0x6B, BTN_TOUCH}, /* Lock Mouse */
  266. {KE_KEY, 0x82, KEY_CAMERA},
  267. {KE_KEY, 0x8A, KEY_PROG1},
  268. {KE_KEY, 0x95, KEY_MEDIA},
  269. {KE_KEY, 0x99, KEY_PHONE},
  270. {KE_KEY, 0xc4, KEY_KBDILLUMUP},
  271. {KE_KEY, 0xc5, KEY_KBDILLUMDOWN},
  272. {KE_END, 0},
  273. };
  274. /*
  275. * This function evaluates an ACPI method, given an int as parameter, the
  276. * method is searched within the scope of the handle, can be NULL. The output
  277. * of the method is written is output, which can also be NULL
  278. *
  279. * returns 0 if write is successful, -1 else.
  280. */
  281. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  282. struct acpi_buffer *output)
  283. {
  284. struct acpi_object_list params; //list of input parameters (an int here)
  285. union acpi_object in_obj; //the only param we use
  286. acpi_status status;
  287. if (!handle)
  288. return 0;
  289. params.count = 1;
  290. params.pointer = &in_obj;
  291. in_obj.type = ACPI_TYPE_INTEGER;
  292. in_obj.integer.value = val;
  293. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  294. if (status == AE_OK)
  295. return 0;
  296. else
  297. return -1;
  298. }
  299. static int read_wireless_status(int mask)
  300. {
  301. unsigned long long status;
  302. acpi_status rv = AE_OK;
  303. if (!wireless_status_handle)
  304. return (hotk->status & mask) ? 1 : 0;
  305. rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
  306. if (ACPI_FAILURE(rv))
  307. pr_warning("Error reading Wireless status\n");
  308. else
  309. return (status & mask) ? 1 : 0;
  310. return (hotk->status & mask) ? 1 : 0;
  311. }
  312. static int read_gps_status(void)
  313. {
  314. unsigned long long status;
  315. acpi_status rv = AE_OK;
  316. rv = acpi_evaluate_integer(gps_status_handle, NULL, NULL, &status);
  317. if (ACPI_FAILURE(rv))
  318. pr_warning("Error reading GPS status\n");
  319. else
  320. return status ? 1 : 0;
  321. return (hotk->status & GPS_ON) ? 1 : 0;
  322. }
  323. /* Generic LED functions */
  324. static int read_status(int mask)
  325. {
  326. /* There is a special method for both wireless devices */
  327. if (mask == BT_ON || mask == WL_ON)
  328. return read_wireless_status(mask);
  329. else if (mask == GPS_ON)
  330. return read_gps_status();
  331. return (hotk->status & mask) ? 1 : 0;
  332. }
  333. static void write_status(acpi_handle handle, int out, int mask)
  334. {
  335. hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
  336. switch (mask) {
  337. case MLED_ON:
  338. out = !(out & 0x1);
  339. break;
  340. case GLED_ON:
  341. out = (out & 0x1) + 1;
  342. break;
  343. case GPS_ON:
  344. handle = (out) ? gps_on_handle : gps_off_handle;
  345. out = 0x02;
  346. break;
  347. default:
  348. out &= 0x1;
  349. break;
  350. }
  351. if (write_acpi_int(handle, NULL, out, NULL))
  352. pr_warning(" write failed %x\n", mask);
  353. }
  354. /* /sys/class/led handlers */
  355. #define ASUS_LED_HANDLER(object, mask) \
  356. static void object##_led_set(struct led_classdev *led_cdev, \
  357. enum led_brightness value) \
  358. { \
  359. object##_led_wk = (value > 0) ? 1 : 0; \
  360. queue_work(led_workqueue, &object##_led_work); \
  361. } \
  362. static void object##_led_update(struct work_struct *ignored) \
  363. { \
  364. int value = object##_led_wk; \
  365. write_status(object##_set_handle, value, (mask)); \
  366. } \
  367. static enum led_brightness object##_led_get( \
  368. struct led_classdev *led_cdev) \
  369. { \
  370. return led_cdev->brightness; \
  371. }
  372. ASUS_LED_HANDLER(mled, MLED_ON);
  373. ASUS_LED_HANDLER(pled, PLED_ON);
  374. ASUS_LED_HANDLER(rled, RLED_ON);
  375. ASUS_LED_HANDLER(tled, TLED_ON);
  376. ASUS_LED_HANDLER(gled, GLED_ON);
  377. /*
  378. * Keyboard backlight
  379. */
  380. static int get_kled_lvl(void)
  381. {
  382. unsigned long long kblv;
  383. struct acpi_object_list params;
  384. union acpi_object in_obj;
  385. acpi_status rv;
  386. params.count = 1;
  387. params.pointer = &in_obj;
  388. in_obj.type = ACPI_TYPE_INTEGER;
  389. in_obj.integer.value = 2;
  390. rv = acpi_evaluate_integer(kled_get_handle, NULL, &params, &kblv);
  391. if (ACPI_FAILURE(rv)) {
  392. pr_warning("Error reading kled level\n");
  393. return 0;
  394. }
  395. return kblv;
  396. }
  397. static int set_kled_lvl(int kblv)
  398. {
  399. if (kblv > 0)
  400. kblv = (1 << 7) | (kblv & 0x7F);
  401. else
  402. kblv = 0;
  403. if (write_acpi_int(kled_set_handle, NULL, kblv, NULL)) {
  404. pr_warning("Keyboard LED display write failed\n");
  405. return -EINVAL;
  406. }
  407. return 0;
  408. }
  409. static void kled_led_set(struct led_classdev *led_cdev,
  410. enum led_brightness value)
  411. {
  412. kled_led_wk = value;
  413. queue_work(led_workqueue, &kled_led_work);
  414. }
  415. static void kled_led_update(struct work_struct *ignored)
  416. {
  417. set_kled_lvl(kled_led_wk);
  418. }
  419. static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
  420. {
  421. return get_kled_lvl();
  422. }
  423. static int get_lcd_state(void)
  424. {
  425. return read_status(LCD_ON);
  426. }
  427. static int set_lcd_state(int value)
  428. {
  429. int lcd = 0;
  430. acpi_status status = 0;
  431. lcd = value ? 1 : 0;
  432. if (lcd == get_lcd_state())
  433. return 0;
  434. if (lcd_switch_handle) {
  435. status = acpi_evaluate_object(lcd_switch_handle,
  436. NULL, NULL, NULL);
  437. if (ACPI_FAILURE(status))
  438. pr_warning("Error switching LCD\n");
  439. }
  440. write_status(NULL, lcd, LCD_ON);
  441. return 0;
  442. }
  443. static void lcd_blank(int blank)
  444. {
  445. struct backlight_device *bd = asus_backlight_device;
  446. if (bd) {
  447. bd->props.power = blank;
  448. backlight_update_status(bd);
  449. }
  450. }
  451. static int read_brightness(struct backlight_device *bd)
  452. {
  453. unsigned long long value;
  454. acpi_status rv = AE_OK;
  455. rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
  456. if (ACPI_FAILURE(rv))
  457. pr_warning("Error reading brightness\n");
  458. return value;
  459. }
  460. static int set_brightness(struct backlight_device *bd, int value)
  461. {
  462. int ret = 0;
  463. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  464. /* 0 <= value <= 15 */
  465. if (write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
  466. pr_warning("Error changing brightness\n");
  467. ret = -EIO;
  468. }
  469. return ret;
  470. }
  471. static int update_bl_status(struct backlight_device *bd)
  472. {
  473. int rv;
  474. int value = bd->props.brightness;
  475. rv = set_brightness(bd, value);
  476. if (rv)
  477. return rv;
  478. value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
  479. return set_lcd_state(value);
  480. }
  481. /*
  482. * Platform device handlers
  483. */
  484. /*
  485. * We write our info in page, we begin at offset off and cannot write more
  486. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  487. * number of bytes written in page
  488. */
  489. static ssize_t show_infos(struct device *dev,
  490. struct device_attribute *attr, char *page)
  491. {
  492. int len = 0;
  493. unsigned long long temp;
  494. char buf[16]; //enough for all info
  495. acpi_status rv = AE_OK;
  496. /*
  497. * We use the easy way, we don't care of off and count, so we don't set eof
  498. * to 1
  499. */
  500. len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
  501. len += sprintf(page + len, "Model reference : %s\n", hotk->name);
  502. /*
  503. * The SFUN method probably allows the original driver to get the list
  504. * of features supported by a given model. For now, 0x0100 or 0x0800
  505. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  506. * The significance of others is yet to be found.
  507. */
  508. rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
  509. if (!ACPI_FAILURE(rv))
  510. len += sprintf(page + len, "SFUN value : %#x\n",
  511. (uint) temp);
  512. /*
  513. * The HWRS method return informations about the hardware.
  514. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  515. * The significance of others is yet to be found.
  516. * If we don't find the method, we assume the device are present.
  517. */
  518. rv = acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &temp);
  519. if (!ACPI_FAILURE(rv))
  520. len += sprintf(page + len, "HRWS value : %#x\n",
  521. (uint) temp);
  522. /*
  523. * Another value for userspace: the ASYM method returns 0x02 for
  524. * battery low and 0x04 for battery critical, its readings tend to be
  525. * more accurate than those provided by _BST.
  526. * Note: since not all the laptops provide this method, errors are
  527. * silently ignored.
  528. */
  529. rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
  530. if (!ACPI_FAILURE(rv))
  531. len += sprintf(page + len, "ASYM value : %#x\n",
  532. (uint) temp);
  533. if (asus_info) {
  534. snprintf(buf, 16, "%d", asus_info->length);
  535. len += sprintf(page + len, "DSDT length : %s\n", buf);
  536. snprintf(buf, 16, "%d", asus_info->checksum);
  537. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  538. snprintf(buf, 16, "%d", asus_info->revision);
  539. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  540. snprintf(buf, 7, "%s", asus_info->oem_id);
  541. len += sprintf(page + len, "OEM id : %s\n", buf);
  542. snprintf(buf, 9, "%s", asus_info->oem_table_id);
  543. len += sprintf(page + len, "OEM table id : %s\n", buf);
  544. snprintf(buf, 16, "%x", asus_info->oem_revision);
  545. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  546. snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
  547. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  548. snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
  549. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  550. }
  551. return len;
  552. }
  553. static int parse_arg(const char *buf, unsigned long count, int *val)
  554. {
  555. if (!count)
  556. return 0;
  557. if (count > 31)
  558. return -EINVAL;
  559. if (sscanf(buf, "%i", val) != 1)
  560. return -EINVAL;
  561. return count;
  562. }
  563. static ssize_t store_status(const char *buf, size_t count,
  564. acpi_handle handle, int mask)
  565. {
  566. int rv, value;
  567. int out = 0;
  568. rv = parse_arg(buf, count, &value);
  569. if (rv > 0)
  570. out = value ? 1 : 0;
  571. write_status(handle, out, mask);
  572. return rv;
  573. }
  574. /*
  575. * LEDD display
  576. */
  577. static ssize_t show_ledd(struct device *dev,
  578. struct device_attribute *attr, char *buf)
  579. {
  580. return sprintf(buf, "0x%08x\n", hotk->ledd_status);
  581. }
  582. static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
  583. const char *buf, size_t count)
  584. {
  585. int rv, value;
  586. rv = parse_arg(buf, count, &value);
  587. if (rv > 0) {
  588. if (write_acpi_int(ledd_set_handle, NULL, value, NULL))
  589. pr_warning("LED display write failed\n");
  590. else
  591. hotk->ledd_status = (u32) value;
  592. }
  593. return rv;
  594. }
  595. /*
  596. * WLAN
  597. */
  598. static ssize_t show_wlan(struct device *dev,
  599. struct device_attribute *attr, char *buf)
  600. {
  601. return sprintf(buf, "%d\n", read_status(WL_ON));
  602. }
  603. static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
  604. const char *buf, size_t count)
  605. {
  606. return store_status(buf, count, wl_switch_handle, WL_ON);
  607. }
  608. /*
  609. * Bluetooth
  610. */
  611. static ssize_t show_bluetooth(struct device *dev,
  612. struct device_attribute *attr, char *buf)
  613. {
  614. return sprintf(buf, "%d\n", read_status(BT_ON));
  615. }
  616. static ssize_t store_bluetooth(struct device *dev,
  617. struct device_attribute *attr, const char *buf,
  618. size_t count)
  619. {
  620. return store_status(buf, count, bt_switch_handle, BT_ON);
  621. }
  622. /*
  623. * Display
  624. */
  625. static void set_display(int value)
  626. {
  627. /* no sanity check needed for now */
  628. if (write_acpi_int(display_set_handle, NULL, value, NULL))
  629. pr_warning("Error setting display\n");
  630. return;
  631. }
  632. static int read_display(void)
  633. {
  634. unsigned long long value = 0;
  635. acpi_status rv = AE_OK;
  636. /* In most of the case, we know how to set the display, but sometime
  637. we can't read it */
  638. if (display_get_handle) {
  639. rv = acpi_evaluate_integer(display_get_handle, NULL,
  640. NULL, &value);
  641. if (ACPI_FAILURE(rv))
  642. pr_warning("Error reading display status\n");
  643. }
  644. value &= 0x0F; /* needed for some models, shouldn't hurt others */
  645. return value;
  646. }
  647. /*
  648. * Now, *this* one could be more user-friendly, but so far, no-one has
  649. * complained. The significance of bits is the same as in store_disp()
  650. */
  651. static ssize_t show_disp(struct device *dev,
  652. struct device_attribute *attr, char *buf)
  653. {
  654. return sprintf(buf, "%d\n", read_display());
  655. }
  656. /*
  657. * Experimental support for display switching. As of now: 1 should activate
  658. * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
  659. * Any combination (bitwise) of these will suffice. I never actually tested 4
  660. * displays hooked up simultaneously, so be warned. See the acpi4asus README
  661. * for more info.
  662. */
  663. static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
  664. const char *buf, size_t count)
  665. {
  666. int rv, value;
  667. rv = parse_arg(buf, count, &value);
  668. if (rv > 0)
  669. set_display(value);
  670. return rv;
  671. }
  672. /*
  673. * Light Sens
  674. */
  675. static void set_light_sens_switch(int value)
  676. {
  677. if (write_acpi_int(ls_switch_handle, NULL, value, NULL))
  678. pr_warning("Error setting light sensor switch\n");
  679. hotk->light_switch = value;
  680. }
  681. static ssize_t show_lssw(struct device *dev,
  682. struct device_attribute *attr, char *buf)
  683. {
  684. return sprintf(buf, "%d\n", hotk->light_switch);
  685. }
  686. static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
  687. const char *buf, size_t count)
  688. {
  689. int rv, value;
  690. rv = parse_arg(buf, count, &value);
  691. if (rv > 0)
  692. set_light_sens_switch(value ? 1 : 0);
  693. return rv;
  694. }
  695. static void set_light_sens_level(int value)
  696. {
  697. if (write_acpi_int(ls_level_handle, NULL, value, NULL))
  698. pr_warning("Error setting light sensor level\n");
  699. hotk->light_level = value;
  700. }
  701. static ssize_t show_lslvl(struct device *dev,
  702. struct device_attribute *attr, char *buf)
  703. {
  704. return sprintf(buf, "%d\n", hotk->light_level);
  705. }
  706. static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
  707. const char *buf, size_t count)
  708. {
  709. int rv, value;
  710. rv = parse_arg(buf, count, &value);
  711. if (rv > 0) {
  712. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  713. /* 0 <= value <= 15 */
  714. set_light_sens_level(value);
  715. }
  716. return rv;
  717. }
  718. /*
  719. * GPS
  720. */
  721. static ssize_t show_gps(struct device *dev,
  722. struct device_attribute *attr, char *buf)
  723. {
  724. return sprintf(buf, "%d\n", read_status(GPS_ON));
  725. }
  726. static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
  727. const char *buf, size_t count)
  728. {
  729. return store_status(buf, count, NULL, GPS_ON);
  730. }
  731. /*
  732. * Hotkey functions
  733. */
  734. static struct key_entry *asus_get_entry_by_scancode(int code)
  735. {
  736. struct key_entry *key;
  737. for (key = asus_keymap; key->type != KE_END; key++)
  738. if (code == key->code)
  739. return key;
  740. return NULL;
  741. }
  742. static struct key_entry *asus_get_entry_by_keycode(int code)
  743. {
  744. struct key_entry *key;
  745. for (key = asus_keymap; key->type != KE_END; key++)
  746. if (code == key->keycode && key->type == KE_KEY)
  747. return key;
  748. return NULL;
  749. }
  750. static int asus_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  751. {
  752. struct key_entry *key = asus_get_entry_by_scancode(scancode);
  753. if (key && key->type == KE_KEY) {
  754. *keycode = key->keycode;
  755. return 0;
  756. }
  757. return -EINVAL;
  758. }
  759. static int asus_setkeycode(struct input_dev *dev, int scancode, int keycode)
  760. {
  761. struct key_entry *key;
  762. int old_keycode;
  763. if (keycode < 0 || keycode > KEY_MAX)
  764. return -EINVAL;
  765. key = asus_get_entry_by_scancode(scancode);
  766. if (key && key->type == KE_KEY) {
  767. old_keycode = key->keycode;
  768. key->keycode = keycode;
  769. set_bit(keycode, dev->keybit);
  770. if (!asus_get_entry_by_keycode(old_keycode))
  771. clear_bit(old_keycode, dev->keybit);
  772. return 0;
  773. }
  774. return -EINVAL;
  775. }
  776. static void asus_hotk_notify(struct acpi_device *device, u32 event)
  777. {
  778. static struct key_entry *key;
  779. u16 count;
  780. /* TODO Find a better way to handle events count. */
  781. if (!hotk)
  782. return;
  783. /*
  784. * We need to tell the backlight device when the backlight power is
  785. * switched
  786. */
  787. if (event == ATKD_LCD_ON) {
  788. write_status(NULL, 1, LCD_ON);
  789. lcd_blank(FB_BLANK_UNBLANK);
  790. } else if (event == ATKD_LCD_OFF) {
  791. write_status(NULL, 0, LCD_ON);
  792. lcd_blank(FB_BLANK_POWERDOWN);
  793. }
  794. count = hotk->event_count[event % 128]++;
  795. acpi_bus_generate_proc_event(hotk->device, event, count);
  796. acpi_bus_generate_netlink_event(hotk->device->pnp.device_class,
  797. dev_name(&hotk->device->dev), event,
  798. count);
  799. if (hotk->inputdev) {
  800. key = asus_get_entry_by_scancode(event);
  801. if (!key)
  802. return ;
  803. switch (key->type) {
  804. case KE_KEY:
  805. input_report_key(hotk->inputdev, key->keycode, 1);
  806. input_sync(hotk->inputdev);
  807. input_report_key(hotk->inputdev, key->keycode, 0);
  808. input_sync(hotk->inputdev);
  809. break;
  810. }
  811. }
  812. }
  813. #define ASUS_CREATE_DEVICE_ATTR(_name) \
  814. struct device_attribute dev_attr_##_name = { \
  815. .attr = { \
  816. .name = __stringify(_name), \
  817. .mode = 0 }, \
  818. .show = NULL, \
  819. .store = NULL, \
  820. }
  821. #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
  822. do { \
  823. dev_attr_##_name.attr.mode = _mode; \
  824. dev_attr_##_name.show = _show; \
  825. dev_attr_##_name.store = _store; \
  826. } while(0)
  827. static ASUS_CREATE_DEVICE_ATTR(infos);
  828. static ASUS_CREATE_DEVICE_ATTR(wlan);
  829. static ASUS_CREATE_DEVICE_ATTR(bluetooth);
  830. static ASUS_CREATE_DEVICE_ATTR(display);
  831. static ASUS_CREATE_DEVICE_ATTR(ledd);
  832. static ASUS_CREATE_DEVICE_ATTR(ls_switch);
  833. static ASUS_CREATE_DEVICE_ATTR(ls_level);
  834. static ASUS_CREATE_DEVICE_ATTR(gps);
  835. static struct attribute *asuspf_attributes[] = {
  836. &dev_attr_infos.attr,
  837. &dev_attr_wlan.attr,
  838. &dev_attr_bluetooth.attr,
  839. &dev_attr_display.attr,
  840. &dev_attr_ledd.attr,
  841. &dev_attr_ls_switch.attr,
  842. &dev_attr_ls_level.attr,
  843. &dev_attr_gps.attr,
  844. NULL
  845. };
  846. static struct attribute_group asuspf_attribute_group = {
  847. .attrs = asuspf_attributes
  848. };
  849. static struct platform_driver asuspf_driver = {
  850. .driver = {
  851. .name = ASUS_HOTK_FILE,
  852. .owner = THIS_MODULE,
  853. }
  854. };
  855. static struct platform_device *asuspf_device;
  856. static void asus_hotk_add_fs(void)
  857. {
  858. ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
  859. if (wl_switch_handle)
  860. ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
  861. if (bt_switch_handle)
  862. ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
  863. show_bluetooth, store_bluetooth);
  864. if (display_set_handle && display_get_handle)
  865. ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
  866. else if (display_set_handle)
  867. ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
  868. if (ledd_set_handle)
  869. ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
  870. if (ls_switch_handle && ls_level_handle) {
  871. ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
  872. ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
  873. }
  874. if (gps_status_handle && gps_on_handle && gps_off_handle)
  875. ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
  876. }
  877. static int asus_handle_init(char *name, acpi_handle * handle,
  878. char **paths, int num_paths)
  879. {
  880. int i;
  881. acpi_status status;
  882. for (i = 0; i < num_paths; i++) {
  883. status = acpi_get_handle(NULL, paths[i], handle);
  884. if (ACPI_SUCCESS(status))
  885. return 0;
  886. }
  887. *handle = NULL;
  888. return -ENODEV;
  889. }
  890. #define ASUS_HANDLE_INIT(object) \
  891. asus_handle_init(#object, &object##_handle, object##_paths, \
  892. ARRAY_SIZE(object##_paths))
  893. /*
  894. * This function is used to initialize the hotk with right values. In this
  895. * method, we can make all the detection we want, and modify the hotk struct
  896. */
  897. static int asus_hotk_get_info(void)
  898. {
  899. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  900. union acpi_object *model = NULL;
  901. unsigned long long bsts_result, hwrs_result;
  902. char *string = NULL;
  903. acpi_status status;
  904. /*
  905. * Get DSDT headers early enough to allow for differentiating between
  906. * models, but late enough to allow acpi_bus_register_driver() to fail
  907. * before doing anything ACPI-specific. Should we encounter a machine,
  908. * which needs special handling (i.e. its hotkey device has a different
  909. * HID), this bit will be moved. A global variable asus_info contains
  910. * the DSDT header.
  911. */
  912. status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
  913. if (ACPI_FAILURE(status))
  914. pr_warning("Couldn't get the DSDT table header\n");
  915. /* We have to write 0 on init this far for all ASUS models */
  916. if (write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
  917. pr_err("Hotkey initialization failed\n");
  918. return -ENODEV;
  919. }
  920. /* This needs to be called for some laptops to init properly */
  921. status =
  922. acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
  923. if (ACPI_FAILURE(status))
  924. pr_warning("Error calling BSTS\n");
  925. else if (bsts_result)
  926. pr_notice("BSTS called, 0x%02x returned\n",
  927. (uint) bsts_result);
  928. /* This too ... */
  929. write_acpi_int(hotk->handle, "CWAP", wapf, NULL);
  930. /*
  931. * Try to match the object returned by INIT to the specific model.
  932. * Handle every possible object (or the lack of thereof) the DSDT
  933. * writers might throw at us. When in trouble, we pass NULL to
  934. * asus_model_match() and try something completely different.
  935. */
  936. if (buffer.pointer) {
  937. model = buffer.pointer;
  938. switch (model->type) {
  939. case ACPI_TYPE_STRING:
  940. string = model->string.pointer;
  941. break;
  942. case ACPI_TYPE_BUFFER:
  943. string = model->buffer.pointer;
  944. break;
  945. default:
  946. string = "";
  947. break;
  948. }
  949. }
  950. hotk->name = kstrdup(string, GFP_KERNEL);
  951. if (!hotk->name)
  952. return -ENOMEM;
  953. if (*string)
  954. pr_notice(" %s model detected\n", string);
  955. ASUS_HANDLE_INIT(mled_set);
  956. ASUS_HANDLE_INIT(tled_set);
  957. ASUS_HANDLE_INIT(rled_set);
  958. ASUS_HANDLE_INIT(pled_set);
  959. ASUS_HANDLE_INIT(gled_set);
  960. ASUS_HANDLE_INIT(ledd_set);
  961. ASUS_HANDLE_INIT(kled_set);
  962. ASUS_HANDLE_INIT(kled_get);
  963. /*
  964. * The HWRS method return informations about the hardware.
  965. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  966. * The significance of others is yet to be found.
  967. * If we don't find the method, we assume the device are present.
  968. */
  969. status =
  970. acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
  971. if (ACPI_FAILURE(status))
  972. hwrs_result = WL_HWRS | BT_HWRS;
  973. if (hwrs_result & WL_HWRS)
  974. ASUS_HANDLE_INIT(wl_switch);
  975. if (hwrs_result & BT_HWRS)
  976. ASUS_HANDLE_INIT(bt_switch);
  977. ASUS_HANDLE_INIT(wireless_status);
  978. ASUS_HANDLE_INIT(brightness_set);
  979. ASUS_HANDLE_INIT(brightness_get);
  980. ASUS_HANDLE_INIT(lcd_switch);
  981. ASUS_HANDLE_INIT(display_set);
  982. ASUS_HANDLE_INIT(display_get);
  983. /* There is a lot of models with "ALSL", but a few get
  984. a real light sens, so we need to check it. */
  985. if (!ASUS_HANDLE_INIT(ls_switch))
  986. ASUS_HANDLE_INIT(ls_level);
  987. ASUS_HANDLE_INIT(gps_on);
  988. ASUS_HANDLE_INIT(gps_off);
  989. ASUS_HANDLE_INIT(gps_status);
  990. kfree(model);
  991. return AE_OK;
  992. }
  993. static int asus_input_init(void)
  994. {
  995. const struct key_entry *key;
  996. int result;
  997. hotk->inputdev = input_allocate_device();
  998. if (!hotk->inputdev) {
  999. pr_info("Unable to allocate input device\n");
  1000. return 0;
  1001. }
  1002. hotk->inputdev->name = "Asus Laptop extra buttons";
  1003. hotk->inputdev->phys = ASUS_HOTK_FILE "/input0";
  1004. hotk->inputdev->id.bustype = BUS_HOST;
  1005. hotk->inputdev->getkeycode = asus_getkeycode;
  1006. hotk->inputdev->setkeycode = asus_setkeycode;
  1007. for (key = asus_keymap; key->type != KE_END; key++) {
  1008. switch (key->type) {
  1009. case KE_KEY:
  1010. set_bit(EV_KEY, hotk->inputdev->evbit);
  1011. set_bit(key->keycode, hotk->inputdev->keybit);
  1012. break;
  1013. }
  1014. }
  1015. result = input_register_device(hotk->inputdev);
  1016. if (result) {
  1017. pr_info("Unable to register input device\n");
  1018. input_free_device(hotk->inputdev);
  1019. }
  1020. return result;
  1021. }
  1022. static int asus_hotk_check(void)
  1023. {
  1024. int result = 0;
  1025. result = acpi_bus_get_status(hotk->device);
  1026. if (result)
  1027. return result;
  1028. if (hotk->device->status.present) {
  1029. result = asus_hotk_get_info();
  1030. } else {
  1031. pr_err("Hotkey device not present, aborting\n");
  1032. return -EINVAL;
  1033. }
  1034. return result;
  1035. }
  1036. static int asus_hotk_found;
  1037. static int asus_hotk_add(struct acpi_device *device)
  1038. {
  1039. int result;
  1040. if (!device)
  1041. return -EINVAL;
  1042. pr_notice("Asus Laptop Support version %s\n",
  1043. ASUS_LAPTOP_VERSION);
  1044. hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
  1045. if (!hotk)
  1046. return -ENOMEM;
  1047. hotk->handle = device->handle;
  1048. strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
  1049. strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
  1050. device->driver_data = hotk;
  1051. hotk->device = device;
  1052. result = asus_hotk_check();
  1053. if (result)
  1054. goto end;
  1055. asus_hotk_add_fs();
  1056. asus_hotk_found = 1;
  1057. /* WLED and BLED are on by default */
  1058. write_status(bt_switch_handle, 1, BT_ON);
  1059. write_status(wl_switch_handle, 1, WL_ON);
  1060. /* If the h/w switch is off, we need to check the real status */
  1061. write_status(NULL, read_status(BT_ON), BT_ON);
  1062. write_status(NULL, read_status(WL_ON), WL_ON);
  1063. /* LCD Backlight is on by default */
  1064. write_status(NULL, 1, LCD_ON);
  1065. /* Keyboard Backlight is on by default */
  1066. if (kled_set_handle)
  1067. set_kled_lvl(1);
  1068. /* LED display is off by default */
  1069. hotk->ledd_status = 0xFFF;
  1070. /* Set initial values of light sensor and level */
  1071. hotk->light_switch = 1; /* Default to light sensor disabled */
  1072. hotk->light_level = 0; /* level 5 for sensor sensitivity */
  1073. if (ls_switch_handle)
  1074. set_light_sens_switch(hotk->light_switch);
  1075. if (ls_level_handle)
  1076. set_light_sens_level(hotk->light_level);
  1077. /* GPS is on by default */
  1078. write_status(NULL, 1, GPS_ON);
  1079. end:
  1080. if (result) {
  1081. kfree(hotk->name);
  1082. kfree(hotk);
  1083. }
  1084. return result;
  1085. }
  1086. static int asus_hotk_remove(struct acpi_device *device, int type)
  1087. {
  1088. if (!device || !acpi_driver_data(device))
  1089. return -EINVAL;
  1090. kfree(hotk->name);
  1091. kfree(hotk);
  1092. return 0;
  1093. }
  1094. static void asus_backlight_exit(void)
  1095. {
  1096. if (asus_backlight_device)
  1097. backlight_device_unregister(asus_backlight_device);
  1098. }
  1099. #define ASUS_LED_UNREGISTER(object) \
  1100. if (object##_led.dev) \
  1101. led_classdev_unregister(&object##_led)
  1102. static void asus_led_exit(void)
  1103. {
  1104. destroy_workqueue(led_workqueue);
  1105. ASUS_LED_UNREGISTER(mled);
  1106. ASUS_LED_UNREGISTER(tled);
  1107. ASUS_LED_UNREGISTER(pled);
  1108. ASUS_LED_UNREGISTER(rled);
  1109. ASUS_LED_UNREGISTER(gled);
  1110. ASUS_LED_UNREGISTER(kled);
  1111. }
  1112. static void asus_input_exit(void)
  1113. {
  1114. if (hotk->inputdev)
  1115. input_unregister_device(hotk->inputdev);
  1116. }
  1117. static void __exit asus_laptop_exit(void)
  1118. {
  1119. asus_backlight_exit();
  1120. asus_led_exit();
  1121. asus_input_exit();
  1122. acpi_bus_unregister_driver(&asus_hotk_driver);
  1123. sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
  1124. platform_device_unregister(asuspf_device);
  1125. platform_driver_unregister(&asuspf_driver);
  1126. }
  1127. static int asus_backlight_init(struct device *dev)
  1128. {
  1129. struct backlight_device *bd;
  1130. if (brightness_set_handle && lcd_switch_handle) {
  1131. bd = backlight_device_register(ASUS_HOTK_FILE, dev,
  1132. NULL, &asusbl_ops);
  1133. if (IS_ERR(bd)) {
  1134. pr_err("Could not register asus backlight device\n");
  1135. asus_backlight_device = NULL;
  1136. return PTR_ERR(bd);
  1137. }
  1138. asus_backlight_device = bd;
  1139. bd->props.max_brightness = 15;
  1140. bd->props.brightness = read_brightness(NULL);
  1141. bd->props.power = FB_BLANK_UNBLANK;
  1142. backlight_update_status(bd);
  1143. }
  1144. return 0;
  1145. }
  1146. static int asus_led_register(acpi_handle handle,
  1147. struct led_classdev *ldev, struct device *dev)
  1148. {
  1149. if (!handle)
  1150. return 0;
  1151. return led_classdev_register(dev, ldev);
  1152. }
  1153. #define ASUS_LED_REGISTER(object, device) \
  1154. asus_led_register(object##_set_handle, &object##_led, device)
  1155. static int asus_led_init(struct device *dev)
  1156. {
  1157. int rv;
  1158. rv = ASUS_LED_REGISTER(mled, dev);
  1159. if (rv)
  1160. goto out;
  1161. rv = ASUS_LED_REGISTER(tled, dev);
  1162. if (rv)
  1163. goto out1;
  1164. rv = ASUS_LED_REGISTER(rled, dev);
  1165. if (rv)
  1166. goto out2;
  1167. rv = ASUS_LED_REGISTER(pled, dev);
  1168. if (rv)
  1169. goto out3;
  1170. rv = ASUS_LED_REGISTER(gled, dev);
  1171. if (rv)
  1172. goto out4;
  1173. if (kled_set_handle && kled_get_handle)
  1174. rv = ASUS_LED_REGISTER(kled, dev);
  1175. if (rv)
  1176. goto out5;
  1177. led_workqueue = create_singlethread_workqueue("led_workqueue");
  1178. if (!led_workqueue)
  1179. goto out6;
  1180. return 0;
  1181. out6:
  1182. rv = -ENOMEM;
  1183. ASUS_LED_UNREGISTER(kled);
  1184. out5:
  1185. ASUS_LED_UNREGISTER(gled);
  1186. out4:
  1187. ASUS_LED_UNREGISTER(pled);
  1188. out3:
  1189. ASUS_LED_UNREGISTER(rled);
  1190. out2:
  1191. ASUS_LED_UNREGISTER(tled);
  1192. out1:
  1193. ASUS_LED_UNREGISTER(mled);
  1194. out:
  1195. return rv;
  1196. }
  1197. static int __init asus_laptop_init(void)
  1198. {
  1199. int result;
  1200. if (acpi_disabled)
  1201. return -ENODEV;
  1202. result = acpi_bus_register_driver(&asus_hotk_driver);
  1203. if (result < 0)
  1204. return result;
  1205. /*
  1206. * This is a bit of a kludge. We only want this module loaded
  1207. * for ASUS systems, but there's currently no way to probe the
  1208. * ACPI namespace for ASUS HIDs. So we just return failure if
  1209. * we didn't find one, which will cause the module to be
  1210. * unloaded.
  1211. */
  1212. if (!asus_hotk_found) {
  1213. acpi_bus_unregister_driver(&asus_hotk_driver);
  1214. return -ENODEV;
  1215. }
  1216. result = asus_input_init();
  1217. if (result)
  1218. goto fail_input;
  1219. /* Register platform stuff */
  1220. result = platform_driver_register(&asuspf_driver);
  1221. if (result)
  1222. goto fail_platform_driver;
  1223. asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
  1224. if (!asuspf_device) {
  1225. result = -ENOMEM;
  1226. goto fail_platform_device1;
  1227. }
  1228. result = platform_device_add(asuspf_device);
  1229. if (result)
  1230. goto fail_platform_device2;
  1231. result = sysfs_create_group(&asuspf_device->dev.kobj,
  1232. &asuspf_attribute_group);
  1233. if (result)
  1234. goto fail_sysfs;
  1235. result = asus_led_init(&asuspf_device->dev);
  1236. if (result)
  1237. goto fail_led;
  1238. if (!acpi_video_backlight_support()) {
  1239. result = asus_backlight_init(&asuspf_device->dev);
  1240. if (result)
  1241. goto fail_backlight;
  1242. } else
  1243. pr_info("Brightness ignored, must be controlled by "
  1244. "ACPI video driver\n");
  1245. return 0;
  1246. fail_backlight:
  1247. asus_led_exit();
  1248. fail_led:
  1249. sysfs_remove_group(&asuspf_device->dev.kobj,
  1250. &asuspf_attribute_group);
  1251. fail_sysfs:
  1252. platform_device_del(asuspf_device);
  1253. fail_platform_device2:
  1254. platform_device_put(asuspf_device);
  1255. fail_platform_device1:
  1256. platform_driver_unregister(&asuspf_driver);
  1257. fail_platform_driver:
  1258. asus_input_exit();
  1259. fail_input:
  1260. return result;
  1261. }
  1262. module_init(asus_laptop_init);
  1263. module_exit(asus_laptop_exit);