asus-laptop.c 38 KB

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