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/ioport.h>
66 #include <linux/interrupt.h>
67 #include <linux/vmalloc.h>
70 #include "sisusb_init.h"
72 #ifdef INCL_SISUSB_CON
74 #define sisusbcon_writew(val, addr) (*(addr) = (val))
75 #define sisusbcon_readw(addr) (*(addr))
76 #define sisusbcon_memmovew(d, s, c) memmove(d, s, c)
77 #define sisusbcon_memcpyw(d, s, c) memcpy(d, s, c)
79 /* vc_data -> sisusb conversion table */
80 static struct sisusb_usb_data *mysisusbs[MAX_NR_CONSOLES];
82 /* Forward declaration */
83 static const struct consw sisusb_con;
86 sisusbcon_memsetw(u16 *s, u16 c, unsigned int count)
90 sisusbcon_writew(c, s++);
94 sisusb_initialize(struct sisusb_usb_data *sisusb)
96 /* Reset cursor and start address */
97 if (sisusb_setidxreg(sisusb, SISCR, 0x0c, 0x00))
99 if (sisusb_setidxreg(sisusb, SISCR, 0x0d, 0x00))
101 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, 0x00))
103 sisusb_setidxreg(sisusb, SISCR, 0x0f, 0x00);
107 sisusbcon_set_start_address(struct sisusb_usb_data *sisusb, struct vc_data *c)
109 sisusb->cur_start_addr = (c->vc_visible_origin - sisusb->scrbuf) / 2;
111 sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8));
112 sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));
116 sisusb_set_cursor(struct sisusb_usb_data *sisusb, unsigned int location)
118 if (sisusb->sisusb_cursor_loc == location)
121 sisusb->sisusb_cursor_loc = location;
123 /* Hardware bug: Text cursor appears twice or not at all
124 * at some positions. Work around it with the cursor skew
128 if ((location & 0x0007) == 0x0007) {
129 sisusb->bad_cursor_pos = 1;
131 if (sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0x1f, 0x20))
133 } else if (sisusb->bad_cursor_pos) {
134 if (sisusb_setidxregand(sisusb, SISCR, 0x0b, 0x1f))
136 sisusb->bad_cursor_pos = 0;
139 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, (location >> 8)))
141 sisusb_setidxreg(sisusb, SISCR, 0x0f, (location & 0xff));
144 static inline struct sisusb_usb_data *
145 sisusb_get_sisusb(unsigned short console)
147 return mysisusbs[console];
151 sisusb_sisusb_valid(struct sisusb_usb_data *sisusb)
153 if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev)
159 static struct sisusb_usb_data *
160 sisusb_get_sisusb_lock_and_check(unsigned short console)
162 struct sisusb_usb_data *sisusb;
164 /* We can't handle console calls in non-schedulable
165 * context due to our locks and the USB transport.
166 * So we simply ignore them. This should only affect
167 * some calls to printk.
172 if (!(sisusb = sisusb_get_sisusb(console)))
175 mutex_lock(&sisusb->lock);
177 if (!sisusb_sisusb_valid(sisusb) ||
178 !sisusb->havethisconsole[console]) {
179 mutex_unlock(&sisusb->lock);
187 sisusb_is_inactive(struct vc_data *c, struct sisusb_usb_data *sisusb)
189 if (sisusb->is_gfx ||
190 sisusb->textmodedestroyed ||
191 c->vc_mode != KD_TEXT)
197 /* con_startup console interface routine */
199 sisusbcon_startup(void)
204 /* con_init console interface routine */
206 sisusbcon_init(struct vc_data *c, int init)
208 struct sisusb_usb_data *sisusb;
211 /* This is called by take_over_console(),
212 * ie by us/under our control. It is
213 * only called after text mode and fonts
214 * are set up/restored.
217 mutex_lock(&disconnect_mutex);
219 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
220 mutex_unlock(&disconnect_mutex);
224 mutex_lock(&sisusb->lock);
226 if (!sisusb_sisusb_valid(sisusb)) {
227 mutex_unlock(&sisusb->lock);
228 mutex_unlock(&disconnect_mutex);
232 c->vc_can_do_color = 1;
234 c->vc_complement_mask = 0x7700;
236 c->vc_hi_font_mask = sisusb->current_font_512 ? 0x0800 : 0;
238 sisusb->haveconsole = 1;
240 sisusb->havethisconsole[c->vc_num] = 1;
242 /* We only support 640x400 */
243 c->vc_scan_lines = 400;
245 c->vc_font.height = sisusb->current_font_height;
247 /* We only support width = 8 */
249 rows = c->vc_scan_lines / c->vc_font.height;
251 /* Increment usage count for our sisusb.
252 * Doing so saves us from upping/downing
253 * the disconnect semaphore; we can't
254 * lose our sisusb until this is undone
255 * in con_deinit. For all other console
256 * interface functions, it suffices to
257 * use sisusb->lock and do a quick check
258 * of sisusb for device disconnection.
260 kref_get(&sisusb->kref);
262 if (!*c->vc_uni_pagedir_loc)
263 con_set_default_unimap(c);
265 mutex_unlock(&sisusb->lock);
267 mutex_unlock(&disconnect_mutex);
273 vc_resize(c, cols, rows);
276 /* con_deinit console interface routine */
278 sisusbcon_deinit(struct vc_data *c)
280 struct sisusb_usb_data *sisusb;
283 /* This is called by take_over_console()
284 * and others, ie not under our control.
287 mutex_lock(&disconnect_mutex);
289 if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
290 mutex_unlock(&disconnect_mutex);
294 mutex_lock(&sisusb->lock);
296 /* Clear ourselves in mysisusbs */
297 mysisusbs[c->vc_num] = NULL;
299 sisusb->havethisconsole[c->vc_num] = 0;
301 /* Free our font buffer if all consoles are gone */
302 if (sisusb->font_backup) {
303 for(i = 0; i < MAX_NR_CONSOLES; i++) {
304 if (sisusb->havethisconsole[c->vc_num])
307 if (i == MAX_NR_CONSOLES) {
308 vfree(sisusb->font_backup);
309 sisusb->font_backup = NULL;
313 mutex_unlock(&sisusb->lock);
315 /* decrement the usage count on our sisusb */
316 kref_put(&sisusb->kref, sisusb_delete);
318 mutex_unlock(&disconnect_mutex);
321 /* interface routine */
323 sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
324 u8 blink, u8 underline, u8 reverse, u8 unused)
329 attr = (attr & 0xf0) | c->vc_ulcolor;
330 else if (intensity == 0)
331 attr = (attr & 0xf0) | c->vc_halfcolor;
334 attr = ((attr) & 0x88) |
336 ((attr) << 4)) & 0x77);
347 /* Interface routine */
349 sisusbcon_invert_region(struct vc_data *vc, u16 *p, int count)
351 /* Invert a region. This is called with a pointer
352 * to the console's internal screen buffer. So we
353 * simply do the inversion there and rely on
354 * a call to putc(s) to update the real screen.
358 u16 a = sisusbcon_readw(p);
361 (((a) & 0x7000) >> 4) |
362 (((a) & 0x0700) << 4);
364 sisusbcon_writew(a, p++);
368 #define SISUSB_VADDR(x,y) \
369 ((u16 *)c->vc_origin + \
370 (y) * sisusb->sisusb_num_columns + \
373 #define SISUSB_HADDR(x,y) \
374 ((u16 *)(sisusb->vrambase + (c->vc_origin - sisusb->scrbuf)) + \
375 (y) * sisusb->sisusb_num_columns + \
378 /* Interface routine */
380 sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
382 struct sisusb_usb_data *sisusb;
385 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
388 /* sisusb->lock is down */
390 /* Don't need to put the character into buffer ourselves,
391 * because the vt does this BEFORE calling us.
394 sisusbcon_writew(ch, SISUSB_VADDR(x, y));
397 if (sisusb_is_inactive(c, sisusb)) {
398 mutex_unlock(&sisusb->lock);
403 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
404 (long)SISUSB_HADDR(x, y), 2, &written);
406 mutex_unlock(&sisusb->lock);
409 /* Interface routine */
411 sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
412 int count, int y, int x)
414 struct sisusb_usb_data *sisusb;
419 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
422 /* sisusb->lock is down */
424 /* Need to put the characters into the buffer ourselves,
425 * because the vt does this AFTER calling us.
428 dest = SISUSB_VADDR(x, y);
430 for (i = count; i > 0; i--)
431 sisusbcon_writew(sisusbcon_readw(s++), dest++);
433 if (sisusb_is_inactive(c, sisusb)) {
434 mutex_unlock(&sisusb->lock);
438 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
439 (long)SISUSB_HADDR(x, y), count * 2, &written);
441 mutex_unlock(&sisusb->lock);
444 /* Interface routine */
446 sisusbcon_clear(struct vc_data *c, int y, int x, int height, int width)
448 struct sisusb_usb_data *sisusb;
449 u16 eattr = c->vc_video_erase_char;
454 if (width <= 0 || height <= 0)
457 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
460 /* sisusb->lock is down */
462 /* Need to clear buffer ourselves, because the vt does
463 * this AFTER calling us.
466 dest = SISUSB_VADDR(x, y);
468 cols = sisusb->sisusb_num_columns;
473 if (x == 0 && width >= c->vc_cols) {
475 sisusbcon_memsetw(dest, eattr, height * cols * 2);
479 for (i = height; i > 0; i--, dest += cols)
480 sisusbcon_memsetw(dest, eattr, width * 2);
484 if (sisusb_is_inactive(c, sisusb)) {
485 mutex_unlock(&sisusb->lock);
489 length = ((height * cols) - x - (cols - width - x)) * 2;
492 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(x, y),
493 (long)SISUSB_HADDR(x, y), length, &written);
495 mutex_unlock(&sisusb->lock);
498 /* Interface routine */
500 sisusbcon_bmove(struct vc_data *c, int sy, int sx,
501 int dy, int dx, int height, int width)
503 struct sisusb_usb_data *sisusb;
511 if (width <= 0 || height <= 0)
514 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
517 /* sisusb->lock is down */
519 cols = sisusb->sisusb_num_columns;
521 /* Don't need to move data outselves, because
522 * vt does this BEFORE calling us.
523 * This is only used by vt's insert/deletechar.
526 if (sx == 0 && dx == 0 && width >= c->vc_cols && width <= cols) {
528 sisusbcon_memmovew(SISUSB_VADDR(0, dy), SISUSB_VADDR(0, sy),
531 } else if (dy < sy || (dy == sy && dx < sx)) {
533 src = SISUSB_VADDR(sx, sy);
534 dest = SISUSB_VADDR(dx, dy);
536 for (i = height; i > 0; i--) {
537 sisusbcon_memmovew(dest, src, width * 2);
544 src = SISUSB_VADDR(sx, sy + height - 1);
545 dest = SISUSB_VADDR(dx, dy + height - 1);
547 for (i = height; i > 0; i--) {
548 sisusbcon_memmovew(dest, src, width * 2);
556 if (sisusb_is_inactive(c, sisusb)) {
557 mutex_unlock(&sisusb->lock);
561 length = ((height * cols) - dx - (cols - width - dx)) * 2;
564 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(dx, dy),
565 (long)SISUSB_HADDR(dx, dy), length, &written);
567 mutex_unlock(&sisusb->lock);
570 /* interface routine */
572 sisusbcon_switch(struct vc_data *c)
574 struct sisusb_usb_data *sisusb;
578 /* Returnvalue 0 means we have fully restored screen,
579 * and vt doesn't need to call do_update_region().
580 * Returnvalue != 0 naturally means the opposite.
583 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
586 /* sisusb->lock is down */
588 /* Don't write to screen if in gfx mode */
589 if (sisusb_is_inactive(c, sisusb)) {
590 mutex_unlock(&sisusb->lock);
594 /* That really should not happen. It would mean we are
595 * being called while the vc is using its private buffer
598 if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
599 mutex_unlock(&sisusb->lock);
600 printk(KERN_DEBUG "sisusb: ASSERT ORIGIN != SCREENBUF!\n");
604 /* Check that we don't copy too much */
605 length = min((int)c->vc_screenbuf_size,
606 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
608 /* Restore the screen contents */
609 sisusbcon_memcpyw((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf,
612 sisusb_copy_memory(sisusb, (unsigned char *)c->vc_origin,
613 (long)SISUSB_HADDR(0, 0),
616 mutex_unlock(&sisusb->lock);
621 /* interface routine */
623 sisusbcon_save_screen(struct vc_data *c)
625 struct sisusb_usb_data *sisusb;
628 /* Save the current screen contents to vc's private
632 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
635 /* sisusb->lock is down */
637 if (sisusb_is_inactive(c, sisusb)) {
638 mutex_unlock(&sisusb->lock);
642 /* Check that we don't copy too much */
643 length = min((int)c->vc_screenbuf_size,
644 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
646 /* Save the screen contents to vc's private buffer */
647 sisusbcon_memcpyw((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin,
650 mutex_unlock(&sisusb->lock);
653 /* interface routine */
655 sisusbcon_set_palette(struct vc_data *c, unsigned char *table)
657 struct sisusb_usb_data *sisusb;
660 /* Return value not used by vt */
662 if (!CON_IS_VISIBLE(c))
665 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
668 /* sisusb->lock is down */
670 if (sisusb_is_inactive(c, sisusb)) {
671 mutex_unlock(&sisusb->lock);
675 for (i = j = 0; i < 16; i++) {
676 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
678 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
680 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
682 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
686 mutex_unlock(&sisusb->lock);
691 /* interface routine */
693 sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
695 struct sisusb_usb_data *sisusb;
696 u8 sr1, cr17, pmreg, cr63;
700 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
703 /* sisusb->lock is down */
706 sisusb->is_gfx = blank ? 1 : 0;
708 if (sisusb_is_inactive(c, sisusb)) {
709 mutex_unlock(&sisusb->lock);
715 case 1: /* Normal blanking: Clear screen */
717 sisusbcon_memsetw((u16 *)c->vc_origin,
718 c->vc_video_erase_char,
719 c->vc_screenbuf_size);
720 sisusb_copy_memory(sisusb,
721 (unsigned char *)c->vc_origin,
722 (u32)(sisusb->vrambase +
723 (c->vc_origin - sisusb->scrbuf)),
724 c->vc_screenbuf_size, &written);
725 sisusb->con_blanked = 1;
729 default: /* VESA blanking */
731 case 0: /* Unblank */
737 sisusb->con_blanked = 0;
739 case VESA_VSYNC_SUSPEND + 1:
745 case VESA_HSYNC_SUSPEND + 1:
751 case VESA_POWERDOWN + 1:
758 mutex_unlock(&sisusb->lock);
762 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
763 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
764 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
765 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
769 mutex_unlock(&sisusb->lock);
774 /* interface routine */
776 sisusbcon_scrolldelta(struct vc_data *c, int lines)
778 struct sisusb_usb_data *sisusb;
779 int margin = c->vc_size_row * 4;
782 /* The return value does not seem to be used */
784 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
787 /* sisusb->lock is down */
789 if (sisusb_is_inactive(c, sisusb)) {
790 mutex_unlock(&sisusb->lock);
794 if (!lines) /* Turn scrollback off */
795 c->vc_visible_origin = c->vc_origin;
798 if (sisusb->con_rolled_over >
799 (c->vc_scr_end - sisusb->scrbuf) + margin) {
801 ul = c->vc_scr_end - sisusb->scrbuf;
802 we = sisusb->con_rolled_over + c->vc_size_row;
807 we = sisusb->scrbuf_size;
811 p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we +
812 lines * c->vc_size_row;
814 st = (c->vc_origin - sisusb->scrbuf - ul + we) % we;
825 c->vc_visible_origin = sisusb->scrbuf + (p + ul) % we;
828 sisusbcon_set_start_address(sisusb, c);
830 mutex_unlock(&sisusb->lock);
835 /* Interface routine */
837 sisusbcon_cursor(struct vc_data *c, int mode)
839 struct sisusb_usb_data *sisusb;
840 int from, to, baseline;
842 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
845 /* sisusb->lock is down */
847 if (sisusb_is_inactive(c, sisusb)) {
848 mutex_unlock(&sisusb->lock);
852 if (c->vc_origin != c->vc_visible_origin) {
853 c->vc_visible_origin = c->vc_origin;
854 sisusbcon_set_start_address(sisusb, c);
857 if (mode == CM_ERASE) {
858 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
859 sisusb->sisusb_cursor_size_to = -1;
860 mutex_unlock(&sisusb->lock);
864 sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
866 baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
868 switch (c->vc_cursor_type & 0x0f) {
869 case CUR_BLOCK: from = 1;
870 to = c->vc_font.height;
872 case CUR_TWO_THIRDS: from = c->vc_font.height / 3;
875 case CUR_LOWER_HALF: from = c->vc_font.height / 2;
878 case CUR_LOWER_THIRD: from = (c->vc_font.height * 2) / 3;
881 case CUR_NONE: from = 31;
885 case CUR_UNDERLINE: from = baseline - 1;
890 if (sisusb->sisusb_cursor_size_from != from ||
891 sisusb->sisusb_cursor_size_to != to) {
893 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
894 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
896 sisusb->sisusb_cursor_size_from = from;
897 sisusb->sisusb_cursor_size_to = to;
900 mutex_unlock(&sisusb->lock);
904 sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
905 int t, int b, int dir, int lines)
907 int cols = sisusb->sisusb_num_columns;
908 int length = ((b - t) * cols) * 2;
909 u16 eattr = c->vc_video_erase_char;
912 /* sisusb->lock is down */
914 /* Scroll an area which does not match the
915 * visible screen's dimensions. This needs
916 * to be done separately, as it does not
917 * use hardware panning.
923 sisusbcon_memmovew(SISUSB_VADDR(0, t),
924 SISUSB_VADDR(0, t + lines),
925 (b - t - lines) * cols * 2);
926 sisusbcon_memsetw(SISUSB_VADDR(0, b - lines), eattr,
931 sisusbcon_memmovew(SISUSB_VADDR(0, t + lines),
933 (b - t - lines) * cols * 2);
934 sisusbcon_memsetw(SISUSB_VADDR(0, t), eattr,
939 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(0, t),
940 (long)SISUSB_HADDR(0, t), length, &written);
942 mutex_unlock(&sisusb->lock);
947 /* Interface routine */
949 sisusbcon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
951 struct sisusb_usb_data *sisusb;
952 u16 eattr = c->vc_video_erase_char;
955 unsigned long oldorigin;
956 unsigned int delta = lines * c->vc_size_row;
959 /* Returning != 0 means we have done the scrolling successfully.
960 * Returning 0 makes vt do the scrolling on its own.
961 * Note that con_scroll is only called if the console is
962 * visible. In that case, the origin should be our buffer,
963 * not the vt's private one.
969 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
972 /* sisusb->lock is down */
974 if (sisusb_is_inactive(c, sisusb)) {
975 mutex_unlock(&sisusb->lock);
980 if (t || b != c->vc_rows)
981 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
983 if (c->vc_origin != c->vc_visible_origin) {
984 c->vc_visible_origin = c->vc_origin;
985 sisusbcon_set_start_address(sisusb, c);
988 /* limit amount to maximum realistic size */
989 if (lines > c->vc_rows)
992 oldorigin = c->vc_origin;
998 if (c->vc_scr_end + delta >=
999 sisusb->scrbuf + sisusb->scrbuf_size) {
1000 sisusbcon_memcpyw((u16 *)sisusb->scrbuf,
1001 (u16 *)(oldorigin + delta),
1002 c->vc_screenbuf_size - delta);
1003 c->vc_origin = sisusb->scrbuf;
1004 sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
1007 c->vc_origin += delta;
1010 (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
1017 if (oldorigin - delta < sisusb->scrbuf) {
1018 sisusbcon_memmovew((u16 *)(sisusb->scrbuf +
1019 sisusb->scrbuf_size -
1020 c->vc_screenbuf_size +
1023 c->vc_screenbuf_size - delta);
1024 c->vc_origin = sisusb->scrbuf +
1025 sisusb->scrbuf_size -
1026 c->vc_screenbuf_size;
1027 sisusb->con_rolled_over = 0;
1030 c->vc_origin -= delta;
1032 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1034 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
1039 originoffset = (u32)(c->vc_origin - sisusb->scrbuf);
1042 sisusb_copy_memory(sisusb,
1043 (char *)c->vc_origin,
1044 (u32)(sisusb->vrambase + originoffset),
1045 c->vc_screenbuf_size, &written);
1046 else if (dir == SM_UP)
1047 sisusb_copy_memory(sisusb,
1048 (char *)c->vc_origin + c->vc_screenbuf_size - delta,
1049 (u32)sisusb->vrambase + originoffset +
1050 c->vc_screenbuf_size - delta,
1053 sisusb_copy_memory(sisusb,
1054 (char *)c->vc_origin,
1055 (u32)(sisusb->vrambase + originoffset),
1058 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1059 c->vc_visible_origin = c->vc_origin;
1061 sisusbcon_set_start_address(sisusb, c);
1063 c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
1065 mutex_unlock(&sisusb->lock);
1070 /* Interface routine */
1072 sisusbcon_set_origin(struct vc_data *c)
1074 struct sisusb_usb_data *sisusb;
1076 /* Returning != 0 means we were successful.
1077 * Returning 0 will vt make to use its own
1078 * screenbuffer as the origin.
1081 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1084 /* sisusb->lock is down */
1086 if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
1087 mutex_unlock(&sisusb->lock);
1091 c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
1093 sisusbcon_set_start_address(sisusb, c);
1095 sisusb->con_rolled_over = 0;
1097 mutex_unlock(&sisusb->lock);
1102 /* Interface routine */
1104 sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows)
1106 struct sisusb_usb_data *sisusb;
1109 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1112 fh = sisusb->current_font_height;
1114 mutex_unlock(&sisusb->lock);
1116 /* We are quite unflexible as regards resizing. The vt code
1117 * handles sizes where the line length isn't equal the pitch
1118 * quite badly. As regards the rows, our panning tricks only
1119 * work well if the number of rows equals the visible number
1123 if (newcols != 80 || c->vc_scan_lines / fh != newrows)
1130 sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
1131 u8 *arg, int cmapsz, int ch512, int dorecalc,
1132 struct vc_data *c, int fh, int uplock)
1134 int font_select = 0x00, i, err = 0;
1138 /* sisusb->lock is down */
1141 * The default font is kept in slot 0.
1142 * A user font is loaded in slot 2 (256 ch)
1146 if ((slot != 0 && slot != 2) || !fh) {
1148 mutex_unlock(&sisusb->lock);
1153 sisusb->font_slot = slot;
1155 /* Default font is always 256 */
1159 offset = 4 * cmapsz;
1161 font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1163 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1164 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1165 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1166 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1171 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1172 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1173 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1180 for (i = 0; i < cmapsz; i++) {
1181 err |= sisusb_writeb(sisusb,
1182 sisusb->vrambase + offset + i,
1188 for (i = 0; i < cmapsz; i++) {
1189 err |= sisusb_readb(sisusb,
1190 sisusb->vrambase + offset + i,
1197 * In 512-character mode, the character map is not contiguous if
1198 * we want to remain EGA compatible -- which we do
1203 for (i = 0; i < cmapsz; i++) {
1204 err |= sisusb_writeb(sisusb,
1205 sisusb->vrambase + offset +
1212 for (i = 0; i < cmapsz; i++) {
1213 err |= sisusb_readb(sisusb,
1214 sisusb->vrambase + offset +
1226 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1227 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1228 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1230 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1231 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1236 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1237 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1238 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1243 if ((set) && (ch512 != sisusb->current_font_512)) {
1245 /* Font is shared among all our consoles.
1246 * And so is the hi_font_mask.
1248 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1249 struct vc_data *c = vc_cons[i].d;
1250 if (c && c->vc_sw == &sisusb_con)
1251 c->vc_hi_font_mask = ch512 ? 0x0800 : 0;
1254 sisusb->current_font_512 = ch512;
1256 /* color plane enable register:
1257 256-char: enable intensity bit
1258 512-char: disable intensity bit */
1259 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1260 sisusb_setreg(sisusb, SISAR, 0x12);
1261 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1263 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1264 sisusb_setreg(sisusb, SISAR, 0x20);
1265 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1271 * Adjust the screen to fit a font of a certain height
1274 unsigned char ovr, vde, fsr;
1275 int rows = 0, maxscan = 0;
1279 /* Number of video rows */
1280 rows = c->vc_scan_lines / fh;
1281 /* Scan lines to actually display-1 */
1282 maxscan = rows * fh - 1;
1284 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1285 rows, maxscan, fh, c->vc_scan_lines);*/
1287 sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1288 vde = maxscan & 0xff;
1289 ovr = (ovr & 0xbd) |
1290 ((maxscan & 0x100) >> 7) |
1291 ((maxscan & 0x200) >> 3);
1292 sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1293 sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1297 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1298 fsr = (fsr & 0xe0) | (fh - 1);
1299 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1300 sisusb->current_font_height = fh;
1302 sisusb->sisusb_cursor_size_from = -1;
1303 sisusb->sisusb_cursor_size_to = -1;
1308 mutex_unlock(&sisusb->lock);
1310 if (dorecalc && c) {
1311 int i, rows = c->vc_scan_lines / fh;
1313 /* Now adjust our consoles' size */
1315 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1316 struct vc_data *vc = vc_cons[i].d;
1318 if (vc && vc->vc_sw == &sisusb_con) {
1319 if (CON_IS_VISIBLE(vc)) {
1320 vc->vc_sw->con_cursor(vc, CM_DRAW);
1322 vc->vc_font.height = fh;
1323 vc_resize(vc, 0, rows);
1332 mutex_unlock(&sisusb->lock);
1337 /* Interface routine */
1339 sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1342 struct sisusb_usb_data *sisusb;
1343 unsigned charcount = font->charcount;
1345 if (font->width != 8 || (charcount != 256 && charcount != 512))
1348 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1351 /* sisusb->lock is down */
1353 /* Save the user-provided font into a buffer. This
1354 * is used for restoring text mode after quitting
1355 * from X and for the con_getfont routine.
1357 if (sisusb->font_backup) {
1358 if (sisusb->font_backup_size < charcount) {
1359 vfree(sisusb->font_backup);
1360 sisusb->font_backup = NULL;
1364 if (!sisusb->font_backup)
1365 sisusb->font_backup = vmalloc(charcount * 32);
1367 if (sisusb->font_backup) {
1368 memcpy(sisusb->font_backup, font->data, charcount * 32);
1369 sisusb->font_backup_size = charcount;
1370 sisusb->font_backup_height = font->height;
1371 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1374 /* do_font_op ups sisusb->lock */
1376 return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1377 8192, (charcount == 512),
1378 (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1379 c, font->height, 1);
1382 /* Interface routine */
1384 sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1386 struct sisusb_usb_data *sisusb;
1388 if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1391 /* sisusb->lock is down */
1394 font->height = c->vc_font.height;
1395 font->charcount = 256;
1398 mutex_unlock(&sisusb->lock);
1402 if (!sisusb->font_backup) {
1403 mutex_unlock(&sisusb->lock);
1407 /* Copy 256 chars only, like vgacon */
1408 memcpy(font->data, sisusb->font_backup, 256 * 32);
1410 mutex_unlock(&sisusb->lock);
1416 * The console `switch' structure for the sisusb console
1419 static const struct consw sisusb_con = {
1420 .owner = THIS_MODULE,
1421 .con_startup = sisusbcon_startup,
1422 .con_init = sisusbcon_init,
1423 .con_deinit = sisusbcon_deinit,
1424 .con_clear = sisusbcon_clear,
1425 .con_putc = sisusbcon_putc,
1426 .con_putcs = sisusbcon_putcs,
1427 .con_cursor = sisusbcon_cursor,
1428 .con_scroll = sisusbcon_scroll,
1429 .con_bmove = sisusbcon_bmove,
1430 .con_switch = sisusbcon_switch,
1431 .con_blank = sisusbcon_blank,
1432 .con_font_set = sisusbcon_font_set,
1433 .con_font_get = sisusbcon_font_get,
1434 .con_set_palette = sisusbcon_set_palette,
1435 .con_scrolldelta = sisusbcon_scrolldelta,
1436 .con_build_attr = sisusbcon_build_attr,
1437 .con_invert_region = sisusbcon_invert_region,
1438 .con_set_origin = sisusbcon_set_origin,
1439 .con_save_screen = sisusbcon_save_screen,
1440 .con_resize = sisusbcon_resize,
1443 /* Our very own dummy console driver */
1445 static const char *sisusbdummycon_startup(void)
1447 return "SISUSBVGADUMMY";
1450 static void sisusbdummycon_init(struct vc_data *vc, int init)
1452 vc->vc_can_do_color = 1;
1457 vc_resize(vc, 80, 25);
1460 static int sisusbdummycon_dummy(void)
1465 #define SISUSBCONDUMMY (void *)sisusbdummycon_dummy
1467 static const struct consw sisusb_dummy_con = {
1468 .owner = THIS_MODULE,
1469 .con_startup = sisusbdummycon_startup,
1470 .con_init = sisusbdummycon_init,
1471 .con_deinit = SISUSBCONDUMMY,
1472 .con_clear = SISUSBCONDUMMY,
1473 .con_putc = SISUSBCONDUMMY,
1474 .con_putcs = SISUSBCONDUMMY,
1475 .con_cursor = SISUSBCONDUMMY,
1476 .con_scroll = SISUSBCONDUMMY,
1477 .con_bmove = SISUSBCONDUMMY,
1478 .con_switch = SISUSBCONDUMMY,
1479 .con_blank = SISUSBCONDUMMY,
1480 .con_font_set = SISUSBCONDUMMY,
1481 .con_font_get = SISUSBCONDUMMY,
1482 .con_font_default = SISUSBCONDUMMY,
1483 .con_font_copy = SISUSBCONDUMMY,
1484 .con_set_palette = SISUSBCONDUMMY,
1485 .con_scrolldelta = SISUSBCONDUMMY,
1489 sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1491 int i, ret, minor = sisusb->minor;
1493 mutex_lock(&disconnect_mutex);
1495 mutex_lock(&sisusb->lock);
1497 /* Erm.. that should not happen */
1498 if (sisusb->haveconsole || !sisusb->SiS_Pr) {
1499 mutex_unlock(&sisusb->lock);
1500 mutex_unlock(&disconnect_mutex);
1504 sisusb->con_first = first;
1505 sisusb->con_last = last;
1508 first > MAX_NR_CONSOLES ||
1509 last > MAX_NR_CONSOLES) {
1510 mutex_unlock(&sisusb->lock);
1511 mutex_unlock(&disconnect_mutex);
1515 /* If gfxcore not initialized or no consoles given, quit graciously */
1516 if (!sisusb->gfxinit || first < 1 || last < 1) {
1517 mutex_unlock(&sisusb->lock);
1518 mutex_unlock(&disconnect_mutex);
1522 sisusb->sisusb_cursor_loc = -1;
1523 sisusb->sisusb_cursor_size_from = -1;
1524 sisusb->sisusb_cursor_size_to = -1;
1526 /* Set up text mode (and upload default font) */
1527 if (sisusb_reset_text_mode(sisusb, 1)) {
1528 mutex_unlock(&sisusb->lock);
1529 mutex_unlock(&disconnect_mutex);
1531 "sisusbvga[%d]: Failed to set up text mode\n",
1536 /* Initialize some gfx registers */
1537 sisusb_initialize(sisusb);
1539 for (i = first - 1; i <= last - 1; i++) {
1540 /* Save sisusb for our interface routines */
1541 mysisusbs[i] = sisusb;
1544 /* Initial console setup */
1545 sisusb->sisusb_num_columns = 80;
1547 /* Use a 32K buffer (matches b8000-bffff area) */
1548 sisusb->scrbuf_size = 32 * 1024;
1550 /* Allocate screen buffer */
1551 if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
1552 mutex_unlock(&sisusb->lock);
1553 mutex_unlock(&disconnect_mutex);
1555 "sisusbvga[%d]: Failed to allocate screen buffer\n",
1560 mutex_unlock(&sisusb->lock);
1561 mutex_unlock(&disconnect_mutex);
1563 /* Now grab the desired console(s) */
1564 ret = take_over_console(&sisusb_con, first - 1, last - 1, 0);
1567 sisusb->haveconsole = 1;
1569 for (i = first - 1; i <= last - 1; i++)
1570 mysisusbs[i] = NULL;
1577 sisusb_console_exit(struct sisusb_usb_data *sisusb)
1581 /* This is called if the device is disconnected
1582 * and while disconnect and lock semaphores
1583 * are up. This should be save because we
1584 * can't lose our sisusb any other way but by
1585 * disconnection (and hence, the disconnect
1586 * sema is for protecting all other access
1587 * functions from disconnection, not the
1591 /* Now what do we do in case of disconnection:
1592 * One alternative would be to simply call
1593 * give_up_console(). Nah, not a good idea.
1594 * give_up_console() is obviously buggy as it
1595 * only discards the consw pointer from the
1596 * driver_map, but doesn't adapt vc->vc_sw
1597 * of the affected consoles. Hence, the next
1598 * call to any of the console functions will
1599 * eventually take a trip to oops county.
1600 * Also, give_up_console for some reason
1601 * doesn't decrement our module refcount.
1602 * Instead, we switch our consoles to a private
1603 * dummy console. This, of course, keeps our
1604 * refcount up as well, but it works perfectly.
1607 if (sisusb->haveconsole) {
1608 for (i = 0; i < MAX_NR_CONSOLES; i++)
1609 if (sisusb->havethisconsole[i])
1610 take_over_console(&sisusb_dummy_con, i, i, 0);
1611 /* At this point, con_deinit for all our
1612 * consoles is executed by take_over_console().
1614 sisusb->haveconsole = 0;
1617 vfree((void *)sisusb->scrbuf);
1620 vfree(sisusb->font_backup);
1621 sisusb->font_backup = NULL;
1624 void __init sisusb_init_concode(void)
1628 for (i = 0; i < MAX_NR_CONSOLES; i++)
1629 mysisusbs[i] = NULL;
1632 #endif /* INCL_CON */