eeepc-laptop.c 25 KB

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