eeepc-laptop.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. struct eeepc_cpufv {
  350. int num;
  351. int cur;
  352. };
  353. static int get_cpufv(struct eeepc_cpufv *c)
  354. {
  355. c->cur = get_acpi(CM_ASL_CPUFV);
  356. c->num = (c->cur >> 8) & 0xff;
  357. c->cur &= 0xff;
  358. if (c->cur < 0 || c->num <= 0 || c->num > 12)
  359. return -ENODEV;
  360. return 0;
  361. }
  362. static ssize_t show_available_cpufv(struct device *dev,
  363. struct device_attribute *attr,
  364. char *buf)
  365. {
  366. struct eeepc_cpufv c;
  367. int i;
  368. ssize_t len = 0;
  369. if (get_cpufv(&c))
  370. return -ENODEV;
  371. for (i = 0; i < c.num; i++)
  372. len += sprintf(buf + len, "%d ", i);
  373. len += sprintf(buf + len, "\n");
  374. return len;
  375. }
  376. static ssize_t show_cpufv(struct device *dev,
  377. struct device_attribute *attr,
  378. char *buf)
  379. {
  380. struct eeepc_cpufv c;
  381. if (get_cpufv(&c))
  382. return -ENODEV;
  383. return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
  384. }
  385. static ssize_t store_cpufv(struct device *dev,
  386. struct device_attribute *attr,
  387. const char *buf, size_t count)
  388. {
  389. struct eeepc_cpufv c;
  390. int rv, value;
  391. if (get_cpufv(&c))
  392. return -ENODEV;
  393. rv = parse_arg(buf, count, &value);
  394. if (rv < 0)
  395. return rv;
  396. if (!rv || value < 0 || value >= c.num)
  397. return -EINVAL;
  398. set_acpi(CM_ASL_CPUFV, value);
  399. return rv;
  400. }
  401. static struct device_attribute dev_attr_cpufv = {
  402. .attr = {
  403. .name = "cpufv",
  404. .mode = 0644 },
  405. .show = show_cpufv,
  406. .store = store_cpufv
  407. };
  408. static struct device_attribute dev_attr_available_cpufv = {
  409. .attr = {
  410. .name = "available_cpufv",
  411. .mode = 0444 },
  412. .show = show_available_cpufv
  413. };
  414. static struct attribute *platform_attributes[] = {
  415. &dev_attr_camera.attr,
  416. &dev_attr_cardr.attr,
  417. &dev_attr_disp.attr,
  418. &dev_attr_cpufv.attr,
  419. &dev_attr_available_cpufv.attr,
  420. NULL
  421. };
  422. static struct attribute_group platform_attribute_group = {
  423. .attrs = platform_attributes
  424. };
  425. /*
  426. * Hotkey functions
  427. */
  428. static struct key_entry *eepc_get_entry_by_scancode(int code)
  429. {
  430. struct key_entry *key;
  431. for (key = eeepc_keymap; key->type != KE_END; key++)
  432. if (code == key->code)
  433. return key;
  434. return NULL;
  435. }
  436. static struct key_entry *eepc_get_entry_by_keycode(int code)
  437. {
  438. struct key_entry *key;
  439. for (key = eeepc_keymap; key->type != KE_END; key++)
  440. if (code == key->keycode && key->type == KE_KEY)
  441. return key;
  442. return NULL;
  443. }
  444. static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  445. {
  446. struct key_entry *key = eepc_get_entry_by_scancode(scancode);
  447. if (key && key->type == KE_KEY) {
  448. *keycode = key->keycode;
  449. return 0;
  450. }
  451. return -EINVAL;
  452. }
  453. static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
  454. {
  455. struct key_entry *key;
  456. int old_keycode;
  457. if (keycode < 0 || keycode > KEY_MAX)
  458. return -EINVAL;
  459. key = eepc_get_entry_by_scancode(scancode);
  460. if (key && key->type == KE_KEY) {
  461. old_keycode = key->keycode;
  462. key->keycode = keycode;
  463. set_bit(keycode, dev->keybit);
  464. if (!eepc_get_entry_by_keycode(old_keycode))
  465. clear_bit(old_keycode, dev->keybit);
  466. return 0;
  467. }
  468. return -EINVAL;
  469. }
  470. static int eeepc_hotk_check(void)
  471. {
  472. const struct key_entry *key;
  473. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  474. int result;
  475. result = acpi_bus_get_status(ehotk->device);
  476. if (result)
  477. return result;
  478. if (ehotk->device->status.present) {
  479. if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
  480. &buffer)) {
  481. printk(EEEPC_ERR "Hotkey initialization failed\n");
  482. return -ENODEV;
  483. } else {
  484. printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
  485. ehotk->init_flag);
  486. }
  487. /* get control methods supported */
  488. if (read_acpi_int(ehotk->handle, "CMSG"
  489. , &ehotk->cm_supported)) {
  490. printk(EEEPC_ERR
  491. "Get control methods supported failed\n");
  492. return -ENODEV;
  493. } else {
  494. printk(EEEPC_INFO
  495. "Get control methods supported: 0x%x\n",
  496. ehotk->cm_supported);
  497. }
  498. ehotk->inputdev = input_allocate_device();
  499. if (!ehotk->inputdev) {
  500. printk(EEEPC_INFO "Unable to allocate input device\n");
  501. return 0;
  502. }
  503. ehotk->inputdev->name = "Asus EeePC extra buttons";
  504. ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
  505. ehotk->inputdev->id.bustype = BUS_HOST;
  506. ehotk->inputdev->getkeycode = eeepc_getkeycode;
  507. ehotk->inputdev->setkeycode = eeepc_setkeycode;
  508. for (key = eeepc_keymap; key->type != KE_END; key++) {
  509. switch (key->type) {
  510. case KE_KEY:
  511. set_bit(EV_KEY, ehotk->inputdev->evbit);
  512. set_bit(key->keycode, ehotk->inputdev->keybit);
  513. break;
  514. }
  515. }
  516. result = input_register_device(ehotk->inputdev);
  517. if (result) {
  518. printk(EEEPC_INFO "Unable to register input device\n");
  519. input_free_device(ehotk->inputdev);
  520. return 0;
  521. }
  522. } else {
  523. printk(EEEPC_ERR "Hotkey device not present, aborting\n");
  524. return -EINVAL;
  525. }
  526. return 0;
  527. }
  528. static int notify_brn(void)
  529. {
  530. /* returns the *previous* brightness, or -1 */
  531. struct backlight_device *bd = eeepc_backlight_device;
  532. if (bd) {
  533. int old = bd->props.brightness;
  534. bd->props.brightness = read_brightness(bd);
  535. return old;
  536. }
  537. return -1;
  538. }
  539. static void eeepc_rfkill_hotplug(void)
  540. {
  541. struct pci_dev *dev;
  542. struct pci_bus *bus = pci_find_bus(0, 1);
  543. bool blocked;
  544. if (!bus) {
  545. printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
  546. return;
  547. }
  548. blocked = eeepc_wlan_rfkill_blocked();
  549. if (!blocked) {
  550. dev = pci_get_slot(bus, 0);
  551. if (dev) {
  552. /* Device already present */
  553. pci_dev_put(dev);
  554. return;
  555. }
  556. dev = pci_scan_single_device(bus, 0);
  557. if (dev) {
  558. pci_bus_assign_resources(bus);
  559. if (pci_bus_add_device(dev))
  560. printk(EEEPC_ERR "Unable to hotplug wifi\n");
  561. }
  562. } else {
  563. dev = pci_get_slot(bus, 0);
  564. if (dev) {
  565. pci_remove_bus_device(dev);
  566. pci_dev_put(dev);
  567. }
  568. }
  569. rfkill_set_sw_state(ehotk->eeepc_wlan_rfkill, blocked);
  570. }
  571. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  572. {
  573. if (event != ACPI_NOTIFY_BUS_CHECK)
  574. return;
  575. eeepc_rfkill_hotplug();
  576. }
  577. static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
  578. {
  579. static struct key_entry *key;
  580. u16 count;
  581. int brn = -ENODEV;
  582. if (!ehotk)
  583. return;
  584. if (event > ACPI_MAX_SYS_NOTIFY)
  585. return;
  586. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
  587. brn = notify_brn();
  588. count = ehotk->event_count[event % 128]++;
  589. acpi_bus_generate_proc_event(ehotk->device, event, count);
  590. acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
  591. dev_name(&ehotk->device->dev), event,
  592. count);
  593. if (ehotk->inputdev) {
  594. if (brn != -ENODEV) {
  595. /* brightness-change events need special
  596. * handling for conversion to key events
  597. */
  598. if (brn < 0)
  599. brn = event;
  600. else
  601. brn += NOTIFY_BRN_MIN;
  602. if (event < brn)
  603. event = NOTIFY_BRN_MIN; /* brightness down */
  604. else if (event > brn)
  605. event = NOTIFY_BRN_MIN + 2; /* ... up */
  606. else
  607. event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
  608. }
  609. key = eepc_get_entry_by_scancode(event);
  610. if (key) {
  611. switch (key->type) {
  612. case KE_KEY:
  613. input_report_key(ehotk->inputdev, key->keycode,
  614. 1);
  615. input_sync(ehotk->inputdev);
  616. input_report_key(ehotk->inputdev, key->keycode,
  617. 0);
  618. input_sync(ehotk->inputdev);
  619. break;
  620. }
  621. }
  622. }
  623. }
  624. static int eeepc_register_rfkill_notifier(char *node)
  625. {
  626. acpi_status status = AE_OK;
  627. acpi_handle handle;
  628. status = acpi_get_handle(NULL, node, &handle);
  629. if (ACPI_SUCCESS(status)) {
  630. status = acpi_install_notify_handler(handle,
  631. ACPI_SYSTEM_NOTIFY,
  632. eeepc_rfkill_notify,
  633. NULL);
  634. if (ACPI_FAILURE(status))
  635. printk(EEEPC_WARNING
  636. "Failed to register notify on %s\n", node);
  637. } else
  638. return -ENODEV;
  639. return 0;
  640. }
  641. static void eeepc_unregister_rfkill_notifier(char *node)
  642. {
  643. acpi_status status = AE_OK;
  644. acpi_handle handle;
  645. status = acpi_get_handle(NULL, node, &handle);
  646. if (ACPI_SUCCESS(status)) {
  647. status = acpi_remove_notify_handler(handle,
  648. ACPI_SYSTEM_NOTIFY,
  649. eeepc_rfkill_notify);
  650. if (ACPI_FAILURE(status))
  651. printk(EEEPC_ERR
  652. "Error removing rfkill notify handler %s\n",
  653. node);
  654. }
  655. }
  656. static int eeepc_hotk_add(struct acpi_device *device)
  657. {
  658. int result;
  659. if (!device)
  660. return -EINVAL;
  661. printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
  662. ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
  663. if (!ehotk)
  664. return -ENOMEM;
  665. ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  666. ehotk->handle = device->handle;
  667. strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
  668. strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
  669. device->driver_data = ehotk;
  670. ehotk->device = device;
  671. result = eeepc_hotk_check();
  672. if (result)
  673. goto ehotk_fail;
  674. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
  675. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
  676. if (get_acpi(CM_ASL_WLAN) != -1) {
  677. ehotk->eeepc_wlan_rfkill = rfkill_alloc("eeepc-wlan",
  678. &device->dev,
  679. RFKILL_TYPE_WLAN,
  680. &eeepc_rfkill_ops,
  681. (void *)CM_ASL_WLAN);
  682. if (!ehotk->eeepc_wlan_rfkill)
  683. goto wlan_fail;
  684. rfkill_init_sw_state(ehotk->eeepc_wlan_rfkill,
  685. get_acpi(CM_ASL_WLAN) != 1);
  686. result = rfkill_register(ehotk->eeepc_wlan_rfkill);
  687. if (result)
  688. goto wlan_fail;
  689. }
  690. if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
  691. ehotk->eeepc_bluetooth_rfkill =
  692. rfkill_alloc("eeepc-bluetooth",
  693. &device->dev,
  694. RFKILL_TYPE_BLUETOOTH,
  695. &eeepc_rfkill_ops,
  696. (void *)CM_ASL_BLUETOOTH);
  697. if (!ehotk->eeepc_bluetooth_rfkill)
  698. goto bluetooth_fail;
  699. rfkill_init_sw_state(ehotk->eeepc_bluetooth_rfkill,
  700. get_acpi(CM_ASL_BLUETOOTH) != 1);
  701. result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
  702. if (result)
  703. goto bluetooth_fail;
  704. }
  705. return 0;
  706. bluetooth_fail:
  707. rfkill_destroy(ehotk->eeepc_bluetooth_rfkill);
  708. rfkill_unregister(ehotk->eeepc_wlan_rfkill);
  709. wlan_fail:
  710. rfkill_destroy(ehotk->eeepc_wlan_rfkill);
  711. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
  712. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  713. ehotk_fail:
  714. kfree(ehotk);
  715. ehotk = NULL;
  716. return result;
  717. }
  718. static int eeepc_hotk_remove(struct acpi_device *device, int type)
  719. {
  720. if (!device || !acpi_driver_data(device))
  721. return -EINVAL;
  722. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
  723. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  724. kfree(ehotk);
  725. return 0;
  726. }
  727. static int eeepc_hotk_resume(struct acpi_device *device)
  728. {
  729. if (ehotk->eeepc_wlan_rfkill) {
  730. bool wlan;
  731. /* Workaround - it seems that _PTS disables the wireless
  732. without notification or changing the value read by WLAN.
  733. Normally this is fine because the correct value is restored
  734. from the non-volatile storage on resume, but we need to do
  735. it ourself if case suspend is aborted, or we lose wireless.
  736. */
  737. wlan = get_acpi(CM_ASL_WLAN);
  738. set_acpi(CM_ASL_WLAN, wlan);
  739. rfkill_set_sw_state(ehotk->eeepc_wlan_rfkill,
  740. wlan != 1);
  741. eeepc_rfkill_hotplug();
  742. }
  743. if (ehotk->eeepc_bluetooth_rfkill)
  744. rfkill_set_sw_state(ehotk->eeepc_bluetooth_rfkill,
  745. get_acpi(CM_ASL_BLUETOOTH) != 1);
  746. return 0;
  747. }
  748. /*
  749. * Hwmon
  750. */
  751. static int eeepc_get_fan_pwm(void)
  752. {
  753. int value = 0;
  754. read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
  755. value = value * 255 / 100;
  756. return (value);
  757. }
  758. static void eeepc_set_fan_pwm(int value)
  759. {
  760. value = SENSORS_LIMIT(value, 0, 255);
  761. value = value * 100 / 255;
  762. ec_write(EEEPC_EC_SC02, value);
  763. }
  764. static int eeepc_get_fan_rpm(void)
  765. {
  766. int high = 0;
  767. int low = 0;
  768. read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
  769. read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
  770. return (high << 8 | low);
  771. }
  772. static int eeepc_get_fan_ctrl(void)
  773. {
  774. int value = 0;
  775. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  776. return ((value & 0x02 ? 1 : 0));
  777. }
  778. static void eeepc_set_fan_ctrl(int manual)
  779. {
  780. int value = 0;
  781. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  782. if (manual)
  783. value |= 0x02;
  784. else
  785. value &= ~0x02;
  786. ec_write(EEEPC_EC_SFB3, value);
  787. }
  788. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  789. {
  790. int rv, value;
  791. rv = parse_arg(buf, count, &value);
  792. if (rv > 0)
  793. set(value);
  794. return rv;
  795. }
  796. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  797. {
  798. return sprintf(buf, "%d\n", get());
  799. }
  800. #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
  801. static ssize_t show_##_name(struct device *dev, \
  802. struct device_attribute *attr, \
  803. char *buf) \
  804. { \
  805. return show_sys_hwmon(_set, buf); \
  806. } \
  807. static ssize_t store_##_name(struct device *dev, \
  808. struct device_attribute *attr, \
  809. const char *buf, size_t count) \
  810. { \
  811. return store_sys_hwmon(_get, buf, count); \
  812. } \
  813. static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
  814. EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
  815. EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
  816. eeepc_get_fan_pwm, eeepc_set_fan_pwm);
  817. EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  818. eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
  819. static ssize_t
  820. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  821. {
  822. return sprintf(buf, "eeepc\n");
  823. }
  824. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  825. static struct attribute *hwmon_attributes[] = {
  826. &sensor_dev_attr_pwm1.dev_attr.attr,
  827. &sensor_dev_attr_fan1_input.dev_attr.attr,
  828. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  829. &sensor_dev_attr_name.dev_attr.attr,
  830. NULL
  831. };
  832. static struct attribute_group hwmon_attribute_group = {
  833. .attrs = hwmon_attributes
  834. };
  835. /*
  836. * exit/init
  837. */
  838. static void eeepc_backlight_exit(void)
  839. {
  840. if (eeepc_backlight_device)
  841. backlight_device_unregister(eeepc_backlight_device);
  842. eeepc_backlight_device = NULL;
  843. }
  844. static void eeepc_rfkill_exit(void)
  845. {
  846. if (ehotk->eeepc_wlan_rfkill)
  847. rfkill_unregister(ehotk->eeepc_wlan_rfkill);
  848. if (ehotk->eeepc_bluetooth_rfkill)
  849. rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
  850. }
  851. static void eeepc_input_exit(void)
  852. {
  853. if (ehotk->inputdev)
  854. input_unregister_device(ehotk->inputdev);
  855. }
  856. static void eeepc_hwmon_exit(void)
  857. {
  858. struct device *hwmon;
  859. hwmon = eeepc_hwmon_device;
  860. if (!hwmon)
  861. return ;
  862. sysfs_remove_group(&hwmon->kobj,
  863. &hwmon_attribute_group);
  864. hwmon_device_unregister(hwmon);
  865. eeepc_hwmon_device = NULL;
  866. }
  867. static void __exit eeepc_laptop_exit(void)
  868. {
  869. eeepc_backlight_exit();
  870. eeepc_rfkill_exit();
  871. eeepc_input_exit();
  872. eeepc_hwmon_exit();
  873. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  874. sysfs_remove_group(&platform_device->dev.kobj,
  875. &platform_attribute_group);
  876. platform_device_unregister(platform_device);
  877. platform_driver_unregister(&platform_driver);
  878. }
  879. static int eeepc_backlight_init(struct device *dev)
  880. {
  881. struct backlight_device *bd;
  882. bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
  883. NULL, &eeepcbl_ops);
  884. if (IS_ERR(bd)) {
  885. printk(EEEPC_ERR
  886. "Could not register eeepc backlight device\n");
  887. eeepc_backlight_device = NULL;
  888. return PTR_ERR(bd);
  889. }
  890. eeepc_backlight_device = bd;
  891. bd->props.max_brightness = 15;
  892. bd->props.brightness = read_brightness(NULL);
  893. bd->props.power = FB_BLANK_UNBLANK;
  894. backlight_update_status(bd);
  895. return 0;
  896. }
  897. static int eeepc_hwmon_init(struct device *dev)
  898. {
  899. struct device *hwmon;
  900. int result;
  901. hwmon = hwmon_device_register(dev);
  902. if (IS_ERR(hwmon)) {
  903. printk(EEEPC_ERR
  904. "Could not register eeepc hwmon device\n");
  905. eeepc_hwmon_device = NULL;
  906. return PTR_ERR(hwmon);
  907. }
  908. eeepc_hwmon_device = hwmon;
  909. result = sysfs_create_group(&hwmon->kobj,
  910. &hwmon_attribute_group);
  911. if (result)
  912. eeepc_hwmon_exit();
  913. return result;
  914. }
  915. static int __init eeepc_laptop_init(void)
  916. {
  917. struct device *dev;
  918. int result;
  919. if (acpi_disabled)
  920. return -ENODEV;
  921. result = acpi_bus_register_driver(&eeepc_hotk_driver);
  922. if (result < 0)
  923. return result;
  924. if (!ehotk) {
  925. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  926. return -ENODEV;
  927. }
  928. dev = acpi_get_physical_device(ehotk->device->handle);
  929. if (!acpi_video_backlight_support()) {
  930. result = eeepc_backlight_init(dev);
  931. if (result)
  932. goto fail_backlight;
  933. } else
  934. printk(EEEPC_INFO "Backlight controlled by ACPI video "
  935. "driver\n");
  936. result = eeepc_hwmon_init(dev);
  937. if (result)
  938. goto fail_hwmon;
  939. eeepc_enable_camera();
  940. /* Register platform stuff */
  941. result = platform_driver_register(&platform_driver);
  942. if (result)
  943. goto fail_platform_driver;
  944. platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
  945. if (!platform_device) {
  946. result = -ENOMEM;
  947. goto fail_platform_device1;
  948. }
  949. result = platform_device_add(platform_device);
  950. if (result)
  951. goto fail_platform_device2;
  952. result = sysfs_create_group(&platform_device->dev.kobj,
  953. &platform_attribute_group);
  954. if (result)
  955. goto fail_sysfs;
  956. return 0;
  957. fail_sysfs:
  958. platform_device_del(platform_device);
  959. fail_platform_device2:
  960. platform_device_put(platform_device);
  961. fail_platform_device1:
  962. platform_driver_unregister(&platform_driver);
  963. fail_platform_driver:
  964. eeepc_hwmon_exit();
  965. fail_hwmon:
  966. eeepc_backlight_exit();
  967. fail_backlight:
  968. eeepc_input_exit();
  969. eeepc_rfkill_exit();
  970. return result;
  971. }
  972. module_init(eeepc_laptop_init);
  973. module_exit(eeepc_laptop_exit);