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