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