eeepc-laptop.c 25 KB

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