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>
54 #include <linux/sched.h>
56 #include <linux/tty.h>
57 #include <linux/console.h>
58 #include <linux/string.h>
60 #include <linux/init.h>
61 #include <linux/slab.h>
62 #include <linux/vt_kern.h>
63 #include <linux/selection.h>
64 #include <linux/spinlock.h>
65 #include <linux/kref.h>
66 #include <linux/smp_lock.h>
67 #include <linux/ioport.h>
68 #include <linux/interrupt.h>
69 #include <linux/vmalloc.h>
72 #include "sisusb_init.h"
74 #ifdef INCL_SISUSB_CON
76 #define sisusbcon_writew(val, addr) (*(addr) = (val))
77 #define sisusbcon_readw(addr) (*(addr))
78 #define sisusbcon_memmovew(d, s, c) memmove(d, s, c)
79 #define sisusbcon_memcpyw(d, s, c) memcpy(d, s, c)
81 /* vc_data -> sisusb conversion table */
82 static struct sisusb_usb_data *mysisusbs[MAX_NR_CONSOLES];
84 /* Forward declaration */
85 static const struct consw sisusb_con;
88 sisusbcon_memsetw(u16 *s, u16 c, unsigned int count)
92 sisusbcon_writew(c, s++);
96 sisusb_initialize(struct sisusb_usb_data *sisusb)
98 /* Reset cursor and start address */
99 if (sisusb_setidxreg(sisusb, SISCR, 0x0c, 0x00))
101 if (sisusb_setidxreg(sisusb, SISCR, 0x0d, 0x00))
103 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, 0x00))
105 sisusb_setidxreg(sisusb, SISCR, 0x0f, 0x00);
109 sisusbcon_set_start_address(struct sisusb_usb_data *sisusb, struct vc_data *c)
111 sisusb->cur_start_addr = (c->vc_visible_origin - sisusb->scrbuf) / 2;
113 sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8));
114 sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));
118 sisusb_set_cursor(struct sisusb_usb_data *sisusb, unsigned int location)
120 if (sisusb->sisusb_cursor_loc == location)
123 sisusb->sisusb_cursor_loc = location;
125 /* Hardware bug: Text cursor appears twice or not at all
126 * at some positions. Work around it with the cursor skew
130 if ((location & 0x0007) == 0x0007) {
131 sisusb->bad_cursor_pos = 1;
133 if (sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0x1f, 0x20))
135 } else if (sisusb->bad_cursor_pos) {
136 if (sisusb_setidxregand(sisusb, SISCR, 0x0b, 0x1f))
138 sisusb->bad_cursor_pos = 0;
141 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, (location >> 8)))
143 sisusb_setidxreg(sisusb, SISCR, 0x0f, (location & 0xff));
146 static inline struct sisusb_usb_data *
147 sisusb_get_sisusb(unsigned short console)
149 return mysisusbs[console];
153 sisusb_sisusb_valid(struct sisusb_usb_data *sisusb)
155 if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev)
161 static struct sisusb_usb_data *
162 sisusb_get_sisusb_lock_and_check(unsigned short console)
164 struct sisusb_usb_data *sisusb;
166 /* We can't handle console calls in non-schedulable
167 * context due to our locks and the USB transport.
168 * So we simply ignore them. This should only affect
169 * some calls to printk.
174 if (!(sisusb = sisusb_get_sisusb(console)))
177 mutex_lock(&sisusb->lock);
179 if (!sisusb_sisusb_valid(sisusb) ||
180 !sisusb->havethisconsole[console]) {
181 mutex_unlock(&sisusb->lock);
189 sisusb_is_inactive(struct vc_data *c, struct sisusb_usb_data *sisusb)
191 if (sisusb->is_gfx ||
192 sisusb->textmodedestroyed ||
193 c->vc_mode != KD_TEXT)
199 /* con_startup console interface routine */
201 sisusbcon_startup(void)
206 /* con_init console interface routine */
208 sisusbcon_init(struct vc_data *c, int init)
210 struct sisusb_usb_data *sisusb;
213 /* This is called by take_over_console(),
214 * ie by us/under our control. It is
215 * only called after text mode and fonts
216 * are set up/restored.
219 mutex_lock(&disconnect_mutex);
221 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
222 mutex_unlock(&disconnect_mutex);
226 mutex_lock(&sisusb->lock);
228 if (!sisusb_sisusb_valid(sisusb)) {
229 mutex_unlock(&sisusb->lock);
230 mutex_unlock(&disconnect_mutex);
234 c->vc_can_do_color = 1;
236 c->vc_complement_mask = 0x7700;
238 c->vc_hi_font_mask = sisusb->current_font_512 ? 0x0800 : 0;
240 sisusb->haveconsole = 1;
242 sisusb->havethisconsole[c->vc_num] = 1;
244 /* We only support 640x400 */
245 c->vc_scan_lines = 400;
247 c->vc_font.height = sisusb->current_font_height;
249 /* We only support width = 8 */
251 rows = c->vc_scan_lines / c->vc_font.height;
253 /* Increment usage count for our sisusb.
254 * Doing so saves us from upping/downing
255 * the disconnect semaphore; we can't
256 * lose our sisusb until this is undone
257 * in con_deinit. For all other console
258 * interface functions, it suffices to
259 * use sisusb->lock and do a quick check
260 * of sisusb for device disconnection.
262 kref_get(&sisusb->kref);
264 if (!*c->vc_uni_pagedir_loc)
265 con_set_default_unimap(c);
267 mutex_unlock(&sisusb->lock);
269 mutex_unlock(&disconnect_mutex);
275 vc_resize(c, cols, rows);
278 /* con_deinit console interface routine */
280 sisusbcon_deinit(struct vc_data *c)
282 struct sisusb_usb_data *sisusb;
285 /* This is called by take_over_console()
286 * and others, ie not under our control.
289 mutex_lock(&disconnect_mutex);
291 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
292 mutex_unlock(&disconnect_mutex);
296 mutex_lock(&sisusb->lock);
298 /* Clear ourselves in mysisusbs */
299 mysisusbs[c->vc_num] = NULL;
301 sisusb->havethisconsole[c->vc_num] = 0;
303 /* Free our font buffer if all consoles are gone */
304 if (sisusb->font_backup) {
305 for(i = 0; i < MAX_NR_CONSOLES; i++) {
306 if (sisusb->havethisconsole[c->vc_num])
309 if (i == MAX_NR_CONSOLES) {
310 vfree(sisusb->font_backup);
311 sisusb->font_backup = NULL;
315 mutex_unlock(&sisusb->lock);
317 /* decrement the usage count on our sisusb */
318 kref_put(&sisusb->kref, sisusb_delete);
320 mutex_unlock(&disconnect_mutex);
323 /* interface routine */
325 sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
326 u8 blink, u8 underline, u8 reverse)
331 attr = (attr & 0xf0) | c->vc_ulcolor;
332 else if (intensity == 0)
333 attr = (attr & 0xf0) | c->vc_halfcolor;
336 attr = ((attr) & 0x88) |
338 ((attr) << 4)) & 0x77);
349 /* Interface routine */
351 sisusbcon_invert_region(struct vc_data *vc, u16 *p, int count)
353 /* Invert a region. This is called with a pointer
354 * to the console's internal screen buffer. So we
355 * simply do the inversion there and rely on
356 * a call to putc(s) to update the real screen.
360 u16 a = sisusbcon_readw(p);
363 (((a) & 0x7000) >> 4) |
364 (((a) & 0x0700) << 4);
366 sisusbcon_writew(a, p++);
370 #define SISUSB_VADDR(x,y) \
371 ((u16 *)c->vc_origin + \
372 (y) * sisusb->sisusb_num_columns + \
375 #define SISUSB_HADDR(x,y) \
376 ((u16 *)(sisusb->vrambase + (c->vc_origin - sisusb->scrbuf)) + \
377 (y) * sisusb->sisusb_num_columns + \
380 /* Interface routine */
382 sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
384 struct sisusb_usb_data *sisusb;
387 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
390 /* sisusb->lock is down */
392 /* Don't need to put the character into buffer ourselves,
393 * because the vt does this BEFORE calling us.
396 sisusbcon_writew(ch, SISUSB_VADDR(x, y));
399 if (sisusb_is_inactive(c, sisusb)) {
400 mutex_unlock(&sisusb->lock);
405 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
406 (long)SISUSB_HADDR(x, y), 2, &written);
408 mutex_unlock(&sisusb->lock);
411 /* Interface routine */
413 sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
414 int count, int y, int x)
416 struct sisusb_usb_data *sisusb;
421 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
424 /* sisusb->lock is down */
426 /* Need to put the characters into the buffer ourselves,
427 * because the vt does this AFTER calling us.
430 dest = SISUSB_VADDR(x, y);
432 for (i = count; i > 0; i--)
433 sisusbcon_writew(sisusbcon_readw(s++), dest++);
435 if (sisusb_is_inactive(c, sisusb)) {
436 mutex_unlock(&sisusb->lock);
440 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
441 (long)SISUSB_HADDR(x, y), count * 2, &written);
443 mutex_unlock(&sisusb->lock);
446 /* Interface routine */
448 sisusbcon_clear(struct vc_data *c, int y, int x, int height, int width)
450 struct sisusb_usb_data *sisusb;
451 u16 eattr = c->vc_video_erase_char;
456 if (width <= 0 || height <= 0)
459 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
462 /* sisusb->lock is down */
464 /* Need to clear buffer ourselves, because the vt does
465 * this AFTER calling us.
468 dest = SISUSB_VADDR(x, y);
470 cols = sisusb->sisusb_num_columns;
475 if (x == 0 && width >= c->vc_cols) {
477 sisusbcon_memsetw(dest, eattr, height * cols * 2);
481 for (i = height; i > 0; i--, dest += cols)
482 sisusbcon_memsetw(dest, eattr, width * 2);
486 if (sisusb_is_inactive(c, sisusb)) {
487 mutex_unlock(&sisusb->lock);
491 length = ((height * cols) - x - (cols - width - x)) * 2;
494 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(x, y),
495 (long)SISUSB_HADDR(x, y), length, &written);
497 mutex_unlock(&sisusb->lock);
500 /* Interface routine */
502 sisusbcon_bmove(struct vc_data *c, int sy, int sx,
503 int dy, int dx, int height, int width)
505 struct sisusb_usb_data *sisusb;
513 if (width <= 0 || height <= 0)
516 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
519 /* sisusb->lock is down */
521 cols = sisusb->sisusb_num_columns;
523 /* Don't need to move data outselves, because
524 * vt does this BEFORE calling us.
525 * This is only used by vt's insert/deletechar.
528 if (sx == 0 && dx == 0 && width >= c->vc_cols && width <= cols) {
530 sisusbcon_memmovew(SISUSB_VADDR(0, dy), SISUSB_VADDR(0, sy),
533 } else if (dy < sy || (dy == sy && dx < sx)) {
535 src = SISUSB_VADDR(sx, sy);
536 dest = SISUSB_VADDR(dx, dy);
538 for (i = height; i > 0; i--) {
539 sisusbcon_memmovew(dest, src, width * 2);
546 src = SISUSB_VADDR(sx, sy + height - 1);
547 dest = SISUSB_VADDR(dx, dy + height - 1);
549 for (i = height; i > 0; i--) {
550 sisusbcon_memmovew(dest, src, width * 2);
558 if (sisusb_is_inactive(c, sisusb)) {
559 mutex_unlock(&sisusb->lock);
563 length = ((height * cols) - dx - (cols - width - dx)) * 2;
566 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(dx, dy),
567 (long)SISUSB_HADDR(dx, dy), length, &written);
569 mutex_unlock(&sisusb->lock);
572 /* interface routine */
574 sisusbcon_switch(struct vc_data *c)
576 struct sisusb_usb_data *sisusb;
580 /* Returnvalue 0 means we have fully restored screen,
581 * and vt doesn't need to call do_update_region().
582 * Returnvalue != 0 naturally means the opposite.
585 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
588 /* sisusb->lock is down */
590 /* Don't write to screen if in gfx mode */
591 if (sisusb_is_inactive(c, sisusb)) {
592 mutex_unlock(&sisusb->lock);
596 /* That really should not happen. It would mean we are
597 * being called while the vc is using its private buffer
600 if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
601 mutex_unlock(&sisusb->lock);
602 printk(KERN_DEBUG "sisusb: ASSERT ORIGIN != SCREENBUF!\n");
606 /* Check that we don't copy too much */
607 length = min((int)c->vc_screenbuf_size,
608 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
610 /* Restore the screen contents */
611 sisusbcon_memcpyw((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf,
614 sisusb_copy_memory(sisusb, (unsigned char *)c->vc_origin,
615 (long)SISUSB_HADDR(0, 0),
618 mutex_unlock(&sisusb->lock);
623 /* interface routine */
625 sisusbcon_save_screen(struct vc_data *c)
627 struct sisusb_usb_data *sisusb;
630 /* Save the current screen contents to vc's private
634 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
637 /* sisusb->lock is down */
639 if (sisusb_is_inactive(c, sisusb)) {
640 mutex_unlock(&sisusb->lock);
644 /* Check that we don't copy too much */
645 length = min((int)c->vc_screenbuf_size,
646 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
648 /* Save the screen contents to vc's private buffer */
649 sisusbcon_memcpyw((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin,
652 mutex_unlock(&sisusb->lock);
655 /* interface routine */
657 sisusbcon_set_palette(struct vc_data *c, unsigned char *table)
659 struct sisusb_usb_data *sisusb;
662 /* Return value not used by vt */
664 if (!CON_IS_VISIBLE(c))
667 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
670 /* sisusb->lock is down */
672 if (sisusb_is_inactive(c, sisusb)) {
673 mutex_unlock(&sisusb->lock);
677 for (i = j = 0; i < 16; i++) {
678 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
680 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
682 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
684 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
688 mutex_unlock(&sisusb->lock);
693 /* interface routine */
695 sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
697 struct sisusb_usb_data *sisusb;
698 u8 sr1, cr17, pmreg, cr63;
702 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
705 /* sisusb->lock is down */
708 sisusb->is_gfx = blank ? 1 : 0;
710 if (sisusb_is_inactive(c, sisusb)) {
711 mutex_unlock(&sisusb->lock);
717 case 1: /* Normal blanking: Clear screen */
719 sisusbcon_memsetw((u16 *)c->vc_origin,
720 c->vc_video_erase_char,
721 c->vc_screenbuf_size);
722 sisusb_copy_memory(sisusb,
723 (unsigned char *)c->vc_origin,
724 (u32)(sisusb->vrambase +
725 (c->vc_origin - sisusb->scrbuf)),
726 c->vc_screenbuf_size, &written);
727 sisusb->con_blanked = 1;
731 default: /* VESA blanking */
733 case 0: /* Unblank */
739 sisusb->con_blanked = 0;
741 case VESA_VSYNC_SUSPEND + 1:
747 case VESA_HSYNC_SUSPEND + 1:
753 case VESA_POWERDOWN + 1:
760 mutex_unlock(&sisusb->lock);
764 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
765 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
766 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
767 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
771 mutex_unlock(&sisusb->lock);
776 /* interface routine */
778 sisusbcon_scrolldelta(struct vc_data *c, int lines)
780 struct sisusb_usb_data *sisusb;
781 int margin = c->vc_size_row * 4;
784 /* The return value does not seem to be used */
786 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
789 /* sisusb->lock is down */
791 if (sisusb_is_inactive(c, sisusb)) {
792 mutex_unlock(&sisusb->lock);
796 if (!lines) /* Turn scrollback off */
797 c->vc_visible_origin = c->vc_origin;
800 if (sisusb->con_rolled_over >
801 (c->vc_scr_end - sisusb->scrbuf) + margin) {
803 ul = c->vc_scr_end - sisusb->scrbuf;
804 we = sisusb->con_rolled_over + c->vc_size_row;
809 we = sisusb->scrbuf_size;
813 p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we +
814 lines * c->vc_size_row;
816 st = (c->vc_origin - sisusb->scrbuf - ul + we) % we;
827 c->vc_visible_origin = sisusb->scrbuf + (p + ul) % we;
830 sisusbcon_set_start_address(sisusb, c);
832 mutex_unlock(&sisusb->lock);
837 /* Interface routine */
839 sisusbcon_cursor(struct vc_data *c, int mode)
841 struct sisusb_usb_data *sisusb;
842 int from, to, baseline;
844 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
847 /* sisusb->lock is down */
849 if (sisusb_is_inactive(c, sisusb)) {
850 mutex_unlock(&sisusb->lock);
854 if (c->vc_origin != c->vc_visible_origin) {
855 c->vc_visible_origin = c->vc_origin;
856 sisusbcon_set_start_address(sisusb, c);
859 if (mode == CM_ERASE) {
860 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
861 sisusb->sisusb_cursor_size_to = -1;
862 mutex_unlock(&sisusb->lock);
866 sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
868 baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
870 switch (c->vc_cursor_type & 0x0f) {
871 case CUR_BLOCK: from = 1;
872 to = c->vc_font.height;
874 case CUR_TWO_THIRDS: from = c->vc_font.height / 3;
877 case CUR_LOWER_HALF: from = c->vc_font.height / 2;
880 case CUR_LOWER_THIRD: from = (c->vc_font.height * 2) / 3;
883 case CUR_NONE: from = 31;
887 case CUR_UNDERLINE: from = baseline - 1;
892 if (sisusb->sisusb_cursor_size_from != from ||
893 sisusb->sisusb_cursor_size_to != to) {
895 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
896 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
898 sisusb->sisusb_cursor_size_from = from;
899 sisusb->sisusb_cursor_size_to = to;
902 mutex_unlock(&sisusb->lock);
906 sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
907 int t, int b, int dir, int lines)
909 int cols = sisusb->sisusb_num_columns;
910 int length = ((b - t) * cols) * 2;
911 u16 eattr = c->vc_video_erase_char;
914 /* sisusb->lock is down */
916 /* Scroll an area which does not match the
917 * visible screen's dimensions. This needs
918 * to be done separately, as it does not
919 * use hardware panning.
925 sisusbcon_memmovew(SISUSB_VADDR(0, t),
926 SISUSB_VADDR(0, t + lines),
927 (b - t - lines) * cols * 2);
928 sisusbcon_memsetw(SISUSB_VADDR(0, b - lines), eattr,
933 sisusbcon_memmovew(SISUSB_VADDR(0, t + lines),
935 (b - t - lines) * cols * 2);
936 sisusbcon_memsetw(SISUSB_VADDR(0, t), eattr,
941 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(0, t),
942 (long)SISUSB_HADDR(0, t), length, &written);
944 mutex_unlock(&sisusb->lock);
949 /* Interface routine */
951 sisusbcon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
953 struct sisusb_usb_data *sisusb;
954 u16 eattr = c->vc_video_erase_char;
957 unsigned long oldorigin;
958 unsigned int delta = lines * c->vc_size_row;
961 /* Returning != 0 means we have done the scrolling successfully.
962 * Returning 0 makes vt do the scrolling on its own.
963 * Note that con_scroll is only called if the console is
964 * visible. In that case, the origin should be our buffer,
965 * not the vt's private one.
971 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
974 /* sisusb->lock is down */
976 if (sisusb_is_inactive(c, sisusb)) {
977 mutex_unlock(&sisusb->lock);
982 if (t || b != c->vc_rows)
983 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
985 if (c->vc_origin != c->vc_visible_origin) {
986 c->vc_visible_origin = c->vc_origin;
987 sisusbcon_set_start_address(sisusb, c);
990 /* limit amount to maximum realistic size */
991 if (lines > c->vc_rows)
994 oldorigin = c->vc_origin;
1000 if (c->vc_scr_end + delta >=
1001 sisusb->scrbuf + sisusb->scrbuf_size) {
1002 sisusbcon_memcpyw((u16 *)sisusb->scrbuf,
1003 (u16 *)(oldorigin + delta),
1004 c->vc_screenbuf_size - delta);
1005 c->vc_origin = sisusb->scrbuf;
1006 sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
1009 c->vc_origin += delta;
1012 (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
1019 if (oldorigin - delta < sisusb->scrbuf) {
1020 sisusbcon_memmovew((u16 *)(sisusb->scrbuf +
1021 sisusb->scrbuf_size -
1022 c->vc_screenbuf_size +
1025 c->vc_screenbuf_size - delta);
1026 c->vc_origin = sisusb->scrbuf +
1027 sisusb->scrbuf_size -
1028 c->vc_screenbuf_size;
1029 sisusb->con_rolled_over = 0;
1032 c->vc_origin -= delta;
1034 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1036 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
1041 originoffset = (u32)(c->vc_origin - sisusb->scrbuf);
1044 sisusb_copy_memory(sisusb,
1045 (char *)c->vc_origin,
1046 (u32)(sisusb->vrambase + originoffset),
1047 c->vc_screenbuf_size, &written);
1048 else if (dir == SM_UP)
1049 sisusb_copy_memory(sisusb,
1050 (char *)c->vc_origin + c->vc_screenbuf_size - delta,
1051 (u32)sisusb->vrambase + originoffset +
1052 c->vc_screenbuf_size - delta,
1055 sisusb_copy_memory(sisusb,
1056 (char *)c->vc_origin,
1057 (u32)(sisusb->vrambase + originoffset),
1060 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1061 c->vc_visible_origin = c->vc_origin;
1063 sisusbcon_set_start_address(sisusb, c);
1065 c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
1067 mutex_unlock(&sisusb->lock);
1072 /* Interface routine */
1074 sisusbcon_set_origin(struct vc_data *c)
1076 struct sisusb_usb_data *sisusb;
1078 /* Returning != 0 means we were successful.
1079 * Returning 0 will vt make to use its own
1080 * screenbuffer as the origin.
1083 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1086 /* sisusb->lock is down */
1088 if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
1089 mutex_unlock(&sisusb->lock);
1093 c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
1095 sisusbcon_set_start_address(sisusb, c);
1097 sisusb->con_rolled_over = 0;
1099 mutex_unlock(&sisusb->lock);
1104 /* Interface routine */
1106 sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows)
1108 struct sisusb_usb_data *sisusb;
1111 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1114 fh = sisusb->current_font_height;
1116 mutex_unlock(&sisusb->lock);
1118 /* We are quite unflexible as regards resizing. The vt code
1119 * handles sizes where the line length isn't equal the pitch
1120 * quite badly. As regards the rows, our panning tricks only
1121 * work well if the number of rows equals the visible number
1125 if (newcols != 80 || c->vc_scan_lines / fh != newrows)
1132 sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
1133 u8 *arg, int cmapsz, int ch512, int dorecalc,
1134 struct vc_data *c, int fh, int uplock)
1136 int font_select = 0x00, i, err = 0;
1140 /* sisusb->lock is down */
1143 * The default font is kept in slot 0.
1144 * A user font is loaded in slot 2 (256 ch)
1148 if ((slot != 0 && slot != 2) || !fh) {
1150 mutex_unlock(&sisusb->lock);
1155 sisusb->font_slot = slot;
1157 /* Default font is always 256 */
1161 offset = 4 * cmapsz;
1163 font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1165 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1166 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1167 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1168 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1173 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1174 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1175 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1182 for (i = 0; i < cmapsz; i++) {
1183 err |= sisusb_writeb(sisusb,
1184 sisusb->vrambase + offset + i,
1190 for (i = 0; i < cmapsz; i++) {
1191 err |= sisusb_readb(sisusb,
1192 sisusb->vrambase + offset + i,
1199 * In 512-character mode, the character map is not contiguous if
1200 * we want to remain EGA compatible -- which we do
1205 for (i = 0; i < cmapsz; i++) {
1206 err |= sisusb_writeb(sisusb,
1207 sisusb->vrambase + offset +
1214 for (i = 0; i < cmapsz; i++) {
1215 err |= sisusb_readb(sisusb,
1216 sisusb->vrambase + offset +
1228 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1229 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1230 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1232 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1233 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1238 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1239 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1240 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1245 if ((set) && (ch512 != sisusb->current_font_512)) {
1247 /* Font is shared among all our consoles.
1248 * And so is the hi_font_mask.
1250 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1251 struct vc_data *c = vc_cons[i].d;
1252 if (c && c->vc_sw == &sisusb_con)
1253 c->vc_hi_font_mask = ch512 ? 0x0800 : 0;
1256 sisusb->current_font_512 = ch512;
1258 /* color plane enable register:
1259 256-char: enable intensity bit
1260 512-char: disable intensity bit */
1261 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1262 sisusb_setreg(sisusb, SISAR, 0x12);
1263 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1265 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1266 sisusb_setreg(sisusb, SISAR, 0x20);
1267 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1273 * Adjust the screen to fit a font of a certain height
1276 unsigned char ovr, vde, fsr;
1277 int rows = 0, maxscan = 0;
1281 /* Number of video rows */
1282 rows = c->vc_scan_lines / fh;
1283 /* Scan lines to actually display-1 */
1284 maxscan = rows * fh - 1;
1286 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1287 rows, maxscan, fh, c->vc_scan_lines);*/
1289 sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1290 vde = maxscan & 0xff;
1291 ovr = (ovr & 0xbd) |
1292 ((maxscan & 0x100) >> 7) |
1293 ((maxscan & 0x200) >> 3);
1294 sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1295 sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1299 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1300 fsr = (fsr & 0xe0) | (fh - 1);
1301 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1302 sisusb->current_font_height = fh;
1304 sisusb->sisusb_cursor_size_from = -1;
1305 sisusb->sisusb_cursor_size_to = -1;
1310 mutex_unlock(&sisusb->lock);
1312 if (dorecalc && c) {
1313 int i, rows = c->vc_scan_lines / fh;
1315 /* Now adjust our consoles' size */
1317 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1318 struct vc_data *vc = vc_cons[i].d;
1320 if (vc && vc->vc_sw == &sisusb_con) {
1321 if (CON_IS_VISIBLE(vc)) {
1322 vc->vc_sw->con_cursor(vc, CM_DRAW);
1324 vc->vc_font.height = fh;
1325 vc_resize(vc, 0, rows);
1334 mutex_unlock(&sisusb->lock);
1339 /* Interface routine */
1341 sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1344 struct sisusb_usb_data *sisusb;
1345 unsigned charcount = font->charcount;
1347 if (font->width != 8 || (charcount != 256 && charcount != 512))
1350 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1353 /* sisusb->lock is down */
1355 /* Save the user-provided font into a buffer. This
1356 * is used for restoring text mode after quitting
1357 * from X and for the con_getfont routine.
1359 if (sisusb->font_backup) {
1360 if (sisusb->font_backup_size < charcount) {
1361 vfree(sisusb->font_backup);
1362 sisusb->font_backup = NULL;
1366 if (!sisusb->font_backup)
1367 sisusb->font_backup = vmalloc(charcount * 32);
1369 if (sisusb->font_backup) {
1370 memcpy(sisusb->font_backup, font->data, charcount * 32);
1371 sisusb->font_backup_size = charcount;
1372 sisusb->font_backup_height = font->height;
1373 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1376 /* do_font_op ups sisusb->lock */
1378 return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1379 8192, (charcount == 512),
1380 (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1381 c, font->height, 1);
1384 /* Interface routine */
1386 sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1388 struct sisusb_usb_data *sisusb;
1390 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1393 /* sisusb->lock is down */
1396 font->height = c->vc_font.height;
1397 font->charcount = 256;
1400 mutex_unlock(&sisusb->lock);
1404 if (!sisusb->font_backup) {
1405 mutex_unlock(&sisusb->lock);
1409 /* Copy 256 chars only, like vgacon */
1410 memcpy(font->data, sisusb->font_backup, 256 * 32);
1412 mutex_unlock(&sisusb->lock);
1418 * The console `switch' structure for the sisusb console
1421 static const struct consw sisusb_con = {
1422 .owner = THIS_MODULE,
1423 .con_startup = sisusbcon_startup,
1424 .con_init = sisusbcon_init,
1425 .con_deinit = sisusbcon_deinit,
1426 .con_clear = sisusbcon_clear,
1427 .con_putc = sisusbcon_putc,
1428 .con_putcs = sisusbcon_putcs,
1429 .con_cursor = sisusbcon_cursor,
1430 .con_scroll = sisusbcon_scroll,
1431 .con_bmove = sisusbcon_bmove,
1432 .con_switch = sisusbcon_switch,
1433 .con_blank = sisusbcon_blank,
1434 .con_font_set = sisusbcon_font_set,
1435 .con_font_get = sisusbcon_font_get,
1436 .con_set_palette = sisusbcon_set_palette,
1437 .con_scrolldelta = sisusbcon_scrolldelta,
1438 .con_build_attr = sisusbcon_build_attr,
1439 .con_invert_region = sisusbcon_invert_region,
1440 .con_set_origin = sisusbcon_set_origin,
1441 .con_save_screen = sisusbcon_save_screen,
1442 .con_resize = sisusbcon_resize,
1445 /* Our very own dummy console driver */
1447 static const char *sisusbdummycon_startup(void)
1449 return "SISUSBVGADUMMY";
1452 static void sisusbdummycon_init(struct vc_data *vc, int init)
1454 vc->vc_can_do_color = 1;
1459 vc_resize(vc, 80, 25);
1462 static int sisusbdummycon_dummy(void)
1467 #define SISUSBCONDUMMY (void *)sisusbdummycon_dummy
1469 static const struct consw sisusb_dummy_con = {
1470 .owner = THIS_MODULE,
1471 .con_startup = sisusbdummycon_startup,
1472 .con_init = sisusbdummycon_init,
1473 .con_deinit = SISUSBCONDUMMY,
1474 .con_clear = SISUSBCONDUMMY,
1475 .con_putc = SISUSBCONDUMMY,
1476 .con_putcs = SISUSBCONDUMMY,
1477 .con_cursor = SISUSBCONDUMMY,
1478 .con_scroll = SISUSBCONDUMMY,
1479 .con_bmove = SISUSBCONDUMMY,
1480 .con_switch = SISUSBCONDUMMY,
1481 .con_blank = SISUSBCONDUMMY,
1482 .con_font_set = SISUSBCONDUMMY,
1483 .con_font_get = SISUSBCONDUMMY,
1484 .con_font_default = SISUSBCONDUMMY,
1485 .con_font_copy = SISUSBCONDUMMY,
1486 .con_set_palette = SISUSBCONDUMMY,
1487 .con_scrolldelta = SISUSBCONDUMMY,
1491 sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1493 int i, ret, minor = sisusb->minor;
1495 mutex_lock(&disconnect_mutex);
1497 mutex_lock(&sisusb->lock);
1499 /* Erm.. that should not happen */
1500 if (sisusb->haveconsole || !sisusb->SiS_Pr) {
1501 mutex_unlock(&sisusb->lock);
1502 mutex_unlock(&disconnect_mutex);
1506 sisusb->con_first = first;
1507 sisusb->con_last = last;
1510 first > MAX_NR_CONSOLES ||
1511 last > MAX_NR_CONSOLES) {
1512 mutex_unlock(&sisusb->lock);
1513 mutex_unlock(&disconnect_mutex);
1517 /* If gfxcore not initialized or no consoles given, quit graciously */
1518 if (!sisusb->gfxinit || first < 1 || last < 1) {
1519 mutex_unlock(&sisusb->lock);
1520 mutex_unlock(&disconnect_mutex);
1524 sisusb->sisusb_cursor_loc = -1;
1525 sisusb->sisusb_cursor_size_from = -1;
1526 sisusb->sisusb_cursor_size_to = -1;
1528 /* Set up text mode (and upload default font) */
1529 if (sisusb_reset_text_mode(sisusb, 1)) {
1530 mutex_unlock(&sisusb->lock);
1531 mutex_unlock(&disconnect_mutex);
1533 "sisusbvga[%d]: Failed to set up text mode\n",
1538 /* Initialize some gfx registers */
1539 sisusb_initialize(sisusb);
1541 for (i = first - 1; i <= last - 1; i++) {
1542 /* Save sisusb for our interface routines */
1543 mysisusbs[i] = sisusb;
1546 /* Initial console setup */
1547 sisusb->sisusb_num_columns = 80;
1549 /* Use a 32K buffer (matches b8000-bffff area) */
1550 sisusb->scrbuf_size = 32 * 1024;
1552 /* Allocate screen buffer */
1553 if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
1554 mutex_unlock(&sisusb->lock);
1555 mutex_unlock(&disconnect_mutex);
1557 "sisusbvga[%d]: Failed to allocate screen buffer\n",
1562 mutex_unlock(&sisusb->lock);
1563 mutex_unlock(&disconnect_mutex);
1565 /* Now grab the desired console(s) */
1566 ret = take_over_console(&sisusb_con, first - 1, last - 1, 0);
1569 sisusb->haveconsole = 1;
1571 for (i = first - 1; i <= last - 1; i++)
1572 mysisusbs[i] = NULL;
1579 sisusb_console_exit(struct sisusb_usb_data *sisusb)
1583 /* This is called if the device is disconnected
1584 * and while disconnect and lock semaphores
1585 * are up. This should be save because we
1586 * can't lose our sisusb any other way but by
1587 * disconnection (and hence, the disconnect
1588 * sema is for protecting all other access
1589 * functions from disconnection, not the
1593 /* Now what do we do in case of disconnection:
1594 * One alternative would be to simply call
1595 * give_up_console(). Nah, not a good idea.
1596 * give_up_console() is obviously buggy as it
1597 * only discards the consw pointer from the
1598 * driver_map, but doesn't adapt vc->vc_sw
1599 * of the affected consoles. Hence, the next
1600 * call to any of the console functions will
1601 * eventually take a trip to oops county.
1602 * Also, give_up_console for some reason
1603 * doesn't decrement our module refcount.
1604 * Instead, we switch our consoles to a private
1605 * dummy console. This, of course, keeps our
1606 * refcount up as well, but it works perfectly.
1609 if (sisusb->haveconsole) {
1610 for (i = 0; i < MAX_NR_CONSOLES; i++)
1611 if (sisusb->havethisconsole[i])
1612 take_over_console(&sisusb_dummy_con, i, i, 0);
1613 /* At this point, con_deinit for all our
1614 * consoles is executed by take_over_console().
1616 sisusb->haveconsole = 0;
1619 vfree((void *)sisusb->scrbuf);
1622 vfree(sisusb->font_backup);
1623 sisusb->font_backup = NULL;
1626 void __init sisusb_init_concode(void)
1630 for (i = 0; i < MAX_NR_CONSOLES; i++)
1631 mysisusbs[i] = NULL;
1634 #endif /* INCL_CON */