Message ID | 20230605144812.15241-29-tzimmermann@suse.de (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | fbdev: Make userspace interfaces optional | expand |
Hi Thomas, kernel test robot noticed the following build warnings: [auto build test WARNING on next-20230605] [cannot apply to drm-misc/drm-misc-next lee-backlight/for-backlight-next staging/staging-testing staging/staging-next staging/staging-linus linus/master lee-backlight/for-backlight-fixes v6.4-rc5 v6.4-rc4 v6.4-rc3 v6.4-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Thomas-Zimmermann/backlight-bd6107-Compare-against-struct-fb_info-device/20230605-225002 base: next-20230605 patch link: https://lore.kernel.org/r/20230605144812.15241-29-tzimmermann%40suse.de patch subject: [PATCH 28/30] fbdev/core: Move file-I/O code into separate file config: sparc-allyesconfig (https://download.01.org/0day-ci/archive/20230606/202306060527.syH2D4Is-lkp@intel.com/config) compiler: sparc64-linux-gcc (GCC) 12.3.0 reproduce (this is a W=1 build): mkdir -p ~/bin wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/intel-lab-lkp/linux/commit/34fb1357f6464f1173e12cd241310efa5577dd79 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Thomas-Zimmermann/backlight-bd6107-Compare-against-struct-fb_info-device/20230605-225002 git checkout 34fb1357f6464f1173e12cd241310efa5577dd79 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.3.0 ~/bin/make.cross W=1 O=build_dir ARCH=sparc olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.3.0 ~/bin/make.cross W=1 O=build_dir ARCH=sparc SHELL=/bin/bash drivers/video/fbdev/core/ If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202306060527.syH2D4Is-lkp@intel.com/ All warnings (new ones prefixed by >>): drivers/video/fbdev/core/fb_devfs.c:174:9: error: unknown type name 'compat_caddr_t' 174 | compat_caddr_t smem_start; | ^~~~~~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:183:9: error: unknown type name 'compat_caddr_t' 183 | compat_caddr_t mmio_start; | ^~~~~~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:192:9: error: unknown type name 'compat_caddr_t' 192 | compat_caddr_t red; | ^~~~~~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:193:9: error: unknown type name 'compat_caddr_t' 193 | compat_caddr_t green; | ^~~~~~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:194:9: error: unknown type name 'compat_caddr_t' 194 | compat_caddr_t blue; | ^~~~~~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:195:9: error: unknown type name 'compat_caddr_t' 195 | compat_caddr_t transp; | ^~~~~~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c: In function 'fb_getput_cmap': drivers/video/fbdev/core/fb_devfs.c:205:37: error: implicit declaration of function 'compat_ptr' [-Werror=implicit-function-declaration] 205 | if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) | ^~~~~~~~~~ >> drivers/video/fbdev/core/fb_devfs.c:205:37: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion] 205 | if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) | ^~~~~~~~~~~~~~~ | | | int In file included from include/linux/sched/task.h:11, from include/linux/sched/signal.h:9, from include/linux/rcuwait.h:6, from include/linux/percpu-rwsem.h:7, from include/linux/fs.h:33, from include/linux/huge_mm.h:8, from include/linux/mm.h:989, from include/linux/kallsyms.h:13, from include/linux/ftrace.h:13, from include/linux/kprobes.h:28, from include/linux/kgdb.h:19, from include/linux/fb.h:6, from drivers/video/fbdev/core/fb_devfs.c:4: include/linux/uaccess.h:180:45: note: expected 'const void *' but argument is of type 'int' 180 | copy_from_user(void *to, const void __user *from, unsigned long n) | ~~~~~~~~~~~~~~~~~~~^~~~ >> drivers/video/fbdev/core/fb_devfs.c:211:27: warning: initialization of '__u16 *' {aka 'short unsigned int *'} from 'int' makes pointer from integer without a cast [-Wint-conversion] 211 | .red = compat_ptr(cmap32.red), | ^~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:211:27: note: (near initialization for '(anonymous).red') drivers/video/fbdev/core/fb_devfs.c:212:27: warning: initialization of '__u16 *' {aka 'short unsigned int *'} from 'int' makes pointer from integer without a cast [-Wint-conversion] 212 | .green = compat_ptr(cmap32.green), | ^~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:212:27: note: (near initialization for '(anonymous).green') drivers/video/fbdev/core/fb_devfs.c:213:27: warning: initialization of '__u16 *' {aka 'short unsigned int *'} from 'int' makes pointer from integer without a cast [-Wint-conversion] 213 | .blue = compat_ptr(cmap32.blue), | ^~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:213:27: note: (near initialization for '(anonymous).blue') drivers/video/fbdev/core/fb_devfs.c:214:27: warning: initialization of '__u16 *' {aka 'short unsigned int *'} from 'int' makes pointer from integer without a cast [-Wint-conversion] 214 | .transp = compat_ptr(cmap32.transp), | ^~~~~~~~~~ drivers/video/fbdev/core/fb_devfs.c:214:27: note: (near initialization for '(anonymous).transp') drivers/video/fbdev/core/fb_devfs.c: In function 'fb_get_fscreeninfo': >> drivers/video/fbdev/core/fb_devfs.c:270:45: warning: passing argument 2 of 'do_fscreeninfo_to_user' makes pointer from integer without a cast [-Wint-conversion] 270 | return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); | ^~~~~~~~~~~~~~~ | | | int drivers/video/fbdev/core/fb_devfs.c:228:70: note: expected 'struct fb_fix_screeninfo32 *' but argument is of type 'int' 228 | struct fb_fix_screeninfo32 __user *fix32) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~ cc1: some warnings being treated as errors vim +/copy_from_user +205 drivers/video/fbdev/core/fb_devfs.c 197 198 static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, 199 unsigned long arg) 200 { 201 struct fb_cmap32 cmap32; 202 struct fb_cmap cmap_from; 203 struct fb_cmap_user cmap; 204 > 205 if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) 206 return -EFAULT; 207 208 cmap = (struct fb_cmap_user) { 209 .start = cmap32.start, 210 .len = cmap32.len, > 211 .red = compat_ptr(cmap32.red), 212 .green = compat_ptr(cmap32.green), 213 .blue = compat_ptr(cmap32.blue), 214 .transp = compat_ptr(cmap32.transp), 215 }; 216 217 if (cmd == FBIOPUTCMAP) 218 return fb_set_user_cmap(&cmap, info); 219 220 lock_fb_info(info); 221 cmap_from = info->cmap; 222 unlock_fb_info(info); 223 224 return fb_cmap_to_user(&cmap_from, &cmap); 225 } 226 227 static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, 228 struct fb_fix_screeninfo32 __user *fix32) 229 { 230 __u32 data; 231 int err; 232 233 err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); 234 235 data = (__u32) (unsigned long) fix->smem_start; 236 err |= put_user(data, &fix32->smem_start); 237 238 err |= put_user(fix->smem_len, &fix32->smem_len); 239 err |= put_user(fix->type, &fix32->type); 240 err |= put_user(fix->type_aux, &fix32->type_aux); 241 err |= put_user(fix->visual, &fix32->visual); 242 err |= put_user(fix->xpanstep, &fix32->xpanstep); 243 err |= put_user(fix->ypanstep, &fix32->ypanstep); 244 err |= put_user(fix->ywrapstep, &fix32->ywrapstep); 245 err |= put_user(fix->line_length, &fix32->line_length); 246 247 data = (__u32) (unsigned long) fix->mmio_start; 248 err |= put_user(data, &fix32->mmio_start); 249 250 err |= put_user(fix->mmio_len, &fix32->mmio_len); 251 err |= put_user(fix->accel, &fix32->accel); 252 err |= copy_to_user(fix32->reserved, fix->reserved, 253 sizeof(fix->reserved)); 254 255 if (err) 256 return -EFAULT; 257 return 0; 258 } 259 260 static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, 261 unsigned long arg) 262 { 263 struct fb_fix_screeninfo fix; 264 265 lock_fb_info(info); 266 fix = info->fix; 267 if (info->flags & FBINFO_HIDE_SMEM_START) 268 fix.smem_start = 0; 269 unlock_fb_info(info); > 270 return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); 271 } 272
Hi Thomas. On Mon, Jun 05, 2023 at 04:48:10PM +0200, Thomas Zimmermann wrote: > Move fbdev's file-I/O code into a separate file and contain it in > init and cleanup helpers. No functional changes. > > Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Consider moving the two helps as noted below. With or without this move: Reviewed-by: Sam Ravnborg <sam@ravnborg.org> > --- > drivers/video/fbdev/core/Makefile | 1 + > drivers/video/fbdev/core/fb_devfs.c | 484 +++++++++++++++++++++++++ > drivers/video/fbdev/core/fb_internal.h | 6 + > drivers/video/fbdev/core/fbmem.c | 478 +----------------------- > 4 files changed, 497 insertions(+), 472 deletions(-) > create mode 100644 drivers/video/fbdev/core/fb_devfs.c > > diff --git a/drivers/video/fbdev/core/Makefile b/drivers/video/fbdev/core/Makefile > index 665320160f70..125d24f50c36 100644 > --- a/drivers/video/fbdev/core/Makefile > +++ b/drivers/video/fbdev/core/Makefile > @@ -2,6 +2,7 @@ > obj-$(CONFIG_FB_NOTIFY) += fb_notify.o > obj-$(CONFIG_FB) += fb.o > fb-y := fb_backlight.o \ > + fb_devfs.o \ > fb_info.o \ > fb_procfs.o \ > fbmem.o fbmon.o fbcmap.o fbsysfs.o \ > diff --git a/drivers/video/fbdev/core/fb_devfs.c b/drivers/video/fbdev/core/fb_devfs.c > new file mode 100644 > index 000000000000..5ab16cb24524 > --- /dev/null > +++ b/drivers/video/fbdev/core/fb_devfs.c devfs gives me another understanding of what this file is used for. fb_ioctl.c? > @@ -0,0 +1,484 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include <linux/console.h> > +#include <linux/fb.h> > +#include <linux/fbcon.h> > +#include <linux/major.h> > + > +#include "fb_internal.h" > + > +/* > + * We hold a reference to the fb_info in file->private_data, > + * but if the current registered fb has changed, we don't > + * actually want to use it. > + * > + * So look up the fb_info using the inode minor number, > + * and just verify it against the reference we have. > + */ > +static struct fb_info *file_fb_info(struct file *file) > +{ > + struct inode *inode = file_inode(file); > + int fbidx = iminor(inode); > + struct fb_info *info = registered_fb[fbidx]; > + > + if (info != file->private_data) > + info = NULL; > + return info; > +} > + > +static ssize_t fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) > +{ > + struct fb_info *info = file_fb_info(file); > + > + if (!info) > + return -ENODEV; > + > + if (info->state != FBINFO_STATE_RUNNING) > + return -EPERM; > + > + if (info->fbops->fb_read) > + return info->fbops->fb_read(info, buf, count, ppos); > + > + return fb_io_read(info, buf, count, ppos); > +} > + > +static ssize_t fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) > +{ > + struct fb_info *info = file_fb_info(file); > + > + if (!info) > + return -ENODEV; > + > + if (info->state != FBINFO_STATE_RUNNING) > + return -EPERM; > + > + if (info->fbops->fb_write) > + return info->fbops->fb_write(info, buf, count, ppos); > + > + return fb_io_write(info, buf, count, ppos); > +} > + > +static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, > + unsigned long arg) > +{ > + const struct fb_ops *fb; > + struct fb_var_screeninfo var; > + struct fb_fix_screeninfo fix; > + struct fb_cmap cmap_from; > + struct fb_cmap_user cmap; > + void __user *argp = (void __user *)arg; > + long ret = 0; > + > + switch (cmd) { > + case FBIOGET_VSCREENINFO: > + lock_fb_info(info); > + var = info->var; > + unlock_fb_info(info); > + > + ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0; > + break; > + case FBIOPUT_VSCREENINFO: > + if (copy_from_user(&var, argp, sizeof(var))) > + return -EFAULT; > + /* only for kernel-internal use */ > + var.activate &= ~FB_ACTIVATE_KD_TEXT; > + console_lock(); > + lock_fb_info(info); > + ret = fbcon_modechange_possible(info, &var); > + if (!ret) > + ret = fb_set_var(info, &var); > + if (!ret) > + fbcon_update_vcs(info, var.activate & FB_ACTIVATE_ALL); > + unlock_fb_info(info); > + console_unlock(); > + if (!ret && copy_to_user(argp, &var, sizeof(var))) > + ret = -EFAULT; > + break; > + case FBIOGET_FSCREENINFO: > + lock_fb_info(info); > + memcpy(&fix, &info->fix, sizeof(fix)); > + if (info->flags & FBINFO_HIDE_SMEM_START) > + fix.smem_start = 0; > + unlock_fb_info(info); > + > + ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0; > + break; > + case FBIOPUTCMAP: > + if (copy_from_user(&cmap, argp, sizeof(cmap))) > + return -EFAULT; > + ret = fb_set_user_cmap(&cmap, info); > + break; > + case FBIOGETCMAP: > + if (copy_from_user(&cmap, argp, sizeof(cmap))) > + return -EFAULT; > + lock_fb_info(info); > + cmap_from = info->cmap; > + unlock_fb_info(info); > + ret = fb_cmap_to_user(&cmap_from, &cmap); > + break; > + case FBIOPAN_DISPLAY: > + if (copy_from_user(&var, argp, sizeof(var))) > + return -EFAULT; > + console_lock(); > + lock_fb_info(info); > + ret = fb_pan_display(info, &var); > + unlock_fb_info(info); > + console_unlock(); > + if (ret == 0 && copy_to_user(argp, &var, sizeof(var))) > + return -EFAULT; > + break; > + case FBIO_CURSOR: > + ret = -EINVAL; > + break; > + case FBIOGET_CON2FBMAP: > + ret = fbcon_get_con2fb_map_ioctl(argp); > + break; > + case FBIOPUT_CON2FBMAP: > + ret = fbcon_set_con2fb_map_ioctl(argp); > + break; > + case FBIOBLANK: > + if (arg > FB_BLANK_POWERDOWN) > + return -EINVAL; > + console_lock(); > + lock_fb_info(info); > + ret = fb_blank(info, arg); > + /* might again call into fb_blank */ > + fbcon_fb_blanked(info, arg); > + unlock_fb_info(info); > + console_unlock(); > + break; > + default: > + lock_fb_info(info); > + fb = info->fbops; > + if (fb->fb_ioctl) > + ret = fb->fb_ioctl(info, cmd, arg); > + else > + ret = -ENOTTY; > + unlock_fb_info(info); > + } > + return ret; > +} > + > +static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) > +{ > + struct fb_info *info = file_fb_info(file); > + > + if (!info) > + return -ENODEV; > + return do_fb_ioctl(info, cmd, arg); > +} > + > +#ifdef CONFIG_COMPAT > +struct fb_fix_screeninfo32 { > + char id[16]; > + compat_caddr_t smem_start; > + u32 smem_len; > + u32 type; > + u32 type_aux; > + u32 visual; > + u16 xpanstep; > + u16 ypanstep; > + u16 ywrapstep; > + u32 line_length; > + compat_caddr_t mmio_start; > + u32 mmio_len; > + u32 accel; > + u16 reserved[3]; > +}; > + > +struct fb_cmap32 { > + u32 start; > + u32 len; > + compat_caddr_t red; > + compat_caddr_t green; > + compat_caddr_t blue; > + compat_caddr_t transp; > +}; > + > +static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, > + unsigned long arg) > +{ > + struct fb_cmap32 cmap32; > + struct fb_cmap cmap_from; > + struct fb_cmap_user cmap; > + > + if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) > + return -EFAULT; > + > + cmap = (struct fb_cmap_user) { > + .start = cmap32.start, > + .len = cmap32.len, > + .red = compat_ptr(cmap32.red), > + .green = compat_ptr(cmap32.green), > + .blue = compat_ptr(cmap32.blue), > + .transp = compat_ptr(cmap32.transp), > + }; > + > + if (cmd == FBIOPUTCMAP) > + return fb_set_user_cmap(&cmap, info); > + > + lock_fb_info(info); > + cmap_from = info->cmap; > + unlock_fb_info(info); > + > + return fb_cmap_to_user(&cmap_from, &cmap); > +} > + > +static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, > + struct fb_fix_screeninfo32 __user *fix32) > +{ > + __u32 data; > + int err; > + > + err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); > + > + data = (__u32) (unsigned long) fix->smem_start; > + err |= put_user(data, &fix32->smem_start); > + > + err |= put_user(fix->smem_len, &fix32->smem_len); > + err |= put_user(fix->type, &fix32->type); > + err |= put_user(fix->type_aux, &fix32->type_aux); > + err |= put_user(fix->visual, &fix32->visual); > + err |= put_user(fix->xpanstep, &fix32->xpanstep); > + err |= put_user(fix->ypanstep, &fix32->ypanstep); > + err |= put_user(fix->ywrapstep, &fix32->ywrapstep); > + err |= put_user(fix->line_length, &fix32->line_length); > + > + data = (__u32) (unsigned long) fix->mmio_start; > + err |= put_user(data, &fix32->mmio_start); > + > + err |= put_user(fix->mmio_len, &fix32->mmio_len); > + err |= put_user(fix->accel, &fix32->accel); > + err |= copy_to_user(fix32->reserved, fix->reserved, > + sizeof(fix->reserved)); > + > + if (err) > + return -EFAULT; > + return 0; > +} > + > +static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, > + unsigned long arg) > +{ > + struct fb_fix_screeninfo fix; > + > + lock_fb_info(info); > + fix = info->fix; > + if (info->flags & FBINFO_HIDE_SMEM_START) > + fix.smem_start = 0; > + unlock_fb_info(info); > + return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); > +} > + > +static long fb_compat_ioctl(struct file *file, unsigned int cmd, > + unsigned long arg) > +{ > + struct fb_info *info = file_fb_info(file); > + const struct fb_ops *fb; > + long ret = -ENOIOCTLCMD; > + > + if (!info) > + return -ENODEV; > + fb = info->fbops; > + switch (cmd) { > + case FBIOGET_VSCREENINFO: > + case FBIOPUT_VSCREENINFO: > + case FBIOPAN_DISPLAY: > + case FBIOGET_CON2FBMAP: > + case FBIOPUT_CON2FBMAP: > + arg = (unsigned long) compat_ptr(arg); > + fallthrough; > + case FBIOBLANK: > + ret = do_fb_ioctl(info, cmd, arg); > + break; > + > + case FBIOGET_FSCREENINFO: > + ret = fb_get_fscreeninfo(info, cmd, arg); > + break; > + > + case FBIOGETCMAP: > + case FBIOPUTCMAP: > + ret = fb_getput_cmap(info, cmd, arg); > + break; > + > + default: > + if (fb->fb_compat_ioctl) > + ret = fb->fb_compat_ioctl(info, cmd, arg); > + break; > + } > + return ret; > +} > +#endif > + > +static int fb_mmap(struct file *file, struct vm_area_struct *vma) > +{ > + struct fb_info *info = file_fb_info(file); > + unsigned long mmio_pgoff; > + unsigned long start; > + u32 len; > + > + if (!info) > + return -ENODEV; > + mutex_lock(&info->mm_lock); > + > + if (info->fbops->fb_mmap) { > + int res; > + > + /* > + * The framebuffer needs to be accessed decrypted, be sure > + * SME protection is removed ahead of the call > + */ > + vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); > + res = info->fbops->fb_mmap(info, vma); > + mutex_unlock(&info->mm_lock); > + return res; > +#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) > + } else if (info->fbdefio) { > + /* > + * FB deferred I/O wants you to handle mmap in your drivers. At a > + * minimum, point struct fb_ops.fb_mmap to fb_deferred_io_mmap(). > + */ > + dev_warn_once(info->dev, "fbdev mmap not set up for deferred I/O.\n"); > + mutex_unlock(&info->mm_lock); > + return -ENODEV; > +#endif > + } > + > + /* > + * Ugh. This can be either the frame buffer mapping, or > + * if pgoff points past it, the mmio mapping. > + */ > + start = info->fix.smem_start; > + len = info->fix.smem_len; > + mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT; > + if (vma->vm_pgoff >= mmio_pgoff) { > + if (info->var.accel_flags) { > + mutex_unlock(&info->mm_lock); > + return -EINVAL; > + } > + > + vma->vm_pgoff -= mmio_pgoff; > + start = info->fix.mmio_start; > + len = info->fix.mmio_len; > + } > + mutex_unlock(&info->mm_lock); > + > + vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); > + fb_pgprotect(file, vma, start); > + > + return vm_iomap_memory(vma, start, len); > +} > + > +static int fb_open(struct inode *inode, struct file *file) > +__acquires(&info->lock) > +__releases(&info->lock) > +{ > + int fbidx = iminor(inode); > + struct fb_info *info; > + int res = 0; > + > + info = get_fb_info(fbidx); > + if (!info) { > + request_module("fb%d", fbidx); > + info = get_fb_info(fbidx); > + if (!info) > + return -ENODEV; > + } > + if (IS_ERR(info)) > + return PTR_ERR(info); > + > + lock_fb_info(info); > + if (!try_module_get(info->fbops->owner)) { > + res = -ENODEV; > + goto out; > + } > + file->private_data = info; > + if (info->fbops->fb_open) { > + res = info->fbops->fb_open(info, 1); > + if (res) > + module_put(info->fbops->owner); > + } > +#ifdef CONFIG_FB_DEFERRED_IO > + if (info->fbdefio) > + fb_deferred_io_open(info, inode, file); > +#endif > +out: > + unlock_fb_info(info); > + if (res) > + put_fb_info(info); > + return res; > +} > + > +static int fb_release(struct inode *inode, struct file *file) > +__acquires(&info->lock) > +__releases(&info->lock) > +{ > + struct fb_info * const info = file->private_data; > + > + lock_fb_info(info); > +#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) > + if (info->fbdefio) > + fb_deferred_io_release(info); > +#endif > + if (info->fbops->fb_release) > + info->fbops->fb_release(info, 1); > + module_put(info->fbops->owner); > + unlock_fb_info(info); > + put_fb_info(info); > + return 0; > +} > + > +#if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU) > +static unsigned long get_fb_unmapped_area(struct file *filp, > + unsigned long addr, unsigned long len, > + unsigned long pgoff, unsigned long flags) > +{ > + struct fb_info * const info = filp->private_data; > + unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len); > + > + if (pgoff > fb_size || len > fb_size - pgoff) > + return -EINVAL; > + > + return (unsigned long)info->screen_base + pgoff; > +} > +#endif > + > +static const struct file_operations fb_fops = { > + .owner = THIS_MODULE, > + .read = fb_read, > + .write = fb_write, > + .unlocked_ioctl = fb_ioctl, > +#ifdef CONFIG_COMPAT > + .compat_ioctl = fb_compat_ioctl, > +#endif > + .mmap = fb_mmap, > + .open = fb_open, > + .release = fb_release, > +#if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \ > + (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \ > + !defined(CONFIG_MMU)) > + .get_unmapped_area = get_fb_unmapped_area, > +#endif > +#ifdef CONFIG_FB_DEFERRED_IO > + .fsync = fb_deferred_io_fsync, > +#endif > + .llseek = default_llseek, > +}; > + > +int fb_register_chrdev(void) > +{ > + int ret; > + > + ret = register_chrdev(FB_MAJOR, "fb", &fb_fops); > + if (ret) { > + pr_err("Unable to get major %d for fb devs\n", FB_MAJOR); > + return ret; > + } > + > + return ret; > +} > + > +void fb_unregister_chrdev(void) > +{ > + unregister_chrdev(FB_MAJOR, "fb"); > +} > diff --git a/drivers/video/fbdev/core/fb_internal.h b/drivers/video/fbdev/core/fb_internal.h > index 51f7c9f04e45..abe06c9da36e 100644 > --- a/drivers/video/fbdev/core/fb_internal.h > +++ b/drivers/video/fbdev/core/fb_internal.h > @@ -6,10 +6,16 @@ > #include <linux/fb.h> > #include <linux/mutex.h> > > +/* fb_devfs.c */ > +int fb_register_chrdev(void); > +void fb_unregister_chrdev(void); > + > /* fbmem.c */ > extern struct mutex registration_lock; > extern struct fb_info *registered_fb[FB_MAX]; > extern int num_registered_fb; > +struct fb_info *get_fb_info(unsigned int idx); > +void put_fb_info(struct fb_info *fb_info); The only users of get_fb_info() and put_fb_info() are now in fb_devfs. So consider moving these two helpers too. > > /* fb_procfs.c */ > int fb_init_procfs(void); > diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c > index de1e45240161..2d26ac46337b 100644 > --- a/drivers/video/fbdev/core/fbmem.c > +++ b/drivers/video/fbdev/core/fbmem.c > @@ -17,7 +17,6 @@ > #include <linux/types.h> > #include <linux/errno.h> > #include <linux/kernel.h> > -#include <linux/major.h> > #include <linux/slab.h> > #include <linux/mm.h> > #include <linux/mman.h> > @@ -54,7 +53,7 @@ bool fb_center_logo __read_mostly; > > int fb_logo_count __read_mostly = -1; > > -static struct fb_info *get_fb_info(unsigned int idx) > +struct fb_info *get_fb_info(unsigned int idx) > { > struct fb_info *fb_info; > > @@ -70,7 +69,7 @@ static struct fb_info *get_fb_info(unsigned int idx) > return fb_info; > } > > -static void put_fb_info(struct fb_info *fb_info) > +void put_fb_info(struct fb_info *fb_info) > { > if (!refcount_dec_and_test(&fb_info->count)) > return; > @@ -699,59 +698,6 @@ int fb_show_logo(struct fb_info *info, int rotate) { return 0; } > EXPORT_SYMBOL(fb_prepare_logo); > EXPORT_SYMBOL(fb_show_logo); Reminds me - consider moving logo stuff to a fb_logo file. This would reduce fbmem with a lot of lines, and it is separate. But it is outside the goal of this patchset. > > -/* > - * We hold a reference to the fb_info in file->private_data, > - * but if the current registered fb has changed, we don't > - * actually want to use it. > - * > - * So look up the fb_info using the inode minor number, > - * and just verify it against the reference we have. > - */ > -static struct fb_info *file_fb_info(struct file *file) > -{ > - struct inode *inode = file_inode(file); > - int fbidx = iminor(inode); > - struct fb_info *info = registered_fb[fbidx]; > - > - if (info != file->private_data) > - info = NULL; > - return info; > -} > - > -static ssize_t > -fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) > -{ > - struct fb_info *info = file_fb_info(file); > - > - if (!info) > - return -ENODEV; > - > - if (info->state != FBINFO_STATE_RUNNING) > - return -EPERM; > - > - if (info->fbops->fb_read) > - return info->fbops->fb_read(info, buf, count, ppos); > - > - return fb_io_read(info, buf, count, ppos); > -} > - > -static ssize_t > -fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) > -{ > - struct fb_info *info = file_fb_info(file); > - > - if (!info) > - return -ENODEV; > - > - if (info->state != FBINFO_STATE_RUNNING) > - return -EPERM; > - > - if (info->fbops->fb_write) > - return info->fbops->fb_write(info, buf, count, ppos); > - > - return fb_io_write(info, buf, count, ppos); > -} > - > int > fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var) > { > @@ -951,416 +897,6 @@ fb_blank(struct fb_info *info, int blank) > } > EXPORT_SYMBOL(fb_blank); > > -static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, > - unsigned long arg) > -{ > - const struct fb_ops *fb; > - struct fb_var_screeninfo var; > - struct fb_fix_screeninfo fix; > - struct fb_cmap cmap_from; > - struct fb_cmap_user cmap; > - void __user *argp = (void __user *)arg; > - long ret = 0; > - > - switch (cmd) { > - case FBIOGET_VSCREENINFO: > - lock_fb_info(info); > - var = info->var; > - unlock_fb_info(info); > - > - ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0; > - break; > - case FBIOPUT_VSCREENINFO: > - if (copy_from_user(&var, argp, sizeof(var))) > - return -EFAULT; > - /* only for kernel-internal use */ > - var.activate &= ~FB_ACTIVATE_KD_TEXT; > - console_lock(); > - lock_fb_info(info); > - ret = fbcon_modechange_possible(info, &var); > - if (!ret) > - ret = fb_set_var(info, &var); > - if (!ret) > - fbcon_update_vcs(info, var.activate & FB_ACTIVATE_ALL); > - unlock_fb_info(info); > - console_unlock(); > - if (!ret && copy_to_user(argp, &var, sizeof(var))) > - ret = -EFAULT; > - break; > - case FBIOGET_FSCREENINFO: > - lock_fb_info(info); > - memcpy(&fix, &info->fix, sizeof(fix)); > - if (info->flags & FBINFO_HIDE_SMEM_START) > - fix.smem_start = 0; > - unlock_fb_info(info); > - > - ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0; > - break; > - case FBIOPUTCMAP: > - if (copy_from_user(&cmap, argp, sizeof(cmap))) > - return -EFAULT; > - ret = fb_set_user_cmap(&cmap, info); > - break; > - case FBIOGETCMAP: > - if (copy_from_user(&cmap, argp, sizeof(cmap))) > - return -EFAULT; > - lock_fb_info(info); > - cmap_from = info->cmap; > - unlock_fb_info(info); > - ret = fb_cmap_to_user(&cmap_from, &cmap); > - break; > - case FBIOPAN_DISPLAY: > - if (copy_from_user(&var, argp, sizeof(var))) > - return -EFAULT; > - console_lock(); > - lock_fb_info(info); > - ret = fb_pan_display(info, &var); > - unlock_fb_info(info); > - console_unlock(); > - if (ret == 0 && copy_to_user(argp, &var, sizeof(var))) > - return -EFAULT; > - break; > - case FBIO_CURSOR: > - ret = -EINVAL; > - break; > - case FBIOGET_CON2FBMAP: > - ret = fbcon_get_con2fb_map_ioctl(argp); > - break; > - case FBIOPUT_CON2FBMAP: > - ret = fbcon_set_con2fb_map_ioctl(argp); > - break; > - case FBIOBLANK: > - if (arg > FB_BLANK_POWERDOWN) > - return -EINVAL; > - console_lock(); > - lock_fb_info(info); > - ret = fb_blank(info, arg); > - /* might again call into fb_blank */ > - fbcon_fb_blanked(info, arg); > - unlock_fb_info(info); > - console_unlock(); > - break; > - default: > - lock_fb_info(info); > - fb = info->fbops; > - if (fb->fb_ioctl) > - ret = fb->fb_ioctl(info, cmd, arg); > - else > - ret = -ENOTTY; > - unlock_fb_info(info); > - } > - return ret; > -} > - > -static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) > -{ > - struct fb_info *info = file_fb_info(file); > - > - if (!info) > - return -ENODEV; > - return do_fb_ioctl(info, cmd, arg); > -} > - > -#ifdef CONFIG_COMPAT > -struct fb_fix_screeninfo32 { > - char id[16]; > - compat_caddr_t smem_start; > - u32 smem_len; > - u32 type; > - u32 type_aux; > - u32 visual; > - u16 xpanstep; > - u16 ypanstep; > - u16 ywrapstep; > - u32 line_length; > - compat_caddr_t mmio_start; > - u32 mmio_len; > - u32 accel; > - u16 reserved[3]; > -}; > - > -struct fb_cmap32 { > - u32 start; > - u32 len; > - compat_caddr_t red; > - compat_caddr_t green; > - compat_caddr_t blue; > - compat_caddr_t transp; > -}; > - > -static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, > - unsigned long arg) > -{ > - struct fb_cmap32 cmap32; > - struct fb_cmap cmap_from; > - struct fb_cmap_user cmap; > - > - if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) > - return -EFAULT; > - > - cmap = (struct fb_cmap_user) { > - .start = cmap32.start, > - .len = cmap32.len, > - .red = compat_ptr(cmap32.red), > - .green = compat_ptr(cmap32.green), > - .blue = compat_ptr(cmap32.blue), > - .transp = compat_ptr(cmap32.transp), > - }; > - > - if (cmd == FBIOPUTCMAP) > - return fb_set_user_cmap(&cmap, info); > - > - lock_fb_info(info); > - cmap_from = info->cmap; > - unlock_fb_info(info); > - > - return fb_cmap_to_user(&cmap_from, &cmap); > -} > - > -static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, > - struct fb_fix_screeninfo32 __user *fix32) > -{ > - __u32 data; > - int err; > - > - err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); > - > - data = (__u32) (unsigned long) fix->smem_start; > - err |= put_user(data, &fix32->smem_start); > - > - err |= put_user(fix->smem_len, &fix32->smem_len); > - err |= put_user(fix->type, &fix32->type); > - err |= put_user(fix->type_aux, &fix32->type_aux); > - err |= put_user(fix->visual, &fix32->visual); > - err |= put_user(fix->xpanstep, &fix32->xpanstep); > - err |= put_user(fix->ypanstep, &fix32->ypanstep); > - err |= put_user(fix->ywrapstep, &fix32->ywrapstep); > - err |= put_user(fix->line_length, &fix32->line_length); > - > - data = (__u32) (unsigned long) fix->mmio_start; > - err |= put_user(data, &fix32->mmio_start); > - > - err |= put_user(fix->mmio_len, &fix32->mmio_len); > - err |= put_user(fix->accel, &fix32->accel); > - err |= copy_to_user(fix32->reserved, fix->reserved, > - sizeof(fix->reserved)); > - > - if (err) > - return -EFAULT; > - return 0; > -} > - > -static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, > - unsigned long arg) > -{ > - struct fb_fix_screeninfo fix; > - > - lock_fb_info(info); > - fix = info->fix; > - if (info->flags & FBINFO_HIDE_SMEM_START) > - fix.smem_start = 0; > - unlock_fb_info(info); > - return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); > -} > - > -static long fb_compat_ioctl(struct file *file, unsigned int cmd, > - unsigned long arg) > -{ > - struct fb_info *info = file_fb_info(file); > - const struct fb_ops *fb; > - long ret = -ENOIOCTLCMD; > - > - if (!info) > - return -ENODEV; > - fb = info->fbops; > - switch(cmd) { > - case FBIOGET_VSCREENINFO: > - case FBIOPUT_VSCREENINFO: > - case FBIOPAN_DISPLAY: > - case FBIOGET_CON2FBMAP: > - case FBIOPUT_CON2FBMAP: > - arg = (unsigned long) compat_ptr(arg); > - fallthrough; > - case FBIOBLANK: > - ret = do_fb_ioctl(info, cmd, arg); > - break; > - > - case FBIOGET_FSCREENINFO: > - ret = fb_get_fscreeninfo(info, cmd, arg); > - break; > - > - case FBIOGETCMAP: > - case FBIOPUTCMAP: > - ret = fb_getput_cmap(info, cmd, arg); > - break; > - > - default: > - if (fb->fb_compat_ioctl) > - ret = fb->fb_compat_ioctl(info, cmd, arg); > - break; > - } > - return ret; > -} > -#endif > - > -static int > -fb_mmap(struct file *file, struct vm_area_struct * vma) > -{ > - struct fb_info *info = file_fb_info(file); > - unsigned long mmio_pgoff; > - unsigned long start; > - u32 len; > - > - if (!info) > - return -ENODEV; > - mutex_lock(&info->mm_lock); > - > - if (info->fbops->fb_mmap) { > - int res; > - > - /* > - * The framebuffer needs to be accessed decrypted, be sure > - * SME protection is removed ahead of the call > - */ > - vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); > - res = info->fbops->fb_mmap(info, vma); > - mutex_unlock(&info->mm_lock); > - return res; > -#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) > - } else if (info->fbdefio) { > - /* > - * FB deferred I/O wants you to handle mmap in your drivers. At a > - * minimum, point struct fb_ops.fb_mmap to fb_deferred_io_mmap(). > - */ > - dev_warn_once(info->dev, "fbdev mmap not set up for deferred I/O.\n"); > - mutex_unlock(&info->mm_lock); > - return -ENODEV; > -#endif > - } > - > - /* > - * Ugh. This can be either the frame buffer mapping, or > - * if pgoff points past it, the mmio mapping. > - */ > - start = info->fix.smem_start; > - len = info->fix.smem_len; > - mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT; > - if (vma->vm_pgoff >= mmio_pgoff) { > - if (info->var.accel_flags) { > - mutex_unlock(&info->mm_lock); > - return -EINVAL; > - } > - > - vma->vm_pgoff -= mmio_pgoff; > - start = info->fix.mmio_start; > - len = info->fix.mmio_len; > - } > - mutex_unlock(&info->mm_lock); > - > - vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); > - fb_pgprotect(file, vma, start); > - > - return vm_iomap_memory(vma, start, len); > -} > - > -static int > -fb_open(struct inode *inode, struct file *file) > -__acquires(&info->lock) > -__releases(&info->lock) > -{ > - int fbidx = iminor(inode); > - struct fb_info *info; > - int res = 0; > - > - info = get_fb_info(fbidx); > - if (!info) { > - request_module("fb%d", fbidx); > - info = get_fb_info(fbidx); > - if (!info) > - return -ENODEV; > - } > - if (IS_ERR(info)) > - return PTR_ERR(info); > - > - lock_fb_info(info); > - if (!try_module_get(info->fbops->owner)) { > - res = -ENODEV; > - goto out; > - } > - file->private_data = info; > - if (info->fbops->fb_open) { > - res = info->fbops->fb_open(info,1); > - if (res) > - module_put(info->fbops->owner); > - } > -#ifdef CONFIG_FB_DEFERRED_IO > - if (info->fbdefio) > - fb_deferred_io_open(info, inode, file); > -#endif > -out: > - unlock_fb_info(info); > - if (res) > - put_fb_info(info); > - return res; > -} > - > -static int > -fb_release(struct inode *inode, struct file *file) > -__acquires(&info->lock) > -__releases(&info->lock) > -{ > - struct fb_info * const info = file->private_data; > - > - lock_fb_info(info); > -#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) > - if (info->fbdefio) > - fb_deferred_io_release(info); > -#endif > - if (info->fbops->fb_release) > - info->fbops->fb_release(info,1); > - module_put(info->fbops->owner); > - unlock_fb_info(info); > - put_fb_info(info); > - return 0; > -} > - > -#if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU) > -static unsigned long get_fb_unmapped_area(struct file *filp, > - unsigned long addr, unsigned long len, > - unsigned long pgoff, unsigned long flags) > -{ > - struct fb_info * const info = filp->private_data; > - unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len); > - > - if (pgoff > fb_size || len > fb_size - pgoff) > - return -EINVAL; > - > - return (unsigned long)info->screen_base + pgoff; > -} > -#endif > - > -static const struct file_operations fb_fops = { > - .owner = THIS_MODULE, > - .read = fb_read, > - .write = fb_write, > - .unlocked_ioctl = fb_ioctl, > -#ifdef CONFIG_COMPAT > - .compat_ioctl = fb_compat_ioctl, > -#endif > - .mmap = fb_mmap, > - .open = fb_open, > - .release = fb_release, > -#if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \ > - (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \ > - !defined(CONFIG_MMU)) > - .get_unmapped_area = get_fb_unmapped_area, > -#endif > -#ifdef CONFIG_FB_DEFERRED_IO > - .fsync = fb_deferred_io_fsync, > -#endif > - .llseek = default_llseek, > -}; > - > struct class *fb_class; > EXPORT_SYMBOL(fb_class); > > @@ -1588,11 +1124,9 @@ fbmem_init(void) > if (ret) > return ret; > > - ret = register_chrdev(FB_MAJOR, "fb", &fb_fops); > - if (ret) { > - printk("unable to get major %d for fb devs\n", FB_MAJOR); > + ret = fb_register_chrdev(); > + if (ret) > goto err_chrdev; > - } > > fb_class = class_create("graphics"); > if (IS_ERR(fb_class)) { > @@ -1607,7 +1141,7 @@ fbmem_init(void) > return 0; > > err_class: > - unregister_chrdev(FB_MAJOR, "fb"); > + fb_unregister_chrdev(); > err_chrdev: > fb_cleanup_procfs(); > return ret; > @@ -1622,7 +1156,7 @@ fbmem_exit(void) > > fb_cleanup_procfs(); > class_destroy(fb_class); > - unregister_chrdev(FB_MAJOR, "fb"); > + fb_unregister_chrdev(); > } > > module_exit(fbmem_exit); > -- > 2.40.1
Thomas Zimmermann <tzimmermann@suse.de> writes: > Move fbdev's file-I/O code into a separate file and contain it in > init and cleanup helpers. No functional changes. > > Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> > --- [...] > + > +#include <linux/console.h> #include <linux/compat.h> here, the robot complained about: drivers/video/fbdev/core/fb_devfs.c:183:9: error: unknown type name 'compat_caddr_t'
Hi Am 07.06.23 um 22:48 schrieb Sam Ravnborg: > Hi Thomas. > > On Mon, Jun 05, 2023 at 04:48:10PM +0200, Thomas Zimmermann wrote: >> Move fbdev's file-I/O code into a separate file and contain it in >> init and cleanup helpers. No functional changes. >> >> Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> > Consider moving the two helps as noted below. > With or without this move: > Reviewed-by: Sam Ravnborg <sam@ravnborg.org> > >> --- >> drivers/video/fbdev/core/Makefile | 1 + >> drivers/video/fbdev/core/fb_devfs.c | 484 +++++++++++++++++++++++++ >> drivers/video/fbdev/core/fb_internal.h | 6 + >> drivers/video/fbdev/core/fbmem.c | 478 +----------------------- >> 4 files changed, 497 insertions(+), 472 deletions(-) >> create mode 100644 drivers/video/fbdev/core/fb_devfs.c >> >> diff --git a/drivers/video/fbdev/core/Makefile b/drivers/video/fbdev/core/Makefile >> index 665320160f70..125d24f50c36 100644 >> --- a/drivers/video/fbdev/core/Makefile >> +++ b/drivers/video/fbdev/core/Makefile >> @@ -2,6 +2,7 @@ >> obj-$(CONFIG_FB_NOTIFY) += fb_notify.o >> obj-$(CONFIG_FB) += fb.o >> fb-y := fb_backlight.o \ >> + fb_devfs.o \ >> fb_info.o \ >> fb_procfs.o \ >> fbmem.o fbmon.o fbcmap.o fbsysfs.o \ >> diff --git a/drivers/video/fbdev/core/fb_devfs.c b/drivers/video/fbdev/core/fb_devfs.c >> new file mode 100644 >> index 000000000000..5ab16cb24524 >> --- /dev/null >> +++ b/drivers/video/fbdev/core/fb_devfs.c > devfs gives me another understanding of what this file is used for. > fb_ioctl.c? I think fb_chrdev.c would be appropriate. Best regards Thomas > >> @@ -0,0 +1,484 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> + >> +#include <linux/console.h> >> +#include <linux/fb.h> >> +#include <linux/fbcon.h> >> +#include <linux/major.h> >> + >> +#include "fb_internal.h" >> + >> +/* >> + * We hold a reference to the fb_info in file->private_data, >> + * but if the current registered fb has changed, we don't >> + * actually want to use it. >> + * >> + * So look up the fb_info using the inode minor number, >> + * and just verify it against the reference we have. >> + */ >> +static struct fb_info *file_fb_info(struct file *file) >> +{ >> + struct inode *inode = file_inode(file); >> + int fbidx = iminor(inode); >> + struct fb_info *info = registered_fb[fbidx]; >> + >> + if (info != file->private_data) >> + info = NULL; >> + return info; >> +} >> + >> +static ssize_t fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) >> +{ >> + struct fb_info *info = file_fb_info(file); >> + >> + if (!info) >> + return -ENODEV; >> + >> + if (info->state != FBINFO_STATE_RUNNING) >> + return -EPERM; >> + >> + if (info->fbops->fb_read) >> + return info->fbops->fb_read(info, buf, count, ppos); >> + >> + return fb_io_read(info, buf, count, ppos); >> +} >> + >> +static ssize_t fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) >> +{ >> + struct fb_info *info = file_fb_info(file); >> + >> + if (!info) >> + return -ENODEV; >> + >> + if (info->state != FBINFO_STATE_RUNNING) >> + return -EPERM; >> + >> + if (info->fbops->fb_write) >> + return info->fbops->fb_write(info, buf, count, ppos); >> + >> + return fb_io_write(info, buf, count, ppos); >> +} >> + >> +static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, >> + unsigned long arg) >> +{ >> + const struct fb_ops *fb; >> + struct fb_var_screeninfo var; >> + struct fb_fix_screeninfo fix; >> + struct fb_cmap cmap_from; >> + struct fb_cmap_user cmap; >> + void __user *argp = (void __user *)arg; >> + long ret = 0; >> + >> + switch (cmd) { >> + case FBIOGET_VSCREENINFO: >> + lock_fb_info(info); >> + var = info->var; >> + unlock_fb_info(info); >> + >> + ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0; >> + break; >> + case FBIOPUT_VSCREENINFO: >> + if (copy_from_user(&var, argp, sizeof(var))) >> + return -EFAULT; >> + /* only for kernel-internal use */ >> + var.activate &= ~FB_ACTIVATE_KD_TEXT; >> + console_lock(); >> + lock_fb_info(info); >> + ret = fbcon_modechange_possible(info, &var); >> + if (!ret) >> + ret = fb_set_var(info, &var); >> + if (!ret) >> + fbcon_update_vcs(info, var.activate & FB_ACTIVATE_ALL); >> + unlock_fb_info(info); >> + console_unlock(); >> + if (!ret && copy_to_user(argp, &var, sizeof(var))) >> + ret = -EFAULT; >> + break; >> + case FBIOGET_FSCREENINFO: >> + lock_fb_info(info); >> + memcpy(&fix, &info->fix, sizeof(fix)); >> + if (info->flags & FBINFO_HIDE_SMEM_START) >> + fix.smem_start = 0; >> + unlock_fb_info(info); >> + >> + ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0; >> + break; >> + case FBIOPUTCMAP: >> + if (copy_from_user(&cmap, argp, sizeof(cmap))) >> + return -EFAULT; >> + ret = fb_set_user_cmap(&cmap, info); >> + break; >> + case FBIOGETCMAP: >> + if (copy_from_user(&cmap, argp, sizeof(cmap))) >> + return -EFAULT; >> + lock_fb_info(info); >> + cmap_from = info->cmap; >> + unlock_fb_info(info); >> + ret = fb_cmap_to_user(&cmap_from, &cmap); >> + break; >> + case FBIOPAN_DISPLAY: >> + if (copy_from_user(&var, argp, sizeof(var))) >> + return -EFAULT; >> + console_lock(); >> + lock_fb_info(info); >> + ret = fb_pan_display(info, &var); >> + unlock_fb_info(info); >> + console_unlock(); >> + if (ret == 0 && copy_to_user(argp, &var, sizeof(var))) >> + return -EFAULT; >> + break; >> + case FBIO_CURSOR: >> + ret = -EINVAL; >> + break; >> + case FBIOGET_CON2FBMAP: >> + ret = fbcon_get_con2fb_map_ioctl(argp); >> + break; >> + case FBIOPUT_CON2FBMAP: >> + ret = fbcon_set_con2fb_map_ioctl(argp); >> + break; >> + case FBIOBLANK: >> + if (arg > FB_BLANK_POWERDOWN) >> + return -EINVAL; >> + console_lock(); >> + lock_fb_info(info); >> + ret = fb_blank(info, arg); >> + /* might again call into fb_blank */ >> + fbcon_fb_blanked(info, arg); >> + unlock_fb_info(info); >> + console_unlock(); >> + break; >> + default: >> + lock_fb_info(info); >> + fb = info->fbops; >> + if (fb->fb_ioctl) >> + ret = fb->fb_ioctl(info, cmd, arg); >> + else >> + ret = -ENOTTY; >> + unlock_fb_info(info); >> + } >> + return ret; >> +} >> + >> +static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) >> +{ >> + struct fb_info *info = file_fb_info(file); >> + >> + if (!info) >> + return -ENODEV; >> + return do_fb_ioctl(info, cmd, arg); >> +} >> + >> +#ifdef CONFIG_COMPAT >> +struct fb_fix_screeninfo32 { >> + char id[16]; >> + compat_caddr_t smem_start; >> + u32 smem_len; >> + u32 type; >> + u32 type_aux; >> + u32 visual; >> + u16 xpanstep; >> + u16 ypanstep; >> + u16 ywrapstep; >> + u32 line_length; >> + compat_caddr_t mmio_start; >> + u32 mmio_len; >> + u32 accel; >> + u16 reserved[3]; >> +}; >> + >> +struct fb_cmap32 { >> + u32 start; >> + u32 len; >> + compat_caddr_t red; >> + compat_caddr_t green; >> + compat_caddr_t blue; >> + compat_caddr_t transp; >> +}; >> + >> +static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, >> + unsigned long arg) >> +{ >> + struct fb_cmap32 cmap32; >> + struct fb_cmap cmap_from; >> + struct fb_cmap_user cmap; >> + >> + if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) >> + return -EFAULT; >> + >> + cmap = (struct fb_cmap_user) { >> + .start = cmap32.start, >> + .len = cmap32.len, >> + .red = compat_ptr(cmap32.red), >> + .green = compat_ptr(cmap32.green), >> + .blue = compat_ptr(cmap32.blue), >> + .transp = compat_ptr(cmap32.transp), >> + }; >> + >> + if (cmd == FBIOPUTCMAP) >> + return fb_set_user_cmap(&cmap, info); >> + >> + lock_fb_info(info); >> + cmap_from = info->cmap; >> + unlock_fb_info(info); >> + >> + return fb_cmap_to_user(&cmap_from, &cmap); >> +} >> + >> +static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, >> + struct fb_fix_screeninfo32 __user *fix32) >> +{ >> + __u32 data; >> + int err; >> + >> + err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); >> + >> + data = (__u32) (unsigned long) fix->smem_start; >> + err |= put_user(data, &fix32->smem_start); >> + >> + err |= put_user(fix->smem_len, &fix32->smem_len); >> + err |= put_user(fix->type, &fix32->type); >> + err |= put_user(fix->type_aux, &fix32->type_aux); >> + err |= put_user(fix->visual, &fix32->visual); >> + err |= put_user(fix->xpanstep, &fix32->xpanstep); >> + err |= put_user(fix->ypanstep, &fix32->ypanstep); >> + err |= put_user(fix->ywrapstep, &fix32->ywrapstep); >> + err |= put_user(fix->line_length, &fix32->line_length); >> + >> + data = (__u32) (unsigned long) fix->mmio_start; >> + err |= put_user(data, &fix32->mmio_start); >> + >> + err |= put_user(fix->mmio_len, &fix32->mmio_len); >> + err |= put_user(fix->accel, &fix32->accel); >> + err |= copy_to_user(fix32->reserved, fix->reserved, >> + sizeof(fix->reserved)); >> + >> + if (err) >> + return -EFAULT; >> + return 0; >> +} >> + >> +static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, >> + unsigned long arg) >> +{ >> + struct fb_fix_screeninfo fix; >> + >> + lock_fb_info(info); >> + fix = info->fix; >> + if (info->flags & FBINFO_HIDE_SMEM_START) >> + fix.smem_start = 0; >> + unlock_fb_info(info); >> + return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); >> +} >> + >> +static long fb_compat_ioctl(struct file *file, unsigned int cmd, >> + unsigned long arg) >> +{ >> + struct fb_info *info = file_fb_info(file); >> + const struct fb_ops *fb; >> + long ret = -ENOIOCTLCMD; >> + >> + if (!info) >> + return -ENODEV; >> + fb = info->fbops; >> + switch (cmd) { >> + case FBIOGET_VSCREENINFO: >> + case FBIOPUT_VSCREENINFO: >> + case FBIOPAN_DISPLAY: >> + case FBIOGET_CON2FBMAP: >> + case FBIOPUT_CON2FBMAP: >> + arg = (unsigned long) compat_ptr(arg); >> + fallthrough; >> + case FBIOBLANK: >> + ret = do_fb_ioctl(info, cmd, arg); >> + break; >> + >> + case FBIOGET_FSCREENINFO: >> + ret = fb_get_fscreeninfo(info, cmd, arg); >> + break; >> + >> + case FBIOGETCMAP: >> + case FBIOPUTCMAP: >> + ret = fb_getput_cmap(info, cmd, arg); >> + break; >> + >> + default: >> + if (fb->fb_compat_ioctl) >> + ret = fb->fb_compat_ioctl(info, cmd, arg); >> + break; >> + } >> + return ret; >> +} >> +#endif >> + >> +static int fb_mmap(struct file *file, struct vm_area_struct *vma) >> +{ >> + struct fb_info *info = file_fb_info(file); >> + unsigned long mmio_pgoff; >> + unsigned long start; >> + u32 len; >> + >> + if (!info) >> + return -ENODEV; >> + mutex_lock(&info->mm_lock); >> + >> + if (info->fbops->fb_mmap) { >> + int res; >> + >> + /* >> + * The framebuffer needs to be accessed decrypted, be sure >> + * SME protection is removed ahead of the call >> + */ >> + vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); >> + res = info->fbops->fb_mmap(info, vma); >> + mutex_unlock(&info->mm_lock); >> + return res; >> +#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) >> + } else if (info->fbdefio) { >> + /* >> + * FB deferred I/O wants you to handle mmap in your drivers. At a >> + * minimum, point struct fb_ops.fb_mmap to fb_deferred_io_mmap(). >> + */ >> + dev_warn_once(info->dev, "fbdev mmap not set up for deferred I/O.\n"); >> + mutex_unlock(&info->mm_lock); >> + return -ENODEV; >> +#endif >> + } >> + >> + /* >> + * Ugh. This can be either the frame buffer mapping, or >> + * if pgoff points past it, the mmio mapping. >> + */ >> + start = info->fix.smem_start; >> + len = info->fix.smem_len; >> + mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT; >> + if (vma->vm_pgoff >= mmio_pgoff) { >> + if (info->var.accel_flags) { >> + mutex_unlock(&info->mm_lock); >> + return -EINVAL; >> + } >> + >> + vma->vm_pgoff -= mmio_pgoff; >> + start = info->fix.mmio_start; >> + len = info->fix.mmio_len; >> + } >> + mutex_unlock(&info->mm_lock); >> + >> + vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); >> + fb_pgprotect(file, vma, start); >> + >> + return vm_iomap_memory(vma, start, len); >> +} >> + >> +static int fb_open(struct inode *inode, struct file *file) >> +__acquires(&info->lock) >> +__releases(&info->lock) >> +{ >> + int fbidx = iminor(inode); >> + struct fb_info *info; >> + int res = 0; >> + >> + info = get_fb_info(fbidx); >> + if (!info) { >> + request_module("fb%d", fbidx); >> + info = get_fb_info(fbidx); >> + if (!info) >> + return -ENODEV; >> + } >> + if (IS_ERR(info)) >> + return PTR_ERR(info); >> + >> + lock_fb_info(info); >> + if (!try_module_get(info->fbops->owner)) { >> + res = -ENODEV; >> + goto out; >> + } >> + file->private_data = info; >> + if (info->fbops->fb_open) { >> + res = info->fbops->fb_open(info, 1); >> + if (res) >> + module_put(info->fbops->owner); >> + } >> +#ifdef CONFIG_FB_DEFERRED_IO >> + if (info->fbdefio) >> + fb_deferred_io_open(info, inode, file); >> +#endif >> +out: >> + unlock_fb_info(info); >> + if (res) >> + put_fb_info(info); >> + return res; >> +} >> + >> +static int fb_release(struct inode *inode, struct file *file) >> +__acquires(&info->lock) >> +__releases(&info->lock) >> +{ >> + struct fb_info * const info = file->private_data; >> + >> + lock_fb_info(info); >> +#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) >> + if (info->fbdefio) >> + fb_deferred_io_release(info); >> +#endif >> + if (info->fbops->fb_release) >> + info->fbops->fb_release(info, 1); >> + module_put(info->fbops->owner); >> + unlock_fb_info(info); >> + put_fb_info(info); >> + return 0; >> +} >> + >> +#if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU) >> +static unsigned long get_fb_unmapped_area(struct file *filp, >> + unsigned long addr, unsigned long len, >> + unsigned long pgoff, unsigned long flags) >> +{ >> + struct fb_info * const info = filp->private_data; >> + unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len); >> + >> + if (pgoff > fb_size || len > fb_size - pgoff) >> + return -EINVAL; >> + >> + return (unsigned long)info->screen_base + pgoff; >> +} >> +#endif >> + >> +static const struct file_operations fb_fops = { >> + .owner = THIS_MODULE, >> + .read = fb_read, >> + .write = fb_write, >> + .unlocked_ioctl = fb_ioctl, >> +#ifdef CONFIG_COMPAT >> + .compat_ioctl = fb_compat_ioctl, >> +#endif >> + .mmap = fb_mmap, >> + .open = fb_open, >> + .release = fb_release, >> +#if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \ >> + (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \ >> + !defined(CONFIG_MMU)) >> + .get_unmapped_area = get_fb_unmapped_area, >> +#endif >> +#ifdef CONFIG_FB_DEFERRED_IO >> + .fsync = fb_deferred_io_fsync, >> +#endif >> + .llseek = default_llseek, >> +}; >> + >> +int fb_register_chrdev(void) >> +{ >> + int ret; >> + >> + ret = register_chrdev(FB_MAJOR, "fb", &fb_fops); >> + if (ret) { >> + pr_err("Unable to get major %d for fb devs\n", FB_MAJOR); >> + return ret; >> + } >> + >> + return ret; >> +} >> + >> +void fb_unregister_chrdev(void) >> +{ >> + unregister_chrdev(FB_MAJOR, "fb"); >> +} >> diff --git a/drivers/video/fbdev/core/fb_internal.h b/drivers/video/fbdev/core/fb_internal.h >> index 51f7c9f04e45..abe06c9da36e 100644 >> --- a/drivers/video/fbdev/core/fb_internal.h >> +++ b/drivers/video/fbdev/core/fb_internal.h >> @@ -6,10 +6,16 @@ >> #include <linux/fb.h> >> #include <linux/mutex.h> >> >> +/* fb_devfs.c */ >> +int fb_register_chrdev(void); >> +void fb_unregister_chrdev(void); >> + >> /* fbmem.c */ >> extern struct mutex registration_lock; >> extern struct fb_info *registered_fb[FB_MAX]; >> extern int num_registered_fb; >> +struct fb_info *get_fb_info(unsigned int idx); >> +void put_fb_info(struct fb_info *fb_info); > The only users of get_fb_info() and put_fb_info() are now in fb_devfs. > So consider moving these two helpers too. > >> >> /* fb_procfs.c */ >> int fb_init_procfs(void); >> diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c >> index de1e45240161..2d26ac46337b 100644 >> --- a/drivers/video/fbdev/core/fbmem.c >> +++ b/drivers/video/fbdev/core/fbmem.c >> @@ -17,7 +17,6 @@ >> #include <linux/types.h> >> #include <linux/errno.h> >> #include <linux/kernel.h> >> -#include <linux/major.h> >> #include <linux/slab.h> >> #include <linux/mm.h> >> #include <linux/mman.h> >> @@ -54,7 +53,7 @@ bool fb_center_logo __read_mostly; >> >> int fb_logo_count __read_mostly = -1; >> >> -static struct fb_info *get_fb_info(unsigned int idx) >> +struct fb_info *get_fb_info(unsigned int idx) >> { >> struct fb_info *fb_info; >> >> @@ -70,7 +69,7 @@ static struct fb_info *get_fb_info(unsigned int idx) >> return fb_info; >> } >> >> -static void put_fb_info(struct fb_info *fb_info) >> +void put_fb_info(struct fb_info *fb_info) >> { >> if (!refcount_dec_and_test(&fb_info->count)) >> return; >> @@ -699,59 +698,6 @@ int fb_show_logo(struct fb_info *info, int rotate) { return 0; } >> EXPORT_SYMBOL(fb_prepare_logo); >> EXPORT_SYMBOL(fb_show_logo); > > Reminds me - consider moving logo stuff to a fb_logo file. > This would reduce fbmem with a lot of lines, and it is separate. > But it is outside the goal of this patchset. > >> >> -/* >> - * We hold a reference to the fb_info in file->private_data, >> - * but if the current registered fb has changed, we don't >> - * actually want to use it. >> - * >> - * So look up the fb_info using the inode minor number, >> - * and just verify it against the reference we have. >> - */ >> -static struct fb_info *file_fb_info(struct file *file) >> -{ >> - struct inode *inode = file_inode(file); >> - int fbidx = iminor(inode); >> - struct fb_info *info = registered_fb[fbidx]; >> - >> - if (info != file->private_data) >> - info = NULL; >> - return info; >> -} >> - >> -static ssize_t >> -fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) >> -{ >> - struct fb_info *info = file_fb_info(file); >> - >> - if (!info) >> - return -ENODEV; >> - >> - if (info->state != FBINFO_STATE_RUNNING) >> - return -EPERM; >> - >> - if (info->fbops->fb_read) >> - return info->fbops->fb_read(info, buf, count, ppos); >> - >> - return fb_io_read(info, buf, count, ppos); >> -} >> - >> -static ssize_t >> -fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) >> -{ >> - struct fb_info *info = file_fb_info(file); >> - >> - if (!info) >> - return -ENODEV; >> - >> - if (info->state != FBINFO_STATE_RUNNING) >> - return -EPERM; >> - >> - if (info->fbops->fb_write) >> - return info->fbops->fb_write(info, buf, count, ppos); >> - >> - return fb_io_write(info, buf, count, ppos); >> -} >> - >> int >> fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var) >> { >> @@ -951,416 +897,6 @@ fb_blank(struct fb_info *info, int blank) >> } >> EXPORT_SYMBOL(fb_blank); >> >> -static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, >> - unsigned long arg) >> -{ >> - const struct fb_ops *fb; >> - struct fb_var_screeninfo var; >> - struct fb_fix_screeninfo fix; >> - struct fb_cmap cmap_from; >> - struct fb_cmap_user cmap; >> - void __user *argp = (void __user *)arg; >> - long ret = 0; >> - >> - switch (cmd) { >> - case FBIOGET_VSCREENINFO: >> - lock_fb_info(info); >> - var = info->var; >> - unlock_fb_info(info); >> - >> - ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0; >> - break; >> - case FBIOPUT_VSCREENINFO: >> - if (copy_from_user(&var, argp, sizeof(var))) >> - return -EFAULT; >> - /* only for kernel-internal use */ >> - var.activate &= ~FB_ACTIVATE_KD_TEXT; >> - console_lock(); >> - lock_fb_info(info); >> - ret = fbcon_modechange_possible(info, &var); >> - if (!ret) >> - ret = fb_set_var(info, &var); >> - if (!ret) >> - fbcon_update_vcs(info, var.activate & FB_ACTIVATE_ALL); >> - unlock_fb_info(info); >> - console_unlock(); >> - if (!ret && copy_to_user(argp, &var, sizeof(var))) >> - ret = -EFAULT; >> - break; >> - case FBIOGET_FSCREENINFO: >> - lock_fb_info(info); >> - memcpy(&fix, &info->fix, sizeof(fix)); >> - if (info->flags & FBINFO_HIDE_SMEM_START) >> - fix.smem_start = 0; >> - unlock_fb_info(info); >> - >> - ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0; >> - break; >> - case FBIOPUTCMAP: >> - if (copy_from_user(&cmap, argp, sizeof(cmap))) >> - return -EFAULT; >> - ret = fb_set_user_cmap(&cmap, info); >> - break; >> - case FBIOGETCMAP: >> - if (copy_from_user(&cmap, argp, sizeof(cmap))) >> - return -EFAULT; >> - lock_fb_info(info); >> - cmap_from = info->cmap; >> - unlock_fb_info(info); >> - ret = fb_cmap_to_user(&cmap_from, &cmap); >> - break; >> - case FBIOPAN_DISPLAY: >> - if (copy_from_user(&var, argp, sizeof(var))) >> - return -EFAULT; >> - console_lock(); >> - lock_fb_info(info); >> - ret = fb_pan_display(info, &var); >> - unlock_fb_info(info); >> - console_unlock(); >> - if (ret == 0 && copy_to_user(argp, &var, sizeof(var))) >> - return -EFAULT; >> - break; >> - case FBIO_CURSOR: >> - ret = -EINVAL; >> - break; >> - case FBIOGET_CON2FBMAP: >> - ret = fbcon_get_con2fb_map_ioctl(argp); >> - break; >> - case FBIOPUT_CON2FBMAP: >> - ret = fbcon_set_con2fb_map_ioctl(argp); >> - break; >> - case FBIOBLANK: >> - if (arg > FB_BLANK_POWERDOWN) >> - return -EINVAL; >> - console_lock(); >> - lock_fb_info(info); >> - ret = fb_blank(info, arg); >> - /* might again call into fb_blank */ >> - fbcon_fb_blanked(info, arg); >> - unlock_fb_info(info); >> - console_unlock(); >> - break; >> - default: >> - lock_fb_info(info); >> - fb = info->fbops; >> - if (fb->fb_ioctl) >> - ret = fb->fb_ioctl(info, cmd, arg); >> - else >> - ret = -ENOTTY; >> - unlock_fb_info(info); >> - } >> - return ret; >> -} >> - >> -static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) >> -{ >> - struct fb_info *info = file_fb_info(file); >> - >> - if (!info) >> - return -ENODEV; >> - return do_fb_ioctl(info, cmd, arg); >> -} >> - >> -#ifdef CONFIG_COMPAT >> -struct fb_fix_screeninfo32 { >> - char id[16]; >> - compat_caddr_t smem_start; >> - u32 smem_len; >> - u32 type; >> - u32 type_aux; >> - u32 visual; >> - u16 xpanstep; >> - u16 ypanstep; >> - u16 ywrapstep; >> - u32 line_length; >> - compat_caddr_t mmio_start; >> - u32 mmio_len; >> - u32 accel; >> - u16 reserved[3]; >> -}; >> - >> -struct fb_cmap32 { >> - u32 start; >> - u32 len; >> - compat_caddr_t red; >> - compat_caddr_t green; >> - compat_caddr_t blue; >> - compat_caddr_t transp; >> -}; >> - >> -static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, >> - unsigned long arg) >> -{ >> - struct fb_cmap32 cmap32; >> - struct fb_cmap cmap_from; >> - struct fb_cmap_user cmap; >> - >> - if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) >> - return -EFAULT; >> - >> - cmap = (struct fb_cmap_user) { >> - .start = cmap32.start, >> - .len = cmap32.len, >> - .red = compat_ptr(cmap32.red), >> - .green = compat_ptr(cmap32.green), >> - .blue = compat_ptr(cmap32.blue), >> - .transp = compat_ptr(cmap32.transp), >> - }; >> - >> - if (cmd == FBIOPUTCMAP) >> - return fb_set_user_cmap(&cmap, info); >> - >> - lock_fb_info(info); >> - cmap_from = info->cmap; >> - unlock_fb_info(info); >> - >> - return fb_cmap_to_user(&cmap_from, &cmap); >> -} >> - >> -static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, >> - struct fb_fix_screeninfo32 __user *fix32) >> -{ >> - __u32 data; >> - int err; >> - >> - err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); >> - >> - data = (__u32) (unsigned long) fix->smem_start; >> - err |= put_user(data, &fix32->smem_start); >> - >> - err |= put_user(fix->smem_len, &fix32->smem_len); >> - err |= put_user(fix->type, &fix32->type); >> - err |= put_user(fix->type_aux, &fix32->type_aux); >> - err |= put_user(fix->visual, &fix32->visual); >> - err |= put_user(fix->xpanstep, &fix32->xpanstep); >> - err |= put_user(fix->ypanstep, &fix32->ypanstep); >> - err |= put_user(fix->ywrapstep, &fix32->ywrapstep); >> - err |= put_user(fix->line_length, &fix32->line_length); >> - >> - data = (__u32) (unsigned long) fix->mmio_start; >> - err |= put_user(data, &fix32->mmio_start); >> - >> - err |= put_user(fix->mmio_len, &fix32->mmio_len); >> - err |= put_user(fix->accel, &fix32->accel); >> - err |= copy_to_user(fix32->reserved, fix->reserved, >> - sizeof(fix->reserved)); >> - >> - if (err) >> - return -EFAULT; >> - return 0; >> -} >> - >> -static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, >> - unsigned long arg) >> -{ >> - struct fb_fix_screeninfo fix; >> - >> - lock_fb_info(info); >> - fix = info->fix; >> - if (info->flags & FBINFO_HIDE_SMEM_START) >> - fix.smem_start = 0; >> - unlock_fb_info(info); >> - return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); >> -} >> - >> -static long fb_compat_ioctl(struct file *file, unsigned int cmd, >> - unsigned long arg) >> -{ >> - struct fb_info *info = file_fb_info(file); >> - const struct fb_ops *fb; >> - long ret = -ENOIOCTLCMD; >> - >> - if (!info) >> - return -ENODEV; >> - fb = info->fbops; >> - switch(cmd) { >> - case FBIOGET_VSCREENINFO: >> - case FBIOPUT_VSCREENINFO: >> - case FBIOPAN_DISPLAY: >> - case FBIOGET_CON2FBMAP: >> - case FBIOPUT_CON2FBMAP: >> - arg = (unsigned long) compat_ptr(arg); >> - fallthrough; >> - case FBIOBLANK: >> - ret = do_fb_ioctl(info, cmd, arg); >> - break; >> - >> - case FBIOGET_FSCREENINFO: >> - ret = fb_get_fscreeninfo(info, cmd, arg); >> - break; >> - >> - case FBIOGETCMAP: >> - case FBIOPUTCMAP: >> - ret = fb_getput_cmap(info, cmd, arg); >> - break; >> - >> - default: >> - if (fb->fb_compat_ioctl) >> - ret = fb->fb_compat_ioctl(info, cmd, arg); >> - break; >> - } >> - return ret; >> -} >> -#endif >> - >> -static int >> -fb_mmap(struct file *file, struct vm_area_struct * vma) >> -{ >> - struct fb_info *info = file_fb_info(file); >> - unsigned long mmio_pgoff; >> - unsigned long start; >> - u32 len; >> - >> - if (!info) >> - return -ENODEV; >> - mutex_lock(&info->mm_lock); >> - >> - if (info->fbops->fb_mmap) { >> - int res; >> - >> - /* >> - * The framebuffer needs to be accessed decrypted, be sure >> - * SME protection is removed ahead of the call >> - */ >> - vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); >> - res = info->fbops->fb_mmap(info, vma); >> - mutex_unlock(&info->mm_lock); >> - return res; >> -#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) >> - } else if (info->fbdefio) { >> - /* >> - * FB deferred I/O wants you to handle mmap in your drivers. At a >> - * minimum, point struct fb_ops.fb_mmap to fb_deferred_io_mmap(). >> - */ >> - dev_warn_once(info->dev, "fbdev mmap not set up for deferred I/O.\n"); >> - mutex_unlock(&info->mm_lock); >> - return -ENODEV; >> -#endif >> - } >> - >> - /* >> - * Ugh. This can be either the frame buffer mapping, or >> - * if pgoff points past it, the mmio mapping. >> - */ >> - start = info->fix.smem_start; >> - len = info->fix.smem_len; >> - mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT; >> - if (vma->vm_pgoff >= mmio_pgoff) { >> - if (info->var.accel_flags) { >> - mutex_unlock(&info->mm_lock); >> - return -EINVAL; >> - } >> - >> - vma->vm_pgoff -= mmio_pgoff; >> - start = info->fix.mmio_start; >> - len = info->fix.mmio_len; >> - } >> - mutex_unlock(&info->mm_lock); >> - >> - vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); >> - fb_pgprotect(file, vma, start); >> - >> - return vm_iomap_memory(vma, start, len); >> -} >> - >> -static int >> -fb_open(struct inode *inode, struct file *file) >> -__acquires(&info->lock) >> -__releases(&info->lock) >> -{ >> - int fbidx = iminor(inode); >> - struct fb_info *info; >> - int res = 0; >> - >> - info = get_fb_info(fbidx); >> - if (!info) { >> - request_module("fb%d", fbidx); >> - info = get_fb_info(fbidx); >> - if (!info) >> - return -ENODEV; >> - } >> - if (IS_ERR(info)) >> - return PTR_ERR(info); >> - >> - lock_fb_info(info); >> - if (!try_module_get(info->fbops->owner)) { >> - res = -ENODEV; >> - goto out; >> - } >> - file->private_data = info; >> - if (info->fbops->fb_open) { >> - res = info->fbops->fb_open(info,1); >> - if (res) >> - module_put(info->fbops->owner); >> - } >> -#ifdef CONFIG_FB_DEFERRED_IO >> - if (info->fbdefio) >> - fb_deferred_io_open(info, inode, file); >> -#endif >> -out: >> - unlock_fb_info(info); >> - if (res) >> - put_fb_info(info); >> - return res; >> -} >> - >> -static int >> -fb_release(struct inode *inode, struct file *file) >> -__acquires(&info->lock) >> -__releases(&info->lock) >> -{ >> - struct fb_info * const info = file->private_data; >> - >> - lock_fb_info(info); >> -#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) >> - if (info->fbdefio) >> - fb_deferred_io_release(info); >> -#endif >> - if (info->fbops->fb_release) >> - info->fbops->fb_release(info,1); >> - module_put(info->fbops->owner); >> - unlock_fb_info(info); >> - put_fb_info(info); >> - return 0; >> -} >> - >> -#if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU) >> -static unsigned long get_fb_unmapped_area(struct file *filp, >> - unsigned long addr, unsigned long len, >> - unsigned long pgoff, unsigned long flags) >> -{ >> - struct fb_info * const info = filp->private_data; >> - unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len); >> - >> - if (pgoff > fb_size || len > fb_size - pgoff) >> - return -EINVAL; >> - >> - return (unsigned long)info->screen_base + pgoff; >> -} >> -#endif >> - >> -static const struct file_operations fb_fops = { >> - .owner = THIS_MODULE, >> - .read = fb_read, >> - .write = fb_write, >> - .unlocked_ioctl = fb_ioctl, >> -#ifdef CONFIG_COMPAT >> - .compat_ioctl = fb_compat_ioctl, >> -#endif >> - .mmap = fb_mmap, >> - .open = fb_open, >> - .release = fb_release, >> -#if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \ >> - (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \ >> - !defined(CONFIG_MMU)) >> - .get_unmapped_area = get_fb_unmapped_area, >> -#endif >> -#ifdef CONFIG_FB_DEFERRED_IO >> - .fsync = fb_deferred_io_fsync, >> -#endif >> - .llseek = default_llseek, >> -}; >> - >> struct class *fb_class; >> EXPORT_SYMBOL(fb_class); >> >> @@ -1588,11 +1124,9 @@ fbmem_init(void) >> if (ret) >> return ret; >> >> - ret = register_chrdev(FB_MAJOR, "fb", &fb_fops); >> - if (ret) { >> - printk("unable to get major %d for fb devs\n", FB_MAJOR); >> + ret = fb_register_chrdev(); >> + if (ret) >> goto err_chrdev; >> - } >> >> fb_class = class_create("graphics"); >> if (IS_ERR(fb_class)) { >> @@ -1607,7 +1141,7 @@ fbmem_init(void) >> return 0; >> >> err_class: >> - unregister_chrdev(FB_MAJOR, "fb"); >> + fb_unregister_chrdev(); >> err_chrdev: >> fb_cleanup_procfs(); >> return ret; >> @@ -1622,7 +1156,7 @@ fbmem_exit(void) >> >> fb_cleanup_procfs(); >> class_destroy(fb_class); >> - unregister_chrdev(FB_MAJOR, "fb"); >> + fb_unregister_chrdev(); >> } >> >> module_exit(fbmem_exit); >> -- >> 2.40.1
diff --git a/drivers/video/fbdev/core/Makefile b/drivers/video/fbdev/core/Makefile index 665320160f70..125d24f50c36 100644 --- a/drivers/video/fbdev/core/Makefile +++ b/drivers/video/fbdev/core/Makefile @@ -2,6 +2,7 @@ obj-$(CONFIG_FB_NOTIFY) += fb_notify.o obj-$(CONFIG_FB) += fb.o fb-y := fb_backlight.o \ + fb_devfs.o \ fb_info.o \ fb_procfs.o \ fbmem.o fbmon.o fbcmap.o fbsysfs.o \ diff --git a/drivers/video/fbdev/core/fb_devfs.c b/drivers/video/fbdev/core/fb_devfs.c new file mode 100644 index 000000000000..5ab16cb24524 --- /dev/null +++ b/drivers/video/fbdev/core/fb_devfs.c @@ -0,0 +1,484 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/console.h> +#include <linux/fb.h> +#include <linux/fbcon.h> +#include <linux/major.h> + +#include "fb_internal.h" + +/* + * We hold a reference to the fb_info in file->private_data, + * but if the current registered fb has changed, we don't + * actually want to use it. + * + * So look up the fb_info using the inode minor number, + * and just verify it against the reference we have. + */ +static struct fb_info *file_fb_info(struct file *file) +{ + struct inode *inode = file_inode(file); + int fbidx = iminor(inode); + struct fb_info *info = registered_fb[fbidx]; + + if (info != file->private_data) + info = NULL; + return info; +} + +static ssize_t fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) +{ + struct fb_info *info = file_fb_info(file); + + if (!info) + return -ENODEV; + + if (info->state != FBINFO_STATE_RUNNING) + return -EPERM; + + if (info->fbops->fb_read) + return info->fbops->fb_read(info, buf, count, ppos); + + return fb_io_read(info, buf, count, ppos); +} + +static ssize_t fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) +{ + struct fb_info *info = file_fb_info(file); + + if (!info) + return -ENODEV; + + if (info->state != FBINFO_STATE_RUNNING) + return -EPERM; + + if (info->fbops->fb_write) + return info->fbops->fb_write(info, buf, count, ppos); + + return fb_io_write(info, buf, count, ppos); +} + +static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, + unsigned long arg) +{ + const struct fb_ops *fb; + struct fb_var_screeninfo var; + struct fb_fix_screeninfo fix; + struct fb_cmap cmap_from; + struct fb_cmap_user cmap; + void __user *argp = (void __user *)arg; + long ret = 0; + + switch (cmd) { + case FBIOGET_VSCREENINFO: + lock_fb_info(info); + var = info->var; + unlock_fb_info(info); + + ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0; + break; + case FBIOPUT_VSCREENINFO: + if (copy_from_user(&var, argp, sizeof(var))) + return -EFAULT; + /* only for kernel-internal use */ + var.activate &= ~FB_ACTIVATE_KD_TEXT; + console_lock(); + lock_fb_info(info); + ret = fbcon_modechange_possible(info, &var); + if (!ret) + ret = fb_set_var(info, &var); + if (!ret) + fbcon_update_vcs(info, var.activate & FB_ACTIVATE_ALL); + unlock_fb_info(info); + console_unlock(); + if (!ret && copy_to_user(argp, &var, sizeof(var))) + ret = -EFAULT; + break; + case FBIOGET_FSCREENINFO: + lock_fb_info(info); + memcpy(&fix, &info->fix, sizeof(fix)); + if (info->flags & FBINFO_HIDE_SMEM_START) + fix.smem_start = 0; + unlock_fb_info(info); + + ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0; + break; + case FBIOPUTCMAP: + if (copy_from_user(&cmap, argp, sizeof(cmap))) + return -EFAULT; + ret = fb_set_user_cmap(&cmap, info); + break; + case FBIOGETCMAP: + if (copy_from_user(&cmap, argp, sizeof(cmap))) + return -EFAULT; + lock_fb_info(info); + cmap_from = info->cmap; + unlock_fb_info(info); + ret = fb_cmap_to_user(&cmap_from, &cmap); + break; + case FBIOPAN_DISPLAY: + if (copy_from_user(&var, argp, sizeof(var))) + return -EFAULT; + console_lock(); + lock_fb_info(info); + ret = fb_pan_display(info, &var); + unlock_fb_info(info); + console_unlock(); + if (ret == 0 && copy_to_user(argp, &var, sizeof(var))) + return -EFAULT; + break; + case FBIO_CURSOR: + ret = -EINVAL; + break; + case FBIOGET_CON2FBMAP: + ret = fbcon_get_con2fb_map_ioctl(argp); + break; + case FBIOPUT_CON2FBMAP: + ret = fbcon_set_con2fb_map_ioctl(argp); + break; + case FBIOBLANK: + if (arg > FB_BLANK_POWERDOWN) + return -EINVAL; + console_lock(); + lock_fb_info(info); + ret = fb_blank(info, arg); + /* might again call into fb_blank */ + fbcon_fb_blanked(info, arg); + unlock_fb_info(info); + console_unlock(); + break; + default: + lock_fb_info(info); + fb = info->fbops; + if (fb->fb_ioctl) + ret = fb->fb_ioctl(info, cmd, arg); + else + ret = -ENOTTY; + unlock_fb_info(info); + } + return ret; +} + +static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct fb_info *info = file_fb_info(file); + + if (!info) + return -ENODEV; + return do_fb_ioctl(info, cmd, arg); +} + +#ifdef CONFIG_COMPAT +struct fb_fix_screeninfo32 { + char id[16]; + compat_caddr_t smem_start; + u32 smem_len; + u32 type; + u32 type_aux; + u32 visual; + u16 xpanstep; + u16 ypanstep; + u16 ywrapstep; + u32 line_length; + compat_caddr_t mmio_start; + u32 mmio_len; + u32 accel; + u16 reserved[3]; +}; + +struct fb_cmap32 { + u32 start; + u32 len; + compat_caddr_t red; + compat_caddr_t green; + compat_caddr_t blue; + compat_caddr_t transp; +}; + +static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, + unsigned long arg) +{ + struct fb_cmap32 cmap32; + struct fb_cmap cmap_from; + struct fb_cmap_user cmap; + + if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) + return -EFAULT; + + cmap = (struct fb_cmap_user) { + .start = cmap32.start, + .len = cmap32.len, + .red = compat_ptr(cmap32.red), + .green = compat_ptr(cmap32.green), + .blue = compat_ptr(cmap32.blue), + .transp = compat_ptr(cmap32.transp), + }; + + if (cmd == FBIOPUTCMAP) + return fb_set_user_cmap(&cmap, info); + + lock_fb_info(info); + cmap_from = info->cmap; + unlock_fb_info(info); + + return fb_cmap_to_user(&cmap_from, &cmap); +} + +static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, + struct fb_fix_screeninfo32 __user *fix32) +{ + __u32 data; + int err; + + err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); + + data = (__u32) (unsigned long) fix->smem_start; + err |= put_user(data, &fix32->smem_start); + + err |= put_user(fix->smem_len, &fix32->smem_len); + err |= put_user(fix->type, &fix32->type); + err |= put_user(fix->type_aux, &fix32->type_aux); + err |= put_user(fix->visual, &fix32->visual); + err |= put_user(fix->xpanstep, &fix32->xpanstep); + err |= put_user(fix->ypanstep, &fix32->ypanstep); + err |= put_user(fix->ywrapstep, &fix32->ywrapstep); + err |= put_user(fix->line_length, &fix32->line_length); + + data = (__u32) (unsigned long) fix->mmio_start; + err |= put_user(data, &fix32->mmio_start); + + err |= put_user(fix->mmio_len, &fix32->mmio_len); + err |= put_user(fix->accel, &fix32->accel); + err |= copy_to_user(fix32->reserved, fix->reserved, + sizeof(fix->reserved)); + + if (err) + return -EFAULT; + return 0; +} + +static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, + unsigned long arg) +{ + struct fb_fix_screeninfo fix; + + lock_fb_info(info); + fix = info->fix; + if (info->flags & FBINFO_HIDE_SMEM_START) + fix.smem_start = 0; + unlock_fb_info(info); + return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); +} + +static long fb_compat_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + struct fb_info *info = file_fb_info(file); + const struct fb_ops *fb; + long ret = -ENOIOCTLCMD; + + if (!info) + return -ENODEV; + fb = info->fbops; + switch (cmd) { + case FBIOGET_VSCREENINFO: + case FBIOPUT_VSCREENINFO: + case FBIOPAN_DISPLAY: + case FBIOGET_CON2FBMAP: + case FBIOPUT_CON2FBMAP: + arg = (unsigned long) compat_ptr(arg); + fallthrough; + case FBIOBLANK: + ret = do_fb_ioctl(info, cmd, arg); + break; + + case FBIOGET_FSCREENINFO: + ret = fb_get_fscreeninfo(info, cmd, arg); + break; + + case FBIOGETCMAP: + case FBIOPUTCMAP: + ret = fb_getput_cmap(info, cmd, arg); + break; + + default: + if (fb->fb_compat_ioctl) + ret = fb->fb_compat_ioctl(info, cmd, arg); + break; + } + return ret; +} +#endif + +static int fb_mmap(struct file *file, struct vm_area_struct *vma) +{ + struct fb_info *info = file_fb_info(file); + unsigned long mmio_pgoff; + unsigned long start; + u32 len; + + if (!info) + return -ENODEV; + mutex_lock(&info->mm_lock); + + if (info->fbops->fb_mmap) { + int res; + + /* + * The framebuffer needs to be accessed decrypted, be sure + * SME protection is removed ahead of the call + */ + vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); + res = info->fbops->fb_mmap(info, vma); + mutex_unlock(&info->mm_lock); + return res; +#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) + } else if (info->fbdefio) { + /* + * FB deferred I/O wants you to handle mmap in your drivers. At a + * minimum, point struct fb_ops.fb_mmap to fb_deferred_io_mmap(). + */ + dev_warn_once(info->dev, "fbdev mmap not set up for deferred I/O.\n"); + mutex_unlock(&info->mm_lock); + return -ENODEV; +#endif + } + + /* + * Ugh. This can be either the frame buffer mapping, or + * if pgoff points past it, the mmio mapping. + */ + start = info->fix.smem_start; + len = info->fix.smem_len; + mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT; + if (vma->vm_pgoff >= mmio_pgoff) { + if (info->var.accel_flags) { + mutex_unlock(&info->mm_lock); + return -EINVAL; + } + + vma->vm_pgoff -= mmio_pgoff; + start = info->fix.mmio_start; + len = info->fix.mmio_len; + } + mutex_unlock(&info->mm_lock); + + vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); + fb_pgprotect(file, vma, start); + + return vm_iomap_memory(vma, start, len); +} + +static int fb_open(struct inode *inode, struct file *file) +__acquires(&info->lock) +__releases(&info->lock) +{ + int fbidx = iminor(inode); + struct fb_info *info; + int res = 0; + + info = get_fb_info(fbidx); + if (!info) { + request_module("fb%d", fbidx); + info = get_fb_info(fbidx); + if (!info) + return -ENODEV; + } + if (IS_ERR(info)) + return PTR_ERR(info); + + lock_fb_info(info); + if (!try_module_get(info->fbops->owner)) { + res = -ENODEV; + goto out; + } + file->private_data = info; + if (info->fbops->fb_open) { + res = info->fbops->fb_open(info, 1); + if (res) + module_put(info->fbops->owner); + } +#ifdef CONFIG_FB_DEFERRED_IO + if (info->fbdefio) + fb_deferred_io_open(info, inode, file); +#endif +out: + unlock_fb_info(info); + if (res) + put_fb_info(info); + return res; +} + +static int fb_release(struct inode *inode, struct file *file) +__acquires(&info->lock) +__releases(&info->lock) +{ + struct fb_info * const info = file->private_data; + + lock_fb_info(info); +#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) + if (info->fbdefio) + fb_deferred_io_release(info); +#endif + if (info->fbops->fb_release) + info->fbops->fb_release(info, 1); + module_put(info->fbops->owner); + unlock_fb_info(info); + put_fb_info(info); + return 0; +} + +#if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU) +static unsigned long get_fb_unmapped_area(struct file *filp, + unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags) +{ + struct fb_info * const info = filp->private_data; + unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len); + + if (pgoff > fb_size || len > fb_size - pgoff) + return -EINVAL; + + return (unsigned long)info->screen_base + pgoff; +} +#endif + +static const struct file_operations fb_fops = { + .owner = THIS_MODULE, + .read = fb_read, + .write = fb_write, + .unlocked_ioctl = fb_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = fb_compat_ioctl, +#endif + .mmap = fb_mmap, + .open = fb_open, + .release = fb_release, +#if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \ + (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \ + !defined(CONFIG_MMU)) + .get_unmapped_area = get_fb_unmapped_area, +#endif +#ifdef CONFIG_FB_DEFERRED_IO + .fsync = fb_deferred_io_fsync, +#endif + .llseek = default_llseek, +}; + +int fb_register_chrdev(void) +{ + int ret; + + ret = register_chrdev(FB_MAJOR, "fb", &fb_fops); + if (ret) { + pr_err("Unable to get major %d for fb devs\n", FB_MAJOR); + return ret; + } + + return ret; +} + +void fb_unregister_chrdev(void) +{ + unregister_chrdev(FB_MAJOR, "fb"); +} diff --git a/drivers/video/fbdev/core/fb_internal.h b/drivers/video/fbdev/core/fb_internal.h index 51f7c9f04e45..abe06c9da36e 100644 --- a/drivers/video/fbdev/core/fb_internal.h +++ b/drivers/video/fbdev/core/fb_internal.h @@ -6,10 +6,16 @@ #include <linux/fb.h> #include <linux/mutex.h> +/* fb_devfs.c */ +int fb_register_chrdev(void); +void fb_unregister_chrdev(void); + /* fbmem.c */ extern struct mutex registration_lock; extern struct fb_info *registered_fb[FB_MAX]; extern int num_registered_fb; +struct fb_info *get_fb_info(unsigned int idx); +void put_fb_info(struct fb_info *fb_info); /* fb_procfs.c */ int fb_init_procfs(void); diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c index de1e45240161..2d26ac46337b 100644 --- a/drivers/video/fbdev/core/fbmem.c +++ b/drivers/video/fbdev/core/fbmem.c @@ -17,7 +17,6 @@ #include <linux/types.h> #include <linux/errno.h> #include <linux/kernel.h> -#include <linux/major.h> #include <linux/slab.h> #include <linux/mm.h> #include <linux/mman.h> @@ -54,7 +53,7 @@ bool fb_center_logo __read_mostly; int fb_logo_count __read_mostly = -1; -static struct fb_info *get_fb_info(unsigned int idx) +struct fb_info *get_fb_info(unsigned int idx) { struct fb_info *fb_info; @@ -70,7 +69,7 @@ static struct fb_info *get_fb_info(unsigned int idx) return fb_info; } -static void put_fb_info(struct fb_info *fb_info) +void put_fb_info(struct fb_info *fb_info) { if (!refcount_dec_and_test(&fb_info->count)) return; @@ -699,59 +698,6 @@ int fb_show_logo(struct fb_info *info, int rotate) { return 0; } EXPORT_SYMBOL(fb_prepare_logo); EXPORT_SYMBOL(fb_show_logo); -/* - * We hold a reference to the fb_info in file->private_data, - * but if the current registered fb has changed, we don't - * actually want to use it. - * - * So look up the fb_info using the inode minor number, - * and just verify it against the reference we have. - */ -static struct fb_info *file_fb_info(struct file *file) -{ - struct inode *inode = file_inode(file); - int fbidx = iminor(inode); - struct fb_info *info = registered_fb[fbidx]; - - if (info != file->private_data) - info = NULL; - return info; -} - -static ssize_t -fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) -{ - struct fb_info *info = file_fb_info(file); - - if (!info) - return -ENODEV; - - if (info->state != FBINFO_STATE_RUNNING) - return -EPERM; - - if (info->fbops->fb_read) - return info->fbops->fb_read(info, buf, count, ppos); - - return fb_io_read(info, buf, count, ppos); -} - -static ssize_t -fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) -{ - struct fb_info *info = file_fb_info(file); - - if (!info) - return -ENODEV; - - if (info->state != FBINFO_STATE_RUNNING) - return -EPERM; - - if (info->fbops->fb_write) - return info->fbops->fb_write(info, buf, count, ppos); - - return fb_io_write(info, buf, count, ppos); -} - int fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var) { @@ -951,416 +897,6 @@ fb_blank(struct fb_info *info, int blank) } EXPORT_SYMBOL(fb_blank); -static long do_fb_ioctl(struct fb_info *info, unsigned int cmd, - unsigned long arg) -{ - const struct fb_ops *fb; - struct fb_var_screeninfo var; - struct fb_fix_screeninfo fix; - struct fb_cmap cmap_from; - struct fb_cmap_user cmap; - void __user *argp = (void __user *)arg; - long ret = 0; - - switch (cmd) { - case FBIOGET_VSCREENINFO: - lock_fb_info(info); - var = info->var; - unlock_fb_info(info); - - ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0; - break; - case FBIOPUT_VSCREENINFO: - if (copy_from_user(&var, argp, sizeof(var))) - return -EFAULT; - /* only for kernel-internal use */ - var.activate &= ~FB_ACTIVATE_KD_TEXT; - console_lock(); - lock_fb_info(info); - ret = fbcon_modechange_possible(info, &var); - if (!ret) - ret = fb_set_var(info, &var); - if (!ret) - fbcon_update_vcs(info, var.activate & FB_ACTIVATE_ALL); - unlock_fb_info(info); - console_unlock(); - if (!ret && copy_to_user(argp, &var, sizeof(var))) - ret = -EFAULT; - break; - case FBIOGET_FSCREENINFO: - lock_fb_info(info); - memcpy(&fix, &info->fix, sizeof(fix)); - if (info->flags & FBINFO_HIDE_SMEM_START) - fix.smem_start = 0; - unlock_fb_info(info); - - ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0; - break; - case FBIOPUTCMAP: - if (copy_from_user(&cmap, argp, sizeof(cmap))) - return -EFAULT; - ret = fb_set_user_cmap(&cmap, info); - break; - case FBIOGETCMAP: - if (copy_from_user(&cmap, argp, sizeof(cmap))) - return -EFAULT; - lock_fb_info(info); - cmap_from = info->cmap; - unlock_fb_info(info); - ret = fb_cmap_to_user(&cmap_from, &cmap); - break; - case FBIOPAN_DISPLAY: - if (copy_from_user(&var, argp, sizeof(var))) - return -EFAULT; - console_lock(); - lock_fb_info(info); - ret = fb_pan_display(info, &var); - unlock_fb_info(info); - console_unlock(); - if (ret == 0 && copy_to_user(argp, &var, sizeof(var))) - return -EFAULT; - break; - case FBIO_CURSOR: - ret = -EINVAL; - break; - case FBIOGET_CON2FBMAP: - ret = fbcon_get_con2fb_map_ioctl(argp); - break; - case FBIOPUT_CON2FBMAP: - ret = fbcon_set_con2fb_map_ioctl(argp); - break; - case FBIOBLANK: - if (arg > FB_BLANK_POWERDOWN) - return -EINVAL; - console_lock(); - lock_fb_info(info); - ret = fb_blank(info, arg); - /* might again call into fb_blank */ - fbcon_fb_blanked(info, arg); - unlock_fb_info(info); - console_unlock(); - break; - default: - lock_fb_info(info); - fb = info->fbops; - if (fb->fb_ioctl) - ret = fb->fb_ioctl(info, cmd, arg); - else - ret = -ENOTTY; - unlock_fb_info(info); - } - return ret; -} - -static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) -{ - struct fb_info *info = file_fb_info(file); - - if (!info) - return -ENODEV; - return do_fb_ioctl(info, cmd, arg); -} - -#ifdef CONFIG_COMPAT -struct fb_fix_screeninfo32 { - char id[16]; - compat_caddr_t smem_start; - u32 smem_len; - u32 type; - u32 type_aux; - u32 visual; - u16 xpanstep; - u16 ypanstep; - u16 ywrapstep; - u32 line_length; - compat_caddr_t mmio_start; - u32 mmio_len; - u32 accel; - u16 reserved[3]; -}; - -struct fb_cmap32 { - u32 start; - u32 len; - compat_caddr_t red; - compat_caddr_t green; - compat_caddr_t blue; - compat_caddr_t transp; -}; - -static int fb_getput_cmap(struct fb_info *info, unsigned int cmd, - unsigned long arg) -{ - struct fb_cmap32 cmap32; - struct fb_cmap cmap_from; - struct fb_cmap_user cmap; - - if (copy_from_user(&cmap32, compat_ptr(arg), sizeof(cmap32))) - return -EFAULT; - - cmap = (struct fb_cmap_user) { - .start = cmap32.start, - .len = cmap32.len, - .red = compat_ptr(cmap32.red), - .green = compat_ptr(cmap32.green), - .blue = compat_ptr(cmap32.blue), - .transp = compat_ptr(cmap32.transp), - }; - - if (cmd == FBIOPUTCMAP) - return fb_set_user_cmap(&cmap, info); - - lock_fb_info(info); - cmap_from = info->cmap; - unlock_fb_info(info); - - return fb_cmap_to_user(&cmap_from, &cmap); -} - -static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix, - struct fb_fix_screeninfo32 __user *fix32) -{ - __u32 data; - int err; - - err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id)); - - data = (__u32) (unsigned long) fix->smem_start; - err |= put_user(data, &fix32->smem_start); - - err |= put_user(fix->smem_len, &fix32->smem_len); - err |= put_user(fix->type, &fix32->type); - err |= put_user(fix->type_aux, &fix32->type_aux); - err |= put_user(fix->visual, &fix32->visual); - err |= put_user(fix->xpanstep, &fix32->xpanstep); - err |= put_user(fix->ypanstep, &fix32->ypanstep); - err |= put_user(fix->ywrapstep, &fix32->ywrapstep); - err |= put_user(fix->line_length, &fix32->line_length); - - data = (__u32) (unsigned long) fix->mmio_start; - err |= put_user(data, &fix32->mmio_start); - - err |= put_user(fix->mmio_len, &fix32->mmio_len); - err |= put_user(fix->accel, &fix32->accel); - err |= copy_to_user(fix32->reserved, fix->reserved, - sizeof(fix->reserved)); - - if (err) - return -EFAULT; - return 0; -} - -static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd, - unsigned long arg) -{ - struct fb_fix_screeninfo fix; - - lock_fb_info(info); - fix = info->fix; - if (info->flags & FBINFO_HIDE_SMEM_START) - fix.smem_start = 0; - unlock_fb_info(info); - return do_fscreeninfo_to_user(&fix, compat_ptr(arg)); -} - -static long fb_compat_ioctl(struct file *file, unsigned int cmd, - unsigned long arg) -{ - struct fb_info *info = file_fb_info(file); - const struct fb_ops *fb; - long ret = -ENOIOCTLCMD; - - if (!info) - return -ENODEV; - fb = info->fbops; - switch(cmd) { - case FBIOGET_VSCREENINFO: - case FBIOPUT_VSCREENINFO: - case FBIOPAN_DISPLAY: - case FBIOGET_CON2FBMAP: - case FBIOPUT_CON2FBMAP: - arg = (unsigned long) compat_ptr(arg); - fallthrough; - case FBIOBLANK: - ret = do_fb_ioctl(info, cmd, arg); - break; - - case FBIOGET_FSCREENINFO: - ret = fb_get_fscreeninfo(info, cmd, arg); - break; - - case FBIOGETCMAP: - case FBIOPUTCMAP: - ret = fb_getput_cmap(info, cmd, arg); - break; - - default: - if (fb->fb_compat_ioctl) - ret = fb->fb_compat_ioctl(info, cmd, arg); - break; - } - return ret; -} -#endif - -static int -fb_mmap(struct file *file, struct vm_area_struct * vma) -{ - struct fb_info *info = file_fb_info(file); - unsigned long mmio_pgoff; - unsigned long start; - u32 len; - - if (!info) - return -ENODEV; - mutex_lock(&info->mm_lock); - - if (info->fbops->fb_mmap) { - int res; - - /* - * The framebuffer needs to be accessed decrypted, be sure - * SME protection is removed ahead of the call - */ - vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); - res = info->fbops->fb_mmap(info, vma); - mutex_unlock(&info->mm_lock); - return res; -#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) - } else if (info->fbdefio) { - /* - * FB deferred I/O wants you to handle mmap in your drivers. At a - * minimum, point struct fb_ops.fb_mmap to fb_deferred_io_mmap(). - */ - dev_warn_once(info->dev, "fbdev mmap not set up for deferred I/O.\n"); - mutex_unlock(&info->mm_lock); - return -ENODEV; -#endif - } - - /* - * Ugh. This can be either the frame buffer mapping, or - * if pgoff points past it, the mmio mapping. - */ - start = info->fix.smem_start; - len = info->fix.smem_len; - mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT; - if (vma->vm_pgoff >= mmio_pgoff) { - if (info->var.accel_flags) { - mutex_unlock(&info->mm_lock); - return -EINVAL; - } - - vma->vm_pgoff -= mmio_pgoff; - start = info->fix.mmio_start; - len = info->fix.mmio_len; - } - mutex_unlock(&info->mm_lock); - - vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); - fb_pgprotect(file, vma, start); - - return vm_iomap_memory(vma, start, len); -} - -static int -fb_open(struct inode *inode, struct file *file) -__acquires(&info->lock) -__releases(&info->lock) -{ - int fbidx = iminor(inode); - struct fb_info *info; - int res = 0; - - info = get_fb_info(fbidx); - if (!info) { - request_module("fb%d", fbidx); - info = get_fb_info(fbidx); - if (!info) - return -ENODEV; - } - if (IS_ERR(info)) - return PTR_ERR(info); - - lock_fb_info(info); - if (!try_module_get(info->fbops->owner)) { - res = -ENODEV; - goto out; - } - file->private_data = info; - if (info->fbops->fb_open) { - res = info->fbops->fb_open(info,1); - if (res) - module_put(info->fbops->owner); - } -#ifdef CONFIG_FB_DEFERRED_IO - if (info->fbdefio) - fb_deferred_io_open(info, inode, file); -#endif -out: - unlock_fb_info(info); - if (res) - put_fb_info(info); - return res; -} - -static int -fb_release(struct inode *inode, struct file *file) -__acquires(&info->lock) -__releases(&info->lock) -{ - struct fb_info * const info = file->private_data; - - lock_fb_info(info); -#if IS_ENABLED(CONFIG_FB_DEFERRED_IO) - if (info->fbdefio) - fb_deferred_io_release(info); -#endif - if (info->fbops->fb_release) - info->fbops->fb_release(info,1); - module_put(info->fbops->owner); - unlock_fb_info(info); - put_fb_info(info); - return 0; -} - -#if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU) -static unsigned long get_fb_unmapped_area(struct file *filp, - unsigned long addr, unsigned long len, - unsigned long pgoff, unsigned long flags) -{ - struct fb_info * const info = filp->private_data; - unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len); - - if (pgoff > fb_size || len > fb_size - pgoff) - return -EINVAL; - - return (unsigned long)info->screen_base + pgoff; -} -#endif - -static const struct file_operations fb_fops = { - .owner = THIS_MODULE, - .read = fb_read, - .write = fb_write, - .unlocked_ioctl = fb_ioctl, -#ifdef CONFIG_COMPAT - .compat_ioctl = fb_compat_ioctl, -#endif - .mmap = fb_mmap, - .open = fb_open, - .release = fb_release, -#if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \ - (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \ - !defined(CONFIG_MMU)) - .get_unmapped_area = get_fb_unmapped_area, -#endif -#ifdef CONFIG_FB_DEFERRED_IO - .fsync = fb_deferred_io_fsync, -#endif - .llseek = default_llseek, -}; - struct class *fb_class; EXPORT_SYMBOL(fb_class); @@ -1588,11 +1124,9 @@ fbmem_init(void) if (ret) return ret; - ret = register_chrdev(FB_MAJOR, "fb", &fb_fops); - if (ret) { - printk("unable to get major %d for fb devs\n", FB_MAJOR); + ret = fb_register_chrdev(); + if (ret) goto err_chrdev; - } fb_class = class_create("graphics"); if (IS_ERR(fb_class)) { @@ -1607,7 +1141,7 @@ fbmem_init(void) return 0; err_class: - unregister_chrdev(FB_MAJOR, "fb"); + fb_unregister_chrdev(); err_chrdev: fb_cleanup_procfs(); return ret; @@ -1622,7 +1156,7 @@ fbmem_exit(void) fb_cleanup_procfs(); class_destroy(fb_class); - unregister_chrdev(FB_MAJOR, "fb"); + fb_unregister_chrdev(); } module_exit(fbmem_exit);
Move fbdev's file-I/O code into a separate file and contain it in init and cleanup helpers. No functional changes. Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> --- drivers/video/fbdev/core/Makefile | 1 + drivers/video/fbdev/core/fb_devfs.c | 484 +++++++++++++++++++++++++ drivers/video/fbdev/core/fb_internal.h | 6 + drivers/video/fbdev/core/fbmem.c | 478 +----------------------- 4 files changed, 497 insertions(+), 472 deletions(-) create mode 100644 drivers/video/fbdev/core/fb_devfs.c