blob: 555cd93c2ee5124c102ab892b49bc0cd3da3d2c8 [file] [log] [blame]
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +10001/*
2 * Drivers for the Total Impact PPC based computer "BRIQ"
3 * by Dr. Karsten Jeppesen
4 *
5 */
6
7#include <linux/module.h>
8
Arnd Bergmann556889a2008-05-20 19:15:41 +02009#include <linux/smp_lock.h>
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100010#include <linux/types.h>
11#include <linux/errno.h>
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100012#include <linux/tty.h>
13#include <linux/timer.h>
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100014#include <linux/kernel.h>
15#include <linux/wait.h>
16#include <linux/string.h>
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100017#include <linux/ioport.h>
18#include <linux/delay.h>
19#include <linux/miscdevice.h>
20#include <linux/fs.h>
21#include <linux/mm.h>
22#include <linux/init.h>
23
24#include <asm/uaccess.h>
25#include <asm/io.h>
26#include <asm/prom.h>
27
28#define BRIQ_PANEL_MINOR 156
29#define BRIQ_PANEL_VFD_IOPORT 0x0390
30#define BRIQ_PANEL_LED_IOPORT 0x0398
31#define BRIQ_PANEL_VER "1.1 (04/20/2002)"
32#define BRIQ_PANEL_MSG0 "Loading Linux"
33
34static int vfd_is_open;
35static unsigned char vfd[40];
36static int vfd_cursor;
37static unsigned char ledpb, led;
38
39static void update_vfd(void)
40{
41 int i;
42
43 /* cursor home */
44 outb(0x02, BRIQ_PANEL_VFD_IOPORT);
45 for (i=0; i<20; i++)
46 outb(vfd[i], BRIQ_PANEL_VFD_IOPORT + 1);
47
48 /* cursor to next line */
49 outb(0xc0, BRIQ_PANEL_VFD_IOPORT);
50 for (i=20; i<40; i++)
51 outb(vfd[i], BRIQ_PANEL_VFD_IOPORT + 1);
52
53}
54
55static void set_led(char state)
56{
57 if (state == 'R')
58 led = 0x01;
59 else if (state == 'G')
60 led = 0x02;
61 else if (state == 'Y')
62 led = 0x03;
63 else if (state == 'X')
64 led = 0x00;
65 outb(led, BRIQ_PANEL_LED_IOPORT);
66}
67
68static int briq_panel_open(struct inode *ino, struct file *filep)
69{
Arnd Bergmann556889a2008-05-20 19:15:41 +020070 lock_kernel();
71 /* enforce single access, vfd_is_open is protected by BKL */
72 if (vfd_is_open) {
73 unlock_kernel();
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100074 return -EBUSY;
Arnd Bergmann556889a2008-05-20 19:15:41 +020075 }
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100076 vfd_is_open = 1;
77
Arnd Bergmann556889a2008-05-20 19:15:41 +020078 unlock_kernel();
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100079 return 0;
80}
81
82static int briq_panel_release(struct inode *ino, struct file *filep)
83{
84 if (!vfd_is_open)
85 return -ENODEV;
86
87 vfd_is_open = 0;
88
89 return 0;
90}
91
Al Viro4ac493b2006-09-23 18:20:56 +010092static ssize_t briq_panel_read(struct file *file, char __user *buf, size_t count,
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100093 loff_t *ppos)
94{
95 unsigned short c;
96 unsigned char cp;
97
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +100098 if (!vfd_is_open)
99 return -ENODEV;
100
101 c = (inb(BRIQ_PANEL_LED_IOPORT) & 0x000c) | (ledpb & 0x0003);
102 set_led(' ');
103 /* upper button released */
104 if ((!(ledpb & 0x0004)) && (c & 0x0004)) {
105 cp = ' ';
106 ledpb = c;
107 if (copy_to_user(buf, &cp, 1))
108 return -EFAULT;
109 return 1;
110 }
111 /* lower button released */
112 else if ((!(ledpb & 0x0008)) && (c & 0x0008)) {
113 cp = '\r';
114 ledpb = c;
115 if (copy_to_user(buf, &cp, 1))
116 return -EFAULT;
117 return 1;
118 } else {
119 ledpb = c;
120 return 0;
121 }
122}
123
124static void scroll_vfd( void )
125{
126 int i;
127
128 for (i=0; i<20; i++) {
129 vfd[i] = vfd[i+20];
130 vfd[i+20] = ' ';
131 }
132 vfd_cursor = 20;
133}
134
Al Viro4ac493b2006-09-23 18:20:56 +0100135static ssize_t briq_panel_write(struct file *file, const char __user *buf, size_t len,
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000136 loff_t *ppos)
137{
138 size_t indx = len;
139 int i, esc = 0;
140
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000141 if (!vfd_is_open)
142 return -EBUSY;
143
144 for (;;) {
Al Viro4ac493b2006-09-23 18:20:56 +0100145 char c;
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000146 if (!indx)
147 break;
Al Viro4ac493b2006-09-23 18:20:56 +0100148 if (get_user(c, buf))
149 return -EFAULT;
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000150 if (esc) {
Al Viro4ac493b2006-09-23 18:20:56 +0100151 set_led(c);
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000152 esc = 0;
Al Viro4ac493b2006-09-23 18:20:56 +0100153 } else if (c == 27) {
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000154 esc = 1;
Al Viro4ac493b2006-09-23 18:20:56 +0100155 } else if (c == 12) {
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000156 /* do a form feed */
157 for (i=0; i<40; i++)
158 vfd[i] = ' ';
159 vfd_cursor = 0;
Al Viro4ac493b2006-09-23 18:20:56 +0100160 } else if (c == 10) {
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000161 if (vfd_cursor < 20)
162 vfd_cursor = 20;
163 else if (vfd_cursor < 40)
164 vfd_cursor = 40;
165 else if (vfd_cursor < 60)
166 vfd_cursor = 60;
167 if (vfd_cursor > 59)
168 scroll_vfd();
169 } else {
170 /* just a character */
171 if (vfd_cursor > 39)
172 scroll_vfd();
Al Viro4ac493b2006-09-23 18:20:56 +0100173 vfd[vfd_cursor++] = c;
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000174 }
175 indx--;
176 buf++;
177 }
178 update_vfd();
179
180 return len;
181}
182
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800183static const struct file_operations briq_panel_fops = {
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000184 .owner = THIS_MODULE,
185 .read = briq_panel_read,
186 .write = briq_panel_write,
187 .open = briq_panel_open,
188 .release = briq_panel_release,
189};
190
191static struct miscdevice briq_panel_miscdev = {
192 BRIQ_PANEL_MINOR,
193 "briq_panel",
194 &briq_panel_fops
195};
196
197static int __init briq_panel_init(void)
198{
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000199 struct device_node *root = of_find_node_by_path("/");
Al Viro13b5aec2006-09-23 16:44:58 +0100200 const char *machine;
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000201 int i;
202
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000203 machine = of_get_property(root, "model", NULL);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000204 if (!machine || strncmp(machine, "TotalImpact,BRIQ-1", 18) != 0) {
205 of_node_put(root);
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000206 return -ENODEV;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000207 }
208 of_node_put(root);
Benjamin Herrenschmidta45b8392006-07-04 15:06:20 +1000209
210 printk(KERN_INFO
211 "briq_panel: v%s Dr. Karsten Jeppesen (kj@totalimpact.com)\n",
212 BRIQ_PANEL_VER);
213
214 if (!request_region(BRIQ_PANEL_VFD_IOPORT, 4, "BRIQ Front Panel"))
215 return -EBUSY;
216
217 if (!request_region(BRIQ_PANEL_LED_IOPORT, 2, "BRIQ Front Panel")) {
218 release_region(BRIQ_PANEL_VFD_IOPORT, 4);
219 return -EBUSY;
220 }
221 ledpb = inb(BRIQ_PANEL_LED_IOPORT) & 0x000c;
222
223 if (misc_register(&briq_panel_miscdev) < 0) {
224 release_region(BRIQ_PANEL_VFD_IOPORT, 4);
225 release_region(BRIQ_PANEL_LED_IOPORT, 2);
226 return -EBUSY;
227 }
228
229 outb(0x38, BRIQ_PANEL_VFD_IOPORT); /* Function set */
230 outb(0x01, BRIQ_PANEL_VFD_IOPORT); /* Clear display */
231 outb(0x0c, BRIQ_PANEL_VFD_IOPORT); /* Display on */
232 outb(0x06, BRIQ_PANEL_VFD_IOPORT); /* Entry normal */
233 for (i=0; i<40; i++)
234 vfd[i]=' ';
235#ifndef MODULE
236 vfd[0] = 'L';
237 vfd[1] = 'o';
238 vfd[2] = 'a';
239 vfd[3] = 'd';
240 vfd[4] = 'i';
241 vfd[5] = 'n';
242 vfd[6] = 'g';
243 vfd[7] = ' ';
244 vfd[8] = '.';
245 vfd[9] = '.';
246 vfd[10] = '.';
247#endif /* !MODULE */
248
249 update_vfd();
250
251 return 0;
252}
253
254static void __exit briq_panel_exit(void)
255{
256 misc_deregister(&briq_panel_miscdev);
257 release_region(BRIQ_PANEL_VFD_IOPORT, 4);
258 release_region(BRIQ_PANEL_LED_IOPORT, 2);
259}
260
261module_init(briq_panel_init);
262module_exit(briq_panel_exit);
263
264MODULE_LICENSE("GPL");
265MODULE_AUTHOR("Karsten Jeppesen <karsten@jeppesens.com>");
266MODULE_DESCRIPTION("Driver for the Total Impact briQ front panel");