sticon.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * linux/drivers/video/console/sticon.c - console driver using HP's STI firmware
  3. *
  4. * Copyright (C) 2000 Philipp Rumpf <[email protected]>
  5. * Copyright (C) 2002-2020 Helge Deller <[email protected]>
  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 <[email protected]>, 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 [[email protected]]
  20. *
  21. * with work by William Rucklidge ([email protected])
  22. * Geert Uytterhoeven
  23. * Jes Sorensen ([email protected])
  24. * Martin Apel
  25. * with work by Guenther Kelleter
  26. * Martin Schaller
  27. * Andreas Schwab
  28. * Emmanuel Marty ([email protected])
  29. * Jakub Jelinek ([email protected])
  30. * Martin Mares <[email protected]>
  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 "../fbdev/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_putc(struct vc_data *conp, int c, int ypos, int xpos)
  66. {
  67. if (vga_is_gfx || console_blanked)
  68. return;
  69. if (conp->vc_mode != KD_TEXT)
  70. return;
  71. sti_putc(sticon_sti, c, ypos, xpos, font_data[conp->vc_num]);
  72. }
  73. static void sticon_putcs(struct vc_data *conp, const unsigned short *s,
  74. int count, int ypos, int xpos)
  75. {
  76. if (vga_is_gfx || console_blanked)
  77. return;
  78. if (conp->vc_mode != KD_TEXT)
  79. return;
  80. while (count--) {
  81. sti_putc(sticon_sti, scr_readw(s++), ypos, xpos++,
  82. font_data[conp->vc_num]);
  83. }
  84. }
  85. static void sticon_cursor(struct vc_data *conp, int mode)
  86. {
  87. unsigned short car1;
  88. /* no cursor update if screen is blanked */
  89. if (vga_is_gfx || console_blanked)
  90. return;
  91. car1 = conp->vc_screenbuf[conp->state.x + conp->state.y * conp->vc_cols];
  92. switch (mode) {
  93. case CM_ERASE:
  94. sti_putc(sticon_sti, car1, conp->state.y, conp->state.x,
  95. font_data[conp->vc_num]);
  96. break;
  97. case CM_MOVE:
  98. case CM_DRAW:
  99. switch (CUR_SIZE(conp->vc_cursor_type)) {
  100. case CUR_UNDERLINE:
  101. case CUR_LOWER_THIRD:
  102. case CUR_LOWER_HALF:
  103. case CUR_TWO_THIRDS:
  104. case CUR_BLOCK:
  105. sti_putc(sticon_sti, (car1 & 255) + (0 << 8) + (7 << 11),
  106. conp->state.y, conp->state.x, font_data[conp->vc_num]);
  107. break;
  108. }
  109. break;
  110. }
  111. }
  112. static bool sticon_scroll(struct vc_data *conp, unsigned int t,
  113. unsigned int b, enum con_scroll dir, unsigned int count)
  114. {
  115. struct sti_struct *sti = sticon_sti;
  116. if (vga_is_gfx)
  117. return false;
  118. sticon_cursor(conp, CM_ERASE);
  119. switch (dir) {
  120. case SM_UP:
  121. sti_bmove(sti, t + count, 0, t, 0, b - t - count, conp->vc_cols,
  122. font_data[conp->vc_num]);
  123. sti_clear(sti, b - count, 0, count, conp->vc_cols,
  124. conp->vc_video_erase_char, font_data[conp->vc_num]);
  125. break;
  126. case SM_DOWN:
  127. sti_bmove(sti, t, 0, t + count, 0, b - t - count, conp->vc_cols,
  128. font_data[conp->vc_num]);
  129. sti_clear(sti, t, 0, count, conp->vc_cols,
  130. conp->vc_video_erase_char, font_data[conp->vc_num]);
  131. break;
  132. }
  133. return false;
  134. }
  135. static int sticon_set_def_font(int unit, struct console_font *op)
  136. {
  137. if (font_data[unit] != STI_DEF_FONT) {
  138. if (--FNTREFCOUNT(font_data[unit]) == 0) {
  139. kfree(font_data[unit]->raw_ptr);
  140. kfree(font_data[unit]);
  141. }
  142. font_data[unit] = STI_DEF_FONT;
  143. }
  144. return 0;
  145. }
  146. static int sticon_set_font(struct vc_data *vc, struct console_font *op)
  147. {
  148. struct sti_struct *sti = sticon_sti;
  149. int vc_cols, vc_rows, vc_old_cols, vc_old_rows;
  150. int unit = vc->vc_num;
  151. int w = op->width;
  152. int h = op->height;
  153. int size, i, bpc, pitch;
  154. struct sti_rom_font *new_font;
  155. struct sti_cooked_font *cooked_font;
  156. unsigned char *data = op->data, *p;
  157. if ((w < 6) || (h < 6) || (w > 32) || (h > 32)
  158. || (op->charcount != 256 && op->charcount != 512))
  159. return -EINVAL;
  160. pitch = ALIGN(w, 8) / 8;
  161. bpc = pitch * h;
  162. size = bpc * op->charcount;
  163. new_font = kmalloc(sizeof(*new_font) + size, STI_LOWMEM);
  164. if (!new_font)
  165. return -ENOMEM;
  166. new_font->first_char = 0;
  167. new_font->last_char = op->charcount - 1;
  168. new_font->width = w;
  169. new_font->height = h;
  170. new_font->font_type = STI_FONT_HPROMAN8;
  171. new_font->bytes_per_char = bpc;
  172. new_font->underline_height = 0;
  173. new_font->underline_pos = 0;
  174. cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
  175. if (!cooked_font) {
  176. kfree(new_font);
  177. return -ENOMEM;
  178. }
  179. cooked_font->raw = new_font;
  180. cooked_font->raw_ptr = new_font;
  181. cooked_font->width = w;
  182. cooked_font->height = h;
  183. FNTREFCOUNT(cooked_font) = 0; /* usage counter */
  184. p = (unsigned char *) new_font;
  185. p += sizeof(*new_font);
  186. for (i = 0; i < op->charcount; i++) {
  187. memcpy(p, data, bpc);
  188. data += pitch*32;
  189. p += bpc;
  190. }
  191. FNTCRC(cooked_font) = crc32(0, new_font, size + sizeof(*new_font));
  192. sti_font_convert_bytemode(sti, cooked_font);
  193. new_font = cooked_font->raw_ptr;
  194. /* check if font is already used by other console */
  195. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  196. if (font_data[i] != STI_DEF_FONT
  197. && (FNTCRC(font_data[i]) == FNTCRC(cooked_font))) {
  198. kfree(new_font);
  199. kfree(cooked_font);
  200. /* current font is the same as the new one */
  201. if (i == unit)
  202. return 0;
  203. cooked_font = font_data[i];
  204. new_font = cooked_font->raw_ptr;
  205. break;
  206. }
  207. }
  208. /* clear screen with old font: we now may have less rows */
  209. vc_old_rows = vc->vc_rows;
  210. vc_old_cols = vc->vc_cols;
  211. sti_clear(sticon_sti, 0, 0, vc_old_rows, vc_old_cols,
  212. vc->vc_video_erase_char, font_data[vc->vc_num]);
  213. /* delete old font in case it is a user font */
  214. sticon_set_def_font(unit, NULL);
  215. FNTREFCOUNT(cooked_font)++;
  216. font_data[unit] = cooked_font;
  217. vc_cols = sti_onscreen_x(sti) / cooked_font->width;
  218. vc_rows = sti_onscreen_y(sti) / cooked_font->height;
  219. vc_resize(vc, vc_cols, vc_rows);
  220. /* need to repaint screen if cols & rows are same as old font */
  221. if (vc_cols == vc_old_cols && vc_rows == vc_old_rows)
  222. update_screen(vc);
  223. return 0;
  224. }
  225. static int sticon_font_default(struct vc_data *vc, struct console_font *op, char *name)
  226. {
  227. return sticon_set_def_font(vc->vc_num, op);
  228. }
  229. static int sticon_font_set(struct vc_data *vc, struct console_font *font,
  230. unsigned int flags)
  231. {
  232. return sticon_set_font(vc, font);
  233. }
  234. static void sticon_init(struct vc_data *c, int init)
  235. {
  236. struct sti_struct *sti = sticon_sti;
  237. int vc_cols, vc_rows;
  238. sti_set(sti, 0, 0, sti_onscreen_y(sti), sti_onscreen_x(sti), 0);
  239. vc_cols = sti_onscreen_x(sti) / sti->font->width;
  240. vc_rows = sti_onscreen_y(sti) / sti->font->height;
  241. c->vc_can_do_color = 1;
  242. if (init) {
  243. c->vc_cols = vc_cols;
  244. c->vc_rows = vc_rows;
  245. } else {
  246. vc_resize(c, vc_cols, vc_rows);
  247. }
  248. }
  249. static void sticon_deinit(struct vc_data *c)
  250. {
  251. int i;
  252. /* free memory used by user font */
  253. for (i = 0; i < MAX_NR_CONSOLES; i++)
  254. sticon_set_def_font(i, NULL);
  255. }
  256. static void sticon_clear(struct vc_data *conp, int sy, int sx, int height,
  257. int width)
  258. {
  259. if (!height || !width)
  260. return;
  261. sti_clear(sticon_sti, sy, sx, height, width,
  262. conp->vc_video_erase_char, font_data[conp->vc_num]);
  263. }
  264. static int sticon_switch(struct vc_data *conp)
  265. {
  266. return 1; /* needs refreshing */
  267. }
  268. static int sticon_blank(struct vc_data *c, int blank, int mode_switch)
  269. {
  270. if (blank == 0) {
  271. if (mode_switch)
  272. vga_is_gfx = 0;
  273. return 1;
  274. }
  275. sti_clear(sticon_sti, 0, 0, c->vc_rows, c->vc_cols, BLANK,
  276. font_data[c->vc_num]);
  277. if (mode_switch)
  278. vga_is_gfx = 1;
  279. return 1;
  280. }
  281. static u8 sticon_build_attr(struct vc_data *conp, u8 color,
  282. enum vc_intensity intens,
  283. bool blink, bool underline, bool reverse,
  284. bool italic)
  285. {
  286. u8 fg = color & 7;
  287. u8 bg = (color & 0x70) >> 4;
  288. if (reverse)
  289. return (fg << 3) | bg;
  290. else
  291. return (bg << 3) | fg;
  292. }
  293. static void sticon_invert_region(struct vc_data *conp, u16 *p, int count)
  294. {
  295. int col = 1; /* vga_can_do_color; */
  296. while (count--) {
  297. u16 a = scr_readw(p);
  298. if (col)
  299. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
  300. else
  301. a = ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
  302. scr_writew(a, p++);
  303. }
  304. }
  305. static const struct consw sti_con = {
  306. .owner = THIS_MODULE,
  307. .con_startup = sticon_startup,
  308. .con_init = sticon_init,
  309. .con_deinit = sticon_deinit,
  310. .con_clear = sticon_clear,
  311. .con_putc = sticon_putc,
  312. .con_putcs = sticon_putcs,
  313. .con_cursor = sticon_cursor,
  314. .con_scroll = sticon_scroll,
  315. .con_switch = sticon_switch,
  316. .con_blank = sticon_blank,
  317. .con_font_set = sticon_font_set,
  318. .con_font_default = sticon_font_default,
  319. .con_build_attr = sticon_build_attr,
  320. .con_invert_region = sticon_invert_region,
  321. };
  322. static int __init sticonsole_init(void)
  323. {
  324. int err, i;
  325. /* already initialized ? */
  326. if (sticon_sti)
  327. return 0;
  328. sticon_sti = sti_get_rom(0);
  329. if (!sticon_sti)
  330. return -ENODEV;
  331. for (i = 0; i < MAX_NR_CONSOLES; i++)
  332. font_data[i] = STI_DEF_FONT;
  333. pr_info("sticon: Initializing STI text console on %s at [%s]\n",
  334. sticon_sti->sti_data->inq_outptr.dev_name,
  335. sticon_sti->pa_path);
  336. console_lock();
  337. err = do_take_over_console(&sti_con, 0, MAX_NR_CONSOLES - 1,
  338. PAGE0->mem_cons.cl_class != CL_DUPLEX);
  339. console_unlock();
  340. return err;
  341. }
  342. module_init(sticonsole_init);
  343. MODULE_LICENSE("GPL");