asus-laptop.c 40 KB

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