Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* sbuslib.c: Helper library for SBUS framebuffer drivers. |
| 2 | * |
| 3 | * Copyright (C) 2003 David S. Miller (davem@redhat.com) |
| 4 | */ |
| 5 | |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 6 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/fb.h> |
| 11 | #include <linux/mm.h> |
| 12 | |
| 13 | #include <asm/oplib.h> |
| 14 | #include <asm/fbio.h> |
| 15 | |
| 16 | #include "sbuslib.h" |
| 17 | |
| 18 | void sbusfb_fill_var(struct fb_var_screeninfo *var, int prom_node, int bpp) |
| 19 | { |
| 20 | memset(var, 0, sizeof(*var)); |
| 21 | |
| 22 | var->xres = prom_getintdefault(prom_node, "width", 1152); |
| 23 | var->yres = prom_getintdefault(prom_node, "height", 900); |
| 24 | var->xres_virtual = var->xres; |
| 25 | var->yres_virtual = var->yres; |
| 26 | var->bits_per_pixel = bpp; |
| 27 | } |
| 28 | |
| 29 | EXPORT_SYMBOL(sbusfb_fill_var); |
| 30 | |
| 31 | static unsigned long sbusfb_mmapsize(long size, unsigned long fbsize) |
| 32 | { |
| 33 | if (size == SBUS_MMAP_EMPTY) return 0; |
| 34 | if (size >= 0) return size; |
| 35 | return fbsize * (-size); |
| 36 | } |
| 37 | |
| 38 | int sbusfb_mmap_helper(struct sbus_mmap_map *map, |
| 39 | unsigned long physbase, |
| 40 | unsigned long fbsize, |
| 41 | unsigned long iospace, |
| 42 | struct vm_area_struct *vma) |
| 43 | { |
| 44 | unsigned int size, page, r, map_size; |
| 45 | unsigned long map_offset = 0; |
| 46 | unsigned long off; |
| 47 | int i; |
| 48 | |
David S. Miller | e445ee6 | 2005-12-24 12:06:10 -0800 | [diff] [blame] | 49 | if (!(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) |
| 50 | return -EINVAL; |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | size = vma->vm_end - vma->vm_start; |
| 53 | if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) |
| 54 | return -EINVAL; |
| 55 | |
| 56 | off = vma->vm_pgoff << PAGE_SHIFT; |
| 57 | |
| 58 | /* To stop the swapper from even considering these pages */ |
| 59 | vma->vm_flags |= (VM_IO | VM_RESERVED); |
| 60 | |
| 61 | /* Each page, see which map applies */ |
| 62 | for (page = 0; page < size; ){ |
| 63 | map_size = 0; |
| 64 | for (i = 0; map[i].size; i++) |
| 65 | if (map[i].voff == off+page) { |
| 66 | map_size = sbusfb_mmapsize(map[i].size, fbsize); |
| 67 | #ifdef __sparc_v9__ |
| 68 | #define POFF_MASK (PAGE_MASK|0x1UL) |
| 69 | #else |
| 70 | #define POFF_MASK (PAGE_MASK) |
| 71 | #endif |
| 72 | map_offset = (physbase + map[i].poff) & POFF_MASK; |
| 73 | break; |
| 74 | } |
| 75 | if (!map_size){ |
| 76 | page += PAGE_SIZE; |
| 77 | continue; |
| 78 | } |
| 79 | if (page + map_size > size) |
| 80 | map_size = size - page; |
| 81 | r = io_remap_pfn_range(vma, |
| 82 | vma->vm_start + page, |
| 83 | MK_IOSPACE_PFN(iospace, |
| 84 | map_offset >> PAGE_SHIFT), |
| 85 | map_size, |
| 86 | vma->vm_page_prot); |
| 87 | if (r) |
| 88 | return -EAGAIN; |
| 89 | page += map_size; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | EXPORT_SYMBOL(sbusfb_mmap_helper); |
| 95 | |
| 96 | int sbusfb_ioctl_helper(unsigned long cmd, unsigned long arg, |
| 97 | struct fb_info *info, |
| 98 | int type, int fb_depth, unsigned long fb_size) |
| 99 | { |
| 100 | switch(cmd) { |
| 101 | case FBIOGTYPE: { |
| 102 | struct fbtype __user *f = (struct fbtype __user *) arg; |
| 103 | |
| 104 | if (put_user(type, &f->fb_type) || |
| 105 | __put_user(info->var.yres, &f->fb_height) || |
| 106 | __put_user(info->var.xres, &f->fb_width) || |
| 107 | __put_user(fb_depth, &f->fb_depth) || |
| 108 | __put_user(0, &f->fb_cmsize) || |
| 109 | __put_user(fb_size, &f->fb_cmsize)) |
| 110 | return -EFAULT; |
| 111 | return 0; |
| 112 | } |
| 113 | case FBIOPUTCMAP_SPARC: { |
| 114 | struct fbcmap __user *c = (struct fbcmap __user *) arg; |
| 115 | struct fb_cmap cmap; |
| 116 | u16 red, green, blue; |
| 117 | u8 red8, green8, blue8; |
| 118 | unsigned char __user *ured; |
| 119 | unsigned char __user *ugreen; |
| 120 | unsigned char __user *ublue; |
| 121 | int index, count, i; |
| 122 | |
| 123 | if (get_user(index, &c->index) || |
| 124 | __get_user(count, &c->count) || |
| 125 | __get_user(ured, &c->red) || |
| 126 | __get_user(ugreen, &c->green) || |
| 127 | __get_user(ublue, &c->blue)) |
| 128 | return -EFAULT; |
| 129 | |
| 130 | cmap.len = 1; |
| 131 | cmap.red = &red; |
| 132 | cmap.green = &green; |
| 133 | cmap.blue = &blue; |
| 134 | cmap.transp = NULL; |
| 135 | for (i = 0; i < count; i++) { |
| 136 | int err; |
| 137 | |
| 138 | if (get_user(red8, &ured[i]) || |
| 139 | get_user(green8, &ugreen[i]) || |
| 140 | get_user(blue8, &ublue[i])) |
| 141 | return -EFAULT; |
| 142 | |
| 143 | red = red8 << 8; |
| 144 | green = green8 << 8; |
| 145 | blue = blue8 << 8; |
| 146 | |
| 147 | cmap.start = index + i; |
| 148 | err = fb_set_cmap(&cmap, info); |
| 149 | if (err) |
| 150 | return err; |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | case FBIOGETCMAP_SPARC: { |
| 155 | struct fbcmap __user *c = (struct fbcmap __user *) arg; |
| 156 | unsigned char __user *ured; |
| 157 | unsigned char __user *ugreen; |
| 158 | unsigned char __user *ublue; |
| 159 | struct fb_cmap *cmap = &info->cmap; |
| 160 | int index, count, i; |
| 161 | u8 red, green, blue; |
| 162 | |
| 163 | if (get_user(index, &c->index) || |
| 164 | __get_user(count, &c->count) || |
| 165 | __get_user(ured, &c->red) || |
| 166 | __get_user(ugreen, &c->green) || |
| 167 | __get_user(ublue, &c->blue)) |
| 168 | return -EFAULT; |
| 169 | |
| 170 | if (index + count > cmap->len) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | for (i = 0; i < count; i++) { |
| 174 | red = cmap->red[index + i] >> 8; |
| 175 | green = cmap->green[index + i] >> 8; |
| 176 | blue = cmap->blue[index + i] >> 8; |
| 177 | if (put_user(red, &ured[i]) || |
| 178 | put_user(green, &ugreen[i]) || |
| 179 | put_user(blue, &ublue[i])) |
| 180 | return -EFAULT; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | default: |
| 185 | return -EINVAL; |
| 186 | }; |
| 187 | } |
| 188 | EXPORT_SYMBOL(sbusfb_ioctl_helper); |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 189 | |
| 190 | #ifdef CONFIG_COMPAT |
| 191 | struct fbcmap32 { |
| 192 | int index; /* first element (0 origin) */ |
| 193 | int count; |
| 194 | u32 red; |
| 195 | u32 green; |
| 196 | u32 blue; |
| 197 | }; |
| 198 | |
| 199 | #define FBIOPUTCMAP32 _IOW('F', 3, struct fbcmap32) |
| 200 | #define FBIOGETCMAP32 _IOW('F', 4, struct fbcmap32) |
| 201 | |
David S. Miller | c700663 | 2006-01-17 15:53:11 -0800 | [diff] [blame] | 202 | static int fbiogetputcmap(struct fb_info *info, unsigned int cmd, unsigned long arg) |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 203 | { |
| 204 | struct fbcmap32 __user *argp = (void __user *)arg; |
| 205 | struct fbcmap __user *p = compat_alloc_user_space(sizeof(*p)); |
| 206 | u32 addr; |
| 207 | int ret; |
| 208 | |
| 209 | ret = copy_in_user(p, argp, 2 * sizeof(int)); |
| 210 | ret |= get_user(addr, &argp->red); |
| 211 | ret |= put_user(compat_ptr(addr), &p->red); |
| 212 | ret |= get_user(addr, &argp->green); |
| 213 | ret |= put_user(compat_ptr(addr), &p->green); |
| 214 | ret |= get_user(addr, &argp->blue); |
| 215 | ret |= put_user(compat_ptr(addr), &p->blue); |
| 216 | if (ret) |
| 217 | return -EFAULT; |
Christoph Hellwig | 5ebdce7 | 2006-01-16 22:14:06 -0800 | [diff] [blame] | 218 | return info->fbops->fb_ioctl(info, |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 219 | (cmd == FBIOPUTCMAP32) ? |
| 220 | FBIOPUTCMAP_SPARC : FBIOGETCMAP_SPARC, |
Christoph Hellwig | 5ebdce7 | 2006-01-16 22:14:06 -0800 | [diff] [blame] | 221 | (unsigned long)p); |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | struct fbcursor32 { |
| 225 | short set; /* what to set, choose from the list above */ |
| 226 | short enable; /* cursor on/off */ |
| 227 | struct fbcurpos pos; /* cursor position */ |
| 228 | struct fbcurpos hot; /* cursor hot spot */ |
| 229 | struct fbcmap32 cmap; /* color map info */ |
| 230 | struct fbcurpos size; /* cursor bit map size */ |
| 231 | u32 image; /* cursor image bits */ |
| 232 | u32 mask; /* cursor mask bits */ |
| 233 | }; |
| 234 | |
| 235 | #define FBIOSCURSOR32 _IOW('F', 24, struct fbcursor32) |
| 236 | #define FBIOGCURSOR32 _IOW('F', 25, struct fbcursor32) |
| 237 | |
David S. Miller | c700663 | 2006-01-17 15:53:11 -0800 | [diff] [blame] | 238 | static int fbiogscursor(struct fb_info *info, unsigned long arg) |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 239 | { |
| 240 | struct fbcursor __user *p = compat_alloc_user_space(sizeof(*p)); |
| 241 | struct fbcursor32 __user *argp = (void __user *)arg; |
| 242 | compat_uptr_t addr; |
| 243 | int ret; |
| 244 | |
| 245 | ret = copy_in_user(p, argp, |
| 246 | 2 * sizeof (short) + 2 * sizeof(struct fbcurpos)); |
| 247 | ret |= copy_in_user(&p->size, &argp->size, sizeof(struct fbcurpos)); |
| 248 | ret |= copy_in_user(&p->cmap, &argp->cmap, 2 * sizeof(int)); |
| 249 | ret |= get_user(addr, &argp->cmap.red); |
| 250 | ret |= put_user(compat_ptr(addr), &p->cmap.red); |
| 251 | ret |= get_user(addr, &argp->cmap.green); |
| 252 | ret |= put_user(compat_ptr(addr), &p->cmap.green); |
| 253 | ret |= get_user(addr, &argp->cmap.blue); |
| 254 | ret |= put_user(compat_ptr(addr), &p->cmap.blue); |
| 255 | ret |= get_user(addr, &argp->mask); |
| 256 | ret |= put_user(compat_ptr(addr), &p->mask); |
| 257 | ret |= get_user(addr, &argp->image); |
| 258 | ret |= put_user(compat_ptr(addr), &p->image); |
| 259 | if (ret) |
| 260 | return -EFAULT; |
Christoph Hellwig | 5ebdce7 | 2006-01-16 22:14:06 -0800 | [diff] [blame] | 261 | return info->fbops->fb_ioctl(info, FBIOSCURSOR, (unsigned long)p); |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 262 | } |
| 263 | |
David S. Miller | c700663 | 2006-01-17 15:53:11 -0800 | [diff] [blame] | 264 | int sbusfb_compat_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg) |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 265 | { |
| 266 | switch (cmd) { |
| 267 | case FBIOGTYPE: |
| 268 | case FBIOSATTR: |
| 269 | case FBIOGATTR: |
| 270 | case FBIOSVIDEO: |
| 271 | case FBIOGVIDEO: |
| 272 | case FBIOGCURSOR32: /* This is not implemented yet. |
| 273 | Later it should be converted... */ |
| 274 | case FBIOSCURPOS: |
| 275 | case FBIOGCURPOS: |
| 276 | case FBIOGCURMAX: |
Christoph Hellwig | 5ebdce7 | 2006-01-16 22:14:06 -0800 | [diff] [blame] | 277 | return info->fbops->fb_ioctl(info, cmd, arg); |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 278 | case FBIOPUTCMAP32: |
Christoph Hellwig | 5ebdce7 | 2006-01-16 22:14:06 -0800 | [diff] [blame] | 279 | return fbiogetputcmap(info, cmd, arg); |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 280 | case FBIOGETCMAP32: |
Christoph Hellwig | 5ebdce7 | 2006-01-16 22:14:06 -0800 | [diff] [blame] | 281 | return fbiogetputcmap(info, cmd, arg); |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 282 | case FBIOSCURSOR32: |
Christoph Hellwig | 5ebdce7 | 2006-01-16 22:14:06 -0800 | [diff] [blame] | 283 | return fbiogscursor(info, arg); |
Christoph Hellwig | 9ffb83b | 2005-11-12 12:11:12 -0800 | [diff] [blame] | 284 | default: |
| 285 | return -ENOIOCTLCMD; |
| 286 | } |
| 287 | } |
| 288 | EXPORT_SYMBOL(sbusfb_compat_ioctl); |
| 289 | #endif |