ideapad_acpi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * ideapad_acpi.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. #define IDEAPAD_DEV_CAMERA 0
  30. #define IDEAPAD_DEV_WLAN 1
  31. #define IDEAPAD_DEV_BLUETOOTH 2
  32. #define IDEAPAD_DEV_3G 3
  33. #define IDEAPAD_DEV_KILLSW 4
  34. struct ideapad_private {
  35. acpi_handle handle;
  36. struct rfkill *rfk[5];
  37. };
  38. static struct {
  39. char *name;
  40. int cfgbit;
  41. int type;
  42. } ideapad_rfk_data[] = {
  43. { "ideapad_camera", 19, NUM_RFKILL_TYPES },
  44. { "ideapad_wlan", 18, RFKILL_TYPE_WLAN },
  45. { "ideapad_bluetooth", 16, RFKILL_TYPE_BLUETOOTH },
  46. { "ideapad_3g", 17, RFKILL_TYPE_WWAN },
  47. { "ideapad_killsw", 0, RFKILL_TYPE_WLAN }
  48. };
  49. /*
  50. * ACPI Helpers
  51. */
  52. #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
  53. static int read_method_int(acpi_handle handle, const char *method, int *val)
  54. {
  55. acpi_status status;
  56. unsigned long long result;
  57. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  58. if (ACPI_FAILURE(status)) {
  59. *val = -1;
  60. return -1;
  61. } else {
  62. *val = result;
  63. return 0;
  64. }
  65. }
  66. static int method_vpcr(acpi_handle handle, int cmd, int *ret)
  67. {
  68. acpi_status status;
  69. unsigned long long result;
  70. struct acpi_object_list params;
  71. union acpi_object in_obj;
  72. params.count = 1;
  73. params.pointer = &in_obj;
  74. in_obj.type = ACPI_TYPE_INTEGER;
  75. in_obj.integer.value = cmd;
  76. status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
  77. if (ACPI_FAILURE(status)) {
  78. *ret = -1;
  79. return -1;
  80. } else {
  81. *ret = result;
  82. return 0;
  83. }
  84. }
  85. static int method_vpcw(acpi_handle handle, int cmd, int data)
  86. {
  87. struct acpi_object_list params;
  88. union acpi_object in_obj[2];
  89. acpi_status status;
  90. params.count = 2;
  91. params.pointer = in_obj;
  92. in_obj[0].type = ACPI_TYPE_INTEGER;
  93. in_obj[0].integer.value = cmd;
  94. in_obj[1].type = ACPI_TYPE_INTEGER;
  95. in_obj[1].integer.value = data;
  96. status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
  97. if (status != AE_OK)
  98. return -1;
  99. return 0;
  100. }
  101. static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
  102. {
  103. int val;
  104. unsigned long int end_jiffies;
  105. if (method_vpcw(handle, 1, cmd))
  106. return -1;
  107. for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
  108. time_before(jiffies, end_jiffies);) {
  109. schedule();
  110. if (method_vpcr(handle, 1, &val))
  111. return -1;
  112. if (val == 0) {
  113. if (method_vpcr(handle, 0, &val))
  114. return -1;
  115. *data = val;
  116. return 0;
  117. }
  118. }
  119. pr_err("timeout in read_ec_cmd\n");
  120. return -1;
  121. }
  122. static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
  123. {
  124. int val;
  125. unsigned long int end_jiffies;
  126. if (method_vpcw(handle, 0, data))
  127. return -1;
  128. if (method_vpcw(handle, 1, cmd))
  129. return -1;
  130. for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
  131. time_before(jiffies, end_jiffies);) {
  132. schedule();
  133. if (method_vpcr(handle, 1, &val))
  134. return -1;
  135. if (val == 0)
  136. return 0;
  137. }
  138. pr_err("timeout in write_ec_cmd\n");
  139. return -1;
  140. }
  141. /* the above is ACPI helpers */
  142. static int ideapad_dev_get_state(int device)
  143. {
  144. acpi_status status;
  145. union acpi_object in_param;
  146. struct acpi_object_list input = { 1, &in_param };
  147. struct acpi_buffer output;
  148. union acpi_object out_obj;
  149. output.length = sizeof(out_obj);
  150. output.pointer = &out_obj;
  151. in_param.type = ACPI_TYPE_INTEGER;
  152. in_param.integer.value = device + 1;
  153. status = acpi_evaluate_object(NULL, "\\_SB_.GECN", &input, &output);
  154. if (ACPI_FAILURE(status)) {
  155. printk(KERN_WARNING "IdeaPAD \\_SB_.GECN method failed %d\n", status);
  156. return -ENODEV;
  157. }
  158. if (out_obj.type != ACPI_TYPE_INTEGER) {
  159. printk(KERN_WARNING "IdeaPAD \\_SB_.GECN method returned unexpected type\n");
  160. return -ENODEV;
  161. }
  162. return out_obj.integer.value;
  163. }
  164. static int ideapad_dev_set_state(int device, int state)
  165. {
  166. acpi_status status;
  167. union acpi_object in_params[2];
  168. struct acpi_object_list input = { 2, in_params };
  169. in_params[0].type = ACPI_TYPE_INTEGER;
  170. in_params[0].integer.value = device + 1;
  171. in_params[1].type = ACPI_TYPE_INTEGER;
  172. in_params[1].integer.value = state;
  173. status = acpi_evaluate_object(NULL, "\\_SB_.SECN", &input, NULL);
  174. if (ACPI_FAILURE(status)) {
  175. printk(KERN_WARNING "IdeaPAD \\_SB_.SECN method failed %d\n", status);
  176. return -ENODEV;
  177. }
  178. return 0;
  179. }
  180. static ssize_t show_ideapad_cam(struct device *dev,
  181. struct device_attribute *attr,
  182. char *buf)
  183. {
  184. struct ideapad_private *priv = dev_get_drvdata(dev);
  185. acpi_handle handle = priv->handle;
  186. unsigned long result;
  187. if (read_ec_data(handle, 0x1D, &result))
  188. return sprintf(buf, "-1\n");
  189. return sprintf(buf, "%lu\n", result);
  190. }
  191. static ssize_t store_ideapad_cam(struct device *dev,
  192. struct device_attribute *attr,
  193. const char *buf, size_t count)
  194. {
  195. struct ideapad_private *priv = dev_get_drvdata(dev);
  196. acpi_handle handle = priv->handle;
  197. int ret, state;
  198. if (!count)
  199. return 0;
  200. if (sscanf(buf, "%i", &state) != 1)
  201. return -EINVAL;
  202. ret = write_ec_cmd(handle, 0x1E, state);
  203. if (ret < 0)
  204. return ret;
  205. return count;
  206. }
  207. static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
  208. static int ideapad_rfk_set(void *data, bool blocked)
  209. {
  210. int device = (unsigned long)data;
  211. if (device == IDEAPAD_DEV_KILLSW)
  212. return -EINVAL;
  213. return ideapad_dev_set_state(device, !blocked);
  214. }
  215. static struct rfkill_ops ideapad_rfk_ops = {
  216. .set_block = ideapad_rfk_set,
  217. };
  218. static void ideapad_sync_rfk_state(struct acpi_device *adevice)
  219. {
  220. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  221. int hw_blocked = !ideapad_dev_get_state(IDEAPAD_DEV_KILLSW);
  222. int i;
  223. rfkill_set_hw_state(priv->rfk[IDEAPAD_DEV_KILLSW], hw_blocked);
  224. for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++)
  225. if (priv->rfk[i])
  226. rfkill_set_hw_state(priv->rfk[i], hw_blocked);
  227. if (hw_blocked)
  228. return;
  229. for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++)
  230. if (priv->rfk[i])
  231. rfkill_set_sw_state(priv->rfk[i], !ideapad_dev_get_state(i));
  232. }
  233. static int ideapad_register_rfkill(struct acpi_device *adevice, int dev)
  234. {
  235. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  236. int ret;
  237. priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev-1].name, &adevice->dev,
  238. ideapad_rfk_data[dev-1].type, &ideapad_rfk_ops,
  239. (void *)(long)dev);
  240. if (!priv->rfk[dev])
  241. return -ENOMEM;
  242. ret = rfkill_register(priv->rfk[dev]);
  243. if (ret) {
  244. rfkill_destroy(priv->rfk[dev]);
  245. return ret;
  246. }
  247. return 0;
  248. }
  249. static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
  250. {
  251. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  252. if (!priv->rfk[dev])
  253. return;
  254. rfkill_unregister(priv->rfk[dev]);
  255. rfkill_destroy(priv->rfk[dev]);
  256. }
  257. static const struct acpi_device_id ideapad_device_ids[] = {
  258. { "VPC2004", 0},
  259. { "", 0},
  260. };
  261. MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
  262. static int ideapad_acpi_add(struct acpi_device *adevice)
  263. {
  264. int i, cfg;
  265. int devs_present[5];
  266. struct ideapad_private *priv;
  267. if (read_method_int(adevice->handle, "_CFG", &cfg))
  268. return -ENODEV;
  269. for (i = IDEAPAD_DEV_CAMERA; i < IDEAPAD_DEV_KILLSW; i++) {
  270. if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg))
  271. devs_present[i] = 1;
  272. else
  273. devs_present[i] = 0;
  274. }
  275. /* The hardware switch is always present */
  276. devs_present[IDEAPAD_DEV_KILLSW] = 1;
  277. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  278. if (!priv)
  279. return -ENOMEM;
  280. if (devs_present[IDEAPAD_DEV_CAMERA]) {
  281. int ret = device_create_file(&adevice->dev, &dev_attr_camera_power);
  282. if (ret) {
  283. kfree(priv);
  284. return ret;
  285. }
  286. }
  287. priv->handle = adevice->handle;
  288. dev_set_drvdata(&adevice->dev, priv);
  289. for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) {
  290. if (!devs_present[i])
  291. continue;
  292. ideapad_register_rfkill(adevice, i);
  293. }
  294. ideapad_sync_rfk_state(adevice);
  295. return 0;
  296. }
  297. static int ideapad_acpi_remove(struct acpi_device *adevice, int type)
  298. {
  299. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  300. int i;
  301. device_remove_file(&adevice->dev, &dev_attr_camera_power);
  302. for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
  303. ideapad_unregister_rfkill(adevice, i);
  304. dev_set_drvdata(&adevice->dev, NULL);
  305. kfree(priv);
  306. return 0;
  307. }
  308. static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
  309. {
  310. acpi_handle handle = adevice->handle;
  311. unsigned long vpc1, vpc2, vpc_bit;
  312. if (read_ec_data(handle, 0x10, &vpc1))
  313. return;
  314. if (read_ec_data(handle, 0x1A, &vpc2))
  315. return;
  316. vpc1 = (vpc2 << 8) | vpc1;
  317. for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
  318. if (test_bit(vpc_bit, &vpc1)) {
  319. if (vpc_bit == 9)
  320. ideapad_sync_rfk_state(adevice);
  321. }
  322. }
  323. }
  324. static struct acpi_driver ideapad_acpi_driver = {
  325. .name = "ideapad_acpi",
  326. .class = "IdeaPad",
  327. .ids = ideapad_device_ids,
  328. .ops.add = ideapad_acpi_add,
  329. .ops.remove = ideapad_acpi_remove,
  330. .ops.notify = ideapad_acpi_notify,
  331. .owner = THIS_MODULE,
  332. };
  333. static int __init ideapad_acpi_module_init(void)
  334. {
  335. acpi_bus_register_driver(&ideapad_acpi_driver);
  336. return 0;
  337. }
  338. static void __exit ideapad_acpi_module_exit(void)
  339. {
  340. acpi_bus_unregister_driver(&ideapad_acpi_driver);
  341. }
  342. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  343. MODULE_DESCRIPTION("IdeaPad ACPI Extras");
  344. MODULE_LICENSE("GPL");
  345. module_init(ideapad_acpi_module_init);
  346. module_exit(ideapad_acpi_module_exit);