asus-laptop.c 36 KB

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