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 
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 static void ud_clear(struct vc_data *vc, struct fb_info *info, int sy,
48 		     int sx, int height, int width)
49 {
50 	struct fb_fillrect region;
51 	int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
52 	u32 vyres = info->var.yres;
53 	u32 vxres = info->var.xres;
54 
55 	region.color = attr_bgcol_ec(bgshift,vc,info);
56 	region.dy = vyres - ((sy + height) * vc->vc_font.height);
57 	region.dx = vxres - ((sx + width) *  vc->vc_font.width);
58 	region.width = width * vc->vc_font.width;
59 	region.height = height * vc->vc_font.height;
60 	region.rop = ROP_COPY;
61 
62 	info->fbops->fb_fillrect(info, &region);
63 }
64 
65 static inline void ud_putcs_aligned(struct vc_data *vc, struct fb_info *info,
66 				    const u16 *s, u32 attr, u32 cnt,
67 				    u32 d_pitch, u32 s_pitch, u32 cellsize,
68 				    struct fb_image *image, u8 *buf, u8 *dst)
69 {
70 	struct fbcon_ops *ops = info->fbcon_par;
71 	u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
72 	u32 idx = vc->vc_font.width >> 3;
73 	u8 *src;
74 
75 	while (cnt--) {
76 		src = ops->fontbuffer + (scr_readw(s--) & charmask)*cellsize;
77 
78 		if (attr) {
79 			ud_update_attr(buf, src, attr, vc);
80 			src = buf;
81 		}
82 
83 		if (likely(idx == 1))
84 			__fb_pad_aligned_buffer(dst, d_pitch, src, idx,
85 						image->height);
86 		else
87 			fb_pad_aligned_buffer(dst, d_pitch, src, idx,
88 					      image->height);
89 
90 		dst += s_pitch;
91 	}
92 
93 	info->fbops->fb_imageblit(info, image);
94 }
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 
110 	while (cnt--) {
111 		src = ops->fontbuffer + (scr_readw(s--) & charmask)*cellsize;
112 
113 		if (attr) {
114 			ud_update_attr(buf, src, attr, vc);
115 			src = buf;
116 		}
117 
118 		fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
119 					image->height, shift_high,
120 					shift_low, mod);
121 		shift_low += mod;
122 		dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
123 		shift_low &= 7;
124 		shift_high = 8 - shift_low;
125 	}
126 
127 	info->fbops->fb_imageblit(info, image);
128 
129 }
130 
131 static void ud_putcs(struct vc_data *vc, struct fb_info *info,
132 		      const unsigned short *s, int count, int yy, int xx,
133 		      int fg, int bg)
134 {
135 	struct fb_image image;
136 	struct fbcon_ops *ops = info->fbcon_par;
137 	u32 width = (vc->vc_font.width + 7)/8;
138 	u32 cellsize = width * vc->vc_font.height;
139 	u32 maxcnt = info->pixmap.size/cellsize;
140 	u32 scan_align = info->pixmap.scan_align - 1;
141 	u32 buf_align = info->pixmap.buf_align - 1;
142 	u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
143 	u32 attribute = get_attribute(info, scr_readw(s));
144 	u8 *dst, *buf = NULL;
145 	u32 vyres = info->var.yres;
146 	u32 vxres = info->var.xres;
147 
148 	if (!ops->fontbuffer)
149 		return;
150 
151 	image.fg_color = fg;
152 	image.bg_color = bg;
153 	image.dy = vyres - ((yy * vc->vc_font.height) + vc->vc_font.height);
154 	image.dx = vxres - ((xx + count) * vc->vc_font.width);
155 	image.height = vc->vc_font.height;
156 	image.depth = 1;
157 
158 	if (attribute) {
159 		buf = kmalloc(cellsize, GFP_KERNEL);
160 		if (!buf)
161 			return;
162 	}
163 
164 	s += count - 1;
165 
166 	while (count) {
167 		if (count > maxcnt)
168 			cnt = maxcnt;
169 		else
170 			cnt = count;
171 
172 		image.width = vc->vc_font.width * cnt;
173 		pitch = ((image.width + 7) >> 3) + scan_align;
174 		pitch &= ~scan_align;
175 		size = pitch * image.height + buf_align;
176 		size &= ~buf_align;
177 		dst = fb_get_buffer_offset(info, &info->pixmap, size);
178 		image.data = dst;
179 
180 		if (!mod)
181 			ud_putcs_aligned(vc, info, s, attribute, cnt, pitch,
182 					 width, cellsize, &image, buf, dst);
183 		else
184 			ud_putcs_unaligned(vc, info, s, attribute, cnt, pitch,
185 					   width, cellsize, &image,
186 					   buf, dst);
187 
188 		image.dx += image.width;
189 		count -= cnt;
190 		s -= cnt;
191 		xx += cnt;
192 	}
193 
194 	/* buf is always NULL except when in monochrome mode, so in this case
195 	   it's a gain to check buf against NULL even though kfree() handles
196 	   NULL pointers just fine */
197 	if (unlikely(buf))
198 		kfree(buf);
199 
200 }
201 
202 static void ud_clear_margins(struct vc_data *vc, struct fb_info *info,
203 			     int color, int bottom_only)
204 {
205 	unsigned int cw = vc->vc_font.width;
206 	unsigned int ch = vc->vc_font.height;
207 	unsigned int rw = info->var.xres - (vc->vc_cols*cw);
208 	unsigned int bh = info->var.yres - (vc->vc_rows*ch);
209 	struct fb_fillrect region;
210 
211 	region.color = color;
212 	region.rop = ROP_COPY;
213 
214 	if ((int) rw > 0 && !bottom_only) {
215 		region.dy = 0;
216 		region.dx = info->var.xoffset;
217 		region.width  = rw;
218 		region.height = info->var.yres_virtual;
219 		info->fbops->fb_fillrect(info, &region);
220 	}
221 
222 	if ((int) bh > 0) {
223 		region.dy = info->var.yoffset;
224 		region.dx = info->var.xoffset;
225                 region.height  = bh;
226                 region.width = info->var.xres;
227 		info->fbops->fb_fillrect(info, &region);
228 	}
229 }
230 
231 static void ud_cursor(struct vc_data *vc, struct fb_info *info, int mode,
232 		      int fg, int bg)
233 {
234 	struct fb_cursor cursor;
235 	struct fbcon_ops *ops = info->fbcon_par;
236 	unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
237 	int w = (vc->vc_font.width + 7) >> 3, c;
238 	int y = real_y(ops->p, vc->state.y);
239 	int attribute, use_sw = vc->vc_cursor_type & CUR_SW;
240 	int err = 1, dx, dy;
241 	char *src;
242 	u32 vyres = info->var.yres;
243 	u32 vxres = info->var.xres;
244 
245 	if (!ops->fontbuffer)
246 		return;
247 
248 	cursor.set = 0;
249 
250  	c = scr_readw((u16 *) vc->vc_pos);
251 	attribute = get_attribute(info, c);
252 	src = ops->fontbuffer + ((c & charmask) * (w * vc->vc_font.height));
253 
254 	if (ops->cursor_state.image.data != src ||
255 	    ops->cursor_reset) {
256 	    ops->cursor_state.image.data = src;
257 	    cursor.set |= FB_CUR_SETIMAGE;
258 	}
259 
260 	if (attribute) {
261 		u8 *dst;
262 
263 		dst = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC);
264 		if (!dst)
265 			return;
266 		kfree(ops->cursor_data);
267 		ops->cursor_data = dst;
268 		ud_update_attr(dst, src, attribute, vc);
269 		src = dst;
270 	}
271 
272 	if (ops->cursor_state.image.fg_color != fg ||
273 	    ops->cursor_state.image.bg_color != bg ||
274 	    ops->cursor_reset) {
275 		ops->cursor_state.image.fg_color = fg;
276 		ops->cursor_state.image.bg_color = bg;
277 		cursor.set |= FB_CUR_SETCMAP;
278 	}
279 
280 	if (ops->cursor_state.image.height != vc->vc_font.height ||
281 	    ops->cursor_state.image.width != vc->vc_font.width ||
282 	    ops->cursor_reset) {
283 		ops->cursor_state.image.height = vc->vc_font.height;
284 		ops->cursor_state.image.width = vc->vc_font.width;
285 		cursor.set |= FB_CUR_SETSIZE;
286 	}
287 
288 	dy = vyres - ((y * vc->vc_font.height) + vc->vc_font.height);
289 	dx = vxres - ((vc->state.x * vc->vc_font.width) + vc->vc_font.width);
290 
291 	if (ops->cursor_state.image.dx != dx ||
292 	    ops->cursor_state.image.dy != dy ||
293 	    ops->cursor_reset) {
294 		ops->cursor_state.image.dx = dx;
295 		ops->cursor_state.image.dy = dy;
296 		cursor.set |= FB_CUR_SETPOS;
297 	}
298 
299 	if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
300 	    ops->cursor_reset) {
301 		ops->cursor_state.hot.x = cursor.hot.y = 0;
302 		cursor.set |= FB_CUR_SETHOT;
303 	}
304 
305 	if (cursor.set & FB_CUR_SETSIZE ||
306 	    vc->vc_cursor_type != ops->p->cursor_shape ||
307 	    ops->cursor_state.mask == NULL ||
308 	    ops->cursor_reset) {
309 		char *mask = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC);
310 		int cur_height, size, i = 0;
311 		u8 msk = 0xff;
312 
313 		if (!mask)
314 			return;
315 
316 		kfree(ops->cursor_state.mask);
317 		ops->cursor_state.mask = mask;
318 
319 		ops->p->cursor_shape = vc->vc_cursor_type;
320 		cursor.set |= FB_CUR_SETSHAPE;
321 
322 		switch (CUR_SIZE(ops->p->cursor_shape)) {
323 		case CUR_NONE:
324 			cur_height = 0;
325 			break;
326 		case CUR_UNDERLINE:
327 			cur_height = (vc->vc_font.height < 10) ? 1 : 2;
328 			break;
329 		case CUR_LOWER_THIRD:
330 			cur_height = vc->vc_font.height/3;
331 			break;
332 		case CUR_LOWER_HALF:
333 			cur_height = vc->vc_font.height >> 1;
334 			break;
335 		case CUR_TWO_THIRDS:
336 			cur_height = (vc->vc_font.height << 1)/3;
337 			break;
338 		case CUR_BLOCK:
339 		default:
340 			cur_height = vc->vc_font.height;
341 			break;
342 		}
343 
344 		size = cur_height * w;
345 
346 		while (size--)
347 			mask[i++] = msk;
348 
349 		size = (vc->vc_font.height - cur_height) * w;
350 
351 		while (size--)
352 			mask[i++] = ~msk;
353 	}
354 
355 	switch (mode) {
356 	case CM_ERASE:
357 		ops->cursor_state.enable = 0;
358 		break;
359 	case CM_DRAW:
360 	case CM_MOVE:
361 	default:
362 		ops->cursor_state.enable = (use_sw) ? 0 : 1;
363 		break;
364 	}
365 
366 	cursor.image.data = src;
367 	cursor.image.fg_color = ops->cursor_state.image.fg_color;
368 	cursor.image.bg_color = ops->cursor_state.image.bg_color;
369 	cursor.image.dx = ops->cursor_state.image.dx;
370 	cursor.image.dy = ops->cursor_state.image.dy;
371 	cursor.image.height = ops->cursor_state.image.height;
372 	cursor.image.width = ops->cursor_state.image.width;
373 	cursor.hot.x = ops->cursor_state.hot.x;
374 	cursor.hot.y = ops->cursor_state.hot.y;
375 	cursor.mask = ops->cursor_state.mask;
376 	cursor.enable = ops->cursor_state.enable;
377 	cursor.image.depth = 1;
378 	cursor.rop = ROP_XOR;
379 
380 	if (info->fbops->fb_cursor)
381 		err = info->fbops->fb_cursor(info, &cursor);
382 
383 	if (err)
384 		soft_cursor(info, &cursor);
385 
386 	ops->cursor_reset = 0;
387 }
388 
389 static int ud_update_start(struct fb_info *info)
390 {
391 	struct fbcon_ops *ops = info->fbcon_par;
392 	int xoffset, yoffset;
393 	u32 vyres = info->var.yres;
394 	u32 vxres = info->var.xres;
395 	int err;
396 
397 	xoffset = vxres - info->var.xres - ops->var.xoffset;
398 	yoffset = vyres - info->var.yres - ops->var.yoffset;
399 	if (yoffset < 0)
400 		yoffset += vyres;
401 	ops->var.xoffset = xoffset;
402 	ops->var.yoffset = yoffset;
403 	err = fb_pan_display(info, &ops->var);
404 	ops->var.xoffset = info->var.xoffset;
405 	ops->var.yoffset = info->var.yoffset;
406 	ops->var.vmode = info->var.vmode;
407 	return err;
408 }
409 
410 void fbcon_rotate_ud(struct fbcon_ops *ops)
411 {
412 	ops->clear = ud_clear;
413 	ops->putcs = ud_putcs;
414 	ops->clear_margins = ud_clear_margins;
415 	ops->cursor = ud_cursor;
416 	ops->update_start = ud_update_start;
417 }
418