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