ideapad-laptop.c 8.9 KB

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