asus-laptop.c 41 KB

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