diff options
Diffstat (limited to 'Documentation/laptops/freefall.c')
-rw-r--r-- | Documentation/laptops/freefall.c | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/Documentation/laptops/freefall.c b/Documentation/laptops/freefall.c new file mode 100644 index 000000000000..aab2ff09e868 --- /dev/null +++ b/Documentation/laptops/freefall.c | |||
@@ -0,0 +1,177 @@ | |||
1 | /* Disk protection for HP/DELL machines. | ||
2 | * | ||
3 | * Copyright 2008 Eric Piel | ||
4 | * Copyright 2009 Pavel Machek <pavel@ucw.cz> | ||
5 | * Copyright 2012 Sonal Santan | ||
6 | * Copyright 2014 Pali Rohár <pali.rohar@gmail.com> | ||
7 | * | ||
8 | * GPLv2. | ||
9 | */ | ||
10 | |||
11 | #include <stdio.h> | ||
12 | #include <stdlib.h> | ||
13 | #include <unistd.h> | ||
14 | #include <fcntl.h> | ||
15 | #include <sys/stat.h> | ||
16 | #include <sys/types.h> | ||
17 | #include <string.h> | ||
18 | #include <stdint.h> | ||
19 | #include <errno.h> | ||
20 | #include <signal.h> | ||
21 | #include <sys/mman.h> | ||
22 | #include <sched.h> | ||
23 | #include <syslog.h> | ||
24 | |||
25 | static int noled; | ||
26 | static char unload_heads_path[64]; | ||
27 | static char device_path[32]; | ||
28 | static const char app_name[] = "FREE FALL"; | ||
29 | |||
30 | static int set_unload_heads_path(char *device) | ||
31 | { | ||
32 | char devname[64]; | ||
33 | |||
34 | if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0) | ||
35 | return -EINVAL; | ||
36 | strncpy(devname, device + 5, sizeof(devname) - 1); | ||
37 | strncpy(device_path, device, sizeof(device_path) - 1); | ||
38 | |||
39 | snprintf(unload_heads_path, sizeof(unload_heads_path) - 1, | ||
40 | "/sys/block/%s/device/unload_heads", devname); | ||
41 | return 0; | ||
42 | } | ||
43 | |||
44 | static int valid_disk(void) | ||
45 | { | ||
46 | int fd = open(unload_heads_path, O_RDONLY); | ||
47 | |||
48 | if (fd < 0) { | ||
49 | perror(unload_heads_path); | ||
50 | return 0; | ||
51 | } | ||
52 | |||
53 | close(fd); | ||
54 | return 1; | ||
55 | } | ||
56 | |||
57 | static void write_int(char *path, int i) | ||
58 | { | ||
59 | char buf[1024]; | ||
60 | int fd = open(path, O_RDWR); | ||
61 | |||
62 | if (fd < 0) { | ||
63 | perror("open"); | ||
64 | exit(1); | ||
65 | } | ||
66 | |||
67 | sprintf(buf, "%d", i); | ||
68 | |||
69 | if (write(fd, buf, strlen(buf)) != strlen(buf)) { | ||
70 | perror("write"); | ||
71 | exit(1); | ||
72 | } | ||
73 | |||
74 | close(fd); | ||
75 | } | ||
76 | |||
77 | static void set_led(int on) | ||
78 | { | ||
79 | if (noled) | ||
80 | return; | ||
81 | write_int("/sys/class/leds/hp::hddprotect/brightness", on); | ||
82 | } | ||
83 | |||
84 | static void protect(int seconds) | ||
85 | { | ||
86 | const char *str = (seconds == 0) ? "Unparked" : "Parked"; | ||
87 | |||
88 | write_int(unload_heads_path, seconds*1000); | ||
89 | syslog(LOG_INFO, "%s %s disk head\n", str, device_path); | ||
90 | } | ||
91 | |||
92 | static int on_ac(void) | ||
93 | { | ||
94 | /* /sys/class/power_supply/AC0/online */ | ||
95 | return 1; | ||
96 | } | ||
97 | |||
98 | static int lid_open(void) | ||
99 | { | ||
100 | /* /proc/acpi/button/lid/LID/state */ | ||
101 | return 1; | ||
102 | } | ||
103 | |||
104 | static void ignore_me(int signum) | ||
105 | { | ||
106 | protect(0); | ||
107 | set_led(0); | ||
108 | } | ||
109 | |||
110 | int main(int argc, char **argv) | ||
111 | { | ||
112 | int fd, ret; | ||
113 | struct stat st; | ||
114 | struct sched_param param; | ||
115 | |||
116 | if (argc == 1) | ||
117 | ret = set_unload_heads_path("/dev/sda"); | ||
118 | else if (argc == 2) | ||
119 | ret = set_unload_heads_path(argv[1]); | ||
120 | else | ||
121 | ret = -EINVAL; | ||
122 | |||
123 | if (ret || !valid_disk()) { | ||
124 | fprintf(stderr, "usage: %s <device> (default: /dev/sda)\n", | ||
125 | argv[0]); | ||
126 | exit(1); | ||
127 | } | ||
128 | |||
129 | fd = open("/dev/freefall", O_RDONLY); | ||
130 | if (fd < 0) { | ||
131 | perror("/dev/freefall"); | ||
132 | return EXIT_FAILURE; | ||
133 | } | ||
134 | |||
135 | if (stat("/sys/class/leds/hp::hddprotect/brightness", &st)) | ||
136 | noled = 1; | ||
137 | |||
138 | if (daemon(0, 0) != 0) { | ||
139 | perror("daemon"); | ||
140 | return EXIT_FAILURE; | ||
141 | } | ||
142 | |||
143 | openlog(app_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); | ||
144 | |||
145 | param.sched_priority = sched_get_priority_max(SCHED_FIFO); | ||
146 | sched_setscheduler(0, SCHED_FIFO, ¶m); | ||
147 | mlockall(MCL_CURRENT|MCL_FUTURE); | ||
148 | |||
149 | signal(SIGALRM, ignore_me); | ||
150 | |||
151 | for (;;) { | ||
152 | unsigned char count; | ||
153 | |||
154 | ret = read(fd, &count, sizeof(count)); | ||
155 | alarm(0); | ||
156 | if ((ret == -1) && (errno == EINTR)) { | ||
157 | /* Alarm expired, time to unpark the heads */ | ||
158 | continue; | ||
159 | } | ||
160 | |||
161 | if (ret != sizeof(count)) { | ||
162 | perror("read"); | ||
163 | break; | ||
164 | } | ||
165 | |||
166 | protect(21); | ||
167 | set_led(1); | ||
168 | if (1 || on_ac() || lid_open()) | ||
169 | alarm(2); | ||
170 | else | ||
171 | alarm(20); | ||
172 | } | ||
173 | |||
174 | closelog(); | ||
175 | close(fd); | ||
176 | return EXIT_SUCCESS; | ||
177 | } | ||