eeepc-laptop.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/types.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/backlight.h>
  24. #include <linux/fb.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <acpi/acpi_drivers.h>
  28. #include <acpi/acpi_bus.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/input.h>
  31. #include <linux/rfkill.h>
  32. #include <linux/pci.h>
  33. #define EEEPC_LAPTOP_VERSION "0.1"
  34. #define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
  35. #define EEEPC_HOTK_FILE "eeepc"
  36. #define EEEPC_HOTK_CLASS "hotkey"
  37. #define EEEPC_HOTK_DEVICE_NAME "Hotkey"
  38. #define EEEPC_HOTK_HID "ASUS010"
  39. #define EEEPC_LOG EEEPC_HOTK_FILE ": "
  40. #define EEEPC_ERR KERN_ERR EEEPC_LOG
  41. #define EEEPC_WARNING KERN_WARNING EEEPC_LOG
  42. #define EEEPC_NOTICE KERN_NOTICE EEEPC_LOG
  43. #define EEEPC_INFO KERN_INFO EEEPC_LOG
  44. /*
  45. * Definitions for Asus EeePC
  46. */
  47. #define NOTIFY_WLAN_ON 0x10
  48. #define NOTIFY_BRN_MIN 0x20
  49. #define NOTIFY_BRN_MAX 0x2f
  50. enum {
  51. DISABLE_ASL_WLAN = 0x0001,
  52. DISABLE_ASL_BLUETOOTH = 0x0002,
  53. DISABLE_ASL_IRDA = 0x0004,
  54. DISABLE_ASL_CAMERA = 0x0008,
  55. DISABLE_ASL_TV = 0x0010,
  56. DISABLE_ASL_GPS = 0x0020,
  57. DISABLE_ASL_DISPLAYSWITCH = 0x0040,
  58. DISABLE_ASL_MODEM = 0x0080,
  59. DISABLE_ASL_CARDREADER = 0x0100
  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_LID
  84. };
  85. static const char *cm_getv[] = {
  86. "WLDG", "BTHG", NULL, NULL,
  87. "CAMG", NULL, NULL, NULL,
  88. NULL, "PBLG", NULL, NULL,
  89. "CFVG", NULL, NULL, NULL,
  90. "USBG", NULL, NULL, "MODG",
  91. "CRDG", "LIDG"
  92. };
  93. static const char *cm_setv[] = {
  94. "WLDS", "BTHS", NULL, NULL,
  95. "CAMS", NULL, NULL, NULL,
  96. "SDSP", "PBLS", "HDPS", NULL,
  97. "CFVS", NULL, NULL, NULL,
  98. "USBG", NULL, NULL, "MODS",
  99. "CRDS", NULL
  100. };
  101. #define EEEPC_EC "\\_SB.PCI0.SBRG.EC0."
  102. #define EEEPC_EC_FAN_PWM EEEPC_EC "SC02" /* Fan PWM duty cycle (%) */
  103. #define EEEPC_EC_SC02 0x63
  104. #define EEEPC_EC_FAN_HRPM EEEPC_EC "SC05" /* High byte, fan speed (RPM) */
  105. #define EEEPC_EC_FAN_LRPM EEEPC_EC "SC06" /* Low byte, fan speed (RPM) */
  106. #define EEEPC_EC_FAN_CTRL EEEPC_EC "SFB3" /* Byte containing SF25 */
  107. #define EEEPC_EC_SFB3 0xD3
  108. /*
  109. * This is the main structure, we can use it to store useful information
  110. * about the hotk device
  111. */
  112. struct eeepc_hotk {
  113. struct acpi_device *device; /* the device we are in */
  114. acpi_handle handle; /* the handle of the hotk device */
  115. u32 cm_supported; /* the control methods supported
  116. by this BIOS */
  117. uint init_flag; /* Init flags */
  118. u16 event_count[128]; /* count for each event */
  119. struct input_dev *inputdev;
  120. u16 *keycode_map;
  121. struct rfkill *eeepc_wlan_rfkill;
  122. struct rfkill *eeepc_bluetooth_rfkill;
  123. };
  124. /* The actual device the driver binds to */
  125. static struct eeepc_hotk *ehotk;
  126. /* Platform device/driver */
  127. static struct platform_driver platform_driver = {
  128. .driver = {
  129. .name = EEEPC_HOTK_FILE,
  130. .owner = THIS_MODULE,
  131. }
  132. };
  133. static struct platform_device *platform_device;
  134. struct key_entry {
  135. char type;
  136. u8 code;
  137. u16 keycode;
  138. };
  139. enum { KE_KEY, KE_END };
  140. static struct key_entry eeepc_keymap[] = {
  141. /* Sleep already handled via generic ACPI code */
  142. {KE_KEY, 0x10, KEY_WLAN },
  143. {KE_KEY, 0x11, KEY_WLAN },
  144. {KE_KEY, 0x12, KEY_PROG1 },
  145. {KE_KEY, 0x13, KEY_MUTE },
  146. {KE_KEY, 0x14, KEY_VOLUMEDOWN },
  147. {KE_KEY, 0x15, KEY_VOLUMEUP },
  148. {KE_KEY, 0x1a, KEY_COFFEE },
  149. {KE_KEY, 0x1b, KEY_ZOOM },
  150. {KE_KEY, 0x1c, KEY_PROG2 },
  151. {KE_KEY, 0x1d, KEY_PROG3 },
  152. {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
  153. {KE_KEY, NOTIFY_BRN_MIN + 2, KEY_BRIGHTNESSUP },
  154. {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
  155. {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
  156. {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
  157. {KE_END, 0},
  158. };
  159. /*
  160. * The hotkey driver declaration
  161. */
  162. static int eeepc_hotk_add(struct acpi_device *device);
  163. static int eeepc_hotk_remove(struct acpi_device *device, int type);
  164. static void eeepc_hotk_notify(struct acpi_device *device, u32 event);
  165. static const struct acpi_device_id eeepc_device_ids[] = {
  166. {EEEPC_HOTK_HID, 0},
  167. {"", 0},
  168. };
  169. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  170. static struct acpi_driver eeepc_hotk_driver = {
  171. .name = EEEPC_HOTK_NAME,
  172. .class = EEEPC_HOTK_CLASS,
  173. .ids = eeepc_device_ids,
  174. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  175. .ops = {
  176. .add = eeepc_hotk_add,
  177. .remove = eeepc_hotk_remove,
  178. .notify = eeepc_hotk_notify,
  179. },
  180. };
  181. /* The backlight device /sys/class/backlight */
  182. static struct backlight_device *eeepc_backlight_device;
  183. /* The hwmon device */
  184. static struct device *eeepc_hwmon_device;
  185. /*
  186. * The backlight class declaration
  187. */
  188. static int read_brightness(struct backlight_device *bd);
  189. static int update_bl_status(struct backlight_device *bd);
  190. static struct backlight_ops eeepcbl_ops = {
  191. .get_brightness = read_brightness,
  192. .update_status = update_bl_status,
  193. };
  194. MODULE_AUTHOR("Corentin Chary, Eric Cooper");
  195. MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
  196. MODULE_LICENSE("GPL");
  197. /*
  198. * ACPI Helpers
  199. */
  200. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  201. struct acpi_buffer *output)
  202. {
  203. struct acpi_object_list params;
  204. union acpi_object in_obj;
  205. acpi_status status;
  206. params.count = 1;
  207. params.pointer = &in_obj;
  208. in_obj.type = ACPI_TYPE_INTEGER;
  209. in_obj.integer.value = val;
  210. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  211. return (status == AE_OK ? 0 : -1);
  212. }
  213. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  214. {
  215. acpi_status status;
  216. unsigned long long result;
  217. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  218. if (ACPI_FAILURE(status)) {
  219. *val = -1;
  220. return -1;
  221. } else {
  222. *val = result;
  223. return 0;
  224. }
  225. }
  226. static int set_acpi(int cm, int value)
  227. {
  228. if (ehotk->cm_supported & (0x1 << cm)) {
  229. const char *method = cm_setv[cm];
  230. if (method == NULL)
  231. return -ENODEV;
  232. if (write_acpi_int(ehotk->handle, method, value, NULL))
  233. printk(EEEPC_WARNING "Error writing %s\n", method);
  234. }
  235. return 0;
  236. }
  237. static int get_acpi(int cm)
  238. {
  239. int value = -1;
  240. if ((ehotk->cm_supported & (0x1 << cm))) {
  241. const char *method = cm_getv[cm];
  242. if (method == NULL)
  243. return -ENODEV;
  244. if (read_acpi_int(ehotk->handle, method, &value))
  245. printk(EEEPC_WARNING "Error reading %s\n", method);
  246. }
  247. return value;
  248. }
  249. /*
  250. * Backlight
  251. */
  252. static int read_brightness(struct backlight_device *bd)
  253. {
  254. return get_acpi(CM_ASL_PANELBRIGHT);
  255. }
  256. static int set_brightness(struct backlight_device *bd, int value)
  257. {
  258. value = max(0, min(15, value));
  259. return set_acpi(CM_ASL_PANELBRIGHT, value);
  260. }
  261. static int update_bl_status(struct backlight_device *bd)
  262. {
  263. return set_brightness(bd, bd->props.brightness);
  264. }
  265. /*
  266. * Rfkill helpers
  267. */
  268. static int eeepc_wlan_rfkill_set(void *data, enum rfkill_state state)
  269. {
  270. if (state == RFKILL_STATE_SOFT_BLOCKED)
  271. return set_acpi(CM_ASL_WLAN, 0);
  272. else
  273. return set_acpi(CM_ASL_WLAN, 1);
  274. }
  275. static int eeepc_wlan_rfkill_state(void *data, enum rfkill_state *state)
  276. {
  277. if (get_acpi(CM_ASL_WLAN) == 1)
  278. *state = RFKILL_STATE_UNBLOCKED;
  279. else
  280. *state = RFKILL_STATE_SOFT_BLOCKED;
  281. return 0;
  282. }
  283. static int eeepc_bluetooth_rfkill_set(void *data, enum rfkill_state state)
  284. {
  285. if (state == RFKILL_STATE_SOFT_BLOCKED)
  286. return set_acpi(CM_ASL_BLUETOOTH, 0);
  287. else
  288. return set_acpi(CM_ASL_BLUETOOTH, 1);
  289. }
  290. static int eeepc_bluetooth_rfkill_state(void *data, enum rfkill_state *state)
  291. {
  292. if (get_acpi(CM_ASL_BLUETOOTH) == 1)
  293. *state = RFKILL_STATE_UNBLOCKED;
  294. else
  295. *state = RFKILL_STATE_SOFT_BLOCKED;
  296. return 0;
  297. }
  298. /*
  299. * Sys helpers
  300. */
  301. static int parse_arg(const char *buf, unsigned long count, int *val)
  302. {
  303. if (!count)
  304. return 0;
  305. if (sscanf(buf, "%i", val) != 1)
  306. return -EINVAL;
  307. return count;
  308. }
  309. static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
  310. {
  311. int rv, value;
  312. rv = parse_arg(buf, count, &value);
  313. if (rv > 0)
  314. set_acpi(cm, value);
  315. return rv;
  316. }
  317. static ssize_t show_sys_acpi(int cm, char *buf)
  318. {
  319. return sprintf(buf, "%d\n", get_acpi(cm));
  320. }
  321. #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
  322. static ssize_t show_##_name(struct device *dev, \
  323. struct device_attribute *attr, \
  324. char *buf) \
  325. { \
  326. return show_sys_acpi(_cm, buf); \
  327. } \
  328. static ssize_t store_##_name(struct device *dev, \
  329. struct device_attribute *attr, \
  330. const char *buf, size_t count) \
  331. { \
  332. return store_sys_acpi(_cm, buf, count); \
  333. } \
  334. static struct device_attribute dev_attr_##_name = { \
  335. .attr = { \
  336. .name = __stringify(_name), \
  337. .mode = 0644 }, \
  338. .show = show_##_name, \
  339. .store = store_##_name, \
  340. }
  341. EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
  342. EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
  343. EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
  344. EEEPC_CREATE_DEVICE_ATTR(cpufv, CM_ASL_CPUFV);
  345. static struct attribute *platform_attributes[] = {
  346. &dev_attr_camera.attr,
  347. &dev_attr_cardr.attr,
  348. &dev_attr_disp.attr,
  349. &dev_attr_cpufv.attr,
  350. NULL
  351. };
  352. static struct attribute_group platform_attribute_group = {
  353. .attrs = platform_attributes
  354. };
  355. /*
  356. * Hotkey functions
  357. */
  358. static struct key_entry *eepc_get_entry_by_scancode(int code)
  359. {
  360. struct key_entry *key;
  361. for (key = eeepc_keymap; key->type != KE_END; key++)
  362. if (code == key->code)
  363. return key;
  364. return NULL;
  365. }
  366. static struct key_entry *eepc_get_entry_by_keycode(int code)
  367. {
  368. struct key_entry *key;
  369. for (key = eeepc_keymap; key->type != KE_END; key++)
  370. if (code == key->keycode && key->type == KE_KEY)
  371. return key;
  372. return NULL;
  373. }
  374. static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  375. {
  376. struct key_entry *key = eepc_get_entry_by_scancode(scancode);
  377. if (key && key->type == KE_KEY) {
  378. *keycode = key->keycode;
  379. return 0;
  380. }
  381. return -EINVAL;
  382. }
  383. static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
  384. {
  385. struct key_entry *key;
  386. int old_keycode;
  387. if (keycode < 0 || keycode > KEY_MAX)
  388. return -EINVAL;
  389. key = eepc_get_entry_by_scancode(scancode);
  390. if (key && key->type == KE_KEY) {
  391. old_keycode = key->keycode;
  392. key->keycode = keycode;
  393. set_bit(keycode, dev->keybit);
  394. if (!eepc_get_entry_by_keycode(old_keycode))
  395. clear_bit(old_keycode, dev->keybit);
  396. return 0;
  397. }
  398. return -EINVAL;
  399. }
  400. static int eeepc_hotk_check(void)
  401. {
  402. const struct key_entry *key;
  403. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  404. int result;
  405. result = acpi_bus_get_status(ehotk->device);
  406. if (result)
  407. return result;
  408. if (ehotk->device->status.present) {
  409. if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
  410. &buffer)) {
  411. printk(EEEPC_ERR "Hotkey initialization failed\n");
  412. return -ENODEV;
  413. } else {
  414. printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
  415. ehotk->init_flag);
  416. }
  417. /* get control methods supported */
  418. if (read_acpi_int(ehotk->handle, "CMSG"
  419. , &ehotk->cm_supported)) {
  420. printk(EEEPC_ERR
  421. "Get control methods supported failed\n");
  422. return -ENODEV;
  423. } else {
  424. printk(EEEPC_INFO
  425. "Get control methods supported: 0x%x\n",
  426. ehotk->cm_supported);
  427. }
  428. ehotk->inputdev = input_allocate_device();
  429. if (!ehotk->inputdev) {
  430. printk(EEEPC_INFO "Unable to allocate input device\n");
  431. return 0;
  432. }
  433. ehotk->inputdev->name = "Asus EeePC extra buttons";
  434. ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
  435. ehotk->inputdev->id.bustype = BUS_HOST;
  436. ehotk->inputdev->getkeycode = eeepc_getkeycode;
  437. ehotk->inputdev->setkeycode = eeepc_setkeycode;
  438. for (key = eeepc_keymap; key->type != KE_END; key++) {
  439. switch (key->type) {
  440. case KE_KEY:
  441. set_bit(EV_KEY, ehotk->inputdev->evbit);
  442. set_bit(key->keycode, ehotk->inputdev->keybit);
  443. break;
  444. }
  445. }
  446. result = input_register_device(ehotk->inputdev);
  447. if (result) {
  448. printk(EEEPC_INFO "Unable to register input device\n");
  449. input_free_device(ehotk->inputdev);
  450. return 0;
  451. }
  452. } else {
  453. printk(EEEPC_ERR "Hotkey device not present, aborting\n");
  454. return -EINVAL;
  455. }
  456. return 0;
  457. }
  458. static int notify_brn(void)
  459. {
  460. /* returns the *previous* brightness, or -1 */
  461. struct backlight_device *bd = eeepc_backlight_device;
  462. if (bd) {
  463. int old = bd->props.brightness;
  464. bd->props.brightness = read_brightness(bd);
  465. return old;
  466. }
  467. return -1;
  468. }
  469. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  470. {
  471. enum rfkill_state state;
  472. struct pci_dev *dev;
  473. struct pci_bus *bus = pci_find_bus(0, 1);
  474. if (event != ACPI_NOTIFY_BUS_CHECK)
  475. return;
  476. if (!bus) {
  477. printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
  478. return;
  479. }
  480. eeepc_wlan_rfkill_state(ehotk->eeepc_wlan_rfkill, &state);
  481. if (state == RFKILL_STATE_UNBLOCKED) {
  482. dev = pci_get_slot(bus, 0);
  483. if (dev) {
  484. /* Device already present */
  485. pci_dev_put(dev);
  486. return;
  487. }
  488. dev = pci_scan_single_device(bus, 0);
  489. if (dev) {
  490. pci_bus_assign_resources(bus);
  491. if (pci_bus_add_device(dev))
  492. printk(EEEPC_ERR "Unable to hotplug wifi\n");
  493. }
  494. } else {
  495. dev = pci_get_slot(bus, 0);
  496. if (dev) {
  497. pci_remove_bus_device(dev);
  498. pci_dev_put(dev);
  499. }
  500. }
  501. rfkill_force_state(ehotk->eeepc_wlan_rfkill, state);
  502. }
  503. static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
  504. {
  505. static struct key_entry *key;
  506. u16 count;
  507. int brn = -ENODEV;
  508. if (!ehotk)
  509. return;
  510. if (event > ACPI_MAX_SYS_NOTIFY)
  511. return;
  512. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
  513. brn = notify_brn();
  514. count = ehotk->event_count[event % 128]++;
  515. acpi_bus_generate_proc_event(ehotk->device, event, count);
  516. acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
  517. dev_name(&ehotk->device->dev), event,
  518. count);
  519. if (ehotk->inputdev) {
  520. if (brn != -ENODEV) {
  521. /* brightness-change events need special
  522. * handling for conversion to key events
  523. */
  524. if (brn < 0)
  525. brn = event;
  526. else
  527. brn += NOTIFY_BRN_MIN;
  528. if (event < brn)
  529. event = NOTIFY_BRN_MIN; /* brightness down */
  530. else if (event > brn)
  531. event = NOTIFY_BRN_MIN + 2; /* ... up */
  532. else
  533. event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
  534. }
  535. key = eepc_get_entry_by_scancode(event);
  536. if (key) {
  537. switch (key->type) {
  538. case KE_KEY:
  539. input_report_key(ehotk->inputdev, key->keycode,
  540. 1);
  541. input_sync(ehotk->inputdev);
  542. input_report_key(ehotk->inputdev, key->keycode,
  543. 0);
  544. input_sync(ehotk->inputdev);
  545. break;
  546. }
  547. }
  548. }
  549. }
  550. static int eeepc_register_rfkill_notifier(char *node)
  551. {
  552. acpi_status status = AE_OK;
  553. acpi_handle handle;
  554. status = acpi_get_handle(NULL, node, &handle);
  555. if (ACPI_SUCCESS(status)) {
  556. status = acpi_install_notify_handler(handle,
  557. ACPI_SYSTEM_NOTIFY,
  558. eeepc_rfkill_notify,
  559. NULL);
  560. if (ACPI_FAILURE(status))
  561. printk(EEEPC_WARNING
  562. "Failed to register notify on %s\n", node);
  563. } else
  564. return -ENODEV;
  565. return 0;
  566. }
  567. static void eeepc_unregister_rfkill_notifier(char *node)
  568. {
  569. acpi_status status = AE_OK;
  570. acpi_handle handle;
  571. status = acpi_get_handle(NULL, node, &handle);
  572. if (ACPI_SUCCESS(status)) {
  573. status = acpi_remove_notify_handler(handle,
  574. ACPI_SYSTEM_NOTIFY,
  575. eeepc_rfkill_notify);
  576. if (ACPI_FAILURE(status))
  577. printk(EEEPC_ERR
  578. "Error removing rfkill notify handler %s\n",
  579. node);
  580. }
  581. }
  582. static int eeepc_hotk_add(struct acpi_device *device)
  583. {
  584. int result;
  585. if (!device)
  586. return -EINVAL;
  587. printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
  588. ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
  589. if (!ehotk)
  590. return -ENOMEM;
  591. ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  592. ehotk->handle = device->handle;
  593. strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
  594. strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
  595. device->driver_data = ehotk;
  596. ehotk->device = device;
  597. result = eeepc_hotk_check();
  598. if (result)
  599. goto ehotk_fail;
  600. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
  601. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
  602. if (get_acpi(CM_ASL_WLAN) != -1) {
  603. ehotk->eeepc_wlan_rfkill = rfkill_allocate(&device->dev,
  604. RFKILL_TYPE_WLAN);
  605. if (!ehotk->eeepc_wlan_rfkill)
  606. goto wlan_fail;
  607. ehotk->eeepc_wlan_rfkill->name = "eeepc-wlan";
  608. ehotk->eeepc_wlan_rfkill->toggle_radio = eeepc_wlan_rfkill_set;
  609. ehotk->eeepc_wlan_rfkill->get_state = eeepc_wlan_rfkill_state;
  610. if (get_acpi(CM_ASL_WLAN) == 1) {
  611. ehotk->eeepc_wlan_rfkill->state =
  612. RFKILL_STATE_UNBLOCKED;
  613. rfkill_set_default(RFKILL_TYPE_WLAN,
  614. RFKILL_STATE_UNBLOCKED);
  615. } else {
  616. ehotk->eeepc_wlan_rfkill->state =
  617. RFKILL_STATE_SOFT_BLOCKED;
  618. rfkill_set_default(RFKILL_TYPE_WLAN,
  619. RFKILL_STATE_SOFT_BLOCKED);
  620. }
  621. result = rfkill_register(ehotk->eeepc_wlan_rfkill);
  622. if (result)
  623. goto wlan_fail;
  624. }
  625. if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
  626. ehotk->eeepc_bluetooth_rfkill =
  627. rfkill_allocate(&device->dev, RFKILL_TYPE_BLUETOOTH);
  628. if (!ehotk->eeepc_bluetooth_rfkill)
  629. goto bluetooth_fail;
  630. ehotk->eeepc_bluetooth_rfkill->name = "eeepc-bluetooth";
  631. ehotk->eeepc_bluetooth_rfkill->toggle_radio =
  632. eeepc_bluetooth_rfkill_set;
  633. ehotk->eeepc_bluetooth_rfkill->get_state =
  634. eeepc_bluetooth_rfkill_state;
  635. if (get_acpi(CM_ASL_BLUETOOTH) == 1) {
  636. ehotk->eeepc_bluetooth_rfkill->state =
  637. RFKILL_STATE_UNBLOCKED;
  638. rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
  639. RFKILL_STATE_UNBLOCKED);
  640. } else {
  641. ehotk->eeepc_bluetooth_rfkill->state =
  642. RFKILL_STATE_SOFT_BLOCKED;
  643. rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
  644. RFKILL_STATE_SOFT_BLOCKED);
  645. }
  646. result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
  647. if (result)
  648. goto bluetooth_fail;
  649. }
  650. return 0;
  651. bluetooth_fail:
  652. if (ehotk->eeepc_bluetooth_rfkill)
  653. rfkill_free(ehotk->eeepc_bluetooth_rfkill);
  654. rfkill_unregister(ehotk->eeepc_wlan_rfkill);
  655. ehotk->eeepc_wlan_rfkill = NULL;
  656. wlan_fail:
  657. if (ehotk->eeepc_wlan_rfkill)
  658. rfkill_free(ehotk->eeepc_wlan_rfkill);
  659. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
  660. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  661. ehotk_fail:
  662. kfree(ehotk);
  663. ehotk = NULL;
  664. return result;
  665. }
  666. static int eeepc_hotk_remove(struct acpi_device *device, int type)
  667. {
  668. if (!device || !acpi_driver_data(device))
  669. return -EINVAL;
  670. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
  671. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  672. kfree(ehotk);
  673. return 0;
  674. }
  675. /*
  676. * Hwmon
  677. */
  678. static int eeepc_get_fan_pwm(void)
  679. {
  680. int value = 0;
  681. read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
  682. value = value * 255 / 100;
  683. return (value);
  684. }
  685. static void eeepc_set_fan_pwm(int value)
  686. {
  687. value = SENSORS_LIMIT(value, 0, 255);
  688. value = value * 100 / 255;
  689. ec_write(EEEPC_EC_SC02, value);
  690. }
  691. static int eeepc_get_fan_rpm(void)
  692. {
  693. int high = 0;
  694. int low = 0;
  695. read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
  696. read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
  697. return (high << 8 | low);
  698. }
  699. static int eeepc_get_fan_ctrl(void)
  700. {
  701. int value = 0;
  702. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  703. return ((value & 0x02 ? 1 : 0));
  704. }
  705. static void eeepc_set_fan_ctrl(int manual)
  706. {
  707. int value = 0;
  708. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  709. if (manual)
  710. value |= 0x02;
  711. else
  712. value &= ~0x02;
  713. ec_write(EEEPC_EC_SFB3, value);
  714. }
  715. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  716. {
  717. int rv, value;
  718. rv = parse_arg(buf, count, &value);
  719. if (rv > 0)
  720. set(value);
  721. return rv;
  722. }
  723. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  724. {
  725. return sprintf(buf, "%d\n", get());
  726. }
  727. #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
  728. static ssize_t show_##_name(struct device *dev, \
  729. struct device_attribute *attr, \
  730. char *buf) \
  731. { \
  732. return show_sys_hwmon(_set, buf); \
  733. } \
  734. static ssize_t store_##_name(struct device *dev, \
  735. struct device_attribute *attr, \
  736. const char *buf, size_t count) \
  737. { \
  738. return store_sys_hwmon(_get, buf, count); \
  739. } \
  740. static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
  741. EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
  742. EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
  743. eeepc_get_fan_pwm, eeepc_set_fan_pwm);
  744. EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  745. eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
  746. static ssize_t
  747. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  748. {
  749. return sprintf(buf, "eeepc\n");
  750. }
  751. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  752. static struct attribute *hwmon_attributes[] = {
  753. &sensor_dev_attr_pwm1.dev_attr.attr,
  754. &sensor_dev_attr_fan1_input.dev_attr.attr,
  755. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  756. &sensor_dev_attr_name.dev_attr.attr,
  757. NULL
  758. };
  759. static struct attribute_group hwmon_attribute_group = {
  760. .attrs = hwmon_attributes
  761. };
  762. /*
  763. * exit/init
  764. */
  765. static void eeepc_backlight_exit(void)
  766. {
  767. if (eeepc_backlight_device)
  768. backlight_device_unregister(eeepc_backlight_device);
  769. eeepc_backlight_device = NULL;
  770. }
  771. static void eeepc_rfkill_exit(void)
  772. {
  773. if (ehotk->eeepc_wlan_rfkill)
  774. rfkill_unregister(ehotk->eeepc_wlan_rfkill);
  775. if (ehotk->eeepc_bluetooth_rfkill)
  776. rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
  777. }
  778. static void eeepc_input_exit(void)
  779. {
  780. if (ehotk->inputdev)
  781. input_unregister_device(ehotk->inputdev);
  782. }
  783. static void eeepc_hwmon_exit(void)
  784. {
  785. struct device *hwmon;
  786. hwmon = eeepc_hwmon_device;
  787. if (!hwmon)
  788. return ;
  789. sysfs_remove_group(&hwmon->kobj,
  790. &hwmon_attribute_group);
  791. hwmon_device_unregister(hwmon);
  792. eeepc_hwmon_device = NULL;
  793. }
  794. static void __exit eeepc_laptop_exit(void)
  795. {
  796. eeepc_backlight_exit();
  797. eeepc_rfkill_exit();
  798. eeepc_input_exit();
  799. eeepc_hwmon_exit();
  800. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  801. sysfs_remove_group(&platform_device->dev.kobj,
  802. &platform_attribute_group);
  803. platform_device_unregister(platform_device);
  804. platform_driver_unregister(&platform_driver);
  805. }
  806. static int eeepc_backlight_init(struct device *dev)
  807. {
  808. struct backlight_device *bd;
  809. bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
  810. NULL, &eeepcbl_ops);
  811. if (IS_ERR(bd)) {
  812. printk(EEEPC_ERR
  813. "Could not register eeepc backlight device\n");
  814. eeepc_backlight_device = NULL;
  815. return PTR_ERR(bd);
  816. }
  817. eeepc_backlight_device = bd;
  818. bd->props.max_brightness = 15;
  819. bd->props.brightness = read_brightness(NULL);
  820. bd->props.power = FB_BLANK_UNBLANK;
  821. backlight_update_status(bd);
  822. return 0;
  823. }
  824. static int eeepc_hwmon_init(struct device *dev)
  825. {
  826. struct device *hwmon;
  827. int result;
  828. hwmon = hwmon_device_register(dev);
  829. if (IS_ERR(hwmon)) {
  830. printk(EEEPC_ERR
  831. "Could not register eeepc hwmon device\n");
  832. eeepc_hwmon_device = NULL;
  833. return PTR_ERR(hwmon);
  834. }
  835. eeepc_hwmon_device = hwmon;
  836. result = sysfs_create_group(&hwmon->kobj,
  837. &hwmon_attribute_group);
  838. if (result)
  839. eeepc_hwmon_exit();
  840. return result;
  841. }
  842. static int __init eeepc_laptop_init(void)
  843. {
  844. struct device *dev;
  845. int result;
  846. if (acpi_disabled)
  847. return -ENODEV;
  848. result = acpi_bus_register_driver(&eeepc_hotk_driver);
  849. if (result < 0)
  850. return result;
  851. if (!ehotk) {
  852. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  853. return -ENODEV;
  854. }
  855. dev = acpi_get_physical_device(ehotk->device->handle);
  856. if (!acpi_video_backlight_support()) {
  857. result = eeepc_backlight_init(dev);
  858. if (result)
  859. goto fail_backlight;
  860. } else
  861. printk(EEEPC_INFO "Backlight controlled by ACPI video "
  862. "driver\n");
  863. result = eeepc_hwmon_init(dev);
  864. if (result)
  865. goto fail_hwmon;
  866. /* Register platform stuff */
  867. result = platform_driver_register(&platform_driver);
  868. if (result)
  869. goto fail_platform_driver;
  870. platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
  871. if (!platform_device) {
  872. result = -ENOMEM;
  873. goto fail_platform_device1;
  874. }
  875. result = platform_device_add(platform_device);
  876. if (result)
  877. goto fail_platform_device2;
  878. result = sysfs_create_group(&platform_device->dev.kobj,
  879. &platform_attribute_group);
  880. if (result)
  881. goto fail_sysfs;
  882. return 0;
  883. fail_sysfs:
  884. platform_device_del(platform_device);
  885. fail_platform_device2:
  886. platform_device_put(platform_device);
  887. fail_platform_device1:
  888. platform_driver_unregister(&platform_driver);
  889. fail_platform_driver:
  890. eeepc_hwmon_exit();
  891. fail_hwmon:
  892. eeepc_backlight_exit();
  893. fail_backlight:
  894. eeepc_input_exit();
  895. eeepc_rfkill_exit();
  896. return result;
  897. }
  898. module_init(eeepc_laptop_init);
  899. module_exit(eeepc_laptop_exit);