eeepc-laptop.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. EEEPC_CREATE_DEVICE_ATTR(cpufv, CM_ASL_CPUFV);
  342. static struct attribute *platform_attributes[] = {
  343. &dev_attr_camera.attr,
  344. &dev_attr_cardr.attr,
  345. &dev_attr_disp.attr,
  346. &dev_attr_cpufv.attr,
  347. NULL
  348. };
  349. static struct attribute_group platform_attribute_group = {
  350. .attrs = platform_attributes
  351. };
  352. /*
  353. * Hotkey functions
  354. */
  355. static struct key_entry *eepc_get_entry_by_scancode(int code)
  356. {
  357. struct key_entry *key;
  358. for (key = eeepc_keymap; key->type != KE_END; key++)
  359. if (code == key->code)
  360. return key;
  361. return NULL;
  362. }
  363. static struct key_entry *eepc_get_entry_by_keycode(int code)
  364. {
  365. struct key_entry *key;
  366. for (key = eeepc_keymap; key->type != KE_END; key++)
  367. if (code == key->keycode && key->type == KE_KEY)
  368. return key;
  369. return NULL;
  370. }
  371. static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  372. {
  373. struct key_entry *key = eepc_get_entry_by_scancode(scancode);
  374. if (key && key->type == KE_KEY) {
  375. *keycode = key->keycode;
  376. return 0;
  377. }
  378. return -EINVAL;
  379. }
  380. static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
  381. {
  382. struct key_entry *key;
  383. int old_keycode;
  384. if (keycode < 0 || keycode > KEY_MAX)
  385. return -EINVAL;
  386. key = eepc_get_entry_by_scancode(scancode);
  387. if (key && key->type == KE_KEY) {
  388. old_keycode = key->keycode;
  389. key->keycode = keycode;
  390. set_bit(keycode, dev->keybit);
  391. if (!eepc_get_entry_by_keycode(old_keycode))
  392. clear_bit(old_keycode, dev->keybit);
  393. return 0;
  394. }
  395. return -EINVAL;
  396. }
  397. static int eeepc_hotk_check(void)
  398. {
  399. const struct key_entry *key;
  400. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  401. int result;
  402. result = acpi_bus_get_status(ehotk->device);
  403. if (result)
  404. return result;
  405. if (ehotk->device->status.present) {
  406. if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
  407. &buffer)) {
  408. printk(EEEPC_ERR "Hotkey initialization failed\n");
  409. return -ENODEV;
  410. } else {
  411. printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
  412. ehotk->init_flag);
  413. }
  414. /* get control methods supported */
  415. if (read_acpi_int(ehotk->handle, "CMSG"
  416. , &ehotk->cm_supported)) {
  417. printk(EEEPC_ERR
  418. "Get control methods supported failed\n");
  419. return -ENODEV;
  420. } else {
  421. printk(EEEPC_INFO
  422. "Get control methods supported: 0x%x\n",
  423. ehotk->cm_supported);
  424. }
  425. ehotk->inputdev = input_allocate_device();
  426. if (!ehotk->inputdev) {
  427. printk(EEEPC_INFO "Unable to allocate input device\n");
  428. return 0;
  429. }
  430. ehotk->inputdev->name = "Asus EeePC extra buttons";
  431. ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
  432. ehotk->inputdev->id.bustype = BUS_HOST;
  433. ehotk->inputdev->getkeycode = eeepc_getkeycode;
  434. ehotk->inputdev->setkeycode = eeepc_setkeycode;
  435. for (key = eeepc_keymap; key->type != KE_END; key++) {
  436. switch (key->type) {
  437. case KE_KEY:
  438. set_bit(EV_KEY, ehotk->inputdev->evbit);
  439. set_bit(key->keycode, ehotk->inputdev->keybit);
  440. break;
  441. }
  442. }
  443. result = input_register_device(ehotk->inputdev);
  444. if (result) {
  445. printk(EEEPC_INFO "Unable to register input device\n");
  446. input_free_device(ehotk->inputdev);
  447. return 0;
  448. }
  449. } else {
  450. printk(EEEPC_ERR "Hotkey device not present, aborting\n");
  451. return -EINVAL;
  452. }
  453. return 0;
  454. }
  455. static int notify_brn(void)
  456. {
  457. /* returns the *previous* brightness, or -1 */
  458. struct backlight_device *bd = eeepc_backlight_device;
  459. if (bd) {
  460. int old = bd->props.brightness;
  461. bd->props.brightness = read_brightness(bd);
  462. return old;
  463. }
  464. return -1;
  465. }
  466. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  467. {
  468. enum rfkill_state state;
  469. struct pci_dev *dev;
  470. struct pci_bus *bus = pci_find_bus(0, 1);
  471. if (event != ACPI_NOTIFY_BUS_CHECK)
  472. return;
  473. if (!bus) {
  474. printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
  475. return;
  476. }
  477. eeepc_wlan_rfkill_state(ehotk->eeepc_wlan_rfkill, &state);
  478. if (state == RFKILL_STATE_UNBLOCKED) {
  479. dev = pci_get_slot(bus, 0);
  480. if (dev) {
  481. /* Device already present */
  482. pci_dev_put(dev);
  483. return;
  484. }
  485. dev = pci_scan_single_device(bus, 0);
  486. if (dev) {
  487. pci_bus_assign_resources(bus);
  488. if (pci_bus_add_device(dev))
  489. printk(EEEPC_ERR "Unable to hotplug wifi\n");
  490. }
  491. } else {
  492. dev = pci_get_slot(bus, 0);
  493. if (dev) {
  494. pci_remove_bus_device(dev);
  495. pci_dev_put(dev);
  496. }
  497. }
  498. rfkill_force_state(ehotk->eeepc_wlan_rfkill, state);
  499. }
  500. static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
  501. {
  502. static struct key_entry *key;
  503. u16 count;
  504. int brn = -ENODEV;
  505. if (!ehotk)
  506. return;
  507. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
  508. brn = notify_brn();
  509. count = ehotk->event_count[event % 128]++;
  510. acpi_bus_generate_proc_event(ehotk->device, event, count);
  511. acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
  512. dev_name(&ehotk->device->dev), event,
  513. count);
  514. if (ehotk->inputdev) {
  515. if (brn != -ENODEV) {
  516. /* brightness-change events need special
  517. * handling for conversion to key events
  518. */
  519. if (brn < 0)
  520. brn = event;
  521. else
  522. brn += NOTIFY_BRN_MIN;
  523. if (event < brn)
  524. event = NOTIFY_BRN_MIN; /* brightness down */
  525. else if (event > brn)
  526. event = NOTIFY_BRN_MIN + 2; /* ... up */
  527. else
  528. event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
  529. }
  530. key = eepc_get_entry_by_scancode(event);
  531. if (key) {
  532. switch (key->type) {
  533. case KE_KEY:
  534. input_report_key(ehotk->inputdev, key->keycode,
  535. 1);
  536. input_sync(ehotk->inputdev);
  537. input_report_key(ehotk->inputdev, key->keycode,
  538. 0);
  539. input_sync(ehotk->inputdev);
  540. break;
  541. }
  542. }
  543. }
  544. }
  545. static int eeepc_register_rfkill_notifier(char *node)
  546. {
  547. acpi_status status = AE_OK;
  548. acpi_handle handle;
  549. status = acpi_get_handle(NULL, node, &handle);
  550. if (ACPI_SUCCESS(status)) {
  551. status = acpi_install_notify_handler(handle,
  552. ACPI_SYSTEM_NOTIFY,
  553. eeepc_rfkill_notify,
  554. NULL);
  555. if (ACPI_FAILURE(status))
  556. printk(EEEPC_WARNING
  557. "Failed to register notify on %s\n", node);
  558. } else
  559. return -ENODEV;
  560. return 0;
  561. }
  562. static void eeepc_unregister_rfkill_notifier(char *node)
  563. {
  564. acpi_status status = AE_OK;
  565. acpi_handle handle;
  566. status = acpi_get_handle(NULL, node, &handle);
  567. if (ACPI_SUCCESS(status)) {
  568. status = acpi_remove_notify_handler(handle,
  569. ACPI_SYSTEM_NOTIFY,
  570. eeepc_rfkill_notify);
  571. if (ACPI_FAILURE(status))
  572. printk(EEEPC_ERR
  573. "Error removing rfkill notify handler %s\n",
  574. node);
  575. }
  576. }
  577. static int eeepc_hotk_add(struct acpi_device *device)
  578. {
  579. acpi_status status = AE_OK;
  580. int result;
  581. if (!device)
  582. return -EINVAL;
  583. printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
  584. ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
  585. if (!ehotk)
  586. return -ENOMEM;
  587. ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  588. ehotk->handle = device->handle;
  589. strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
  590. strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
  591. device->driver_data = ehotk;
  592. ehotk->device = device;
  593. result = eeepc_hotk_check();
  594. if (result)
  595. goto ehotk_fail;
  596. status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
  597. eeepc_hotk_notify, ehotk);
  598. if (ACPI_FAILURE(status))
  599. printk(EEEPC_ERR "Error installing notify handler\n");
  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. ehotk_fail:
  660. kfree(ehotk);
  661. ehotk = NULL;
  662. return result;
  663. }
  664. static int eeepc_hotk_remove(struct acpi_device *device, int type)
  665. {
  666. acpi_status status = 0;
  667. if (!device || !acpi_driver_data(device))
  668. return -EINVAL;
  669. status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
  670. eeepc_hotk_notify);
  671. if (ACPI_FAILURE(status))
  672. printk(EEEPC_ERR "Error removing notify handler\n");
  673. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
  674. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  675. kfree(ehotk);
  676. return 0;
  677. }
  678. /*
  679. * Hwmon
  680. */
  681. static int eeepc_get_fan_pwm(void)
  682. {
  683. int value = 0;
  684. read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
  685. value = value * 255 / 100;
  686. return (value);
  687. }
  688. static void eeepc_set_fan_pwm(int value)
  689. {
  690. value = SENSORS_LIMIT(value, 0, 255);
  691. value = value * 100 / 255;
  692. ec_write(EEEPC_EC_SC02, value);
  693. }
  694. static int eeepc_get_fan_rpm(void)
  695. {
  696. int high = 0;
  697. int low = 0;
  698. read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
  699. read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
  700. return (high << 8 | low);
  701. }
  702. static int eeepc_get_fan_ctrl(void)
  703. {
  704. int value = 0;
  705. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  706. return ((value & 0x02 ? 1 : 0));
  707. }
  708. static void eeepc_set_fan_ctrl(int manual)
  709. {
  710. int value = 0;
  711. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  712. if (manual)
  713. value |= 0x02;
  714. else
  715. value &= ~0x02;
  716. ec_write(EEEPC_EC_SFB3, value);
  717. }
  718. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  719. {
  720. int rv, value;
  721. rv = parse_arg(buf, count, &value);
  722. if (rv > 0)
  723. set(value);
  724. return rv;
  725. }
  726. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  727. {
  728. return sprintf(buf, "%d\n", get());
  729. }
  730. #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
  731. static ssize_t show_##_name(struct device *dev, \
  732. struct device_attribute *attr, \
  733. char *buf) \
  734. { \
  735. return show_sys_hwmon(_set, buf); \
  736. } \
  737. static ssize_t store_##_name(struct device *dev, \
  738. struct device_attribute *attr, \
  739. const char *buf, size_t count) \
  740. { \
  741. return store_sys_hwmon(_get, buf, count); \
  742. } \
  743. static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
  744. EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
  745. EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
  746. eeepc_get_fan_pwm, eeepc_set_fan_pwm);
  747. EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  748. eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
  749. static ssize_t
  750. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  751. {
  752. return sprintf(buf, "eeepc\n");
  753. }
  754. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  755. static struct attribute *hwmon_attributes[] = {
  756. &sensor_dev_attr_pwm1.dev_attr.attr,
  757. &sensor_dev_attr_fan1_input.dev_attr.attr,
  758. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  759. &sensor_dev_attr_name.dev_attr.attr,
  760. NULL
  761. };
  762. static struct attribute_group hwmon_attribute_group = {
  763. .attrs = hwmon_attributes
  764. };
  765. /*
  766. * exit/init
  767. */
  768. static void eeepc_backlight_exit(void)
  769. {
  770. if (eeepc_backlight_device)
  771. backlight_device_unregister(eeepc_backlight_device);
  772. eeepc_backlight_device = NULL;
  773. }
  774. static void eeepc_rfkill_exit(void)
  775. {
  776. if (ehotk->eeepc_wlan_rfkill)
  777. rfkill_unregister(ehotk->eeepc_wlan_rfkill);
  778. if (ehotk->eeepc_bluetooth_rfkill)
  779. rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
  780. }
  781. static void eeepc_input_exit(void)
  782. {
  783. if (ehotk->inputdev)
  784. input_unregister_device(ehotk->inputdev);
  785. }
  786. static void eeepc_hwmon_exit(void)
  787. {
  788. struct device *hwmon;
  789. hwmon = eeepc_hwmon_device;
  790. if (!hwmon)
  791. return ;
  792. sysfs_remove_group(&hwmon->kobj,
  793. &hwmon_attribute_group);
  794. hwmon_device_unregister(hwmon);
  795. eeepc_hwmon_device = NULL;
  796. }
  797. static void __exit eeepc_laptop_exit(void)
  798. {
  799. eeepc_backlight_exit();
  800. eeepc_rfkill_exit();
  801. eeepc_input_exit();
  802. eeepc_hwmon_exit();
  803. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  804. sysfs_remove_group(&platform_device->dev.kobj,
  805. &platform_attribute_group);
  806. platform_device_unregister(platform_device);
  807. platform_driver_unregister(&platform_driver);
  808. }
  809. static int eeepc_backlight_init(struct device *dev)
  810. {
  811. struct backlight_device *bd;
  812. bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
  813. NULL, &eeepcbl_ops);
  814. if (IS_ERR(bd)) {
  815. printk(EEEPC_ERR
  816. "Could not register eeepc backlight device\n");
  817. eeepc_backlight_device = NULL;
  818. return PTR_ERR(bd);
  819. }
  820. eeepc_backlight_device = bd;
  821. bd->props.max_brightness = 15;
  822. bd->props.brightness = read_brightness(NULL);
  823. bd->props.power = FB_BLANK_UNBLANK;
  824. backlight_update_status(bd);
  825. return 0;
  826. }
  827. static int eeepc_hwmon_init(struct device *dev)
  828. {
  829. struct device *hwmon;
  830. int result;
  831. hwmon = hwmon_device_register(dev);
  832. if (IS_ERR(hwmon)) {
  833. printk(EEEPC_ERR
  834. "Could not register eeepc hwmon device\n");
  835. eeepc_hwmon_device = NULL;
  836. return PTR_ERR(hwmon);
  837. }
  838. eeepc_hwmon_device = hwmon;
  839. result = sysfs_create_group(&hwmon->kobj,
  840. &hwmon_attribute_group);
  841. if (result)
  842. eeepc_hwmon_exit();
  843. return result;
  844. }
  845. static int __init eeepc_laptop_init(void)
  846. {
  847. struct device *dev;
  848. int result;
  849. if (acpi_disabled)
  850. return -ENODEV;
  851. result = acpi_bus_register_driver(&eeepc_hotk_driver);
  852. if (result < 0)
  853. return result;
  854. if (!ehotk) {
  855. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  856. return -ENODEV;
  857. }
  858. dev = acpi_get_physical_device(ehotk->device->handle);
  859. if (!acpi_video_backlight_support()) {
  860. result = eeepc_backlight_init(dev);
  861. if (result)
  862. goto fail_backlight;
  863. } else
  864. printk(EEEPC_INFO "Backlight controlled by ACPI video "
  865. "driver\n");
  866. result = eeepc_hwmon_init(dev);
  867. if (result)
  868. goto fail_hwmon;
  869. /* Register platform stuff */
  870. result = platform_driver_register(&platform_driver);
  871. if (result)
  872. goto fail_platform_driver;
  873. platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
  874. if (!platform_device) {
  875. result = -ENOMEM;
  876. goto fail_platform_device1;
  877. }
  878. result = platform_device_add(platform_device);
  879. if (result)
  880. goto fail_platform_device2;
  881. result = sysfs_create_group(&platform_device->dev.kobj,
  882. &platform_attribute_group);
  883. if (result)
  884. goto fail_sysfs;
  885. return 0;
  886. fail_sysfs:
  887. platform_device_del(platform_device);
  888. fail_platform_device2:
  889. platform_device_put(platform_device);
  890. fail_platform_device1:
  891. platform_driver_unregister(&platform_driver);
  892. fail_platform_driver:
  893. eeepc_hwmon_exit();
  894. fail_hwmon:
  895. eeepc_backlight_exit();
  896. fail_backlight:
  897. eeepc_input_exit();
  898. eeepc_rfkill_exit();
  899. return result;
  900. }
  901. module_init(eeepc_laptop_init);
  902. module_exit(eeepc_laptop_exit);