eeepc-laptop.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. * eepc-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 <acpi/acpi_drivers.h>
  29. #include <acpi/acpi_bus.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/input.h>
  32. #include <linux/rfkill.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci_hotplug.h>
  35. #define EEEPC_LAPTOP_VERSION "0.1"
  36. #define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
  37. #define EEEPC_HOTK_FILE "eeepc"
  38. #define EEEPC_HOTK_CLASS "hotkey"
  39. #define EEEPC_HOTK_DEVICE_NAME "Hotkey"
  40. #define EEEPC_HOTK_HID "ASUS010"
  41. /*
  42. * Definitions for Asus EeePC
  43. */
  44. #define NOTIFY_WLAN_ON 0x10
  45. #define NOTIFY_BRN_MIN 0x20
  46. #define NOTIFY_BRN_MAX 0x2f
  47. enum {
  48. DISABLE_ASL_WLAN = 0x0001,
  49. DISABLE_ASL_BLUETOOTH = 0x0002,
  50. DISABLE_ASL_IRDA = 0x0004,
  51. DISABLE_ASL_CAMERA = 0x0008,
  52. DISABLE_ASL_TV = 0x0010,
  53. DISABLE_ASL_GPS = 0x0020,
  54. DISABLE_ASL_DISPLAYSWITCH = 0x0040,
  55. DISABLE_ASL_MODEM = 0x0080,
  56. DISABLE_ASL_CARDREADER = 0x0100,
  57. DISABLE_ASL_3G = 0x0200,
  58. DISABLE_ASL_WIMAX = 0x0400,
  59. DISABLE_ASL_HWCF = 0x0800
  60. };
  61. enum {
  62. CM_ASL_WLAN = 0,
  63. CM_ASL_BLUETOOTH,
  64. CM_ASL_IRDA,
  65. CM_ASL_1394,
  66. CM_ASL_CAMERA,
  67. CM_ASL_TV,
  68. CM_ASL_GPS,
  69. CM_ASL_DVDROM,
  70. CM_ASL_DISPLAYSWITCH,
  71. CM_ASL_PANELBRIGHT,
  72. CM_ASL_BIOSFLASH,
  73. CM_ASL_ACPIFLASH,
  74. CM_ASL_CPUFV,
  75. CM_ASL_CPUTEMPERATURE,
  76. CM_ASL_FANCPU,
  77. CM_ASL_FANCHASSIS,
  78. CM_ASL_USBPORT1,
  79. CM_ASL_USBPORT2,
  80. CM_ASL_USBPORT3,
  81. CM_ASL_MODEM,
  82. CM_ASL_CARDREADER,
  83. CM_ASL_3G,
  84. CM_ASL_WIMAX,
  85. CM_ASL_HWCF,
  86. CM_ASL_LID,
  87. CM_ASL_TYPE,
  88. CM_ASL_PANELPOWER, /*P901*/
  89. CM_ASL_TPD
  90. };
  91. static const char *cm_getv[] = {
  92. "WLDG", "BTHG", NULL, NULL,
  93. "CAMG", NULL, NULL, NULL,
  94. NULL, "PBLG", NULL, NULL,
  95. "CFVG", NULL, NULL, NULL,
  96. "USBG", NULL, NULL, "MODG",
  97. "CRDG", "M3GG", "WIMG", "HWCF",
  98. "LIDG", "TYPE", "PBPG", "TPDG"
  99. };
  100. static const char *cm_setv[] = {
  101. "WLDS", "BTHS", NULL, NULL,
  102. "CAMS", NULL, NULL, NULL,
  103. "SDSP", "PBLS", "HDPS", NULL,
  104. "CFVS", NULL, NULL, NULL,
  105. "USBG", NULL, NULL, "MODS",
  106. "CRDS", "M3GS", "WIMS", NULL,
  107. NULL, NULL, "PBPS", "TPDS"
  108. };
  109. #define EEEPC_EC "\\_SB.PCI0.SBRG.EC0."
  110. #define EEEPC_EC_FAN_PWM EEEPC_EC "SC02" /* Fan PWM duty cycle (%) */
  111. #define EEEPC_EC_SC02 0x63
  112. #define EEEPC_EC_FAN_HRPM EEEPC_EC "SC05" /* High byte, fan speed (RPM) */
  113. #define EEEPC_EC_FAN_LRPM EEEPC_EC "SC06" /* Low byte, fan speed (RPM) */
  114. #define EEEPC_EC_FAN_CTRL EEEPC_EC "SFB3" /* Byte containing SF25 */
  115. #define EEEPC_EC_SFB3 0xD3
  116. /*
  117. * This is the main structure, we can use it to store useful information
  118. * about the hotk device
  119. */
  120. struct eeepc_hotk {
  121. struct acpi_device *device; /* the device we are in */
  122. acpi_handle handle; /* the handle of the hotk device */
  123. u32 cm_supported; /* the control methods supported
  124. by this BIOS */
  125. uint init_flag; /* Init flags */
  126. u16 event_count[128]; /* count for each event */
  127. struct input_dev *inputdev;
  128. u16 *keycode_map;
  129. struct rfkill *wlan_rfkill;
  130. struct rfkill *bluetooth_rfkill;
  131. struct hotplug_slot *hotplug_slot;
  132. };
  133. /* The actual device the driver binds to */
  134. static struct eeepc_hotk *ehotk;
  135. /* Platform device/driver */
  136. static struct platform_driver platform_driver = {
  137. .driver = {
  138. .name = EEEPC_HOTK_FILE,
  139. .owner = THIS_MODULE,
  140. }
  141. };
  142. static struct platform_device *platform_device;
  143. struct key_entry {
  144. char type;
  145. u8 code;
  146. u16 keycode;
  147. };
  148. enum { KE_KEY, KE_END };
  149. static struct key_entry eeepc_keymap[] = {
  150. /* Sleep already handled via generic ACPI code */
  151. {KE_KEY, 0x10, KEY_WLAN },
  152. {KE_KEY, 0x11, KEY_WLAN },
  153. {KE_KEY, 0x12, KEY_PROG1 },
  154. {KE_KEY, 0x13, KEY_MUTE },
  155. {KE_KEY, 0x14, KEY_VOLUMEDOWN },
  156. {KE_KEY, 0x15, KEY_VOLUMEUP },
  157. {KE_KEY, 0x1a, KEY_COFFEE },
  158. {KE_KEY, 0x1b, KEY_ZOOM },
  159. {KE_KEY, 0x1c, KEY_PROG2 },
  160. {KE_KEY, 0x1d, KEY_PROG3 },
  161. {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
  162. {KE_KEY, NOTIFY_BRN_MIN + 2, KEY_BRIGHTNESSUP },
  163. {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
  164. {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
  165. {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
  166. {KE_END, 0},
  167. };
  168. /*
  169. * The hotkey driver declaration
  170. */
  171. static int eeepc_hotk_add(struct acpi_device *device);
  172. static int eeepc_hotk_remove(struct acpi_device *device, int type);
  173. static int eeepc_hotk_resume(struct acpi_device *device);
  174. static void eeepc_hotk_notify(struct acpi_device *device, u32 event);
  175. static const struct acpi_device_id eeepc_device_ids[] = {
  176. {EEEPC_HOTK_HID, 0},
  177. {"", 0},
  178. };
  179. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  180. static struct acpi_driver eeepc_hotk_driver = {
  181. .name = EEEPC_HOTK_NAME,
  182. .class = EEEPC_HOTK_CLASS,
  183. .ids = eeepc_device_ids,
  184. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  185. .ops = {
  186. .add = eeepc_hotk_add,
  187. .remove = eeepc_hotk_remove,
  188. .resume = eeepc_hotk_resume,
  189. .notify = eeepc_hotk_notify,
  190. },
  191. };
  192. /* PCI hotplug ops */
  193. static int eeepc_get_adapter_status(struct hotplug_slot *slot, u8 *value);
  194. static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
  195. .owner = THIS_MODULE,
  196. .get_adapter_status = eeepc_get_adapter_status,
  197. .get_power_status = eeepc_get_adapter_status,
  198. };
  199. /* The backlight device /sys/class/backlight */
  200. static struct backlight_device *eeepc_backlight_device;
  201. /* The hwmon device */
  202. static struct device *eeepc_hwmon_device;
  203. /*
  204. * The backlight class declaration
  205. */
  206. static int read_brightness(struct backlight_device *bd);
  207. static int update_bl_status(struct backlight_device *bd);
  208. static struct backlight_ops eeepcbl_ops = {
  209. .get_brightness = read_brightness,
  210. .update_status = update_bl_status,
  211. };
  212. MODULE_AUTHOR("Corentin Chary, Eric Cooper");
  213. MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
  214. MODULE_LICENSE("GPL");
  215. /*
  216. * ACPI Helpers
  217. */
  218. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  219. struct acpi_buffer *output)
  220. {
  221. struct acpi_object_list params;
  222. union acpi_object in_obj;
  223. acpi_status status;
  224. params.count = 1;
  225. params.pointer = &in_obj;
  226. in_obj.type = ACPI_TYPE_INTEGER;
  227. in_obj.integer.value = val;
  228. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  229. return (status == AE_OK ? 0 : -1);
  230. }
  231. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  232. {
  233. acpi_status status;
  234. unsigned long long result;
  235. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  236. if (ACPI_FAILURE(status)) {
  237. *val = -1;
  238. return -1;
  239. } else {
  240. *val = result;
  241. return 0;
  242. }
  243. }
  244. static int set_acpi(int cm, int value)
  245. {
  246. if (ehotk->cm_supported & (0x1 << cm)) {
  247. const char *method = cm_setv[cm];
  248. if (method == NULL)
  249. return -ENODEV;
  250. if (write_acpi_int(ehotk->handle, method, value, NULL))
  251. pr_warning("Error writing %s\n", method);
  252. }
  253. return 0;
  254. }
  255. static int get_acpi(int cm)
  256. {
  257. int value = -ENODEV;
  258. if ((ehotk->cm_supported & (0x1 << cm))) {
  259. const char *method = cm_getv[cm];
  260. if (method == NULL)
  261. return -ENODEV;
  262. if (read_acpi_int(ehotk->handle, method, &value))
  263. pr_warning("Error reading %s\n", method);
  264. }
  265. return value;
  266. }
  267. /*
  268. * Backlight
  269. */
  270. static int read_brightness(struct backlight_device *bd)
  271. {
  272. return get_acpi(CM_ASL_PANELBRIGHT);
  273. }
  274. static int set_brightness(struct backlight_device *bd, int value)
  275. {
  276. value = max(0, min(15, value));
  277. return set_acpi(CM_ASL_PANELBRIGHT, value);
  278. }
  279. static int update_bl_status(struct backlight_device *bd)
  280. {
  281. return set_brightness(bd, bd->props.brightness);
  282. }
  283. /*
  284. * Rfkill helpers
  285. */
  286. static bool eeepc_wlan_rfkill_blocked(void)
  287. {
  288. if (get_acpi(CM_ASL_WLAN) == 1)
  289. return false;
  290. return true;
  291. }
  292. static int eeepc_rfkill_set(void *data, bool blocked)
  293. {
  294. unsigned long asl = (unsigned long)data;
  295. return set_acpi(asl, !blocked);
  296. }
  297. static const struct rfkill_ops eeepc_rfkill_ops = {
  298. .set_block = eeepc_rfkill_set,
  299. };
  300. static void __init eeepc_enable_camera(void)
  301. {
  302. /*
  303. * If the following call to set_acpi() fails, it's because there's no
  304. * camera so we can ignore the error.
  305. */
  306. set_acpi(CM_ASL_CAMERA, 1);
  307. }
  308. /*
  309. * Sys helpers
  310. */
  311. static int parse_arg(const char *buf, unsigned long count, int *val)
  312. {
  313. if (!count)
  314. return 0;
  315. if (sscanf(buf, "%i", val) != 1)
  316. return -EINVAL;
  317. return count;
  318. }
  319. static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
  320. {
  321. int rv, value;
  322. rv = parse_arg(buf, count, &value);
  323. if (rv > 0)
  324. value = set_acpi(cm, value);
  325. if (value < 0)
  326. return value;
  327. return rv;
  328. }
  329. static ssize_t show_sys_acpi(int cm, char *buf)
  330. {
  331. int value = get_acpi(cm);
  332. if (value < 0)
  333. return value;
  334. return sprintf(buf, "%d\n", value);
  335. }
  336. #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
  337. static ssize_t show_##_name(struct device *dev, \
  338. struct device_attribute *attr, \
  339. char *buf) \
  340. { \
  341. return show_sys_acpi(_cm, buf); \
  342. } \
  343. static ssize_t store_##_name(struct device *dev, \
  344. struct device_attribute *attr, \
  345. const char *buf, size_t count) \
  346. { \
  347. return store_sys_acpi(_cm, buf, count); \
  348. } \
  349. static struct device_attribute dev_attr_##_name = { \
  350. .attr = { \
  351. .name = __stringify(_name), \
  352. .mode = 0644 }, \
  353. .show = show_##_name, \
  354. .store = store_##_name, \
  355. }
  356. EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
  357. EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
  358. EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
  359. struct eeepc_cpufv {
  360. int num;
  361. int cur;
  362. };
  363. static int get_cpufv(struct eeepc_cpufv *c)
  364. {
  365. c->cur = get_acpi(CM_ASL_CPUFV);
  366. c->num = (c->cur >> 8) & 0xff;
  367. c->cur &= 0xff;
  368. if (c->cur < 0 || c->num <= 0 || c->num > 12)
  369. return -ENODEV;
  370. return 0;
  371. }
  372. static ssize_t show_available_cpufv(struct device *dev,
  373. struct device_attribute *attr,
  374. char *buf)
  375. {
  376. struct eeepc_cpufv c;
  377. int i;
  378. ssize_t len = 0;
  379. if (get_cpufv(&c))
  380. return -ENODEV;
  381. for (i = 0; i < c.num; i++)
  382. len += sprintf(buf + len, "%d ", i);
  383. len += sprintf(buf + len, "\n");
  384. return len;
  385. }
  386. static ssize_t show_cpufv(struct device *dev,
  387. struct device_attribute *attr,
  388. char *buf)
  389. {
  390. struct eeepc_cpufv c;
  391. if (get_cpufv(&c))
  392. return -ENODEV;
  393. return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
  394. }
  395. static ssize_t store_cpufv(struct device *dev,
  396. struct device_attribute *attr,
  397. const char *buf, size_t count)
  398. {
  399. struct eeepc_cpufv c;
  400. int rv, value;
  401. if (get_cpufv(&c))
  402. return -ENODEV;
  403. rv = parse_arg(buf, count, &value);
  404. if (rv < 0)
  405. return rv;
  406. if (!rv || value < 0 || value >= c.num)
  407. return -EINVAL;
  408. set_acpi(CM_ASL_CPUFV, value);
  409. return rv;
  410. }
  411. static struct device_attribute dev_attr_cpufv = {
  412. .attr = {
  413. .name = "cpufv",
  414. .mode = 0644 },
  415. .show = show_cpufv,
  416. .store = store_cpufv
  417. };
  418. static struct device_attribute dev_attr_available_cpufv = {
  419. .attr = {
  420. .name = "available_cpufv",
  421. .mode = 0444 },
  422. .show = show_available_cpufv
  423. };
  424. static struct attribute *platform_attributes[] = {
  425. &dev_attr_camera.attr,
  426. &dev_attr_cardr.attr,
  427. &dev_attr_disp.attr,
  428. &dev_attr_cpufv.attr,
  429. &dev_attr_available_cpufv.attr,
  430. NULL
  431. };
  432. static struct attribute_group platform_attribute_group = {
  433. .attrs = platform_attributes
  434. };
  435. /*
  436. * Hotkey functions
  437. */
  438. static struct key_entry *eepc_get_entry_by_scancode(int code)
  439. {
  440. struct key_entry *key;
  441. for (key = eeepc_keymap; key->type != KE_END; key++)
  442. if (code == key->code)
  443. return key;
  444. return NULL;
  445. }
  446. static struct key_entry *eepc_get_entry_by_keycode(int code)
  447. {
  448. struct key_entry *key;
  449. for (key = eeepc_keymap; key->type != KE_END; key++)
  450. if (code == key->keycode && key->type == KE_KEY)
  451. return key;
  452. return NULL;
  453. }
  454. static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  455. {
  456. struct key_entry *key = eepc_get_entry_by_scancode(scancode);
  457. if (key && key->type == KE_KEY) {
  458. *keycode = key->keycode;
  459. return 0;
  460. }
  461. return -EINVAL;
  462. }
  463. static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
  464. {
  465. struct key_entry *key;
  466. int old_keycode;
  467. if (keycode < 0 || keycode > KEY_MAX)
  468. return -EINVAL;
  469. key = eepc_get_entry_by_scancode(scancode);
  470. if (key && key->type == KE_KEY) {
  471. old_keycode = key->keycode;
  472. key->keycode = keycode;
  473. set_bit(keycode, dev->keybit);
  474. if (!eepc_get_entry_by_keycode(old_keycode))
  475. clear_bit(old_keycode, dev->keybit);
  476. return 0;
  477. }
  478. return -EINVAL;
  479. }
  480. static void cmsg_quirk(int cm, const char *name)
  481. {
  482. int dummy;
  483. /* Some BIOSes do not report cm although it is avaliable.
  484. Check if cm_getv[cm] works and, if yes, assume cm should be set. */
  485. if (!(ehotk->cm_supported & (1 << cm))
  486. && !read_acpi_int(ehotk->handle, cm_getv[cm], &dummy)) {
  487. pr_info("%s (%x) not reported by BIOS,"
  488. " enabling anyway\n", name, 1 << cm);
  489. ehotk->cm_supported |= 1 << cm;
  490. }
  491. }
  492. static void cmsg_quirks(void)
  493. {
  494. cmsg_quirk(CM_ASL_LID, "LID");
  495. cmsg_quirk(CM_ASL_TYPE, "TYPE");
  496. cmsg_quirk(CM_ASL_PANELPOWER, "PANELPOWER");
  497. cmsg_quirk(CM_ASL_TPD, "TPD");
  498. }
  499. static int eeepc_hotk_check(void)
  500. {
  501. const struct key_entry *key;
  502. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  503. int result;
  504. result = acpi_bus_get_status(ehotk->device);
  505. if (result)
  506. return result;
  507. if (ehotk->device->status.present) {
  508. if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
  509. &buffer)) {
  510. pr_err("Hotkey initialization failed\n");
  511. return -ENODEV;
  512. } else {
  513. pr_notice("Hotkey init flags 0x%x\n", ehotk->init_flag);
  514. }
  515. /* get control methods supported */
  516. if (read_acpi_int(ehotk->handle, "CMSG"
  517. , &ehotk->cm_supported)) {
  518. pr_err("Get control methods supported failed\n");
  519. return -ENODEV;
  520. } else {
  521. cmsg_quirks();
  522. pr_info("Get control methods supported: 0x%x\n",
  523. ehotk->cm_supported);
  524. }
  525. ehotk->inputdev = input_allocate_device();
  526. if (!ehotk->inputdev) {
  527. pr_info("Unable to allocate input device\n");
  528. return 0;
  529. }
  530. ehotk->inputdev->name = "Asus EeePC extra buttons";
  531. ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
  532. ehotk->inputdev->id.bustype = BUS_HOST;
  533. ehotk->inputdev->getkeycode = eeepc_getkeycode;
  534. ehotk->inputdev->setkeycode = eeepc_setkeycode;
  535. for (key = eeepc_keymap; key->type != KE_END; key++) {
  536. switch (key->type) {
  537. case KE_KEY:
  538. set_bit(EV_KEY, ehotk->inputdev->evbit);
  539. set_bit(key->keycode, ehotk->inputdev->keybit);
  540. break;
  541. }
  542. }
  543. result = input_register_device(ehotk->inputdev);
  544. if (result) {
  545. pr_info("Unable to register input device\n");
  546. input_free_device(ehotk->inputdev);
  547. return 0;
  548. }
  549. } else {
  550. pr_err("Hotkey device not present, aborting\n");
  551. return -EINVAL;
  552. }
  553. return 0;
  554. }
  555. static int notify_brn(void)
  556. {
  557. /* returns the *previous* brightness, or -1 */
  558. struct backlight_device *bd = eeepc_backlight_device;
  559. if (bd) {
  560. int old = bd->props.brightness;
  561. bd->props.brightness = read_brightness(bd);
  562. return old;
  563. }
  564. return -1;
  565. }
  566. static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
  567. u8 *value)
  568. {
  569. int val = get_acpi(CM_ASL_WLAN);
  570. if (val == 1 || val == 0)
  571. *value = val;
  572. else
  573. return -EINVAL;
  574. return 0;
  575. }
  576. static void eeepc_rfkill_hotplug(void)
  577. {
  578. struct pci_dev *dev;
  579. struct pci_bus *bus = pci_find_bus(0, 1);
  580. bool blocked;
  581. if (!bus) {
  582. pr_warning("Unable to find PCI bus 1?\n");
  583. return;
  584. }
  585. blocked = eeepc_wlan_rfkill_blocked();
  586. if (!blocked) {
  587. dev = pci_get_slot(bus, 0);
  588. if (dev) {
  589. /* Device already present */
  590. pci_dev_put(dev);
  591. return;
  592. }
  593. dev = pci_scan_single_device(bus, 0);
  594. if (dev) {
  595. pci_bus_assign_resources(bus);
  596. if (pci_bus_add_device(dev))
  597. pr_err("Unable to hotplug wifi\n");
  598. }
  599. } else {
  600. dev = pci_get_slot(bus, 0);
  601. if (dev) {
  602. pci_remove_bus_device(dev);
  603. pci_dev_put(dev);
  604. }
  605. }
  606. rfkill_set_sw_state(ehotk->wlan_rfkill, blocked);
  607. }
  608. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  609. {
  610. if (event != ACPI_NOTIFY_BUS_CHECK)
  611. return;
  612. eeepc_rfkill_hotplug();
  613. }
  614. static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
  615. {
  616. static struct key_entry *key;
  617. u16 count;
  618. int brn = -ENODEV;
  619. if (!ehotk)
  620. return;
  621. if (event > ACPI_MAX_SYS_NOTIFY)
  622. return;
  623. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
  624. brn = notify_brn();
  625. count = ehotk->event_count[event % 128]++;
  626. acpi_bus_generate_proc_event(ehotk->device, event, count);
  627. acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
  628. dev_name(&ehotk->device->dev), event,
  629. count);
  630. if (ehotk->inputdev) {
  631. if (brn != -ENODEV) {
  632. /* brightness-change events need special
  633. * handling for conversion to key events
  634. */
  635. if (brn < 0)
  636. brn = event;
  637. else
  638. brn += NOTIFY_BRN_MIN;
  639. if (event < brn)
  640. event = NOTIFY_BRN_MIN; /* brightness down */
  641. else if (event > brn)
  642. event = NOTIFY_BRN_MIN + 2; /* ... up */
  643. else
  644. event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
  645. }
  646. key = eepc_get_entry_by_scancode(event);
  647. if (key) {
  648. switch (key->type) {
  649. case KE_KEY:
  650. input_report_key(ehotk->inputdev, key->keycode,
  651. 1);
  652. input_sync(ehotk->inputdev);
  653. input_report_key(ehotk->inputdev, key->keycode,
  654. 0);
  655. input_sync(ehotk->inputdev);
  656. break;
  657. }
  658. }
  659. }
  660. }
  661. static int eeepc_register_rfkill_notifier(char *node)
  662. {
  663. acpi_status status = AE_OK;
  664. acpi_handle handle;
  665. status = acpi_get_handle(NULL, node, &handle);
  666. if (ACPI_SUCCESS(status)) {
  667. status = acpi_install_notify_handler(handle,
  668. ACPI_SYSTEM_NOTIFY,
  669. eeepc_rfkill_notify,
  670. NULL);
  671. if (ACPI_FAILURE(status))
  672. pr_warning("Failed to register notify on %s\n", node);
  673. } else
  674. return -ENODEV;
  675. return 0;
  676. }
  677. static void eeepc_unregister_rfkill_notifier(char *node)
  678. {
  679. acpi_status status = AE_OK;
  680. acpi_handle handle;
  681. status = acpi_get_handle(NULL, node, &handle);
  682. if (ACPI_SUCCESS(status)) {
  683. status = acpi_remove_notify_handler(handle,
  684. ACPI_SYSTEM_NOTIFY,
  685. eeepc_rfkill_notify);
  686. if (ACPI_FAILURE(status))
  687. pr_err("Error removing rfkill notify handler %s\n",
  688. node);
  689. }
  690. }
  691. static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  692. {
  693. kfree(hotplug_slot->info);
  694. kfree(hotplug_slot);
  695. }
  696. static int eeepc_setup_pci_hotplug(void)
  697. {
  698. int ret = -ENOMEM;
  699. struct pci_bus *bus = pci_find_bus(0, 1);
  700. if (!bus) {
  701. pr_err("Unable to find wifi PCI bus\n");
  702. return -ENODEV;
  703. }
  704. ehotk->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  705. if (!ehotk->hotplug_slot)
  706. goto error_slot;
  707. ehotk->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  708. GFP_KERNEL);
  709. if (!ehotk->hotplug_slot->info)
  710. goto error_info;
  711. ehotk->hotplug_slot->private = ehotk;
  712. ehotk->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
  713. ehotk->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
  714. eeepc_get_adapter_status(ehotk->hotplug_slot,
  715. &ehotk->hotplug_slot->info->adapter_status);
  716. ret = pci_hp_register(ehotk->hotplug_slot, bus, 0, "eeepc-wifi");
  717. if (ret) {
  718. pr_err("Unable to register hotplug slot - %d\n", ret);
  719. goto error_register;
  720. }
  721. return 0;
  722. error_register:
  723. kfree(ehotk->hotplug_slot->info);
  724. error_info:
  725. kfree(ehotk->hotplug_slot);
  726. ehotk->hotplug_slot = NULL;
  727. error_slot:
  728. return ret;
  729. }
  730. static int eeepc_hotk_add(struct acpi_device *device)
  731. {
  732. int result;
  733. if (!device)
  734. return -EINVAL;
  735. pr_notice(EEEPC_HOTK_NAME "\n");
  736. ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
  737. if (!ehotk)
  738. return -ENOMEM;
  739. ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  740. ehotk->handle = device->handle;
  741. strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
  742. strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
  743. device->driver_data = ehotk;
  744. ehotk->device = device;
  745. result = eeepc_hotk_check();
  746. if (result)
  747. goto ehotk_fail;
  748. return 0;
  749. ehotk_fail:
  750. kfree(ehotk);
  751. ehotk = NULL;
  752. return result;
  753. }
  754. static int eeepc_hotk_remove(struct acpi_device *device, int type)
  755. {
  756. if (!device || !acpi_driver_data(device))
  757. return -EINVAL;
  758. kfree(ehotk);
  759. return 0;
  760. }
  761. static int eeepc_hotk_resume(struct acpi_device *device)
  762. {
  763. if (ehotk->wlan_rfkill) {
  764. bool wlan;
  765. /* Workaround - it seems that _PTS disables the wireless
  766. without notification or changing the value read by WLAN.
  767. Normally this is fine because the correct value is restored
  768. from the non-volatile storage on resume, but we need to do
  769. it ourself if case suspend is aborted, or we lose wireless.
  770. */
  771. wlan = get_acpi(CM_ASL_WLAN);
  772. set_acpi(CM_ASL_WLAN, wlan);
  773. rfkill_set_sw_state(ehotk->wlan_rfkill, wlan != 1);
  774. eeepc_rfkill_hotplug();
  775. }
  776. if (ehotk->bluetooth_rfkill)
  777. rfkill_set_sw_state(ehotk->bluetooth_rfkill,
  778. get_acpi(CM_ASL_BLUETOOTH) != 1);
  779. return 0;
  780. }
  781. /*
  782. * Hwmon
  783. */
  784. static int eeepc_get_fan_pwm(void)
  785. {
  786. int value = 0;
  787. read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
  788. value = value * 255 / 100;
  789. return (value);
  790. }
  791. static void eeepc_set_fan_pwm(int value)
  792. {
  793. value = SENSORS_LIMIT(value, 0, 255);
  794. value = value * 100 / 255;
  795. ec_write(EEEPC_EC_SC02, value);
  796. }
  797. static int eeepc_get_fan_rpm(void)
  798. {
  799. int high = 0;
  800. int low = 0;
  801. read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
  802. read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
  803. return (high << 8 | low);
  804. }
  805. static int eeepc_get_fan_ctrl(void)
  806. {
  807. int value = 0;
  808. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  809. return ((value & 0x02 ? 1 : 0));
  810. }
  811. static void eeepc_set_fan_ctrl(int manual)
  812. {
  813. int value = 0;
  814. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  815. if (manual)
  816. value |= 0x02;
  817. else
  818. value &= ~0x02;
  819. ec_write(EEEPC_EC_SFB3, value);
  820. }
  821. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  822. {
  823. int rv, value;
  824. rv = parse_arg(buf, count, &value);
  825. if (rv > 0)
  826. set(value);
  827. return rv;
  828. }
  829. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  830. {
  831. return sprintf(buf, "%d\n", get());
  832. }
  833. #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
  834. static ssize_t show_##_name(struct device *dev, \
  835. struct device_attribute *attr, \
  836. char *buf) \
  837. { \
  838. return show_sys_hwmon(_set, buf); \
  839. } \
  840. static ssize_t store_##_name(struct device *dev, \
  841. struct device_attribute *attr, \
  842. const char *buf, size_t count) \
  843. { \
  844. return store_sys_hwmon(_get, buf, count); \
  845. } \
  846. static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
  847. EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
  848. EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
  849. eeepc_get_fan_pwm, eeepc_set_fan_pwm);
  850. EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  851. eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
  852. static ssize_t
  853. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  854. {
  855. return sprintf(buf, "eeepc\n");
  856. }
  857. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  858. static struct attribute *hwmon_attributes[] = {
  859. &sensor_dev_attr_pwm1.dev_attr.attr,
  860. &sensor_dev_attr_fan1_input.dev_attr.attr,
  861. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  862. &sensor_dev_attr_name.dev_attr.attr,
  863. NULL
  864. };
  865. static struct attribute_group hwmon_attribute_group = {
  866. .attrs = hwmon_attributes
  867. };
  868. /*
  869. * exit/init
  870. */
  871. static void eeepc_backlight_exit(void)
  872. {
  873. if (eeepc_backlight_device)
  874. backlight_device_unregister(eeepc_backlight_device);
  875. eeepc_backlight_device = NULL;
  876. }
  877. static void eeepc_rfkill_exit(void)
  878. {
  879. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
  880. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  881. if (ehotk->wlan_rfkill)
  882. rfkill_unregister(ehotk->wlan_rfkill);
  883. if (ehotk->bluetooth_rfkill)
  884. rfkill_unregister(ehotk->bluetooth_rfkill);
  885. if (ehotk->hotplug_slot)
  886. pci_hp_deregister(ehotk->hotplug_slot);
  887. }
  888. static void eeepc_input_exit(void)
  889. {
  890. if (ehotk->inputdev)
  891. input_unregister_device(ehotk->inputdev);
  892. }
  893. static void eeepc_hwmon_exit(void)
  894. {
  895. struct device *hwmon;
  896. hwmon = eeepc_hwmon_device;
  897. if (!hwmon)
  898. return ;
  899. sysfs_remove_group(&hwmon->kobj,
  900. &hwmon_attribute_group);
  901. hwmon_device_unregister(hwmon);
  902. eeepc_hwmon_device = NULL;
  903. }
  904. static void __exit eeepc_laptop_exit(void)
  905. {
  906. eeepc_backlight_exit();
  907. eeepc_rfkill_exit();
  908. eeepc_input_exit();
  909. eeepc_hwmon_exit();
  910. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  911. sysfs_remove_group(&platform_device->dev.kobj,
  912. &platform_attribute_group);
  913. platform_device_unregister(platform_device);
  914. platform_driver_unregister(&platform_driver);
  915. }
  916. static int eeepc_new_rfkill(struct rfkill **rfkill,
  917. const char *name, struct device *dev,
  918. enum rfkill_type type, int cm)
  919. {
  920. int result;
  921. result = get_acpi(cm);
  922. if (result < 0)
  923. return result;
  924. *rfkill = rfkill_alloc(name, dev, type,
  925. &eeepc_rfkill_ops, (void *)(unsigned long)cm);
  926. if (!*rfkill)
  927. return -EINVAL;
  928. rfkill_init_sw_state(*rfkill, get_acpi(cm) != 1);
  929. result = rfkill_register(*rfkill);
  930. if (result) {
  931. rfkill_destroy(*rfkill);
  932. *rfkill = NULL;
  933. return result;
  934. }
  935. return 0;
  936. }
  937. static int eeepc_rfkill_init(struct device *dev)
  938. {
  939. int result = 0;
  940. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
  941. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
  942. result = eeepc_new_rfkill(&ehotk->wlan_rfkill,
  943. "eeepc-wlan", dev,
  944. RFKILL_TYPE_WLAN, CM_ASL_WLAN);
  945. if (result && result != -ENODEV)
  946. goto exit;
  947. result = eeepc_new_rfkill(&ehotk->bluetooth_rfkill,
  948. "eeepc-bluetooth", dev,
  949. RFKILL_TYPE_BLUETOOTH, CM_ASL_BLUETOOTH);
  950. if (result && result != -ENODEV)
  951. goto exit;
  952. result = eeepc_setup_pci_hotplug();
  953. /*
  954. * If we get -EBUSY then something else is handling the PCI hotplug -
  955. * don't fail in this case
  956. */
  957. if (result == -EBUSY)
  958. result = 0;
  959. exit:
  960. if (result && result != -ENODEV)
  961. eeepc_rfkill_exit();
  962. return result;
  963. }
  964. static int eeepc_backlight_init(struct device *dev)
  965. {
  966. struct backlight_device *bd;
  967. bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
  968. NULL, &eeepcbl_ops);
  969. if (IS_ERR(bd)) {
  970. pr_err("Could not register eeepc backlight device\n");
  971. eeepc_backlight_device = NULL;
  972. return PTR_ERR(bd);
  973. }
  974. eeepc_backlight_device = bd;
  975. bd->props.max_brightness = 15;
  976. bd->props.brightness = read_brightness(NULL);
  977. bd->props.power = FB_BLANK_UNBLANK;
  978. backlight_update_status(bd);
  979. return 0;
  980. }
  981. static int eeepc_hwmon_init(struct device *dev)
  982. {
  983. struct device *hwmon;
  984. int result;
  985. hwmon = hwmon_device_register(dev);
  986. if (IS_ERR(hwmon)) {
  987. pr_err("Could not register eeepc hwmon device\n");
  988. eeepc_hwmon_device = NULL;
  989. return PTR_ERR(hwmon);
  990. }
  991. eeepc_hwmon_device = hwmon;
  992. result = sysfs_create_group(&hwmon->kobj,
  993. &hwmon_attribute_group);
  994. if (result)
  995. eeepc_hwmon_exit();
  996. return result;
  997. }
  998. static int __init eeepc_laptop_init(void)
  999. {
  1000. struct device *dev;
  1001. int result;
  1002. if (acpi_disabled)
  1003. return -ENODEV;
  1004. result = acpi_bus_register_driver(&eeepc_hotk_driver);
  1005. if (result < 0)
  1006. return result;
  1007. if (!ehotk) {
  1008. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  1009. return -ENODEV;
  1010. }
  1011. eeepc_enable_camera();
  1012. /* Register platform stuff */
  1013. result = platform_driver_register(&platform_driver);
  1014. if (result)
  1015. goto fail_platform_driver;
  1016. platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
  1017. if (!platform_device) {
  1018. result = -ENOMEM;
  1019. goto fail_platform_device1;
  1020. }
  1021. result = platform_device_add(platform_device);
  1022. if (result)
  1023. goto fail_platform_device2;
  1024. result = sysfs_create_group(&platform_device->dev.kobj,
  1025. &platform_attribute_group);
  1026. if (result)
  1027. goto fail_sysfs;
  1028. dev = &platform_device->dev;
  1029. if (!acpi_video_backlight_support()) {
  1030. result = eeepc_backlight_init(dev);
  1031. if (result)
  1032. goto fail_backlight;
  1033. } else
  1034. pr_info("Backlight controlled by ACPI video "
  1035. "driver\n");
  1036. result = eeepc_hwmon_init(dev);
  1037. if (result)
  1038. goto fail_hwmon;
  1039. result = eeepc_rfkill_init(dev);
  1040. if (result)
  1041. goto fail_rfkill;
  1042. return 0;
  1043. fail_rfkill:
  1044. eeepc_hwmon_exit();
  1045. fail_hwmon:
  1046. eeepc_backlight_exit();
  1047. fail_backlight:
  1048. sysfs_remove_group(&platform_device->dev.kobj,
  1049. &platform_attribute_group);
  1050. fail_sysfs:
  1051. platform_device_del(platform_device);
  1052. fail_platform_device2:
  1053. platform_device_put(platform_device);
  1054. fail_platform_device1:
  1055. platform_driver_unregister(&platform_driver);
  1056. fail_platform_driver:
  1057. eeepc_input_exit();
  1058. return result;
  1059. }
  1060. module_init(eeepc_laptop_init);
  1061. module_exit(eeepc_laptop_exit);