uvesafb: don't treat valid modes returned byfb_find_mode() as errors
[linux-2.6/mini2440.git] / drivers / video / uvesafb.c
blobc4f4d76163171d5b8111d282d576feddafee9c38
1 /*
2 * A framebuffer driver for VBE 2.0+ compliant video cards
4 * (c) 2007 Michal Januszewski <spock@gentoo.org>
5 * Loosely based upon the vesafb driver.
7 */
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/skbuff.h>
12 #include <linux/timer.h>
13 #include <linux/completion.h>
14 #include <linux/connector.h>
15 #include <linux/random.h>
16 #include <linux/platform_device.h>
17 #include <linux/limits.h>
18 #include <linux/fb.h>
19 #include <linux/io.h>
20 #include <linux/mutex.h>
21 #include <video/edid.h>
22 #include <video/uvesafb.h>
23 #ifdef CONFIG_X86
24 #include <video/vga.h>
25 #endif
26 #ifdef CONFIG_MTRR
27 #include <asm/mtrr.h>
28 #endif
29 #include "edid.h"
31 static struct cb_id uvesafb_cn_id = {
32 .idx = CN_IDX_V86D,
33 .val = CN_VAL_V86D_UVESAFB
35 static char v86d_path[PATH_MAX] = "/sbin/v86d";
36 static char v86d_started; /* has v86d been started by uvesafb? */
38 static struct fb_fix_screeninfo uvesafb_fix __devinitdata = {
39 .id = "VESA VGA",
40 .type = FB_TYPE_PACKED_PIXELS,
41 .accel = FB_ACCEL_NONE,
42 .visual = FB_VISUAL_TRUECOLOR,
45 static int mtrr __devinitdata = 3; /* enable mtrr by default */
46 static int blank = 1; /* enable blanking by default */
47 static int ypan = 1; /* 0: scroll, 1: ypan, 2: ywrap */
48 static int pmi_setpal __devinitdata = 1; /* use PMI for palette changes */
49 static int nocrtc __devinitdata; /* ignore CRTC settings */
50 static int noedid __devinitdata; /* don't try DDC transfers */
51 static int vram_remap __devinitdata; /* set amt. of memory to be used */
52 static int vram_total __devinitdata; /* set total amount of memory */
53 static u16 maxclk __devinitdata; /* maximum pixel clock */
54 static u16 maxvf __devinitdata; /* maximum vertical frequency */
55 static u16 maxhf __devinitdata; /* maximum horizontal frequency */
56 static u16 vbemode __devinitdata; /* force use of a specific VBE mode */
57 static char *mode_option __devinitdata;
59 static struct uvesafb_ktask *uvfb_tasks[UVESAFB_TASKS_MAX];
60 static DEFINE_MUTEX(uvfb_lock);
63 * A handler for replies from userspace.
65 * Make sure each message passes consistency checks and if it does,
66 * find the kernel part of the task struct, copy the registers and
67 * the buffer contents and then complete the task.
69 static void uvesafb_cn_callback(void *data)
71 struct cn_msg *msg = data;
72 struct uvesafb_task *utask;
73 struct uvesafb_ktask *task;
75 if (msg->seq >= UVESAFB_TASKS_MAX)
76 return;
78 mutex_lock(&uvfb_lock);
79 task = uvfb_tasks[msg->seq];
81 if (!task || msg->ack != task->ack) {
82 mutex_unlock(&uvfb_lock);
83 return;
86 utask = (struct uvesafb_task *)msg->data;
88 /* Sanity checks for the buffer length. */
89 if (task->t.buf_len < utask->buf_len ||
90 utask->buf_len > msg->len - sizeof(*utask)) {
91 mutex_unlock(&uvfb_lock);
92 return;
95 uvfb_tasks[msg->seq] = NULL;
96 mutex_unlock(&uvfb_lock);
98 memcpy(&task->t, utask, sizeof(*utask));
100 if (task->t.buf_len && task->buf)
101 memcpy(task->buf, utask + 1, task->t.buf_len);
103 complete(task->done);
104 return;
107 static int uvesafb_helper_start(void)
109 char *envp[] = {
110 "HOME=/",
111 "PATH=/sbin:/bin",
112 NULL,
115 char *argv[] = {
116 v86d_path,
117 NULL,
120 return call_usermodehelper(v86d_path, argv, envp, 1);
124 * Execute a uvesafb task.
126 * Returns 0 if the task is executed successfully.
128 * A message sent to the userspace consists of the uvesafb_task
129 * struct and (optionally) a buffer. The uvesafb_task struct is
130 * a simplified version of uvesafb_ktask (its kernel counterpart)
131 * containing only the register values, flags and the length of
132 * the buffer.
134 * Each message is assigned a sequence number (increased linearly)
135 * and a random ack number. The sequence number is used as a key
136 * for the uvfb_tasks array which holds pointers to uvesafb_ktask
137 * structs for all requests.
139 static int uvesafb_exec(struct uvesafb_ktask *task)
141 static int seq;
142 struct cn_msg *m;
143 int err;
144 int len = sizeof(task->t) + task->t.buf_len;
147 * Check whether the message isn't longer than the maximum
148 * allowed by connector.
150 if (sizeof(*m) + len > CONNECTOR_MAX_MSG_SIZE) {
151 printk(KERN_WARNING "uvesafb: message too long (%d), "
152 "can't execute task\n", (int)(sizeof(*m) + len));
153 return -E2BIG;
156 m = kzalloc(sizeof(*m) + len, GFP_KERNEL);
157 if (!m)
158 return -ENOMEM;
160 init_completion(task->done);
162 memcpy(&m->id, &uvesafb_cn_id, sizeof(m->id));
163 m->seq = seq;
164 m->len = len;
165 m->ack = random32();
167 /* uvesafb_task structure */
168 memcpy(m + 1, &task->t, sizeof(task->t));
170 /* Buffer */
171 memcpy((u8 *)(m + 1) + sizeof(task->t), task->buf, task->t.buf_len);
174 * Save the message ack number so that we can find the kernel
175 * part of this task when a reply is received from userspace.
177 task->ack = m->ack;
179 mutex_lock(&uvfb_lock);
181 /* If all slots are taken -- bail out. */
182 if (uvfb_tasks[seq]) {
183 mutex_unlock(&uvfb_lock);
184 return -EBUSY;
187 /* Save a pointer to the kernel part of the task struct. */
188 uvfb_tasks[seq] = task;
189 mutex_unlock(&uvfb_lock);
191 err = cn_netlink_send(m, 0, gfp_any());
192 if (err == -ESRCH) {
194 * Try to start the userspace helper if sending
195 * the request failed the first time.
197 err = uvesafb_helper_start();
198 if (err) {
199 printk(KERN_ERR "uvesafb: failed to execute %s\n",
200 v86d_path);
201 printk(KERN_ERR "uvesafb: make sure that the v86d "
202 "helper is installed and executable\n");
203 } else {
204 v86d_started = 1;
205 err = cn_netlink_send(m, 0, gfp_any());
208 kfree(m);
210 if (!err && !(task->t.flags & TF_EXIT))
211 err = !wait_for_completion_timeout(task->done,
212 msecs_to_jiffies(UVESAFB_TIMEOUT));
214 mutex_lock(&uvfb_lock);
215 uvfb_tasks[seq] = NULL;
216 mutex_unlock(&uvfb_lock);
218 seq++;
219 if (seq >= UVESAFB_TASKS_MAX)
220 seq = 0;
222 return err;
226 * Free a uvesafb_ktask struct.
228 static void uvesafb_free(struct uvesafb_ktask *task)
230 if (task) {
231 if (task->done)
232 kfree(task->done);
233 kfree(task);
238 * Prepare a uvesafb_ktask struct to be used again.
240 static void uvesafb_reset(struct uvesafb_ktask *task)
242 struct completion *cpl = task->done;
244 memset(task, 0, sizeof(*task));
245 task->done = cpl;
249 * Allocate and prepare a uvesafb_ktask struct.
251 static struct uvesafb_ktask *uvesafb_prep(void)
253 struct uvesafb_ktask *task;
255 task = kzalloc(sizeof(*task), GFP_KERNEL);
256 if (task) {
257 task->done = kzalloc(sizeof(*task->done), GFP_KERNEL);
258 if (!task->done) {
259 kfree(task);
260 task = NULL;
263 return task;
266 static void uvesafb_setup_var(struct fb_var_screeninfo *var,
267 struct fb_info *info, struct vbe_mode_ib *mode)
269 struct uvesafb_par *par = info->par;
271 var->vmode = FB_VMODE_NONINTERLACED;
272 var->sync = FB_SYNC_VERT_HIGH_ACT;
274 var->xres = mode->x_res;
275 var->yres = mode->y_res;
276 var->xres_virtual = mode->x_res;
277 var->yres_virtual = (par->ypan) ?
278 info->fix.smem_len / mode->bytes_per_scan_line :
279 mode->y_res;
280 var->xoffset = 0;
281 var->yoffset = 0;
282 var->bits_per_pixel = mode->bits_per_pixel;
284 if (var->bits_per_pixel == 15)
285 var->bits_per_pixel = 16;
287 if (var->bits_per_pixel > 8) {
288 var->red.offset = mode->red_off;
289 var->red.length = mode->red_len;
290 var->green.offset = mode->green_off;
291 var->green.length = mode->green_len;
292 var->blue.offset = mode->blue_off;
293 var->blue.length = mode->blue_len;
294 var->transp.offset = mode->rsvd_off;
295 var->transp.length = mode->rsvd_len;
296 } else {
297 var->red.offset = 0;
298 var->green.offset = 0;
299 var->blue.offset = 0;
300 var->transp.offset = 0;
303 * We're assuming that we can switch the DAC to 8 bits. If
304 * this proves to be incorrect, we'll update the fields
305 * later in set_par().
307 if (par->vbe_ib.capabilities & VBE_CAP_CAN_SWITCH_DAC) {
308 var->red.length = 8;
309 var->green.length = 8;
310 var->blue.length = 8;
311 var->transp.length = 0;
312 } else {
313 var->red.length = 6;
314 var->green.length = 6;
315 var->blue.length = 6;
316 var->transp.length = 0;
321 static int uvesafb_vbe_find_mode(struct uvesafb_par *par,
322 int xres, int yres, int depth, unsigned char flags)
324 int i, match = -1, h = 0, d = 0x7fffffff;
326 for (i = 0; i < par->vbe_modes_cnt; i++) {
327 h = abs(par->vbe_modes[i].x_res - xres) +
328 abs(par->vbe_modes[i].y_res - yres) +
329 abs(depth - par->vbe_modes[i].depth);
332 * We have an exact match in terms of resolution
333 * and depth.
335 if (h == 0)
336 return i;
338 if (h < d || (h == d && par->vbe_modes[i].depth > depth)) {
339 d = h;
340 match = i;
343 i = 1;
345 if (flags & UVESAFB_EXACT_DEPTH &&
346 par->vbe_modes[match].depth != depth)
347 i = 0;
349 if (flags & UVESAFB_EXACT_RES && d > 24)
350 i = 0;
352 if (i != 0)
353 return match;
354 else
355 return -1;
358 static u8 *uvesafb_vbe_state_save(struct uvesafb_par *par)
360 struct uvesafb_ktask *task;
361 u8 *state;
362 int err;
364 if (!par->vbe_state_size)
365 return NULL;
367 state = kmalloc(par->vbe_state_size, GFP_KERNEL);
368 if (!state)
369 return NULL;
371 task = uvesafb_prep();
372 if (!task) {
373 kfree(state);
374 return NULL;
377 task->t.regs.eax = 0x4f04;
378 task->t.regs.ecx = 0x000f;
379 task->t.regs.edx = 0x0001;
380 task->t.flags = TF_BUF_RET | TF_BUF_ESBX;
381 task->t.buf_len = par->vbe_state_size;
382 task->buf = state;
383 err = uvesafb_exec(task);
385 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
386 printk(KERN_WARNING "uvesafb: VBE get state call "
387 "failed (eax=0x%x, err=%d)\n",
388 task->t.regs.eax, err);
389 kfree(state);
390 state = NULL;
393 uvesafb_free(task);
394 return state;
397 static void uvesafb_vbe_state_restore(struct uvesafb_par *par, u8 *state_buf)
399 struct uvesafb_ktask *task;
400 int err;
402 if (!state_buf)
403 return;
405 task = uvesafb_prep();
406 if (!task)
407 return;
409 task->t.regs.eax = 0x4f04;
410 task->t.regs.ecx = 0x000f;
411 task->t.regs.edx = 0x0002;
412 task->t.buf_len = par->vbe_state_size;
413 task->t.flags = TF_BUF_ESBX;
414 task->buf = state_buf;
416 err = uvesafb_exec(task);
417 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
418 printk(KERN_WARNING "uvesafb: VBE state restore call "
419 "failed (eax=0x%x, err=%d)\n",
420 task->t.regs.eax, err);
422 uvesafb_free(task);
425 static int __devinit uvesafb_vbe_getinfo(struct uvesafb_ktask *task,
426 struct uvesafb_par *par)
428 int err;
430 task->t.regs.eax = 0x4f00;
431 task->t.flags = TF_VBEIB;
432 task->t.buf_len = sizeof(struct vbe_ib);
433 task->buf = &par->vbe_ib;
434 strncpy(par->vbe_ib.vbe_signature, "VBE2", 4);
436 err = uvesafb_exec(task);
437 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
438 printk(KERN_ERR "uvesafb: Getting VBE info block failed "
439 "(eax=0x%x, err=%d)\n", (u32)task->t.regs.eax,
440 err);
441 return -EINVAL;
444 if (par->vbe_ib.vbe_version < 0x0200) {
445 printk(KERN_ERR "uvesafb: Sorry, pre-VBE 2.0 cards are "
446 "not supported.\n");
447 return -EINVAL;
450 if (!par->vbe_ib.mode_list_ptr) {
451 printk(KERN_ERR "uvesafb: Missing mode list!\n");
452 return -EINVAL;
455 printk(KERN_INFO "uvesafb: ");
458 * Convert string pointers and the mode list pointer into
459 * usable addresses. Print informational messages about the
460 * video adapter and its vendor.
462 if (par->vbe_ib.oem_vendor_name_ptr)
463 printk("%s, ",
464 ((char *)task->buf) + par->vbe_ib.oem_vendor_name_ptr);
466 if (par->vbe_ib.oem_product_name_ptr)
467 printk("%s, ",
468 ((char *)task->buf) + par->vbe_ib.oem_product_name_ptr);
470 if (par->vbe_ib.oem_product_rev_ptr)
471 printk("%s, ",
472 ((char *)task->buf) + par->vbe_ib.oem_product_rev_ptr);
474 if (par->vbe_ib.oem_string_ptr)
475 printk("OEM: %s, ",
476 ((char *)task->buf) + par->vbe_ib.oem_string_ptr);
478 printk("VBE v%d.%d\n", ((par->vbe_ib.vbe_version & 0xff00) >> 8),
479 par->vbe_ib.vbe_version & 0xff);
481 return 0;
484 static int __devinit uvesafb_vbe_getmodes(struct uvesafb_ktask *task,
485 struct uvesafb_par *par)
487 int off = 0, err;
488 u16 *mode;
490 par->vbe_modes_cnt = 0;
492 /* Count available modes. */
493 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
494 while (*mode != 0xffff) {
495 par->vbe_modes_cnt++;
496 mode++;
499 par->vbe_modes = kzalloc(sizeof(struct vbe_mode_ib) *
500 par->vbe_modes_cnt, GFP_KERNEL);
501 if (!par->vbe_modes)
502 return -ENOMEM;
504 /* Get info about all available modes. */
505 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
506 while (*mode != 0xffff) {
507 struct vbe_mode_ib *mib;
509 uvesafb_reset(task);
510 task->t.regs.eax = 0x4f01;
511 task->t.regs.ecx = (u32) *mode;
512 task->t.flags = TF_BUF_RET | TF_BUF_ESDI;
513 task->t.buf_len = sizeof(struct vbe_mode_ib);
514 task->buf = par->vbe_modes + off;
516 err = uvesafb_exec(task);
517 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
518 printk(KERN_ERR "uvesafb: Getting mode info block "
519 "for mode 0x%x failed (eax=0x%x, err=%d)\n",
520 *mode, (u32)task->t.regs.eax, err);
521 return -EINVAL;
524 mib = task->buf;
525 mib->mode_id = *mode;
528 * We only want modes that are supported with the current
529 * hardware configuration, color, graphics and that have
530 * support for the LFB.
532 if ((mib->mode_attr & VBE_MODE_MASK) == VBE_MODE_MASK &&
533 mib->bits_per_pixel >= 8)
534 off++;
535 else
536 par->vbe_modes_cnt--;
538 mode++;
539 mib->depth = mib->red_len + mib->green_len + mib->blue_len;
542 * Handle 8bpp modes and modes with broken color component
543 * lengths.
545 if (mib->depth == 0 || (mib->depth == 24 &&
546 mib->bits_per_pixel == 32))
547 mib->depth = mib->bits_per_pixel;
550 return 0;
554 * The Protected Mode Interface is 32-bit x86 code, so we only run it on
555 * x86 and not x86_64.
557 #ifdef CONFIG_X86_32
558 static int __devinit uvesafb_vbe_getpmi(struct uvesafb_ktask *task,
559 struct uvesafb_par *par)
561 int i, err;
563 uvesafb_reset(task);
564 task->t.regs.eax = 0x4f0a;
565 task->t.regs.ebx = 0x0;
566 err = uvesafb_exec(task);
568 if ((task->t.regs.eax & 0xffff) != 0x4f || task->t.regs.es < 0xc000) {
569 par->pmi_setpal = par->ypan = 0;
570 } else {
571 par->pmi_base = (u16 *)phys_to_virt(((u32)task->t.regs.es << 4)
572 + task->t.regs.edi);
573 par->pmi_start = (u8 *)par->pmi_base + par->pmi_base[1];
574 par->pmi_pal = (u8 *)par->pmi_base + par->pmi_base[2];
575 printk(KERN_INFO "uvesafb: protected mode interface info at "
576 "%04x:%04x\n",
577 (u16)task->t.regs.es, (u16)task->t.regs.edi);
578 printk(KERN_INFO "uvesafb: pmi: set display start = %p, "
579 "set palette = %p\n", par->pmi_start,
580 par->pmi_pal);
582 if (par->pmi_base[3]) {
583 printk(KERN_INFO "uvesafb: pmi: ports = ");
584 for (i = par->pmi_base[3]/2;
585 par->pmi_base[i] != 0xffff; i++)
586 printk("%x ", par->pmi_base[i]);
587 printk("\n");
589 if (par->pmi_base[i] != 0xffff) {
590 printk(KERN_INFO "uvesafb: can't handle memory"
591 " requests, pmi disabled\n");
592 par->ypan = par->pmi_setpal = 0;
596 return 0;
598 #endif /* CONFIG_X86_32 */
601 * Check whether a video mode is supported by the Video BIOS and is
602 * compatible with the monitor limits.
604 static int __devinit uvesafb_is_valid_mode(struct fb_videomode *mode,
605 struct fb_info *info)
607 if (info->monspecs.gtf) {
608 fb_videomode_to_var(&info->var, mode);
609 if (fb_validate_mode(&info->var, info))
610 return 0;
613 if (uvesafb_vbe_find_mode(info->par, mode->xres, mode->yres, 8,
614 UVESAFB_EXACT_RES) == -1)
615 return 0;
617 return 1;
620 static int __devinit uvesafb_vbe_getedid(struct uvesafb_ktask *task,
621 struct fb_info *info)
623 struct uvesafb_par *par = info->par;
624 int err = 0;
626 if (noedid || par->vbe_ib.vbe_version < 0x0300)
627 return -EINVAL;
629 task->t.regs.eax = 0x4f15;
630 task->t.regs.ebx = 0;
631 task->t.regs.ecx = 0;
632 task->t.buf_len = 0;
633 task->t.flags = 0;
635 err = uvesafb_exec(task);
637 if ((task->t.regs.eax & 0xffff) != 0x004f || err)
638 return -EINVAL;
640 if ((task->t.regs.ebx & 0x3) == 3) {
641 printk(KERN_INFO "uvesafb: VBIOS/hardware supports both "
642 "DDC1 and DDC2 transfers\n");
643 } else if ((task->t.regs.ebx & 0x3) == 2) {
644 printk(KERN_INFO "uvesafb: VBIOS/hardware supports DDC2 "
645 "transfers\n");
646 } else if ((task->t.regs.ebx & 0x3) == 1) {
647 printk(KERN_INFO "uvesafb: VBIOS/hardware supports DDC1 "
648 "transfers\n");
649 } else {
650 printk(KERN_INFO "uvesafb: VBIOS/hardware doesn't support "
651 "DDC transfers\n");
652 return -EINVAL;
655 task->t.regs.eax = 0x4f15;
656 task->t.regs.ebx = 1;
657 task->t.regs.ecx = task->t.regs.edx = 0;
658 task->t.flags = TF_BUF_RET | TF_BUF_ESDI;
659 task->t.buf_len = EDID_LENGTH;
660 task->buf = kzalloc(EDID_LENGTH, GFP_KERNEL);
662 err = uvesafb_exec(task);
664 if ((task->t.regs.eax & 0xffff) == 0x004f && !err) {
665 fb_edid_to_monspecs(task->buf, &info->monspecs);
667 if (info->monspecs.vfmax && info->monspecs.hfmax) {
669 * If the maximum pixel clock wasn't specified in
670 * the EDID block, set it to 300 MHz.
672 if (info->monspecs.dclkmax == 0)
673 info->monspecs.dclkmax = 300 * 1000000;
674 info->monspecs.gtf = 1;
676 } else {
677 err = -EINVAL;
680 kfree(task->buf);
681 return err;
684 static void __devinit uvesafb_vbe_getmonspecs(struct uvesafb_ktask *task,
685 struct fb_info *info)
687 struct uvesafb_par *par = info->par;
688 int i;
690 memset(&info->monspecs, 0, sizeof(info->monspecs));
693 * If we don't get all necessary data from the EDID block,
694 * mark it as incompatible with the GTF and set nocrtc so
695 * that we always use the default BIOS refresh rate.
697 if (uvesafb_vbe_getedid(task, info)) {
698 info->monspecs.gtf = 0;
699 par->nocrtc = 1;
702 /* Kernel command line overrides. */
703 if (maxclk)
704 info->monspecs.dclkmax = maxclk * 1000000;
705 if (maxvf)
706 info->monspecs.vfmax = maxvf;
707 if (maxhf)
708 info->monspecs.hfmax = maxhf * 1000;
711 * In case DDC transfers are not supported, the user can provide
712 * monitor limits manually. Lower limits are set to "safe" values.
714 if (info->monspecs.gtf == 0 && maxclk && maxvf && maxhf) {
715 info->monspecs.dclkmin = 0;
716 info->monspecs.vfmin = 60;
717 info->monspecs.hfmin = 29000;
718 info->monspecs.gtf = 1;
719 par->nocrtc = 0;
722 if (info->monspecs.gtf)
723 printk(KERN_INFO
724 "uvesafb: monitor limits: vf = %d Hz, hf = %d kHz, "
725 "clk = %d MHz\n", info->monspecs.vfmax,
726 (int)(info->monspecs.hfmax / 1000),
727 (int)(info->monspecs.dclkmax / 1000000));
728 else
729 printk(KERN_INFO "uvesafb: no monitor limits have been set, "
730 "default refresh rate will be used\n");
732 /* Add VBE modes to the modelist. */
733 for (i = 0; i < par->vbe_modes_cnt; i++) {
734 struct fb_var_screeninfo var;
735 struct vbe_mode_ib *mode;
736 struct fb_videomode vmode;
738 mode = &par->vbe_modes[i];
739 memset(&var, 0, sizeof(var));
741 var.xres = mode->x_res;
742 var.yres = mode->y_res;
744 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, &var, info);
745 fb_var_to_videomode(&vmode, &var);
746 fb_add_videomode(&vmode, &info->modelist);
749 /* Add valid VESA modes to our modelist. */
750 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
751 if (uvesafb_is_valid_mode((struct fb_videomode *)
752 &vesa_modes[i], info))
753 fb_add_videomode(&vesa_modes[i], &info->modelist);
756 for (i = 0; i < info->monspecs.modedb_len; i++) {
757 if (uvesafb_is_valid_mode(&info->monspecs.modedb[i], info))
758 fb_add_videomode(&info->monspecs.modedb[i],
759 &info->modelist);
762 return;
765 static void __devinit uvesafb_vbe_getstatesize(struct uvesafb_ktask *task,
766 struct uvesafb_par *par)
768 int err;
770 uvesafb_reset(task);
773 * Get the VBE state buffer size. We want all available
774 * hardware state data (CL = 0x0f).
776 task->t.regs.eax = 0x4f04;
777 task->t.regs.ecx = 0x000f;
778 task->t.regs.edx = 0x0000;
779 task->t.flags = 0;
781 err = uvesafb_exec(task);
783 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
784 printk(KERN_WARNING "uvesafb: VBE state buffer size "
785 "cannot be determined (eax=0x%x, err=%d)\n",
786 task->t.regs.eax, err);
787 par->vbe_state_size = 0;
788 return;
791 par->vbe_state_size = 64 * (task->t.regs.ebx & 0xffff);
794 static int __devinit uvesafb_vbe_init(struct fb_info *info)
796 struct uvesafb_ktask *task = NULL;
797 struct uvesafb_par *par = info->par;
798 int err;
800 task = uvesafb_prep();
801 if (!task)
802 return -ENOMEM;
804 err = uvesafb_vbe_getinfo(task, par);
805 if (err)
806 goto out;
808 err = uvesafb_vbe_getmodes(task, par);
809 if (err)
810 goto out;
812 par->nocrtc = nocrtc;
813 #ifdef CONFIG_X86_32
814 par->pmi_setpal = pmi_setpal;
815 par->ypan = ypan;
817 if (par->pmi_setpal || par->ypan)
818 uvesafb_vbe_getpmi(task, par);
819 #else
820 /* The protected mode interface is not available on non-x86. */
821 par->pmi_setpal = par->ypan = 0;
822 #endif
824 INIT_LIST_HEAD(&info->modelist);
825 uvesafb_vbe_getmonspecs(task, info);
826 uvesafb_vbe_getstatesize(task, par);
828 out: uvesafb_free(task);
829 return err;
832 static int __devinit uvesafb_vbe_init_mode(struct fb_info *info)
834 struct list_head *pos;
835 struct fb_modelist *modelist;
836 struct fb_videomode *mode;
837 struct uvesafb_par *par = info->par;
838 int i, modeid;
840 /* Has the user requested a specific VESA mode? */
841 if (vbemode) {
842 for (i = 0; i < par->vbe_modes_cnt; i++) {
843 if (par->vbe_modes[i].mode_id == vbemode) {
844 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
845 &info->var, info);
847 * With pixclock set to 0, the default BIOS
848 * timings will be used in set_par().
850 info->var.pixclock = 0;
851 modeid = i;
852 goto gotmode;
855 printk(KERN_INFO "uvesafb: requested VBE mode 0x%x is "
856 "unavailable\n", vbemode);
857 vbemode = 0;
860 /* Count the modes in the modelist */
861 i = 0;
862 list_for_each(pos, &info->modelist)
863 i++;
866 * Convert the modelist into a modedb so that we can use it with
867 * fb_find_mode().
869 mode = kzalloc(i * sizeof(*mode), GFP_KERNEL);
870 if (mode) {
871 i = 0;
872 list_for_each(pos, &info->modelist) {
873 modelist = list_entry(pos, struct fb_modelist, list);
874 mode[i] = modelist->mode;
875 i++;
878 if (!mode_option)
879 mode_option = UVESAFB_DEFAULT_MODE;
881 i = fb_find_mode(&info->var, info, mode_option, mode, i,
882 NULL, 8);
884 kfree(mode);
887 /* fb_find_mode() failed */
888 if (i == 0) {
889 info->var.xres = 640;
890 info->var.yres = 480;
891 mode = (struct fb_videomode *)
892 fb_find_best_mode(&info->var, &info->modelist);
894 if (mode) {
895 fb_videomode_to_var(&info->var, mode);
896 } else {
897 modeid = par->vbe_modes[0].mode_id;
898 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
899 &info->var, info);
900 goto gotmode;
904 /* Look for a matching VBE mode. */
905 modeid = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres,
906 info->var.bits_per_pixel, UVESAFB_EXACT_RES);
908 if (modeid == -1)
909 return -EINVAL;
911 gotmode:
912 uvesafb_setup_var(&info->var, info, &par->vbe_modes[modeid]);
915 * If we are not VBE3.0+ compliant, we're done -- the BIOS will
916 * ignore our timings anyway.
918 if (par->vbe_ib.vbe_version < 0x0300 || par->nocrtc)
919 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
920 &info->var, info);
922 return modeid;
925 static int uvesafb_setpalette(struct uvesafb_pal_entry *entries, int count,
926 int start, struct fb_info *info)
928 struct uvesafb_ktask *task;
929 #ifdef CONFIG_X86
930 struct uvesafb_par *par = info->par;
931 int i = par->mode_idx;
932 #endif
933 int err = 0;
936 * We support palette modifications for 8 bpp modes only, so
937 * there can never be more than 256 entries.
939 if (start + count > 256)
940 return -EINVAL;
942 #ifdef CONFIG_X86
943 /* Use VGA registers if mode is VGA-compatible. */
944 if (i >= 0 && i < par->vbe_modes_cnt &&
945 par->vbe_modes[i].mode_attr & VBE_MODE_VGACOMPAT) {
946 for (i = 0; i < count; i++) {
947 outb_p(start + i, dac_reg);
948 outb_p(entries[i].red, dac_val);
949 outb_p(entries[i].green, dac_val);
950 outb_p(entries[i].blue, dac_val);
953 #ifdef CONFIG_X86_32
954 else if (par->pmi_setpal) {
955 __asm__ __volatile__(
956 "call *(%%esi)"
957 : /* no return value */
958 : "a" (0x4f09), /* EAX */
959 "b" (0), /* EBX */
960 "c" (count), /* ECX */
961 "d" (start), /* EDX */
962 "D" (entries), /* EDI */
963 "S" (&par->pmi_pal)); /* ESI */
965 #endif /* CONFIG_X86_32 */
966 else
967 #endif /* CONFIG_X86 */
969 task = uvesafb_prep();
970 if (!task)
971 return -ENOMEM;
973 task->t.regs.eax = 0x4f09;
974 task->t.regs.ebx = 0x0;
975 task->t.regs.ecx = count;
976 task->t.regs.edx = start;
977 task->t.flags = TF_BUF_ESDI;
978 task->t.buf_len = sizeof(struct uvesafb_pal_entry) * count;
979 task->buf = entries;
981 err = uvesafb_exec(task);
982 if ((task->t.regs.eax & 0xffff) != 0x004f)
983 err = 1;
985 uvesafb_free(task);
987 return err;
990 static int uvesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
991 unsigned blue, unsigned transp,
992 struct fb_info *info)
994 struct uvesafb_pal_entry entry;
995 int shift = 16 - info->var.green.length;
996 int err = 0;
998 if (regno >= info->cmap.len)
999 return -EINVAL;
1001 if (info->var.bits_per_pixel == 8) {
1002 entry.red = red >> shift;
1003 entry.green = green >> shift;
1004 entry.blue = blue >> shift;
1005 entry.pad = 0;
1007 err = uvesafb_setpalette(&entry, 1, regno, info);
1008 } else if (regno < 16) {
1009 switch (info->var.bits_per_pixel) {
1010 case 16:
1011 if (info->var.red.offset == 10) {
1012 /* 1:5:5:5 */
1013 ((u32 *) (info->pseudo_palette))[regno] =
1014 ((red & 0xf800) >> 1) |
1015 ((green & 0xf800) >> 6) |
1016 ((blue & 0xf800) >> 11);
1017 } else {
1018 /* 0:5:6:5 */
1019 ((u32 *) (info->pseudo_palette))[regno] =
1020 ((red & 0xf800) ) |
1021 ((green & 0xfc00) >> 5) |
1022 ((blue & 0xf800) >> 11);
1024 break;
1026 case 24:
1027 case 32:
1028 red >>= 8;
1029 green >>= 8;
1030 blue >>= 8;
1031 ((u32 *)(info->pseudo_palette))[regno] =
1032 (red << info->var.red.offset) |
1033 (green << info->var.green.offset) |
1034 (blue << info->var.blue.offset);
1035 break;
1038 return err;
1041 static int uvesafb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1043 struct uvesafb_pal_entry *entries;
1044 int shift = 16 - info->var.green.length;
1045 int i, err = 0;
1047 if (info->var.bits_per_pixel == 8) {
1048 if (cmap->start + cmap->len > info->cmap.start +
1049 info->cmap.len || cmap->start < info->cmap.start)
1050 return -EINVAL;
1052 entries = kmalloc(sizeof(*entries) * cmap->len, GFP_KERNEL);
1053 if (!entries)
1054 return -ENOMEM;
1056 for (i = 0; i < cmap->len; i++) {
1057 entries[i].red = cmap->red[i] >> shift;
1058 entries[i].green = cmap->green[i] >> shift;
1059 entries[i].blue = cmap->blue[i] >> shift;
1060 entries[i].pad = 0;
1062 err = uvesafb_setpalette(entries, cmap->len, cmap->start, info);
1063 kfree(entries);
1064 } else {
1066 * For modes with bpp > 8, we only set the pseudo palette in
1067 * the fb_info struct. We rely on uvesafb_setcolreg to do all
1068 * sanity checking.
1070 for (i = 0; i < cmap->len; i++) {
1071 err |= uvesafb_setcolreg(cmap->start + i, cmap->red[i],
1072 cmap->green[i], cmap->blue[i],
1073 0, info);
1076 return err;
1079 static int uvesafb_pan_display(struct fb_var_screeninfo *var,
1080 struct fb_info *info)
1082 #ifdef CONFIG_X86_32
1083 int offset;
1084 struct uvesafb_par *par = info->par;
1086 offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
1089 * It turns out it's not the best idea to do panning via vm86,
1090 * so we only allow it if we have a PMI.
1092 if (par->pmi_start) {
1093 __asm__ __volatile__(
1094 "call *(%%edi)"
1095 : /* no return value */
1096 : "a" (0x4f07), /* EAX */
1097 "b" (0), /* EBX */
1098 "c" (offset), /* ECX */
1099 "d" (offset >> 16), /* EDX */
1100 "D" (&par->pmi_start)); /* EDI */
1102 #endif
1103 return 0;
1106 static int uvesafb_blank(int blank, struct fb_info *info)
1108 struct uvesafb_ktask *task;
1109 int err = 1;
1110 #ifdef CONFIG_X86
1111 struct uvesafb_par *par = info->par;
1113 if (par->vbe_ib.capabilities & VBE_CAP_VGACOMPAT) {
1114 int loop = 10000;
1115 u8 seq = 0, crtc17 = 0;
1117 if (blank == FB_BLANK_POWERDOWN) {
1118 seq = 0x20;
1119 crtc17 = 0x00;
1120 err = 0;
1121 } else {
1122 seq = 0x00;
1123 crtc17 = 0x80;
1124 err = (blank == FB_BLANK_UNBLANK) ? 0 : -EINVAL;
1127 vga_wseq(NULL, 0x00, 0x01);
1128 seq |= vga_rseq(NULL, 0x01) & ~0x20;
1129 vga_wseq(NULL, 0x00, seq);
1131 crtc17 |= vga_rcrt(NULL, 0x17) & ~0x80;
1132 while (loop--);
1133 vga_wcrt(NULL, 0x17, crtc17);
1134 vga_wseq(NULL, 0x00, 0x03);
1135 } else
1136 #endif /* CONFIG_X86 */
1138 task = uvesafb_prep();
1139 if (!task)
1140 return -ENOMEM;
1142 task->t.regs.eax = 0x4f10;
1143 switch (blank) {
1144 case FB_BLANK_UNBLANK:
1145 task->t.regs.ebx = 0x0001;
1146 break;
1147 case FB_BLANK_NORMAL:
1148 task->t.regs.ebx = 0x0101; /* standby */
1149 break;
1150 case FB_BLANK_POWERDOWN:
1151 task->t.regs.ebx = 0x0401; /* powerdown */
1152 break;
1153 default:
1154 goto out;
1157 err = uvesafb_exec(task);
1158 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
1159 err = 1;
1160 out: uvesafb_free(task);
1162 return err;
1165 static int uvesafb_open(struct fb_info *info, int user)
1167 struct uvesafb_par *par = info->par;
1168 int cnt = atomic_read(&par->ref_count);
1170 if (!cnt && par->vbe_state_size)
1171 par->vbe_state_orig = uvesafb_vbe_state_save(par);
1173 atomic_inc(&par->ref_count);
1174 return 0;
1177 static int uvesafb_release(struct fb_info *info, int user)
1179 struct uvesafb_ktask *task = NULL;
1180 struct uvesafb_par *par = info->par;
1181 int cnt = atomic_read(&par->ref_count);
1183 if (!cnt)
1184 return -EINVAL;
1186 if (cnt != 1)
1187 goto out;
1189 task = uvesafb_prep();
1190 if (!task)
1191 goto out;
1193 /* First, try to set the standard 80x25 text mode. */
1194 task->t.regs.eax = 0x0003;
1195 uvesafb_exec(task);
1198 * Now try to restore whatever hardware state we might have
1199 * saved when the fb device was first opened.
1201 uvesafb_vbe_state_restore(par, par->vbe_state_orig);
1202 out:
1203 atomic_dec(&par->ref_count);
1204 if (task)
1205 uvesafb_free(task);
1206 return 0;
1209 static int uvesafb_set_par(struct fb_info *info)
1211 struct uvesafb_par *par = info->par;
1212 struct uvesafb_ktask *task = NULL;
1213 struct vbe_crtc_ib *crtc = NULL;
1214 struct vbe_mode_ib *mode = NULL;
1215 int i, err = 0, depth = info->var.bits_per_pixel;
1217 if (depth > 8 && depth != 32)
1218 depth = info->var.red.length + info->var.green.length +
1219 info->var.blue.length;
1221 i = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres, depth,
1222 UVESAFB_EXACT_RES | UVESAFB_EXACT_DEPTH);
1223 if (i >= 0)
1224 mode = &par->vbe_modes[i];
1225 else
1226 return -EINVAL;
1228 task = uvesafb_prep();
1229 if (!task)
1230 return -ENOMEM;
1231 setmode:
1232 task->t.regs.eax = 0x4f02;
1233 task->t.regs.ebx = mode->mode_id | 0x4000; /* use LFB */
1235 if (par->vbe_ib.vbe_version >= 0x0300 && !par->nocrtc &&
1236 info->var.pixclock != 0) {
1237 task->t.regs.ebx |= 0x0800; /* use CRTC data */
1238 task->t.flags = TF_BUF_ESDI;
1239 crtc = kzalloc(sizeof(struct vbe_crtc_ib), GFP_KERNEL);
1240 if (!crtc) {
1241 err = -ENOMEM;
1242 goto out;
1244 crtc->horiz_start = info->var.xres + info->var.right_margin;
1245 crtc->horiz_end = crtc->horiz_start + info->var.hsync_len;
1246 crtc->horiz_total = crtc->horiz_end + info->var.left_margin;
1248 crtc->vert_start = info->var.yres + info->var.lower_margin;
1249 crtc->vert_end = crtc->vert_start + info->var.vsync_len;
1250 crtc->vert_total = crtc->vert_end + info->var.upper_margin;
1252 crtc->pixel_clock = PICOS2KHZ(info->var.pixclock) * 1000;
1253 crtc->refresh_rate = (u16)(100 * (crtc->pixel_clock /
1254 (crtc->vert_total * crtc->horiz_total)));
1256 if (info->var.vmode & FB_VMODE_DOUBLE)
1257 crtc->flags |= 0x1;
1258 if (info->var.vmode & FB_VMODE_INTERLACED)
1259 crtc->flags |= 0x2;
1260 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
1261 crtc->flags |= 0x4;
1262 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
1263 crtc->flags |= 0x8;
1264 memcpy(&par->crtc, crtc, sizeof(*crtc));
1265 } else {
1266 memset(&par->crtc, 0, sizeof(*crtc));
1269 task->t.buf_len = sizeof(struct vbe_crtc_ib);
1270 task->buf = &par->crtc;
1272 err = uvesafb_exec(task);
1273 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
1275 * The mode switch might have failed because we tried to
1276 * use our own timings. Try again with the default timings.
1278 if (crtc != NULL) {
1279 printk(KERN_WARNING "uvesafb: mode switch failed "
1280 "(eax=0x%x, err=%d). Trying again with "
1281 "default timings.\n", task->t.regs.eax, err);
1282 uvesafb_reset(task);
1283 kfree(crtc);
1284 crtc = NULL;
1285 info->var.pixclock = 0;
1286 goto setmode;
1287 } else {
1288 printk(KERN_ERR "uvesafb: mode switch failed (eax="
1289 "0x%x, err=%d)\n", task->t.regs.eax, err);
1290 err = -EINVAL;
1291 goto out;
1294 par->mode_idx = i;
1296 /* For 8bpp modes, always try to set the DAC to 8 bits. */
1297 if (par->vbe_ib.capabilities & VBE_CAP_CAN_SWITCH_DAC &&
1298 mode->bits_per_pixel <= 8) {
1299 uvesafb_reset(task);
1300 task->t.regs.eax = 0x4f08;
1301 task->t.regs.ebx = 0x0800;
1303 err = uvesafb_exec(task);
1304 if (err || (task->t.regs.eax & 0xffff) != 0x004f ||
1305 ((task->t.regs.ebx & 0xff00) >> 8) != 8) {
1307 * We've failed to set the DAC palette format -
1308 * time to correct var.
1310 info->var.red.length = 6;
1311 info->var.green.length = 6;
1312 info->var.blue.length = 6;
1316 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1317 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1318 info->fix.line_length = mode->bytes_per_scan_line;
1320 out: if (crtc != NULL)
1321 kfree(crtc);
1322 uvesafb_free(task);
1324 return err;
1327 static void uvesafb_check_limits(struct fb_var_screeninfo *var,
1328 struct fb_info *info)
1330 const struct fb_videomode *mode;
1331 struct uvesafb_par *par = info->par;
1334 * If pixclock is set to 0, then we're using default BIOS timings
1335 * and thus don't have to perform any checks here.
1337 if (!var->pixclock)
1338 return;
1340 if (par->vbe_ib.vbe_version < 0x0300) {
1341 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, var, info);
1342 return;
1345 if (!fb_validate_mode(var, info))
1346 return;
1348 mode = fb_find_best_mode(var, &info->modelist);
1349 if (mode) {
1350 if (mode->xres == var->xres && mode->yres == var->yres &&
1351 !(mode->vmode & (FB_VMODE_INTERLACED | FB_VMODE_DOUBLE))) {
1352 fb_videomode_to_var(var, mode);
1353 return;
1357 if (info->monspecs.gtf && !fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1358 return;
1359 /* Use default refresh rate */
1360 var->pixclock = 0;
1363 static int uvesafb_check_var(struct fb_var_screeninfo *var,
1364 struct fb_info *info)
1366 struct uvesafb_par *par = info->par;
1367 struct vbe_mode_ib *mode = NULL;
1368 int match = -1;
1369 int depth = var->red.length + var->green.length + var->blue.length;
1372 * Various apps will use bits_per_pixel to set the color depth,
1373 * which is theoretically incorrect, but which we'll try to handle
1374 * here.
1376 if (depth == 0 || abs(depth - var->bits_per_pixel) >= 8)
1377 depth = var->bits_per_pixel;
1379 match = uvesafb_vbe_find_mode(par, var->xres, var->yres, depth,
1380 UVESAFB_EXACT_RES);
1381 if (match == -1)
1382 return -EINVAL;
1384 mode = &par->vbe_modes[match];
1385 uvesafb_setup_var(var, info, mode);
1388 * Check whether we have remapped enough memory for this mode.
1389 * We might be called at an early stage, when we haven't remapped
1390 * any memory yet, in which case we simply skip the check.
1392 if (var->yres * mode->bytes_per_scan_line > info->fix.smem_len
1393 && info->fix.smem_len)
1394 return -EINVAL;
1396 if ((var->vmode & FB_VMODE_DOUBLE) &&
1397 !(par->vbe_modes[match].mode_attr & 0x100))
1398 var->vmode &= ~FB_VMODE_DOUBLE;
1400 if ((var->vmode & FB_VMODE_INTERLACED) &&
1401 !(par->vbe_modes[match].mode_attr & 0x200))
1402 var->vmode &= ~FB_VMODE_INTERLACED;
1404 uvesafb_check_limits(var, info);
1406 var->xres_virtual = var->xres;
1407 var->yres_virtual = (par->ypan) ?
1408 info->fix.smem_len / mode->bytes_per_scan_line :
1409 var->yres;
1410 return 0;
1413 static void uvesafb_save_state(struct fb_info *info)
1415 struct uvesafb_par *par = info->par;
1417 if (par->vbe_state_saved)
1418 kfree(par->vbe_state_saved);
1420 par->vbe_state_saved = uvesafb_vbe_state_save(par);
1423 static void uvesafb_restore_state(struct fb_info *info)
1425 struct uvesafb_par *par = info->par;
1427 uvesafb_vbe_state_restore(par, par->vbe_state_saved);
1430 static struct fb_ops uvesafb_ops = {
1431 .owner = THIS_MODULE,
1432 .fb_open = uvesafb_open,
1433 .fb_release = uvesafb_release,
1434 .fb_setcolreg = uvesafb_setcolreg,
1435 .fb_setcmap = uvesafb_setcmap,
1436 .fb_pan_display = uvesafb_pan_display,
1437 .fb_blank = uvesafb_blank,
1438 .fb_fillrect = cfb_fillrect,
1439 .fb_copyarea = cfb_copyarea,
1440 .fb_imageblit = cfb_imageblit,
1441 .fb_check_var = uvesafb_check_var,
1442 .fb_set_par = uvesafb_set_par,
1443 .fb_save_state = uvesafb_save_state,
1444 .fb_restore_state = uvesafb_restore_state,
1447 static void __devinit uvesafb_init_info(struct fb_info *info,
1448 struct vbe_mode_ib *mode)
1450 unsigned int size_vmode;
1451 unsigned int size_remap;
1452 unsigned int size_total;
1453 struct uvesafb_par *par = info->par;
1454 int i, h;
1456 info->pseudo_palette = ((u8 *)info->par + sizeof(struct uvesafb_par));
1457 info->fix = uvesafb_fix;
1458 info->fix.ypanstep = par->ypan ? 1 : 0;
1459 info->fix.ywrapstep = (par->ypan > 1) ? 1 : 0;
1462 * If we were unable to get the state buffer size, disable
1463 * functions for saving and restoring the hardware state.
1465 if (par->vbe_state_size == 0) {
1466 info->fbops->fb_save_state = NULL;
1467 info->fbops->fb_restore_state = NULL;
1470 /* Disable blanking if the user requested so. */
1471 if (!blank)
1472 info->fbops->fb_blank = NULL;
1475 * Find out how much IO memory is required for the mode with
1476 * the highest resolution.
1478 size_remap = 0;
1479 for (i = 0; i < par->vbe_modes_cnt; i++) {
1480 h = par->vbe_modes[i].bytes_per_scan_line *
1481 par->vbe_modes[i].y_res;
1482 if (h > size_remap)
1483 size_remap = h;
1485 size_remap *= 2;
1488 * size_vmode -- that is the amount of memory needed for the
1489 * used video mode, i.e. the minimum amount of
1490 * memory we need.
1492 if (mode != NULL) {
1493 size_vmode = info->var.yres * mode->bytes_per_scan_line;
1494 } else {
1495 size_vmode = info->var.yres * info->var.xres *
1496 ((info->var.bits_per_pixel + 7) >> 3);
1500 * size_total -- all video memory we have. Used for mtrr
1501 * entries, resource allocation and bounds
1502 * checking.
1504 size_total = par->vbe_ib.total_memory * 65536;
1505 if (vram_total)
1506 size_total = vram_total * 1024 * 1024;
1507 if (size_total < size_vmode)
1508 size_total = size_vmode;
1511 * size_remap -- the amount of video memory we are going to
1512 * use for vesafb. With modern cards it is no
1513 * option to simply use size_total as th
1514 * wastes plenty of kernel address space.
1516 if (vram_remap)
1517 size_remap = vram_remap * 1024 * 1024;
1518 if (size_remap < size_vmode)
1519 size_remap = size_vmode;
1520 if (size_remap > size_total)
1521 size_remap = size_total;
1523 info->fix.smem_len = size_remap;
1524 info->fix.smem_start = mode->phys_base_ptr;
1527 * We have to set yres_virtual here because when setup_var() was
1528 * called, smem_len wasn't defined yet.
1530 info->var.yres_virtual = info->fix.smem_len /
1531 mode->bytes_per_scan_line;
1533 if (par->ypan && info->var.yres_virtual > info->var.yres) {
1534 printk(KERN_INFO "uvesafb: scrolling: %s "
1535 "using protected mode interface, "
1536 "yres_virtual=%d\n",
1537 (par->ypan > 1) ? "ywrap" : "ypan",
1538 info->var.yres_virtual);
1539 } else {
1540 printk(KERN_INFO "uvesafb: scrolling: redraw\n");
1541 info->var.yres_virtual = info->var.yres;
1542 par->ypan = 0;
1545 info->flags = FBINFO_FLAG_DEFAULT |
1546 (par->ypan) ? FBINFO_HWACCEL_YPAN : 0;
1548 if (!par->ypan)
1549 info->fbops->fb_pan_display = NULL;
1552 static void __devinit uvesafb_init_mtrr(struct fb_info *info)
1554 #ifdef CONFIG_MTRR
1555 if (mtrr && !(info->fix.smem_start & (PAGE_SIZE - 1))) {
1556 int temp_size = info->fix.smem_len;
1557 unsigned int type = 0;
1559 switch (mtrr) {
1560 case 1:
1561 type = MTRR_TYPE_UNCACHABLE;
1562 break;
1563 case 2:
1564 type = MTRR_TYPE_WRBACK;
1565 break;
1566 case 3:
1567 type = MTRR_TYPE_WRCOMB;
1568 break;
1569 case 4:
1570 type = MTRR_TYPE_WRTHROUGH;
1571 break;
1572 default:
1573 type = 0;
1574 break;
1577 if (type) {
1578 int rc;
1580 /* Find the largest power-of-two */
1581 while (temp_size & (temp_size - 1))
1582 temp_size &= (temp_size - 1);
1584 /* Try and find a power of two to add */
1585 do {
1586 rc = mtrr_add(info->fix.smem_start,
1587 temp_size, type, 1);
1588 temp_size >>= 1;
1589 } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
1592 #endif /* CONFIG_MTRR */
1596 static ssize_t uvesafb_show_vbe_ver(struct device *dev,
1597 struct device_attribute *attr, char *buf)
1599 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1600 struct uvesafb_par *par = info->par;
1602 return snprintf(buf, PAGE_SIZE, "%.4x\n", par->vbe_ib.vbe_version);
1605 static DEVICE_ATTR(vbe_version, S_IRUGO, uvesafb_show_vbe_ver, NULL);
1607 static ssize_t uvesafb_show_vbe_modes(struct device *dev,
1608 struct device_attribute *attr, char *buf)
1610 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1611 struct uvesafb_par *par = info->par;
1612 int ret = 0, i;
1614 for (i = 0; i < par->vbe_modes_cnt && ret < PAGE_SIZE; i++) {
1615 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1616 "%dx%d-%d, 0x%.4x\n",
1617 par->vbe_modes[i].x_res, par->vbe_modes[i].y_res,
1618 par->vbe_modes[i].depth, par->vbe_modes[i].mode_id);
1621 return ret;
1624 static DEVICE_ATTR(vbe_modes, S_IRUGO, uvesafb_show_vbe_modes, NULL);
1626 static ssize_t uvesafb_show_vendor(struct device *dev,
1627 struct device_attribute *attr, char *buf)
1629 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1630 struct uvesafb_par *par = info->par;
1632 if (par->vbe_ib.oem_vendor_name_ptr)
1633 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1634 (&par->vbe_ib) + par->vbe_ib.oem_vendor_name_ptr);
1635 else
1636 return 0;
1639 static DEVICE_ATTR(oem_vendor, S_IRUGO, uvesafb_show_vendor, NULL);
1641 static ssize_t uvesafb_show_product_name(struct device *dev,
1642 struct device_attribute *attr, char *buf)
1644 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1645 struct uvesafb_par *par = info->par;
1647 if (par->vbe_ib.oem_product_name_ptr)
1648 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1649 (&par->vbe_ib) + par->vbe_ib.oem_product_name_ptr);
1650 else
1651 return 0;
1654 static DEVICE_ATTR(oem_product_name, S_IRUGO, uvesafb_show_product_name, NULL);
1656 static ssize_t uvesafb_show_product_rev(struct device *dev,
1657 struct device_attribute *attr, char *buf)
1659 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1660 struct uvesafb_par *par = info->par;
1662 if (par->vbe_ib.oem_product_rev_ptr)
1663 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1664 (&par->vbe_ib) + par->vbe_ib.oem_product_rev_ptr);
1665 else
1666 return 0;
1669 static DEVICE_ATTR(oem_product_rev, S_IRUGO, uvesafb_show_product_rev, NULL);
1671 static ssize_t uvesafb_show_oem_string(struct device *dev,
1672 struct device_attribute *attr, char *buf)
1674 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1675 struct uvesafb_par *par = info->par;
1677 if (par->vbe_ib.oem_string_ptr)
1678 return snprintf(buf, PAGE_SIZE, "%s\n",
1679 (char *)(&par->vbe_ib) + par->vbe_ib.oem_string_ptr);
1680 else
1681 return 0;
1684 static DEVICE_ATTR(oem_string, S_IRUGO, uvesafb_show_oem_string, NULL);
1686 static ssize_t uvesafb_show_nocrtc(struct device *dev,
1687 struct device_attribute *attr, char *buf)
1689 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1690 struct uvesafb_par *par = info->par;
1692 return snprintf(buf, PAGE_SIZE, "%d\n", par->nocrtc);
1695 static ssize_t uvesafb_store_nocrtc(struct device *dev,
1696 struct device_attribute *attr, const char *buf, size_t count)
1698 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1699 struct uvesafb_par *par = info->par;
1701 if (count > 0) {
1702 if (buf[0] == '0')
1703 par->nocrtc = 0;
1704 else
1705 par->nocrtc = 1;
1707 return count;
1710 static DEVICE_ATTR(nocrtc, S_IRUGO | S_IWUSR, uvesafb_show_nocrtc,
1711 uvesafb_store_nocrtc);
1713 static struct attribute *uvesafb_dev_attrs[] = {
1714 &dev_attr_vbe_version.attr,
1715 &dev_attr_vbe_modes.attr,
1716 &dev_attr_oem_vendor.attr,
1717 &dev_attr_oem_product_name.attr,
1718 &dev_attr_oem_product_rev.attr,
1719 &dev_attr_oem_string.attr,
1720 &dev_attr_nocrtc.attr,
1721 NULL,
1724 static struct attribute_group uvesafb_dev_attgrp = {
1725 .name = NULL,
1726 .attrs = uvesafb_dev_attrs,
1729 static int __devinit uvesafb_probe(struct platform_device *dev)
1731 struct fb_info *info;
1732 struct vbe_mode_ib *mode = NULL;
1733 struct uvesafb_par *par;
1734 int err = 0, i;
1736 info = framebuffer_alloc(sizeof(*par) + sizeof(u32) * 256, &dev->dev);
1737 if (!info)
1738 return -ENOMEM;
1740 par = info->par;
1742 err = uvesafb_vbe_init(info);
1743 if (err) {
1744 printk(KERN_ERR "uvesafb: vbe_init() failed with %d\n", err);
1745 goto out;
1748 info->fbops = &uvesafb_ops;
1750 i = uvesafb_vbe_init_mode(info);
1751 if (i < 0) {
1752 err = -EINVAL;
1753 goto out;
1754 } else {
1755 mode = &par->vbe_modes[i];
1758 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1759 err = -ENXIO;
1760 goto out;
1763 uvesafb_init_info(info, mode);
1765 if (!request_mem_region(info->fix.smem_start, info->fix.smem_len,
1766 "uvesafb")) {
1767 printk(KERN_ERR "uvesafb: cannot reserve video memory at "
1768 "0x%lx\n", info->fix.smem_start);
1769 err = -EIO;
1770 goto out_mode;
1773 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
1775 if (!info->screen_base) {
1776 printk(KERN_ERR
1777 "uvesafb: abort, cannot ioremap 0x%x bytes of video "
1778 "memory at 0x%lx\n",
1779 info->fix.smem_len, info->fix.smem_start);
1780 err = -EIO;
1781 goto out_mem;
1784 if (!request_region(0x3c0, 32, "uvesafb")) {
1785 printk(KERN_ERR "uvesafb: request region 0x3c0-0x3e0 failed\n");
1786 err = -EIO;
1787 goto out_unmap;
1790 uvesafb_init_mtrr(info);
1791 platform_set_drvdata(dev, info);
1793 if (register_framebuffer(info) < 0) {
1794 printk(KERN_ERR
1795 "uvesafb: failed to register framebuffer device\n");
1796 err = -EINVAL;
1797 goto out_reg;
1800 printk(KERN_INFO "uvesafb: framebuffer at 0x%lx, mapped to 0x%p, "
1801 "using %dk, total %dk\n", info->fix.smem_start,
1802 info->screen_base, info->fix.smem_len/1024,
1803 par->vbe_ib.total_memory * 64);
1804 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
1805 info->fix.id);
1807 err = sysfs_create_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1808 if (err != 0)
1809 printk(KERN_WARNING "fb%d: failed to register attributes\n",
1810 info->node);
1812 return 0;
1814 out_reg:
1815 release_region(0x3c0, 32);
1816 out_unmap:
1817 iounmap(info->screen_base);
1818 out_mem:
1819 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1820 out_mode:
1821 if (!list_empty(&info->modelist))
1822 fb_destroy_modelist(&info->modelist);
1823 fb_destroy_modedb(info->monspecs.modedb);
1824 fb_dealloc_cmap(&info->cmap);
1825 out:
1826 if (par->vbe_modes)
1827 kfree(par->vbe_modes);
1829 framebuffer_release(info);
1830 return err;
1833 static int uvesafb_remove(struct platform_device *dev)
1835 struct fb_info *info = platform_get_drvdata(dev);
1837 if (info) {
1838 struct uvesafb_par *par = info->par;
1840 sysfs_remove_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1841 unregister_framebuffer(info);
1842 release_region(0x3c0, 32);
1843 iounmap(info->screen_base);
1844 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1845 fb_destroy_modedb(info->monspecs.modedb);
1846 fb_dealloc_cmap(&info->cmap);
1848 if (par) {
1849 if (par->vbe_modes)
1850 kfree(par->vbe_modes);
1851 if (par->vbe_state_orig)
1852 kfree(par->vbe_state_orig);
1853 if (par->vbe_state_saved)
1854 kfree(par->vbe_state_saved);
1857 framebuffer_release(info);
1859 return 0;
1862 static struct platform_driver uvesafb_driver = {
1863 .probe = uvesafb_probe,
1864 .remove = uvesafb_remove,
1865 .driver = {
1866 .name = "uvesafb",
1870 static struct platform_device *uvesafb_device;
1872 #ifndef MODULE
1873 static int __devinit uvesafb_setup(char *options)
1875 char *this_opt;
1877 if (!options || !*options)
1878 return 0;
1880 while ((this_opt = strsep(&options, ",")) != NULL) {
1881 if (!*this_opt) continue;
1883 if (!strcmp(this_opt, "redraw"))
1884 ypan = 0;
1885 else if (!strcmp(this_opt, "ypan"))
1886 ypan = 1;
1887 else if (!strcmp(this_opt, "ywrap"))
1888 ypan = 2;
1889 else if (!strcmp(this_opt, "vgapal"))
1890 pmi_setpal = 0;
1891 else if (!strcmp(this_opt, "pmipal"))
1892 pmi_setpal = 1;
1893 else if (!strncmp(this_opt, "mtrr:", 5))
1894 mtrr = simple_strtoul(this_opt+5, NULL, 0);
1895 else if (!strcmp(this_opt, "nomtrr"))
1896 mtrr = 0;
1897 else if (!strcmp(this_opt, "nocrtc"))
1898 nocrtc = 1;
1899 else if (!strcmp(this_opt, "noedid"))
1900 noedid = 1;
1901 else if (!strcmp(this_opt, "noblank"))
1902 blank = 0;
1903 else if (!strncmp(this_opt, "vtotal:", 7))
1904 vram_total = simple_strtoul(this_opt + 7, NULL, 0);
1905 else if (!strncmp(this_opt, "vremap:", 7))
1906 vram_remap = simple_strtoul(this_opt + 7, NULL, 0);
1907 else if (!strncmp(this_opt, "maxhf:", 6))
1908 maxhf = simple_strtoul(this_opt + 6, NULL, 0);
1909 else if (!strncmp(this_opt, "maxvf:", 6))
1910 maxvf = simple_strtoul(this_opt + 6, NULL, 0);
1911 else if (!strncmp(this_opt, "maxclk:", 7))
1912 maxclk = simple_strtoul(this_opt + 7, NULL, 0);
1913 else if (!strncmp(this_opt, "vbemode:", 8))
1914 vbemode = simple_strtoul(this_opt + 8, NULL, 0);
1915 else if (this_opt[0] >= '0' && this_opt[0] <= '9') {
1916 mode_option = this_opt;
1917 } else {
1918 printk(KERN_WARNING
1919 "uvesafb: unrecognized option %s\n", this_opt);
1923 return 0;
1925 #endif /* !MODULE */
1927 static ssize_t show_v86d(struct device_driver *dev, char *buf)
1929 return snprintf(buf, PAGE_SIZE, "%s\n", v86d_path);
1932 static ssize_t store_v86d(struct device_driver *dev, const char *buf,
1933 size_t count)
1935 strncpy(v86d_path, buf, PATH_MAX);
1936 return count;
1939 static DRIVER_ATTR(v86d, S_IRUGO | S_IWUSR, show_v86d, store_v86d);
1941 static int __devinit uvesafb_init(void)
1943 int err;
1945 #ifndef MODULE
1946 char *option = NULL;
1948 if (fb_get_options("uvesafb", &option))
1949 return -ENODEV;
1950 uvesafb_setup(option);
1951 #endif
1952 err = cn_add_callback(&uvesafb_cn_id, "uvesafb", uvesafb_cn_callback);
1953 if (err)
1954 return err;
1956 err = platform_driver_register(&uvesafb_driver);
1958 if (!err) {
1959 uvesafb_device = platform_device_alloc("uvesafb", 0);
1960 if (uvesafb_device)
1961 err = platform_device_add(uvesafb_device);
1962 else
1963 err = -ENOMEM;
1965 if (err) {
1966 platform_device_put(uvesafb_device);
1967 platform_driver_unregister(&uvesafb_driver);
1968 cn_del_callback(&uvesafb_cn_id);
1969 return err;
1972 err = driver_create_file(&uvesafb_driver.driver,
1973 &driver_attr_v86d);
1974 if (err) {
1975 printk(KERN_WARNING "uvesafb: failed to register "
1976 "attributes\n");
1977 err = 0;
1980 return err;
1983 module_init(uvesafb_init);
1985 static void __devexit uvesafb_exit(void)
1987 struct uvesafb_ktask *task;
1989 if (v86d_started) {
1990 task = uvesafb_prep();
1991 if (task) {
1992 task->t.flags = TF_EXIT;
1993 uvesafb_exec(task);
1994 uvesafb_free(task);
1998 cn_del_callback(&uvesafb_cn_id);
1999 driver_remove_file(&uvesafb_driver.driver, &driver_attr_v86d);
2000 platform_device_unregister(uvesafb_device);
2001 platform_driver_unregister(&uvesafb_driver);
2004 module_exit(uvesafb_exit);
2006 static int param_get_scroll(char *buffer, struct kernel_param *kp)
2008 return 0;
2011 static int param_set_scroll(const char *val, struct kernel_param *kp)
2013 ypan = 0;
2015 if (!strcmp(val, "redraw"))
2016 ypan = 0;
2017 else if (!strcmp(val, "ypan"))
2018 ypan = 1;
2019 else if (!strcmp(val, "ywrap"))
2020 ypan = 2;
2022 return 0;
2025 #define param_check_scroll(name, p) __param_check(name, p, void)
2027 module_param_named(scroll, ypan, scroll, 0);
2028 MODULE_PARM_DESC(scroll,
2029 "Scrolling mode, set to 'redraw', 'ypan', or 'ywrap'");
2030 module_param_named(vgapal, pmi_setpal, invbool, 0);
2031 MODULE_PARM_DESC(vgapal, "Set palette using VGA registers");
2032 module_param_named(pmipal, pmi_setpal, bool, 0);
2033 MODULE_PARM_DESC(pmipal, "Set palette using PMI calls");
2034 module_param(mtrr, uint, 0);
2035 MODULE_PARM_DESC(mtrr,
2036 "Memory Type Range Registers setting. Use 0 to disable.");
2037 module_param(blank, bool, 0);
2038 MODULE_PARM_DESC(blank, "Enable hardware blanking");
2039 module_param(nocrtc, bool, 0);
2040 MODULE_PARM_DESC(nocrtc, "Ignore CRTC timings when setting modes");
2041 module_param(noedid, bool, 0);
2042 MODULE_PARM_DESC(noedid,
2043 "Ignore EDID-provided monitor limits when setting modes");
2044 module_param(vram_remap, uint, 0);
2045 MODULE_PARM_DESC(vram_remap, "Set amount of video memory to be used [MiB]");
2046 module_param(vram_total, uint, 0);
2047 MODULE_PARM_DESC(vram_total, "Set total amount of video memoery [MiB]");
2048 module_param(maxclk, ushort, 0);
2049 MODULE_PARM_DESC(maxclk, "Maximum pixelclock [MHz], overrides EDID data");
2050 module_param(maxhf, ushort, 0);
2051 MODULE_PARM_DESC(maxhf,
2052 "Maximum horizontal frequency [kHz], overrides EDID data");
2053 module_param(maxvf, ushort, 0);
2054 MODULE_PARM_DESC(maxvf,
2055 "Maximum vertical frequency [Hz], overrides EDID data");
2056 module_param_named(mode, mode_option, charp, 0);
2057 MODULE_PARM_DESC(mode,
2058 "Specify initial video mode as \"<xres>x<yres>[-<bpp>][@<refresh>]\"");
2059 module_param(vbemode, ushort, 0);
2060 MODULE_PARM_DESC(vbemode,
2061 "VBE mode number to set, overrides the 'mode' option");
2062 module_param_string(v86d, v86d_path, PATH_MAX, 0660);
2063 MODULE_PARM_DESC(v86d, "Path to the v86d userspace helper.");
2065 MODULE_LICENSE("GPL");
2066 MODULE_AUTHOR("Michal Januszewski <spock@gentoo.org>");
2067 MODULE_DESCRIPTION("Framebuffer driver for VBE2.0+ compliant graphics boards");