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