+/*
+ * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/wait.h>
+#include <linux/mutex.h>
+#include <linux/proc_fs.h>
+#include <linux/uaccess.h>
+
+MODULE_LICENSE("GPL");
+
+DECLARE_MUTEX(test_mutex);
+DECLARE_WAIT_QUEUE_HEAD(test_wait);
+
+static char test_buffer[16];
+static size_t test_len;
+
+static int test_open(struct inode* i, struct file *f)
+{
+ return 0;
+}
+
+static ssize_t test_read(struct file *f, char * __user buf,
+ size_t s, loff_t *o)
+{
+ int r;
+ if (wait_event_interruptible(test_wait, test_len > 0))
+ return -ERESTARTSYS;
+ if (down_interruptible(&test_mutex))
+ return -ERESTARTSYS;
+ if (s > test_len)
+ s = test_len;
+ test_len = 0;
+ r = copy_to_user(buf, test_buffer, s);
+ up(&test_mutex);
+ if (r)
+ return -EFAULT;
+ *o += s;
+ return s;
+}
+
+static ssize_t test_write(struct file *f, const char * __user buf,
+ size_t s, loff_t *o)
+{
+ int r;
+ if (s > sizeof(test_buffer))
+ s = sizeof(test_buffer);
+ if (down_interruptible(&test_mutex))
+ return -ERESTARTSYS;
+ r = copy_from_user(test_buffer, buf, s);
+ test_len = s;
+ up(&test_mutex);
+ if (r)
+ return -EFAULT;
+ wake_up(&test_wait);
+ *o += s;
+ return s;
+}
+
+static const struct file_operations test_fops =
+{
+ .open = test_open,
+ .read = test_read,
+ .write = test_write,
+};
+
+static int test_wait_init(void)
+{
+ proc_create("test_wait", 0666, NULL, &test_fops);
+ return 0;
+}
+
+static void test_wait_exit(void)
+{
+ remove_proc_entry("test_wait", NULL);
+}
+
+module_init(test_wait_init);
+module_exit(test_wait_exit);