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