devicelock_n810.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "devicelock_n810.h"
  15. #include "util.h"
  16. #include "log.h"
  17. #include "main.h"
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <signal.h>
  21. #define KB_BASEPATH "/devices/platform/i2c_omap.2/i2c-2/2-0045"
  22. #define TS_BASEPATH "/devices/platform/omap2_mcspi.1/spi1.0"
  23. #define INPUT_BASEPATH "/devices/virtual/input"
  24. #define GPIOSW_BASEPATH "/devices/platform/gpio-switch"
  25. static void devicelock_n810_toggle(struct devicelock *s)
  26. {
  27. struct devicelock_n810 *sn = container_of(s, struct devicelock_n810, devicelock);
  28. int err, lock;
  29. lock = !(backend.backlight->screen_is_locked(backend.backlight));
  30. logdebug("devicelock: %s the device\n", lock ? "Locking" : "Unlocking");
  31. if (lock)
  32. autodim_suspend(backend.autodim);
  33. /* Set backlight lock state */
  34. err = backlight_screen_lock(backend.backlight, lock);
  35. if (err)
  36. logerr("Failed to set backlight lock state\n");
  37. /* Set touchscreen lock state */
  38. err = file_write_int(sn->ts_disable_file, lock, 10);
  39. if (err)
  40. logerr("Failed to write touchscreen disable file\n");
  41. /* Set keyboard lock state */
  42. err = file_write_int(sn->kb_disable_file, lock, 10);
  43. if (err)
  44. logerr("Failed to write keyboard disable file\n");
  45. if (!lock)
  46. autodim_resume(backend.autodim);
  47. }
  48. static void devicelock_n810_event(struct devicelock *s)
  49. {
  50. struct devicelock_n810 *sn = container_of(s, struct devicelock_n810, devicelock);
  51. char buf[32], *bufp;
  52. int count, switch_state;
  53. count = file_read_string(sn->kb_lock_state_file, buf, sizeof(buf));
  54. if (count <= 0)
  55. return;
  56. bufp = string_strip(buf);
  57. if (strcmp(bufp, "open") == 0) {
  58. switch_state = 0;
  59. } else if (strcmp(bufp, "closed") == 0) {
  60. switch_state = 1;
  61. } else {
  62. logerr("devicelock: Invalid state\n");
  63. return;
  64. }
  65. if (switch_state == sn->switch_state)
  66. return;
  67. sn->switch_state = switch_state;
  68. if (switch_state)
  69. devicelock_n810_toggle(s);
  70. }
  71. static void devicelock_n810_destroy(struct devicelock *s)
  72. {
  73. struct devicelock_n810 *sn = container_of(s, struct devicelock_n810, devicelock);
  74. close(sn->kb_lock_evdev_fd);
  75. file_close(sn->kb_lock_state_file);
  76. file_close(sn->kb_disable_file);
  77. file_close(sn->ts_disable_file);
  78. free(sn);
  79. }
  80. static struct devicelock * devicelock_n810_probe(void)
  81. {
  82. struct devicelock_n810 *sn;
  83. struct fileaccess *ts_disable = NULL;
  84. struct fileaccess *kb_disable = NULL;
  85. struct fileaccess *kb_lock_state = NULL;
  86. struct fileaccess *file;
  87. int kb_lock_evdev = -1;
  88. int kb_lock_evdev_fd = -1;
  89. struct dir_entry *dir_entry;
  90. LIST_HEAD(dir_entries);
  91. int err, count;
  92. unsigned int i;
  93. char buf[PATH_MAX + 1];
  94. ts_disable = sysfs_file_open(O_RDWR, TS_BASEPATH "/disable_ts");
  95. if (!ts_disable)
  96. goto err_close;
  97. kb_disable = sysfs_file_open(O_RDWR, KB_BASEPATH "/disable_kp");
  98. if (!kb_disable)
  99. goto err_close;
  100. kb_lock_state = sysfs_file_open(O_RDONLY, GPIOSW_BASEPATH "/kb_lock/state");
  101. if (!kb_lock_state)
  102. goto err_close;
  103. count = list_sysfs_directory(&dir_entries, INPUT_BASEPATH);
  104. if (count <= 0)
  105. goto err_close;
  106. list_for_each_entry(dir_entry, &dir_entries, list) {
  107. if (sscanf(dir_entry->name, "input%u", &i) != 1)
  108. continue;
  109. file = sysfs_file_open(O_RDONLY, "%s/%s/name",
  110. INPUT_BASEPATH, dir_entry->name);
  111. if (!file)
  112. continue;
  113. count = file_read_string(file, buf, sizeof(buf));
  114. file_close(file);
  115. if (count <= 0)
  116. continue;
  117. if (strcmp(string_strip(buf), "kb_lock") == 0) {
  118. kb_lock_evdev = i;
  119. break;
  120. }
  121. }
  122. dir_entries_free(&dir_entries);
  123. if (kb_lock_evdev < 0)
  124. goto err_close;
  125. snprintf(buf, sizeof(buf), "/dev/input/event%u", kb_lock_evdev);
  126. kb_lock_evdev_fd = open(buf, O_RDONLY);
  127. if (kb_lock_evdev_fd < 0)
  128. goto err_close;
  129. err = fcntl(kb_lock_evdev_fd, F_SETFL, O_ASYNC | O_NONBLOCK);
  130. err |= fcntl(kb_lock_evdev_fd, F_SETSIG, SIGUSR2);
  131. err |= fcntl(kb_lock_evdev_fd, F_SETOWN, getpid());
  132. if (err)
  133. goto err_close;
  134. sn = zalloc(sizeof(*sn));
  135. if (!sn)
  136. goto err_close;
  137. sn->kb_lock_evdev_fd = kb_lock_evdev_fd;
  138. sn->kb_lock_state_file = kb_lock_state;
  139. sn->ts_disable_file = ts_disable;
  140. sn->kb_disable_file = kb_disable;
  141. devicelock_init(&sn->devicelock, "n810");
  142. sn->devicelock.event = devicelock_n810_event;
  143. sn->devicelock.destroy = devicelock_n810_destroy;
  144. logdebug("Screenlock toggle on %s\n", buf);
  145. return &sn->devicelock;
  146. err_close:
  147. if (kb_lock_evdev_fd)
  148. close(kb_lock_evdev_fd);
  149. file_close(kb_lock_state);
  150. file_close(ts_disable);
  151. file_close(kb_disable);
  152. return NULL;
  153. }
  154. DEVICELOCK_PROBE(n810, devicelock_n810_probe);