asus-laptop.c 41 KB

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