blob: dd292dcec684a21b7209076cff2668cf1dc503c0 [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>
Al Viro29abfbd2016-09-05 11:35:50 -040019#include <linux/uaccess.h>
Brian Murphy1f21d2b2007-08-21 22:34:16 +020020
21#include "picvue.h"
22
Daniel Walker8a39c522008-01-10 20:53:21 -080023static DEFINE_MUTEX(pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020024static char pvc_lines[PVC_NLINES][PVC_LINELEN+1];
25static int pvc_linedata[PVC_NLINES];
Brian Murphy1f21d2b2007-08-21 22:34:16 +020026static char *pvc_linename[PVC_NLINES] = {"line1", "line2"};
27#define DISPLAY_DIR_NAME "display"
28static int scroll_dir, scroll_interval;
29
30static struct timer_list timer;
31
32static void pvc_display(unsigned long data)
33{
34 int i;
35
36 pvc_clear();
37 for (i = 0; i < PVC_NLINES; i++)
38 pvc_write_string(pvc_lines[i], 0, i);
39}
40
41static DECLARE_TASKLET(pvc_display_tasklet, &pvc_display, 0);
42
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030043static int pvc_line_proc_show(struct seq_file *m, void *v)
Brian Murphy1f21d2b2007-08-21 22:34:16 +020044{
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030045 int lineno = *(int *)m->private;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020046
Dan Carpenter9b987c42013-11-08 12:44:31 +030047 if (lineno < 0 || lineno >= PVC_NLINES) {
Brian Murphy1f21d2b2007-08-21 22:34:16 +020048 printk(KERN_WARNING "proc_read_line: invalid lineno %d\n", lineno);
49 return 0;
50 }
51
Daniel Walker8a39c522008-01-10 20:53:21 -080052 mutex_lock(&pvc_mutex);
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030053 seq_printf(m, "%s\n", pvc_lines[lineno]);
Daniel Walker8a39c522008-01-10 20:53:21 -080054 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020055
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030056 return 0;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020057}
58
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030059static int pvc_line_proc_open(struct inode *inode, struct file *file)
Brian Murphy1f21d2b2007-08-21 22:34:16 +020060{
Al Virod9dda782013-03-31 18:16:14 -040061 return single_open(file, pvc_line_proc_show, PDE_DATA(inode));
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030062}
Brian Murphy1f21d2b2007-08-21 22:34:16 +020063
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030064static ssize_t pvc_line_proc_write(struct file *file, const char __user *buf,
65 size_t count, loff_t *pos)
66{
Al Virod9dda782013-03-31 18:16:14 -040067 int lineno = *(int *)PDE_DATA(file_inode(file));
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030068 char kbuf[PVC_LINELEN];
69 size_t len;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020070
Dan Carpenter9b987c42013-11-08 12:44:31 +030071 BUG_ON(lineno < 0 || lineno >= PVC_NLINES);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020072
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030073 len = min(count, sizeof(kbuf) - 1);
74 if (copy_from_user(kbuf, buf, len))
75 return -EFAULT;
76 kbuf[len] = '\0';
77
78 if (len > 0 && kbuf[len - 1] == '\n')
79 len--;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020080
Daniel Walker8a39c522008-01-10 20:53:21 -080081 mutex_lock(&pvc_mutex);
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030082 strncpy(pvc_lines[lineno], kbuf, len);
83 pvc_lines[lineno][len] = '\0';
Daniel Walker8a39c522008-01-10 20:53:21 -080084 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +020085
86 tasklet_schedule(&pvc_display_tasklet);
87
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030088 return count;
Brian Murphy1f21d2b2007-08-21 22:34:16 +020089}
90
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +030091static const struct file_operations pvc_line_proc_fops = {
92 .owner = THIS_MODULE,
93 .open = pvc_line_proc_open,
94 .read = seq_read,
95 .llseek = seq_lseek,
96 .release = single_release,
97 .write = pvc_line_proc_write,
98};
99
100static ssize_t pvc_scroll_proc_write(struct file *file, const char __user *buf,
101 size_t count, loff_t *pos)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200102{
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300103 char kbuf[42];
104 size_t len;
105 int cmd;
106
107 len = min(count, sizeof(kbuf) - 1);
108 if (copy_from_user(kbuf, buf, len))
109 return -EFAULT;
110 kbuf[len] = '\0';
111
112 cmd = simple_strtol(kbuf, NULL, 10);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200113
Daniel Walker8a39c522008-01-10 20:53:21 -0800114 mutex_lock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200115 if (scroll_interval != 0)
116 del_timer(&timer);
117
118 if (cmd == 0) {
119 scroll_dir = 0;
120 scroll_interval = 0;
121 } else {
122 if (cmd < 0) {
123 scroll_dir = -1;
124 scroll_interval = -cmd;
125 } else {
126 scroll_dir = 1;
127 scroll_interval = cmd;
128 }
129 add_timer(&timer);
130 }
Daniel Walker8a39c522008-01-10 20:53:21 -0800131 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200132
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300133 return count;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200134}
135
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300136static int pvc_scroll_proc_show(struct seq_file *m, void *v)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200137{
Daniel Walker8a39c522008-01-10 20:53:21 -0800138 mutex_lock(&pvc_mutex);
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300139 seq_printf(m, "%d\n", scroll_dir * scroll_interval);
Daniel Walker8a39c522008-01-10 20:53:21 -0800140 mutex_unlock(&pvc_mutex);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200141
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300142 return 0;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200143}
144
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300145static int pvc_scroll_proc_open(struct inode *inode, struct file *file)
146{
147 return single_open(file, pvc_scroll_proc_show, NULL);
148}
149
150static const struct file_operations pvc_scroll_proc_fops = {
151 .owner = THIS_MODULE,
152 .open = pvc_scroll_proc_open,
153 .read = seq_read,
154 .llseek = seq_lseek,
155 .release = single_release,
156 .write = pvc_scroll_proc_write,
157};
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200158
159void pvc_proc_timerfunc(unsigned long data)
160{
161 if (scroll_dir < 0)
162 pvc_move(DISPLAY|RIGHT);
163 else if (scroll_dir > 0)
164 pvc_move(DISPLAY|LEFT);
165
166 timer.expires = jiffies + scroll_interval;
167 add_timer(&timer);
168}
169
170static void pvc_proc_cleanup(void)
171{
Al Viroc62432b2015-12-06 12:18:55 -0500172 remove_proc_subtree(DISPLAY_DIR_NAME, NULL);
Julia Lawall49992cb2014-03-26 22:33:43 +0100173 del_timer_sync(&timer);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200174}
175
176static int __init pvc_proc_init(void)
177{
Al Viroc62432b2015-12-06 12:18:55 -0500178 struct proc_dir_entry *dir, *proc_entry;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200179 int i;
180
Al Viroc62432b2015-12-06 12:18:55 -0500181 dir = proc_mkdir(DISPLAY_DIR_NAME, NULL);
182 if (dir == NULL)
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200183 goto error;
184
185 for (i = 0; i < PVC_NLINES; i++) {
186 strcpy(pvc_lines[i], "");
187 pvc_linedata[i] = i;
188 }
189 for (i = 0; i < PVC_NLINES; i++) {
Al Viroc62432b2015-12-06 12:18:55 -0500190 proc_entry = proc_create_data(pvc_linename[i], 0644, dir,
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300191 &pvc_line_proc_fops, &pvc_linedata[i]);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200192 if (proc_entry == NULL)
193 goto error;
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200194 }
Al Viroc62432b2015-12-06 12:18:55 -0500195 proc_entry = proc_create("scroll", 0644, dir,
Alexey Dobriyanc0b4abd2009-11-27 09:55:03 +0300196 &pvc_scroll_proc_fops);
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200197 if (proc_entry == NULL)
198 goto error;
199
Brian Murphy1f21d2b2007-08-21 22:34:16 +0200200 init_timer(&timer);
201 timer.function = pvc_proc_timerfunc;
202
203 return 0;
204error:
205 pvc_proc_cleanup();
206 return -ENOMEM;
207}
208
209module_init(pvc_proc_init);
210module_exit(pvc_proc_cleanup);
211MODULE_LICENSE("GPL");