asus-laptop.c 38 KB

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