PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/mips/lasat/picvue_proc.c

http://github.com/mirrors/linux
C | 208 lines | 161 code | 40 blank | 7 comment | 27 complexity | 263f831c90afe9ebbdc5324766c42f6f MD5 | raw file
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Picvue PVC160206 display driver
  4 *
  5 * Brian Murphy <brian.murphy@eicon.com>
  6 *
  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>
 13
 14#include <linux/proc_fs.h>
 15#include <linux/seq_file.h>
 16#include <linux/interrupt.h>
 17
 18#include <linux/timer.h>
 19#include <linux/mutex.h>
 20#include <linux/uaccess.h>
 21
 22#include "picvue.h"
 23
 24static DEFINE_MUTEX(pvc_mutex);
 25static char pvc_lines[PVC_NLINES][PVC_LINELEN+1];
 26static int pvc_linedata[PVC_NLINES];
 27static char *pvc_linename[PVC_NLINES] = {"line1", "line2"};
 28#define DISPLAY_DIR_NAME "display"
 29static int scroll_dir, scroll_interval;
 30
 31static struct timer_list timer;
 32
 33static void pvc_display(unsigned long data)
 34{
 35	int i;
 36
 37	pvc_clear();
 38	for (i = 0; i < PVC_NLINES; i++)
 39		pvc_write_string(pvc_lines[i], 0, i);
 40}
 41
 42static DECLARE_TASKLET(pvc_display_tasklet, &pvc_display, 0);
 43
 44static int pvc_line_proc_show(struct seq_file *m, void *v)
 45{
 46	int lineno = *(int *)m->private;
 47
 48	if (lineno < 0 || lineno >= PVC_NLINES) {
 49		printk(KERN_WARNING "proc_read_line: invalid lineno %d\n", lineno);
 50		return 0;
 51	}
 52
 53	mutex_lock(&pvc_mutex);
 54	seq_printf(m, "%s\n", pvc_lines[lineno]);
 55	mutex_unlock(&pvc_mutex);
 56
 57	return 0;
 58}
 59
 60static int pvc_line_proc_open(struct inode *inode, struct file *file)
 61{
 62	return single_open(file, pvc_line_proc_show, PDE_DATA(inode));
 63}
 64
 65static ssize_t pvc_line_proc_write(struct file *file, const char __user *buf,
 66				   size_t count, loff_t *pos)
 67{
 68	int lineno = *(int *)PDE_DATA(file_inode(file));
 69	char kbuf[PVC_LINELEN];
 70	size_t len;
 71
 72	BUG_ON(lineno < 0 || lineno >= PVC_NLINES);
 73
 74	len = min(count, sizeof(kbuf) - 1);
 75	if (copy_from_user(kbuf, buf, len))
 76		return -EFAULT;
 77	kbuf[len] = '\0';
 78
 79	if (len > 0 && kbuf[len - 1] == '\n')
 80		len--;
 81
 82	mutex_lock(&pvc_mutex);
 83	strncpy(pvc_lines[lineno], kbuf, len);
 84	pvc_lines[lineno][len] = '\0';
 85	mutex_unlock(&pvc_mutex);
 86
 87	tasklet_schedule(&pvc_display_tasklet);
 88
 89	return count;
 90}
 91
 92static 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,
 98};
 99
100static ssize_t pvc_scroll_proc_write(struct file *file, const char __user *buf,
101				     size_t count, loff_t *pos)
102{
103	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);
113
114	mutex_lock(&pvc_mutex);
115	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	}
131	mutex_unlock(&pvc_mutex);
132
133	return count;
134}
135
136static int pvc_scroll_proc_show(struct seq_file *m, void *v)
137{
138	mutex_lock(&pvc_mutex);
139	seq_printf(m, "%d\n", scroll_dir * scroll_interval);
140	mutex_unlock(&pvc_mutex);
141
142	return 0;
143}
144
145static 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 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,
156};
157
158void pvc_proc_timerfunc(struct timer_list *unused)
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{
171	remove_proc_subtree(DISPLAY_DIR_NAME, NULL);
172	del_timer_sync(&timer);
173}
174
175static int __init pvc_proc_init(void)
176{
177	struct proc_dir_entry *dir, *proc_entry;
178	int i;
179
180	dir = proc_mkdir(DISPLAY_DIR_NAME, NULL);
181	if (dir == NULL)
182		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++) {
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;
193	}
194	proc_entry = proc_create("scroll", 0644, dir, &pvc_scroll_proc_ops);
195	if (proc_entry == NULL)
196		goto error;
197
198	timer_setup(&timer, pvc_proc_timerfunc, 0);
199
200	return 0;
201error:
202	pvc_proc_cleanup();
203	return -ENOMEM;
204}
205
206module_init(pvc_proc_init);
207module_exit(pvc_proc_cleanup);
208MODULE_LICENSE("GPL");