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