ideapad-laptop.c 12 KB

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