Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/pci-2.6
[linux-2.6] / drivers / usb / misc / sisusbvga / sisusb_con.c
1 /*
2  * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles
3  *
4  * VGA text mode console part
5  *
6  * Copyright (C) 2005 by Thomas Winischhofer, Vienna, Austria
7  *
8  * If distributed as part of the Linux kernel, this code is licensed under the
9  * terms of the GPL v2.
10  *
11  * Otherwise, the following license terms apply:
12  *
13  * * Redistribution and use in source and binary forms, with or without
14  * * modification, are permitted provided that the following conditions
15  * * are met:
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.
23  * *
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.
34  *
35  * Author: Thomas Winischhofer <thomas@winischhofer.net>
36  *
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
41  *                          1995  Jay Estabrook
42  *
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
46  * detects this state.
47  *
48  */
49
50 #include <linux/config.h>
51 #include <linux/mutex.h>
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/signal.h>
55 #include <linux/sched.h>
56 #include <linux/fs.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/slab.h>
63 #include <linux/vt_kern.h>
64 #include <linux/selection.h>
65 #include <linux/spinlock.h>
66 #include <linux/kref.h>
67 #include <linux/smp_lock.h>
68 #include <linux/ioport.h>
69 #include <linux/interrupt.h>
70 #include <linux/vmalloc.h>
71
72 #include "sisusb.h"
73 #include "sisusb_init.h"
74
75 #ifdef INCL_SISUSB_CON
76
77 #define sisusbcon_writew(val, addr)     (*(addr) = (val))
78 #define sisusbcon_readw(addr)           (*(addr))
79 #define sisusbcon_memmovew(d, s, c)     memmove(d, s, c)
80 #define sisusbcon_memcpyw(d, s, c)      memcpy(d, s, c)
81
82 /* vc_data -> sisusb conversion table */
83 static struct sisusb_usb_data *mysisusbs[MAX_NR_CONSOLES];
84
85 /* Forward declaration */
86 static const struct consw sisusb_con;
87
88 static inline void
89 sisusbcon_memsetw(u16 *s, u16 c, unsigned int count)
90 {
91         count /= 2;
92         while (count--)
93                 sisusbcon_writew(c, s++);
94 }
95
96 static inline void
97 sisusb_initialize(struct sisusb_usb_data *sisusb)
98 {
99         /* Reset cursor and start address */
100         if (sisusb_setidxreg(sisusb, SISCR, 0x0c, 0x00))
101                 return;
102         if (sisusb_setidxreg(sisusb, SISCR, 0x0d, 0x00))
103                 return;
104         if (sisusb_setidxreg(sisusb, SISCR, 0x0e, 0x00))
105                 return;
106         sisusb_setidxreg(sisusb, SISCR, 0x0f, 0x00);
107 }
108
109 static inline void
110 sisusbcon_set_start_address(struct sisusb_usb_data *sisusb, struct vc_data *c)
111 {
112         sisusb->cur_start_addr = (c->vc_visible_origin - sisusb->scrbuf) / 2;
113
114         sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8));
115         sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));
116 }
117
118 void
119 sisusb_set_cursor(struct sisusb_usb_data *sisusb, unsigned int location)
120 {
121         if (sisusb->sisusb_cursor_loc == location)
122                 return;
123
124         sisusb->sisusb_cursor_loc = location;
125
126         /* Hardware bug: Text cursor appears twice or not at all
127          * at some positions. Work around it with the cursor skew
128          * bits.
129          */
130
131         if ((location & 0x0007) == 0x0007) {
132                 sisusb->bad_cursor_pos = 1;
133                 location--;
134                 if (sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0x1f, 0x20))
135                         return;
136         } else if (sisusb->bad_cursor_pos) {
137                 if (sisusb_setidxregand(sisusb, SISCR, 0x0b, 0x1f))
138                         return;
139                 sisusb->bad_cursor_pos = 0;
140         }
141
142         if (sisusb_setidxreg(sisusb, SISCR, 0x0e, (location >> 8)))
143                 return;
144         sisusb_setidxreg(sisusb, SISCR, 0x0f, (location & 0xff));
145 }
146
147 static inline struct sisusb_usb_data *
148 sisusb_get_sisusb(unsigned short console)
149 {
150         return mysisusbs[console];
151 }
152
153 static inline int
154 sisusb_sisusb_valid(struct sisusb_usb_data *sisusb)
155 {
156         if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev)
157                 return 0;
158
159         return 1;
160 }
161
162 static struct sisusb_usb_data *
163 sisusb_get_sisusb_lock_and_check(unsigned short console)
164 {
165         struct sisusb_usb_data *sisusb;
166
167         /* We can't handle console calls in non-schedulable
168          * context due to our locks and the USB transport.
169          * So we simply ignore them. This should only affect
170          * some calls to printk.
171          */
172         if (in_atomic())
173                 return NULL;
174
175         if (!(sisusb = sisusb_get_sisusb(console)))
176                 return NULL;
177
178         mutex_lock(&sisusb->lock);
179
180         if (!sisusb_sisusb_valid(sisusb) ||
181             !sisusb->havethisconsole[console]) {
182                 mutex_unlock(&sisusb->lock);
183                 return NULL;
184         }
185
186         return sisusb;
187 }
188
189 static int
190 sisusb_is_inactive(struct vc_data *c, struct sisusb_usb_data *sisusb)
191 {
192         if (sisusb->is_gfx ||
193             sisusb->textmodedestroyed ||
194             c->vc_mode != KD_TEXT)
195                 return 1;
196
197         return 0;
198 }
199
200 /* con_startup console interface routine */
201 static const char *
202 sisusbcon_startup(void)
203 {
204         return "SISUSBCON";
205 }
206
207 /* con_init console interface routine */
208 static void
209 sisusbcon_init(struct vc_data *c, int init)
210 {
211         struct sisusb_usb_data *sisusb;
212         int cols, rows;
213
214         /* This is called by take_over_console(),
215          * ie by us/under our control. It is
216          * only called after text mode and fonts
217          * are set up/restored.
218          */
219
220         mutex_lock(&disconnect_mutex);
221
222         if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
223                 mutex_unlock(&disconnect_mutex);
224                 return;
225         }
226
227         mutex_lock(&sisusb->lock);
228
229         if (!sisusb_sisusb_valid(sisusb)) {
230                 mutex_unlock(&sisusb->lock);
231                 mutex_unlock(&disconnect_mutex);
232                 return;
233         }
234
235         c->vc_can_do_color = 1;
236
237         c->vc_complement_mask = 0x7700;
238
239         c->vc_hi_font_mask = sisusb->current_font_512 ? 0x0800 : 0;
240
241         sisusb->haveconsole = 1;
242
243         sisusb->havethisconsole[c->vc_num] = 1;
244
245         /* We only support 640x400 */
246         c->vc_scan_lines = 400;
247
248         c->vc_font.height = sisusb->current_font_height;
249
250         /* We only support width = 8 */
251         cols = 80;
252         rows = c->vc_scan_lines / c->vc_font.height;
253
254         /* Increment usage count for our sisusb.
255          * Doing so saves us from upping/downing
256          * the disconnect semaphore; we can't
257          * lose our sisusb until this is undone
258          * in con_deinit. For all other console
259          * interface functions, it suffices to
260          * use sisusb->lock and do a quick check
261          * of sisusb for device disconnection.
262          */
263         kref_get(&sisusb->kref);
264
265         if (!*c->vc_uni_pagedir_loc)
266                 con_set_default_unimap(c);
267
268         mutex_unlock(&sisusb->lock);
269
270         mutex_unlock(&disconnect_mutex);
271
272         if (init) {
273                 c->vc_cols = cols;
274                 c->vc_rows = rows;
275         } else
276                 vc_resize(c, cols, rows);
277 }
278
279 /* con_deinit console interface routine */
280 static void
281 sisusbcon_deinit(struct vc_data *c)
282 {
283         struct sisusb_usb_data *sisusb;
284         int i;
285
286         /* This is called by take_over_console()
287          * and others, ie not under our control.
288          */
289
290         mutex_lock(&disconnect_mutex);
291
292         if (!(sisusb = sisusb_get_sisusb(c->vc_num))) {
293                 mutex_unlock(&disconnect_mutex);
294                 return;
295         }
296
297         mutex_lock(&sisusb->lock);
298
299         /* Clear ourselves in mysisusbs */
300         mysisusbs[c->vc_num] = NULL;
301
302         sisusb->havethisconsole[c->vc_num] = 0;
303
304         /* Free our font buffer if all consoles are gone */
305         if (sisusb->font_backup) {
306                 for(i = 0; i < MAX_NR_CONSOLES; i++) {
307                         if (sisusb->havethisconsole[c->vc_num])
308                                 break;
309                 }
310                 if (i == MAX_NR_CONSOLES) {
311                         vfree(sisusb->font_backup);
312                         sisusb->font_backup = NULL;
313                 }
314         }
315
316         mutex_unlock(&sisusb->lock);
317
318         /* decrement the usage count on our sisusb */
319         kref_put(&sisusb->kref, sisusb_delete);
320
321         mutex_unlock(&disconnect_mutex);
322 }
323
324 /* interface routine */
325 static u8
326 sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
327                             u8 blink, u8 underline, u8 reverse)
328 {
329         u8 attr = color;
330
331         if (underline)
332                 attr = (attr & 0xf0) | c->vc_ulcolor;
333         else if (intensity == 0)
334                 attr = (attr & 0xf0) | c->vc_halfcolor;
335
336         if (reverse)
337                 attr = ((attr) & 0x88) |
338                        ((((attr) >> 4) |
339                        ((attr) << 4)) & 0x77);
340
341         if (blink)
342                 attr ^= 0x80;
343
344         if (intensity == 2)
345                 attr ^= 0x08;
346
347         return attr;
348 }
349
350 /* Interface routine */
351 static void
352 sisusbcon_invert_region(struct vc_data *vc, u16 *p, int count)
353 {
354         /* Invert a region. This is called with a pointer
355          * to the console's internal screen buffer. So we
356          * simply do the inversion there and rely on
357          * a call to putc(s) to update the real screen.
358          */
359
360         while (count--) {
361                 u16 a = sisusbcon_readw(p);
362
363                 a = ((a) & 0x88ff)        |
364                     (((a) & 0x7000) >> 4) |
365                     (((a) & 0x0700) << 4);
366
367                 sisusbcon_writew(a, p++);
368         }
369 }
370
371 #define SISUSB_VADDR(x,y) \
372         ((u16 *)c->vc_origin + \
373         (y) * sisusb->sisusb_num_columns + \
374         (x))
375
376 #define SISUSB_HADDR(x,y) \
377         ((u16 *)(sisusb->vrambase + (c->vc_origin - sisusb->scrbuf)) + \
378         (y) * sisusb->sisusb_num_columns + \
379         (x))
380
381 /* Interface routine */
382 static void
383 sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
384 {
385         struct sisusb_usb_data *sisusb;
386         ssize_t written;
387
388         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
389                 return;
390
391         /* sisusb->lock is down */
392
393         /* Don't need to put the character into buffer ourselves,
394          * because the vt does this BEFORE calling us.
395          */
396 #if 0
397         sisusbcon_writew(ch, SISUSB_VADDR(x, y));
398 #endif
399
400         if (sisusb_is_inactive(c, sisusb)) {
401                 mutex_unlock(&sisusb->lock);
402                 return;
403         }
404
405
406         sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
407                                 (u32)SISUSB_HADDR(x, y), 2, &written);
408
409         mutex_unlock(&sisusb->lock);
410 }
411
412 /* Interface routine */
413 static void
414 sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
415                          int count, int y, int x)
416 {
417         struct sisusb_usb_data *sisusb;
418         ssize_t written;
419         u16 *dest;
420         int i;
421
422         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
423                 return;
424
425         /* sisusb->lock is down */
426
427         /* Need to put the characters into the buffer ourselves,
428          * because the vt does this AFTER calling us.
429          */
430
431         dest = SISUSB_VADDR(x, y);
432
433         for (i = count; i > 0; i--)
434                 sisusbcon_writew(sisusbcon_readw(s++), dest++);
435
436         if (sisusb_is_inactive(c, sisusb)) {
437                 mutex_unlock(&sisusb->lock);
438                 return;
439         }
440
441         sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
442                                 (u32)SISUSB_HADDR(x, y), count * 2, &written);
443
444         mutex_unlock(&sisusb->lock);
445 }
446
447 /* Interface routine */
448 static void
449 sisusbcon_clear(struct vc_data *c, int y, int x, int height, int width)
450 {
451         struct sisusb_usb_data *sisusb;
452         u16 eattr = c->vc_video_erase_char;
453         ssize_t written;
454         int i, length, cols;
455         u16 *dest;
456
457         if (width <= 0 || height <= 0)
458                 return;
459
460         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
461                 return;
462
463         /* sisusb->lock is down */
464
465         /* Need to clear buffer ourselves, because the vt does
466          * this AFTER calling us.
467          */
468
469         dest = SISUSB_VADDR(x, y);
470
471         cols = sisusb->sisusb_num_columns;
472
473         if (width > cols)
474                 width = cols;
475
476         if (x == 0 && width >= c->vc_cols) {
477
478                 sisusbcon_memsetw(dest, eattr, height * cols * 2);
479
480         } else {
481
482                 for (i = height; i > 0; i--, dest += cols)
483                         sisusbcon_memsetw(dest, eattr, width * 2);
484
485         }
486
487         if (sisusb_is_inactive(c, sisusb)) {
488                 mutex_unlock(&sisusb->lock);
489                 return;
490         }
491
492         length = ((height * cols) - x - (cols - width - x)) * 2;
493
494
495         sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(x, y),
496                                 (u32)SISUSB_HADDR(x, y), length, &written);
497
498         mutex_unlock(&sisusb->lock);
499 }
500
501 /* Interface routine */
502 static void
503 sisusbcon_bmove(struct vc_data *c, int sy, int sx,
504                          int dy, int dx, int height, int width)
505 {
506         struct sisusb_usb_data *sisusb;
507         ssize_t written;
508         int cols, length;
509 #if 0
510         u16 *src, *dest;
511         int i;
512 #endif
513
514         if (width <= 0 || height <= 0)
515                 return;
516
517         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
518                 return;
519
520         /* sisusb->lock is down */
521
522         cols = sisusb->sisusb_num_columns;
523
524         /* Don't need to move data outselves, because
525          * vt does this BEFORE calling us.
526          * This is only used by vt's insert/deletechar.
527          */
528 #if 0
529         if (sx == 0 && dx == 0 && width >= c->vc_cols && width <= cols) {
530
531                 sisusbcon_memmovew(SISUSB_VADDR(0, dy), SISUSB_VADDR(0, sy),
532                                         height * width * 2);
533
534         } else if (dy < sy || (dy == sy && dx < sx)) {
535
536                 src  = SISUSB_VADDR(sx, sy);
537                 dest = SISUSB_VADDR(dx, dy);
538
539                 for (i = height; i > 0; i--) {
540                         sisusbcon_memmovew(dest, src, width * 2);
541                         src  += cols;
542                         dest += cols;
543                 }
544
545         } else {
546
547                 src  = SISUSB_VADDR(sx, sy + height - 1);
548                 dest = SISUSB_VADDR(dx, dy + height - 1);
549
550                 for (i = height; i > 0; i--) {
551                         sisusbcon_memmovew(dest, src, width * 2);
552                         src  -= cols;
553                         dest -= cols;
554                 }
555
556         }
557 #endif
558
559         if (sisusb_is_inactive(c, sisusb)) {
560                 mutex_unlock(&sisusb->lock);
561                 return;
562         }
563
564         length = ((height * cols) - dx - (cols - width - dx)) * 2;
565
566
567         sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(dx, dy),
568                                 (u32)SISUSB_HADDR(dx, dy), length, &written);
569
570         mutex_unlock(&sisusb->lock);
571 }
572
573 /* interface routine */
574 static int
575 sisusbcon_switch(struct vc_data *c)
576 {
577         struct sisusb_usb_data *sisusb;
578         ssize_t written;
579         int length;
580
581         /* Returnvalue 0 means we have fully restored screen,
582          *      and vt doesn't need to call do_update_region().
583          * Returnvalue != 0 naturally means the opposite.
584          */
585
586         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
587                 return 0;
588
589         /* sisusb->lock is down */
590
591         /* Don't write to screen if in gfx mode */
592         if (sisusb_is_inactive(c, sisusb)) {
593                 mutex_unlock(&sisusb->lock);
594                 return 0;
595         }
596
597         /* That really should not happen. It would mean we are
598          * being called while the vc is using its private buffer
599          * as origin.
600          */
601         if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
602                 mutex_unlock(&sisusb->lock);
603                 printk(KERN_DEBUG "sisusb: ASSERT ORIGIN != SCREENBUF!\n");
604                 return 0;
605         }
606
607         /* Check that we don't copy too much */
608         length = min((int)c->vc_screenbuf_size,
609                         (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
610
611         /* Restore the screen contents */
612         sisusbcon_memcpyw((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf,
613                                                                 length);
614
615         sisusb_copy_memory(sisusb, (unsigned char *)c->vc_origin,
616                                 (u32)SISUSB_HADDR(0, 0),
617                                 length, &written);
618
619         mutex_unlock(&sisusb->lock);
620
621         return 0;
622 }
623
624 /* interface routine */
625 static void
626 sisusbcon_save_screen(struct vc_data *c)
627 {
628         struct sisusb_usb_data *sisusb;
629         int length;
630
631         /* Save the current screen contents to vc's private
632          * buffer.
633          */
634
635         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
636                 return;
637
638         /* sisusb->lock is down */
639
640         if (sisusb_is_inactive(c, sisusb)) {
641                 mutex_unlock(&sisusb->lock);
642                 return;
643         }
644
645         /* Check that we don't copy too much */
646         length = min((int)c->vc_screenbuf_size,
647                         (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
648
649         /* Save the screen contents to vc's private buffer */
650         sisusbcon_memcpyw((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin,
651                                                                 length);
652
653         mutex_unlock(&sisusb->lock);
654 }
655
656 /* interface routine */
657 static int
658 sisusbcon_set_palette(struct vc_data *c, unsigned char *table)
659 {
660         struct sisusb_usb_data *sisusb;
661         int i, j;
662
663         /* Return value not used by vt */
664
665         if (!CON_IS_VISIBLE(c))
666                 return -EINVAL;
667
668         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
669                 return -EINVAL;
670
671         /* sisusb->lock is down */
672
673         if (sisusb_is_inactive(c, sisusb)) {
674                 mutex_unlock(&sisusb->lock);
675                 return -EINVAL;
676         }
677
678         for (i = j = 0; i < 16; i++) {
679                 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
680                         break;
681                 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
682                         break;
683                 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
684                         break;
685                 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
686                         break;
687         }
688
689         mutex_unlock(&sisusb->lock);
690
691         return 0;
692 }
693
694 /* interface routine */
695 static int
696 sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
697 {
698         struct sisusb_usb_data *sisusb;
699         u8 sr1, cr17, pmreg, cr63;
700         ssize_t written;
701         int ret = 0;
702
703         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
704                 return 0;
705
706         /* sisusb->lock is down */
707
708         if (mode_switch)
709                 sisusb->is_gfx = blank ? 1 : 0;
710
711         if (sisusb_is_inactive(c, sisusb)) {
712                 mutex_unlock(&sisusb->lock);
713                 return 0;
714         }
715
716         switch (blank) {
717
718         case 1:         /* Normal blanking: Clear screen */
719         case -1:
720                 sisusbcon_memsetw((u16 *)c->vc_origin,
721                                 c->vc_video_erase_char,
722                                 c->vc_screenbuf_size);
723                 sisusb_copy_memory(sisusb,
724                                 (unsigned char *)c->vc_origin,
725                                 (u32)(sisusb->vrambase +
726                                         (c->vc_origin - sisusb->scrbuf)),
727                                 c->vc_screenbuf_size, &written);
728                 sisusb->con_blanked = 1;
729                 ret = 1;
730                 break;
731
732         default:        /* VESA blanking */
733                 switch (blank) {
734                 case 0: /* Unblank */
735                         sr1   = 0x00;
736                         cr17  = 0x80;
737                         pmreg = 0x00;
738                         cr63  = 0x00;
739                         ret = 1;
740                         sisusb->con_blanked = 0;
741                         break;
742                 case VESA_VSYNC_SUSPEND + 1:
743                         sr1   = 0x20;
744                         cr17  = 0x80;
745                         pmreg = 0x80;
746                         cr63  = 0x40;
747                         break;
748                 case VESA_HSYNC_SUSPEND + 1:
749                         sr1   = 0x20;
750                         cr17  = 0x80;
751                         pmreg = 0x40;
752                         cr63  = 0x40;
753                         break;
754                 case VESA_POWERDOWN + 1:
755                         sr1   = 0x20;
756                         cr17  = 0x00;
757                         pmreg = 0xc0;
758                         cr63  = 0x40;
759                         break;
760                 default:
761                         mutex_unlock(&sisusb->lock);
762                         return -EINVAL;
763                 }
764
765                 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
766                 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
767                 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
768                 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
769
770         }
771
772         mutex_unlock(&sisusb->lock);
773
774         return ret;
775 }
776
777 /* interface routine */
778 static int
779 sisusbcon_scrolldelta(struct vc_data *c, int lines)
780 {
781         struct sisusb_usb_data *sisusb;
782         int margin = c->vc_size_row * 4;
783         int ul, we, p, st;
784
785         /* The return value does not seem to be used */
786
787         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
788                 return 0;
789
790         /* sisusb->lock is down */
791
792         if (sisusb_is_inactive(c, sisusb)) {
793                 mutex_unlock(&sisusb->lock);
794                 return 0;
795         }
796
797         if (!lines)             /* Turn scrollback off */
798                 c->vc_visible_origin = c->vc_origin;
799         else {
800
801                 if (sisusb->con_rolled_over >
802                                 (c->vc_scr_end - sisusb->scrbuf) + margin) {
803
804                         ul = c->vc_scr_end - sisusb->scrbuf;
805                         we = sisusb->con_rolled_over + c->vc_size_row;
806
807                 } else {
808
809                         ul = 0;
810                         we = sisusb->scrbuf_size;
811
812                 }
813
814                 p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we +
815                                 lines * c->vc_size_row;
816
817                 st = (c->vc_origin - sisusb->scrbuf - ul + we) % we;
818
819                 if (st < 2 * margin)
820                         margin = 0;
821
822                 if (p < margin)
823                         p = 0;
824
825                 if (p > st - margin)
826                         p = st;
827
828                 c->vc_visible_origin = sisusb->scrbuf + (p + ul) % we;
829         }
830
831         sisusbcon_set_start_address(sisusb, c);
832
833         mutex_unlock(&sisusb->lock);
834
835         return 1;
836 }
837
838 /* Interface routine */
839 static void
840 sisusbcon_cursor(struct vc_data *c, int mode)
841 {
842         struct sisusb_usb_data *sisusb;
843         int from, to, baseline;
844
845         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
846                 return;
847
848         /* sisusb->lock is down */
849
850         if (sisusb_is_inactive(c, sisusb)) {
851                 mutex_unlock(&sisusb->lock);
852                 return;
853         }
854
855         if (c->vc_origin != c->vc_visible_origin) {
856                 c->vc_visible_origin = c->vc_origin;
857                 sisusbcon_set_start_address(sisusb, c);
858         }
859
860         if (mode == CM_ERASE) {
861                 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
862                 sisusb->sisusb_cursor_size_to = -1;
863                 mutex_unlock(&sisusb->lock);
864                 return;
865         }
866
867         sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
868
869         baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
870
871         switch (c->vc_cursor_type & 0x0f) {
872                 case CUR_BLOCK:         from = 1;
873                                         to   = c->vc_font.height;
874                                         break;
875                 case CUR_TWO_THIRDS:    from = c->vc_font.height / 3;
876                                         to   = baseline;
877                                         break;
878                 case CUR_LOWER_HALF:    from = c->vc_font.height / 2;
879                                         to   = baseline;
880                                         break;
881                 case CUR_LOWER_THIRD:   from = (c->vc_font.height * 2) / 3;
882                                         to   = baseline;
883                                         break;
884                 case CUR_NONE:          from = 31;
885                                         to = 30;
886                                         break;
887                 default:
888                 case CUR_UNDERLINE:     from = baseline - 1;
889                                         to   = baseline;
890                                         break;
891         }
892
893         if (sisusb->sisusb_cursor_size_from != from ||
894             sisusb->sisusb_cursor_size_to != to) {
895
896                 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
897                 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
898
899                 sisusb->sisusb_cursor_size_from = from;
900                 sisusb->sisusb_cursor_size_to   = to;
901         }
902
903         mutex_unlock(&sisusb->lock);
904 }
905
906 static int
907 sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
908                                         int t, int b, int dir, int lines)
909 {
910         int cols = sisusb->sisusb_num_columns;
911         int length = ((b - t) * cols) * 2;
912         u16 eattr = c->vc_video_erase_char;
913         ssize_t written;
914
915         /* sisusb->lock is down */
916
917         /* Scroll an area which does not match the
918          * visible screen's dimensions. This needs
919          * to be done separately, as it does not
920          * use hardware panning.
921          */
922
923         switch (dir) {
924
925                 case SM_UP:
926                         sisusbcon_memmovew(SISUSB_VADDR(0, t),
927                                            SISUSB_VADDR(0, t + lines),
928                                            (b - t - lines) * cols * 2);
929                         sisusbcon_memsetw(SISUSB_VADDR(0, b - lines), eattr,
930                                           lines * cols * 2);
931                         break;
932
933                 case SM_DOWN:
934                         sisusbcon_memmovew(SISUSB_VADDR(0, t + lines),
935                                            SISUSB_VADDR(0, t),
936                                            (b - t - lines) * cols * 2);
937                         sisusbcon_memsetw(SISUSB_VADDR(0, t), eattr,
938                                           lines * cols * 2);
939                         break;
940         }
941
942         sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(0, t),
943                                 (u32)SISUSB_HADDR(0, t), length, &written);
944
945         mutex_unlock(&sisusb->lock);
946
947         return 1;
948 }
949
950 /* Interface routine */
951 static int
952 sisusbcon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
953 {
954         struct sisusb_usb_data *sisusb;
955         u16 eattr = c->vc_video_erase_char;
956         ssize_t written;
957         int copyall = 0;
958         unsigned long oldorigin;
959         unsigned int delta = lines * c->vc_size_row;
960         u32 originoffset;
961
962         /* Returning != 0 means we have done the scrolling successfully.
963          * Returning 0 makes vt do the scrolling on its own.
964          * Note that con_scroll is only called if the console is
965          * visible. In that case, the origin should be our buffer,
966          * not the vt's private one.
967          */
968
969         if (!lines)
970                 return 1;
971
972         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
973                 return 0;
974
975         /* sisusb->lock is down */
976
977         if (sisusb_is_inactive(c, sisusb)) {
978                 mutex_unlock(&sisusb->lock);
979                 return 0;
980         }
981
982         /* Special case */
983         if (t || b != c->vc_rows)
984                 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
985
986         if (c->vc_origin != c->vc_visible_origin) {
987                 c->vc_visible_origin = c->vc_origin;
988                 sisusbcon_set_start_address(sisusb, c);
989         }
990
991         /* limit amount to maximum realistic size */
992         if (lines > c->vc_rows)
993                 lines = c->vc_rows;
994
995         oldorigin = c->vc_origin;
996
997         switch (dir) {
998
999         case SM_UP:
1000
1001                 if (c->vc_scr_end + delta >=
1002                                 sisusb->scrbuf + sisusb->scrbuf_size) {
1003                         sisusbcon_memcpyw((u16 *)sisusb->scrbuf,
1004                                           (u16 *)(oldorigin + delta),
1005                                           c->vc_screenbuf_size - delta);
1006                         c->vc_origin = sisusb->scrbuf;
1007                         sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
1008                         copyall = 1;
1009                 } else
1010                         c->vc_origin += delta;
1011
1012                 sisusbcon_memsetw(
1013                         (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
1014                                         eattr, delta);
1015
1016                 break;
1017
1018         case SM_DOWN:
1019
1020                 if (oldorigin - delta < sisusb->scrbuf) {
1021                         sisusbcon_memmovew((u16 *)(sisusb->scrbuf +
1022                                                         sisusb->scrbuf_size -
1023                                                         c->vc_screenbuf_size +
1024                                                         delta),
1025                                            (u16 *)oldorigin,
1026                                            c->vc_screenbuf_size - delta);
1027                         c->vc_origin = sisusb->scrbuf +
1028                                         sisusb->scrbuf_size -
1029                                         c->vc_screenbuf_size;
1030                         sisusb->con_rolled_over = 0;
1031                         copyall = 1;
1032                 } else
1033                         c->vc_origin -= delta;
1034
1035                 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1036
1037                 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
1038
1039                 break;
1040         }
1041
1042         originoffset = (u32)(c->vc_origin - sisusb->scrbuf);
1043
1044         if (copyall)
1045                 sisusb_copy_memory(sisusb,
1046                         (char *)c->vc_origin,
1047                         (u32)(sisusb->vrambase + originoffset),
1048                         c->vc_screenbuf_size, &written);
1049         else if (dir == SM_UP)
1050                 sisusb_copy_memory(sisusb,
1051                         (char *)c->vc_origin + c->vc_screenbuf_size - delta,
1052                         (u32)sisusb->vrambase + originoffset +
1053                                         c->vc_screenbuf_size - delta,
1054                         delta, &written);
1055         else
1056                 sisusb_copy_memory(sisusb,
1057                         (char *)c->vc_origin,
1058                         (u32)(sisusb->vrambase + originoffset),
1059                         delta, &written);
1060
1061         c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1062         c->vc_visible_origin = c->vc_origin;
1063
1064         sisusbcon_set_start_address(sisusb, c);
1065
1066         c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
1067
1068         mutex_unlock(&sisusb->lock);
1069
1070         return 1;
1071 }
1072
1073 /* Interface routine */
1074 static int
1075 sisusbcon_set_origin(struct vc_data *c)
1076 {
1077         struct sisusb_usb_data *sisusb;
1078
1079         /* Returning != 0 means we were successful.
1080          * Returning 0 will vt make to use its own
1081          *      screenbuffer as the origin.
1082          */
1083
1084         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1085                 return 0;
1086
1087         /* sisusb->lock is down */
1088
1089         if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
1090                 mutex_unlock(&sisusb->lock);
1091                 return 0;
1092         }
1093
1094         c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
1095
1096         sisusbcon_set_start_address(sisusb, c);
1097
1098         sisusb->con_rolled_over = 0;
1099
1100         mutex_unlock(&sisusb->lock);
1101
1102         return 1;
1103 }
1104
1105 /* Interface routine */
1106 static int
1107 sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows)
1108 {
1109         struct sisusb_usb_data *sisusb;
1110         int fh;
1111
1112         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1113                 return -ENODEV;
1114
1115         fh = sisusb->current_font_height;
1116
1117         mutex_unlock(&sisusb->lock);
1118
1119         /* We are quite unflexible as regards resizing. The vt code
1120          * handles sizes where the line length isn't equal the pitch
1121          * quite badly. As regards the rows, our panning tricks only
1122          * work well if the number of rows equals the visible number
1123          * of rows.
1124          */
1125
1126         if (newcols != 80 || c->vc_scan_lines / fh != newrows)
1127                 return -EINVAL;
1128
1129         return 0;
1130 }
1131
1132 int
1133 sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
1134                         u8 *arg, int cmapsz, int ch512, int dorecalc,
1135                         struct vc_data *c, int fh, int uplock)
1136 {
1137         int font_select = 0x00, i, err = 0;
1138         u32 offset = 0;
1139         u8 dummy;
1140
1141         /* sisusb->lock is down */
1142
1143         /*
1144          * The default font is kept in slot 0.
1145          * A user font is loaded in slot 2 (256 ch)
1146          * or 2+3 (512 ch).
1147          */
1148
1149         if ((slot != 0 && slot != 2) || !fh) {
1150                 if (uplock)
1151                         mutex_unlock(&sisusb->lock);
1152                 return -EINVAL;
1153         }
1154
1155         if (set)
1156                 sisusb->font_slot = slot;
1157
1158         /* Default font is always 256 */
1159         if (slot == 0)
1160                 ch512 = 0;
1161         else
1162                 offset = 4 * cmapsz;
1163
1164         font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1165
1166         err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1167         err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1168         err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1169         err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1170
1171         if (err)
1172                 goto font_op_error;
1173
1174         err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1175         err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1176         err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1177
1178         if (err)
1179                 goto font_op_error;
1180
1181         if (arg) {
1182                 if (set)
1183                         for (i = 0; i < cmapsz; i++) {
1184                                 err |= sisusb_writeb(sisusb,
1185                                         sisusb->vrambase + offset + i,
1186                                         arg[i]);
1187                                 if (err)
1188                                         break;
1189                         }
1190                 else
1191                         for (i = 0; i < cmapsz; i++) {
1192                                 err |= sisusb_readb(sisusb,
1193                                         sisusb->vrambase + offset + i,
1194                                         &arg[i]);
1195                                 if (err)
1196                                         break;
1197                         }
1198
1199                 /*
1200                  * In 512-character mode, the character map is not contiguous if
1201                  * we want to remain EGA compatible -- which we do
1202                  */
1203
1204                 if (ch512) {
1205                         if (set)
1206                                 for (i = 0; i < cmapsz; i++) {
1207                                         err |= sisusb_writeb(sisusb,
1208                                                 sisusb->vrambase + offset +
1209                                                         (2 * cmapsz) + i,
1210                                                 arg[cmapsz + i]);
1211                                         if (err)
1212                                                 break;
1213                                 }
1214                         else
1215                                 for (i = 0; i < cmapsz; i++) {
1216                                         err |= sisusb_readb(sisusb,
1217                                                 sisusb->vrambase + offset +
1218                                                         (2 * cmapsz) + i,
1219                                                 &arg[cmapsz + i]);
1220                                         if (err)
1221                                                 break;
1222                                 }
1223                 }
1224         }
1225
1226         if (err)
1227                 goto font_op_error;
1228
1229         err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1230         err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1231         err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1232         if (set)
1233                 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1234         err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1235
1236         if (err)
1237                 goto font_op_error;
1238
1239         err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1240         err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1241         err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1242
1243         if (err)
1244                 goto font_op_error;
1245
1246         if ((set) && (ch512 != sisusb->current_font_512)) {
1247
1248                 /* Font is shared among all our consoles.
1249                  * And so is the hi_font_mask.
1250                  */
1251                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1252                         struct vc_data *c = vc_cons[i].d;
1253                         if (c && c->vc_sw == &sisusb_con)
1254                                 c->vc_hi_font_mask = ch512 ? 0x0800 : 0;
1255                 }
1256
1257                 sisusb->current_font_512 = ch512;
1258
1259                 /* color plane enable register:
1260                         256-char: enable intensity bit
1261                         512-char: disable intensity bit */
1262                 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1263                 sisusb_setreg(sisusb, SISAR, 0x12);
1264                 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1265
1266                 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1267                 sisusb_setreg(sisusb, SISAR, 0x20);
1268                 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1269         }
1270
1271         if (dorecalc) {
1272
1273                 /*
1274                  * Adjust the screen to fit a font of a certain height
1275                  */
1276
1277                 unsigned char ovr, vde, fsr;
1278                 int rows = 0, maxscan = 0;
1279
1280                 if (c) {
1281
1282                         /* Number of video rows */
1283                         rows = c->vc_scan_lines / fh;
1284                         /* Scan lines to actually display-1 */
1285                         maxscan = rows * fh - 1;
1286
1287                         /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1288                                 rows, maxscan, fh, c->vc_scan_lines);*/
1289
1290                         sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1291                         vde = maxscan & 0xff;
1292                         ovr = (ovr & 0xbd) |
1293                               ((maxscan & 0x100) >> 7) |
1294                               ((maxscan & 0x200) >> 3);
1295                         sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1296                         sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1297
1298                 }
1299
1300                 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1301                 fsr = (fsr & 0xe0) | (fh - 1);
1302                 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1303                 sisusb->current_font_height = fh;
1304
1305                 sisusb->sisusb_cursor_size_from = -1;
1306                 sisusb->sisusb_cursor_size_to   = -1;
1307
1308         }
1309
1310         if (uplock)
1311                 mutex_unlock(&sisusb->lock);
1312
1313         if (dorecalc && c) {
1314                 int i, rows = c->vc_scan_lines / fh;
1315
1316                 /* Now adjust our consoles' size */
1317
1318                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1319                         struct vc_data *vc = vc_cons[i].d;
1320
1321                         if (vc && vc->vc_sw == &sisusb_con) {
1322                                 if (CON_IS_VISIBLE(vc)) {
1323                                         vc->vc_sw->con_cursor(vc, CM_DRAW);
1324                                 }
1325                                 vc->vc_font.height = fh;
1326                                 vc_resize(vc, 0, rows);
1327                         }
1328                 }
1329         }
1330
1331         return 0;
1332
1333 font_op_error:
1334         if (uplock)
1335                 mutex_unlock(&sisusb->lock);
1336
1337         return -EIO;
1338 }
1339
1340 /* Interface routine */
1341 static int
1342 sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1343                                                         unsigned flags)
1344 {
1345         struct sisusb_usb_data *sisusb;
1346         unsigned charcount = font->charcount;
1347
1348         if (font->width != 8 || (charcount != 256 && charcount != 512))
1349                 return -EINVAL;
1350
1351         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1352                 return -ENODEV;
1353
1354         /* sisusb->lock is down */
1355
1356         /* Save the user-provided font into a buffer. This
1357          * is used for restoring text mode after quitting
1358          * from X and for the con_getfont routine.
1359          */
1360         if (sisusb->font_backup) {
1361                 if (sisusb->font_backup_size < charcount) {
1362                         vfree(sisusb->font_backup);
1363                         sisusb->font_backup = NULL;
1364                 }
1365         }
1366
1367         if (!sisusb->font_backup)
1368                 sisusb->font_backup = vmalloc(charcount * 32);
1369
1370         if (sisusb->font_backup) {
1371                 memcpy(sisusb->font_backup, font->data, charcount * 32);
1372                 sisusb->font_backup_size = charcount;
1373                 sisusb->font_backup_height = font->height;
1374                 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1375         }
1376
1377         /* do_font_op ups sisusb->lock */
1378
1379         return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1380                         8192, (charcount == 512),
1381                         (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1382                         c, font->height, 1);
1383 }
1384
1385 /* Interface routine */
1386 static int
1387 sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1388 {
1389         struct sisusb_usb_data *sisusb;
1390
1391         if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num)))
1392                 return -ENODEV;
1393
1394         /* sisusb->lock is down */
1395
1396         font->width = 8;
1397         font->height = c->vc_font.height;
1398         font->charcount = 256;
1399
1400         if (!font->data) {
1401                 mutex_unlock(&sisusb->lock);
1402                 return 0;
1403         }
1404
1405         if (!sisusb->font_backup) {
1406                 mutex_unlock(&sisusb->lock);
1407                 return -ENODEV;
1408         }
1409
1410         /* Copy 256 chars only, like vgacon */
1411         memcpy(font->data, sisusb->font_backup, 256 * 32);
1412
1413         mutex_unlock(&sisusb->lock);
1414
1415         return 0;
1416 }
1417
1418 /*
1419  *  The console `switch' structure for the sisusb console
1420  */
1421
1422 static const struct consw sisusb_con = {
1423         .owner =                THIS_MODULE,
1424         .con_startup =          sisusbcon_startup,
1425         .con_init =             sisusbcon_init,
1426         .con_deinit =           sisusbcon_deinit,
1427         .con_clear =            sisusbcon_clear,
1428         .con_putc =             sisusbcon_putc,
1429         .con_putcs =            sisusbcon_putcs,
1430         .con_cursor =           sisusbcon_cursor,
1431         .con_scroll =           sisusbcon_scroll,
1432         .con_bmove =            sisusbcon_bmove,
1433         .con_switch =           sisusbcon_switch,
1434         .con_blank =            sisusbcon_blank,
1435         .con_font_set =         sisusbcon_font_set,
1436         .con_font_get =         sisusbcon_font_get,
1437         .con_set_palette =      sisusbcon_set_palette,
1438         .con_scrolldelta =      sisusbcon_scrolldelta,
1439         .con_build_attr =       sisusbcon_build_attr,
1440         .con_invert_region =    sisusbcon_invert_region,
1441         .con_set_origin =       sisusbcon_set_origin,
1442         .con_save_screen =      sisusbcon_save_screen,
1443         .con_resize =           sisusbcon_resize,
1444 };
1445
1446 /* Our very own dummy console driver */
1447
1448 static const char *sisusbdummycon_startup(void)
1449 {
1450     return "SISUSBVGADUMMY";
1451 }
1452
1453 static void sisusbdummycon_init(struct vc_data *vc, int init)
1454 {
1455     vc->vc_can_do_color = 1;
1456     if (init) {
1457         vc->vc_cols = 80;
1458         vc->vc_rows = 25;
1459     } else
1460         vc_resize(vc, 80, 25);
1461 }
1462
1463 static int sisusbdummycon_dummy(void)
1464 {
1465     return 0;
1466 }
1467
1468 #define SISUSBCONDUMMY  (void *)sisusbdummycon_dummy
1469
1470 static const struct consw sisusb_dummy_con = {
1471         .owner =                THIS_MODULE,
1472         .con_startup =          sisusbdummycon_startup,
1473         .con_init =             sisusbdummycon_init,
1474         .con_deinit =           SISUSBCONDUMMY,
1475         .con_clear =            SISUSBCONDUMMY,
1476         .con_putc =             SISUSBCONDUMMY,
1477         .con_putcs =            SISUSBCONDUMMY,
1478         .con_cursor =           SISUSBCONDUMMY,
1479         .con_scroll =           SISUSBCONDUMMY,
1480         .con_bmove =            SISUSBCONDUMMY,
1481         .con_switch =           SISUSBCONDUMMY,
1482         .con_blank =            SISUSBCONDUMMY,
1483         .con_font_set =         SISUSBCONDUMMY,
1484         .con_font_get =         SISUSBCONDUMMY,
1485         .con_font_default =     SISUSBCONDUMMY,
1486         .con_font_copy =        SISUSBCONDUMMY,
1487         .con_set_palette =      SISUSBCONDUMMY,
1488         .con_scrolldelta =      SISUSBCONDUMMY,
1489 };
1490
1491 int
1492 sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1493 {
1494         int i, ret, minor = sisusb->minor;
1495
1496         mutex_lock(&disconnect_mutex);
1497
1498         mutex_lock(&sisusb->lock);
1499
1500         /* Erm.. that should not happen */
1501         if (sisusb->haveconsole || !sisusb->SiS_Pr) {
1502                 mutex_unlock(&sisusb->lock);
1503                 mutex_unlock(&disconnect_mutex);
1504                 return 1;
1505         }
1506
1507         sisusb->con_first = first;
1508         sisusb->con_last  = last;
1509
1510         if (first > last ||
1511             first > MAX_NR_CONSOLES ||
1512             last > MAX_NR_CONSOLES) {
1513                 mutex_unlock(&sisusb->lock);
1514                 mutex_unlock(&disconnect_mutex);
1515                 return 1;
1516         }
1517
1518         /* If gfxcore not initialized or no consoles given, quit graciously */
1519         if (!sisusb->gfxinit || first < 1 || last < 1) {
1520                 mutex_unlock(&sisusb->lock);
1521                 mutex_unlock(&disconnect_mutex);
1522                 return 0;
1523         }
1524
1525         sisusb->sisusb_cursor_loc       = -1;
1526         sisusb->sisusb_cursor_size_from = -1;
1527         sisusb->sisusb_cursor_size_to   = -1;
1528
1529         /* Set up text mode (and upload  default font) */
1530         if (sisusb_reset_text_mode(sisusb, 1)) {
1531                 mutex_unlock(&sisusb->lock);
1532                 mutex_unlock(&disconnect_mutex);
1533                 printk(KERN_ERR
1534                         "sisusbvga[%d]: Failed to set up text mode\n",
1535                         minor);
1536                 return 1;
1537         }
1538
1539         /* Initialize some gfx registers */
1540         sisusb_initialize(sisusb);
1541
1542         for (i = first - 1; i <= last - 1; i++) {
1543                 /* Save sisusb for our interface routines */
1544                 mysisusbs[i] = sisusb;
1545         }
1546
1547         /* Initial console setup */
1548         sisusb->sisusb_num_columns = 80;
1549
1550         /* Use a 32K buffer (matches b8000-bffff area) */
1551         sisusb->scrbuf_size = 32 * 1024;
1552
1553         /* Allocate screen buffer */
1554         if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
1555                 mutex_unlock(&sisusb->lock);
1556                 mutex_unlock(&disconnect_mutex);
1557                 printk(KERN_ERR
1558                         "sisusbvga[%d]: Failed to allocate screen buffer\n",
1559                         minor);
1560                 return 1;
1561         }
1562
1563         mutex_unlock(&sisusb->lock);
1564         mutex_unlock(&disconnect_mutex);
1565
1566         /* Now grab the desired console(s) */
1567         ret = take_over_console(&sisusb_con, first - 1, last - 1, 0);
1568
1569         if (!ret)
1570                 sisusb->haveconsole = 1;
1571         else {
1572                 for (i = first - 1; i <= last - 1; i++)
1573                         mysisusbs[i] = NULL;
1574         }
1575
1576         return ret;
1577 }
1578
1579 void
1580 sisusb_console_exit(struct sisusb_usb_data *sisusb)
1581 {
1582         int i;
1583
1584         /* This is called if the device is disconnected
1585          * and while disconnect and lock semaphores
1586          * are up. This should be save because we
1587          * can't lose our sisusb any other way but by
1588          * disconnection (and hence, the disconnect
1589          * sema is for protecting all other access
1590          * functions from disconnection, not the
1591          * other way round).
1592          */
1593
1594         /* Now what do we do in case of disconnection:
1595          * One alternative would be to simply call
1596          * give_up_console(). Nah, not a good idea.
1597          * give_up_console() is obviously buggy as it
1598          * only discards the consw pointer from the
1599          * driver_map, but doesn't adapt vc->vc_sw
1600          * of the affected consoles. Hence, the next
1601          * call to any of the console functions will
1602          * eventually take a trip to oops county.
1603          * Also, give_up_console for some reason
1604          * doesn't decrement our module refcount.
1605          * Instead, we switch our consoles to a private
1606          * dummy console. This, of course, keeps our
1607          * refcount up as well, but it works perfectly.
1608          */
1609
1610         if (sisusb->haveconsole) {
1611                 for (i = 0; i < MAX_NR_CONSOLES; i++)
1612                         if (sisusb->havethisconsole[i])
1613                                 take_over_console(&sisusb_dummy_con, i, i, 0);
1614                                 /* At this point, con_deinit for all our
1615                                  * consoles is executed by take_over_console().
1616                                  */
1617                 sisusb->haveconsole = 0;
1618         }
1619
1620         vfree((void *)sisusb->scrbuf);
1621         sisusb->scrbuf = 0;
1622
1623         vfree(sisusb->font_backup);
1624         sisusb->font_backup = NULL;
1625 }
1626
1627 void __init sisusb_init_concode(void)
1628 {
1629         int i;
1630
1631         for (i = 0; i < MAX_NR_CONSOLES; i++)
1632                 mysisusbs[i] = NULL;
1633 }
1634
1635 #endif /* INCL_CON */
1636
1637
1638