asus-laptop.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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. struct backlight_properties props;
  549. if (!acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) &&
  550. !acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) &&
  551. lcd_switch_handle) {
  552. memset(&props, 0, sizeof(struct backlight_properties));
  553. props.max_brightness = 15;
  554. bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
  555. asus, &asusbl_ops, &props);
  556. if (IS_ERR(bd)) {
  557. pr_err("Could not register asus backlight device\n");
  558. asus->backlight_device = NULL;
  559. return PTR_ERR(bd);
  560. }
  561. asus->backlight_device = bd;
  562. bd->props.power = FB_BLANK_UNBLANK;
  563. bd->props.brightness = asus_read_brightness(bd);
  564. backlight_update_status(bd);
  565. }
  566. return 0;
  567. }
  568. static void asus_backlight_exit(struct asus_laptop *asus)
  569. {
  570. if (asus->backlight_device)
  571. backlight_device_unregister(asus->backlight_device);
  572. asus->backlight_device = NULL;
  573. }
  574. /*
  575. * Platform device handlers
  576. */
  577. /*
  578. * We write our info in page, we begin at offset off and cannot write more
  579. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  580. * number of bytes written in page
  581. */
  582. static ssize_t show_infos(struct device *dev,
  583. struct device_attribute *attr, char *page)
  584. {
  585. struct asus_laptop *asus = dev_get_drvdata(dev);
  586. int len = 0;
  587. unsigned long long temp;
  588. char buf[16]; /* enough for all info */
  589. acpi_status rv = AE_OK;
  590. /*
  591. * We use the easy way, we don't care of off and count,
  592. * so we don't set eof to 1
  593. */
  594. len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
  595. len += sprintf(page + len, "Model reference : %s\n", asus->name);
  596. /*
  597. * The SFUN method probably allows the original driver to get the list
  598. * of features supported by a given model. For now, 0x0100 or 0x0800
  599. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  600. * The significance of others is yet to be found.
  601. */
  602. rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
  603. if (!ACPI_FAILURE(rv))
  604. len += sprintf(page + len, "SFUN value : %#x\n",
  605. (uint) temp);
  606. /*
  607. * The HWRS method return informations about the hardware.
  608. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  609. * The significance of others is yet to be found.
  610. * If we don't find the method, we assume the device are present.
  611. */
  612. rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
  613. if (!ACPI_FAILURE(rv))
  614. len += sprintf(page + len, "HRWS value : %#x\n",
  615. (uint) temp);
  616. /*
  617. * Another value for userspace: the ASYM method returns 0x02 for
  618. * battery low and 0x04 for battery critical, its readings tend to be
  619. * more accurate than those provided by _BST.
  620. * Note: since not all the laptops provide this method, errors are
  621. * silently ignored.
  622. */
  623. rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
  624. if (!ACPI_FAILURE(rv))
  625. len += sprintf(page + len, "ASYM value : %#x\n",
  626. (uint) temp);
  627. if (asus->dsdt_info) {
  628. snprintf(buf, 16, "%d", asus->dsdt_info->length);
  629. len += sprintf(page + len, "DSDT length : %s\n", buf);
  630. snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
  631. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  632. snprintf(buf, 16, "%d", asus->dsdt_info->revision);
  633. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  634. snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
  635. len += sprintf(page + len, "OEM id : %s\n", buf);
  636. snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
  637. len += sprintf(page + len, "OEM table id : %s\n", buf);
  638. snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
  639. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  640. snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
  641. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  642. snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
  643. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  644. }
  645. return len;
  646. }
  647. static int parse_arg(const char *buf, unsigned long count, int *val)
  648. {
  649. if (!count)
  650. return 0;
  651. if (count > 31)
  652. return -EINVAL;
  653. if (sscanf(buf, "%i", val) != 1)
  654. return -EINVAL;
  655. return count;
  656. }
  657. static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
  658. const char *buf, size_t count,
  659. const char *method)
  660. {
  661. int rv, value;
  662. int out = 0;
  663. rv = parse_arg(buf, count, &value);
  664. if (rv > 0)
  665. out = value ? 1 : 0;
  666. if (write_acpi_int(asus->handle, method, value))
  667. return -ENODEV;
  668. return rv;
  669. }
  670. /*
  671. * LEDD display
  672. */
  673. static ssize_t show_ledd(struct device *dev,
  674. struct device_attribute *attr, char *buf)
  675. {
  676. struct asus_laptop *asus = dev_get_drvdata(dev);
  677. return sprintf(buf, "0x%08x\n", asus->ledd_status);
  678. }
  679. static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
  680. const char *buf, size_t count)
  681. {
  682. struct asus_laptop *asus = dev_get_drvdata(dev);
  683. int rv, value;
  684. rv = parse_arg(buf, count, &value);
  685. if (rv > 0) {
  686. if (write_acpi_int(asus->handle, METHOD_LEDD, value))
  687. pr_warning("LED display write failed\n");
  688. else
  689. asus->ledd_status = (u32) value;
  690. }
  691. return rv;
  692. }
  693. /*
  694. * Wireless
  695. */
  696. static int asus_wireless_status(struct asus_laptop *asus, int mask)
  697. {
  698. unsigned long long status;
  699. acpi_status rv = AE_OK;
  700. if (!asus->have_rsts)
  701. return (asus->wireless_status & mask) ? 1 : 0;
  702. rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
  703. NULL, &status);
  704. if (ACPI_FAILURE(rv)) {
  705. pr_warning("Error reading Wireless status\n");
  706. return -EINVAL;
  707. }
  708. return !!(status & mask);
  709. }
  710. /*
  711. * WLAN
  712. */
  713. static int asus_wlan_set(struct asus_laptop *asus, int status)
  714. {
  715. if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
  716. pr_warning("Error setting wlan status to %d", status);
  717. return -EIO;
  718. }
  719. return 0;
  720. }
  721. static ssize_t show_wlan(struct device *dev,
  722. struct device_attribute *attr, char *buf)
  723. {
  724. struct asus_laptop *asus = dev_get_drvdata(dev);
  725. return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
  726. }
  727. static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
  728. const char *buf, size_t count)
  729. {
  730. struct asus_laptop *asus = dev_get_drvdata(dev);
  731. return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
  732. }
  733. /*
  734. * Bluetooth
  735. */
  736. static int asus_bluetooth_set(struct asus_laptop *asus, int status)
  737. {
  738. if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
  739. pr_warning("Error setting bluetooth status to %d", status);
  740. return -EIO;
  741. }
  742. return 0;
  743. }
  744. static ssize_t show_bluetooth(struct device *dev,
  745. struct device_attribute *attr, char *buf)
  746. {
  747. struct asus_laptop *asus = dev_get_drvdata(dev);
  748. return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
  749. }
  750. static ssize_t store_bluetooth(struct device *dev,
  751. struct device_attribute *attr, const char *buf,
  752. size_t count)
  753. {
  754. struct asus_laptop *asus = dev_get_drvdata(dev);
  755. return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
  756. }
  757. /*
  758. * Display
  759. */
  760. static void asus_set_display(struct asus_laptop *asus, int value)
  761. {
  762. /* no sanity check needed for now */
  763. if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
  764. pr_warning("Error setting display\n");
  765. return;
  766. }
  767. static int read_display(struct asus_laptop *asus)
  768. {
  769. unsigned long long value = 0;
  770. acpi_status rv = AE_OK;
  771. /*
  772. * In most of the case, we know how to set the display, but sometime
  773. * we can't read it
  774. */
  775. if (display_get_handle) {
  776. rv = acpi_evaluate_integer(display_get_handle, NULL,
  777. NULL, &value);
  778. if (ACPI_FAILURE(rv))
  779. pr_warning("Error reading display status\n");
  780. }
  781. value &= 0x0F; /* needed for some models, shouldn't hurt others */
  782. return value;
  783. }
  784. /*
  785. * Now, *this* one could be more user-friendly, but so far, no-one has
  786. * complained. The significance of bits is the same as in store_disp()
  787. */
  788. static ssize_t show_disp(struct device *dev,
  789. struct device_attribute *attr, char *buf)
  790. {
  791. struct asus_laptop *asus = dev_get_drvdata(dev);
  792. if (!display_get_handle)
  793. return -ENODEV;
  794. return sprintf(buf, "%d\n", read_display(asus));
  795. }
  796. /*
  797. * Experimental support for display switching. As of now: 1 should activate
  798. * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
  799. * Any combination (bitwise) of these will suffice. I never actually tested 4
  800. * displays hooked up simultaneously, so be warned. See the acpi4asus README
  801. * for more info.
  802. */
  803. static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
  804. const char *buf, size_t count)
  805. {
  806. struct asus_laptop *asus = dev_get_drvdata(dev);
  807. int rv, value;
  808. rv = parse_arg(buf, count, &value);
  809. if (rv > 0)
  810. asus_set_display(asus, value);
  811. return rv;
  812. }
  813. /*
  814. * Light Sens
  815. */
  816. static void asus_als_switch(struct asus_laptop *asus, int value)
  817. {
  818. if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
  819. pr_warning("Error setting light sensor switch\n");
  820. asus->light_switch = value;
  821. }
  822. static ssize_t show_lssw(struct device *dev,
  823. struct device_attribute *attr, char *buf)
  824. {
  825. struct asus_laptop *asus = dev_get_drvdata(dev);
  826. return sprintf(buf, "%d\n", asus->light_switch);
  827. }
  828. static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
  829. const char *buf, size_t count)
  830. {
  831. struct asus_laptop *asus = dev_get_drvdata(dev);
  832. int rv, value;
  833. rv = parse_arg(buf, count, &value);
  834. if (rv > 0)
  835. asus_als_switch(asus, value ? 1 : 0);
  836. return rv;
  837. }
  838. static void asus_als_level(struct asus_laptop *asus, int value)
  839. {
  840. if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
  841. pr_warning("Error setting light sensor level\n");
  842. asus->light_level = value;
  843. }
  844. static ssize_t show_lslvl(struct device *dev,
  845. struct device_attribute *attr, char *buf)
  846. {
  847. struct asus_laptop *asus = dev_get_drvdata(dev);
  848. return sprintf(buf, "%d\n", asus->light_level);
  849. }
  850. static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
  851. const char *buf, size_t count)
  852. {
  853. struct asus_laptop *asus = dev_get_drvdata(dev);
  854. int rv, value;
  855. rv = parse_arg(buf, count, &value);
  856. if (rv > 0) {
  857. value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
  858. /* 0 <= value <= 15 */
  859. asus_als_level(asus, value);
  860. }
  861. return rv;
  862. }
  863. /*
  864. * GPS
  865. */
  866. static int asus_gps_status(struct asus_laptop *asus)
  867. {
  868. unsigned long long status;
  869. acpi_status rv = AE_OK;
  870. rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
  871. NULL, &status);
  872. if (ACPI_FAILURE(rv)) {
  873. pr_warning("Error reading GPS status\n");
  874. return -ENODEV;
  875. }
  876. return !!status;
  877. }
  878. static int asus_gps_switch(struct asus_laptop *asus, int status)
  879. {
  880. const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
  881. if (write_acpi_int(asus->handle, meth, 0x02))
  882. return -ENODEV;
  883. return 0;
  884. }
  885. static ssize_t show_gps(struct device *dev,
  886. struct device_attribute *attr, char *buf)
  887. {
  888. struct asus_laptop *asus = dev_get_drvdata(dev);
  889. return sprintf(buf, "%d\n", asus_gps_status(asus));
  890. }
  891. static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
  892. const char *buf, size_t count)
  893. {
  894. struct asus_laptop *asus = dev_get_drvdata(dev);
  895. int rv, value;
  896. int ret;
  897. rv = parse_arg(buf, count, &value);
  898. if (rv <= 0)
  899. return -EINVAL;
  900. ret = asus_gps_switch(asus, !!value);
  901. if (ret)
  902. return ret;
  903. rfkill_set_sw_state(asus->gps_rfkill, !value);
  904. return rv;
  905. }
  906. /*
  907. * rfkill
  908. */
  909. static int asus_gps_rfkill_set(void *data, bool blocked)
  910. {
  911. acpi_handle handle = data;
  912. return asus_gps_switch(handle, !blocked);
  913. }
  914. static const struct rfkill_ops asus_gps_rfkill_ops = {
  915. .set_block = asus_gps_rfkill_set,
  916. };
  917. static void asus_rfkill_exit(struct asus_laptop *asus)
  918. {
  919. if (asus->gps_rfkill) {
  920. rfkill_unregister(asus->gps_rfkill);
  921. rfkill_destroy(asus->gps_rfkill);
  922. asus->gps_rfkill = NULL;
  923. }
  924. }
  925. static int asus_rfkill_init(struct asus_laptop *asus)
  926. {
  927. int result;
  928. if (acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) ||
  929. acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) ||
  930. acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
  931. return 0;
  932. asus->gps_rfkill = rfkill_alloc("asus-gps", &asus->platform_device->dev,
  933. RFKILL_TYPE_GPS,
  934. &asus_gps_rfkill_ops, NULL);
  935. if (!asus->gps_rfkill)
  936. return -EINVAL;
  937. result = rfkill_register(asus->gps_rfkill);
  938. if (result) {
  939. rfkill_destroy(asus->gps_rfkill);
  940. asus->gps_rfkill = NULL;
  941. }
  942. return result;
  943. }
  944. /*
  945. * Input device (i.e. hotkeys)
  946. */
  947. static void asus_input_notify(struct asus_laptop *asus, int event)
  948. {
  949. if (asus->inputdev)
  950. sparse_keymap_report_event(asus->inputdev, event, 1, true);
  951. }
  952. static int asus_input_init(struct asus_laptop *asus)
  953. {
  954. struct input_dev *input;
  955. int error;
  956. input = input_allocate_device();
  957. if (!input) {
  958. pr_info("Unable to allocate input device\n");
  959. return 0;
  960. }
  961. input->name = "Asus Laptop extra buttons";
  962. input->phys = ASUS_LAPTOP_FILE "/input0";
  963. input->id.bustype = BUS_HOST;
  964. input->dev.parent = &asus->platform_device->dev;
  965. input_set_drvdata(input, asus);
  966. error = sparse_keymap_setup(input, asus_keymap, NULL);
  967. if (error) {
  968. pr_err("Unable to setup input device keymap\n");
  969. goto err_keymap;
  970. }
  971. error = input_register_device(input);
  972. if (error) {
  973. pr_info("Unable to register input device\n");
  974. goto err_device;
  975. }
  976. asus->inputdev = input;
  977. return 0;
  978. err_keymap:
  979. sparse_keymap_free(input);
  980. err_device:
  981. input_free_device(input);
  982. return error;
  983. }
  984. static void asus_input_exit(struct asus_laptop *asus)
  985. {
  986. if (asus->inputdev) {
  987. sparse_keymap_free(asus->inputdev);
  988. input_unregister_device(asus->inputdev);
  989. }
  990. }
  991. /*
  992. * ACPI driver
  993. */
  994. static void asus_acpi_notify(struct acpi_device *device, u32 event)
  995. {
  996. struct asus_laptop *asus = acpi_driver_data(device);
  997. u16 count;
  998. /*
  999. * We need to tell the backlight device when the backlight power is
  1000. * switched
  1001. */
  1002. if (event == ATKD_LCD_ON)
  1003. lcd_blank(asus, FB_BLANK_UNBLANK);
  1004. else if (event == ATKD_LCD_OFF)
  1005. lcd_blank(asus, FB_BLANK_POWERDOWN);
  1006. /* TODO Find a better way to handle events count. */
  1007. count = asus->event_count[event % 128]++;
  1008. acpi_bus_generate_proc_event(asus->device, event, count);
  1009. acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
  1010. dev_name(&asus->device->dev), event,
  1011. count);
  1012. /* Brightness events are special */
  1013. if (event >= ATKD_BR_MIN && event <= ATKD_BR_MAX) {
  1014. /* Ignore them completely if the acpi video driver is used */
  1015. if (asus->backlight_device != NULL) {
  1016. /* Update the backlight device. */
  1017. asus_backlight_notify(asus);
  1018. }
  1019. return ;
  1020. }
  1021. asus_input_notify(asus, event);
  1022. }
  1023. static DEVICE_ATTR(infos, S_IRUGO, show_infos, NULL);
  1024. static DEVICE_ATTR(wlan, S_IRUGO | S_IWUSR, show_wlan, store_wlan);
  1025. static DEVICE_ATTR(bluetooth, S_IRUGO | S_IWUSR, show_bluetooth,
  1026. store_bluetooth);
  1027. static DEVICE_ATTR(display, S_IRUGO | S_IWUSR, show_disp, store_disp);
  1028. static DEVICE_ATTR(ledd, S_IRUGO | S_IWUSR, show_ledd, store_ledd);
  1029. static DEVICE_ATTR(ls_level, S_IRUGO | S_IWUSR, show_lslvl, store_lslvl);
  1030. static DEVICE_ATTR(ls_switch, S_IRUGO | S_IWUSR, show_lssw, store_lssw);
  1031. static DEVICE_ATTR(gps, S_IRUGO | S_IWUSR, show_gps, store_gps);
  1032. static void asus_sysfs_exit(struct asus_laptop *asus)
  1033. {
  1034. struct platform_device *device = asus->platform_device;
  1035. device_remove_file(&device->dev, &dev_attr_infos);
  1036. device_remove_file(&device->dev, &dev_attr_wlan);
  1037. device_remove_file(&device->dev, &dev_attr_bluetooth);
  1038. device_remove_file(&device->dev, &dev_attr_display);
  1039. device_remove_file(&device->dev, &dev_attr_ledd);
  1040. device_remove_file(&device->dev, &dev_attr_ls_switch);
  1041. device_remove_file(&device->dev, &dev_attr_ls_level);
  1042. device_remove_file(&device->dev, &dev_attr_gps);
  1043. }
  1044. static int asus_sysfs_init(struct asus_laptop *asus)
  1045. {
  1046. struct platform_device *device = asus->platform_device;
  1047. int err;
  1048. err = device_create_file(&device->dev, &dev_attr_infos);
  1049. if (err)
  1050. return err;
  1051. if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL)) {
  1052. err = device_create_file(&device->dev, &dev_attr_wlan);
  1053. if (err)
  1054. return err;
  1055. }
  1056. if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL)) {
  1057. err = device_create_file(&device->dev, &dev_attr_bluetooth);
  1058. if (err)
  1059. return err;
  1060. }
  1061. if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
  1062. err = device_create_file(&device->dev, &dev_attr_display);
  1063. if (err)
  1064. return err;
  1065. }
  1066. if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL)) {
  1067. err = device_create_file(&device->dev, &dev_attr_ledd);
  1068. if (err)
  1069. return err;
  1070. }
  1071. if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1072. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1073. err = device_create_file(&device->dev, &dev_attr_ls_switch);
  1074. if (err)
  1075. return err;
  1076. err = device_create_file(&device->dev, &dev_attr_ls_level);
  1077. if (err)
  1078. return err;
  1079. }
  1080. if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
  1081. !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
  1082. !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL)) {
  1083. err = device_create_file(&device->dev, &dev_attr_gps);
  1084. if (err)
  1085. return err;
  1086. }
  1087. return err;
  1088. }
  1089. static int asus_platform_init(struct asus_laptop *asus)
  1090. {
  1091. int err;
  1092. asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
  1093. if (!asus->platform_device)
  1094. return -ENOMEM;
  1095. platform_set_drvdata(asus->platform_device, asus);
  1096. err = platform_device_add(asus->platform_device);
  1097. if (err)
  1098. goto fail_platform_device;
  1099. err = asus_sysfs_init(asus);
  1100. if (err)
  1101. goto fail_sysfs;
  1102. return 0;
  1103. fail_sysfs:
  1104. asus_sysfs_exit(asus);
  1105. platform_device_del(asus->platform_device);
  1106. fail_platform_device:
  1107. platform_device_put(asus->platform_device);
  1108. return err;
  1109. }
  1110. static void asus_platform_exit(struct asus_laptop *asus)
  1111. {
  1112. asus_sysfs_exit(asus);
  1113. platform_device_unregister(asus->platform_device);
  1114. }
  1115. static struct platform_driver platform_driver = {
  1116. .driver = {
  1117. .name = ASUS_LAPTOP_FILE,
  1118. .owner = THIS_MODULE,
  1119. }
  1120. };
  1121. static int asus_handle_init(char *name, acpi_handle * handle,
  1122. char **paths, int num_paths)
  1123. {
  1124. int i;
  1125. acpi_status status;
  1126. for (i = 0; i < num_paths; i++) {
  1127. status = acpi_get_handle(NULL, paths[i], handle);
  1128. if (ACPI_SUCCESS(status))
  1129. return 0;
  1130. }
  1131. *handle = NULL;
  1132. return -ENODEV;
  1133. }
  1134. #define ASUS_HANDLE_INIT(object) \
  1135. asus_handle_init(#object, &object##_handle, object##_paths, \
  1136. ARRAY_SIZE(object##_paths))
  1137. /*
  1138. * This function is used to initialize the context with right values. In this
  1139. * method, we can make all the detection we want, and modify the asus_laptop
  1140. * struct
  1141. */
  1142. static int asus_laptop_get_info(struct asus_laptop *asus)
  1143. {
  1144. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1145. union acpi_object *model = NULL;
  1146. unsigned long long bsts_result, hwrs_result;
  1147. char *string = NULL;
  1148. acpi_status status;
  1149. /*
  1150. * Get DSDT headers early enough to allow for differentiating between
  1151. * models, but late enough to allow acpi_bus_register_driver() to fail
  1152. * before doing anything ACPI-specific. Should we encounter a machine,
  1153. * which needs special handling (i.e. its hotkey device has a different
  1154. * HID), this bit will be moved.
  1155. */
  1156. status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
  1157. if (ACPI_FAILURE(status))
  1158. pr_warning("Couldn't get the DSDT table header\n");
  1159. /* We have to write 0 on init this far for all ASUS models */
  1160. if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
  1161. pr_err("Hotkey initialization failed\n");
  1162. return -ENODEV;
  1163. }
  1164. /* This needs to be called for some laptops to init properly */
  1165. status =
  1166. acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
  1167. if (ACPI_FAILURE(status))
  1168. pr_warning("Error calling BSTS\n");
  1169. else if (bsts_result)
  1170. pr_notice("BSTS called, 0x%02x returned\n",
  1171. (uint) bsts_result);
  1172. /* This too ... */
  1173. if (write_acpi_int(asus->handle, "CWAP", wapf))
  1174. pr_err("Error calling CWAP(%d)\n", wapf);
  1175. /*
  1176. * Try to match the object returned by INIT to the specific model.
  1177. * Handle every possible object (or the lack of thereof) the DSDT
  1178. * writers might throw at us. When in trouble, we pass NULL to
  1179. * asus_model_match() and try something completely different.
  1180. */
  1181. if (buffer.pointer) {
  1182. model = buffer.pointer;
  1183. switch (model->type) {
  1184. case ACPI_TYPE_STRING:
  1185. string = model->string.pointer;
  1186. break;
  1187. case ACPI_TYPE_BUFFER:
  1188. string = model->buffer.pointer;
  1189. break;
  1190. default:
  1191. string = "";
  1192. break;
  1193. }
  1194. }
  1195. asus->name = kstrdup(string, GFP_KERNEL);
  1196. if (!asus->name)
  1197. return -ENOMEM;
  1198. if (*string)
  1199. pr_notice(" %s model detected\n", string);
  1200. /*
  1201. * The HWRS method return informations about the hardware.
  1202. * 0x80 bit is for WLAN, 0x100 for Bluetooth.
  1203. * The significance of others is yet to be found.
  1204. */
  1205. status =
  1206. acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
  1207. if (!ACPI_FAILURE(status))
  1208. pr_notice(" HRWS returned %x", (int)hwrs_result);
  1209. if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
  1210. asus->have_rsts = true;
  1211. /* Scheduled for removal */
  1212. ASUS_HANDLE_INIT(lcd_switch);
  1213. ASUS_HANDLE_INIT(display_get);
  1214. kfree(model);
  1215. return AE_OK;
  1216. }
  1217. static bool asus_device_present;
  1218. static int __devinit asus_acpi_init(struct asus_laptop *asus)
  1219. {
  1220. int result = 0;
  1221. result = acpi_bus_get_status(asus->device);
  1222. if (result)
  1223. return result;
  1224. if (!asus->device->status.present) {
  1225. pr_err("Hotkey device not present, aborting\n");
  1226. return -ENODEV;
  1227. }
  1228. result = asus_laptop_get_info(asus);
  1229. if (result)
  1230. return result;
  1231. /* WLED and BLED are on by default */
  1232. if (bluetooth_status >= 0)
  1233. asus_bluetooth_set(asus, !!bluetooth_status);
  1234. if (wlan_status >= 0)
  1235. asus_wlan_set(asus, !!wlan_status);
  1236. /* Keyboard Backlight is on by default */
  1237. if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
  1238. asus_kled_set(asus, 1);
  1239. /* LED display is off by default */
  1240. asus->ledd_status = 0xFFF;
  1241. /* Set initial values of light sensor and level */
  1242. asus->light_switch = 0; /* Default to light sensor disabled */
  1243. asus->light_level = 5; /* level 5 for sensor sensitivity */
  1244. if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
  1245. !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
  1246. asus_als_switch(asus, asus->light_switch);
  1247. asus_als_level(asus, asus->light_level);
  1248. }
  1249. asus->lcd_state = 1; /* LCD should be on when the module load */
  1250. return result;
  1251. }
  1252. static int __devinit asus_acpi_add(struct acpi_device *device)
  1253. {
  1254. struct asus_laptop *asus;
  1255. int result;
  1256. pr_notice("Asus Laptop Support version %s\n",
  1257. ASUS_LAPTOP_VERSION);
  1258. asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
  1259. if (!asus)
  1260. return -ENOMEM;
  1261. asus->handle = device->handle;
  1262. strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
  1263. strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
  1264. device->driver_data = asus;
  1265. asus->device = device;
  1266. result = asus_acpi_init(asus);
  1267. if (result)
  1268. goto fail_platform;
  1269. /*
  1270. * Register the platform device first. It is used as a parent for the
  1271. * sub-devices below.
  1272. */
  1273. result = asus_platform_init(asus);
  1274. if (result)
  1275. goto fail_platform;
  1276. if (!acpi_video_backlight_support()) {
  1277. result = asus_backlight_init(asus);
  1278. if (result)
  1279. goto fail_backlight;
  1280. } else
  1281. pr_info("Backlight controlled by ACPI video driver\n");
  1282. result = asus_input_init(asus);
  1283. if (result)
  1284. goto fail_input;
  1285. result = asus_led_init(asus);
  1286. if (result)
  1287. goto fail_led;
  1288. result = asus_rfkill_init(asus);
  1289. if (result)
  1290. goto fail_rfkill;
  1291. asus_device_present = true;
  1292. return 0;
  1293. fail_rfkill:
  1294. asus_led_exit(asus);
  1295. fail_led:
  1296. asus_input_exit(asus);
  1297. fail_input:
  1298. asus_backlight_exit(asus);
  1299. fail_backlight:
  1300. asus_platform_exit(asus);
  1301. fail_platform:
  1302. kfree(asus->name);
  1303. kfree(asus);
  1304. return result;
  1305. }
  1306. static int asus_acpi_remove(struct acpi_device *device, int type)
  1307. {
  1308. struct asus_laptop *asus = acpi_driver_data(device);
  1309. asus_backlight_exit(asus);
  1310. asus_rfkill_exit(asus);
  1311. asus_led_exit(asus);
  1312. asus_input_exit(asus);
  1313. asus_platform_exit(asus);
  1314. kfree(asus->name);
  1315. kfree(asus);
  1316. return 0;
  1317. }
  1318. static const struct acpi_device_id asus_device_ids[] = {
  1319. {"ATK0100", 0},
  1320. {"ATK0101", 0},
  1321. {"", 0},
  1322. };
  1323. MODULE_DEVICE_TABLE(acpi, asus_device_ids);
  1324. static struct acpi_driver asus_acpi_driver = {
  1325. .name = ASUS_LAPTOP_NAME,
  1326. .class = ASUS_LAPTOP_CLASS,
  1327. .owner = THIS_MODULE,
  1328. .ids = asus_device_ids,
  1329. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1330. .ops = {
  1331. .add = asus_acpi_add,
  1332. .remove = asus_acpi_remove,
  1333. .notify = asus_acpi_notify,
  1334. },
  1335. };
  1336. static int __init asus_laptop_init(void)
  1337. {
  1338. int result;
  1339. result = platform_driver_register(&platform_driver);
  1340. if (result < 0)
  1341. return result;
  1342. result = acpi_bus_register_driver(&asus_acpi_driver);
  1343. if (result < 0)
  1344. goto fail_acpi_driver;
  1345. if (!asus_device_present) {
  1346. result = -ENODEV;
  1347. goto fail_no_device;
  1348. }
  1349. return 0;
  1350. fail_no_device:
  1351. acpi_bus_unregister_driver(&asus_acpi_driver);
  1352. fail_acpi_driver:
  1353. platform_driver_unregister(&platform_driver);
  1354. return result;
  1355. }
  1356. static void __exit asus_laptop_exit(void)
  1357. {
  1358. acpi_bus_unregister_driver(&asus_acpi_driver);
  1359. platform_driver_unregister(&platform_driver);
  1360. }
  1361. module_init(asus_laptop_init);
  1362. module_exit(asus_laptop_exit);