asus-laptop.c 43 KB

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