Skip to content

Commit a1ac250

Browse files
peilin-yedanvet
authored andcommitted
fbcon: Avoid using FNTCHARCNT() and hard-coded built-in font charcount
For user-provided fonts, the framebuffer layer is using a magic negative-indexing macro, FNTCHARCNT(), to keep track of their number of characters: #define FNTCHARCNT(fd) (((int *)(fd))[-3]) For built-in fonts, it is using hard-coded values (256). This results in something like the following: map.length = (ops->p->userfont) ? FNTCHARCNT(ops->p->fontdata) : 256; This is unsatisfactory. In fact, there is already a `charcount` field in our virtual console descriptor (see `struct console_font` inside `struct vc_data`), let us use it: map.length = vc->vc_font.charcount; Recently we added a `charcount` field to `struct font_desc`. Use it to set `vc->vc_font.charcount` properly. The idea is: - We only use FNTCHARCNT() on `vc->vc_font.data` and `p->fontdata`. Assume FNTCHARCNT() is working as intended; - Whenever `vc->vc_font.data` is set, also set `vc->vc_font.charcount` properly; - We can now replace `FNTCHARCNT(vc->vc_font.data)` with `vc->vc_font.charcount`; - Since `p->fontdata` always point to the same font data buffer with `vc->vc_font.data`, we can also replace `FNTCHARCNT(p->fontdata)` with `vc->vc_font.charcount`. In conclusion, set `vc->vc_font.charcount` properly in fbcon_startup(), fbcon_init(), fbcon_set_disp() and fbcon_do_set_font(), then replace FNTCHARCNT() with `vc->vc_font.charcount`. No more if-else between negative-indexing macros and hard-coded values. Do not include <linux/font.h> in fbcon_rotate.c and tileblit.c, since they no longer need it. Depends on patch "Fonts: Add charcount field to font_desc". Suggested-by: Daniel Vetter <[email protected]> Signed-off-by: Peilin Ye <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Daniel Vetter <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/e460a5780e54e3022661d5f09555144583b4cc59.1605169912.git.yepeilin.cs@gmail.com
1 parent 4497364 commit a1ac250

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

drivers/video/fbdev/core/fbcon.c

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ static const char *fbcon_startup(void)
10041004
vc->vc_font.width = font->width;
10051005
vc->vc_font.height = font->height;
10061006
vc->vc_font.data = (void *)(p->fontdata = font->data);
1007-
vc->vc_font.charcount = 256; /* FIXME Need to support more fonts */
1007+
vc->vc_font.charcount = font->charcount;
10081008
} else {
10091009
p->fontdata = vc->vc_font.data;
10101010
}
@@ -1032,7 +1032,7 @@ static void fbcon_init(struct vc_data *vc, int init)
10321032
struct vc_data **default_mode = vc->vc_display_fg;
10331033
struct vc_data *svc = *default_mode;
10341034
struct fbcon_display *t, *p = &fb_display[vc->vc_num];
1035-
int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
1035+
int logo = 1, new_rows, new_cols, rows, cols;
10361036
int cap, ret;
10371037

10381038
if (WARN_ON(info_idx == -1))
@@ -1068,6 +1068,7 @@ static void fbcon_init(struct vc_data *vc, int init)
10681068
fvc->vc_font.data);
10691069
vc->vc_font.width = fvc->vc_font.width;
10701070
vc->vc_font.height = fvc->vc_font.height;
1071+
vc->vc_font.charcount = fvc->vc_font.charcount;
10711072
p->userfont = t->userfont;
10721073

10731074
if (p->userfont)
@@ -1083,17 +1084,13 @@ static void fbcon_init(struct vc_data *vc, int init)
10831084
vc->vc_font.width = font->width;
10841085
vc->vc_font.height = font->height;
10851086
vc->vc_font.data = (void *)(p->fontdata = font->data);
1086-
vc->vc_font.charcount = 256; /* FIXME Need to
1087-
support more fonts */
1087+
vc->vc_font.charcount = font->charcount;
10881088
}
10891089
}
10901090

