2 * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles
4 * VGA text mode console part
6 * Copyright (C) 2005 by Thomas Winischhofer, Vienna, Austria
8 * If distributed as part of the Linux kernel, this code is licensed under the
11 * Otherwise, the following license terms apply:
13 * * Redistribution and use in source and binary forms, with or without
14 * * modification, are permitted provided that the following conditions
16 * * 1) Redistributions of source code must retain the above copyright
17 * * notice, this list of conditions and the following disclaimer.
18 * * 2) Redistributions in binary form must reproduce the above copyright
19 * * notice, this list of conditions and the following disclaimer in the
20 * * documentation and/or other materials provided with the distribution.
21 * * 3) The name of the author may not be used to endorse or promote products
22 * * derived from this software without specific psisusbr written permission.
24 * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR
25 * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Author: Thomas Winischhofer <thomas@winischhofer.net>
37 * Portions based on vgacon.c which are
38 * Created 28 Sep 1997 by Geert Uytterhoeven
39 * Rewritten by Martin Mares <mj@ucw.cz>, July 1998
40 * based on code Copyright (C) 1991, 1992 Linus Torvalds
43 * A note on using in_atomic() in here: We can't handle console
44 * calls from non-schedulable context due to our USB-dependend
45 * nature. For now, this driver just ignores any calls if it
50 #include <linux/mutex.h>
51 #include <linux/module.h>
52 #include <linux/kernel.h>
53 #include <linux/signal.h>
55 #include <linux/tty.h>
56 #include <linux/console.h>
57 #include <linux/string.h>
59 #include <linux/init.h>
60 #include <linux/slab.h>
61 #include <linux/vt_kern.h>
62 #include <linux/selection.h>
63 #include <linux/spinlock.h>
64 #include <linux/kref.h>
65 #include <linux/smp_lock.h>
66 #include <linux/ioport.h>
67 #include <linux/interrupt.h>
68 #include <linux/vmalloc.h>
71 #include "sisusb_init.h"
73 #ifdef INCL_SISUSB_CON
75 #define sisusbcon_writew(val, addr) (*(addr) = (val))
76 #define sisusbcon_readw(addr) (*(addr))
77 #define sisusbcon_memmovew(d, s, c) memmove(d, s, c)
78 #define sisusbcon_memcpyw(d, s, c) memcpy(d, s, c)
80 /* vc_data -> sisusb conversion table */
81 static struct sisusb_usb_data *mysisusbs[MAX_NR_CONSOLES];
83 /* Forward declaration */
84 static const struct consw sisusb_con;
87 sisusbcon_memsetw(u16 *s, u16 c, unsigned int count)
91 sisusbcon_writew(c, s++);
95 sisusb_initialize(struct sisusb_usb_data *sisusb)
97 /* Reset cursor and start address */
98 if (sisusb_setidxreg(sisusb, SISCR, 0x0c, 0x00))
100 if (sisusb_setidxreg(sisusb, SISCR, 0x0d, 0x00))
102 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, 0x00))
104 sisusb_setidxreg(sisusb, SISCR, 0x0f, 0x00);
108 sisusbcon_set_start_address(struct sisusb_usb_data *sisusb, struct vc_data *c)
110 sisusb->cur_start_addr = (c->vc_visible_origin - sisusb->scrbuf) / 2;
112 sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8));
113 sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));
117 sisusb_set_cursor(struct sisusb_usb_data *sisusb, unsigned int location)
119 if (sisusb->sisusb_cursor_loc == location)
122 sisusb->sisusb_cursor_loc = location;
124 /* Hardware bug: Text cursor appears twice or not at all
125 * at some positions. Work around it with the cursor skew
129 if ((location & 0x0007) == 0x0007) {
130 sisusb->bad_cursor_pos = 1;
132 if (sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0x1f, 0x20))
134 } else if (sisusb->bad_cursor_pos) {
135 if (sisusb_setidxregand(sisusb, SISCR, 0x0b, 0x1f))
137 sisusb->bad_cursor_pos = 0;
140 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, (location >> 8)))
142 sisusb_setidxreg(sisusb, SISCR, 0x0f, (location & 0xff));
145 static inline struct sisusb_usb_data *
146 sisusb_get_sisusb(unsigned short console)
148 return mysisusbs[console];
152 sisusb_sisusb_valid(struct sisusb_usb_data *sisusb)
154 if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev)
160 static struct sisusb_usb_data *
161 sisusb_get_sisusb_lock_and_check(unsigned short console)
163 struct sisusb_usb_data *sisusb;
165 /* We can't handle console calls in non-schedulable
166 * context due to our locks and the USB transport.
167 * So we simply ignore them. This should only affect
168 * some calls to printk.
173 if (!(sisusb = sisusb_get_sisusb(console)))
176 mutex_lock(&sisusb->lock);
178 if (!sisusb_sisusb_valid(sisusb) ||
179 !sisusb->havethisconsole[console]) {
180 mutex_unlock(&sisusb->lock);
188 sisusb_is_inactive(struct vc_data *c, struct sisusb_usb_data *sisusb)
190 if (sisusb->is_gfx ||
191 sisusb->textmodedestroyed ||
192 c->vc_mode != KD_TEXT)
198 /* con_startup console interface routine */
200 sisusbcon_startup(void)
205 /* con_init console interface routine */
207 sisusbcon_init(struct vc_data *c, int init)
209 struct sisusb_usb_data *sisusb;
212 /* This is called by take_over_console(),
213 * ie by us/under our control. It is
214 * only called after text mode and fonts
215 * are set up/restored.
218 mutex_lock(&disconnect_mutex);
220 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
221 mutex_unlock(&disconnect_mutex);
225 mutex_lock(&sisusb->lock);
227 if (!sisusb_sisusb_valid(sisusb)) {
228 mutex_unlock(&sisusb->lock);
229 mutex_unlock(&disconnect_mutex);
233 c->vc_can_do_color = 1;
235 c->vc_complement_mask = 0x7700;
237 c->vc_hi_font_mask = sisusb->current_font_512 ? 0x0800 : 0;
239 sisusb->haveconsole = 1;
241 sisusb->havethisconsole[c->vc_num] = 1;
243 /* We only support 640x400 */
244 c->vc_scan_lines = 400;
246 c->vc_font.height = sisusb->current_font_height;
248 /* We only support width = 8 */
250 rows = c->vc_scan_lines / c->vc_font.height;
252 /* Increment usage count for our sisusb.
253 * Doing so saves us from upping/downing
254 * the disconnect semaphore; we can't
255 * lose our sisusb until this is undone
256 * in con_deinit. For all other console
257 * interface functions, it suffices to
258 * use sisusb->lock and do a quick check
259 * of sisusb for device disconnection.
261 kref_get(&sisusb->kref);
263 if (!*c->vc_uni_pagedir_loc)
264 con_set_default_unimap(c);
266 mutex_unlock(&sisusb->lock);
268 mutex_unlock(&disconnect_mutex);
274 vc_resize(c, cols, rows);
277 /* con_deinit console interface routine */
279 sisusbcon_deinit(struct vc_data *c)
281 struct sisusb_usb_data *sisusb;
284 /* This is called by take_over_console()
285 * and others, ie not under our control.
288 mutex_lock(&disconnect_mutex);
290 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
291 mutex_unlock(&disconnect_mutex);
295 mutex_lock(&sisusb->lock);
297 /* Clear ourselves in mysisusbs */
298 mysisusbs[c->vc_num] = NULL;
300 sisusb->havethisconsole[c->vc_num] = 0;
302 /* Free our font buffer if all consoles are gone */
303 if (sisusb->font_backup) {
304 for(i = 0; i < MAX_NR_CONSOLES; i++) {
305 if (sisusb->havethisconsole[c->vc_num])
308 if (i == MAX_NR_CONSOLES) {
309 vfree(sisusb->font_backup);
310 sisusb->font_backup = NULL;
314 mutex_unlock(&sisusb->lock);
316 /* decrement the usage count on our sisusb */
317 kref_put(&sisusb->kref, sisusb_delete);
319 mutex_unlock(&disconnect_mutex);
322 /* interface routine */
324 sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
325 u8 blink, u8 underline, u8 reverse)
330 attr = (attr & 0xf0) | c->vc_ulcolor;
331 else if (intensity == 0)
332 attr = (attr & 0xf0) | c->vc_halfcolor;
335 attr = ((attr) & 0x88) |
337 ((attr) << 4)) & 0x77);
348 /* Interface routine */
350 sisusbcon_invert_region(struct vc_data *vc, u16 *p, int count)
352 /* Invert a region. This is called with a pointer
353 * to the console's internal screen buffer. So we
354 * simply do the inversion there and rely on
355 * a call to putc(s) to update the real screen.
359 u16 a = sisusbcon_readw(p);
362 (((a) & 0x7000) >> 4) |
363 (((a) & 0x0700) << 4);
365 sisusbcon_writew(a, p++);
369 #define SISUSB_VADDR(x,y) \
370 ((u16 *)c->vc_origin + \
371 (y) * sisusb->sisusb_num_columns + \
374 #define SISUSB_HADDR(x,y) \
375 ((u16 *)(sisusb->vrambase + (c->vc_origin - sisusb->scrbuf)) + \
376 (y) * sisusb->sisusb_num_columns + \
379 /* Interface routine */
381 sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
383 struct sisusb_usb_data *sisusb;
386 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
389 /* sisusb->lock is down */
391 /* Don't need to put the character into buffer ourselves,
392 * because the vt does this BEFORE calling us.
395 sisusbcon_writew(ch, SISUSB_VADDR(x, y));
398 if (sisusb_is_inactive(c, sisusb)) {
399 mutex_unlock(&sisusb->lock);
404 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
405 (long)SISUSB_HADDR(x, y), 2, &written);
407 mutex_unlock(&sisusb->lock);
410 /* Interface routine */
412 sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
413 int count, int y, int x)
415 struct sisusb_usb_data *sisusb;
420 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
423 /* sisusb->lock is down */
425 /* Need to put the characters into the buffer ourselves,
426 * because the vt does this AFTER calling us.
429 dest = SISUSB_VADDR(x, y);
431 for (i = count; i > 0; i--)
432 sisusbcon_writew(sisusbcon_readw(s++), dest++);
434 if (sisusb_is_inactive(c, sisusb)) {
435 mutex_unlock(&sisusb->lock);
439 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
440 (long)SISUSB_HADDR(x, y), count * 2, &written);
442 mutex_unlock(&sisusb->lock);
445 /* Interface routine */
447 sisusbcon_clear(struct vc_data *c, int y, int x, int height, int width)
449 struct sisusb_usb_data *sisusb;
450 u16 eattr = c->vc_video_erase_char;
455 if (width <= 0 || height <= 0)
458 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
461 /* sisusb->lock is down */
463 /* Need to clear buffer ourselves, because the vt does
464 * this AFTER calling us.
467 dest = SISUSB_VADDR(x, y);
469 cols = sisusb->sisusb_num_columns;
474 if (x == 0 && width >= c->vc_cols) {
476 sisusbcon_memsetw(dest, eattr, height * cols * 2);
480 for (i = height; i > 0; i--, dest += cols)
481 sisusbcon_memsetw(dest, eattr, width * 2);
485 if (sisusb_is_inactive(c, sisusb)) {
486 mutex_unlock(&sisusb->lock);
490 length = ((height * cols) - x - (cols - width - x)) * 2;
493 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(x, y),
494 (long)SISUSB_HADDR(x, y), length, &written);
496 mutex_unlock(&sisusb->lock);
499 /* Interface routine */
501 sisusbcon_bmove(struct vc_data *c, int sy, int sx,
502 int dy, int dx, int height, int width)
504 struct sisusb_usb_data *sisusb;
512 if (width <= 0 || height <= 0)
515 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
518 /* sisusb->lock is down */
520 cols = sisusb->sisusb_num_columns;
522 /* Don't need to move data outselves, because
523 * vt does this BEFORE calling us.
524 * This is only used by vt's insert/deletechar.
527 if (sx == 0 && dx == 0 && width >= c->vc_cols && width <= cols) {
529 sisusbcon_memmovew(SISUSB_VADDR(0, dy), SISUSB_VADDR(0, sy),
532 } else if (dy < sy || (dy == sy && dx < sx)) {
534 src = SISUSB_VADDR(sx, sy);
535 dest = SISUSB_VADDR(dx, dy);
537 for (i = height; i > 0; i--) {
538 sisusbcon_memmovew(dest, src, width * 2);
545 src = SISUSB_VADDR(sx, sy + height - 1);
546 dest = SISUSB_VADDR(dx, dy + height - 1);
548 for (i = height; i > 0; i--) {
549 sisusbcon_memmovew(dest, src, width * 2);
557 if (sisusb_is_inactive(c, sisusb)) {
558 mutex_unlock(&sisusb->lock);
562 length = ((height * cols) - dx - (cols - width - dx)) * 2;
565 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(dx, dy),
566 (long)SISUSB_HADDR(dx, dy), length, &written);
568 mutex_unlock(&sisusb->lock);
571 /* interface routine */
573 sisusbcon_switch(struct vc_data *c)
575 struct sisusb_usb_data *sisusb;
579 /* Returnvalue 0 means we have fully restored screen,
580 * and vt doesn't need to call do_update_region().
581 * Returnvalue != 0 naturally means the opposite.
584 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
587 /* sisusb->lock is down */
589 /* Don't write to screen if in gfx mode */
590 if (sisusb_is_inactive(c, sisusb)) {
591 mutex_unlock(&sisusb->lock);
595 /* That really should not happen. It would mean we are
596 * being called while the vc is using its private buffer
599 if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
600 mutex_unlock(&sisusb->lock);
601 printk(KERN_DEBUG "sisusb: ASSERT ORIGIN != SCREENBUF!\n");
605 /* Check that we don't copy too much */
606 length = min((int)c->vc_screenbuf_size,
607 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
609 /* Restore the screen contents */
610 sisusbcon_memcpyw((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf,
613 sisusb_copy_memory(sisusb, (unsigned char *)c->vc_origin,
614 (long)SISUSB_HADDR(0, 0),
617 mutex_unlock(&sisusb->lock);
622 /* interface routine */
624 sisusbcon_save_screen(struct vc_data *c)
626 struct sisusb_usb_data *sisusb;
629 /* Save the current screen contents to vc's private
633 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
636 /* sisusb->lock is down */
638 if (sisusb_is_inactive(c, sisusb)) {
639 mutex_unlock(&sisusb->lock);
643 /* Check that we don't copy too much */
644 length = min((int)c->vc_screenbuf_size,
645 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
647 /* Save the screen contents to vc's private buffer */
648 sisusbcon_memcpyw((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin,
651 mutex_unlock(&sisusb->lock);
654 /* interface routine */
656 sisusbcon_set_palette(struct vc_data *c, unsigned char *table)
658 struct sisusb_usb_data *sisusb;
661 /* Return value not used by vt */
663 if (!CON_IS_VISIBLE(c))
666 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
669 /* sisusb->lock is down */
671 if (sisusb_is_inactive(c, sisusb)) {
672 mutex_unlock(&sisusb->lock);
676 for (i = j = 0; i < 16; i++) {
677 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
679 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
681 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
683 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
687 mutex_unlock(&sisusb->lock);
692 /* interface routine */
694 sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
696 struct sisusb_usb_data *sisusb;
697 u8 sr1, cr17, pmreg, cr63;
701 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
704 /* sisusb->lock is down */
707 sisusb->is_gfx = blank ? 1 : 0;
709 if (sisusb_is_inactive(c, sisusb)) {
710 mutex_unlock(&sisusb->lock);
716 case 1: /* Normal blanking: Clear screen */
718 sisusbcon_memsetw((u16 *)c->vc_origin,
719 c->vc_video_erase_char,
720 c->vc_screenbuf_size);
721 sisusb_copy_memory(sisusb,
722 (unsigned char *)c->vc_origin,
723 (u32)(sisusb->vrambase +
724 (c->vc_origin - sisusb->scrbuf)),
725 c->vc_screenbuf_size, &written);
726 sisusb->con_blanked = 1;
730 default: /* VESA blanking */
732 case 0: /* Unblank */
738 sisusb->con_blanked = 0;
740 case VESA_VSYNC_SUSPEND + 1:
746 case VESA_HSYNC_SUSPEND + 1:
752 case VESA_POWERDOWN + 1:
759 mutex_unlock(&sisusb->lock);
763 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
764 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
765 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
766 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
770 mutex_unlock(&sisusb->lock);
775 /* interface routine */
777 sisusbcon_scrolldelta(struct vc_data *c, int lines)
779 struct sisusb_usb_data *sisusb;
780 int margin = c->vc_size_row * 4;
783 /* The return value does not seem to be used */
785 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
788 /* sisusb->lock is down */
790 if (sisusb_is_inactive(c, sisusb)) {
791 mutex_unlock(&sisusb->lock);
795 if (!lines) /* Turn scrollback off */
796 c->vc_visible_origin = c->vc_origin;
799 if (sisusb->con_rolled_over >
800 (c->vc_scr_end - sisusb->scrbuf) + margin) {
802 ul = c->vc_scr_end - sisusb->scrbuf;
803 we = sisusb->con_rolled_over + c->vc_size_row;
808 we = sisusb->scrbuf_size;
812 p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we +
813 lines * c->vc_size_row;
815 st = (c->vc_origin - sisusb->scrbuf - ul + we) % we;
826 c->vc_visible_origin = sisusb->scrbuf + (p + ul) % we;
829 sisusbcon_set_start_address(sisusb, c);
831 mutex_unlock(&sisusb->lock);
836 /* Interface routine */
838 sisusbcon_cursor(struct vc_data *c, int mode)
840 struct sisusb_usb_data *sisusb;
841 int from, to, baseline;
843 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
846 /* sisusb->lock is down */
848 if (sisusb_is_inactive(c, sisusb)) {
849 mutex_unlock(&sisusb->lock);
853 if (c->vc_origin != c->vc_visible_origin) {
854 c->vc_visible_origin = c->vc_origin;
855 sisusbcon_set_start_address(sisusb, c);
858 if (mode == CM_ERASE) {
859 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
860 sisusb->sisusb_cursor_size_to = -1;
861 mutex_unlock(&sisusb->lock);
865 sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
867 baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
869 switch (c->vc_cursor_type & 0x0f) {
870 case CUR_BLOCK: from = 1;
871 to = c->vc_font.height;
873 case CUR_TWO_THIRDS: from = c->vc_font.height / 3;
876 case CUR_LOWER_HALF: from = c->vc_font.height / 2;
879 case CUR_LOWER_THIRD: from = (c->vc_font.height * 2) / 3;
882 case CUR_NONE: from = 31;
886 case CUR_UNDERLINE: from = baseline - 1;
891 if (sisusb->sisusb_cursor_size_from != from ||
892 sisusb->sisusb_cursor_size_to != to) {
894 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
895 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
897 sisusb->sisusb_cursor_size_from = from;
898 sisusb->sisusb_cursor_size_to = to;
901 mutex_unlock(&sisusb->lock);
905 sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
906 int t, int b, int dir, int lines)
908 int cols = sisusb->sisusb_num_columns;
909 int length = ((b - t) * cols) * 2;
910 u16 eattr = c->vc_video_erase_char;
913 /* sisusb->lock is down */
915 /* Scroll an area which does not match the
916 * visible screen's dimensions. This needs
917 * to be done separately, as it does not
918 * use hardware panning.
924 sisusbcon_memmovew(SISUSB_VADDR(0, t),
925 SISUSB_VADDR(0, t + lines),
926 (b - t - lines) * cols * 2);
927 sisusbcon_memsetw(SISUSB_VADDR(0, b - lines), eattr,
932 sisusbcon_memmovew(SISUSB_VADDR(0, t + lines),
934 (b - t - lines) * cols * 2);
935 sisusbcon_memsetw(SISUSB_VADDR(0, t), eattr,
940 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(0, t),
941 (long)SISUSB_HADDR(0, t), length, &written);
943 mutex_unlock(&sisusb->lock);
948 /* Interface routine */
950 sisusbcon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
952 struct sisusb_usb_data *sisusb;
953 u16 eattr = c->vc_video_erase_char;
956 unsigned long oldorigin;
957 unsigned int delta = lines * c->vc_size_row;
960 /* Returning != 0 means we have done the scrolling successfully.
961 * Returning 0 makes vt do the scrolling on its own.
962 * Note that con_scroll is only called if the console is
963 * visible. In that case, the origin should be our buffer,
964 * not the vt's private one.
970 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
973 /* sisusb->lock is down */
975 if (sisusb_is_inactive(c, sisusb)) {
976 mutex_unlock(&sisusb->lock);
981 if (t || b != c->vc_rows)
982 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
984 if (c->vc_origin != c->vc_visible_origin) {
985 c->vc_visible_origin = c->vc_origin;
986 sisusbcon_set_start_address(sisusb, c);
989 /* limit amount to maximum realistic size */
990 if (lines > c->vc_rows)
993 oldorigin = c->vc_origin;
999 if (c->vc_scr_end + delta >=
1000 sisusb->scrbuf + sisusb->scrbuf_size) {
1001 sisusbcon_memcpyw((u16 *)sisusb->scrbuf,
1002 (u16 *)(oldorigin + delta),
1003 c->vc_screenbuf_size - delta);
1004 c->vc_origin = sisusb->scrbuf;
1005 sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
1008 c->vc_origin += delta;
1011 (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
1018 if (oldorigin - delta < sisusb->scrbuf) {
1019 sisusbcon_memmovew((u16 *)(sisusb->scrbuf +
1020 sisusb->scrbuf_size -
1021 c->vc_screenbuf_size +
1024 c->vc_screenbuf_size - delta);
1025 c->vc_origin = sisusb->scrbuf +
1026 sisusb->scrbuf_size -
1027 c->vc_screenbuf_size;
1028 sisusb->con_rolled_over = 0;
1031 c->vc_origin -= delta;
1033 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1035 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
1040 originoffset = (u32)(c->vc_origin - sisusb->scrbuf);
1043 sisusb_copy_memory(sisusb,
1044 (char *)c->vc_origin,
1045 (u32)(sisusb->vrambase + originoffset),
1046 c->vc_screenbuf_size, &written);
1047 else if (dir == SM_UP)
1048 sisusb_copy_memory(sisusb,
1049 (char *)c->vc_origin + c->vc_screenbuf_size - delta,
1050 (u32)sisusb->vrambase + originoffset +
1051 c->vc_screenbuf_size - delta,
1054 sisusb_copy_memory(sisusb,
1055 (char *)c->vc_origin,
1056 (u32)(sisusb->vrambase + originoffset),
1059 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1060 c->vc_visible_origin = c->vc_origin;
1062 sisusbcon_set_start_address(sisusb, c);
1064 c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
1066 mutex_unlock(&sisusb->lock);
1071 /* Interface routine */
1073 sisusbcon_set_origin(struct vc_data *c)
1075 struct sisusb_usb_data *sisusb;
1077 /* Returning != 0 means we were successful.
1078 * Returning 0 will vt make to use its own
1079 * screenbuffer as the origin.
1082 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1085 /* sisusb->lock is down */
1087 if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
1088 mutex_unlock(&sisusb->lock);
1092 c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
1094 sisusbcon_set_start_address(sisusb, c);
1096 sisusb->con_rolled_over = 0;
1098 mutex_unlock(&sisusb->lock);
1103 /* Interface routine */
1105 sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows)
1107 struct sisusb_usb_data *sisusb;
1110 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1113 fh = sisusb->current_font_height;
1115 mutex_unlock(&sisusb->lock);
1117 /* We are quite unflexible as regards resizing. The vt code
1118 * handles sizes where the line length isn't equal the pitch
1119 * quite badly. As regards the rows, our panning tricks only
1120 * work well if the number of rows equals the visible number
1124 if (newcols != 80 || c->vc_scan_lines / fh != newrows)
1131 sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
1132 u8 *arg, int cmapsz, int ch512, int dorecalc,
1133 struct vc_data *c, int fh, int uplock)
1135 int font_select = 0x00, i, err = 0;
1139 /* sisusb->lock is down */
1142 * The default font is kept in slot 0.
1143 * A user font is loaded in slot 2 (256 ch)
1147 if ((slot != 0 && slot != 2) || !fh) {
1149 mutex_unlock(&sisusb->lock);
1154 sisusb->font_slot = slot;
1156 /* Default font is always 256 */
1160 offset = 4 * cmapsz;
1162 font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1164 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1165 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1166 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1167 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1172 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1173 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1174 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1181 for (i = 0; i < cmapsz; i++) {
1182 err |= sisusb_writeb(sisusb,
1183 sisusb->vrambase + offset + i,
1189 for (i = 0; i < cmapsz; i++) {
1190 err |= sisusb_readb(sisusb,
1191 sisusb->vrambase + offset + i,
1198 * In 512-character mode, the character map is not contiguous if
1199 * we want to remain EGA compatible -- which we do
1204 for (i = 0; i < cmapsz; i++) {
1205 err |= sisusb_writeb(sisusb,
1206 sisusb->vrambase + offset +
1213 for (i = 0; i < cmapsz; i++) {
1214 err |= sisusb_readb(sisusb,
1215 sisusb->vrambase + offset +
1227 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1228 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1229 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1231 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1232 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1237 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1238 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1239 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1244 if ((set) && (ch512 != sisusb->current_font_512)) {
1246 /* Font is shared among all our consoles.
1247 * And so is the hi_font_mask.
1249 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1250 struct vc_data *c = vc_cons[i].d;
1251 if (c && c->vc_sw == &sisusb_con)
1252 c->vc_hi_font_mask = ch512 ? 0x0800 : 0;
1255 sisusb->current_font_512 = ch512;
1257 /* color plane enable register:
1258 256-char: enable intensity bit
1259 512-char: disable intensity bit */
1260 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1261 sisusb_setreg(sisusb, SISAR, 0x12);
1262 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1264 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1265 sisusb_setreg(sisusb, SISAR, 0x20);
1266 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1272 * Adjust the screen to fit a font of a certain height
1275 unsigned char ovr, vde, fsr;
1276 int rows = 0, maxscan = 0;
1280 /* Number of video rows */
1281 rows = c->vc_scan_lines / fh;
1282 /* Scan lines to actually display-1 */
1283 maxscan = rows * fh - 1;
1285 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1286 rows, maxscan, fh, c->vc_scan_lines);*/
1288 sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1289 vde = maxscan & 0xff;
1290 ovr = (ovr & 0xbd) |
1291 ((maxscan & 0x100) >> 7) |
1292 ((maxscan & 0x200) >> 3);
1293 sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1294 sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1298 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1299 fsr = (fsr & 0xe0) | (fh - 1);
1300 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1301 sisusb->current_font_height = fh;
1303 sisusb->sisusb_cursor_size_from = -1;
1304 sisusb->sisusb_cursor_size_to = -1;
1309 mutex_unlock(&sisusb->lock);
1311 if (dorecalc && c) {
1312 int i, rows = c->vc_scan_lines / fh;
1314 /* Now adjust our consoles' size */
1316 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1317 struct vc_data *vc = vc_cons[i].d;
1319 if (vc && vc->vc_sw == &sisusb_con) {
1320 if (CON_IS_VISIBLE(vc)) {
1321 vc->vc_sw->con_cursor(vc, CM_DRAW);
1323 vc->vc_font.height = fh;
1324 vc_resize(vc, 0, rows);
1333 mutex_unlock(&sisusb->lock);
1338 /* Interface routine */
1340 sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1343 struct sisusb_usb_data *sisusb;
1344 unsigned charcount = font->charcount;
1346 if (font->width != 8 || (charcount != 256 && charcount != 512))
1349 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1352 /* sisusb->lock is down */
1354 /* Save the user-provided font into a buffer. This
1355 * is used for restoring text mode after quitting
1356 * from X and for the con_getfont routine.
1358 if (sisusb->font_backup) {
1359 if (sisusb->font_backup_size < charcount) {
1360 vfree(sisusb->font_backup);
1361 sisusb->font_backup = NULL;
1365 if (!sisusb->font_backup)
1366 sisusb->font_backup = vmalloc(charcount * 32);
1368 if (sisusb->font_backup) {
1369 memcpy(sisusb->font_backup, font->data, charcount * 32);
1370 sisusb->font_backup_size = charcount;
1371 sisusb->font_backup_height = font->height;
1372 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1375 /* do_font_op ups sisusb->lock */
1377 return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1378 8192, (charcount == 512),
1379 (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1380 c, font->height, 1);
1383 /* Interface routine */
1385 sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1387 struct sisusb_usb_data *sisusb;
1389 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1392 /* sisusb->lock is down */
1395 font->height = c->vc_font.height;
1396 font->charcount = 256;
1399 mutex_unlock(&sisusb->lock);
1403 if (!sisusb->font_backup) {
1404 mutex_unlock(&sisusb->lock);
1408 /* Copy 256 chars only, like vgacon */
1409 memcpy(font->data, sisusb->font_backup, 256 * 32);
1411 mutex_unlock(&sisusb->lock);
1417 * The console `switch' structure for the sisusb console
1420 static const struct consw sisusb_con = {
1421 .owner = THIS_MODULE,
1422 .con_startup = sisusbcon_startup,
1423 .con_init = sisusbcon_init,
1424 .con_deinit = sisusbcon_deinit,
1425 .con_clear = sisusbcon_clear,
1426 .con_putc = sisusbcon_putc,
1427 .con_putcs = sisusbcon_putcs,
1428 .con_cursor = sisusbcon_cursor,
1429 .con_scroll = sisusbcon_scroll,
1430 .con_bmove = sisusbcon_bmove,
1431 .con_switch = sisusbcon_switch,
1432 .con_blank = sisusbcon_blank,
1433 .con_font_set = sisusbcon_font_set,
1434 .con_font_get = sisusbcon_font_get,
1435 .con_set_palette = sisusbcon_set_palette,
1436 .con_scrolldelta = sisusbcon_scrolldelta,
1437 .con_build_attr = sisusbcon_build_attr,
1438 .con_invert_region = sisusbcon_invert_region,
1439 .con_set_origin = sisusbcon_set_origin,
1440 .con_save_screen = sisusbcon_save_screen,
1441 .con_resize = sisusbcon_resize,
1444 /* Our very own dummy console driver */
1446 static const char *sisusbdummycon_startup(void)
1448 return "SISUSBVGADUMMY";
1451 static void sisusbdummycon_init(struct vc_data *vc, int init)
1453 vc->vc_can_do_color = 1;
1458 vc_resize(vc, 80, 25);
1461 static int sisusbdummycon_dummy(void)
1466 #define SISUSBCONDUMMY (void *)sisusbdummycon_dummy
1468 static const struct consw sisusb_dummy_con = {
1469 .owner = THIS_MODULE,
1470 .con_startup = sisusbdummycon_startup,
1471 .con_init = sisusbdummycon_init,
1472 .con_deinit = SISUSBCONDUMMY,
1473 .con_clear = SISUSBCONDUMMY,
1474 .con_putc = SISUSBCONDUMMY,
1475 .con_putcs = SISUSBCONDUMMY,
1476 .con_cursor = SISUSBCONDUMMY,
1477 .con_scroll = SISUSBCONDUMMY,
1478 .con_bmove = SISUSBCONDUMMY,
1479 .con_switch = SISUSBCONDUMMY,
1480 .con_blank = SISUSBCONDUMMY,
1481 .con_font_set = SISUSBCONDUMMY,
1482 .con_font_get = SISUSBCONDUMMY,
1483 .con_font_default = SISUSBCONDUMMY,
1484 .con_font_copy = SISUSBCONDUMMY,
1485 .con_set_palette = SISUSBCONDUMMY,
1486 .con_scrolldelta = SISUSBCONDUMMY,
1490 sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1492 int i, ret, minor = sisusb->minor;
1494 mutex_lock(&disconnect_mutex);
1496 mutex_lock(&sisusb->lock);
1498 /* Erm.. that should not happen */
1499 if (sisusb->haveconsole || !sisusb->SiS_Pr) {
1500 mutex_unlock(&sisusb->lock);
1501 mutex_unlock(&disconnect_mutex);
1505 sisusb->con_first = first;
1506 sisusb->con_last = last;
1509 first > MAX_NR_CONSOLES ||
1510 last > MAX_NR_CONSOLES) {
1511 mutex_unlock(&sisusb->lock);
1512 mutex_unlock(&disconnect_mutex);
1516 /* If gfxcore not initialized or no consoles given, quit graciously */
1517 if (!sisusb->gfxinit || first < 1 || last < 1) {
1518 mutex_unlock(&sisusb->lock);
1519 mutex_unlock(&disconnect_mutex);
1523 sisusb->sisusb_cursor_loc = -1;
1524 sisusb->sisusb_cursor_size_from = -1;
1525 sisusb->sisusb_cursor_size_to = -1;
1527 /* Set up text mode (and upload default font) */
1528 if (sisusb_reset_text_mode(sisusb, 1)) {
1529 mutex_unlock(&sisusb->lock);
1530 mutex_unlock(&disconnect_mutex);
1532 "sisusbvga[%d]: Failed to set up text mode\n",
1537 /* Initialize some gfx registers */
1538 sisusb_initialize(sisusb);
1540 for (i = first - 1; i <= last - 1; i++) {
1541 /* Save sisusb for our interface routines */
1542 mysisusbs[i] = sisusb;
1545 /* Initial console setup */
1546 sisusb->sisusb_num_columns = 80;
1548 /* Use a 32K buffer (matches b8000-bffff area) */
1549 sisusb->scrbuf_size = 32 * 1024;
1551 /* Allocate screen buffer */
1552 if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
1553 mutex_unlock(&sisusb->lock);
1554 mutex_unlock(&disconnect_mutex);
1556 "sisusbvga[%d]: Failed to allocate screen buffer\n",
1561 mutex_unlock(&sisusb->lock);
1562 mutex_unlock(&disconnect_mutex);
1564 /* Now grab the desired console(s) */
1565 ret = take_over_console(&sisusb_con, first - 1, last - 1, 0);
1568 sisusb->haveconsole = 1;
1570 for (i = first - 1; i <= last - 1; i++)
1571 mysisusbs[i] = NULL;
1578 sisusb_console_exit(struct sisusb_usb_data *sisusb)
1582 /* This is called if the device is disconnected
1583 * and while disconnect and lock semaphores
1584 * are up. This should be save because we
1585 * can't lose our sisusb any other way but by
1586 * disconnection (and hence, the disconnect
1587 * sema is for protecting all other access
1588 * functions from disconnection, not the
1592 /* Now what do we do in case of disconnection:
1593 * One alternative would be to simply call
1594 * give_up_console(). Nah, not a good idea.
1595 * give_up_console() is obviously buggy as it
1596 * only discards the consw pointer from the
1597 * driver_map, but doesn't adapt vc->vc_sw
1598 * of the affected consoles. Hence, the next
1599 * call to any of the console functions will
1600 * eventually take a trip to oops county.
1601 * Also, give_up_console for some reason
1602 * doesn't decrement our module refcount.
1603 * Instead, we switch our consoles to a private
1604 * dummy console. This, of course, keeps our
1605 * refcount up as well, but it works perfectly.
1608 if (sisusb->haveconsole) {
1609 for (i = 0; i < MAX_NR_CONSOLES; i++)
1610 if (sisusb->havethisconsole[i])
1611 take_over_console(&sisusb_dummy_con, i, i, 0);
1612 /* At this point, con_deinit for all our
1613 * consoles is executed by take_over_console().
1615 sisusb->haveconsole = 0;
1618 vfree((void *)sisusb->scrbuf);
1621 vfree(sisusb->font_backup);
1622 sisusb->font_backup = NULL;
1625 void __init sisusb_init_concode(void)
1629 for (i = 0; i < MAX_NR_CONSOLES; i++)
1630 mysisusbs[i] = NULL;
1633 #endif /* INCL_CON */