ideapad_acpi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_set_state(int device, int state)
  143. {
  144. acpi_status status;
  145. union acpi_object in_params[2];
  146. struct acpi_object_list input = { 2, in_params };
  147. in_params[0].type = ACPI_TYPE_INTEGER;
  148. in_params[0].integer.value = device + 1;
  149. in_params[1].type = ACPI_TYPE_INTEGER;
  150. in_params[1].integer.value = state;
  151. status = acpi_evaluate_object(NULL, "\\_SB_.SECN", &input, NULL);
  152. if (ACPI_FAILURE(status)) {
  153. printk(KERN_WARNING "IdeaPAD \\_SB_.SECN method failed %d\n", status);
  154. return -ENODEV;
  155. }
  156. return 0;
  157. }
  158. static ssize_t show_ideapad_cam(struct device *dev,
  159. struct device_attribute *attr,
  160. char *buf)
  161. {
  162. struct ideapad_private *priv = dev_get_drvdata(dev);
  163. acpi_handle handle = priv->handle;
  164. unsigned long result;
  165. if (read_ec_data(handle, 0x1D, &result))
  166. return sprintf(buf, "-1\n");
  167. return sprintf(buf, "%lu\n", result);
  168. }
  169. static ssize_t store_ideapad_cam(struct device *dev,
  170. struct device_attribute *attr,
  171. const char *buf, size_t count)
  172. {
  173. struct ideapad_private *priv = dev_get_drvdata(dev);
  174. acpi_handle handle = priv->handle;
  175. int ret, state;
  176. if (!count)
  177. return 0;
  178. if (sscanf(buf, "%i", &state) != 1)
  179. return -EINVAL;
  180. ret = write_ec_cmd(handle, 0x1E, state);
  181. if (ret < 0)
  182. return ret;
  183. return count;
  184. }
  185. static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
  186. static int ideapad_rfk_set(void *data, bool blocked)
  187. {
  188. int device = (unsigned long)data;
  189. if (device == IDEAPAD_DEV_KILLSW)
  190. return -EINVAL;
  191. return ideapad_dev_set_state(device, !blocked);
  192. }
  193. static struct rfkill_ops ideapad_rfk_ops = {
  194. .set_block = ideapad_rfk_set,
  195. };
  196. static void ideapad_sync_rfk_state(struct acpi_device *adevice)
  197. {
  198. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  199. acpi_handle handle = priv->handle;
  200. unsigned long hw_blocked;
  201. int i;
  202. if (read_ec_data(handle, 0x23, &hw_blocked))
  203. return;
  204. hw_blocked = !hw_blocked;
  205. for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
  206. if (priv->rfk[i])
  207. rfkill_set_hw_state(priv->rfk[i], hw_blocked);
  208. }
  209. static int ideapad_register_rfkill(struct acpi_device *adevice, int dev)
  210. {
  211. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  212. int ret;
  213. unsigned long sw_blocked;
  214. priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
  215. ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
  216. (void *)(long)dev);
  217. if (!priv->rfk[dev])
  218. return -ENOMEM;
  219. if (read_ec_data(ideapad_priv->handle, ideapad_rfk_data[dev].opcode-1,
  220. &sw_blocked)) {
  221. rfkill_init_sw_state(priv->rfk[dev], 0);
  222. } else {
  223. sw_blocked = !sw_blocked;
  224. rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
  225. }
  226. ret = rfkill_register(priv->rfk[dev]);
  227. if (ret) {
  228. rfkill_destroy(priv->rfk[dev]);
  229. return ret;
  230. }
  231. return 0;
  232. }
  233. static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
  234. {
  235. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  236. if (!priv->rfk[dev])
  237. return;
  238. rfkill_unregister(priv->rfk[dev]);
  239. rfkill_destroy(priv->rfk[dev]);
  240. }
  241. static const struct acpi_device_id ideapad_device_ids[] = {
  242. { "VPC2004", 0},
  243. { "", 0},
  244. };
  245. MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
  246. static int ideapad_acpi_add(struct acpi_device *adevice)
  247. {
  248. int i, cfg;
  249. int devs_present[5];
  250. struct ideapad_private *priv;
  251. if (read_method_int(adevice->handle, "_CFG", &cfg))
  252. return -ENODEV;
  253. for (i = IDEAPAD_DEV_CAMERA; i < IDEAPAD_DEV_KILLSW; i++) {
  254. if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg))
  255. devs_present[i] = 1;
  256. else
  257. devs_present[i] = 0;
  258. }
  259. /* The hardware switch is always present */
  260. devs_present[IDEAPAD_DEV_KILLSW] = 1;
  261. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  262. if (!priv)
  263. return -ENOMEM;
  264. if (devs_present[IDEAPAD_DEV_CAMERA]) {
  265. int ret = device_create_file(&adevice->dev, &dev_attr_camera_power);
  266. if (ret) {
  267. kfree(priv);
  268. return ret;
  269. }
  270. }
  271. priv->handle = adevice->handle;
  272. dev_set_drvdata(&adevice->dev, priv);
  273. for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) {
  274. if (!devs_present[i])
  275. continue;
  276. ideapad_register_rfkill(adevice, i);
  277. }
  278. ideapad_sync_rfk_state(adevice);
  279. return 0;
  280. }
  281. static int ideapad_acpi_remove(struct acpi_device *adevice, int type)
  282. {
  283. struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
  284. int i;
  285. device_remove_file(&adevice->dev, &dev_attr_camera_power);
  286. for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
  287. ideapad_unregister_rfkill(adevice, i);
  288. dev_set_drvdata(&adevice->dev, NULL);
  289. kfree(priv);
  290. return 0;
  291. }
  292. static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
  293. {
  294. acpi_handle handle = adevice->handle;
  295. unsigned long vpc1, vpc2, vpc_bit;
  296. if (read_ec_data(handle, 0x10, &vpc1))
  297. return;
  298. if (read_ec_data(handle, 0x1A, &vpc2))
  299. return;
  300. vpc1 = (vpc2 << 8) | vpc1;
  301. for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
  302. if (test_bit(vpc_bit, &vpc1)) {
  303. if (vpc_bit == 9)
  304. ideapad_sync_rfk_state(adevice);
  305. }
  306. }
  307. }
  308. static struct acpi_driver ideapad_acpi_driver = {
  309. .name = "ideapad_acpi",
  310. .class = "IdeaPad",
  311. .ids = ideapad_device_ids,
  312. .ops.add = ideapad_acpi_add,
  313. .ops.remove = ideapad_acpi_remove,
  314. .ops.notify = ideapad_acpi_notify,
  315. .owner = THIS_MODULE,
  316. };
  317. static int __init ideapad_acpi_module_init(void)
  318. {
  319. acpi_bus_register_driver(&ideapad_acpi_driver);
  320. return 0;
  321. }
  322. static void __exit ideapad_acpi_module_exit(void)
  323. {
  324. acpi_bus_unregister_driver(&ideapad_acpi_driver);
  325. }
  326. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  327. MODULE_DESCRIPTION("IdeaPad ACPI Extras");
  328. MODULE_LICENSE("GPL");
  329. module_init(ideapad_acpi_module_init);
  330. module_exit(ideapad_acpi_module_exit);