1091-
if (p->userfont)
1092-
charcnt = FNTCHARCNT(p->fontdata);
1093-
10941091
vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
10951092
vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1096-
if (charcnt == 256) {
1093+
if (vc->vc_font.charcount == 256) {
10971094
vc->vc_hi_font_mask = 0;
10981095
} else {
10991096
vc->vc_hi_font_mask = 0x100;
@@ -1358,7 +1355,7 @@ static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
13581355
struct vc_data **default_mode, *vc;
13591356
struct vc_data *svc;
13601357
struct fbcon_ops *ops = info->fbcon_par;
1361-
int rows, cols, charcnt = 256;
1358+
int rows, cols;
13621359

13631360
p = &fb_display[unit];
13641361

@@ -1378,12 +1375,11 @@ static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
13781375
vc->vc_font.data = (void *)(p->fontdata = t->fontdata);
13791376
vc->vc_font.width = (*default_mode)->vc_font.width;
13801377
vc->vc_font.height = (*default_mode)->vc_font.height;
1378+
vc->vc_font.charcount = (*default_mode)->vc_font.charcount;
13811379
p->userfont = t->userfont;
13821380
if (p->userfont)
13831381
REFCOUNT(p->fontdata)++;
13841382
}
1385-
if (p->userfont)
1386-
charcnt = FNTCHARCNT(p->fontdata);
13871383

13881384
var->activate = FB_ACTIVATE_NOW;
13891385
info->var.activate = var->activate;
@@ -1393,7 +1389,7 @@ static void fbcon_set_disp(struct fb_info *info, struct fb_var_screeninfo *var,
13931389
ops->var = info->var;
13941390
vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
13951391
vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1396-
if (charcnt == 256) {
1392+
if (vc->vc_font.charcount == 256) {
13971393
vc->vc_hi_font_mask = 0;
13981394
} else {
13991395
vc->vc_hi_font_mask = 0x100;
@@ -2027,7 +2023,7 @@ static int fbcon_resize(struct vc_data *vc, unsigned int width,
20272023
*/
20282024
if (pitch <= 0)
20292025
return -EINVAL;
2030-
size = CALC_FONTSZ(vc->vc_font.height, pitch, FNTCHARCNT(vc->vc_font.data));
2026+
size = CALC_FONTSZ(vc->vc_font.height, pitch, vc->vc_font.charcount);
20312027
if (size > FNTSIZE(vc->vc_font.data))
20322028
return -EINVAL;
20332029
}
@@ -2075,7 +2071,7 @@ static int fbcon_switch(struct vc_data *vc)
20752071
struct fbcon_ops *ops;
20762072
struct fbcon_display *p = &fb_display[vc->vc_num];
20772073
struct fb_var_screeninfo var;
2078-
int i, ret, prev_console, charcnt = 256;
2074+
int i, ret, prev_console;
20792075

20802076
info = registered_fb[con2fb_map[vc->vc_num]];
20812077
ops = info->fbcon_par;
@@ -2152,10 +2148,7 @@ static int fbcon_switch(struct vc_data *vc)
21522148
vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
21532149
vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
21542150

2155-
if (p->userfont)
2156-
charcnt = FNTCHARCNT(vc->vc_font.data);
2157-
2158-
if (charcnt > 256)
2151+
if (vc->vc_font.charcount > 256)
21592152
vc->vc_complement_mask <<= 1;
21602153

21612154
updatescrollmode(p, info, vc);
@@ -2405,31 +2398,27 @@ static void set_vc_hi_font(struct vc_data *vc, bool set)
24052398
}
24062399
}
24072400

2408-
static int fbcon_do_set_font(struct vc_data *vc, int w, int h,
2401+
static int fbcon_do_set_font(struct vc_data *vc, int w, int h, int charcount,
24092402
const u8 * data, int userfont)
24102403
{
24112404
struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
24122405
struct fbcon_ops *ops = info->fbcon_par;
24132406
struct fbcon_display *p = &fb_display[vc->vc_num];
24142407
int resize;
2415-
int cnt;
24162408
char *old_data = NULL;
24172409

24182410
resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
24192411
if (p->userfont)
24202412
old_data = vc->vc_font.data;
2421-
if (userfont)
2422-
cnt = FNTCHARCNT(data);
2423-
else
2424-
cnt = 256;
24252413
vc->vc_font.data = (void *)(p->fontdata = data);
24262414
if ((p->userfont = userfont))
24272415
REFCOUNT(data)++;
24282416
vc->vc_font.width = w;
24292417
vc->vc_font.height = h;
2430-
if (vc->vc_hi_font_mask && cnt == 256)
2418+
vc->vc_font.charcount = charcount;
2419+
if (vc->vc_hi_font_mask && charcount == 256)
24312420
set_vc_hi_font(vc, false);
2432-
else if (!vc->vc_hi_font_mask && cnt == 512)
2421+
else if (!vc->vc_hi_font_mask && charcount == 512)
24332422
set_vc_hi_font(vc, true);
24342423

24352424
if (resize) {
@@ -2496,9 +2485,10 @@ static int fbcon_set_font(struct vc_data *vc, struct console_font *font,
24962485
if (!new_data)
24972486
return -ENOMEM;
24982487

2488+
memset(new_data, 0, FONT_EXTRA_WORDS * sizeof(int));
2489+
24992490
new_data += FONT_EXTRA_WORDS * sizeof(int);
25002491
FNTSIZE(new_data) = size;
2501-
FNTCHARCNT(new_data) = charcount;
25022492
REFCOUNT(new_data) = 0; /* usage counter */
25032493
for (i=0; i< charcount; i++) {
25042494
memcpy(new_data + i*h*pitch, data + i*32*pitch, h*pitch);
@@ -2524,7 +2514,7 @@ static int fbcon_set_font(struct vc_data *vc, struct console_font *font,
25242514
break;
25252515
}
25262516
}
2527-
return fbcon_do_set_font(vc, font->width, font->height, new_data, 1);
2517+
return fbcon_do_set_font(vc, font->width, font->height, charcount, new_data, 1);
25282518
}
25292519

25302520
static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, char *name)
@@ -2540,7 +2530,7 @@ static int fbcon_set_def_font(struct vc_data *vc, struct console_font *font, cha
25402530

25412531
font->width = f->width;
25422532
font->height = f->height;
2543-
return fbcon_do_set_font(vc, f->width, f->height, f->data, 0);
2533+
return fbcon_do_set_font(vc, f->width, f->height, f->charcount, f->data, 0);
25442534
}
25452535

25462536
static u16 palette_red[16];
@@ -3009,7 +2999,6 @@ void fbcon_get_requirement(struct fb_info *info,
30092999
struct fb_blit_caps *caps)
30103000
{
30113001
struct vc_data *vc;
3012-
struct fbcon_display *p;
30133002

30143003
if (caps->flags) {
30153004
int i, charcnt;
@@ -3018,11 +3007,9 @@ void fbcon_get_requirement(struct fb_info *info,
30183007
vc = vc_cons[i].d;
30193008
if (vc && vc->vc_mode == KD_TEXT &&
30203009
info->node == con2fb_map[i]) {
3021-
p = &fb_display[i];
30223010
caps->x |= 1 << (vc->vc_font.width - 1);
30233011
caps->y |= 1 << (vc->vc_font.height - 1);
3024-
charcnt = (p->userfont) ?
3025-
FNTCHARCNT(p->fontdata) : 256;
3012+
charcnt = vc->vc_font.charcount;
30263013
if (caps->len < charcnt)
30273014
caps->len = charcnt;
30283015
}
@@ -3032,11 +3019,9 @@ void fbcon_get_requirement(struct fb_info *info,
30323019

30333020
if (vc && vc->vc_mode == KD_TEXT &&
30343021
info->node == con2fb_map[fg_console]) {
3035-
p = &fb_display[fg_console];
30363022
caps->x = 1 << (vc->vc_font.width - 1);
30373023
caps->y = 1 << (vc->vc_font.height - 1);
3038-
caps->len = (p->userfont) ?
3039-
FNTCHARCNT(p->fontdata) : 256;
3024+
caps->len = vc->vc_font.charcount;
30403025
}
30413026
}
30423027
}

drivers/video/fbdev/core/fbcon_rotate.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <linux/fb.h>
1515
#include <linux/vt_kern.h>
1616
#include <linux/console.h>
17-
#include <linux/font.h>
1817
#include <asm/types.h>
1918
#include "fbcon.h"
2019
#include "fbcon_rotate.h"
@@ -33,7 +32,7 @@ static int fbcon_rotate_font(struct fb_info *info, struct vc_data *vc)
3332

3433
src = ops->fontdata = vc->vc_font.data;
3534
ops->cur_rotate = ops->p->con_rotate;
36-
len = (!ops->p->userfont) ? 256 : FNTCHARCNT(src);
35+
len = vc->vc_font.charcount;
3736
s_cellsize = ((vc->vc_font.width + 7)/8) *
3837
vc->vc_font.height;
3938
d_cellsize = s_cellsize;

drivers/video/fbdev/core/tileblit.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <linux/fb.h>
1414
#include <linux/vt_kern.h>
1515
#include <linux/console.h>
16-
#include <linux/font.h>
1716
#include <asm/types.h>
1817
#include "fbcon.h"
1918

@@ -145,8 +144,7 @@ void fbcon_set_tileops(struct vc_data *vc, struct fb_info *info)
145144
map.width = vc->vc_font.width;
146145
map.height = vc->vc_font.height;
147146
map.depth = 1;
148-
map.length = (ops->p->userfont) ?
149-
FNTCHARCNT(ops->p->fontdata) : 256;
147+
map.length = vc->vc_font.charcount;
150148
map.data = ops->p->fontdata;
151149
info->tileops->fb_settile(info, &map);
152150
}

0 commit comments

Comments
 (0)