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/config.h>
51 #include <linux/mutex.h>
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/signal.h>
55 #include <linux/sched.h>
57 #include <linux/tty.h>
58 #include <linux/console.h>
59 #include <linux/string.h>
61 #include <linux/init.h>
62 #include <linux/slab.h>
63 #include <linux/vt_kern.h>
64 #include <linux/selection.h>
65 #include <linux/spinlock.h>
66 #include <linux/kref.h>
67 #include <linux/smp_lock.h>
68 #include <linux/ioport.h>
69 #include <linux/interrupt.h>
70 #include <linux/vmalloc.h>
73 #include "sisusb_init.h"
75 #ifdef INCL_SISUSB_CON
77 #define sisusbcon_writew(val, addr) (*(addr) = (val))
78 #define sisusbcon_readw(addr) (*(addr))
79 #define sisusbcon_memmovew(d, s, c) memmove(d, s, c)
80 #define sisusbcon_memcpyw(d, s, c) memcpy(d, s, c)
82 /* vc_data -> sisusb conversion table */
83 static struct sisusb_usb_data *mysisusbs[MAX_NR_CONSOLES];
85 /* Forward declaration */
86 static const struct consw sisusb_con;
89 sisusbcon_memsetw(u16 *s, u16 c, unsigned int count)
93 sisusbcon_writew(c, s++);
97 sisusb_initialize(struct sisusb_usb_data *sisusb)
99 /* Reset cursor and start address */
100 if (sisusb_setidxreg(sisusb, SISCR, 0x0c, 0x00))
102 if (sisusb_setidxreg(sisusb, SISCR, 0x0d, 0x00))
104 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, 0x00))
106 sisusb_setidxreg(sisusb, SISCR, 0x0f, 0x00);
110 sisusbcon_set_start_address(struct sisusb_usb_data *sisusb, struct vc_data *c)
112 sisusb->cur_start_addr = (c->vc_visible_origin - sisusb->scrbuf) / 2;
114 sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8));
115 sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));
119 sisusb_set_cursor(struct sisusb_usb_data *sisusb, unsigned int location)
121 if (sisusb->sisusb_cursor_loc == location)
124 sisusb->sisusb_cursor_loc = location;
126 /* Hardware bug: Text cursor appears twice or not at all
127 * at some positions. Work around it with the cursor skew
131 if ((location & 0x0007) == 0x0007) {
132 sisusb->bad_cursor_pos = 1;
134 if (sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0x1f, 0x20))
136 } else if (sisusb->bad_cursor_pos) {
137 if (sisusb_setidxregand(sisusb, SISCR, 0x0b, 0x1f))
139 sisusb->bad_cursor_pos = 0;
142 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, (location >> 8)))
144 sisusb_setidxreg(sisusb, SISCR, 0x0f, (location & 0xff));
147 static inline struct sisusb_usb_data *
148 sisusb_get_sisusb(unsigned short console)
150 return mysisusbs[console];
154 sisusb_sisusb_valid(struct sisusb_usb_data *sisusb)
156 if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev)
162 static struct sisusb_usb_data *
163 sisusb_get_sisusb_lock_and_check(unsigned short console)
165 struct sisusb_usb_data *sisusb;
167 /* We can't handle console calls in non-schedulable
168 * context due to our locks and the USB transport.
169 * So we simply ignore them. This should only affect
170 * some calls to printk.
175 if (!(sisusb = sisusb_get_sisusb(console)))
178 mutex_lock(&sisusb->lock);
180 if (!sisusb_sisusb_valid(sisusb) ||
181 !sisusb->havethisconsole[console]) {
182 mutex_unlock(&sisusb->lock);
190 sisusb_is_inactive(struct vc_data *c, struct sisusb_usb_data *sisusb)
192 if (sisusb->is_gfx ||
193 sisusb->textmodedestroyed ||
194 c->vc_mode != KD_TEXT)
200 /* con_startup console interface routine */
202 sisusbcon_startup(void)
207 /* con_init console interface routine */
209 sisusbcon_init(struct vc_data *c, int init)
211 struct sisusb_usb_data *sisusb;
214 /* This is called by take_over_console(),
215 * ie by us/under our control. It is
216 * only called after text mode and fonts
217 * are set up/restored.
220 mutex_lock(&disconnect_mutex);
222 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
223 mutex_unlock(&disconnect_mutex);
227 mutex_lock(&sisusb->lock);
229 if (!sisusb_sisusb_valid(sisusb)) {
230 mutex_unlock(&sisusb->lock);
231 mutex_unlock(&disconnect_mutex);
235 c->vc_can_do_color = 1;
237 c->vc_complement_mask = 0x7700;
239 c->vc_hi_font_mask = sisusb->current_font_512 ? 0x0800 : 0;
241 sisusb->haveconsole = 1;
243 sisusb->havethisconsole[c->vc_num] = 1;
245 /* We only support 640x400 */
246 c->vc_scan_lines = 400;
248 c->vc_font.height = sisusb->current_font_height;
250 /* We only support width = 8 */
252 rows = c->vc_scan_lines / c->vc_font.height;
254 /* Increment usage count for our sisusb.
255 * Doing so saves us from upping/downing
256 * the disconnect semaphore; we can't
257 * lose our sisusb until this is undone
258 * in con_deinit. For all other console
259 * interface functions, it suffices to
260 * use sisusb->lock and do a quick check
261 * of sisusb for device disconnection.
263 kref_get(&sisusb->kref);
265 if (!*c->vc_uni_pagedir_loc)
266 con_set_default_unimap(c);
268 mutex_unlock(&sisusb->lock);
270 mutex_unlock(&disconnect_mutex);
276 vc_resize(c, cols, rows);
279 /* con_deinit console interface routine */
281 sisusbcon_deinit(struct vc_data *c)
283 struct sisusb_usb_data *sisusb;
286 /* This is called by take_over_console()
287 * and others, ie not under our control.
290 mutex_lock(&disconnect_mutex);
292 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
293 mutex_unlock(&disconnect_mutex);
297 mutex_lock(&sisusb->lock);
299 /* Clear ourselves in mysisusbs */
300 mysisusbs[c->vc_num] = NULL;
302 sisusb->havethisconsole[c->vc_num] = 0;
304 /* Free our font buffer if all consoles are gone */
305 if (sisusb->font_backup) {
306 for(i = 0; i < MAX_NR_CONSOLES; i++) {
307 if (sisusb->havethisconsole[c->vc_num])
310 if (i == MAX_NR_CONSOLES) {
311 vfree(sisusb->font_backup);
312 sisusb->font_backup = NULL;
316 mutex_unlock(&sisusb->lock);
318 /* decrement the usage count on our sisusb */
319 kref_put(&sisusb->kref, sisusb_delete);
321 mutex_unlock(&disconnect_mutex);
324 /* interface routine */
326 sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
327 u8 blink, u8 underline, u8 reverse)
332 attr = (attr & 0xf0) | c->vc_ulcolor;
333 else if (intensity == 0)
334 attr = (attr & 0xf0) | c->vc_halfcolor;
337 attr = ((attr) & 0x88) |
339 ((attr) << 4)) & 0x77);
350 /* Interface routine */
352 sisusbcon_invert_region(struct vc_data *vc, u16 *p, int count)
354 /* Invert a region. This is called with a pointer
355 * to the console's internal screen buffer. So we
356 * simply do the inversion there and rely on
357 * a call to putc(s) to update the real screen.
361 u16 a = sisusbcon_readw(p);
364 (((a) & 0x7000) >> 4) |
365 (((a) & 0x0700) << 4);
367 sisusbcon_writew(a, p++);
371 #define SISUSB_VADDR(x,y) \
372 ((u16 *)c->vc_origin + \
373 (y) * sisusb->sisusb_num_columns + \
376 #define SISUSB_HADDR(x,y) \
377 ((u16 *)(sisusb->vrambase + (c->vc_origin - sisusb->scrbuf)) + \
378 (y) * sisusb->sisusb_num_columns + \
381 /* Interface routine */
383 sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
385 struct sisusb_usb_data *sisusb;
388 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
391 /* sisusb->lock is down */
393 /* Don't need to put the character into buffer ourselves,
394 * because the vt does this BEFORE calling us.
397 sisusbcon_writew(ch, SISUSB_VADDR(x, y));
400 if (sisusb_is_inactive(c, sisusb)) {
401 mutex_unlock(&sisusb->lock);
406 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
407 (u32)SISUSB_HADDR(x, y), 2, &written);
409 mutex_unlock(&sisusb->lock);
412 /* Interface routine */
414 sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
415 int count, int y, int x)
417 struct sisusb_usb_data *sisusb;
422 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
425 /* sisusb->lock is down */
427 /* Need to put the characters into the buffer ourselves,
428 * because the vt does this AFTER calling us.
431 dest = SISUSB_VADDR(x, y);
433 for (i = count; i > 0; i--)
434 sisusbcon_writew(sisusbcon_readw(s++), dest++);
436 if (sisusb_is_inactive(c, sisusb)) {
437 mutex_unlock(&sisusb->lock);
441 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
442 (u32)SISUSB_HADDR(x, y), count * 2, &written);
444 mutex_unlock(&sisusb->lock);
447 /* Interface routine */
449 sisusbcon_clear(struct vc_data *c, int y, int x, int height, int width)
451 struct sisusb_usb_data *sisusb;
452 u16 eattr = c->vc_video_erase_char;
457 if (width <= 0 || height <= 0)
460 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
463 /* sisusb->lock is down */
465 /* Need to clear buffer ourselves, because the vt does
466 * this AFTER calling us.
469 dest = SISUSB_VADDR(x, y);
471 cols = sisusb->sisusb_num_columns;
476 if (x == 0 && width >= c->vc_cols) {
478 sisusbcon_memsetw(dest, eattr, height * cols * 2);
482 for (i = height; i > 0; i--, dest += cols)
483 sisusbcon_memsetw(dest, eattr, width * 2);
487 if (sisusb_is_inactive(c, sisusb)) {
488 mutex_unlock(&sisusb->lock);
492 length = ((height * cols) - x - (cols - width - x)) * 2;
495 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(x, y),
496 (u32)SISUSB_HADDR(x, y), length, &written);
498 mutex_unlock(&sisusb->lock);
501 /* Interface routine */
503 sisusbcon_bmove(struct vc_data *c, int sy, int sx,
504 int dy, int dx, int height, int width)
506 struct sisusb_usb_data *sisusb;
514 if (width <= 0 || height <= 0)
517 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
520 /* sisusb->lock is down */
522 cols = sisusb->sisusb_num_columns;
524 /* Don't need to move data outselves, because
525 * vt does this BEFORE calling us.
526 * This is only used by vt's insert/deletechar.
529 if (sx == 0 && dx == 0 && width >= c->vc_cols && width <= cols) {
531 sisusbcon_memmovew(SISUSB_VADDR(0, dy), SISUSB_VADDR(0, sy),
534 } else if (dy < sy || (dy == sy && dx < sx)) {
536 src = SISUSB_VADDR(sx, sy);
537 dest = SISUSB_VADDR(dx, dy);
539 for (i = height; i > 0; i--) {
540 sisusbcon_memmovew(dest, src, width * 2);
547 src = SISUSB_VADDR(sx, sy + height - 1);
548 dest = SISUSB_VADDR(dx, dy + height - 1);
550 for (i = height; i > 0; i--) {
551 sisusbcon_memmovew(dest, src, width * 2);
559 if (sisusb_is_inactive(c, sisusb)) {
560 mutex_unlock(&sisusb->lock);
564 length = ((height * cols) - dx - (cols - width - dx)) * 2;
567 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(dx, dy),
568 (u32)SISUSB_HADDR(dx, dy), length, &written);
570 mutex_unlock(&sisusb->lock);
573 /* interface routine */
575 sisusbcon_switch(struct vc_data *c)
577 struct sisusb_usb_data *sisusb;
581 /* Returnvalue 0 means we have fully restored screen,
582 * and vt doesn't need to call do_update_region().
583 * Returnvalue != 0 naturally means the opposite.
586 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
589 /* sisusb->lock is down */
591 /* Don't write to screen if in gfx mode */
592 if (sisusb_is_inactive(c, sisusb)) {
593 mutex_unlock(&sisusb->lock);
597 /* That really should not happen. It would mean we are
598 * being called while the vc is using its private buffer
601 if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
602 mutex_unlock(&sisusb->lock);
603 printk(KERN_DEBUG "sisusb: ASSERT ORIGIN != SCREENBUF!\n");
607 /* Check that we don't copy too much */
608 length = min((int)c->vc_screenbuf_size,
609 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
611 /* Restore the screen contents */
612 sisusbcon_memcpyw((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf,
615 sisusb_copy_memory(sisusb, (unsigned char *)c->vc_origin,
616 (u32)SISUSB_HADDR(0, 0),
619 mutex_unlock(&sisusb->lock);
624 /* interface routine */
626 sisusbcon_save_screen(struct vc_data *c)
628 struct sisusb_usb_data *sisusb;
631 /* Save the current screen contents to vc's private
635 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
638 /* sisusb->lock is down */
640 if (sisusb_is_inactive(c, sisusb)) {
641 mutex_unlock(&sisusb->lock);
645 /* Check that we don't copy too much */
646 length = min((int)c->vc_screenbuf_size,
647 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
649 /* Save the screen contents to vc's private buffer */
650 sisusbcon_memcpyw((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin,
653 mutex_unlock(&sisusb->lock);
656 /* interface routine */
658 sisusbcon_set_palette(struct vc_data *c, unsigned char *table)
660 struct sisusb_usb_data *sisusb;
663 /* Return value not used by vt */
665 if (!CON_IS_VISIBLE(c))
668 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
671 /* sisusb->lock is down */
673 if (sisusb_is_inactive(c, sisusb)) {
674 mutex_unlock(&sisusb->lock);
678 for (i = j = 0; i < 16; i++) {
679 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
681 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
683 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
685 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
689 mutex_unlock(&sisusb->lock);
694 /* interface routine */
696 sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
698 struct sisusb_usb_data *sisusb;
699 u8 sr1, cr17, pmreg, cr63;
703 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
706 /* sisusb->lock is down */
709 sisusb->is_gfx = blank ? 1 : 0;
711 if (sisusb_is_inactive(c, sisusb)) {
712 mutex_unlock(&sisusb->lock);
718 case 1: /* Normal blanking: Clear screen */
720 sisusbcon_memsetw((u16 *)c->vc_origin,
721 c->vc_video_erase_char,
722 c->vc_screenbuf_size);
723 sisusb_copy_memory(sisusb,
724 (unsigned char *)c->vc_origin,
725 (u32)(sisusb->vrambase +
726 (c->vc_origin - sisusb->scrbuf)),
727 c->vc_screenbuf_size, &written);
728 sisusb->con_blanked = 1;
732 default: /* VESA blanking */
734 case 0: /* Unblank */
740 sisusb->con_blanked = 0;
742 case VESA_VSYNC_SUSPEND + 1:
748 case VESA_HSYNC_SUSPEND + 1:
754 case VESA_POWERDOWN + 1:
761 mutex_unlock(&sisusb->lock);
765 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
766 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
767 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
768 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
772 mutex_unlock(&sisusb->lock);
777 /* interface routine */
779 sisusbcon_scrolldelta(struct vc_data *c, int lines)
781 struct sisusb_usb_data *sisusb;
782 int margin = c->vc_size_row * 4;
785 /* The return value does not seem to be used */
787 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
790 /* sisusb->lock is down */
792 if (sisusb_is_inactive(c, sisusb)) {
793 mutex_unlock(&sisusb->lock);
797 if (!lines) /* Turn scrollback off */
798 c->vc_visible_origin = c->vc_origin;
801 if (sisusb->con_rolled_over >
802 (c->vc_scr_end - sisusb->scrbuf) + margin) {
804 ul = c->vc_scr_end - sisusb->scrbuf;
805 we = sisusb->con_rolled_over + c->vc_size_row;
810 we = sisusb->scrbuf_size;
814 p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we +
815 lines * c->vc_size_row;
817 st = (c->vc_origin - sisusb->scrbuf - ul + we) % we;
828 c->vc_visible_origin = sisusb->scrbuf + (p + ul) % we;
831 sisusbcon_set_start_address(sisusb, c);
833 mutex_unlock(&sisusb->lock);
838 /* Interface routine */
840 sisusbcon_cursor(struct vc_data *c, int mode)
842 struct sisusb_usb_data *sisusb;
843 int from, to, baseline;
845 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
848 /* sisusb->lock is down */
850 if (sisusb_is_inactive(c, sisusb)) {
851 mutex_unlock(&sisusb->lock);
855 if (c->vc_origin != c->vc_visible_origin) {
856 c->vc_visible_origin = c->vc_origin;
857 sisusbcon_set_start_address(sisusb, c);
860 if (mode == CM_ERASE) {
861 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
862 sisusb->sisusb_cursor_size_to = -1;
863 mutex_unlock(&sisusb->lock);
867 sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
869 baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
871 switch (c->vc_cursor_type & 0x0f) {
872 case CUR_BLOCK: from = 1;
873 to = c->vc_font.height;
875 case CUR_TWO_THIRDS: from = c->vc_font.height / 3;
878 case CUR_LOWER_HALF: from = c->vc_font.height / 2;
881 case CUR_LOWER_THIRD: from = (c->vc_font.height * 2) / 3;
884 case CUR_NONE: from = 31;
888 case CUR_UNDERLINE: from = baseline - 1;
893 if (sisusb->sisusb_cursor_size_from != from ||
894 sisusb->sisusb_cursor_size_to != to) {
896 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
897 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
899 sisusb->sisusb_cursor_size_from = from;
900 sisusb->sisusb_cursor_size_to = to;
903 mutex_unlock(&sisusb->lock);
907 sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
908 int t, int b, int dir, int lines)
910 int cols = sisusb->sisusb_num_columns;
911 int length = ((b - t) * cols) * 2;
912 u16 eattr = c->vc_video_erase_char;
915 /* sisusb->lock is down */
917 /* Scroll an area which does not match the
918 * visible screen's dimensions. This needs
919 * to be done separately, as it does not
920 * use hardware panning.
926 sisusbcon_memmovew(SISUSB_VADDR(0, t),
927 SISUSB_VADDR(0, t + lines),
928 (b - t - lines) * cols * 2);
929 sisusbcon_memsetw(SISUSB_VADDR(0, b - lines), eattr,
934 sisusbcon_memmovew(SISUSB_VADDR(0, t + lines),
936 (b - t - lines) * cols * 2);
937 sisusbcon_memsetw(SISUSB_VADDR(0, t), eattr,
942 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(0, t),
943 (u32)SISUSB_HADDR(0, t), length, &written);
945 mutex_unlock(&sisusb->lock);
950 /* Interface routine */
952 sisusbcon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
954 struct sisusb_usb_data *sisusb;
955 u16 eattr = c->vc_video_erase_char;
958 unsigned long oldorigin;
959 unsigned int delta = lines * c->vc_size_row;
962 /* Returning != 0 means we have done the scrolling successfully.
963 * Returning 0 makes vt do the scrolling on its own.
964 * Note that con_scroll is only called if the console is
965 * visible. In that case, the origin should be our buffer,
966 * not the vt's private one.
972 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
975 /* sisusb->lock is down */
977 if (sisusb_is_inactive(c, sisusb)) {
978 mutex_unlock(&sisusb->lock);
983 if (t || b != c->vc_rows)
984 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
986 if (c->vc_origin != c->vc_visible_origin) {
987 c->vc_visible_origin = c->vc_origin;
988 sisusbcon_set_start_address(sisusb, c);
991 /* limit amount to maximum realistic size */
992 if (lines > c->vc_rows)
995 oldorigin = c->vc_origin;
1001 if (c->vc_scr_end + delta >=
1002 sisusb->scrbuf + sisusb->scrbuf_size) {
1003 sisusbcon_memcpyw((u16 *)sisusb->scrbuf,
1004 (u16 *)(oldorigin + delta),
1005 c->vc_screenbuf_size - delta);
1006 c->vc_origin = sisusb->scrbuf;
1007 sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
1010 c->vc_origin += delta;
1013 (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
1020 if (oldorigin - delta < sisusb->scrbuf) {
1021 sisusbcon_memmovew((u16 *)(sisusb->scrbuf +
1022 sisusb->scrbuf_size -
1023 c->vc_screenbuf_size +
1026 c->vc_screenbuf_size - delta);
1027 c->vc_origin = sisusb->scrbuf +
1028 sisusb->scrbuf_size -
1029 c->vc_screenbuf_size;
1030 sisusb->con_rolled_over = 0;
1033 c->vc_origin -= delta;
1035 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1037 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
1042 originoffset = (u32)(c->vc_origin - sisusb->scrbuf);
1045 sisusb_copy_memory(sisusb,
1046 (char *)c->vc_origin,
1047 (u32)(sisusb->vrambase + originoffset),
1048 c->vc_screenbuf_size, &written);
1049 else if (dir == SM_UP)
1050 sisusb_copy_memory(sisusb,
1051 (char *)c->vc_origin + c->vc_screenbuf_size - delta,
1052 (u32)sisusb->vrambase + originoffset +
1053 c->vc_screenbuf_size - delta,
1056 sisusb_copy_memory(sisusb,
1057 (char *)c->vc_origin,
1058 (u32)(sisusb->vrambase + originoffset),
1061 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1062 c->vc_visible_origin = c->vc_origin;
1064 sisusbcon_set_start_address(sisusb, c);
1066 c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
1068 mutex_unlock(&sisusb->lock);
1073 /* Interface routine */
1075 sisusbcon_set_origin(struct vc_data *c)
1077 struct sisusb_usb_data *sisusb;
1079 /* Returning != 0 means we were successful.
1080 * Returning 0 will vt make to use its own
1081 * screenbuffer as the origin.
1084 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1087 /* sisusb->lock is down */
1089 if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
1090 mutex_unlock(&sisusb->lock);
1094 c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
1096 sisusbcon_set_start_address(sisusb, c);
1098 sisusb->con_rolled_over = 0;
1100 mutex_unlock(&sisusb->lock);
1105 /* Interface routine */
1107 sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows)
1109 struct sisusb_usb_data *sisusb;
1112 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1115 fh = sisusb->current_font_height;
1117 mutex_unlock(&sisusb->lock);
1119 /* We are quite unflexible as regards resizing. The vt code
1120 * handles sizes where the line length isn't equal the pitch
1121 * quite badly. As regards the rows, our panning tricks only
1122 * work well if the number of rows equals the visible number
1126 if (newcols != 80 || c->vc_scan_lines / fh != newrows)
1133 sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
1134 u8 *arg, int cmapsz, int ch512, int dorecalc,
1135 struct vc_data *c, int fh, int uplock)
1137 int font_select = 0x00, i, err = 0;
1141 /* sisusb->lock is down */
1144 * The default font is kept in slot 0.
1145 * A user font is loaded in slot 2 (256 ch)
1149 if ((slot != 0 && slot != 2) || !fh) {
1151 mutex_unlock(&sisusb->lock);
1156 sisusb->font_slot = slot;
1158 /* Default font is always 256 */
1162 offset = 4 * cmapsz;
1164 font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1166 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1167 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1168 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1169 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1174 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1175 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1176 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1183 for (i = 0; i < cmapsz; i++) {
1184 err |= sisusb_writeb(sisusb,
1185 sisusb->vrambase + offset + i,
1191 for (i = 0; i < cmapsz; i++) {
1192 err |= sisusb_readb(sisusb,
1193 sisusb->vrambase + offset + i,
1200 * In 512-character mode, the character map is not contiguous if
1201 * we want to remain EGA compatible -- which we do
1206 for (i = 0; i < cmapsz; i++) {
1207 err |= sisusb_writeb(sisusb,
1208 sisusb->vrambase + offset +
1215 for (i = 0; i < cmapsz; i++) {
1216 err |= sisusb_readb(sisusb,
1217 sisusb->vrambase + offset +
1229 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1230 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1231 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1233 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1234 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1239 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1240 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1241 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1246 if ((set) && (ch512 != sisusb->current_font_512)) {
1248 /* Font is shared among all our consoles.
1249 * And so is the hi_font_mask.
1251 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1252 struct vc_data *c = vc_cons[i].d;
1253 if (c && c->vc_sw == &sisusb_con)
1254 c->vc_hi_font_mask = ch512 ? 0x0800 : 0;
1257 sisusb->current_font_512 = ch512;
1259 /* color plane enable register:
1260 256-char: enable intensity bit
1261 512-char: disable intensity bit */
1262 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1263 sisusb_setreg(sisusb, SISAR, 0x12);
1264 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1266 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1267 sisusb_setreg(sisusb, SISAR, 0x20);
1268 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1274 * Adjust the screen to fit a font of a certain height
1277 unsigned char ovr, vde, fsr;
1278 int rows = 0, maxscan = 0;
1282 /* Number of video rows */
1283 rows = c->vc_scan_lines / fh;
1284 /* Scan lines to actually display-1 */
1285 maxscan = rows * fh - 1;
1287 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1288 rows, maxscan, fh, c->vc_scan_lines);*/
1290 sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1291 vde = maxscan & 0xff;
1292 ovr = (ovr & 0xbd) |
1293 ((maxscan & 0x100) >> 7) |
1294 ((maxscan & 0x200) >> 3);
1295 sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1296 sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1300 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1301 fsr = (fsr & 0xe0) | (fh - 1);
1302 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1303 sisusb->current_font_height = fh;
1305 sisusb->sisusb_cursor_size_from = -1;
1306 sisusb->sisusb_cursor_size_to = -1;
1311 mutex_unlock(&sisusb->lock);
1313 if (dorecalc && c) {
1314 int i, rows = c->vc_scan_lines / fh;
1316 /* Now adjust our consoles' size */
1318 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1319 struct vc_data *vc = vc_cons[i].d;
1321 if (vc && vc->vc_sw == &sisusb_con) {
1322 if (CON_IS_VISIBLE(vc)) {
1323 vc->vc_sw->con_cursor(vc, CM_DRAW);
1325 vc->vc_font.height = fh;
1326 vc_resize(vc, 0, rows);
1335 mutex_unlock(&sisusb->lock);
1340 /* Interface routine */
1342 sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1345 struct sisusb_usb_data *sisusb;
1346 unsigned charcount = font->charcount;
1348 if (font->width != 8 || (charcount != 256 && charcount != 512))
1351 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1354 /* sisusb->lock is down */
1356 /* Save the user-provided font into a buffer. This
1357 * is used for restoring text mode after quitting
1358 * from X and for the con_getfont routine.
1360 if (sisusb->font_backup) {
1361 if (sisusb->font_backup_size < charcount) {
1362 vfree(sisusb->font_backup);
1363 sisusb->font_backup = NULL;
1367 if (!sisusb->font_backup)
1368 sisusb->font_backup = vmalloc(charcount * 32);
1370 if (sisusb->font_backup) {
1371 memcpy(sisusb->font_backup, font->data, charcount * 32);
1372 sisusb->font_backup_size = charcount;
1373 sisusb->font_backup_height = font->height;
1374 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1377 /* do_font_op ups sisusb->lock */
1379 return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1380 8192, (charcount == 512),
1381 (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1382 c, font->height, 1);
1385 /* Interface routine */
1387 sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1389 struct sisusb_usb_data *sisusb;
1391 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1394 /* sisusb->lock is down */
1397 font->height = c->vc_font.height;
1398 font->charcount = 256;
1401 mutex_unlock(&sisusb->lock);
1405 if (!sisusb->font_backup) {
1406 mutex_unlock(&sisusb->lock);
1410 /* Copy 256 chars only, like vgacon */
1411 memcpy(font->data, sisusb->font_backup, 256 * 32);
1413 mutex_unlock(&sisusb->lock);
1419 * The console `switch' structure for the sisusb console
1422 static const struct consw sisusb_con = {
1423 .owner = THIS_MODULE,
1424 .con_startup = sisusbcon_startup,
1425 .con_init = sisusbcon_init,
1426 .con_deinit = sisusbcon_deinit,
1427 .con_clear = sisusbcon_clear,
1428 .con_putc = sisusbcon_putc,
1429 .con_putcs = sisusbcon_putcs,
1430 .con_cursor = sisusbcon_cursor,
1431 .con_scroll = sisusbcon_scroll,
1432 .con_bmove = sisusbcon_bmove,
1433 .con_switch = sisusbcon_switch,
1434 .con_blank = sisusbcon_blank,
1435 .con_font_set = sisusbcon_font_set,
1436 .con_font_get = sisusbcon_font_get,
1437 .con_set_palette = sisusbcon_set_palette,
1438 .con_scrolldelta = sisusbcon_scrolldelta,
1439 .con_build_attr = sisusbcon_build_attr,
1440 .con_invert_region = sisusbcon_invert_region,
1441 .con_set_origin = sisusbcon_set_origin,
1442 .con_save_screen = sisusbcon_save_screen,
1443 .con_resize = sisusbcon_resize,
1446 /* Our very own dummy console driver */
1448 static const char *sisusbdummycon_startup(void)
1450 return "SISUSBVGADUMMY";
1453 static void sisusbdummycon_init(struct vc_data *vc, int init)
1455 vc->vc_can_do_color = 1;
1460 vc_resize(vc, 80, 25);
1463 static int sisusbdummycon_dummy(void)
1468 #define SISUSBCONDUMMY (void *)sisusbdummycon_dummy
1470 static const struct consw sisusb_dummy_con = {
1471 .owner = THIS_MODULE,
1472 .con_startup = sisusbdummycon_startup,
1473 .con_init = sisusbdummycon_init,
1474 .con_deinit = SISUSBCONDUMMY,
1475 .con_clear = SISUSBCONDUMMY,
1476 .con_putc = SISUSBCONDUMMY,
1477 .con_putcs = SISUSBCONDUMMY,
1478 .con_cursor = SISUSBCONDUMMY,
1479 .con_scroll = SISUSBCONDUMMY,
1480 .con_bmove = SISUSBCONDUMMY,
1481 .con_switch = SISUSBCONDUMMY,
1482 .con_blank = SISUSBCONDUMMY,
1483 .con_font_set = SISUSBCONDUMMY,
1484 .con_font_get = SISUSBCONDUMMY,
1485 .con_font_default = SISUSBCONDUMMY,
1486 .con_font_copy = SISUSBCONDUMMY,
1487 .con_set_palette = SISUSBCONDUMMY,
1488 .con_scrolldelta = SISUSBCONDUMMY,
1492 sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1494 int i, ret, minor = sisusb->minor;
1496 mutex_lock(&disconnect_mutex);
1498 mutex_lock(&sisusb->lock);
1500 /* Erm.. that should not happen */
1501 if (sisusb->haveconsole || !sisusb->SiS_Pr) {
1502 mutex_unlock(&sisusb->lock);
1503 mutex_unlock(&disconnect_mutex);
1507 sisusb->con_first = first;
1508 sisusb->con_last = last;
1511 first > MAX_NR_CONSOLES ||
1512 last > MAX_NR_CONSOLES) {
1513 mutex_unlock(&sisusb->lock);
1514 mutex_unlock(&disconnect_mutex);
1518 /* If gfxcore not initialized or no consoles given, quit graciously */
1519 if (!sisusb->gfxinit || first < 1 || last < 1) {
1520 mutex_unlock(&sisusb->lock);
1521 mutex_unlock(&disconnect_mutex);
1525 sisusb->sisusb_cursor_loc = -1;
1526 sisusb->sisusb_cursor_size_from = -1;
1527 sisusb->sisusb_cursor_size_to = -1;
1529 /* Set up text mode (and upload default font) */
1530 if (sisusb_reset_text_mode(sisusb, 1)) {
1531 mutex_unlock(&sisusb->lock);
1532 mutex_unlock(&disconnect_mutex);
1534 "sisusbvga[%d]: Failed to set up text mode\n",
1539 /* Initialize some gfx registers */
1540 sisusb_initialize(sisusb);
1542 for (i = first - 1; i <= last - 1; i++) {
1543 /* Save sisusb for our interface routines */
1544 mysisusbs[i] = sisusb;
1547 /* Initial console setup */
1548 sisusb->sisusb_num_columns = 80;
1550 /* Use a 32K buffer (matches b8000-bffff area) */
1551 sisusb->scrbuf_size = 32 * 1024;
1553 /* Allocate screen buffer */
1554 if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
1555 mutex_unlock(&sisusb->lock);
1556 mutex_unlock(&disconnect_mutex);
1558 "sisusbvga[%d]: Failed to allocate screen buffer\n",
1563 mutex_unlock(&sisusb->lock);
1564 mutex_unlock(&disconnect_mutex);
1566 /* Now grab the desired console(s) */
1567 ret = take_over_console(&sisusb_con, first - 1, last - 1, 0);
1570 sisusb->haveconsole = 1;
1572 for (i = first - 1; i <= last - 1; i++)
1573 mysisusbs[i] = NULL;
1580 sisusb_console_exit(struct sisusb_usb_data *sisusb)
1584 /* This is called if the device is disconnected
1585 * and while disconnect and lock semaphores
1586 * are up. This should be save because we
1587 * can't lose our sisusb any other way but by
1588 * disconnection (and hence, the disconnect
1589 * sema is for protecting all other access
1590 * functions from disconnection, not the
1594 /* Now what do we do in case of disconnection:
1595 * One alternative would be to simply call
1596 * give_up_console(). Nah, not a good idea.
1597 * give_up_console() is obviously buggy as it
1598 * only discards the consw pointer from the
1599 * driver_map, but doesn't adapt vc->vc_sw
1600 * of the affected consoles. Hence, the next
1601 * call to any of the console functions will
1602 * eventually take a trip to oops county.
1603 * Also, give_up_console for some reason
1604 * doesn't decrement our module refcount.
1605 * Instead, we switch our consoles to a private
1606 * dummy console. This, of course, keeps our
1607 * refcount up as well, but it works perfectly.
1610 if (sisusb->haveconsole) {
1611 for (i = 0; i < MAX_NR_CONSOLES; i++)
1612 if (sisusb->havethisconsole[i])
1613 take_over_console(&sisusb_dummy_con, i, i, 0);
1614 /* At this point, con_deinit for all our
1615 * consoles is executed by take_over_console().
1617 sisusb->haveconsole = 0;
1620 vfree((void *)sisusb->scrbuf);
1623 vfree(sisusb->font_backup);
1624 sisusb->font_backup = NULL;
1627 void __init sisusb_init_concode(void)
1631 for (i = 0; i < MAX_NR_CONSOLES; i++)
1632 mysisusbs[i] = NULL;
1635 #endif /* INCL_CON */