asus-laptop.c 32 KB

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