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