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