asus-laptop.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  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, 0444);
  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, 0444);
  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, 0444);
  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 -1;
  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. return -ENODEV;
  690. }
  691. asus->ledd_status = (u32) value;
  692. }
  693. return rv;
  694. }
  695. /*
  696. * Wireless
  697. */
  698. static int asus_wireless_status(struct asus_laptop *asus, int mask)
  699. {
  700. unsigned long long status;
  701. acpi_status rv = AE_OK;
  702. if (!asus->have_rsts)
  703. return (asus->wireless_status & mask) ? 1 : 0;
  704. rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
  705. NULL, &status);
  706. if (ACPI_FAILURE(rv)) {
  707. pr_warning("Error reading Wireless status\n");
  708. return -EINVAL;
  709. }
  710. return !!(status & mask);
  711. }
  712. /*
  713. * WLAN
  714. */
  715. static int asus_wlan_set(struct asus_laptop *asus, int status)
  716. {
  717. if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
  718. pr_warning("Error setting wlan status to %d", status);
  719. return -EIO;
  720. }
  721. return 0;
  722. }
  723. static ssize_t show_wlan(struct device *dev,
  724. struct device_attribute *attr, char *buf)
  725. {
  726. struct asus_laptop *asus = dev_get_drvdata(dev);
  727. return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
  728. }
  729. static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
  730. const char *buf, size_t count)
  731. {
  732. struct asus_laptop *asus = dev_get_drvdata(dev);
  733. return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
  734. }
  735. /*
  736. * Bluetooth
  737. */
  738. static int asus_bluetooth_set(struct asus_laptop *asus, int status)
  739. {
  740. if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
  741. pr_warning("Error setting bluetooth status to %d", status);
  742. return -EIO;
  743. }
  744. return 0;
  745. }
  746. static ssize_t show_bluetooth(struct device *dev,
  747. struct device_attribute *attr, char *buf)
  748. {
  749. struct asus_laptop *asus = dev_get_drvdata(dev);
  750. return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
  751. }
  752. static ssize_t store_bluetooth(struct device *dev,
  753. struct device_attribute *attr, const char *buf,
  754. size_t count)
  755. {
  756. struct asus_laptop *asus = dev_get_drvdata(dev);
  757. return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
  758. }
  759. /*
  760. * Display
  761. */
  762. static void asus_set_display(struct asus_laptop *asus, int value)
  763. {
  764. /* no sanity check needed for now */
  765. if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
  766. pr_warning("Error setting display\n");
  767. return;
  768. }
  769. static int read_display(struct asus_laptop *asus)
  770. {
  771. unsigned long long value = 0;
  772. acpi_status rv = AE_OK;
  773. /*
  774. * In most of the case, we know how to set the display, but sometime
  775. * we can't read it
  776. */
  777. if (display_get_handle) {
  778. rv = acpi_evaluate_integer(display_get_handle, NULL,
  779. NULL, &value);
  780. if (ACPI_FAILURE(rv))
  781. pr_warning("Error reading display status\n");
  782. }
  783. value &= 0x0F; /* needed for some models, shouldn't hurt others */
  784. return value;
  785. }
  786. /*
  787. * Now, *this* one could be more user-friendly, but so far, no-one has
  788. * complained. The significance of bits is the same as in store_disp()
  789. */
  790. static ssize_t show_disp(struct device *dev,
  791. struct device_attribute *attr, char *buf)
  792. {
  793. struct asus_laptop *asus = dev_get_drvdata(dev);
  794. if (!display_get_handle)
  795. return -ENODEV;
  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. */
  868. static int asus_gps_status(struct asus_laptop *asus)
  869. {
  870. unsigned long long status;
  871. acpi_status rv = AE_OK;
  872. rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
  873. NULL, &status);
  874. if (ACPI_FAILURE(rv)) {
  875. pr_warning("Error reading GPS status\n");
  876. return -ENODEV;
  877. }
  878. return !!status;
  879. }
  880. static int asus_gps_switch(struct asus_laptop *asus, int status)
  881. {
  882. const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
  883. if (write_acpi_int(asus->handle, meth, 0x02))
  884. return -ENODEV;
  885. return 0;
  886. }
  887. static ssize_t show_gps(struct device *dev,
  888. struct device_attribute *attr, char *buf)
  889. {
  890. struct asus_laptop *asus = dev_get_drvdata(dev);
  891. return sprintf(buf, "%d\n", asus_gps_status(asus));
  892. }
  893. static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
  894. const char *buf, size_t count)
  895. {
  896. struct asus_laptop *asus = dev_get_drvdata(dev);
  897. int rv, value;
  898. int ret;
  899. rv = parse_arg(buf, count, &value);
  900. if (rv <= 0)
  901. return -EINVAL;
  902. ret = asus_gps_switch(asus, !!value);
  903. if (ret)
  904. return ret;
  905. rfkill_set_sw_state(asus->gps_rfkill, !value);
  906. return rv;
  907. }
  908. /*
  909. * rfkill
  910. */
  911. static int asus_gps_rfkill_set(void *data, bool blocked)
  912. {
  913. acpi_handle handle = data;
  914. return asus_gps_switch(handle, !blocked);
  915. }
  916. static const struct rfkill_ops asus_gps_rfkill_ops = {
  917. .set_block = asus_gps_rfkill_set,
  918. };
  919. static void asus_rfkill_exit(struct asus_laptop *asus)
  920. {
  921. if (asus->gps_rfkill) {
  922. rfkill_unregister(asus->gps_rfkill);
  923. rfkill_destroy(asus->gps_rfkill);
  924. asus->gps_rfkill = NULL;
  925. }
  926. }
  927. static int asus_rfkill_init(struct asus_laptop *asus)
  928. {
  929. int result;
  930. if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
  931. acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
  932. acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
  933. return 0;
  934. asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
  935. RFKILL_TYPE_GPS,
  936. &asus_gps_rfkill_ops, NULL);
  937. if (!asus->gps_rfkill)
  938. return -EINVAL;
  939. result = rfkill_register(asus->gps_rfkill);
  940. if (result) {
  941. rfkill_destroy(asus->gps_rfkill);
  942. asus->gps_rfkill = NULL;
  943. }
  944. return result;
  945. }
  946. /*
  947. * Input device (i.e. hotkeys)
  948. */
  949. static void asus_input_notify(struct asus_laptop *asus, int event)
  950. {
  951. if (asus->inputdev)
  952. sparse_keymap_report_event(asus->inputdev, event, 1, true);
  953. }
  954. static int asus_input_init(struct asus_laptop *asus)
  955. {
  956. struct input_dev *input;
  957. int error;
  958. input = input_allocate_device();
  959. if (!input) {
  960. pr_info("Unable to allocate input device\n");
  961. return -ENOMEM;
  962. }
  963. input->name = "Asus Laptop extra buttons";
  964. input->phys = ASUS_LAPTOP_FILE "/input0";
  965. input->id.bustype = BUS_HOST;
  966. input->dev.parent = &asus->platform_device->dev;
  967. input_set_drvdata(input, asus);
  968. error = sparse_keymap_setup(input, asus_keymap, NULL);
  969. if (error) {
  970. pr_err("Unable to setup input device keymap\n");
  971. goto err_free_dev;
  972. }
  973. error = input_register_device(input);
  974. if (error) {
  975. pr_info("Unable to register input device\n");
  976. goto err_free_keymap;
  977. }
  978. asus->inputdev = input;
  979. return 0;
  980. err_free_keymap:
  981. sparse_keymap_free(input);
  982. err_free_dev:
  983. input_free_device(input);
  984. return error;
  985. }
  986. static void asus_input_exit(struct asus_laptop *asus)
  987. {
  988. if (asus->inputdev) {
  989. sparse_keymap_free(asus->inputdev);
  990. input_unregister_device(asus->inputdev);
  991. }
  992. }
  993. /*
  994. * ACPI driver
  995. */
  996. static void asus_acpi_notify(struct acpi_device *device, u32 event)
  997. {
  998. struct asus_laptop *asus = acpi_driver_data(device);
  999. u16 count;
  1000. /*
  1001. * We need to tell the backlight device when the backlight power is
  1002. * switched
  1003. */
  1004. if (event == ATKD_LCD_ON)
  1005. lcd_blank(asus, FB_BLANK_UNBLANK);
  1006. else if (event == ATKD_LCD_OFF)
  1007. lcd_blank(asus, FB_BLANK_POWERDOWN);
  1008. /* TODO Find a better way to handle events count. */
  1009. count = asus->event_count[event % 128]++;
  1010. acpi_bus_generate_proc_event(asus->device, event, count);
  1011. acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
  1012. dev_name(&asus->device->dev), event,
  1013. count);
  1014. /* Brightness events are special */
  1015. if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
  1016. /* Ignore them completely if the acpi video driver is used */
  1017. if (asus->backlight_device != NULL) {
  1018. /* Update the backlight device. */
  1019. asus_backlight_notify(asus);
  1020. }
  1021. return ;
  1022. }
  1023. asus_input_notify(asus, event);
  1024. }
  1025. static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
  1026. static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
  1027. static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR, show_bluetooth,
  1028. store_bluetooth);
  1029. static DEVICE_ATTR(display, S_IRUGO | S_IWUSR, show_disp, store_disp);
  1030. static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
  1031. static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
  1032. static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
  1033. static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
  1034. static void asus_sysfs_exit(struct asus_laptop *asus)
  1035. {
  1036. struct platform_device *device = asus->platform_device;
  1037. device_remove_file(&device->dev, &dev_attr_infos);
  1038. device_remove_file(&device->dev, &dev_attr_wlan);
  1039. device_remove_file(&device->dev, &dev_attr_bluetooth);
  1040. device_remove_file(&device->dev, &dev_attr_display);
  1041. device_remove_file(&device->dev, &dev_attr_ledd);
  1042. device_remove_file(&device->dev, &dev_attr_ls_switch);
  1043. device_remove_file(&device->dev, &dev_attr_ls_level);
  1044. device_remove_file(&device->dev, &dev_attr_gps);
  1045. }
  1046. static int asus_sysfs_init(struct asus_laptop *asus)
  1047. {
  1048. struct platform_device *device = asus->platform_device;
  1049. int err;
  1050. err = device_create_file(&device->dev, &dev_attr_infos);
  1051. if (err)
  1052. return err;
  1053. if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL)) {
  1054. err = device_create_file(&device->dev, &dev_attr_wlan);
  1055. if (err)
  1056. return err;
  1057. }
  1058. if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL)) {
  1059. err = device_create_file(&device->dev, &dev_attr_bluetooth);
  1060. if (err)
  1061. return err;
  1062. }
  1063. if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
  1064. err = device_create_file(&device->dev, &dev_attr_display);
  1065. if (err)
  1066. return err;
  1067. }
  1068. if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL)) {
  1069. err = device_create_file(&device->dev, &dev_attr_ledd);
  1070. if (err)
  1071. return err;
  1072. }
  1073. if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1074. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1075. err = device_create_file(&device->dev, &dev_attr_ls_switch);
  1076. if (err)
  1077. return err;
  1078. err = device_create_file(&device->dev, &dev_attr_ls_level);
  1079. if (err)
  1080. return err;
  1081. }
  1082. if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
  1083. !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
  1084. !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL)) {
  1085. err = device_create_file(&device->dev, &dev_attr_gps);
  1086. if (err)
  1087. return err;
  1088. }
  1089. return err;
  1090. }
  1091. static int asus_platform_init(struct asus_laptop *asus)
  1092. {
  1093. int err;
  1094. asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
  1095. if (!asus->platform_device)
  1096. return -ENOMEM;
  1097. platform_set_drvdata(asus->platform_device, asus);
  1098. err = platform_device_add(asus->platform_device);
  1099. if (err)
  1100. goto fail_platform_device;
  1101. err = asus_sysfs_init(asus);
  1102. if (err)
  1103. goto fail_sysfs;
  1104. return 0;
  1105. fail_sysfs:
  1106. asus_sysfs_exit(asus);
  1107. platform_device_del(asus->platform_device);
  1108. fail_platform_device:
  1109. platform_device_put(asus->platform_device);
  1110. return err;
  1111. }
  1112. static void asus_platform_exit(struct asus_laptop *asus)
  1113. {
  1114. asus_sysfs_exit(asus);
  1115. platform_device_unregister(asus->platform_device);
  1116. }
  1117. static struct platform_driver platform_driver = {
  1118. .driver = {
  1119. .name = ASUS_LAPTOP_FILE,
  1120. .owner = THIS_MODULE,
  1121. }
  1122. };
  1123. static int asus_handle_init(char *name, acpi_handle * handle,
  1124. char **paths, int num_paths)
  1125. {
  1126. int i;
  1127. acpi_status status;
  1128. for (i = 0; i < num_paths; i++) {
  1129. status = acpi_get_handle(NULL, paths[i], handle);
  1130. if (ACPI_SUCCESS(status))
  1131. return 0;
  1132. }
  1133. *handle = NULL;
  1134. return -ENODEV;
  1135. }
  1136. #define ASUS_HANDLE_INIT(object) \
  1137. asus_handle_init(#object, &object##_handle, object##_paths, \
  1138. ARRAY_SIZE(object##_paths))
  1139. /*
  1140. * This function is used to initialize the context with right values. In this
  1141. * method, we can make all the detection we want, and modify the asus_laptop
  1142. * struct
  1143. */
  1144. static int asus_laptop_get_info(struct asus_laptop *asus)
  1145. {
  1146. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1147. union acpi_object *model = NULL;
  1148. unsigned long long bsts_result, hwrs_result;
  1149. char *string = NULL;
  1150. acpi_status status;
  1151. /*
  1152. * Get DSDT headers early enough to allow for differentiating between
  1153. * models, but late enough to allow acpi_bus_register_driver() to fail
  1154. * before doing anything ACPI-specific. Should we encounter a machine,
  1155. * which needs special handling (i.e. its hotkey device has a different
  1156. * HID), this bit will be moved.
  1157. */
  1158. status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
  1159. if (ACPI_FAILURE(status))
  1160. pr_warning("Couldn't get the DSDT table header\n");
  1161. /* We have to write 0 on init this far for all ASUS models */
  1162. if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
  1163. pr_err("Hotkey initialization failed\n");
  1164. return -ENODEV;
  1165. }
  1166. /* This needs to be called for some laptops to init properly */
  1167. status =
  1168. acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
  1169. if (ACPI_FAILURE(status))
  1170. pr_warning("Error calling BSTS\n");
  1171. else if (bsts_result)
  1172. pr_notice("BSTS called, 0x%02x returned\n",
  1173. (uint) bsts_result);
  1174. /* This too ... */
  1175. if (write_acpi_int(asus->handle, "CWAP", wapf))
  1176. pr_err("Error calling CWAP(%d)\n", wapf);
  1177. /*
  1178. * Try to match the object returned by INIT to the specific model.
  1179. * Handle every possible object (or the lack of thereof) the DSDT
  1180. * writers might throw at us. When in trouble, we pass NULL to
  1181. * asus_model_match() and try something completely different.
  1182. */
  1183. if (buffer.pointer) {
  1184. model = buffer.pointer;
  1185. switch (model->type) {
  1186. case ACPI_TYPE_STRING:
  1187. string = model->string.pointer;
  1188. break;
  1189. case ACPI_TYPE_BUFFER:
  1190. string = model->buffer.pointer;
  1191. break;
  1192. default:
  1193. string = "";
  1194. break;
  1195. }
  1196. }
  1197. asus->name = kstrdup(string, GFP_KERNEL);
  1198. if (!asus->name) {
  1199. kfree(buffer.pointer);
  1200. return -ENOMEM;
  1201. }
  1202. if (*string)
  1203. pr_notice(" %s model detected\n", string);
  1204. /*
  1205. * The HWRS method return informations about the hardware.
  1206. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  1207. * The significance of others is yet to be found.
  1208. */
  1209. status =
  1210. acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
  1211. if (!ACPI_FAILURE(status))
  1212. pr_notice(" HRWS returned %x", (int)hwrs_result);
  1213. if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
  1214. asus->have_rsts = true;
  1215. /* Scheduled for removal */
  1216. ASUS_HANDLE_INIT(lcd_switch);
  1217. ASUS_HANDLE_INIT(display_get);
  1218. kfree(model);
  1219. return AE_OK;
  1220. }
  1221. static bool asus_device_present;
  1222. static int __devinit asus_acpi_init(struct asus_laptop *asus)
  1223. {
  1224. int result = 0;
  1225. result = acpi_bus_get_status(asus->device);
  1226. if (result)
  1227. return result;
  1228. if (!asus->device->status.present) {
  1229. pr_err("Hotkey device not present, aborting\n");
  1230. return -ENODEV;
  1231. }
  1232. result = asus_laptop_get_info(asus);
  1233. if (result)
  1234. return result;
  1235. /* WLED and BLED are on by default */
  1236. if (bluetooth_status >= 0)
  1237. asus_bluetooth_set(asus, !!bluetooth_status);
  1238. if (wlan_status >= 0)
  1239. asus_wlan_set(asus, !!wlan_status);
  1240. /* Keyboard Backlight is on by default */
  1241. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
  1242. asus_kled_set(asus, 1);
  1243. /* LED display is off by default */
  1244. asus->ledd_status = 0xFFF;
  1245. /* Set initial values of light sensor and level */
  1246. asus->light_switch = 0; /* Default to light sensor disabled */
  1247. asus->light_level = 5; /* level 5 for sensor sensitivity */
  1248. if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1249. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1250. asus_als_switch(asus, asus->light_switch);
  1251. asus_als_level(asus, asus->light_level);
  1252. }
  1253. asus->lcd_state = 1; /* LCD should be on when the module load */
  1254. return result;
  1255. }
  1256. static int __devinit asus_acpi_add(struct acpi_device *device)
  1257. {
  1258. struct asus_laptop *asus;
  1259. int result;
  1260. pr_notice("Asus Laptop Support version %s\n",
  1261. ASUS_LAPTOP_VERSION);
  1262. asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
  1263. if (!asus)
  1264. return -ENOMEM;
  1265. asus->handle = device->handle;
  1266. strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
  1267. strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
  1268. device->driver_data = asus;
  1269. asus->device = device;
  1270. result = asus_acpi_init(asus);
  1271. if (result)
  1272. goto fail_platform;
  1273. /*
  1274. * Register the platform device first. It is used as a parent for the
  1275. * sub-devices below.
  1276. */
  1277. result = asus_platform_init(asus);
  1278. if (result)
  1279. goto fail_platform;
  1280. if (!acpi_video_backlight_support()) {
  1281. result = asus_backlight_init(asus);
  1282. if (result)
  1283. goto fail_backlight;
  1284. } else
  1285. pr_info("Backlight controlled by ACPI video driver\n");
  1286. result = asus_input_init(asus);
  1287. if (result)
  1288. goto fail_input;
  1289. result = asus_led_init(asus);
  1290. if (result)
  1291. goto fail_led;
  1292. result = asus_rfkill_init(asus);
  1293. if (result)
  1294. goto fail_rfkill;
  1295. asus_device_present = true;
  1296. return 0;
  1297. fail_rfkill:
  1298. asus_led_exit(asus);
  1299. fail_led:
  1300. asus_input_exit(asus);
  1301. fail_input:
  1302. asus_backlight_exit(asus);
  1303. fail_backlight:
  1304. asus_platform_exit(asus);
  1305. fail_platform:
  1306. kfree(asus->name);
  1307. kfree(asus);
  1308. return result;
  1309. }
  1310. static int asus_acpi_remove(struct acpi_device *device, int type)
  1311. {
  1312. struct asus_laptop *asus = acpi_driver_data(device);
  1313. asus_backlight_exit(asus);
  1314. asus_rfkill_exit(asus);
  1315. asus_led_exit(asus);
  1316. asus_input_exit(asus);
  1317. asus_platform_exit(asus);
  1318. kfree(asus->name);
  1319. kfree(asus);
  1320. return 0;
  1321. }
  1322. static const struct acpi_device_id asus_device_ids[] = {
  1323. {"ATK0100", 0},
  1324. {"ATK0101", 0},
  1325. {"", 0},
  1326. };
  1327. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  1328. static struct acpi_driver asus_acpi_driver = {
  1329. .name = ASUS_LAPTOP_NAME,
  1330. .class = ASUS_LAPTOP_CLASS,
  1331. .owner = THIS_MODULE,
  1332. .ids = asus_device_ids,
  1333. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1334. .ops = {
  1335. .add = asus_acpi_add,
  1336. .remove = asus_acpi_remove,
  1337. .notify = asus_acpi_notify,
  1338. },
  1339. };
  1340. static int __init asus_laptop_init(void)
  1341. {
  1342. int result;
  1343. result = platform_driver_register(&platform_driver);
  1344. if (result < 0)
  1345. return result;
  1346. result = acpi_bus_register_driver(&asus_acpi_driver);
  1347. if (result < 0)
  1348. goto fail_acpi_driver;
  1349. if (!asus_device_present) {
  1350. result = -ENODEV;
  1351. goto fail_no_device;
  1352. }
  1353. return 0;
  1354. fail_no_device:
  1355. acpi_bus_unregister_driver(&asus_acpi_driver);
  1356. fail_acpi_driver:
  1357. platform_driver_unregister(&platform_driver);
  1358. return result;
  1359. }
  1360. static void __exit asus_laptop_exit(void)
  1361. {
  1362. acpi_bus_unregister_driver(&asus_acpi_driver);
  1363. platform_driver_unregister(&platform_driver);
  1364. }
  1365. module_init(asus_laptop_init);
  1366. module_exit(asus_laptop_exit);