xref: /linux/drivers/video/fbdev/core/fbcon_ccw.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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/font.h>
16 #include <linux/vt_kern.h>
17 #include <linux/console.h>
18 #include <asm/types.h>
19 #include "fbcon.h"
20 #include "fbcon_rotate.h"
21 
22 /*
23  * Rotation 270 degrees
24  */
25 
26 static void ccw_update_attr(u8 *dst, u8 *src, int attribute,
27 				  struct vc_data *vc)
28 {
29 	int i, j, offset = (vc->vc_font.height < 10) ? 1 : 2;
30 	int width = font_glyph_pitch(vc->vc_font.height);
31 	int mod = vc->vc_font.height % 8;
32 	u8 c, msk = ~(0xff << offset), msk1 = 0;
33 
34 	if (mod)
35 		msk <<= (8 - mod);
36 
37 	if (offset > mod)
38 		msk1 |= 0x01;
39 
40 	for (i = 0; i < vc->vc_font.width; i++) {
41 		for (j = 0; j < width; j++) {
42 			c = *src;
43 
44 			if (attribute & FBCON_ATTRIBUTE_UNDERLINE) {
45 				if (j == width - 1)
46 					c |= msk;
47 
48 				if (msk1 && j == width - 2)
49 					c |= msk1;
50 			}
51 
52 			if (attribute & FBCON_ATTRIBUTE_BOLD && i)
53 				*(dst - width) |= c;
54 
55 			if (attribute & FBCON_ATTRIBUTE_REVERSE)
56 				c = ~c;
57 			src++;
58 			*dst++ = c;
59 		}
60 	}
61 }
62 
63 
64 static void ccw_bmove(struct vc_data *vc, struct fb_info *info, int sy,
65 		     int sx, int dy, int dx, int height, int width)
66 {
67 	struct fbcon_par *par = info->fbcon_par;
68 	struct fb_copyarea area;
69 	u32 vyres = GETVYRES(par->p, info);
70 
71 	area.sx = sy * vc->vc_font.height;
72 	area.sy = vyres - ((sx + width) * vc->vc_font.width);
73 	area.dx = dy * vc->vc_font.height;
74 	area.dy = vyres - ((dx + width) * vc->vc_font.width);
75 	area.width = height * vc->vc_font.height;
76 	area.height  = width * vc->vc_font.width;
77 
78 	info->fbops->fb_copyarea(info, &area);
79 }
80 
81 static void ccw_clear(struct vc_data *vc, struct fb_info *info, int sy,
82 		     int sx, int height, int width, int fg, int bg)
83 {
84 	struct fbcon_par *par = info->fbcon_par;
85 	struct fb_fillrect region;
86 	u32 vyres = GETVYRES(par->p, info);
87 
88 	region.color = bg;
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_par *par = info->fbcon_par;
104 	u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
105 	u32 idx = font_glyph_pitch(vc->vc_font.height);
106 	u8 *src;
107 
108 	while (cnt--) {
109 		src = par->rotated.buf + (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_par *par = info->fbcon_par;
135 	u32 width = font_glyph_pitch(vc->vc_font.height);
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(par->p, info);
144 
145 	if (!par->rotated.buf)
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, bool enable,
222 		       int fg, int bg)
223 {
224 	struct fb_cursor cursor;
225 	struct fbcon_par *par = info->fbcon_par;
226 	unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
227 	int w = font_glyph_pitch(vc->vc_font.height);
228 	int c;
229 	int y = real_y(par->p, vc->state.y);
230 	int attribute, use_sw = vc->vc_cursor_type & CUR_SW;
231 	int err = 1, dx, dy;
232 	char *src;
233 	u32 vyres = GETVYRES(par->p, info);
234 
235 	if (!par->rotated.buf)
236 		return;
237 
238 	cursor.set = 0;
239 
240  	c = scr_readw((u16 *) vc->vc_pos);
241 	attribute = get_attribute(info, c);
242 	src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.width));
243 
244 	if (par->cursor_state.image.data != src ||
245 	    par->cursor_reset) {
246 		par->cursor_state.image.data = src;
247 		cursor.set |= FB_CUR_SETIMAGE;
248 	}
249 
250 	if (attribute) {
251 		u8 *dst;
252 
253 		dst = kmalloc_array(w, vc->vc_font.width, GFP_ATOMIC);
254 		if (!dst)
255 			return;
256 		kfree(par->cursor_data);
257 		par->cursor_data = dst;
258 		ccw_update_attr(dst, src, attribute, vc);
259 		src = dst;
260 	}
261 
262 	if (par->cursor_state.image.fg_color != fg ||
263 	    par->cursor_state.image.bg_color != bg ||
264 	    par->cursor_reset) {
265 		par->cursor_state.image.fg_color = fg;
266 		par->cursor_state.image.bg_color = bg;
267 		cursor.set |= FB_CUR_SETCMAP;
268 	}
269 
270 	if (par->cursor_state.image.height != vc->vc_font.width ||
271 	    par->cursor_state.image.width != vc->vc_font.height ||
272 	    par->cursor_reset) {
273 		par->cursor_state.image.height = vc->vc_font.width;
274 		par->cursor_state.image.width = vc->vc_font.height;
275 		cursor.set |= FB_CUR_SETSIZE;
276 	}
277 
278 	dx = y * vc->vc_font.height;
279 	dy = vyres - ((vc->state.x + 1) * vc->vc_font.width);
280 
281 	if (par->cursor_state.image.dx != dx ||
282 	    par->cursor_state.image.dy != dy ||
283 	    par->cursor_reset) {
284 		par->cursor_state.image.dx = dx;
285 		par->cursor_state.image.dy = dy;
286 		cursor.set |= FB_CUR_SETPOS;
287 	}
288 
289 	if (par->cursor_state.hot.x || par->cursor_state.hot.y ||
290 	    par->cursor_reset) {
291 		par->cursor_state.hot.x = cursor.hot.y = 0;
292 		cursor.set |= FB_CUR_SETHOT;
293 	}
294 
295 	if (cursor.set & FB_CUR_SETSIZE ||
296 	    vc->vc_cursor_type != par->p->cursor_shape ||
297 	    par->cursor_state.mask == NULL ||
298 	    par->cursor_reset) {
299 		unsigned char *tmp, *mask;
300 
301 		tmp = kmalloc_array(vc->vc_font.height, vc_font_pitch(&vc->vc_font), GFP_ATOMIC);
302 		if (!tmp)
303 			return;
304 		fbcon_fill_cursor_mask(par, vc, tmp);
305 
306 		mask = kmalloc_array(vc->vc_font.width, w, GFP_ATOMIC);
307 		if (!mask) {
308 			kfree(tmp);
309 			return;
310 		}
311 		font_glyph_rotate_270(tmp, vc->vc_font.width, vc->vc_font.height, mask);
312 		kfree(tmp);
313 
314 		kfree(par->cursor_state.mask);
315 		par->cursor_state.mask = (const char *)mask;
316 
317 		par->p->cursor_shape = vc->vc_cursor_type;
318 		cursor.set |= FB_CUR_SETSHAPE;
319 	}
320 
321 	par->cursor_state.enable = enable && !use_sw;
322 
323 	cursor.image.data = src;
324 	cursor.image.fg_color = par->cursor_state.image.fg_color;
325 	cursor.image.bg_color = par->cursor_state.image.bg_color;
326 	cursor.image.dx = par->cursor_state.image.dx;
327 	cursor.image.dy = par->cursor_state.image.dy;
328 	cursor.image.height = par->cursor_state.image.height;
329 	cursor.image.width = par->cursor_state.image.width;
330 	cursor.hot.x = par->cursor_state.hot.x;
331 	cursor.hot.y = par->cursor_state.hot.y;
332 	cursor.mask = par->cursor_state.mask;
333 	cursor.enable = par->cursor_state.enable;
334 	cursor.image.depth = 1;
335 	cursor.rop = ROP_XOR;
336 
337 	if (info->fbops->fb_cursor)
338 		err = info->fbops->fb_cursor(info, &cursor);
339 
340 	if (err)
341 		soft_cursor(info, &cursor);
342 
343 	par->cursor_reset = 0;
344 }
345 
346 static int ccw_update_start(struct fb_info *info)
347 {
348 	struct fbcon_par *par = info->fbcon_par;
349 	u32 yoffset;
350 	u32 vyres = GETVYRES(par->p, info);
351 	int err;
352 
353 	yoffset = (vyres - info->var.yres) - par->var.xoffset;
354 	par->var.xoffset = par->var.yoffset;
355 	par->var.yoffset = yoffset;
356 	err = fb_pan_display(info, &par->var);
357 	par->var.xoffset = info->var.xoffset;
358 	par->var.yoffset = info->var.yoffset;
359 	par->var.vmode = info->var.vmode;
360 	return err;
361 }
362 
363 static const struct fbcon_bitops ccw_fbcon_bitops = {
364 	.bmove = ccw_bmove,
365 	.clear = ccw_clear,
366 	.putcs = ccw_putcs,
367 	.clear_margins = ccw_clear_margins,
368 	.cursor = ccw_cursor,
369 	.update_start = ccw_update_start,
370 	.rotate_font = fbcon_rotate_font,
371 };
372 
373 void fbcon_set_bitops_ccw(struct fbcon_par *par)
374 {
375 	par->bitops = &ccw_fbcon_bitops;
376 }
377