eeepc-wmi.c 21 KB

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