David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
| 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; |
| 30 | |
| 31 | static struct timer_list timer; |
| 32 | |
| 33 | static 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 | |
| 42 | static DECLARE_TASKLET(pvc_display_tasklet, &pvc_display, 0); |
| 43 | |
| 44 | static 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 | |
| 60 | static 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 | |
| 65 | static 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 | |
| 92 | static const struct file_operations pvc_line_proc_fops = { |
| 93 | .owner = THIS_MODULE, |
| 94 | .open = pvc_line_proc_open, |
| 95 | .read = seq_read, |
| 96 | .llseek = seq_lseek, |
| 97 | .release = single_release, |
| 98 | .write = pvc_line_proc_write, |
| 99 | }; |
| 100 | |
| 101 | static ssize_t pvc_scroll_proc_write(struct file *file, const char __user *buf, |
| 102 | size_t count, loff_t *pos) |
| 103 | { |
| 104 | char kbuf[42]; |
| 105 | size_t len; |
| 106 | int cmd; |
| 107 | |
| 108 | len = min(count, sizeof(kbuf) - 1); |
| 109 | if (copy_from_user(kbuf, buf, len)) |
| 110 | return -EFAULT; |
| 111 | kbuf[len] = '\0'; |
| 112 | |
| 113 | cmd = simple_strtol(kbuf, NULL, 10); |
| 114 | |
| 115 | mutex_lock(&pvc_mutex); |
| 116 | if (scroll_interval != 0) |
| 117 | del_timer(&timer); |
| 118 | |
| 119 | if (cmd == 0) { |
| 120 | scroll_dir = 0; |
| 121 | scroll_interval = 0; |
| 122 | } else { |
| 123 | if (cmd < 0) { |
| 124 | scroll_dir = -1; |
| 125 | scroll_interval = -cmd; |
| 126 | } else { |
| 127 | scroll_dir = 1; |
| 128 | scroll_interval = cmd; |
| 129 | } |
| 130 | add_timer(&timer); |
| 131 | } |
| 132 | mutex_unlock(&pvc_mutex); |
| 133 | |
| 134 | return count; |
| 135 | } |
| 136 | |
| 137 | static int pvc_scroll_proc_show(struct seq_file *m, void *v) |
| 138 | { |
| 139 | mutex_lock(&pvc_mutex); |
| 140 | seq_printf(m, "%d\n", scroll_dir * scroll_interval); |
| 141 | mutex_unlock(&pvc_mutex); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int pvc_scroll_proc_open(struct inode *inode, struct file *file) |
| 147 | { |
| 148 | return single_open(file, pvc_scroll_proc_show, NULL); |
| 149 | } |
| 150 | |
| 151 | static const struct file_operations pvc_scroll_proc_fops = { |
| 152 | .owner = THIS_MODULE, |
| 153 | .open = pvc_scroll_proc_open, |
| 154 | .read = seq_read, |
| 155 | .llseek = seq_lseek, |
| 156 | .release = single_release, |
| 157 | .write = pvc_scroll_proc_write, |
| 158 | }; |
| 159 | |
| 160 | void pvc_proc_timerfunc(struct timer_list *unused) |
| 161 | { |
| 162 | if (scroll_dir < 0) |
| 163 | pvc_move(DISPLAY|RIGHT); |
| 164 | else if (scroll_dir > 0) |
| 165 | pvc_move(DISPLAY|LEFT); |
| 166 | |
| 167 | timer.expires = jiffies + scroll_interval; |
| 168 | add_timer(&timer); |
| 169 | } |
| 170 | |
| 171 | static void pvc_proc_cleanup(void) |
| 172 | { |
| 173 | remove_proc_subtree(DISPLAY_DIR_NAME, NULL); |
| 174 | del_timer_sync(&timer); |
| 175 | } |
| 176 | |
| 177 | static int __init pvc_proc_init(void) |
| 178 | { |
| 179 | struct proc_dir_entry *dir, *proc_entry; |
| 180 | int i; |
| 181 | |
| 182 | dir = proc_mkdir(DISPLAY_DIR_NAME, NULL); |
| 183 | if (dir == NULL) |
| 184 | goto error; |
| 185 | |
| 186 | for (i = 0; i < PVC_NLINES; i++) { |
| 187 | strcpy(pvc_lines[i], ""); |
| 188 | pvc_linedata[i] = i; |
| 189 | } |
| 190 | for (i = 0; i < PVC_NLINES; i++) { |
| 191 | proc_entry = proc_create_data(pvc_linename[i], 0644, dir, |
| 192 | &pvc_line_proc_fops, &pvc_linedata[i]); |
| 193 | if (proc_entry == NULL) |
| 194 | goto error; |
| 195 | } |
| 196 | proc_entry = proc_create("scroll", 0644, dir, |
| 197 | &pvc_scroll_proc_fops); |
| 198 | if (proc_entry == NULL) |
| 199 | goto error; |
| 200 | |
| 201 | timer_setup(&timer, pvc_proc_timerfunc, 0); |
| 202 | |
| 203 | return 0; |
| 204 | error: |
| 205 | pvc_proc_cleanup(); |
| 206 | return -ENOMEM; |
| 207 | } |
| 208 | |
| 209 | module_init(pvc_proc_init); |
| 210 | module_exit(pvc_proc_cleanup); |
| 211 | MODULE_LICENSE("GPL"); |