drm/i915: Initialize HDMI outputs as HDMI connectors, not DVI.
[linux-2.6/mini2440.git] / drivers / video / uvesafb.c
blobe98baf6916b88fd9c8d6f25b1161c5ce099a4d99
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 bool pmi_setpal __devinitdata = true; /* 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;
58 static u8 dac_width = 6;
60 static struct uvesafb_ktask *uvfb_tasks[UVESAFB_TASKS_MAX];
61 static DEFINE_MUTEX(uvfb_lock);
64 * A handler for replies from userspace.
66 * Make sure each message passes consistency checks and if it does,
67 * find the kernel part of the task struct, copy the registers and
68 * the buffer contents and then complete the task.
70 static void uvesafb_cn_callback(struct cn_msg *msg)
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 err = -EBUSY;
185 goto out;
188 /* Save a pointer to the kernel part of the task struct. */
189 uvfb_tasks[seq] = task;
190 mutex_unlock(&uvfb_lock);
192 err = cn_netlink_send(m, 0, GFP_KERNEL);
193 if (err == -ESRCH) {
195 * Try to start the userspace helper if sending
196 * the request failed the first time.
198 err = uvesafb_helper_start();
199 if (err) {
200 printk(KERN_ERR "uvesafb: failed to execute %s\n",
201 v86d_path);
202 printk(KERN_ERR "uvesafb: make sure that the v86d "
203 "helper is installed and executable\n");
204 } else {
205 v86d_started = 1;
206 err = cn_netlink_send(m, 0, gfp_any());
207 if (err == -ENOBUFS)
208 err = 0;
210 } else if (err == -ENOBUFS)
211 err = 0;
213 if (!err && !(task->t.flags & TF_EXIT))
214 err = !wait_for_completion_timeout(task->done,
215 msecs_to_jiffies(UVESAFB_TIMEOUT));
217 mutex_lock(&uvfb_lock);
218 uvfb_tasks[seq] = NULL;
219 mutex_unlock(&uvfb_lock);
221 seq++;
222 if (seq >= UVESAFB_TASKS_MAX)
223 seq = 0;
224 out:
225 kfree(m);
226 return err;
230 * Free a uvesafb_ktask struct.
232 static void uvesafb_free(struct uvesafb_ktask *task)
234 if (task) {
235 if (task->done)
236 kfree(task->done);
237 kfree(task);
242 * Prepare a uvesafb_ktask struct to be used again.
244 static void uvesafb_reset(struct uvesafb_ktask *task)
246 struct completion *cpl = task->done;
248 memset(task, 0, sizeof(*task));
249 task->done = cpl;
253 * Allocate and prepare a uvesafb_ktask struct.
255 static struct uvesafb_ktask *uvesafb_prep(void)
257 struct uvesafb_ktask *task;
259 task = kzalloc(sizeof(*task), GFP_KERNEL);
260 if (task) {
261 task->done = kzalloc(sizeof(*task->done), GFP_KERNEL);
262 if (!task->done) {
263 kfree(task);
264 task = NULL;
267 return task;
270 static void uvesafb_setup_var(struct fb_var_screeninfo *var,
271 struct fb_info *info, struct vbe_mode_ib *mode)
273 struct uvesafb_par *par = info->par;
275 var->vmode = FB_VMODE_NONINTERLACED;
276 var->sync = FB_SYNC_VERT_HIGH_ACT;
278 var->xres = mode->x_res;
279 var->yres = mode->y_res;
280 var->xres_virtual = mode->x_res;
281 var->yres_virtual = (par->ypan) ?
282 info->fix.smem_len / mode->bytes_per_scan_line :
283 mode->y_res;
284 var->xoffset = 0;
285 var->yoffset = 0;
286 var->bits_per_pixel = mode->bits_per_pixel;
288 if (var->bits_per_pixel == 15)
289 var->bits_per_pixel = 16;
291 if (var->bits_per_pixel > 8) {
292 var->red.offset = mode->red_off;
293 var->red.length = mode->red_len;
294 var->green.offset = mode->green_off;
295 var->green.length = mode->green_len;
296 var->blue.offset = mode->blue_off;
297 var->blue.length = mode->blue_len;
298 var->transp.offset = mode->rsvd_off;
299 var->transp.length = mode->rsvd_len;
300 } else {
301 var->red.offset = 0;
302 var->green.offset = 0;
303 var->blue.offset = 0;
304 var->transp.offset = 0;
306 var->red.length = 8;
307 var->green.length = 8;
308 var->blue.length = 8;
309 var->transp.length = 0;
313 static int uvesafb_vbe_find_mode(struct uvesafb_par *par,
314 int xres, int yres, int depth, unsigned char flags)
316 int i, match = -1, h = 0, d = 0x7fffffff;
318 for (i = 0; i < par->vbe_modes_cnt; i++) {
319 h = abs(par->vbe_modes[i].x_res - xres) +
320 abs(par->vbe_modes[i].y_res - yres) +
321 abs(depth - par->vbe_modes[i].depth);
324 * We have an exact match in terms of resolution
325 * and depth.
327 if (h == 0)
328 return i;
330 if (h < d || (h == d && par->vbe_modes[i].depth > depth)) {
331 d = h;
332 match = i;
335 i = 1;
337 if (flags & UVESAFB_EXACT_DEPTH &&
338 par->vbe_modes[match].depth != depth)
339 i = 0;
341 if (flags & UVESAFB_EXACT_RES && d > 24)
342 i = 0;
344 if (i != 0)
345 return match;
346 else
347 return -1;
350 static u8 *uvesafb_vbe_state_save(struct uvesafb_par *par)
352 struct uvesafb_ktask *task;
353 u8 *state;
354 int err;
356 if (!par->vbe_state_size)
357 return NULL;
359 state = kmalloc(par->vbe_state_size, GFP_KERNEL);
360 if (!state)
361 return NULL;
363 task = uvesafb_prep();
364 if (!task) {
365 kfree(state);
366 return NULL;
369 task->t.regs.eax = 0x4f04;
370 task->t.regs.ecx = 0x000f;
371 task->t.regs.edx = 0x0001;
372 task->t.flags = TF_BUF_RET | TF_BUF_ESBX;
373 task->t.buf_len = par->vbe_state_size;
374 task->buf = state;
375 err = uvesafb_exec(task);
377 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
378 printk(KERN_WARNING "uvesafb: VBE get state call "
379 "failed (eax=0x%x, err=%d)\n",
380 task->t.regs.eax, err);
381 kfree(state);
382 state = NULL;
385 uvesafb_free(task);
386 return state;
389 static void uvesafb_vbe_state_restore(struct uvesafb_par *par, u8 *state_buf)
391 struct uvesafb_ktask *task;
392 int err;
394 if (!state_buf)
395 return;
397 task = uvesafb_prep();
398 if (!task)
399 return;
401 task->t.regs.eax = 0x4f04;
402 task->t.regs.ecx = 0x000f;
403 task->t.regs.edx = 0x0002;
404 task->t.buf_len = par->vbe_state_size;
405 task->t.flags = TF_BUF_ESBX;
406 task->buf = state_buf;
408 err = uvesafb_exec(task);
409 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
410 printk(KERN_WARNING "uvesafb: VBE state restore call "
411 "failed (eax=0x%x, err=%d)\n",
412 task->t.regs.eax, err);
414 uvesafb_free(task);
417 static int __devinit uvesafb_vbe_getinfo(struct uvesafb_ktask *task,
418 struct uvesafb_par *par)
420 int err;
422 task->t.regs.eax = 0x4f00;
423 task->t.flags = TF_VBEIB;
424 task->t.buf_len = sizeof(struct vbe_ib);
425 task->buf = &par->vbe_ib;
426 strncpy(par->vbe_ib.vbe_signature, "VBE2", 4);
428 err = uvesafb_exec(task);
429 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
430 printk(KERN_ERR "uvesafb: Getting VBE info block failed "
431 "(eax=0x%x, err=%d)\n", (u32)task->t.regs.eax,
432 err);
433 return -EINVAL;
436 if (par->vbe_ib.vbe_version < 0x0200) {
437 printk(KERN_ERR "uvesafb: Sorry, pre-VBE 2.0 cards are "
438 "not supported.\n");
439 return -EINVAL;
442 if (!par->vbe_ib.mode_list_ptr) {
443 printk(KERN_ERR "uvesafb: Missing mode list!\n");
444 return -EINVAL;
447 printk(KERN_INFO "uvesafb: ");
450 * Convert string pointers and the mode list pointer into
451 * usable addresses. Print informational messages about the
452 * video adapter and its vendor.
454 if (par->vbe_ib.oem_vendor_name_ptr)
455 printk("%s, ",
456 ((char *)task->buf) + par->vbe_ib.oem_vendor_name_ptr);
458 if (par->vbe_ib.oem_product_name_ptr)
459 printk("%s, ",
460 ((char *)task->buf) + par->vbe_ib.oem_product_name_ptr);
462 if (par->vbe_ib.oem_product_rev_ptr)
463 printk("%s, ",
464 ((char *)task->buf) + par->vbe_ib.oem_product_rev_ptr);
466 if (par->vbe_ib.oem_string_ptr)
467 printk("OEM: %s, ",
468 ((char *)task->buf) + par->vbe_ib.oem_string_ptr);
470 printk("VBE v%d.%d\n", ((par->vbe_ib.vbe_version & 0xff00) >> 8),
471 par->vbe_ib.vbe_version & 0xff);
473 return 0;
476 static int __devinit uvesafb_vbe_getmodes(struct uvesafb_ktask *task,
477 struct uvesafb_par *par)
479 int off = 0, err;
480 u16 *mode;
482 par->vbe_modes_cnt = 0;
484 /* Count available modes. */
485 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
486 while (*mode != 0xffff) {
487 par->vbe_modes_cnt++;
488 mode++;
491 par->vbe_modes = kzalloc(sizeof(struct vbe_mode_ib) *
492 par->vbe_modes_cnt, GFP_KERNEL);
493 if (!par->vbe_modes)
494 return -ENOMEM;
496 /* Get info about all available modes. */
497 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
498 while (*mode != 0xffff) {
499 struct vbe_mode_ib *mib;
501 uvesafb_reset(task);
502 task->t.regs.eax = 0x4f01;
503 task->t.regs.ecx = (u32) *mode;
504 task->t.flags = TF_BUF_RET | TF_BUF_ESDI;
505 task->t.buf_len = sizeof(struct vbe_mode_ib);
506 task->buf = par->vbe_modes + off;
508 err = uvesafb_exec(task);
509 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
510 printk(KERN_WARNING "uvesafb: Getting mode info block "
511 "for mode 0x%x failed (eax=0x%x, err=%d)\n",
512 *mode, (u32)task->t.regs.eax, err);
513 mode++;
514 par->vbe_modes_cnt--;
515 continue;
518 mib = task->buf;
519 mib->mode_id = *mode;
522 * We only want modes that are supported with the current
523 * hardware configuration, color, graphics and that have
524 * support for the LFB.
526 if ((mib->mode_attr & VBE_MODE_MASK) == VBE_MODE_MASK &&
527 mib->bits_per_pixel >= 8)
528 off++;
529 else
530 par->vbe_modes_cnt--;
532 mode++;
533 mib->depth = mib->red_len + mib->green_len + mib->blue_len;
536 * Handle 8bpp modes and modes with broken color component
537 * lengths.
539 if (mib->depth == 0 || (mib->depth == 24 &&
540 mib->bits_per_pixel == 32))
541 mib->depth = mib->bits_per_pixel;
544 if (par->vbe_modes_cnt > 0)
545 return 0;
546 else
547 return -EINVAL;
551 * The Protected Mode Interface is 32-bit x86 code, so we only run it on
552 * x86 and not x86_64.
554 #ifdef CONFIG_X86_32
555 static int __devinit uvesafb_vbe_getpmi(struct uvesafb_ktask *task,
556 struct uvesafb_par *par)
558 int i, err;
560 uvesafb_reset(task);
561 task->t.regs.eax = 0x4f0a;
562 task->t.regs.ebx = 0x0;
563 err = uvesafb_exec(task);
565 if ((task->t.regs.eax & 0xffff) != 0x4f || task->t.regs.es < 0xc000) {
566 par->pmi_setpal = par->ypan = 0;
567 } else {
568 par->pmi_base = (u16 *)phys_to_virt(((u32)task->t.regs.es << 4)
569 + task->t.regs.edi);
570 par->pmi_start = (u8 *)par->pmi_base + par->pmi_base[1];
571 par->pmi_pal = (u8 *)par->pmi_base + par->pmi_base[2];
572 printk(KERN_INFO "uvesafb: protected mode interface info at "
573 "%04x:%04x\n",
574 (u16)task->t.regs.es, (u16)task->t.regs.edi);
575 printk(KERN_INFO "uvesafb: pmi: set display start = %p, "
576 "set palette = %p\n", par->pmi_start,
577 par->pmi_pal);
579 if (par->pmi_base[3]) {
580 printk(KERN_INFO "uvesafb: pmi: ports = ");
581 for (i = par->pmi_base[3]/2;
582 par->pmi_base[i] != 0xffff; i++)
583 printk("%x ", par->pmi_base[i]);
584 printk("\n");
586 if (par->pmi_base[i] != 0xffff) {
587 printk(KERN_INFO "uvesafb: can't handle memory"
588 " requests, pmi disabled\n");
589 par->ypan = par->pmi_setpal = 0;
593 return 0;
595 #endif /* CONFIG_X86_32 */
598 * Check whether a video mode is supported by the Video BIOS and is
599 * compatible with the monitor limits.
601 static int __devinit uvesafb_is_valid_mode(struct fb_videomode *mode,
602 struct fb_info *info)
604 if (info->monspecs.gtf) {
605 fb_videomode_to_var(&info->var, mode);
606 if (fb_validate_mode(&info->var, info))
607 return 0;
610 if (uvesafb_vbe_find_mode(info->par, mode->xres, mode->yres, 8,
611 UVESAFB_EXACT_RES) == -1)
612 return 0;
614 return 1;
617 static int __devinit uvesafb_vbe_getedid(struct uvesafb_ktask *task,
618 struct fb_info *info)
620 struct uvesafb_par *par = info->par;
621 int err = 0;
623 if (noedid || par->vbe_ib.vbe_version < 0x0300)
624 return -EINVAL;
626 task->t.regs.eax = 0x4f15;
627 task->t.regs.ebx = 0;
628 task->t.regs.ecx = 0;
629 task->t.buf_len = 0;
630 task->t.flags = 0;
632 err = uvesafb_exec(task);
634 if ((task->t.regs.eax & 0xffff) != 0x004f || err)
635 return -EINVAL;
637 if ((task->t.regs.ebx & 0x3) == 3) {
638 printk(KERN_INFO "uvesafb: VBIOS/hardware supports both "
639 "DDC1 and DDC2 transfers\n");
640 } else if ((task->t.regs.ebx & 0x3) == 2) {
641 printk(KERN_INFO "uvesafb: VBIOS/hardware supports DDC2 "
642 "transfers\n");
643 } else if ((task->t.regs.ebx & 0x3) == 1) {
644 printk(KERN_INFO "uvesafb: VBIOS/hardware supports DDC1 "
645 "transfers\n");
646 } else {
647 printk(KERN_INFO "uvesafb: VBIOS/hardware doesn't support "
648 "DDC transfers\n");
649 return -EINVAL;
652 task->t.regs.eax = 0x4f15;
653 task->t.regs.ebx = 1;
654 task->t.regs.ecx = task->t.regs.edx = 0;
655 task->t.flags = TF_BUF_RET | TF_BUF_ESDI;
656 task->t.buf_len = EDID_LENGTH;
657 task->buf = kzalloc(EDID_LENGTH, GFP_KERNEL);
659 err = uvesafb_exec(task);
661 if ((task->t.regs.eax & 0xffff) == 0x004f && !err) {
662 fb_edid_to_monspecs(task->buf, &info->monspecs);
664 if (info->monspecs.vfmax && info->monspecs.hfmax) {
666 * If the maximum pixel clock wasn't specified in
667 * the EDID block, set it to 300 MHz.
669 if (info->monspecs.dclkmax == 0)
670 info->monspecs.dclkmax = 300 * 1000000;
671 info->monspecs.gtf = 1;
673 } else {
674 err = -EINVAL;
677 kfree(task->buf);
678 return err;
681 static void __devinit uvesafb_vbe_getmonspecs(struct uvesafb_ktask *task,
682 struct fb_info *info)
684 struct uvesafb_par *par = info->par;
685 int i;
687 memset(&info->monspecs, 0, sizeof(info->monspecs));
690 * If we don't get all necessary data from the EDID block,
691 * mark it as incompatible with the GTF and set nocrtc so
692 * that we always use the default BIOS refresh rate.
694 if (uvesafb_vbe_getedid(task, info)) {
695 info->monspecs.gtf = 0;
696 par->nocrtc = 1;
699 /* Kernel command line overrides. */
700 if (maxclk)
701 info->monspecs.dclkmax = maxclk * 1000000;
702 if (maxvf)
703 info->monspecs.vfmax = maxvf;
704 if (maxhf)
705 info->monspecs.hfmax = maxhf * 1000;
708 * In case DDC transfers are not supported, the user can provide
709 * monitor limits manually. Lower limits are set to "safe" values.
711 if (info->monspecs.gtf == 0 && maxclk && maxvf && maxhf) {
712 info->monspecs.dclkmin = 0;
713 info->monspecs.vfmin = 60;
714 info->monspecs.hfmin = 29000;
715 info->monspecs.gtf = 1;
716 par->nocrtc = 0;
719 if (info->monspecs.gtf)
720 printk(KERN_INFO
721 "uvesafb: monitor limits: vf = %d Hz, hf = %d kHz, "
722 "clk = %d MHz\n", info->monspecs.vfmax,
723 (int)(info->monspecs.hfmax / 1000),
724 (int)(info->monspecs.dclkmax / 1000000));
725 else
726 printk(KERN_INFO "uvesafb: no monitor limits have been set, "
727 "default refresh rate will be used\n");
729 /* Add VBE modes to the modelist. */
730 for (i = 0; i < par->vbe_modes_cnt; i++) {
731 struct fb_var_screeninfo var;
732 struct vbe_mode_ib *mode;
733 struct fb_videomode vmode;
735 mode = &par->vbe_modes[i];
736 memset(&var, 0, sizeof(var));
738 var.xres = mode->x_res;
739 var.yres = mode->y_res;
741 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, &var, info);
742 fb_var_to_videomode(&vmode, &var);
743 fb_add_videomode(&vmode, &info->modelist);
746 /* Add valid VESA modes to our modelist. */
747 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
748 if (uvesafb_is_valid_mode((struct fb_videomode *)
749 &vesa_modes[i], info))
750 fb_add_videomode(&vesa_modes[i], &info->modelist);
753 for (i = 0; i < info->monspecs.modedb_len; i++) {
754 if (uvesafb_is_valid_mode(&info->monspecs.modedb[i], info))
755 fb_add_videomode(&info->monspecs.modedb[i],
756 &info->modelist);
759 return;
762 static void __devinit uvesafb_vbe_getstatesize(struct uvesafb_ktask *task,
763 struct uvesafb_par *par)
765 int err;
767 uvesafb_reset(task);
770 * Get the VBE state buffer size. We want all available
771 * hardware state data (CL = 0x0f).
773 task->t.regs.eax = 0x4f04;
774 task->t.regs.ecx = 0x000f;
775 task->t.regs.edx = 0x0000;
776 task->t.flags = 0;
778 err = uvesafb_exec(task);
780 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
781 printk(KERN_WARNING "uvesafb: VBE state buffer size "
782 "cannot be determined (eax=0x%x, err=%d)\n",
783 task->t.regs.eax, err);
784 par->vbe_state_size = 0;
785 return;
788 par->vbe_state_size = 64 * (task->t.regs.ebx & 0xffff);
791 static int __devinit uvesafb_vbe_init(struct fb_info *info)
793 struct uvesafb_ktask *task = NULL;
794 struct uvesafb_par *par = info->par;
795 int err;
797 task = uvesafb_prep();
798 if (!task)
799 return -ENOMEM;
801 err = uvesafb_vbe_getinfo(task, par);
802 if (err)
803 goto out;
805 err = uvesafb_vbe_getmodes(task, par);
806 if (err)
807 goto out;
809 par->nocrtc = nocrtc;
810 #ifdef CONFIG_X86_32
811 par->pmi_setpal = pmi_setpal;
812 par->ypan = ypan;
814 if (par->pmi_setpal || par->ypan)
815 uvesafb_vbe_getpmi(task, par);
816 #else
817 /* The protected mode interface is not available on non-x86. */
818 par->pmi_setpal = par->ypan = 0;
819 #endif
821 INIT_LIST_HEAD(&info->modelist);
822 uvesafb_vbe_getmonspecs(task, info);
823 uvesafb_vbe_getstatesize(task, par);
825 out: uvesafb_free(task);
826 return err;
829 static int __devinit uvesafb_vbe_init_mode(struct fb_info *info)
831 struct list_head *pos;
832 struct fb_modelist *modelist;
833 struct fb_videomode *mode;
834 struct uvesafb_par *par = info->par;
835 int i, modeid;
837 /* Has the user requested a specific VESA mode? */
838 if (vbemode) {
839 for (i = 0; i < par->vbe_modes_cnt; i++) {
840 if (par->vbe_modes[i].mode_id == vbemode) {
841 modeid = i;
842 uvesafb_setup_var(&info->var, info,
843 &par->vbe_modes[modeid]);
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 goto gotmode;
854 printk(KERN_INFO "uvesafb: requested VBE mode 0x%x is "
855 "unavailable\n", vbemode);
856 vbemode = 0;
859 /* Count the modes in the modelist */
860 i = 0;
861 list_for_each(pos, &info->modelist)
862 i++;
865 * Convert the modelist into a modedb so that we can use it with
866 * fb_find_mode().
868 mode = kzalloc(i * sizeof(*mode), GFP_KERNEL);
869 if (mode) {
870 i = 0;
871 list_for_each(pos, &info->modelist) {
872 modelist = list_entry(pos, struct fb_modelist, list);
873 mode[i] = modelist->mode;
874 i++;
877 if (!mode_option)
878 mode_option = UVESAFB_DEFAULT_MODE;
880 i = fb_find_mode(&info->var, info, mode_option, mode, i,
881 NULL, 8);
883 kfree(mode);
886 /* fb_find_mode() failed */
887 if (i == 0) {
888 info->var.xres = 640;
889 info->var.yres = 480;
890 mode = (struct fb_videomode *)
891 fb_find_best_mode(&info->var, &info->modelist);
893 if (mode) {
894 fb_videomode_to_var(&info->var, mode);
895 } else {
896 modeid = par->vbe_modes[0].mode_id;
897 uvesafb_setup_var(&info->var, info,
898 &par->vbe_modes[modeid]);
899 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
900 &info->var, info);
902 goto gotmode;
906 /* Look for a matching VBE mode. */
907 modeid = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres,
908 info->var.bits_per_pixel, UVESAFB_EXACT_RES);
910 if (modeid == -1)
911 return -EINVAL;
913 uvesafb_setup_var(&info->var, info, &par->vbe_modes[modeid]);
915 gotmode:
917 * If we are not VBE3.0+ compliant, we're done -- the BIOS will
918 * ignore our timings anyway.
920 if (par->vbe_ib.vbe_version < 0x0300 || par->nocrtc)
921 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
922 &info->var, info);
924 return modeid;
927 static int uvesafb_setpalette(struct uvesafb_pal_entry *entries, int count,
928 int start, struct fb_info *info)
930 struct uvesafb_ktask *task;
931 #ifdef CONFIG_X86
932 struct uvesafb_par *par = info->par;
933 int i = par->mode_idx;
934 #endif
935 int err = 0;
938 * We support palette modifications for 8 bpp modes only, so
939 * there can never be more than 256 entries.
941 if (start + count > 256)
942 return -EINVAL;
944 #ifdef CONFIG_X86
945 /* Use VGA registers if mode is VGA-compatible. */
946 if (i >= 0 && i < par->vbe_modes_cnt &&
947 par->vbe_modes[i].mode_attr & VBE_MODE_VGACOMPAT) {
948 for (i = 0; i < count; i++) {
949 outb_p(start + i, dac_reg);
950 outb_p(entries[i].red, dac_val);
951 outb_p(entries[i].green, dac_val);
952 outb_p(entries[i].blue, dac_val);
955 #ifdef CONFIG_X86_32
956 else if (par->pmi_setpal) {
957 __asm__ __volatile__(
958 "call *(%%esi)"
959 : /* no return value */
960 : "a" (0x4f09), /* EAX */
961 "b" (0), /* EBX */
962 "c" (count), /* ECX */
963 "d" (start), /* EDX */
964 "D" (entries), /* EDI */
965 "S" (&par->pmi_pal)); /* ESI */
967 #endif /* CONFIG_X86_32 */
968 else
969 #endif /* CONFIG_X86 */
971 task = uvesafb_prep();
972 if (!task)
973 return -ENOMEM;
975 task->t.regs.eax = 0x4f09;
976 task->t.regs.ebx = 0x0;
977 task->t.regs.ecx = count;
978 task->t.regs.edx = start;
979 task->t.flags = TF_BUF_ESDI;
980 task->t.buf_len = sizeof(struct uvesafb_pal_entry) * count;
981 task->buf = entries;
983 err = uvesafb_exec(task);
984 if ((task->t.regs.eax & 0xffff) != 0x004f)
985 err = 1;
987 uvesafb_free(task);
989 return err;
992 static int uvesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
993 unsigned blue, unsigned transp,
994 struct fb_info *info)
996 struct uvesafb_pal_entry entry;
997 int shift = 16 - dac_width;
998 int err = 0;
1000 if (regno >= info->cmap.len)
1001 return -EINVAL;
1003 if (info->var.bits_per_pixel == 8) {
1004 entry.red = red >> shift;
1005 entry.green = green >> shift;
1006 entry.blue = blue >> shift;
1007 entry.pad = 0;
1009 err = uvesafb_setpalette(&entry, 1, regno, info);
1010 } else if (regno < 16) {
1011 switch (info->var.bits_per_pixel) {
1012 case 16:
1013 if (info->var.red.offset == 10) {
1014 /* 1:5:5:5 */
1015 ((u32 *) (info->pseudo_palette))[regno] =
1016 ((red & 0xf800) >> 1) |
1017 ((green & 0xf800) >> 6) |
1018 ((blue & 0xf800) >> 11);
1019 } else {
1020 /* 0:5:6:5 */
1021 ((u32 *) (info->pseudo_palette))[regno] =
1022 ((red & 0xf800) ) |
1023 ((green & 0xfc00) >> 5) |
1024 ((blue & 0xf800) >> 11);
1026 break;
1028 case 24:
1029 case 32:
1030 red >>= 8;
1031 green >>= 8;
1032 blue >>= 8;
1033 ((u32 *)(info->pseudo_palette))[regno] =
1034 (red << info->var.red.offset) |
1035 (green << info->var.green.offset) |
1036 (blue << info->var.blue.offset);
1037 break;
1040 return err;
1043 static int uvesafb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1045 struct uvesafb_pal_entry *entries;
1046 int shift = 16 - dac_width;
1047 int i, err = 0;
1049 if (info->var.bits_per_pixel == 8) {
1050 if (cmap->start + cmap->len > info->cmap.start +
1051 info->cmap.len || cmap->start < info->cmap.start)
1052 return -EINVAL;
1054 entries = kmalloc(sizeof(*entries) * cmap->len, GFP_KERNEL);
1055 if (!entries)
1056 return -ENOMEM;
1058 for (i = 0; i < cmap->len; i++) {
1059 entries[i].red = cmap->red[i] >> shift;
1060 entries[i].green = cmap->green[i] >> shift;
1061 entries[i].blue = cmap->blue[i] >> shift;
1062 entries[i].pad = 0;
1064 err = uvesafb_setpalette(entries, cmap->len, cmap->start, info);
1065 kfree(entries);
1066 } else {
1068 * For modes with bpp > 8, we only set the pseudo palette in
1069 * the fb_info struct. We rely on uvesafb_setcolreg to do all
1070 * sanity checking.
1072 for (i = 0; i < cmap->len; i++) {
1073 err |= uvesafb_setcolreg(cmap->start + i, cmap->red[i],
1074 cmap->green[i], cmap->blue[i],
1075 0, info);
1078 return err;
1081 static int uvesafb_pan_display(struct fb_var_screeninfo *var,
1082 struct fb_info *info)
1084 #ifdef CONFIG_X86_32
1085 int offset;
1086 struct uvesafb_par *par = info->par;
1088 offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
1091 * It turns out it's not the best idea to do panning via vm86,
1092 * so we only allow it if we have a PMI.
1094 if (par->pmi_start) {
1095 __asm__ __volatile__(
1096 "call *(%%edi)"
1097 : /* no return value */
1098 : "a" (0x4f07), /* EAX */
1099 "b" (0), /* EBX */
1100 "c" (offset), /* ECX */
1101 "d" (offset >> 16), /* EDX */
1102 "D" (&par->pmi_start)); /* EDI */
1104 #endif
1105 return 0;
1108 static int uvesafb_blank(int blank, struct fb_info *info)
1110 struct uvesafb_ktask *task;
1111 int err = 1;
1112 #ifdef CONFIG_X86
1113 struct uvesafb_par *par = info->par;
1115 if (par->vbe_ib.capabilities & VBE_CAP_VGACOMPAT) {
1116 int loop = 10000;
1117 u8 seq = 0, crtc17 = 0;
1119 if (blank == FB_BLANK_POWERDOWN) {
1120 seq = 0x20;
1121 crtc17 = 0x00;
1122 err = 0;
1123 } else {
1124 seq = 0x00;
1125 crtc17 = 0x80;
1126 err = (blank == FB_BLANK_UNBLANK) ? 0 : -EINVAL;
1129 vga_wseq(NULL, 0x00, 0x01);
1130 seq |= vga_rseq(NULL, 0x01) & ~0x20;
1131 vga_wseq(NULL, 0x00, seq);
1133 crtc17 |= vga_rcrt(NULL, 0x17) & ~0x80;
1134 while (loop--);
1135 vga_wcrt(NULL, 0x17, crtc17);
1136 vga_wseq(NULL, 0x00, 0x03);
1137 } else
1138 #endif /* CONFIG_X86 */
1140 task = uvesafb_prep();
1141 if (!task)
1142 return -ENOMEM;
1144 task->t.regs.eax = 0x4f10;
1145 switch (blank) {
1146 case FB_BLANK_UNBLANK:
1147 task->t.regs.ebx = 0x0001;
1148 break;
1149 case FB_BLANK_NORMAL:
1150 task->t.regs.ebx = 0x0101; /* standby */
1151 break;
1152 case FB_BLANK_POWERDOWN:
1153 task->t.regs.ebx = 0x0401; /* powerdown */
1154 break;
1155 default:
1156 goto out;
1159 err = uvesafb_exec(task);
1160 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
1161 err = 1;
1162 out: uvesafb_free(task);
1164 return err;
1167 static int uvesafb_open(struct fb_info *info, int user)
1169 struct uvesafb_par *par = info->par;
1170 int cnt = atomic_read(&par->ref_count);
1172 if (!cnt && par->vbe_state_size)
1173 par->vbe_state_orig = uvesafb_vbe_state_save(par);
1175 atomic_inc(&par->ref_count);
1176 return 0;
1179 static int uvesafb_release(struct fb_info *info, int user)
1181 struct uvesafb_ktask *task = NULL;
1182 struct uvesafb_par *par = info->par;
1183 int cnt = atomic_read(&par->ref_count);
1185 if (!cnt)
1186 return -EINVAL;
1188 if (cnt != 1)
1189 goto out;
1191 task = uvesafb_prep();
1192 if (!task)
1193 goto out;
1195 /* First, try to set the standard 80x25 text mode. */
1196 task->t.regs.eax = 0x0003;
1197 uvesafb_exec(task);
1200 * Now try to restore whatever hardware state we might have
1201 * saved when the fb device was first opened.
1203 uvesafb_vbe_state_restore(par, par->vbe_state_orig);
1204 out:
1205 atomic_dec(&par->ref_count);
1206 if (task)
1207 uvesafb_free(task);
1208 return 0;
1211 static int uvesafb_set_par(struct fb_info *info)
1213 struct uvesafb_par *par = info->par;
1214 struct uvesafb_ktask *task = NULL;
1215 struct vbe_crtc_ib *crtc = NULL;
1216 struct vbe_mode_ib *mode = NULL;
1217 int i, err = 0, depth = info->var.bits_per_pixel;
1219 if (depth > 8 && depth != 32)
1220 depth = info->var.red.length + info->var.green.length +
1221 info->var.blue.length;
1223 i = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres, depth,
1224 UVESAFB_EXACT_RES | UVESAFB_EXACT_DEPTH);
1225 if (i >= 0)
1226 mode = &par->vbe_modes[i];
1227 else
1228 return -EINVAL;
1230 task = uvesafb_prep();
1231 if (!task)
1232 return -ENOMEM;
1233 setmode:
1234 task->t.regs.eax = 0x4f02;
1235 task->t.regs.ebx = mode->mode_id | 0x4000; /* use LFB */
1237 if (par->vbe_ib.vbe_version >= 0x0300 && !par->nocrtc &&
1238 info->var.pixclock != 0) {
1239 task->t.regs.ebx |= 0x0800; /* use CRTC data */
1240 task->t.flags = TF_BUF_ESDI;
1241 crtc = kzalloc(sizeof(struct vbe_crtc_ib), GFP_KERNEL);
1242 if (!crtc) {
1243 err = -ENOMEM;
1244 goto out;
1246 crtc->horiz_start = info->var.xres + info->var.right_margin;
1247 crtc->horiz_end = crtc->horiz_start + info->var.hsync_len;
1248 crtc->horiz_total = crtc->horiz_end + info->var.left_margin;
1250 crtc->vert_start = info->var.yres + info->var.lower_margin;
1251 crtc->vert_end = crtc->vert_start + info->var.vsync_len;
1252 crtc->vert_total = crtc->vert_end + info->var.upper_margin;
1254 crtc->pixel_clock = PICOS2KHZ(info->var.pixclock) * 1000;
1255 crtc->refresh_rate = (u16)(100 * (crtc->pixel_clock /
1256 (crtc->vert_total * crtc->horiz_total)));
1258 if (info->var.vmode & FB_VMODE_DOUBLE)
1259 crtc->flags |= 0x1;
1260 if (info->var.vmode & FB_VMODE_INTERLACED)
1261 crtc->flags |= 0x2;
1262 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
1263 crtc->flags |= 0x4;
1264 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
1265 crtc->flags |= 0x8;
1266 memcpy(&par->crtc, crtc, sizeof(*crtc));
1267 } else {
1268 memset(&par->crtc, 0, sizeof(*crtc));
1271 task->t.buf_len = sizeof(struct vbe_crtc_ib);
1272 task->buf = &par->crtc;
1274 err = uvesafb_exec(task);
1275 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
1277 * The mode switch might have failed because we tried to
1278 * use our own timings. Try again with the default timings.
1280 if (crtc != NULL) {
1281 printk(KERN_WARNING "uvesafb: mode switch failed "
1282 "(eax=0x%x, err=%d). Trying again with "
1283 "default timings.\n", task->t.regs.eax, err);
1284 uvesafb_reset(task);
1285 kfree(crtc);
1286 crtc = NULL;
1287 info->var.pixclock = 0;
1288 goto setmode;
1289 } else {
1290 printk(KERN_ERR "uvesafb: mode switch failed (eax="
1291 "0x%x, err=%d)\n", task->t.regs.eax, err);
1292 err = -EINVAL;
1293 goto out;
1296 par->mode_idx = i;
1298 /* For 8bpp modes, always try to set the DAC to 8 bits. */
1299 if (par->vbe_ib.capabilities & VBE_CAP_CAN_SWITCH_DAC &&
1300 mode->bits_per_pixel <= 8) {
1301 uvesafb_reset(task);
1302 task->t.regs.eax = 0x4f08;
1303 task->t.regs.ebx = 0x0800;
1305 err = uvesafb_exec(task);
1306 if (err || (task->t.regs.eax & 0xffff) != 0x004f ||
1307 ((task->t.regs.ebx & 0xff00) >> 8) != 8) {
1308 dac_width = 6;
1309 } else {
1310 dac_width = 8;
1314 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1315 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1316 info->fix.line_length = mode->bytes_per_scan_line;
1318 out: if (crtc != NULL)
1319 kfree(crtc);
1320 uvesafb_free(task);
1322 return err;
1325 static void uvesafb_check_limits(struct fb_var_screeninfo *var,
1326 struct fb_info *info)
1328 const struct fb_videomode *mode;
1329 struct uvesafb_par *par = info->par;
1332 * If pixclock is set to 0, then we're using default BIOS timings
1333 * and thus don't have to perform any checks here.
1335 if (!var->pixclock)
1336 return;
1338 if (par->vbe_ib.vbe_version < 0x0300) {
1339 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, var, info);
1340 return;
1343 if (!fb_validate_mode(var, info))
1344 return;
1346 mode = fb_find_best_mode(var, &info->modelist);
1347 if (mode) {
1348 if (mode->xres == var->xres && mode->yres == var->yres &&
1349 !(mode->vmode & (FB_VMODE_INTERLACED | FB_VMODE_DOUBLE))) {
1350 fb_videomode_to_var(var, mode);
1351 return;
1355 if (info->monspecs.gtf && !fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1356 return;
1357 /* Use default refresh rate */
1358 var->pixclock = 0;
1361 static int uvesafb_check_var(struct fb_var_screeninfo *var,
1362 struct fb_info *info)
1364 struct uvesafb_par *par = info->par;
1365 struct vbe_mode_ib *mode = NULL;
1366 int match = -1;
1367 int depth = var->red.length + var->green.length + var->blue.length;
1370 * Various apps will use bits_per_pixel to set the color depth,
1371 * which is theoretically incorrect, but which we'll try to handle
1372 * here.
1374 if (depth == 0 || abs(depth - var->bits_per_pixel) >= 8)
1375 depth = var->bits_per_pixel;
1377 match = uvesafb_vbe_find_mode(par, var->xres, var->yres, depth,
1378 UVESAFB_EXACT_RES);
1379 if (match == -1)
1380 return -EINVAL;
1382 mode = &par->vbe_modes[match];
1383 uvesafb_setup_var(var, info, mode);
1386 * Check whether we have remapped enough memory for this mode.
1387 * We might be called at an early stage, when we haven't remapped
1388 * any memory yet, in which case we simply skip the check.
1390 if (var->yres * mode->bytes_per_scan_line > info->fix.smem_len
1391 && info->fix.smem_len)
1392 return -EINVAL;
1394 if ((var->vmode & FB_VMODE_DOUBLE) &&
1395 !(par->vbe_modes[match].mode_attr & 0x100))
1396 var->vmode &= ~FB_VMODE_DOUBLE;
1398 if ((var->vmode & FB_VMODE_INTERLACED) &&
1399 !(par->vbe_modes[match].mode_attr & 0x200))
1400 var->vmode &= ~FB_VMODE_INTERLACED;
1402 uvesafb_check_limits(var, info);
1404 var->xres_virtual = var->xres;
1405 var->yres_virtual = (par->ypan) ?
1406 info->fix.smem_len / mode->bytes_per_scan_line :
1407 var->yres;
1408 return 0;
1411 static void uvesafb_save_state(struct fb_info *info)
1413 struct uvesafb_par *par = info->par;
1415 if (par->vbe_state_saved)
1416 kfree(par->vbe_state_saved);
1418 par->vbe_state_saved = uvesafb_vbe_state_save(par);
1421 static void uvesafb_restore_state(struct fb_info *info)
1423 struct uvesafb_par *par = info->par;
1425 uvesafb_vbe_state_restore(par, par->vbe_state_saved);
1428 static struct fb_ops uvesafb_ops = {
1429 .owner = THIS_MODULE,
1430 .fb_open = uvesafb_open,
1431 .fb_release = uvesafb_release,
1432 .fb_setcolreg = uvesafb_setcolreg,
1433 .fb_setcmap = uvesafb_setcmap,
1434 .fb_pan_display = uvesafb_pan_display,
1435 .fb_blank = uvesafb_blank,
1436 .fb_fillrect = cfb_fillrect,
1437 .fb_copyarea = cfb_copyarea,
1438 .fb_imageblit = cfb_imageblit,
1439 .fb_check_var = uvesafb_check_var,
1440 .fb_set_par = uvesafb_set_par,
1441 .fb_save_state = uvesafb_save_state,
1442 .fb_restore_state = uvesafb_restore_state,
1445 static void __devinit uvesafb_init_info(struct fb_info *info,
1446 struct vbe_mode_ib *mode)
1448 unsigned int size_vmode;
1449 unsigned int size_remap;
1450 unsigned int size_total;
1451 struct uvesafb_par *par = info->par;
1452 int i, h;
1454 info->pseudo_palette = ((u8 *)info->par + sizeof(struct uvesafb_par));
1455 info->fix = uvesafb_fix;
1456 info->fix.ypanstep = par->ypan ? 1 : 0;
1457 info->fix.ywrapstep = (par->ypan > 1) ? 1 : 0;
1460 * If we were unable to get the state buffer size, disable
1461 * functions for saving and restoring the hardware state.
1463 if (par->vbe_state_size == 0) {
1464 info->fbops->fb_save_state = NULL;
1465 info->fbops->fb_restore_state = NULL;
1468 /* Disable blanking if the user requested so. */
1469 if (!blank)
1470 info->fbops->fb_blank = NULL;
1473 * Find out how much IO memory is required for the mode with
1474 * the highest resolution.
1476 size_remap = 0;
1477 for (i = 0; i < par->vbe_modes_cnt; i++) {
1478 h = par->vbe_modes[i].bytes_per_scan_line *
1479 par->vbe_modes[i].y_res;
1480 if (h > size_remap)
1481 size_remap = h;
1483 size_remap *= 2;
1486 * size_vmode -- that is the amount of memory needed for the
1487 * used video mode, i.e. the minimum amount of
1488 * memory we need.
1490 if (mode != NULL) {
1491 size_vmode = info->var.yres * mode->bytes_per_scan_line;
1492 } else {
1493 size_vmode = info->var.yres * info->var.xres *
1494 ((info->var.bits_per_pixel + 7) >> 3);
1498 * size_total -- all video memory we have. Used for mtrr
1499 * entries, resource allocation and bounds
1500 * checking.
1502 size_total = par->vbe_ib.total_memory * 65536;
1503 if (vram_total)
1504 size_total = vram_total * 1024 * 1024;
1505 if (size_total < size_vmode)
1506 size_total = size_vmode;
1509 * size_remap -- the amount of video memory we are going to
1510 * use for vesafb. With modern cards it is no
1511 * option to simply use size_total as th
1512 * wastes plenty of kernel address space.
1514 if (vram_remap)
1515 size_remap = vram_remap * 1024 * 1024;
1516 if (size_remap < size_vmode)
1517 size_remap = size_vmode;
1518 if (size_remap > size_total)
1519 size_remap = size_total;
1521 info->fix.smem_len = size_remap;
1522 info->fix.smem_start = mode->phys_base_ptr;
1525 * We have to set yres_virtual here because when setup_var() was
1526 * called, smem_len wasn't defined yet.
1528 info->var.yres_virtual = info->fix.smem_len /
1529 mode->bytes_per_scan_line;
1531 if (par->ypan && info->var.yres_virtual > info->var.yres) {
1532 printk(KERN_INFO "uvesafb: scrolling: %s "
1533 "using protected mode interface, "
1534 "yres_virtual=%d\n",
1535 (par->ypan > 1) ? "ywrap" : "ypan",
1536 info->var.yres_virtual);
1537 } else {
1538 printk(KERN_INFO "uvesafb: scrolling: redraw\n");
1539 info->var.yres_virtual = info->var.yres;
1540 par->ypan = 0;
1543 info->flags = FBINFO_FLAG_DEFAULT |
1544 (par->ypan ? FBINFO_HWACCEL_YPAN : 0);
1546 if (!par->ypan)
1547 info->fbops->fb_pan_display = NULL;
1550 static void __devinit uvesafb_init_mtrr(struct fb_info *info)
1552 #ifdef CONFIG_MTRR
1553 if (mtrr && !(info->fix.smem_start & (PAGE_SIZE - 1))) {
1554 int temp_size = info->fix.smem_len;
1555 unsigned int type = 0;
1557 switch (mtrr) {
1558 case 1:
1559 type = MTRR_TYPE_UNCACHABLE;
1560 break;
1561 case 2:
1562 type = MTRR_TYPE_WRBACK;
1563 break;
1564 case 3:
1565 type = MTRR_TYPE_WRCOMB;
1566 break;
1567 case 4:
1568 type = MTRR_TYPE_WRTHROUGH;
1569 break;
1570 default:
1571 type = 0;
1572 break;
1575 if (type) {
1576 int rc;
1578 /* Find the largest power-of-two */
1579 while (temp_size & (temp_size - 1))
1580 temp_size &= (temp_size - 1);
1582 /* Try and find a power of two to add */
1583 do {
1584 rc = mtrr_add(info->fix.smem_start,
1585 temp_size, type, 1);
1586 temp_size >>= 1;
1587 } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
1590 #endif /* CONFIG_MTRR */
1594 static ssize_t uvesafb_show_vbe_ver(struct device *dev,
1595 struct device_attribute *attr, char *buf)
1597 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1598 struct uvesafb_par *par = info->par;
1600 return snprintf(buf, PAGE_SIZE, "%.4x\n", par->vbe_ib.vbe_version);
1603 static DEVICE_ATTR(vbe_version, S_IRUGO, uvesafb_show_vbe_ver, NULL);
1605 static ssize_t uvesafb_show_vbe_modes(struct device *dev,
1606 struct device_attribute *attr, char *buf)
1608 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1609 struct uvesafb_par *par = info->par;
1610 int ret = 0, i;
1612 for (i = 0; i < par->vbe_modes_cnt && ret < PAGE_SIZE; i++) {
1613 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1614 "%dx%d-%d, 0x%.4x\n",
1615 par->vbe_modes[i].x_res, par->vbe_modes[i].y_res,
1616 par->vbe_modes[i].depth, par->vbe_modes[i].mode_id);
1619 return ret;
1622 static DEVICE_ATTR(vbe_modes, S_IRUGO, uvesafb_show_vbe_modes, NULL);
1624 static ssize_t uvesafb_show_vendor(struct device *dev,
1625 struct device_attribute *attr, char *buf)
1627 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1628 struct uvesafb_par *par = info->par;
1630 if (par->vbe_ib.oem_vendor_name_ptr)
1631 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1632 (&par->vbe_ib) + par->vbe_ib.oem_vendor_name_ptr);
1633 else
1634 return 0;
1637 static DEVICE_ATTR(oem_vendor, S_IRUGO, uvesafb_show_vendor, NULL);
1639 static ssize_t uvesafb_show_product_name(struct device *dev,
1640 struct device_attribute *attr, char *buf)
1642 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1643 struct uvesafb_par *par = info->par;
1645 if (par->vbe_ib.oem_product_name_ptr)
1646 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1647 (&par->vbe_ib) + par->vbe_ib.oem_product_name_ptr);
1648 else
1649 return 0;
1652 static DEVICE_ATTR(oem_product_name, S_IRUGO, uvesafb_show_product_name, NULL);
1654 static ssize_t uvesafb_show_product_rev(struct device *dev,
1655 struct device_attribute *attr, char *buf)
1657 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1658 struct uvesafb_par *par = info->par;
1660 if (par->vbe_ib.oem_product_rev_ptr)
1661 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1662 (&par->vbe_ib) + par->vbe_ib.oem_product_rev_ptr);
1663 else
1664 return 0;
1667 static DEVICE_ATTR(oem_product_rev, S_IRUGO, uvesafb_show_product_rev, NULL);
1669 static ssize_t uvesafb_show_oem_string(struct device *dev,
1670 struct device_attribute *attr, char *buf)
1672 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1673 struct uvesafb_par *par = info->par;
1675 if (par->vbe_ib.oem_string_ptr)
1676 return snprintf(buf, PAGE_SIZE, "%s\n",
1677 (char *)(&par->vbe_ib) + par->vbe_ib.oem_string_ptr);
1678 else
1679 return 0;
1682 static DEVICE_ATTR(oem_string, S_IRUGO, uvesafb_show_oem_string, NULL);
1684 static ssize_t uvesafb_show_nocrtc(struct device *dev,
1685 struct device_attribute *attr, char *buf)
1687 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1688 struct uvesafb_par *par = info->par;
1690 return snprintf(buf, PAGE_SIZE, "%d\n", par->nocrtc);
1693 static ssize_t uvesafb_store_nocrtc(struct device *dev,
1694 struct device_attribute *attr, const char *buf, size_t count)
1696 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1697 struct uvesafb_par *par = info->par;
1699 if (count > 0) {
1700 if (buf[0] == '0')
1701 par->nocrtc = 0;
1702 else
1703 par->nocrtc = 1;
1705 return count;
1708 static DEVICE_ATTR(nocrtc, S_IRUGO | S_IWUSR, uvesafb_show_nocrtc,
1709 uvesafb_store_nocrtc);
1711 static struct attribute *uvesafb_dev_attrs[] = {
1712 &dev_attr_vbe_version.attr,
1713 &dev_attr_vbe_modes.attr,
1714 &dev_attr_oem_vendor.attr,
1715 &dev_attr_oem_product_name.attr,
1716 &dev_attr_oem_product_rev.attr,
1717 &dev_attr_oem_string.attr,
1718 &dev_attr_nocrtc.attr,
1719 NULL,
1722 static struct attribute_group uvesafb_dev_attgrp = {
1723 .name = NULL,
1724 .attrs = uvesafb_dev_attrs,
1727 static int __devinit uvesafb_probe(struct platform_device *dev)
1729 struct fb_info *info;
1730 struct vbe_mode_ib *mode = NULL;
1731 struct uvesafb_par *par;
1732 int err = 0, i;
1734 info = framebuffer_alloc(sizeof(*par) + sizeof(u32) * 256, &dev->dev);
1735 if (!info)
1736 return -ENOMEM;
1738 par = info->par;
1740 err = uvesafb_vbe_init(info);
1741 if (err) {
1742 printk(KERN_ERR "uvesafb: vbe_init() failed with %d\n", err);
1743 goto out;
1746 info->fbops = &uvesafb_ops;
1748 i = uvesafb_vbe_init_mode(info);
1749 if (i < 0) {
1750 err = -EINVAL;
1751 goto out;
1752 } else {
1753 mode = &par->vbe_modes[i];
1756 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1757 err = -ENXIO;
1758 goto out;
1761 uvesafb_init_info(info, mode);
1763 if (!request_mem_region(info->fix.smem_start, info->fix.smem_len,
1764 "uvesafb")) {
1765 printk(KERN_ERR "uvesafb: cannot reserve video memory at "
1766 "0x%lx\n", info->fix.smem_start);
1767 err = -EIO;
1768 goto out_mode;
1771 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
1773 if (!info->screen_base) {
1774 printk(KERN_ERR
1775 "uvesafb: abort, cannot ioremap 0x%x bytes of video "
1776 "memory at 0x%lx\n",
1777 info->fix.smem_len, info->fix.smem_start);
1778 err = -EIO;
1779 goto out_mem;
1782 if (!request_region(0x3c0, 32, "uvesafb")) {
1783 printk(KERN_ERR "uvesafb: request region 0x3c0-0x3e0 failed\n");
1784 err = -EIO;
1785 goto out_unmap;
1788 uvesafb_init_mtrr(info);
1789 platform_set_drvdata(dev, info);
1791 if (register_framebuffer(info) < 0) {
1792 printk(KERN_ERR
1793 "uvesafb: failed to register framebuffer device\n");
1794 err = -EINVAL;
1795 goto out_reg;
1798 printk(KERN_INFO "uvesafb: framebuffer at 0x%lx, mapped to 0x%p, "
1799 "using %dk, total %dk\n", info->fix.smem_start,
1800 info->screen_base, info->fix.smem_len/1024,
1801 par->vbe_ib.total_memory * 64);
1802 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
1803 info->fix.id);
1805 err = sysfs_create_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1806 if (err != 0)
1807 printk(KERN_WARNING "fb%d: failed to register attributes\n",
1808 info->node);
1810 return 0;
1812 out_reg:
1813 release_region(0x3c0, 32);
1814 out_unmap:
1815 iounmap(info->screen_base);
1816 out_mem:
1817 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1818 out_mode:
1819 if (!list_empty(&info->modelist))
1820 fb_destroy_modelist(&info->modelist);
1821 fb_destroy_modedb(info->monspecs.modedb);
1822 fb_dealloc_cmap(&info->cmap);
1823 out:
1824 if (par->vbe_modes)
1825 kfree(par->vbe_modes);
1827 framebuffer_release(info);
1828 return err;
1831 static int uvesafb_remove(struct platform_device *dev)
1833 struct fb_info *info = platform_get_drvdata(dev);
1835 if (info) {
1836 struct uvesafb_par *par = info->par;
1838 sysfs_remove_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1839 unregister_framebuffer(info);
1840 release_region(0x3c0, 32);
1841 iounmap(info->screen_base);
1842 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1843 fb_destroy_modedb(info->monspecs.modedb);
1844 fb_dealloc_cmap(&info->cmap);
1846 if (par) {
1847 if (par->vbe_modes)
1848 kfree(par->vbe_modes);
1849 if (par->vbe_state_orig)
1850 kfree(par->vbe_state_orig);
1851 if (par->vbe_state_saved)
1852 kfree(par->vbe_state_saved);
1855 framebuffer_release(info);
1857 return 0;
1860 static struct platform_driver uvesafb_driver = {
1861 .probe = uvesafb_probe,
1862 .remove = uvesafb_remove,
1863 .driver = {
1864 .name = "uvesafb",
1868 static struct platform_device *uvesafb_device;
1870 #ifndef MODULE
1871 static int __devinit uvesafb_setup(char *options)
1873 char *this_opt;
1875 if (!options || !*options)
1876 return 0;
1878 while ((this_opt = strsep(&options, ",")) != NULL) {
1879 if (!*this_opt) continue;
1881 if (!strcmp(this_opt, "redraw"))
1882 ypan = 0;
1883 else if (!strcmp(this_opt, "ypan"))
1884 ypan = 1;
1885 else if (!strcmp(this_opt, "ywrap"))
1886 ypan = 2;
1887 else if (!strcmp(this_opt, "vgapal"))
1888 pmi_setpal = 0;
1889 else if (!strcmp(this_opt, "pmipal"))
1890 pmi_setpal = 1;
1891 else if (!strncmp(this_opt, "mtrr:", 5))
1892 mtrr = simple_strtoul(this_opt+5, NULL, 0);
1893 else if (!strcmp(this_opt, "nomtrr"))
1894 mtrr = 0;
1895 else if (!strcmp(this_opt, "nocrtc"))
1896 nocrtc = 1;
1897 else if (!strcmp(this_opt, "noedid"))
1898 noedid = 1;
1899 else if (!strcmp(this_opt, "noblank"))
1900 blank = 0;
1901 else if (!strncmp(this_opt, "vtotal:", 7))
1902 vram_total = simple_strtoul(this_opt + 7, NULL, 0);
1903 else if (!strncmp(this_opt, "vremap:", 7))
1904 vram_remap = simple_strtoul(this_opt + 7, NULL, 0);
1905 else if (!strncmp(this_opt, "maxhf:", 6))
1906 maxhf = simple_strtoul(this_opt + 6, NULL, 0);
1907 else if (!strncmp(this_opt, "maxvf:", 6))
1908 maxvf = simple_strtoul(this_opt + 6, NULL, 0);
1909 else if (!strncmp(this_opt, "maxclk:", 7))
1910 maxclk = simple_strtoul(this_opt + 7, NULL, 0);
1911 else if (!strncmp(this_opt, "vbemode:", 8))
1912 vbemode = simple_strtoul(this_opt + 8, NULL, 0);
1913 else if (this_opt[0] >= '0' && this_opt[0] <= '9') {
1914 mode_option = this_opt;
1915 } else {
1916 printk(KERN_WARNING
1917 "uvesafb: unrecognized option %s\n", this_opt);
1921 return 0;
1923 #endif /* !MODULE */
1925 static ssize_t show_v86d(struct device_driver *dev, char *buf)
1927 return snprintf(buf, PAGE_SIZE, "%s\n", v86d_path);
1930 static ssize_t store_v86d(struct device_driver *dev, const char *buf,
1931 size_t count)
1933 strncpy(v86d_path, buf, PATH_MAX);
1934 return count;
1937 static DRIVER_ATTR(v86d, S_IRUGO | S_IWUSR, show_v86d, store_v86d);
1939 static int __devinit uvesafb_init(void)
1941 int err;
1943 #ifndef MODULE
1944 char *option = NULL;
1946 if (fb_get_options("uvesafb", &option))
1947 return -ENODEV;
1948 uvesafb_setup(option);
1949 #endif
1950 err = cn_add_callback(&uvesafb_cn_id, "uvesafb", uvesafb_cn_callback);
1951 if (err)
1952 return err;
1954 err = platform_driver_register(&uvesafb_driver);
1956 if (!err) {
1957 uvesafb_device = platform_device_alloc("uvesafb", 0);
1958 if (uvesafb_device)
1959 err = platform_device_add(uvesafb_device);
1960 else
1961 err = -ENOMEM;
1963 if (err) {
1964 platform_device_put(uvesafb_device);
1965 platform_driver_unregister(&uvesafb_driver);
1966 cn_del_callback(&uvesafb_cn_id);
1967 return err;
1970 err = driver_create_file(&uvesafb_driver.driver,
1971 &driver_attr_v86d);
1972 if (err) {
1973 printk(KERN_WARNING "uvesafb: failed to register "
1974 "attributes\n");
1975 err = 0;
1978 return err;
1981 module_init(uvesafb_init);
1983 static void __devexit uvesafb_exit(void)
1985 struct uvesafb_ktask *task;
1987 if (v86d_started) {
1988 task = uvesafb_prep();
1989 if (task) {
1990 task->t.flags = TF_EXIT;
1991 uvesafb_exec(task);
1992 uvesafb_free(task);
1996 cn_del_callback(&uvesafb_cn_id);
1997 driver_remove_file(&uvesafb_driver.driver, &driver_attr_v86d);
1998 platform_device_unregister(uvesafb_device);
1999 platform_driver_unregister(&uvesafb_driver);
2002 module_exit(uvesafb_exit);
2004 #define param_get_scroll NULL
2005 static int param_set_scroll(const char *val, struct kernel_param *kp)
2007 ypan = 0;
2009 if (!strcmp(val, "redraw"))
2010 ypan = 0;
2011 else if (!strcmp(val, "ypan"))
2012 ypan = 1;
2013 else if (!strcmp(val, "ywrap"))
2014 ypan = 2;
2015 else
2016 return -EINVAL;
2018 return 0;
2021 #define param_check_scroll(name, p) __param_check(name, p, void)
2023 module_param_named(scroll, ypan, scroll, 0);
2024 MODULE_PARM_DESC(scroll,
2025 "Scrolling mode, set to 'redraw', 'ypan', or 'ywrap'");
2026 module_param_named(vgapal, pmi_setpal, invbool, 0);
2027 MODULE_PARM_DESC(vgapal, "Set palette using VGA registers");
2028 module_param_named(pmipal, pmi_setpal, bool, 0);
2029 MODULE_PARM_DESC(pmipal, "Set palette using PMI calls");
2030 module_param(mtrr, uint, 0);
2031 MODULE_PARM_DESC(mtrr,
2032 "Memory Type Range Registers setting. Use 0 to disable.");
2033 module_param(blank, bool, 0);
2034 MODULE_PARM_DESC(blank, "Enable hardware blanking");
2035 module_param(nocrtc, bool, 0);
2036 MODULE_PARM_DESC(nocrtc, "Ignore CRTC timings when setting modes");
2037 module_param(noedid, bool, 0);
2038 MODULE_PARM_DESC(noedid,
2039 "Ignore EDID-provided monitor limits when setting modes");
2040 module_param(vram_remap, uint, 0);
2041 MODULE_PARM_DESC(vram_remap, "Set amount of video memory to be used [MiB]");
2042 module_param(vram_total, uint, 0);
2043 MODULE_PARM_DESC(vram_total, "Set total amount of video memoery [MiB]");
2044 module_param(maxclk, ushort, 0);
2045 MODULE_PARM_DESC(maxclk, "Maximum pixelclock [MHz], overrides EDID data");
2046 module_param(maxhf, ushort, 0);
2047 MODULE_PARM_DESC(maxhf,
2048 "Maximum horizontal frequency [kHz], overrides EDID data");
2049 module_param(maxvf, ushort, 0);
2050 MODULE_PARM_DESC(maxvf,
2051 "Maximum vertical frequency [Hz], overrides EDID data");
2052 module_param(mode_option, charp, 0);
2053 MODULE_PARM_DESC(mode_option,
2054 "Specify initial video mode as \"<xres>x<yres>[-<bpp>][@<refresh>]\"");
2055 module_param(vbemode, ushort, 0);
2056 MODULE_PARM_DESC(vbemode,
2057 "VBE mode number to set, overrides the 'mode' option");
2058 module_param_string(v86d, v86d_path, PATH_MAX, 0660);
2059 MODULE_PARM_DESC(v86d, "Path to the v86d userspace helper.");
2061 MODULE_LICENSE("GPL");
2062 MODULE_AUTHOR("Michal Januszewski <spock@gentoo.org>");
2063 MODULE_DESCRIPTION("Framebuffer driver for VBE2.0+ compliant graphics boards");