treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / misc / sisusbvga / sisusb_con.c
blobcd0155310fead19f02d97d510acd1844a6597643
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 /*
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
16 * * are met:
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.
24 * *
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
42 * 1995 Jay Estabrook
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
47 * detects this state.
51 #include <linux/mutex.h>
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/signal.h>
55 #include <linux/fs.h>
56 #include <linux/usb.h>
57 #include <linux/tty.h>
58 #include <linux/console.h>
59 #include <linux/string.h>
60 #include <linux/kd.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>
70 #include "sisusb.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;
79 static inline void
80 sisusbcon_memsetw(u16 *s, u16 c, unsigned int count)
82 memset16(s, c, count / 2);
85 static inline void
86 sisusb_initialize(struct sisusb_usb_data *sisusb)
88 /* Reset cursor and start address */
89 if (sisusb_setidxreg(sisusb, SISCR, 0x0c, 0x00))
90 return;
91 if (sisusb_setidxreg(sisusb, SISCR, 0x0d, 0x00))
92 return;
93 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, 0x00))
94 return;
95 sisusb_setidxreg(sisusb, SISCR, 0x0f, 0x00);
98 static inline void
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));
107 void
108 sisusb_set_cursor(struct sisusb_usb_data *sisusb, unsigned int location)
110 if (sisusb->sisusb_cursor_loc == location)
111 return;
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
117 * bits.
120 if ((location & 0x0007) == 0x0007) {
121 sisusb->bad_cursor_pos = 1;
122 location--;
123 if (sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0x1f, 0x20))
124 return;
125 } else if (sisusb->bad_cursor_pos) {
126 if (sisusb_setidxregand(sisusb, SISCR, 0x0b, 0x1f))
127 return;
128 sisusb->bad_cursor_pos = 0;
131 if (sisusb_setidxreg(sisusb, SISCR, 0x0e, (location >> 8)))
132 return;
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];
142 static inline int
143 sisusb_sisusb_valid(struct sisusb_usb_data *sisusb)
145 if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev)
146 return 0;
148 return 1;
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.
161 if (in_atomic())
162 return NULL;
164 sisusb = sisusb_get_sisusb(console);
165 if (!sisusb)
166 return NULL;
168 mutex_lock(&sisusb->lock);
170 if (!sisusb_sisusb_valid(sisusb) ||
171 !sisusb->havethisconsole[console]) {
172 mutex_unlock(&sisusb->lock);
173 return NULL;
176 return sisusb;
179 static int
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)
185 return 1;
187 return 0;
190 /* con_startup console interface routine */
191 static const char *
192 sisusbcon_startup(void)
194 return "SISUSBCON";
197 /* con_init console interface routine */
198 static void
199 sisusbcon_init(struct vc_data *c, int init)
201 struct sisusb_usb_data *sisusb;
202 int cols, rows;
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);
211 if (!sisusb)
212 return;
214 mutex_lock(&sisusb->lock);
216 if (!sisusb_sisusb_valid(sisusb)) {
217 mutex_unlock(&sisusb->lock);
218 return;
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 */
237 cols = 80;
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);
256 if (init) {
257 c->vc_cols = cols;
258 c->vc_rows = rows;
259 } else
260 vc_resize(c, cols, rows);
263 /* con_deinit console interface routine */
264 static void
265 sisusbcon_deinit(struct vc_data *c)
267 struct sisusb_usb_data *sisusb;
268 int i;
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);
275 if (!sisusb)
276 return;
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])
289 break;
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 */
304 static u8
305 sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
306 u8 blink, u8 underline, u8 reverse, u8 unused)
308 u8 attr = color;
310 if (underline)
311 attr = (attr & 0xf0) | c->vc_ulcolor;
312 else if (intensity == 0)
313 attr = (attr & 0xf0) | c->vc_halfcolor;
315 if (reverse)
316 attr = ((attr) & 0x88) |
317 ((((attr) >> 4) |
318 ((attr) << 4)) & 0x77);
320 if (blink)
321 attr ^= 0x80;
323 if (intensity == 2)
324 attr ^= 0x08;
326 return attr;
329 /* Interface routine */
330 static void
331 sisusbcon_invert_region(struct vc_data *vc, u16 *p, int count)
333 /* Invert a region. This is called with a pointer
334 * to the console's internal screen buffer. So we
335 * simply do the inversion there and rely on
336 * a call to putc(s) to update the real screen.
339 while (count--) {
340 u16 a = *p;
342 *p++ = ((a) & 0x88ff) |
343 (((a) & 0x7000) >> 4) |
344 (((a) & 0x0700) << 4);
348 static inline void *sisusb_vaddr(const struct sisusb_usb_data *sisusb,
349 const struct vc_data *c, unsigned int x, unsigned int y)
351 return (u16 *)c->vc_origin + y * sisusb->sisusb_num_columns + x;
354 static inline unsigned long sisusb_haddr(const struct sisusb_usb_data *sisusb,
355 const struct vc_data *c, unsigned int x, unsigned int y)
357 unsigned long offset = c->vc_origin - sisusb->scrbuf;
359 /* 2 bytes per each character */
360 offset += 2 * (y * sisusb->sisusb_num_columns + x);
362 return sisusb->vrambase + offset;
365 /* Interface routine */
366 static void
367 sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
369 struct sisusb_usb_data *sisusb;
371 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
372 if (!sisusb)
373 return;
375 /* sisusb->lock is down */
376 if (sisusb_is_inactive(c, sisusb)) {
377 mutex_unlock(&sisusb->lock);
378 return;
381 sisusb_copy_memory(sisusb, sisusb_vaddr(sisusb, c, x, y),
382 sisusb_haddr(sisusb, c, x, y), 2);
384 mutex_unlock(&sisusb->lock);
387 /* Interface routine */
388 static void
389 sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
390 int count, int y, int x)
392 struct sisusb_usb_data *sisusb;
394 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
395 if (!sisusb)
396 return;
398 /* sisusb->lock is down */
400 /* Need to put the characters into the buffer ourselves,
401 * because the vt does this AFTER calling us.
404 memcpy(sisusb_vaddr(sisusb, c, x, y), s, count * 2);
406 if (sisusb_is_inactive(c, sisusb)) {
407 mutex_unlock(&sisusb->lock);
408 return;
411 sisusb_copy_memory(sisusb, sisusb_vaddr(sisusb, c, x, y),
412 sisusb_haddr(sisusb, c, x, y), count * 2);
414 mutex_unlock(&sisusb->lock);
417 /* Interface routine */
418 static void
419 sisusbcon_clear(struct vc_data *c, int y, int x, int height, int width)
421 struct sisusb_usb_data *sisusb;
422 u16 eattr = c->vc_video_erase_char;
423 int i, length, cols;
424 u16 *dest;
426 if (width <= 0 || height <= 0)
427 return;
429 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
430 if (!sisusb)
431 return;
433 /* sisusb->lock is down */
435 /* Need to clear buffer ourselves, because the vt does
436 * this AFTER calling us.
439 dest = sisusb_vaddr(sisusb, c, x, y);
441 cols = sisusb->sisusb_num_columns;
443 if (width > cols)
444 width = cols;
446 if (x == 0 && width >= c->vc_cols) {
448 sisusbcon_memsetw(dest, eattr, height * cols * 2);
450 } else {
452 for (i = height; i > 0; i--, dest += cols)
453 sisusbcon_memsetw(dest, eattr, width * 2);
457 if (sisusb_is_inactive(c, sisusb)) {
458 mutex_unlock(&sisusb->lock);
459 return;
462 length = ((height * cols) - x - (cols - width - x)) * 2;
465 sisusb_copy_memory(sisusb, sisusb_vaddr(sisusb, c, x, y),
466 sisusb_haddr(sisusb, c, x, y), length);
468 mutex_unlock(&sisusb->lock);
471 /* interface routine */
472 static int
473 sisusbcon_switch(struct vc_data *c)
475 struct sisusb_usb_data *sisusb;
476 int length;
478 /* Returnvalue 0 means we have fully restored screen,
479 * and vt doesn't need to call do_update_region().
480 * Returnvalue != 0 naturally means the opposite.
483 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
484 if (!sisusb)
485 return 0;
487 /* sisusb->lock is down */
489 /* Don't write to screen if in gfx mode */
490 if (sisusb_is_inactive(c, sisusb)) {
491 mutex_unlock(&sisusb->lock);
492 return 0;
495 /* That really should not happen. It would mean we are
496 * being called while the vc is using its private buffer
497 * as origin.
499 if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
500 mutex_unlock(&sisusb->lock);
501 dev_dbg(&sisusb->sisusb_dev->dev, "ASSERT ORIGIN != SCREENBUF!\n");
502 return 0;
505 /* Check that we don't copy too much */
506 length = min((int)c->vc_screenbuf_size,
507 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
509 /* Restore the screen contents */
510 memcpy((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf, length);
512 sisusb_copy_memory(sisusb, (char *)c->vc_origin,
513 sisusb_haddr(sisusb, c, 0, 0), length);
515 mutex_unlock(&sisusb->lock);
517 return 0;
520 /* interface routine */
521 static void
522 sisusbcon_save_screen(struct vc_data *c)
524 struct sisusb_usb_data *sisusb;
525 int length;
527 /* Save the current screen contents to vc's private
528 * buffer.
531 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
532 if (!sisusb)
533 return;
535 /* sisusb->lock is down */
537 if (sisusb_is_inactive(c, sisusb)) {
538 mutex_unlock(&sisusb->lock);
539 return;
542 /* Check that we don't copy too much */
543 length = min((int)c->vc_screenbuf_size,
544 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
546 /* Save the screen contents to vc's private buffer */
547 memcpy((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin, length);
549 mutex_unlock(&sisusb->lock);
552 /* interface routine */
553 static void
554 sisusbcon_set_palette(struct vc_data *c, const unsigned char *table)
556 struct sisusb_usb_data *sisusb;
557 int i, j;
559 /* Return value not used by vt */
561 if (!con_is_visible(c))
562 return;
564 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
565 if (!sisusb)
566 return;
568 /* sisusb->lock is down */
570 if (sisusb_is_inactive(c, sisusb)) {
571 mutex_unlock(&sisusb->lock);
572 return;
575 for (i = j = 0; i < 16; i++) {
576 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
577 break;
578 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
579 break;
580 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
581 break;
582 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
583 break;
586 mutex_unlock(&sisusb->lock);
589 /* interface routine */
590 static int
591 sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
593 struct sisusb_usb_data *sisusb;
594 u8 sr1, cr17, pmreg, cr63;
595 int ret = 0;
597 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
598 if (!sisusb)
599 return 0;
601 /* sisusb->lock is down */
603 if (mode_switch)
604 sisusb->is_gfx = blank ? 1 : 0;
606 if (sisusb_is_inactive(c, sisusb)) {
607 mutex_unlock(&sisusb->lock);
608 return 0;
611 switch (blank) {
613 case 1: /* Normal blanking: Clear screen */
614 case -1:
615 sisusbcon_memsetw((u16 *)c->vc_origin,
616 c->vc_video_erase_char,
617 c->vc_screenbuf_size);
618 sisusb_copy_memory(sisusb, (char *)c->vc_origin,
619 sisusb_haddr(sisusb, c, 0, 0),
620 c->vc_screenbuf_size);
621 sisusb->con_blanked = 1;
622 ret = 1;
623 break;
625 default: /* VESA blanking */
626 switch (blank) {
627 case 0: /* Unblank */
628 sr1 = 0x00;
629 cr17 = 0x80;
630 pmreg = 0x00;
631 cr63 = 0x00;
632 ret = 1;
633 sisusb->con_blanked = 0;
634 break;
635 case VESA_VSYNC_SUSPEND + 1:
636 sr1 = 0x20;
637 cr17 = 0x80;
638 pmreg = 0x80;
639 cr63 = 0x40;
640 break;
641 case VESA_HSYNC_SUSPEND + 1:
642 sr1 = 0x20;
643 cr17 = 0x80;
644 pmreg = 0x40;
645 cr63 = 0x40;
646 break;
647 case VESA_POWERDOWN + 1:
648 sr1 = 0x20;
649 cr17 = 0x00;
650 pmreg = 0xc0;
651 cr63 = 0x40;
652 break;
653 default:
654 mutex_unlock(&sisusb->lock);
655 return -EINVAL;
658 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
659 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
660 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
661 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
665 mutex_unlock(&sisusb->lock);
667 return ret;
670 /* interface routine */
671 static void
672 sisusbcon_scrolldelta(struct vc_data *c, int lines)
674 struct sisusb_usb_data *sisusb;
676 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
677 if (!sisusb)
678 return;
680 /* sisusb->lock is down */
682 if (sisusb_is_inactive(c, sisusb)) {
683 mutex_unlock(&sisusb->lock);
684 return;
687 vc_scrolldelta_helper(c, lines, sisusb->con_rolled_over,
688 (void *)sisusb->scrbuf, sisusb->scrbuf_size);
690 sisusbcon_set_start_address(sisusb, c);
692 mutex_unlock(&sisusb->lock);
695 /* Interface routine */
696 static void
697 sisusbcon_cursor(struct vc_data *c, int mode)
699 struct sisusb_usb_data *sisusb;
700 int from, to, baseline;
702 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
703 if (!sisusb)
704 return;
706 /* sisusb->lock is down */
708 if (sisusb_is_inactive(c, sisusb)) {
709 mutex_unlock(&sisusb->lock);
710 return;
713 if (c->vc_origin != c->vc_visible_origin) {
714 c->vc_visible_origin = c->vc_origin;
715 sisusbcon_set_start_address(sisusb, c);
718 if (mode == CM_ERASE) {
719 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
720 sisusb->sisusb_cursor_size_to = -1;
721 mutex_unlock(&sisusb->lock);
722 return;
725 sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
727 baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
729 switch (c->vc_cursor_type & 0x0f) {
730 case CUR_BLOCK: from = 1;
731 to = c->vc_font.height;
732 break;
733 case CUR_TWO_THIRDS: from = c->vc_font.height / 3;
734 to = baseline;
735 break;
736 case CUR_LOWER_HALF: from = c->vc_font.height / 2;
737 to = baseline;
738 break;
739 case CUR_LOWER_THIRD: from = (c->vc_font.height * 2) / 3;
740 to = baseline;
741 break;
742 case CUR_NONE: from = 31;
743 to = 30;
744 break;
745 default:
746 case CUR_UNDERLINE: from = baseline - 1;
747 to = baseline;
748 break;
751 if (sisusb->sisusb_cursor_size_from != from ||
752 sisusb->sisusb_cursor_size_to != to) {
754 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
755 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
757 sisusb->sisusb_cursor_size_from = from;
758 sisusb->sisusb_cursor_size_to = to;
761 mutex_unlock(&sisusb->lock);
764 static bool
765 sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
766 unsigned int t, unsigned int b, enum con_scroll dir,
767 unsigned int lines)
769 int cols = sisusb->sisusb_num_columns;
770 int length = ((b - t) * cols) * 2;
771 u16 eattr = c->vc_video_erase_char;
773 /* sisusb->lock is down */
775 /* Scroll an area which does not match the
776 * visible screen's dimensions. This needs
777 * to be done separately, as it does not
778 * use hardware panning.
781 switch (dir) {
783 case SM_UP:
784 memmove(sisusb_vaddr(sisusb, c, 0, t),
785 sisusb_vaddr(sisusb, c, 0, t + lines),
786 (b - t - lines) * cols * 2);
787 sisusbcon_memsetw(sisusb_vaddr(sisusb, c, 0, b - lines),
788 eattr, lines * cols * 2);
789 break;
791 case SM_DOWN:
792 memmove(sisusb_vaddr(sisusb, c, 0, t + lines),
793 sisusb_vaddr(sisusb, c, 0, t),
794 (b - t - lines) * cols * 2);
795 sisusbcon_memsetw(sisusb_vaddr(sisusb, c, 0, t), eattr,
796 lines * cols * 2);
797 break;
800 sisusb_copy_memory(sisusb, sisusb_vaddr(sisusb, c, 0, t),
801 sisusb_haddr(sisusb, c, 0, t), length);
803 mutex_unlock(&sisusb->lock);
805 return true;
808 /* Interface routine */
809 static bool
810 sisusbcon_scroll(struct vc_data *c, unsigned int t, unsigned int b,
811 enum con_scroll dir, unsigned int lines)
813 struct sisusb_usb_data *sisusb;
814 u16 eattr = c->vc_video_erase_char;
815 int copyall = 0;
816 unsigned long oldorigin;
817 unsigned int delta = lines * c->vc_size_row;
819 /* Returning != 0 means we have done the scrolling successfully.
820 * Returning 0 makes vt do the scrolling on its own.
821 * Note that con_scroll is only called if the console is
822 * visible. In that case, the origin should be our buffer,
823 * not the vt's private one.
826 if (!lines)
827 return true;
829 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
830 if (!sisusb)
831 return false;
833 /* sisusb->lock is down */
835 if (sisusb_is_inactive(c, sisusb)) {
836 mutex_unlock(&sisusb->lock);
837 return false;
840 /* Special case */
841 if (t || b != c->vc_rows)
842 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
844 if (c->vc_origin != c->vc_visible_origin) {
845 c->vc_visible_origin = c->vc_origin;
846 sisusbcon_set_start_address(sisusb, c);
849 /* limit amount to maximum realistic size */
850 if (lines > c->vc_rows)
851 lines = c->vc_rows;
853 oldorigin = c->vc_origin;
855 switch (dir) {
857 case SM_UP:
859 if (c->vc_scr_end + delta >=
860 sisusb->scrbuf + sisusb->scrbuf_size) {
861 memcpy((u16 *)sisusb->scrbuf,
862 (u16 *)(oldorigin + delta),
863 c->vc_screenbuf_size - delta);
864 c->vc_origin = sisusb->scrbuf;
865 sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
866 copyall = 1;
867 } else
868 c->vc_origin += delta;
870 sisusbcon_memsetw(
871 (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
872 eattr, delta);
874 break;
876 case SM_DOWN:
878 if (oldorigin - delta < sisusb->scrbuf) {
879 memmove((void *)sisusb->scrbuf + sisusb->scrbuf_size -
880 c->vc_screenbuf_size + delta,
881 (u16 *)oldorigin,
882 c->vc_screenbuf_size - delta);
883 c->vc_origin = sisusb->scrbuf +
884 sisusb->scrbuf_size -
885 c->vc_screenbuf_size;
886 sisusb->con_rolled_over = 0;
887 copyall = 1;
888 } else
889 c->vc_origin -= delta;
891 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
893 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
895 break;
898 if (copyall)
899 sisusb_copy_memory(sisusb,
900 (char *)c->vc_origin,
901 sisusb_haddr(sisusb, c, 0, 0),
902 c->vc_screenbuf_size);
903 else if (dir == SM_UP)
904 sisusb_copy_memory(sisusb,
905 (char *)c->vc_origin + c->vc_screenbuf_size - delta,
906 sisusb_haddr(sisusb, c, 0, 0) +
907 c->vc_screenbuf_size - delta,
908 delta);
909 else
910 sisusb_copy_memory(sisusb,
911 (char *)c->vc_origin,
912 sisusb_haddr(sisusb, c, 0, 0),
913 delta);
915 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
916 c->vc_visible_origin = c->vc_origin;
918 sisusbcon_set_start_address(sisusb, c);
920 c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
922 mutex_unlock(&sisusb->lock);
924 return true;
927 /* Interface routine */
928 static int
929 sisusbcon_set_origin(struct vc_data *c)
931 struct sisusb_usb_data *sisusb;
933 /* Returning != 0 means we were successful.
934 * Returning 0 will vt make to use its own
935 * screenbuffer as the origin.
938 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
939 if (!sisusb)
940 return 0;
942 /* sisusb->lock is down */
944 if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
945 mutex_unlock(&sisusb->lock);
946 return 0;
949 c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
951 sisusbcon_set_start_address(sisusb, c);
953 sisusb->con_rolled_over = 0;
955 mutex_unlock(&sisusb->lock);
957 return true;
960 /* Interface routine */
961 static int
962 sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows,
963 unsigned int user)
965 struct sisusb_usb_data *sisusb;
966 int fh;
968 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
969 if (!sisusb)
970 return -ENODEV;
972 fh = sisusb->current_font_height;
974 mutex_unlock(&sisusb->lock);
976 /* We are quite unflexible as regards resizing. The vt code
977 * handles sizes where the line length isn't equal the pitch
978 * quite badly. As regards the rows, our panning tricks only
979 * work well if the number of rows equals the visible number
980 * of rows.
983 if (newcols != 80 || c->vc_scan_lines / fh != newrows)
984 return -EINVAL;
986 return 0;
990 sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
991 u8 *arg, int cmapsz, int ch512, int dorecalc,
992 struct vc_data *c, int fh, int uplock)
994 int font_select = 0x00, i, err = 0;
995 u32 offset = 0;
996 u8 dummy;
998 /* sisusb->lock is down */
1001 * The default font is kept in slot 0.
1002 * A user font is loaded in slot 2 (256 ch)
1003 * or 2+3 (512 ch).
1006 if ((slot != 0 && slot != 2) || !fh) {
1007 if (uplock)
1008 mutex_unlock(&sisusb->lock);
1009 return -EINVAL;
1012 if (set)
1013 sisusb->font_slot = slot;
1015 /* Default font is always 256 */
1016 if (slot == 0)
1017 ch512 = 0;
1018 else
1019 offset = 4 * cmapsz;
1021 font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1023 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1024 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1025 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1026 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1028 if (err)
1029 goto font_op_error;
1031 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1032 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1033 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1035 if (err)
1036 goto font_op_error;
1038 if (arg) {
1039 if (set)
1040 for (i = 0; i < cmapsz; i++) {
1041 err |= sisusb_writeb(sisusb,
1042 sisusb->vrambase + offset + i,
1043 arg[i]);
1044 if (err)
1045 break;
1047 else
1048 for (i = 0; i < cmapsz; i++) {
1049 err |= sisusb_readb(sisusb,
1050 sisusb->vrambase + offset + i,
1051 &arg[i]);
1052 if (err)
1053 break;
1057 * In 512-character mode, the character map is not contiguous if
1058 * we want to remain EGA compatible -- which we do
1061 if (ch512) {
1062 if (set)
1063 for (i = 0; i < cmapsz; i++) {
1064 err |= sisusb_writeb(sisusb,
1065 sisusb->vrambase + offset +
1066 (2 * cmapsz) + i,
1067 arg[cmapsz + i]);
1068 if (err)
1069 break;
1071 else
1072 for (i = 0; i < cmapsz; i++) {
1073 err |= sisusb_readb(sisusb,
1074 sisusb->vrambase + offset +
1075 (2 * cmapsz) + i,
1076 &arg[cmapsz + i]);
1077 if (err)
1078 break;
1083 if (err)
1084 goto font_op_error;
1086 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1087 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1088 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1089 if (set)
1090 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1091 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1093 if (err)
1094 goto font_op_error;
1096 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1097 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1098 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1100 if (err)
1101 goto font_op_error;
1103 if ((set) && (ch512 != sisusb->current_font_512)) {
1105 /* Font is shared among all our consoles.
1106 * And so is the hi_font_mask.
1108 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1109 struct vc_data *d = vc_cons[i].d;
1110 if (d && d->vc_sw == &sisusb_con)
1111 d->vc_hi_font_mask = ch512 ? 0x0800 : 0;
1114 sisusb->current_font_512 = ch512;
1116 /* color plane enable register:
1117 256-char: enable intensity bit
1118 512-char: disable intensity bit */
1119 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1120 sisusb_setreg(sisusb, SISAR, 0x12);
1121 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1123 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1124 sisusb_setreg(sisusb, SISAR, 0x20);
1125 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1128 if (dorecalc) {
1131 * Adjust the screen to fit a font of a certain height
1134 unsigned char ovr, vde, fsr;
1135 int rows = 0, maxscan = 0;
1137 if (c) {
1139 /* Number of video rows */
1140 rows = c->vc_scan_lines / fh;
1141 /* Scan lines to actually display-1 */
1142 maxscan = rows * fh - 1;
1144 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1145 rows, maxscan, fh, c->vc_scan_lines);*/
1147 sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1148 vde = maxscan & 0xff;
1149 ovr = (ovr & 0xbd) |
1150 ((maxscan & 0x100) >> 7) |
1151 ((maxscan & 0x200) >> 3);
1152 sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1153 sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1157 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1158 fsr = (fsr & 0xe0) | (fh - 1);
1159 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1160 sisusb->current_font_height = fh;
1162 sisusb->sisusb_cursor_size_from = -1;
1163 sisusb->sisusb_cursor_size_to = -1;
1167 if (uplock)
1168 mutex_unlock(&sisusb->lock);
1170 if (dorecalc && c) {
1171 int rows = c->vc_scan_lines / fh;
1173 /* Now adjust our consoles' size */
1175 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1176 struct vc_data *vc = vc_cons[i].d;
1178 if (vc && vc->vc_sw == &sisusb_con) {
1179 if (con_is_visible(vc)) {
1180 vc->vc_sw->con_cursor(vc, CM_DRAW);
1182 vc->vc_font.height = fh;
1183 vc_resize(vc, 0, rows);
1188 return 0;
1190 font_op_error:
1191 if (uplock)
1192 mutex_unlock(&sisusb->lock);
1194 return -EIO;
1197 /* Interface routine */
1198 static int
1199 sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1200 unsigned int flags)
1202 struct sisusb_usb_data *sisusb;
1203 unsigned charcount = font->charcount;
1205 if (font->width != 8 || (charcount != 256 && charcount != 512))
1206 return -EINVAL;
1208 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1209 if (!sisusb)
1210 return -ENODEV;
1212 /* sisusb->lock is down */
1214 /* Save the user-provided font into a buffer. This
1215 * is used for restoring text mode after quitting
1216 * from X and for the con_getfont routine.
1218 if (sisusb->font_backup) {
1219 if (sisusb->font_backup_size < charcount) {
1220 vfree(sisusb->font_backup);
1221 sisusb->font_backup = NULL;
1225 if (!sisusb->font_backup)
1226 sisusb->font_backup = vmalloc(array_size(charcount, 32));
1228 if (sisusb->font_backup) {
1229 memcpy(sisusb->font_backup, font->data, charcount * 32);
1230 sisusb->font_backup_size = charcount;
1231 sisusb->font_backup_height = font->height;
1232 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1235 /* do_font_op ups sisusb->lock */
1237 return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1238 8192, (charcount == 512),
1239 (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1240 c, font->height, 1);
1243 /* Interface routine */
1244 static int
1245 sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1247 struct sisusb_usb_data *sisusb;
1249 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1250 if (!sisusb)
1251 return -ENODEV;
1253 /* sisusb->lock is down */
1255 font->width = 8;
1256 font->height = c->vc_font.height;
1257 font->charcount = 256;
1259 if (!font->data) {
1260 mutex_unlock(&sisusb->lock);
1261 return 0;
1264 if (!sisusb->font_backup) {
1265 mutex_unlock(&sisusb->lock);
1266 return -ENODEV;
1269 /* Copy 256 chars only, like vgacon */
1270 memcpy(font->data, sisusb->font_backup, 256 * 32);
1272 mutex_unlock(&sisusb->lock);
1274 return 0;
1278 * The console `switch' structure for the sisusb console
1281 static const struct consw sisusb_con = {
1282 .owner = THIS_MODULE,
1283 .con_startup = sisusbcon_startup,
1284 .con_init = sisusbcon_init,
1285 .con_deinit = sisusbcon_deinit,
1286 .con_clear = sisusbcon_clear,
1287 .con_putc = sisusbcon_putc,
1288 .con_putcs = sisusbcon_putcs,
1289 .con_cursor = sisusbcon_cursor,
1290 .con_scroll = sisusbcon_scroll,
1291 .con_switch = sisusbcon_switch,
1292 .con_blank = sisusbcon_blank,
1293 .con_font_set = sisusbcon_font_set,
1294 .con_font_get = sisusbcon_font_get,
1295 .con_set_palette = sisusbcon_set_palette,
1296 .con_scrolldelta = sisusbcon_scrolldelta,
1297 .con_build_attr = sisusbcon_build_attr,
1298 .con_invert_region = sisusbcon_invert_region,
1299 .con_set_origin = sisusbcon_set_origin,
1300 .con_save_screen = sisusbcon_save_screen,
1301 .con_resize = sisusbcon_resize,
1304 /* Our very own dummy console driver */
1306 static const char *sisusbdummycon_startup(void)
1308 return "SISUSBVGADUMMY";
1311 static void sisusbdummycon_init(struct vc_data *vc, int init)
1313 vc->vc_can_do_color = 1;
1314 if (init) {
1315 vc->vc_cols = 80;
1316 vc->vc_rows = 25;
1317 } else
1318 vc_resize(vc, 80, 25);
1321 static void sisusbdummycon_deinit(struct vc_data *vc) { }
1322 static void sisusbdummycon_clear(struct vc_data *vc, int sy, int sx,
1323 int height, int width) { }
1324 static void sisusbdummycon_putc(struct vc_data *vc, int c, int ypos,
1325 int xpos) { }
1326 static void sisusbdummycon_putcs(struct vc_data *vc, const unsigned short *s,
1327 int count, int ypos, int xpos) { }
1328 static void sisusbdummycon_cursor(struct vc_data *vc, int mode) { }
1330 static bool sisusbdummycon_scroll(struct vc_data *vc, unsigned int top,
1331 unsigned int bottom, enum con_scroll dir,
1332 unsigned int lines)
1334 return false;
1337 static int sisusbdummycon_switch(struct vc_data *vc)
1339 return 0;
1342 static int sisusbdummycon_blank(struct vc_data *vc, int blank, int mode_switch)
1344 return 0;
1347 static int sisusbdummycon_font_set(struct vc_data *vc,
1348 struct console_font *font,
1349 unsigned int flags)
1351 return 0;
1354 static int sisusbdummycon_font_default(struct vc_data *vc,
1355 struct console_font *font, char *name)
1357 return 0;
1360 static int sisusbdummycon_font_copy(struct vc_data *vc, int con)
1362 return 0;
1365 static const struct consw sisusb_dummy_con = {
1366 .owner = THIS_MODULE,
1367 .con_startup = sisusbdummycon_startup,
1368 .con_init = sisusbdummycon_init,
1369 .con_deinit = sisusbdummycon_deinit,
1370 .con_clear = sisusbdummycon_clear,
1371 .con_putc = sisusbdummycon_putc,
1372 .con_putcs = sisusbdummycon_putcs,
1373 .con_cursor = sisusbdummycon_cursor,
1374 .con_scroll = sisusbdummycon_scroll,
1375 .con_switch = sisusbdummycon_switch,
1376 .con_blank = sisusbdummycon_blank,
1377 .con_font_set = sisusbdummycon_font_set,
1378 .con_font_default = sisusbdummycon_font_default,
1379 .con_font_copy = sisusbdummycon_font_copy,
1383 sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1385 int i, ret;
1387 mutex_lock(&sisusb->lock);
1389 /* Erm.. that should not happen */
1390 if (sisusb->haveconsole || !sisusb->SiS_Pr) {
1391 mutex_unlock(&sisusb->lock);
1392 return 1;
1395 sisusb->con_first = first;
1396 sisusb->con_last = last;
1398 if (first > last ||
1399 first > MAX_NR_CONSOLES ||
1400 last > MAX_NR_CONSOLES) {
1401 mutex_unlock(&sisusb->lock);
1402 return 1;
1405 /* If gfxcore not initialized or no consoles given, quit graciously */
1406 if (!sisusb->gfxinit || first < 1 || last < 1) {
1407 mutex_unlock(&sisusb->lock);
1408 return 0;
1411 sisusb->sisusb_cursor_loc = -1;
1412 sisusb->sisusb_cursor_size_from = -1;
1413 sisusb->sisusb_cursor_size_to = -1;
1415 /* Set up text mode (and upload default font) */
1416 if (sisusb_reset_text_mode(sisusb, 1)) {
1417 mutex_unlock(&sisusb->lock);
1418 dev_err(&sisusb->sisusb_dev->dev, "Failed to set up text mode\n");
1419 return 1;
1422 /* Initialize some gfx registers */
1423 sisusb_initialize(sisusb);
1425 for (i = first - 1; i <= last - 1; i++) {
1426 /* Save sisusb for our interface routines */
1427 mysisusbs[i] = sisusb;
1430 /* Initial console setup */
1431 sisusb->sisusb_num_columns = 80;
1433 /* Use a 32K buffer (matches b8000-bffff area) */
1434 sisusb->scrbuf_size = 32 * 1024;
1436 /* Allocate screen buffer */
1437 if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
1438 mutex_unlock(&sisusb->lock);
1439 dev_err(&sisusb->sisusb_dev->dev, "Failed to allocate screen buffer\n");
1440 return 1;
1443 mutex_unlock(&sisusb->lock);
1445 /* Now grab the desired console(s) */
1446 console_lock();
1447 ret = do_take_over_console(&sisusb_con, first - 1, last - 1, 0);
1448 console_unlock();
1449 if (!ret)
1450 sisusb->haveconsole = 1;
1451 else {
1452 for (i = first - 1; i <= last - 1; i++)
1453 mysisusbs[i] = NULL;
1456 return ret;
1459 void
1460 sisusb_console_exit(struct sisusb_usb_data *sisusb)
1462 int i;
1464 /* This is called if the device is disconnected
1465 * and while disconnect and lock semaphores
1466 * are up. This should be save because we
1467 * can't lose our sisusb any other way but by
1468 * disconnection (and hence, the disconnect
1469 * sema is for protecting all other access
1470 * functions from disconnection, not the
1471 * other way round).
1474 /* Now what do we do in case of disconnection:
1475 * One alternative would be to simply call
1476 * give_up_console(). Nah, not a good idea.
1477 * give_up_console() is obviously buggy as it
1478 * only discards the consw pointer from the
1479 * driver_map, but doesn't adapt vc->vc_sw
1480 * of the affected consoles. Hence, the next
1481 * call to any of the console functions will
1482 * eventually take a trip to oops county.
1483 * Also, give_up_console for some reason
1484 * doesn't decrement our module refcount.
1485 * Instead, we switch our consoles to a private
1486 * dummy console. This, of course, keeps our
1487 * refcount up as well, but it works perfectly.
1490 if (sisusb->haveconsole) {
1491 for (i = 0; i < MAX_NR_CONSOLES; i++)
1492 if (sisusb->havethisconsole[i]) {
1493 console_lock();
1494 do_take_over_console(&sisusb_dummy_con, i, i, 0);
1495 console_unlock();
1496 /* At this point, con_deinit for all our
1497 * consoles is executed by do_take_over_console().
1500 sisusb->haveconsole = 0;
1503 vfree((void *)sisusb->scrbuf);
1504 sisusb->scrbuf = 0;
1506 vfree(sisusb->font_backup);
1507 sisusb->font_backup = NULL;
1510 void __init sisusb_init_concode(void)
1512 int i;
1514 for (i = 0; i < MAX_NR_CONSOLES; i++)
1515 mysisusbs[i] = NULL;