Merge branch 'drm-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6] / drivers / video / console / fbcon_ccw.c
1 /*
2  *  linux/drivers/video/console/fbcon_ccw.c -- Software Rotation - 270 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
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 /*
21  * Rotation 270 degrees
22  */
23
24 static inline void ccw_update_attr(u8 *dst, u8 *src, int attribute,
25                                   struct vc_data *vc)
26 {
27         int i, j, offset = (vc->vc_font.height < 10) ? 1 : 2;
28         int width = (vc->vc_font.height + 7) >> 3;
29         int mod = vc->vc_font.height % 8;
30         u8 c, msk = ~(0xff << offset), msk1 = 0;
31
32         if (mod)
33                 msk <<= (8 - mod);
34
35         if (offset > mod)
36                 msk1 |= 0x01;
37
38         for (i = 0; i < vc->vc_font.width; i++) {
39                 for (j = 0; j < width; j++) {
40                         c = *src;
41
42                         if (attribute & FBCON_ATTRIBUTE_UNDERLINE) {
43                                 if (j == width - 1)
44                                         c |= msk;
45
46                                 if (msk1 && j == width - 2)
47                                         c |= msk1;
48                         }
49
50                         if (attribute & FBCON_ATTRIBUTE_BOLD && i)
51                                 *(dst - width) |= c;
52
53                         if (attribute & FBCON_ATTRIBUTE_REVERSE)
54                                 c = ~c;
55                         src++;
56                         *dst++ = c;
57                 }
58         }
59 }
60
61
62 static void ccw_bmove(struct vc_data *vc, struct fb_info *info, int sy,
63                      int sx, int dy, int dx, int height, int width)
64 {
65         struct fbcon_ops *ops = info->fbcon_par;
66         struct fb_copyarea area;
67         u32 vyres = GETVYRES(ops->p->scrollmode, info);
68
69         area.sx = sy * vc->vc_font.height;
70         area.sy = vyres - ((sx + width) * vc->vc_font.width);
71         area.dx = dy * vc->vc_font.height;
72         area.dy = vyres - ((dx + width) * vc->vc_font.width);
73         area.width = height * vc->vc_font.height;
74         area.height  = width * vc->vc_font.width;
75
76         info->fbops->fb_copyarea(info, &area);
77 }
78
79 static void ccw_clear(struct vc_data *vc, struct fb_info *info, int sy,
80                      int sx, int height, int width)
81 {
82         struct fbcon_ops *ops = info->fbcon_par;
83         struct fb_fillrect region;
84         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
85         u32 vyres = GETVYRES(ops->p->scrollmode, info);
86
87         region.color = attr_bgcol_ec(bgshift,vc);
88         region.dx = sy * vc->vc_font.height;
89         region.dy = vyres - ((sx + width) * vc->vc_font.width);
90         region.height = width * vc->vc_font.width;
91         region.width = height * vc->vc_font.height;
92         region.rop = ROP_COPY;
93
94         info->fbops->fb_fillrect(info, &region);
95 }
96
97 static inline void ccw_putcs_aligned(struct vc_data *vc, struct fb_info *info,
98                                     const u16 *s, u32 attr, u32 cnt,
99                                     u32 d_pitch, u32 s_pitch, u32 cellsize,
100                                     struct fb_image *image, u8 *buf, u8 *dst)
101 {
102         struct fbcon_ops *ops = info->fbcon_par;
103         u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
104         u32 idx = (vc->vc_font.height + 7) >> 3;
105         u8 *src;
106
107         while (cnt--) {
108                 src = ops->fontbuffer + (scr_readw(s--) & charmask)*cellsize;
109
110                 if (attr) {
111                         ccw_update_attr(buf, src, attr, vc);
112                         src = buf;
113                 }
114
115                 if (likely(idx == 1))
116                         __fb_pad_aligned_buffer(dst, d_pitch, src, idx,
117                                                 vc->vc_font.width);
118                 else
119                         fb_pad_aligned_buffer(dst, d_pitch, src, idx,
120                                               vc->vc_font.width);
121
122                 dst += d_pitch * vc->vc_font.width;
123         }
124
125         info->fbops->fb_imageblit(info, image);
126 }
127
128 static void ccw_putcs(struct vc_data *vc, struct fb_info *info,
129                       const unsigned short *s, int count, int yy, int xx,
130                       int fg, int bg)
131 {
132         struct fb_image image;
133         struct fbcon_ops *ops = info->fbcon_par;
134         u32 width = (vc->vc_font.height + 7)/8;
135         u32 cellsize = width * vc->vc_font.width;
136         u32 maxcnt = info->pixmap.size/cellsize;
137         u32 scan_align = info->pixmap.scan_align - 1;
138         u32 buf_align = info->pixmap.buf_align - 1;
139         u32 cnt, pitch, size;
140         u32 attribute = get_attribute(info, scr_readw(s));
141         u8 *dst, *buf = NULL;
142         u32 vyres = GETVYRES(ops->p->scrollmode, info);
143
144         if (!ops->fontbuffer)
145                 return;
146
147         image.fg_color = fg;
148         image.bg_color = bg;
149         image.dx = yy * vc->vc_font.height;
150         image.dy = vyres - ((xx + count) * vc->vc_font.width);
151         image.width = vc->vc_font.height;
152         image.depth = 1;
153
154         if (attribute) {
155                 buf = kmalloc(cellsize, GFP_KERNEL);
156                 if (!buf)
157                         return;
158         }
159
160         s += count - 1;
161
162         while (count) {
163                 if (count > maxcnt)
164                         cnt = maxcnt;
165                 else
166                         cnt = count;
167
168                 image.height = vc->vc_font.width * cnt;
169                 pitch = ((image.width + 7) >> 3) + scan_align;
170                 pitch &= ~scan_align;
171                 size = pitch * image.height + buf_align;
172                 size &= ~buf_align;
173                 dst = fb_get_buffer_offset(info, &info->pixmap, size);
174                 image.data = dst;
175                 ccw_putcs_aligned(vc, info, s, attribute, cnt, pitch,
176                                  width, cellsize, &image, buf, dst);
177                 image.dy += image.height;
178                 count -= cnt;
179                 s -= cnt;
180         }
181
182         /* buf is always NULL except when in monochrome mode, so in this case
183            it's a gain to check buf against NULL even though kfree() handles
184            NULL pointers just fine */
185         if (unlikely(buf))
186                 kfree(buf);
187
188 }
189
190 static void ccw_clear_margins(struct vc_data *vc, struct fb_info *info,
191                              int bottom_only)
192 {
193         unsigned int cw = vc->vc_font.width;
194         unsigned int ch = vc->vc_font.height;
195         unsigned int rw = info->var.yres - (vc->vc_cols*cw);
196         unsigned int bh = info->var.xres - (vc->vc_rows*ch);
197         unsigned int bs = vc->vc_rows*ch;
198         struct fb_fillrect region;
199         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
200
201         region.color = attr_bgcol_ec(bgshift,vc);
202         region.rop = ROP_COPY;
203
204         if (rw && !bottom_only) {
205                 region.dx = 0;
206                 region.dy = info->var.yoffset;
207                 region.height = rw;
208                 region.width = info->var.xres_virtual;
209                 info->fbops->fb_fillrect(info, &region);
210         }
211
212         if (bh) {
213                 region.dx = info->var.xoffset + bs;
214                 region.dy = 0;
215                 region.height = info->var.yres_virtual;
216                 region.width = bh;
217                 info->fbops->fb_fillrect(info, &region);
218         }
219 }
220
221 static void ccw_cursor(struct vc_data *vc, struct fb_info *info, int mode,
222                        int softback_lines, int fg, int bg)
223 {
224         struct fb_cursor cursor;
225         struct fbcon_ops *ops = info->fbcon_par;
226         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
227         int w = (vc->vc_font.height + 7) >> 3, c;
228         int y = real_y(ops->p, vc->vc_y);
229         int attribute, use_sw = (vc->vc_cursor_type & 0x10);
230         int err = 1, dx, dy;
231         char *src;
232         u32 vyres = GETVYRES(ops->p->scrollmode, info);
233
234         if (!ops->fontbuffer)
235                 return;
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 = ops->fontbuffer + ((c & charmask) * (w * vc->vc_font.width));
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.width, GFP_ATOMIC);
262                 if (!dst)
263                         return;
264                 kfree(ops->cursor_data);
265                 ops->cursor_data = dst;
266                 ccw_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.height != vc->vc_font.width ||
279             ops->cursor_state.image.width != vc->vc_font.height ||
280             ops->cursor_reset) {
281                 ops->cursor_state.image.height = vc->vc_font.width;
282                 ops->cursor_state.image.width = vc->vc_font.height;
283                 cursor.set |= FB_CUR_SETSIZE;
284         }
285
286         dx = y * vc->vc_font.height;
287         dy = vyres - ((vc->vc_x + 1) * vc->vc_font.width);
288
289         if (ops->cursor_state.image.dx != dx ||
290             ops->cursor_state.image.dy != dy ||
291             ops->cursor_reset) {
292                 ops->cursor_state.image.dx = dx;
293                 ops->cursor_state.image.dy = dy;
294                 cursor.set |= FB_CUR_SETPOS;
295         }
296
297         if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
298             ops->cursor_reset) {
299                 ops->cursor_state.hot.x = cursor.hot.y = 0;
300                 cursor.set |= FB_CUR_SETHOT;
301         }
302
303         if (cursor.set & FB_CUR_SETSIZE ||
304             vc->vc_cursor_type != ops->p->cursor_shape ||
305             ops->cursor_state.mask == NULL ||
306             ops->cursor_reset) {
307                 char *tmp, *mask = kmalloc(w*vc->vc_font.width, GFP_ATOMIC);
308                 int cur_height, size, i = 0;
309                 int width = (vc->vc_font.width + 7)/8;
310
311                 if (!mask)
312                         return;
313
314                 tmp = kmalloc(width * vc->vc_font.height, GFP_ATOMIC);
315
316                 if (!tmp) {
317                         kfree(mask);
318                         return;
319                 }
320
321                 kfree(ops->cursor_state.mask);
322                 ops->cursor_state.mask = mask;
323
324                 ops->p->cursor_shape = vc->vc_cursor_type;
325                 cursor.set |= FB_CUR_SETSHAPE;
326
327                 switch (ops->p->cursor_shape & CUR_HWMASK) {
328                 case CUR_NONE:
329                         cur_height = 0;
330                         break;
331                 case CUR_UNDERLINE:
332                         cur_height = (vc->vc_font.height < 10) ? 1 : 2;
333                         break;
334                 case CUR_LOWER_THIRD:
335                         cur_height = vc->vc_font.height/3;
336                         break;
337                 case CUR_LOWER_HALF:
338                         cur_height = vc->vc_font.height >> 1;
339                         break;
340                 case CUR_TWO_THIRDS:
341                         cur_height = (vc->vc_font.height << 1)/3;
342                         break;
343                 case CUR_BLOCK:
344                 default:
345                         cur_height = vc->vc_font.height;
346                         break;
347                 }
348
349                 size = (vc->vc_font.height - cur_height) * width;
350                 while (size--)
351                         tmp[i++] = 0;
352                 size = cur_height * width;
353                 while (size--)
354                         tmp[i++] = 0xff;
355                 memset(mask, 0, w * vc->vc_font.width);
356                 rotate_ccw(tmp, mask, vc->vc_font.width, vc->vc_font.height);
357                 kfree(tmp);
358         }
359
360         switch (mode) {
361         case CM_ERASE:
362                 ops->cursor_state.enable = 0;
363                 break;
364         case CM_DRAW:
365         case CM_MOVE:
366         default:
367                 ops->cursor_state.enable = (use_sw) ? 0 : 1;
368                 break;
369         }
370
371         cursor.image.data = src;
372         cursor.image.fg_color = ops->cursor_state.image.fg_color;
373         cursor.image.bg_color = ops->cursor_state.image.bg_color;
374         cursor.image.dx = ops->cursor_state.image.dx;
375         cursor.image.dy = ops->cursor_state.image.dy;
376         cursor.image.height = ops->cursor_state.image.height;
377         cursor.image.width = ops->cursor_state.image.width;
378         cursor.hot.x = ops->cursor_state.hot.x;
379         cursor.hot.y = ops->cursor_state.hot.y;
380         cursor.mask = ops->cursor_state.mask;
381         cursor.enable = ops->cursor_state.enable;
382         cursor.image.depth = 1;
383         cursor.rop = ROP_XOR;
384
385         if (info->fbops->fb_cursor)
386                 err = info->fbops->fb_cursor(info, &cursor);
387
388         if (err)
389                 soft_cursor(info, &cursor);
390
391         ops->cursor_reset = 0;
392 }
393
394 int ccw_update_start(struct fb_info *info)
395 {
396         struct fbcon_ops *ops = info->fbcon_par;
397         u32 yoffset;
398         u32 vyres = GETVYRES(ops->p->scrollmode, info);
399         int err;
400
401         yoffset = (vyres - info->var.yres) - ops->var.xoffset;
402         ops->var.xoffset = ops->var.yoffset;
403         ops->var.yoffset = yoffset;
404         err = fb_pan_display(info, &ops->var);
405         ops->var.xoffset = info->var.xoffset;
406         ops->var.yoffset = info->var.yoffset;
407         ops->var.vmode = info->var.vmode;
408         return err;
409 }
410
411 void fbcon_rotate_ccw(struct fbcon_ops *ops)
412 {
413         ops->bmove = ccw_bmove;
414         ops->clear = ccw_clear;
415         ops->putcs = ccw_putcs;
416         ops->clear_margins = ccw_clear_margins;
417         ops->cursor = ccw_cursor;
418         ops->update_start = ccw_update_start;
419 }
420 EXPORT_SYMBOL(fbcon_rotate_ccw);
421
422 MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
423 MODULE_DESCRIPTION("Console Rotation (270 degrees) Support");
424 MODULE_LICENSE("GPL");