asus-laptop.c 41 KB

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