fbcon_ud.c 12 KB

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