eeepc-laptop.c 34 KB

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