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