ideapad-laptop.c 12 KB

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