ideapad-laptop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
  3. *
  4. * Copyright © 2010 Intel Corporation
  5. * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #include <acpi/acpi_bus.h>
  28. #include <acpi/acpi_drivers.h>
  29. #include <linux/rfkill.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/input.h>
  32. #include <linux/input/sparse-keymap.h>
  33. #define IDEAPAD_RFKILL_DEV_NUM (3)
  34. struct ideapad_private {
  35. struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
  36. struct platform_device *platform_device;
  37. struct input_dev *inputdev;
  38. };
  39. static acpi_handle ideapad_handle;
  40. static bool no_bt_rfkill;
  41. module_param(no_bt_rfkill, bool, 0444);
  42. MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
  43. /*
  44. * ACPI Helpers
  45. */
  46. #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
  47. static int read_method_int(acpi_handle handle, const char *method, int *val)
  48. {
  49. acpi_status status;
  50. unsigned long long result;
  51. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  52. if (ACPI_FAILURE(status)) {
  53. *val = -1;
  54. return -1;
  55. } else {
  56. *val = result;
  57. return 0;
  58. }
  59. }
  60. static int method_vpcr(acpi_handle handle, int cmd, int *ret)
  61. {
  62. acpi_status status;
  63. unsigned long long result;
  64. struct acpi_object_list params;
  65. union acpi_object in_obj;
  66. params.count = 1;
  67. params.pointer = &in_obj;
  68. in_obj.type = ACPI_TYPE_INTEGER;
  69. in_obj.integer.value = cmd;
  70. status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
  71. if (ACPI_FAILURE(status)) {
  72. *ret = -1;
  73. return -1;
  74. } else {
  75. *ret = result;
  76. return 0;
  77. }
  78. }
  79. static int method_vpcw(acpi_handle handle, int cmd, int data)
  80. {
  81. struct acpi_object_list params;
  82. union acpi_object in_obj[2];
  83. acpi_status status;
  84. params.count = 2;
  85. params.pointer = in_obj;
  86. in_obj[0].type = ACPI_TYPE_INTEGER;
  87. in_obj[0].integer.value = cmd;
  88. in_obj[1].type = ACPI_TYPE_INTEGER;
  89. in_obj[1].integer.value = data;
  90. status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
  91. if (status != AE_OK)
  92. return -1;
  93. return 0;
  94. }
  95. static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
  96. {
  97. int val;
  98. unsigned long int end_jiffies;
  99. if (method_vpcw(handle, 1, cmd))
  100. return -1;
  101. for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
  102. time_before(jiffies, end_jiffies);) {
  103. schedule();
  104. if (method_vpcr(handle, 1, &val))
  105. return -1;
  106. if (val == 0) {
  107. if (method_vpcr(handle, 0, &val))
  108. return -1;
  109. *data = val;
  110. return 0;
  111. }
  112. }
  113. pr_err("timeout in read_ec_cmd\n");
  114. return -1;
  115. }
  116. static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
  117. {
  118. int val;
  119. unsigned long int end_jiffies;
  120. if (method_vpcw(handle, 0, data))
  121. return -1;
  122. if (method_vpcw(handle, 1, cmd))
  123. return -1;
  124. for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
  125. time_before(jiffies, end_jiffies);) {
  126. schedule();
  127. if (method_vpcr(handle, 1, &val))
  128. return -1;
  129. if (val == 0)
  130. return 0;
  131. }
  132. pr_err("timeout in write_ec_cmd\n");
  133. return -1;
  134. }
  135. /*
  136. * camera power
  137. */
  138. static ssize_t show_ideapad_cam(struct device *dev,
  139. struct device_attribute *attr,
  140. char *buf)
  141. {
  142. unsigned long result;
  143. if (read_ec_data(ideapad_handle, 0x1D, &result))
  144. return sprintf(buf, "-1\n");
  145. return sprintf(buf, "%lu\n", result);
  146. }
  147. static ssize_t store_ideapad_cam(struct device *dev,
  148. struct device_attribute *attr,
  149. const char *buf, size_t count)
  150. {
  151. int ret, state;
  152. if (!count)
  153. return 0;
  154. if (sscanf(buf, "%i", &state) != 1)
  155. return -EINVAL;
  156. ret = write_ec_cmd(ideapad_handle, 0x1E, state);
  157. if (ret < 0)
  158. return ret;
  159. return count;
  160. }
  161. static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
  162. /*
  163. * Rfkill
  164. */
  165. struct ideapad_rfk_data {
  166. char *name;
  167. int cfgbit;
  168. int opcode;
  169. int type;
  170. };
  171. const struct ideapad_rfk_data ideapad_rfk_data[] = {
  172. { "ideapad_wlan", 18, 0x15, RFKILL_TYPE_WLAN },
  173. { "ideapad_bluetooth", 16, 0x17, RFKILL_TYPE_BLUETOOTH },
  174. { "ideapad_3g", 17, 0x20, RFKILL_TYPE_WWAN },
  175. };
  176. static int ideapad_rfk_set(void *data, bool blocked)
  177. {
  178. unsigned long opcode = (unsigned long)data;
  179. return write_ec_cmd(ideapad_handle, opcode, !blocked);
  180. }
  181. static struct rfkill_ops ideapad_rfk_ops = {
  182. .set_block = ideapad_rfk_set,
  183. };
  184. static void ideapad_sync_rfk_state(struct acpi_device *adevice)
  185. {
  186. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  187. unsigned long hw_blocked;
  188. int i;
  189. if (read_ec_data(ideapad_handle, 0x23, &hw_blocked))
  190. return;
  191. hw_blocked = !hw_blocked;
  192. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  193. if (priv->rfk[i])
  194. rfkill_set_hw_state(priv->rfk[i], hw_blocked);
  195. }
  196. static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
  197. int dev)
  198. {
  199. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  200. int ret;
  201. unsigned long sw_blocked;
  202. if (no_bt_rfkill &&
  203. (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
  204. /* Force to enable bluetooth when no_bt_rfkill=1 */
  205. write_ec_cmd(ideapad_handle,
  206. ideapad_rfk_data[dev].opcode, 1);
  207. return 0;
  208. }
  209. priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
  210. ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
  211. (void *)(long)dev);
  212. if (!priv->rfk[dev])
  213. return -ENOMEM;
  214. if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1,
  215. &sw_blocked)) {
  216. rfkill_init_sw_state(priv->rfk[dev], 0);
  217. } else {
  218. sw_blocked = !sw_blocked;
  219. rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
  220. }
  221. ret = rfkill_register(priv->rfk[dev]);
  222. if (ret) {
  223. rfkill_destroy(priv->rfk[dev]);
  224. return ret;
  225. }
  226. return 0;
  227. }
  228. static void __devexit ideapad_unregister_rfkill(struct acpi_device *adevice,
  229. int dev)
  230. {
  231. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  232. if (!priv->rfk[dev])
  233. return;
  234. rfkill_unregister(priv->rfk[dev]);
  235. rfkill_destroy(priv->rfk[dev]);
  236. }
  237. /*
  238. * Platform device
  239. */
  240. static struct attribute *ideapad_attributes[] = {
  241. &dev_attr_camera_power.attr,
  242. NULL
  243. };
  244. static struct attribute_group ideapad_attribute_group = {
  245. .attrs = ideapad_attributes
  246. };
  247. static int __devinit ideapad_platform_init(struct ideapad_private *priv)
  248. {
  249. int result;
  250. priv->platform_device = platform_device_alloc("ideapad", -1);
  251. if (!priv->platform_device)
  252. return -ENOMEM;
  253. platform_set_drvdata(priv->platform_device, priv);
  254. result = platform_device_add(priv->platform_device);
  255. if (result)
  256. goto fail_platform_device;
  257. result = sysfs_create_group(&priv->platform_device->dev.kobj,
  258. &ideapad_attribute_group);
  259. if (result)
  260. goto fail_sysfs;
  261. return 0;
  262. fail_sysfs:
  263. platform_device_del(priv->platform_device);
  264. fail_platform_device:
  265. platform_device_put(priv->platform_device);
  266. return result;
  267. }
  268. static void ideapad_platform_exit(struct ideapad_private *priv)
  269. {
  270. sysfs_remove_group(&priv->platform_device->dev.kobj,
  271. &ideapad_attribute_group);
  272. platform_device_unregister(priv->platform_device);
  273. }
  274. /*
  275. * input device
  276. */
  277. static const struct key_entry ideapad_keymap[] = {
  278. { KE_KEY, 0x06, { KEY_SWITCHVIDEOMODE } },
  279. { KE_KEY, 0x0D, { KEY_WLAN } },
  280. { KE_END, 0 },
  281. };
  282. static int __devinit ideapad_input_init(struct ideapad_private *priv)
  283. {
  284. struct input_dev *inputdev;
  285. int error;
  286. inputdev = input_allocate_device();
  287. if (!inputdev) {
  288. pr_info("Unable to allocate input device\n");
  289. return -ENOMEM;
  290. }
  291. inputdev->name = "Ideapad extra buttons";
  292. inputdev->phys = "ideapad/input0";
  293. inputdev->id.bustype = BUS_HOST;
  294. inputdev->dev.parent = &priv->platform_device->dev;
  295. error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
  296. if (error) {
  297. pr_err("Unable to setup input device keymap\n");
  298. goto err_free_dev;
  299. }
  300. error = input_register_device(inputdev);
  301. if (error) {
  302. pr_err("Unable to register input device\n");
  303. goto err_free_keymap;
  304. }
  305. priv->inputdev = inputdev;
  306. return 0;
  307. err_free_keymap:
  308. sparse_keymap_free(inputdev);
  309. err_free_dev:
  310. input_free_device(inputdev);
  311. return error;
  312. }
  313. static void __devexit ideapad_input_exit(struct ideapad_private *priv)
  314. {
  315. sparse_keymap_free(priv->inputdev);
  316. input_unregister_device(priv->inputdev);
  317. priv->inputdev = NULL;
  318. }
  319. static void ideapad_input_report(struct ideapad_private *priv,
  320. unsigned long scancode)
  321. {
  322. sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
  323. }
  324. /*
  325. * module init/exit
  326. */
  327. static const struct acpi_device_id ideapad_device_ids[] = {
  328. { "VPC2004", 0},
  329. { "", 0},
  330. };
  331. MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
  332. static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
  333. {
  334. int ret, i, cfg;
  335. struct ideapad_private *priv;
  336. if (read_method_int(adevice->handle, "_CFG", &cfg))
  337. return -ENODEV;
  338. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  339. if (!priv)
  340. return -ENOMEM;
  341. dev_set_drvdata(&adevice->dev, priv);
  342. ideapad_handle = adevice->handle;
  343. ret = ideapad_platform_init(priv);
  344. if (ret)
  345. goto platform_failed;
  346. ret = ideapad_input_init(priv);
  347. if (ret)
  348. goto input_failed;
  349. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
  350. if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg))
  351. ideapad_register_rfkill(adevice, i);
  352. else
  353. priv->rfk[i] = NULL;
  354. }
  355. ideapad_sync_rfk_state(adevice);
  356. return 0;
  357. input_failed:
  358. ideapad_platform_exit(priv);
  359. platform_failed:
  360. kfree(priv);
  361. return ret;
  362. }
  363. static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
  364. {
  365. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  366. int i;
  367. for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
  368. ideapad_unregister_rfkill(adevice, i);
  369. ideapad_input_exit(priv);
  370. ideapad_platform_exit(priv);
  371. dev_set_drvdata(&adevice->dev, NULL);
  372. kfree(priv);
  373. return 0;
  374. }
  375. static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
  376. {
  377. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  378. acpi_handle handle = adevice->handle;
  379. unsigned long vpc1, vpc2, vpc_bit;
  380. if (read_ec_data(handle, 0x10, &vpc1))
  381. return;
  382. if (read_ec_data(handle, 0x1A, &vpc2))
  383. return;
  384. vpc1 = (vpc2 << 8) | vpc1;
  385. for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
  386. if (test_bit(vpc_bit, &vpc1)) {
  387. if (vpc_bit == 9)
  388. ideapad_sync_rfk_state(adevice);
  389. else if (vpc_bit == 4)
  390. read_ec_data(handle, 0x12, &vpc2);
  391. else
  392. ideapad_input_report(priv, vpc_bit);
  393. }
  394. }
  395. }
  396. static struct acpi_driver ideapad_acpi_driver = {
  397. .name = "ideapad_acpi",
  398. .class = "IdeaPad",
  399. .ids = ideapad_device_ids,
  400. .ops.add = ideapad_acpi_add,
  401. .ops.remove = ideapad_acpi_remove,
  402. .ops.notify = ideapad_acpi_notify,
  403. .owner = THIS_MODULE,
  404. };
  405. static int __init ideapad_acpi_module_init(void)
  406. {
  407. return acpi_bus_register_driver(&ideapad_acpi_driver);
  408. }
  409. static void __exit ideapad_acpi_module_exit(void)
  410. {
  411. acpi_bus_unregister_driver(&ideapad_acpi_driver);
  412. }
  413. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  414. MODULE_DESCRIPTION("IdeaPad ACPI Extras");
  415. MODULE_LICENSE("GPL");
  416. module_init(ideapad_acpi_module_init);
  417. module_exit(ideapad_acpi_module_exit);