Merge tag 'staging-3.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6.git] / drivers / video / uvesafb.c
blob7aec6f39fdd5461beb16d700ed201212ba9d1704
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 <linux/slab.h>
22 #include <video/edid.h>
23 #include <video/uvesafb.h>
24 #ifdef CONFIG_X86
25 #include <video/vga.h>
26 #endif
27 #include "edid.h"
29 static struct cb_id uvesafb_cn_id = {
30 .idx = CN_IDX_V86D,
31 .val = CN_VAL_V86D_UVESAFB
33 static char v86d_path[PATH_MAX] = "/sbin/v86d";
34 static char v86d_started; /* has v86d been started by uvesafb? */
36 static struct fb_fix_screeninfo uvesafb_fix = {
37 .id = "VESA VGA",
38 .type = FB_TYPE_PACKED_PIXELS,
39 .accel = FB_ACCEL_NONE,
40 .visual = FB_VISUAL_TRUECOLOR,
43 static int mtrr = 3; /* enable mtrr by default */
44 static bool blank = 1; /* enable blanking by default */
45 static int ypan = 1; /* 0: scroll, 1: ypan, 2: ywrap */
46 static bool pmi_setpal = true; /* use PMI for palette changes */
47 static bool nocrtc; /* ignore CRTC settings */
48 static bool noedid; /* don't try DDC transfers */
49 static int vram_remap; /* set amt. of memory to be used */
50 static int vram_total; /* set total amount of memory */
51 static u16 maxclk; /* maximum pixel clock */
52 static u16 maxvf; /* maximum vertical frequency */
53 static u16 maxhf; /* maximum horizontal frequency */
54 static u16 vbemode; /* force use of a specific VBE mode */
55 static char *mode_option;
56 static u8 dac_width = 6;
58 static struct uvesafb_ktask *uvfb_tasks[UVESAFB_TASKS_MAX];
59 static DEFINE_MUTEX(uvfb_lock);
62 * A handler for replies from userspace.
64 * Make sure each message passes consistency checks and if it does,
65 * find the kernel part of the task struct, copy the registers and
66 * the buffer contents and then complete the task.
68 static void uvesafb_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
70 struct uvesafb_task *utask;
71 struct uvesafb_ktask *task;
73 if (!capable(CAP_SYS_ADMIN))
74 return;
76 if (msg->seq >= UVESAFB_TASKS_MAX)
77 return;
79 mutex_lock(&uvfb_lock);
80 task = uvfb_tasks[msg->seq];
82 if (!task || msg->ack != task->ack) {
83 mutex_unlock(&uvfb_lock);
84 return;
87 utask = (struct uvesafb_task *)msg->data;
89 /* Sanity checks for the buffer length. */
90 if (task->t.buf_len < utask->buf_len ||
91 utask->buf_len > msg->len - sizeof(*utask)) {
92 mutex_unlock(&uvfb_lock);
93 return;
96 uvfb_tasks[msg->seq] = NULL;
97 mutex_unlock(&uvfb_lock);
99 memcpy(&task->t, utask, sizeof(*utask));
101 if (task->t.buf_len && task->buf)
102 memcpy(task->buf, utask + 1, task->t.buf_len);
104 complete(task->done);
105 return;
108 static int uvesafb_helper_start(void)
110 char *envp[] = {
111 "HOME=/",
112 "PATH=/sbin:/bin",
113 NULL,
116 char *argv[] = {
117 v86d_path,
118 NULL,
121 return call_usermodehelper(v86d_path, argv, envp, UMH_WAIT_PROC);
125 * Execute a uvesafb task.
127 * Returns 0 if the task is executed successfully.
129 * A message sent to the userspace consists of the uvesafb_task
130 * struct and (optionally) a buffer. The uvesafb_task struct is
131 * a simplified version of uvesafb_ktask (its kernel counterpart)
132 * containing only the register values, flags and the length of
133 * the buffer.
135 * Each message is assigned a sequence number (increased linearly)
136 * and a random ack number. The sequence number is used as a key
137 * for the uvfb_tasks array which holds pointers to uvesafb_ktask
138 * structs for all requests.
140 static int uvesafb_exec(struct uvesafb_ktask *task)
142 static int seq;
143 struct cn_msg *m;
144 int err;
145 int len = sizeof(task->t) + task->t.buf_len;
148 * Check whether the message isn't longer than the maximum
149 * allowed by connector.
151 if (sizeof(*m) + len > CONNECTOR_MAX_MSG_SIZE) {
152 printk(KERN_WARNING "uvesafb: message too long (%d), "
153 "can't execute task\n", (int)(sizeof(*m) + len));
154 return -E2BIG;
157 m = kzalloc(sizeof(*m) + len, GFP_KERNEL);
158 if (!m)
159 return -ENOMEM;
161 init_completion(task->done);
163 memcpy(&m->id, &uvesafb_cn_id, sizeof(m->id));
164 m->seq = seq;
165 m->len = len;
166 m->ack = prandom_u32();
168 /* uvesafb_task structure */
169 memcpy(m + 1, &task->t, sizeof(task->t));
171 /* Buffer */
172 memcpy((u8 *)(m + 1) + sizeof(task->t), task->buf, task->t.buf_len);
175 * Save the message ack number so that we can find the kernel
176 * part of this task when a reply is received from userspace.
178 task->ack = m->ack;
180 mutex_lock(&uvfb_lock);
182 /* If all slots are taken -- bail out. */
183 if (uvfb_tasks[seq]) {
184 mutex_unlock(&uvfb_lock);
185 err = -EBUSY;
186 goto out;
189 /* Save a pointer to the kernel part of the task struct. */
190 uvfb_tasks[seq] = task;
191 mutex_unlock(&uvfb_lock);
193 err = cn_netlink_send(m, 0, GFP_KERNEL);
194 if (err == -ESRCH) {
196 * Try to start the userspace helper if sending
197 * the request failed the first time.
199 err = uvesafb_helper_start();
200 if (err) {
201 printk(KERN_ERR "uvesafb: failed to execute %s\n",
202 v86d_path);
203 printk(KERN_ERR "uvesafb: make sure that the v86d "
204 "helper is installed and executable\n");
205 } else {
206 v86d_started = 1;
207 err = cn_netlink_send(m, 0, gfp_any());
208 if (err == -ENOBUFS)
209 err = 0;
211 } else if (err == -ENOBUFS)
212 err = 0;
214 if (!err && !(task->t.flags & TF_EXIT))
215 err = !wait_for_completion_timeout(task->done,
216 msecs_to_jiffies(UVESAFB_TIMEOUT));
218 mutex_lock(&uvfb_lock);
219 uvfb_tasks[seq] = NULL;
220 mutex_unlock(&uvfb_lock);
222 seq++;
223 if (seq >= UVESAFB_TASKS_MAX)
224 seq = 0;
225 out:
226 kfree(m);
227 return err;
231 * Free a uvesafb_ktask struct.
233 static void uvesafb_free(struct uvesafb_ktask *task)
235 if (task) {
236 if (task->done)
237 kfree(task->done);
238 kfree(task);
243 * Prepare a uvesafb_ktask struct to be used again.
245 static void uvesafb_reset(struct uvesafb_ktask *task)
247 struct completion *cpl = task->done;
249 memset(task, 0, sizeof(*task));
250 task->done = cpl;
254 * Allocate and prepare a uvesafb_ktask struct.
256 static struct uvesafb_ktask *uvesafb_prep(void)
258 struct uvesafb_ktask *task;
260 task = kzalloc(sizeof(*task), GFP_KERNEL);
261 if (task) {
262 task->done = kzalloc(sizeof(*task->done), GFP_KERNEL);
263 if (!task->done) {
264 kfree(task);
265 task = NULL;
268 return task;
271 static void uvesafb_setup_var(struct fb_var_screeninfo *var,
272 struct fb_info *info, struct vbe_mode_ib *mode)
274 struct uvesafb_par *par = info->par;
276 var->vmode = FB_VMODE_NONINTERLACED;
277 var->sync = FB_SYNC_VERT_HIGH_ACT;
279 var->xres = mode->x_res;
280 var->yres = mode->y_res;
281 var->xres_virtual = mode->x_res;
282 var->yres_virtual = (par->ypan) ?
283 info->fix.smem_len / mode->bytes_per_scan_line :
284 mode->y_res;
285 var->xoffset = 0;
286 var->yoffset = 0;
287 var->bits_per_pixel = mode->bits_per_pixel;
289 if (var->bits_per_pixel == 15)
290 var->bits_per_pixel = 16;
292 if (var->bits_per_pixel > 8) {
293 var->red.offset = mode->red_off;
294 var->red.length = mode->red_len;
295 var->green.offset = mode->green_off;
296 var->green.length = mode->green_len;
297 var->blue.offset = mode->blue_off;
298 var->blue.length = mode->blue_len;
299 var->transp.offset = mode->rsvd_off;
300 var->transp.length = mode->rsvd_len;
301 } else {
302 var->red.offset = 0;
303 var->green.offset = 0;
304 var->blue.offset = 0;
305 var->transp.offset = 0;
307 var->red.length = 8;
308 var->green.length = 8;
309 var->blue.length = 8;
310 var->transp.length = 0;
314 static int uvesafb_vbe_find_mode(struct uvesafb_par *par,
315 int xres, int yres, int depth, unsigned char flags)
317 int i, match = -1, h = 0, d = 0x7fffffff;
319 for (i = 0; i < par->vbe_modes_cnt; i++) {
320 h = abs(par->vbe_modes[i].x_res - xres) +
321 abs(par->vbe_modes[i].y_res - yres) +
322 abs(depth - par->vbe_modes[i].depth);
325 * We have an exact match in terms of resolution
326 * and depth.
328 if (h == 0)
329 return i;
331 if (h < d || (h == d && par->vbe_modes[i].depth > depth)) {
332 d = h;
333 match = i;
336 i = 1;
338 if (flags & UVESAFB_EXACT_DEPTH &&
339 par->vbe_modes[match].depth != depth)
340 i = 0;
342 if (flags & UVESAFB_EXACT_RES && d > 24)
343 i = 0;
345 if (i != 0)
346 return match;
347 else
348 return -1;
351 static u8 *uvesafb_vbe_state_save(struct uvesafb_par *par)
353 struct uvesafb_ktask *task;
354 u8 *state;
355 int err;
357 if (!par->vbe_state_size)
358 return NULL;
360 state = kmalloc(par->vbe_state_size, GFP_KERNEL);
361 if (!state)
362 return ERR_PTR(-ENOMEM);
364 task = uvesafb_prep();
365 if (!task) {
366 kfree(state);
367 return NULL;
370 task->t.regs.eax = 0x4f04;
371 task->t.regs.ecx = 0x000f;
372 task->t.regs.edx = 0x0001;
373 task->t.flags = TF_BUF_RET | TF_BUF_ESBX;
374 task->t.buf_len = par->vbe_state_size;
375 task->buf = state;
376 err = uvesafb_exec(task);
378 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
379 printk(KERN_WARNING "uvesafb: VBE get state call "
380 "failed (eax=0x%x, err=%d)\n",
381 task->t.regs.eax, err);
382 kfree(state);
383 state = NULL;
386 uvesafb_free(task);
387 return state;
390 static void uvesafb_vbe_state_restore(struct uvesafb_par *par, u8 *state_buf)
392 struct uvesafb_ktask *task;
393 int err;
395 if (!state_buf)
396 return;
398 task = uvesafb_prep();
399 if (!task)
400 return;
402 task->t.regs.eax = 0x4f04;
403 task->t.regs.ecx = 0x000f;
404 task->t.regs.edx = 0x0002;
405 task->t.buf_len = par->vbe_state_size;
406 task->t.flags = TF_BUF_ESBX;
407 task->buf = state_buf;
409 err = uvesafb_exec(task);
410 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
411 printk(KERN_WARNING "uvesafb: VBE state restore call "
412 "failed (eax=0x%x, err=%d)\n",
413 task->t.regs.eax, err);
415 uvesafb_free(task);
418 static int uvesafb_vbe_getinfo(struct uvesafb_ktask *task,
419 struct uvesafb_par *par)
421 int err;
423 task->t.regs.eax = 0x4f00;
424 task->t.flags = TF_VBEIB;
425 task->t.buf_len = sizeof(struct vbe_ib);
426 task->buf = &par->vbe_ib;
427 strncpy(par->vbe_ib.vbe_signature, "VBE2", 4);
429 err = uvesafb_exec(task);
430 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
431 printk(KERN_ERR "uvesafb: Getting VBE info block failed "
432 "(eax=0x%x, err=%d)\n", (u32)task->t.regs.eax,
433 err);
434 return -EINVAL;
437 if (par->vbe_ib.vbe_version < 0x0200) {
438 printk(KERN_ERR "uvesafb: Sorry, pre-VBE 2.0 cards are "
439 "not supported.\n");
440 return -EINVAL;
443 if (!par->vbe_ib.mode_list_ptr) {
444 printk(KERN_ERR "uvesafb: Missing mode list!\n");
445 return -EINVAL;
448 printk(KERN_INFO "uvesafb: ");
451 * Convert string pointers and the mode list pointer into
452 * usable addresses. Print informational messages about the
453 * video adapter and its vendor.
455 if (par->vbe_ib.oem_vendor_name_ptr)
456 printk("%s, ",
457 ((char *)task->buf) + par->vbe_ib.oem_vendor_name_ptr);
459 if (par->vbe_ib.oem_product_name_ptr)
460 printk("%s, ",
461 ((char *)task->buf) + par->vbe_ib.oem_product_name_ptr);
463 if (par->vbe_ib.oem_product_rev_ptr)
464 printk("%s, ",
465 ((char *)task->buf) + par->vbe_ib.oem_product_rev_ptr);
467 if (par->vbe_ib.oem_string_ptr)
468 printk("OEM: %s, ",
469 ((char *)task->buf) + par->vbe_ib.oem_string_ptr);
471 printk("VBE v%d.%d\n", ((par->vbe_ib.vbe_version & 0xff00) >> 8),
472 par->vbe_ib.vbe_version & 0xff);
474 return 0;
477 static int uvesafb_vbe_getmodes(struct uvesafb_ktask *task,
478 struct uvesafb_par *par)
480 int off = 0, err;
481 u16 *mode;
483 par->vbe_modes_cnt = 0;
485 /* Count available modes. */
486 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
487 while (*mode != 0xffff) {
488 par->vbe_modes_cnt++;
489 mode++;
492 par->vbe_modes = kzalloc(sizeof(struct vbe_mode_ib) *
493 par->vbe_modes_cnt, GFP_KERNEL);
494 if (!par->vbe_modes)
495 return -ENOMEM;
497 /* Get info about all available modes. */
498 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
499 while (*mode != 0xffff) {
500 struct vbe_mode_ib *mib;
502 uvesafb_reset(task);
503 task->t.regs.eax = 0x4f01;
504 task->t.regs.ecx = (u32) *mode;
505 task->t.flags = TF_BUF_RET | TF_BUF_ESDI;
506 task->t.buf_len = sizeof(struct vbe_mode_ib);
507 task->buf = par->vbe_modes + off;
509 err = uvesafb_exec(task);
510 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
511 printk(KERN_WARNING "uvesafb: Getting mode info block "
512 "for mode 0x%x failed (eax=0x%x, err=%d)\n",
513 *mode, (u32)task->t.regs.eax, err);
514 mode++;
515 par->vbe_modes_cnt--;
516 continue;
519 mib = task->buf;
520 mib->mode_id = *mode;
523 * We only want modes that are supported with the current
524 * hardware configuration, color, graphics and that have
525 * support for the LFB.
527 if ((mib->mode_attr & VBE_MODE_MASK) == VBE_MODE_MASK &&
528 mib->bits_per_pixel >= 8)
529 off++;
530 else
531 par->vbe_modes_cnt--;
533 mode++;
534 mib->depth = mib->red_len + mib->green_len + mib->blue_len;
537 * Handle 8bpp modes and modes with broken color component
538 * lengths.
540 if (mib->depth == 0 || (mib->depth == 24 &&
541 mib->bits_per_pixel == 32))
542 mib->depth = mib->bits_per_pixel;
545 if (par->vbe_modes_cnt > 0)
546 return 0;
547 else
548 return -EINVAL;
552 * The Protected Mode Interface is 32-bit x86 code, so we only run it on
553 * x86 and not x86_64.
555 #ifdef CONFIG_X86_32
556 static int uvesafb_vbe_getpmi(struct uvesafb_ktask *task,
557 struct uvesafb_par *par)
559 int i, err;
561 uvesafb_reset(task);
562 task->t.regs.eax = 0x4f0a;
563 task->t.regs.ebx = 0x0;
564 err = uvesafb_exec(task);
566 if ((task->t.regs.eax & 0xffff) != 0x4f || task->t.regs.es < 0xc000) {
567 par->pmi_setpal = par->ypan = 0;
568 } else {
569 par->pmi_base = (u16 *)phys_to_virt(((u32)task->t.regs.es << 4)
570 + task->t.regs.edi);
571 par->pmi_start = (u8 *)par->pmi_base + par->pmi_base[1];
572 par->pmi_pal = (u8 *)par->pmi_base + par->pmi_base[2];
573 printk(KERN_INFO "uvesafb: protected mode interface info at "
574 "%04x:%04x\n",
575 (u16)task->t.regs.es, (u16)task->t.regs.edi);
576 printk(KERN_INFO "uvesafb: pmi: set display start = %p, "
577 "set palette = %p\n", par->pmi_start,
578 par->pmi_pal);
580 if (par->pmi_base[3]) {
581 printk(KERN_INFO "uvesafb: pmi: ports = ");
582 for (i = par->pmi_base[3]/2;
583 par->pmi_base[i] != 0xffff; i++)
584 printk("%x ", par->pmi_base[i]);
585 printk("\n");
587 if (par->pmi_base[i] != 0xffff) {
588 printk(KERN_INFO "uvesafb: can't handle memory"
589 " requests, pmi disabled\n");
590 par->ypan = par->pmi_setpal = 0;
594 return 0;
596 #endif /* CONFIG_X86_32 */
599 * Check whether a video mode is supported by the Video BIOS and is
600 * compatible with the monitor limits.
602 static int uvesafb_is_valid_mode(struct fb_videomode *mode,
603 struct fb_info *info)
605 if (info->monspecs.gtf) {
606 fb_videomode_to_var(&info->var, mode);
607 if (fb_validate_mode(&info->var, info))
608 return 0;
611 if (uvesafb_vbe_find_mode(info->par, mode->xres, mode->yres, 8,
612 UVESAFB_EXACT_RES) == -1)
613 return 0;
615 return 1;
618 static int uvesafb_vbe_getedid(struct uvesafb_ktask *task, 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);
658 if (!task->buf)
659 return -ENOMEM;
661 err = uvesafb_exec(task);
663 if ((task->t.regs.eax & 0xffff) == 0x004f && !err) {
664 fb_edid_to_monspecs(task->buf, &info->monspecs);
666 if (info->monspecs.vfmax && info->monspecs.hfmax) {
668 * If the maximum pixel clock wasn't specified in
669 * the EDID block, set it to 300 MHz.
671 if (info->monspecs.dclkmax == 0)
672 info->monspecs.dclkmax = 300 * 1000000;
673 info->monspecs.gtf = 1;
675 } else {
676 err = -EINVAL;
679 kfree(task->buf);
680 return err;
683 static void uvesafb_vbe_getmonspecs(struct uvesafb_ktask *task,
684 struct fb_info *info)
686 struct uvesafb_par *par = info->par;
687 int i;
689 memset(&info->monspecs, 0, sizeof(info->monspecs));
692 * If we don't get all necessary data from the EDID block,
693 * mark it as incompatible with the GTF and set nocrtc so
694 * that we always use the default BIOS refresh rate.
696 if (uvesafb_vbe_getedid(task, info)) {
697 info->monspecs.gtf = 0;
698 par->nocrtc = 1;
701 /* Kernel command line overrides. */
702 if (maxclk)
703 info->monspecs.dclkmax = maxclk * 1000000;
704 if (maxvf)
705 info->monspecs.vfmax = maxvf;
706 if (maxhf)
707 info->monspecs.hfmax = maxhf * 1000;
710 * In case DDC transfers are not supported, the user can provide
711 * monitor limits manually. Lower limits are set to "safe" values.
713 if (info->monspecs.gtf == 0 && maxclk && maxvf && maxhf) {
714 info->monspecs.dclkmin = 0;
715 info->monspecs.vfmin = 60;
716 info->monspecs.hfmin = 29000;
717 info->monspecs.gtf = 1;
718 par->nocrtc = 0;
721 if (info->monspecs.gtf)
722 printk(KERN_INFO
723 "uvesafb: monitor limits: vf = %d Hz, hf = %d kHz, "
724 "clk = %d MHz\n", info->monspecs.vfmax,
725 (int)(info->monspecs.hfmax / 1000),
726 (int)(info->monspecs.dclkmax / 1000000));
727 else
728 printk(KERN_INFO "uvesafb: no monitor limits have been set, "
729 "default refresh rate will be used\n");
731 /* Add VBE modes to the modelist. */
732 for (i = 0; i < par->vbe_modes_cnt; i++) {
733 struct fb_var_screeninfo var;
734 struct vbe_mode_ib *mode;
735 struct fb_videomode vmode;
737 mode = &par->vbe_modes[i];
738 memset(&var, 0, sizeof(var));
740 var.xres = mode->x_res;
741 var.yres = mode->y_res;
743 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, &var, info);
744 fb_var_to_videomode(&vmode, &var);
745 fb_add_videomode(&vmode, &info->modelist);
748 /* Add valid VESA modes to our modelist. */
749 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
750 if (uvesafb_is_valid_mode((struct fb_videomode *)
751 &vesa_modes[i], info))
752 fb_add_videomode(&vesa_modes[i], &info->modelist);
755 for (i = 0; i < info->monspecs.modedb_len; i++) {
756 if (uvesafb_is_valid_mode(&info->monspecs.modedb[i], info))
757 fb_add_videomode(&info->monspecs.modedb[i],
758 &info->modelist);
761 return;
764 static void uvesafb_vbe_getstatesize(struct uvesafb_ktask *task,
765 struct uvesafb_par *par)
767 int err;
769 uvesafb_reset(task);
772 * Get the VBE state buffer size. We want all available
773 * hardware state data (CL = 0x0f).
775 task->t.regs.eax = 0x4f04;
776 task->t.regs.ecx = 0x000f;
777 task->t.regs.edx = 0x0000;
778 task->t.flags = 0;
780 err = uvesafb_exec(task);
782 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
783 printk(KERN_WARNING "uvesafb: VBE state buffer size "
784 "cannot be determined (eax=0x%x, err=%d)\n",
785 task->t.regs.eax, err);
786 par->vbe_state_size = 0;
787 return;
790 par->vbe_state_size = 64 * (task->t.regs.ebx & 0xffff);
793 static int uvesafb_vbe_init(struct fb_info *info)
795 struct uvesafb_ktask *task = NULL;
796 struct uvesafb_par *par = info->par;
797 int err;
799 task = uvesafb_prep();
800 if (!task)
801 return -ENOMEM;
803 err = uvesafb_vbe_getinfo(task, par);
804 if (err)
805 goto out;
807 err = uvesafb_vbe_getmodes(task, par);
808 if (err)
809 goto out;
811 par->nocrtc = nocrtc;
812 #ifdef CONFIG_X86_32
813 par->pmi_setpal = pmi_setpal;
814 par->ypan = ypan;
816 if (par->pmi_setpal || par->ypan) {
817 if (__supported_pte_mask & _PAGE_NX) {
818 par->pmi_setpal = par->ypan = 0;
819 printk(KERN_WARNING "uvesafb: NX protection is active, "
820 "better not use the PMI.\n");
821 } else {
822 uvesafb_vbe_getpmi(task, par);
825 #else
826 /* The protected mode interface is not available on non-x86. */
827 par->pmi_setpal = par->ypan = 0;
828 #endif
830 INIT_LIST_HEAD(&info->modelist);
831 uvesafb_vbe_getmonspecs(task, info);
832 uvesafb_vbe_getstatesize(task, par);
834 out: uvesafb_free(task);
835 return err;
838 static int uvesafb_vbe_init_mode(struct fb_info *info)
840 struct list_head *pos;
841 struct fb_modelist *modelist;
842 struct fb_videomode *mode;
843 struct uvesafb_par *par = info->par;
844 int i, modeid;
846 /* Has the user requested a specific VESA mode? */
847 if (vbemode) {
848 for (i = 0; i < par->vbe_modes_cnt; i++) {
849 if (par->vbe_modes[i].mode_id == vbemode) {
850 modeid = i;
851 uvesafb_setup_var(&info->var, info,
852 &par->vbe_modes[modeid]);
853 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
854 &info->var, info);
856 * With pixclock set to 0, the default BIOS
857 * timings will be used in set_par().
859 info->var.pixclock = 0;
860 goto gotmode;
863 printk(KERN_INFO "uvesafb: requested VBE mode 0x%x is "
864 "unavailable\n", vbemode);
865 vbemode = 0;
868 /* Count the modes in the modelist */
869 i = 0;
870 list_for_each(pos, &info->modelist)
871 i++;
874 * Convert the modelist into a modedb so that we can use it with
875 * fb_find_mode().
877 mode = kzalloc(i * sizeof(*mode), GFP_KERNEL);
878 if (mode) {
879 i = 0;
880 list_for_each(pos, &info->modelist) {
881 modelist = list_entry(pos, struct fb_modelist, list);
882 mode[i] = modelist->mode;
883 i++;
886 if (!mode_option)
887 mode_option = UVESAFB_DEFAULT_MODE;
889 i = fb_find_mode(&info->var, info, mode_option, mode, i,
890 NULL, 8);
892 kfree(mode);
895 /* fb_find_mode() failed */
896 if (i == 0) {
897 info->var.xres = 640;
898 info->var.yres = 480;
899 mode = (struct fb_videomode *)
900 fb_find_best_mode(&info->var, &info->modelist);
902 if (mode) {
903 fb_videomode_to_var(&info->var, mode);
904 } else {
905 modeid = par->vbe_modes[0].mode_id;
906 uvesafb_setup_var(&info->var, info,
907 &par->vbe_modes[modeid]);
908 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
909 &info->var, info);
911 goto gotmode;
915 /* Look for a matching VBE mode. */
916 modeid = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres,
917 info->var.bits_per_pixel, UVESAFB_EXACT_RES);
919 if (modeid == -1)
920 return -EINVAL;
922 uvesafb_setup_var(&info->var, info, &par->vbe_modes[modeid]);
924 gotmode:
926 * If we are not VBE3.0+ compliant, we're done -- the BIOS will
927 * ignore our timings anyway.
929 if (par->vbe_ib.vbe_version < 0x0300 || par->nocrtc)
930 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
931 &info->var, info);
933 return modeid;
936 static int uvesafb_setpalette(struct uvesafb_pal_entry *entries, int count,
937 int start, struct fb_info *info)
939 struct uvesafb_ktask *task;
940 #ifdef CONFIG_X86
941 struct uvesafb_par *par = info->par;
942 int i = par->mode_idx;
943 #endif
944 int err = 0;
947 * We support palette modifications for 8 bpp modes only, so
948 * there can never be more than 256 entries.
950 if (start + count > 256)
951 return -EINVAL;
953 #ifdef CONFIG_X86
954 /* Use VGA registers if mode is VGA-compatible. */
955 if (i >= 0 && i < par->vbe_modes_cnt &&
956 par->vbe_modes[i].mode_attr & VBE_MODE_VGACOMPAT) {
957 for (i = 0; i < count; i++) {
958 outb_p(start + i, dac_reg);
959 outb_p(entries[i].red, dac_val);
960 outb_p(entries[i].green, dac_val);
961 outb_p(entries[i].blue, dac_val);
964 #ifdef CONFIG_X86_32
965 else if (par->pmi_setpal) {
966 __asm__ __volatile__(
967 "call *(%%esi)"
968 : /* no return value */
969 : "a" (0x4f09), /* EAX */
970 "b" (0), /* EBX */
971 "c" (count), /* ECX */
972 "d" (start), /* EDX */
973 "D" (entries), /* EDI */
974 "S" (&par->pmi_pal)); /* ESI */
976 #endif /* CONFIG_X86_32 */
977 else
978 #endif /* CONFIG_X86 */
980 task = uvesafb_prep();
981 if (!task)
982 return -ENOMEM;
984 task->t.regs.eax = 0x4f09;
985 task->t.regs.ebx = 0x0;
986 task->t.regs.ecx = count;
987 task->t.regs.edx = start;
988 task->t.flags = TF_BUF_ESDI;
989 task->t.buf_len = sizeof(struct uvesafb_pal_entry) * count;
990 task->buf = entries;
992 err = uvesafb_exec(task);
993 if ((task->t.regs.eax & 0xffff) != 0x004f)
994 err = 1;
996 uvesafb_free(task);
998 return err;
1001 static int uvesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
1002 unsigned blue, unsigned transp,
1003 struct fb_info *info)
1005 struct uvesafb_pal_entry entry;
1006 int shift = 16 - dac_width;
1007 int err = 0;
1009 if (regno >= info->cmap.len)
1010 return -EINVAL;
1012 if (info->var.bits_per_pixel == 8) {
1013 entry.red = red >> shift;
1014 entry.green = green >> shift;
1015 entry.blue = blue >> shift;
1016 entry.pad = 0;
1018 err = uvesafb_setpalette(&entry, 1, regno, info);
1019 } else if (regno < 16) {
1020 switch (info->var.bits_per_pixel) {
1021 case 16:
1022 if (info->var.red.offset == 10) {
1023 /* 1:5:5:5 */
1024 ((u32 *) (info->pseudo_palette))[regno] =
1025 ((red & 0xf800) >> 1) |
1026 ((green & 0xf800) >> 6) |
1027 ((blue & 0xf800) >> 11);
1028 } else {
1029 /* 0:5:6:5 */
1030 ((u32 *) (info->pseudo_palette))[regno] =
1031 ((red & 0xf800) ) |
1032 ((green & 0xfc00) >> 5) |
1033 ((blue & 0xf800) >> 11);
1035 break;
1037 case 24:
1038 case 32:
1039 red >>= 8;
1040 green >>= 8;
1041 blue >>= 8;
1042 ((u32 *)(info->pseudo_palette))[regno] =
1043 (red << info->var.red.offset) |
1044 (green << info->var.green.offset) |
1045 (blue << info->var.blue.offset);
1046 break;
1049 return err;
1052 static int uvesafb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1054 struct uvesafb_pal_entry *entries;
1055 int shift = 16 - dac_width;
1056 int i, err = 0;
1058 if (info->var.bits_per_pixel == 8) {
1059 if (cmap->start + cmap->len > info->cmap.start +
1060 info->cmap.len || cmap->start < info->cmap.start)
1061 return -EINVAL;
1063 entries = kmalloc(sizeof(*entries) * cmap->len, GFP_KERNEL);
1064 if (!entries)
1065 return -ENOMEM;
1067 for (i = 0; i < cmap->len; i++) {
1068 entries[i].red = cmap->red[i] >> shift;
1069 entries[i].green = cmap->green[i] >> shift;
1070 entries[i].blue = cmap->blue[i] >> shift;
1071 entries[i].pad = 0;
1073 err = uvesafb_setpalette(entries, cmap->len, cmap->start, info);
1074 kfree(entries);
1075 } else {
1077 * For modes with bpp > 8, we only set the pseudo palette in
1078 * the fb_info struct. We rely on uvesafb_setcolreg to do all
1079 * sanity checking.
1081 for (i = 0; i < cmap->len; i++) {
1082 err |= uvesafb_setcolreg(cmap->start + i, cmap->red[i],
1083 cmap->green[i], cmap->blue[i],
1084 0, info);
1087 return err;
1090 static int uvesafb_pan_display(struct fb_var_screeninfo *var,
1091 struct fb_info *info)
1093 #ifdef CONFIG_X86_32
1094 int offset;
1095 struct uvesafb_par *par = info->par;
1097 offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
1100 * It turns out it's not the best idea to do panning via vm86,
1101 * so we only allow it if we have a PMI.
1103 if (par->pmi_start) {
1104 __asm__ __volatile__(
1105 "call *(%%edi)"
1106 : /* no return value */
1107 : "a" (0x4f07), /* EAX */
1108 "b" (0), /* EBX */
1109 "c" (offset), /* ECX */
1110 "d" (offset >> 16), /* EDX */
1111 "D" (&par->pmi_start)); /* EDI */
1113 #endif
1114 return 0;
1117 static int uvesafb_blank(int blank, struct fb_info *info)
1119 struct uvesafb_ktask *task;
1120 int err = 1;
1121 #ifdef CONFIG_X86
1122 struct uvesafb_par *par = info->par;
1124 if (par->vbe_ib.capabilities & VBE_CAP_VGACOMPAT) {
1125 int loop = 10000;
1126 u8 seq = 0, crtc17 = 0;
1128 if (blank == FB_BLANK_POWERDOWN) {
1129 seq = 0x20;
1130 crtc17 = 0x00;
1131 err = 0;
1132 } else {
1133 seq = 0x00;
1134 crtc17 = 0x80;
1135 err = (blank == FB_BLANK_UNBLANK) ? 0 : -EINVAL;
1138 vga_wseq(NULL, 0x00, 0x01);
1139 seq |= vga_rseq(NULL, 0x01) & ~0x20;
1140 vga_wseq(NULL, 0x00, seq);
1142 crtc17 |= vga_rcrt(NULL, 0x17) & ~0x80;
1143 while (loop--);
1144 vga_wcrt(NULL, 0x17, crtc17);
1145 vga_wseq(NULL, 0x00, 0x03);
1146 } else
1147 #endif /* CONFIG_X86 */
1149 task = uvesafb_prep();
1150 if (!task)
1151 return -ENOMEM;
1153 task->t.regs.eax = 0x4f10;
1154 switch (blank) {
1155 case FB_BLANK_UNBLANK:
1156 task->t.regs.ebx = 0x0001;
1157 break;
1158 case FB_BLANK_NORMAL:
1159 task->t.regs.ebx = 0x0101; /* standby */
1160 break;
1161 case FB_BLANK_POWERDOWN:
1162 task->t.regs.ebx = 0x0401; /* powerdown */
1163 break;
1164 default:
1165 goto out;
1168 err = uvesafb_exec(task);
1169 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
1170 err = 1;
1171 out: uvesafb_free(task);
1173 return err;
1176 static int uvesafb_open(struct fb_info *info, int user)
1178 struct uvesafb_par *par = info->par;
1179 int cnt = atomic_read(&par->ref_count);
1180 u8 *buf = NULL;
1182 if (!cnt && par->vbe_state_size) {
1183 buf = uvesafb_vbe_state_save(par);
1184 if (IS_ERR(buf)) {
1185 printk(KERN_WARNING "uvesafb: save hardware state"
1186 "failed, error code is %ld!\n", PTR_ERR(buf));
1187 } else {
1188 par->vbe_state_orig = buf;
1192 atomic_inc(&par->ref_count);
1193 return 0;
1196 static int uvesafb_release(struct fb_info *info, int user)
1198 struct uvesafb_ktask *task = NULL;
1199 struct uvesafb_par *par = info->par;
1200 int cnt = atomic_read(&par->ref_count);
1202 if (!cnt)
1203 return -EINVAL;
1205 if (cnt != 1)
1206 goto out;
1208 task = uvesafb_prep();
1209 if (!task)
1210 goto out;
1212 /* First, try to set the standard 80x25 text mode. */
1213 task->t.regs.eax = 0x0003;
1214 uvesafb_exec(task);
1217 * Now try to restore whatever hardware state we might have
1218 * saved when the fb device was first opened.
1220 uvesafb_vbe_state_restore(par, par->vbe_state_orig);
1221 out:
1222 atomic_dec(&par->ref_count);
1223 if (task)
1224 uvesafb_free(task);
1225 return 0;
1228 static int uvesafb_set_par(struct fb_info *info)
1230 struct uvesafb_par *par = info->par;
1231 struct uvesafb_ktask *task = NULL;
1232 struct vbe_crtc_ib *crtc = NULL;
1233 struct vbe_mode_ib *mode = NULL;
1234 int i, err = 0, depth = info->var.bits_per_pixel;
1236 if (depth > 8 && depth != 32)
1237 depth = info->var.red.length + info->var.green.length +
1238 info->var.blue.length;
1240 i = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres, depth,
1241 UVESAFB_EXACT_RES | UVESAFB_EXACT_DEPTH);
1242 if (i >= 0)
1243 mode = &par->vbe_modes[i];
1244 else
1245 return -EINVAL;
1247 task = uvesafb_prep();
1248 if (!task)
1249 return -ENOMEM;
1250 setmode:
1251 task->t.regs.eax = 0x4f02;
1252 task->t.regs.ebx = mode->mode_id | 0x4000; /* use LFB */
1254 if (par->vbe_ib.vbe_version >= 0x0300 && !par->nocrtc &&
1255 info->var.pixclock != 0) {
1256 task->t.regs.ebx |= 0x0800; /* use CRTC data */
1257 task->t.flags = TF_BUF_ESDI;
1258 crtc = kzalloc(sizeof(struct vbe_crtc_ib), GFP_KERNEL);
1259 if (!crtc) {
1260 err = -ENOMEM;
1261 goto out;
1263 crtc->horiz_start = info->var.xres + info->var.right_margin;
1264 crtc->horiz_end = crtc->horiz_start + info->var.hsync_len;
1265 crtc->horiz_total = crtc->horiz_end + info->var.left_margin;
1267 crtc->vert_start = info->var.yres + info->var.lower_margin;
1268 crtc->vert_end = crtc->vert_start + info->var.vsync_len;
1269 crtc->vert_total = crtc->vert_end + info->var.upper_margin;
1271 crtc->pixel_clock = PICOS2KHZ(info->var.pixclock) * 1000;
1272 crtc->refresh_rate = (u16)(100 * (crtc->pixel_clock /
1273 (crtc->vert_total * crtc->horiz_total)));
1275 if (info->var.vmode & FB_VMODE_DOUBLE)
1276 crtc->flags |= 0x1;
1277 if (info->var.vmode & FB_VMODE_INTERLACED)
1278 crtc->flags |= 0x2;
1279 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
1280 crtc->flags |= 0x4;
1281 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
1282 crtc->flags |= 0x8;
1283 memcpy(&par->crtc, crtc, sizeof(*crtc));
1284 } else {
1285 memset(&par->crtc, 0, sizeof(*crtc));
1288 task->t.buf_len = sizeof(struct vbe_crtc_ib);
1289 task->buf = &par->crtc;
1291 err = uvesafb_exec(task);
1292 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
1294 * The mode switch might have failed because we tried to
1295 * use our own timings. Try again with the default timings.
1297 if (crtc != NULL) {
1298 printk(KERN_WARNING "uvesafb: mode switch failed "
1299 "(eax=0x%x, err=%d). Trying again with "
1300 "default timings.\n", task->t.regs.eax, err);
1301 uvesafb_reset(task);
1302 kfree(crtc);
1303 crtc = NULL;
1304 info->var.pixclock = 0;
1305 goto setmode;
1306 } else {
1307 printk(KERN_ERR "uvesafb: mode switch failed (eax="
1308 "0x%x, err=%d)\n", task->t.regs.eax, err);
1309 err = -EINVAL;
1310 goto out;
1313 par->mode_idx = i;
1315 /* For 8bpp modes, always try to set the DAC to 8 bits. */
1316 if (par->vbe_ib.capabilities & VBE_CAP_CAN_SWITCH_DAC &&
1317 mode->bits_per_pixel <= 8) {
1318 uvesafb_reset(task);
1319 task->t.regs.eax = 0x4f08;
1320 task->t.regs.ebx = 0x0800;
1322 err = uvesafb_exec(task);
1323 if (err || (task->t.regs.eax & 0xffff) != 0x004f ||
1324 ((task->t.regs.ebx & 0xff00) >> 8) != 8) {
1325 dac_width = 6;
1326 } else {
1327 dac_width = 8;
1331 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1332 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1333 info->fix.line_length = mode->bytes_per_scan_line;
1335 out: if (crtc != NULL)
1336 kfree(crtc);
1337 uvesafb_free(task);
1339 return err;
1342 static void uvesafb_check_limits(struct fb_var_screeninfo *var,
1343 struct fb_info *info)
1345 const struct fb_videomode *mode;
1346 struct uvesafb_par *par = info->par;
1349 * If pixclock is set to 0, then we're using default BIOS timings
1350 * and thus don't have to perform any checks here.
1352 if (!var->pixclock)
1353 return;
1355 if (par->vbe_ib.vbe_version < 0x0300) {
1356 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, var, info);
1357 return;
1360 if (!fb_validate_mode(var, info))
1361 return;
1363 mode = fb_find_best_mode(var, &info->modelist);
1364 if (mode) {
1365 if (mode->xres == var->xres && mode->yres == var->yres &&
1366 !(mode->vmode & (FB_VMODE_INTERLACED | FB_VMODE_DOUBLE))) {
1367 fb_videomode_to_var(var, mode);
1368 return;
1372 if (info->monspecs.gtf && !fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1373 return;
1374 /* Use default refresh rate */
1375 var->pixclock = 0;
1378 static int uvesafb_check_var(struct fb_var_screeninfo *var,
1379 struct fb_info *info)
1381 struct uvesafb_par *par = info->par;
1382 struct vbe_mode_ib *mode = NULL;
1383 int match = -1;
1384 int depth = var->red.length + var->green.length + var->blue.length;
1387 * Various apps will use bits_per_pixel to set the color depth,
1388 * which is theoretically incorrect, but which we'll try to handle
1389 * here.
1391 if (depth == 0 || abs(depth - var->bits_per_pixel) >= 8)
1392 depth = var->bits_per_pixel;
1394 match = uvesafb_vbe_find_mode(par, var->xres, var->yres, depth,
1395 UVESAFB_EXACT_RES);
1396 if (match == -1)
1397 return -EINVAL;
1399 mode = &par->vbe_modes[match];
1400 uvesafb_setup_var(var, info, mode);
1403 * Check whether we have remapped enough memory for this mode.
1404 * We might be called at an early stage, when we haven't remapped
1405 * any memory yet, in which case we simply skip the check.
1407 if (var->yres * mode->bytes_per_scan_line > info->fix.smem_len
1408 && info->fix.smem_len)
1409 return -EINVAL;
1411 if ((var->vmode & FB_VMODE_DOUBLE) &&
1412 !(par->vbe_modes[match].mode_attr & 0x100))
1413 var->vmode &= ~FB_VMODE_DOUBLE;
1415 if ((var->vmode & FB_VMODE_INTERLACED) &&
1416 !(par->vbe_modes[match].mode_attr & 0x200))
1417 var->vmode &= ~FB_VMODE_INTERLACED;
1419 uvesafb_check_limits(var, info);
1421 var->xres_virtual = var->xres;
1422 var->yres_virtual = (par->ypan) ?
1423 info->fix.smem_len / mode->bytes_per_scan_line :
1424 var->yres;
1425 return 0;
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,
1443 static void uvesafb_init_info(struct fb_info *info, struct vbe_mode_ib *mode)
1445 unsigned int size_vmode;
1446 unsigned int size_remap;
1447 unsigned int size_total;
1448 struct uvesafb_par *par = info->par;
1449 int i, h;
1451 info->pseudo_palette = ((u8 *)info->par + sizeof(struct uvesafb_par));
1452 info->fix = uvesafb_fix;
1453 info->fix.ypanstep = par->ypan ? 1 : 0;
1454 info->fix.ywrapstep = (par->ypan > 1) ? 1 : 0;
1456 /* Disable blanking if the user requested so. */
1457 if (!blank)
1458 info->fbops->fb_blank = NULL;
1461 * Find out how much IO memory is required for the mode with
1462 * the highest resolution.
1464 size_remap = 0;
1465 for (i = 0; i < par->vbe_modes_cnt; i++) {
1466 h = par->vbe_modes[i].bytes_per_scan_line *
1467 par->vbe_modes[i].y_res;
1468 if (h > size_remap)
1469 size_remap = h;
1471 size_remap *= 2;
1474 * size_vmode -- that is the amount of memory needed for the
1475 * used video mode, i.e. the minimum amount of
1476 * memory we need.
1478 if (mode != NULL) {
1479 size_vmode = info->var.yres * mode->bytes_per_scan_line;
1480 } else {
1481 size_vmode = info->var.yres * info->var.xres *
1482 ((info->var.bits_per_pixel + 7) >> 3);
1486 * size_total -- all video memory we have. Used for mtrr
1487 * entries, resource allocation and bounds
1488 * checking.
1490 size_total = par->vbe_ib.total_memory * 65536;
1491 if (vram_total)
1492 size_total = vram_total * 1024 * 1024;
1493 if (size_total < size_vmode)
1494 size_total = size_vmode;
1497 * size_remap -- the amount of video memory we are going to
1498 * use for vesafb. With modern cards it is no
1499 * option to simply use size_total as th
1500 * wastes plenty of kernel address space.
1502 if (vram_remap)
1503 size_remap = vram_remap * 1024 * 1024;
1504 if (size_remap < size_vmode)
1505 size_remap = size_vmode;
1506 if (size_remap > size_total)
1507 size_remap = size_total;
1509 info->fix.smem_len = size_remap;
1510 info->fix.smem_start = mode->phys_base_ptr;
1513 * We have to set yres_virtual here because when setup_var() was
1514 * called, smem_len wasn't defined yet.
1516 info->var.yres_virtual = info->fix.smem_len /
1517 mode->bytes_per_scan_line;
1519 if (par->ypan && info->var.yres_virtual > info->var.yres) {
1520 printk(KERN_INFO "uvesafb: scrolling: %s "
1521 "using protected mode interface, "
1522 "yres_virtual=%d\n",
1523 (par->ypan > 1) ? "ywrap" : "ypan",
1524 info->var.yres_virtual);
1525 } else {
1526 printk(KERN_INFO "uvesafb: scrolling: redraw\n");
1527 info->var.yres_virtual = info->var.yres;
1528 par->ypan = 0;
1531 info->flags = FBINFO_FLAG_DEFAULT |
1532 (par->ypan ? FBINFO_HWACCEL_YPAN : 0);
1534 if (!par->ypan)
1535 info->fbops->fb_pan_display = NULL;
1538 static void uvesafb_init_mtrr(struct fb_info *info)
1540 struct uvesafb_par *par = info->par;
1542 if (mtrr && !(info->fix.smem_start & (PAGE_SIZE - 1))) {
1543 int temp_size = info->fix.smem_len;
1545 int rc;
1547 /* Find the largest power-of-two */
1548 temp_size = roundup_pow_of_two(temp_size);
1550 /* Try and find a power of two to add */
1551 do {
1552 rc = arch_phys_wc_add(info->fix.smem_start, temp_size);
1553 temp_size >>= 1;
1554 } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
1556 if (rc >= 0)
1557 par->mtrr_handle = rc;
1561 static void uvesafb_ioremap(struct fb_info *info)
1563 info->screen_base = ioremap_wc(info->fix.smem_start, info->fix.smem_len);
1566 static ssize_t uvesafb_show_vbe_ver(struct device *dev,
1567 struct device_attribute *attr, char *buf)
1569 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1570 struct uvesafb_par *par = info->par;
1572 return snprintf(buf, PAGE_SIZE, "%.4x\n", par->vbe_ib.vbe_version);
1575 static DEVICE_ATTR(vbe_version, S_IRUGO, uvesafb_show_vbe_ver, NULL);
1577 static ssize_t uvesafb_show_vbe_modes(struct device *dev,
1578 struct device_attribute *attr, char *buf)
1580 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1581 struct uvesafb_par *par = info->par;
1582 int ret = 0, i;
1584 for (i = 0; i < par->vbe_modes_cnt && ret < PAGE_SIZE; i++) {
1585 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1586 "%dx%d-%d, 0x%.4x\n",
1587 par->vbe_modes[i].x_res, par->vbe_modes[i].y_res,
1588 par->vbe_modes[i].depth, par->vbe_modes[i].mode_id);
1591 return ret;
1594 static DEVICE_ATTR(vbe_modes, S_IRUGO, uvesafb_show_vbe_modes, NULL);
1596 static ssize_t uvesafb_show_vendor(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 if (par->vbe_ib.oem_vendor_name_ptr)
1603 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1604 (&par->vbe_ib) + par->vbe_ib.oem_vendor_name_ptr);
1605 else
1606 return 0;
1609 static DEVICE_ATTR(oem_vendor, S_IRUGO, uvesafb_show_vendor, NULL);
1611 static ssize_t uvesafb_show_product_name(struct device *dev,
1612 struct device_attribute *attr, char *buf)
1614 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1615 struct uvesafb_par *par = info->par;
1617 if (par->vbe_ib.oem_product_name_ptr)
1618 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1619 (&par->vbe_ib) + par->vbe_ib.oem_product_name_ptr);
1620 else
1621 return 0;
1624 static DEVICE_ATTR(oem_product_name, S_IRUGO, uvesafb_show_product_name, NULL);
1626 static ssize_t uvesafb_show_product_rev(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_product_rev_ptr)
1633 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1634 (&par->vbe_ib) + par->vbe_ib.oem_product_rev_ptr);
1635 else
1636 return 0;
1639 static DEVICE_ATTR(oem_product_rev, S_IRUGO, uvesafb_show_product_rev, NULL);
1641 static ssize_t uvesafb_show_oem_string(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_string_ptr)
1648 return snprintf(buf, PAGE_SIZE, "%s\n",
1649 (char *)(&par->vbe_ib) + par->vbe_ib.oem_string_ptr);
1650 else
1651 return 0;
1654 static DEVICE_ATTR(oem_string, S_IRUGO, uvesafb_show_oem_string, NULL);
1656 static ssize_t uvesafb_show_nocrtc(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 return snprintf(buf, PAGE_SIZE, "%d\n", par->nocrtc);
1665 static ssize_t uvesafb_store_nocrtc(struct device *dev,
1666 struct device_attribute *attr, const char *buf, size_t count)
1668 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1669 struct uvesafb_par *par = info->par;
1671 if (count > 0) {
1672 if (buf[0] == '0')
1673 par->nocrtc = 0;
1674 else
1675 par->nocrtc = 1;
1677 return count;
1680 static DEVICE_ATTR(nocrtc, S_IRUGO | S_IWUSR, uvesafb_show_nocrtc,
1681 uvesafb_store_nocrtc);
1683 static struct attribute *uvesafb_dev_attrs[] = {
1684 &dev_attr_vbe_version.attr,
1685 &dev_attr_vbe_modes.attr,
1686 &dev_attr_oem_vendor.attr,
1687 &dev_attr_oem_product_name.attr,
1688 &dev_attr_oem_product_rev.attr,
1689 &dev_attr_oem_string.attr,
1690 &dev_attr_nocrtc.attr,
1691 NULL,
1694 static struct attribute_group uvesafb_dev_attgrp = {
1695 .name = NULL,
1696 .attrs = uvesafb_dev_attrs,
1699 static int uvesafb_probe(struct platform_device *dev)
1701 struct fb_info *info;
1702 struct vbe_mode_ib *mode = NULL;
1703 struct uvesafb_par *par;
1704 int err = 0, i;
1706 info = framebuffer_alloc(sizeof(*par) + sizeof(u32) * 256, &dev->dev);
1707 if (!info)
1708 return -ENOMEM;
1710 par = info->par;
1712 err = uvesafb_vbe_init(info);
1713 if (err) {
1714 printk(KERN_ERR "uvesafb: vbe_init() failed with %d\n", err);
1715 goto out;
1718 info->fbops = &uvesafb_ops;
1720 i = uvesafb_vbe_init_mode(info);
1721 if (i < 0) {
1722 err = -EINVAL;
1723 goto out;
1724 } else {
1725 mode = &par->vbe_modes[i];
1728 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1729 err = -ENXIO;
1730 goto out;
1733 uvesafb_init_info(info, mode);
1735 if (!request_region(0x3c0, 32, "uvesafb")) {
1736 printk(KERN_ERR "uvesafb: request region 0x3c0-0x3e0 failed\n");
1737 err = -EIO;
1738 goto out_mode;
1741 if (!request_mem_region(info->fix.smem_start, info->fix.smem_len,
1742 "uvesafb")) {
1743 printk(KERN_ERR "uvesafb: cannot reserve video memory at "
1744 "0x%lx\n", info->fix.smem_start);
1745 err = -EIO;
1746 goto out_reg;
1749 uvesafb_init_mtrr(info);
1750 uvesafb_ioremap(info);
1752 if (!info->screen_base) {
1753 printk(KERN_ERR
1754 "uvesafb: abort, cannot ioremap 0x%x bytes of video "
1755 "memory at 0x%lx\n",
1756 info->fix.smem_len, info->fix.smem_start);
1757 err = -EIO;
1758 goto out_mem;
1761 platform_set_drvdata(dev, info);
1763 if (register_framebuffer(info) < 0) {
1764 printk(KERN_ERR
1765 "uvesafb: failed to register framebuffer device\n");
1766 err = -EINVAL;
1767 goto out_unmap;
1770 printk(KERN_INFO "uvesafb: framebuffer at 0x%lx, mapped to 0x%p, "
1771 "using %dk, total %dk\n", info->fix.smem_start,
1772 info->screen_base, info->fix.smem_len/1024,
1773 par->vbe_ib.total_memory * 64);
1774 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
1775 info->fix.id);
1777 err = sysfs_create_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1778 if (err != 0)
1779 printk(KERN_WARNING "fb%d: failed to register attributes\n",
1780 info->node);
1782 return 0;
1784 out_unmap:
1785 iounmap(info->screen_base);
1786 out_mem:
1787 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1788 out_reg:
1789 release_region(0x3c0, 32);
1790 out_mode:
1791 if (!list_empty(&info->modelist))
1792 fb_destroy_modelist(&info->modelist);
1793 fb_destroy_modedb(info->monspecs.modedb);
1794 fb_dealloc_cmap(&info->cmap);
1795 out:
1796 if (par->vbe_modes)
1797 kfree(par->vbe_modes);
1799 framebuffer_release(info);
1800 return err;
1803 static int uvesafb_remove(struct platform_device *dev)
1805 struct fb_info *info = platform_get_drvdata(dev);
1807 if (info) {
1808 struct uvesafb_par *par = info->par;
1810 sysfs_remove_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1811 unregister_framebuffer(info);
1812 release_region(0x3c0, 32);
1813 iounmap(info->screen_base);
1814 arch_phys_wc_del(par->mtrr_handle);
1815 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1816 fb_destroy_modedb(info->monspecs.modedb);
1817 fb_dealloc_cmap(&info->cmap);
1819 if (par) {
1820 if (par->vbe_modes)
1821 kfree(par->vbe_modes);
1822 if (par->vbe_state_orig)
1823 kfree(par->vbe_state_orig);
1824 if (par->vbe_state_saved)
1825 kfree(par->vbe_state_saved);
1828 framebuffer_release(info);
1830 return 0;
1833 static struct platform_driver uvesafb_driver = {
1834 .probe = uvesafb_probe,
1835 .remove = uvesafb_remove,
1836 .driver = {
1837 .name = "uvesafb",
1841 static struct platform_device *uvesafb_device;
1843 #ifndef MODULE
1844 static int uvesafb_setup(char *options)
1846 char *this_opt;
1848 if (!options || !*options)
1849 return 0;
1851 while ((this_opt = strsep(&options, ",")) != NULL) {
1852 if (!*this_opt) continue;
1854 if (!strcmp(this_opt, "redraw"))
1855 ypan = 0;
1856 else if (!strcmp(this_opt, "ypan"))
1857 ypan = 1;
1858 else if (!strcmp(this_opt, "ywrap"))
1859 ypan = 2;
1860 else if (!strcmp(this_opt, "vgapal"))
1861 pmi_setpal = 0;
1862 else if (!strcmp(this_opt, "pmipal"))
1863 pmi_setpal = 1;
1864 else if (!strncmp(this_opt, "mtrr:", 5))
1865 mtrr = simple_strtoul(this_opt+5, NULL, 0);
1866 else if (!strcmp(this_opt, "nomtrr"))
1867 mtrr = 0;
1868 else if (!strcmp(this_opt, "nocrtc"))
1869 nocrtc = 1;
1870 else if (!strcmp(this_opt, "noedid"))
1871 noedid = 1;
1872 else if (!strcmp(this_opt, "noblank"))
1873 blank = 0;
1874 else if (!strncmp(this_opt, "vtotal:", 7))
1875 vram_total = simple_strtoul(this_opt + 7, NULL, 0);
1876 else if (!strncmp(this_opt, "vremap:", 7))
1877 vram_remap = simple_strtoul(this_opt + 7, NULL, 0);
1878 else if (!strncmp(this_opt, "maxhf:", 6))
1879 maxhf = simple_strtoul(this_opt + 6, NULL, 0);
1880 else if (!strncmp(this_opt, "maxvf:", 6))
1881 maxvf = simple_strtoul(this_opt + 6, NULL, 0);
1882 else if (!strncmp(this_opt, "maxclk:", 7))
1883 maxclk = simple_strtoul(this_opt + 7, NULL, 0);
1884 else if (!strncmp(this_opt, "vbemode:", 8))
1885 vbemode = simple_strtoul(this_opt + 8, NULL, 0);
1886 else if (this_opt[0] >= '0' && this_opt[0] <= '9') {
1887 mode_option = this_opt;
1888 } else {
1889 printk(KERN_WARNING
1890 "uvesafb: unrecognized option %s\n", this_opt);
1894 if (mtrr != 3 && mtrr != 0)
1895 pr_warn("uvesafb: mtrr should be set to 0 or 3; %d is unsupported", mtrr);
1897 return 0;
1899 #endif /* !MODULE */
1901 static ssize_t show_v86d(struct device_driver *dev, char *buf)
1903 return snprintf(buf, PAGE_SIZE, "%s\n", v86d_path);
1906 static ssize_t store_v86d(struct device_driver *dev, const char *buf,
1907 size_t count)
1909 strncpy(v86d_path, buf, PATH_MAX);
1910 return count;
1913 static DRIVER_ATTR(v86d, S_IRUGO | S_IWUSR, show_v86d, store_v86d);
1915 static int uvesafb_init(void)
1917 int err;
1919 #ifndef MODULE
1920 char *option = NULL;
1922 if (fb_get_options("uvesafb", &option))
1923 return -ENODEV;
1924 uvesafb_setup(option);
1925 #endif
1926 err = cn_add_callback(&uvesafb_cn_id, "uvesafb", uvesafb_cn_callback);
1927 if (err)
1928 return err;
1930 err = platform_driver_register(&uvesafb_driver);
1932 if (!err) {
1933 uvesafb_device = platform_device_alloc("uvesafb", 0);
1934 if (uvesafb_device)
1935 err = platform_device_add(uvesafb_device);
1936 else
1937 err = -ENOMEM;
1939 if (err) {
1940 if (uvesafb_device)
1941 platform_device_put(uvesafb_device);
1942 platform_driver_unregister(&uvesafb_driver);
1943 cn_del_callback(&uvesafb_cn_id);
1944 return err;
1947 err = driver_create_file(&uvesafb_driver.driver,
1948 &driver_attr_v86d);
1949 if (err) {
1950 printk(KERN_WARNING "uvesafb: failed to register "
1951 "attributes\n");
1952 err = 0;
1955 return err;
1958 module_init(uvesafb_init);
1960 static void uvesafb_exit(void)
1962 struct uvesafb_ktask *task;
1964 if (v86d_started) {
1965 task = uvesafb_prep();
1966 if (task) {
1967 task->t.flags = TF_EXIT;
1968 uvesafb_exec(task);
1969 uvesafb_free(task);
1973 cn_del_callback(&uvesafb_cn_id);
1974 driver_remove_file(&uvesafb_driver.driver, &driver_attr_v86d);
1975 platform_device_unregister(uvesafb_device);
1976 platform_driver_unregister(&uvesafb_driver);
1979 module_exit(uvesafb_exit);
1981 static int param_set_scroll(const char *val, const struct kernel_param *kp)
1983 ypan = 0;
1985 if (!strcmp(val, "redraw"))
1986 ypan = 0;
1987 else if (!strcmp(val, "ypan"))
1988 ypan = 1;
1989 else if (!strcmp(val, "ywrap"))
1990 ypan = 2;
1991 else
1992 return -EINVAL;
1994 return 0;
1996 static struct kernel_param_ops param_ops_scroll = {
1997 .set = param_set_scroll,
1999 #define param_check_scroll(name, p) __param_check(name, p, void)
2001 module_param_named(scroll, ypan, scroll, 0);
2002 MODULE_PARM_DESC(scroll,
2003 "Scrolling mode, set to 'redraw', 'ypan', or 'ywrap'");
2004 module_param_named(vgapal, pmi_setpal, invbool, 0);
2005 MODULE_PARM_DESC(vgapal, "Set palette using VGA registers");
2006 module_param_named(pmipal, pmi_setpal, bool, 0);
2007 MODULE_PARM_DESC(pmipal, "Set palette using PMI calls");
2008 module_param(mtrr, uint, 0);
2009 MODULE_PARM_DESC(mtrr,
2010 "Memory Type Range Registers setting. Use 0 to disable.");
2011 module_param(blank, bool, 0);
2012 MODULE_PARM_DESC(blank, "Enable hardware blanking");
2013 module_param(nocrtc, bool, 0);
2014 MODULE_PARM_DESC(nocrtc, "Ignore CRTC timings when setting modes");
2015 module_param(noedid, bool, 0);
2016 MODULE_PARM_DESC(noedid,
2017 "Ignore EDID-provided monitor limits when setting modes");
2018 module_param(vram_remap, uint, 0);
2019 MODULE_PARM_DESC(vram_remap, "Set amount of video memory to be used [MiB]");
2020 module_param(vram_total, uint, 0);
2021 MODULE_PARM_DESC(vram_total, "Set total amount of video memoery [MiB]");
2022 module_param(maxclk, ushort, 0);
2023 MODULE_PARM_DESC(maxclk, "Maximum pixelclock [MHz], overrides EDID data");
2024 module_param(maxhf, ushort, 0);
2025 MODULE_PARM_DESC(maxhf,
2026 "Maximum horizontal frequency [kHz], overrides EDID data");
2027 module_param(maxvf, ushort, 0);
2028 MODULE_PARM_DESC(maxvf,
2029 "Maximum vertical frequency [Hz], overrides EDID data");
2030 module_param(mode_option, charp, 0);
2031 MODULE_PARM_DESC(mode_option,
2032 "Specify initial video mode as \"<xres>x<yres>[-<bpp>][@<refresh>]\"");
2033 module_param(vbemode, ushort, 0);
2034 MODULE_PARM_DESC(vbemode,
2035 "VBE mode number to set, overrides the 'mode' option");
2036 module_param_string(v86d, v86d_path, PATH_MAX, 0660);
2037 MODULE_PARM_DESC(v86d, "Path to the v86d userspace helper.");
2039 MODULE_LICENSE("GPL");
2040 MODULE_AUTHOR("Michal Januszewski <spock@gentoo.org>");
2041 MODULE_DESCRIPTION("Framebuffer driver for VBE2.0+ compliant graphics boards");