eeepc-laptop.c 27 KB

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