eeepc-wmi.c 21 KB

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