📄 sisusb_con.c
字号:
/* * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles * * VGA text mode console part * * Copyright (C) 2005 by Thomas Winischhofer, Vienna, Austria * * If distributed as part of the Linux kernel, this code is licensed under the * terms of the GPL v2. * * Otherwise, the following license terms apply: * * * Redistribution and use in source and binary forms, with or without * * modification, are permitted provided that the following conditions * * are met: * * 1) Redistributions of source code must retain the above copyright * * notice, this list of conditions and the following disclaimer. * * 2) Redistributions in binary form must reproduce the above copyright * * notice, this list of conditions and the following disclaimer in the * * documentation and/or other materials provided with the distribution. * * 3) The name of the author may not be used to endorse or promote products * * derived from this software without specific psisusbr written permission. * * * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Author: Thomas Winischhofer <thomas@winischhofer.net> * * Portions based on vgacon.c which are * Created 28 Sep 1997 by Geert Uytterhoeven * Rewritten by Martin Mares <mj@ucw.cz>, July 1998 * based on code Copyright (C) 1991, 1992 Linus Torvalds * 1995 Jay Estabrook * * A note on using in_atomic() in here: We can't handle console * calls from non-schedulable context due to our USB-dependend * nature. For now, this driver just ignores any calls if it * detects this state. * */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/signal.h>#include <linux/sched.h>#include <linux/fs.h>#include <linux/tty.h>#include <linux/console.h>#include <linux/string.h>#include <linux/kd.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/vt_kern.h>#include <linux/selection.h>#include <linux/spinlock.h>#include <linux/kref.h>#include <linux/smp_lock.h>#include <linux/ioport.h>#include <linux/interrupt.h>#include <linux/vmalloc.h>#include "sisusb.h"#ifdef INCL_SISUSB_CONextern int sisusb_setreg(struct sisusb_usb_data *, int, u8);extern int sisusb_getreg(struct sisusb_usb_data *, int, u8 *);extern int sisusb_setidxreg(struct sisusb_usb_data *, int, u8, u8);extern int sisusb_getidxreg(struct sisusb_usb_data *, int, u8, u8 *);extern int sisusb_setidxregor(struct sisusb_usb_data *, int, u8, u8);extern int sisusb_setidxregand(struct sisusb_usb_data *, int, u8, u8);extern int sisusb_setidxregandor(struct sisusb_usb_data *, int, u8, u8, u8);extern int sisusb_writeb(struct sisusb_usb_data *sisusb, u32 adr, u8 data);extern int sisusb_readb(struct sisusb_usb_data *sisusb, u32 adr, u8 *data);extern int sisusb_writew(struct sisusb_usb_data *sisusb, u32 adr, u16 data);extern int sisusb_readw(struct sisusb_usb_data *sisusb, u32 adr, u16 *data);extern int sisusb_copy_memory(struct sisusb_usb_data *sisusb, char *src, u32 dest, int length, size_t *bytes_written);extern void sisusb_delete(struct kref *kref);extern int sisusb_reset_text_mode(struct sisusb_usb_data *sisusb, int init);extern int SiSUSBSetMode(struct SiS_Private *SiS_Pr, unsigned short ModeNo);#define sisusbcon_writew(val, addr) (*(addr) = (val))#define sisusbcon_readw(addr) (*(addr))#define sisusbcon_memmovew(d, s, c) memmove(d, s, c)#define sisusbcon_memcpyw(d, s, c) memcpy(d, s, c)/* vc_data -> sisusb conversion table */static struct sisusb_usb_data *mysisusbs[MAX_NR_CONSOLES];/* Forward declaration */static const struct consw sisusb_con;extern struct semaphore disconnect_sem;static inline voidsisusbcon_memsetw(u16 *s, u16 c, unsigned int count){ count /= 2; while (count--) sisusbcon_writew(c, s++);}static inline voidsisusb_initialize(struct sisusb_usb_data *sisusb){ /* Reset cursor and start address */ if (sisusb_setidxreg(sisusb, SISCR, 0x0c, 0x00)) return; if (sisusb_setidxreg(sisusb, SISCR, 0x0d, 0x00)) return; if (sisusb_setidxreg(sisusb, SISCR, 0x0e, 0x00)) return; sisusb_setidxreg(sisusb, SISCR, 0x0f, 0x00);}static inline voidsisusbcon_set_start_address(struct sisusb_usb_data *sisusb, struct vc_data *c){ sisusb->cur_start_addr = (c->vc_visible_origin - sisusb->scrbuf) / 2; sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8)); sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));}voidsisusb_set_cursor(struct sisusb_usb_data *sisusb, unsigned int location){ if (sisusb->sisusb_cursor_loc == location) return; sisusb->sisusb_cursor_loc = location; /* Hardware bug: Text cursor appears twice or not at all * at some positions. Work around it with the cursor skew * bits. */ if ((location & 0x0007) == 0x0007) { sisusb->bad_cursor_pos = 1; location--; if (sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0x1f, 0x20)) return; } else if (sisusb->bad_cursor_pos) { if (sisusb_setidxregand(sisusb, SISCR, 0x0b, 0x1f)) return; sisusb->bad_cursor_pos = 0; } if (sisusb_setidxreg(sisusb, SISCR, 0x0e, (location >> 8))) return; sisusb_setidxreg(sisusb, SISCR, 0x0f, (location & 0xff));}static inline struct sisusb_usb_data *sisusb_get_sisusb(unsigned short console){ return mysisusbs[console];}static inline intsisusb_sisusb_valid(struct sisusb_usb_data *sisusb){ if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) return 0; return 1;}static struct sisusb_usb_data *sisusb_get_sisusb_lock_and_check(unsigned short console){ struct sisusb_usb_data *sisusb; /* We can't handle console calls in non-schedulable * context due to our locks and the USB transport. * So we simply ignore them. This should only affect * some calls to printk. */ if (in_atomic()) return NULL; if (!(sisusb = sisusb_get_sisusb(console))) return NULL; down(&sisusb->lock); if (!sisusb_sisusb_valid(sisusb) || !sisusb->havethisconsole[console]) { up(&sisusb->lock); return NULL; } return sisusb;}static intsisusb_is_inactive(struct vc_data *c, struct sisusb_usb_data *sisusb){ if (sisusb->is_gfx || sisusb->textmodedestroyed || c->vc_mode != KD_TEXT) return 1; return 0;}/* con_startup console interface routine */static const char *sisusbcon_startup(void){ return "SISUSBCON";}/* con_init console interface routine */static voidsisusbcon_init(struct vc_data *c, int init){ struct sisusb_usb_data *sisusb; int cols, rows; /* This is called by take_over_console(), * ie by us/under our control. It is * only called after text mode and fonts * are set up/restored. */ down(&disconnect_sem); if (!(sisusb = sisusb_get_sisusb(c->vc_num))) { up(&disconnect_sem); return; } down(&sisusb->lock); if (!sisusb_sisusb_valid(sisusb)) { up(&sisusb->lock); up(&disconnect_sem); return; } c->vc_can_do_color = 1; c->vc_complement_mask = 0x7700; c->vc_hi_font_mask = sisusb->current_font_512 ? 0x0800 : 0; sisusb->haveconsole = 1; sisusb->havethisconsole[c->vc_num] = 1; /* We only support 640x400 */ c->vc_scan_lines = 400; c->vc_font.height = sisusb->current_font_height; /* We only support width = 8 */ cols = 80; rows = c->vc_scan_lines / c->vc_font.height; /* Increment usage count for our sisusb. * Doing so saves us from upping/downing * the disconnect semaphore; we can't * lose our sisusb until this is undone * in con_deinit. For all other console * interface functions, it suffices to * use sisusb->lock and do a quick check * of sisusb for device disconnection. */ kref_get(&sisusb->kref); if (!*c->vc_uni_pagedir_loc) con_set_default_unimap(c); up(&sisusb->lock); up(&disconnect_sem); if (init) { c->vc_cols = cols; c->vc_rows = rows; } else vc_resize(c, cols, rows);}/* con_deinit console interface routine */static voidsisusbcon_deinit(struct vc_data *c){ struct sisusb_usb_data *sisusb; int i; /* This is called by take_over_console() * and others, ie not under our control. */ down(&disconnect_sem); if (!(sisusb = sisusb_get_sisusb(c->vc_num))) { up(&disconnect_sem); return; } down(&sisusb->lock); /* Clear ourselves in mysisusbs */ mysisusbs[c->vc_num] = NULL; sisusb->havethisconsole[c->vc_num] = 0; /* Free our font buffer if all consoles are gone */ if (sisusb->font_backup) { for(i = 0; i < MAX_NR_CONSOLES; i++) { if (sisusb->havethisconsole[c->vc_num]) break; } if (i == MAX_NR_CONSOLES) { vfree(sisusb->font_backup); sisusb->font_backup = NULL; } } up(&sisusb->lock); /* decrement the usage count on our sisusb */ kref_put(&sisusb->kref, sisusb_delete); up(&disconnect_sem);}/* interface routine */static u8sisusbcon_build_attr(struct vc_data *c, u8 color, u8 intensity, u8 blink, u8 underline, u8 reverse){ u8 attr = color; if (underline) attr = (attr & 0xf0) | c->vc_ulcolor; else if (intensity == 0) attr = (attr & 0xf0) | c->vc_halfcolor; if (reverse) attr = ((attr) & 0x88) | ((((attr) >> 4) | ((attr) << 4)) & 0x77); if (blink) attr ^= 0x80; if (intensity == 2) attr ^= 0x08; return attr;}/* Interface routine */static voidsisusbcon_invert_region(struct vc_data *vc, u16 *p, int count){ /* Invert a region. This is called with a pointer * to the console's internal screen buffer. So we * simply do the inversion there and rely on * a call to putc(s) to update the real screen. */ while (count--) { u16 a = sisusbcon_readw(p); a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4); sisusbcon_writew(a, p++); }}#define SISUSB_VADDR(x,y) \ ((u16 *)c->vc_origin + \ (y) * sisusb->sisusb_num_columns + \ (x))#define SISUSB_HADDR(x,y) \ ((u16 *)(sisusb->vrambase + (c->vc_origin - sisusb->scrbuf)) + \ (y) * sisusb->sisusb_num_columns + \ (x))/* Interface routine */static voidsisusbcon_putc(struct vc_data *c, int ch, int y, int x){ struct sisusb_usb_data *sisusb; ssize_t written; if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num))) return; /* sisusb->lock is down */ /* Don't need to put the character into buffer ourselves, * because the vt does this BEFORE calling us. */#if 0 sisusbcon_writew(ch, SISUSB_VADDR(x, y));#endif if (sisusb_is_inactive(c, sisusb)) { up(&sisusb->lock); return; } sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y), (u32)SISUSB_HADDR(x, y), 2, &written); up(&sisusb->lock);}/* Interface routine */static voidsisusbcon_putcs(struct vc_data *c, const unsigned short *s, int count, int y, int x){ struct sisusb_usb_data *sisusb; ssize_t written; u16 *dest; int i; if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num))) return; /* sisusb->lock is down */ /* Need to put the characters into the buffer ourselves, * because the vt does this AFTER calling us. */ dest = SISUSB_VADDR(x, y); for (i = count; i > 0; i--) sisusbcon_writew(sisusbcon_readw(s++), dest++); if (sisusb_is_inactive(c, sisusb)) { up(&sisusb->lock); return; } sisusb_copy_memory(sisusb, (char *)SISUSB_VADDR(x, y), (u32)SISUSB_HADDR(x, y), count * 2, &written); up(&sisusb->lock);}/* Interface routine */static voidsisusbcon_clear(struct vc_data *c, int y, int x, int height, int width){ struct sisusb_usb_data *sisusb; u16 eattr = c->vc_video_erase_char; ssize_t written; int i, length, cols; u16 *dest; if (width <= 0 || height <= 0) return; if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num))) return; /* sisusb->lock is down */ /* Need to clear buffer ourselves, because the vt does * this AFTER calling us. */ dest = SISUSB_VADDR(x, y); cols = sisusb->sisusb_num_columns; if (width > cols) width = cols; if (x == 0 && width >= c->vc_cols) { sisusbcon_memsetw(dest, eattr, height * cols * 2); } else { for (i = height; i > 0; i--, dest += cols) sisusbcon_memsetw(dest, eattr, width * 2); } if (sisusb_is_inactive(c, sisusb)) { up(&sisusb->lock); return; } length = ((height * cols) - x - (cols - width - x)) * 2; sisusb_copy_memory(sisusb, (unsigned char *)SISUSB_VADDR(x, y), (u32)SISUSB_HADDR(x, y), length, &written); up(&sisusb->lock);}/* Interface routine */static voidsisusbcon_bmove(struct vc_data *c, int sy, int sx, int dy, int dx, int height, int width){ struct sisusb_usb_data *sisusb; ssize_t written; int cols, length;#if 0 u16 *src, *dest; int i;#endif if (width <= 0 || height <= 0) return; if (!(sisusb = sisusb_get_sisusb_lock_and_check(c->vc_num))) return; /* sisusb->lock is down */ cols = sisusb->sisusb_num_columns; /* Don't need to move data outselves, because * vt does this BEFORE calling us. * This is only used by vt's insert/deletechar. */#if 0 if (sx == 0 && dx == 0 && width >= c->vc_cols && width <= cols) { sisusbcon_memmovew(SISUSB_VADDR(0, dy), SISUSB_VADDR(0, sy), height * width * 2); } else if (dy < sy || (dy == sy && dx < sx)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -