asus-laptop.c 46 KB

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