1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles
5 * VGA text mode console part
7 * Copyright (C) 2005 by Thomas Winischhofer, Vienna, Austria
9 * If distributed as part of the Linux kernel, this code is licensed under the
10 * terms of the GPL v2.
12 * Otherwise, the following license terms apply:
14 * * Redistribution and use in source and binary forms, with or without
15 * * modification, are permitted provided that the following conditions
17 * * 1) Redistributions of source code must retain the above copyright
18 * * notice, this list of conditions and the following disclaimer.
19 * * 2) Redistributions in binary form must reproduce the above copyright
20 * * notice, this list of conditions and the following disclaimer in the
21 * * documentation and/or other materials provided with the distribution.
22 * * 3) The name of the author may not be used to endorse or promote products
23 * * derived from this software without specific psisusbr written permission.
25 * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR
26 * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Author: Thomas Winischhofer <thomas@winischhofer.net>
38 * Portions based on vgacon.c which are
39 * Created 28 Sep 1997 by Geert Uytterhoeven
40 * Rewritten by Martin Mares <mj@ucw.cz>, July 1998
41 * based on code Copyright (C) 1991, 1992 Linus Torvalds
44 * A note on using in_atomic() in here: We can't handle console
45 * calls from non-schedulable context due to our USB-dependend
46 * nature. For now, this driver just ignores any calls if it
51 #include <linux/mutex.h>
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/signal.h>
56 #include <linux/usb.h>
57 #include <linux/tty.h>
58 #include <linux/console.h>
59 #include <linux/string.h>
61 #include <linux/init.h>
62 #include <linux/vt_kern.h>
63 #include <linux/selection.h>
64 #include <linux/spinlock.h>
65 #include <linux/kref.h>
66 #include <linux/ioport.h>
67 #include <linux/interrupt.h>
68 #include <linux/vmalloc.h>
71 #include "sisusb_init.h"
73 /* vc_data -> sisusb conversion table */
74 static struct sisusb_usb_data
*mysisusbs
[MAX_NR_CONSOLES
];
76 /* Forward declaration */
77 static const struct consw sisusb_con
;
80 sisusbcon_memsetw(u16
*s
, u16 c
, unsigned int count
)
82 memset16(s
, c
, count
/ 2);
86 sisusb_initialize(struct sisusb_usb_data
*sisusb
)
88 /* Reset cursor and start address */
89 if (sisusb_setidxreg(sisusb
, SISCR
, 0x0c, 0x00))
91 if (sisusb_setidxreg(sisusb
, SISCR
, 0x0d, 0x00))
93 if (sisusb_setidxreg(sisusb
, SISCR
, 0x0e, 0x00))
95 sisusb_setidxreg(sisusb
, SISCR
, 0x0f, 0x00);
99 sisusbcon_set_start_address(struct sisusb_usb_data
*sisusb
, struct vc_data
*c
)
101 sisusb
->cur_start_addr
= (c
->vc_visible_origin
- sisusb
->scrbuf
) / 2;
103 sisusb_setidxreg(sisusb
, SISCR
, 0x0c, (sisusb
->cur_start_addr
>> 8));
104 sisusb_setidxreg(sisusb
, SISCR
, 0x0d, (sisusb
->cur_start_addr
& 0xff));
108 sisusb_set_cursor(struct sisusb_usb_data
*sisusb
, unsigned int location
)
110 if (sisusb
->sisusb_cursor_loc
== location
)
113 sisusb
->sisusb_cursor_loc
= location
;
115 /* Hardware bug: Text cursor appears twice or not at all
116 * at some positions. Work around it with the cursor skew
120 if ((location
& 0x0007) == 0x0007) {
121 sisusb
->bad_cursor_pos
= 1;
123 if (sisusb_setidxregandor(sisusb
, SISCR
, 0x0b, 0x1f, 0x20))
125 } else if (sisusb
->bad_cursor_pos
) {
126 if (sisusb_setidxregand(sisusb
, SISCR
, 0x0b, 0x1f))
128 sisusb
->bad_cursor_pos
= 0;
131 if (sisusb_setidxreg(sisusb
, SISCR
, 0x0e, (location
>> 8)))
133 sisusb_setidxreg(sisusb
, SISCR
, 0x0f, (location
& 0xff));
136 static inline struct sisusb_usb_data
*
137 sisusb_get_sisusb(unsigned short console
)
139 return mysisusbs
[console
];
143 sisusb_sisusb_valid(struct sisusb_usb_data
*sisusb
)
145 if (!sisusb
->present
|| !sisusb
->ready
|| !sisusb
->sisusb_dev
)
151 static struct sisusb_usb_data
*
152 sisusb_get_sisusb_lock_and_check(unsigned short console
)
154 struct sisusb_usb_data
*sisusb
;
156 /* We can't handle console calls in non-schedulable
157 * context due to our locks and the USB transport.
158 * So we simply ignore them. This should only affect
159 * some calls to printk.
164 sisusb
= sisusb_get_sisusb(console
);
168 mutex_lock(&sisusb
->lock
);
170 if (!sisusb_sisusb_valid(sisusb
) ||
171 !sisusb
->havethisconsole
[console
]) {
172 mutex_unlock(&sisusb
->lock
);
180 sisusb_is_inactive(struct vc_data
*c
, struct sisusb_usb_data
*sisusb
)
182 if (sisusb
->is_gfx
||
183 sisusb
->textmodedestroyed
||
184 c
->vc_mode
!= KD_TEXT
)
190 /* con_startup console interface routine */
192 sisusbcon_startup(void)
197 /* con_init console interface routine */
199 sisusbcon_init(struct vc_data
*c
, int init
)
201 struct sisusb_usb_data
*sisusb
;
204 /* This is called by do_take_over_console(),
205 * ie by us/under our control. It is
206 * only called after text mode and fonts
207 * are set up/restored.
210 sisusb
= sisusb_get_sisusb(c
->vc_num
);
214 mutex_lock(&sisusb
->lock
);
216 if (!sisusb_sisusb_valid(sisusb
)) {
217 mutex_unlock(&sisusb
->lock
);
221 c
->vc_can_do_color
= 1;
223 c
->vc_complement_mask
= 0x7700;
225 c
->vc_hi_font_mask
= sisusb
->current_font_512
? 0x0800 : 0;
227 sisusb
->haveconsole
= 1;
229 sisusb
->havethisconsole
[c
->vc_num
] = 1;
231 /* We only support 640x400 */
232 c
->vc_scan_lines
= 400;
234 c
->vc_font
.height
= sisusb
->current_font_height
;
236 /* We only support width = 8 */
238 rows
= c
->vc_scan_lines
/ c
->vc_font
.height
;
240 /* Increment usage count for our sisusb.
241 * Doing so saves us from upping/downing
242 * the disconnect semaphore; we can't
243 * lose our sisusb until this is undone
244 * in con_deinit. For all other console
245 * interface functions, it suffices to
246 * use sisusb->lock and do a quick check
247 * of sisusb for device disconnection.
249 kref_get(&sisusb
->kref
);
251 if (!*c
->vc_uni_pagedir_loc
)
252 con_set_default_unimap(c
);
254 mutex_unlock(&sisusb
->lock
);
260 vc_resize(c
, cols
, rows
);
263 /* con_deinit console interface routine */
265 sisusbcon_deinit(struct vc_data
*c
)
267 struct sisusb_usb_data
*sisusb
;
270 /* This is called by do_take_over_console()
271 * and others, ie not under our control.
274 sisusb
= sisusb_get_sisusb(c
->vc_num
);
278 mutex_lock(&sisusb
->lock
);
280 /* Clear ourselves in mysisusbs */
281 mysisusbs
[c
->vc_num
] = NULL
;
283 sisusb
->havethisconsole
[c
->vc_num
] = 0;
285 /* Free our font buffer if all consoles are gone */
286 if (sisusb
->font_backup
) {
287 for(i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
288 if (sisusb
->havethisconsole
[c
->vc_num
])
291 if (i
== MAX_NR_CONSOLES
) {
292 vfree(sisusb
->font_backup
);
293 sisusb
->font_backup
= NULL
;
297 mutex_unlock(&sisusb
->lock
);
299 /* decrement the usage count on our sisusb */
300 kref_put(&sisusb
->kref
, sisusb_delete
);
303 /* interface routine */
305 sisusbcon_build_attr(struct vc_data
*c
, u8 color
, enum vc_intensity intensity
,
306 bool blink
, bool underline
, bool reverse
,
312 attr
= (attr
& 0xf0) | c
->vc_ulcolor
;
313 else if (intensity
== VCI_HALF_BRIGHT
)
314 attr
= (attr
& 0xf0) | c
->vc_halfcolor
;
317 attr
= ((attr
) & 0x88) |
319 ((attr
) << 4)) & 0x77);
324 if (intensity
== VCI_BOLD
)
330 /* Interface routine */
332 sisusbcon_invert_region(struct vc_data
*vc
, u16
*p
, int count
)
334 /* Invert a region. This is called with a pointer
335 * to the console's internal screen buffer. So we
336 * simply do the inversion there and rely on
337 * a call to putc(s) to update the real screen.
343 *p
++ = ((a
) & 0x88ff) |
344 (((a
) & 0x7000) >> 4) |
345 (((a
) & 0x0700) << 4);
349 static inline void *sisusb_vaddr(const struct sisusb_usb_data
*sisusb
,
350 const struct vc_data
*c
, unsigned int x
, unsigned int y
)
352 return (u16
*)c
->vc_origin
+ y
* sisusb
->sisusb_num_columns
+ x
;
355 static inline unsigned long sisusb_haddr(const struct sisusb_usb_data
*sisusb
,
356 const struct vc_data
*c
, unsigned int x
, unsigned int y
)
358 unsigned long offset
= c
->vc_origin
- sisusb
->scrbuf
;
360 /* 2 bytes per each character */
361 offset
+= 2 * (y
* sisusb
->sisusb_num_columns
+ x
);
363 return sisusb
->vrambase
+ offset
;
366 /* Interface routine */
368 sisusbcon_putc(struct vc_data
*c
, int ch
, int y
, int x
)
370 struct sisusb_usb_data
*sisusb
;
372 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
376 /* sisusb->lock is down */
377 if (sisusb_is_inactive(c
, sisusb
)) {
378 mutex_unlock(&sisusb
->lock
);
382 sisusb_copy_memory(sisusb
, sisusb_vaddr(sisusb
, c
, x
, y
),
383 sisusb_haddr(sisusb
, c
, x
, y
), 2);
385 mutex_unlock(&sisusb
->lock
);
388 /* Interface routine */
390 sisusbcon_putcs(struct vc_data
*c
, const unsigned short *s
,
391 int count
, int y
, int x
)
393 struct sisusb_usb_data
*sisusb
;
395 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
399 /* sisusb->lock is down */
401 /* Need to put the characters into the buffer ourselves,
402 * because the vt does this AFTER calling us.
405 memcpy(sisusb_vaddr(sisusb
, c
, x
, y
), s
, count
* 2);
407 if (sisusb_is_inactive(c
, sisusb
)) {
408 mutex_unlock(&sisusb
->lock
);
412 sisusb_copy_memory(sisusb
, sisusb_vaddr(sisusb
, c
, x
, y
),
413 sisusb_haddr(sisusb
, c
, x
, y
), count
* 2);
415 mutex_unlock(&sisusb
->lock
);
418 /* Interface routine */
420 sisusbcon_clear(struct vc_data
*c
, int y
, int x
, int height
, int width
)
422 struct sisusb_usb_data
*sisusb
;
423 u16 eattr
= c
->vc_video_erase_char
;
427 if (width
<= 0 || height
<= 0)
430 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
434 /* sisusb->lock is down */
436 /* Need to clear buffer ourselves, because the vt does
437 * this AFTER calling us.
440 dest
= sisusb_vaddr(sisusb
, c
, x
, y
);
442 cols
= sisusb
->sisusb_num_columns
;
447 if (x
== 0 && width
>= c
->vc_cols
) {
449 sisusbcon_memsetw(dest
, eattr
, height
* cols
* 2);
453 for (i
= height
; i
> 0; i
--, dest
+= cols
)
454 sisusbcon_memsetw(dest
, eattr
, width
* 2);
458 if (sisusb_is_inactive(c
, sisusb
)) {
459 mutex_unlock(&sisusb
->lock
);
463 length
= ((height
* cols
) - x
- (cols
- width
- x
)) * 2;
466 sisusb_copy_memory(sisusb
, sisusb_vaddr(sisusb
, c
, x
, y
),
467 sisusb_haddr(sisusb
, c
, x
, y
), length
);
469 mutex_unlock(&sisusb
->lock
);
472 /* interface routine */
474 sisusbcon_switch(struct vc_data
*c
)
476 struct sisusb_usb_data
*sisusb
;
479 /* Returnvalue 0 means we have fully restored screen,
480 * and vt doesn't need to call do_update_region().
481 * Returnvalue != 0 naturally means the opposite.
484 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
488 /* sisusb->lock is down */
490 /* Don't write to screen if in gfx mode */
491 if (sisusb_is_inactive(c
, sisusb
)) {
492 mutex_unlock(&sisusb
->lock
);
496 /* That really should not happen. It would mean we are
497 * being called while the vc is using its private buffer
500 if (c
->vc_origin
== (unsigned long)c
->vc_screenbuf
) {
501 mutex_unlock(&sisusb
->lock
);
502 dev_dbg(&sisusb
->sisusb_dev
->dev
, "ASSERT ORIGIN != SCREENBUF!\n");
506 /* Check that we don't copy too much */
507 length
= min((int)c
->vc_screenbuf_size
,
508 (int)(sisusb
->scrbuf
+ sisusb
->scrbuf_size
- c
->vc_origin
));
510 /* Restore the screen contents */
511 memcpy((u16
*)c
->vc_origin
, (u16
*)c
->vc_screenbuf
, length
);
513 sisusb_copy_memory(sisusb
, (u8
*)c
->vc_origin
,
514 sisusb_haddr(sisusb
, c
, 0, 0), length
);
516 mutex_unlock(&sisusb
->lock
);
521 /* interface routine */
523 sisusbcon_save_screen(struct vc_data
*c
)
525 struct sisusb_usb_data
*sisusb
;
528 /* Save the current screen contents to vc's private
532 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
536 /* sisusb->lock is down */
538 if (sisusb_is_inactive(c
, sisusb
)) {
539 mutex_unlock(&sisusb
->lock
);
543 /* Check that we don't copy too much */
544 length
= min((int)c
->vc_screenbuf_size
,
545 (int)(sisusb
->scrbuf
+ sisusb
->scrbuf_size
- c
->vc_origin
));
547 /* Save the screen contents to vc's private buffer */
548 memcpy((u16
*)c
->vc_screenbuf
, (u16
*)c
->vc_origin
, length
);
550 mutex_unlock(&sisusb
->lock
);
553 /* interface routine */
555 sisusbcon_set_palette(struct vc_data
*c
, const unsigned char *table
)
557 struct sisusb_usb_data
*sisusb
;
560 /* Return value not used by vt */
562 if (!con_is_visible(c
))
565 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
569 /* sisusb->lock is down */
571 if (sisusb_is_inactive(c
, sisusb
)) {
572 mutex_unlock(&sisusb
->lock
);
576 for (i
= j
= 0; i
< 16; i
++) {
577 if (sisusb_setreg(sisusb
, SISCOLIDX
, table
[i
]))
579 if (sisusb_setreg(sisusb
, SISCOLDATA
, c
->vc_palette
[j
++] >> 2))
581 if (sisusb_setreg(sisusb
, SISCOLDATA
, c
->vc_palette
[j
++] >> 2))
583 if (sisusb_setreg(sisusb
, SISCOLDATA
, c
->vc_palette
[j
++] >> 2))
587 mutex_unlock(&sisusb
->lock
);
590 /* interface routine */
592 sisusbcon_blank(struct vc_data
*c
, int blank
, int mode_switch
)
594 struct sisusb_usb_data
*sisusb
;
595 u8 sr1
, cr17
, pmreg
, cr63
;
598 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
602 /* sisusb->lock is down */
605 sisusb
->is_gfx
= blank
? 1 : 0;
607 if (sisusb_is_inactive(c
, sisusb
)) {
608 mutex_unlock(&sisusb
->lock
);
614 case 1: /* Normal blanking: Clear screen */
616 sisusbcon_memsetw((u16
*)c
->vc_origin
,
617 c
->vc_video_erase_char
,
618 c
->vc_screenbuf_size
);
619 sisusb_copy_memory(sisusb
, (u8
*)c
->vc_origin
,
620 sisusb_haddr(sisusb
, c
, 0, 0),
621 c
->vc_screenbuf_size
);
622 sisusb
->con_blanked
= 1;
626 default: /* VESA blanking */
628 case 0: /* Unblank */
634 sisusb
->con_blanked
= 0;
636 case VESA_VSYNC_SUSPEND
+ 1:
642 case VESA_HSYNC_SUSPEND
+ 1:
648 case VESA_POWERDOWN
+ 1:
655 mutex_unlock(&sisusb
->lock
);
659 sisusb_setidxregandor(sisusb
, SISSR
, 0x01, ~0x20, sr1
);
660 sisusb_setidxregandor(sisusb
, SISCR
, 0x17, 0x7f, cr17
);
661 sisusb_setidxregandor(sisusb
, SISSR
, 0x1f, 0x3f, pmreg
);
662 sisusb_setidxregandor(sisusb
, SISCR
, 0x63, 0xbf, cr63
);
666 mutex_unlock(&sisusb
->lock
);
671 /* interface routine */
673 sisusbcon_scrolldelta(struct vc_data
*c
, int lines
)
675 struct sisusb_usb_data
*sisusb
;
677 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
681 /* sisusb->lock is down */
683 if (sisusb_is_inactive(c
, sisusb
)) {
684 mutex_unlock(&sisusb
->lock
);
688 vc_scrolldelta_helper(c
, lines
, sisusb
->con_rolled_over
,
689 (void *)sisusb
->scrbuf
, sisusb
->scrbuf_size
);
691 sisusbcon_set_start_address(sisusb
, c
);
693 mutex_unlock(&sisusb
->lock
);
696 /* Interface routine */
698 sisusbcon_cursor(struct vc_data
*c
, int mode
)
700 struct sisusb_usb_data
*sisusb
;
701 int from
, to
, baseline
;
703 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
707 /* sisusb->lock is down */
709 if (sisusb_is_inactive(c
, sisusb
)) {
710 mutex_unlock(&sisusb
->lock
);
714 if (c
->vc_origin
!= c
->vc_visible_origin
) {
715 c
->vc_visible_origin
= c
->vc_origin
;
716 sisusbcon_set_start_address(sisusb
, c
);
719 if (mode
== CM_ERASE
) {
720 sisusb_setidxregor(sisusb
, SISCR
, 0x0a, 0x20);
721 sisusb
->sisusb_cursor_size_to
= -1;
722 mutex_unlock(&sisusb
->lock
);
726 sisusb_set_cursor(sisusb
, (c
->vc_pos
- sisusb
->scrbuf
) / 2);
728 baseline
= c
->vc_font
.height
- (c
->vc_font
.height
< 10 ? 1 : 2);
730 switch (CUR_SIZE(c
->vc_cursor_type
)) {
731 case CUR_BLOCK
: from
= 1;
732 to
= c
->vc_font
.height
;
734 case CUR_TWO_THIRDS
: from
= c
->vc_font
.height
/ 3;
737 case CUR_LOWER_HALF
: from
= c
->vc_font
.height
/ 2;
740 case CUR_LOWER_THIRD
: from
= (c
->vc_font
.height
* 2) / 3;
743 case CUR_NONE
: from
= 31;
747 case CUR_UNDERLINE
: from
= baseline
- 1;
752 if (sisusb
->sisusb_cursor_size_from
!= from
||
753 sisusb
->sisusb_cursor_size_to
!= to
) {
755 sisusb_setidxreg(sisusb
, SISCR
, 0x0a, from
);
756 sisusb_setidxregandor(sisusb
, SISCR
, 0x0b, 0xe0, to
);
758 sisusb
->sisusb_cursor_size_from
= from
;
759 sisusb
->sisusb_cursor_size_to
= to
;
762 mutex_unlock(&sisusb
->lock
);
766 sisusbcon_scroll_area(struct vc_data
*c
, struct sisusb_usb_data
*sisusb
,
767 unsigned int t
, unsigned int b
, enum con_scroll dir
,
770 int cols
= sisusb
->sisusb_num_columns
;
771 int length
= ((b
- t
) * cols
) * 2;
772 u16 eattr
= c
->vc_video_erase_char
;
774 /* sisusb->lock is down */
776 /* Scroll an area which does not match the
777 * visible screen's dimensions. This needs
778 * to be done separately, as it does not
779 * use hardware panning.
785 memmove(sisusb_vaddr(sisusb
, c
, 0, t
),
786 sisusb_vaddr(sisusb
, c
, 0, t
+ lines
),
787 (b
- t
- lines
) * cols
* 2);
788 sisusbcon_memsetw(sisusb_vaddr(sisusb
, c
, 0, b
- lines
),
789 eattr
, lines
* cols
* 2);
793 memmove(sisusb_vaddr(sisusb
, c
, 0, t
+ lines
),
794 sisusb_vaddr(sisusb
, c
, 0, t
),
795 (b
- t
- lines
) * cols
* 2);
796 sisusbcon_memsetw(sisusb_vaddr(sisusb
, c
, 0, t
), eattr
,
801 sisusb_copy_memory(sisusb
, sisusb_vaddr(sisusb
, c
, 0, t
),
802 sisusb_haddr(sisusb
, c
, 0, t
), length
);
804 mutex_unlock(&sisusb
->lock
);
809 /* Interface routine */
811 sisusbcon_scroll(struct vc_data
*c
, unsigned int t
, unsigned int b
,
812 enum con_scroll dir
, unsigned int lines
)
814 struct sisusb_usb_data
*sisusb
;
815 u16 eattr
= c
->vc_video_erase_char
;
817 unsigned long oldorigin
;
818 unsigned int delta
= lines
* c
->vc_size_row
;
820 /* Returning != 0 means we have done the scrolling successfully.
821 * Returning 0 makes vt do the scrolling on its own.
822 * Note that con_scroll is only called if the console is
823 * visible. In that case, the origin should be our buffer,
824 * not the vt's private one.
830 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
834 /* sisusb->lock is down */
836 if (sisusb_is_inactive(c
, sisusb
)) {
837 mutex_unlock(&sisusb
->lock
);
842 if (t
|| b
!= c
->vc_rows
)
843 return sisusbcon_scroll_area(c
, sisusb
, t
, b
, dir
, lines
);
845 if (c
->vc_origin
!= c
->vc_visible_origin
) {
846 c
->vc_visible_origin
= c
->vc_origin
;
847 sisusbcon_set_start_address(sisusb
, c
);
850 /* limit amount to maximum realistic size */
851 if (lines
> c
->vc_rows
)
854 oldorigin
= c
->vc_origin
;
860 if (c
->vc_scr_end
+ delta
>=
861 sisusb
->scrbuf
+ sisusb
->scrbuf_size
) {
862 memcpy((u16
*)sisusb
->scrbuf
,
863 (u16
*)(oldorigin
+ delta
),
864 c
->vc_screenbuf_size
- delta
);
865 c
->vc_origin
= sisusb
->scrbuf
;
866 sisusb
->con_rolled_over
= oldorigin
- sisusb
->scrbuf
;
869 c
->vc_origin
+= delta
;
872 (u16
*)(c
->vc_origin
+ c
->vc_screenbuf_size
- delta
),
879 if (oldorigin
- delta
< sisusb
->scrbuf
) {
880 memmove((void *)sisusb
->scrbuf
+ sisusb
->scrbuf_size
-
881 c
->vc_screenbuf_size
+ delta
,
883 c
->vc_screenbuf_size
- delta
);
884 c
->vc_origin
= sisusb
->scrbuf
+
885 sisusb
->scrbuf_size
-
886 c
->vc_screenbuf_size
;
887 sisusb
->con_rolled_over
= 0;
890 c
->vc_origin
-= delta
;
892 c
->vc_scr_end
= c
->vc_origin
+ c
->vc_screenbuf_size
;
894 scr_memsetw((u16
*)(c
->vc_origin
), eattr
, delta
);
900 sisusb_copy_memory(sisusb
,
902 sisusb_haddr(sisusb
, c
, 0, 0),
903 c
->vc_screenbuf_size
);
904 else if (dir
== SM_UP
)
905 sisusb_copy_memory(sisusb
,
906 (u8
*)c
->vc_origin
+ c
->vc_screenbuf_size
- delta
,
907 sisusb_haddr(sisusb
, c
, 0, 0) +
908 c
->vc_screenbuf_size
- delta
,
911 sisusb_copy_memory(sisusb
,
913 sisusb_haddr(sisusb
, c
, 0, 0),
916 c
->vc_scr_end
= c
->vc_origin
+ c
->vc_screenbuf_size
;
917 c
->vc_visible_origin
= c
->vc_origin
;
919 sisusbcon_set_start_address(sisusb
, c
);
921 c
->vc_pos
= c
->vc_pos
- oldorigin
+ c
->vc_origin
;
923 mutex_unlock(&sisusb
->lock
);
928 /* Interface routine */
930 sisusbcon_set_origin(struct vc_data
*c
)
932 struct sisusb_usb_data
*sisusb
;
934 /* Returning != 0 means we were successful.
935 * Returning 0 will vt make to use its own
936 * screenbuffer as the origin.
939 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
943 /* sisusb->lock is down */
945 if (sisusb_is_inactive(c
, sisusb
) || sisusb
->con_blanked
) {
946 mutex_unlock(&sisusb
->lock
);
950 c
->vc_origin
= c
->vc_visible_origin
= sisusb
->scrbuf
;
952 sisusbcon_set_start_address(sisusb
, c
);
954 sisusb
->con_rolled_over
= 0;
956 mutex_unlock(&sisusb
->lock
);
961 /* Interface routine */
963 sisusbcon_resize(struct vc_data
*c
, unsigned int newcols
, unsigned int newrows
,
966 struct sisusb_usb_data
*sisusb
;
969 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
973 fh
= sisusb
->current_font_height
;
975 mutex_unlock(&sisusb
->lock
);
977 /* We are quite unflexible as regards resizing. The vt code
978 * handles sizes where the line length isn't equal the pitch
979 * quite badly. As regards the rows, our panning tricks only
980 * work well if the number of rows equals the visible number
984 if (newcols
!= 80 || c
->vc_scan_lines
/ fh
!= newrows
)
991 sisusbcon_do_font_op(struct sisusb_usb_data
*sisusb
, int set
, int slot
,
992 u8
*arg
, int cmapsz
, int ch512
, int dorecalc
,
993 struct vc_data
*c
, int fh
, int uplock
)
995 int font_select
= 0x00, i
, err
= 0;
999 /* sisusb->lock is down */
1002 * The default font is kept in slot 0.
1003 * A user font is loaded in slot 2 (256 ch)
1007 if ((slot
!= 0 && slot
!= 2) || !fh
) {
1009 mutex_unlock(&sisusb
->lock
);
1014 sisusb
->font_slot
= slot
;
1016 /* Default font is always 256 */
1020 offset
= 4 * cmapsz
;
1022 font_select
= (slot
== 0) ? 0x00 : (ch512
? 0x0e : 0x0a);
1024 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x00, 0x01); /* Reset */
1025 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x02, 0x04); /* Write to plane 2 */
1026 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x04, 0x07); /* Memory mode a0-bf */
1027 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x00, 0x03); /* Reset */
1032 err
|= sisusb_setidxreg(sisusb
, SISGR
, 0x04, 0x03); /* Select plane read 2 */
1033 err
|= sisusb_setidxreg(sisusb
, SISGR
, 0x05, 0x00); /* Disable odd/even */
1034 err
|= sisusb_setidxreg(sisusb
, SISGR
, 0x06, 0x00); /* Address range a0-bf */
1041 for (i
= 0; i
< cmapsz
; i
++) {
1042 err
|= sisusb_writeb(sisusb
,
1043 sisusb
->vrambase
+ offset
+ i
,
1049 for (i
= 0; i
< cmapsz
; i
++) {
1050 err
|= sisusb_readb(sisusb
,
1051 sisusb
->vrambase
+ offset
+ i
,
1058 * In 512-character mode, the character map is not contiguous if
1059 * we want to remain EGA compatible -- which we do
1064 for (i
= 0; i
< cmapsz
; i
++) {
1065 err
|= sisusb_writeb(sisusb
,
1066 sisusb
->vrambase
+ offset
+
1073 for (i
= 0; i
< cmapsz
; i
++) {
1074 err
|= sisusb_readb(sisusb
,
1075 sisusb
->vrambase
+ offset
+
1087 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x00, 0x01); /* Reset */
1088 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x02, 0x03); /* Write to planes 0+1 */
1089 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x04, 0x03); /* Memory mode a0-bf */
1091 sisusb_setidxreg(sisusb
, SISSR
, 0x03, font_select
);
1092 err
|= sisusb_setidxreg(sisusb
, SISSR
, 0x00, 0x03); /* Reset end */
1097 err
|= sisusb_setidxreg(sisusb
, SISGR
, 0x04, 0x00); /* Select plane read 0 */
1098 err
|= sisusb_setidxreg(sisusb
, SISGR
, 0x05, 0x10); /* Enable odd/even */
1099 err
|= sisusb_setidxreg(sisusb
, SISGR
, 0x06, 0x06); /* Address range b8-bf */
1104 if ((set
) && (ch512
!= sisusb
->current_font_512
)) {
1106 /* Font is shared among all our consoles.
1107 * And so is the hi_font_mask.
1109 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
1110 struct vc_data
*d
= vc_cons
[i
].d
;
1111 if (d
&& d
->vc_sw
== &sisusb_con
)
1112 d
->vc_hi_font_mask
= ch512
? 0x0800 : 0;
1115 sisusb
->current_font_512
= ch512
;
1117 /* color plane enable register:
1118 256-char: enable intensity bit
1119 512-char: disable intensity bit */
1120 sisusb_getreg(sisusb
, SISINPSTAT
, &dummy
);
1121 sisusb_setreg(sisusb
, SISAR
, 0x12);
1122 sisusb_setreg(sisusb
, SISAR
, ch512
? 0x07 : 0x0f);
1124 sisusb_getreg(sisusb
, SISINPSTAT
, &dummy
);
1125 sisusb_setreg(sisusb
, SISAR
, 0x20);
1126 sisusb_getreg(sisusb
, SISINPSTAT
, &dummy
);
1132 * Adjust the screen to fit a font of a certain height
1135 unsigned char ovr
, vde
, fsr
;
1136 int rows
= 0, maxscan
= 0;
1140 /* Number of video rows */
1141 rows
= c
->vc_scan_lines
/ fh
;
1142 /* Scan lines to actually display-1 */
1143 maxscan
= rows
* fh
- 1;
1145 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1146 rows, maxscan, fh, c->vc_scan_lines);*/
1148 sisusb_getidxreg(sisusb
, SISCR
, 0x07, &ovr
);
1149 vde
= maxscan
& 0xff;
1150 ovr
= (ovr
& 0xbd) |
1151 ((maxscan
& 0x100) >> 7) |
1152 ((maxscan
& 0x200) >> 3);
1153 sisusb_setidxreg(sisusb
, SISCR
, 0x07, ovr
);
1154 sisusb_setidxreg(sisusb
, SISCR
, 0x12, vde
);
1158 sisusb_getidxreg(sisusb
, SISCR
, 0x09, &fsr
);
1159 fsr
= (fsr
& 0xe0) | (fh
- 1);
1160 sisusb_setidxreg(sisusb
, SISCR
, 0x09, fsr
);
1161 sisusb
->current_font_height
= fh
;
1163 sisusb
->sisusb_cursor_size_from
= -1;
1164 sisusb
->sisusb_cursor_size_to
= -1;
1169 mutex_unlock(&sisusb
->lock
);
1171 if (dorecalc
&& c
) {
1172 int rows
= c
->vc_scan_lines
/ fh
;
1174 /* Now adjust our consoles' size */
1176 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
1177 struct vc_data
*vc
= vc_cons
[i
].d
;
1179 if (vc
&& vc
->vc_sw
== &sisusb_con
) {
1180 if (con_is_visible(vc
)) {
1181 vc
->vc_sw
->con_cursor(vc
, CM_DRAW
);
1183 vc
->vc_font
.height
= fh
;
1184 vc_resize(vc
, 0, rows
);
1193 mutex_unlock(&sisusb
->lock
);
1198 /* Interface routine */
1200 sisusbcon_font_set(struct vc_data
*c
, struct console_font
*font
,
1203 struct sisusb_usb_data
*sisusb
;
1204 unsigned charcount
= font
->charcount
;
1206 if (font
->width
!= 8 || (charcount
!= 256 && charcount
!= 512))
1209 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
1213 /* sisusb->lock is down */
1215 /* Save the user-provided font into a buffer. This
1216 * is used for restoring text mode after quitting
1217 * from X and for the con_getfont routine.
1219 if (sisusb
->font_backup
) {
1220 if (sisusb
->font_backup_size
< charcount
) {
1221 vfree(sisusb
->font_backup
);
1222 sisusb
->font_backup
= NULL
;
1226 if (!sisusb
->font_backup
)
1227 sisusb
->font_backup
= vmalloc(array_size(charcount
, 32));
1229 if (sisusb
->font_backup
) {
1230 memcpy(sisusb
->font_backup
, font
->data
, array_size(charcount
, 32));
1231 sisusb
->font_backup_size
= charcount
;
1232 sisusb
->font_backup_height
= font
->height
;
1233 sisusb
->font_backup_512
= (charcount
== 512) ? 1 : 0;
1236 /* do_font_op ups sisusb->lock */
1238 return sisusbcon_do_font_op(sisusb
, 1, 2, font
->data
,
1239 8192, (charcount
== 512),
1240 (!(flags
& KD_FONT_FLAG_DONT_RECALC
)) ? 1 : 0,
1241 c
, font
->height
, 1);
1244 /* Interface routine */
1246 sisusbcon_font_get(struct vc_data
*c
, struct console_font
*font
)
1248 struct sisusb_usb_data
*sisusb
;
1250 sisusb
= sisusb_get_sisusb_lock_and_check(c
->vc_num
);
1254 /* sisusb->lock is down */
1257 font
->height
= c
->vc_font
.height
;
1258 font
->charcount
= 256;
1261 mutex_unlock(&sisusb
->lock
);
1265 if (!sisusb
->font_backup
) {
1266 mutex_unlock(&sisusb
->lock
);
1270 /* Copy 256 chars only, like vgacon */
1271 memcpy(font
->data
, sisusb
->font_backup
, 256 * 32);
1273 mutex_unlock(&sisusb
->lock
);
1279 * The console `switch' structure for the sisusb console
1282 static const struct consw sisusb_con
= {
1283 .owner
= THIS_MODULE
,
1284 .con_startup
= sisusbcon_startup
,
1285 .con_init
= sisusbcon_init
,
1286 .con_deinit
= sisusbcon_deinit
,
1287 .con_clear
= sisusbcon_clear
,
1288 .con_putc
= sisusbcon_putc
,
1289 .con_putcs
= sisusbcon_putcs
,
1290 .con_cursor
= sisusbcon_cursor
,
1291 .con_scroll
= sisusbcon_scroll
,
1292 .con_switch
= sisusbcon_switch
,
1293 .con_blank
= sisusbcon_blank
,
1294 .con_font_set
= sisusbcon_font_set
,
1295 .con_font_get
= sisusbcon_font_get
,
1296 .con_set_palette
= sisusbcon_set_palette
,
1297 .con_scrolldelta
= sisusbcon_scrolldelta
,
1298 .con_build_attr
= sisusbcon_build_attr
,
1299 .con_invert_region
= sisusbcon_invert_region
,
1300 .con_set_origin
= sisusbcon_set_origin
,
1301 .con_save_screen
= sisusbcon_save_screen
,
1302 .con_resize
= sisusbcon_resize
,
1305 /* Our very own dummy console driver */
1307 static const char *sisusbdummycon_startup(void)
1309 return "SISUSBVGADUMMY";
1312 static void sisusbdummycon_init(struct vc_data
*vc
, int init
)
1314 vc
->vc_can_do_color
= 1;
1319 vc_resize(vc
, 80, 25);
1322 static void sisusbdummycon_deinit(struct vc_data
*vc
) { }
1323 static void sisusbdummycon_clear(struct vc_data
*vc
, int sy
, int sx
,
1324 int height
, int width
) { }
1325 static void sisusbdummycon_putc(struct vc_data
*vc
, int c
, int ypos
,
1327 static void sisusbdummycon_putcs(struct vc_data
*vc
, const unsigned short *s
,
1328 int count
, int ypos
, int xpos
) { }
1329 static void sisusbdummycon_cursor(struct vc_data
*vc
, int mode
) { }
1331 static bool sisusbdummycon_scroll(struct vc_data
*vc
, unsigned int top
,
1332 unsigned int bottom
, enum con_scroll dir
,
1338 static int sisusbdummycon_switch(struct vc_data
*vc
)
1343 static int sisusbdummycon_blank(struct vc_data
*vc
, int blank
, int mode_switch
)
1348 static const struct consw sisusb_dummy_con
= {
1349 .owner
= THIS_MODULE
,
1350 .con_startup
= sisusbdummycon_startup
,
1351 .con_init
= sisusbdummycon_init
,
1352 .con_deinit
= sisusbdummycon_deinit
,
1353 .con_clear
= sisusbdummycon_clear
,
1354 .con_putc
= sisusbdummycon_putc
,
1355 .con_putcs
= sisusbdummycon_putcs
,
1356 .con_cursor
= sisusbdummycon_cursor
,
1357 .con_scroll
= sisusbdummycon_scroll
,
1358 .con_switch
= sisusbdummycon_switch
,
1359 .con_blank
= sisusbdummycon_blank
,
1363 sisusb_console_init(struct sisusb_usb_data
*sisusb
, int first
, int last
)
1367 mutex_lock(&sisusb
->lock
);
1369 /* Erm.. that should not happen */
1370 if (sisusb
->haveconsole
|| !sisusb
->SiS_Pr
) {
1371 mutex_unlock(&sisusb
->lock
);
1375 sisusb
->con_first
= first
;
1376 sisusb
->con_last
= last
;
1379 first
> MAX_NR_CONSOLES
||
1380 last
> MAX_NR_CONSOLES
) {
1381 mutex_unlock(&sisusb
->lock
);
1385 /* If gfxcore not initialized or no consoles given, quit graciously */
1386 if (!sisusb
->gfxinit
|| first
< 1 || last
< 1) {
1387 mutex_unlock(&sisusb
->lock
);
1391 sisusb
->sisusb_cursor_loc
= -1;
1392 sisusb
->sisusb_cursor_size_from
= -1;
1393 sisusb
->sisusb_cursor_size_to
= -1;
1395 /* Set up text mode (and upload default font) */
1396 if (sisusb_reset_text_mode(sisusb
, 1)) {
1397 mutex_unlock(&sisusb
->lock
);
1398 dev_err(&sisusb
->sisusb_dev
->dev
, "Failed to set up text mode\n");
1402 /* Initialize some gfx registers */
1403 sisusb_initialize(sisusb
);
1405 for (i
= first
- 1; i
<= last
- 1; i
++) {
1406 /* Save sisusb for our interface routines */
1407 mysisusbs
[i
] = sisusb
;
1410 /* Initial console setup */
1411 sisusb
->sisusb_num_columns
= 80;
1413 /* Use a 32K buffer (matches b8000-bffff area) */
1414 sisusb
->scrbuf_size
= 32 * 1024;
1416 /* Allocate screen buffer */
1417 if (!(sisusb
->scrbuf
= (unsigned long)vmalloc(sisusb
->scrbuf_size
))) {
1418 mutex_unlock(&sisusb
->lock
);
1419 dev_err(&sisusb
->sisusb_dev
->dev
, "Failed to allocate screen buffer\n");
1423 mutex_unlock(&sisusb
->lock
);
1425 /* Now grab the desired console(s) */
1427 ret
= do_take_over_console(&sisusb_con
, first
- 1, last
- 1, 0);
1430 sisusb
->haveconsole
= 1;
1432 for (i
= first
- 1; i
<= last
- 1; i
++)
1433 mysisusbs
[i
] = NULL
;
1440 sisusb_console_exit(struct sisusb_usb_data
*sisusb
)
1444 /* This is called if the device is disconnected
1445 * and while disconnect and lock semaphores
1446 * are up. This should be save because we
1447 * can't lose our sisusb any other way but by
1448 * disconnection (and hence, the disconnect
1449 * sema is for protecting all other access
1450 * functions from disconnection, not the
1454 /* Now what do we do in case of disconnection:
1455 * One alternative would be to simply call
1456 * give_up_console(). Nah, not a good idea.
1457 * give_up_console() is obviously buggy as it
1458 * only discards the consw pointer from the
1459 * driver_map, but doesn't adapt vc->vc_sw
1460 * of the affected consoles. Hence, the next
1461 * call to any of the console functions will
1462 * eventually take a trip to oops county.
1463 * Also, give_up_console for some reason
1464 * doesn't decrement our module refcount.
1465 * Instead, we switch our consoles to a private
1466 * dummy console. This, of course, keeps our
1467 * refcount up as well, but it works perfectly.
1470 if (sisusb
->haveconsole
) {
1471 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++)
1472 if (sisusb
->havethisconsole
[i
]) {
1474 do_take_over_console(&sisusb_dummy_con
, i
, i
, 0);
1476 /* At this point, con_deinit for all our
1477 * consoles is executed by do_take_over_console().
1480 sisusb
->haveconsole
= 0;
1483 vfree((void *)sisusb
->scrbuf
);
1486 vfree(sisusb
->font_backup
);
1487 sisusb
->font_backup
= NULL
;
1490 void __init
sisusb_init_concode(void)
1494 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++)
1495 mysisusbs
[i
] = NULL
;