ideapad-laptop.c 9.2 KB

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