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/module.h>
12 #include <linux/slab.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 
ud_update_attr(u8 * dst,u8 * src,int attribute,struct vc_data * vc)25 static 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 
ud_bmove(struct vc_data * vc,struct fb_info * info,int sy,int sx,int dy,int dx,int height,int width)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 fbcon_ops *ops = info->fbcon_par;
52 	struct fb_copyarea area;
53 	u32 vyres = GETVYRES(ops->p->scrollmode, info);
54 	u32 vxres = GETVXRES(ops->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 
ud_clear(struct vc_data * vc,struct fb_info * info,int sy,int sx,int height,int width)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 fbcon_ops *ops = info->fbcon_par;
70 	struct fb_fillrect region;
71 	int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
72 	u32 vyres = GETVYRES(ops->p->scrollmode, info);
73 	u32 vxres = GETVXRES(ops->p->scrollmode, info);
74 
75 	region.color = attr_bgcol_ec(bgshift,vc,info);
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 
ud_putcs_aligned(struct vc_data * vc,struct fb_info * info,const u16 * s,u32 attr,u32 cnt,u32 d_pitch,u32 s_pitch,u32 cellsize,struct fb_image * image,u8 * buf,u8 * dst)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 
ud_putcs_unaligned(struct vc_data * vc,struct fb_info * info,const u16 * s,u32 attr,u32 cnt,u32 d_pitch,u32 s_pitch,u32 cellsize,struct fb_image * image,u8 * buf,u8 * dst)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 
ud_putcs(struct vc_data * vc,struct fb_info * info,const unsigned short * s,int count,int yy,int xx,int fg,int bg)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 fbcon_ops *ops = info->fbcon_par;
157 	u32 width = (vc->vc_font.width + 7)/8;
158 	u32 cellsize = width * vc->vc_font.height;
159 	u32 maxcnt = info->pixmap.size/cellsize;
160 	u32 scan_align = info->pixmap.scan_align - 1;
161 	u32 buf_align = info->pixmap.buf_align - 1;
162 	u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
163 	u32 attribute = get_attribute(info, scr_readw(s));
164 	u8 *dst, *buf = NULL;
165 	u32 vyres = GETVYRES(ops->p->scrollmode, info);
166 	u32 vxres = GETVXRES(ops->p->scrollmode, info);
167 
168 	if (!ops->fontbuffer)
169 		return;
170 
171 	image.fg_color = fg;
172 	image.bg_color = bg;
173 	image.dy = vyres - ((yy * vc->vc_font.height) + vc->vc_font.height);
174 	image.dx = vxres - ((xx + count) * vc->vc_font.width);
175 	image.height = vc->vc_font.height;
176 	image.depth = 1;
177 
178 	if (attribute) {
179 		buf = kmalloc(cellsize, GFP_KERNEL);
180 		if (!buf)
181 			return;
182 	}
183 
184 	s += count - 1;
185 
186 	while (count) {
187 		if (count > maxcnt)
188 			cnt = maxcnt;
189 		else
190 			cnt = count;
191 
192 		image.width = vc->vc_font.width * cnt;
193 		pitch = ((image.width + 7) >> 3) + scan_align;
194 		pitch &= ~scan_align;
195 		size = pitch * image.height + buf_align;
196 		size &= ~buf_align;
197 		dst = fb_get_buffer_offset(info, &info->pixmap, size);
198 		image.data = dst;
199 
200 		if (!mod)
201 			ud_putcs_aligned(vc, info, s, attribute, cnt, pitch,
202 					 width, cellsize, &image, buf, dst);
203 		else
204 			ud_putcs_unaligned(vc, info, s, attribute, cnt, pitch,
205 					   width, cellsize, &image,
206 					   buf, dst);
207 
208 		image.dx += image.width;
209 		count -= cnt;
210 		s -= cnt;
211 		xx += cnt;
212 	}
213 
214 	/* buf is always NULL except when in monochrome mode, so in this case
215 	   it's a gain to check buf against NULL even though kfree() handles
216 	   NULL pointers just fine */
217 	if (unlikely(buf))
218 		kfree(buf);
219 
220 }
221 
ud_clear_margins(struct vc_data * vc,struct fb_info * info,int bottom_only)222 static void ud_clear_margins(struct vc_data *vc, struct fb_info *info,
223 			     int bottom_only)
224 {
225 	unsigned int cw = vc->vc_font.width;
226 	unsigned int ch = vc->vc_font.height;
227 	unsigned int rw = info->var.xres - (vc->vc_cols*cw);
228 	unsigned int bh = info->var.yres - (vc->vc_rows*ch);
229 	struct fb_fillrect region;
230 	int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
231 
232 	region.color = attr_bgcol_ec(bgshift,vc,info);
233 	region.rop = ROP_COPY;
234 
235 	if (rw && !bottom_only) {
236 		region.dy = 0;
237 		region.dx = info->var.xoffset;
238 		region.width  = rw;
239 		region.height = info->var.yres_virtual;
240 		info->fbops->fb_fillrect(info, &region);
241 	}
242 
243 	if (bh) {
244 		region.dy = info->var.yoffset;
245 		region.dx = info->var.xoffset;
246                 region.height  = bh;
247                 region.width = info->var.xres;
248 		info->fbops->fb_fillrect(info, &region);
249 	}
250 }
251 
ud_cursor(struct vc_data * vc,struct fb_info * info,int mode,int softback_lines,int fg,int bg)252 static void ud_cursor(struct vc_data *vc, struct fb_info *info, int mode,
253 		      int softback_lines, int fg, int bg)
254 {
255 	struct fb_cursor cursor;
256 	struct fbcon_ops *ops = info->fbcon_par;
257 	unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
258 	int w = (vc->vc_font.width + 7) >> 3, c;
259 	int y = real_y(ops->p, vc->vc_y);
260 	int attribute, use_sw = (vc->vc_cursor_type & 0x10);
261 	int err = 1, dx, dy;
262 	char *src;
263 	u32 vyres = GETVYRES(ops->p->scrollmode, info);
264 	u32 vxres = GETVXRES(ops->p->scrollmode, info);
265 
266 	if (!ops->fontbuffer)
267 		return;
268 
269 	cursor.set = 0;
270 
271 	if (softback_lines) {
272 		if (y + softback_lines >= vc->vc_rows) {
273 			mode = CM_ERASE;
274 			ops->cursor_flash = 0;
275 			return;
276 		} else
277 			y += softback_lines;
278 	}
279 
280  	c = scr_readw((u16 *) vc->vc_pos);
281 	attribute = get_attribute(info, c);
282 	src = ops->fontbuffer + ((c & charmask) * (w * vc->vc_font.height));
283 
284 	if (ops->cursor_state.image.data != src ||
285 	    ops->cursor_reset) {
286 	    ops->cursor_state.image.data = src;
287 	    cursor.set |= FB_CUR_SETIMAGE;
288 	}
289 
290 	if (attribute) {
291 		u8 *dst;
292 
293 		dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
294 		if (!dst)
295 			return;
296 		kfree(ops->cursor_data);
297 		ops->cursor_data = dst;
298 		ud_update_attr(dst, src, attribute, vc);
299 		src = dst;
300 	}
301 
302 	if (ops->cursor_state.image.fg_color != fg ||
303 	    ops->cursor_state.image.bg_color != bg ||
304 	    ops->cursor_reset) {
305 		ops->cursor_state.image.fg_color = fg;
306 		ops->cursor_state.image.bg_color = bg;
307 		cursor.set |= FB_CUR_SETCMAP;
308 	}
309 
310 	if (ops->cursor_state.image.height != vc->vc_font.height ||
311 	    ops->cursor_state.image.width != vc->vc_font.width ||
312 	    ops->cursor_reset) {
313 		ops->cursor_state.image.height = vc->vc_font.height;
314 		ops->cursor_state.image.width = vc->vc_font.width;
315 		cursor.set |= FB_CUR_SETSIZE;
316 	}
317 
318 	dy = vyres - ((y * vc->vc_font.height) + vc->vc_font.height);
319 	dx = vxres - ((vc->vc_x * vc->vc_font.width) + vc->vc_font.width);
320 
321 	if (ops->cursor_state.image.dx != dx ||
322 	    ops->cursor_state.image.dy != dy ||
323 	    ops->cursor_reset) {
324 		ops->cursor_state.image.dx = dx;
325 		ops->cursor_state.image.dy = dy;
326 		cursor.set |= FB_CUR_SETPOS;
327 	}
328 
329 	if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
330 	    ops->cursor_reset) {
331 		ops->cursor_state.hot.x = cursor.hot.y = 0;
332 		cursor.set |= FB_CUR_SETHOT;
333 	}
334 
335 	if (cursor.set & FB_CUR_SETSIZE ||
336 	    vc->vc_cursor_type != ops->p->cursor_shape ||
337 	    ops->cursor_state.mask == NULL ||
338 	    ops->cursor_reset) {
339 		char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
340 		int cur_height, size, i = 0;
341 		u8 msk = 0xff;
342 
343 		if (!mask)
344 			return;
345 
346 		kfree(ops->cursor_state.mask);
347 		ops->cursor_state.mask = mask;
348 
349 		ops->p->cursor_shape = vc->vc_cursor_type;
350 		cursor.set |= FB_CUR_SETSHAPE;
351 
352 		switch (ops->p->cursor_shape & CUR_HWMASK) {
353 		case CUR_NONE:
354 			cur_height = 0;
355 			break;
356 		case CUR_UNDERLINE:
357 			cur_height = (vc->vc_font.height < 10) ? 1 : 2;
358 			break;
359 		case CUR_LOWER_THIRD:
360 			cur_height = vc->vc_font.height/3;
361 			break;
362 		case CUR_LOWER_HALF:
363 			cur_height = vc->vc_font.height >> 1;
364 			break;
365 		case CUR_TWO_THIRDS:
366 			cur_height = (vc->vc_font.height << 1)/3;
367 			break;
368 		case CUR_BLOCK:
369 		default:
370 			cur_height = vc->vc_font.height;
371 			break;
372 		}
373 
374 		size = cur_height * w;
375 
376 		while (size--)
377 			mask[i++] = msk;
378 
379 		size = (vc->vc_font.height - cur_height) * w;
380 
381 		while (size--)
382 			mask[i++] = ~msk;
383 	}
384 
385 	switch (mode) {
386 	case CM_ERASE:
387 		ops->cursor_state.enable = 0;
388 		break;
389 	case CM_DRAW:
390 	case CM_MOVE:
391 	default:
392 		ops->cursor_state.enable = (use_sw) ? 0 : 1;
393 		break;
394 	}
395 
396 	cursor.image.data = src;
397 	cursor.image.fg_color = ops->cursor_state.image.fg_color;
398 	cursor.image.bg_color = ops->cursor_state.image.bg_color;
399 	cursor.image.dx = ops->cursor_state.image.dx;
400 	cursor.image.dy = ops->cursor_state.image.dy;
401 	cursor.image.height = ops->cursor_state.image.height;
402 	cursor.image.width = ops->cursor_state.image.width;
403 	cursor.hot.x = ops->cursor_state.hot.x;
404 	cursor.hot.y = ops->cursor_state.hot.y;
405 	cursor.mask = ops->cursor_state.mask;
406 	cursor.enable = ops->cursor_state.enable;
407 	cursor.image.depth = 1;
408 	cursor.rop = ROP_XOR;
409 
410 	if (info->fbops->fb_cursor)
411 		err = info->fbops->fb_cursor(info, &cursor);
412 
413 	if (err)
414 		soft_cursor(info, &cursor);
415 
416 	ops->cursor_reset = 0;
417 }
418 
ud_update_start(struct fb_info * info)419 static int ud_update_start(struct fb_info *info)
420 {
421 	struct fbcon_ops *ops = info->fbcon_par;
422 	int xoffset, yoffset;
423 	u32 vyres = GETVYRES(ops->p->scrollmode, info);
424 	u32 vxres = GETVXRES(ops->p->scrollmode, info);
425 	int err;
426 
427 	xoffset = vxres - info->var.xres - ops->var.xoffset;
428 	yoffset = vyres - info->var.yres - ops->var.yoffset;
429 	if (yoffset < 0)
430 		yoffset += vyres;
431 	ops->var.xoffset = xoffset;
432 	ops->var.yoffset = yoffset;
433 	err = fb_pan_display(info, &ops->var);
434 	ops->var.xoffset = info->var.xoffset;
435 	ops->var.yoffset = info->var.yoffset;
436 	ops->var.vmode = info->var.vmode;
437 	return err;
438 }
439 
fbcon_rotate_ud(struct fbcon_ops * ops)440 void fbcon_rotate_ud(struct fbcon_ops *ops)
441 {
442 	ops->bmove = ud_bmove;
443 	ops->clear = ud_clear;
444 	ops->putcs = ud_putcs;
445 	ops->clear_margins = ud_clear_margins;
446 	ops->cursor = ud_cursor;
447 	ops->update_start = ud_update_start;
448 }
449 EXPORT_SYMBOL(fbcon_rotate_ud);
450 
451 MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
452 MODULE_DESCRIPTION("Console Rotation (180 degrees) Support");
453 MODULE_LICENSE("GPL");
454