blob: b420958806678a3b24b7b5b4c8d7dd4d393a7214 [file] [log] [blame]
Brian Murphy1f21d2b2007-08-21 22:34:16 +02001/*
2 * Picvue PVC160206 display driver
3 *
4 * Brian Murphy <brian.murphy@eicon.com>
5 *
6 */
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +03007#include <linux/bug.h>
Brian Murphy1f21d2b2007-08-21 22:34:16 +02008#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/errno.h>
12
13#include <linux/proc_fs.h>
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030014#include <linux/seq_file.h>
Brian Murphy1f21d2b2007-08-21 22:34:16 +020015#include <linux/interrupt.h>
16
17#include <linux/timer.h>
Daniel Walker8a39c522008-01-10 20:53:21 -080018#include <linux/mutex.h>
Brian Murphy1f21d2b2007-08-21 22:34:16 +020019
20#include "picvue.h"
21
Daniel Walker8a39c522008-01-10 20:53:21 -080022static DEFINE_MUTEX(pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020023static char pvc_lines[PVC_NLINES][PVC_LINELEN+1];
24static int pvc_linedata[PVC_NLINES];
Brian Murphy1f21d2b2007-08-21 22:34:16 +020025static char *pvc_linename[PVC_NLINES] = {"line1", "line2"};
26#define DISPLAY_DIR_NAME "display"
27static int scroll_dir, scroll_interval;
28
29static struct timer_list timer;
30
31static void pvc_display(unsigned long data)
32{
33 int i;
34
35 pvc_clear();
36 for (i = 0; i < PVC_NLINES; i++)
37 pvc_write_string(pvc_lines[i], 0, i);
38}
39
40static DECLARE_TASKLET(pvc_display_tasklet, &pvc_display, 0);
41
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030042static int pvc_line_proc_show(struct seq_file *m, void *v)
Brian Murphy1f21d2b2007-08-21 22:34:16 +020043{
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030044 int lineno = *(int *)m->private;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020045
46 if (lineno < 0 || lineno > PVC_NLINES) {
47 printk(KERN_WARNING "proc_read_line: invalid lineno %d\n", lineno);
48 return 0;
49 }
50
Daniel Walker8a39c522008-01-10 20:53:21 -080051 mutex_lock(&pvc_mutex);
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030052 seq_printf(m, "%s\n", pvc_lines[lineno]);
Daniel Walker8a39c522008-01-10 20:53:21 -080053 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020054
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030055 return 0;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020056}
57
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030058static int pvc_line_proc_open(struct inode *inode, struct file *file)
Brian Murphy1f21d2b2007-08-21 22:34:16 +020059{
Al Virod9dda782013-03-31 18:16:14 -040060 return single_open(file, pvc_line_proc_show, PDE_DATA(inode));
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030061}
Brian Murphy1f21d2b2007-08-21 22:34:16 +020062
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030063static ssize_t pvc_line_proc_write(struct file *file, const char __user *buf,
64 size_t count, loff_t *pos)
65{
Al Virod9dda782013-03-31 18:16:14 -040066 int lineno = *(int *)PDE_DATA(file_inode(file));
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030067 char kbuf[PVC_LINELEN];
68 size_t len;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020069
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030070 BUG_ON(lineno < 0 || lineno > PVC_NLINES);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020071
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030072 len = min(count, sizeof(kbuf) - 1);
73 if (copy_from_user(kbuf, buf, len))
74 return -EFAULT;
75 kbuf[len] = '\0';
76
77 if (len > 0 && kbuf[len - 1] == '\n')
78 len--;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020079
Daniel Walker8a39c522008-01-10 20:53:21 -080080 mutex_lock(&pvc_mutex);
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030081 strncpy(pvc_lines[lineno], kbuf, len);
82 pvc_lines[lineno][len] = '\0';
Daniel Walker8a39c522008-01-10 20:53:21 -080083 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020084
85 tasklet_schedule(&pvc_display_tasklet);
86
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030087 return count;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020088}
89
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030090static const struct file_operations pvc_line_proc_fops = {
91 .owner = THIS_MODULE,
92 .open = pvc_line_proc_open,
93 .read = seq_read,
94 .llseek = seq_lseek,
95 .release = single_release,
96 .write = pvc_line_proc_write,
97};
98
99static ssize_t pvc_scroll_proc_write(struct file *file, const char __user *buf,
100 size_t count, loff_t *pos)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200101{
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300102 char kbuf[42];
103 size_t len;
104 int cmd;
105
106 len = min(count, sizeof(kbuf) - 1);
107 if (copy_from_user(kbuf, buf, len))
108 return -EFAULT;
109 kbuf[len] = '\0';
110
111 cmd = simple_strtol(kbuf, NULL, 10);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200112
Daniel Walker8a39c522008-01-10 20:53:21 -0800113 mutex_lock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200114 if (scroll_interval != 0)
115 del_timer(&timer);
116
117 if (cmd == 0) {
118 scroll_dir = 0;
119 scroll_interval = 0;
120 } else {
121 if (cmd < 0) {
122 scroll_dir = -1;
123 scroll_interval = -cmd;
124 } else {
125 scroll_dir = 1;
126 scroll_interval = cmd;
127 }
128 add_timer(&timer);
129 }
Daniel Walker8a39c522008-01-10 20:53:21 -0800130 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200131
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300132 return count;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200133}
134
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300135static int pvc_scroll_proc_show(struct seq_file *m, void *v)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200136{
Daniel Walker8a39c522008-01-10 20:53:21 -0800137 mutex_lock(&pvc_mutex);
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300138 seq_printf(m, "%d\n", scroll_dir * scroll_interval);
Daniel Walker8a39c522008-01-10 20:53:21 -0800139 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200140
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300141 return 0;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200142}
143
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300144static int pvc_scroll_proc_open(struct inode *inode, struct file *file)
145{
146 return single_open(file, pvc_scroll_proc_show, NULL);
147}
148
149static const struct file_operations pvc_scroll_proc_fops = {
150 .owner = THIS_MODULE,
151 .open = pvc_scroll_proc_open,
152 .read = seq_read,
153 .llseek = seq_lseek,
154 .release = single_release,
155 .write = pvc_scroll_proc_write,
156};
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200157
158void pvc_proc_timerfunc(unsigned long data)
159{
160 if (scroll_dir < 0)
161 pvc_move(DISPLAY|RIGHT);
162 else if (scroll_dir > 0)
163 pvc_move(DISPLAY|LEFT);
164
165 timer.expires = jiffies + scroll_interval;
166 add_timer(&timer);
167}
168
169static void pvc_proc_cleanup(void)
170{
Al Viroc62432b2015-12-06 12:18:55 -0500171 remove_proc_subtree(DISPLAY_DIR_NAME, NULL);
Julia Lawall49992cb2014-03-26 22:33:43 +0100172 del_timer_sync(&timer);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200173}
174
175static int __init pvc_proc_init(void)
176{
Al Viroc62432b2015-12-06 12:18:55 -0500177 struct proc_dir_entry *dir, *proc_entry;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200178 int i;
179
Al Viroc62432b2015-12-06 12:18:55 -0500180 dir = proc_mkdir(DISPLAY_DIR_NAME, NULL);
181 if (dir == NULL)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200182 goto error;
183
184 for (i = 0; i < PVC_NLINES; i++) {
185 strcpy(pvc_lines[i], "");
186 pvc_linedata[i] = i;
187 }
188 for (i = 0; i < PVC_NLINES; i++) {
Al Viroc62432b2015-12-06 12:18:55 -0500189 proc_entry = proc_create_data(pvc_linename[i], 0644, dir,
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300190 &pvc_line_proc_fops, &pvc_linedata[i]);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200191 if (proc_entry == NULL)
192 goto error;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200193 }
Al Viroc62432b2015-12-06 12:18:55 -0500194 proc_entry = proc_create("scroll", 0644, dir,
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300195 &pvc_scroll_proc_fops);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200196 if (proc_entry == NULL)
197 goto error;
198
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200199 init_timer(&timer);
200 timer.function = pvc_proc_timerfunc;
201
202 return 0;
203error:
204 pvc_proc_cleanup();
205 return -ENOMEM;
206}
207
208module_init(pvc_proc_init);
209module_exit(pvc_proc_cleanup);
210MODULE_LICENSE("GPL");