Import 2.3.18pre1
[davej-history.git] / drivers / video / sbusfb.c
blobaafa045421540b803a89e006a1f4fc9bc09ba1a5
1 /*
2 * linux/drivers/video/sbusfb.c -- SBUS or UPA based frame buffer device
4 * Copyright (C) 1998 Jakub Jelinek
6 * This driver is partly based on the Open Firmware console driver
8 * Copyright (C) 1997 Geert Uytterhoeven
10 * and SPARC console subsystem
12 * Copyright (C) 1995 Peter Zaitcev (zaitcev@lab.ipmce.su)
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)
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.
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/malloc.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>
42 #include <asm/uaccess.h>
43 #include <asm/pgtable.h> /* io_remap_page_range() */
45 #include <video/sbusfb.h>
47 #define DEFAULT_CURSOR_BLINK_RATE (2*HZ/5)
49 #define CURSOR_SHAPE 1
50 #define CURSOR_BLINK 2
53 * Interface used by the world
56 int sbusfb_init(void);
57 int sbusfb_setup(char*);
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 },
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_pan_display(struct fb_var_screeninfo *var, int con,
89 struct fb_info *info);
90 static int sbusfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
91 struct fb_info *info);
92 static int sbusfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
93 struct fb_info *info);
94 static int sbusfb_ioctl(struct inode *inode, struct file *file, u_int cmd,
95 u_long arg, int con, struct fb_info *info);
96 static void sbusfb_cursor(struct display *p, int mode, int x, int y);
97 static void sbusfb_clear_margin(struct display *p, int s);
101 * Interface to the low level console driver
104 static int sbusfbcon_switch(int con, struct fb_info *info);
105 static int sbusfbcon_updatevar(int con, struct fb_info *info);
106 static void sbusfbcon_blank(int blank, struct fb_info *info);
110 * Internal routines
113 static int sbusfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
114 u_int *transp, struct fb_info *info);
115 static int sbusfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
116 u_int transp, struct fb_info *info);
117 static void do_install_cmap(int con, struct fb_info *info);
119 static struct fb_ops sbusfb_ops = {
120 sbusfb_open, sbusfb_release, sbusfb_get_fix, sbusfb_get_var, sbusfb_set_var,
121 sbusfb_get_cmap, sbusfb_set_cmap, sbusfb_pan_display, sbusfb_ioctl, sbusfb_mmap
126 * Open/Release the frame buffer device
129 static int sbusfb_open(struct fb_info *info, int user)
131 struct fb_info_sbusfb *fb = sbusfbinfo(info);
133 if (user) {
134 if (fb->open) return -EBUSY;
135 fb->mmaped = 0;
136 fb->open = 1;
137 fb->vtconsole = -1;
138 } else
139 fb->consolecnt++;
140 MOD_INC_USE_COUNT;
141 return 0;
144 static int sbusfb_release(struct fb_info *info, int user)
146 struct fb_info_sbusfb *fb = sbusfbinfo(info);
148 if (user) {
149 if (fb->vtconsole != -1) {
150 vt_cons[fb->vtconsole]->vc_mode = KD_TEXT;
151 if (fb->mmaped) {
152 fb->graphmode--;
153 sbusfb_clear_margin(&fb_display[fb->vtconsole], 0);
156 if (fb->reset)
157 fb->reset(fb);
158 fb->open = 0;
159 } else
160 fb->consolecnt--;
161 MOD_DEC_USE_COUNT;
162 return 0;
165 static unsigned long sbusfb_mmapsize(struct fb_info_sbusfb *fb, long size)
167 if (size == SBUS_MMAP_EMPTY) return 0;
168 if (size >= 0) return size;
169 return fb->type.fb_size * (-size);
172 static int sbusfb_mmap(struct fb_info *info, struct file *file,
173 struct vm_area_struct *vma)
175 struct fb_info_sbusfb *fb = sbusfbinfo(info);
176 unsigned int size, page, r, map_size;
177 unsigned long map_offset = 0;
178 int i;
180 size = vma->vm_end - vma->vm_start;
181 if (vma->vm_offset & ~PAGE_MASK)
182 return -ENXIO;
184 /* To stop the swapper from even considering these pages */
185 vma->vm_flags |= (VM_SHM| VM_LOCKED);
187 #ifdef __sparc_v9__
188 /* Align it as much as desirable */
190 unsigned long j, alignment, s = 0;
191 int max = -1;
193 map_offset = vma->vm_offset+size;
194 for (i = 0; fb->mmap_map[i].size; i++) {
195 if (fb->mmap_map[i].voff < vma->vm_offset)
196 continue;
197 if (fb->mmap_map[i].voff >= map_offset)
198 break;
199 if (max < 0 || sbusfb_mmapsize(fb,fb->mmap_map[i].size) > s) {
200 max = i;
201 s = sbusfb_mmapsize(fb,fb->mmap_map[max].size);
204 if (max >= 0) {
205 j = s;
206 if (fb->mmap_map[max].voff + j > map_offset)
207 j = map_offset - fb->mmap_map[max].voff;
208 for (alignment = 0x400000; alignment > PAGE_SIZE; alignment >>= 3)
209 if (j >= alignment && !(fb->mmap_map[max].poff & (alignment - 1)))
210 break;
211 if (alignment > PAGE_SIZE) {
212 j = alignment;
213 alignment = j - ((vma->vm_start + fb->mmap_map[max].voff - vma->vm_offset) & (j - 1));
214 if (alignment != j) {
215 struct vm_area_struct *vmm = find_vma(current->mm, vma->vm_start);
216 if (!vmm || vmm->vm_start >= vma->vm_end + alignment) {
217 vma->vm_start += alignment;
218 vma->vm_end += alignment;
224 #endif
226 /* Each page, see which map applies */
227 for (page = 0; page < size; ){
228 map_size = 0;
229 for (i = 0; fb->mmap_map[i].size; i++)
230 if (fb->mmap_map[i].voff == vma->vm_offset+page) {
231 map_size = sbusfb_mmapsize(fb,fb->mmap_map[i].size);
232 #ifdef __sparc_v9__
233 #define POFF_MASK (PAGE_MASK|0x1UL)
234 #else
235 #define POFF_MASK (PAGE_MASK)
236 #endif
237 map_offset = (fb->physbase + fb->mmap_map[i].poff) & POFF_MASK;
238 break;
240 if (!map_size){
241 page += PAGE_SIZE;
242 continue;
244 if (page + map_size > size)
245 map_size = size - page;
246 r = io_remap_page_range (vma->vm_start+page, map_offset, map_size, vma->vm_page_prot, fb->iospace);
247 if (r)
248 return -EAGAIN;
249 page += map_size;
252 vma->vm_flags |= VM_IO;
253 if (!fb->mmaped) {
254 int lastconsole = 0;
256 if (info->display_fg)
257 lastconsole = info->display_fg->vc_num;
258 fb->mmaped = 1;
259 if (fb->consolecnt && fb_display[lastconsole].fb_info == info) {
260 fb->vtconsole = lastconsole;
261 fb->graphmode++;
262 vt_cons [lastconsole]->vc_mode = KD_GRAPHICS;
263 vc_cons[lastconsole].d->vc_sw->con_cursor(vc_cons[lastconsole].d,CM_ERASE);
264 } else if (fb->unblank && !fb->blanked)
265 (*fb->unblank)(fb);
267 return 0;
270 static void sbusfb_clear_margin(struct display *p, int s)
272 struct fb_info_sbusfb *fb = sbusfbinfod(p);
274 if (fb->switch_from_graph)
275 (*fb->switch_from_graph)(fb);
276 if (fb->fill) {
277 unsigned short rects [16];
279 rects [0] = 0;
280 rects [1] = 0;
281 rects [2] = fb->var.xres_virtual;
282 rects [3] = fb->y_margin;
283 rects [4] = 0;
284 rects [5] = fb->y_margin;
285 rects [6] = fb->x_margin;
286 rects [7] = fb->var.yres_virtual;
287 rects [8] = fb->var.xres_virtual - fb->x_margin;
288 rects [9] = fb->y_margin;
289 rects [10] = fb->var.xres_virtual;
290 rects [11] = fb->var.yres_virtual;
291 rects [12] = fb->x_margin;
292 rects [13] = fb->var.yres_virtual - fb->y_margin;
293 rects [14] = fb->var.xres_virtual - fb->x_margin;
294 rects [15] = fb->var.yres_virtual;
295 (*fb->fill)(fb, p, s, 4, rects);
296 } else {
297 unsigned char *fb_base = p->screen_base, *q;
298 int skip_bytes = fb->y_margin * fb->var.xres_virtual;
299 int scr_size = fb->var.xres_virtual * fb->var.yres_virtual;
300 int h, he, incr, size;
302 he = fb->var.yres;
303 if (fb->var.bits_per_pixel == 1) {
304 fb_base -= (skip_bytes + fb->x_margin) / 8;
305 skip_bytes /= 8;
306 scr_size /= 8;
307 mymemset (fb_base, skip_bytes - fb->x_margin / 8);
308 mymemset (fb_base + scr_size - skip_bytes + fb->x_margin / 8, skip_bytes - fb->x_margin / 8);
309 incr = fb->var.xres_virtual / 8;
310 size = fb->x_margin / 8 * 2;
311 for (q = fb_base + skip_bytes - fb->x_margin / 8, h = 0;
312 h <= he; q += incr, h++)
313 mymemset (q, size);
314 } else {
315 fb_base -= (skip_bytes + fb->x_margin);
316 memset (fb_base, attr_bgcol(p,s), skip_bytes - fb->x_margin);
317 memset (fb_base + scr_size - skip_bytes + fb->x_margin, attr_bgcol(p,s), skip_bytes - fb->x_margin);
318 incr = fb->var.xres_virtual;
319 size = fb->x_margin * 2;
320 for (q = fb_base + skip_bytes - fb->x_margin, h = 0;
321 h <= he; q += incr, h++)
322 memset (q, attr_bgcol(p,s), size);
327 static void sbusfb_disp_setup(struct display *p)
329 struct fb_info_sbusfb *fb = sbusfbinfod(p);
331 if (fb->setup)
332 fb->setup(p);
333 sbusfb_clear_margin(p, 0);
337 * Get the Fixed Part of the Display
340 static int sbusfb_get_fix(struct fb_fix_screeninfo *fix, int con,
341 struct fb_info *info)
343 struct fb_info_sbusfb *fb = sbusfbinfo(info);
345 memcpy(fix, &fb->fix, sizeof(struct fb_fix_screeninfo));
346 return 0;
350 * Get the User Defined Part of the Display
353 static int sbusfb_get_var(struct fb_var_screeninfo *var, int con,
354 struct fb_info *info)
356 struct fb_info_sbusfb *fb = sbusfbinfo(info);
358 memcpy(var, &fb->var, sizeof(struct fb_var_screeninfo));
359 return 0;
363 * Set the User Defined Part of the Display
366 static int sbusfb_set_var(struct fb_var_screeninfo *var, int con,
367 struct fb_info *info)
369 struct display *display;
370 int activate = var->activate;
372 if(con >= 0)
373 display = &fb_display[con];
374 else
375 display = info->disp;
377 /* simple check for equality until fully implemented -E */
378 if ((activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
379 if (display->var.xres != var->xres ||
380 display->var.yres != var->yres ||
381 display->var.xres_virtual != var->xres_virtual ||
382 display->var.yres_virtual != var->yres_virtual ||
383 display->var.bits_per_pixel != var->bits_per_pixel ||
384 display->var.accel_flags != var->accel_flags) {
385 return -EINVAL;
388 return 0;
393 * Pan or Wrap the Display
395 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
398 static int sbusfb_pan_display(struct fb_var_screeninfo *var, int con,
399 struct fb_info *info)
401 if (var->xoffset || var->yoffset)
402 return -EINVAL;
403 else
404 return 0;
408 * Hardware cursor
411 static int sbus_hw_scursor (struct fbcursor *cursor, struct fb_info_sbusfb *fb)
413 int op;
414 int i, bytes = 0;
415 struct fbcursor f;
416 char red[2], green[2], blue[2];
418 if (copy_from_user (&f, cursor, sizeof(struct fbcursor)))
419 return -EFAULT;
420 op = f.set;
421 if (op & FB_CUR_SETSHAPE){
422 if ((u32) f.size.fbx > fb->cursor.hwsize.fbx)
423 return -EINVAL;
424 if ((u32) f.size.fby > fb->cursor.hwsize.fby)
425 return -EINVAL;
426 if (f.size.fbx > 32)
427 bytes = f.size.fby << 3;
428 else
429 bytes = f.size.fby << 2;
431 if (op & FB_CUR_SETCMAP){
432 if (f.cmap.index || f.cmap.count != 2)
433 return -EINVAL;
434 if (copy_from_user (red, f.cmap.red, 2) ||
435 copy_from_user (green, f.cmap.green, 2) ||
436 copy_from_user (blue, f.cmap.blue, 2))
437 return -EFAULT;
439 if (op & FB_CUR_SETCMAP)
440 (*fb->setcursormap) (fb, red, green, blue);
441 if (op & FB_CUR_SETSHAPE){
442 u32 u;
444 fb->cursor.size = f.size;
445 memset ((void *)&fb->cursor.bits, 0, sizeof (fb->cursor.bits));
446 if (copy_from_user (fb->cursor.bits [0], f.mask, bytes) ||
447 copy_from_user (fb->cursor.bits [1], f.image, bytes))
448 return -EFAULT;
449 if (f.size.fbx <= 32) {
450 u = 0xffffffff << (32 - f.size.fbx);
451 for (i = fb->cursor.size.fby - 1; i >= 0; i--) {
452 fb->cursor.bits [0][i] &= u;
453 fb->cursor.bits [1][i] &= fb->cursor.bits [0][i];
455 } else {
456 u = 0xffffffff << (64 - f.size.fbx);
457 for (i = fb->cursor.size.fby - 1; i >= 0; i--) {
458 fb->cursor.bits [0][2*i+1] &= u;
459 fb->cursor.bits [1][2*i] &= fb->cursor.bits [0][2*i];
460 fb->cursor.bits [1][2*i+1] &= fb->cursor.bits [0][2*i+1];
463 (*fb->setcurshape) (fb);
465 if (op & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)){
466 if (op & FB_CUR_SETCUR)
467 fb->cursor.enable = f.enable;
468 if (op & FB_CUR_SETPOS)
469 fb->cursor.cpos = f.pos;
470 if (op & FB_CUR_SETHOT)
471 fb->cursor.chot = f.hot;
472 (*fb->setcursor) (fb);
474 return 0;
477 static unsigned char hw_cursor_cmap[2] = { 0, 0xff };
479 static void
480 sbusfb_cursor_timer_handler(unsigned long dev_addr)
482 struct fb_info_sbusfb *fb = (struct fb_info_sbusfb *)dev_addr;
484 if (!fb->setcursor) return;
486 if (fb->cursor.mode & CURSOR_BLINK) {
487 fb->cursor.enable ^= 1;
488 fb->setcursor(fb);
491 fb->cursor.timer.expires = jiffies + fb->cursor.blink_rate;
492 add_timer(&fb->cursor.timer);
495 static void sbusfb_cursor(struct display *p, int mode, int x, int y)
497 struct fb_info_sbusfb *fb = sbusfbinfod(p);
499 switch (mode) {
500 case CM_ERASE:
501 fb->cursor.mode &= ~CURSOR_BLINK;
502 fb->cursor.enable = 0;
503 (*fb->setcursor)(fb);
504 break;
506 case CM_MOVE:
507 case CM_DRAW:
508 if (fb->cursor.mode & CURSOR_SHAPE) {
509 fb->cursor.size.fbx = fontwidth(p);
510 fb->cursor.size.fby = fontheight(p);
511 fb->cursor.chot.fbx = 0;
512 fb->cursor.chot.fby = 0;
513 fb->cursor.enable = 1;
514 memset (fb->cursor.bits, 0, sizeof (fb->cursor.bits));
515 fb->cursor.bits[0][fontheight(p) - 2] = (0xffffffff << (32 - fontwidth(p)));
516 fb->cursor.bits[1][fontheight(p) - 2] = (0xffffffff << (32 - fontwidth(p)));
517 fb->cursor.bits[0][fontheight(p) - 1] = (0xffffffff << (32 - fontwidth(p)));
518 fb->cursor.bits[1][fontheight(p) - 1] = (0xffffffff << (32 - fontwidth(p)));
519 (*fb->setcursormap) (fb, hw_cursor_cmap, hw_cursor_cmap, hw_cursor_cmap);
520 (*fb->setcurshape) (fb);
522 fb->cursor.mode = CURSOR_BLINK;
523 if (fontwidthlog(p))
524 fb->cursor.cpos.fbx = (x << fontwidthlog(p)) + fb->x_margin;
525 else
526 fb->cursor.cpos.fbx = (x * fontwidth(p)) + fb->x_margin;
527 if (fontheightlog(p))
528 fb->cursor.cpos.fby = (y << fontheightlog(p)) + fb->y_margin;
529 else
530 fb->cursor.cpos.fby = (y * fontheight(p)) + fb->y_margin;
531 (*fb->setcursor)(fb);
532 break;
537 * Get the Colormap
540 static int sbusfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
541 struct fb_info *info)
543 if (!info->display_fg || con == info->display_fg->vc_num) /* current console? */
544 return fb_get_cmap(cmap, kspc, sbusfb_getcolreg, info);
545 else if (fb_display[con].cmap.len) /* non default colormap? */
546 fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
547 else
548 fb_copy_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel), cmap, kspc ? 0 : 2);
549 return 0;
553 * Set the Colormap
556 static int sbusfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
557 struct fb_info *info)
559 int err;
560 struct display *disp;
562 if (con >= 0)
563 disp = &fb_display[con];
564 else
565 disp = info->disp;
566 if (!disp->cmap.len) { /* no colormap allocated? */
567 if ((err = fb_alloc_cmap(&disp->cmap, 1<<disp->var.bits_per_pixel, 0)))
568 return err;
570 if (con == currcon) { /* current console? */
571 err = fb_set_cmap(cmap, kspc, sbusfb_setcolreg, info);
572 if (!err) {
573 struct fb_info_sbusfb *fb = sbusfbinfo(info);
575 if (fb->loadcmap)
576 (*fb->loadcmap)(fb, &fb_display[con], cmap->start, cmap->len);
578 return err;
579 } else
580 fb_copy_cmap(cmap, &disp->cmap, kspc ? 0 : 1);
581 return 0;
584 static int sbusfb_ioctl(struct inode *inode, struct file *file, u_int cmd,
585 u_long arg, int con, struct fb_info *info)
587 struct fb_info_sbusfb *fb = sbusfbinfo(info);
588 int i;
589 int lastconsole;
591 switch (cmd){
592 case FBIOGTYPE: /* return frame buffer type */
593 copy_to_user_ret((struct fbtype *)arg, &fb->type, sizeof(struct fbtype), -EFAULT);
594 break;
595 case FBIOGATTR: {
596 struct fbgattr *fba = (struct fbgattr *) arg;
598 i = verify_area (VERIFY_WRITE, (void *) arg, sizeof (struct fbgattr));
599 if (i) return i;
600 __put_user_ret(fb->emulations[0], &fba->real_type, -EFAULT);
601 __put_user_ret(0, &fba->owner, -EFAULT);
602 __copy_to_user_ret(&fba->fbtype, &fb->type,
603 sizeof(struct fbtype), -EFAULT);
604 __put_user_ret(0, &fba->sattr.flags, -EFAULT);
605 __put_user_ret(fb->type.fb_type, &fba->sattr.emu_type, -EFAULT);
606 __put_user_ret(-1, &fba->sattr.dev_specific[0], -EFAULT);
607 for (i = 0; i < 4; i++)
608 put_user_ret(fb->emulations[i], &fba->emu_types[i], -EFAULT);
609 break;
611 case FBIOSATTR:
612 i = verify_area (VERIFY_READ, (void *) arg, sizeof (struct fbsattr));
613 if (i) return i;
614 return -EINVAL;
615 case FBIOSVIDEO:
616 if (fb->consolecnt) {
617 lastconsole = info->display_fg->vc_num;
618 if (vt_cons[lastconsole]->vc_mode == KD_TEXT)
619 break;
621 get_user_ret(i, (int *)arg, -EFAULT);
622 if (i){
623 if (!fb->blanked || !fb->unblank)
624 break;
625 if (fb->consolecnt || (fb->open && fb->mmaped))
626 (*fb->unblank)(fb);
627 fb->blanked = 0;
628 } else {
629 if (fb->blanked || !fb->blank)
630 break;
631 (*fb->blank)(fb);
632 fb->blanked = 1;
634 break;
635 case FBIOGVIDEO:
636 put_user_ret(fb->blanked, (int *) arg, -EFAULT);
637 break;
638 case FBIOGETCMAP_SPARC: {
639 char *rp, *gp, *bp;
640 int end, count, index;
641 struct fbcmap *cmap;
643 if (!fb->loadcmap)
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 __get_user_ret(count, &cmap->count, -EFAULT);
649 __get_user_ret(index, &cmap->index, -EFAULT);
650 if ((index < 0) || (index > 255))
651 return -EINVAL;
652 if (index + count > 256)
653 count = 256 - index;
654 __get_user_ret(rp, &cmap->red, -EFAULT);
655 __get_user_ret(gp, &cmap->green, -EFAULT);
656 __get_user_ret(bp, &cmap->blue, -EFAULT);
657 if(verify_area (VERIFY_WRITE, rp, count)) return -EFAULT;
658 if(verify_area (VERIFY_WRITE, gp, count)) return -EFAULT;
659 if(verify_area (VERIFY_WRITE, bp, count)) return -EFAULT;
660 end = index + count;
661 for (i = index; i < end; i++){
662 __put_user_ret(fb->color_map CM(i,0), rp, -EFAULT);
663 __put_user_ret(fb->color_map CM(i,1), gp, -EFAULT);
664 __put_user_ret(fb->color_map CM(i,2), bp, -EFAULT);
665 rp++; gp++; bp++;
667 (*fb->loadcmap)(fb, NULL, index, count);
668 break;
670 case FBIOPUTCMAP_SPARC: { /* load color map entries */
671 char *rp, *gp, *bp;
672 int end, count, index;
673 struct fbcmap *cmap;
675 if (!fb->loadcmap)
676 return -EINVAL;
677 i = verify_area (VERIFY_READ, (void *) arg, sizeof (struct fbcmap));
678 if (i) return i;
679 cmap = (struct fbcmap *) arg;
680 __get_user_ret(count, &cmap->count, -EFAULT);
681 __get_user_ret(index, &cmap->index, -EFAULT);
682 if ((index < 0) || (index > 255))
683 return -EINVAL;
684 if (index + count > 256)
685 count = 256 - index;
686 __get_user_ret(rp, &cmap->red, -EFAULT);
687 __get_user_ret(gp, &cmap->green, -EFAULT);
688 __get_user_ret(bp, &cmap->blue, -EFAULT);
689 if(verify_area (VERIFY_READ, rp, count)) return -EFAULT;
690 if(verify_area (VERIFY_READ, gp, count)) return -EFAULT;
691 if(verify_area (VERIFY_READ, bp, count)) return -EFAULT;
693 end = index + count;
694 for (i = index; i < end; i++){
695 __get_user_ret(fb->color_map CM(i,0), rp, -EFAULT);
696 __get_user_ret(fb->color_map CM(i,1), gp, -EFAULT);
697 __get_user_ret(fb->color_map CM(i,2), bp, -EFAULT);
698 rp++; gp++; bp++;
700 (*fb->loadcmap)(fb, NULL, index, count);
701 break;
703 case FBIOGCURMAX: {
704 struct fbcurpos *p = (struct fbcurpos *) arg;
705 if (!fb->setcursor) return -EINVAL;
706 if(verify_area (VERIFY_WRITE, p, sizeof (struct fbcurpos)))
707 return -EFAULT;
708 __put_user_ret(fb->cursor.hwsize.fbx, &p->fbx, -EFAULT);
709 __put_user_ret(fb->cursor.hwsize.fby, &p->fby, -EFAULT);
710 break;
712 case FBIOSCURSOR:
713 if (!fb->setcursor) return -EINVAL;
714 if (fb->consolecnt) {
715 lastconsole = info->display_fg->vc_num;
716 if (vt_cons[lastconsole]->vc_mode == KD_TEXT)
717 return -EINVAL; /* Don't let graphics programs hide our nice text cursor */
718 fb->cursor.mode = CURSOR_SHAPE; /* Forget state of our text cursor */
720 return sbus_hw_scursor ((struct fbcursor *) arg, fb);
722 case FBIOSCURPOS:
723 if (!fb->setcursor) return -EINVAL;
724 /* Don't let graphics programs move our nice text cursor */
725 if (fb->consolecnt) {
726 lastconsole = info->display_fg->vc_num;
727 if (vt_cons[lastconsole]->vc_mode == KD_TEXT)
728 return -EINVAL; /* Don't let graphics programs move our nice text cursor */
730 if (copy_from_user(&fb->cursor.cpos, (void *)arg, sizeof(struct fbcurpos)))
731 return -EFAULT;
732 (*fb->setcursor) (fb);
733 break;
734 default:
735 if (fb->ioctl)
736 return fb->ioctl(fb, cmd, arg);
737 return -EINVAL;
739 return 0;
743 * Setup: parse used options
746 int __init sbusfb_setup(char *options)
748 char *p;
750 for (p = options;;) {
751 if (!strncmp(p, "nomargins", 9)) {
752 defx_margin = 0; defy_margin = 0;
753 } else if (!strncmp(p, "margins=", 8)) {
754 int i, j;
755 char *q;
757 i = simple_strtoul(p+8,&q,10);
758 if (i >= 0 && *q == 'x') {
759 j = simple_strtoul(q+1,&q,10);
760 if (j >= 0 && (*q == ' ' || !*q)) {
761 defx_margin = i; defy_margin = j;
764 } else if (!strncmp(p, "font=", 5)) {
765 int i;
767 for (i = 0; i < sizeof(fontname) - 1; i++)
768 if (p[i+5] == ' ' || !p[i+5])
769 break;
770 memcpy(fontname, p+5, i);
771 fontname[i] = 0;
772 } else if (!strncmp(p, "noblink", 7))
773 curblink = 0;
774 while (*p && *p != ' ' && *p != ',') p++;
775 if (*p != ',') break;
776 p++;
778 return 0;
781 static int sbusfbcon_switch(int con, struct fb_info *info)
783 int x_margin, y_margin;
784 struct fb_info_sbusfb *fb = sbusfbinfo(info);
785 int lastconsole;
787 /* Do we have to save the colormap? */
788 if (fb_display[currcon].cmap.len)
789 fb_get_cmap(&fb_display[currcon].cmap, 1, sbusfb_getcolreg, info);
791 if (info->display_fg) {
792 lastconsole = info->display_fg->vc_num;
793 if (lastconsole != con &&
794 (fontwidth(&fb_display[lastconsole]) != fontwidth(&fb_display[con]) ||
795 fontheight(&fb_display[lastconsole]) != fontheight(&fb_display[con])))
796 fb->cursor.mode |= CURSOR_SHAPE;
798 x_margin = (fb_display[con].var.xres_virtual - fb_display[con].var.xres) / 2;
799 y_margin = (fb_display[con].var.yres_virtual - fb_display[con].var.yres) / 2;
800 if (fb->margins)
801 fb->margins(fb, &fb_display[con], x_margin, y_margin);
802 if (fb->graphmode || fb->x_margin != x_margin || fb->y_margin != y_margin) {
803 fb->x_margin = x_margin; fb->y_margin = y_margin;
804 sbusfb_clear_margin(&fb_display[con], 0);
806 currcon = con;
807 /* Install new colormap */
808 do_install_cmap(con, info);
809 return 0;
813 * Update the `var' structure (called by fbcon.c)
816 static int sbusfbcon_updatevar(int con, struct fb_info *info)
818 /* Nothing */
819 return 0;
823 * Blank the display.
826 static void sbusfbcon_blank(int blank, struct fb_info *info)
828 struct fb_info_sbusfb *fb = sbusfbinfo(info);
830 if (blank && fb->blank)
831 return fb->blank(fb);
832 else if (!blank && fb->unblank)
833 return fb->unblank(fb);
837 * Read a single color register and split it into
838 * colors/transparent. Return != 0 for invalid regno.
841 static int sbusfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
842 u_int *transp, struct fb_info *info)
844 struct fb_info_sbusfb *fb = sbusfbinfo(info);
846 if (!fb->color_map || regno > 255)
847 return 1;
848 *red = (fb->color_map CM(regno, 0)<<8) | fb->color_map CM(regno, 0);
849 *green = (fb->color_map CM(regno, 1)<<8) | fb->color_map CM(regno, 1);
850 *blue = (fb->color_map CM(regno, 2)<<8) | fb->color_map CM(regno, 2);
851 *transp = 0;
852 return 0;
857 * Set a single color register. The values supplied are already
858 * rounded down to the hardware's capabilities (according to the
859 * entries in the var structure). Return != 0 for invalid regno.
862 static int sbusfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
863 u_int transp, struct fb_info *info)
865 struct fb_info_sbusfb *fb = sbusfbinfo(info);
867 if (!fb->color_map || regno > 255)
868 return 1;
869 red >>= 8;
870 green >>= 8;
871 blue >>= 8;
872 fb->color_map CM(regno, 0) = red;
873 fb->color_map CM(regno, 1) = green;
874 fb->color_map CM(regno, 2) = blue;
875 return 0;
879 static void do_install_cmap(int con, struct fb_info *info)
881 struct fb_info_sbusfb *fb = sbusfbinfo(info);
883 if (con != currcon)
884 return;
885 if (fb_display[con].cmap.len)
886 fb_set_cmap(&fb_display[con].cmap, 1, sbusfb_setcolreg, info);
887 else
888 fb_set_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
889 1, sbusfb_setcolreg, info);
890 if (fb->loadcmap)
891 (*fb->loadcmap)(fb, &fb_display[con], 0, 256);
894 static int sbusfb_set_font(struct display *p, int width, int height)
896 int margin;
897 int w = p->var.xres_virtual, h = p->var.yres_virtual;
898 int depth = p->var.bits_per_pixel;
899 struct fb_info_sbusfb *fb = sbusfbinfod(p);
900 int x_margin, y_margin;
902 if (depth > 8) depth = 8;
903 x_margin = 0;
904 y_margin = 0;
905 if (defx_margin < 0 || defy_margin < 0) {
906 for (margin = 0; def_margins[margin].depth; margin++)
907 if (w == def_margins[margin].xres &&
908 h == def_margins[margin].yres &&
909 depth == def_margins[margin].depth) {
910 x_margin = def_margins[margin].x_margin;
911 y_margin = def_margins[margin].y_margin;
912 break;
914 } else {
915 x_margin = defx_margin;
916 y_margin = defy_margin;
918 x_margin += ((w - 2*x_margin) % width) / 2;
919 y_margin += ((h - 2*y_margin) % height) / 2;
921 p->var.xres = w - 2*x_margin;
922 p->var.yres = h - 2*y_margin;
924 fb->cursor.mode |= CURSOR_SHAPE;
926 if (fb->margins)
927 fb->margins(fb, p, x_margin, y_margin);
928 if (fb->x_margin != x_margin || fb->y_margin != y_margin) {
929 fb->x_margin = x_margin; fb->y_margin = y_margin;
930 sbusfb_clear_margin(p, 0);
933 return 1;
936 void sbusfb_palette(int enter)
938 int i;
939 struct display *p;
941 for (i = 0; i < MAX_NR_CONSOLES; i++) {
942 p = &fb_display[i];
943 if (p->dispsw && p->dispsw->setup == sbusfb_disp_setup &&
944 p->fb_info->display_fg &&
945 p->fb_info->display_fg->vc_num == i) {
946 struct fb_info_sbusfb *fb = sbusfbinfod(p);
948 if (fb->restore_palette) {
949 if (enter)
950 fb->restore_palette(fb);
951 else if (vt_cons[i]->vc_mode != KD_GRAPHICS)
952 vc_cons[i].d->vc_sw->con_set_palette(vc_cons[i].d, color_table);
959 * Initialisation
962 extern void (*prom_palette)(int);
964 static void __init sbusfb_init_fb(int node, int parent, int fbtype,
965 struct linux_sbus_device *sbdp)
967 struct fb_fix_screeninfo *fix;
968 struct fb_var_screeninfo *var;
969 struct display *disp;
970 struct fb_info_sbusfb *fb;
971 struct fbtype *type;
972 int linebytes, w, h, depth;
973 char *p = NULL;
974 int margin;
976 fb = kmalloc(sizeof(struct fb_info_sbusfb), GFP_ATOMIC);
977 if (!fb) {
978 prom_printf("Could not allocate sbusfb structure\n");
979 return;
982 if (!prom_palette)
983 prom_palette = sbusfb_palette;
985 memset(fb, 0, sizeof(struct fb_info_sbusfb));
986 fix = &fb->fix;
987 var = &fb->var;
988 disp = &fb->disp;
989 type = &fb->type;
991 fb->prom_node = node;
992 fb->prom_parent = parent;
993 fb->sbdp = sbdp;
994 if (sbdp)
995 fb->iospace = sbdp->reg_addrs[0].which_io;
997 type->fb_type = fbtype;
998 memset(&fb->emulations, 0xff, sizeof(fb->emulations));
999 fb->emulations[0] = fbtype;
1001 #ifndef __sparc_v9__
1002 disp->screen_base = (unsigned char *)prom_getintdefault(node, "address", 0);
1003 #endif
1005 type->fb_height = h = prom_getintdefault(node, "height", 900);
1006 type->fb_width = w = prom_getintdefault(node, "width", 1152);
1007 sizechange:
1008 type->fb_depth = depth = (fbtype == FBTYPE_SUN2BW) ? 1 : 8;
1009 linebytes = prom_getintdefault(node, "linebytes", w * depth / 8);
1010 type->fb_size = PAGE_ALIGN((linebytes) * h);
1012 if (defx_margin < 0 || defy_margin < 0) {
1013 for (margin = 0; def_margins[margin].depth; margin++)
1014 if (w == def_margins[margin].xres &&
1015 h == def_margins[margin].yres &&
1016 depth == def_margins[margin].depth) {
1017 fb->x_margin = def_margins[margin].x_margin;
1018 fb->y_margin = def_margins[margin].y_margin;
1019 break;
1021 } else {
1022 fb->x_margin = defx_margin;
1023 fb->y_margin = defy_margin;
1025 fb->x_margin += ((w - 2*fb->x_margin) & 7) / 2;
1026 fb->y_margin += ((h - 2*fb->y_margin) & 15) / 2;
1028 var->xres_virtual = w;
1029 var->yres_virtual = h;
1030 var->xres = w - 2*fb->x_margin;
1031 var->yres = h - 2*fb->y_margin;
1033 var->bits_per_pixel = depth;
1034 var->height = var->width = -1;
1035 var->pixclock = 10000;
1036 var->vmode = FB_VMODE_NONINTERLACED;
1037 var->red.length = var->green.length = var->blue.length = 8;
1039 fix->line_length = linebytes;
1040 fix->smem_len = type->fb_size;
1041 fix->type = FB_TYPE_PACKED_PIXELS;
1042 fix->visual = FB_VISUAL_PSEUDOCOLOR;
1044 fb->info.node = -1;
1045 fb->info.fbops = &sbusfb_ops;
1046 fb->info.disp = disp;
1047 strcpy(fb->info.fontname, fontname);
1048 fb->info.changevar = NULL;
1049 fb->info.switch_con = &sbusfbcon_switch;
1050 fb->info.updatevar = &sbusfbcon_updatevar;
1051 fb->info.blank = &sbusfbcon_blank;
1052 fb->info.flags = FBINFO_FLAG_DEFAULT;
1054 fb->cursor.hwsize.fbx = 32;
1055 fb->cursor.hwsize.fby = 32;
1057 if (depth > 1 && !fb->color_map)
1058 fb->color_map = kmalloc(256 * 3, GFP_ATOMIC);
1060 switch(fbtype) {
1061 #ifdef CONFIG_FB_CREATOR
1062 case FBTYPE_CREATOR:
1063 p = creatorfb_init(fb); break;
1064 #endif
1065 #ifdef CONFIG_FB_CGSIX
1066 case FBTYPE_SUNFAST_COLOR:
1067 p = cgsixfb_init(fb); break;
1068 #endif
1069 #ifdef CONFIG_FB_CGTHREE
1070 case FBTYPE_SUN3COLOR:
1071 p = cgthreefb_init(fb); break;
1072 #endif
1073 #ifdef CONFIG_FB_TCX
1074 case FBTYPE_TCXCOLOR:
1075 p = tcxfb_init(fb); break;
1076 #endif
1077 #ifdef CONFIG_FB_LEO
1078 case FBTYPE_SUNLEO:
1079 p = leofb_init(fb); break;
1080 #endif
1081 #ifdef CONFIG_FB_BWTWO
1082 case FBTYPE_SUN2BW:
1083 p = bwtwofb_init(fb); break;
1084 #endif
1085 #ifdef CONFIG_FB_CGFOURTEEN
1086 case FBTYPE_MDICOLOR:
1087 p = cgfourteenfb_init(fb); break;
1088 #endif
1089 #ifdef CONFIG_FB_P9100
1090 case FBTYPE_P9100COLOR:
1091 /* Temporary crock. For now we are a cg3 */
1092 p = p9100fb_init(fb); type->fb_type = FBTYPE_SUN3COLOR; break;
1093 #endif
1096 if (!p) {
1097 kfree(fb);
1098 return;
1101 if (p == SBUSFBINIT_SIZECHANGE)
1102 goto sizechange;
1104 disp->dispsw = &fb->dispsw;
1105 if (fb->setcursor) {
1106 fb->dispsw.cursor = sbusfb_cursor;
1107 if (curblink) {
1108 fb->cursor.blink_rate = DEFAULT_CURSOR_BLINK_RATE;
1109 init_timer(&fb->cursor.timer);
1110 fb->cursor.timer.expires = jiffies + fb->cursor.blink_rate;
1111 fb->cursor.timer.data = (unsigned long)fb;
1112 fb->cursor.timer.function = sbusfb_cursor_timer_handler;
1113 add_timer(&fb->cursor.timer);
1116 fb->cursor.mode = CURSOR_SHAPE;
1117 fb->dispsw.set_font = sbusfb_set_font;
1118 fb->setup = fb->dispsw.setup;
1119 fb->dispsw.setup = sbusfb_disp_setup;
1120 fb->dispsw.clear_margins = NULL;
1122 disp->var = *var;
1123 disp->visual = fix->visual;
1124 disp->type = fix->type;
1125 disp->type_aux = fix->type_aux;
1126 disp->line_length = fix->line_length;
1128 if (fb->blank)
1129 disp->can_soft_blank = 1;
1131 sbusfb_set_var(var, -1, &fb->info);
1133 if (register_framebuffer(&fb->info) < 0) {
1134 kfree(fb);
1135 return;
1137 printk(KERN_INFO "fb%d: %s\n", GET_FB_IDX(fb->info.node), p);
1140 static inline int known_card(char *name)
1142 char *p;
1143 for (p = name; *p && *p != ','; p++);
1144 if (*p == ',') name = p + 1;
1145 if (!strcmp(name, "cgsix") || !strcmp(name, "cgthree+"))
1146 return FBTYPE_SUNFAST_COLOR;
1147 if (!strcmp(name, "cgthree") || !strcmp(name, "cgRDI"))
1148 return FBTYPE_SUN3COLOR;
1149 if (!strcmp(name, "cgfourteen"))
1150 return FBTYPE_MDICOLOR;
1151 if (!strcmp(name, "leo"))
1152 return FBTYPE_SUNLEO;
1153 if (!strcmp(name, "bwtwo"))
1154 return FBTYPE_SUN2BW;
1155 if (!strcmp(name, "tcx"))
1156 return FBTYPE_TCXCOLOR;
1157 if (!strcmp(name, "p9100"))
1158 return FBTYPE_P9100COLOR;
1159 return FBTYPE_NOTYPE;
1162 int __init sbusfb_init(void)
1164 int type;
1165 struct linux_sbus_device *sbdp;
1166 struct linux_sbus *sbus;
1167 char prom_name[40];
1168 extern int con_is_present(void);
1170 if (!con_is_present()) return -ENXIO;
1172 #ifdef CONFIG_FB_CREATOR
1174 int root, node;
1175 root = prom_getchild(prom_root_node);
1176 for (node = prom_searchsiblings(root, "SUNW,ffb"); node;
1177 node = prom_searchsiblings(prom_getsibling(node), "SUNW,ffb"))
1178 sbusfb_init_fb(node, prom_root_node, FBTYPE_CREATOR, NULL);
1179 for (node = prom_searchsiblings(root, "SUNW,afb"); node;
1180 node = prom_searchsiblings(prom_getsibling(node), "SUNW,afb"))
1181 sbusfb_init_fb(node, prom_root_node, FBTYPE_CREATOR, NULL);
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__)
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);
1197 #endif
1198 if (!SBus_chain) return 0;
1199 for_all_sbusdev(sbdp, sbus) {
1200 type = known_card(sbdp->prom_name);
1201 if (type == FBTYPE_NOTYPE) continue;
1202 if (prom_getproperty(sbdp->prom_node, "emulation", prom_name, sizeof(prom_name)) > 0) {
1203 type = known_card(prom_name);
1204 if (type == FBTYPE_NOTYPE) type = known_card(sbdp->prom_name);
1206 prom_apply_sbus_ranges(sbdp->my_bus, &sbdp->reg_addrs[0],
1207 sbdp->num_registers, sbdp);
1208 sbusfb_init_fb(sbdp->prom_node, sbdp->my_bus->prom_node, type, sbdp);
1210 return 0;