asus-laptop.c 41 KB

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