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