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/string.h>
  12. #include <linux/fb.h>
  13. #include <linux/vt_kern.h>
  14. #include <linux/console.h>
  15. #include <asm/types.h>
  16. #include "fbcon.h"
  17. #include "fbcon_rotate.h"
  18. /*
  19. * Rotation 180 degrees
  20. */
  21. static inline void ud_update_attr(u8 *dst, u8 *src, int attribute,
  22. struct vc_data *vc)
  23. {
  24. int i, offset = (vc->vc_font.height < 10) ? 1 : 2;
  25. int width = (vc->vc_font.width + 7) >> 3;
  26. unsigned int cellsize = vc->vc_font.height * width;
  27. u8 c;
  28. offset = offset * width;
  29. for (i = 0; i < cellsize; i++) {
  30. c = src[i];
  31. if (attribute & FBCON_ATTRIBUTE_UNDERLINE && i < offset)
  32. c = 0xff;
  33. if (attribute & FBCON_ATTRIBUTE_BOLD)
  34. c |= c << 1;
  35. if (attribute & FBCON_ATTRIBUTE_REVERSE)
  36. c = ~c;
  37. dst[i] = c;
  38. }
  39. }
  40. static void ud_bmove(struct vc_data *vc, struct fb_info *info, int sy,
  41. int sx, int dy, int dx, int height, int width)
  42. {
  43. struct fbcon_ops *ops = info->fbcon_par;
  44. struct fb_copyarea area;
  45. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  46. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  47. area.sy = vyres - ((sy + height) * vc->vc_font.height);
  48. area.sx = vxres - ((sx + width) * vc->vc_font.width);
  49. area.dy = vyres - ((dy + height) * vc->vc_font.height);
  50. area.dx = vxres - ((dx + width) * vc->vc_font.width);
  51. area.height = height * vc->vc_font.height;
  52. area.width = width * vc->vc_font.width;
  53. info->fbops->fb_copyarea(info, &area);
  54. }
  55. static void ud_clear(struct vc_data *vc, struct fb_info *info, int sy,
  56. int sx, int height, int width)
  57. {
  58. struct fbcon_ops *ops = info->fbcon_par;
  59. struct fb_fillrect region;
  60. int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
  61. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  62. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  63. region.color = attr_bgcol_ec(bgshift,vc,info);
  64. region.dy = vyres - ((sy + height) * vc->vc_font.height);
  65. region.dx = vxres - ((sx + width) * vc->vc_font.width);
  66. region.width = width * vc->vc_font.width;
  67. region.height = height * vc->vc_font.height;
  68. region.rop = ROP_COPY;
  69. info->fbops->fb_fillrect(info, &region);
  70. }
  71. static inline void ud_putcs_aligned(struct vc_data *vc, struct fb_info *info,
  72. const u16 *s, u32 attr, u32 cnt,
  73. u32 d_pitch, u32 s_pitch, u32 cellsize,
  74. struct fb_image *image, u8 *buf, u8 *dst)
  75. {
  76. struct fbcon_ops *ops = info->fbcon_par;
  77. u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  78. u32 idx = vc->vc_font.width >> 3;
  79. u8 *src;
  80. while (cnt--) {
  81. src = ops->fontbuffer + (scr_readw(s--) & charmask)*cellsize;
  82. if (attr) {
  83. ud_update_attr(buf, src, attr, vc);
  84. src = buf;
  85. }
  86. if (likely(idx == 1))
  87. __fb_pad_aligned_buffer(dst, d_pitch, src, idx,
  88. image->height);
  89. else
  90. fb_pad_aligned_buffer(dst, d_pitch, src, idx,
  91. image->height);
  92. dst += s_pitch;
  93. }
  94. info->fbops->fb_imageblit(info, image);
  95. }
  96. static inline void ud_putcs_unaligned(struct vc_data *vc,
  97. struct fb_info *info, const u16 *s,
  98. u32 attr, u32 cnt, u32 d_pitch,
  99. u32 s_pitch, u32 cellsize,
  100. struct fb_image *image, u8 *buf,
  101. u8 *dst)
  102. {
  103. struct fbcon_ops *ops = info->fbcon_par;
  104. u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  105. u32 shift_low = 0, mod = vc->vc_font.width % 8;
  106. u32 shift_high = 8;
  107. u32 idx = vc->vc_font.width >> 3;
  108. u8 *src;
  109. while (cnt--) {
  110. src = ops->fontbuffer + (scr_readw(s--) & charmask)*cellsize;
  111. if (attr) {
  112. ud_update_attr(buf, src, attr, vc);
  113. src = buf;
  114. }
  115. fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
  116. image->height, shift_high,
  117. shift_low, mod);
  118. shift_low += mod;
  119. dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
  120. shift_low &= 7;
  121. shift_high = 8 - shift_low;
  122. }
  123. info->fbops->fb_imageblit(info, image);
  124. }
  125. static void ud_putcs(struct vc_data *vc, struct fb_info *info,
  126. const unsigned short *s, int count, int yy, int xx,
  127. int fg, int bg)
  128. {
  129. struct fb_image image;
  130. struct fbcon_ops *ops = info->fbcon_par;
  131. u32 width = (vc->vc_font.width + 7)/8;
  132. u32 cellsize = width * vc->vc_font.height;
  133. u32 maxcnt = info->pixmap.size/cellsize;
  134. u32 scan_align = info->pixmap.scan_align - 1;
  135. u32 buf_align = info->pixmap.buf_align - 1;
  136. u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
  137. u32 attribute = get_attribute(info, scr_readw(s));
  138. u8 *dst, *buf = NULL;
  139. u32 vyres = GETVYRES(ops->p->scrollmode, info);
  140. u32 vxres = GETVXRES(ops->p->scrollmode, info);
  141. if (!ops->fontbuffer)
  142. return;
  143. image.fg_color = fg;
  144. image.bg_color = bg;
  145. image.dy = vyres - ((yy * vc->vc_font.height) + vc->vc_font.height);
  146. image.dx = vxres - ((xx + count) * vc->vc_font.width);
  147. image.height = vc->vc_font.height;
  148. image.depth = 1;
  149. if (attribute) {
  150. buf = kmalloc(cellsize, GFP_KERNEL);
  151. if (!buf)
  152. return;
  153. }
  154. s += count - 1;
  155. while (count) {
  156. if (count > maxcnt)
  157. cnt = maxcnt;
  158. else
  159. cnt = count;
  160. image.width = vc->vc_font.width * cnt;
  161. pitch = ((image.width + 7) >> 3) + scan_align;
  162. pitch &= ~scan_align;
  163. size = pitch * image.height + buf_align;
  164. size &= ~buf_align;
  165. dst = fb_get_buffer_offset(info, &info->pixmap, size);
  166. image.data = dst;
  167. if (!mod)
  168. ud_putcs_aligned(vc, info, s, attribute, cnt, pitch,
  169. width, cellsize, &image, buf, dst);
  170. else
  171. ud_putcs_unaligned(vc, info, s, attribute, cnt, pitch,
  172. width, cellsize, &image,
  173. buf, dst);
  174. image.dx += image.width;
  175. count -= cnt;
  176. s -= cnt;
  177. xx += cnt;
  178. }
  179. /* buf is always NULL except when in monochrome mode, so in this case
  180. it's a gain to check buf against NULL even though kfree() handles
  181. NULL pointers just fine */
  182. if (unlikely(buf))
  183. kfree(buf);
  184. }
  185. static void ud_clear_margins(struct vc_data *vc, struct fb_info *info,
  186. int bottom_only)
  187. {
  188. unsigned int cw = vc->vc_font.width;
  189. unsigned int ch = vc->vc_font.height;
  190. unsigned int rw = info->var.xres - (vc->vc_cols*cw);
  191. unsigned int bh = info->var.yres - (vc->vc_rows*ch);
  192. struct fb_fillrect region;
  193. int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
  194. region.color = attr_bgcol_ec(bgshift,vc,info);
  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");