asus-laptop.c 40 KB

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