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