eeepc-wmi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Eee PC WMI hotkey driver
  3. *
  4. * Copyright(C) 2010 Intel Corporation.
  5. * Copyright(C) 2010 Corentin Chary <corentin.chary@gmail.com>
  6. *
  7. * Portions based on wistron_btns.c:
  8. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  9. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  10. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/slab.h>
  32. #include <linux/input.h>
  33. #include <linux/input/sparse-keymap.h>
  34. #include <linux/fb.h>
  35. #include <linux/backlight.h>
  36. #include <linux/leds.h>
  37. #include <linux/rfkill.h>
  38. #include <linux/debugfs.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/platform_device.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/acpi_drivers.h>
  43. #define EEEPC_WMI_FILE "eeepc-wmi"
  44. MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
  45. MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
  46. MODULE_LICENSE("GPL");
  47. #define EEEPC_ACPI_HID "ASUS010" /* old _HID used in eeepc-laptop */
  48. #define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
  49. #define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
  50. MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
  51. MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
  52. #define NOTIFY_BRNUP_MIN 0x11
  53. #define NOTIFY_BRNUP_MAX 0x1f
  54. #define NOTIFY_BRNDOWN_MIN 0x20
  55. #define NOTIFY_BRNDOWN_MAX 0x2e
  56. #define EEEPC_WMI_METHODID_DEVS 0x53564544
  57. #define EEEPC_WMI_METHODID_DSTS 0x53544344
  58. #define EEEPC_WMI_METHODID_CFVS 0x53564643
  59. #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
  60. #define EEEPC_WMI_DEVID_TPDLED 0x00100011
  61. #define EEEPC_WMI_DEVID_WLAN 0x00010011
  62. #define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
  63. #define EEEPC_WMI_DEVID_WWAN3G 0x00010019
  64. static const struct key_entry eeepc_wmi_keymap[] = {
  65. /* Sleep already handled via generic ACPI code */
  66. { KE_KEY, 0x5d, { KEY_WLAN } },
  67. { KE_KEY, 0x32, { KEY_MUTE } },
  68. { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
  69. { KE_KEY, 0x30, { KEY_VOLUMEUP } },
  70. { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
  71. { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
  72. { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
  73. { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
  74. { KE_KEY, 0xe1, { KEY_F14 } },
  75. { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
  76. { KE_KEY, 0xe0, { KEY_PROG1 } },
  77. { KE_KEY, 0x5c, { KEY_F15 } },
  78. { KE_END, 0},
  79. };
  80. struct bios_args {
  81. u32 dev_id;
  82. u32 ctrl_param;
  83. };
  84. /*
  85. * eeepc-wmi/ - debugfs root directory
  86. * dev_id - current dev_id
  87. * ctrl_param - current ctrl_param
  88. * devs - call DEVS(dev_id, ctrl_param) and print result
  89. * dsts - call DSTS(dev_id) and print result
  90. */
  91. struct eeepc_wmi_debug {
  92. struct dentry *root;
  93. u32 dev_id;
  94. u32 ctrl_param;
  95. };
  96. struct eeepc_wmi {
  97. struct input_dev *inputdev;
  98. struct backlight_device *backlight_device;
  99. struct platform_device *platform_device;
  100. struct led_classdev tpd_led;
  101. int tpd_led_wk;
  102. struct workqueue_struct *led_workqueue;
  103. struct work_struct tpd_led_work;
  104. struct rfkill *wlan_rfkill;
  105. struct rfkill *bluetooth_rfkill;
  106. struct rfkill *wwan3g_rfkill;
  107. struct eeepc_wmi_debug debug;
  108. };
  109. /* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
  110. static struct platform_device *platform_device;
  111. static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
  112. {
  113. int err;
  114. eeepc->inputdev = input_allocate_device();
  115. if (!eeepc->inputdev)
  116. return -ENOMEM;
  117. eeepc->inputdev->name = "Eee PC WMI hotkeys";
  118. eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
  119. eeepc->inputdev->id.bustype = BUS_HOST;
  120. eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
  121. err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
  122. if (err)
  123. goto err_free_dev;
  124. err = input_register_device(eeepc->inputdev);
  125. if (err)
  126. goto err_free_keymap;
  127. return 0;
  128. err_free_keymap:
  129. sparse_keymap_free(eeepc->inputdev);
  130. err_free_dev:
  131. input_free_device(eeepc->inputdev);
  132. return err;
  133. }
  134. static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
  135. {
  136. if (eeepc->inputdev) {
  137. sparse_keymap_free(eeepc->inputdev);
  138. input_unregister_device(eeepc->inputdev);
  139. }
  140. eeepc->inputdev = NULL;
  141. }
  142. static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
  143. {
  144. struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
  145. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  146. union acpi_object *obj;
  147. acpi_status status;
  148. u32 tmp;
  149. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  150. 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
  151. if (ACPI_FAILURE(status))
  152. return status;
  153. obj = (union acpi_object *)output.pointer;
  154. if (obj && obj->type == ACPI_TYPE_INTEGER)
  155. tmp = (u32)obj->integer.value;
  156. else
  157. tmp = 0;
  158. if (retval)
  159. *retval = tmp;
  160. kfree(obj);
  161. return status;
  162. }
  163. static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
  164. u32 *retval)
  165. {
  166. struct bios_args args = {
  167. .dev_id = dev_id,
  168. .ctrl_param = ctrl_param,
  169. };
  170. struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
  171. acpi_status status;
  172. if (!retval) {
  173. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
  174. EEEPC_WMI_METHODID_DEVS,
  175. &input, NULL);
  176. } else {
  177. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  178. union acpi_object *obj;
  179. u32 tmp;
  180. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
  181. EEEPC_WMI_METHODID_DEVS,
  182. &input, &output);
  183. if (ACPI_FAILURE(status))
  184. return status;
  185. obj = (union acpi_object *)output.pointer;
  186. if (obj && obj->type == ACPI_TYPE_INTEGER)
  187. tmp = (u32)obj->integer.value;
  188. else
  189. tmp = 0;
  190. *retval = tmp;
  191. kfree(obj);
  192. }
  193. return status;
  194. }
  195. /*
  196. * LEDs
  197. */
  198. /*
  199. * These functions actually update the LED's, and are called from a
  200. * workqueue. By doing this as separate work rather than when the LED
  201. * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
  202. * potentially bad time, such as a timer interrupt.
  203. */
  204. static void tpd_led_update(struct work_struct *work)
  205. {
  206. int ctrl_param;
  207. struct eeepc_wmi *eeepc;
  208. eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
  209. ctrl_param = eeepc->tpd_led_wk;
  210. eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
  211. }
  212. static void tpd_led_set(struct led_classdev *led_cdev,
  213. enum led_brightness value)
  214. {
  215. struct eeepc_wmi *eeepc;
  216. eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
  217. eeepc->tpd_led_wk = !!value;
  218. queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
  219. }
  220. static int read_tpd_state(struct eeepc_wmi *eeepc)
  221. {
  222. u32 retval;
  223. acpi_status status;
  224. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_TPDLED, &retval);
  225. if (ACPI_FAILURE(status))
  226. return -1;
  227. else if (!retval || retval == 0x00060000)
  228. /*
  229. * if touchpad led is present, DSTS will set some bits,
  230. * usually 0x00020000.
  231. * 0x00060000 means that the device is not supported
  232. */
  233. return -ENODEV;
  234. else
  235. /* Status is stored in the first bit */
  236. return retval & 0x1;
  237. }
  238. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  239. {
  240. struct eeepc_wmi *eeepc;
  241. eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
  242. return read_tpd_state(eeepc);
  243. }
  244. static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
  245. {
  246. int rv;
  247. if (read_tpd_state(eeepc) < 0)
  248. return 0;
  249. eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
  250. if (!eeepc->led_workqueue)
  251. return -ENOMEM;
  252. INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
  253. eeepc->tpd_led.name = "eeepc::touchpad";
  254. eeepc->tpd_led.brightness_set = tpd_led_set;
  255. eeepc->tpd_led.brightness_get = tpd_led_get;
  256. eeepc->tpd_led.max_brightness = 1;
  257. rv = led_classdev_register(&eeepc->platform_device->dev,
  258. &eeepc->tpd_led);
  259. if (rv) {
  260. destroy_workqueue(eeepc->led_workqueue);
  261. return rv;
  262. }
  263. return 0;
  264. }
  265. static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
  266. {
  267. if (eeepc->tpd_led.dev)
  268. led_classdev_unregister(&eeepc->tpd_led);
  269. if (eeepc->led_workqueue)
  270. destroy_workqueue(eeepc->led_workqueue);
  271. }
  272. /*
  273. * Rfkill devices
  274. */
  275. static int eeepc_rfkill_set(void *data, bool blocked)
  276. {
  277. int dev_id = (unsigned long)data;
  278. u32 ctrl_param = !blocked;
  279. return eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
  280. }
  281. static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
  282. {
  283. int dev_id = (unsigned long)data;
  284. u32 retval;
  285. acpi_status status;
  286. status = eeepc_wmi_get_devstate(dev_id, &retval);
  287. if (ACPI_FAILURE(status))
  288. return ;
  289. rfkill_set_sw_state(rfkill, !(retval & 0x1));
  290. }
  291. static const struct rfkill_ops eeepc_rfkill_ops = {
  292. .set_block = eeepc_rfkill_set,
  293. .query = eeepc_rfkill_query,
  294. };
  295. static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
  296. struct rfkill **rfkill,
  297. const char *name,
  298. enum rfkill_type type, int dev_id)
  299. {
  300. int result;
  301. u32 retval;
  302. acpi_status status;
  303. status = eeepc_wmi_get_devstate(dev_id, &retval);
  304. if (ACPI_FAILURE(status))
  305. return -1;
  306. /* If the device is present, DSTS will always set some bits
  307. * 0x00070000 - 1110000000000000000 - device supported
  308. * 0x00060000 - 1100000000000000000 - not supported
  309. * 0x00020000 - 0100000000000000000 - device supported
  310. * 0x00010000 - 0010000000000000000 - not supported / special mode ?
  311. */
  312. if (!retval || retval == 0x00060000)
  313. return -ENODEV;
  314. *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
  315. &eeepc_rfkill_ops, (void *)(long)dev_id);
  316. if (!*rfkill)
  317. return -EINVAL;
  318. rfkill_init_sw_state(*rfkill, !(retval & 0x1));
  319. result = rfkill_register(*rfkill);
  320. if (result) {
  321. rfkill_destroy(*rfkill);
  322. *rfkill = NULL;
  323. return result;
  324. }
  325. return 0;
  326. }
  327. static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
  328. {
  329. if (eeepc->wlan_rfkill) {
  330. rfkill_unregister(eeepc->wlan_rfkill);
  331. rfkill_destroy(eeepc->wlan_rfkill);
  332. eeepc->wlan_rfkill = NULL;
  333. }
  334. if (eeepc->bluetooth_rfkill) {
  335. rfkill_unregister(eeepc->bluetooth_rfkill);
  336. rfkill_destroy(eeepc->bluetooth_rfkill);
  337. eeepc->bluetooth_rfkill = NULL;
  338. }
  339. if (eeepc->wwan3g_rfkill) {
  340. rfkill_unregister(eeepc->wwan3g_rfkill);
  341. rfkill_destroy(eeepc->wwan3g_rfkill);
  342. eeepc->wwan3g_rfkill = NULL;
  343. }
  344. }
  345. static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
  346. {
  347. int result = 0;
  348. result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
  349. "eeepc-wlan", RFKILL_TYPE_WLAN,
  350. EEEPC_WMI_DEVID_WLAN);
  351. if (result && result != -ENODEV)
  352. goto exit;
  353. result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
  354. "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
  355. EEEPC_WMI_DEVID_BLUETOOTH);
  356. if (result && result != -ENODEV)
  357. goto exit;
  358. result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
  359. "eeepc-wwan3g", RFKILL_TYPE_WWAN,
  360. EEEPC_WMI_DEVID_WWAN3G);
  361. if (result && result != -ENODEV)
  362. goto exit;
  363. exit:
  364. if (result && result != -ENODEV)
  365. eeepc_wmi_rfkill_exit(eeepc);
  366. if (result == -ENODEV)
  367. result = 0;
  368. return result;
  369. }
  370. /*
  371. * Backlight
  372. */
  373. static int read_brightness(struct backlight_device *bd)
  374. {
  375. u32 retval;
  376. acpi_status status;
  377. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
  378. if (ACPI_FAILURE(status))
  379. return -1;
  380. else
  381. return retval & 0xFF;
  382. }
  383. static int update_bl_status(struct backlight_device *bd)
  384. {
  385. u32 ctrl_param;
  386. acpi_status status;
  387. ctrl_param = bd->props.brightness;
  388. status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
  389. ctrl_param, NULL);
  390. if (ACPI_FAILURE(status))
  391. return -1;
  392. else
  393. return 0;
  394. }
  395. static const struct backlight_ops eeepc_wmi_bl_ops = {
  396. .get_brightness = read_brightness,
  397. .update_status = update_bl_status,
  398. };
  399. static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
  400. {
  401. struct backlight_device *bd = eeepc->backlight_device;
  402. int old = bd->props.brightness;
  403. int new = old;
  404. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  405. new = code - NOTIFY_BRNUP_MIN + 1;
  406. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  407. new = code - NOTIFY_BRNDOWN_MIN;
  408. bd->props.brightness = new;
  409. backlight_update_status(bd);
  410. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  411. return old;
  412. }
  413. static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
  414. {
  415. struct backlight_device *bd;
  416. struct backlight_properties props;
  417. memset(&props, 0, sizeof(struct backlight_properties));
  418. props.max_brightness = 15;
  419. bd = backlight_device_register(EEEPC_WMI_FILE,
  420. &eeepc->platform_device->dev, eeepc,
  421. &eeepc_wmi_bl_ops, &props);
  422. if (IS_ERR(bd)) {
  423. pr_err("Could not register backlight device\n");
  424. return PTR_ERR(bd);
  425. }
  426. eeepc->backlight_device = bd;
  427. bd->props.brightness = read_brightness(bd);
  428. bd->props.power = FB_BLANK_UNBLANK;
  429. backlight_update_status(bd);
  430. return 0;
  431. }
  432. static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
  433. {
  434. if (eeepc->backlight_device)
  435. backlight_device_unregister(eeepc->backlight_device);
  436. eeepc->backlight_device = NULL;
  437. }
  438. static void eeepc_wmi_notify(u32 value, void *context)
  439. {
  440. struct eeepc_wmi *eeepc = context;
  441. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  442. union acpi_object *obj;
  443. acpi_status status;
  444. int code;
  445. int orig_code;
  446. status = wmi_get_event_data(value, &response);
  447. if (status != AE_OK) {
  448. pr_err("bad event status 0x%x\n", status);
  449. return;
  450. }
  451. obj = (union acpi_object *)response.pointer;
  452. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  453. code = obj->integer.value;
  454. orig_code = code;
  455. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  456. code = NOTIFY_BRNUP_MIN;
  457. else if (code >= NOTIFY_BRNDOWN_MIN &&
  458. code <= NOTIFY_BRNDOWN_MAX)
  459. code = NOTIFY_BRNDOWN_MIN;
  460. if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
  461. if (!acpi_video_backlight_support())
  462. eeepc_wmi_backlight_notify(eeepc, orig_code);
  463. }
  464. if (!sparse_keymap_report_event(eeepc->inputdev,
  465. code, 1, true))
  466. pr_info("Unknown key %x pressed\n", code);
  467. }
  468. kfree(obj);
  469. }
  470. static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
  471. const char *buf, size_t count)
  472. {
  473. int value;
  474. struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
  475. acpi_status status;
  476. if (!count || sscanf(buf, "%i", &value) != 1)
  477. return -EINVAL;
  478. if (value < 0 || value > 2)
  479. return -EINVAL;
  480. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  481. 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
  482. if (ACPI_FAILURE(status))
  483. return -EIO;
  484. else
  485. return count;
  486. }
  487. static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
  488. static struct attribute *platform_attributes[] = {
  489. &dev_attr_cpufv.attr,
  490. NULL
  491. };
  492. static struct attribute_group platform_attribute_group = {
  493. .attrs = platform_attributes
  494. };
  495. static void eeepc_wmi_sysfs_exit(struct platform_device *device)
  496. {
  497. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  498. }
  499. static int eeepc_wmi_sysfs_init(struct platform_device *device)
  500. {
  501. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  502. }
  503. /*
  504. * Platform device
  505. */
  506. static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
  507. {
  508. int err;
  509. eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
  510. if (!eeepc->platform_device)
  511. return -ENOMEM;
  512. platform_set_drvdata(eeepc->platform_device, eeepc);
  513. err = platform_device_add(eeepc->platform_device);
  514. if (err)
  515. goto fail_platform_device;
  516. err = eeepc_wmi_sysfs_init(eeepc->platform_device);
  517. if (err)
  518. goto fail_sysfs;
  519. return 0;
  520. fail_sysfs:
  521. platform_device_del(eeepc->platform_device);
  522. fail_platform_device:
  523. platform_device_put(eeepc->platform_device);
  524. return err;
  525. }
  526. static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
  527. {
  528. eeepc_wmi_sysfs_exit(eeepc->platform_device);
  529. platform_device_unregister(eeepc->platform_device);
  530. }
  531. /*
  532. * debugfs
  533. */
  534. struct eeepc_wmi_debugfs_node {
  535. struct eeepc_wmi *eeepc;
  536. char *name;
  537. int (*show)(struct seq_file *m, void *data);
  538. };
  539. static int show_dsts(struct seq_file *m, void *data)
  540. {
  541. struct eeepc_wmi *eeepc = m->private;
  542. acpi_status status;
  543. u32 retval = -1;
  544. status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
  545. if (ACPI_FAILURE(status))
  546. return -EIO;
  547. seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
  548. return 0;
  549. }
  550. static int show_devs(struct seq_file *m, void *data)
  551. {
  552. struct eeepc_wmi *eeepc = m->private;
  553. acpi_status status;
  554. u32 retval = -1;
  555. status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
  556. eeepc->debug.ctrl_param, &retval);
  557. if (ACPI_FAILURE(status))
  558. return -EIO;
  559. seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
  560. eeepc->debug.ctrl_param, retval);
  561. return 0;
  562. }
  563. static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
  564. { NULL, "devs", show_devs },
  565. { NULL, "dsts", show_dsts },
  566. };
  567. static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
  568. {
  569. struct eeepc_wmi_debugfs_node *node = inode->i_private;
  570. return single_open(file, node->show, node->eeepc);
  571. }
  572. static const struct file_operations eeepc_wmi_debugfs_io_ops = {
  573. .owner = THIS_MODULE,
  574. .open = eeepc_wmi_debugfs_open,
  575. .read = seq_read,
  576. .llseek = seq_lseek,
  577. .release = single_release,
  578. };
  579. static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
  580. {
  581. debugfs_remove_recursive(eeepc->debug.root);
  582. }
  583. static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
  584. {
  585. struct dentry *dent;
  586. int i;
  587. eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
  588. if (!eeepc->debug.root) {
  589. pr_err("failed to create debugfs directory");
  590. goto error_debugfs;
  591. }
  592. dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
  593. eeepc->debug.root, &eeepc->debug.dev_id);
  594. if (!dent)
  595. goto error_debugfs;
  596. dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
  597. eeepc->debug.root, &eeepc->debug.ctrl_param);
  598. if (!dent)
  599. goto error_debugfs;
  600. for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
  601. struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
  602. node->eeepc = eeepc;
  603. dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  604. eeepc->debug.root, node,
  605. &eeepc_wmi_debugfs_io_ops);
  606. if (!dent) {
  607. pr_err("failed to create debug file: %s\n", node->name);
  608. goto error_debugfs;
  609. }
  610. }
  611. return 0;
  612. error_debugfs:
  613. eeepc_wmi_debugfs_exit(eeepc);
  614. return -ENOMEM;
  615. }
  616. /*
  617. * WMI Driver
  618. */
  619. static struct platform_device * __init eeepc_wmi_add(void)
  620. {
  621. struct eeepc_wmi *eeepc;
  622. acpi_status status;
  623. int err;
  624. eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
  625. if (!eeepc)
  626. return ERR_PTR(-ENOMEM);
  627. /*
  628. * Register the platform device first. It is used as a parent for the
  629. * sub-devices below.
  630. */
  631. err = eeepc_wmi_platform_init(eeepc);
  632. if (err)
  633. goto fail_platform;
  634. err = eeepc_wmi_input_init(eeepc);
  635. if (err)
  636. goto fail_input;
  637. err = eeepc_wmi_led_init(eeepc);
  638. if (err)
  639. goto fail_leds;
  640. err = eeepc_wmi_rfkill_init(eeepc);
  641. if (err)
  642. goto fail_rfkill;
  643. if (!acpi_video_backlight_support()) {
  644. err = eeepc_wmi_backlight_init(eeepc);
  645. if (err)
  646. goto fail_backlight;
  647. } else
  648. pr_info("Backlight controlled by ACPI video driver\n");
  649. status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
  650. eeepc_wmi_notify, eeepc);
  651. if (ACPI_FAILURE(status)) {
  652. pr_err("Unable to register notify handler - %d\n",
  653. status);
  654. err = -ENODEV;
  655. goto fail_wmi_handler;
  656. }
  657. err = eeepc_wmi_debugfs_init(eeepc);
  658. if (err)
  659. goto fail_debugfs;
  660. return eeepc->platform_device;
  661. fail_debugfs:
  662. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  663. fail_wmi_handler:
  664. eeepc_wmi_backlight_exit(eeepc);
  665. fail_backlight:
  666. eeepc_wmi_rfkill_exit(eeepc);
  667. fail_rfkill:
  668. eeepc_wmi_led_exit(eeepc);
  669. fail_leds:
  670. eeepc_wmi_input_exit(eeepc);
  671. fail_input:
  672. eeepc_wmi_platform_exit(eeepc);
  673. fail_platform:
  674. kfree(eeepc);
  675. return ERR_PTR(err);
  676. }
  677. static int eeepc_wmi_remove(struct platform_device *device)
  678. {
  679. struct eeepc_wmi *eeepc;
  680. eeepc = platform_get_drvdata(device);
  681. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  682. eeepc_wmi_backlight_exit(eeepc);
  683. eeepc_wmi_input_exit(eeepc);
  684. eeepc_wmi_led_exit(eeepc);
  685. eeepc_wmi_rfkill_exit(eeepc);
  686. eeepc_wmi_debugfs_exit(eeepc);
  687. eeepc_wmi_platform_exit(eeepc);
  688. kfree(eeepc);
  689. return 0;
  690. }
  691. static struct platform_driver platform_driver = {
  692. .driver = {
  693. .name = EEEPC_WMI_FILE,
  694. .owner = THIS_MODULE,
  695. },
  696. };
  697. static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
  698. void *context, void **retval)
  699. {
  700. pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
  701. *(bool *)context = true;
  702. return AE_CTRL_TERMINATE;
  703. }
  704. static int __init eeepc_wmi_check_atkd(void)
  705. {
  706. acpi_status status;
  707. bool found = false;
  708. status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
  709. &found, NULL);
  710. if (ACPI_FAILURE(status) || !found)
  711. return 0;
  712. return -1;
  713. }
  714. static int __init eeepc_wmi_init(void)
  715. {
  716. int err;
  717. if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
  718. !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
  719. pr_warning("No known WMI GUID found\n");
  720. return -ENODEV;
  721. }
  722. if (eeepc_wmi_check_atkd()) {
  723. pr_warning("WMI device present, but legacy ATKD device is also "
  724. "present and enabled.");
  725. pr_warning("You probably booted with acpi_osi=\"Linux\" or "
  726. "acpi_osi=\"!Windows 2009\"");
  727. pr_warning("Can't load eeepc-wmi, use default acpi_osi "
  728. "(preferred) or eeepc-laptop");
  729. return -ENODEV;
  730. }
  731. platform_device = eeepc_wmi_add();
  732. if (IS_ERR(platform_device)) {
  733. err = PTR_ERR(platform_device);
  734. goto fail_eeepc_wmi;
  735. }
  736. err = platform_driver_register(&platform_driver);
  737. if (err) {
  738. pr_warning("Unable to register platform driver\n");
  739. goto fail_platform_driver;
  740. }
  741. return 0;
  742. fail_platform_driver:
  743. eeepc_wmi_remove(platform_device);
  744. fail_eeepc_wmi:
  745. return err;
  746. }
  747. static void __exit eeepc_wmi_exit(void)
  748. {
  749. eeepc_wmi_remove(platform_device);
  750. platform_driver_unregister(&platform_driver);
  751. }
  752. module_init(eeepc_wmi_init);
  753. module_exit(eeepc_wmi_exit);