eeepc-laptop.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * eeepc-laptop.c - Asus Eee PC extras
  3. *
  4. * Based on asus_acpi.c as patched for the Eee PC by Asus:
  5. * ftp://ftp.asus.com/pub/ASUS/EeePC/701/ASUS_ACPI_071126.rar
  6. * Based on eee.c from eeepc-linux
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/backlight.h>
  25. #include <linux/fb.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/slab.h>
  29. #include <linux/acpi.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/input.h>
  32. #include <linux/input/sparse-keymap.h>
  33. #include <linux/rfkill.h>
  34. #include <linux/pci.h>
  35. #include <linux/pci_hotplug.h>
  36. #include <linux/leds.h>
  37. #include <linux/dmi.h>
  38. #define EEEPC_LAPTOP_VERSION "0.1"
  39. #define EEEPC_LAPTOP_NAME "Eee PC Hotkey Driver"
  40. #define EEEPC_LAPTOP_FILE "eeepc"
  41. #define EEEPC_ACPI_CLASS "hotkey"
  42. #define EEEPC_ACPI_DEVICE_NAME "Hotkey"
  43. #define EEEPC_ACPI_HID "ASUS010"
  44. MODULE_AUTHOR("Corentin Chary, Eric Cooper");
  45. MODULE_DESCRIPTION(EEEPC_LAPTOP_NAME);
  46. MODULE_LICENSE("GPL");
  47. static bool hotplug_disabled;
  48. module_param(hotplug_disabled, bool, 0444);
  49. MODULE_PARM_DESC(hotplug_disabled,
  50. "Disable hotplug for wireless device. "
  51. "If your laptop need that, please report to "
  52. "acpi4asus-user@lists.sourceforge.net.");
  53. /*
  54. * Definitions for Asus EeePC
  55. */
  56. #define NOTIFY_BRN_MIN 0x20
  57. #define NOTIFY_BRN_MAX 0x2f
  58. enum {
  59. DISABLE_ASL_WLAN = 0x0001,
  60. DISABLE_ASL_BLUETOOTH = 0x0002,
  61. DISABLE_ASL_IRDA = 0x0004,
  62. DISABLE_ASL_CAMERA = 0x0008,
  63. DISABLE_ASL_TV = 0x0010,
  64. DISABLE_ASL_GPS = 0x0020,
  65. DISABLE_ASL_DISPLAYSWITCH = 0x0040,
  66. DISABLE_ASL_MODEM = 0x0080,
  67. DISABLE_ASL_CARDREADER = 0x0100,
  68. DISABLE_ASL_3G = 0x0200,
  69. DISABLE_ASL_WIMAX = 0x0400,
  70. DISABLE_ASL_HWCF = 0x0800
  71. };
  72. enum {
  73. CM_ASL_WLAN = 0,
  74. CM_ASL_BLUETOOTH,
  75. CM_ASL_IRDA,
  76. CM_ASL_1394,
  77. CM_ASL_CAMERA,
  78. CM_ASL_TV,
  79. CM_ASL_GPS,
  80. CM_ASL_DVDROM,
  81. CM_ASL_DISPLAYSWITCH,
  82. CM_ASL_PANELBRIGHT,
  83. CM_ASL_BIOSFLASH,
  84. CM_ASL_ACPIFLASH,
  85. CM_ASL_CPUFV,
  86. CM_ASL_CPUTEMPERATURE,
  87. CM_ASL_FANCPU,
  88. CM_ASL_FANCHASSIS,
  89. CM_ASL_USBPORT1,
  90. CM_ASL_USBPORT2,
  91. CM_ASL_USBPORT3,
  92. CM_ASL_MODEM,
  93. CM_ASL_CARDREADER,
  94. CM_ASL_3G,
  95. CM_ASL_WIMAX,
  96. CM_ASL_HWCF,
  97. CM_ASL_LID,
  98. CM_ASL_TYPE,
  99. CM_ASL_PANELPOWER, /*P901*/
  100. CM_ASL_TPD
  101. };
  102. static const char *cm_getv[] = {
  103. "WLDG", "BTHG", NULL, NULL,
  104. "CAMG", NULL, NULL, NULL,
  105. NULL, "PBLG", NULL, NULL,
  106. "CFVG", NULL, NULL, NULL,
  107. "USBG", NULL, NULL, "MODG",
  108. "CRDG", "M3GG", "WIMG", "HWCF",
  109. "LIDG", "TYPE", "PBPG", "TPDG"
  110. };
  111. static const char *cm_setv[] = {
  112. "WLDS", "BTHS", NULL, NULL,
  113. "CAMS", NULL, NULL, NULL,
  114. "SDSP", "PBLS", "HDPS", NULL,
  115. "CFVS", NULL, NULL, NULL,
  116. "USBG", NULL, NULL, "MODS",
  117. "CRDS", "M3GS", "WIMS", NULL,
  118. NULL, NULL, "PBPS", "TPDS"
  119. };
  120. static const struct key_entry eeepc_keymap[] = {
  121. { KE_KEY, 0x10, { KEY_WLAN } },
  122. { KE_KEY, 0x11, { KEY_WLAN } },
  123. { KE_KEY, 0x12, { KEY_PROG1 } },
  124. { KE_KEY, 0x13, { KEY_MUTE } },
  125. { KE_KEY, 0x14, { KEY_VOLUMEDOWN } },
  126. { KE_KEY, 0x15, { KEY_VOLUMEUP } },
  127. { KE_KEY, 0x16, { KEY_DISPLAY_OFF } },
  128. { KE_KEY, 0x1a, { KEY_COFFEE } },
  129. { KE_KEY, 0x1b, { KEY_ZOOM } },
  130. { KE_KEY, 0x1c, { KEY_PROG2 } },
  131. { KE_KEY, 0x1d, { KEY_PROG3 } },
  132. { KE_KEY, NOTIFY_BRN_MIN, { KEY_BRIGHTNESSDOWN } },
  133. { KE_KEY, NOTIFY_BRN_MAX, { KEY_BRIGHTNESSUP } },
  134. { KE_KEY, 0x30, { KEY_SWITCHVIDEOMODE } },
  135. { KE_KEY, 0x31, { KEY_SWITCHVIDEOMODE } },
  136. { KE_KEY, 0x32, { KEY_SWITCHVIDEOMODE } },
  137. { KE_KEY, 0x37, { KEY_F13 } }, /* Disable Touchpad */
  138. { KE_KEY, 0x38, { KEY_F14 } },
  139. { KE_END, 0 },
  140. };
  141. /*
  142. * This is the main structure, we can use it to store useful information
  143. */
  144. struct eeepc_laptop {
  145. acpi_handle handle; /* the handle of the acpi device */
  146. u32 cm_supported; /* the control methods supported
  147. by this BIOS */
  148. bool cpufv_disabled;
  149. bool hotplug_disabled;
  150. u16 event_count[128]; /* count for each event */
  151. struct platform_device *platform_device;
  152. struct acpi_device *device; /* the device we are in */
  153. struct backlight_device *backlight_device;
  154. struct input_dev *inputdev;
  155. struct rfkill *wlan_rfkill;
  156. struct rfkill *bluetooth_rfkill;
  157. struct rfkill *wwan3g_rfkill;
  158. struct rfkill *wimax_rfkill;
  159. struct hotplug_slot *hotplug_slot;
  160. struct mutex hotplug_lock;
  161. struct led_classdev tpd_led;
  162. int tpd_led_wk;
  163. struct workqueue_struct *led_workqueue;
  164. struct work_struct tpd_led_work;
  165. };
  166. /*
  167. * ACPI Helpers
  168. */
  169. static int write_acpi_int(acpi_handle handle, const char *method, int val)
  170. {
  171. acpi_status status;
  172. status = acpi_execute_simple_method(handle, (char *)method, val);
  173. return (status == AE_OK ? 0 : -1);
  174. }
  175. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  176. {
  177. acpi_status status;
  178. unsigned long long result;
  179. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  180. if (ACPI_FAILURE(status)) {
  181. *val = -1;
  182. return -1;
  183. } else {
  184. *val = result;
  185. return 0;
  186. }
  187. }
  188. static int set_acpi(struct eeepc_laptop *eeepc, int cm, int value)
  189. {
  190. const char *method = cm_setv[cm];
  191. if (method == NULL)
  192. return -ENODEV;
  193. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  194. return -ENODEV;
  195. if (write_acpi_int(eeepc->handle, method, value))
  196. pr_warn("Error writing %s\n", method);
  197. return 0;
  198. }
  199. static int get_acpi(struct eeepc_laptop *eeepc, int cm)
  200. {
  201. const char *method = cm_getv[cm];
  202. int value;
  203. if (method == NULL)
  204. return -ENODEV;
  205. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  206. return -ENODEV;
  207. if (read_acpi_int(eeepc->handle, method, &value))
  208. pr_warn("Error reading %s\n", method);
  209. return value;
  210. }
  211. static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
  212. acpi_handle *handle)
  213. {
  214. const char *method = cm_setv[cm];
  215. acpi_status status;
  216. if (method == NULL)
  217. return -ENODEV;
  218. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  219. return -ENODEV;
  220. status = acpi_get_handle(eeepc->handle, (char *)method,
  221. handle);
  222. if (status != AE_OK) {
  223. pr_warn("Error finding %s\n", method);
  224. return -ENODEV;
  225. }
  226. return 0;
  227. }
  228. /*
  229. * Sys helpers
  230. */
  231. static int parse_arg(const char *buf, int *val)
  232. {
  233. if (sscanf(buf, "%i", val) != 1)
  234. return -EINVAL;
  235. return 0;
  236. }
  237. static ssize_t store_sys_acpi(struct device *dev, int cm,
  238. const char *buf, size_t count)
  239. {
  240. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  241. int rv, value;
  242. rv = parse_arg(buf, &value);
  243. if (rv < 0)
  244. return rv;
  245. rv = set_acpi(eeepc, cm, value);
  246. if (rv < 0)
  247. return -EIO;
  248. return count;
  249. }
  250. static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
  251. {
  252. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  253. int value = get_acpi(eeepc, cm);
  254. if (value < 0)
  255. return -EIO;
  256. return sprintf(buf, "%d\n", value);
  257. }
  258. #define EEEPC_ACPI_SHOW_FUNC(_name, _cm) \
  259. static ssize_t _name##_show(struct device *dev, \
  260. struct device_attribute *attr, \
  261. char *buf) \
  262. { \
  263. return show_sys_acpi(dev, _cm, buf); \
  264. }
  265. #define EEEPC_ACPI_STORE_FUNC(_name, _cm) \
  266. static ssize_t _name##_store(struct device *dev, \
  267. struct device_attribute *attr, \
  268. const char *buf, size_t count) \
  269. { \
  270. return store_sys_acpi(dev, _cm, buf, count); \
  271. }
  272. #define EEEPC_CREATE_DEVICE_ATTR_RW(_name, _cm) \
  273. EEEPC_ACPI_SHOW_FUNC(_name, _cm) \
  274. EEEPC_ACPI_STORE_FUNC(_name, _cm) \
  275. static DEVICE_ATTR_RW(_name)
  276. #define EEEPC_CREATE_DEVICE_ATTR_WO(_name, _cm) \
  277. EEEPC_ACPI_STORE_FUNC(_name, _cm) \
  278. static DEVICE_ATTR_WO(_name)
  279. EEEPC_CREATE_DEVICE_ATTR_RW(camera, CM_ASL_CAMERA);
  280. EEEPC_CREATE_DEVICE_ATTR_RW(cardr, CM_ASL_CARDREADER);
  281. EEEPC_CREATE_DEVICE_ATTR_WO(disp, CM_ASL_DISPLAYSWITCH);
  282. struct eeepc_cpufv {
  283. int num;
  284. int cur;
  285. };
  286. static int get_cpufv(struct eeepc_laptop *eeepc, struct eeepc_cpufv *c)
  287. {
  288. c->cur = get_acpi(eeepc, CM_ASL_CPUFV);
  289. if (c->cur < 0)
  290. return -ENODEV;
  291. c->num = (c->cur >> 8) & 0xff;
  292. c->cur &= 0xff;
  293. if (c->num == 0 || c->num > 12)
  294. return -ENODEV;
  295. return 0;
  296. }
  297. static ssize_t available_cpufv_show(struct device *dev,
  298. struct device_attribute *attr,
  299. char *buf)
  300. {
  301. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  302. struct eeepc_cpufv c;
  303. int i;
  304. ssize_t len = 0;
  305. if (get_cpufv(eeepc, &c))
  306. return -ENODEV;
  307. for (i = 0; i < c.num; i++)
  308. len += sprintf(buf + len, "%d ", i);
  309. len += sprintf(buf + len, "\n");
  310. return len;
  311. }
  312. static ssize_t cpufv_show(struct device *dev,
  313. struct device_attribute *attr,
  314. char *buf)
  315. {
  316. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  317. struct eeepc_cpufv c;
  318. if (get_cpufv(eeepc, &c))
  319. return -ENODEV;
  320. return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
  321. }
  322. static ssize_t cpufv_store(struct device *dev,
  323. struct device_attribute *attr,
  324. const char *buf, size_t count)
  325. {
  326. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  327. struct eeepc_cpufv c;
  328. int rv, value;
  329. if (eeepc->cpufv_disabled)
  330. return -EPERM;
  331. if (get_cpufv(eeepc, &c))
  332. return -ENODEV;
  333. rv = parse_arg(buf, &value);
  334. if (rv < 0)
  335. return rv;
  336. if (value < 0 || value >= c.num)
  337. return -EINVAL;
  338. rv = set_acpi(eeepc, CM_ASL_CPUFV, value);
  339. if (rv)
  340. return rv;
  341. return count;
  342. }
  343. static ssize_t cpufv_disabled_show(struct device *dev,
  344. struct device_attribute *attr,
  345. char *buf)
  346. {
  347. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  348. return sprintf(buf, "%d\n", eeepc->cpufv_disabled);
  349. }
  350. static ssize_t cpufv_disabled_store(struct device *dev,
  351. struct device_attribute *attr,
  352. const char *buf, size_t count)
  353. {
  354. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  355. int rv, value;
  356. rv = parse_arg(buf, &value);
  357. if (rv < 0)
  358. return rv;
  359. switch (value) {
  360. case 0:
  361. if (eeepc->cpufv_disabled)
  362. pr_warn("cpufv enabled (not officially supported on this model)\n");
  363. eeepc->cpufv_disabled = false;
  364. return count;
  365. case 1:
  366. return -EPERM;
  367. default:
  368. return -EINVAL;
  369. }
  370. }
  371. static DEVICE_ATTR_RW(cpufv);
  372. static DEVICE_ATTR_RO(available_cpufv);
  373. static DEVICE_ATTR_RW(cpufv_disabled);
  374. static struct attribute *platform_attributes[] = {
  375. &dev_attr_camera.attr,
  376. &dev_attr_cardr.attr,
  377. &dev_attr_disp.attr,
  378. &dev_attr_cpufv.attr,
  379. &dev_attr_available_cpufv.attr,
  380. &dev_attr_cpufv_disabled.attr,
  381. NULL
  382. };
  383. static struct attribute_group platform_attribute_group = {
  384. .attrs = platform_attributes
  385. };
  386. static int eeepc_platform_init(struct eeepc_laptop *eeepc)
  387. {
  388. int result;
  389. eeepc->platform_device = platform_device_alloc(EEEPC_LAPTOP_FILE, -1);
  390. if (!eeepc->platform_device)
  391. return -ENOMEM;
  392. platform_set_drvdata(eeepc->platform_device, eeepc);
  393. result = platform_device_add(eeepc->platform_device);
  394. if (result)
  395. goto fail_platform_device;
  396. result = sysfs_create_group(&eeepc->platform_device->dev.kobj,
  397. &platform_attribute_group);
  398. if (result)
  399. goto fail_sysfs;
  400. return 0;
  401. fail_sysfs:
  402. platform_device_del(eeepc->platform_device);
  403. fail_platform_device:
  404. platform_device_put(eeepc->platform_device);
  405. return result;
  406. }
  407. static void eeepc_platform_exit(struct eeepc_laptop *eeepc)
  408. {
  409. sysfs_remove_group(&eeepc->platform_device->dev.kobj,
  410. &platform_attribute_group);
  411. platform_device_unregister(eeepc->platform_device);
  412. }
  413. /*
  414. * LEDs
  415. */
  416. /*
  417. * These functions actually update the LED's, and are called from a
  418. * workqueue. By doing this as separate work rather than when the LED
  419. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  420. * potentially bad time, such as a timer interrupt.
  421. */
  422. static void tpd_led_update(struct work_struct *work)
  423. {
  424. struct eeepc_laptop *eeepc;
  425. eeepc = container_of(work, struct eeepc_laptop, tpd_led_work);
  426. set_acpi(eeepc, CM_ASL_TPD, eeepc->tpd_led_wk);
  427. }
  428. static void tpd_led_set(struct led_classdev *led_cdev,
  429. enum led_brightness value)
  430. {
  431. struct eeepc_laptop *eeepc;
  432. eeepc = container_of(led_cdev, struct eeepc_laptop, tpd_led);
  433. eeepc->tpd_led_wk = (value > 0) ? 1 : 0;
  434. queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
  435. }
  436. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  437. {
  438. struct eeepc_laptop *eeepc;
  439. eeepc = container_of(led_cdev, struct eeepc_laptop, tpd_led);
  440. return get_acpi(eeepc, CM_ASL_TPD);
  441. }
  442. static int eeepc_led_init(struct eeepc_laptop *eeepc)
  443. {
  444. int rv;
  445. if (get_acpi(eeepc, CM_ASL_TPD) == -ENODEV)
  446. return 0;
  447. eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
  448. if (!eeepc->led_workqueue)
  449. return -ENOMEM;
  450. INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
  451. eeepc->tpd_led.name = "eeepc::touchpad";
  452. eeepc->tpd_led.brightness_set = tpd_led_set;
  453. if (get_acpi(eeepc, CM_ASL_TPD) >= 0) /* if method is available */
  454. eeepc->tpd_led.brightness_get = tpd_led_get;
  455. eeepc->tpd_led.max_brightness = 1;
  456. rv = led_classdev_register(&eeepc->platform_device->dev,
  457. &eeepc->tpd_led);
  458. if (rv) {
  459. destroy_workqueue(eeepc->led_workqueue);
  460. return rv;
  461. }
  462. return 0;
  463. }
  464. static void eeepc_led_exit(struct eeepc_laptop *eeepc)
  465. {
  466. if (!IS_ERR_OR_NULL(eeepc->tpd_led.dev))
  467. led_classdev_unregister(&eeepc->tpd_led);
  468. if (eeepc->led_workqueue)
  469. destroy_workqueue(eeepc->led_workqueue);
  470. }
  471. /*
  472. * PCI hotplug (for wlan rfkill)
  473. */
  474. static bool eeepc_wlan_rfkill_blocked(struct eeepc_laptop *eeepc)
  475. {
  476. if (get_acpi(eeepc, CM_ASL_WLAN) == 1)
  477. return false;
  478. return true;
  479. }
  480. static void eeepc_rfkill_hotplug(struct eeepc_laptop *eeepc, acpi_handle handle)
  481. {
  482. struct pci_dev *port;
  483. struct pci_dev *dev;
  484. struct pci_bus *bus;
  485. bool blocked = eeepc_wlan_rfkill_blocked(eeepc);
  486. bool absent;
  487. u32 l;
  488. if (eeepc->wlan_rfkill)
  489. rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
  490. mutex_lock(&eeepc->hotplug_lock);
  491. pci_lock_rescan_remove();
  492. if (!eeepc->hotplug_slot)
  493. goto out_unlock;
  494. port = acpi_get_pci_dev(handle);
  495. if (!port) {
  496. pr_warning("Unable to find port\n");
  497. goto out_unlock;
  498. }
  499. bus = port->subordinate;
  500. if (!bus) {
  501. pr_warn("Unable to find PCI bus 1?\n");
  502. goto out_put_dev;
  503. }
  504. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  505. pr_err("Unable to read PCI config space?\n");
  506. goto out_put_dev;
  507. }
  508. absent = (l == 0xffffffff);
  509. if (blocked != absent) {
  510. pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
  511. blocked ? "blocked" : "unblocked",
  512. absent ? "absent" : "present");
  513. pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
  514. goto out_put_dev;
  515. }
  516. if (!blocked) {
  517. dev = pci_get_slot(bus, 0);
  518. if (dev) {
  519. /* Device already present */
  520. pci_dev_put(dev);
  521. goto out_put_dev;
  522. }
  523. dev = pci_scan_single_device(bus, 0);
  524. if (dev) {
  525. pci_bus_assign_resources(bus);
  526. pci_bus_add_device(dev);
  527. }
  528. } else {
  529. dev = pci_get_slot(bus, 0);
  530. if (dev) {
  531. pci_stop_and_remove_bus_device(dev);
  532. pci_dev_put(dev);
  533. }
  534. }
  535. out_put_dev:
  536. pci_dev_put(port);
  537. out_unlock:
  538. pci_unlock_rescan_remove();
  539. mutex_unlock(&eeepc->hotplug_lock);
  540. }
  541. static void eeepc_rfkill_hotplug_update(struct eeepc_laptop *eeepc, char *node)
  542. {
  543. acpi_status status = AE_OK;
  544. acpi_handle handle;
  545. status = acpi_get_handle(NULL, node, &handle);
  546. if (ACPI_SUCCESS(status))
  547. eeepc_rfkill_hotplug(eeepc, handle);
  548. }
  549. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  550. {
  551. struct eeepc_laptop *eeepc = data;
  552. if (event != ACPI_NOTIFY_BUS_CHECK)
  553. return;
  554. eeepc_rfkill_hotplug(eeepc, handle);
  555. }
  556. static int eeepc_register_rfkill_notifier(struct eeepc_laptop *eeepc,
  557. char *node)
  558. {
  559. acpi_status status;
  560. acpi_handle handle;
  561. status = acpi_get_handle(NULL, node, &handle);
  562. if (ACPI_FAILURE(status))
  563. return -ENODEV;
  564. status = acpi_install_notify_handler(handle,
  565. ACPI_SYSTEM_NOTIFY,
  566. eeepc_rfkill_notify,
  567. eeepc);
  568. if (ACPI_FAILURE(status))
  569. pr_warn("Failed to register notify on %s\n", node);
  570. /*
  571. * Refresh pci hotplug in case the rfkill state was
  572. * changed during setup.
  573. */
  574. eeepc_rfkill_hotplug(eeepc, handle);
  575. return 0;
  576. }
  577. static void eeepc_unregister_rfkill_notifier(struct eeepc_laptop *eeepc,
  578. char *node)
  579. {
  580. acpi_status status = AE_OK;
  581. acpi_handle handle;
  582. status = acpi_get_handle(NULL, node, &handle);
  583. if (ACPI_FAILURE(status))
  584. return;
  585. status = acpi_remove_notify_handler(handle,
  586. ACPI_SYSTEM_NOTIFY,
  587. eeepc_rfkill_notify);
  588. if (ACPI_FAILURE(status))
  589. pr_err("Error removing rfkill notify handler %s\n",
  590. node);
  591. /*
  592. * Refresh pci hotplug in case the rfkill
  593. * state was changed after
  594. * eeepc_unregister_rfkill_notifier()
  595. */
  596. eeepc_rfkill_hotplug(eeepc, handle);
  597. }
  598. static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
  599. u8 *value)
  600. {
  601. struct eeepc_laptop *eeepc = hotplug_slot->private;
  602. int val = get_acpi(eeepc, CM_ASL_WLAN);
  603. if (val == 1 || val == 0)
  604. *value = val;
  605. else
  606. return -EINVAL;
  607. return 0;
  608. }
  609. static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  610. {
  611. kfree(hotplug_slot->info);
  612. kfree(hotplug_slot);
  613. }
  614. static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
  615. .owner = THIS_MODULE,
  616. .get_adapter_status = eeepc_get_adapter_status,
  617. .get_power_status = eeepc_get_adapter_status,
  618. };
  619. static int eeepc_setup_pci_hotplug(struct eeepc_laptop *eeepc)
  620. {
  621. int ret = -ENOMEM;
  622. struct pci_bus *bus = pci_find_bus(0, 1);
  623. if (!bus) {
  624. pr_err("Unable to find wifi PCI bus\n");
  625. return -ENODEV;
  626. }
  627. eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  628. if (!eeepc->hotplug_slot)
  629. goto error_slot;
  630. eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  631. GFP_KERNEL);
  632. if (!eeepc->hotplug_slot->info)
  633. goto error_info;
  634. eeepc->hotplug_slot->private = eeepc;
  635. eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
  636. eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
  637. eeepc_get_adapter_status(eeepc->hotplug_slot,
  638. &eeepc->hotplug_slot->info->adapter_status);
  639. ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
  640. if (ret) {
  641. pr_err("Unable to register hotplug slot - %d\n", ret);
  642. goto error_register;
  643. }
  644. return 0;
  645. error_register:
  646. kfree(eeepc->hotplug_slot->info);
  647. error_info:
  648. kfree(eeepc->hotplug_slot);
  649. eeepc->hotplug_slot = NULL;
  650. error_slot:
  651. return ret;
  652. }
  653. /*
  654. * Rfkill devices
  655. */
  656. static int eeepc_rfkill_set(void *data, bool blocked)
  657. {
  658. acpi_handle handle = data;
  659. return write_acpi_int(handle, NULL, !blocked);
  660. }
  661. static const struct rfkill_ops eeepc_rfkill_ops = {
  662. .set_block = eeepc_rfkill_set,
  663. };
  664. static int eeepc_new_rfkill(struct eeepc_laptop *eeepc,
  665. struct rfkill **rfkill,
  666. const char *name,
  667. enum rfkill_type type, int cm)
  668. {
  669. acpi_handle handle;
  670. int result;
  671. result = acpi_setter_handle(eeepc, cm, &handle);
  672. if (result < 0)
  673. return result;
  674. *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
  675. &eeepc_rfkill_ops, handle);
  676. if (!*rfkill)
  677. return -EINVAL;
  678. rfkill_init_sw_state(*rfkill, get_acpi(eeepc, cm) != 1);
  679. result = rfkill_register(*rfkill);
  680. if (result) {
  681. rfkill_destroy(*rfkill);
  682. *rfkill = NULL;
  683. return result;
  684. }
  685. return 0;
  686. }
  687. static char EEEPC_RFKILL_NODE_1[] = "\\_SB.PCI0.P0P5";
  688. static char EEEPC_RFKILL_NODE_2[] = "\\_SB.PCI0.P0P6";
  689. static char EEEPC_RFKILL_NODE_3[] = "\\_SB.PCI0.P0P7";
  690. static void eeepc_rfkill_exit(struct eeepc_laptop *eeepc)
  691. {
  692. eeepc_unregister_rfkill_notifier(eeepc, EEEPC_RFKILL_NODE_1);
  693. eeepc_unregister_rfkill_notifier(eeepc, EEEPC_RFKILL_NODE_2);
  694. eeepc_unregister_rfkill_notifier(eeepc, EEEPC_RFKILL_NODE_3);
  695. if (eeepc->wlan_rfkill) {
  696. rfkill_unregister(eeepc->wlan_rfkill);
  697. rfkill_destroy(eeepc->wlan_rfkill);
  698. eeepc->wlan_rfkill = NULL;
  699. }
  700. if (eeepc->hotplug_slot)
  701. pci_hp_deregister(eeepc->hotplug_slot);
  702. if (eeepc->bluetooth_rfkill) {
  703. rfkill_unregister(eeepc->bluetooth_rfkill);
  704. rfkill_destroy(eeepc->bluetooth_rfkill);
  705. eeepc->bluetooth_rfkill = NULL;
  706. }
  707. if (eeepc->wwan3g_rfkill) {
  708. rfkill_unregister(eeepc->wwan3g_rfkill);
  709. rfkill_destroy(eeepc->wwan3g_rfkill);
  710. eeepc->wwan3g_rfkill = NULL;
  711. }
  712. if (eeepc->wimax_rfkill) {
  713. rfkill_unregister(eeepc->wimax_rfkill);
  714. rfkill_destroy(eeepc->wimax_rfkill);
  715. eeepc->wimax_rfkill = NULL;
  716. }
  717. }
  718. static int eeepc_rfkill_init(struct eeepc_laptop *eeepc)
  719. {
  720. int result = 0;
  721. mutex_init(&eeepc->hotplug_lock);
  722. result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
  723. "eeepc-wlan", RFKILL_TYPE_WLAN,
  724. CM_ASL_WLAN);
  725. if (result && result != -ENODEV)
  726. goto exit;
  727. result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
  728. "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
  729. CM_ASL_BLUETOOTH);
  730. if (result && result != -ENODEV)
  731. goto exit;
  732. result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
  733. "eeepc-wwan3g", RFKILL_TYPE_WWAN,
  734. CM_ASL_3G);
  735. if (result && result != -ENODEV)
  736. goto exit;
  737. result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
  738. "eeepc-wimax", RFKILL_TYPE_WIMAX,
  739. CM_ASL_WIMAX);
  740. if (result && result != -ENODEV)
  741. goto exit;
  742. if (eeepc->hotplug_disabled)
  743. return 0;
  744. result = eeepc_setup_pci_hotplug(eeepc);
  745. /*
  746. * If we get -EBUSY then something else is handling the PCI hotplug -
  747. * don't fail in this case
  748. */
  749. if (result == -EBUSY)
  750. result = 0;
  751. eeepc_register_rfkill_notifier(eeepc, EEEPC_RFKILL_NODE_1);
  752. eeepc_register_rfkill_notifier(eeepc, EEEPC_RFKILL_NODE_2);
  753. eeepc_register_rfkill_notifier(eeepc, EEEPC_RFKILL_NODE_3);
  754. exit:
  755. if (result && result != -ENODEV)
  756. eeepc_rfkill_exit(eeepc);
  757. return result;
  758. }
  759. /*
  760. * Platform driver - hibernate/resume callbacks
  761. */
  762. static int eeepc_hotk_thaw(struct device *device)
  763. {
  764. struct eeepc_laptop *eeepc = dev_get_drvdata(device);
  765. if (eeepc->wlan_rfkill) {
  766. int wlan;
  767. /*
  768. * Work around bios bug - acpi _PTS turns off the wireless led
  769. * during suspend. Normally it restores it on resume, but
  770. * we should kick it ourselves in case hibernation is aborted.
  771. */
  772. wlan = get_acpi(eeepc, CM_ASL_WLAN);
  773. if (wlan >= 0)
  774. set_acpi(eeepc, CM_ASL_WLAN, wlan);
  775. }
  776. return 0;
  777. }
  778. static int eeepc_hotk_restore(struct device *device)
  779. {
  780. struct eeepc_laptop *eeepc = dev_get_drvdata(device);
  781. /* Refresh both wlan rfkill state and pci hotplug */
  782. if (eeepc->wlan_rfkill) {
  783. eeepc_rfkill_hotplug_update(eeepc, EEEPC_RFKILL_NODE_1);
  784. eeepc_rfkill_hotplug_update(eeepc, EEEPC_RFKILL_NODE_2);
  785. eeepc_rfkill_hotplug_update(eeepc, EEEPC_RFKILL_NODE_3);
  786. }
  787. if (eeepc->bluetooth_rfkill)
  788. rfkill_set_sw_state(eeepc->bluetooth_rfkill,
  789. get_acpi(eeepc, CM_ASL_BLUETOOTH) != 1);
  790. if (eeepc->wwan3g_rfkill)
  791. rfkill_set_sw_state(eeepc->wwan3g_rfkill,
  792. get_acpi(eeepc, CM_ASL_3G) != 1);
  793. if (eeepc->wimax_rfkill)
  794. rfkill_set_sw_state(eeepc->wimax_rfkill,
  795. get_acpi(eeepc, CM_ASL_WIMAX) != 1);
  796. return 0;
  797. }
  798. static const struct dev_pm_ops eeepc_pm_ops = {
  799. .thaw = eeepc_hotk_thaw,
  800. .restore = eeepc_hotk_restore,
  801. };
  802. static struct platform_driver platform_driver = {
  803. .driver = {
  804. .name = EEEPC_LAPTOP_FILE,
  805. .pm = &eeepc_pm_ops,
  806. }
  807. };
  808. /*
  809. * Hwmon device
  810. */
  811. #define EEEPC_EC_SC00 0x61
  812. #define EEEPC_EC_FAN_PWM (EEEPC_EC_SC00 + 2) /* Fan PWM duty cycle (%) */
  813. #define EEEPC_EC_FAN_HRPM (EEEPC_EC_SC00 + 5) /* High byte, fan speed (RPM) */
  814. #define EEEPC_EC_FAN_LRPM (EEEPC_EC_SC00 + 6) /* Low byte, fan speed (RPM) */
  815. #define EEEPC_EC_SFB0 0xD0
  816. #define EEEPC_EC_FAN_CTRL (EEEPC_EC_SFB0 + 3) /* Byte containing SF25 */
  817. static inline int eeepc_pwm_to_lmsensors(int value)
  818. {
  819. return value * 255 / 100;
  820. }
  821. static inline int eeepc_lmsensors_to_pwm(int value)
  822. {
  823. value = clamp_val(value, 0, 255);
  824. return value * 100 / 255;
  825. }
  826. static int eeepc_get_fan_pwm(void)
  827. {
  828. u8 value = 0;
  829. ec_read(EEEPC_EC_FAN_PWM, &value);
  830. return eeepc_pwm_to_lmsensors(value);
  831. }
  832. static void eeepc_set_fan_pwm(int value)
  833. {
  834. value = eeepc_lmsensors_to_pwm(value);
  835. ec_write(EEEPC_EC_FAN_PWM, value);
  836. }
  837. static int eeepc_get_fan_rpm(void)
  838. {
  839. u8 high = 0;
  840. u8 low = 0;
  841. ec_read(EEEPC_EC_FAN_HRPM, &high);
  842. ec_read(EEEPC_EC_FAN_LRPM, &low);
  843. return high << 8 | low;
  844. }
  845. #define EEEPC_EC_FAN_CTRL_BIT 0x02
  846. #define EEEPC_FAN_CTRL_MANUAL 1
  847. #define EEEPC_FAN_CTRL_AUTO 2
  848. static int eeepc_get_fan_ctrl(void)
  849. {
  850. u8 value = 0;
  851. ec_read(EEEPC_EC_FAN_CTRL, &value);
  852. if (value & EEEPC_EC_FAN_CTRL_BIT)
  853. return EEEPC_FAN_CTRL_MANUAL;
  854. else
  855. return EEEPC_FAN_CTRL_AUTO;
  856. }
  857. static void eeepc_set_fan_ctrl(int manual)
  858. {
  859. u8 value = 0;
  860. ec_read(EEEPC_EC_FAN_CTRL, &value);
  861. if (manual == EEEPC_FAN_CTRL_MANUAL)
  862. value |= EEEPC_EC_FAN_CTRL_BIT;
  863. else
  864. value &= ~EEEPC_EC_FAN_CTRL_BIT;
  865. ec_write(EEEPC_EC_FAN_CTRL, value);
  866. }
  867. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  868. {
  869. int rv, value;
  870. rv = parse_arg(buf, &value);
  871. if (rv < 0)
  872. return rv;
  873. set(value);
  874. return count;
  875. }
  876. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  877. {
  878. return sprintf(buf, "%d\n", get());
  879. }
  880. #define EEEPC_SENSOR_SHOW_FUNC(_name, _get) \
  881. static ssize_t _name##_show(struct device *dev, \
  882. struct device_attribute *attr, \
  883. char *buf) \
  884. { \
  885. return show_sys_hwmon(_get, buf); \
  886. }
  887. #define EEEPC_SENSOR_STORE_FUNC(_name, _set) \
  888. static ssize_t _name##_store(struct device *dev, \
  889. struct device_attribute *attr, \
  890. const char *buf, size_t count) \
  891. { \
  892. return store_sys_hwmon(_set, buf, count); \
  893. }
  894. #define EEEPC_CREATE_SENSOR_ATTR_RW(_name, _get, _set) \
  895. EEEPC_SENSOR_SHOW_FUNC(_name, _get) \
  896. EEEPC_SENSOR_STORE_FUNC(_name, _set) \
  897. static DEVICE_ATTR_RW(_name)
  898. #define EEEPC_CREATE_SENSOR_ATTR_RO(_name, _get) \
  899. EEEPC_SENSOR_SHOW_FUNC(_name, _get) \
  900. static DEVICE_ATTR_RO(_name)
  901. EEEPC_CREATE_SENSOR_ATTR_RO(fan1_input, eeepc_get_fan_rpm);
  902. EEEPC_CREATE_SENSOR_ATTR_RW(pwm1, eeepc_get_fan_pwm,
  903. eeepc_set_fan_pwm);
  904. EEEPC_CREATE_SENSOR_ATTR_RW(pwm1_enable, eeepc_get_fan_ctrl,
  905. eeepc_set_fan_ctrl);
  906. static struct attribute *hwmon_attrs[] = {
  907. &dev_attr_pwm1.attr,
  908. &dev_attr_fan1_input.attr,
  909. &dev_attr_pwm1_enable.attr,
  910. NULL
  911. };
  912. ATTRIBUTE_GROUPS(hwmon);
  913. static int eeepc_hwmon_init(struct eeepc_laptop *eeepc)
  914. {
  915. struct device *dev = &eeepc->platform_device->dev;
  916. struct device *hwmon;
  917. hwmon = devm_hwmon_device_register_with_groups(dev, "eeepc", NULL,
  918. hwmon_groups);
  919. if (IS_ERR(hwmon)) {
  920. pr_err("Could not register eeepc hwmon device\n");
  921. return PTR_ERR(hwmon);
  922. }
  923. return 0;
  924. }
  925. /*
  926. * Backlight device
  927. */
  928. static int read_brightness(struct backlight_device *bd)
  929. {
  930. struct eeepc_laptop *eeepc = bl_get_data(bd);
  931. return get_acpi(eeepc, CM_ASL_PANELBRIGHT);
  932. }
  933. static int set_brightness(struct backlight_device *bd, int value)
  934. {
  935. struct eeepc_laptop *eeepc = bl_get_data(bd);
  936. return set_acpi(eeepc, CM_ASL_PANELBRIGHT, value);
  937. }
  938. static int update_bl_status(struct backlight_device *bd)
  939. {
  940. return set_brightness(bd, bd->props.brightness);
  941. }
  942. static const struct backlight_ops eeepcbl_ops = {
  943. .get_brightness = read_brightness,
  944. .update_status = update_bl_status,
  945. };
  946. static int eeepc_backlight_notify(struct eeepc_laptop *eeepc)
  947. {
  948. struct backlight_device *bd = eeepc->backlight_device;
  949. int old = bd->props.brightness;
  950. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  951. return old;
  952. }
  953. static int eeepc_backlight_init(struct eeepc_laptop *eeepc)
  954. {
  955. struct backlight_properties props;
  956. struct backlight_device *bd;
  957. memset(&props, 0, sizeof(struct backlight_properties));
  958. props.type = BACKLIGHT_PLATFORM;
  959. props.max_brightness = 15;
  960. bd = backlight_device_register(EEEPC_LAPTOP_FILE,
  961. &eeepc->platform_device->dev, eeepc,
  962. &eeepcbl_ops, &props);
  963. if (IS_ERR(bd)) {
  964. pr_err("Could not register eeepc backlight device\n");
  965. eeepc->backlight_device = NULL;
  966. return PTR_ERR(bd);
  967. }
  968. eeepc->backlight_device = bd;
  969. bd->props.brightness = read_brightness(bd);
  970. bd->props.power = FB_BLANK_UNBLANK;
  971. backlight_update_status(bd);
  972. return 0;
  973. }
  974. static void eeepc_backlight_exit(struct eeepc_laptop *eeepc)
  975. {
  976. backlight_device_unregister(eeepc->backlight_device);
  977. eeepc->backlight_device = NULL;
  978. }
  979. /*
  980. * Input device (i.e. hotkeys)
  981. */
  982. static int eeepc_input_init(struct eeepc_laptop *eeepc)
  983. {
  984. struct input_dev *input;
  985. int error;
  986. input = input_allocate_device();
  987. if (!input)
  988. return -ENOMEM;
  989. input->name = "Asus EeePC extra buttons";
  990. input->phys = EEEPC_LAPTOP_FILE "/input0";
  991. input->id.bustype = BUS_HOST;
  992. input->dev.parent = &eeepc->platform_device->dev;
  993. error = sparse_keymap_setup(input, eeepc_keymap, NULL);
  994. if (error) {
  995. pr_err("Unable to setup input device keymap\n");
  996. goto err_free_dev;
  997. }
  998. error = input_register_device(input);
  999. if (error) {
  1000. pr_err("Unable to register input device\n");
  1001. goto err_free_keymap;
  1002. }
  1003. eeepc->inputdev = input;
  1004. return 0;
  1005. err_free_keymap:
  1006. sparse_keymap_free(input);
  1007. err_free_dev:
  1008. input_free_device(input);
  1009. return error;
  1010. }
  1011. static void eeepc_input_exit(struct eeepc_laptop *eeepc)
  1012. {
  1013. if (eeepc->inputdev) {
  1014. sparse_keymap_free(eeepc->inputdev);
  1015. input_unregister_device(eeepc->inputdev);
  1016. }
  1017. eeepc->inputdev = NULL;
  1018. }
  1019. /*
  1020. * ACPI driver
  1021. */
  1022. static void eeepc_input_notify(struct eeepc_laptop *eeepc, int event)
  1023. {
  1024. if (!eeepc->inputdev)
  1025. return;
  1026. if (!sparse_keymap_report_event(eeepc->inputdev, event, 1, true))
  1027. pr_info("Unknown key %x pressed\n", event);
  1028. }
  1029. static void eeepc_acpi_notify(struct acpi_device *device, u32 event)
  1030. {
  1031. struct eeepc_laptop *eeepc = acpi_driver_data(device);
  1032. int old_brightness, new_brightness;
  1033. u16 count;
  1034. if (event > ACPI_MAX_SYS_NOTIFY)
  1035. return;
  1036. count = eeepc->event_count[event % 128]++;
  1037. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1038. dev_name(&device->dev), event,
  1039. count);
  1040. /* Brightness events are special */
  1041. if (event < NOTIFY_BRN_MIN || event > NOTIFY_BRN_MAX) {
  1042. eeepc_input_notify(eeepc, event);
  1043. return;
  1044. }
  1045. /* Ignore them completely if the acpi video driver is used */
  1046. if (!eeepc->backlight_device)
  1047. return;
  1048. /* Update the backlight device. */
  1049. old_brightness = eeepc_backlight_notify(eeepc);
  1050. /* Convert event to keypress (obsolescent hack) */
  1051. new_brightness = event - NOTIFY_BRN_MIN;
  1052. if (new_brightness < old_brightness) {
  1053. event = NOTIFY_BRN_MIN; /* brightness down */
  1054. } else if (new_brightness > old_brightness) {
  1055. event = NOTIFY_BRN_MAX; /* brightness up */
  1056. } else {
  1057. /*
  1058. * no change in brightness - already at min/max,
  1059. * event will be desired value (or else ignored)
  1060. */
  1061. }
  1062. eeepc_input_notify(eeepc, event);
  1063. }
  1064. static void eeepc_dmi_check(struct eeepc_laptop *eeepc)
  1065. {
  1066. const char *model;
  1067. model = dmi_get_system_info(DMI_PRODUCT_NAME);
  1068. if (!model)
  1069. return;
  1070. /*
  1071. * Blacklist for setting cpufv (cpu speed).
  1072. *
  1073. * EeePC 4G ("701") implements CFVS, but it is not supported
  1074. * by the pre-installed OS, and the original option to change it
  1075. * in the BIOS setup screen was removed in later versions.
  1076. *
  1077. * Judging by the lack of "Super Hybrid Engine" on Asus product pages,
  1078. * this applies to all "701" models (4G/4G Surf/2G Surf).
  1079. *
  1080. * So Asus made a deliberate decision not to support it on this model.
  1081. * We have several reports that using it can cause the system to hang
  1082. *
  1083. * The hang has also been reported on a "702" (Model name "8G"?).
  1084. *
  1085. * We avoid dmi_check_system() / dmi_match(), because they use
  1086. * substring matching. We don't want to affect the "701SD"
  1087. * and "701SDX" models, because they do support S.H.E.
  1088. */
  1089. if (strcmp(model, "701") == 0 || strcmp(model, "702") == 0) {
  1090. eeepc->cpufv_disabled = true;
  1091. pr_info("model %s does not officially support setting cpu speed\n",
  1092. model);
  1093. pr_info("cpufv disabled to avoid instability\n");
  1094. }
  1095. /*
  1096. * Blacklist for wlan hotplug
  1097. *
  1098. * Eeepc 1005HA doesn't work like others models and don't need the
  1099. * hotplug code. In fact, current hotplug code seems to unplug another
  1100. * device...
  1101. */
  1102. if (strcmp(model, "1005HA") == 0 || strcmp(model, "1201N") == 0 ||
  1103. strcmp(model, "1005PE") == 0) {
  1104. eeepc->hotplug_disabled = true;
  1105. pr_info("wlan hotplug disabled\n");
  1106. }
  1107. }
  1108. static void cmsg_quirk(struct eeepc_laptop *eeepc, int cm, const char *name)
  1109. {
  1110. int dummy;
  1111. /* Some BIOSes do not report cm although it is available.
  1112. Check if cm_getv[cm] works and, if yes, assume cm should be set. */
  1113. if (!(eeepc->cm_supported & (1 << cm))
  1114. && !read_acpi_int(eeepc->handle, cm_getv[cm], &dummy)) {
  1115. pr_info("%s (%x) not reported by BIOS, enabling anyway\n",
  1116. name, 1 << cm);
  1117. eeepc->cm_supported |= 1 << cm;
  1118. }
  1119. }
  1120. static void cmsg_quirks(struct eeepc_laptop *eeepc)
  1121. {
  1122. cmsg_quirk(eeepc, CM_ASL_LID, "LID");
  1123. cmsg_quirk(eeepc, CM_ASL_TYPE, "TYPE");
  1124. cmsg_quirk(eeepc, CM_ASL_PANELPOWER, "PANELPOWER");
  1125. cmsg_quirk(eeepc, CM_ASL_TPD, "TPD");
  1126. }
  1127. static int eeepc_acpi_init(struct eeepc_laptop *eeepc)
  1128. {
  1129. unsigned int init_flags;
  1130. int result;
  1131. result = acpi_bus_get_status(eeepc->device);
  1132. if (result)
  1133. return result;
  1134. if (!eeepc->device->status.present) {
  1135. pr_err("Hotkey device not present, aborting\n");
  1136. return -ENODEV;
  1137. }
  1138. init_flags = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  1139. pr_notice("Hotkey init flags 0x%x\n", init_flags);
  1140. if (write_acpi_int(eeepc->handle, "INIT", init_flags)) {
  1141. pr_err("Hotkey initialization failed\n");
  1142. return -ENODEV;
  1143. }
  1144. /* get control methods supported */
  1145. if (read_acpi_int(eeepc->handle, "CMSG", &eeepc->cm_supported)) {
  1146. pr_err("Get control methods supported failed\n");
  1147. return -ENODEV;
  1148. }
  1149. cmsg_quirks(eeepc);
  1150. pr_info("Get control methods supported: 0x%x\n", eeepc->cm_supported);
  1151. return 0;
  1152. }
  1153. static void eeepc_enable_camera(struct eeepc_laptop *eeepc)
  1154. {
  1155. /*
  1156. * If the following call to set_acpi() fails, it's because there's no
  1157. * camera so we can ignore the error.
  1158. */
  1159. if (get_acpi(eeepc, CM_ASL_CAMERA) == 0)
  1160. set_acpi(eeepc, CM_ASL_CAMERA, 1);
  1161. }
  1162. static bool eeepc_device_present;
  1163. static int eeepc_acpi_add(struct acpi_device *device)
  1164. {
  1165. struct eeepc_laptop *eeepc;
  1166. int result;
  1167. pr_notice(EEEPC_LAPTOP_NAME "\n");
  1168. eeepc = kzalloc(sizeof(struct eeepc_laptop), GFP_KERNEL);
  1169. if (!eeepc)
  1170. return -ENOMEM;
  1171. eeepc->handle = device->handle;
  1172. strcpy(acpi_device_name(device), EEEPC_ACPI_DEVICE_NAME);
  1173. strcpy(acpi_device_class(device), EEEPC_ACPI_CLASS);
  1174. device->driver_data = eeepc;
  1175. eeepc->device = device;
  1176. eeepc->hotplug_disabled = hotplug_disabled;
  1177. eeepc_dmi_check(eeepc);
  1178. result = eeepc_acpi_init(eeepc);
  1179. if (result)
  1180. goto fail_platform;
  1181. eeepc_enable_camera(eeepc);
  1182. /*
  1183. * Register the platform device first. It is used as a parent for the
  1184. * sub-devices below.
  1185. *
  1186. * Note that if there are multiple instances of this ACPI device it
  1187. * will bail out, because the platform device is registered with a
  1188. * fixed name. Of course it doesn't make sense to have more than one,
  1189. * and machine-specific scripts find the fixed name convenient. But
  1190. * It's also good for us to exclude multiple instances because both
  1191. * our hwmon and our wlan rfkill subdevice use global ACPI objects
  1192. * (the EC and the wlan PCI slot respectively).
  1193. */
  1194. result = eeepc_platform_init(eeepc);
  1195. if (result)
  1196. goto fail_platform;
  1197. if (!acpi_video_backlight_support()) {
  1198. result = eeepc_backlight_init(eeepc);
  1199. if (result)
  1200. goto fail_backlight;
  1201. } else {
  1202. pr_info("Backlight controlled by ACPI video driver\n");
  1203. }
  1204. result = eeepc_input_init(eeepc);
  1205. if (result)
  1206. goto fail_input;
  1207. result = eeepc_hwmon_init(eeepc);
  1208. if (result)
  1209. goto fail_hwmon;
  1210. result = eeepc_led_init(eeepc);
  1211. if (result)
  1212. goto fail_led;
  1213. result = eeepc_rfkill_init(eeepc);
  1214. if (result)
  1215. goto fail_rfkill;
  1216. eeepc_device_present = true;
  1217. return 0;
  1218. fail_rfkill:
  1219. eeepc_led_exit(eeepc);
  1220. fail_led:
  1221. fail_hwmon:
  1222. eeepc_input_exit(eeepc);
  1223. fail_input:
  1224. eeepc_backlight_exit(eeepc);
  1225. fail_backlight:
  1226. eeepc_platform_exit(eeepc);
  1227. fail_platform:
  1228. kfree(eeepc);
  1229. return result;
  1230. }
  1231. static int eeepc_acpi_remove(struct acpi_device *device)
  1232. {
  1233. struct eeepc_laptop *eeepc = acpi_driver_data(device);
  1234. eeepc_backlight_exit(eeepc);
  1235. eeepc_rfkill_exit(eeepc);
  1236. eeepc_input_exit(eeepc);
  1237. eeepc_led_exit(eeepc);
  1238. eeepc_platform_exit(eeepc);
  1239. kfree(eeepc);
  1240. return 0;
  1241. }
  1242. static const struct acpi_device_id eeepc_device_ids[] = {
  1243. {EEEPC_ACPI_HID, 0},
  1244. {"", 0},
  1245. };
  1246. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  1247. static struct acpi_driver eeepc_acpi_driver = {
  1248. .name = EEEPC_LAPTOP_NAME,
  1249. .class = EEEPC_ACPI_CLASS,
  1250. .owner = THIS_MODULE,
  1251. .ids = eeepc_device_ids,
  1252. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1253. .ops = {
  1254. .add = eeepc_acpi_add,
  1255. .remove = eeepc_acpi_remove,
  1256. .notify = eeepc_acpi_notify,
  1257. },
  1258. };
  1259. static int __init eeepc_laptop_init(void)
  1260. {
  1261. int result;
  1262. result = platform_driver_register(&platform_driver);
  1263. if (result < 0)
  1264. return result;
  1265. result = acpi_bus_register_driver(&eeepc_acpi_driver);
  1266. if (result < 0)
  1267. goto fail_acpi_driver;
  1268. if (!eeepc_device_present) {
  1269. result = -ENODEV;
  1270. goto fail_no_device;
  1271. }
  1272. return 0;
  1273. fail_no_device:
  1274. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1275. fail_acpi_driver:
  1276. platform_driver_unregister(&platform_driver);
  1277. return result;
  1278. }
  1279. static void __exit eeepc_laptop_exit(void)
  1280. {
  1281. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1282. platform_driver_unregister(&platform_driver);
  1283. }
  1284. module_init(eeepc_laptop_init);
  1285. module_exit(eeepc_laptop_exit);