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