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