eeepc-laptop.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. /*
  2. * eeepc-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 <linux/slab.h>
  29. #include <acpi/acpi_drivers.h>
  30. #include <acpi/acpi_bus.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/input.h>
  33. #include <linux/input/sparse-keymap.h>
  34. #include <linux/rfkill.h>
  35. #include <linux/pci.h>
  36. #include <linux/pci_hotplug.h>
  37. #include <linux/leds.h>
  38. #include <linux/dmi.h>
  39. #define EEEPC_LAPTOP_VERSION "0.1"
  40. #define EEEPC_LAPTOP_NAME "Eee PC Hotkey Driver"
  41. #define EEEPC_LAPTOP_FILE "eeepc"
  42. #define EEEPC_ACPI_CLASS "hotkey"
  43. #define EEEPC_ACPI_DEVICE_NAME "Hotkey"
  44. #define EEEPC_ACPI_HID "ASUS010"
  45. MODULE_AUTHOR("Corentin Chary, Eric Cooper");
  46. MODULE_DESCRIPTION(EEEPC_LAPTOP_NAME);
  47. MODULE_LICENSE("GPL");
  48. static bool hotplug_disabled;
  49. module_param(hotplug_disabled, bool, 0444);
  50. MODULE_PARM_DESC(hotplug_disabled,
  51. "Disable hotplug for wireless device. "
  52. "If your laptop need that, please report to "
  53. "acpi4asus-user@lists.sourceforge.net.");
  54. /*
  55. * Definitions for Asus EeePC
  56. */
  57. #define NOTIFY_BRN_MIN 0x20
  58. #define NOTIFY_BRN_MAX 0x2f
  59. enum {
  60. DISABLE_ASL_WLAN = 0x0001,
  61. DISABLE_ASL_BLUETOOTH = 0x0002,
  62. DISABLE_ASL_IRDA = 0x0004,
  63. DISABLE_ASL_CAMERA = 0x0008,
  64. DISABLE_ASL_TV = 0x0010,
  65. DISABLE_ASL_GPS = 0x0020,
  66. DISABLE_ASL_DISPLAYSWITCH = 0x0040,
  67. DISABLE_ASL_MODEM = 0x0080,
  68. DISABLE_ASL_CARDREADER = 0x0100,
  69. DISABLE_ASL_3G = 0x0200,
  70. DISABLE_ASL_WIMAX = 0x0400,
  71. DISABLE_ASL_HWCF = 0x0800
  72. };
  73. enum {
  74. CM_ASL_WLAN = 0,
  75. CM_ASL_BLUETOOTH,
  76. CM_ASL_IRDA,
  77. CM_ASL_1394,
  78. CM_ASL_CAMERA,
  79. CM_ASL_TV,
  80. CM_ASL_GPS,
  81. CM_ASL_DVDROM,
  82. CM_ASL_DISPLAYSWITCH,
  83. CM_ASL_PANELBRIGHT,
  84. CM_ASL_BIOSFLASH,
  85. CM_ASL_ACPIFLASH,
  86. CM_ASL_CPUFV,
  87. CM_ASL_CPUTEMPERATURE,
  88. CM_ASL_FANCPU,
  89. CM_ASL_FANCHASSIS,
  90. CM_ASL_USBPORT1,
  91. CM_ASL_USBPORT2,
  92. CM_ASL_USBPORT3,
  93. CM_ASL_MODEM,
  94. CM_ASL_CARDREADER,
  95. CM_ASL_3G,
  96. CM_ASL_WIMAX,
  97. CM_ASL_HWCF,
  98. CM_ASL_LID,
  99. CM_ASL_TYPE,
  100. CM_ASL_PANELPOWER, /*P901*/
  101. CM_ASL_TPD
  102. };
  103. static const char *cm_getv[] = {
  104. "WLDG", "BTHG", NULL, NULL,
  105. "CAMG", NULL, NULL, NULL,
  106. NULL, "PBLG", NULL, NULL,
  107. "CFVG", NULL, NULL, NULL,
  108. "USBG", NULL, NULL, "MODG",
  109. "CRDG", "M3GG", "WIMG", "HWCF",
  110. "LIDG", "TYPE", "PBPG", "TPDG"
  111. };
  112. static const char *cm_setv[] = {
  113. "WLDS", "BTHS", NULL, NULL,
  114. "CAMS", NULL, NULL, NULL,
  115. "SDSP", "PBLS", "HDPS", NULL,
  116. "CFVS", NULL, NULL, NULL,
  117. "USBG", NULL, NULL, "MODS",
  118. "CRDS", "M3GS", "WIMS", NULL,
  119. NULL, NULL, "PBPS", "TPDS"
  120. };
  121. static const struct key_entry eeepc_keymap[] = {
  122. { KE_KEY, 0x10, { KEY_WLAN } },
  123. { KE_KEY, 0x11, { KEY_WLAN } },
  124. { KE_KEY, 0x12, { KEY_PROG1 } },
  125. { KE_KEY, 0x13, { KEY_MUTE } },
  126. { KE_KEY, 0x14, { KEY_VOLUMEDOWN } },
  127. { KE_KEY, 0x15, { KEY_VOLUMEUP } },
  128. { KE_KEY, 0x16, { KEY_DISPLAY_OFF } },
  129. { KE_KEY, 0x1a, { KEY_COFFEE } },
  130. { KE_KEY, 0x1b, { KEY_ZOOM } },
  131. { KE_KEY, 0x1c, { KEY_PROG2 } },
  132. { KE_KEY, 0x1d, { KEY_PROG3 } },
  133. { KE_KEY, NOTIFY_BRN_MIN, { KEY_BRIGHTNESSDOWN } },
  134. { KE_KEY, NOTIFY_BRN_MAX, { KEY_BRIGHTNESSUP } },
  135. { KE_KEY, 0x30, { KEY_SWITCHVIDEOMODE } },
  136. { KE_KEY, 0x31, { KEY_SWITCHVIDEOMODE } },
  137. { KE_KEY, 0x32, { KEY_SWITCHVIDEOMODE } },
  138. { KE_KEY, 0x37, { KEY_F13 } }, /* Disable Touchpad */
  139. { KE_KEY, 0x38, { KEY_F14 } },
  140. { KE_END, 0 },
  141. };
  142. /*
  143. * This is the main structure, we can use it to store useful information
  144. */
  145. struct eeepc_laptop {
  146. acpi_handle handle; /* the handle of the acpi device */
  147. u32 cm_supported; /* the control methods supported
  148. by this BIOS */
  149. bool cpufv_disabled;
  150. bool hotplug_disabled;
  151. u16 event_count[128]; /* count for each event */
  152. struct platform_device *platform_device;
  153. struct acpi_device *device; /* the device we are in */
  154. struct device *hwmon_device;
  155. struct backlight_device *backlight_device;
  156. struct input_dev *inputdev;
  157. struct rfkill *wlan_rfkill;
  158. struct rfkill *bluetooth_rfkill;
  159. struct rfkill *wwan3g_rfkill;
  160. struct rfkill *wimax_rfkill;
  161. struct hotplug_slot *hotplug_slot;
  162. struct mutex hotplug_lock;
  163. struct led_classdev tpd_led;
  164. int tpd_led_wk;
  165. struct workqueue_struct *led_workqueue;
  166. struct work_struct tpd_led_work;
  167. };
  168. /*
  169. * ACPI Helpers
  170. */
  171. static int write_acpi_int(acpi_handle handle, const char *method, int val)
  172. {
  173. struct acpi_object_list params;
  174. union acpi_object in_obj;
  175. acpi_status status;
  176. params.count = 1;
  177. params.pointer = &in_obj;
  178. in_obj.type = ACPI_TYPE_INTEGER;
  179. in_obj.integer.value = val;
  180. status = acpi_evaluate_object(handle, (char *)method, &params, NULL);
  181. return (status == AE_OK ? 0 : -1);
  182. }
  183. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  184. {
  185. acpi_status status;
  186. unsigned long long result;
  187. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  188. if (ACPI_FAILURE(status)) {
  189. *val = -1;
  190. return -1;
  191. } else {
  192. *val = result;
  193. return 0;
  194. }
  195. }
  196. static int set_acpi(struct eeepc_laptop *eeepc, int cm, int value)
  197. {
  198. const char *method = cm_setv[cm];
  199. if (method == NULL)
  200. return -ENODEV;
  201. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  202. return -ENODEV;
  203. if (write_acpi_int(eeepc->handle, method, value))
  204. pr_warning("Error writing %s\n", method);
  205. return 0;
  206. }
  207. static int get_acpi(struct eeepc_laptop *eeepc, int cm)
  208. {
  209. const char *method = cm_getv[cm];
  210. int value;
  211. if (method == NULL)
  212. return -ENODEV;
  213. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  214. return -ENODEV;
  215. if (read_acpi_int(eeepc->handle, method, &value))
  216. pr_warning("Error reading %s\n", method);
  217. return value;
  218. }
  219. static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
  220. acpi_handle *handle)
  221. {
  222. const char *method = cm_setv[cm];
  223. acpi_status status;
  224. if (method == NULL)
  225. return -ENODEV;
  226. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  227. return -ENODEV;
  228. status = acpi_get_handle(eeepc->handle, (char *)method,
  229. handle);
  230. if (status != AE_OK) {
  231. pr_warning("Error finding %s\n", method);
  232. return -ENODEV;
  233. }
  234. return 0;
  235. }
  236. /*
  237. * Sys helpers
  238. */
  239. static int parse_arg(const char *buf, unsigned long count, int *val)
  240. {
  241. if (!count)
  242. return 0;
  243. if (sscanf(buf, "%i", val) != 1)
  244. return -EINVAL;
  245. return count;
  246. }
  247. static ssize_t store_sys_acpi(struct device *dev, int cm,
  248. const char *buf, size_t count)
  249. {
  250. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  251. int rv, value;
  252. rv = parse_arg(buf, count, &value);
  253. if (rv > 0)
  254. value = set_acpi(eeepc, cm, value);
  255. if (value < 0)
  256. return -EIO;
  257. return rv;
  258. }
  259. static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
  260. {
  261. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  262. int value = get_acpi(eeepc, cm);
  263. if (value < 0)
  264. return -EIO;
  265. return sprintf(buf, "%d\n", value);
  266. }
  267. #define EEEPC_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
  268. static ssize_t show_##_name(struct device *dev, \
  269. struct device_attribute *attr, \
  270. char *buf) \
  271. { \
  272. return show_sys_acpi(dev, _cm, buf); \
  273. } \
  274. static ssize_t store_##_name(struct device *dev, \
  275. struct device_attribute *attr, \
  276. const char *buf, size_t count) \
  277. { \
  278. return store_sys_acpi(dev, _cm, buf, count); \
  279. } \
  280. static struct device_attribute dev_attr_##_name = { \
  281. .attr = { \
  282. .name = __stringify(_name), \
  283. .mode = _mode }, \
  284. .show = show_##_name, \
  285. .store = store_##_name, \
  286. }
  287. EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA);
  288. EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);
  289. EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
  290. struct eeepc_cpufv {
  291. int num;
  292. int cur;
  293. };
  294. static int get_cpufv(struct eeepc_laptop *eeepc, struct eeepc_cpufv *c)
  295. {
  296. c->cur = get_acpi(eeepc, CM_ASL_CPUFV);
  297. c->num = (c->cur >> 8) & 0xff;
  298. c->cur &= 0xff;
  299. if (c->cur < 0 || c->num <= 0 || c->num > 12)
  300. return -ENODEV;
  301. return 0;
  302. }
  303. static ssize_t show_available_cpufv(struct device *dev,
  304. struct device_attribute *attr,
  305. char *buf)
  306. {
  307. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  308. struct eeepc_cpufv c;
  309. int i;
  310. ssize_t len = 0;
  311. if (get_cpufv(eeepc, &c))
  312. return -ENODEV;
  313. for (i = 0; i < c.num; i++)
  314. len += sprintf(buf + len, "%d ", i);
  315. len += sprintf(buf + len, "\n");
  316. return len;
  317. }
  318. static ssize_t show_cpufv(struct device *dev,
  319. struct device_attribute *attr,
  320. char *buf)
  321. {
  322. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  323. struct eeepc_cpufv c;
  324. if (get_cpufv(eeepc, &c))
  325. return -ENODEV;
  326. return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
  327. }
  328. static ssize_t store_cpufv(struct device *dev,
  329. struct device_attribute *attr,
  330. const char *buf, size_t count)
  331. {
  332. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  333. struct eeepc_cpufv c;
  334. int rv, value;
  335. if (eeepc->cpufv_disabled)
  336. return -EPERM;
  337. if (get_cpufv(eeepc, &c))
  338. return -ENODEV;
  339. rv = parse_arg(buf, count, &value);
  340. if (rv < 0)
  341. return rv;
  342. if (!rv || value < 0 || value >= c.num)
  343. return -EINVAL;
  344. set_acpi(eeepc, CM_ASL_CPUFV, value);
  345. return rv;
  346. }
  347. static ssize_t show_cpufv_disabled(struct device *dev,
  348. struct device_attribute *attr,
  349. char *buf)
  350. {
  351. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  352. return sprintf(buf, "%d\n", eeepc->cpufv_disabled);
  353. }
  354. static ssize_t store_cpufv_disabled(struct device *dev,
  355. struct device_attribute *attr,
  356. const char *buf, size_t count)
  357. {
  358. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  359. int rv, value;
  360. rv = parse_arg(buf, count, &value);
  361. if (rv < 0)
  362. return rv;
  363. switch (value) {
  364. case 0:
  365. if (eeepc->cpufv_disabled)
  366. pr_warning("cpufv enabled (not officially supported "
  367. "on this model)\n");
  368. eeepc->cpufv_disabled = false;
  369. return rv;
  370. case 1:
  371. return -EPERM;
  372. default:
  373. return -EINVAL;
  374. }
  375. }
  376. static struct device_attribute dev_attr_cpufv = {
  377. .attr = {
  378. .name = "cpufv",
  379. .mode = 0644 },
  380. .show = show_cpufv,
  381. .store = store_cpufv
  382. };
  383. static struct device_attribute dev_attr_available_cpufv = {
  384. .attr = {
  385. .name = "available_cpufv",
  386. .mode = 0444 },
  387. .show = show_available_cpufv
  388. };
  389. static struct device_attribute dev_attr_cpufv_disabled = {
  390. .attr = {
  391. .name = "cpufv_disabled",
  392. .mode = 0644 },
  393. .show = show_cpufv_disabled,
  394. .store = store_cpufv_disabled
  395. };
  396. static struct attribute *platform_attributes[] = {
  397. &dev_attr_camera.attr,
  398. &dev_attr_cardr.attr,
  399. &dev_attr_disp.attr,
  400. &dev_attr_cpufv.attr,
  401. &dev_attr_available_cpufv.attr,
  402. &dev_attr_cpufv_disabled.attr,
  403. NULL
  404. };
  405. static struct attribute_group platform_attribute_group = {
  406. .attrs = platform_attributes
  407. };
  408. static int eeepc_platform_init(struct eeepc_laptop *eeepc)
  409. {
  410. int result;
  411. eeepc->platform_device = platform_device_alloc(EEEPC_LAPTOP_FILE, -1);
  412. if (!eeepc->platform_device)
  413. return -ENOMEM;
  414. platform_set_drvdata(eeepc->platform_device, eeepc);
  415. result = platform_device_add(eeepc->platform_device);
  416. if (result)
  417. goto fail_platform_device;
  418. result = sysfs_create_group(&eeepc->platform_device->dev.kobj,
  419. &platform_attribute_group);
  420. if (result)
  421. goto fail_sysfs;
  422. return 0;
  423. fail_sysfs:
  424. platform_device_del(eeepc->platform_device);
  425. fail_platform_device:
  426. platform_device_put(eeepc->platform_device);
  427. return result;
  428. }
  429. static void eeepc_platform_exit(struct eeepc_laptop *eeepc)
  430. {
  431. sysfs_remove_group(&eeepc->platform_device->dev.kobj,
  432. &platform_attribute_group);
  433. platform_device_unregister(eeepc->platform_device);
  434. }
  435. /*
  436. * LEDs
  437. */
  438. /*
  439. * These functions actually update the LED's, and are called from a
  440. * workqueue. By doing this as separate work rather than when the LED
  441. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  442. * potentially bad time, such as a timer interrupt.
  443. */
  444. static void tpd_led_update(struct work_struct *work)
  445. {
  446. struct eeepc_laptop *eeepc;
  447. eeepc = container_of(work, struct eeepc_laptop, tpd_led_work);
  448. set_acpi(eeepc, CM_ASL_TPD, eeepc->tpd_led_wk);
  449. }
  450. static void tpd_led_set(struct led_classdev *led_cdev,
  451. enum led_brightness value)
  452. {
  453. struct eeepc_laptop *eeepc;
  454. eeepc = container_of(led_cdev, struct eeepc_laptop, tpd_led);
  455. eeepc->tpd_led_wk = (value > 0) ? 1 : 0;
  456. queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
  457. }
  458. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  459. {
  460. struct eeepc_laptop *eeepc;
  461. eeepc = container_of(led_cdev, struct eeepc_laptop, tpd_led);
  462. return get_acpi(eeepc, CM_ASL_TPD);
  463. }
  464. static int eeepc_led_init(struct eeepc_laptop *eeepc)
  465. {
  466. int rv;
  467. if (get_acpi(eeepc, CM_ASL_TPD) == -ENODEV)
  468. return 0;
  469. eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
  470. if (!eeepc->led_workqueue)
  471. return -ENOMEM;
  472. INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
  473. eeepc->tpd_led.name = "eeepc::touchpad";
  474. eeepc->tpd_led.brightness_set = tpd_led_set;
  475. if (get_acpi(eeepc, CM_ASL_TPD) >= 0) /* if method is available */
  476. eeepc->tpd_led.brightness_get = tpd_led_get;
  477. eeepc->tpd_led.max_brightness = 1;
  478. rv = led_classdev_register(&eeepc->platform_device->dev,
  479. &eeepc->tpd_led);
  480. if (rv) {
  481. destroy_workqueue(eeepc->led_workqueue);
  482. return rv;
  483. }
  484. return 0;
  485. }
  486. static void eeepc_led_exit(struct eeepc_laptop *eeepc)
  487. {
  488. if (eeepc->tpd_led.dev)
  489. led_classdev_unregister(&eeepc->tpd_led);
  490. if (eeepc->led_workqueue)
  491. destroy_workqueue(eeepc->led_workqueue);
  492. }
  493. /*
  494. * PCI hotplug (for wlan rfkill)
  495. */
  496. static bool eeepc_wlan_rfkill_blocked(struct eeepc_laptop *eeepc)
  497. {
  498. if (get_acpi(eeepc, CM_ASL_WLAN) == 1)
  499. return false;
  500. return true;
  501. }
  502. static void eeepc_rfkill_hotplug(struct eeepc_laptop *eeepc)
  503. {
  504. struct pci_dev *dev;
  505. struct pci_bus *bus;
  506. bool blocked = eeepc_wlan_rfkill_blocked(eeepc);
  507. bool absent;
  508. u32 l;
  509. if (eeepc->wlan_rfkill)
  510. rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
  511. mutex_lock(&eeepc->hotplug_lock);
  512. if (eeepc->hotplug_slot) {
  513. bus = pci_find_bus(0, 1);
  514. if (!bus) {
  515. pr_warning("Unable to find PCI bus 1?\n");
  516. goto out_unlock;
  517. }
  518. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  519. pr_err("Unable to read PCI config space?\n");
  520. goto out_unlock;
  521. }
  522. absent = (l == 0xffffffff);
  523. if (blocked != absent) {
  524. pr_warning("BIOS says wireless lan is %s, "
  525. "but the pci device is %s\n",
  526. blocked ? "blocked" : "unblocked",
  527. absent ? "absent" : "present");
  528. pr_warning("skipped wireless hotplug as probably "
  529. "inappropriate for this model\n");
  530. goto out_unlock;
  531. }
  532. if (!blocked) {
  533. dev = pci_get_slot(bus, 0);
  534. if (dev) {
  535. /* Device already present */
  536. pci_dev_put(dev);
  537. goto out_unlock;
  538. }
  539. dev = pci_scan_single_device(bus, 0);
  540. if (dev) {
  541. pci_bus_assign_resources(bus);
  542. if (pci_bus_add_device(dev))
  543. pr_err("Unable to hotplug wifi\n");
  544. }
  545. } else {
  546. dev = pci_get_slot(bus, 0);
  547. if (dev) {
  548. pci_remove_bus_device(dev);
  549. pci_dev_put(dev);
  550. }
  551. }
  552. }
  553. out_unlock:
  554. mutex_unlock(&eeepc->hotplug_lock);
  555. }
  556. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  557. {
  558. struct eeepc_laptop *eeepc = data;
  559. if (event != ACPI_NOTIFY_BUS_CHECK)
  560. return;
  561. eeepc_rfkill_hotplug(eeepc);
  562. }
  563. static int eeepc_register_rfkill_notifier(struct eeepc_laptop *eeepc,
  564. char *node)
  565. {
  566. acpi_status status;
  567. acpi_handle handle;
  568. status = acpi_get_handle(NULL, node, &handle);
  569. if (ACPI_SUCCESS(status)) {
  570. status = acpi_install_notify_handler(handle,
  571. ACPI_SYSTEM_NOTIFY,
  572. eeepc_rfkill_notify,
  573. eeepc);
  574. if (ACPI_FAILURE(status))
  575. pr_warning("Failed to register notify on %s\n", node);
  576. } else
  577. return -ENODEV;
  578. return 0;
  579. }
  580. static void eeepc_unregister_rfkill_notifier(struct eeepc_laptop *eeepc,
  581. char *node)
  582. {
  583. acpi_status status = AE_OK;
  584. acpi_handle handle;
  585. status = acpi_get_handle(NULL, node, &handle);
  586. if (ACPI_SUCCESS(status)) {
  587. status = acpi_remove_notify_handler(handle,
  588. ACPI_SYSTEM_NOTIFY,
  589. eeepc_rfkill_notify);
  590. if (ACPI_FAILURE(status))
  591. pr_err("Error removing rfkill notify handler %s\n",
  592. node);
  593. }
  594. }
  595. static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
  596. u8 *value)
  597. {
  598. struct eeepc_laptop *eeepc = hotplug_slot->private;
  599. int val = get_acpi(eeepc, CM_ASL_WLAN);
  600. if (val == 1 || val == 0)
  601. *value = val;
  602. else
  603. return -EINVAL;
  604. return 0;
  605. }
  606. static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  607. {
  608. kfree(hotplug_slot->info);
  609. kfree(hotplug_slot);
  610. }
  611. static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
  612. .owner = THIS_MODULE,
  613. .get_adapter_status = eeepc_get_adapter_status,
  614. .get_power_status = eeepc_get_adapter_status,
  615. };
  616. static int eeepc_setup_pci_hotplug(struct eeepc_laptop *eeepc)
  617. {
  618. int ret = -ENOMEM;
  619. struct pci_bus *bus = pci_find_bus(0, 1);
  620. if (!bus) {
  621. pr_err("Unable to find wifi PCI bus\n");
  622. return -ENODEV;
  623. }
  624. eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  625. if (!eeepc->hotplug_slot)
  626. goto error_slot;
  627. eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  628. GFP_KERNEL);
  629. if (!eeepc->hotplug_slot->info)
  630. goto error_info;
  631. eeepc->hotplug_slot->private = eeepc;
  632. eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
  633. eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
  634. eeepc_get_adapter_status(eeepc->hotplug_slot,
  635. &eeepc->hotplug_slot->info->adapter_status);
  636. ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
  637. if (ret) {
  638. pr_err("Unable to register hotplug slot - %d\n", ret);
  639. goto error_register;
  640. }
  641. return 0;
  642. error_register:
  643. kfree(eeepc->hotplug_slot->info);
  644. error_info:
  645. kfree(eeepc->hotplug_slot);
  646. eeepc->hotplug_slot = NULL;
  647. error_slot:
  648. return ret;
  649. }
  650. /*
  651. * Rfkill devices
  652. */
  653. static int eeepc_rfkill_set(void *data, bool blocked)
  654. {
  655. acpi_handle handle = data;
  656. return write_acpi_int(handle, NULL, !blocked);
  657. }
  658. static const struct rfkill_ops eeepc_rfkill_ops = {
  659. .set_block = eeepc_rfkill_set,
  660. };
  661. static int eeepc_new_rfkill(struct eeepc_laptop *eeepc,
  662. struct rfkill **rfkill,
  663. const char *name,
  664. enum rfkill_type type, int cm)
  665. {
  666. acpi_handle handle;
  667. int result;
  668. result = acpi_setter_handle(eeepc, cm, &handle);
  669. if (result < 0)
  670. return result;
  671. *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
  672. &eeepc_rfkill_ops, handle);
  673. if (!*rfkill)
  674. return -EINVAL;
  675. rfkill_init_sw_state(*rfkill, get_acpi(eeepc, cm) != 1);
  676. result = rfkill_register(*rfkill);
  677. if (result) {
  678. rfkill_destroy(*rfkill);
  679. *rfkill = NULL;
  680. return result;
  681. }
  682. return 0;
  683. }
  684. static void eeepc_rfkill_exit(struct eeepc_laptop *eeepc)
  685. {
  686. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  687. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  688. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  689. if (eeepc->wlan_rfkill) {
  690. rfkill_unregister(eeepc->wlan_rfkill);
  691. rfkill_destroy(eeepc->wlan_rfkill);
  692. eeepc->wlan_rfkill = NULL;
  693. }
  694. /*
  695. * Refresh pci hotplug in case the rfkill state was changed after
  696. * eeepc_unregister_rfkill_notifier()
  697. */
  698. eeepc_rfkill_hotplug(eeepc);
  699. if (eeepc->hotplug_slot)
  700. pci_hp_deregister(eeepc->hotplug_slot);
  701. if (eeepc->bluetooth_rfkill) {
  702. rfkill_unregister(eeepc->bluetooth_rfkill);
  703. rfkill_destroy(eeepc->bluetooth_rfkill);
  704. eeepc->bluetooth_rfkill = NULL;
  705. }
  706. if (eeepc->wwan3g_rfkill) {
  707. rfkill_unregister(eeepc->wwan3g_rfkill);
  708. rfkill_destroy(eeepc->wwan3g_rfkill);
  709. eeepc->wwan3g_rfkill = NULL;
  710. }
  711. if (eeepc->wimax_rfkill) {
  712. rfkill_unregister(eeepc->wimax_rfkill);
  713. rfkill_destroy(eeepc->wimax_rfkill);
  714. eeepc->wimax_rfkill = NULL;
  715. }
  716. }
  717. static int eeepc_rfkill_init(struct eeepc_laptop *eeepc)
  718. {
  719. int result = 0;
  720. mutex_init(&eeepc->hotplug_lock);
  721. result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
  722. "eeepc-wlan", RFKILL_TYPE_WLAN,
  723. CM_ASL_WLAN);
  724. if (result && result != -ENODEV)
  725. goto exit;
  726. result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
  727. "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
  728. CM_ASL_BLUETOOTH);
  729. if (result && result != -ENODEV)
  730. goto exit;
  731. result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
  732. "eeepc-wwan3g", RFKILL_TYPE_WWAN,
  733. CM_ASL_3G);
  734. if (result && result != -ENODEV)
  735. goto exit;
  736. result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
  737. "eeepc-wimax", RFKILL_TYPE_WIMAX,
  738. CM_ASL_WIMAX);
  739. if (result && result != -ENODEV)
  740. goto exit;
  741. if (eeepc->hotplug_disabled)
  742. return 0;
  743. result = eeepc_setup_pci_hotplug(eeepc);
  744. /*
  745. * If we get -EBUSY then something else is handling the PCI hotplug -
  746. * don't fail in this case
  747. */
  748. if (result == -EBUSY)
  749. result = 0;
  750. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  751. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  752. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  753. /*
  754. * Refresh pci hotplug in case the rfkill state was changed during
  755. * setup.
  756. */
  757. eeepc_rfkill_hotplug(eeepc);
  758. exit:
  759. if (result && result != -ENODEV)
  760. eeepc_rfkill_exit(eeepc);
  761. return result;
  762. }
  763. /*
  764. * Platform driver - hibernate/resume callbacks
  765. */
  766. static int eeepc_hotk_thaw(struct device *device)
  767. {
  768. struct eeepc_laptop *eeepc = dev_get_drvdata(device);
  769. if (eeepc->wlan_rfkill) {
  770. bool wlan;
  771. /*
  772. * Work around bios bug - acpi _PTS turns off the wireless led
  773. * during suspend. Normally it restores it on resume, but
  774. * we should kick it ourselves in case hibernation is aborted.
  775. */
  776. wlan = get_acpi(eeepc, CM_ASL_WLAN);
  777. set_acpi(eeepc, CM_ASL_WLAN, wlan);
  778. }
  779. return 0;
  780. }
  781. static int eeepc_hotk_restore(struct device *device)
  782. {
  783. struct eeepc_laptop *eeepc = dev_get_drvdata(device);
  784. /* Refresh both wlan rfkill state and pci hotplug */
  785. if (eeepc->wlan_rfkill)
  786. eeepc_rfkill_hotplug(eeepc);
  787. if (eeepc->bluetooth_rfkill)
  788. rfkill_set_sw_state(eeepc->bluetooth_rfkill,
  789. get_acpi(eeepc, CM_ASL_BLUETOOTH) != 1);
  790. if (eeepc->wwan3g_rfkill)
  791. rfkill_set_sw_state(eeepc->wwan3g_rfkill,
  792. get_acpi(eeepc, CM_ASL_3G) != 1);
  793. if (eeepc->wimax_rfkill)
  794. rfkill_set_sw_state(eeepc->wimax_rfkill,
  795. get_acpi(eeepc, CM_ASL_WIMAX) != 1);
  796. return 0;
  797. }
  798. static const struct dev_pm_ops eeepc_pm_ops = {
  799. .thaw = eeepc_hotk_thaw,
  800. .restore = eeepc_hotk_restore,
  801. };
  802. static struct platform_driver platform_driver = {
  803. .driver = {
  804. .name = EEEPC_LAPTOP_FILE,
  805. .owner = THIS_MODULE,
  806. .pm = &eeepc_pm_ops,
  807. }
  808. };
  809. /*
  810. * Hwmon device
  811. */
  812. #define EEEPC_EC_SC00 0x61
  813. #define EEEPC_EC_FAN_PWM (EEEPC_EC_SC00 + 2) /* Fan PWM duty cycle (%) */
  814. #define EEEPC_EC_FAN_HRPM (EEEPC_EC_SC00 + 5) /* High byte, fan speed (RPM) */
  815. #define EEEPC_EC_FAN_LRPM (EEEPC_EC_SC00 + 6) /* Low byte, fan speed (RPM) */
  816. #define EEEPC_EC_SFB0 0xD0
  817. #define EEEPC_EC_FAN_CTRL (EEEPC_EC_SFB0 + 3) /* Byte containing SF25 */
  818. static int eeepc_get_fan_pwm(void)
  819. {
  820. u8 value = 0;
  821. ec_read(EEEPC_EC_FAN_PWM, &value);
  822. return value * 255 / 100;
  823. }
  824. static void eeepc_set_fan_pwm(int value)
  825. {
  826. value = SENSORS_LIMIT(value, 0, 255);
  827. value = value * 100 / 255;
  828. ec_write(EEEPC_EC_FAN_PWM, value);
  829. }
  830. static int eeepc_get_fan_rpm(void)
  831. {
  832. u8 high = 0;
  833. u8 low = 0;
  834. ec_read(EEEPC_EC_FAN_HRPM, &high);
  835. ec_read(EEEPC_EC_FAN_LRPM, &low);
  836. return high << 8 | low;
  837. }
  838. static int eeepc_get_fan_ctrl(void)
  839. {
  840. u8 value = 0;
  841. ec_read(EEEPC_EC_FAN_CTRL, &value);
  842. if (value & 0x02)
  843. return 1; /* manual */
  844. else
  845. return 2; /* automatic */
  846. }
  847. static void eeepc_set_fan_ctrl(int manual)
  848. {
  849. u8 value = 0;
  850. ec_read(EEEPC_EC_FAN_CTRL, &value);
  851. if (manual == 1)
  852. value |= 0x02;
  853. else
  854. value &= ~0x02;
  855. ec_write(EEEPC_EC_FAN_CTRL, value);
  856. }
  857. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  858. {
  859. int rv, value;
  860. rv = parse_arg(buf, count, &value);
  861. if (rv > 0)
  862. set(value);
  863. return rv;
  864. }
  865. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  866. {
  867. return sprintf(buf, "%d\n", get());
  868. }
  869. #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
  870. static ssize_t show_##_name(struct device *dev, \
  871. struct device_attribute *attr, \
  872. char *buf) \
  873. { \
  874. return show_sys_hwmon(_set, buf); \
  875. } \
  876. static ssize_t store_##_name(struct device *dev, \
  877. struct device_attribute *attr, \
  878. const char *buf, size_t count) \
  879. { \
  880. return store_sys_hwmon(_get, buf, count); \
  881. } \
  882. static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
  883. EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
  884. EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
  885. eeepc_get_fan_pwm, eeepc_set_fan_pwm);
  886. EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  887. eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
  888. static ssize_t
  889. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  890. {
  891. return sprintf(buf, "eeepc\n");
  892. }
  893. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  894. static struct attribute *hwmon_attributes[] = {
  895. &sensor_dev_attr_pwm1.dev_attr.attr,
  896. &sensor_dev_attr_fan1_input.dev_attr.attr,
  897. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  898. &sensor_dev_attr_name.dev_attr.attr,
  899. NULL
  900. };
  901. static struct attribute_group hwmon_attribute_group = {
  902. .attrs = hwmon_attributes
  903. };
  904. static void eeepc_hwmon_exit(struct eeepc_laptop *eeepc)
  905. {
  906. struct device *hwmon;
  907. hwmon = eeepc->hwmon_device;
  908. if (!hwmon)
  909. return;
  910. sysfs_remove_group(&hwmon->kobj,
  911. &hwmon_attribute_group);
  912. hwmon_device_unregister(hwmon);
  913. eeepc->hwmon_device = NULL;
  914. }
  915. static int eeepc_hwmon_init(struct eeepc_laptop *eeepc)
  916. {
  917. struct device *hwmon;
  918. int result;
  919. hwmon = hwmon_device_register(&eeepc->platform_device->dev);
  920. if (IS_ERR(hwmon)) {
  921. pr_err("Could not register eeepc hwmon device\n");
  922. eeepc->hwmon_device = NULL;
  923. return PTR_ERR(hwmon);
  924. }
  925. eeepc->hwmon_device = hwmon;
  926. result = sysfs_create_group(&hwmon->kobj,
  927. &hwmon_attribute_group);
  928. if (result)
  929. eeepc_hwmon_exit(eeepc);
  930. return result;
  931. }
  932. /*
  933. * Backlight device
  934. */
  935. static int read_brightness(struct backlight_device *bd)
  936. {
  937. struct eeepc_laptop *eeepc = bl_get_data(bd);
  938. return get_acpi(eeepc, CM_ASL_PANELBRIGHT);
  939. }
  940. static int set_brightness(struct backlight_device *bd, int value)
  941. {
  942. struct eeepc_laptop *eeepc = bl_get_data(bd);
  943. return set_acpi(eeepc, CM_ASL_PANELBRIGHT, value);
  944. }
  945. static int update_bl_status(struct backlight_device *bd)
  946. {
  947. return set_brightness(bd, bd->props.brightness);
  948. }
  949. static struct backlight_ops eeepcbl_ops = {
  950. .get_brightness = read_brightness,
  951. .update_status = update_bl_status,
  952. };
  953. static int eeepc_backlight_notify(struct eeepc_laptop *eeepc)
  954. {
  955. struct backlight_device *bd = eeepc->backlight_device;
  956. int old = bd->props.brightness;
  957. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  958. return old;
  959. }
  960. static int eeepc_backlight_init(struct eeepc_laptop *eeepc)
  961. {
  962. struct backlight_properties props;
  963. struct backlight_device *bd;
  964. memset(&props, 0, sizeof(struct backlight_properties));
  965. props.max_brightness = 15;
  966. bd = backlight_device_register(EEEPC_LAPTOP_FILE,
  967. &eeepc->platform_device->dev, eeepc,
  968. &eeepcbl_ops, &props);
  969. if (IS_ERR(bd)) {
  970. pr_err("Could not register eeepc backlight device\n");
  971. eeepc->backlight_device = NULL;
  972. return PTR_ERR(bd);
  973. }
  974. eeepc->backlight_device = bd;
  975. bd->props.brightness = read_brightness(bd);
  976. bd->props.power = FB_BLANK_UNBLANK;
  977. backlight_update_status(bd);
  978. return 0;
  979. }
  980. static void eeepc_backlight_exit(struct eeepc_laptop *eeepc)
  981. {
  982. if (eeepc->backlight_device)
  983. backlight_device_unregister(eeepc->backlight_device);
  984. eeepc->backlight_device = NULL;
  985. }
  986. /*
  987. * Input device (i.e. hotkeys)
  988. */
  989. static int eeepc_input_init(struct eeepc_laptop *eeepc)
  990. {
  991. struct input_dev *input;
  992. int error;
  993. input = input_allocate_device();
  994. if (!input) {
  995. pr_info("Unable to allocate input device\n");
  996. return -ENOMEM;
  997. }
  998. input->name = "Asus EeePC extra buttons";
  999. input->phys = EEEPC_LAPTOP_FILE "/input0";
  1000. input->id.bustype = BUS_HOST;
  1001. input->dev.parent = &eeepc->platform_device->dev;
  1002. error = sparse_keymap_setup(input, eeepc_keymap, NULL);
  1003. if (error) {
  1004. pr_err("Unable to setup input device keymap\n");
  1005. goto err_free_dev;
  1006. }
  1007. error = input_register_device(input);
  1008. if (error) {
  1009. pr_err("Unable to register input device\n");
  1010. goto err_free_keymap;
  1011. }
  1012. eeepc->inputdev = input;
  1013. return 0;
  1014. err_free_keymap:
  1015. sparse_keymap_free(input);
  1016. err_free_dev:
  1017. input_free_device(input);
  1018. return error;
  1019. }
  1020. static void eeepc_input_exit(struct eeepc_laptop *eeepc)
  1021. {
  1022. if (eeepc->inputdev) {
  1023. sparse_keymap_free(eeepc->inputdev);
  1024. input_unregister_device(eeepc->inputdev);
  1025. }
  1026. eeepc->inputdev = NULL;
  1027. }
  1028. /*
  1029. * ACPI driver
  1030. */
  1031. static void eeepc_acpi_notify(struct acpi_device *device, u32 event)
  1032. {
  1033. struct eeepc_laptop *eeepc = acpi_driver_data(device);
  1034. u16 count;
  1035. if (event > ACPI_MAX_SYS_NOTIFY)
  1036. return;
  1037. count = eeepc->event_count[event % 128]++;
  1038. acpi_bus_generate_proc_event(device, event, count);
  1039. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1040. dev_name(&device->dev), event,
  1041. count);
  1042. /* Brightness events are special */
  1043. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX) {
  1044. /* Ignore them completely if the acpi video driver is used */
  1045. if (eeepc->backlight_device != NULL) {
  1046. int old_brightness, new_brightness;
  1047. /* Update the backlight device. */
  1048. old_brightness = eeepc_backlight_notify(eeepc);
  1049. /* Convert event to keypress (obsolescent hack) */
  1050. new_brightness = event - NOTIFY_BRN_MIN;
  1051. if (new_brightness < old_brightness) {
  1052. event = NOTIFY_BRN_MIN; /* brightness down */
  1053. } else if (new_brightness > old_brightness) {
  1054. event = NOTIFY_BRN_MAX; /* brightness up */
  1055. } else {
  1056. /*
  1057. * no change in brightness - already at min/max,
  1058. * event will be desired value (or else ignored)
  1059. */
  1060. }
  1061. sparse_keymap_report_event(eeepc->inputdev, event,
  1062. 1, true);
  1063. }
  1064. } else {
  1065. /* Everything else is a bona-fide keypress event */
  1066. sparse_keymap_report_event(eeepc->inputdev, event, 1, true);
  1067. }
  1068. }
  1069. static void eeepc_dmi_check(struct eeepc_laptop *eeepc)
  1070. {
  1071. const char *model;
  1072. model = dmi_get_system_info(DMI_PRODUCT_NAME);
  1073. if (!model)
  1074. return;
  1075. /*
  1076. * Blacklist for setting cpufv (cpu speed).
  1077. *
  1078. * EeePC 4G ("701") implements CFVS, but it is not supported
  1079. * by the pre-installed OS, and the original option to change it
  1080. * in the BIOS setup screen was removed in later versions.
  1081. *
  1082. * Judging by the lack of "Super Hybrid Engine" on Asus product pages,
  1083. * this applies to all "701" models (4G/4G Surf/2G Surf).
  1084. *
  1085. * So Asus made a deliberate decision not to support it on this model.
  1086. * We have several reports that using it can cause the system to hang
  1087. *
  1088. * The hang has also been reported on a "702" (Model name "8G"?).
  1089. *
  1090. * We avoid dmi_check_system() / dmi_match(), because they use
  1091. * substring matching. We don't want to affect the "701SD"
  1092. * and "701SDX" models, because they do support S.H.E.
  1093. */
  1094. if (strcmp(model, "701") == 0 || strcmp(model, "702") == 0) {
  1095. eeepc->cpufv_disabled = true;
  1096. pr_info("model %s does not officially support setting cpu "
  1097. "speed\n", model);
  1098. pr_info("cpufv disabled to avoid instability\n");
  1099. }
  1100. /*
  1101. * Blacklist for wlan hotplug
  1102. *
  1103. * Eeepc 1005HA doesn't work like others models and don't need the
  1104. * hotplug code. In fact, current hotplug code seems to unplug another
  1105. * device...
  1106. */
  1107. if (strcmp(model, "1005HA") == 0 || strcmp(model, "1201N") == 0 ||
  1108. strcmp(model, "1005PE") == 0) {
  1109. eeepc->hotplug_disabled = true;
  1110. pr_info("wlan hotplug disabled\n");
  1111. }
  1112. }
  1113. static void cmsg_quirk(struct eeepc_laptop *eeepc, int cm, const char *name)
  1114. {
  1115. int dummy;
  1116. /* Some BIOSes do not report cm although it is avaliable.
  1117. Check if cm_getv[cm] works and, if yes, assume cm should be set. */
  1118. if (!(eeepc->cm_supported & (1 << cm))
  1119. && !read_acpi_int(eeepc->handle, cm_getv[cm], &dummy)) {
  1120. pr_info("%s (%x) not reported by BIOS,"
  1121. " enabling anyway\n", name, 1 << cm);
  1122. eeepc->cm_supported |= 1 << cm;
  1123. }
  1124. }
  1125. static void cmsg_quirks(struct eeepc_laptop *eeepc)
  1126. {
  1127. cmsg_quirk(eeepc, CM_ASL_LID, "LID");
  1128. cmsg_quirk(eeepc, CM_ASL_TYPE, "TYPE");
  1129. cmsg_quirk(eeepc, CM_ASL_PANELPOWER, "PANELPOWER");
  1130. cmsg_quirk(eeepc, CM_ASL_TPD, "TPD");
  1131. }
  1132. static int __devinit eeepc_acpi_init(struct eeepc_laptop *eeepc)
  1133. {
  1134. unsigned int init_flags;
  1135. int result;
  1136. result = acpi_bus_get_status(eeepc->device);
  1137. if (result)
  1138. return result;
  1139. if (!eeepc->device->status.present) {
  1140. pr_err("Hotkey device not present, aborting\n");
  1141. return -ENODEV;
  1142. }
  1143. init_flags = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  1144. pr_notice("Hotkey init flags 0x%x\n", init_flags);
  1145. if (write_acpi_int(eeepc->handle, "INIT", init_flags)) {
  1146. pr_err("Hotkey initialization failed\n");
  1147. return -ENODEV;
  1148. }
  1149. /* get control methods supported */
  1150. if (read_acpi_int(eeepc->handle, "CMSG", &eeepc->cm_supported)) {
  1151. pr_err("Get control methods supported failed\n");
  1152. return -ENODEV;
  1153. }
  1154. cmsg_quirks(eeepc);
  1155. pr_info("Get control methods supported: 0x%x\n", eeepc->cm_supported);
  1156. return 0;
  1157. }
  1158. static void __devinit eeepc_enable_camera(struct eeepc_laptop *eeepc)
  1159. {
  1160. /*
  1161. * If the following call to set_acpi() fails, it's because there's no
  1162. * camera so we can ignore the error.
  1163. */
  1164. if (get_acpi(eeepc, CM_ASL_CAMERA) == 0)
  1165. set_acpi(eeepc, CM_ASL_CAMERA, 1);
  1166. }
  1167. static bool eeepc_device_present;
  1168. static int __devinit eeepc_acpi_add(struct acpi_device *device)
  1169. {
  1170. struct eeepc_laptop *eeepc;
  1171. int result;
  1172. pr_notice(EEEPC_LAPTOP_NAME "\n");
  1173. eeepc = kzalloc(sizeof(struct eeepc_laptop), GFP_KERNEL);
  1174. if (!eeepc)
  1175. return -ENOMEM;
  1176. eeepc->handle = device->handle;
  1177. strcpy(acpi_device_name(device), EEEPC_ACPI_DEVICE_NAME);
  1178. strcpy(acpi_device_class(device), EEEPC_ACPI_CLASS);
  1179. device->driver_data = eeepc;
  1180. eeepc->device = device;
  1181. eeepc->hotplug_disabled = hotplug_disabled;
  1182. eeepc_dmi_check(eeepc);
  1183. result = eeepc_acpi_init(eeepc);
  1184. if (result)
  1185. goto fail_platform;
  1186. eeepc_enable_camera(eeepc);
  1187. /*
  1188. * Register the platform device first. It is used as a parent for the
  1189. * sub-devices below.
  1190. *
  1191. * Note that if there are multiple instances of this ACPI device it
  1192. * will bail out, because the platform device is registered with a
  1193. * fixed name. Of course it doesn't make sense to have more than one,
  1194. * and machine-specific scripts find the fixed name convenient. But
  1195. * It's also good for us to exclude multiple instances because both
  1196. * our hwmon and our wlan rfkill subdevice use global ACPI objects
  1197. * (the EC and the wlan PCI slot respectively).
  1198. */
  1199. result = eeepc_platform_init(eeepc);
  1200. if (result)
  1201. goto fail_platform;
  1202. if (!acpi_video_backlight_support()) {
  1203. result = eeepc_backlight_init(eeepc);
  1204. if (result)
  1205. goto fail_backlight;
  1206. } else
  1207. pr_info("Backlight controlled by ACPI video driver\n");
  1208. result = eeepc_input_init(eeepc);
  1209. if (result)
  1210. goto fail_input;
  1211. result = eeepc_hwmon_init(eeepc);
  1212. if (result)
  1213. goto fail_hwmon;
  1214. result = eeepc_led_init(eeepc);
  1215. if (result)
  1216. goto fail_led;
  1217. result = eeepc_rfkill_init(eeepc);
  1218. if (result)
  1219. goto fail_rfkill;
  1220. eeepc_device_present = true;
  1221. return 0;
  1222. fail_rfkill:
  1223. eeepc_led_exit(eeepc);
  1224. fail_led:
  1225. eeepc_hwmon_exit(eeepc);
  1226. fail_hwmon:
  1227. eeepc_input_exit(eeepc);
  1228. fail_input:
  1229. eeepc_backlight_exit(eeepc);
  1230. fail_backlight:
  1231. eeepc_platform_exit(eeepc);
  1232. fail_platform:
  1233. kfree(eeepc);
  1234. return result;
  1235. }
  1236. static int eeepc_acpi_remove(struct acpi_device *device, int type)
  1237. {
  1238. struct eeepc_laptop *eeepc = acpi_driver_data(device);
  1239. eeepc_backlight_exit(eeepc);
  1240. eeepc_rfkill_exit(eeepc);
  1241. eeepc_input_exit(eeepc);
  1242. eeepc_hwmon_exit(eeepc);
  1243. eeepc_led_exit(eeepc);
  1244. eeepc_platform_exit(eeepc);
  1245. kfree(eeepc);
  1246. return 0;
  1247. }
  1248. static const struct acpi_device_id eeepc_device_ids[] = {
  1249. {EEEPC_ACPI_HID, 0},
  1250. {"", 0},
  1251. };
  1252. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  1253. static struct acpi_driver eeepc_acpi_driver = {
  1254. .name = EEEPC_LAPTOP_NAME,
  1255. .class = EEEPC_ACPI_CLASS,
  1256. .owner = THIS_MODULE,
  1257. .ids = eeepc_device_ids,
  1258. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1259. .ops = {
  1260. .add = eeepc_acpi_add,
  1261. .remove = eeepc_acpi_remove,
  1262. .notify = eeepc_acpi_notify,
  1263. },
  1264. };
  1265. static int __init eeepc_laptop_init(void)
  1266. {
  1267. int result;
  1268. result = platform_driver_register(&platform_driver);
  1269. if (result < 0)
  1270. return result;
  1271. result = acpi_bus_register_driver(&eeepc_acpi_driver);
  1272. if (result < 0)
  1273. goto fail_acpi_driver;
  1274. if (!eeepc_device_present) {
  1275. result = -ENODEV;
  1276. goto fail_no_device;
  1277. }
  1278. return 0;
  1279. fail_no_device:
  1280. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1281. fail_acpi_driver:
  1282. platform_driver_unregister(&platform_driver);
  1283. return result;
  1284. }
  1285. static void __exit eeepc_laptop_exit(void)
  1286. {
  1287. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1288. platform_driver_unregister(&platform_driver);
  1289. }
  1290. module_init(eeepc_laptop_init);
  1291. module_exit(eeepc_laptop_exit);