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