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/tty.h>
12#include <linux/timer.h>
13#include <linux/kernel.h>
14#include <linux/wait.h>
15#include <linux/string.h>
16#include <linux/slab.h>
17#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{
70	/* enforce single access */
71	if (vfd_is_open)
72		return -EBUSY;
73	vfd_is_open = 1;
74
75	return 0;
76}
77
78static int briq_panel_release(struct inode *ino, struct file *filep)
79{
80	if (!vfd_is_open)
81		return -ENODEV;
82
83	vfd_is_open = 0;
84
85	return 0;
86}
87
88static ssize_t briq_panel_read(struct file *file, char __user *buf, size_t count,
89			 loff_t *ppos)
90{
91	unsigned short c;
92	unsigned char cp;
93
94
95	if (!vfd_is_open)
96		return -ENODEV;
97
98	c = (inb(BRIQ_PANEL_LED_IOPORT) & 0x000c) | (ledpb & 0x0003);
99	set_led(' ');
100	/* upper button released */
101	if ((!(ledpb & 0x0004)) && (c & 0x0004)) {
102		cp = ' ';
103		ledpb = c;
104		if (copy_to_user(buf, &cp, 1))
105			return -EFAULT;
106		return 1;
107	}
108	/* lower button released */
109	else if ((!(ledpb & 0x0008)) && (c & 0x0008)) {
110		cp = '\r';
111		ledpb = c;
112		if (copy_to_user(buf, &cp, 1))
113			return -EFAULT;
114		return 1;
115	} else {
116		ledpb = c;
117		return 0;
118	}
119}
120
121static void scroll_vfd( void )
122{
123	int	i;
124
125	for (i=0; i<20; i++) {
126		vfd[i] = vfd[i+20];
127		vfd[i+20] = ' ';
128	}
129	vfd_cursor = 20;
130}
131
132static ssize_t briq_panel_write(struct file *file, const char __user *buf, size_t len,
133			  loff_t *ppos)
134{
135	size_t indx = len;
136	int i, esc = 0;
137
138
139	if (!vfd_is_open)
140		return -EBUSY;
141
142	for (;;) {
143		char c;
144		if (!indx)
145			break;
146		if (get_user(c, buf))
147			return -EFAULT;
148		if (esc) {
149			set_led(c);
150			esc = 0;
151		} else if (c == 27) {
152			esc = 1;
153		} else if (c == 12) {
154			/* do a form feed */
155			for (i=0; i<40; i++)
156				vfd[i] = ' ';
157			vfd_cursor = 0;
158		} else if (c == 10) {
159			if (vfd_cursor < 20)
160				vfd_cursor = 20;
161			else if (vfd_cursor < 40)
162				vfd_cursor = 40;
163			else if (vfd_cursor < 60)
164				vfd_cursor = 60;
165			if (vfd_cursor > 59)
166				scroll_vfd();
167		} else {
168			/* just a character */
169			if (vfd_cursor > 39)
170				scroll_vfd();
171			vfd[vfd_cursor++] = c;
172		}
173		indx--;
174		buf++;
175	}
176	update_vfd();
177
178	return len;
179}
180
181static const struct file_operations briq_panel_fops = {
182	.owner		= THIS_MODULE,
183	.read		= briq_panel_read,
184	.write		= briq_panel_write,
185	.open		= briq_panel_open,
186	.release	= briq_panel_release,
187};
188
189static struct miscdevice briq_panel_miscdev = {
190	BRIQ_PANEL_MINOR,
191	"briq_panel",
192	&briq_panel_fops
193};
194
195static int __init briq_panel_init(void)
196{
197	struct device_node *root = of_find_node_by_path("/");
198	const char *machine;
199	int i;
200
201	machine = of_get_property(root, "model", NULL);
202	if (!machine || strncmp(machine, "TotalImpact,BRIQ-1", 18) != 0) {
203		of_node_put(root);
204		return -ENODEV;
205	}
206	of_node_put(root);
207
208	printk(KERN_INFO
209		"briq_panel: v%s Dr. Karsten Jeppesen (kj@totalimpact.com)\n",
210		BRIQ_PANEL_VER);
211
212	if (!request_region(BRIQ_PANEL_VFD_IOPORT, 4, "BRIQ Front Panel"))
213		return -EBUSY;
214
215	if (!request_region(BRIQ_PANEL_LED_IOPORT, 2, "BRIQ Front Panel")) {
216		release_region(BRIQ_PANEL_VFD_IOPORT, 4);
217		return -EBUSY;
218	}
219	ledpb = inb(BRIQ_PANEL_LED_IOPORT) & 0x000c;
220
221	if (misc_register(&briq_panel_miscdev) < 0) {
222		release_region(BRIQ_PANEL_VFD_IOPORT, 4);
223		release_region(BRIQ_PANEL_LED_IOPORT, 2);
224		return -EBUSY;
225	}
226
227	outb(0x38, BRIQ_PANEL_VFD_IOPORT);	/* Function set */
228	outb(0x01, BRIQ_PANEL_VFD_IOPORT);	/* Clear display */
229	outb(0x0c, BRIQ_PANEL_VFD_IOPORT);	/* Display on */
230	outb(0x06, BRIQ_PANEL_VFD_IOPORT);	/* Entry normal */
231	for (i=0; i<40; i++)
232		vfd[i]=' ';
233#ifndef MODULE
234	vfd[0] = 'L';
235	vfd[1] = 'o';
236	vfd[2] = 'a';
237	vfd[3] = 'd';
238	vfd[4] = 'i';
239	vfd[5] = 'n';
240	vfd[6] = 'g';
241	vfd[7] = ' ';
242	vfd[8] = '.';
243	vfd[9] = '.';
244	vfd[10] = '.';
245#endif /* !MODULE */
246
247	update_vfd();
248
249	return 0;
250}
251
252static void __exit briq_panel_exit(void)
253{
254	misc_deregister(&briq_panel_miscdev);
255	release_region(BRIQ_PANEL_VFD_IOPORT, 4);
256	release_region(BRIQ_PANEL_LED_IOPORT, 2);
257}
258
259module_init(briq_panel_init);
260module_exit(briq_panel_exit);
261
262MODULE_LICENSE("GPL");
263MODULE_AUTHOR("Karsten Jeppesen <karsten@jeppesens.com>");
264MODULE_DESCRIPTION("Driver for the Total Impact briQ front panel");
265