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/usb.h>
56 #include <linux/tty.h>
57 #include <linux/console.h>
58 #include <linux/string.h>
60 #include <linux/init.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 sisusb = sisusb_get_sisusb(console);
176 mutex_lock(&sisusb->lock);
178 if (!sisusb_sisusb_valid(sisusb) ||
179 !sisusb->havethisconsole[console]) {
180 mutex_unlock(&sisusb->lock);
188 sisusb_is_inactive(struct vc_data *c, struct sisusb_usb_data *sisusb)
190 if (sisusb->is_gfx ||
191 sisusb->textmodedestroyed ||
192 c->vc_mode != KD_TEXT)
198 /* con_startup console interface routine */
200 sisusbcon_startup(void)
205 /* con_init console interface routine */
207 sisusbcon_init(struct vc_data *c, int init)
209 struct sisusb_usb_data *sisusb;
212 /* This is called by do_take_over_console(),
213 * ie by us/under our control. It is
214 * only called after text mode and fonts
215 * are set up/restored.
218 sisusb = sisusb_get_sisusb(c->vc_num);
222 mutex_lock(&sisusb->lock);
224 if (!sisusb_sisusb_valid(sisusb)) {
225 mutex_unlock(&sisusb->lock);
229 c->vc_can_do_color = 1;
231 c->vc_complement_mask = 0x7700;
233 c->vc_hi_font_mask = sisusb->current_font_512 ? 0x0800 : 0;
235 sisusb->haveconsole = 1;
237 sisusb->havethisconsole[c->vc_num] = 1;
239 /* We only support 640x400 */
240 c->vc_scan_lines = 400;
242 c->vc_font.height = sisusb->current_font_height;
244 /* We only support width = 8 */
246 rows = c->vc_scan_lines / c->vc_font.height;
248 /* Increment usage count for our sisusb.
249 * Doing so saves us from upping/downing
250 * the disconnect semaphore; we can't
251 * lose our sisusb until this is undone
252 * in con_deinit. For all other console
253 * interface functions, it suffices to
254 * use sisusb->lock and do a quick check
255 * of sisusb for device disconnection.
257 kref_get(&sisusb->kref);
259 if (!*c->vc_uni_pagedir_loc)
260 con_set_default_unimap(c);
262 mutex_unlock(&sisusb->lock);
268 vc_resize(c, cols, rows);
271 /* con_deinit console interface routine */
273 sisusbcon_deinit(struct vc_data *c)
275 struct sisusb_usb_data *sisusb;
278 /* This is called by do_take_over_console()
279 * and others, ie not under our control.
282 sisusb = sisusb_get_sisusb(c->vc_num);
286 mutex_lock(&sisusb->lock);
288 /* Clear ourselves in mysisusbs */
289 mysisusbs[c->vc_num] = NULL;
291 sisusb->havethisconsole[c->vc_num] = 0;
293 /* Free our font buffer if all consoles are gone */
294 if (sisusb->font_backup) {
295 for(i = 0; i < MAX_NR_CONSOLES; i++) {
296 if (sisusb->havethisconsole[c->vc_num])
299 if (i == MAX_NR_CONSOLES) {
300 vfree(sisusb->font_backup);
301 sisusb->font_backup = NULL;
305 mutex_unlock(&sisusb->lock);
307 /* decrement the usage count on our sisusb */
308 kref_put(&sisusb->kref, sisusb_delete);
311 /* interface routine */
313 sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
314 u8 blink, u8 underline, u8 reverse, u8 unused)
319 attr = (attr & 0xf0) | c->vc_ulcolor;
320 else if (intensity == 0)
321 attr = (attr & 0xf0) | c->vc_halfcolor;
324 attr = ((attr) & 0x88) |
326 ((attr) << 4)) & 0x77);
337 /* Interface routine */
339 sisusbcon_invert_region(struct vc_data *vc, u16 *p, int count)
341 /* Invert a region. This is called with a pointer
342 * to the console's internal screen buffer. So we
343 * simply do the inversion there and rely on
344 * a call to putc(s) to update the real screen.
348 u16 a = sisusbcon_readw(p);
351 (((a) & 0x7000) >> 4) |
352 (((a) & 0x0700) << 4);
354 sisusbcon_writew(a, p++);
358 #define SISUSB_VADDR(x,y) \
359 ((u16 *)c->vc_origin + \
360 (y) * sisusb->sisusb_num_columns + \
363 #define SISUSB_HADDR(x,y) \
364 ((u16 *)(sisusb->vrambase + (c->vc_origin - sisusb->scrbuf)) + \
365 (y) * sisusb->sisusb_num_columns + \
368 /* Interface routine */
370 sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
372 struct sisusb_usb_data *sisusb;
374 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
378 /* sisusb->lock is down */
379 if (sisusb_is_inactive(c, sisusb)) {
380 mutex_unlock(&sisusb->lock);
385 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
386 (long)SISUSB_HADDR(x, y), 2);
388 mutex_unlock(&sisusb->lock);
391 /* Interface routine */
393 sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
394 int count, int y, int x)
396 struct sisusb_usb_data *sisusb;
400 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
404 /* sisusb->lock is down */
406 /* Need to put the characters into the buffer ourselves,
407 * because the vt does this AFTER calling us.
410 dest = SISUSB_VADDR(x, y);
412 for (i = count; i > 0; i--)
413 sisusbcon_writew(sisusbcon_readw(s++), dest++);
415 if (sisusb_is_inactive(c, sisusb)) {
416 mutex_unlock(&sisusb->lock);
420 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
421 (long)SISUSB_HADDR(x, y), count * 2);
423 mutex_unlock(&sisusb->lock);
426 /* Interface routine */
428 sisusbcon_clear(struct vc_data *c, int y, int x, int height, int width)
430 struct sisusb_usb_data *sisusb;
431 u16 eattr = c->vc_video_erase_char;
435 if (width <= 0 || height <= 0)
438 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
442 /* sisusb->lock is down */
444 /* Need to clear buffer ourselves, because the vt does
445 * this AFTER calling us.
448 dest = SISUSB_VADDR(x, y);
450 cols = sisusb->sisusb_num_columns;
455 if (x == 0 && width >= c->vc_cols) {
457 sisusbcon_memsetw(dest, eattr, height * cols * 2);
461 for (i = height; i > 0; i--, dest += cols)
462 sisusbcon_memsetw(dest, eattr, width * 2);
466 if (sisusb_is_inactive(c, sisusb)) {
467 mutex_unlock(&sisusb->lock);
471 length = ((height * cols) - x - (cols - width - x)) * 2;
474 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(x, y),
475 (long)SISUSB_HADDR(x, y), length);
477 mutex_unlock(&sisusb->lock);
480 /* Interface routine */
482 sisusbcon_bmove(struct vc_data *c, int sy, int sx,
483 int dy, int dx, int height, int width)
485 struct sisusb_usb_data *sisusb;
488 if (width <= 0 || height <= 0)
491 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
495 /* sisusb->lock is down */
497 cols = sisusb->sisusb_num_columns;
499 if (sisusb_is_inactive(c, sisusb)) {
500 mutex_unlock(&sisusb->lock);
504 length = ((height * cols) - dx - (cols - width - dx)) * 2;
507 sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(dx, dy),
508 (long)SISUSB_HADDR(dx, dy), length);
510 mutex_unlock(&sisusb->lock);
513 /* interface routine */
515 sisusbcon_switch(struct vc_data *c)
517 struct sisusb_usb_data *sisusb;
520 /* Returnvalue 0 means we have fully restored screen,
521 * and vt doesn't need to call do_update_region().
522 * Returnvalue != 0 naturally means the opposite.
525 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
529 /* sisusb->lock is down */
531 /* Don't write to screen if in gfx mode */
532 if (sisusb_is_inactive(c, sisusb)) {
533 mutex_unlock(&sisusb->lock);
537 /* That really should not happen. It would mean we are
538 * being called while the vc is using its private buffer
541 if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
542 mutex_unlock(&sisusb->lock);
543 dev_dbg(&sisusb->sisusb_dev->dev, "ASSERT ORIGIN != SCREENBUF!\n");
547 /* Check that we don't copy too much */
548 length = min((int)c->vc_screenbuf_size,
549 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
551 /* Restore the screen contents */
552 sisusbcon_memcpyw((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf,
555 sisusb_copy_memory(sisusb, (unsigned char *)c->vc_origin,
556 (long)SISUSB_HADDR(0, 0),
559 mutex_unlock(&sisusb->lock);
564 /* interface routine */
566 sisusbcon_save_screen(struct vc_data *c)
568 struct sisusb_usb_data *sisusb;
571 /* Save the current screen contents to vc's private
575 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
579 /* sisusb->lock is down */
581 if (sisusb_is_inactive(c, sisusb)) {
582 mutex_unlock(&sisusb->lock);
586 /* Check that we don't copy too much */
587 length = min((int)c->vc_screenbuf_size,
588 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
590 /* Save the screen contents to vc's private buffer */
591 sisusbcon_memcpyw((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin,
594 mutex_unlock(&sisusb->lock);
597 /* interface routine */
599 sisusbcon_set_palette(struct vc_data *c, const unsigned char *table)
601 struct sisusb_usb_data *sisusb;
604 /* Return value not used by vt */
606 if (!CON_IS_VISIBLE(c))
609 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
613 /* sisusb->lock is down */
615 if (sisusb_is_inactive(c, sisusb)) {
616 mutex_unlock(&sisusb->lock);
620 for (i = j = 0; i < 16; i++) {
621 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
623 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
625 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
627 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
631 mutex_unlock(&sisusb->lock);
634 /* interface routine */
636 sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
638 struct sisusb_usb_data *sisusb;
639 u8 sr1, cr17, pmreg, cr63;
642 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
646 /* sisusb->lock is down */
649 sisusb->is_gfx = blank ? 1 : 0;
651 if (sisusb_is_inactive(c, sisusb)) {
652 mutex_unlock(&sisusb->lock);
658 case 1: /* Normal blanking: Clear screen */
660 sisusbcon_memsetw((u16 *)c->vc_origin,
661 c->vc_video_erase_char,
662 c->vc_screenbuf_size);
663 sisusb_copy_memory(sisusb,
664 (unsigned char *)c->vc_origin,
665 (u32)(sisusb->vrambase +
666 (c->vc_origin - sisusb->scrbuf)),
667 c->vc_screenbuf_size);
668 sisusb->con_blanked = 1;
672 default: /* VESA blanking */
674 case 0: /* Unblank */
680 sisusb->con_blanked = 0;
682 case VESA_VSYNC_SUSPEND + 1:
688 case VESA_HSYNC_SUSPEND + 1:
694 case VESA_POWERDOWN + 1:
701 mutex_unlock(&sisusb->lock);
705 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
706 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
707 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
708 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
712 mutex_unlock(&sisusb->lock);
717 /* interface routine */
719 sisusbcon_scrolldelta(struct vc_data *c, int lines)
721 struct sisusb_usb_data *sisusb;
722 int margin = c->vc_size_row * 4;
725 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
729 /* sisusb->lock is down */
731 if (sisusb_is_inactive(c, sisusb)) {
732 mutex_unlock(&sisusb->lock);
736 if (!lines) /* Turn scrollback off */
737 c->vc_visible_origin = c->vc_origin;
740 if (sisusb->con_rolled_over >
741 (c->vc_scr_end - sisusb->scrbuf) + margin) {
743 ul = c->vc_scr_end - sisusb->scrbuf;
744 we = sisusb->con_rolled_over + c->vc_size_row;
749 we = sisusb->scrbuf_size;
753 p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we +
754 lines * c->vc_size_row;
756 st = (c->vc_origin - sisusb->scrbuf - ul + we) % we;
767 c->vc_visible_origin = sisusb->scrbuf + (p + ul) % we;
770 sisusbcon_set_start_address(sisusb, c);
772 mutex_unlock(&sisusb->lock);
775 /* Interface routine */
777 sisusbcon_cursor(struct vc_data *c, int mode)
779 struct sisusb_usb_data *sisusb;
780 int from, to, baseline;
782 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
786 /* sisusb->lock is down */
788 if (sisusb_is_inactive(c, sisusb)) {
789 mutex_unlock(&sisusb->lock);
793 if (c->vc_origin != c->vc_visible_origin) {
794 c->vc_visible_origin = c->vc_origin;
795 sisusbcon_set_start_address(sisusb, c);
798 if (mode == CM_ERASE) {
799 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
800 sisusb->sisusb_cursor_size_to = -1;
801 mutex_unlock(&sisusb->lock);
805 sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
807 baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
809 switch (c->vc_cursor_type & 0x0f) {
810 case CUR_BLOCK: from = 1;
811 to = c->vc_font.height;
813 case CUR_TWO_THIRDS: from = c->vc_font.height / 3;
816 case CUR_LOWER_HALF: from = c->vc_font.height / 2;
819 case CUR_LOWER_THIRD: from = (c->vc_font.height * 2) / 3;
822 case CUR_NONE: from = 31;
826 case CUR_UNDERLINE: from = baseline - 1;
831 if (sisusb->sisusb_cursor_size_from != from ||
832 sisusb->sisusb_cursor_size_to != to) {
834 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
835 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
837 sisusb->sisusb_cursor_size_from = from;
838 sisusb->sisusb_cursor_size_to = to;
841 mutex_unlock(&sisusb->lock);
845 sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
846 int t, int b, int dir, int lines)
848 int cols = sisusb->sisusb_num_columns;
849 int length = ((b - t) * cols) * 2;
850 u16 eattr = c->vc_video_erase_char;
852 /* sisusb->lock is down */
854 /* Scroll an area which does not match the
855 * visible screen's dimensions. This needs
856 * to be done separately, as it does not
857 * use hardware panning.
863 sisusbcon_memmovew(SISUSB_VADDR(0, t),
864 SISUSB_VADDR(0, t + lines),
865 (b - t - lines) * cols * 2);
866 sisusbcon_memsetw(SISUSB_VADDR(0, b - lines), eattr,
871 sisusbcon_memmovew(SISUSB_VADDR(0, t + lines),
873 (b - t - lines) * cols * 2);
874 sisusbcon_memsetw(SISUSB_VADDR(0, t), eattr,
879 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(0, t),
880 (long)SISUSB_HADDR(0, t), length);
882 mutex_unlock(&sisusb->lock);
887 /* Interface routine */
889 sisusbcon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
891 struct sisusb_usb_data *sisusb;
892 u16 eattr = c->vc_video_erase_char;
894 unsigned long oldorigin;
895 unsigned int delta = lines * c->vc_size_row;
898 /* Returning != 0 means we have done the scrolling successfully.
899 * Returning 0 makes vt do the scrolling on its own.
900 * Note that con_scroll is only called if the console is
901 * visible. In that case, the origin should be our buffer,
902 * not the vt's private one.
908 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
912 /* sisusb->lock is down */
914 if (sisusb_is_inactive(c, sisusb)) {
915 mutex_unlock(&sisusb->lock);
920 if (t || b != c->vc_rows)
921 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
923 if (c->vc_origin != c->vc_visible_origin) {
924 c->vc_visible_origin = c->vc_origin;
925 sisusbcon_set_start_address(sisusb, c);
928 /* limit amount to maximum realistic size */
929 if (lines > c->vc_rows)
932 oldorigin = c->vc_origin;
938 if (c->vc_scr_end + delta >=
939 sisusb->scrbuf + sisusb->scrbuf_size) {
940 sisusbcon_memcpyw((u16 *)sisusb->scrbuf,
941 (u16 *)(oldorigin + delta),
942 c->vc_screenbuf_size - delta);
943 c->vc_origin = sisusb->scrbuf;
944 sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
947 c->vc_origin += delta;
950 (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
957 if (oldorigin - delta < sisusb->scrbuf) {
958 sisusbcon_memmovew((u16 *)(sisusb->scrbuf +
959 sisusb->scrbuf_size -
960 c->vc_screenbuf_size +
963 c->vc_screenbuf_size - delta);
964 c->vc_origin = sisusb->scrbuf +
965 sisusb->scrbuf_size -
966 c->vc_screenbuf_size;
967 sisusb->con_rolled_over = 0;
970 c->vc_origin -= delta;
972 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
974 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
979 originoffset = (u32)(c->vc_origin - sisusb->scrbuf);
982 sisusb_copy_memory(sisusb,
983 (char *)c->vc_origin,
984 (u32)(sisusb->vrambase + originoffset),
985 c->vc_screenbuf_size);
986 else if (dir == SM_UP)
987 sisusb_copy_memory(sisusb,
988 (char *)c->vc_origin + c->vc_screenbuf_size - delta,
989 (u32)sisusb->vrambase + originoffset +
990 c->vc_screenbuf_size - delta,
993 sisusb_copy_memory(sisusb,
994 (char *)c->vc_origin,
995 (u32)(sisusb->vrambase + originoffset),
998 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
999 c->vc_visible_origin = c->vc_origin;
1001 sisusbcon_set_start_address(sisusb, c);
1003 c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
1005 mutex_unlock(&sisusb->lock);
1010 /* Interface routine */
1012 sisusbcon_set_origin(struct vc_data *c)
1014 struct sisusb_usb_data *sisusb;
1016 /* Returning != 0 means we were successful.
1017 * Returning 0 will vt make to use its own
1018 * screenbuffer as the origin.
1021 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1025 /* sisusb->lock is down */
1027 if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
1028 mutex_unlock(&sisusb->lock);
1032 c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
1034 sisusbcon_set_start_address(sisusb, c);
1036 sisusb->con_rolled_over = 0;
1038 mutex_unlock(&sisusb->lock);
1043 /* Interface routine */
1045 sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows,
1048 struct sisusb_usb_data *sisusb;
1051 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1055 fh = sisusb->current_font_height;
1057 mutex_unlock(&sisusb->lock);
1059 /* We are quite unflexible as regards resizing. The vt code
1060 * handles sizes where the line length isn't equal the pitch
1061 * quite badly. As regards the rows, our panning tricks only
1062 * work well if the number of rows equals the visible number
1066 if (newcols != 80 || c->vc_scan_lines / fh != newrows)
1073 sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
1074 u8 *arg, int cmapsz, int ch512, int dorecalc,
1075 struct vc_data *c, int fh, int uplock)
1077 int font_select = 0x00, i, err = 0;
1081 /* sisusb->lock is down */
1084 * The default font is kept in slot 0.
1085 * A user font is loaded in slot 2 (256 ch)
1089 if ((slot != 0 && slot != 2) || !fh) {
1091 mutex_unlock(&sisusb->lock);
1096 sisusb->font_slot = slot;
1098 /* Default font is always 256 */
1102 offset = 4 * cmapsz;
1104 font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1106 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1107 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1108 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1109 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1114 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1115 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1116 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1123 for (i = 0; i < cmapsz; i++) {
1124 err |= sisusb_writeb(sisusb,
1125 sisusb->vrambase + offset + i,
1131 for (i = 0; i < cmapsz; i++) {
1132 err |= sisusb_readb(sisusb,
1133 sisusb->vrambase + offset + i,
1140 * In 512-character mode, the character map is not contiguous if
1141 * we want to remain EGA compatible -- which we do
1146 for (i = 0; i < cmapsz; i++) {
1147 err |= sisusb_writeb(sisusb,
1148 sisusb->vrambase + offset +
1155 for (i = 0; i < cmapsz; i++) {
1156 err |= sisusb_readb(sisusb,
1157 sisusb->vrambase + offset +
1169 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1170 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1171 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1173 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1174 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1179 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1180 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1181 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1186 if ((set) && (ch512 != sisusb->current_font_512)) {
1188 /* Font is shared among all our consoles.
1189 * And so is the hi_font_mask.
1191 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1192 struct vc_data *d = vc_cons[i].d;
1193 if (d && d->vc_sw == &sisusb_con)
1194 d->vc_hi_font_mask = ch512 ? 0x0800 : 0;
1197 sisusb->current_font_512 = ch512;
1199 /* color plane enable register:
1200 256-char: enable intensity bit
1201 512-char: disable intensity bit */
1202 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1203 sisusb_setreg(sisusb, SISAR, 0x12);
1204 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1206 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1207 sisusb_setreg(sisusb, SISAR, 0x20);
1208 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1214 * Adjust the screen to fit a font of a certain height
1217 unsigned char ovr, vde, fsr;
1218 int rows = 0, maxscan = 0;
1222 /* Number of video rows */
1223 rows = c->vc_scan_lines / fh;
1224 /* Scan lines to actually display-1 */
1225 maxscan = rows * fh - 1;
1227 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1228 rows, maxscan, fh, c->vc_scan_lines);*/
1230 sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1231 vde = maxscan & 0xff;
1232 ovr = (ovr & 0xbd) |
1233 ((maxscan & 0x100) >> 7) |
1234 ((maxscan & 0x200) >> 3);
1235 sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1236 sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1240 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1241 fsr = (fsr & 0xe0) | (fh - 1);
1242 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1243 sisusb->current_font_height = fh;
1245 sisusb->sisusb_cursor_size_from = -1;
1246 sisusb->sisusb_cursor_size_to = -1;
1251 mutex_unlock(&sisusb->lock);
1253 if (dorecalc && c) {
1254 int rows = c->vc_scan_lines / fh;
1256 /* Now adjust our consoles' size */
1258 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1259 struct vc_data *vc = vc_cons[i].d;
1261 if (vc && vc->vc_sw == &sisusb_con) {
1262 if (CON_IS_VISIBLE(vc)) {
1263 vc->vc_sw->con_cursor(vc, CM_DRAW);
1265 vc->vc_font.height = fh;
1266 vc_resize(vc, 0, rows);
1275 mutex_unlock(&sisusb->lock);
1280 /* Interface routine */
1282 sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1285 struct sisusb_usb_data *sisusb;
1286 unsigned charcount = font->charcount;
1288 if (font->width != 8 || (charcount != 256 && charcount != 512))
1291 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1295 /* sisusb->lock is down */
1297 /* Save the user-provided font into a buffer. This
1298 * is used for restoring text mode after quitting
1299 * from X and for the con_getfont routine.
1301 if (sisusb->font_backup) {
1302 if (sisusb->font_backup_size < charcount) {
1303 vfree(sisusb->font_backup);
1304 sisusb->font_backup = NULL;
1308 if (!sisusb->font_backup)
1309 sisusb->font_backup = vmalloc(charcount * 32);
1311 if (sisusb->font_backup) {
1312 memcpy(sisusb->font_backup, font->data, charcount * 32);
1313 sisusb->font_backup_size = charcount;
1314 sisusb->font_backup_height = font->height;
1315 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1318 /* do_font_op ups sisusb->lock */
1320 return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1321 8192, (charcount == 512),
1322 (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1323 c, font->height, 1);
1326 /* Interface routine */
1328 sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1330 struct sisusb_usb_data *sisusb;
1332 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1336 /* sisusb->lock is down */
1339 font->height = c->vc_font.height;
1340 font->charcount = 256;
1343 mutex_unlock(&sisusb->lock);
1347 if (!sisusb->font_backup) {
1348 mutex_unlock(&sisusb->lock);
1352 /* Copy 256 chars only, like vgacon */
1353 memcpy(font->data, sisusb->font_backup, 256 * 32);
1355 mutex_unlock(&sisusb->lock);
1361 * The console `switch' structure for the sisusb console
1364 static const struct consw sisusb_con = {
1365 .owner = THIS_MODULE,
1366 .con_startup = sisusbcon_startup,
1367 .con_init = sisusbcon_init,
1368 .con_deinit = sisusbcon_deinit,
1369 .con_clear = sisusbcon_clear,
1370 .con_putc = sisusbcon_putc,
1371 .con_putcs = sisusbcon_putcs,
1372 .con_cursor = sisusbcon_cursor,
1373 .con_scroll = sisusbcon_scroll,
1374 .con_bmove = sisusbcon_bmove,
1375 .con_switch = sisusbcon_switch,
1376 .con_blank = sisusbcon_blank,
1377 .con_font_set = sisusbcon_font_set,
1378 .con_font_get = sisusbcon_font_get,
1379 .con_set_palette = sisusbcon_set_palette,
1380 .con_scrolldelta = sisusbcon_scrolldelta,
1381 .con_build_attr = sisusbcon_build_attr,
1382 .con_invert_region = sisusbcon_invert_region,
1383 .con_set_origin = sisusbcon_set_origin,
1384 .con_save_screen = sisusbcon_save_screen,
1385 .con_resize = sisusbcon_resize,
1388 /* Our very own dummy console driver */
1390 static const char *sisusbdummycon_startup(void)
1392 return "SISUSBVGADUMMY";
1395 static void sisusbdummycon_init(struct vc_data *vc, int init)
1397 vc->vc_can_do_color = 1;
1402 vc_resize(vc, 80, 25);
1405 static int sisusbdummycon_dummy(void)
1410 #define SISUSBCONDUMMY (void *)sisusbdummycon_dummy
1412 static const struct consw sisusb_dummy_con = {
1413 .owner = THIS_MODULE,
1414 .con_startup = sisusbdummycon_startup,
1415 .con_init = sisusbdummycon_init,
1416 .con_deinit = SISUSBCONDUMMY,
1417 .con_clear = SISUSBCONDUMMY,
1418 .con_putc = SISUSBCONDUMMY,
1419 .con_putcs = SISUSBCONDUMMY,
1420 .con_cursor = SISUSBCONDUMMY,
1421 .con_scroll = SISUSBCONDUMMY,
1422 .con_bmove = SISUSBCONDUMMY,
1423 .con_switch = SISUSBCONDUMMY,
1424 .con_blank = SISUSBCONDUMMY,
1425 .con_font_set = SISUSBCONDUMMY,
1426 .con_font_get = SISUSBCONDUMMY,
1427 .con_font_default = SISUSBCONDUMMY,
1428 .con_font_copy = SISUSBCONDUMMY,
1432 sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1436 mutex_lock(&sisusb->lock);
1438 /* Erm.. that should not happen */
1439 if (sisusb->haveconsole || !sisusb->SiS_Pr) {
1440 mutex_unlock(&sisusb->lock);
1444 sisusb->con_first = first;
1445 sisusb->con_last = last;
1448 first > MAX_NR_CONSOLES ||
1449 last > MAX_NR_CONSOLES) {
1450 mutex_unlock(&sisusb->lock);
1454 /* If gfxcore not initialized or no consoles given, quit graciously */
1455 if (!sisusb->gfxinit || first < 1 || last < 1) {
1456 mutex_unlock(&sisusb->lock);
1460 sisusb->sisusb_cursor_loc = -1;
1461 sisusb->sisusb_cursor_size_from = -1;
1462 sisusb->sisusb_cursor_size_to = -1;
1464 /* Set up text mode (and upload default font) */
1465 if (sisusb_reset_text_mode(sisusb, 1)) {
1466 mutex_unlock(&sisusb->lock);
1467 dev_err(&sisusb->sisusb_dev->dev, "Failed to set up text mode\n");
1471 /* Initialize some gfx registers */
1472 sisusb_initialize(sisusb);
1474 for (i = first - 1; i <= last - 1; i++) {
1475 /* Save sisusb for our interface routines */
1476 mysisusbs[i] = sisusb;
1479 /* Initial console setup */
1480 sisusb->sisusb_num_columns = 80;
1482 /* Use a 32K buffer (matches b8000-bffff area) */
1483 sisusb->scrbuf_size = 32 * 1024;
1485 /* Allocate screen buffer */
1486 if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
1487 mutex_unlock(&sisusb->lock);
1488 dev_err(&sisusb->sisusb_dev->dev, "Failed to allocate screen buffer\n");
1492 mutex_unlock(&sisusb->lock);
1494 /* Now grab the desired console(s) */
1496 ret = do_take_over_console(&sisusb_con, first - 1, last - 1, 0);
1499 sisusb->haveconsole = 1;
1501 for (i = first - 1; i <= last - 1; i++)
1502 mysisusbs[i] = NULL;
1509 sisusb_console_exit(struct sisusb_usb_data *sisusb)
1513 /* This is called if the device is disconnected
1514 * and while disconnect and lock semaphores
1515 * are up. This should be save because we
1516 * can't lose our sisusb any other way but by
1517 * disconnection (and hence, the disconnect
1518 * sema is for protecting all other access
1519 * functions from disconnection, not the
1523 /* Now what do we do in case of disconnection:
1524 * One alternative would be to simply call
1525 * give_up_console(). Nah, not a good idea.
1526 * give_up_console() is obviously buggy as it
1527 * only discards the consw pointer from the
1528 * driver_map, but doesn't adapt vc->vc_sw
1529 * of the affected consoles. Hence, the next
1530 * call to any of the console functions will
1531 * eventually take a trip to oops county.
1532 * Also, give_up_console for some reason
1533 * doesn't decrement our module refcount.
1534 * Instead, we switch our consoles to a private
1535 * dummy console. This, of course, keeps our
1536 * refcount up as well, but it works perfectly.
1539 if (sisusb->haveconsole) {
1540 for (i = 0; i < MAX_NR_CONSOLES; i++)
1541 if (sisusb->havethisconsole[i]) {
1543 do_take_over_console(&sisusb_dummy_con, i, i, 0);
1545 /* At this point, con_deinit for all our
1546 * consoles is executed by do_take_over_console().
1549 sisusb->haveconsole = 0;
1552 vfree((void *)sisusb->scrbuf);
1555 vfree(sisusb->font_backup);
1556 sisusb->font_backup = NULL;
1559 void __init sisusb_init_concode(void)
1563 for (i = 0; i < MAX_NR_CONSOLES; i++)
1564 mysisusbs[i] = NULL;
1567 #endif /* INCL_CON */