treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / mips / lasat / picvue_proc.c
blob61c033494af5b37ec9515e4220f19cf8183740d5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Picvue PVC160206 display driver
5 * Brian Murphy <brian.murphy@eicon.com>
7 */
8 #include <linux/bug.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/errno.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/interrupt.h>
18 #include <linux/timer.h>
19 #include <linux/mutex.h>
20 #include <linux/uaccess.h>
22 #include "picvue.h"
24 static DEFINE_MUTEX(pvc_mutex);
25 static char pvc_lines[PVC_NLINES][PVC_LINELEN+1];
26 static int pvc_linedata[PVC_NLINES];
27 static char *pvc_linename[PVC_NLINES] = {"line1", "line2"};
28 #define DISPLAY_DIR_NAME "display"
29 static int scroll_dir, scroll_interval;
31 static struct timer_list timer;
33 static void pvc_display(unsigned long data)
35 int i;
37 pvc_clear();
38 for (i = 0; i < PVC_NLINES; i++)
39 pvc_write_string(pvc_lines[i], 0, i);
42 static DECLARE_TASKLET(pvc_display_tasklet, &pvc_display, 0);
44 static int pvc_line_proc_show(struct seq_file *m, void *v)
46 int lineno = *(int *)m->private;
48 if (lineno < 0 || lineno >= PVC_NLINES) {
49 printk(KERN_WARNING "proc_read_line: invalid lineno %d\n", lineno);
50 return 0;
53 mutex_lock(&pvc_mutex);
54 seq_printf(m, "%s\n", pvc_lines[lineno]);
55 mutex_unlock(&pvc_mutex);
57 return 0;
60 static int pvc_line_proc_open(struct inode *inode, struct file *file)
62 return single_open(file, pvc_line_proc_show, PDE_DATA(inode));
65 static ssize_t pvc_line_proc_write(struct file *file, const char __user *buf,
66 size_t count, loff_t *pos)
68 int lineno = *(int *)PDE_DATA(file_inode(file));
69 char kbuf[PVC_LINELEN];
70 size_t len;
72 BUG_ON(lineno < 0 || lineno >= PVC_NLINES);
74 len = min(count, sizeof(kbuf) - 1);
75 if (copy_from_user(kbuf, buf, len))
76 return -EFAULT;
77 kbuf[len] = '\0';
79 if (len > 0 && kbuf[len - 1] == '\n')
80 len--;
82 mutex_lock(&pvc_mutex);
83 strncpy(pvc_lines[lineno], kbuf, len);
84 pvc_lines[lineno][len] = '\0';
85 mutex_unlock(&pvc_mutex);
87 tasklet_schedule(&pvc_display_tasklet);
89 return count;
92 static const struct proc_ops pvc_line_proc_ops = {
93 .proc_open = pvc_line_proc_open,
94 .proc_read = seq_read,
95 .proc_lseek = seq_lseek,
96 .proc_release = single_release,
97 .proc_write = pvc_line_proc_write,
100 static ssize_t pvc_scroll_proc_write(struct file *file, const char __user *buf,
101 size_t count, loff_t *pos)
103 char kbuf[42];
104 size_t len;
105 int cmd;
107 len = min(count, sizeof(kbuf) - 1);
108 if (copy_from_user(kbuf, buf, len))
109 return -EFAULT;
110 kbuf[len] = '\0';
112 cmd = simple_strtol(kbuf, NULL, 10);
114 mutex_lock(&pvc_mutex);
115 if (scroll_interval != 0)
116 del_timer(&timer);
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;
129 add_timer(&timer);
131 mutex_unlock(&pvc_mutex);
133 return count;
136 static int pvc_scroll_proc_show(struct seq_file *m, void *v)
138 mutex_lock(&pvc_mutex);
139 seq_printf(m, "%d\n", scroll_dir * scroll_interval);
140 mutex_unlock(&pvc_mutex);
142 return 0;
145 static int pvc_scroll_proc_open(struct inode *inode, struct file *file)
147 return single_open(file, pvc_scroll_proc_show, NULL);
150 static const struct proc_ops pvc_scroll_proc_ops = {
151 .proc_open = pvc_scroll_proc_open,
152 .proc_read = seq_read,
153 .proc_lseek = seq_lseek,
154 .proc_release = single_release,
155 .proc_write = pvc_scroll_proc_write,
158 void pvc_proc_timerfunc(struct timer_list *unused)
160 if (scroll_dir < 0)
161 pvc_move(DISPLAY|RIGHT);
162 else if (scroll_dir > 0)
163 pvc_move(DISPLAY|LEFT);
165 timer.expires = jiffies + scroll_interval;
166 add_timer(&timer);
169 static void pvc_proc_cleanup(void)
171 remove_proc_subtree(DISPLAY_DIR_NAME, NULL);
172 del_timer_sync(&timer);
175 static int __init pvc_proc_init(void)
177 struct proc_dir_entry *dir, *proc_entry;
178 int i;
180 dir = proc_mkdir(DISPLAY_DIR_NAME, NULL);
181 if (dir == NULL)
182 goto error;
184 for (i = 0; i < PVC_NLINES; i++) {
185 strcpy(pvc_lines[i], "");
186 pvc_linedata[i] = i;
188 for (i = 0; i < PVC_NLINES; i++) {
189 proc_entry = proc_create_data(pvc_linename[i], 0644, dir,
190 &pvc_line_proc_ops, &pvc_linedata[i]);
191 if (proc_entry == NULL)
192 goto error;
194 proc_entry = proc_create("scroll", 0644, dir, &pvc_scroll_proc_ops);
195 if (proc_entry == NULL)
196 goto error;
198 timer_setup(&timer, pvc_proc_timerfunc, 0);
200 return 0;
201 error:
202 pvc_proc_cleanup();
203 return -ENOMEM;
206 module_init(pvc_proc_init);
207 module_exit(pvc_proc_cleanup);
208 MODULE_LICENSE("GPL");