eeepc-laptop.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/backlight.h>
  25. #include <linux/fb.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <acpi/acpi_drivers.h>
  29. #include <acpi/acpi_bus.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/input.h>
  32. #include <linux/rfkill.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci_hotplug.h>
  35. #define EEEPC_LAPTOP_VERSION "0.1"
  36. #define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
  37. #define EEEPC_HOTK_FILE "eeepc"
  38. #define EEEPC_HOTK_CLASS "hotkey"
  39. #define EEEPC_HOTK_DEVICE_NAME "Hotkey"
  40. #define EEEPC_HOTK_HID "ASUS010"
  41. /*
  42. * Definitions for Asus EeePC
  43. */
  44. #define NOTIFY_WLAN_ON 0x10
  45. #define NOTIFY_BRN_MIN 0x20
  46. #define NOTIFY_BRN_MAX 0x2f
  47. enum {
  48. DISABLE_ASL_WLAN = 0x0001,
  49. DISABLE_ASL_BLUETOOTH = 0x0002,
  50. DISABLE_ASL_IRDA = 0x0004,
  51. DISABLE_ASL_CAMERA = 0x0008,
  52. DISABLE_ASL_TV = 0x0010,
  53. DISABLE_ASL_GPS = 0x0020,
  54. DISABLE_ASL_DISPLAYSWITCH = 0x0040,
  55. DISABLE_ASL_MODEM = 0x0080,
  56. DISABLE_ASL_CARDREADER = 0x0100,
  57. DISABLE_ASL_3G = 0x0200,
  58. DISABLE_ASL_WIMAX = 0x0400,
  59. DISABLE_ASL_HWCF = 0x0800
  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_3G,
  84. CM_ASL_WIMAX,
  85. CM_ASL_HWCF,
  86. CM_ASL_LID,
  87. CM_ASL_TYPE,
  88. CM_ASL_PANELPOWER, /*P901*/
  89. CM_ASL_TPD
  90. };
  91. static const char *cm_getv[] = {
  92. "WLDG", "BTHG", NULL, NULL,
  93. "CAMG", NULL, NULL, NULL,
  94. NULL, "PBLG", NULL, NULL,
  95. "CFVG", NULL, NULL, NULL,
  96. "USBG", NULL, NULL, "MODG",
  97. "CRDG", "M3GG", "WIMG", "HWCF",
  98. "LIDG", "TYPE", "PBPG", "TPDG"
  99. };
  100. static const char *cm_setv[] = {
  101. "WLDS", "BTHS", NULL, NULL,
  102. "CAMS", NULL, NULL, NULL,
  103. "SDSP", "PBLS", "HDPS", NULL,
  104. "CFVS", NULL, NULL, NULL,
  105. "USBG", NULL, NULL, "MODS",
  106. "CRDS", "M3GS", "WIMS", NULL,
  107. NULL, NULL, "PBPS", "TPDS"
  108. };
  109. #define EEEPC_EC "\\_SB.PCI0.SBRG.EC0."
  110. #define EEEPC_EC_FAN_PWM EEEPC_EC "SC02" /* Fan PWM duty cycle (%) */
  111. #define EEEPC_EC_SC02 0x63
  112. #define EEEPC_EC_FAN_HRPM EEEPC_EC "SC05" /* High byte, fan speed (RPM) */
  113. #define EEEPC_EC_FAN_LRPM EEEPC_EC "SC06" /* Low byte, fan speed (RPM) */
  114. #define EEEPC_EC_FAN_CTRL EEEPC_EC "SFB3" /* Byte containing SF25 */
  115. #define EEEPC_EC_SFB3 0xD3
  116. /*
  117. * This is the main structure, we can use it to store useful information
  118. * about the hotk device
  119. */
  120. struct eeepc_hotk {
  121. struct acpi_device *device; /* the device we are in */
  122. acpi_handle handle; /* the handle of the hotk device */
  123. u32 cm_supported; /* the control methods supported
  124. by this BIOS */
  125. uint init_flag; /* Init flags */
  126. u16 event_count[128]; /* count for each event */
  127. struct input_dev *inputdev;
  128. u16 *keycode_map;
  129. struct rfkill *wlan_rfkill;
  130. struct rfkill *bluetooth_rfkill;
  131. struct rfkill *wwan3g_rfkill;
  132. struct hotplug_slot *hotplug_slot;
  133. };
  134. /* The actual device the driver binds to */
  135. static struct eeepc_hotk *ehotk;
  136. /* Platform device/driver */
  137. static struct platform_driver platform_driver = {
  138. .driver = {
  139. .name = EEEPC_HOTK_FILE,
  140. .owner = THIS_MODULE,
  141. }
  142. };
  143. static struct platform_device *platform_device;
  144. struct key_entry {
  145. char type;
  146. u8 code;
  147. u16 keycode;
  148. };
  149. enum { KE_KEY, KE_END };
  150. static struct key_entry eeepc_keymap[] = {
  151. /* Sleep already handled via generic ACPI code */
  152. {KE_KEY, 0x10, KEY_WLAN },
  153. {KE_KEY, 0x11, KEY_WLAN },
  154. {KE_KEY, 0x12, KEY_PROG1 },
  155. {KE_KEY, 0x13, KEY_MUTE },
  156. {KE_KEY, 0x14, KEY_VOLUMEDOWN },
  157. {KE_KEY, 0x15, KEY_VOLUMEUP },
  158. {KE_KEY, 0x1a, KEY_COFFEE },
  159. {KE_KEY, 0x1b, KEY_ZOOM },
  160. {KE_KEY, 0x1c, KEY_PROG2 },
  161. {KE_KEY, 0x1d, KEY_PROG3 },
  162. {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
  163. {KE_KEY, NOTIFY_BRN_MIN + 2, KEY_BRIGHTNESSUP },
  164. {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
  165. {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
  166. {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
  167. {KE_END, 0},
  168. };
  169. /*
  170. * The hotkey driver declaration
  171. */
  172. static int eeepc_hotk_add(struct acpi_device *device);
  173. static int eeepc_hotk_remove(struct acpi_device *device, int type);
  174. static int eeepc_hotk_resume(struct acpi_device *device);
  175. static void eeepc_hotk_notify(struct acpi_device *device, u32 event);
  176. static const struct acpi_device_id eeepc_device_ids[] = {
  177. {EEEPC_HOTK_HID, 0},
  178. {"", 0},
  179. };
  180. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  181. static struct acpi_driver eeepc_hotk_driver = {
  182. .name = EEEPC_HOTK_NAME,
  183. .class = EEEPC_HOTK_CLASS,
  184. .ids = eeepc_device_ids,
  185. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  186. .ops = {
  187. .add = eeepc_hotk_add,
  188. .remove = eeepc_hotk_remove,
  189. .resume = eeepc_hotk_resume,
  190. .notify = eeepc_hotk_notify,
  191. },
  192. };
  193. /* PCI hotplug ops */
  194. static int eeepc_get_adapter_status(struct hotplug_slot *slot, u8 *value);
  195. static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
  196. .owner = THIS_MODULE,
  197. .get_adapter_status = eeepc_get_adapter_status,
  198. .get_power_status = eeepc_get_adapter_status,
  199. };
  200. /* The backlight device /sys/class/backlight */
  201. static struct backlight_device *eeepc_backlight_device;
  202. /* The hwmon device */
  203. static struct device *eeepc_hwmon_device;
  204. /*
  205. * The backlight class declaration
  206. */
  207. static int read_brightness(struct backlight_device *bd);
  208. static int update_bl_status(struct backlight_device *bd);
  209. static struct backlight_ops eeepcbl_ops = {
  210. .get_brightness = read_brightness,
  211. .update_status = update_bl_status,
  212. };
  213. MODULE_AUTHOR("Corentin Chary, Eric Cooper");
  214. MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
  215. MODULE_LICENSE("GPL");
  216. /*
  217. * ACPI Helpers
  218. */
  219. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  220. struct acpi_buffer *output)
  221. {
  222. struct acpi_object_list params;
  223. union acpi_object in_obj;
  224. acpi_status status;
  225. params.count = 1;
  226. params.pointer = &in_obj;
  227. in_obj.type = ACPI_TYPE_INTEGER;
  228. in_obj.integer.value = val;
  229. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  230. return (status == AE_OK ? 0 : -1);
  231. }
  232. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  233. {
  234. acpi_status status;
  235. unsigned long long result;
  236. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  237. if (ACPI_FAILURE(status)) {
  238. *val = -1;
  239. return -1;
  240. } else {
  241. *val = result;
  242. return 0;
  243. }
  244. }
  245. static int set_acpi(int cm, int value)
  246. {
  247. if (ehotk->cm_supported & (0x1 << cm)) {
  248. const char *method = cm_setv[cm];
  249. if (method == NULL)
  250. return -ENODEV;
  251. if (write_acpi_int(ehotk->handle, method, value, NULL))
  252. pr_warning("Error writing %s\n", method);
  253. }
  254. return 0;
  255. }
  256. static int get_acpi(int cm)
  257. {
  258. int value = -ENODEV;
  259. if ((ehotk->cm_supported & (0x1 << cm))) {
  260. const char *method = cm_getv[cm];
  261. if (method == NULL)
  262. return -ENODEV;
  263. if (read_acpi_int(ehotk->handle, method, &value))
  264. pr_warning("Error reading %s\n", method);
  265. }
  266. return value;
  267. }
  268. /*
  269. * Backlight
  270. */
  271. static int read_brightness(struct backlight_device *bd)
  272. {
  273. return get_acpi(CM_ASL_PANELBRIGHT);
  274. }
  275. static int set_brightness(struct backlight_device *bd, int value)
  276. {
  277. value = max(0, min(15, value));
  278. return set_acpi(CM_ASL_PANELBRIGHT, value);
  279. }
  280. static int update_bl_status(struct backlight_device *bd)
  281. {
  282. return set_brightness(bd, bd->props.brightness);
  283. }
  284. /*
  285. * Rfkill helpers
  286. */
  287. static bool eeepc_wlan_rfkill_blocked(void)
  288. {
  289. if (get_acpi(CM_ASL_WLAN) == 1)
  290. return false;
  291. return true;
  292. }
  293. static int eeepc_rfkill_set(void *data, bool blocked)
  294. {
  295. unsigned long asl = (unsigned long)data;
  296. return set_acpi(asl, !blocked);
  297. }
  298. static const struct rfkill_ops eeepc_rfkill_ops = {
  299. .set_block = eeepc_rfkill_set,
  300. };
  301. static void __init eeepc_enable_camera(void)
  302. {
  303. /*
  304. * If the following call to set_acpi() fails, it's because there's no
  305. * camera so we can ignore the error.
  306. */
  307. set_acpi(CM_ASL_CAMERA, 1);
  308. }
  309. /*
  310. * Sys helpers
  311. */
  312. static int parse_arg(const char *buf, unsigned long count, int *val)
  313. {
  314. if (!count)
  315. return 0;
  316. if (sscanf(buf, "%i", val) != 1)
  317. return -EINVAL;
  318. return count;
  319. }
  320. static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
  321. {
  322. int rv, value;
  323. rv = parse_arg(buf, count, &value);
  324. if (rv > 0)
  325. value = set_acpi(cm, value);
  326. if (value < 0)
  327. return value;
  328. return rv;
  329. }
  330. static ssize_t show_sys_acpi(int cm, char *buf)
  331. {
  332. int value = get_acpi(cm);
  333. if (value < 0)
  334. return value;
  335. return sprintf(buf, "%d\n", value);
  336. }
  337. #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
  338. static ssize_t show_##_name(struct device *dev, \
  339. struct device_attribute *attr, \
  340. char *buf) \
  341. { \
  342. return show_sys_acpi(_cm, buf); \
  343. } \
  344. static ssize_t store_##_name(struct device *dev, \
  345. struct device_attribute *attr, \
  346. const char *buf, size_t count) \
  347. { \
  348. return store_sys_acpi(_cm, buf, count); \
  349. } \
  350. static struct device_attribute dev_attr_##_name = { \
  351. .attr = { \
  352. .name = __stringify(_name), \
  353. .mode = 0644 }, \
  354. .show = show_##_name, \
  355. .store = store_##_name, \
  356. }
  357. EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
  358. EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
  359. EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
  360. struct eeepc_cpufv {
  361. int num;
  362. int cur;
  363. };
  364. static int get_cpufv(struct eeepc_cpufv *c)
  365. {
  366. c->cur = get_acpi(CM_ASL_CPUFV);
  367. c->num = (c->cur >> 8) & 0xff;
  368. c->cur &= 0xff;
  369. if (c->cur < 0 || c->num <= 0 || c->num > 12)
  370. return -ENODEV;
  371. return 0;
  372. }
  373. static ssize_t show_available_cpufv(struct device *dev,
  374. struct device_attribute *attr,
  375. char *buf)
  376. {
  377. struct eeepc_cpufv c;
  378. int i;
  379. ssize_t len = 0;
  380. if (get_cpufv(&c))
  381. return -ENODEV;
  382. for (i = 0; i < c.num; i++)
  383. len += sprintf(buf + len, "%d ", i);
  384. len += sprintf(buf + len, "\n");
  385. return len;
  386. }
  387. static ssize_t show_cpufv(struct device *dev,
  388. struct device_attribute *attr,
  389. char *buf)
  390. {
  391. struct eeepc_cpufv c;
  392. if (get_cpufv(&c))
  393. return -ENODEV;
  394. return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
  395. }
  396. static ssize_t store_cpufv(struct device *dev,
  397. struct device_attribute *attr,
  398. const char *buf, size_t count)
  399. {
  400. struct eeepc_cpufv c;
  401. int rv, value;
  402. if (get_cpufv(&c))
  403. return -ENODEV;
  404. rv = parse_arg(buf, count, &value);
  405. if (rv < 0)
  406. return rv;
  407. if (!rv || value < 0 || value >= c.num)
  408. return -EINVAL;
  409. set_acpi(CM_ASL_CPUFV, value);
  410. return rv;
  411. }
  412. static struct device_attribute dev_attr_cpufv = {
  413. .attr = {
  414. .name = "cpufv",
  415. .mode = 0644 },
  416. .show = show_cpufv,
  417. .store = store_cpufv
  418. };
  419. static struct device_attribute dev_attr_available_cpufv = {
  420. .attr = {
  421. .name = "available_cpufv",
  422. .mode = 0444 },
  423. .show = show_available_cpufv
  424. };
  425. static struct attribute *platform_attributes[] = {
  426. &dev_attr_camera.attr,
  427. &dev_attr_cardr.attr,
  428. &dev_attr_disp.attr,
  429. &dev_attr_cpufv.attr,
  430. &dev_attr_available_cpufv.attr,
  431. NULL
  432. };
  433. static struct attribute_group platform_attribute_group = {
  434. .attrs = platform_attributes
  435. };
  436. /*
  437. * Hotkey functions
  438. */
  439. static struct key_entry *eepc_get_entry_by_scancode(int code)
  440. {
  441. struct key_entry *key;
  442. for (key = eeepc_keymap; key->type != KE_END; key++)
  443. if (code == key->code)
  444. return key;
  445. return NULL;
  446. }
  447. static struct key_entry *eepc_get_entry_by_keycode(int code)
  448. {
  449. struct key_entry *key;
  450. for (key = eeepc_keymap; key->type != KE_END; key++)
  451. if (code == key->keycode && key->type == KE_KEY)
  452. return key;
  453. return NULL;
  454. }
  455. static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  456. {
  457. struct key_entry *key = eepc_get_entry_by_scancode(scancode);
  458. if (key && key->type == KE_KEY) {
  459. *keycode = key->keycode;
  460. return 0;
  461. }
  462. return -EINVAL;
  463. }
  464. static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
  465. {
  466. struct key_entry *key;
  467. int old_keycode;
  468. if (keycode < 0 || keycode > KEY_MAX)
  469. return -EINVAL;
  470. key = eepc_get_entry_by_scancode(scancode);
  471. if (key && key->type == KE_KEY) {
  472. old_keycode = key->keycode;
  473. key->keycode = keycode;
  474. set_bit(keycode, dev->keybit);
  475. if (!eepc_get_entry_by_keycode(old_keycode))
  476. clear_bit(old_keycode, dev->keybit);
  477. return 0;
  478. }
  479. return -EINVAL;
  480. }
  481. static void cmsg_quirk(int cm, const char *name)
  482. {
  483. int dummy;
  484. /* Some BIOSes do not report cm although it is avaliable.
  485. Check if cm_getv[cm] works and, if yes, assume cm should be set. */
  486. if (!(ehotk->cm_supported & (1 << cm))
  487. && !read_acpi_int(ehotk->handle, cm_getv[cm], &dummy)) {
  488. pr_info("%s (%x) not reported by BIOS,"
  489. " enabling anyway\n", name, 1 << cm);
  490. ehotk->cm_supported |= 1 << cm;
  491. }
  492. }
  493. static void cmsg_quirks(void)
  494. {
  495. cmsg_quirk(CM_ASL_LID, "LID");
  496. cmsg_quirk(CM_ASL_TYPE, "TYPE");
  497. cmsg_quirk(CM_ASL_PANELPOWER, "PANELPOWER");
  498. cmsg_quirk(CM_ASL_TPD, "TPD");
  499. }
  500. static int eeepc_hotk_check(void)
  501. {
  502. const struct key_entry *key;
  503. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  504. int result;
  505. result = acpi_bus_get_status(ehotk->device);
  506. if (result)
  507. return result;
  508. if (ehotk->device->status.present) {
  509. if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
  510. &buffer)) {
  511. pr_err("Hotkey initialization failed\n");
  512. return -ENODEV;
  513. } else {
  514. pr_notice("Hotkey init flags 0x%x\n", ehotk->init_flag);
  515. }
  516. /* get control methods supported */
  517. if (read_acpi_int(ehotk->handle, "CMSG"
  518. , &ehotk->cm_supported)) {
  519. pr_err("Get control methods supported failed\n");
  520. return -ENODEV;
  521. } else {
  522. cmsg_quirks();
  523. pr_info("Get control methods supported: 0x%x\n",
  524. ehotk->cm_supported);
  525. }
  526. ehotk->inputdev = input_allocate_device();
  527. if (!ehotk->inputdev) {
  528. pr_info("Unable to allocate input device\n");
  529. return 0;
  530. }
  531. ehotk->inputdev->name = "Asus EeePC extra buttons";
  532. ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
  533. ehotk->inputdev->id.bustype = BUS_HOST;
  534. ehotk->inputdev->getkeycode = eeepc_getkeycode;
  535. ehotk->inputdev->setkeycode = eeepc_setkeycode;
  536. for (key = eeepc_keymap; key->type != KE_END; key++) {
  537. switch (key->type) {
  538. case KE_KEY:
  539. set_bit(EV_KEY, ehotk->inputdev->evbit);
  540. set_bit(key->keycode, ehotk->inputdev->keybit);
  541. break;
  542. }
  543. }
  544. result = input_register_device(ehotk->inputdev);
  545. if (result) {
  546. pr_info("Unable to register input device\n");
  547. input_free_device(ehotk->inputdev);
  548. return 0;
  549. }
  550. } else {
  551. pr_err("Hotkey device not present, aborting\n");
  552. return -EINVAL;
  553. }
  554. return 0;
  555. }
  556. static int notify_brn(void)
  557. {
  558. /* returns the *previous* brightness, or -1 */
  559. struct backlight_device *bd = eeepc_backlight_device;
  560. if (bd) {
  561. int old = bd->props.brightness;
  562. bd->props.brightness = read_brightness(bd);
  563. return old;
  564. }
  565. return -1;
  566. }
  567. static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
  568. u8 *value)
  569. {
  570. int val = get_acpi(CM_ASL_WLAN);
  571. if (val == 1 || val == 0)
  572. *value = val;
  573. else
  574. return -EINVAL;
  575. return 0;
  576. }
  577. static void eeepc_rfkill_hotplug(void)
  578. {
  579. struct pci_dev *dev;
  580. struct pci_bus *bus = pci_find_bus(0, 1);
  581. bool blocked;
  582. if (!bus) {
  583. pr_warning("Unable to find PCI bus 1?\n");
  584. return;
  585. }
  586. blocked = eeepc_wlan_rfkill_blocked();
  587. if (!blocked) {
  588. dev = pci_get_slot(bus, 0);
  589. if (dev) {
  590. /* Device already present */
  591. pci_dev_put(dev);
  592. return;
  593. }
  594. dev = pci_scan_single_device(bus, 0);
  595. if (dev) {
  596. pci_bus_assign_resources(bus);
  597. if (pci_bus_add_device(dev))
  598. pr_err("Unable to hotplug wifi\n");
  599. }
  600. } else {
  601. dev = pci_get_slot(bus, 0);
  602. if (dev) {
  603. pci_remove_bus_device(dev);
  604. pci_dev_put(dev);
  605. }
  606. }
  607. rfkill_set_sw_state(ehotk->wlan_rfkill, blocked);
  608. }
  609. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  610. {
  611. if (event != ACPI_NOTIFY_BUS_CHECK)
  612. return;
  613. eeepc_rfkill_hotplug();
  614. }
  615. static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
  616. {
  617. static struct key_entry *key;
  618. u16 count;
  619. int brn = -ENODEV;
  620. if (!ehotk)
  621. return;
  622. if (event > ACPI_MAX_SYS_NOTIFY)
  623. return;
  624. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
  625. brn = notify_brn();
  626. count = ehotk->event_count[event % 128]++;
  627. acpi_bus_generate_proc_event(ehotk->device, event, count);
  628. acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
  629. dev_name(&ehotk->device->dev), event,
  630. count);
  631. if (ehotk->inputdev) {
  632. if (brn != -ENODEV) {
  633. /* brightness-change events need special
  634. * handling for conversion to key events
  635. */
  636. if (brn < 0)
  637. brn = event;
  638. else
  639. brn += NOTIFY_BRN_MIN;
  640. if (event < brn)
  641. event = NOTIFY_BRN_MIN; /* brightness down */
  642. else if (event > brn)
  643. event = NOTIFY_BRN_MIN + 2; /* ... up */
  644. else
  645. event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
  646. }
  647. key = eepc_get_entry_by_scancode(event);
  648. if (key) {
  649. switch (key->type) {
  650. case KE_KEY:
  651. input_report_key(ehotk->inputdev, key->keycode,
  652. 1);
  653. input_sync(ehotk->inputdev);
  654. input_report_key(ehotk->inputdev, key->keycode,
  655. 0);
  656. input_sync(ehotk->inputdev);
  657. break;
  658. }
  659. }
  660. }
  661. }
  662. static int eeepc_register_rfkill_notifier(char *node)
  663. {
  664. acpi_status status = AE_OK;
  665. acpi_handle handle;
  666. status = acpi_get_handle(NULL, node, &handle);
  667. if (ACPI_SUCCESS(status)) {
  668. status = acpi_install_notify_handler(handle,
  669. ACPI_SYSTEM_NOTIFY,
  670. eeepc_rfkill_notify,
  671. NULL);
  672. if (ACPI_FAILURE(status))
  673. pr_warning("Failed to register notify on %s\n", node);
  674. } else
  675. return -ENODEV;
  676. return 0;
  677. }
  678. static void eeepc_unregister_rfkill_notifier(char *node)
  679. {
  680. acpi_status status = AE_OK;
  681. acpi_handle handle;
  682. status = acpi_get_handle(NULL, node, &handle);
  683. if (ACPI_SUCCESS(status)) {
  684. status = acpi_remove_notify_handler(handle,
  685. ACPI_SYSTEM_NOTIFY,
  686. eeepc_rfkill_notify);
  687. if (ACPI_FAILURE(status))
  688. pr_err("Error removing rfkill notify handler %s\n",
  689. node);
  690. }
  691. }
  692. static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  693. {
  694. kfree(hotplug_slot->info);
  695. kfree(hotplug_slot);
  696. }
  697. static int eeepc_setup_pci_hotplug(void)
  698. {
  699. int ret = -ENOMEM;
  700. struct pci_bus *bus = pci_find_bus(0, 1);
  701. if (!bus) {
  702. pr_err("Unable to find wifi PCI bus\n");
  703. return -ENODEV;
  704. }
  705. ehotk->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  706. if (!ehotk->hotplug_slot)
  707. goto error_slot;
  708. ehotk->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  709. GFP_KERNEL);
  710. if (!ehotk->hotplug_slot->info)
  711. goto error_info;
  712. ehotk->hotplug_slot->private = ehotk;
  713. ehotk->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
  714. ehotk->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
  715. eeepc_get_adapter_status(ehotk->hotplug_slot,
  716. &ehotk->hotplug_slot->info->adapter_status);
  717. ret = pci_hp_register(ehotk->hotplug_slot, bus, 0, "eeepc-wifi");
  718. if (ret) {
  719. pr_err("Unable to register hotplug slot - %d\n", ret);
  720. goto error_register;
  721. }
  722. return 0;
  723. error_register:
  724. kfree(ehotk->hotplug_slot->info);
  725. error_info:
  726. kfree(ehotk->hotplug_slot);
  727. ehotk->hotplug_slot = NULL;
  728. error_slot:
  729. return ret;
  730. }
  731. static int eeepc_hotk_add(struct acpi_device *device)
  732. {
  733. int result;
  734. if (!device)
  735. return -EINVAL;
  736. pr_notice(EEEPC_HOTK_NAME "\n");
  737. ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
  738. if (!ehotk)
  739. return -ENOMEM;
  740. ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  741. ehotk->handle = device->handle;
  742. strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
  743. strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
  744. device->driver_data = ehotk;
  745. ehotk->device = device;
  746. result = eeepc_hotk_check();
  747. if (result)
  748. goto ehotk_fail;
  749. return 0;
  750. ehotk_fail:
  751. kfree(ehotk);
  752. ehotk = NULL;
  753. return result;
  754. }
  755. static int eeepc_hotk_remove(struct acpi_device *device, int type)
  756. {
  757. if (!device || !acpi_driver_data(device))
  758. return -EINVAL;
  759. kfree(ehotk);
  760. return 0;
  761. }
  762. static int eeepc_hotk_resume(struct acpi_device *device)
  763. {
  764. if (ehotk->wlan_rfkill) {
  765. bool wlan;
  766. /* Workaround - it seems that _PTS disables the wireless
  767. without notification or changing the value read by WLAN.
  768. Normally this is fine because the correct value is restored
  769. from the non-volatile storage on resume, but we need to do
  770. it ourself if case suspend is aborted, or we lose wireless.
  771. */
  772. wlan = get_acpi(CM_ASL_WLAN);
  773. set_acpi(CM_ASL_WLAN, wlan);
  774. rfkill_set_sw_state(ehotk->wlan_rfkill, wlan != 1);
  775. eeepc_rfkill_hotplug();
  776. }
  777. if (ehotk->bluetooth_rfkill)
  778. rfkill_set_sw_state(ehotk->bluetooth_rfkill,
  779. get_acpi(CM_ASL_BLUETOOTH) != 1);
  780. return 0;
  781. }
  782. /*
  783. * Hwmon
  784. */
  785. static int eeepc_get_fan_pwm(void)
  786. {
  787. int value = 0;
  788. read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
  789. value = value * 255 / 100;
  790. return (value);
  791. }
  792. static void eeepc_set_fan_pwm(int value)
  793. {
  794. value = SENSORS_LIMIT(value, 0, 255);
  795. value = value * 100 / 255;
  796. ec_write(EEEPC_EC_SC02, value);
  797. }
  798. static int eeepc_get_fan_rpm(void)
  799. {
  800. int high = 0;
  801. int low = 0;
  802. read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
  803. read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
  804. return (high << 8 | low);
  805. }
  806. static int eeepc_get_fan_ctrl(void)
  807. {
  808. int value = 0;
  809. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  810. return ((value & 0x02 ? 1 : 0));
  811. }
  812. static void eeepc_set_fan_ctrl(int manual)
  813. {
  814. int value = 0;
  815. read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
  816. if (manual)
  817. value |= 0x02;
  818. else
  819. value &= ~0x02;
  820. ec_write(EEEPC_EC_SFB3, value);
  821. }
  822. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  823. {
  824. int rv, value;
  825. rv = parse_arg(buf, count, &value);
  826. if (rv > 0)
  827. set(value);
  828. return rv;
  829. }
  830. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  831. {
  832. return sprintf(buf, "%d\n", get());
  833. }
  834. #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
  835. static ssize_t show_##_name(struct device *dev, \
  836. struct device_attribute *attr, \
  837. char *buf) \
  838. { \
  839. return show_sys_hwmon(_set, buf); \
  840. } \
  841. static ssize_t store_##_name(struct device *dev, \
  842. struct device_attribute *attr, \
  843. const char *buf, size_t count) \
  844. { \
  845. return store_sys_hwmon(_get, buf, count); \
  846. } \
  847. static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
  848. EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
  849. EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
  850. eeepc_get_fan_pwm, eeepc_set_fan_pwm);
  851. EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  852. eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
  853. static ssize_t
  854. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  855. {
  856. return sprintf(buf, "eeepc\n");
  857. }
  858. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  859. static struct attribute *hwmon_attributes[] = {
  860. &sensor_dev_attr_pwm1.dev_attr.attr,
  861. &sensor_dev_attr_fan1_input.dev_attr.attr,
  862. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  863. &sensor_dev_attr_name.dev_attr.attr,
  864. NULL
  865. };
  866. static struct attribute_group hwmon_attribute_group = {
  867. .attrs = hwmon_attributes
  868. };
  869. /*
  870. * exit/init
  871. */
  872. static void eeepc_backlight_exit(void)
  873. {
  874. if (eeepc_backlight_device)
  875. backlight_device_unregister(eeepc_backlight_device);
  876. eeepc_backlight_device = NULL;
  877. }
  878. static void eeepc_rfkill_exit(void)
  879. {
  880. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
  881. eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  882. if (ehotk->wlan_rfkill)
  883. rfkill_unregister(ehotk->wlan_rfkill);
  884. if (ehotk->bluetooth_rfkill)
  885. rfkill_unregister(ehotk->bluetooth_rfkill);
  886. if (ehotk->wwan3g_rfkill)
  887. rfkill_unregister(ehotk->wwan3g_rfkill);
  888. if (ehotk->hotplug_slot)
  889. pci_hp_deregister(ehotk->hotplug_slot);
  890. }
  891. static void eeepc_input_exit(void)
  892. {
  893. if (ehotk->inputdev)
  894. input_unregister_device(ehotk->inputdev);
  895. }
  896. static void eeepc_hwmon_exit(void)
  897. {
  898. struct device *hwmon;
  899. hwmon = eeepc_hwmon_device;
  900. if (!hwmon)
  901. return ;
  902. sysfs_remove_group(&hwmon->kobj,
  903. &hwmon_attribute_group);
  904. hwmon_device_unregister(hwmon);
  905. eeepc_hwmon_device = NULL;
  906. }
  907. static void __exit eeepc_laptop_exit(void)
  908. {
  909. eeepc_backlight_exit();
  910. eeepc_rfkill_exit();
  911. eeepc_input_exit();
  912. eeepc_hwmon_exit();
  913. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  914. sysfs_remove_group(&platform_device->dev.kobj,
  915. &platform_attribute_group);
  916. platform_device_unregister(platform_device);
  917. platform_driver_unregister(&platform_driver);
  918. }
  919. static int eeepc_new_rfkill(struct rfkill **rfkill,
  920. const char *name, struct device *dev,
  921. enum rfkill_type type, int cm)
  922. {
  923. int result;
  924. result = get_acpi(cm);
  925. if (result < 0)
  926. return result;
  927. *rfkill = rfkill_alloc(name, dev, type,
  928. &eeepc_rfkill_ops, (void *)(unsigned long)cm);
  929. if (!*rfkill)
  930. return -EINVAL;
  931. rfkill_init_sw_state(*rfkill, get_acpi(cm) != 1);
  932. result = rfkill_register(*rfkill);
  933. if (result) {
  934. rfkill_destroy(*rfkill);
  935. *rfkill = NULL;
  936. return result;
  937. }
  938. return 0;
  939. }
  940. static int eeepc_rfkill_init(struct device *dev)
  941. {
  942. int result = 0;
  943. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
  944. eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
  945. result = eeepc_new_rfkill(&ehotk->wlan_rfkill,
  946. "eeepc-wlan", dev,
  947. RFKILL_TYPE_WLAN, CM_ASL_WLAN);
  948. if (result && result != -ENODEV)
  949. goto exit;
  950. result = eeepc_new_rfkill(&ehotk->bluetooth_rfkill,
  951. "eeepc-bluetooth", dev,
  952. RFKILL_TYPE_BLUETOOTH, CM_ASL_BLUETOOTH);
  953. if (result && result != -ENODEV)
  954. goto exit;
  955. result = eeepc_new_rfkill(&ehotk->wwan3g_rfkill,
  956. "eeepc-wwan3g", dev,
  957. RFKILL_TYPE_WWAN, CM_ASL_3G);
  958. if (result && result != -ENODEV)
  959. goto exit;
  960. result = eeepc_setup_pci_hotplug();
  961. /*
  962. * If we get -EBUSY then something else is handling the PCI hotplug -
  963. * don't fail in this case
  964. */
  965. if (result == -EBUSY)
  966. result = 0;
  967. exit:
  968. if (result && result != -ENODEV)
  969. eeepc_rfkill_exit();
  970. return result;
  971. }
  972. static int eeepc_backlight_init(struct device *dev)
  973. {
  974. struct backlight_device *bd;
  975. bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
  976. NULL, &eeepcbl_ops);
  977. if (IS_ERR(bd)) {
  978. pr_err("Could not register eeepc backlight device\n");
  979. eeepc_backlight_device = NULL;
  980. return PTR_ERR(bd);
  981. }
  982. eeepc_backlight_device = bd;
  983. bd->props.max_brightness = 15;
  984. bd->props.brightness = read_brightness(NULL);
  985. bd->props.power = FB_BLANK_UNBLANK;
  986. backlight_update_status(bd);
  987. return 0;
  988. }
  989. static int eeepc_hwmon_init(struct device *dev)
  990. {
  991. struct device *hwmon;
  992. int result;
  993. hwmon = hwmon_device_register(dev);
  994. if (IS_ERR(hwmon)) {
  995. pr_err("Could not register eeepc hwmon device\n");
  996. eeepc_hwmon_device = NULL;
  997. return PTR_ERR(hwmon);
  998. }
  999. eeepc_hwmon_device = hwmon;
  1000. result = sysfs_create_group(&hwmon->kobj,
  1001. &hwmon_attribute_group);
  1002. if (result)
  1003. eeepc_hwmon_exit();
  1004. return result;
  1005. }
  1006. static int __init eeepc_laptop_init(void)
  1007. {
  1008. struct device *dev;
  1009. int result;
  1010. if (acpi_disabled)
  1011. return -ENODEV;
  1012. result = acpi_bus_register_driver(&eeepc_hotk_driver);
  1013. if (result < 0)
  1014. return result;
  1015. if (!ehotk) {
  1016. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  1017. return -ENODEV;
  1018. }
  1019. eeepc_enable_camera();
  1020. /* Register platform stuff */
  1021. result = platform_driver_register(&platform_driver);
  1022. if (result)
  1023. goto fail_platform_driver;
  1024. platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
  1025. if (!platform_device) {
  1026. result = -ENOMEM;
  1027. goto fail_platform_device1;
  1028. }
  1029. result = platform_device_add(platform_device);
  1030. if (result)
  1031. goto fail_platform_device2;
  1032. result = sysfs_create_group(&platform_device->dev.kobj,
  1033. &platform_attribute_group);
  1034. if (result)
  1035. goto fail_sysfs;
  1036. dev = &platform_device->dev;
  1037. if (!acpi_video_backlight_support()) {
  1038. result = eeepc_backlight_init(dev);
  1039. if (result)
  1040. goto fail_backlight;
  1041. } else
  1042. pr_info("Backlight controlled by ACPI video "
  1043. "driver\n");
  1044. result = eeepc_hwmon_init(dev);
  1045. if (result)
  1046. goto fail_hwmon;
  1047. result = eeepc_rfkill_init(dev);
  1048. if (result)
  1049. goto fail_rfkill;
  1050. return 0;
  1051. fail_rfkill:
  1052. eeepc_hwmon_exit();
  1053. fail_hwmon:
  1054. eeepc_backlight_exit();
  1055. fail_backlight:
  1056. sysfs_remove_group(&platform_device->dev.kobj,
  1057. &platform_attribute_group);
  1058. fail_sysfs:
  1059. platform_device_del(platform_device);
  1060. fail_platform_device2:
  1061. platform_device_put(platform_device);
  1062. fail_platform_device1:
  1063. platform_driver_unregister(&platform_driver);
  1064. fail_platform_driver:
  1065. eeepc_input_exit();
  1066. return result;
  1067. }
  1068. module_init(eeepc_laptop_init);
  1069. module_exit(eeepc_laptop_exit);