asus-laptop.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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. ASUS_LED(mled, "mail", 1);
  329. ASUS_LED(tled, "touchpad", 1);
  330. ASUS_LED(rled, "record", 1);
  331. ASUS_LED(pled, "phone", 1);
  332. ASUS_LED(gled, "gaming", 1);
  333. ASUS_LED(kled, "kbd_backlight", 3);
  334. /* /sys/class/led handlers */
  335. #define ASUS_LED_HANDLER(object, method) \
  336. static void object##_led_set(struct led_classdev *led_cdev, \
  337. enum led_brightness value) \
  338. { \
  339. struct asus_laptop *asus = \
  340. led_to_asus(led_cdev, object); \
  341. \
  342. asus->leds.object##_wk = (value > 0) ? 1 : 0; \
  343. queue_work(asus->leds.workqueue, \
  344. &asus->leds.object##_work); \
  345. } \
  346. static void object##_led_update(struct work_struct *work) \
  347. { \
  348. struct asus_laptop *asus = work_to_asus(work, object); \
  349. \
  350. int value = asus->leds.object##_wk; \
  351. asus_led_set(asus, method, value); \
  352. } \
  353. static enum led_brightness object##_led_get( \
  354. struct led_classdev *led_cdev) \
  355. { \
  356. return led_cdev->brightness; \
  357. }
  358. ASUS_LED_HANDLER(mled, METHOD_MLED);
  359. ASUS_LED_HANDLER(pled, METHOD_PLED);
  360. ASUS_LED_HANDLER(rled, METHOD_RLED);
  361. ASUS_LED_HANDLER(tled, METHOD_TLED);
  362. ASUS_LED_HANDLER(gled, METHOD_GLED);
  363. /*
  364. * Keyboard backlight (also a LED)
  365. */
  366. static int asus_kled_lvl(struct asus_laptop *asus)
  367. {
  368. unsigned long long kblv;
  369. struct acpi_object_list params;
  370. union acpi_object in_obj;
  371. acpi_status rv;
  372. params.count = 1;
  373. params.pointer = &in_obj;
  374. in_obj.type = ACPI_TYPE_INTEGER;
  375. in_obj.integer.value = 2;
  376. rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
  377. &params, &kblv);
  378. if (ACPI_FAILURE(rv)) {
  379. pr_warning("Error reading kled level\n");
  380. return -ENODEV;
  381. }
  382. return kblv;
  383. }
  384. static int asus_kled_set(struct asus_laptop *asus, int kblv)
  385. {
  386. if (kblv > 0)
  387. kblv = (1 << 7) | (kblv & 0x7F);
  388. else
  389. kblv = 0;
  390. if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
  391. pr_warning("Keyboard LED display write failed\n");
  392. return -EINVAL;
  393. }
  394. return 0;
  395. }
  396. static void kled_led_set(struct led_classdev *led_cdev,
  397. enum led_brightness value)
  398. {
  399. struct asus_laptop *asus = led_to_asus(led_cdev, kled);
  400. asus->leds.kled_wk = value;
  401. queue_work(asus->leds.workqueue, &asus->leds.kled_work);
  402. }
  403. static void kled_led_update(struct work_struct *work)
  404. {
  405. struct asus_laptop *asus = work_to_asus(work, kled);
  406. asus_kled_set(asus, asus->leds.kled_wk);
  407. }
  408. static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
  409. {
  410. struct asus_laptop *asus = led_to_asus(led_cdev, kled);
  411. return asus_kled_lvl(asus);
  412. }
  413. static void asus_led_exit(struct asus_laptop *asus)
  414. {
  415. if (asus->leds.mled.dev)
  416. led_classdev_unregister(&asus->leds.mled);
  417. if (asus->leds.tled.dev)
  418. led_classdev_unregister(&asus->leds.tled);
  419. if (asus->leds.pled.dev)
  420. led_classdev_unregister(&asus->leds.pled);
  421. if (asus->leds.rled.dev)
  422. led_classdev_unregister(&asus->leds.rled);
  423. if (asus->leds.gled.dev)
  424. led_classdev_unregister(&asus->leds.gled);
  425. if (asus->leds.kled.dev)
  426. led_classdev_unregister(&asus->leds.kled);
  427. if (asus->leds.workqueue) {
  428. destroy_workqueue(asus->leds.workqueue);
  429. asus->leds.workqueue = NULL;
  430. }
  431. }
  432. /* Ugly macro, need to fix that later */
  433. #define ASUS_LED_REGISTER(asus, object, _name, max, method) \
  434. do { \
  435. struct led_classdev *ldev = &asus->leds.object; \
  436. \
  437. if (method && acpi_check_handle(asus->handle, method, NULL)) \
  438. break ; \
  439. \
  440. INIT_WORK(&asus->leds.object##_work, object##_led_update); \
  441. ldev->name = "asus::" _name; \
  442. ldev->brightness_set = object##_led_set; \
  443. ldev->brightness_get = object##_led_get; \
  444. ldev->max_brightness = max; \
  445. rv = led_classdev_register(&asus->platform_device->dev, ldev); \
  446. if (rv) \
  447. goto error; \
  448. } while (0)
  449. static int asus_led_init(struct asus_laptop *asus)
  450. {
  451. int rv;
  452. /*
  453. * Functions that actually update the LED's are called from a
  454. * workqueue. By doing this as separate work rather than when the LED
  455. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  456. * potentially bad time, such as a timer interrupt.
  457. */
  458. asus->leds.workqueue = create_singlethread_workqueue("led_workqueue");
  459. if (!asus->leds.workqueue)
  460. return -ENOMEM;
  461. ASUS_LED_REGISTER(asus, mled, "mail", 1, METHOD_MLED);
  462. ASUS_LED_REGISTER(asus, tled, "touchpad", 1, METHOD_TLED);
  463. ASUS_LED_REGISTER(asus, rled, "record", 1, METHOD_RLED);
  464. ASUS_LED_REGISTER(asus, pled, "phone", 1, METHOD_PLED);
  465. ASUS_LED_REGISTER(asus, gled, "gaming", 1, METHOD_GLED);
  466. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
  467. !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL))
  468. ASUS_LED_REGISTER(asus, kled, "kbd_backlight", 3, NULL);
  469. error:
  470. if (rv)
  471. asus_led_exit(asus);
  472. return rv;
  473. }
  474. /*
  475. * Backlight device
  476. */
  477. static int asus_lcd_status(struct asus_laptop *asus)
  478. {
  479. return asus->lcd_state;
  480. }
  481. static int asus_lcd_set(struct asus_laptop *asus, int value)
  482. {
  483. int lcd = 0;
  484. acpi_status status = 0;
  485. lcd = !!value;
  486. if (lcd == asus_lcd_status(asus))
  487. return 0;
  488. if (!lcd_switch_handle)
  489. return -ENODEV;
  490. status = acpi_evaluate_object(lcd_switch_handle,
  491. NULL, NULL, NULL);
  492. if (ACPI_FAILURE(status)) {
  493. pr_warning("Error switching LCD\n");
  494. return -ENODEV;
  495. }
  496. asus->lcd_state = lcd;
  497. return 0;
  498. }
  499. static void lcd_blank(struct asus_laptop *asus, int blank)
  500. {
  501. struct backlight_device *bd = asus->backlight_device;
  502. asus->lcd_state = (blank == FB_BLANK_UNBLANK);
  503. if (bd) {
  504. bd->props.power = blank;
  505. backlight_update_status(bd);
  506. }
  507. }
  508. static int asus_read_brightness(struct backlight_device *bd)
  509. {
  510. struct asus_laptop *asus = bl_get_data(bd);
  511. unsigned long long value;
  512. acpi_status rv = AE_OK;
  513. rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
  514. NULL, &value);
  515. if (ACPI_FAILURE(rv))
  516. pr_warning("Error reading brightness\n");
  517. return value;
  518. }
  519. static int asus_set_brightness(struct backlight_device *bd, int value)
  520. {
  521. struct asus_laptop *asus = bl_get_data(bd);
  522. if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
  523. pr_warning("Error changing brightness\n");
  524. return -EIO;
  525. }
  526. return 0;
  527. }
  528. static int update_bl_status(struct backlight_device *bd)
  529. {
  530. struct asus_laptop *asus = bl_get_data(bd);
  531. int rv;
  532. int value = bd->props.brightness;
  533. rv = asus_set_brightness(bd, value);
  534. if (rv)
  535. return rv;
  536. value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
  537. return asus_lcd_set(asus, value);
  538. }
  539. static struct backlight_ops asusbl_ops = {
  540. .get_brightness = asus_read_brightness,
  541. .update_status = update_bl_status,
  542. };
  543. static int asus_backlight_notify(struct asus_laptop *asus)
  544. {
  545. struct backlight_device *bd = asus->backlight_device;
  546. int old = bd->props.brightness;
  547. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  548. return old;
  549. }
  550. static int asus_backlight_init(struct asus_laptop *asus)
  551. {
  552. struct backlight_device *bd;
  553. struct device *dev = &asus->platform_device->dev;
  554. if (!acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) &&
  555. !acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) &&
  556. lcd_switch_handle) {
  557. bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
  558. asus, &asusbl_ops);
  559. if (IS_ERR(bd)) {
  560. pr_err("Could not register asus backlight device\n");
  561. asus->backlight_device = NULL;
  562. return PTR_ERR(bd);
  563. }
  564. asus->backlight_device = bd;
  565. bd->props.max_brightness = 15;
  566. bd->props.power = FB_BLANK_UNBLANK;
  567. bd->props.brightness = asus_read_brightness(bd);
  568. backlight_update_status(bd);
  569. }
  570. return 0;
  571. }
  572. static void asus_backlight_exit(struct asus_laptop *asus)
  573. {
  574. if (asus->backlight_device)
  575. backlight_device_unregister(asus->backlight_device);
  576. asus->backlight_device = NULL;
  577. }
  578. /*
  579. * Platform device handlers
  580. */
  581. /*
  582. * We write our info in page, we begin at offset off and cannot write more
  583. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  584. * number of bytes written in page
  585. */
  586. static ssize_t show_infos(struct device *dev,
  587. struct device_attribute *attr, char *page)
  588. {
  589. struct asus_laptop *asus = dev_get_drvdata(dev);
  590. int len = 0;
  591. unsigned long long temp;
  592. char buf[16]; /* enough for all info */
  593. acpi_status rv = AE_OK;
  594. /*
  595. * We use the easy way, we don't care of off and count, so we don't set eof
  596. * to 1
  597. */
  598. len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
  599. len += sprintf(page + len, "Model reference : %s\n", asus->name);
  600. /*
  601. * The SFUN method probably allows the original driver to get the list
  602. * of features supported by a given model. For now, 0x0100 or 0x0800
  603. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  604. * The significance of others is yet to be found.
  605. */
  606. rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
  607. if (!ACPI_FAILURE(rv))
  608. len += sprintf(page + len, "SFUN value : %#x\n",
  609. (uint) temp);
  610. /*
  611. * The HWRS method return informations about the hardware.
  612. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  613. * The significance of others is yet to be found.
  614. * If we don't find the method, we assume the device are present.
  615. */
  616. rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
  617. if (!ACPI_FAILURE(rv))
  618. len += sprintf(page + len, "HRWS value : %#x\n",
  619. (uint) temp);
  620. /*
  621. * Another value for userspace: the ASYM method returns 0x02 for
  622. * battery low and 0x04 for battery critical, its readings tend to be
  623. * more accurate than those provided by _BST.
  624. * Note: since not all the laptops provide this method, errors are
  625. * silently ignored.
  626. */
  627. rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
  628. if (!ACPI_FAILURE(rv))
  629. len += sprintf(page + len, "ASYM value : %#x\n",
  630. (uint) temp);
  631. if (asus->dsdt_info) {
  632. snprintf(buf, 16, "%d", asus->dsdt_info->length);
  633. len += sprintf(page + len, "DSDT length : %s\n", buf);
  634. snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
  635. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  636. snprintf(buf, 16, "%d", asus->dsdt_info->revision);
  637. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  638. snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
  639. len += sprintf(page + len, "OEM id : %s\n", buf);
  640. snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
  641. len += sprintf(page + len, "OEM table id : %s\n", buf);
  642. snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
  643. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  644. snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
  645. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  646. snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
  647. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  648. }
  649. return len;
  650. }
  651. static int parse_arg(const char *buf, unsigned long count, int *val)
  652. {
  653. if (!count)
  654. return 0;
  655. if (count > 31)
  656. return -EINVAL;
  657. if (sscanf(buf, "%i", val) != 1)
  658. return -EINVAL;
  659. return count;
  660. }
  661. static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
  662. const char *buf, size_t count,
  663. const char *method)
  664. {
  665. int rv, value;
  666. int out = 0;
  667. rv = parse_arg(buf, count, &value);
  668. if (rv > 0)
  669. out = value ? 1 : 0;
  670. if (write_acpi_int(asus->handle, method, value))
  671. return -ENODEV;
  672. return rv;
  673. }
  674. /*
  675. * LEDD display
  676. */
  677. static ssize_t show_ledd(struct device *dev,
  678. struct device_attribute *attr, char *buf)
  679. {
  680. struct asus_laptop *asus = dev_get_drvdata(dev);
  681. return sprintf(buf, "0x%08x\n", asus->ledd_status);
  682. }
  683. static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
  684. const char *buf, size_t count)
  685. {
  686. struct asus_laptop *asus = dev_get_drvdata(dev);
  687. int rv, value;
  688. rv = parse_arg(buf, count, &value);
  689. if (rv > 0) {
  690. if (write_acpi_int(asus->handle, METHOD_LEDD, value))
  691. pr_warning("LED display write failed\n");
  692. else
  693. asus->ledd_status = (u32) value;
  694. }
  695. return rv;
  696. }
  697. /*
  698. * Wireless
  699. */
  700. static int asus_wireless_status(struct asus_laptop *asus, int mask)
  701. {
  702. unsigned long long status;
  703. acpi_status rv = AE_OK;
  704. if (!asus->have_rsts)
  705. return (asus->wireless_status & mask) ? 1 : 0;
  706. rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
  707. NULL, &status);
  708. if (ACPI_FAILURE(rv)) {
  709. pr_warning("Error reading Wireless status\n");
  710. return -EINVAL;
  711. }
  712. return !!(status & mask);
  713. }
  714. /*
  715. * WLAN
  716. */
  717. static int asus_wlan_set(struct asus_laptop *asus, int status)
  718. {
  719. if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
  720. pr_warning("Error setting wlan status to %d", status);
  721. return -EIO;
  722. }
  723. return 0;
  724. }
  725. static ssize_t show_wlan(struct device *dev,
  726. struct device_attribute *attr, char *buf)
  727. {
  728. struct asus_laptop *asus = dev_get_drvdata(dev);
  729. return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
  730. }
  731. static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
  732. const char *buf, size_t count)
  733. {
  734. struct asus_laptop *asus = dev_get_drvdata(dev);
  735. return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
  736. }
  737. /*
  738. * Bluetooth
  739. */
  740. static int asus_bluetooth_set(struct asus_laptop *asus, int status)
  741. {
  742. if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
  743. pr_warning("Error setting bluetooth status to %d", status);
  744. return -EIO;
  745. }
  746. return 0;
  747. }
  748. static ssize_t show_bluetooth(struct device *dev,
  749. struct device_attribute *attr, char *buf)
  750. {
  751. struct asus_laptop *asus = dev_get_drvdata(dev);
  752. return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
  753. }
  754. static ssize_t store_bluetooth(struct device *dev,
  755. struct device_attribute *attr, const char *buf,
  756. size_t count)
  757. {
  758. struct asus_laptop *asus = dev_get_drvdata(dev);
  759. return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
  760. }
  761. /*
  762. * Display
  763. */
  764. static void asus_set_display(struct asus_laptop *asus, int value)
  765. {
  766. /* no sanity check needed for now */
  767. if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
  768. pr_warning("Error setting display\n");
  769. return;
  770. }
  771. static int read_display(struct asus_laptop *asus)
  772. {
  773. unsigned long long value = 0;
  774. acpi_status rv = AE_OK;
  775. /*
  776. * In most of the case, we know how to set the display, but sometime
  777. * we can't read it
  778. */
  779. if (display_get_handle) {
  780. rv = acpi_evaluate_integer(display_get_handle, NULL,
  781. NULL, &value);
  782. if (ACPI_FAILURE(rv))
  783. pr_warning("Error reading display status\n");
  784. }
  785. value &= 0x0F; /* needed for some models, shouldn't hurt others */
  786. return value;
  787. }
  788. /*
  789. * Now, *this* one could be more user-friendly, but so far, no-one has
  790. * complained. The significance of bits is the same as in store_disp()
  791. */
  792. static ssize_t show_disp(struct device *dev,
  793. struct device_attribute *attr, char *buf)
  794. {
  795. struct asus_laptop *asus = dev_get_drvdata(dev);
  796. return sprintf(buf, "%d\n", read_display(asus));
  797. }
  798. /*
  799. * Experimental support for display switching. As of now: 1 should activate
  800. * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
  801. * Any combination (bitwise) of these will suffice. I never actually tested 4
  802. * displays hooked up simultaneously, so be warned. See the acpi4asus README
  803. * for more info.
  804. */
  805. static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
  806. const char *buf, size_t count)
  807. {
  808. struct asus_laptop *asus = dev_get_drvdata(dev);
  809. int rv, value;
  810. rv = parse_arg(buf, count, &value);
  811. if (rv > 0)
  812. asus_set_display(asus, value);
  813. return rv;
  814. }
  815. /*
  816. * Light Sens
  817. */
  818. static void asus_als_switch(struct asus_laptop *asus, int value)
  819. {
  820. if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
  821. pr_warning("Error setting light sensor switch\n");
  822. asus->light_switch = value;
  823. }
  824. static ssize_t show_lssw(struct device *dev,
  825. struct device_attribute *attr, char *buf)
  826. {
  827. struct asus_laptop *asus = dev_get_drvdata(dev);
  828. return sprintf(buf, "%d\n", asus->light_switch);
  829. }
  830. static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
  831. const char *buf, size_t count)
  832. {
  833. struct asus_laptop *asus = dev_get_drvdata(dev);
  834. int rv, value;
  835. rv = parse_arg(buf, count, &value);
  836. if (rv > 0)
  837. asus_als_switch(asus, value ? 1 : 0);
  838. return rv;
  839. }
  840. static void asus_als_level(struct asus_laptop *asus, int value)
  841. {
  842. if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
  843. pr_warning("Error setting light sensor level\n");
  844. asus->light_level = value;
  845. }
  846. static ssize_t show_lslvl(struct device *dev,
  847. struct device_attribute *attr, char *buf)
  848. {
  849. struct asus_laptop *asus = dev_get_drvdata(dev);
  850. return sprintf(buf, "%d\n", asus->light_level);
  851. }
  852. static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
  853. const char *buf, size_t count)
  854. {
  855. struct asus_laptop *asus = dev_get_drvdata(dev);
  856. int rv, value;
  857. rv = parse_arg(buf, count, &value);
  858. if (rv > 0) {
  859. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  860. /* 0 <= value <= 15 */
  861. asus_als_level(asus, value);
  862. }
  863. return rv;
  864. }
  865. /*
  866. * GPS
  867. * TODO: use rfkill
  868. */
  869. static int asus_gps_status(struct asus_laptop *asus)
  870. {
  871. unsigned long long status;
  872. acpi_status rv = AE_OK;
  873. rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
  874. NULL, &status);
  875. if (ACPI_FAILURE(rv)) {
  876. pr_warning("Error reading GPS status\n");
  877. return -ENODEV;
  878. }
  879. return !!status;
  880. }
  881. static int asus_gps_switch(struct asus_laptop *asus, int status)
  882. {
  883. const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
  884. if (write_acpi_int(asus->handle, meth, 0x02))
  885. return -ENODEV;
  886. return 0;
  887. }
  888. static ssize_t show_gps(struct device *dev,
  889. struct device_attribute *attr, char *buf)
  890. {
  891. struct asus_laptop *asus = dev_get_drvdata(dev);
  892. return sprintf(buf, "%d\n", asus_gps_status(asus));
  893. }
  894. static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
  895. const char *buf, size_t count)
  896. {
  897. struct asus_laptop *asus = dev_get_drvdata(dev);
  898. int rv, value;
  899. int ret;
  900. rv = parse_arg(buf, count, &value);
  901. if (rv <= 0)
  902. return -EINVAL;
  903. ret = asus_gps_switch(asus, !!value);
  904. if (ret)
  905. return ret;
  906. return rv;
  907. }
  908. /*
  909. * Input device (i.e. hotkeys)
  910. */
  911. static void asus_input_notify(struct asus_laptop *asus, int event)
  912. {
  913. if (asus->inputdev)
  914. sparse_keymap_report_event(asus->inputdev, event, 1, true);
  915. }
  916. static int asus_input_init(struct asus_laptop *asus)
  917. {
  918. struct input_dev *input;
  919. int error;
  920. input = input_allocate_device();
  921. if (!input) {
  922. pr_info("Unable to allocate input device\n");
  923. return 0;
  924. }
  925. input->name = "Asus Laptop extra buttons";
  926. input->phys = ASUS_LAPTOP_FILE "/input0";
  927. input->id.bustype = BUS_HOST;
  928. input->dev.parent = &asus->platform_device->dev;
  929. input_set_drvdata(input, asus);
  930. error = sparse_keymap_setup(input, asus_keymap, NULL);
  931. if (error) {
  932. pr_err("Unable to setup input device keymap\n");
  933. goto err_keymap;
  934. }
  935. error = input_register_device(input);
  936. if (error) {
  937. pr_info("Unable to register input device\n");
  938. goto err_device;
  939. }
  940. asus->inputdev = input;
  941. return 0;
  942. err_keymap:
  943. sparse_keymap_free(input);
  944. err_device:
  945. input_free_device(input);
  946. return error;
  947. }
  948. static void asus_input_exit(struct asus_laptop *asus)
  949. {
  950. if (asus->inputdev) {
  951. sparse_keymap_free(asus->inputdev);
  952. input_unregister_device(asus->inputdev);
  953. }
  954. }
  955. /*
  956. * ACPI driver
  957. */
  958. static void asus_acpi_notify(struct acpi_device *device, u32 event)
  959. {
  960. struct asus_laptop *asus = acpi_driver_data(device);
  961. u16 count;
  962. /*
  963. * We need to tell the backlight device when the backlight power is
  964. * switched
  965. */
  966. if (event == ATKD_LCD_ON)
  967. lcd_blank(asus, FB_BLANK_UNBLANK);
  968. else if (event == ATKD_LCD_OFF)
  969. lcd_blank(asus, FB_BLANK_POWERDOWN);
  970. /* TODO Find a better way to handle events count. */
  971. count = asus->event_count[event % 128]++;
  972. acpi_bus_generate_proc_event(asus->device, event, count);
  973. acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
  974. dev_name(&asus->device->dev), event,
  975. count);
  976. /* Brightness events are special */
  977. if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
  978. /* Ignore them completely if the acpi video driver is used */
  979. if (asus->backlight_device != NULL) {
  980. /* Update the backlight device. */
  981. asus_backlight_notify(asus);
  982. }
  983. return ;
  984. }
  985. asus_input_notify(asus, event);
  986. }
  987. #define ASUS_CREATE_DEVICE_ATTR(_name) \
  988. struct device_attribute dev_attr_##_name = { \
  989. .attr = { \
  990. .name = __stringify(_name), \
  991. .mode = 0 }, \
  992. .show = NULL, \
  993. .store = NULL, \
  994. }
  995. #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
  996. do { \
  997. dev_attr_##_name.attr.mode = _mode; \
  998. dev_attr_##_name.show = _show; \
  999. dev_attr_##_name.store = _store; \
  1000. } while(0)
  1001. static ASUS_CREATE_DEVICE_ATTR(infos);
  1002. static ASUS_CREATE_DEVICE_ATTR(wlan);
  1003. static ASUS_CREATE_DEVICE_ATTR(bluetooth);
  1004. static ASUS_CREATE_DEVICE_ATTR(display);
  1005. static ASUS_CREATE_DEVICE_ATTR(ledd);
  1006. static ASUS_CREATE_DEVICE_ATTR(ls_switch);
  1007. static ASUS_CREATE_DEVICE_ATTR(ls_level);
  1008. static ASUS_CREATE_DEVICE_ATTR(gps);
  1009. static struct attribute *asuspf_attributes[] = {
  1010. &dev_attr_infos.attr,
  1011. &dev_attr_wlan.attr,
  1012. &dev_attr_bluetooth.attr,
  1013. &dev_attr_display.attr,
  1014. &dev_attr_ledd.attr,
  1015. &dev_attr_ls_switch.attr,
  1016. &dev_attr_ls_level.attr,
  1017. &dev_attr_gps.attr,
  1018. NULL
  1019. };
  1020. static struct attribute_group platform_attribute_group = {
  1021. .attrs = asuspf_attributes
  1022. };
  1023. static int asus_platform_init(struct asus_laptop *asus)
  1024. {
  1025. int result;
  1026. asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
  1027. if (!asus->platform_device)
  1028. return -ENOMEM;
  1029. platform_set_drvdata(asus->platform_device, asus);
  1030. result = platform_device_add(asus->platform_device);
  1031. if (result)
  1032. goto fail_platform_device;
  1033. result = sysfs_create_group(&asus->platform_device->dev.kobj,
  1034. &platform_attribute_group);
  1035. if (result)
  1036. goto fail_sysfs;
  1037. return 0;
  1038. fail_sysfs:
  1039. platform_device_del(asus->platform_device);
  1040. fail_platform_device:
  1041. platform_device_put(asus->platform_device);
  1042. return result;
  1043. }
  1044. static void asus_platform_exit(struct asus_laptop *asus)
  1045. {
  1046. sysfs_remove_group(&asus->platform_device->dev.kobj,
  1047. &platform_attribute_group);
  1048. platform_device_unregister(asus->platform_device);
  1049. }
  1050. static struct platform_driver platform_driver = {
  1051. .driver = {
  1052. .name = ASUS_LAPTOP_FILE,
  1053. .owner = THIS_MODULE,
  1054. }
  1055. };
  1056. static void asus_laptop_add_fs(struct asus_laptop *asus)
  1057. {
  1058. ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
  1059. if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL))
  1060. ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
  1061. if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL))
  1062. ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
  1063. show_bluetooth, store_bluetooth);
  1064. if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
  1065. if (display_get_handle)
  1066. ASUS_SET_DEVICE_ATTR(display, 0644, show_disp,
  1067. store_disp);
  1068. else
  1069. ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
  1070. }
  1071. if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL))
  1072. ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
  1073. if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1074. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1075. ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
  1076. ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
  1077. }
  1078. if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
  1079. !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
  1080. !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
  1081. ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
  1082. }
  1083. static int asus_handle_init(char *name, acpi_handle * handle,
  1084. char **paths, int num_paths)
  1085. {
  1086. int i;
  1087. acpi_status status;
  1088. for (i = 0; i < num_paths; i++) {
  1089. status = acpi_get_handle(NULL, paths[i], handle);
  1090. if (ACPI_SUCCESS(status))
  1091. return 0;
  1092. }
  1093. *handle = NULL;
  1094. return -ENODEV;
  1095. }
  1096. #define ASUS_HANDLE_INIT(object) \
  1097. asus_handle_init(#object, &object##_handle, object##_paths, \
  1098. ARRAY_SIZE(object##_paths))
  1099. /*
  1100. * This function is used to initialize the context with right values. In this
  1101. * method, we can make all the detection we want, and modify the asus_laptop
  1102. * struct
  1103. */
  1104. static int asus_laptop_get_info(struct asus_laptop *asus)
  1105. {
  1106. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1107. union acpi_object *model = NULL;
  1108. unsigned long long bsts_result, hwrs_result;
  1109. char *string = NULL;
  1110. acpi_status status;
  1111. /*
  1112. * Get DSDT headers early enough to allow for differentiating between
  1113. * models, but late enough to allow acpi_bus_register_driver() to fail
  1114. * before doing anything ACPI-specific. Should we encounter a machine,
  1115. * which needs special handling (i.e. its hotkey device has a different
  1116. * HID), this bit will be moved.
  1117. */
  1118. status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
  1119. if (ACPI_FAILURE(status))
  1120. pr_warning("Couldn't get the DSDT table header\n");
  1121. /* We have to write 0 on init this far for all ASUS models */
  1122. if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
  1123. pr_err("Hotkey initialization failed\n");
  1124. return -ENODEV;
  1125. }
  1126. /* This needs to be called for some laptops to init properly */
  1127. status =
  1128. acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
  1129. if (ACPI_FAILURE(status))
  1130. pr_warning("Error calling BSTS\n");
  1131. else if (bsts_result)
  1132. pr_notice("BSTS called, 0x%02x returned\n",
  1133. (uint) bsts_result);
  1134. /* This too ... */
  1135. if (write_acpi_int(asus->handle, "CWAP", wapf))
  1136. pr_err("Error calling CWAP(%d)\n", wapf);
  1137. /*
  1138. * Try to match the object returned by INIT to the specific model.
  1139. * Handle every possible object (or the lack of thereof) the DSDT
  1140. * writers might throw at us. When in trouble, we pass NULL to
  1141. * asus_model_match() and try something completely different.
  1142. */
  1143. if (buffer.pointer) {
  1144. model = buffer.pointer;
  1145. switch (model->type) {
  1146. case ACPI_TYPE_STRING:
  1147. string = model->string.pointer;
  1148. break;
  1149. case ACPI_TYPE_BUFFER:
  1150. string = model->buffer.pointer;
  1151. break;
  1152. default:
  1153. string = "";
  1154. break;
  1155. }
  1156. }
  1157. asus->name = kstrdup(string, GFP_KERNEL);
  1158. if (!asus->name)
  1159. return -ENOMEM;
  1160. if (*string)
  1161. pr_notice(" %s model detected\n", string);
  1162. /*
  1163. * The HWRS method return informations about the hardware.
  1164. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  1165. * The significance of others is yet to be found.
  1166. */
  1167. status =
  1168. acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
  1169. if (!ACPI_FAILURE(status))
  1170. pr_notice(" HRWS returned %x", (int)hwrs_result);
  1171. if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
  1172. asus->have_rsts = true;
  1173. /* Scheduled for removal */
  1174. ASUS_HANDLE_INIT(lcd_switch);
  1175. ASUS_HANDLE_INIT(display_get);
  1176. kfree(model);
  1177. return AE_OK;
  1178. }
  1179. static bool asus_device_present;
  1180. static int __devinit asus_acpi_init(struct asus_laptop *asus)
  1181. {
  1182. int result = 0;
  1183. result = acpi_bus_get_status(asus->device);
  1184. if (result)
  1185. return result;
  1186. if (!asus->device->status.present) {
  1187. pr_err("Hotkey device not present, aborting\n");
  1188. return -ENODEV;
  1189. }
  1190. result = asus_laptop_get_info(asus);
  1191. if (result)
  1192. return result;
  1193. asus_laptop_add_fs(asus);
  1194. /* WLED and BLED are on by default */
  1195. if (bluetooth_status >= 0)
  1196. asus_bluetooth_set(asus, !!bluetooth_status);
  1197. if (wlan_status >= 0)
  1198. asus_wlan_set(asus, !!wlan_status);
  1199. /* Keyboard Backlight is on by default */
  1200. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
  1201. asus_kled_set(asus, 1);
  1202. /* LED display is off by default */
  1203. asus->ledd_status = 0xFFF;
  1204. /* Set initial values of light sensor and level */
  1205. asus->light_switch = 0; /* Default to light sensor disabled */
  1206. asus->light_level = 5; /* level 5 for sensor sensitivity */
  1207. if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1208. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1209. asus_als_switch(asus, asus->light_switch);
  1210. asus_als_level(asus, asus->light_level);
  1211. }
  1212. /* GPS is on by default */
  1213. if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
  1214. !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
  1215. !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
  1216. asus_gps_switch(asus, 1);
  1217. return result;
  1218. }
  1219. static int __devinit asus_acpi_add(struct acpi_device *device)
  1220. {
  1221. struct asus_laptop *asus;
  1222. int result;
  1223. pr_notice("Asus Laptop Support version %s\n",
  1224. ASUS_LAPTOP_VERSION);
  1225. asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
  1226. if (!asus)
  1227. return -ENOMEM;
  1228. asus->handle = device->handle;
  1229. strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
  1230. strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
  1231. device->driver_data = asus;
  1232. asus->device = device;
  1233. result = asus_acpi_init(asus);
  1234. if (result)
  1235. goto fail_platform;
  1236. /*
  1237. * Register the platform device first. It is used as a parent for the
  1238. * sub-devices below.
  1239. */
  1240. result = asus_platform_init(asus);
  1241. if (result)
  1242. goto fail_platform;
  1243. if (!acpi_video_backlight_support()) {
  1244. result = asus_backlight_init(asus);
  1245. if (result)
  1246. goto fail_backlight;
  1247. } else
  1248. pr_info("Backlight controlled by ACPI video driver\n");
  1249. result = asus_input_init(asus);
  1250. if (result)
  1251. goto fail_input;
  1252. result = asus_led_init(asus);
  1253. if (result)
  1254. goto fail_led;
  1255. asus_device_present = true;
  1256. return 0;
  1257. fail_led:
  1258. asus_input_exit(asus);
  1259. fail_input:
  1260. asus_backlight_exit(asus);
  1261. fail_backlight:
  1262. asus_platform_exit(asus);
  1263. fail_platform:
  1264. kfree(asus->name);
  1265. kfree(asus);
  1266. return result;
  1267. }
  1268. static int asus_acpi_remove(struct acpi_device *device, int type)
  1269. {
  1270. struct asus_laptop *asus = acpi_driver_data(device);
  1271. asus_backlight_exit(asus);
  1272. asus_led_exit(asus);
  1273. asus_input_exit(asus);
  1274. asus_platform_exit(asus);
  1275. kfree(asus->name);
  1276. kfree(asus);
  1277. return 0;
  1278. }
  1279. static const struct acpi_device_id asus_device_ids[] = {
  1280. {"ATK0100", 0},
  1281. {"ATK0101", 0},
  1282. {"", 0},
  1283. };
  1284. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  1285. static struct acpi_driver asus_acpi_driver = {
  1286. .name = ASUS_LAPTOP_NAME,
  1287. .class = ASUS_LAPTOP_CLASS,
  1288. .owner = THIS_MODULE,
  1289. .ids = asus_device_ids,
  1290. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1291. .ops = {
  1292. .add = asus_acpi_add,
  1293. .remove = asus_acpi_remove,
  1294. .notify = asus_acpi_notify,
  1295. },
  1296. };
  1297. static int __init asus_laptop_init(void)
  1298. {
  1299. int result;
  1300. result = platform_driver_register(&platform_driver);
  1301. if (result < 0)
  1302. return result;
  1303. result = acpi_bus_register_driver(&asus_acpi_driver);
  1304. if (result < 0)
  1305. goto fail_acpi_driver;
  1306. if (!asus_device_present) {
  1307. result = -ENODEV;
  1308. goto fail_no_device;
  1309. }
  1310. return 0;
  1311. fail_no_device:
  1312. acpi_bus_unregister_driver(&asus_acpi_driver);
  1313. fail_acpi_driver:
  1314. platform_driver_unregister(&platform_driver);
  1315. return result;
  1316. }
  1317. static void __exit asus_laptop_exit(void)
  1318. {
  1319. acpi_bus_unregister_driver(&asus_acpi_driver);
  1320. platform_driver_unregister(&platform_driver);
  1321. }
  1322. module_init(asus_laptop_init);
  1323. module_exit(asus_laptop_exit);