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