asus-laptop.c 38 KB

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