Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / video / uvesafb.c
blob8408543e4e768fec791d555a59fcefba1daa06a0
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 #ifdef CONFIG_MTRR
28 #include <asm/mtrr.h>
29 #endif
30 #include "edid.h"
32 static struct cb_id uvesafb_cn_id = {
33 .idx = CN_IDX_V86D,
34 .val = CN_VAL_V86D_UVESAFB
36 static char v86d_path[PATH_MAX] = "/sbin/v86d";
37 static char v86d_started; /* has v86d been started by uvesafb? */
39 static struct fb_fix_screeninfo uvesafb_fix __devinitdata = {
40 .id = "VESA VGA",
41 .type = FB_TYPE_PACKED_PIXELS,
42 .accel = FB_ACCEL_NONE,
43 .visual = FB_VISUAL_TRUECOLOR,
46 static int mtrr __devinitdata = 3; /* enable mtrr by default */
47 static bool blank = 1; /* enable blanking by default */
48 static int ypan = 1; /* 0: scroll, 1: ypan, 2: ywrap */
49 static bool pmi_setpal __devinitdata = true; /* use PMI for palette changes */
50 static bool nocrtc __devinitdata; /* ignore CRTC settings */
51 static bool noedid __devinitdata; /* don't try DDC transfers */
52 static int vram_remap __devinitdata; /* set amt. of memory to be used */
53 static int vram_total __devinitdata; /* set total amount of memory */
54 static u16 maxclk __devinitdata; /* maximum pixel clock */
55 static u16 maxvf __devinitdata; /* maximum vertical frequency */
56 static u16 maxhf __devinitdata; /* maximum horizontal frequency */
57 static u16 vbemode __devinitdata; /* force use of a specific VBE mode */
58 static char *mode_option __devinitdata;
59 static u8 dac_width = 6;
61 static struct uvesafb_ktask *uvfb_tasks[UVESAFB_TASKS_MAX];
62 static DEFINE_MUTEX(uvfb_lock);
65 * A handler for replies from userspace.
67 * Make sure each message passes consistency checks and if it does,
68 * find the kernel part of the task struct, copy the registers and
69 * the buffer contents and then complete the task.
71 static void uvesafb_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
73 struct uvesafb_task *utask;
74 struct uvesafb_ktask *task;
76 if (!cap_raised(current_cap(), CAP_SYS_ADMIN))
77 return;
79 if (msg->seq >= UVESAFB_TASKS_MAX)
80 return;
82 mutex_lock(&uvfb_lock);
83 task = uvfb_tasks[msg->seq];
85 if (!task || msg->ack != task->ack) {
86 mutex_unlock(&uvfb_lock);
87 return;
90 utask = (struct uvesafb_task *)msg->data;
92 /* Sanity checks for the buffer length. */
93 if (task->t.buf_len < utask->buf_len ||
94 utask->buf_len > msg->len - sizeof(*utask)) {
95 mutex_unlock(&uvfb_lock);
96 return;
99 uvfb_tasks[msg->seq] = NULL;
100 mutex_unlock(&uvfb_lock);
102 memcpy(&task->t, utask, sizeof(*utask));
104 if (task->t.buf_len && task->buf)
105 memcpy(task->buf, utask + 1, task->t.buf_len);
107 complete(task->done);
108 return;
111 static int uvesafb_helper_start(void)
113 char *envp[] = {
114 "HOME=/",
115 "PATH=/sbin:/bin",
116 NULL,
119 char *argv[] = {
120 v86d_path,
121 NULL,
124 return call_usermodehelper(v86d_path, argv, envp, 1);
128 * Execute a uvesafb task.
130 * Returns 0 if the task is executed successfully.
132 * A message sent to the userspace consists of the uvesafb_task
133 * struct and (optionally) a buffer. The uvesafb_task struct is
134 * a simplified version of uvesafb_ktask (its kernel counterpart)
135 * containing only the register values, flags and the length of
136 * the buffer.
138 * Each message is assigned a sequence number (increased linearly)
139 * and a random ack number. The sequence number is used as a key
140 * for the uvfb_tasks array which holds pointers to uvesafb_ktask
141 * structs for all requests.
143 static int uvesafb_exec(struct uvesafb_ktask *task)
145 static int seq;
146 struct cn_msg *m;
147 int err;
148 int len = sizeof(task->t) + task->t.buf_len;
151 * Check whether the message isn't longer than the maximum
152 * allowed by connector.
154 if (sizeof(*m) + len > CONNECTOR_MAX_MSG_SIZE) {
155 printk(KERN_WARNING "uvesafb: message too long (%d), "
156 "can't execute task\n", (int)(sizeof(*m) + len));
157 return -E2BIG;
160 m = kzalloc(sizeof(*m) + len, GFP_KERNEL);
161 if (!m)
162 return -ENOMEM;
164 init_completion(task->done);
166 memcpy(&m->id, &uvesafb_cn_id, sizeof(m->id));
167 m->seq = seq;
168 m->len = len;
169 m->ack = random32();
171 /* uvesafb_task structure */
172 memcpy(m + 1, &task->t, sizeof(task->t));
174 /* Buffer */
175 memcpy((u8 *)(m + 1) + sizeof(task->t), task->buf, task->t.buf_len);
178 * Save the message ack number so that we can find the kernel
179 * part of this task when a reply is received from userspace.
181 task->ack = m->ack;
183 mutex_lock(&uvfb_lock);
185 /* If all slots are taken -- bail out. */
186 if (uvfb_tasks[seq]) {
187 mutex_unlock(&uvfb_lock);
188 err = -EBUSY;
189 goto out;
192 /* Save a pointer to the kernel part of the task struct. */
193 uvfb_tasks[seq] = task;
194 mutex_unlock(&uvfb_lock);
196 err = cn_netlink_send(m, 0, GFP_KERNEL);
197 if (err == -ESRCH) {
199 * Try to start the userspace helper if sending
200 * the request failed the first time.
202 err = uvesafb_helper_start();
203 if (err) {
204 printk(KERN_ERR "uvesafb: failed to execute %s\n",
205 v86d_path);
206 printk(KERN_ERR "uvesafb: make sure that the v86d "
207 "helper is installed and executable\n");
208 } else {
209 v86d_started = 1;
210 err = cn_netlink_send(m, 0, gfp_any());
211 if (err == -ENOBUFS)
212 err = 0;
214 } else if (err == -ENOBUFS)
215 err = 0;
217 if (!err && !(task->t.flags & TF_EXIT))
218 err = !wait_for_completion_timeout(task->done,
219 msecs_to_jiffies(UVESAFB_TIMEOUT));
221 mutex_lock(&uvfb_lock);
222 uvfb_tasks[seq] = NULL;
223 mutex_unlock(&uvfb_lock);
225 seq++;
226 if (seq >= UVESAFB_TASKS_MAX)
227 seq = 0;
228 out:
229 kfree(m);
230 return err;
234 * Free a uvesafb_ktask struct.
236 static void uvesafb_free(struct uvesafb_ktask *task)
238 if (task) {
239 if (task->done)
240 kfree(task->done);
241 kfree(task);
246 * Prepare a uvesafb_ktask struct to be used again.
248 static void uvesafb_reset(struct uvesafb_ktask *task)
250 struct completion *cpl = task->done;
252 memset(task, 0, sizeof(*task));
253 task->done = cpl;
257 * Allocate and prepare a uvesafb_ktask struct.
259 static struct uvesafb_ktask *uvesafb_prep(void)
261 struct uvesafb_ktask *task;
263 task = kzalloc(sizeof(*task), GFP_KERNEL);
264 if (task) {
265 task->done = kzalloc(sizeof(*task->done), GFP_KERNEL);
266 if (!task->done) {
267 kfree(task);
268 task = NULL;
271 return task;
274 static void uvesafb_setup_var(struct fb_var_screeninfo *var,
275 struct fb_info *info, struct vbe_mode_ib *mode)
277 struct uvesafb_par *par = info->par;
279 var->vmode = FB_VMODE_NONINTERLACED;
280 var->sync = FB_SYNC_VERT_HIGH_ACT;
282 var->xres = mode->x_res;
283 var->yres = mode->y_res;
284 var->xres_virtual = mode->x_res;
285 var->yres_virtual = (par->ypan) ?
286 info->fix.smem_len / mode->bytes_per_scan_line :
287 mode->y_res;
288 var->xoffset = 0;
289 var->yoffset = 0;
290 var->bits_per_pixel = mode->bits_per_pixel;
292 if (var->bits_per_pixel == 15)
293 var->bits_per_pixel = 16;
295 if (var->bits_per_pixel > 8) {
296 var->red.offset = mode->red_off;
297 var->red.length = mode->red_len;
298 var->green.offset = mode->green_off;
299 var->green.length = mode->green_len;
300 var->blue.offset = mode->blue_off;
301 var->blue.length = mode->blue_len;
302 var->transp.offset = mode->rsvd_off;
303 var->transp.length = mode->rsvd_len;
304 } else {
305 var->red.offset = 0;
306 var->green.offset = 0;
307 var->blue.offset = 0;
308 var->transp.offset = 0;
310 var->red.length = 8;
311 var->green.length = 8;
312 var->blue.length = 8;
313 var->transp.length = 0;
317 static int uvesafb_vbe_find_mode(struct uvesafb_par *par,
318 int xres, int yres, int depth, unsigned char flags)
320 int i, match = -1, h = 0, d = 0x7fffffff;
322 for (i = 0; i < par->vbe_modes_cnt; i++) {
323 h = abs(par->vbe_modes[i].x_res - xres) +
324 abs(par->vbe_modes[i].y_res - yres) +
325 abs(depth - par->vbe_modes[i].depth);
328 * We have an exact match in terms of resolution
329 * and depth.
331 if (h == 0)
332 return i;
334 if (h < d || (h == d && par->vbe_modes[i].depth > depth)) {
335 d = h;
336 match = i;
339 i = 1;
341 if (flags & UVESAFB_EXACT_DEPTH &&
342 par->vbe_modes[match].depth != depth)
343 i = 0;
345 if (flags & UVESAFB_EXACT_RES && d > 24)
346 i = 0;
348 if (i != 0)
349 return match;
350 else
351 return -1;
354 static u8 *uvesafb_vbe_state_save(struct uvesafb_par *par)
356 struct uvesafb_ktask *task;
357 u8 *state;
358 int err;
360 if (!par->vbe_state_size)
361 return NULL;
363 state = kmalloc(par->vbe_state_size, GFP_KERNEL);
364 if (!state)
365 return NULL;
367 task = uvesafb_prep();
368 if (!task) {
369 kfree(state);
370 return NULL;
373 task->t.regs.eax = 0x4f04;
374 task->t.regs.ecx = 0x000f;
375 task->t.regs.edx = 0x0001;
376 task->t.flags = TF_BUF_RET | TF_BUF_ESBX;
377 task->t.buf_len = par->vbe_state_size;
378 task->buf = state;
379 err = uvesafb_exec(task);
381 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
382 printk(KERN_WARNING "uvesafb: VBE get state call "
383 "failed (eax=0x%x, err=%d)\n",
384 task->t.regs.eax, err);
385 kfree(state);
386 state = NULL;
389 uvesafb_free(task);
390 return state;
393 static void uvesafb_vbe_state_restore(struct uvesafb_par *par, u8 *state_buf)
395 struct uvesafb_ktask *task;
396 int err;
398 if (!state_buf)
399 return;
401 task = uvesafb_prep();
402 if (!task)
403 return;
405 task->t.regs.eax = 0x4f04;
406 task->t.regs.ecx = 0x000f;
407 task->t.regs.edx = 0x0002;
408 task->t.buf_len = par->vbe_state_size;
409 task->t.flags = TF_BUF_ESBX;
410 task->buf = state_buf;
412 err = uvesafb_exec(task);
413 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
414 printk(KERN_WARNING "uvesafb: VBE state restore call "
415 "failed (eax=0x%x, err=%d)\n",
416 task->t.regs.eax, err);
418 uvesafb_free(task);
421 static int __devinit uvesafb_vbe_getinfo(struct uvesafb_ktask *task,
422 struct uvesafb_par *par)
424 int err;
426 task->t.regs.eax = 0x4f00;
427 task->t.flags = TF_VBEIB;
428 task->t.buf_len = sizeof(struct vbe_ib);
429 task->buf = &par->vbe_ib;
430 strncpy(par->vbe_ib.vbe_signature, "VBE2", 4);
432 err = uvesafb_exec(task);
433 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
434 printk(KERN_ERR "uvesafb: Getting VBE info block failed "
435 "(eax=0x%x, err=%d)\n", (u32)task->t.regs.eax,
436 err);
437 return -EINVAL;
440 if (par->vbe_ib.vbe_version < 0x0200) {
441 printk(KERN_ERR "uvesafb: Sorry, pre-VBE 2.0 cards are "
442 "not supported.\n");
443 return -EINVAL;
446 if (!par->vbe_ib.mode_list_ptr) {
447 printk(KERN_ERR "uvesafb: Missing mode list!\n");
448 return -EINVAL;
451 printk(KERN_INFO "uvesafb: ");
454 * Convert string pointers and the mode list pointer into
455 * usable addresses. Print informational messages about the
456 * video adapter and its vendor.
458 if (par->vbe_ib.oem_vendor_name_ptr)
459 printk("%s, ",
460 ((char *)task->buf) + par->vbe_ib.oem_vendor_name_ptr);
462 if (par->vbe_ib.oem_product_name_ptr)
463 printk("%s, ",
464 ((char *)task->buf) + par->vbe_ib.oem_product_name_ptr);
466 if (par->vbe_ib.oem_product_rev_ptr)
467 printk("%s, ",
468 ((char *)task->buf) + par->vbe_ib.oem_product_rev_ptr);
470 if (par->vbe_ib.oem_string_ptr)
471 printk("OEM: %s, ",
472 ((char *)task->buf) + par->vbe_ib.oem_string_ptr);
474 printk("VBE v%d.%d\n", ((par->vbe_ib.vbe_version & 0xff00) >> 8),
475 par->vbe_ib.vbe_version & 0xff);
477 return 0;
480 static int __devinit uvesafb_vbe_getmodes(struct uvesafb_ktask *task,
481 struct uvesafb_par *par)
483 int off = 0, err;
484 u16 *mode;
486 par->vbe_modes_cnt = 0;
488 /* Count available modes. */
489 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
490 while (*mode != 0xffff) {
491 par->vbe_modes_cnt++;
492 mode++;
495 par->vbe_modes = kzalloc(sizeof(struct vbe_mode_ib) *
496 par->vbe_modes_cnt, GFP_KERNEL);
497 if (!par->vbe_modes)
498 return -ENOMEM;
500 /* Get info about all available modes. */
501 mode = (u16 *) (((u8 *)&par->vbe_ib) + par->vbe_ib.mode_list_ptr);
502 while (*mode != 0xffff) {
503 struct vbe_mode_ib *mib;
505 uvesafb_reset(task);
506 task->t.regs.eax = 0x4f01;
507 task->t.regs.ecx = (u32) *mode;
508 task->t.flags = TF_BUF_RET | TF_BUF_ESDI;
509 task->t.buf_len = sizeof(struct vbe_mode_ib);
510 task->buf = par->vbe_modes + off;
512 err = uvesafb_exec(task);
513 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
514 printk(KERN_WARNING "uvesafb: Getting mode info block "
515 "for mode 0x%x failed (eax=0x%x, err=%d)\n",
516 *mode, (u32)task->t.regs.eax, err);
517 mode++;
518 par->vbe_modes_cnt--;
519 continue;
522 mib = task->buf;
523 mib->mode_id = *mode;
526 * We only want modes that are supported with the current
527 * hardware configuration, color, graphics and that have
528 * support for the LFB.
530 if ((mib->mode_attr & VBE_MODE_MASK) == VBE_MODE_MASK &&
531 mib->bits_per_pixel >= 8)
532 off++;
533 else
534 par->vbe_modes_cnt--;
536 mode++;
537 mib->depth = mib->red_len + mib->green_len + mib->blue_len;
540 * Handle 8bpp modes and modes with broken color component
541 * lengths.
543 if (mib->depth == 0 || (mib->depth == 24 &&
544 mib->bits_per_pixel == 32))
545 mib->depth = mib->bits_per_pixel;
548 if (par->vbe_modes_cnt > 0)
549 return 0;
550 else
551 return -EINVAL;
555 * The Protected Mode Interface is 32-bit x86 code, so we only run it on
556 * x86 and not x86_64.
558 #ifdef CONFIG_X86_32
559 static int __devinit uvesafb_vbe_getpmi(struct uvesafb_ktask *task,
560 struct uvesafb_par *par)
562 int i, err;
564 uvesafb_reset(task);
565 task->t.regs.eax = 0x4f0a;
566 task->t.regs.ebx = 0x0;
567 err = uvesafb_exec(task);
569 if ((task->t.regs.eax & 0xffff) != 0x4f || task->t.regs.es < 0xc000) {
570 par->pmi_setpal = par->ypan = 0;
571 } else {
572 par->pmi_base = (u16 *)phys_to_virt(((u32)task->t.regs.es << 4)
573 + task->t.regs.edi);
574 par->pmi_start = (u8 *)par->pmi_base + par->pmi_base[1];
575 par->pmi_pal = (u8 *)par->pmi_base + par->pmi_base[2];
576 printk(KERN_INFO "uvesafb: protected mode interface info at "
577 "%04x:%04x\n",
578 (u16)task->t.regs.es, (u16)task->t.regs.edi);
579 printk(KERN_INFO "uvesafb: pmi: set display start = %p, "
580 "set palette = %p\n", par->pmi_start,
581 par->pmi_pal);
583 if (par->pmi_base[3]) {
584 printk(KERN_INFO "uvesafb: pmi: ports = ");
585 for (i = par->pmi_base[3]/2;
586 par->pmi_base[i] != 0xffff; i++)
587 printk("%x ", par->pmi_base[i]);
588 printk("\n");
590 if (par->pmi_base[i] != 0xffff) {
591 printk(KERN_INFO "uvesafb: can't handle memory"
592 " requests, pmi disabled\n");
593 par->ypan = par->pmi_setpal = 0;
597 return 0;
599 #endif /* CONFIG_X86_32 */
602 * Check whether a video mode is supported by the Video BIOS and is
603 * compatible with the monitor limits.
605 static int __devinit uvesafb_is_valid_mode(struct fb_videomode *mode,
606 struct fb_info *info)
608 if (info->monspecs.gtf) {
609 fb_videomode_to_var(&info->var, mode);
610 if (fb_validate_mode(&info->var, info))
611 return 0;
614 if (uvesafb_vbe_find_mode(info->par, mode->xres, mode->yres, 8,
615 UVESAFB_EXACT_RES) == -1)
616 return 0;
618 return 1;
621 static int __devinit uvesafb_vbe_getedid(struct uvesafb_ktask *task,
622 struct fb_info *info)
624 struct uvesafb_par *par = info->par;
625 int err = 0;
627 if (noedid || par->vbe_ib.vbe_version < 0x0300)
628 return -EINVAL;
630 task->t.regs.eax = 0x4f15;
631 task->t.regs.ebx = 0;
632 task->t.regs.ecx = 0;
633 task->t.buf_len = 0;
634 task->t.flags = 0;
636 err = uvesafb_exec(task);
638 if ((task->t.regs.eax & 0xffff) != 0x004f || err)
639 return -EINVAL;
641 if ((task->t.regs.ebx & 0x3) == 3) {
642 printk(KERN_INFO "uvesafb: VBIOS/hardware supports both "
643 "DDC1 and DDC2 transfers\n");
644 } else if ((task->t.regs.ebx & 0x3) == 2) {
645 printk(KERN_INFO "uvesafb: VBIOS/hardware supports DDC2 "
646 "transfers\n");
647 } else if ((task->t.regs.ebx & 0x3) == 1) {
648 printk(KERN_INFO "uvesafb: VBIOS/hardware supports DDC1 "
649 "transfers\n");
650 } else {
651 printk(KERN_INFO "uvesafb: VBIOS/hardware doesn't support "
652 "DDC transfers\n");
653 return -EINVAL;
656 task->t.regs.eax = 0x4f15;
657 task->t.regs.ebx = 1;
658 task->t.regs.ecx = task->t.regs.edx = 0;
659 task->t.flags = TF_BUF_RET | TF_BUF_ESDI;
660 task->t.buf_len = EDID_LENGTH;
661 task->buf = kzalloc(EDID_LENGTH, GFP_KERNEL);
663 err = uvesafb_exec(task);
665 if ((task->t.regs.eax & 0xffff) == 0x004f && !err) {
666 fb_edid_to_monspecs(task->buf, &info->monspecs);
668 if (info->monspecs.vfmax && info->monspecs.hfmax) {
670 * If the maximum pixel clock wasn't specified in
671 * the EDID block, set it to 300 MHz.
673 if (info->monspecs.dclkmax == 0)
674 info->monspecs.dclkmax = 300 * 1000000;
675 info->monspecs.gtf = 1;
677 } else {
678 err = -EINVAL;
681 kfree(task->buf);
682 return err;
685 static void __devinit uvesafb_vbe_getmonspecs(struct uvesafb_ktask *task,
686 struct fb_info *info)
688 struct uvesafb_par *par = info->par;
689 int i;
691 memset(&info->monspecs, 0, sizeof(info->monspecs));
694 * If we don't get all necessary data from the EDID block,
695 * mark it as incompatible with the GTF and set nocrtc so
696 * that we always use the default BIOS refresh rate.
698 if (uvesafb_vbe_getedid(task, info)) {
699 info->monspecs.gtf = 0;
700 par->nocrtc = 1;
703 /* Kernel command line overrides. */
704 if (maxclk)
705 info->monspecs.dclkmax = maxclk * 1000000;
706 if (maxvf)
707 info->monspecs.vfmax = maxvf;
708 if (maxhf)
709 info->monspecs.hfmax = maxhf * 1000;
712 * In case DDC transfers are not supported, the user can provide
713 * monitor limits manually. Lower limits are set to "safe" values.
715 if (info->monspecs.gtf == 0 && maxclk && maxvf && maxhf) {
716 info->monspecs.dclkmin = 0;
717 info->monspecs.vfmin = 60;
718 info->monspecs.hfmin = 29000;
719 info->monspecs.gtf = 1;
720 par->nocrtc = 0;
723 if (info->monspecs.gtf)
724 printk(KERN_INFO
725 "uvesafb: monitor limits: vf = %d Hz, hf = %d kHz, "
726 "clk = %d MHz\n", info->monspecs.vfmax,
727 (int)(info->monspecs.hfmax / 1000),
728 (int)(info->monspecs.dclkmax / 1000000));
729 else
730 printk(KERN_INFO "uvesafb: no monitor limits have been set, "
731 "default refresh rate will be used\n");
733 /* Add VBE modes to the modelist. */
734 for (i = 0; i < par->vbe_modes_cnt; i++) {
735 struct fb_var_screeninfo var;
736 struct vbe_mode_ib *mode;
737 struct fb_videomode vmode;
739 mode = &par->vbe_modes[i];
740 memset(&var, 0, sizeof(var));
742 var.xres = mode->x_res;
743 var.yres = mode->y_res;
745 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, &var, info);
746 fb_var_to_videomode(&vmode, &var);
747 fb_add_videomode(&vmode, &info->modelist);
750 /* Add valid VESA modes to our modelist. */
751 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
752 if (uvesafb_is_valid_mode((struct fb_videomode *)
753 &vesa_modes[i], info))
754 fb_add_videomode(&vesa_modes[i], &info->modelist);
757 for (i = 0; i < info->monspecs.modedb_len; i++) {
758 if (uvesafb_is_valid_mode(&info->monspecs.modedb[i], info))
759 fb_add_videomode(&info->monspecs.modedb[i],
760 &info->modelist);
763 return;
766 static void __devinit uvesafb_vbe_getstatesize(struct uvesafb_ktask *task,
767 struct uvesafb_par *par)
769 int err;
771 uvesafb_reset(task);
774 * Get the VBE state buffer size. We want all available
775 * hardware state data (CL = 0x0f).
777 task->t.regs.eax = 0x4f04;
778 task->t.regs.ecx = 0x000f;
779 task->t.regs.edx = 0x0000;
780 task->t.flags = 0;
782 err = uvesafb_exec(task);
784 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
785 printk(KERN_WARNING "uvesafb: VBE state buffer size "
786 "cannot be determined (eax=0x%x, err=%d)\n",
787 task->t.regs.eax, err);
788 par->vbe_state_size = 0;
789 return;
792 par->vbe_state_size = 64 * (task->t.regs.ebx & 0xffff);
795 static int __devinit uvesafb_vbe_init(struct fb_info *info)
797 struct uvesafb_ktask *task = NULL;
798 struct uvesafb_par *par = info->par;
799 int err;
801 task = uvesafb_prep();
802 if (!task)
803 return -ENOMEM;
805 err = uvesafb_vbe_getinfo(task, par);
806 if (err)
807 goto out;
809 err = uvesafb_vbe_getmodes(task, par);
810 if (err)
811 goto out;
813 par->nocrtc = nocrtc;
814 #ifdef CONFIG_X86_32
815 par->pmi_setpal = pmi_setpal;
816 par->ypan = ypan;
818 if (par->pmi_setpal || par->ypan) {
819 if (__supported_pte_mask & _PAGE_NX) {
820 par->pmi_setpal = par->ypan = 0;
821 printk(KERN_WARNING "uvesafb: NX protection is actively."
822 "We have better not to use the PMI.\n");
823 } else {
824 uvesafb_vbe_getpmi(task, par);
827 #else
828 /* The protected mode interface is not available on non-x86. */
829 par->pmi_setpal = par->ypan = 0;
830 #endif
832 INIT_LIST_HEAD(&info->modelist);
833 uvesafb_vbe_getmonspecs(task, info);
834 uvesafb_vbe_getstatesize(task, par);
836 out: uvesafb_free(task);
837 return err;
840 static int __devinit uvesafb_vbe_init_mode(struct fb_info *info)
842 struct list_head *pos;
843 struct fb_modelist *modelist;
844 struct fb_videomode *mode;
845 struct uvesafb_par *par = info->par;
846 int i, modeid;
848 /* Has the user requested a specific VESA mode? */
849 if (vbemode) {
850 for (i = 0; i < par->vbe_modes_cnt; i++) {
851 if (par->vbe_modes[i].mode_id == vbemode) {
852 modeid = i;
853 uvesafb_setup_var(&info->var, info,
854 &par->vbe_modes[modeid]);
855 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
856 &info->var, info);
858 * With pixclock set to 0, the default BIOS
859 * timings will be used in set_par().
861 info->var.pixclock = 0;
862 goto gotmode;
865 printk(KERN_INFO "uvesafb: requested VBE mode 0x%x is "
866 "unavailable\n", vbemode);
867 vbemode = 0;
870 /* Count the modes in the modelist */
871 i = 0;
872 list_for_each(pos, &info->modelist)
873 i++;
876 * Convert the modelist into a modedb so that we can use it with
877 * fb_find_mode().
879 mode = kzalloc(i * sizeof(*mode), GFP_KERNEL);
880 if (mode) {
881 i = 0;
882 list_for_each(pos, &info->modelist) {
883 modelist = list_entry(pos, struct fb_modelist, list);
884 mode[i] = modelist->mode;
885 i++;
888 if (!mode_option)
889 mode_option = UVESAFB_DEFAULT_MODE;
891 i = fb_find_mode(&info->var, info, mode_option, mode, i,
892 NULL, 8);
894 kfree(mode);
897 /* fb_find_mode() failed */
898 if (i == 0) {
899 info->var.xres = 640;
900 info->var.yres = 480;
901 mode = (struct fb_videomode *)
902 fb_find_best_mode(&info->var, &info->modelist);
904 if (mode) {
905 fb_videomode_to_var(&info->var, mode);
906 } else {
907 modeid = par->vbe_modes[0].mode_id;
908 uvesafb_setup_var(&info->var, info,
909 &par->vbe_modes[modeid]);
910 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
911 &info->var, info);
913 goto gotmode;
917 /* Look for a matching VBE mode. */
918 modeid = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres,
919 info->var.bits_per_pixel, UVESAFB_EXACT_RES);
921 if (modeid == -1)
922 return -EINVAL;
924 uvesafb_setup_var(&info->var, info, &par->vbe_modes[modeid]);
926 gotmode:
928 * If we are not VBE3.0+ compliant, we're done -- the BIOS will
929 * ignore our timings anyway.
931 if (par->vbe_ib.vbe_version < 0x0300 || par->nocrtc)
932 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
933 &info->var, info);
935 return modeid;
938 static int uvesafb_setpalette(struct uvesafb_pal_entry *entries, int count,
939 int start, struct fb_info *info)
941 struct uvesafb_ktask *task;
942 #ifdef CONFIG_X86
943 struct uvesafb_par *par = info->par;
944 int i = par->mode_idx;
945 #endif
946 int err = 0;
949 * We support palette modifications for 8 bpp modes only, so
950 * there can never be more than 256 entries.
952 if (start + count > 256)
953 return -EINVAL;
955 #ifdef CONFIG_X86
956 /* Use VGA registers if mode is VGA-compatible. */
957 if (i >= 0 && i < par->vbe_modes_cnt &&
958 par->vbe_modes[i].mode_attr & VBE_MODE_VGACOMPAT) {
959 for (i = 0; i < count; i++) {
960 outb_p(start + i, dac_reg);
961 outb_p(entries[i].red, dac_val);
962 outb_p(entries[i].green, dac_val);
963 outb_p(entries[i].blue, dac_val);
966 #ifdef CONFIG_X86_32
967 else if (par->pmi_setpal) {
968 __asm__ __volatile__(
969 "call *(%%esi)"
970 : /* no return value */
971 : "a" (0x4f09), /* EAX */
972 "b" (0), /* EBX */
973 "c" (count), /* ECX */
974 "d" (start), /* EDX */
975 "D" (entries), /* EDI */
976 "S" (&par->pmi_pal)); /* ESI */
978 #endif /* CONFIG_X86_32 */
979 else
980 #endif /* CONFIG_X86 */
982 task = uvesafb_prep();
983 if (!task)
984 return -ENOMEM;
986 task->t.regs.eax = 0x4f09;
987 task->t.regs.ebx = 0x0;
988 task->t.regs.ecx = count;
989 task->t.regs.edx = start;
990 task->t.flags = TF_BUF_ESDI;
991 task->t.buf_len = sizeof(struct uvesafb_pal_entry) * count;
992 task->buf = entries;
994 err = uvesafb_exec(task);
995 if ((task->t.regs.eax & 0xffff) != 0x004f)
996 err = 1;
998 uvesafb_free(task);
1000 return err;
1003 static int uvesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
1004 unsigned blue, unsigned transp,
1005 struct fb_info *info)
1007 struct uvesafb_pal_entry entry;
1008 int shift = 16 - dac_width;
1009 int err = 0;
1011 if (regno >= info->cmap.len)
1012 return -EINVAL;
1014 if (info->var.bits_per_pixel == 8) {
1015 entry.red = red >> shift;
1016 entry.green = green >> shift;
1017 entry.blue = blue >> shift;
1018 entry.pad = 0;
1020 err = uvesafb_setpalette(&entry, 1, regno, info);
1021 } else if (regno < 16) {
1022 switch (info->var.bits_per_pixel) {
1023 case 16:
1024 if (info->var.red.offset == 10) {
1025 /* 1:5:5:5 */
1026 ((u32 *) (info->pseudo_palette))[regno] =
1027 ((red & 0xf800) >> 1) |
1028 ((green & 0xf800) >> 6) |
1029 ((blue & 0xf800) >> 11);
1030 } else {
1031 /* 0:5:6:5 */
1032 ((u32 *) (info->pseudo_palette))[regno] =
1033 ((red & 0xf800) ) |
1034 ((green & 0xfc00) >> 5) |
1035 ((blue & 0xf800) >> 11);
1037 break;
1039 case 24:
1040 case 32:
1041 red >>= 8;
1042 green >>= 8;
1043 blue >>= 8;
1044 ((u32 *)(info->pseudo_palette))[regno] =
1045 (red << info->var.red.offset) |
1046 (green << info->var.green.offset) |
1047 (blue << info->var.blue.offset);
1048 break;
1051 return err;
1054 static int uvesafb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1056 struct uvesafb_pal_entry *entries;
1057 int shift = 16 - dac_width;
1058 int i, err = 0;
1060 if (info->var.bits_per_pixel == 8) {
1061 if (cmap->start + cmap->len > info->cmap.start +
1062 info->cmap.len || cmap->start < info->cmap.start)
1063 return -EINVAL;
1065 entries = kmalloc(sizeof(*entries) * cmap->len, GFP_KERNEL);
1066 if (!entries)
1067 return -ENOMEM;
1069 for (i = 0; i < cmap->len; i++) {
1070 entries[i].red = cmap->red[i] >> shift;
1071 entries[i].green = cmap->green[i] >> shift;
1072 entries[i].blue = cmap->blue[i] >> shift;
1073 entries[i].pad = 0;
1075 err = uvesafb_setpalette(entries, cmap->len, cmap->start, info);
1076 kfree(entries);
1077 } else {
1079 * For modes with bpp > 8, we only set the pseudo palette in
1080 * the fb_info struct. We rely on uvesafb_setcolreg to do all
1081 * sanity checking.
1083 for (i = 0; i < cmap->len; i++) {
1084 err |= uvesafb_setcolreg(cmap->start + i, cmap->red[i],
1085 cmap->green[i], cmap->blue[i],
1086 0, info);
1089 return err;
1092 static int uvesafb_pan_display(struct fb_var_screeninfo *var,
1093 struct fb_info *info)
1095 #ifdef CONFIG_X86_32
1096 int offset;
1097 struct uvesafb_par *par = info->par;
1099 offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
1102 * It turns out it's not the best idea to do panning via vm86,
1103 * so we only allow it if we have a PMI.
1105 if (par->pmi_start) {
1106 __asm__ __volatile__(
1107 "call *(%%edi)"
1108 : /* no return value */
1109 : "a" (0x4f07), /* EAX */
1110 "b" (0), /* EBX */
1111 "c" (offset), /* ECX */
1112 "d" (offset >> 16), /* EDX */
1113 "D" (&par->pmi_start)); /* EDI */
1115 #endif
1116 return 0;
1119 static int uvesafb_blank(int blank, struct fb_info *info)
1121 struct uvesafb_ktask *task;
1122 int err = 1;
1123 #ifdef CONFIG_X86
1124 struct uvesafb_par *par = info->par;
1126 if (par->vbe_ib.capabilities & VBE_CAP_VGACOMPAT) {
1127 int loop = 10000;
1128 u8 seq = 0, crtc17 = 0;
1130 if (blank == FB_BLANK_POWERDOWN) {
1131 seq = 0x20;
1132 crtc17 = 0x00;
1133 err = 0;
1134 } else {
1135 seq = 0x00;
1136 crtc17 = 0x80;
1137 err = (blank == FB_BLANK_UNBLANK) ? 0 : -EINVAL;
1140 vga_wseq(NULL, 0x00, 0x01);
1141 seq |= vga_rseq(NULL, 0x01) & ~0x20;
1142 vga_wseq(NULL, 0x00, seq);
1144 crtc17 |= vga_rcrt(NULL, 0x17) & ~0x80;
1145 while (loop--);
1146 vga_wcrt(NULL, 0x17, crtc17);
1147 vga_wseq(NULL, 0x00, 0x03);
1148 } else
1149 #endif /* CONFIG_X86 */
1151 task = uvesafb_prep();
1152 if (!task)
1153 return -ENOMEM;
1155 task->t.regs.eax = 0x4f10;
1156 switch (blank) {
1157 case FB_BLANK_UNBLANK:
1158 task->t.regs.ebx = 0x0001;
1159 break;
1160 case FB_BLANK_NORMAL:
1161 task->t.regs.ebx = 0x0101; /* standby */
1162 break;
1163 case FB_BLANK_POWERDOWN:
1164 task->t.regs.ebx = 0x0401; /* powerdown */
1165 break;
1166 default:
1167 goto out;
1170 err = uvesafb_exec(task);
1171 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
1172 err = 1;
1173 out: uvesafb_free(task);
1175 return err;
1178 static int uvesafb_open(struct fb_info *info, int user)
1180 struct uvesafb_par *par = info->par;
1181 int cnt = atomic_read(&par->ref_count);
1183 if (!cnt && par->vbe_state_size)
1184 par->vbe_state_orig = uvesafb_vbe_state_save(par);
1186 atomic_inc(&par->ref_count);
1187 return 0;
1190 static int uvesafb_release(struct fb_info *info, int user)
1192 struct uvesafb_ktask *task = NULL;
1193 struct uvesafb_par *par = info->par;
1194 int cnt = atomic_read(&par->ref_count);
1196 if (!cnt)
1197 return -EINVAL;
1199 if (cnt != 1)
1200 goto out;
1202 task = uvesafb_prep();
1203 if (!task)
1204 goto out;
1206 /* First, try to set the standard 80x25 text mode. */
1207 task->t.regs.eax = 0x0003;
1208 uvesafb_exec(task);
1211 * Now try to restore whatever hardware state we might have
1212 * saved when the fb device was first opened.
1214 uvesafb_vbe_state_restore(par, par->vbe_state_orig);
1215 out:
1216 atomic_dec(&par->ref_count);
1217 if (task)
1218 uvesafb_free(task);
1219 return 0;
1222 static int uvesafb_set_par(struct fb_info *info)
1224 struct uvesafb_par *par = info->par;
1225 struct uvesafb_ktask *task = NULL;
1226 struct vbe_crtc_ib *crtc = NULL;
1227 struct vbe_mode_ib *mode = NULL;
1228 int i, err = 0, depth = info->var.bits_per_pixel;
1230 if (depth > 8 && depth != 32)
1231 depth = info->var.red.length + info->var.green.length +
1232 info->var.blue.length;
1234 i = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres, depth,
1235 UVESAFB_EXACT_RES | UVESAFB_EXACT_DEPTH);
1236 if (i >= 0)
1237 mode = &par->vbe_modes[i];
1238 else
1239 return -EINVAL;
1241 task = uvesafb_prep();
1242 if (!task)
1243 return -ENOMEM;
1244 setmode:
1245 task->t.regs.eax = 0x4f02;
1246 task->t.regs.ebx = mode->mode_id | 0x4000; /* use LFB */
1248 if (par->vbe_ib.vbe_version >= 0x0300 && !par->nocrtc &&
1249 info->var.pixclock != 0) {
1250 task->t.regs.ebx |= 0x0800; /* use CRTC data */
1251 task->t.flags = TF_BUF_ESDI;
1252 crtc = kzalloc(sizeof(struct vbe_crtc_ib), GFP_KERNEL);
1253 if (!crtc) {
1254 err = -ENOMEM;
1255 goto out;
1257 crtc->horiz_start = info->var.xres + info->var.right_margin;
1258 crtc->horiz_end = crtc->horiz_start + info->var.hsync_len;
1259 crtc->horiz_total = crtc->horiz_end + info->var.left_margin;
1261 crtc->vert_start = info->var.yres + info->var.lower_margin;
1262 crtc->vert_end = crtc->vert_start + info->var.vsync_len;
1263 crtc->vert_total = crtc->vert_end + info->var.upper_margin;
1265 crtc->pixel_clock = PICOS2KHZ(info->var.pixclock) * 1000;
1266 crtc->refresh_rate = (u16)(100 * (crtc->pixel_clock /
1267 (crtc->vert_total * crtc->horiz_total)));
1269 if (info->var.vmode & FB_VMODE_DOUBLE)
1270 crtc->flags |= 0x1;
1271 if (info->var.vmode & FB_VMODE_INTERLACED)
1272 crtc->flags |= 0x2;
1273 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
1274 crtc->flags |= 0x4;
1275 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
1276 crtc->flags |= 0x8;
1277 memcpy(&par->crtc, crtc, sizeof(*crtc));
1278 } else {
1279 memset(&par->crtc, 0, sizeof(*crtc));
1282 task->t.buf_len = sizeof(struct vbe_crtc_ib);
1283 task->buf = &par->crtc;
1285 err = uvesafb_exec(task);
1286 if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
1288 * The mode switch might have failed because we tried to
1289 * use our own timings. Try again with the default timings.
1291 if (crtc != NULL) {
1292 printk(KERN_WARNING "uvesafb: mode switch failed "
1293 "(eax=0x%x, err=%d). Trying again with "
1294 "default timings.\n", task->t.regs.eax, err);
1295 uvesafb_reset(task);
1296 kfree(crtc);
1297 crtc = NULL;
1298 info->var.pixclock = 0;
1299 goto setmode;
1300 } else {
1301 printk(KERN_ERR "uvesafb: mode switch failed (eax="
1302 "0x%x, err=%d)\n", task->t.regs.eax, err);
1303 err = -EINVAL;
1304 goto out;
1307 par->mode_idx = i;
1309 /* For 8bpp modes, always try to set the DAC to 8 bits. */
1310 if (par->vbe_ib.capabilities & VBE_CAP_CAN_SWITCH_DAC &&
1311 mode->bits_per_pixel <= 8) {
1312 uvesafb_reset(task);
1313 task->t.regs.eax = 0x4f08;
1314 task->t.regs.ebx = 0x0800;
1316 err = uvesafb_exec(task);
1317 if (err || (task->t.regs.eax & 0xffff) != 0x004f ||
1318 ((task->t.regs.ebx & 0xff00) >> 8) != 8) {
1319 dac_width = 6;
1320 } else {
1321 dac_width = 8;
1325 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1326 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1327 info->fix.line_length = mode->bytes_per_scan_line;
1329 out: if (crtc != NULL)
1330 kfree(crtc);
1331 uvesafb_free(task);
1333 return err;
1336 static void uvesafb_check_limits(struct fb_var_screeninfo *var,
1337 struct fb_info *info)
1339 const struct fb_videomode *mode;
1340 struct uvesafb_par *par = info->par;
1343 * If pixclock is set to 0, then we're using default BIOS timings
1344 * and thus don't have to perform any checks here.
1346 if (!var->pixclock)
1347 return;
1349 if (par->vbe_ib.vbe_version < 0x0300) {
1350 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, var, info);
1351 return;
1354 if (!fb_validate_mode(var, info))
1355 return;
1357 mode = fb_find_best_mode(var, &info->modelist);
1358 if (mode) {
1359 if (mode->xres == var->xres && mode->yres == var->yres &&
1360 !(mode->vmode & (FB_VMODE_INTERLACED | FB_VMODE_DOUBLE))) {
1361 fb_videomode_to_var(var, mode);
1362 return;
1366 if (info->monspecs.gtf && !fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1367 return;
1368 /* Use default refresh rate */
1369 var->pixclock = 0;
1372 static int uvesafb_check_var(struct fb_var_screeninfo *var,
1373 struct fb_info *info)
1375 struct uvesafb_par *par = info->par;
1376 struct vbe_mode_ib *mode = NULL;
1377 int match = -1;
1378 int depth = var->red.length + var->green.length + var->blue.length;
1381 * Various apps will use bits_per_pixel to set the color depth,
1382 * which is theoretically incorrect, but which we'll try to handle
1383 * here.
1385 if (depth == 0 || abs(depth - var->bits_per_pixel) >= 8)
1386 depth = var->bits_per_pixel;
1388 match = uvesafb_vbe_find_mode(par, var->xres, var->yres, depth,
1389 UVESAFB_EXACT_RES);
1390 if (match == -1)
1391 return -EINVAL;
1393 mode = &par->vbe_modes[match];
1394 uvesafb_setup_var(var, info, mode);
1397 * Check whether we have remapped enough memory for this mode.
1398 * We might be called at an early stage, when we haven't remapped
1399 * any memory yet, in which case we simply skip the check.
1401 if (var->yres * mode->bytes_per_scan_line > info->fix.smem_len
1402 && info->fix.smem_len)
1403 return -EINVAL;
1405 if ((var->vmode & FB_VMODE_DOUBLE) &&
1406 !(par->vbe_modes[match].mode_attr & 0x100))
1407 var->vmode &= ~FB_VMODE_DOUBLE;
1409 if ((var->vmode & FB_VMODE_INTERLACED) &&
1410 !(par->vbe_modes[match].mode_attr & 0x200))
1411 var->vmode &= ~FB_VMODE_INTERLACED;
1413 uvesafb_check_limits(var, info);
1415 var->xres_virtual = var->xres;
1416 var->yres_virtual = (par->ypan) ?
1417 info->fix.smem_len / mode->bytes_per_scan_line :
1418 var->yres;
1419 return 0;
1422 static struct fb_ops uvesafb_ops = {
1423 .owner = THIS_MODULE,
1424 .fb_open = uvesafb_open,
1425 .fb_release = uvesafb_release,
1426 .fb_setcolreg = uvesafb_setcolreg,
1427 .fb_setcmap = uvesafb_setcmap,
1428 .fb_pan_display = uvesafb_pan_display,
1429 .fb_blank = uvesafb_blank,
1430 .fb_fillrect = cfb_fillrect,
1431 .fb_copyarea = cfb_copyarea,
1432 .fb_imageblit = cfb_imageblit,
1433 .fb_check_var = uvesafb_check_var,
1434 .fb_set_par = uvesafb_set_par,
1437 static void __devinit uvesafb_init_info(struct fb_info *info,
1438 struct vbe_mode_ib *mode)
1440 unsigned int size_vmode;
1441 unsigned int size_remap;
1442 unsigned int size_total;
1443 struct uvesafb_par *par = info->par;
1444 int i, h;
1446 info->pseudo_palette = ((u8 *)info->par + sizeof(struct uvesafb_par));
1447 info->fix = uvesafb_fix;
1448 info->fix.ypanstep = par->ypan ? 1 : 0;
1449 info->fix.ywrapstep = (par->ypan > 1) ? 1 : 0;
1451 /* Disable blanking if the user requested so. */
1452 if (!blank)
1453 info->fbops->fb_blank = NULL;
1456 * Find out how much IO memory is required for the mode with
1457 * the highest resolution.
1459 size_remap = 0;
1460 for (i = 0; i < par->vbe_modes_cnt; i++) {
1461 h = par->vbe_modes[i].bytes_per_scan_line *
1462 par->vbe_modes[i].y_res;
1463 if (h > size_remap)
1464 size_remap = h;
1466 size_remap *= 2;
1469 * size_vmode -- that is the amount of memory needed for the
1470 * used video mode, i.e. the minimum amount of
1471 * memory we need.
1473 if (mode != NULL) {
1474 size_vmode = info->var.yres * mode->bytes_per_scan_line;
1475 } else {
1476 size_vmode = info->var.yres * info->var.xres *
1477 ((info->var.bits_per_pixel + 7) >> 3);
1481 * size_total -- all video memory we have. Used for mtrr
1482 * entries, resource allocation and bounds
1483 * checking.
1485 size_total = par->vbe_ib.total_memory * 65536;
1486 if (vram_total)
1487 size_total = vram_total * 1024 * 1024;
1488 if (size_total < size_vmode)
1489 size_total = size_vmode;
1492 * size_remap -- the amount of video memory we are going to
1493 * use for vesafb. With modern cards it is no
1494 * option to simply use size_total as th
1495 * wastes plenty of kernel address space.
1497 if (vram_remap)
1498 size_remap = vram_remap * 1024 * 1024;
1499 if (size_remap < size_vmode)
1500 size_remap = size_vmode;
1501 if (size_remap > size_total)
1502 size_remap = size_total;
1504 info->fix.smem_len = size_remap;
1505 info->fix.smem_start = mode->phys_base_ptr;
1508 * We have to set yres_virtual here because when setup_var() was
1509 * called, smem_len wasn't defined yet.
1511 info->var.yres_virtual = info->fix.smem_len /
1512 mode->bytes_per_scan_line;
1514 if (par->ypan && info->var.yres_virtual > info->var.yres) {
1515 printk(KERN_INFO "uvesafb: scrolling: %s "
1516 "using protected mode interface, "
1517 "yres_virtual=%d\n",
1518 (par->ypan > 1) ? "ywrap" : "ypan",
1519 info->var.yres_virtual);
1520 } else {
1521 printk(KERN_INFO "uvesafb: scrolling: redraw\n");
1522 info->var.yres_virtual = info->var.yres;
1523 par->ypan = 0;
1526 info->flags = FBINFO_FLAG_DEFAULT |
1527 (par->ypan ? FBINFO_HWACCEL_YPAN : 0);
1529 if (!par->ypan)
1530 info->fbops->fb_pan_display = NULL;
1533 static void __devinit uvesafb_init_mtrr(struct fb_info *info)
1535 #ifdef CONFIG_MTRR
1536 if (mtrr && !(info->fix.smem_start & (PAGE_SIZE - 1))) {
1537 int temp_size = info->fix.smem_len;
1538 unsigned int type = 0;
1540 switch (mtrr) {
1541 case 1:
1542 type = MTRR_TYPE_UNCACHABLE;
1543 break;
1544 case 2:
1545 type = MTRR_TYPE_WRBACK;
1546 break;
1547 case 3:
1548 type = MTRR_TYPE_WRCOMB;
1549 break;
1550 case 4:
1551 type = MTRR_TYPE_WRTHROUGH;
1552 break;
1553 default:
1554 type = 0;
1555 break;
1558 if (type) {
1559 int rc;
1561 /* Find the largest power-of-two */
1562 temp_size = roundup_pow_of_two(temp_size);
1564 /* Try and find a power of two to add */
1565 do {
1566 rc = mtrr_add(info->fix.smem_start,
1567 temp_size, type, 1);
1568 temp_size >>= 1;
1569 } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
1572 #endif /* CONFIG_MTRR */
1575 static void __devinit uvesafb_ioremap(struct fb_info *info)
1577 #ifdef CONFIG_X86
1578 switch (mtrr) {
1579 case 1: /* uncachable */
1580 info->screen_base = ioremap_nocache(info->fix.smem_start, info->fix.smem_len);
1581 break;
1582 case 2: /* write-back */
1583 info->screen_base = ioremap_cache(info->fix.smem_start, info->fix.smem_len);
1584 break;
1585 case 3: /* write-combining */
1586 info->screen_base = ioremap_wc(info->fix.smem_start, info->fix.smem_len);
1587 break;
1588 case 4: /* write-through */
1589 default:
1590 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
1591 break;
1593 #else
1594 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
1595 #endif /* CONFIG_X86 */
1598 static ssize_t uvesafb_show_vbe_ver(struct device *dev,
1599 struct device_attribute *attr, char *buf)
1601 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1602 struct uvesafb_par *par = info->par;
1604 return snprintf(buf, PAGE_SIZE, "%.4x\n", par->vbe_ib.vbe_version);
1607 static DEVICE_ATTR(vbe_version, S_IRUGO, uvesafb_show_vbe_ver, NULL);
1609 static ssize_t uvesafb_show_vbe_modes(struct device *dev,
1610 struct device_attribute *attr, char *buf)
1612 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1613 struct uvesafb_par *par = info->par;
1614 int ret = 0, i;
1616 for (i = 0; i < par->vbe_modes_cnt && ret < PAGE_SIZE; i++) {
1617 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1618 "%dx%d-%d, 0x%.4x\n",
1619 par->vbe_modes[i].x_res, par->vbe_modes[i].y_res,
1620 par->vbe_modes[i].depth, par->vbe_modes[i].mode_id);
1623 return ret;
1626 static DEVICE_ATTR(vbe_modes, S_IRUGO, uvesafb_show_vbe_modes, NULL);
1628 static ssize_t uvesafb_show_vendor(struct device *dev,
1629 struct device_attribute *attr, char *buf)
1631 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1632 struct uvesafb_par *par = info->par;
1634 if (par->vbe_ib.oem_vendor_name_ptr)
1635 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1636 (&par->vbe_ib) + par->vbe_ib.oem_vendor_name_ptr);
1637 else
1638 return 0;
1641 static DEVICE_ATTR(oem_vendor, S_IRUGO, uvesafb_show_vendor, NULL);
1643 static ssize_t uvesafb_show_product_name(struct device *dev,
1644 struct device_attribute *attr, char *buf)
1646 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1647 struct uvesafb_par *par = info->par;
1649 if (par->vbe_ib.oem_product_name_ptr)
1650 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1651 (&par->vbe_ib) + par->vbe_ib.oem_product_name_ptr);
1652 else
1653 return 0;
1656 static DEVICE_ATTR(oem_product_name, S_IRUGO, uvesafb_show_product_name, NULL);
1658 static ssize_t uvesafb_show_product_rev(struct device *dev,
1659 struct device_attribute *attr, char *buf)
1661 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1662 struct uvesafb_par *par = info->par;
1664 if (par->vbe_ib.oem_product_rev_ptr)
1665 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1666 (&par->vbe_ib) + par->vbe_ib.oem_product_rev_ptr);
1667 else
1668 return 0;
1671 static DEVICE_ATTR(oem_product_rev, S_IRUGO, uvesafb_show_product_rev, NULL);
1673 static ssize_t uvesafb_show_oem_string(struct device *dev,
1674 struct device_attribute *attr, char *buf)
1676 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1677 struct uvesafb_par *par = info->par;
1679 if (par->vbe_ib.oem_string_ptr)
1680 return snprintf(buf, PAGE_SIZE, "%s\n",
1681 (char *)(&par->vbe_ib) + par->vbe_ib.oem_string_ptr);
1682 else
1683 return 0;
1686 static DEVICE_ATTR(oem_string, S_IRUGO, uvesafb_show_oem_string, NULL);
1688 static ssize_t uvesafb_show_nocrtc(struct device *dev,
1689 struct device_attribute *attr, char *buf)
1691 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1692 struct uvesafb_par *par = info->par;
1694 return snprintf(buf, PAGE_SIZE, "%d\n", par->nocrtc);
1697 static ssize_t uvesafb_store_nocrtc(struct device *dev,
1698 struct device_attribute *attr, const char *buf, size_t count)
1700 struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1701 struct uvesafb_par *par = info->par;
1703 if (count > 0) {
1704 if (buf[0] == '0')
1705 par->nocrtc = 0;
1706 else
1707 par->nocrtc = 1;
1709 return count;
1712 static DEVICE_ATTR(nocrtc, S_IRUGO | S_IWUSR, uvesafb_show_nocrtc,
1713 uvesafb_store_nocrtc);
1715 static struct attribute *uvesafb_dev_attrs[] = {
1716 &dev_attr_vbe_version.attr,
1717 &dev_attr_vbe_modes.attr,
1718 &dev_attr_oem_vendor.attr,
1719 &dev_attr_oem_product_name.attr,
1720 &dev_attr_oem_product_rev.attr,
1721 &dev_attr_oem_string.attr,
1722 &dev_attr_nocrtc.attr,
1723 NULL,
1726 static struct attribute_group uvesafb_dev_attgrp = {
1727 .name = NULL,
1728 .attrs = uvesafb_dev_attrs,
1731 static int __devinit uvesafb_probe(struct platform_device *dev)
1733 struct fb_info *info;
1734 struct vbe_mode_ib *mode = NULL;
1735 struct uvesafb_par *par;
1736 int err = 0, i;
1738 info = framebuffer_alloc(sizeof(*par) + sizeof(u32) * 256, &dev->dev);
1739 if (!info)
1740 return -ENOMEM;
1742 par = info->par;
1744 err = uvesafb_vbe_init(info);
1745 if (err) {
1746 printk(KERN_ERR "uvesafb: vbe_init() failed with %d\n", err);
1747 goto out;
1750 info->fbops = &uvesafb_ops;
1752 i = uvesafb_vbe_init_mode(info);
1753 if (i < 0) {
1754 err = -EINVAL;
1755 goto out;
1756 } else {
1757 mode = &par->vbe_modes[i];
1760 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1761 err = -ENXIO;
1762 goto out;
1765 uvesafb_init_info(info, mode);
1767 if (!request_region(0x3c0, 32, "uvesafb")) {
1768 printk(KERN_ERR "uvesafb: request region 0x3c0-0x3e0 failed\n");
1769 err = -EIO;
1770 goto out_mode;
1773 if (!request_mem_region(info->fix.smem_start, info->fix.smem_len,
1774 "uvesafb")) {
1775 printk(KERN_ERR "uvesafb: cannot reserve video memory at "
1776 "0x%lx\n", info->fix.smem_start);
1777 err = -EIO;
1778 goto out_reg;
1781 uvesafb_init_mtrr(info);
1782 uvesafb_ioremap(info);
1784 if (!info->screen_base) {
1785 printk(KERN_ERR
1786 "uvesafb: abort, cannot ioremap 0x%x bytes of video "
1787 "memory at 0x%lx\n",
1788 info->fix.smem_len, info->fix.smem_start);
1789 err = -EIO;
1790 goto out_mem;
1793 platform_set_drvdata(dev, info);
1795 if (register_framebuffer(info) < 0) {
1796 printk(KERN_ERR
1797 "uvesafb: failed to register framebuffer device\n");
1798 err = -EINVAL;
1799 goto out_unmap;
1802 printk(KERN_INFO "uvesafb: framebuffer at 0x%lx, mapped to 0x%p, "
1803 "using %dk, total %dk\n", info->fix.smem_start,
1804 info->screen_base, info->fix.smem_len/1024,
1805 par->vbe_ib.total_memory * 64);
1806 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
1807 info->fix.id);
1809 err = sysfs_create_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1810 if (err != 0)
1811 printk(KERN_WARNING "fb%d: failed to register attributes\n",
1812 info->node);
1814 return 0;
1816 out_unmap:
1817 iounmap(info->screen_base);
1818 out_mem:
1819 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1820 out_reg:
1821 release_region(0x3c0, 32);
1822 out_mode:
1823 if (!list_empty(&info->modelist))
1824 fb_destroy_modelist(&info->modelist);
1825 fb_destroy_modedb(info->monspecs.modedb);
1826 fb_dealloc_cmap(&info->cmap);
1827 out:
1828 if (par->vbe_modes)
1829 kfree(par->vbe_modes);
1831 framebuffer_release(info);
1832 return err;
1835 static int uvesafb_remove(struct platform_device *dev)
1837 struct fb_info *info = platform_get_drvdata(dev);
1839 if (info) {
1840 struct uvesafb_par *par = info->par;
1842 sysfs_remove_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1843 unregister_framebuffer(info);
1844 release_region(0x3c0, 32);
1845 iounmap(info->screen_base);
1846 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1847 fb_destroy_modedb(info->monspecs.modedb);
1848 fb_dealloc_cmap(&info->cmap);
1850 if (par) {
1851 if (par->vbe_modes)
1852 kfree(par->vbe_modes);
1853 if (par->vbe_state_orig)
1854 kfree(par->vbe_state_orig);
1855 if (par->vbe_state_saved)
1856 kfree(par->vbe_state_saved);
1859 framebuffer_release(info);
1861 return 0;
1864 static struct platform_driver uvesafb_driver = {
1865 .probe = uvesafb_probe,
1866 .remove = uvesafb_remove,
1867 .driver = {
1868 .name = "uvesafb",
1872 static struct platform_device *uvesafb_device;
1874 #ifndef MODULE
1875 static int __devinit uvesafb_setup(char *options)
1877 char *this_opt;
1879 if (!options || !*options)
1880 return 0;
1882 while ((this_opt = strsep(&options, ",")) != NULL) {
1883 if (!*this_opt) continue;
1885 if (!strcmp(this_opt, "redraw"))
1886 ypan = 0;
1887 else if (!strcmp(this_opt, "ypan"))
1888 ypan = 1;
1889 else if (!strcmp(this_opt, "ywrap"))
1890 ypan = 2;
1891 else if (!strcmp(this_opt, "vgapal"))
1892 pmi_setpal = 0;
1893 else if (!strcmp(this_opt, "pmipal"))
1894 pmi_setpal = 1;
1895 else if (!strncmp(this_opt, "mtrr:", 5))
1896 mtrr = simple_strtoul(this_opt+5, NULL, 0);
1897 else if (!strcmp(this_opt, "nomtrr"))
1898 mtrr = 0;
1899 else if (!strcmp(this_opt, "nocrtc"))
1900 nocrtc = 1;
1901 else if (!strcmp(this_opt, "noedid"))
1902 noedid = 1;
1903 else if (!strcmp(this_opt, "noblank"))
1904 blank = 0;
1905 else if (!strncmp(this_opt, "vtotal:", 7))
1906 vram_total = simple_strtoul(this_opt + 7, NULL, 0);
1907 else if (!strncmp(this_opt, "vremap:", 7))
1908 vram_remap = simple_strtoul(this_opt + 7, NULL, 0);
1909 else if (!strncmp(this_opt, "maxhf:", 6))
1910 maxhf = simple_strtoul(this_opt + 6, NULL, 0);
1911 else if (!strncmp(this_opt, "maxvf:", 6))
1912 maxvf = simple_strtoul(this_opt + 6, NULL, 0);
1913 else if (!strncmp(this_opt, "maxclk:", 7))
1914 maxclk = simple_strtoul(this_opt + 7, NULL, 0);
1915 else if (!strncmp(this_opt, "vbemode:", 8))
1916 vbemode = simple_strtoul(this_opt + 8, NULL, 0);
1917 else if (this_opt[0] >= '0' && this_opt[0] <= '9') {
1918 mode_option = this_opt;
1919 } else {
1920 printk(KERN_WARNING
1921 "uvesafb: unrecognized option %s\n", this_opt);
1925 return 0;
1927 #endif /* !MODULE */
1929 static ssize_t show_v86d(struct device_driver *dev, char *buf)
1931 return snprintf(buf, PAGE_SIZE, "%s\n", v86d_path);
1934 static ssize_t store_v86d(struct device_driver *dev, const char *buf,
1935 size_t count)
1937 strncpy(v86d_path, buf, PATH_MAX);
1938 return count;
1941 static DRIVER_ATTR(v86d, S_IRUGO | S_IWUSR, show_v86d, store_v86d);
1943 static int __devinit uvesafb_init(void)
1945 int err;
1947 #ifndef MODULE
1948 char *option = NULL;
1950 if (fb_get_options("uvesafb", &option))
1951 return -ENODEV;
1952 uvesafb_setup(option);
1953 #endif
1954 err = cn_add_callback(&uvesafb_cn_id, "uvesafb", uvesafb_cn_callback);
1955 if (err)
1956 return err;
1958 err = platform_driver_register(&uvesafb_driver);
1960 if (!err) {
1961 uvesafb_device = platform_device_alloc("uvesafb", 0);
1962 if (uvesafb_device)
1963 err = platform_device_add(uvesafb_device);
1964 else
1965 err = -ENOMEM;
1967 if (err) {
1968 platform_device_put(uvesafb_device);
1969 platform_driver_unregister(&uvesafb_driver);
1970 cn_del_callback(&uvesafb_cn_id);
1971 return err;
1974 err = driver_create_file(&uvesafb_driver.driver,
1975 &driver_attr_v86d);
1976 if (err) {
1977 printk(KERN_WARNING "uvesafb: failed to register "
1978 "attributes\n");
1979 err = 0;
1982 return err;
1985 module_init(uvesafb_init);
1987 static void __devexit uvesafb_exit(void)
1989 struct uvesafb_ktask *task;
1991 if (v86d_started) {
1992 task = uvesafb_prep();
1993 if (task) {
1994 task->t.flags = TF_EXIT;
1995 uvesafb_exec(task);
1996 uvesafb_free(task);
2000 cn_del_callback(&uvesafb_cn_id);
2001 driver_remove_file(&uvesafb_driver.driver, &driver_attr_v86d);
2002 platform_device_unregister(uvesafb_device);
2003 platform_driver_unregister(&uvesafb_driver);
2006 module_exit(uvesafb_exit);
2008 static int param_set_scroll(const char *val, const struct kernel_param *kp)
2010 ypan = 0;
2012 if (!strcmp(val, "redraw"))
2013 ypan = 0;
2014 else if (!strcmp(val, "ypan"))
2015 ypan = 1;
2016 else if (!strcmp(val, "ywrap"))
2017 ypan = 2;
2018 else
2019 return -EINVAL;
2021 return 0;
2023 static struct kernel_param_ops param_ops_scroll = {
2024 .set = param_set_scroll,
2026 #define param_check_scroll(name, p) __param_check(name, p, void)
2028 module_param_named(scroll, ypan, scroll, 0);
2029 MODULE_PARM_DESC(scroll,
2030 "Scrolling mode, set to 'redraw', 'ypan', or 'ywrap'");
2031 module_param_named(vgapal, pmi_setpal, invbool, 0);
2032 MODULE_PARM_DESC(vgapal, "Set palette using VGA registers");
2033 module_param_named(pmipal, pmi_setpal, bool, 0);
2034 MODULE_PARM_DESC(pmipal, "Set palette using PMI calls");
2035 module_param(mtrr, uint, 0);
2036 MODULE_PARM_DESC(mtrr,
2037 "Memory Type Range Registers setting. Use 0 to disable.");
2038 module_param(blank, bool, 0);
2039 MODULE_PARM_DESC(blank, "Enable hardware blanking");
2040 module_param(nocrtc, bool, 0);
2041 MODULE_PARM_DESC(nocrtc, "Ignore CRTC timings when setting modes");
2042 module_param(noedid, bool, 0);
2043 MODULE_PARM_DESC(noedid,
2044 "Ignore EDID-provided monitor limits when setting modes");
2045 module_param(vram_remap, uint, 0);
2046 MODULE_PARM_DESC(vram_remap, "Set amount of video memory to be used [MiB]");
2047 module_param(vram_total, uint, 0);
2048 MODULE_PARM_DESC(vram_total, "Set total amount of video memoery [MiB]");
2049 module_param(maxclk, ushort, 0);
2050 MODULE_PARM_DESC(maxclk, "Maximum pixelclock [MHz], overrides EDID data");
2051 module_param(maxhf, ushort, 0);
2052 MODULE_PARM_DESC(maxhf,
2053 "Maximum horizontal frequency [kHz], overrides EDID data");
2054 module_param(maxvf, ushort, 0);
2055 MODULE_PARM_DESC(maxvf,
2056 "Maximum vertical frequency [Hz], overrides EDID data");
2057 module_param(mode_option, charp, 0);
2058 MODULE_PARM_DESC(mode_option,
2059 "Specify initial video mode as \"<xres>x<yres>[-<bpp>][@<refresh>]\"");
2060 module_param(vbemode, ushort, 0);
2061 MODULE_PARM_DESC(vbemode,
2062 "VBE mode number to set, overrides the 'mode' option");
2063 module_param_string(v86d, v86d_path, PATH_MAX, 0660);
2064 MODULE_PARM_DESC(v86d, "Path to the v86d userspace helper.");
2066 MODULE_LICENSE("GPL");
2067 MODULE_AUTHOR("Michal Januszewski <spock@gentoo.org>");
2068 MODULE_DESCRIPTION("Framebuffer driver for VBE2.0+ compliant graphics boards");