xref: /linux/drivers/video/fbdev/core/fbcon_cw.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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/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 90 degrees
24  */
25 
26 static void cw_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 	u8 c, msk = ~(0xff >> offset);
32 
33 	for (i = 0; i < vc->vc_font.width; i++) {
34 		for (j = 0; j < width; j++) {
35 			c = *src;
36 			if (attribute & FBCON_ATTRIBUTE_UNDERLINE && !j)
37 				c |= msk;
38 			if (attribute & FBCON_ATTRIBUTE_BOLD && i)
39 				c |= *(src-width);
40 			if (attribute & FBCON_ATTRIBUTE_REVERSE)
41 				c = ~c;
42 			src++;
43 			*dst++ = c;
44 		}
45 	}
46 }
47 
48 
49 static void cw_bmove(struct vc_data *vc, struct fb_info *info, int sy,
50 		     int sx, int dy, int dx, int height, int width)
51 {
52 	struct fbcon_par *par = info->fbcon_par;
53 	struct fb_copyarea area;
54 	u32 vxres = GETVXRES(par->p, info);
55 
56 	area.sx = vxres - ((sy + height) * vc->vc_font.height);
57 	area.sy = sx * vc->vc_font.width;
58 	area.dx = vxres - ((dy + height) * vc->vc_font.height);
59 	area.dy = dx * vc->vc_font.width;
60 	area.width = height * vc->vc_font.height;
61 	area.height  = width * vc->vc_font.width;
62 
63 	info->fbops->fb_copyarea(info, &area);
64 }
65 
66 static void cw_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 vxres = GETVXRES(par->p, info);
72 
73 	region.color = bg;
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 
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_par *par = info->fbcon_par;
89 	u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
90 	u32 idx = font_glyph_pitch(vc->vc_font.height);
91 	u8 *src;
92 
93 	while (cnt--) {
94 		src = par->rotated.buf + (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 
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_par *par = info->fbcon_par;
120 	u32 width = font_glyph_pitch(vc->vc_font.height);
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(par->p, info);
129 
130 	if (!par->rotated.buf)
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 
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 
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_par *par = info->fbcon_par;
209 	unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
210 	int w = font_glyph_pitch(vc->vc_font.height);
211 	int c;
212 	int y = real_y(par->p, vc->state.y);
213 	int attribute, use_sw = vc->vc_cursor_type & CUR_SW;
214 	int err = 1, dx, dy;
215 	char *src;
216 	u32 vxres = GETVXRES(par->p, info);
217 
218 	if (!par->rotated.buf)
219 		return;
220 
221 	cursor.set = 0;
222 
223  	c = scr_readw((u16 *) vc->vc_pos);
224 	attribute = get_attribute(info, c);
225 	src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.width));
226 
227 	if (par->cursor_state.image.data != src ||
228 	    par->cursor_reset) {
229 		par->cursor_state.image.data = src;
230 		cursor.set |= FB_CUR_SETIMAGE;
231 	}
232 
233 	if (attribute) {
234 		u8 *dst;
235 
236 		dst = kmalloc_array(w, vc->vc_font.width, GFP_ATOMIC);
237 		if (!dst)
238 			return;
239 		kfree(par->cursor_data);
240 		par->cursor_data = dst;
241 		cw_update_attr(dst, src, attribute, vc);
242 		src = dst;
243 	}
244 
245 	if (par->cursor_state.image.fg_color != fg ||
246 	    par->cursor_state.image.bg_color != bg ||
247 	    par->cursor_reset) {
248 		par->cursor_state.image.fg_color = fg;
249 		par->cursor_state.image.bg_color = bg;
250 		cursor.set |= FB_CUR_SETCMAP;
251 	}
252 
253 	if (par->cursor_state.image.height != vc->vc_font.width ||
254 	    par->cursor_state.image.width != vc->vc_font.height ||
255 	    par->cursor_reset) {
256 		par->cursor_state.image.height = vc->vc_font.width;
257 		par->cursor_state.image.width = vc->vc_font.height;
258 		cursor.set |= FB_CUR_SETSIZE;
259 	}
260 
261 	dx = vxres - ((y * vc->vc_font.height) + vc->vc_font.height);
262 	dy = vc->state.x * vc->vc_font.width;
263 
264 	if (par->cursor_state.image.dx != dx ||
265 	    par->cursor_state.image.dy != dy ||
266 	    par->cursor_reset) {
267 		par->cursor_state.image.dx = dx;
268 		par->cursor_state.image.dy = dy;
269 		cursor.set |= FB_CUR_SETPOS;
270 	}
271 
272 	if (par->cursor_state.hot.x || par->cursor_state.hot.y ||
273 	    par->cursor_reset) {
274 		par->cursor_state.hot.x = cursor.hot.y = 0;
275 		cursor.set |= FB_CUR_SETHOT;
276 	}
277 
278 	if (cursor.set & FB_CUR_SETSIZE ||
279 	    vc->vc_cursor_type != par->p->cursor_shape ||
280 	    par->cursor_state.mask == NULL ||
281 	    par->cursor_reset) {
282 		unsigned char *tmp, *mask;
283 
284 		tmp = kmalloc_array(vc->vc_font.height, vc_font_pitch(&vc->vc_font), GFP_ATOMIC);
285 		if (!tmp)
286 			return;
287 		fbcon_fill_cursor_mask(par, vc, tmp);
288 
289 		mask = kmalloc_array(vc->vc_font.width, w, GFP_ATOMIC);
290 		if (!mask) {
291 			kfree(tmp);
292 			return;
293 		}
294 		font_glyph_rotate_90(tmp, vc->vc_font.width, vc->vc_font.height, mask);
295 		kfree(tmp);
296 
297 		kfree(par->cursor_state.mask);
298 		par->cursor_state.mask = (const char *)mask;
299 
300 		par->p->cursor_shape = vc->vc_cursor_type;
301 		cursor.set |= FB_CUR_SETSHAPE;
302 	}
303 
304 	par->cursor_state.enable = enable && !use_sw;
305 
306 	cursor.image.data = src;
307 	cursor.image.fg_color = par->cursor_state.image.fg_color;
308 	cursor.image.bg_color = par->cursor_state.image.bg_color;
309 	cursor.image.dx = par->cursor_state.image.dx;
310 	cursor.image.dy = par->cursor_state.image.dy;
311 	cursor.image.height = par->cursor_state.image.height;
312 	cursor.image.width = par->cursor_state.image.width;
313 	cursor.hot.x = par->cursor_state.hot.x;
314 	cursor.hot.y = par->cursor_state.hot.y;
315 	cursor.mask = par->cursor_state.mask;
316 	cursor.enable = par->cursor_state.enable;
317 	cursor.image.depth = 1;
318 	cursor.rop = ROP_XOR;
319 
320 	if (info->fbops->fb_cursor)
321 		err = info->fbops->fb_cursor(info, &cursor);
322 
323 	if (err)
324 		soft_cursor(info, &cursor);
325 
326 	par->cursor_reset = 0;
327 }
328 
329 static int cw_update_start(struct fb_info *info)
330 {
331 	struct fbcon_par *par = info->fbcon_par;
332 	u32 vxres = GETVXRES(par->p, info);
333 	u32 xoffset;
334 	int err;
335 
336 	xoffset = vxres - (info->var.xres + par->var.yoffset);
337 	par->var.yoffset = par->var.xoffset;
338 	par->var.xoffset = xoffset;
339 	err = fb_pan_display(info, &par->var);
340 	par->var.xoffset = info->var.xoffset;
341 	par->var.yoffset = info->var.yoffset;
342 	par->var.vmode = info->var.vmode;
343 	return err;
344 }
345 
346 static const struct fbcon_bitops cw_fbcon_bitops = {
347 	.bmove = cw_bmove,
348 	.clear = cw_clear,
349 	.putcs = cw_putcs,
350 	.clear_margins = cw_clear_margins,
351 	.cursor = cw_cursor,
352 	.update_start = cw_update_start,
353 	.rotate_font = fbcon_rotate_font,
354 };
355 
356 void fbcon_set_bitops_cw(struct fbcon_par *par)
357 {
358 	par->bitops = &cw_fbcon_bitops;
359 }
360