ideapad-laptop.c 11 KB

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