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