fbcon_ud.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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, int mode,
  213. int softback_lines, int fg, int bg)
  214. {
  215. struct fb_cursor cursor;
  216. struct fbcon_ops *ops = info->fbcon_par;
  217. unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  218. int w = (vc->vc_font.width + 7) >> 3, c;
  219. int y = real_y(ops->p, vc->vc_y);
  220. int attribute, use_sw = (vc->vc_cursor_type & 0x10);
  221. int err = 1, dx, dy;
  222. char *src;
  223. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  224. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  225. if (!ops->fontbuffer)
  226. return;
  227. cursor.set = 0;
  228. if (softback_lines) {
  229. if (y + softback_lines >= vc->vc_rows) {
  230. mode = CM_ERASE;
  231. ops->cursor_flash = 0;
  232. return;
  233. } else
  234. y += softback_lines;
  235. }
  236. c = scr_readw((u16 *) vc->vc_pos);
  237. attribute = get_attribute(info, c);
  238. src = ops->fontbuffer + ((c & charmask) * (w * vc->vc_font.height));
  239. if (ops->cursor_state.image.data != src ||
  240. ops->cursor_reset) {
  241. ops->cursor_state.image.data = src;
  242. cursor.set |= FB_CUR_SETIMAGE;
  243. }
  244. if (attribute) {
  245. u8 *dst;
  246. dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
  247. if (!dst)
  248. return;
  249. kfree(ops->cursor_data);
  250. ops->cursor_data = dst;
  251. ud_update_attr(dst, src, attribute, vc);
  252. src = dst;
  253. }
  254. if (ops->cursor_state.image.fg_color != fg ||
  255. ops->cursor_state.image.bg_color != bg ||
  256. ops->cursor_reset) {
  257. ops->cursor_state.image.fg_color = fg;
  258. ops->cursor_state.image.bg_color = bg;
  259. cursor.set |= FB_CUR_SETCMAP;
  260. }
  261. if (ops->cursor_state.image.height != vc->vc_font.height ||
  262. ops->cursor_state.image.width != vc->vc_font.width ||
  263. ops->cursor_reset) {
  264. ops->cursor_state.image.height = vc->vc_font.height;
  265. ops->cursor_state.image.width = vc->vc_font.width;
  266. cursor.set |= FB_CUR_SETSIZE;
  267. }
  268. dy = vyres - ((y * vc->vc_font.height) + vc->vc_font.height);
  269. dx = vxres - ((vc->vc_x * vc->vc_font.width) + vc->vc_font.width);
  270. if (ops->cursor_state.image.dx != dx ||
  271. ops->cursor_state.image.dy != dy ||
  272. ops->cursor_reset) {
  273. ops->cursor_state.image.dx = dx;
  274. ops->cursor_state.image.dy = dy;
  275. cursor.set |= FB_CUR_SETPOS;
  276. }
  277. if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
  278. ops->cursor_reset) {
  279. ops->cursor_state.hot.x = cursor.hot.y = 0;
  280. cursor.set |= FB_CUR_SETHOT;
  281. }
  282. if (cursor.set & FB_CUR_SETSIZE ||
  283. vc->vc_cursor_type != ops->p->cursor_shape ||
  284. ops->cursor_state.mask == NULL ||
  285. ops->cursor_reset) {
  286. char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
  287. int cur_height, size, i = 0;
  288. u8 msk = 0xff;
  289. if (!mask)
  290. return;
  291. kfree(ops->cursor_state.mask);
  292. ops->cursor_state.mask = mask;
  293. ops->p->cursor_shape = vc->vc_cursor_type;
  294. cursor.set |= FB_CUR_SETSHAPE;
  295. switch (ops->p->cursor_shape & CUR_HWMASK) {
  296. case CUR_NONE:
  297. cur_height = 0;
  298. break;
  299. case CUR_UNDERLINE:
  300. cur_height = (vc->vc_font.height < 10) ? 1 : 2;
  301. break;
  302. case CUR_LOWER_THIRD:
  303. cur_height = vc->vc_font.height/3;
  304. break;
  305. case CUR_LOWER_HALF:
  306. cur_height = vc->vc_font.height >> 1;
  307. break;
  308. case CUR_TWO_THIRDS:
  309. cur_height = (vc->vc_font.height << 1)/3;
  310. break;
  311. case CUR_BLOCK:
  312. default:
  313. cur_height = vc->vc_font.height;
  314. break;
  315. }
  316. size = cur_height * w;
  317. while (size--)
  318. mask[i++] = msk;
  319. size = (vc->vc_font.height - cur_height) * w;
  320. while (size--)
  321. mask[i++] = ~msk;
  322. }
  323. switch (mode) {
  324. case CM_ERASE:
  325. ops->cursor_state.enable = 0;
  326. break;
  327. case CM_DRAW:
  328. case CM_MOVE:
  329. default:
  330. ops->cursor_state.enable = (use_sw) ? 0 : 1;
  331. break;
  332. }
  333. cursor.image.data = src;
  334. cursor.image.fg_color = ops->cursor_state.image.fg_color;
  335. cursor.image.bg_color = ops->cursor_state.image.bg_color;
  336. cursor.image.dx = ops->cursor_state.image.dx;
  337. cursor.image.dy = ops->cursor_state.image.dy;
  338. cursor.image.height = ops->cursor_state.image.height;
  339. cursor.image.width = ops->cursor_state.image.width;
  340. cursor.hot.x = ops->cursor_state.hot.x;
  341. cursor.hot.y = ops->cursor_state.hot.y;
  342. cursor.mask = ops->cursor_state.mask;
  343. cursor.enable = ops->cursor_state.enable;
  344. cursor.image.depth = 1;
  345. cursor.rop = ROP_XOR;
  346. if (info->fbops->fb_cursor)
  347. err = info->fbops->fb_cursor(info, &cursor);
  348. if (err)
  349. soft_cursor(info, &cursor);
  350. ops->cursor_reset = 0;
  351. }
  352. int ud_update_start(struct fb_info *info)
  353. {
  354. struct fbcon_ops *ops = info->fbcon_par;
  355. int xoffset, yoffset;
  356. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  357. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  358. int err;
  359. xoffset = vxres - info->var.xres - ops->var.xoffset;
  360. yoffset = vyres - info->var.yres - ops->var.yoffset;
  361. if (yoffset < 0)
  362. yoffset += vyres;
  363. ops->var.xoffset = xoffset;
  364. ops->var.yoffset = yoffset;
  365. err = fb_pan_display(info, &ops->var);
  366. ops->var.xoffset = info->var.xoffset;
  367. ops->var.yoffset = info->var.yoffset;
  368. ops->var.vmode = info->var.vmode;
  369. return err;
  370. }
  371. void fbcon_rotate_ud(struct fbcon_ops *ops)
  372. {
  373. ops->bmove = ud_bmove;
  374. ops->clear = ud_clear;
  375. ops->putcs = ud_putcs;
  376. ops->clear_margins = ud_clear_margins;
  377. ops->cursor = ud_cursor;
  378. ops->update_start = ud_update_start;
  379. }
  380. EXPORT_SYMBOL(fbcon_rotate_ud);
  381. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  382. MODULE_DESCRIPTION("Console Rotation (180 degrees) Support");
  383. MODULE_LICENSE("GPL");