asus-laptop.c 38 KB

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