tty: vt, consw->con_scrolldelta cleanup
[cascardo/linux.git] / drivers / video / console / sticon.c
1 /*
2  *  linux/drivers/video/console/sticon.c - console driver using HP's STI firmware
3  *
4  *      Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
5  *      Copyright (C) 2002 Helge Deller <deller@gmx.de>
6  *
7  *  Based on linux/drivers/video/vgacon.c and linux/drivers/video/fbcon.c,
8  *  which were
9  *
10  *      Created 28 Sep 1997 by Geert Uytterhoeven
11  *      Rewritten by Martin Mares <mj@ucw.cz>, July 1998
12  *      Copyright (C) 1991, 1992  Linus Torvalds
13  *                          1995  Jay Estabrook
14  *      Copyright (C) 1995 Geert Uytterhoeven
15  *      Copyright (C) 1993 Bjoern Brauel
16  *                         Roman Hodek
17  *      Copyright (C) 1993 Hamish Macdonald
18  *                         Greg Harp
19  *      Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
20  *
21  *            with work by William Rucklidge (wjr@cs.cornell.edu)
22  *                         Geert Uytterhoeven
23  *                         Jes Sorensen (jds@kom.auc.dk)
24  *                         Martin Apel
25  *            with work by Guenther Kelleter
26  *                         Martin Schaller
27  *                         Andreas Schwab
28  *                         Emmanuel Marty (core@ggi-project.org)
29  *                         Jakub Jelinek (jj@ultra.linux.cz)
30  *                         Martin Mares <mj@ucw.cz>
31  *
32  *  This file is subject to the terms and conditions of the GNU General Public
33  *  License.  See the file COPYING in the main directory of this archive for
34  *  more details.
35  *
36  */
37
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/console.h>
41 #include <linux/errno.h>
42 #include <linux/vt_kern.h>
43 #include <linux/kd.h>
44 #include <linux/selection.h>
45 #include <linux/module.h>
46
47 #include <asm/io.h>
48
49 #include "../fbdev/sticore.h"
50
51 /* switching to graphics mode */
52 #define BLANK 0
53 static int vga_is_gfx;
54
55 /* this is the sti_struct used for this console */
56 static struct sti_struct *sticon_sti;
57
58 /* Software scrollback */
59 static unsigned long softback_buf, softback_curr;
60 static unsigned long softback_in;
61 static unsigned long /* softback_top, */ softback_end;
62 static int softback_lines;
63
64 /* software cursor */
65 static int cursor_drawn;
66 #define CURSOR_DRAW_DELAY               (1)
67 #define DEFAULT_CURSOR_BLINK_RATE       (20)
68
69 static int vbl_cursor_cnt;
70
71 static inline void cursor_undrawn(void)
72 {
73     vbl_cursor_cnt = 0;
74     cursor_drawn = 0;
75 }
76
77 static const char *sticon_startup(void)
78 {
79     return "STI console";
80 }
81
82 static int sticon_set_palette(struct vc_data *c, const unsigned char *table)
83 {
84     return -EINVAL;
85 }
86
87 static void sticon_putc(struct vc_data *conp, int c, int ypos, int xpos)
88 {
89     int redraw_cursor = 0;
90
91     if (vga_is_gfx || console_blanked)
92             return;
93
94     if (conp->vc_mode != KD_TEXT)
95             return;
96 #if 0
97     if ((p->cursor_x == xpos) && (p->cursor_y == ypos)) {
98             cursor_undrawn();
99             redraw_cursor = 1;
100     }
101 #endif
102
103     sti_putc(sticon_sti, c, ypos, xpos);
104
105     if (redraw_cursor)
106             vbl_cursor_cnt = CURSOR_DRAW_DELAY;
107 }
108
109 static void sticon_putcs(struct vc_data *conp, const unsigned short *s,
110                          int count, int ypos, int xpos)
111 {
112     int redraw_cursor = 0;
113
114     if (vga_is_gfx || console_blanked)
115             return;
116
117     if (conp->vc_mode != KD_TEXT)
118             return;
119
120 #if 0
121     if ((p->cursor_y == ypos) && (xpos <= p->cursor_x) &&
122         (p->cursor_x < (xpos + count))) {
123             cursor_undrawn();
124             redraw_cursor = 1;
125     }
126 #endif
127
128     while (count--) {
129         sti_putc(sticon_sti, scr_readw(s++), ypos, xpos++);
130     }
131
132     if (redraw_cursor)
133             vbl_cursor_cnt = CURSOR_DRAW_DELAY;
134 }
135
136 static void sticon_cursor(struct vc_data *conp, int mode)
137 {
138     unsigned short car1;
139
140     car1 = conp->vc_screenbuf[conp->vc_x + conp->vc_y * conp->vc_cols];
141     switch (mode) {
142     case CM_ERASE:
143         sti_putc(sticon_sti, car1, conp->vc_y, conp->vc_x);
144         break;
145     case CM_MOVE:
146     case CM_DRAW:
147         switch (conp->vc_cursor_type & 0x0f) {
148         case CUR_UNDERLINE:
149         case CUR_LOWER_THIRD:
150         case CUR_LOWER_HALF:
151         case CUR_TWO_THIRDS:
152         case CUR_BLOCK:
153             sti_putc(sticon_sti, (car1 & 255) + (0 << 8) + (7 << 11),
154                      conp->vc_y, conp->vc_x);
155             break;
156         }
157         break;
158     }
159 }
160
161 static int sticon_scroll(struct vc_data *conp, int t, int b, int dir, int count)
162 {
163     struct sti_struct *sti = sticon_sti;
164
165     if (vga_is_gfx)
166         return 0;
167
168     sticon_cursor(conp, CM_ERASE);
169
170     switch (dir) {
171     case SM_UP:
172         sti_bmove(sti, t + count, 0, t, 0, b - t - count, conp->vc_cols);
173         sti_clear(sti, b - count, 0, count, conp->vc_cols, conp->vc_video_erase_char);
174         break;
175
176     case SM_DOWN:
177         sti_bmove(sti, t, 0, t + count, 0, b - t - count, conp->vc_cols);
178         sti_clear(sti, t, 0, count, conp->vc_cols, conp->vc_video_erase_char);
179         break;
180     }
181
182     return 0;
183 }
184
185 static void sticon_bmove(struct vc_data *conp, int sy, int sx, 
186         int dy, int dx, int height, int width)
187 {
188     if (!width || !height)
189             return;
190 #if 0
191     if (((sy <= p->cursor_y) && (p->cursor_y < sy+height) &&
192         (sx <= p->cursor_x) && (p->cursor_x < sx+width)) ||
193         ((dy <= p->cursor_y) && (p->cursor_y < dy+height) &&
194         (dx <= p->cursor_x) && (p->cursor_x < dx+width)))
195                 sticon_cursor(p, CM_ERASE /*|CM_SOFTBACK*/);
196 #endif
197
198     sti_bmove(sticon_sti, sy, sx, dy, dx, height, width);
199 }
200
201 static void sticon_init(struct vc_data *c, int init)
202 {
203     struct sti_struct *sti = sticon_sti;
204     int vc_cols, vc_rows;
205
206     sti_set(sti, 0, 0, sti_onscreen_y(sti), sti_onscreen_x(sti), 0);
207     vc_cols = sti_onscreen_x(sti) / sti->font_width;
208     vc_rows = sti_onscreen_y(sti) / sti->font_height;
209     c->vc_can_do_color = 1;
210     
211     if (init) {
212         c->vc_cols = vc_cols;
213         c->vc_rows = vc_rows;
214     } else {
215         /* vc_rows = (c->vc_rows > vc_rows) ? vc_rows : c->vc_rows; */
216         /* vc_cols = (c->vc_cols > vc_cols) ? vc_cols : c->vc_cols; */
217         vc_resize(c, vc_cols, vc_rows);
218 /*      vc_resize_con(vc_rows, vc_cols, c->vc_num); */
219     }
220 }
221
222 static void sticon_deinit(struct vc_data *c)
223 {
224 }
225
226 static void sticon_clear(struct vc_data *conp, int sy, int sx, int height,
227                          int width)
228 {
229     if (!height || !width)
230         return;
231
232     sti_clear(sticon_sti, sy, sx, height, width, conp->vc_video_erase_char);
233 }
234
235 static int sticon_switch(struct vc_data *conp)
236 {
237     return 1;   /* needs refreshing */
238 }
239
240 static int sticon_set_origin(struct vc_data *conp)
241 {
242     return 0;
243 }
244
245 static int sticon_blank(struct vc_data *c, int blank, int mode_switch)
246 {
247     if (blank == 0) {
248         if (mode_switch)
249             vga_is_gfx = 0;
250         return 1;
251     }
252     sticon_set_origin(c);
253     sti_clear(sticon_sti, 0,0, c->vc_rows, c->vc_cols, BLANK);
254     if (mode_switch)
255         vga_is_gfx = 1;
256     return 1;
257 }
258
259 static u16 *sticon_screen_pos(struct vc_data *conp, int offset)
260 {
261     int line;
262     unsigned long p;
263
264     if (conp->vc_num != fg_console || !softback_lines)
265         return (u16 *)(conp->vc_origin + offset);
266     line = offset / conp->vc_size_row;
267     if (line >= softback_lines)
268         return (u16 *)(conp->vc_origin + offset - softback_lines * conp->vc_size_row);
269     p = softback_curr + offset;
270     if (p >= softback_end)
271         p += softback_buf - softback_end;
272     return (u16 *)p;
273 }
274
275 static unsigned long sticon_getxy(struct vc_data *conp, unsigned long pos,
276                                   int *px, int *py)
277 {
278     int x, y;
279     unsigned long ret;
280     if (pos >= conp->vc_origin && pos < conp->vc_scr_end) {
281         unsigned long offset = (pos - conp->vc_origin) / 2;
282         
283         x = offset % conp->vc_cols;
284         y = offset / conp->vc_cols;
285         if (conp->vc_num == fg_console)
286             y += softback_lines;
287         ret = pos + (conp->vc_cols - x) * 2;
288     } else if (conp->vc_num == fg_console && softback_lines) {
289         unsigned long offset = pos - softback_curr;
290         
291         if (pos < softback_curr)
292             offset += softback_end - softback_buf;
293         offset /= 2;
294         x = offset % conp->vc_cols;
295         y = offset / conp->vc_cols;
296         ret = pos + (conp->vc_cols - x) * 2;
297         if (ret == softback_end)
298             ret = softback_buf;
299         if (ret == softback_in)
300             ret = conp->vc_origin;
301     } else {
302         /* Should not happen */
303         x = y = 0;
304         ret = conp->vc_origin;
305     }
306     if (px) *px = x;
307     if (py) *py = y;
308     return ret;
309 }
310
311 static u8 sticon_build_attr(struct vc_data *conp, u8 color, u8 intens,
312                             u8 blink, u8 underline, u8 reverse, u8 italic)
313 {
314     u8 attr = ((color & 0x70) >> 1) | ((color & 7));
315
316     if (reverse) {
317         color = ((color >> 3) & 0x7) | ((color & 0x7) << 3);
318     }
319
320     return attr;
321 }
322
323 static void sticon_invert_region(struct vc_data *conp, u16 *p, int count)
324 {
325     int col = 1; /* vga_can_do_color; */
326
327     while (count--) {
328         u16 a = scr_readw(p);
329
330         if (col)
331                 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
332         else
333                 a = ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
334
335         scr_writew(a, p++);
336     }
337 }
338
339 static void sticon_save_screen(struct vc_data *conp)
340 {
341 }
342
343 static const struct consw sti_con = {
344         .owner                  = THIS_MODULE,
345         .con_startup            = sticon_startup,
346         .con_init               = sticon_init,
347         .con_deinit             = sticon_deinit,
348         .con_clear              = sticon_clear,
349         .con_putc               = sticon_putc,
350         .con_putcs              = sticon_putcs,
351         .con_cursor             = sticon_cursor,
352         .con_scroll             = sticon_scroll,
353         .con_bmove              = sticon_bmove,
354         .con_switch             = sticon_switch,
355         .con_blank              = sticon_blank,
356         .con_set_palette        = sticon_set_palette,
357         .con_set_origin         = sticon_set_origin,
358         .con_save_screen        = sticon_save_screen, 
359         .con_build_attr         = sticon_build_attr,
360         .con_invert_region      = sticon_invert_region, 
361         .con_screen_pos         = sticon_screen_pos,
362         .con_getxy              = sticon_getxy,
363 };
364
365
366
367 static int __init sticonsole_init(void)
368 {
369     int err;
370     /* already initialized ? */
371     if (sticon_sti)
372          return 0;
373
374     sticon_sti = sti_get_rom(0);
375     if (!sticon_sti)
376         return -ENODEV;
377
378     if (conswitchp == &dummy_con) {
379         printk(KERN_INFO "sticon: Initializing STI text console.\n");
380         console_lock();
381         err = do_take_over_console(&sti_con, 0, MAX_NR_CONSOLES - 1, 1);
382         console_unlock();
383         return err;
384     }
385     return 0;
386 }
387
388 module_init(sticonsole_init);
389 MODULE_LICENSE("GPL");