b8c22255f6ada92615b2e70d8ad717927e04c9ae
[pandora-kernel.git] / drivers / char / briq_panel.c
1 /*
2  * Drivers for the Total Impact PPC based computer "BRIQ"
3  * by Dr. Karsten Jeppesen
4  *
5  */
6
7 #include <linux/module.h>
8
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/sched.h>
12 #include <linux/tty.h>
13 #include <linux/timer.h>
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/wait.h>
17 #include <linux/string.h>
18 #include <linux/slab.h>
19 #include <linux/ioport.h>
20 #include <linux/delay.h>
21 #include <linux/miscdevice.h>
22 #include <linux/fs.h>
23 #include <linux/mm.h>
24 #include <linux/init.h>
25
26 #include <asm/uaccess.h>
27 #include <asm/io.h>
28 #include <asm/prom.h>
29
30 #define         BRIQ_PANEL_MINOR        156
31 #define         BRIQ_PANEL_VFD_IOPORT   0x0390
32 #define         BRIQ_PANEL_LED_IOPORT   0x0398
33 #define         BRIQ_PANEL_VER          "1.1 (04/20/2002)"
34 #define         BRIQ_PANEL_MSG0         "Loading Linux"
35
36 static int              vfd_is_open;
37 static unsigned char    vfd[40];
38 static int              vfd_cursor;
39 static unsigned char    ledpb, led;
40
41 static void update_vfd(void)
42 {
43         int     i;
44
45         /* cursor home */
46         outb(0x02, BRIQ_PANEL_VFD_IOPORT);
47         for (i=0; i<20; i++)
48                 outb(vfd[i], BRIQ_PANEL_VFD_IOPORT + 1);
49
50         /* cursor to next line */
51         outb(0xc0, BRIQ_PANEL_VFD_IOPORT);
52         for (i=20; i<40; i++)
53                 outb(vfd[i], BRIQ_PANEL_VFD_IOPORT + 1);
54
55 }
56
57 static void set_led(char state)
58 {
59         if (state == 'R')
60                 led = 0x01;
61         else if (state == 'G')
62                 led = 0x02;
63         else if (state == 'Y')
64                 led = 0x03;
65         else if (state == 'X')
66                 led = 0x00;
67         outb(led, BRIQ_PANEL_LED_IOPORT);
68 }
69
70 static int briq_panel_open(struct inode *ino, struct file *filep)
71 {
72         /* enforce single access */
73         if (vfd_is_open)
74                 return -EBUSY;
75         vfd_is_open = 1;
76
77         return 0;
78 }
79
80 static int briq_panel_release(struct inode *ino, struct file *filep)
81 {
82         if (!vfd_is_open)
83                 return -ENODEV;
84
85         vfd_is_open = 0;
86
87         return 0;
88 }
89
90 static ssize_t briq_panel_read(struct file *file, char __user *buf, size_t count,
91                          loff_t *ppos)
92 {
93         unsigned short c;
94         unsigned char cp;
95
96 #if 0   /*  Can't seek (pread) on this device  */
97         if (ppos != &file->f_pos)
98                 return -ESPIPE;
99 #endif
100
101         if (!vfd_is_open)
102                 return -ENODEV;
103
104         c = (inb(BRIQ_PANEL_LED_IOPORT) & 0x000c) | (ledpb & 0x0003);
105         set_led(' ');
106         /* upper button released */
107         if ((!(ledpb & 0x0004)) && (c & 0x0004)) {
108                 cp = ' ';
109                 ledpb = c;
110                 if (copy_to_user(buf, &cp, 1))
111                         return -EFAULT;
112                 return 1;
113         }
114         /* lower button released */
115         else if ((!(ledpb & 0x0008)) && (c & 0x0008)) {
116                 cp = '\r';
117                 ledpb = c;
118                 if (copy_to_user(buf, &cp, 1))
119                         return -EFAULT;
120                 return 1;
121         } else {
122                 ledpb = c;
123                 return 0;
124         }
125 }
126
127 static void scroll_vfd( void )
128 {
129         int     i;
130
131         for (i=0; i<20; i++) {
132                 vfd[i] = vfd[i+20];
133                 vfd[i+20] = ' ';
134         }
135         vfd_cursor = 20;
136 }
137
138 static ssize_t briq_panel_write(struct file *file, const char __user *buf, size_t len,
139                           loff_t *ppos)
140 {
141         size_t indx = len;
142         int i, esc = 0;
143
144 #if 0   /*  Can't seek (pwrite) on this device  */
145         if (ppos != &file->f_pos)
146                 return -ESPIPE;
147 #endif
148
149         if (!vfd_is_open)
150                 return -EBUSY;
151
152         for (;;) {
153                 char c;
154                 if (!indx)
155                         break;
156                 if (get_user(c, buf))
157                         return -EFAULT;
158                 if (esc) {
159                         set_led(c);
160                         esc = 0;
161                 } else if (c == 27) {
162                         esc = 1;
163                 } else if (c == 12) {
164                         /* do a form feed */
165                         for (i=0; i<40; i++)
166                                 vfd[i] = ' ';
167                         vfd_cursor = 0;
168                 } else if (c == 10) {
169                         if (vfd_cursor < 20)
170                                 vfd_cursor = 20;
171                         else if (vfd_cursor < 40)
172                                 vfd_cursor = 40;
173                         else if (vfd_cursor < 60)
174                                 vfd_cursor = 60;
175                         if (vfd_cursor > 59)
176                                 scroll_vfd();
177                 } else {
178                         /* just a character */
179                         if (vfd_cursor > 39)
180                                 scroll_vfd();
181                         vfd[vfd_cursor++] = c;
182                 }
183                 indx--;
184                 buf++;
185         }
186         update_vfd();
187
188         return len;
189 }
190
191 static struct file_operations briq_panel_fops = {
192         .owner          = THIS_MODULE,
193         .read           = briq_panel_read,
194         .write          = briq_panel_write,
195         .open           = briq_panel_open,
196         .release        = briq_panel_release,
197 };
198
199 static struct miscdevice briq_panel_miscdev = {
200         BRIQ_PANEL_MINOR,
201         "briq_panel",
202         &briq_panel_fops
203 };
204
205 static int __init briq_panel_init(void)
206 {
207         struct device_node *root = find_path_device("/");
208         const char *machine;
209         int i;
210
211         machine = get_property(root, "model", NULL);
212         if (!machine || strncmp(machine, "TotalImpact,BRIQ-1", 18) != 0)
213                 return -ENODEV;
214
215         printk(KERN_INFO
216                 "briq_panel: v%s Dr. Karsten Jeppesen (kj@totalimpact.com)\n",
217                 BRIQ_PANEL_VER);
218
219         if (!request_region(BRIQ_PANEL_VFD_IOPORT, 4, "BRIQ Front Panel"))
220                 return -EBUSY;
221
222         if (!request_region(BRIQ_PANEL_LED_IOPORT, 2, "BRIQ Front Panel")) {
223                 release_region(BRIQ_PANEL_VFD_IOPORT, 4);
224                 return -EBUSY;
225         }
226         ledpb = inb(BRIQ_PANEL_LED_IOPORT) & 0x000c;
227
228         if (misc_register(&briq_panel_miscdev) < 0) {
229                 release_region(BRIQ_PANEL_VFD_IOPORT, 4);
230                 release_region(BRIQ_PANEL_LED_IOPORT, 2);
231                 return -EBUSY;
232         }
233
234         outb(0x38, BRIQ_PANEL_VFD_IOPORT);      /* Function set */
235         outb(0x01, BRIQ_PANEL_VFD_IOPORT);      /* Clear display */
236         outb(0x0c, BRIQ_PANEL_VFD_IOPORT);      /* Display on */
237         outb(0x06, BRIQ_PANEL_VFD_IOPORT);      /* Entry normal */
238         for (i=0; i<40; i++)
239                 vfd[i]=' ';
240 #ifndef MODULE
241         vfd[0] = 'L';
242         vfd[1] = 'o';
243         vfd[2] = 'a';
244         vfd[3] = 'd';
245         vfd[4] = 'i';
246         vfd[5] = 'n';
247         vfd[6] = 'g';
248         vfd[7] = ' ';
249         vfd[8] = '.';
250         vfd[9] = '.';
251         vfd[10] = '.';
252 #endif /* !MODULE */
253
254         update_vfd();
255
256         return 0;
257 }
258
259 static void __exit briq_panel_exit(void)
260 {
261         misc_deregister(&briq_panel_miscdev);
262         release_region(BRIQ_PANEL_VFD_IOPORT, 4);
263         release_region(BRIQ_PANEL_LED_IOPORT, 2);
264 }
265
266 module_init(briq_panel_init);
267 module_exit(briq_panel_exit);
268
269 MODULE_LICENSE("GPL");
270 MODULE_AUTHOR("Karsten Jeppesen <karsten@jeppesens.com>");
271 MODULE_DESCRIPTION("Driver for the Total Impact briQ front panel");