asus-laptop.c 41 KB

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