fbcon_ud.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * linux/drivers/video/console/fbcon_ud.c -- Software Rotation - 180 degrees
  3. *
  4. * Copyright (C) 2005 Antonino Daplas <adaplas @pol.net>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. */
  10. #include <linux/config.h>
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <linux/fb.h>
  14. #include <linux/vt_kern.h>
  15. #include <linux/console.h>
  16. #include <asm/types.h>
  17. #include "fbcon.h"
  18. #include "fbcon_rotate.h"
  19. /*
  20. * Rotation 180 degrees
  21. */
  22. static inline void ud_update_attr(u8 *dst, u8 *src, int attribute,
  23. struct vc_data *vc)
  24. {
  25. int i, offset = (vc->vc_font.height < 10) ? 1 : 2;
  26. int width = (vc->vc_font.width + 7) >> 3;
  27. unsigned int cellsize = vc->vc_font.height * width;
  28. u8 c;
  29. offset = offset * width;
  30. for (i = 0; i < cellsize; i++) {
  31. c = src[i];
  32. if (attribute & FBCON_ATTRIBUTE_UNDERLINE && i < offset)
  33. c = 0xff;
  34. if (attribute & FBCON_ATTRIBUTE_BOLD)
  35. c |= c << 1;
  36. if (attribute & FBCON_ATTRIBUTE_REVERSE)
  37. c = ~c;
  38. dst[i] = c;
  39. }
  40. }
  41. static void ud_bmove(struct vc_data *vc, struct fb_info *info, int sy,
  42. int sx, int dy, int dx, int height, int width)
  43. {
  44. struct fbcon_ops *ops = info->fbcon_par;
  45. struct fb_copyarea area;
  46. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  47. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  48. area.sy = vyres - ((sy + height) * vc->vc_font.height);
  49. area.sx = vxres - ((sx + width) * vc->vc_font.width);
  50. area.dy = vyres - ((dy + height) * vc->vc_font.height);
  51. area.dx = vxres - ((dx + width) * vc->vc_font.width);
  52. area.height = height * vc->vc_font.height;
  53. area.width = width * vc->vc_font.width;
  54. info->fbops->fb_copyarea(info, &area);
  55. }
  56. static void ud_clear(struct vc_data *vc, struct fb_info *info, int sy,
  57. int sx, int height, int width)
  58. {
  59. struct fbcon_ops *ops = info->fbcon_par;
  60. struct fb_fillrect region;
  61. int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
  62. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  63. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  64. region.color = attr_bgcol_ec(bgshift,vc);
  65. region.dy = vyres - ((sy + height) * vc->vc_font.height);
  66. region.dx = vxres - ((sx + width) * vc->vc_font.width);
  67. region.width = width * vc->vc_font.width;
  68. region.height = height * vc->vc_font.height;
  69. region.rop = ROP_COPY;
  70. info->fbops->fb_fillrect(info, &region);
  71. }
  72. static inline void ud_putcs_aligned(struct vc_data *vc, struct fb_info *info,
  73. const u16 *s, u32 attr, u32 cnt,
  74. u32 d_pitch, u32 s_pitch, u32 cellsize,
  75. struct fb_image *image, u8 *buf, u8 *dst)
  76. {
  77. struct fbcon_ops *ops = info->fbcon_par;
  78. u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  79. u32 idx = vc->vc_font.width >> 3;
  80. u8 *src;
  81. while (cnt--) {
  82. src = ops->fontbuffer + (scr_readw(s--) & charmask)*cellsize;
  83. if (attr) {
  84. ud_update_attr(buf, src, attr, vc);
  85. src = buf;
  86. }
  87. if (likely(idx == 1))
  88. __fb_pad_aligned_buffer(dst, d_pitch, src, idx,
  89. image->height);
  90. else
  91. fb_pad_aligned_buffer(dst, d_pitch, src, idx,
  92. image->height);
  93. dst += s_pitch;
  94. }
  95. info->fbops->fb_imageblit(info, image);
  96. }
  97. static inline void ud_putcs_unaligned(struct vc_data *vc,
  98. struct fb_info *info, const u16 *s,
  99. u32 attr, u32 cnt, u32 d_pitch,
  100. u32 s_pitch, u32 cellsize,
  101. struct fb_image *image, u8 *buf,
  102. u8 *dst)
  103. {
  104. struct fbcon_ops *ops = info->fbcon_par;
  105. u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  106. u32 shift_low = 0, mod = vc->vc_font.width % 8;
  107. u32 shift_high = 8;
  108. u32 idx = vc->vc_font.width >> 3;
  109. u8 *src;
  110. while (cnt--) {
  111. src = ops->fontbuffer + (scr_readw(s--) & charmask)*cellsize;
  112. if (attr) {
  113. ud_update_attr(buf, src, attr, vc);
  114. src = buf;
  115. }
  116. fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
  117. image->height, shift_high,
  118. shift_low, mod);
  119. shift_low += mod;
  120. dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
  121. shift_low &= 7;
  122. shift_high = 8 - shift_low;
  123. }
  124. info->fbops->fb_imageblit(info, image);
  125. }
  126. static void ud_putcs(struct vc_data *vc, struct fb_info *info,
  127. const unsigned short *s, int count, int yy, int xx,
  128. int fg, int bg)
  129. {
  130. struct fb_image image;
  131. struct fbcon_ops *ops = info->fbcon_par;
  132. u32 width = (vc->vc_font.width + 7)/8;
  133. u32 cellsize = width * vc->vc_font.height;
  134. u32 maxcnt = info->pixmap.size/cellsize;
  135. u32 scan_align = info->pixmap.scan_align - 1;
  136. u32 buf_align = info->pixmap.buf_align - 1;
  137. u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
  138. u32 attribute = get_attribute(info, scr_readw(s));
  139. u8 *dst, *buf = NULL;
  140. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  141. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  142. if (!ops->fontbuffer)
  143. return;
  144. image.fg_color = fg;
  145. image.bg_color = bg;
  146. image.dy = vyres - ((yy * vc->vc_font.height) + vc->vc_font.height);
  147. image.dx = vxres - ((xx + count) * vc->vc_font.width);
  148. image.height = vc->vc_font.height;
  149. image.depth = 1;
  150. if (attribute) {
  151. buf = kmalloc(cellsize, GFP_KERNEL);
  152. if (!buf)
  153. return;
  154. }
  155. s += count - 1;
  156. while (count) {
  157. if (count > maxcnt)
  158. cnt = maxcnt;
  159. else
  160. cnt = count;
  161. image.width = vc->vc_font.width * cnt;
  162. pitch = ((image.width + 7) >> 3) + scan_align;
  163. pitch &= ~scan_align;
  164. size = pitch * image.height + buf_align;
  165. size &= ~buf_align;
  166. dst = fb_get_buffer_offset(info, &info->pixmap, size);
  167. image.data = dst;
  168. if (!mod)
  169. ud_putcs_aligned(vc, info, s, attribute, cnt, pitch,
  170. width, cellsize, &image, buf, dst);
  171. else
  172. ud_putcs_unaligned(vc, info, s, attribute, cnt, pitch,
  173. width, cellsize, &image,
  174. buf, dst);
  175. image.dx += image.width;
  176. count -= cnt;
  177. s -= cnt;
  178. xx += cnt;
  179. }
  180. /* buf is always NULL except when in monochrome mode, so in this case
  181. it's a gain to check buf against NULL even though kfree() handles
  182. NULL pointers just fine */
  183. if (unlikely(buf))
  184. kfree(buf);
  185. }
  186. static void ud_clear_margins(struct vc_data *vc, struct fb_info *info,
  187. int bottom_only)
  188. {
  189. unsigned int cw = vc->vc_font.width;
  190. unsigned int ch = vc->vc_font.height;
  191. unsigned int rw = info->var.xres - (vc->vc_cols*cw);
  192. unsigned int bh = info->var.yres - (vc->vc_rows*ch);
  193. struct fb_fillrect region;
  194. int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
  195. region.color = attr_bgcol_ec(bgshift,vc);
  196. region.rop = ROP_COPY;
  197. if (rw && !bottom_only) {
  198. region.dy = 0;
  199. region.dx = info->var.xoffset;
  200. region.width = rw;
  201. region.height = info->var.yres_virtual;
  202. info->fbops->fb_fillrect(info, &region);
  203. }
  204. if (bh) {
  205. region.dy = info->var.yoffset;
  206. region.dx = info->var.xoffset;
  207. region.height = bh;
  208. region.width = info->var.xres;
  209. info->fbops->fb_fillrect(info, &region);
  210. }
  211. }
  212. static void ud_cursor(struct vc_data *vc, struct fb_info *info,
  213. struct display *p, int mode, int softback_lines,
  214. int fg, int bg)
  215. {
  216. struct fb_cursor cursor;
  217. struct fbcon_ops *ops = (struct fbcon_ops *) info->fbcon_par;
  218. unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  219. int w = (vc->vc_font.width + 7) >> 3, c;
  220. int y = real_y(p, vc->vc_y);
  221. int attribute, use_sw = (vc->vc_cursor_type & 0x10);
  222. int err = 1, dx, dy;
  223. char *src;
  224. u32 vyres = GETVYRES(p->scrollmode, info);
  225. u32 vxres = GETVXRES(p->scrollmode, info);
  226. if (!ops->fontbuffer)
  227. return;
  228. cursor.set = 0;
  229. if (softback_lines) {
  230. if (y + softback_lines >= vc->vc_rows) {
  231. mode = CM_ERASE;
  232. ops->cursor_flash = 0;
  233. return;
  234. } else
  235. y += softback_lines;
  236. }
  237. c = scr_readw((u16 *) vc->vc_pos);
  238. attribute = get_attribute(info, c);
  239. src = ops->fontbuffer + ((c & charmask) * (w * vc->vc_font.height));
  240. if (ops->cursor_state.image.data != src ||
  241. ops->cursor_reset) {
  242. ops->cursor_state.image.data = src;
  243. cursor.set |= FB_CUR_SETIMAGE;
  244. }
  245. if (attribute) {
  246. u8 *dst;
  247. dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
  248. if (!dst)
  249. return;
  250. kfree(ops->cursor_data);
  251. ops->cursor_data = dst;
  252. ud_update_attr(dst, src, attribute, vc);
  253. src = dst;
  254. }
  255. if (ops->cursor_state.image.fg_color != fg ||
  256. ops->cursor_state.image.bg_color != bg ||
  257. ops->cursor_reset) {
  258. ops->cursor_state.image.fg_color = fg;
  259. ops->cursor_state.image.bg_color = bg;
  260. cursor.set |= FB_CUR_SETCMAP;
  261. }
  262. if (ops->cursor_state.image.height != vc->vc_font.height ||
  263. ops->cursor_state.image.width != vc->vc_font.width ||
  264. ops->cursor_reset) {
  265. ops->cursor_state.image.height = vc->vc_font.height;
  266. ops->cursor_state.image.width = vc->vc_font.width;
  267. cursor.set |= FB_CUR_SETSIZE;
  268. }
  269. dy = vyres - ((y * vc->vc_font.height) + vc->vc_font.height);
  270. dx = vxres - ((vc->vc_x * vc->vc_font.width) + vc->vc_font.width);
  271. if (ops->cursor_state.image.dx != dx ||
  272. ops->cursor_state.image.dy != dy ||
  273. ops->cursor_reset) {
  274. ops->cursor_state.image.dx = dx;
  275. ops->cursor_state.image.dy = dy;
  276. cursor.set |= FB_CUR_SETPOS;
  277. }
  278. if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
  279. ops->cursor_reset) {
  280. ops->cursor_state.hot.x = cursor.hot.y = 0;
  281. cursor.set |= FB_CUR_SETHOT;
  282. }
  283. if (cursor.set & FB_CUR_SETSIZE ||
  284. vc->vc_cursor_type != p->cursor_shape ||
  285. ops->cursor_state.mask == NULL ||
  286. ops->cursor_reset) {
  287. char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
  288. int cur_height, size, i = 0;
  289. u8 msk = 0xff;
  290. if (!mask)
  291. return;
  292. kfree(ops->cursor_state.mask);
  293. ops->cursor_state.mask = mask;
  294. p->cursor_shape = vc->vc_cursor_type;
  295. cursor.set |= FB_CUR_SETSHAPE;
  296. switch (p->cursor_shape & CUR_HWMASK) {
  297. case CUR_NONE:
  298. cur_height = 0;
  299. break;
  300. case CUR_UNDERLINE:
  301. cur_height = (vc->vc_font.height < 10) ? 1 : 2;
  302. break;
  303. case CUR_LOWER_THIRD:
  304. cur_height = vc->vc_font.height/3;
  305. break;
  306. case CUR_LOWER_HALF:
  307. cur_height = vc->vc_font.height >> 1;
  308. break;
  309. case CUR_TWO_THIRDS:
  310. cur_height = (vc->vc_font.height << 1)/3;
  311. break;
  312. case CUR_BLOCK:
  313. default:
  314. cur_height = vc->vc_font.height;
  315. break;
  316. }
  317. size = cur_height * w;
  318. while (size--)
  319. mask[i++] = msk;
  320. size = (vc->vc_font.height - cur_height) * w;
  321. while (size--)
  322. mask[i++] = ~msk;
  323. }
  324. switch (mode) {
  325. case CM_ERASE:
  326. ops->cursor_state.enable = 0;
  327. break;
  328. case CM_DRAW:
  329. case CM_MOVE:
  330. default:
  331. ops->cursor_state.enable = (use_sw) ? 0 : 1;
  332. break;
  333. }
  334. cursor.image.data = src;
  335. cursor.image.fg_color = ops->cursor_state.image.fg_color;
  336. cursor.image.bg_color = ops->cursor_state.image.bg_color;
  337. cursor.image.dx = ops->cursor_state.image.dx;
  338. cursor.image.dy = ops->cursor_state.image.dy;
  339. cursor.image.height = ops->cursor_state.image.height;
  340. cursor.image.width = ops->cursor_state.image.width;
  341. cursor.hot.x = ops->cursor_state.hot.x;
  342. cursor.hot.y = ops->cursor_state.hot.y;
  343. cursor.mask = ops->cursor_state.mask;
  344. cursor.enable = ops->cursor_state.enable;
  345. cursor.image.depth = 1;
  346. cursor.rop = ROP_XOR;
  347. if (info->fbops->fb_cursor)
  348. err = info->fbops->fb_cursor(info, &cursor);
  349. if (err)
  350. soft_cursor(info, &cursor);
  351. ops->cursor_reset = 0;
  352. }
  353. int ud_update_start(struct fb_info *info)
  354. {
  355. struct fbcon_ops *ops = info->fbcon_par;
  356. int xoffset, yoffset;
  357. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  358. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  359. int err;
  360. xoffset = vxres - info->var.xres - ops->var.xoffset;
  361. yoffset = vyres - info->var.yres - ops->var.yoffset;
  362. if (yoffset < 0)
  363. yoffset += vyres;
  364. ops->var.xoffset = xoffset;
  365. ops->var.yoffset = yoffset;
  366. err = fb_pan_display(info, &ops->var);
  367. ops->var.xoffset = info->var.xoffset;
  368. ops->var.yoffset = info->var.yoffset;
  369. ops->var.vmode = info->var.vmode;
  370. return err;
  371. }
  372. void fbcon_rotate_ud(struct fbcon_ops *ops)
  373. {
  374. ops->bmove = ud_bmove;
  375. ops->clear = ud_clear;
  376. ops->putcs = ud_putcs;
  377. ops->clear_margins = ud_clear_margins;
  378. ops->cursor = ud_cursor;
  379. ops->update_start = ud_update_start;
  380. }
  381. EXPORT_SYMBOL(fbcon_rotate_ud);
  382. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  383. MODULE_DESCRIPTION("Console Rotation (180 degrees) Support");
  384. MODULE_LICENSE("GPL");