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