Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | /* * linux/drivers/char/fbmem.c * * Copyright (C) 1994 Martin Schaller * * This file is subject to the terms and conditions of the GNU General Public * License. See the file COPYING in the main directory of this archive * for more details. */ #include <linux/types.h> #include <linux/errno.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/major.h> #include <linux/malloc.h> #include <linux/mman.h> #include <linux/tty.h> #include <asm/uaccess.h> #include <asm/bootinfo.h> #include <asm/page.h> #include <asm/pgtable.h> #include <linux/fb.h> #define FB_MAJOR 29 #define FB_MODES_SHIFT 5 /* 32 modes per framebuffer */ #define FB_NUM_MINORS 256 /* 256 Minors */ #define FB_MAX (FB_NUM_MINORS / (1 << FB_MODES_SHIFT)) #define GET_INODE(i) MKDEV(FB_MAJOR, (i) << FB_MODES_SHIFT) #define GET_FB_IDX(node) (MINOR(node) >> FB_MODES_SHIFT) #define GET_FB_VAR_IDX(node) (MINOR(node) & ((1 << FB_MODES_SHIFT)-1)) struct fb_ops *registered_fb[FB_MAX]; struct fb_var_screeninfo *registered_fb_var[FB_MAX]; int registered_fb_var_num[FB_MAX]; int fb_curr_open[FB_MAX]; int fb_open_count[FB_MAX]; static inline int PROC_CONSOLE(void) { if (!current->tty) return fg_console; if (current->tty->driver.type != TTY_DRIVER_TYPE_CONSOLE) /* XXX Should report error here? */ return fg_console; if (MINOR(current->tty->device) < 1) return fg_console; return MINOR(current->tty->device) - 1; } static int fb_read(struct inode *inode, struct file *file, char *buf, int count) { unsigned long p = file->f_pos; struct fb_ops *fb = registered_fb[GET_FB_IDX(inode->i_rdev)]; struct fb_fix_screeninfo fix; char *base_addr; int copy_size; if (! fb) return -ENODEV; if (count < 0) return -EINVAL; fb->fb_get_fix(&fix,PROC_CONSOLE()); base_addr=(char *) fix.smem_start; copy_size=(count + p <= fix.smem_len ? count : fix.smem_len - p); copy_to_user(buf, base_addr+p, copy_size); file->f_pos += copy_size; return copy_size; } static int fb_write(struct inode *inode, struct file *file, const char *buf, int count) { unsigned long p = file->f_pos; struct fb_ops *fb = registered_fb[GET_FB_IDX(inode->i_rdev)]; struct fb_fix_screeninfo fix; char *base_addr; int copy_size; if (! fb) return -ENODEV; if (count < 0) return -EINVAL; fb->fb_get_fix(&fix, PROC_CONSOLE()); base_addr=(char *) fix.smem_start; copy_size=(count + p <= fix.smem_len ? count : fix.smem_len - p); copy_from_user(base_addr+p, buf, copy_size); file->f_pos += copy_size; return copy_size; } static int fb_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { struct fb_ops *fb = registered_fb[GET_FB_IDX(inode->i_rdev)]; struct fb_cmap cmap; struct fb_var_screeninfo var; struct fb_fix_screeninfo fix; int i,fbidx,vidx; if (! fb) return -ENODEV; switch (cmd) { case FBIOGET_VSCREENINFO: i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct fb_var_screeninfo)); if (i) return i; fbidx=GET_FB_IDX(inode->i_rdev); vidx=GET_FB_VAR_IDX(inode->i_rdev); if (! vidx) /* ask device driver for current */ i=fb->fb_get_var(&var, PROC_CONSOLE()); else var=registered_fb_var[fbidx][vidx-1]; copy_to_user((void *) arg, &var, sizeof(var)); return i; case FBIOPUT_VSCREENINFO: i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct fb_var_screeninfo)); if (i) return i; copy_from_user(&var, (void *) arg, sizeof(var)); i=fb->fb_set_var(&var, PROC_CONSOLE()); copy_to_user((void *) arg, &var, sizeof(var)); fbidx=GET_FB_IDX(inode->i_rdev); vidx=GET_FB_VAR_IDX(inode->i_rdev); if (! i && vidx) registered_fb_var[fbidx][vidx-1]=var; return i; case FBIOGET_FSCREENINFO: i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct fb_fix_screeninfo)); if (i) return i; i=fb->fb_get_fix(&fix, PROC_CONSOLE()); copy_to_user((void *) arg, &fix, sizeof(fix)); return i; case FBIOPUTCMAP: i = verify_area(VERIFY_READ, (void *) arg, sizeof(struct fb_cmap)); if (i) return i; copy_from_user(&cmap, (void *) arg, sizeof(cmap)); i = verify_area(VERIFY_READ, (void *) cmap.red, cmap.len * sizeof(unsigned short)); if (i) return i; i = verify_area(VERIFY_READ, (void *) cmap.green, cmap.len * sizeof(unsigned short)); if (i) return i; i = verify_area(VERIFY_READ, (void *) cmap.blue, cmap.len * sizeof(unsigned short)); if (i) return i; if (cmap.transp) { i = verify_area(VERIFY_READ, (void *) cmap.transp, cmap.len * sizeof(unsigned short)); if (i) return i; } return (fb->fb_set_cmap(&cmap, 0, PROC_CONSOLE())); case FBIOGETCMAP: i = verify_area(VERIFY_READ, (void *) arg, sizeof(struct fb_cmap)); if (i) return i; copy_from_user(&cmap, (void *) arg, sizeof(cmap)); i = verify_area(VERIFY_WRITE, (void *) cmap.red, cmap.len * sizeof(unsigned short)); if (i) return i; i = verify_area(VERIFY_WRITE, (void *) cmap.green, cmap.len * sizeof(unsigned short)); if (i) return i; i = verify_area(VERIFY_WRITE, (void *) cmap.blue, cmap.len * sizeof(unsigned short)); if (i) return i; if (cmap.transp) { i = verify_area(VERIFY_WRITE, (void *) cmap.transp, cmap.len * sizeof(unsigned short)); if (i) return i; } return (fb->fb_get_cmap(&cmap, 0, PROC_CONSOLE())); case FBIOPAN_DISPLAY: i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct fb_var_screeninfo)); if (i) return i; copy_from_user(&var, (void *) arg, sizeof(var)); i=fb->fb_pan_display(&var, PROC_CONSOLE()); copy_to_user((void *) arg, &var, sizeof(var)); fbidx=GET_FB_IDX(inode->i_rdev); vidx=GET_FB_VAR_IDX(inode->i_rdev); if (! i && vidx) registered_fb_var[fbidx][vidx-1]=var; return i; default: return (fb->fb_ioctl(inode, file, cmd, arg, PROC_CONSOLE())); } } static int fb_mmap(struct inode *inode, struct file *file, struct vm_area_struct * vma) { struct fb_ops *fb = registered_fb[GET_FB_IDX(inode->i_rdev)]; struct fb_fix_screeninfo fix; if (! fb) return -ENODEV; fb->fb_get_fix(&fix, PROC_CONSOLE()); if ((vma->vm_end - vma->vm_start + vma->vm_offset) > fix.smem_len) return -EINVAL; vma->vm_offset += fix.smem_start; if (vma->vm_offset & ~PAGE_MASK) return -ENXIO; if (m68k_is040or060) { pgprot_val(vma->vm_page_prot) &= _CACHEMASK040; /* Use write-through cache mode */ pgprot_val(vma->vm_page_prot) |= _PAGE_CACHE040W; } if (remap_page_range(vma->vm_start, vma->vm_offset, vma->vm_end - vma->vm_start, vma->vm_page_prot)) return -EAGAIN; vma->vm_inode = inode; inode->i_count++; return 0; } static int fb_open(struct inode *inode, struct file *file) { int fbidx=GET_FB_IDX(inode->i_rdev); int vidx=GET_FB_VAR_IDX(inode->i_rdev); struct fb_ops *fb = registered_fb[fbidx]; int err; if (! vidx) /* fb?current always succeeds */ return 0; if (vidx > registered_fb_var_num[fbidx]) return -EINVAL; if (fb_curr_open[fbidx] && fb_curr_open[fbidx] != vidx) return -EBUSY; if (file->f_mode & 2) /* only set parameters if opened writeable */ if ((err=fb->fb_set_var(registered_fb_var[fbidx] + vidx-1, PROC_CONSOLE()))) return err; fb_curr_open[fbidx] = vidx; fb_open_count[fbidx]++; return 0; } static void fb_release(struct inode *inode, struct file *file) { int fbidx=GET_FB_IDX(inode->i_rdev); int vidx=GET_FB_VAR_IDX(inode->i_rdev); if (! vidx) return; if (! (--fb_open_count[fbidx])) fb_curr_open[fbidx]=0; } static struct file_operations fb_fops = { NULL, /* lseek */ fb_read, /* read */ fb_write, /* write */ NULL, /* readdir */ NULL, /* select */ fb_ioctl, /* ioctl */ fb_mmap, /* mmap */ fb_open, /* open */ fb_release, /* release */ NULL /* fsync */ }; int register_framebuffer(char *id, int *node, struct fb_ops *fbops, int fbvar_num, struct fb_var_screeninfo *fbvar) { int i; for (i = 0 ; i < FB_MAX; i++) if (! registered_fb[i]) break; if (i == FB_MAX) return -ENXIO; registered_fb[i]=fbops; registered_fb_var[i]=fbvar; registered_fb_var_num[i]=fbvar_num; *node=GET_INODE(i); return 0; } int unregister_framebuffer(int node) { int i=GET_FB_IDX(node); if (! registered_fb[i]) return -EINVAL; registered_fb[i]=NULL; registered_fb_var[i]=NULL; return 0; } void fbmem_init(void) { if (register_chrdev(FB_MAJOR,"fb",&fb_fops)) printk("unable to get major %d for fb devs\n", FB_MAJOR); } |