blob: 6331965daa0bcdf5e715b3103ebe9310979cac11 [file] [log] [blame]
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001/*
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
Arjan van de Ven2682d272006-03-28 01:00:21 -080050#include <linux/mutex.h>
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020051#include <linux/module.h>
52#include <linux/kernel.h>
53#include <linux/signal.h>
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020054#include <linux/fs.h>
Felipe Balbi7b5cd5f2007-08-15 10:38:12 -040055#include <linux/usb.h>
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020056#include <linux/tty.h>
57#include <linux/console.h>
58#include <linux/string.h>
59#include <linux/kd.h>
60#include <linux/init.h>
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020061#include <linux/vt_kern.h>
62#include <linux/selection.h>
63#include <linux/spinlock.h>
64#include <linux/kref.h>
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020065#include <linux/ioport.h>
66#include <linux/interrupt.h>
67#include <linux/vmalloc.h>
68
69#include "sisusb.h"
Adrian Bunkdf47e5332006-04-15 11:17:27 +020070#include "sisusb_init.h"
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020071
72#ifdef INCL_SISUSB_CON
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020073
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 */
80static struct sisusb_usb_data *mysisusbs[MAX_NR_CONSOLES];
81
82/* Forward declaration */
83static const struct consw sisusb_con;
84
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +020085static inline void
86sisusbcon_memsetw(u16 *s, u16 c, unsigned int count)
87{
88 count /= 2;
89 while (count--)
90 sisusbcon_writew(c, s++);
91}
92
93static inline void
94sisusb_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
106static inline void
107sisusbcon_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
115void
116sisusb_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
144static inline struct sisusb_usb_data *
145sisusb_get_sisusb(unsigned short console)
146{
147 return mysisusbs[console];
148}
149
150static inline int
151sisusb_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
159static struct sisusb_usb_data *
160sisusb_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
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200172 sisusb = sisusb_get_sisusb(console);
173 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200174 return NULL;
175
Arjan van de Ven2682d272006-03-28 01:00:21 -0800176 mutex_lock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200177
178 if (!sisusb_sisusb_valid(sisusb) ||
179 !sisusb->havethisconsole[console]) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800180 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200181 return NULL;
182 }
183
184 return sisusb;
185}
186
187static int
188sisusb_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 */
199static const char *
200sisusbcon_startup(void)
201{
202 return "SISUSBCON";
203}
204
205/* con_init console interface routine */
206static void
207sisusbcon_init(struct vc_data *c, int init)
208{
209 struct sisusb_usb_data *sisusb;
210 int cols, rows;
211
Wang YanQing155957f2013-05-21 13:15:12 +0800212 /* This is called by do_take_over_console(),
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200213 * ie by us/under our control. It is
214 * only called after text mode and fonts
215 * are set up/restored.
216 */
217
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200218 sisusb = sisusb_get_sisusb(c->vc_num);
219 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200220 return;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200221
Arjan van de Ven2682d272006-03-28 01:00:21 -0800222 mutex_lock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200223
224 if (!sisusb_sisusb_valid(sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800225 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200226 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
Arjan van de Ven2682d272006-03-28 01:00:21 -0800262 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200263
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200264 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 */
272static void
273sisusbcon_deinit(struct vc_data *c)
274{
275 struct sisusb_usb_data *sisusb;
276 int i;
277
Wang YanQing155957f2013-05-21 13:15:12 +0800278 /* This is called by do_take_over_console()
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200279 * and others, ie not under our control.
280 */
281
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200282 sisusb = sisusb_get_sisusb(c->vc_num);
283 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200284 return;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200285
Arjan van de Ven2682d272006-03-28 01:00:21 -0800286 mutex_lock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200287
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
Arjan van de Ven2682d272006-03-28 01:00:21 -0800305 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200306
307 /* decrement the usage count on our sisusb */
308 kref_put(&sisusb->kref, sisusb_delete);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200309}
310
311/* interface routine */
312static u8
313sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity,
WANG Cong31990a92007-05-08 00:38:06 -0700314 u8 blink, u8 underline, u8 reverse, u8 unused)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200315{
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 */
338static void
339sisusbcon_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 */
369static void
370sisusbcon_putc(struct vc_data *c, int ch, int y, int x)
371{
372 struct sisusb_usb_data *sisusb;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200373
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200374 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
375 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200376 return;
377
378 /* sisusb->lock is down */
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200379 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800380 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200381 return;
382 }
383
384
385 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200386 (long)SISUSB_HADDR(x, y), 2);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200387
Arjan van de Ven2682d272006-03-28 01:00:21 -0800388 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200389}
390
391/* Interface routine */
392static void
393sisusbcon_putcs(struct vc_data *c, const unsigned short *s,
394 int count, int y, int x)
395{
396 struct sisusb_usb_data *sisusb;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200397 u16 *dest;
398 int i;
399
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200400 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
401 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200402 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)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800416 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200417 return;
418 }
419
420 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200421 (long)SISUSB_HADDR(x, y), count * 2);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200422
Arjan van de Ven2682d272006-03-28 01:00:21 -0800423 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200424}
425
426/* Interface routine */
427static void
428sisusbcon_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;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200432 int i, length, cols;
433 u16 *dest;
434
435 if (width <= 0 || height <= 0)
436 return;
437
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200438 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
439 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200440 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)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800467 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200468 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),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200475 (long)SISUSB_HADDR(x, y), length);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200476
Arjan van de Ven2682d272006-03-28 01:00:21 -0800477 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200478}
479
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200480/* interface routine */
481static int
482sisusbcon_switch(struct vc_data *c)
483{
484 struct sisusb_usb_data *sisusb;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200485 int length;
486
487 /* Returnvalue 0 means we have fully restored screen,
488 * and vt doesn't need to call do_update_region().
489 * Returnvalue != 0 naturally means the opposite.
490 */
491
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200492 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
493 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200494 return 0;
495
496 /* sisusb->lock is down */
497
498 /* Don't write to screen if in gfx mode */
499 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800500 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200501 return 0;
502 }
503
504 /* That really should not happen. It would mean we are
505 * being called while the vc is using its private buffer
506 * as origin.
507 */
508 if (c->vc_origin == (unsigned long)c->vc_screenbuf) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800509 mutex_unlock(&sisusb->lock);
Felipe Balbi7b5cd5f2007-08-15 10:38:12 -0400510 dev_dbg(&sisusb->sisusb_dev->dev, "ASSERT ORIGIN != SCREENBUF!\n");
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200511 return 0;
512 }
513
514 /* Check that we don't copy too much */
515 length = min((int)c->vc_screenbuf_size,
516 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
517
518 /* Restore the screen contents */
519 sisusbcon_memcpyw((u16 *)c->vc_origin, (u16 *)c->vc_screenbuf,
520 length);
521
522 sisusb_copy_memory(sisusb, (unsigned char *)c->vc_origin,
Andrew Mortonc067dfc2007-01-03 16:45:21 -0800523 (long)SISUSB_HADDR(0, 0),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200524 length);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200525
Arjan van de Ven2682d272006-03-28 01:00:21 -0800526 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200527
528 return 0;
529}
530
531/* interface routine */
532static void
533sisusbcon_save_screen(struct vc_data *c)
534{
535 struct sisusb_usb_data *sisusb;
536 int length;
537
538 /* Save the current screen contents to vc's private
539 * buffer.
540 */
541
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200542 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
543 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200544 return;
545
546 /* sisusb->lock is down */
547
548 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800549 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200550 return;
551 }
552
553 /* Check that we don't copy too much */
554 length = min((int)c->vc_screenbuf_size,
555 (int)(sisusb->scrbuf + sisusb->scrbuf_size - c->vc_origin));
556
557 /* Save the screen contents to vc's private buffer */
558 sisusbcon_memcpyw((u16 *)c->vc_screenbuf, (u16 *)c->vc_origin,
559 length);
560
Arjan van de Ven2682d272006-03-28 01:00:21 -0800561 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200562}
563
564/* interface routine */
Jiri Slaby709280d2016-06-23 13:34:27 +0200565static void
Jiri Slaby8ede5cc2016-03-31 10:08:16 +0200566sisusbcon_set_palette(struct vc_data *c, const unsigned char *table)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200567{
568 struct sisusb_usb_data *sisusb;
569 int i, j;
570
571 /* Return value not used by vt */
572
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200573 if (!con_is_visible(c))
Jiri Slaby709280d2016-06-23 13:34:27 +0200574 return;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200575
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200576 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
577 if (!sisusb)
Jiri Slaby709280d2016-06-23 13:34:27 +0200578 return;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200579
580 /* sisusb->lock is down */
581
582 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800583 mutex_unlock(&sisusb->lock);
Jiri Slaby709280d2016-06-23 13:34:27 +0200584 return;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200585 }
586
587 for (i = j = 0; i < 16; i++) {
588 if (sisusb_setreg(sisusb, SISCOLIDX, table[i]))
589 break;
590 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
591 break;
592 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
593 break;
594 if (sisusb_setreg(sisusb, SISCOLDATA, c->vc_palette[j++] >> 2))
595 break;
596 }
597
Arjan van de Ven2682d272006-03-28 01:00:21 -0800598 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200599}
600
601/* interface routine */
602static int
603sisusbcon_blank(struct vc_data *c, int blank, int mode_switch)
604{
605 struct sisusb_usb_data *sisusb;
606 u8 sr1, cr17, pmreg, cr63;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200607 int ret = 0;
608
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200609 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
610 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200611 return 0;
612
613 /* sisusb->lock is down */
614
615 if (mode_switch)
616 sisusb->is_gfx = blank ? 1 : 0;
617
618 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800619 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200620 return 0;
621 }
622
623 switch (blank) {
624
625 case 1: /* Normal blanking: Clear screen */
626 case -1:
627 sisusbcon_memsetw((u16 *)c->vc_origin,
628 c->vc_video_erase_char,
629 c->vc_screenbuf_size);
630 sisusb_copy_memory(sisusb,
631 (unsigned char *)c->vc_origin,
632 (u32)(sisusb->vrambase +
633 (c->vc_origin - sisusb->scrbuf)),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200634 c->vc_screenbuf_size);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200635 sisusb->con_blanked = 1;
636 ret = 1;
637 break;
638
639 default: /* VESA blanking */
640 switch (blank) {
641 case 0: /* Unblank */
642 sr1 = 0x00;
643 cr17 = 0x80;
644 pmreg = 0x00;
645 cr63 = 0x00;
646 ret = 1;
647 sisusb->con_blanked = 0;
648 break;
649 case VESA_VSYNC_SUSPEND + 1:
650 sr1 = 0x20;
651 cr17 = 0x80;
652 pmreg = 0x80;
653 cr63 = 0x40;
654 break;
655 case VESA_HSYNC_SUSPEND + 1:
656 sr1 = 0x20;
657 cr17 = 0x80;
658 pmreg = 0x40;
659 cr63 = 0x40;
660 break;
661 case VESA_POWERDOWN + 1:
662 sr1 = 0x20;
663 cr17 = 0x00;
664 pmreg = 0xc0;
665 cr63 = 0x40;
666 break;
667 default:
Arjan van de Ven2682d272006-03-28 01:00:21 -0800668 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200669 return -EINVAL;
670 }
671
672 sisusb_setidxregandor(sisusb, SISSR, 0x01, ~0x20, sr1);
673 sisusb_setidxregandor(sisusb, SISCR, 0x17, 0x7f, cr17);
674 sisusb_setidxregandor(sisusb, SISSR, 0x1f, 0x3f, pmreg);
675 sisusb_setidxregandor(sisusb, SISCR, 0x63, 0xbf, cr63);
676
677 }
678
Arjan van de Ven2682d272006-03-28 01:00:21 -0800679 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200680
681 return ret;
682}
683
684/* interface routine */
Jiri Slaby97293de2016-06-23 13:34:26 +0200685static void
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200686sisusbcon_scrolldelta(struct vc_data *c, int lines)
687{
688 struct sisusb_usb_data *sisusb;
689 int margin = c->vc_size_row * 4;
690 int ul, we, p, st;
691
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200692 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
693 if (!sisusb)
Jiri Slaby97293de2016-06-23 13:34:26 +0200694 return;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200695
696 /* sisusb->lock is down */
697
698 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800699 mutex_unlock(&sisusb->lock);
Jiri Slaby97293de2016-06-23 13:34:26 +0200700 return;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200701 }
702
703 if (!lines) /* Turn scrollback off */
704 c->vc_visible_origin = c->vc_origin;
705 else {
706
707 if (sisusb->con_rolled_over >
708 (c->vc_scr_end - sisusb->scrbuf) + margin) {
709
710 ul = c->vc_scr_end - sisusb->scrbuf;
711 we = sisusb->con_rolled_over + c->vc_size_row;
712
713 } else {
714
715 ul = 0;
716 we = sisusb->scrbuf_size;
717
718 }
719
720 p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we +
721 lines * c->vc_size_row;
722
723 st = (c->vc_origin - sisusb->scrbuf - ul + we) % we;
724
725 if (st < 2 * margin)
726 margin = 0;
727
728 if (p < margin)
729 p = 0;
730
731 if (p > st - margin)
732 p = st;
733
734 c->vc_visible_origin = sisusb->scrbuf + (p + ul) % we;
735 }
736
737 sisusbcon_set_start_address(sisusb, c);
738
Arjan van de Ven2682d272006-03-28 01:00:21 -0800739 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200740}
741
742/* Interface routine */
743static void
744sisusbcon_cursor(struct vc_data *c, int mode)
745{
746 struct sisusb_usb_data *sisusb;
747 int from, to, baseline;
748
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200749 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
750 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200751 return;
752
753 /* sisusb->lock is down */
754
755 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800756 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200757 return;
758 }
759
760 if (c->vc_origin != c->vc_visible_origin) {
761 c->vc_visible_origin = c->vc_origin;
762 sisusbcon_set_start_address(sisusb, c);
763 }
764
765 if (mode == CM_ERASE) {
766 sisusb_setidxregor(sisusb, SISCR, 0x0a, 0x20);
767 sisusb->sisusb_cursor_size_to = -1;
Arjan van de Ven2682d272006-03-28 01:00:21 -0800768 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200769 return;
770 }
771
772 sisusb_set_cursor(sisusb, (c->vc_pos - sisusb->scrbuf) / 2);
773
774 baseline = c->vc_font.height - (c->vc_font.height < 10 ? 1 : 2);
775
776 switch (c->vc_cursor_type & 0x0f) {
777 case CUR_BLOCK: from = 1;
778 to = c->vc_font.height;
779 break;
780 case CUR_TWO_THIRDS: from = c->vc_font.height / 3;
781 to = baseline;
782 break;
783 case CUR_LOWER_HALF: from = c->vc_font.height / 2;
784 to = baseline;
785 break;
786 case CUR_LOWER_THIRD: from = (c->vc_font.height * 2) / 3;
787 to = baseline;
788 break;
789 case CUR_NONE: from = 31;
790 to = 30;
791 break;
792 default:
793 case CUR_UNDERLINE: from = baseline - 1;
794 to = baseline;
795 break;
796 }
797
798 if (sisusb->sisusb_cursor_size_from != from ||
799 sisusb->sisusb_cursor_size_to != to) {
800
801 sisusb_setidxreg(sisusb, SISCR, 0x0a, from);
802 sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0, to);
803
804 sisusb->sisusb_cursor_size_from = from;
805 sisusb->sisusb_cursor_size_to = to;
806 }
807
Arjan van de Ven2682d272006-03-28 01:00:21 -0800808 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200809}
810
Jiri Slabyd705ff32016-10-03 11:18:33 +0200811static bool
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200812sisusbcon_scroll_area(struct vc_data *c, struct sisusb_usb_data *sisusb,
Jiri Slabyd705ff32016-10-03 11:18:33 +0200813 unsigned int t, unsigned int b, enum con_scroll dir,
814 unsigned int lines)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200815{
816 int cols = sisusb->sisusb_num_columns;
817 int length = ((b - t) * cols) * 2;
818 u16 eattr = c->vc_video_erase_char;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200819
820 /* sisusb->lock is down */
821
822 /* Scroll an area which does not match the
823 * visible screen's dimensions. This needs
824 * to be done separately, as it does not
825 * use hardware panning.
826 */
827
828 switch (dir) {
829
830 case SM_UP:
831 sisusbcon_memmovew(SISUSB_VADDR(0, t),
832 SISUSB_VADDR(0, t + lines),
833 (b - t - lines) * cols * 2);
834 sisusbcon_memsetw(SISUSB_VADDR(0, b - lines), eattr,
835 lines * cols * 2);
836 break;
837
838 case SM_DOWN:
839 sisusbcon_memmovew(SISUSB_VADDR(0, t + lines),
840 SISUSB_VADDR(0, t),
841 (b - t - lines) * cols * 2);
842 sisusbcon_memsetw(SISUSB_VADDR(0, t), eattr,
843 lines * cols * 2);
844 break;
845 }
846
847 sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(0, t),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200848 (long)SISUSB_HADDR(0, t), length);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200849
Arjan van de Ven2682d272006-03-28 01:00:21 -0800850 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200851
852 return 1;
853}
854
855/* Interface routine */
Jiri Slabyd705ff32016-10-03 11:18:33 +0200856static bool
857sisusbcon_scroll(struct vc_data *c, unsigned int t, unsigned int b,
858 enum con_scroll dir, unsigned int lines)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200859{
860 struct sisusb_usb_data *sisusb;
861 u16 eattr = c->vc_video_erase_char;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200862 int copyall = 0;
863 unsigned long oldorigin;
864 unsigned int delta = lines * c->vc_size_row;
865 u32 originoffset;
866
867 /* Returning != 0 means we have done the scrolling successfully.
868 * Returning 0 makes vt do the scrolling on its own.
869 * Note that con_scroll is only called if the console is
870 * visible. In that case, the origin should be our buffer,
871 * not the vt's private one.
872 */
873
874 if (!lines)
Jiri Slabyd705ff32016-10-03 11:18:33 +0200875 return true;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200876
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200877 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
878 if (!sisusb)
Jiri Slabyd705ff32016-10-03 11:18:33 +0200879 return false;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200880
881 /* sisusb->lock is down */
882
883 if (sisusb_is_inactive(c, sisusb)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800884 mutex_unlock(&sisusb->lock);
Jiri Slabyd705ff32016-10-03 11:18:33 +0200885 return false;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200886 }
887
888 /* Special case */
889 if (t || b != c->vc_rows)
890 return sisusbcon_scroll_area(c, sisusb, t, b, dir, lines);
891
892 if (c->vc_origin != c->vc_visible_origin) {
893 c->vc_visible_origin = c->vc_origin;
894 sisusbcon_set_start_address(sisusb, c);
895 }
896
897 /* limit amount to maximum realistic size */
898 if (lines > c->vc_rows)
899 lines = c->vc_rows;
900
901 oldorigin = c->vc_origin;
902
903 switch (dir) {
904
905 case SM_UP:
906
907 if (c->vc_scr_end + delta >=
908 sisusb->scrbuf + sisusb->scrbuf_size) {
909 sisusbcon_memcpyw((u16 *)sisusb->scrbuf,
910 (u16 *)(oldorigin + delta),
911 c->vc_screenbuf_size - delta);
912 c->vc_origin = sisusb->scrbuf;
913 sisusb->con_rolled_over = oldorigin - sisusb->scrbuf;
914 copyall = 1;
915 } else
916 c->vc_origin += delta;
917
918 sisusbcon_memsetw(
919 (u16 *)(c->vc_origin + c->vc_screenbuf_size - delta),
920 eattr, delta);
921
922 break;
923
924 case SM_DOWN:
925
926 if (oldorigin - delta < sisusb->scrbuf) {
927 sisusbcon_memmovew((u16 *)(sisusb->scrbuf +
928 sisusb->scrbuf_size -
929 c->vc_screenbuf_size +
930 delta),
931 (u16 *)oldorigin,
932 c->vc_screenbuf_size - delta);
933 c->vc_origin = sisusb->scrbuf +
934 sisusb->scrbuf_size -
935 c->vc_screenbuf_size;
936 sisusb->con_rolled_over = 0;
937 copyall = 1;
938 } else
939 c->vc_origin -= delta;
940
941 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
942
943 scr_memsetw((u16 *)(c->vc_origin), eattr, delta);
944
945 break;
946 }
947
948 originoffset = (u32)(c->vc_origin - sisusb->scrbuf);
949
950 if (copyall)
951 sisusb_copy_memory(sisusb,
952 (char *)c->vc_origin,
953 (u32)(sisusb->vrambase + originoffset),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200954 c->vc_screenbuf_size);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200955 else if (dir == SM_UP)
956 sisusb_copy_memory(sisusb,
957 (char *)c->vc_origin + c->vc_screenbuf_size - delta,
958 (u32)sisusb->vrambase + originoffset +
959 c->vc_screenbuf_size - delta,
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200960 delta);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200961 else
962 sisusb_copy_memory(sisusb,
963 (char *)c->vc_origin,
964 (u32)(sisusb->vrambase + originoffset),
Jiri Slaby3cc5be72016-06-23 13:34:25 +0200965 delta);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200966
967 c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
968 c->vc_visible_origin = c->vc_origin;
969
970 sisusbcon_set_start_address(sisusb, c);
971
972 c->vc_pos = c->vc_pos - oldorigin + c->vc_origin;
973
Arjan van de Ven2682d272006-03-28 01:00:21 -0800974 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200975
Jiri Slabyd705ff32016-10-03 11:18:33 +0200976 return true;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200977}
978
979/* Interface routine */
980static int
981sisusbcon_set_origin(struct vc_data *c)
982{
983 struct sisusb_usb_data *sisusb;
984
985 /* Returning != 0 means we were successful.
986 * Returning 0 will vt make to use its own
987 * screenbuffer as the origin.
988 */
989
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +0200990 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
991 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200992 return 0;
993
994 /* sisusb->lock is down */
995
996 if (sisusb_is_inactive(c, sisusb) || sisusb->con_blanked) {
Arjan van de Ven2682d272006-03-28 01:00:21 -0800997 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +0200998 return 0;
999 }
1000
1001 c->vc_origin = c->vc_visible_origin = sisusb->scrbuf;
1002
1003 sisusbcon_set_start_address(sisusb, c);
1004
1005 sisusb->con_rolled_over = 0;
1006
Arjan van de Ven2682d272006-03-28 01:00:21 -08001007 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001008
1009 return 1;
1010}
1011
1012/* Interface routine */
1013static int
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001014sisusbcon_resize(struct vc_data *c, unsigned int newcols, unsigned int newrows,
1015 unsigned int user)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001016{
1017 struct sisusb_usb_data *sisusb;
1018 int fh;
1019
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +02001020 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1021 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001022 return -ENODEV;
1023
1024 fh = sisusb->current_font_height;
1025
Arjan van de Ven2682d272006-03-28 01:00:21 -08001026 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001027
1028 /* We are quite unflexible as regards resizing. The vt code
1029 * handles sizes where the line length isn't equal the pitch
1030 * quite badly. As regards the rows, our panning tricks only
1031 * work well if the number of rows equals the visible number
1032 * of rows.
1033 */
1034
1035 if (newcols != 80 || c->vc_scan_lines / fh != newrows)
1036 return -EINVAL;
1037
1038 return 0;
1039}
1040
1041int
1042sisusbcon_do_font_op(struct sisusb_usb_data *sisusb, int set, int slot,
1043 u8 *arg, int cmapsz, int ch512, int dorecalc,
1044 struct vc_data *c, int fh, int uplock)
1045{
1046 int font_select = 0x00, i, err = 0;
1047 u32 offset = 0;
1048 u8 dummy;
1049
1050 /* sisusb->lock is down */
1051
1052 /*
1053 * The default font is kept in slot 0.
1054 * A user font is loaded in slot 2 (256 ch)
1055 * or 2+3 (512 ch).
1056 */
1057
1058 if ((slot != 0 && slot != 2) || !fh) {
1059 if (uplock)
Arjan van de Ven2682d272006-03-28 01:00:21 -08001060 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001061 return -EINVAL;
1062 }
1063
1064 if (set)
1065 sisusb->font_slot = slot;
1066
1067 /* Default font is always 256 */
1068 if (slot == 0)
1069 ch512 = 0;
1070 else
1071 offset = 4 * cmapsz;
1072
1073 font_select = (slot == 0) ? 0x00 : (ch512 ? 0x0e : 0x0a);
1074
1075 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1076 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x04); /* Write to plane 2 */
1077 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x07); /* Memory mode a0-bf */
1078 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset */
1079
1080 if (err)
1081 goto font_op_error;
1082
1083 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x03); /* Select plane read 2 */
1084 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x00); /* Disable odd/even */
1085 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x00); /* Address range a0-bf */
1086
1087 if (err)
1088 goto font_op_error;
1089
1090 if (arg) {
1091 if (set)
1092 for (i = 0; i < cmapsz; i++) {
1093 err |= sisusb_writeb(sisusb,
1094 sisusb->vrambase + offset + i,
1095 arg[i]);
1096 if (err)
1097 break;
1098 }
1099 else
1100 for (i = 0; i < cmapsz; i++) {
1101 err |= sisusb_readb(sisusb,
1102 sisusb->vrambase + offset + i,
1103 &arg[i]);
1104 if (err)
1105 break;
1106 }
1107
1108 /*
1109 * In 512-character mode, the character map is not contiguous if
1110 * we want to remain EGA compatible -- which we do
1111 */
1112
1113 if (ch512) {
1114 if (set)
1115 for (i = 0; i < cmapsz; i++) {
1116 err |= sisusb_writeb(sisusb,
1117 sisusb->vrambase + offset +
1118 (2 * cmapsz) + i,
1119 arg[cmapsz + i]);
1120 if (err)
1121 break;
1122 }
1123 else
1124 for (i = 0; i < cmapsz; i++) {
1125 err |= sisusb_readb(sisusb,
1126 sisusb->vrambase + offset +
1127 (2 * cmapsz) + i,
1128 &arg[cmapsz + i]);
1129 if (err)
1130 break;
1131 }
1132 }
1133 }
1134
1135 if (err)
1136 goto font_op_error;
1137
1138 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x01); /* Reset */
1139 err |= sisusb_setidxreg(sisusb, SISSR, 0x02, 0x03); /* Write to planes 0+1 */
1140 err |= sisusb_setidxreg(sisusb, SISSR, 0x04, 0x03); /* Memory mode a0-bf */
1141 if (set)
1142 sisusb_setidxreg(sisusb, SISSR, 0x03, font_select);
1143 err |= sisusb_setidxreg(sisusb, SISSR, 0x00, 0x03); /* Reset end */
1144
1145 if (err)
1146 goto font_op_error;
1147
1148 err |= sisusb_setidxreg(sisusb, SISGR, 0x04, 0x00); /* Select plane read 0 */
1149 err |= sisusb_setidxreg(sisusb, SISGR, 0x05, 0x10); /* Enable odd/even */
1150 err |= sisusb_setidxreg(sisusb, SISGR, 0x06, 0x06); /* Address range b8-bf */
1151
1152 if (err)
1153 goto font_op_error;
1154
1155 if ((set) && (ch512 != sisusb->current_font_512)) {
1156
1157 /* Font is shared among all our consoles.
1158 * And so is the hi_font_mask.
1159 */
1160 for (i = 0; i < MAX_NR_CONSOLES; i++) {
Bill Pemberton993e33b2010-04-28 16:59:36 -04001161 struct vc_data *d = vc_cons[i].d;
1162 if (d && d->vc_sw == &sisusb_con)
1163 d->vc_hi_font_mask = ch512 ? 0x0800 : 0;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001164 }
1165
1166 sisusb->current_font_512 = ch512;
1167
1168 /* color plane enable register:
1169 256-char: enable intensity bit
1170 512-char: disable intensity bit */
1171 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1172 sisusb_setreg(sisusb, SISAR, 0x12);
1173 sisusb_setreg(sisusb, SISAR, ch512 ? 0x07 : 0x0f);
1174
1175 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1176 sisusb_setreg(sisusb, SISAR, 0x20);
1177 sisusb_getreg(sisusb, SISINPSTAT, &dummy);
1178 }
1179
1180 if (dorecalc) {
1181
1182 /*
1183 * Adjust the screen to fit a font of a certain height
1184 */
1185
1186 unsigned char ovr, vde, fsr;
1187 int rows = 0, maxscan = 0;
1188
1189 if (c) {
1190
1191 /* Number of video rows */
1192 rows = c->vc_scan_lines / fh;
1193 /* Scan lines to actually display-1 */
1194 maxscan = rows * fh - 1;
1195
1196 /*printk(KERN_DEBUG "sisusb recalc rows %d maxscan %d fh %d sl %d\n",
1197 rows, maxscan, fh, c->vc_scan_lines);*/
1198
1199 sisusb_getidxreg(sisusb, SISCR, 0x07, &ovr);
1200 vde = maxscan & 0xff;
1201 ovr = (ovr & 0xbd) |
1202 ((maxscan & 0x100) >> 7) |
1203 ((maxscan & 0x200) >> 3);
1204 sisusb_setidxreg(sisusb, SISCR, 0x07, ovr);
1205 sisusb_setidxreg(sisusb, SISCR, 0x12, vde);
1206
1207 }
1208
1209 sisusb_getidxreg(sisusb, SISCR, 0x09, &fsr);
1210 fsr = (fsr & 0xe0) | (fh - 1);
1211 sisusb_setidxreg(sisusb, SISCR, 0x09, fsr);
1212 sisusb->current_font_height = fh;
1213
1214 sisusb->sisusb_cursor_size_from = -1;
1215 sisusb->sisusb_cursor_size_to = -1;
1216
1217 }
1218
1219 if (uplock)
Arjan van de Ven2682d272006-03-28 01:00:21 -08001220 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001221
1222 if (dorecalc && c) {
Bill Pemberton993e33b2010-04-28 16:59:36 -04001223 int rows = c->vc_scan_lines / fh;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001224
1225 /* Now adjust our consoles' size */
1226
1227 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1228 struct vc_data *vc = vc_cons[i].d;
1229
1230 if (vc && vc->vc_sw == &sisusb_con) {
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001231 if (con_is_visible(vc)) {
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001232 vc->vc_sw->con_cursor(vc, CM_DRAW);
1233 }
1234 vc->vc_font.height = fh;
1235 vc_resize(vc, 0, rows);
1236 }
1237 }
1238 }
1239
1240 return 0;
1241
1242font_op_error:
1243 if (uplock)
Arjan van de Ven2682d272006-03-28 01:00:21 -08001244 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001245
1246 return -EIO;
1247}
1248
1249/* Interface routine */
1250static int
1251sisusbcon_font_set(struct vc_data *c, struct console_font *font,
1252 unsigned flags)
1253{
1254 struct sisusb_usb_data *sisusb;
1255 unsigned charcount = font->charcount;
1256
1257 if (font->width != 8 || (charcount != 256 && charcount != 512))
1258 return -EINVAL;
1259
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +02001260 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1261 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001262 return -ENODEV;
1263
1264 /* sisusb->lock is down */
1265
1266 /* Save the user-provided font into a buffer. This
1267 * is used for restoring text mode after quitting
1268 * from X and for the con_getfont routine.
1269 */
1270 if (sisusb->font_backup) {
1271 if (sisusb->font_backup_size < charcount) {
1272 vfree(sisusb->font_backup);
1273 sisusb->font_backup = NULL;
1274 }
1275 }
1276
1277 if (!sisusb->font_backup)
1278 sisusb->font_backup = vmalloc(charcount * 32);
1279
1280 if (sisusb->font_backup) {
1281 memcpy(sisusb->font_backup, font->data, charcount * 32);
1282 sisusb->font_backup_size = charcount;
1283 sisusb->font_backup_height = font->height;
1284 sisusb->font_backup_512 = (charcount == 512) ? 1 : 0;
1285 }
1286
1287 /* do_font_op ups sisusb->lock */
1288
1289 return sisusbcon_do_font_op(sisusb, 1, 2, font->data,
1290 8192, (charcount == 512),
1291 (!(flags & KD_FONT_FLAG_DONT_RECALC)) ? 1 : 0,
1292 c, font->height, 1);
1293}
1294
1295/* Interface routine */
1296static int
1297sisusbcon_font_get(struct vc_data *c, struct console_font *font)
1298{
1299 struct sisusb_usb_data *sisusb;
1300
Greg Kroah-Hartmanb87f5672015-04-30 11:33:03 +02001301 sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num);
1302 if (!sisusb)
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001303 return -ENODEV;
1304
1305 /* sisusb->lock is down */
1306
1307 font->width = 8;
1308 font->height = c->vc_font.height;
1309 font->charcount = 256;
1310
1311 if (!font->data) {
Arjan van de Ven2682d272006-03-28 01:00:21 -08001312 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001313 return 0;
1314 }
1315
1316 if (!sisusb->font_backup) {
Arjan van de Ven2682d272006-03-28 01:00:21 -08001317 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001318 return -ENODEV;
1319 }
1320
1321 /* Copy 256 chars only, like vgacon */
1322 memcpy(font->data, sisusb->font_backup, 256 * 32);
1323
Arjan van de Ven2682d272006-03-28 01:00:21 -08001324 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001325
1326 return 0;
1327}
1328
1329/*
1330 * The console `switch' structure for the sisusb console
1331 */
1332
1333static const struct consw sisusb_con = {
1334 .owner = THIS_MODULE,
1335 .con_startup = sisusbcon_startup,
1336 .con_init = sisusbcon_init,
1337 .con_deinit = sisusbcon_deinit,
1338 .con_clear = sisusbcon_clear,
1339 .con_putc = sisusbcon_putc,
1340 .con_putcs = sisusbcon_putcs,
1341 .con_cursor = sisusbcon_cursor,
1342 .con_scroll = sisusbcon_scroll,
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001343 .con_switch = sisusbcon_switch,
1344 .con_blank = sisusbcon_blank,
1345 .con_font_set = sisusbcon_font_set,
1346 .con_font_get = sisusbcon_font_get,
1347 .con_set_palette = sisusbcon_set_palette,
1348 .con_scrolldelta = sisusbcon_scrolldelta,
1349 .con_build_attr = sisusbcon_build_attr,
1350 .con_invert_region = sisusbcon_invert_region,
1351 .con_set_origin = sisusbcon_set_origin,
1352 .con_save_screen = sisusbcon_save_screen,
1353 .con_resize = sisusbcon_resize,
1354};
1355
1356/* Our very own dummy console driver */
1357
1358static const char *sisusbdummycon_startup(void)
1359{
1360 return "SISUSBVGADUMMY";
1361}
1362
1363static void sisusbdummycon_init(struct vc_data *vc, int init)
1364{
1365 vc->vc_can_do_color = 1;
1366 if (init) {
1367 vc->vc_cols = 80;
1368 vc->vc_rows = 25;
1369 } else
1370 vc_resize(vc, 80, 25);
1371}
1372
1373static int sisusbdummycon_dummy(void)
1374{
1375 return 0;
1376}
1377
1378#define SISUSBCONDUMMY (void *)sisusbdummycon_dummy
1379
Adrian Bunkdf47e5332006-04-15 11:17:27 +02001380static const struct consw sisusb_dummy_con = {
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001381 .owner = THIS_MODULE,
1382 .con_startup = sisusbdummycon_startup,
1383 .con_init = sisusbdummycon_init,
1384 .con_deinit = SISUSBCONDUMMY,
1385 .con_clear = SISUSBCONDUMMY,
1386 .con_putc = SISUSBCONDUMMY,
1387 .con_putcs = SISUSBCONDUMMY,
1388 .con_cursor = SISUSBCONDUMMY,
1389 .con_scroll = SISUSBCONDUMMY,
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001390 .con_switch = SISUSBCONDUMMY,
1391 .con_blank = SISUSBCONDUMMY,
1392 .con_font_set = SISUSBCONDUMMY,
1393 .con_font_get = SISUSBCONDUMMY,
1394 .con_font_default = SISUSBCONDUMMY,
1395 .con_font_copy = SISUSBCONDUMMY,
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001396};
1397
1398int
1399sisusb_console_init(struct sisusb_usb_data *sisusb, int first, int last)
1400{
Felipe Balbi7b5cd5f2007-08-15 10:38:12 -04001401 int i, ret;
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001402
Arjan van de Ven2682d272006-03-28 01:00:21 -08001403 mutex_lock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001404
1405 /* Erm.. that should not happen */
1406 if (sisusb->haveconsole || !sisusb->SiS_Pr) {
Arjan van de Ven2682d272006-03-28 01:00:21 -08001407 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001408 return 1;
1409 }
1410
1411 sisusb->con_first = first;
1412 sisusb->con_last = last;
1413
1414 if (first > last ||
1415 first > MAX_NR_CONSOLES ||
1416 last > MAX_NR_CONSOLES) {
Arjan van de Ven2682d272006-03-28 01:00:21 -08001417 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001418 return 1;
1419 }
1420
1421 /* If gfxcore not initialized or no consoles given, quit graciously */
1422 if (!sisusb->gfxinit || first < 1 || last < 1) {
Arjan van de Ven2682d272006-03-28 01:00:21 -08001423 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001424 return 0;
1425 }
1426
1427 sisusb->sisusb_cursor_loc = -1;
1428 sisusb->sisusb_cursor_size_from = -1;
1429 sisusb->sisusb_cursor_size_to = -1;
1430
1431 /* Set up text mode (and upload default font) */
1432 if (sisusb_reset_text_mode(sisusb, 1)) {
Arjan van de Ven2682d272006-03-28 01:00:21 -08001433 mutex_unlock(&sisusb->lock);
Felipe Balbi7b5cd5f2007-08-15 10:38:12 -04001434 dev_err(&sisusb->sisusb_dev->dev, "Failed to set up text mode\n");
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001435 return 1;
1436 }
1437
1438 /* Initialize some gfx registers */
1439 sisusb_initialize(sisusb);
1440
1441 for (i = first - 1; i <= last - 1; i++) {
1442 /* Save sisusb for our interface routines */
1443 mysisusbs[i] = sisusb;
1444 }
1445
1446 /* Initial console setup */
1447 sisusb->sisusb_num_columns = 80;
1448
1449 /* Use a 32K buffer (matches b8000-bffff area) */
1450 sisusb->scrbuf_size = 32 * 1024;
1451
1452 /* Allocate screen buffer */
1453 if (!(sisusb->scrbuf = (unsigned long)vmalloc(sisusb->scrbuf_size))) {
Arjan van de Ven2682d272006-03-28 01:00:21 -08001454 mutex_unlock(&sisusb->lock);
Felipe Balbi7b5cd5f2007-08-15 10:38:12 -04001455 dev_err(&sisusb->sisusb_dev->dev, "Failed to allocate screen buffer\n");
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001456 return 1;
1457 }
1458
Arjan van de Ven2682d272006-03-28 01:00:21 -08001459 mutex_unlock(&sisusb->lock);
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001460
1461 /* Now grab the desired console(s) */
Wang YanQing155957f2013-05-21 13:15:12 +08001462 console_lock();
1463 ret = do_take_over_console(&sisusb_con, first - 1, last - 1, 0);
1464 console_unlock();
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001465 if (!ret)
1466 sisusb->haveconsole = 1;
1467 else {
1468 for (i = first - 1; i <= last - 1; i++)
1469 mysisusbs[i] = NULL;
1470 }
1471
1472 return ret;
1473}
1474
1475void
1476sisusb_console_exit(struct sisusb_usb_data *sisusb)
1477{
1478 int i;
1479
1480 /* This is called if the device is disconnected
1481 * and while disconnect and lock semaphores
1482 * are up. This should be save because we
1483 * can't lose our sisusb any other way but by
1484 * disconnection (and hence, the disconnect
1485 * sema is for protecting all other access
1486 * functions from disconnection, not the
1487 * other way round).
1488 */
1489
1490 /* Now what do we do in case of disconnection:
1491 * One alternative would be to simply call
1492 * give_up_console(). Nah, not a good idea.
1493 * give_up_console() is obviously buggy as it
1494 * only discards the consw pointer from the
1495 * driver_map, but doesn't adapt vc->vc_sw
1496 * of the affected consoles. Hence, the next
1497 * call to any of the console functions will
1498 * eventually take a trip to oops county.
1499 * Also, give_up_console for some reason
1500 * doesn't decrement our module refcount.
1501 * Instead, we switch our consoles to a private
1502 * dummy console. This, of course, keeps our
1503 * refcount up as well, but it works perfectly.
1504 */
1505
1506 if (sisusb->haveconsole) {
1507 for (i = 0; i < MAX_NR_CONSOLES; i++)
Wang YanQing155957f2013-05-21 13:15:12 +08001508 if (sisusb->havethisconsole[i]) {
1509 console_lock();
1510 do_take_over_console(&sisusb_dummy_con, i, i, 0);
1511 console_unlock();
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001512 /* At this point, con_deinit for all our
Wang YanQing155957f2013-05-21 13:15:12 +08001513 * consoles is executed by do_take_over_console().
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001514 */
Wang YanQing155957f2013-05-21 13:15:12 +08001515 }
Thomas Winischhofer1bbb4f22005-08-29 17:01:16 +02001516 sisusb->haveconsole = 0;
1517 }
1518
1519 vfree((void *)sisusb->scrbuf);
1520 sisusb->scrbuf = 0;
1521
1522 vfree(sisusb->font_backup);
1523 sisusb->font_backup = NULL;
1524}
1525
1526void __init sisusb_init_concode(void)
1527{
1528 int i;
1529
1530 for (i = 0; i < MAX_NR_CONSOLES; i++)
1531 mysisusbs[i] = NULL;
1532}
1533
1534#endif /* INCL_CON */
1535
1536
1537