make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / video / sbusfb.c
1 /*
2  *  linux/drivers/video/sbusfb.c -- SBUS or UPA based frame buffer device
3  *
4  *      Copyright (C) 1998 Jakub Jelinek
5  *
6  *  This driver is partly based on the Open Firmware console driver
7  *
8  *      Copyright (C) 1997 Geert Uytterhoeven
9  *
10  *  and SPARC console subsystem
11  *
12  *      Copyright (C) 1995 Peter Zaitcev (zaitcev@yahoo.com)
13  *      Copyright (C) 1995-1997 David S. Miller (davem@caip.rutgers.edu)
14  *      Copyright (C) 1995-1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
15  *      Copyright (C) 1996 Dave Redman (djhr@tadpole.co.uk)
16  *      Copyright (C) 1996-1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
17  *      Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
18  *
19  *  This file is subject to the terms and conditions of the GNU General Public
20  *  License. See the file COPYING in the main directory of this archive for
21  *  more details.
22  */
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/mm.h>
30 #include <linux/tty.h>
31 #include <linux/slab.h>
32 #include <linux/vmalloc.h>
33 #include <linux/delay.h>
34 #include <linux/interrupt.h>
35 #include <linux/fb.h>
36 #include <linux/selection.h>
37 #include <linux/init.h>
38 #include <linux/console.h>
39 #include <linux/kd.h>
40 #include <linux/vt_kern.h>
41
42 #include <asm/uaccess.h>
43 #include <asm/pgtable.h>        /* io_remap_page_range() */
44
45 #include <video/sbusfb.h>
46
47 #define DEFAULT_CURSOR_BLINK_RATE       (2*HZ/5)
48
49 #define CURSOR_SHAPE                    1
50 #define CURSOR_BLINK                    2
51
52     /*
53      *  Interface used by the world
54      */
55
56 int sbusfb_init(void);
57 int sbusfb_setup(char*);
58
59 static int currcon;
60 static int defx_margin = -1, defy_margin = -1;
61 static char fontname[40] __initdata = { 0 };
62 static int curblink __initdata = 1;
63 static struct {
64         int depth;
65         int xres, yres;
66         int x_margin, y_margin;
67 } def_margins [] = {
68         { 8, 1280, 1024, 64, 80 },
69         { 8, 1152, 1024, 64, 80 },
70         { 8, 1152, 900,  64, 18 },
71         { 8, 1024, 768,  0,  0 },
72         { 8, 800, 600, 16, 12 },
73         { 8, 640, 480, 0, 0 },
74         { 1, 1152, 900,  8,  18 },
75         { 0 },
76 };
77
78 static int sbusfb_open(struct fb_info *info, int user);
79 static int sbusfb_release(struct fb_info *info, int user);
80 static int sbusfb_mmap(struct fb_info *info, struct file *file, 
81                         struct vm_area_struct *vma);
82 static int sbusfb_get_fix(struct fb_fix_screeninfo *fix, int con,
83                         struct fb_info *info);
84 static int sbusfb_get_var(struct fb_var_screeninfo *var, int con,
85                         struct fb_info *info);
86 static int sbusfb_set_var(struct fb_var_screeninfo *var, int con,
87                         struct fb_info *info);
88 static int sbusfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
89                         struct fb_info *info);
90 static int sbusfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
91                         struct fb_info *info);
92 static int sbusfb_ioctl(struct inode *inode, struct file *file, u_int cmd,
93                             u_long arg, int con, struct fb_info *info);
94 static void sbusfb_cursor(struct display *p, int mode, int x, int y);
95 static void sbusfb_clear_margin(struct display *p, int s);
96                             
97
98     /*
99      *  Interface to the low level console driver
100      */
101
102 static int sbusfbcon_switch(int con, struct fb_info *info);
103 static int sbusfbcon_updatevar(int con, struct fb_info *info);
104 static void sbusfbcon_blank(int blank, struct fb_info *info);
105
106
107     /*
108      *  Internal routines
109      */
110
111 static int sbusfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
112                             u_int *transp, struct fb_info *info);
113 static int sbusfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
114                             u_int transp, struct fb_info *info);
115 static void do_install_cmap(int con, struct fb_info *info);
116
117 static struct fb_ops sbusfb_ops = {
118         owner:          THIS_MODULE,
119         fb_open:        sbusfb_open,
120         fb_release:     sbusfb_release,
121         fb_get_fix:     sbusfb_get_fix,
122         fb_get_var:     sbusfb_get_var,
123         fb_set_var:     sbusfb_set_var,
124         fb_get_cmap:    sbusfb_get_cmap,
125         fb_set_cmap:    sbusfb_set_cmap,
126         fb_ioctl:       sbusfb_ioctl,
127         fb_mmap:        sbusfb_mmap,
128 };
129
130     /*
131      *  Open/Release the frame buffer device
132      */
133
134 static int sbusfb_open(struct fb_info *info, int user)
135 {
136         struct fb_info_sbusfb *fb = sbusfbinfo(info);
137         
138         if (user) {
139                 if (fb->open == 0) {
140                         fb->mmaped = 0;
141                         fb->vtconsole = -1;
142                 }
143                 fb->open++;
144         } else
145                 fb->consolecnt++;
146         return 0;
147 }
148
149 static int sbusfb_release(struct fb_info *info, int user)
150 {
151         struct fb_info_sbusfb *fb = sbusfbinfo(info);
152
153         if (user) {     
154                 fb->open--;
155                 if (fb->open == 0) {
156                         if (fb->vtconsole != -1) {
157                                 vt_cons[fb->vtconsole]->vc_mode = KD_TEXT;
158                                 if (fb->mmaped) {
159                                         fb->graphmode--;
160                                         sbusfb_clear_margin(&fb_display[fb->vtconsole], 0);
161                                 }
162                         }
163                         if (fb->reset)
164                                 fb->reset(fb);
165                 }
166         } else
167                 fb->consolecnt--;
168         return 0;
169 }
170
171 static unsigned long sbusfb_mmapsize(struct fb_info_sbusfb *fb, long size)
172 {
173         if (size == SBUS_MMAP_EMPTY) return 0;
174         if (size >= 0) return size;
175         return fb->type.fb_size * (-size);
176 }
177
178 static int sbusfb_mmap(struct fb_info *info, struct file *file, 
179                         struct vm_area_struct *vma)
180 {
181         struct fb_info_sbusfb *fb = sbusfbinfo(info);
182         unsigned int size, page, r, map_size;
183         unsigned long map_offset = 0;
184         unsigned long off;
185         int i;
186                                         
187         size = vma->vm_end - vma->vm_start;
188         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
189                 return -EINVAL;
190
191         off = vma->vm_pgoff << PAGE_SHIFT;
192
193         /* To stop the swapper from even considering these pages */
194         vma->vm_flags |= (VM_SHM| VM_LOCKED);
195         
196
197         /* Each page, see which map applies */
198         for (page = 0; page < size; ){
199                 map_size = 0;
200                 for (i = 0; fb->mmap_map[i].size; i++)
201                         if (fb->mmap_map[i].voff == off+page) {
202                                 map_size = sbusfb_mmapsize(fb,fb->mmap_map[i].size);
203 #ifdef __sparc_v9__
204 #define POFF_MASK       (PAGE_MASK|0x1UL)
205 #else
206 #define POFF_MASK       (PAGE_MASK)
207 #endif                          
208                                 map_offset = (fb->physbase + fb->mmap_map[i].poff) & POFF_MASK;
209                                 break;
210                         }
211                 if (!map_size){
212                         page += PAGE_SIZE;
213                         continue;
214                 }
215                 if (page + map_size > size)
216                         map_size = size - page;
217                 r = io_remap_page_range (vma->vm_start+page, map_offset, map_size, vma->vm_page_prot, fb->iospace);
218                 if (r)
219                         return -EAGAIN;
220                 page += map_size;
221         }
222         
223         vma->vm_flags |= VM_IO;
224         if (!fb->mmaped) {
225                 int lastconsole = 0;
226                 
227                 if (info->display_fg)
228                         lastconsole = info->display_fg->vc_num;
229                 fb->mmaped = 1;
230                 if (fb->consolecnt && fb_display[lastconsole].fb_info == info) {
231                         fb->vtconsole = lastconsole;
232                         fb->graphmode++;
233                         vt_cons [lastconsole]->vc_mode = KD_GRAPHICS;
234                         vc_cons[lastconsole].d->vc_sw->con_cursor(vc_cons[lastconsole].d,CM_ERASE);
235                 } else if (fb->unblank && !fb->blanked)
236                         (*fb->unblank)(fb);
237         }
238         return 0;
239 }
240
241 static void sbusfb_clear_margin(struct display *p, int s)
242 {
243         struct fb_info_sbusfb *fb = sbusfbinfod(p);
244
245         if (fb->switch_from_graph)
246                 (*fb->switch_from_graph)(fb);
247         if (fb->fill) {
248                 unsigned short rects [16];
249
250                 rects [0] = 0;
251                 rects [1] = 0;
252                 rects [2] = fb->var.xres_virtual;
253                 rects [3] = fb->y_margin;
254                 rects [4] = 0;
255                 rects [5] = fb->y_margin;
256                 rects [6] = fb->x_margin;
257                 rects [7] = fb->var.yres_virtual;
258                 rects [8] = fb->var.xres_virtual - fb->x_margin;
259                 rects [9] = fb->y_margin;
260                 rects [10] = fb->var.xres_virtual;
261                 rects [11] = fb->var.yres_virtual;
262                 rects [12] = fb->x_margin;
263                 rects [13] = fb->var.yres_virtual - fb->y_margin;
264                 rects [14] = fb->var.xres_virtual - fb->x_margin;
265                 rects [15] = fb->var.yres_virtual;
266                 (*fb->fill)(fb, p, s, 4, rects);
267         } else {
268                 unsigned char *fb_base = p->screen_base, *q;
269                 int skip_bytes = fb->y_margin * fb->var.xres_virtual;
270                 int scr_size = fb->var.xres_virtual * fb->var.yres_virtual;
271                 int h, he, incr, size;
272
273                 he = fb->var.yres;
274                 if (fb->var.bits_per_pixel == 1) {
275                         fb_base -= (skip_bytes + fb->x_margin) / 8;
276                         skip_bytes /= 8;
277                         scr_size /= 8;
278                         fb_memset255 (fb_base, skip_bytes - fb->x_margin / 8);
279                         fb_memset255 (fb_base + scr_size - skip_bytes + fb->x_margin / 8, skip_bytes - fb->x_margin / 8);
280                         incr = fb->var.xres_virtual / 8;
281                         size = fb->x_margin / 8 * 2;
282                         for (q = fb_base + skip_bytes - fb->x_margin / 8, h = 0;
283                              h <= he; q += incr, h++)
284                                 fb_memset255 (q, size);
285                 } else {
286                         fb_base -= (skip_bytes + fb->x_margin);
287                         memset (fb_base, attr_bgcol(p,s), skip_bytes - fb->x_margin);
288                         memset (fb_base + scr_size - skip_bytes + fb->x_margin, attr_bgcol(p,s), skip_bytes - fb->x_margin);
289                         incr = fb->var.xres_virtual;
290                         size = fb->x_margin * 2;
291                         for (q = fb_base + skip_bytes - fb->x_margin, h = 0;
292                              h <= he; q += incr, h++)
293                                 memset (q, attr_bgcol(p,s), size);
294                 }
295         }
296 }
297
298 static void sbusfb_disp_setup(struct display *p)
299 {
300         struct fb_info_sbusfb *fb = sbusfbinfod(p);
301
302         if (fb->setup)
303                 fb->setup(p);   
304         sbusfb_clear_margin(p, 0);
305 }
306
307     /*
308      *  Get the Fixed Part of the Display
309      */
310
311 static int sbusfb_get_fix(struct fb_fix_screeninfo *fix, int con,
312                           struct fb_info *info)
313 {
314         struct fb_info_sbusfb *fb = sbusfbinfo(info);
315
316         memcpy(fix, &fb->fix, sizeof(struct fb_fix_screeninfo));
317         return 0;
318 }
319
320     /*
321      *  Get the User Defined Part of the Display
322      */
323
324 static int sbusfb_get_var(struct fb_var_screeninfo *var, int con,
325                           struct fb_info *info)
326 {
327         struct fb_info_sbusfb *fb = sbusfbinfo(info);
328
329         memcpy(var, &fb->var, sizeof(struct fb_var_screeninfo));
330         return 0;
331 }
332
333     /*
334      *  Set the User Defined Part of the Display
335      */
336
337 static int sbusfb_set_var(struct fb_var_screeninfo *var, int con,
338                           struct fb_info *info)
339 {
340        struct display *display;
341        int activate = var->activate;
342
343        if(con >= 0)
344                display = &fb_display[con];
345        else
346                display = info->disp;
347
348        /* simple check for equality until fully implemented -E */
349        if ((activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
350                if (display->var.xres != var->xres ||
351                        display->var.yres != var->yres ||
352                        display->var.xres_virtual != var->xres_virtual ||
353                        display->var.yres_virtual != var->yres_virtual ||
354                        display->var.bits_per_pixel != var->bits_per_pixel ||
355                        display->var.accel_flags != var->accel_flags) {
356                        return -EINVAL;
357                }
358        }
359        return 0;
360
361 }
362
363     /*
364      *  Hardware cursor
365      */
366      
367 static int sbus_hw_scursor (struct fbcursor *cursor, struct fb_info_sbusfb *fb)
368 {
369         int op;
370         int i, bytes = 0;
371         struct fbcursor f;
372         char red[2], green[2], blue[2];
373         
374         if (copy_from_user (&f, cursor, sizeof(struct fbcursor)))
375                 return -EFAULT;
376         op = f.set;
377         if (op & FB_CUR_SETSHAPE){
378                 if ((u32) f.size.fbx > fb->cursor.hwsize.fbx)
379                         return -EINVAL;
380                 if ((u32) f.size.fby > fb->cursor.hwsize.fby)
381                         return -EINVAL;
382                 if (f.size.fbx > 32)
383                         bytes = f.size.fby << 3;
384                 else
385                         bytes = f.size.fby << 2;
386         }
387         if (op & FB_CUR_SETCMAP){
388                 if (f.cmap.index || f.cmap.count != 2)
389                         return -EINVAL;
390                 if (copy_from_user (red, f.cmap.red, 2) ||
391                     copy_from_user (green, f.cmap.green, 2) ||
392                     copy_from_user (blue, f.cmap.blue, 2))
393                         return -EFAULT;
394         }
395         if (op & FB_CUR_SETCMAP)
396                 (*fb->setcursormap) (fb, red, green, blue);
397         if (op & FB_CUR_SETSHAPE){
398                 u32 u;
399                 
400                 fb->cursor.size = f.size;
401                 memset ((void *)&fb->cursor.bits, 0, sizeof (fb->cursor.bits));
402                 if (copy_from_user (fb->cursor.bits [0], f.mask, bytes) ||
403                     copy_from_user (fb->cursor.bits [1], f.image, bytes))
404                         return -EFAULT;
405                 if (f.size.fbx <= 32) {
406                         u = 0xffffffff << (32 - f.size.fbx);
407                         for (i = fb->cursor.size.fby - 1; i >= 0; i--) {
408                                 fb->cursor.bits [0][i] &= u;
409                                 fb->cursor.bits [1][i] &= fb->cursor.bits [0][i];
410                         }
411                 } else {
412                         u = 0xffffffff << (64 - f.size.fbx);
413                         for (i = fb->cursor.size.fby - 1; i >= 0; i--) {
414                                 fb->cursor.bits [0][2*i+1] &= u;
415                                 fb->cursor.bits [1][2*i] &= fb->cursor.bits [0][2*i];
416                                 fb->cursor.bits [1][2*i+1] &= fb->cursor.bits [0][2*i+1];
417                         }
418                 }
419                 (*fb->setcurshape) (fb);
420         }
421         if (op & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)){
422                 if (op & FB_CUR_SETCUR)
423                         fb->cursor.enable = f.enable;
424                 if (op & FB_CUR_SETPOS)
425                         fb->cursor.cpos = f.pos;
426                 if (op & FB_CUR_SETHOT)
427                         fb->cursor.chot = f.hot;
428                 (*fb->setcursor) (fb);
429         }
430         return 0;
431 }
432
433 static unsigned char hw_cursor_cmap[2] = { 0, 0xff };
434
435 static void
436 sbusfb_cursor_timer_handler(unsigned long dev_addr)
437 {
438         struct fb_info_sbusfb *fb = (struct fb_info_sbusfb *)dev_addr;
439         
440         if (!fb->setcursor) return;
441                                 
442         if (fb->cursor.mode & CURSOR_BLINK) {
443                 fb->cursor.enable ^= 1;
444                 fb->setcursor(fb);
445         }
446         
447         fb->cursor.timer.expires = jiffies + fb->cursor.blink_rate;
448         add_timer(&fb->cursor.timer);
449 }
450
451 static void sbusfb_cursor(struct display *p, int mode, int x, int y)
452 {
453         struct fb_info_sbusfb *fb = sbusfbinfod(p);
454         
455         switch (mode) {
456         case CM_ERASE:
457                 fb->cursor.mode &= ~CURSOR_BLINK;
458                 fb->cursor.enable = 0;
459                 (*fb->setcursor)(fb);
460                 break;
461                                   
462         case CM_MOVE:
463         case CM_DRAW:
464                 if (fb->cursor.mode & CURSOR_SHAPE) {
465                         fb->cursor.size.fbx = fontwidth(p);
466                         fb->cursor.size.fby = fontheight(p);
467                         fb->cursor.chot.fbx = 0;
468                         fb->cursor.chot.fby = 0;
469                         fb->cursor.enable = 1;
470                         memset (fb->cursor.bits, 0, sizeof (fb->cursor.bits));
471                         fb->cursor.bits[0][fontheight(p) - 2] = (0xffffffff << (32 - fontwidth(p)));
472                         fb->cursor.bits[1][fontheight(p) - 2] = (0xffffffff << (32 - fontwidth(p)));
473                         fb->cursor.bits[0][fontheight(p) - 1] = (0xffffffff << (32 - fontwidth(p)));
474                         fb->cursor.bits[1][fontheight(p) - 1] = (0xffffffff << (32 - fontwidth(p)));
475                         (*fb->setcursormap) (fb, hw_cursor_cmap, hw_cursor_cmap, hw_cursor_cmap);
476                         (*fb->setcurshape) (fb);
477                 }
478                 fb->cursor.mode = CURSOR_BLINK;
479                 if (fontwidthlog(p))
480                         fb->cursor.cpos.fbx = (x << fontwidthlog(p)) + fb->x_margin;
481                 else
482                         fb->cursor.cpos.fbx = (x * fontwidth(p)) + fb->x_margin;
483                 if (fontheightlog(p))
484                         fb->cursor.cpos.fby = (y << fontheightlog(p)) + fb->y_margin;
485                 else
486                         fb->cursor.cpos.fby = (y * fontheight(p)) + fb->y_margin;
487                 (*fb->setcursor)(fb);
488                 break;
489         }
490 }
491
492     /*
493      *  Get the Colormap
494      */
495
496 static int sbusfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
497                          struct fb_info *info)
498 {
499         if (!info->display_fg || con == info->display_fg->vc_num) /* current console? */
500                 return fb_get_cmap(cmap, kspc, sbusfb_getcolreg, info);
501         else if (fb_display[con].cmap.len) /* non default colormap? */
502                 fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
503         else
504                 fb_copy_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel), cmap, kspc ? 0 : 2);
505         return 0;
506 }
507
508     /*
509      *  Set the Colormap
510      */
511
512 static int sbusfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
513                          struct fb_info *info)
514 {
515         int err;
516         struct display *disp;
517
518         if (con >= 0)
519                 disp = &fb_display[con];
520         else
521                 disp = info->disp;
522         if (!disp->cmap.len) {  /* no colormap allocated? */
523                 if ((err = fb_alloc_cmap(&disp->cmap, 1<<disp->var.bits_per_pixel, 0)))
524                         return err;
525         }
526         if (con == currcon) {                   /* current console? */
527                 err = fb_set_cmap(cmap, kspc, sbusfb_setcolreg, info);
528                 if (!err) {
529                         struct fb_info_sbusfb *fb = sbusfbinfo(info);
530                         
531                         if (fb->loadcmap)
532                                 (*fb->loadcmap)(fb, &fb_display[con], cmap->start, cmap->len);
533                 }
534                 return err;
535         } else
536                 fb_copy_cmap(cmap, &disp->cmap, kspc ? 0 : 1);
537         return 0;
538 }
539
540 static int sbusfb_ioctl(struct inode *inode, struct file *file, u_int cmd,
541                         u_long arg, int con, struct fb_info *info)
542 {
543         struct fb_info_sbusfb *fb = sbusfbinfo(info);
544         int i;
545         int lastconsole;
546         
547         switch (cmd){
548         case FBIOGTYPE:         /* return frame buffer type */
549                 if (copy_to_user((struct fbtype *)arg, &fb->type, sizeof(struct fbtype)))
550                         return -EFAULT;
551                 break;
552         case FBIOGATTR: {
553                 struct fbgattr *fba = (struct fbgattr *) arg;
554                 
555                 i = verify_area (VERIFY_WRITE, (void *) arg, sizeof (struct fbgattr));
556                 if (i) return i;
557                 if (__put_user(fb->emulations[0], &fba->real_type) ||
558                     __put_user(0, &fba->owner) ||
559                     __copy_to_user(&fba->fbtype, &fb->type,
560                                    sizeof(struct fbtype)) ||
561                     __put_user(0, &fba->sattr.flags) ||
562                     __put_user(fb->type.fb_type, &fba->sattr.emu_type) ||
563                     __put_user(-1, &fba->sattr.dev_specific[0]))
564                         return -EFAULT;
565                 for (i = 0; i < 4; i++) {
566                         if (put_user(fb->emulations[i], &fba->emu_types[i]))
567                                 return -EFAULT;
568                 }
569                 break;
570         }
571         case FBIOSATTR:
572                 i = verify_area (VERIFY_READ, (void *) arg, sizeof (struct fbsattr));
573                 if (i) return i;
574                 return -EINVAL;
575         case FBIOSVIDEO:
576                 if (fb->consolecnt) {
577                         lastconsole = info->display_fg->vc_num;
578                         if (vt_cons[lastconsole]->vc_mode == KD_TEXT)
579                                 break;
580                 }
581                 if (get_user(i, (int *)arg))
582                         return -EFAULT;
583                 if (i){
584                         if (!fb->blanked || !fb->unblank)
585                                 break;
586                         if (fb->consolecnt || (fb->open && fb->mmaped))
587                                 (*fb->unblank)(fb);
588                         fb->blanked = 0;
589                 } else {
590                         if (fb->blanked || !fb->blank)
591                                 break;
592                         (*fb->blank)(fb);
593                         fb->blanked = 1;
594                 }
595                 break;
596         case FBIOGVIDEO:
597                 if (put_user(fb->blanked, (int *) arg))
598                         return -EFAULT;
599                 break;
600         case FBIOGETCMAP_SPARC: {
601                 char *rp, *gp, *bp;
602                 int end, count, index;
603                 struct fbcmap *cmap;
604
605                 if (!fb->loadcmap)
606                         return -EINVAL;
607                 i = verify_area (VERIFY_READ, (void *) arg, sizeof (struct fbcmap));
608                 if (i) return i;
609                 cmap = (struct fbcmap *) arg;
610                 if (__get_user(count, &cmap->count) ||
611                     __get_user(index, &cmap->index))
612                         return -EFAULT;
613                 if ((index < 0) || (index > 255))
614                         return -EINVAL;
615                 if (index + count > 256)
616                         count = 256 - index;
617                 if (__get_user(rp, &cmap->red) ||
618                     __get_user(gp, &cmap->green) ||
619                     __get_user(bp, &cmap->blue))
620                         return -EFAULT;
621                 if (verify_area (VERIFY_WRITE, rp, count))
622                         return -EFAULT;
623                 if (verify_area (VERIFY_WRITE, gp, count))
624                         return -EFAULT;
625                 if (verify_area (VERIFY_WRITE, bp, count))
626                         return -EFAULT;
627                 end = index + count;
628                 for (i = index; i < end; i++){
629                         if (__put_user(fb->color_map CM(i,0), rp) ||
630                             __put_user(fb->color_map CM(i,1), gp) ||
631                             __put_user(fb->color_map CM(i,2), bp))
632                                 return -EFAULT;
633                         rp++; gp++; bp++;
634                 }
635                 (*fb->loadcmap)(fb, NULL, index, count);
636                 break;                  
637         }
638         case FBIOPUTCMAP_SPARC: {       /* load color map entries */
639                 char *rp, *gp, *bp;
640                 int end, count, index;
641                 struct fbcmap *cmap;
642                 
643                 if (!fb->loadcmap || !fb->color_map)
644                         return -EINVAL;
645                 i = verify_area (VERIFY_READ, (void *) arg, sizeof (struct fbcmap));
646                 if (i) return i;
647                 cmap = (struct fbcmap *) arg;
648                 if (__get_user(count, &cmap->count) ||
649                     __get_user(index, &cmap->index))
650                         return -EFAULT;
651                 if ((index < 0) || (index > 255))
652                         return -EINVAL;
653                 if (index + count > 256)
654                         count = 256 - index;
655                 if (__get_user(rp, &cmap->red) ||
656                     __get_user(gp, &cmap->green) ||
657                     __get_user(bp, &cmap->blue))
658                         return -EFAULT;
659                 if (verify_area (VERIFY_READ, rp, count))
660                         return -EFAULT;
661                 if (verify_area (VERIFY_READ, gp, count))
662                         return -EFAULT;
663                 if (verify_area (VERIFY_READ, bp, count))
664                         return -EFAULT;
665
666                 end = index + count;
667                 for (i = index; i < end; i++){
668                         if (__get_user(fb->color_map CM(i,0), rp))
669                                 return -EFAULT;
670                         if (__get_user(fb->color_map CM(i,1), gp))
671                                 return -EFAULT;
672                         if (__get_user(fb->color_map CM(i,2), bp))
673                                 return -EFAULT;
674                         rp++; gp++; bp++;
675                 }
676                 (*fb->loadcmap)(fb, NULL, index, count);
677                 break;                  
678         }
679         case FBIOGCURMAX: {
680                 struct fbcurpos *p = (struct fbcurpos *) arg;
681                 if (!fb->setcursor) return -EINVAL;
682                 if(verify_area (VERIFY_WRITE, p, sizeof (struct fbcurpos)))
683                         return -EFAULT;
684                 if (__put_user(fb->cursor.hwsize.fbx, &p->fbx) ||
685                     __put_user(fb->cursor.hwsize.fby, &p->fby))
686                         return -EFAULT;
687                 break;
688         }
689         case FBIOSCURSOR:
690                 if (!fb->setcursor) return -EINVAL;
691                 if (fb->consolecnt) {
692                         lastconsole = info->display_fg->vc_num; 
693                         if (vt_cons[lastconsole]->vc_mode == KD_TEXT)
694                                 return -EINVAL; /* Don't let graphics programs hide our nice text cursor */
695                         fb->cursor.mode = CURSOR_SHAPE; /* Forget state of our text cursor */
696                 }
697                 return sbus_hw_scursor ((struct fbcursor *) arg, fb);
698
699         case FBIOSCURPOS:
700                 if (!fb->setcursor) return -EINVAL;
701                 /* Don't let graphics programs move our nice text cursor */
702                 if (fb->consolecnt) {
703                         lastconsole = info->display_fg->vc_num; 
704                         if (vt_cons[lastconsole]->vc_mode == KD_TEXT)
705                                 return -EINVAL; /* Don't let graphics programs move our nice text cursor */
706                 }
707                 if (copy_from_user(&fb->cursor.cpos, (void *)arg, sizeof(struct fbcurpos)))
708                         return -EFAULT;
709                 (*fb->setcursor) (fb);
710                 break;
711         default:
712                 if (fb->ioctl)
713                         return fb->ioctl(fb, cmd, arg);
714                 return -EINVAL;
715         }               
716         return 0;
717 }
718
719     /*
720      *  Setup: parse used options
721      */
722
723 int __init sbusfb_setup(char *options)
724 {
725         char *p;
726         
727         for (p = options;;) {
728                 if (!strncmp(p, "nomargins", 9)) {
729                         defx_margin = 0; defy_margin = 0;
730                 } else if (!strncmp(p, "margins=", 8)) {
731                         int i, j;
732                         char *q;
733                         
734                         i = simple_strtoul(p+8,&q,10);
735                         if (i >= 0 && *q == 'x') {
736                             j = simple_strtoul(q+1,&q,10);
737                             if (j >= 0 && (*q == ' ' || !*q)) {
738                                 defx_margin = i; defy_margin = j;
739                             }
740                         }
741                 } else if (!strncmp(p, "font=", 5)) {
742                         int i;
743                         
744                         for (i = 0; i < sizeof(fontname) - 1; i++)
745                                 if (p[i+5] == ' ' || !p[i+5])
746                                         break;
747                         memcpy(fontname, p+5, i);
748                         fontname[i] = 0;
749                 } else if (!strncmp(p, "noblink", 7))
750                         curblink = 0;
751                 while (*p && *p != ' ' && *p != ',') p++;
752                 if (*p != ',') break;
753                 p++;
754         }
755         return 0;
756 }
757
758 static int sbusfbcon_switch(int con, struct fb_info *info)
759 {
760         int x_margin, y_margin;
761         struct fb_info_sbusfb *fb = sbusfbinfo(info);
762         int lastconsole;
763     
764         /* Do we have to save the colormap? */
765         if (fb_display[currcon].cmap.len)
766                 fb_get_cmap(&fb_display[currcon].cmap, 1, sbusfb_getcolreg, info);
767
768         if (info->display_fg) {
769                 lastconsole = info->display_fg->vc_num;
770                 if (lastconsole != con && 
771                     (fontwidth(&fb_display[lastconsole]) != fontwidth(&fb_display[con]) ||
772                      fontheight(&fb_display[lastconsole]) != fontheight(&fb_display[con])))
773                         fb->cursor.mode |= CURSOR_SHAPE;
774         }
775         x_margin = (fb_display[con].var.xres_virtual - fb_display[con].var.xres) / 2;
776         y_margin = (fb_display[con].var.yres_virtual - fb_display[con].var.yres) / 2;
777         if (fb->margins)
778                 fb->margins(fb, &fb_display[con], x_margin, y_margin);
779         if (fb->graphmode || fb->x_margin != x_margin || fb->y_margin != y_margin) {
780                 fb->x_margin = x_margin; fb->y_margin = y_margin;
781                 sbusfb_clear_margin(&fb_display[con], 0);
782         }
783         currcon = con;
784         /* Install new colormap */
785         do_install_cmap(con, info);
786         return 0;
787 }
788
789     /*
790      *  Update the `var' structure (called by fbcon.c)
791      */
792
793 static int sbusfbcon_updatevar(int con, struct fb_info *info)
794 {
795         /* Nothing */
796         return 0;
797 }
798
799     /*
800      *  Blank the display.
801      */
802
803 static void sbusfbcon_blank(int blank, struct fb_info *info)
804 {
805     struct fb_info_sbusfb *fb = sbusfbinfo(info);
806     
807     if (blank && fb->blank)
808         return fb->blank(fb);
809     else if (!blank && fb->unblank)
810         return fb->unblank(fb);
811 }
812
813     /*
814      *  Read a single color register and split it into
815      *  colors/transparent. Return != 0 for invalid regno.
816      */
817
818 static int sbusfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
819                           u_int *transp, struct fb_info *info)
820 {
821         struct fb_info_sbusfb *fb = sbusfbinfo(info);
822
823         if (!fb->color_map || regno > 255)
824                 return 1;
825         *red = (fb->color_map CM(regno, 0)<<8) | fb->color_map CM(regno, 0);
826         *green = (fb->color_map CM(regno, 1)<<8) | fb->color_map CM(regno, 1);
827         *blue = (fb->color_map CM(regno, 2)<<8) | fb->color_map CM(regno, 2);
828         *transp = 0;
829         return 0;
830 }
831
832
833     /*
834      *  Set a single color register. The values supplied are already
835      *  rounded down to the hardware's capabilities (according to the
836      *  entries in the var structure). Return != 0 for invalid regno.
837      */
838
839 static int sbusfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
840                             u_int transp, struct fb_info *info)
841 {
842         struct fb_info_sbusfb *fb = sbusfbinfo(info);
843
844         if (!fb->color_map || regno > 255)
845                 return 1;
846         red >>= 8;
847         green >>= 8;
848         blue >>= 8;
849         fb->color_map CM(regno, 0) = red;
850         fb->color_map CM(regno, 1) = green;
851         fb->color_map CM(regno, 2) = blue;
852         return 0;
853 }
854
855
856 static void do_install_cmap(int con, struct fb_info *info)
857 {
858         struct fb_info_sbusfb *fb = sbusfbinfo(info);
859         
860         if (con != currcon)
861                 return;
862         if (fb_display[con].cmap.len)
863                 fb_set_cmap(&fb_display[con].cmap, 1, sbusfb_setcolreg, info);
864         else
865                 fb_set_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
866                             1, sbusfb_setcolreg, info);
867         if (fb->loadcmap)
868                 (*fb->loadcmap)(fb, &fb_display[con], 0, 256);
869 }
870
871 static int sbusfb_set_font(struct display *p, int width, int height)
872 {
873         int margin;
874         int w = p->var.xres_virtual, h = p->var.yres_virtual;
875         int depth = p->var.bits_per_pixel;
876         struct fb_info_sbusfb *fb = sbusfbinfod(p);
877         int x_margin, y_margin;
878         
879         if (depth > 8) depth = 8;
880         x_margin = 0;
881         y_margin = 0;
882         if (defx_margin < 0 || defy_margin < 0) {
883                 for (margin = 0; def_margins[margin].depth; margin++)
884                         if (w == def_margins[margin].xres &&
885                             h == def_margins[margin].yres &&
886                             depth == def_margins[margin].depth) {
887                                 x_margin = def_margins[margin].x_margin;
888                                 y_margin = def_margins[margin].y_margin;
889                                 break;
890                         }
891         } else {
892                 x_margin = defx_margin;
893                 y_margin = defy_margin;
894         }
895         x_margin += ((w - 2*x_margin) % width) / 2;
896         y_margin += ((h - 2*y_margin) % height) / 2;
897
898         p->var.xres = w - 2*x_margin;
899         p->var.yres = h - 2*y_margin;
900         
901         fb->cursor.mode |= CURSOR_SHAPE;
902         
903         if (fb->margins)
904                 fb->margins(fb, p, x_margin, y_margin);
905         if (fb->x_margin != x_margin || fb->y_margin != y_margin) {
906                 fb->x_margin = x_margin; fb->y_margin = y_margin;
907                 sbusfb_clear_margin(p, 0);
908         }
909
910         return 1;
911 }
912
913 void sbusfb_palette(int enter)
914 {
915         int i;
916         struct display *p;
917         
918         for (i = 0; i < MAX_NR_CONSOLES; i++) {
919                 p = &fb_display[i];
920                 if (p->dispsw && p->dispsw->setup == sbusfb_disp_setup &&
921                     p->fb_info->display_fg &&
922                     p->fb_info->display_fg->vc_num == i) {
923                         struct fb_info_sbusfb *fb = sbusfbinfod(p);
924
925                         if (fb->restore_palette) {
926                                 if (enter)
927                                         fb->restore_palette(fb);
928                                 else if (vt_cons[i]->vc_mode != KD_GRAPHICS)
929                                          vc_cons[i].d->vc_sw->con_set_palette(vc_cons[i].d, color_table);
930                         }
931                 }
932         }
933 }
934
935     /*
936      *  Initialisation
937      */
938      
939 extern void (*prom_palette)(int);
940
941 static void __init sbusfb_init_fb(int node, int parent, int fbtype,
942                                   struct sbus_dev *sbdp)
943 {
944         struct fb_fix_screeninfo *fix;
945         struct fb_var_screeninfo *var;
946         struct display *disp;
947         struct fb_info_sbusfb *fb;
948         struct fbtype *type;
949         int linebytes, w, h, depth;
950         char *p = NULL;
951         int margin;
952
953         fb = kmalloc(sizeof(struct fb_info_sbusfb), GFP_ATOMIC);
954         if (!fb) {
955                 prom_printf("Could not allocate sbusfb structure\n");
956                 return;
957         }
958         
959         if (!prom_palette)
960                 prom_palette = sbusfb_palette;
961         
962         memset(fb, 0, sizeof(struct fb_info_sbusfb));
963         fix = &fb->fix;
964         var = &fb->var;
965         disp = &fb->disp;
966         type = &fb->type;
967         
968         spin_lock_init(&fb->lock);
969         fb->prom_node = node;
970         fb->prom_parent = parent;
971         fb->sbdp = sbdp;
972         if (sbdp)
973                 fb->iospace = sbdp->reg_addrs[0].which_io;
974
975         type->fb_type = fbtype;
976         memset(&fb->emulations, 0xff, sizeof(fb->emulations));
977         fb->emulations[0] = fbtype;
978         
979 #ifndef __sparc_v9__
980         disp->screen_base = (unsigned char *)prom_getintdefault(node, "address", 0);
981 #endif
982         
983         type->fb_height = h = prom_getintdefault(node, "height", 900);
984         type->fb_width  = w = prom_getintdefault(node, "width", 1152);
985 sizechange:
986         type->fb_depth  = depth = (fbtype == FBTYPE_SUN2BW) ? 1 : 8;
987         linebytes = prom_getintdefault(node, "linebytes", w * depth / 8);
988         type->fb_size   = PAGE_ALIGN((linebytes) * h);
989         
990         if (defx_margin < 0 || defy_margin < 0) {
991                 for (margin = 0; def_margins[margin].depth; margin++)
992                         if (w == def_margins[margin].xres &&
993                             h == def_margins[margin].yres &&
994                             depth == def_margins[margin].depth) {
995                                 fb->x_margin = def_margins[margin].x_margin;
996                                 fb->y_margin = def_margins[margin].y_margin;
997                                 break;
998                         }
999         } else {
1000                 fb->x_margin = defx_margin;
1001                 fb->y_margin = defy_margin;
1002         }
1003         fb->x_margin += ((w - 2*fb->x_margin) & 7) / 2;
1004         fb->y_margin += ((h - 2*fb->y_margin) & 15) / 2;
1005
1006         var->xres_virtual = w;
1007         var->yres_virtual = h;
1008         var->xres = w - 2*fb->x_margin;
1009         var->yres = h - 2*fb->y_margin;
1010         
1011         var->bits_per_pixel = depth;
1012         var->height = var->width = -1;
1013         var->pixclock = 10000;
1014         var->vmode = FB_VMODE_NONINTERLACED;
1015         var->red.length = var->green.length = var->blue.length = 8;
1016
1017         fix->line_length = linebytes;
1018         fix->smem_len = type->fb_size;
1019         fix->type = FB_TYPE_PACKED_PIXELS;
1020         fix->visual = FB_VISUAL_PSEUDOCOLOR;
1021         
1022         fb->info.node = -1;
1023         fb->info.fbops = &sbusfb_ops;
1024         fb->info.disp = disp;
1025         strcpy(fb->info.fontname, fontname);
1026         fb->info.changevar = NULL;
1027         fb->info.switch_con = &sbusfbcon_switch;
1028         fb->info.updatevar = &sbusfbcon_updatevar;
1029         fb->info.blank = &sbusfbcon_blank;
1030         fb->info.flags = FBINFO_FLAG_DEFAULT;
1031         
1032         fb->cursor.hwsize.fbx = 32;
1033         fb->cursor.hwsize.fby = 32;
1034         
1035         if (depth > 1 && !fb->color_map)
1036                 fb->color_map = kmalloc(256 * 3, GFP_ATOMIC);
1037                 
1038         switch(fbtype) {
1039 #ifdef CONFIG_FB_CREATOR
1040         case FBTYPE_CREATOR:
1041                 p = creatorfb_init(fb); break;
1042 #endif
1043 #ifdef CONFIG_FB_CGSIX
1044         case FBTYPE_SUNFAST_COLOR:
1045                 p = cgsixfb_init(fb); break;
1046 #endif
1047 #ifdef CONFIG_FB_CGTHREE
1048         case FBTYPE_SUN3COLOR:
1049                 p = cgthreefb_init(fb); break;
1050 #endif
1051 #ifdef CONFIG_FB_TCX
1052         case FBTYPE_TCXCOLOR:
1053                 p = tcxfb_init(fb); break;
1054 #endif
1055 #ifdef CONFIG_FB_LEO
1056         case FBTYPE_SUNLEO:
1057                 p = leofb_init(fb); break;
1058 #endif
1059 #ifdef CONFIG_FB_BWTWO
1060         case FBTYPE_SUN2BW:
1061                 p = bwtwofb_init(fb); break;
1062 #endif
1063 #ifdef CONFIG_FB_CGFOURTEEN
1064         case FBTYPE_MDICOLOR:
1065                 p = cgfourteenfb_init(fb); break;
1066 #endif
1067 #ifdef CONFIG_FB_P9100
1068         case FBTYPE_P9100COLOR:
1069                 /* Temporary crock. For now we are a cg3 */
1070                 p = p9100fb_init(fb); type->fb_type = FBTYPE_SUN3COLOR; break;
1071 #endif
1072         }
1073         
1074         if (!p) {
1075                 if (fb->color_map)
1076                         kfree(fb->color_map);
1077                 kfree(fb);
1078                 return;
1079         }
1080         
1081         if (p == SBUSFBINIT_SIZECHANGE)
1082                 goto sizechange;
1083
1084         disp->dispsw = &fb->dispsw;
1085         if (fb->setcursor) {
1086                 fb->dispsw.cursor = sbusfb_cursor;
1087                 if (curblink) {
1088                         fb->cursor.blink_rate = DEFAULT_CURSOR_BLINK_RATE;
1089                         init_timer(&fb->cursor.timer);
1090                         fb->cursor.timer.expires = jiffies + fb->cursor.blink_rate;
1091                         fb->cursor.timer.data = (unsigned long)fb;
1092                         fb->cursor.timer.function = sbusfb_cursor_timer_handler;
1093                         add_timer(&fb->cursor.timer);
1094                 }
1095         }
1096         fb->cursor.mode = CURSOR_SHAPE;
1097         fb->dispsw.set_font = sbusfb_set_font;
1098         fb->setup = fb->dispsw.setup;
1099         fb->dispsw.setup = sbusfb_disp_setup;
1100         fb->dispsw.clear_margins = NULL;
1101
1102         disp->var = *var;
1103         disp->visual = fix->visual;
1104         disp->type = fix->type;
1105         disp->type_aux = fix->type_aux;
1106         disp->line_length = fix->line_length;
1107         
1108         if (fb->blank)
1109                 disp->can_soft_blank = 1;
1110
1111         sbusfb_set_var(var, -1, &fb->info);
1112
1113         if (register_framebuffer(&fb->info) < 0) {
1114                 if (fb->color_map)
1115                         kfree(fb->color_map);
1116                 kfree(fb);
1117                 return;
1118         }
1119         printk(KERN_INFO "fb%d: %s\n", GET_FB_IDX(fb->info.node), p);
1120 }
1121
1122 static inline int known_card(char *name)
1123 {
1124         char *p;
1125         for (p = name; *p && *p != ','; p++);
1126         if (*p == ',') name = p + 1;
1127         if (!strcmp(name, "cgsix") || !strcmp(name, "cgthree+"))
1128                 return FBTYPE_SUNFAST_COLOR;
1129         if (!strcmp(name, "cgthree") || !strcmp(name, "cgRDI"))
1130                 return FBTYPE_SUN3COLOR;
1131         if (!strcmp(name, "cgfourteen"))
1132                 return FBTYPE_MDICOLOR;
1133         if (!strcmp(name, "leo"))
1134                 return FBTYPE_SUNLEO;
1135         if (!strcmp(name, "bwtwo"))
1136                 return FBTYPE_SUN2BW;
1137         if (!strcmp(name, "tcx"))
1138                 return FBTYPE_TCXCOLOR;
1139         if (!strcmp(name, "p9100"))
1140                 return FBTYPE_P9100COLOR;
1141         return FBTYPE_NOTYPE;
1142 }
1143
1144 #ifdef CONFIG_FB_CREATOR
1145 static void creator_fb_scan_siblings(int root)
1146 {
1147         int node, child;
1148
1149         child = prom_getchild(root);
1150         for (node = prom_searchsiblings(child, "SUNW,ffb"); node;
1151              node = prom_searchsiblings(prom_getsibling(node), "SUNW,ffb"))
1152                 sbusfb_init_fb(node, root, FBTYPE_CREATOR, NULL);
1153         for (node = prom_searchsiblings(child, "SUNW,afb"); node;
1154              node = prom_searchsiblings(prom_getsibling(node), "SUNW,afb"))
1155                 sbusfb_init_fb(node, root, FBTYPE_CREATOR, NULL);
1156 }
1157
1158 static void creator_fb_scan(void)
1159 {
1160         int root;
1161
1162         creator_fb_scan_siblings(prom_root_node);
1163
1164         root = prom_getchild(prom_root_node);
1165         for (root = prom_searchsiblings(root, "upa"); root;
1166              root = prom_searchsiblings(prom_getsibling(root), "upa"))
1167                 creator_fb_scan_siblings(root);
1168 }
1169 #endif
1170
1171 int __init sbusfb_init(void)
1172 {
1173         int type;
1174         struct sbus_dev *sbdp;
1175         struct sbus_bus *sbus;
1176         char prom_name[40];
1177         extern int con_is_present(void);
1178         
1179         if (!con_is_present()) return -ENXIO;
1180         
1181 #ifdef CONFIG_FB_CREATOR
1182         creator_fb_scan();
1183 #endif
1184 #ifdef CONFIG_SUN4
1185         sbusfb_init_fb(0, 0, FBTYPE_SUN2BW, NULL);
1186 #endif
1187 #if defined(CONFIG_FB_CGFOURTEEN) && !defined(__sparc_v9__)
1188         {
1189                 int root, node;
1190                 root = prom_getchild(prom_root_node);
1191                 root = prom_searchsiblings(root, "obio");
1192                 if (root && 
1193                     (node = prom_searchsiblings(prom_getchild(root), "cgfourteen"))) {
1194                         sbusfb_init_fb(node, root, FBTYPE_MDICOLOR, NULL);
1195                 }
1196         }
1197 #endif
1198         if (sbus_root == NULL)
1199                 return 0;
1200         for_all_sbusdev(sbdp, sbus) {
1201                 type = known_card(sbdp->prom_name);
1202                 if (type == FBTYPE_NOTYPE)
1203                         continue;
1204                 if (prom_getproperty(sbdp->prom_node, "emulation",
1205                                      prom_name, sizeof(prom_name)) > 0) {
1206                         type = known_card(prom_name);
1207                         if (type == FBTYPE_NOTYPE)
1208                                 type = known_card(sbdp->prom_name);
1209                 }
1210                 sbusfb_init_fb(sbdp->prom_node, sbdp->bus->prom_node, type, sbdp);
1211         }
1212         return 0;
1213 }
1214
1215 MODULE_LICENSE("GPL");