summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2012-04-03 03:35:44 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2012-04-03 03:35:44 +0200
commit1f545859f41a28d0ba120c24d33a7e790704cc42 (patch)
tree544cbd3be35a6bcc684490808f1e786018d672fd
parentDon't compile x86 code on other platforms. (diff)
downloadandroid-connect-blinker-1f545859f41a28d0ba120c24d33a7e790704cc42.tar.xz
android-connect-blinker-1f545859f41a28d0ba120c24d33a7e790704cc42.zip
Autodetect backlight
-rw-r--r--blinker.c101
1 files changed, 39 insertions, 62 deletions
diff --git a/blinker.c b/blinker.c
index dbfaa68..36d80f2 100644
--- a/blinker.c
+++ b/blinker.c
@@ -3,25 +3,21 @@
* -- by Jason A. Donenfeld --
* Jason@zx2c4.com
*
- * This creates three files in proc:
+ * This creates two files in proc:
*
* /proc/blinker/trigger_port
- * /proc/blinker/backlight_file
* /proc/blinker/delay_ms
*
* When the system attempts to make a connection to localhost using the port
- * specified in trigger_port, backlight_file is used to blink the backlight
- * for delay_ms milliseconds.
+ * specified in trigger_port, the backlight blinks over delay_ms milliseconds.
*
* This occurs regardless of whether or not there exists anything listening
* on trigger_port.
*
* # cd /proc/blinker/
* # ls
- * backlight_file delay_ms trigger_port
+ * delay_ms trigger_port
* # echo 9184 > trigger_port
- * # cat backlight_file
- * /sys/class/backlight/s5p_bl/brightness
* # cat delay_ms
* 100
* # cat trigger_port
@@ -29,14 +25,11 @@
* # nc localhost 9184
* ZX2C4-Laptop [127.0.0.1] 9184 (?) : Connection refused
* # dmesg | tail -n 4
- * [ 910.109435] blinker: connected to the magic port 9184
- * [ 910.109466] blinker: read backlight level of 8
- * [ 910.110363] blinker: set backlight to 0
- * [ 910.212381] blinker: restored backlight
*
- * Development note:
- * Though this is intended to work on Android/ARM, presently it is only
- * implemented on x86. Work in progress.
+ * [ 184.003146] blinker: connected to the magic port 9184
+ * [ 184.003176] blinker: read backlight sony level of 8
+ * [ 184.004086] blinker: set backlight sony to 0
+ * [ 184.105603] blinker: restored backlight sony
*
*/
@@ -54,7 +47,6 @@
#define PROC_BLINKER "blinker"
#define PROC_PORT "trigger_port"
-#define PROC_FILE "backlight_file"
#define PROC_DELAY "delay_ms"
#define MAX_LENGTH 512
@@ -65,9 +57,8 @@ MODULE_DESCRIPTION("Android Screen Blinker");
extern struct proto_ops inet_stream_ops;
extern void msleep_interruptible(unsigned long msecs);
-static struct proc_dir_entry *proc_blinker, *proc_port, *proc_file, *proc_delay;
+static struct proc_dir_entry *proc_blinker, *proc_port, *proc_delay;
static unsigned int trigger_port, delay_ms;
-static unsigned char backlight_file[MAX_LENGTH];
static void disable_page_protection(void)
{
@@ -91,50 +82,62 @@ static void enable_page_protection(void)
}
#endif
}
-
-void blink(void)
+static int blink_entry(void *__buf, const char *name, int namelen, loff_t dir_offset, u64 ino, unsigned int d_type)
{
struct file *filp;
- struct cred *creds;
- mm_segment_t oldfs;
unsigned long long offset;
char buf[MAX_LENGTH];
- unsigned long previous_backlight;
- creds = prepare_creds();
- commit_creds(prepare_kernel_cred(NULL));
+ if (dir_offset < 2)
+ return 0;
- oldfs = get_fs();
- set_fs(get_ds());
+ snprintf(buf, MAX_LENGTH, "/sys/class/backlight/%s/brightness", name);
- filp = filp_open(backlight_file, O_RDWR, 0);
+ filp = filp_open(buf, O_RDWR, 0);
if(IS_ERR(filp))
goto error;
offset = 0;
+ memset(buf, 0, MAX_LENGTH);
vfs_read(filp, buf, MAX_LENGTH - 1, &offset);
- buf[MAX_LENGTH - 1] = 0;
- sscanf(buf, "%lu", &previous_backlight);
- printk(KERN_INFO "blinker: read backlight level of %lu", previous_backlight);
+ printk(KERN_INFO "blinker: read backlight %s level of %s", name, buf);
offset = 0;
vfs_write(filp, "0\n", 2, &offset);
generic_file_fsync(filp, 0, 2, 0);
- printk(KERN_INFO "blinker: set backlight to 0\n");
+ printk(KERN_INFO "blinker: set backlight %s to 0\n", name);
msleep_interruptible(delay_ms);
- snprintf(buf, MAX_LENGTH, "%lu\n", previous_backlight);
offset = 0;
vfs_write(filp, buf, MAX_LENGTH, &offset);
- printk(KERN_INFO "blinker: restored backlight\n");
+ printk(KERN_INFO "blinker: restored backlight %s\n", name);
+ goto out;
+error:
+ printk(KERN_ERR "blinker: could not open %s\n", buf);
+out:
filp_close(filp, NULL);
+ return 0;
+}
+void blink(void)
+{
+ struct file *filp;
+ struct cred *creds;
+ mm_segment_t oldfs;
- goto out;
+ creds = prepare_creds();
+ commit_creds(prepare_kernel_cred(NULL));
+
+ oldfs = get_fs();
+ set_fs(get_ds());
+ filp = filp_open("/sys/class/backlight/", O_RDONLY | O_DIRECTORY, 0);
+ if(IS_ERR(filp))
+ goto error;
+ vfs_readdir(filp, blink_entry, NULL);
+
+ filp_close(filp, NULL);
error:
- printk(KERN_ERR "blinker: could not work with %s, error %ld\n", backlight_file, PTR_ERR(filp));
-out:
set_fs(oldfs);
commit_creds(creds);
}
@@ -167,27 +170,6 @@ int port_read(char *page, char **start, off_t off, int count, int *eof, void *da
printk(KERN_INFO "blinker: called get_port\n");
return snprintf(page, MAX_LENGTH, "%u\n", trigger_port);
}
-int file_write(struct file *file, const char __user *ubuf, unsigned long count, void *data)
-{
- char buf[MAX_LENGTH];
- char *line;
- printk(KERN_INFO "blinker: called set_file\n");
- if (count > MAX_LENGTH - 1)
- count = MAX_LENGTH - 1;
- if (copy_from_user(&buf, ubuf, count))
- return -EFAULT;
- buf[count + 1] = 0;
- line = strchr(buf, '\n');
- if (line)
- *line = 0;
- memcpy(backlight_file, buf, sizeof(backlight_file));
- return count;
-}
-int file_read(char *page, char **start, off_t off, int count, int *eof, void *data)
-{
- printk(KERN_INFO "blinker: called get_file\n");
- return snprintf(page, MAX_LENGTH, "%s\n", backlight_file);
-}
int delay_write(struct file *file, const char __user *ubuf, unsigned long count, void *data)
{
char buf[MAX_LENGTH];
@@ -213,9 +195,6 @@ static int init(void)
proc_port = create_proc_entry(PROC_PORT, 0600, proc_blinker);
proc_port->read_proc = port_read;
proc_port->write_proc = port_write;
- proc_file = create_proc_entry(PROC_FILE, 0600, proc_blinker);
- proc_file->read_proc = file_read;
- proc_file->write_proc = file_write;
proc_delay = create_proc_entry(PROC_DELAY, 0600, proc_blinker);
proc_delay->read_proc = delay_read;
proc_delay->write_proc = delay_write;
@@ -229,7 +208,6 @@ static int init(void)
trigger_port = 9191;
delay_ms = 100;
- strcpy(backlight_file, "/sys/class/backlight/s5p_bl/brightness");
return 0;
}
@@ -241,7 +219,6 @@ static void exit(void)
printk(KERN_INFO "blinker: unremapped inet_stream_ops.connect\n");
remove_proc_entry(PROC_PORT, proc_blinker);
- remove_proc_entry(PROC_FILE, proc_blinker);
remove_proc_entry(PROC_DELAY, proc_blinker);
remove_proc_entry(PROC_BLINKER, NULL);
printk(KERN_INFO "blinker: removed /proc/blinker/\n");