sticon.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * linux/drivers/video/console/sticon.c - console driver using HP's STI firmware
  3. *
  4. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  5. * Copyright (C) 2002-2020 Helge Deller <deller@gmx.de>
  6. *
  7. * Based on linux/drivers/video/vgacon.c and linux/drivers/video/fbcon.c,
  8. * which were
  9. *
  10. * Created 28 Sep 1997 by Geert Uytterhoeven
  11. * Rewritten by Martin Mares <mj@ucw.cz>, July 1998
  12. * Copyright (C) 1991, 1992 Linus Torvalds
  13. * 1995 Jay Estabrook
  14. * Copyright (C) 1995 Geert Uytterhoeven
  15. * Copyright (C) 1993 Bjoern Brauel
  16. * Roman Hodek
  17. * Copyright (C) 1993 Hamish Macdonald
  18. * Greg Harp
  19. * Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
  20. *
  21. * with work by William Rucklidge (wjr@cs.cornell.edu)
  22. * Geert Uytterhoeven
  23. * Jes Sorensen (jds@kom.auc.dk)
  24. * Martin Apel
  25. * with work by Guenther Kelleter
  26. * Martin Schaller
  27. * Andreas Schwab
  28. * Emmanuel Marty (core@ggi-project.org)
  29. * Jakub Jelinek (jj@ultra.linux.cz)
  30. * Martin Mares <mj@ucw.cz>
  31. *
  32. * This file is subject to the terms and conditions of the GNU General Public
  33. * License. See the file COPYING in the main directory of this archive for
  34. * more details.
  35. *
  36. */
  37. #include <linux/init.h>
  38. #include <linux/kernel.h>
  39. #include <linux/console.h>
  40. #include <linux/errno.h>
  41. #include <linux/vt_kern.h>
  42. #include <linux/kd.h>
  43. #include <linux/selection.h>
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/font.h>
  47. #include <linux/crc32.h>
  48. #include <linux/fb.h>
  49. #include <asm/io.h>
  50. #include <video/sticore.h>
  51. /* switching to graphics mode */
  52. #define BLANK 0
  53. static int vga_is_gfx;
  54. #define STI_DEF_FONT sticon_sti->font
  55. /* borrowed from fbcon.c */
  56. #define FNTREFCOUNT(fd) (fd->refcount)
  57. #define FNTCRC(fd) (fd->crc)
  58. static struct sti_cooked_font *font_data[MAX_NR_CONSOLES];
  59. /* this is the sti_struct used for this console */
  60. static struct sti_struct *sticon_sti;
  61. static const char *sticon_startup(void)
  62. {
  63. return "STI console";
  64. }
  65. static void sticon_putcs(struct vc_data *conp, const u16 *s, unsigned int count,
  66. unsigned int ypos, unsigned int xpos)
  67. {
  68. if (vga_is_gfx || console_blanked)
  69. return;
  70. if (conp->vc_mode != KD_TEXT)
  71. return;
  72. while (count--) {
  73. sti_putc(sticon_sti, scr_readw(s++), ypos, xpos++,
  74. font_data[conp->vc_num]);
  75. }
  76. }
  77. static void sticon_cursor(struct vc_data *conp, bool enable)
  78. {
  79. unsigned short car1;
  80. /* no cursor update if screen is blanked */
  81. if (vga_is_gfx || console_blanked)
  82. return;
  83. car1 = conp->vc_screenbuf[conp->state.x + conp->state.y * conp->vc_cols];
  84. if (!enable) {
  85. sti_putc(sticon_sti, car1, conp->state.y, conp->state.x,
  86. font_data[conp->vc_num]);
  87. return;
  88. }
  89. switch (CUR_SIZE(conp->vc_cursor_type)) {
  90. case CUR_UNDERLINE:
  91. case CUR_LOWER_THIRD:
  92. case CUR_LOWER_HALF:
  93. case CUR_TWO_THIRDS:
  94. case CUR_BLOCK:
  95. sti_putc(sticon_sti, (car1 & 255) + (0 << 8) + (7 << 11),
  96. conp->state.y, conp->state.x, font_data[conp->vc_num]);
  97. break;
  98. }
  99. }
  100. static bool sticon_scroll(struct vc_data *conp, unsigned int t,
  101. unsigned int b, enum con_scroll dir, unsigned int count)
  102. {
  103. struct sti_struct *sti = sticon_sti;
  104. if (vga_is_gfx)
  105. return false;
  106. sticon_cursor(conp, false);
  107. switch (dir) {
  108. case SM_UP:
  109. sti_bmove(sti, t + count, 0, t, 0, b - t - count, conp->vc_cols,
  110. font_data[conp->vc_num]);
  111. sti_clear(sti, b - count, 0, count, conp->vc_cols,
  112. conp->vc_video_erase_char, font_data[conp->vc_num]);
  113. break;
  114. case SM_DOWN:
  115. sti_bmove(sti, t, 0, t + count, 0, b - t - count, conp->vc_cols,
  116. font_data[conp->vc_num]);
  117. sti_clear(sti, t, 0, count, conp->vc_cols,
  118. conp->vc_video_erase_char, font_data[conp->vc_num]);
  119. break;
  120. }
  121. return false;
  122. }
  123. static void sticon_set_def_font(int unit)
  124. {
  125. if (font_data[unit] != STI_DEF_FONT) {
  126. if (--FNTREFCOUNT(font_data[unit]) == 0) {
  127. kfree(font_data[unit]->raw_ptr);
  128. kfree(font_data[unit]);
  129. }
  130. font_data[unit] = STI_DEF_FONT;
  131. }
  132. }
  133. static int sticon_set_font(struct vc_data *vc, const struct console_font *op,
  134. unsigned int vpitch)
  135. {
  136. struct sti_struct *sti = sticon_sti;
  137. int vc_cols, vc_rows, vc_old_cols, vc_old_rows;
  138. int unit = vc->vc_num;
  139. int w = op->width;
  140. int h = op->height;
  141. int size, i, bpc, pitch;
  142. struct sti_rom_font *new_font;
  143. struct sti_cooked_font *cooked_font;
  144. unsigned char *data = op->data, *p;
  145. if ((w < 6) || (h < 6) || (w > 32) || (h > 32) || (vpitch != 32)
  146. || (op->charcount != 256 && op->charcount != 512))
  147. return -EINVAL;
  148. pitch = ALIGN(w, 8) / 8;
  149. bpc = pitch * h;
  150. size = bpc * op->charcount;
  151. new_font = kmalloc(sizeof(*new_font) + size, STI_LOWMEM);
  152. if (!new_font)
  153. return -ENOMEM;
  154. new_font->first_char = 0;
  155. new_font->last_char = op->charcount - 1;
  156. new_font->width = w;
  157. new_font->height = h;
  158. new_font->font_type = STI_FONT_HPROMAN8;
  159. new_font->bytes_per_char = bpc;
  160. new_font->underline_height = 0;
  161. new_font->underline_pos = 0;
  162. cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
  163. if (!cooked_font) {
  164. kfree(new_font);
  165. return -ENOMEM;
  166. }
  167. cooked_font->raw = new_font;
  168. cooked_font->raw_ptr = new_font;
  169. cooked_font->width = w;
  170. cooked_font->height = h;
  171. FNTREFCOUNT(cooked_font) = 0; /* usage counter */
  172. p = (unsigned char *) new_font;
  173. p += sizeof(*new_font);
  174. for (i = 0; i < op->charcount; i++) {
  175. memcpy(p, data, bpc);
  176. data += pitch*32;
  177. p += bpc;
  178. }
  179. FNTCRC(cooked_font) = crc32(0, new_font, size + sizeof(*new_font));
  180. sti_font_convert_bytemode(sti, cooked_font);
  181. new_font = cooked_font->raw_ptr;
  182. /* check if font is already used by other console */
  183. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  184. if (font_data[i] != STI_DEF_FONT
  185. && (FNTCRC(font_data[i]) == FNTCRC(cooked_font))) {
  186. kfree(new_font);
  187. kfree(cooked_font);
  188. /* current font is the same as the new one */
  189. if (i == unit)
  190. return 0;
  191. cooked_font = font_data[i];
  192. new_font = cooked_font->raw_ptr;
  193. break;
  194. }
  195. }
  196. /* clear screen with old font: we now may have less rows */
  197. vc_old_rows = vc->vc_rows;
  198. vc_old_cols = vc->vc_cols;
  199. sti_clear(sticon_sti, 0, 0, vc_old_rows, vc_old_cols,
  200. vc->vc_video_erase_char, font_data[vc->vc_num]);
  201. /* delete old font in case it is a user font */
  202. sticon_set_def_font(unit);
  203. FNTREFCOUNT(cooked_font)++;
  204. font_data[unit] = cooked_font;
  205. vc_cols = sti_onscreen_x(sti) / cooked_font->width;
  206. vc_rows = sti_onscreen_y(sti) / cooked_font->height;
  207. vc_resize(vc, vc_cols, vc_rows);
  208. /* need to repaint screen if cols & rows are same as old font */
  209. if (vc_cols == vc_old_cols && vc_rows == vc_old_rows)
  210. update_screen(vc);
  211. return 0;
  212. }
  213. static int sticon_font_default(struct vc_data *vc, struct console_font *op,
  214. const char *name)
  215. {
  216. sticon_set_def_font(vc->vc_num);
  217. return 0;
  218. }
  219. static int sticon_font_set(struct vc_data *vc, const struct console_font *font,
  220. unsigned int vpitch, unsigned int flags)
  221. {
  222. return sticon_set_font(vc, font, vpitch);
  223. }
  224. static void sticon_init(struct vc_data *c, bool init)
  225. {
  226. struct sti_struct *sti = sticon_sti;
  227. int vc_cols, vc_rows;
  228. sti_set(sti, 0, 0, sti_onscreen_y(sti), sti_onscreen_x(sti), 0);
  229. vc_cols = sti_onscreen_x(sti) / sti->font->width;
  230. vc_rows = sti_onscreen_y(sti) / sti->font->height;
  231. c->vc_can_do_color = 1;
  232. if (init) {
  233. c->vc_cols = vc_cols;
  234. c->vc_rows = vc_rows;
  235. } else {
  236. vc_resize(c, vc_cols, vc_rows);
  237. }
  238. }
  239. static void sticon_deinit(struct vc_data *c)
  240. {
  241. int i;
  242. /* free memory used by user font */
  243. for (i = 0; i < MAX_NR_CONSOLES; i++)
  244. sticon_set_def_font(i);
  245. }
  246. static void sticon_clear(struct vc_data *conp, unsigned int sy, unsigned int sx,
  247. unsigned int width)
  248. {
  249. sti_clear(sticon_sti, sy, sx, 1, width,
  250. conp->vc_video_erase_char, font_data[conp->vc_num]);
  251. }
  252. static bool sticon_switch(struct vc_data *conp)
  253. {
  254. return true; /* needs refreshing */
  255. }
  256. static bool sticon_blank(struct vc_data *c, enum vesa_blank_mode blank,
  257. bool mode_switch)
  258. {
  259. if (blank == VESA_NO_BLANKING) {
  260. if (mode_switch)
  261. vga_is_gfx = 0;
  262. return true;
  263. }
  264. sti_clear(sticon_sti, 0, 0, c->vc_rows, c->vc_cols, BLANK,
  265. font_data[c->vc_num]);
  266. if (mode_switch)
  267. vga_is_gfx = 1;
  268. return true;
  269. }
  270. static u8 sticon_build_attr(struct vc_data *conp, u8 color,
  271. enum vc_intensity intens,
  272. bool blink, bool underline, bool reverse,
  273. bool italic)
  274. {
  275. u8 fg = color & 7;
  276. u8 bg = (color & 0x70) >> 4;
  277. if (reverse)
  278. return (fg << 3) | bg;
  279. else
  280. return (bg << 3) | fg;
  281. }
  282. static void sticon_invert_region(struct vc_data *conp, u16 *p, int count)
  283. {
  284. int col = 1; /* vga_can_do_color; */
  285. while (count--) {
  286. u16 a = scr_readw(p);
  287. if (col)
  288. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
  289. else
  290. a = ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
  291. scr_writew(a, p++);
  292. }
  293. }
  294. static const struct consw sti_con = {
  295. .owner = THIS_MODULE,
  296. .con_startup = sticon_startup,
  297. .con_init = sticon_init,
  298. .con_deinit = sticon_deinit,
  299. .con_clear = sticon_clear,
  300. .con_putcs = sticon_putcs,
  301. .con_cursor = sticon_cursor,
  302. .con_scroll = sticon_scroll,
  303. .con_switch = sticon_switch,
  304. .con_blank = sticon_blank,
  305. .con_font_set = sticon_font_set,
  306. .con_font_default = sticon_font_default,
  307. .con_build_attr = sticon_build_attr,
  308. .con_invert_region = sticon_invert_region,
  309. };
  310. static int __init sticonsole_init(void)
  311. {
  312. int err, i;
  313. /* already initialized ? */
  314. if (sticon_sti)
  315. return 0;
  316. sticon_sti = sti_get_rom(0);
  317. if (!sticon_sti)
  318. return -ENODEV;
  319. for (i = 0; i < MAX_NR_CONSOLES; i++)
  320. font_data[i] = STI_DEF_FONT;
  321. pr_info("sticon: Initializing STI text console on %s at [%s]\n",
  322. sticon_sti->sti_data->inq_outptr.dev_name,
  323. sticon_sti->pa_path);
  324. console_lock();
  325. err = do_take_over_console(&sti_con, 0, MAX_NR_CONSOLES - 1,
  326. PAGE0->mem_cons.cl_class != CL_DUPLEX);
  327. console_unlock();
  328. return err;
  329. }
  330. module_init(sticonsole_init);
  331. MODULE_DESCRIPTION("HP STI console driver");
  332. MODULE_LICENSE("GPL");