uvesafb: change mode parameter to mode_option
[linux-flexiantxendom0.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_ERR "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                         return -EINVAL;
523                 }
524
525                 mib = task->buf;
526                 mib->mode_id = *mode;
527
528                 /*
529                  * We only want modes that are supported with the current
530                  * hardware configuration, color, graphics and that have
531                  * support for the LFB.
532                  */
533                 if ((mib->mode_attr & VBE_MODE_MASK) == VBE_MODE_MASK &&
534                                  mib->bits_per_pixel >= 8)
535                         off++;
536                 else
537                         par->vbe_modes_cnt--;
538
539                 mode++;
540                 mib->depth = mib->red_len + mib->green_len + mib->blue_len;
541
542                 /*
543                  * Handle 8bpp modes and modes with broken color component
544                  * lengths.
545                  */
546                 if (mib->depth == 0 || (mib->depth == 24 &&
547                                         mib->bits_per_pixel == 32))
548                         mib->depth = mib->bits_per_pixel;
549         }
550
551         return 0;
552 }
553
554 /*
555  * The Protected Mode Interface is 32-bit x86 code, so we only run it on
556  * x86 and not x86_64.
557  */
558 #ifdef CONFIG_X86_32
559 static int __devinit uvesafb_vbe_getpmi(struct uvesafb_ktask *task,
560                 struct uvesafb_par *par)
561 {
562         int i, err;
563
564         uvesafb_reset(task);
565         task->t.regs.eax = 0x4f0a;
566         task->t.regs.ebx = 0x0;
567         err = uvesafb_exec(task);
568
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);
582
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");
589
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;
594                         }
595                 }
596         }
597         return 0;
598 }
599 #endif /* CONFIG_X86_32 */
600
601 /*
602  * Check whether a video mode is supported by the Video BIOS and is
603  * compatible with the monitor limits.
604  */
605 static int __devinit uvesafb_is_valid_mode(struct fb_videomode *mode,
606                 struct fb_info *info)
607 {
608         if (info->monspecs.gtf) {
609                 fb_videomode_to_var(&info->var, mode);
610                 if (fb_validate_mode(&info->var, info))
611                         return 0;
612         }
613
614         if (uvesafb_vbe_find_mode(info->par, mode->xres, mode->yres, 8,
615                                 UVESAFB_EXACT_RES) == -1)
616                 return 0;
617
618         return 1;
619 }
620
621 static int __devinit uvesafb_vbe_getedid(struct uvesafb_ktask *task,
622                 struct fb_info *info)
623 {
624         struct uvesafb_par *par = info->par;
625         int err = 0;
626
627         if (noedid || par->vbe_ib.vbe_version < 0x0300)
628                 return -EINVAL;
629
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;
635
636         err = uvesafb_exec(task);
637
638         if ((task->t.regs.eax & 0xffff) != 0x004f || err)
639                 return -EINVAL;
640
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;
654         }
655
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);
662
663         err = uvesafb_exec(task);
664
665         if ((task->t.regs.eax & 0xffff) == 0x004f && !err) {
666                 fb_edid_to_monspecs(task->buf, &info->monspecs);
667
668                 if (info->monspecs.vfmax && info->monspecs.hfmax) {
669                         /*
670                          * If the maximum pixel clock wasn't specified in
671                          * the EDID block, set it to 300 MHz.
672                          */
673                         if (info->monspecs.dclkmax == 0)
674                                 info->monspecs.dclkmax = 300 * 1000000;
675                         info->monspecs.gtf = 1;
676                 }
677         } else {
678                 err = -EINVAL;
679         }
680
681         kfree(task->buf);
682         return err;
683 }
684
685 static void __devinit uvesafb_vbe_getmonspecs(struct uvesafb_ktask *task,
686                 struct fb_info *info)
687 {
688         struct uvesafb_par *par = info->par;
689         int i;
690
691         memset(&info->monspecs, 0, sizeof(info->monspecs));
692
693         /*
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.
697          */
698         if (uvesafb_vbe_getedid(task, info)) {
699                 info->monspecs.gtf = 0;
700                 par->nocrtc = 1;
701         }
702
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;
710
711         /*
712          * In case DDC transfers are not supported, the user can provide
713          * monitor limits manually. Lower limits are set to "safe" values.
714          */
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;
721         }
722
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");
732
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;
738
739                 mode = &par->vbe_modes[i];
740                 memset(&var, 0, sizeof(var));
741
742                 var.xres = mode->x_res;
743                 var.yres = mode->y_res;
744
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);
748         }
749
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);
755         }
756
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);
761         }
762
763         return;
764 }
765
766 static void __devinit uvesafb_vbe_getstatesize(struct uvesafb_ktask *task,
767                 struct uvesafb_par *par)
768 {
769         int err;
770
771         uvesafb_reset(task);
772
773         /*
774          * Get the VBE state buffer size. We want all available
775          * hardware state data (CL = 0x0f).
776          */
777         task->t.regs.eax = 0x4f04;
778         task->t.regs.ecx = 0x000f;
779         task->t.regs.edx = 0x0000;
780         task->t.flags = 0;
781
782         err = uvesafb_exec(task);
783
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;
790         }
791
792         par->vbe_state_size = 64 * (task->t.regs.ebx & 0xffff);
793 }
794
795 static int __devinit uvesafb_vbe_init(struct fb_info *info)
796 {
797         struct uvesafb_ktask *task = NULL;
798         struct uvesafb_par *par = info->par;
799         int err;
800
801         task = uvesafb_prep();
802         if (!task)
803                 return -ENOMEM;
804
805         err = uvesafb_vbe_getinfo(task, par);
806         if (err)
807                 goto out;
808
809         err = uvesafb_vbe_getmodes(task, par);
810         if (err)
811                 goto out;
812
813         par->nocrtc = nocrtc;
814 #ifdef CONFIG_X86_32
815         par->pmi_setpal = pmi_setpal;
816         par->ypan = ypan;
817
818         if (par->pmi_setpal || par->ypan)
819                 uvesafb_vbe_getpmi(task, par);
820 #else
821         /* The protected mode interface is not available on non-x86. */
822         par->pmi_setpal = par->ypan = 0;
823 #endif
824
825         INIT_LIST_HEAD(&info->modelist);
826         uvesafb_vbe_getmonspecs(task, info);
827         uvesafb_vbe_getstatesize(task, par);
828
829 out:    uvesafb_free(task);
830         return err;
831 }
832
833 static int __devinit uvesafb_vbe_init_mode(struct fb_info *info)
834 {
835         struct list_head *pos;
836         struct fb_modelist *modelist;
837         struct fb_videomode *mode;
838         struct uvesafb_par *par = info->par;
839         int i, modeid;
840
841         /* Has the user requested a specific VESA mode? */
842         if (vbemode) {
843                 for (i = 0; i < par->vbe_modes_cnt; i++) {
844                         if (par->vbe_modes[i].mode_id == vbemode) {
845                                 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
846                                                         &info->var, info);
847                                 /*
848                                  * With pixclock set to 0, the default BIOS
849                                  * timings will be used in set_par().
850                                  */
851                                 info->var.pixclock = 0;
852                                 modeid = i;
853                                 goto gotmode;
854                         }
855                 }
856                 printk(KERN_INFO "uvesafb: requested VBE mode 0x%x is "
857                                  "unavailable\n", vbemode);
858                 vbemode = 0;
859         }
860
861         /* Count the modes in the modelist */
862         i = 0;
863         list_for_each(pos, &info->modelist)
864                 i++;
865
866         /*
867          * Convert the modelist into a modedb so that we can use it with
868          * fb_find_mode().
869          */
870         mode = kzalloc(i * sizeof(*mode), GFP_KERNEL);
871         if (mode) {
872                 i = 0;
873                 list_for_each(pos, &info->modelist) {
874                         modelist = list_entry(pos, struct fb_modelist, list);
875                         mode[i] = modelist->mode;
876                         i++;
877                 }
878
879                 if (!mode_option)
880                         mode_option = UVESAFB_DEFAULT_MODE;
881
882                 i = fb_find_mode(&info->var, info, mode_option, mode, i,
883                         NULL, 8);
884
885                 kfree(mode);
886         }
887
888         /* fb_find_mode() failed */
889         if (i == 0) {
890                 info->var.xres = 640;
891                 info->var.yres = 480;
892                 mode = (struct fb_videomode *)
893                                 fb_find_best_mode(&info->var, &info->modelist);
894
895                 if (mode) {
896                         fb_videomode_to_var(&info->var, mode);
897                 } else {
898                         modeid = par->vbe_modes[0].mode_id;
899                         fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
900                                     &info->var, info);
901                         goto gotmode;
902                 }
903         }
904
905         /* Look for a matching VBE mode. */
906         modeid = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres,
907                         info->var.bits_per_pixel, UVESAFB_EXACT_RES);
908
909         if (modeid == -1)
910                 return -EINVAL;
911
912 gotmode:
913         uvesafb_setup_var(&info->var, info, &par->vbe_modes[modeid]);
914
915         /*
916          * If we are not VBE3.0+ compliant, we're done -- the BIOS will
917          * ignore our timings anyway.
918          */
919         if (par->vbe_ib.vbe_version < 0x0300 || par->nocrtc)
920                 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60,
921                                         &info->var, info);
922
923         return modeid;
924 }
925
926 static int uvesafb_setpalette(struct uvesafb_pal_entry *entries, int count,
927                 int start, struct fb_info *info)
928 {
929         struct uvesafb_ktask *task;
930 #ifdef CONFIG_X86
931         struct uvesafb_par *par = info->par;
932         int i = par->mode_idx;
933 #endif
934         int err = 0;
935
936         /*
937          * We support palette modifications for 8 bpp modes only, so
938          * there can never be more than 256 entries.
939          */
940         if (start + count > 256)
941                 return -EINVAL;
942
943 #ifdef CONFIG_X86
944         /* Use VGA registers if mode is VGA-compatible. */
945         if (i >= 0 && i < par->vbe_modes_cnt &&
946             par->vbe_modes[i].mode_attr & VBE_MODE_VGACOMPAT) {
947                 for (i = 0; i < count; i++) {
948                         outb_p(start + i,        dac_reg);
949                         outb_p(entries[i].red,   dac_val);
950                         outb_p(entries[i].green, dac_val);
951                         outb_p(entries[i].blue,  dac_val);
952                 }
953         }
954 #ifdef CONFIG_X86_32
955         else if (par->pmi_setpal) {
956                 __asm__ __volatile__(
957                 "call *(%%esi)"
958                 : /* no return value */
959                 : "a" (0x4f09),         /* EAX */
960                   "b" (0),              /* EBX */
961                   "c" (count),          /* ECX */
962                   "d" (start),          /* EDX */
963                   "D" (entries),        /* EDI */
964                   "S" (&par->pmi_pal)); /* ESI */
965         }
966 #endif /* CONFIG_X86_32 */
967         else
968 #endif /* CONFIG_X86 */
969         {
970                 task = uvesafb_prep();
971                 if (!task)
972                         return -ENOMEM;
973
974                 task->t.regs.eax = 0x4f09;
975                 task->t.regs.ebx = 0x0;
976                 task->t.regs.ecx = count;
977                 task->t.regs.edx = start;
978                 task->t.flags = TF_BUF_ESDI;
979                 task->t.buf_len = sizeof(struct uvesafb_pal_entry) * count;
980                 task->buf = entries;
981
982                 err = uvesafb_exec(task);
983                 if ((task->t.regs.eax & 0xffff) != 0x004f)
984                         err = 1;
985
986                 uvesafb_free(task);
987         }
988         return err;
989 }
990
991 static int uvesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
992                 unsigned blue, unsigned transp,
993                 struct fb_info *info)
994 {
995         struct uvesafb_pal_entry entry;
996         int shift = 16 - info->var.green.length;
997         int err = 0;
998
999         if (regno >= info->cmap.len)
1000                 return -EINVAL;
1001
1002         if (info->var.bits_per_pixel == 8) {
1003                 entry.red   = red   >> shift;
1004                 entry.green = green >> shift;
1005                 entry.blue  = blue  >> shift;
1006                 entry.pad   = 0;
1007
1008                 err = uvesafb_setpalette(&entry, 1, regno, info);
1009         } else if (regno < 16) {
1010                 switch (info->var.bits_per_pixel) {
1011                 case 16:
1012                         if (info->var.red.offset == 10) {
1013                                 /* 1:5:5:5 */
1014                                 ((u32 *) (info->pseudo_palette))[regno] =
1015                                                 ((red   & 0xf800) >>  1) |
1016                                                 ((green & 0xf800) >>  6) |
1017                                                 ((blue  & 0xf800) >> 11);
1018                         } else {
1019                                 /* 0:5:6:5 */
1020                                 ((u32 *) (info->pseudo_palette))[regno] =
1021                                                 ((red   & 0xf800)      ) |
1022                                                 ((green & 0xfc00) >>  5) |
1023                                                 ((blue  & 0xf800) >> 11);
1024                         }
1025                         break;
1026
1027                 case 24:
1028                 case 32:
1029                         red   >>= 8;
1030                         green >>= 8;
1031                         blue  >>= 8;
1032                         ((u32 *)(info->pseudo_palette))[regno] =
1033                                 (red   << info->var.red.offset)   |
1034                                 (green << info->var.green.offset) |
1035                                 (blue  << info->var.blue.offset);
1036                         break;
1037                 }
1038         }
1039         return err;
1040 }
1041
1042 static int uvesafb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1043 {
1044         struct uvesafb_pal_entry *entries;
1045         int shift = 16 - info->var.green.length;
1046         int i, err = 0;
1047
1048         if (info->var.bits_per_pixel == 8) {
1049                 if (cmap->start + cmap->len > info->cmap.start +
1050                     info->cmap.len || cmap->start < info->cmap.start)
1051                         return -EINVAL;
1052
1053                 entries = kmalloc(sizeof(*entries) * cmap->len, GFP_KERNEL);
1054                 if (!entries)
1055                         return -ENOMEM;
1056
1057                 for (i = 0; i < cmap->len; i++) {
1058                         entries[i].red   = cmap->red[i]   >> shift;
1059                         entries[i].green = cmap->green[i] >> shift;
1060                         entries[i].blue  = cmap->blue[i]  >> shift;
1061                         entries[i].pad   = 0;
1062                 }
1063                 err = uvesafb_setpalette(entries, cmap->len, cmap->start, info);
1064                 kfree(entries);
1065         } else {
1066                 /*
1067                  * For modes with bpp > 8, we only set the pseudo palette in
1068                  * the fb_info struct. We rely on uvesafb_setcolreg to do all
1069                  * sanity checking.
1070                  */
1071                 for (i = 0; i < cmap->len; i++) {
1072                         err |= uvesafb_setcolreg(cmap->start + i, cmap->red[i],
1073                                                 cmap->green[i], cmap->blue[i],
1074                                                 0, info);
1075                 }
1076         }
1077         return err;
1078 }
1079
1080 static int uvesafb_pan_display(struct fb_var_screeninfo *var,
1081                 struct fb_info *info)
1082 {
1083 #ifdef CONFIG_X86_32
1084         int offset;
1085         struct uvesafb_par *par = info->par;
1086
1087         offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
1088
1089         /*
1090          * It turns out it's not the best idea to do panning via vm86,
1091          * so we only allow it if we have a PMI.
1092          */
1093         if (par->pmi_start) {
1094                 __asm__ __volatile__(
1095                         "call *(%%edi)"
1096                         : /* no return value */
1097                         : "a" (0x4f07),         /* EAX */
1098                           "b" (0),              /* EBX */
1099                           "c" (offset),         /* ECX */
1100                           "d" (offset >> 16),   /* EDX */
1101                           "D" (&par->pmi_start));    /* EDI */
1102         }
1103 #endif
1104         return 0;
1105 }
1106
1107 static int uvesafb_blank(int blank, struct fb_info *info)
1108 {
1109         struct uvesafb_ktask *task;
1110         int err = 1;
1111 #ifdef CONFIG_X86
1112         struct uvesafb_par *par = info->par;
1113
1114         if (par->vbe_ib.capabilities & VBE_CAP_VGACOMPAT) {
1115                 int loop = 10000;
1116                 u8 seq = 0, crtc17 = 0;
1117
1118                 if (blank == FB_BLANK_POWERDOWN) {
1119                         seq = 0x20;
1120                         crtc17 = 0x00;
1121                         err = 0;
1122                 } else {
1123                         seq = 0x00;
1124                         crtc17 = 0x80;
1125                         err = (blank == FB_BLANK_UNBLANK) ? 0 : -EINVAL;
1126                 }
1127
1128                 vga_wseq(NULL, 0x00, 0x01);
1129                 seq |= vga_rseq(NULL, 0x01) & ~0x20;
1130                 vga_wseq(NULL, 0x00, seq);
1131
1132                 crtc17 |= vga_rcrt(NULL, 0x17) & ~0x80;
1133                 while (loop--);
1134                 vga_wcrt(NULL, 0x17, crtc17);
1135                 vga_wseq(NULL, 0x00, 0x03);
1136         } else
1137 #endif /* CONFIG_X86 */
1138         {
1139                 task = uvesafb_prep();
1140                 if (!task)
1141                         return -ENOMEM;
1142
1143                 task->t.regs.eax = 0x4f10;
1144                 switch (blank) {
1145                 case FB_BLANK_UNBLANK:
1146                         task->t.regs.ebx = 0x0001;
1147                         break;
1148                 case FB_BLANK_NORMAL:
1149                         task->t.regs.ebx = 0x0101;      /* standby */
1150                         break;
1151                 case FB_BLANK_POWERDOWN:
1152                         task->t.regs.ebx = 0x0401;      /* powerdown */
1153                         break;
1154                 default:
1155                         goto out;
1156                 }
1157
1158                 err = uvesafb_exec(task);
1159                 if (err || (task->t.regs.eax & 0xffff) != 0x004f)
1160                         err = 1;
1161 out:            uvesafb_free(task);
1162         }
1163         return err;
1164 }
1165
1166 static int uvesafb_open(struct fb_info *info, int user)
1167 {
1168         struct uvesafb_par *par = info->par;
1169         int cnt = atomic_read(&par->ref_count);
1170
1171         if (!cnt && par->vbe_state_size)
1172                 par->vbe_state_orig = uvesafb_vbe_state_save(par);
1173
1174         atomic_inc(&par->ref_count);
1175         return 0;
1176 }
1177
1178 static int uvesafb_release(struct fb_info *info, int user)
1179 {
1180         struct uvesafb_ktask *task = NULL;
1181         struct uvesafb_par *par = info->par;
1182         int cnt = atomic_read(&par->ref_count);
1183
1184         if (!cnt)
1185                 return -EINVAL;
1186
1187         if (cnt != 1)
1188                 goto out;
1189
1190         task = uvesafb_prep();
1191         if (!task)
1192                 goto out;
1193
1194         /* First, try to set the standard 80x25 text mode. */
1195         task->t.regs.eax = 0x0003;
1196         uvesafb_exec(task);
1197
1198         /*
1199          * Now try to restore whatever hardware state we might have
1200          * saved when the fb device was first opened.
1201          */
1202         uvesafb_vbe_state_restore(par, par->vbe_state_orig);
1203 out:
1204         atomic_dec(&par->ref_count);
1205         if (task)
1206                 uvesafb_free(task);
1207         return 0;
1208 }
1209
1210 static int uvesafb_set_par(struct fb_info *info)
1211 {
1212         struct uvesafb_par *par = info->par;
1213         struct uvesafb_ktask *task = NULL;
1214         struct vbe_crtc_ib *crtc = NULL;
1215         struct vbe_mode_ib *mode = NULL;
1216         int i, err = 0, depth = info->var.bits_per_pixel;
1217
1218         if (depth > 8 && depth != 32)
1219                 depth = info->var.red.length + info->var.green.length +
1220                         info->var.blue.length;
1221
1222         i = uvesafb_vbe_find_mode(par, info->var.xres, info->var.yres, depth,
1223                                  UVESAFB_EXACT_RES | UVESAFB_EXACT_DEPTH);
1224         if (i >= 0)
1225                 mode = &par->vbe_modes[i];
1226         else
1227                 return -EINVAL;
1228
1229         task = uvesafb_prep();
1230         if (!task)
1231                 return -ENOMEM;
1232 setmode:
1233         task->t.regs.eax = 0x4f02;
1234         task->t.regs.ebx = mode->mode_id | 0x4000;      /* use LFB */
1235
1236         if (par->vbe_ib.vbe_version >= 0x0300 && !par->nocrtc &&
1237             info->var.pixclock != 0) {
1238                 task->t.regs.ebx |= 0x0800;             /* use CRTC data */
1239                 task->t.flags = TF_BUF_ESDI;
1240                 crtc = kzalloc(sizeof(struct vbe_crtc_ib), GFP_KERNEL);
1241                 if (!crtc) {
1242                         err = -ENOMEM;
1243                         goto out;
1244                 }
1245                 crtc->horiz_start = info->var.xres + info->var.right_margin;
1246                 crtc->horiz_end   = crtc->horiz_start + info->var.hsync_len;
1247                 crtc->horiz_total = crtc->horiz_end + info->var.left_margin;
1248
1249                 crtc->vert_start  = info->var.yres + info->var.lower_margin;
1250                 crtc->vert_end    = crtc->vert_start + info->var.vsync_len;
1251                 crtc->vert_total  = crtc->vert_end + info->var.upper_margin;
1252
1253                 crtc->pixel_clock = PICOS2KHZ(info->var.pixclock) * 1000;
1254                 crtc->refresh_rate = (u16)(100 * (crtc->pixel_clock /
1255                                 (crtc->vert_total * crtc->horiz_total)));
1256
1257                 if (info->var.vmode & FB_VMODE_DOUBLE)
1258                         crtc->flags |= 0x1;
1259                 if (info->var.vmode & FB_VMODE_INTERLACED)
1260                         crtc->flags |= 0x2;
1261                 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
1262                         crtc->flags |= 0x4;
1263                 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
1264                         crtc->flags |= 0x8;
1265                 memcpy(&par->crtc, crtc, sizeof(*crtc));
1266         } else {
1267                 memset(&par->crtc, 0, sizeof(*crtc));
1268         }
1269
1270         task->t.buf_len = sizeof(struct vbe_crtc_ib);
1271         task->buf = &par->crtc;
1272
1273         err = uvesafb_exec(task);
1274         if (err || (task->t.regs.eax & 0xffff) != 0x004f) {
1275                 /*
1276                  * The mode switch might have failed because we tried to
1277                  * use our own timings.  Try again with the default timings.
1278                  */
1279                 if (crtc != NULL) {
1280                         printk(KERN_WARNING "uvesafb: mode switch failed "
1281                                 "(eax=0x%x, err=%d). Trying again with "
1282                                 "default timings.\n", task->t.regs.eax, err);
1283                         uvesafb_reset(task);
1284                         kfree(crtc);
1285                         crtc = NULL;
1286                         info->var.pixclock = 0;
1287                         goto setmode;
1288                 } else {
1289                         printk(KERN_ERR "uvesafb: mode switch failed (eax="
1290                                 "0x%x, err=%d)\n", task->t.regs.eax, err);
1291                         err = -EINVAL;
1292                         goto out;
1293                 }
1294         }
1295         par->mode_idx = i;
1296
1297         /* For 8bpp modes, always try to set the DAC to 8 bits. */
1298         if (par->vbe_ib.capabilities & VBE_CAP_CAN_SWITCH_DAC &&
1299             mode->bits_per_pixel <= 8) {
1300                 uvesafb_reset(task);
1301                 task->t.regs.eax = 0x4f08;
1302                 task->t.regs.ebx = 0x0800;
1303
1304                 err = uvesafb_exec(task);
1305                 if (err || (task->t.regs.eax & 0xffff) != 0x004f ||
1306                     ((task->t.regs.ebx & 0xff00) >> 8) != 8) {
1307                         /*
1308                          * We've failed to set the DAC palette format -
1309                          * time to correct var.
1310                          */
1311                         info->var.red.length    = 6;
1312                         info->var.green.length  = 6;
1313                         info->var.blue.length   = 6;
1314                 }
1315         }
1316
1317         info->fix.visual = (info->var.bits_per_pixel == 8) ?
1318                                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1319         info->fix.line_length = mode->bytes_per_scan_line;
1320
1321 out:    if (crtc != NULL)
1322                 kfree(crtc);
1323         uvesafb_free(task);
1324
1325         return err;
1326 }
1327
1328 static void uvesafb_check_limits(struct fb_var_screeninfo *var,
1329                 struct fb_info *info)
1330 {
1331         const struct fb_videomode *mode;
1332         struct uvesafb_par *par = info->par;
1333
1334         /*
1335          * If pixclock is set to 0, then we're using default BIOS timings
1336          * and thus don't have to perform any checks here.
1337          */
1338         if (!var->pixclock)
1339                 return;
1340
1341         if (par->vbe_ib.vbe_version < 0x0300) {
1342                 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 60, var, info);
1343                 return;
1344         }
1345
1346         if (!fb_validate_mode(var, info))
1347                 return;
1348
1349         mode = fb_find_best_mode(var, &info->modelist);
1350         if (mode) {
1351                 if (mode->xres == var->xres && mode->yres == var->yres &&
1352                     !(mode->vmode & (FB_VMODE_INTERLACED | FB_VMODE_DOUBLE))) {
1353                         fb_videomode_to_var(var, mode);
1354                         return;
1355                 }
1356         }
1357
1358         if (info->monspecs.gtf && !fb_get_mode(FB_MAXTIMINGS, 0, var, info))
1359                 return;
1360         /* Use default refresh rate */
1361         var->pixclock = 0;
1362 }
1363
1364 static int uvesafb_check_var(struct fb_var_screeninfo *var,
1365                 struct fb_info *info)
1366 {
1367         struct uvesafb_par *par = info->par;
1368         struct vbe_mode_ib *mode = NULL;
1369         int match = -1;
1370         int depth = var->red.length + var->green.length + var->blue.length;
1371
1372         /*
1373          * Various apps will use bits_per_pixel to set the color depth,
1374          * which is theoretically incorrect, but which we'll try to handle
1375          * here.
1376          */
1377         if (depth == 0 || abs(depth - var->bits_per_pixel) >= 8)
1378                 depth = var->bits_per_pixel;
1379
1380         match = uvesafb_vbe_find_mode(par, var->xres, var->yres, depth,
1381                                                 UVESAFB_EXACT_RES);
1382         if (match == -1)
1383                 return -EINVAL;
1384
1385         mode = &par->vbe_modes[match];
1386         uvesafb_setup_var(var, info, mode);
1387
1388         /*
1389          * Check whether we have remapped enough memory for this mode.
1390          * We might be called at an early stage, when we haven't remapped
1391          * any memory yet, in which case we simply skip the check.
1392          */
1393         if (var->yres * mode->bytes_per_scan_line > info->fix.smem_len
1394                                                 && info->fix.smem_len)
1395                 return -EINVAL;
1396
1397         if ((var->vmode & FB_VMODE_DOUBLE) &&
1398                                 !(par->vbe_modes[match].mode_attr & 0x100))
1399                 var->vmode &= ~FB_VMODE_DOUBLE;
1400
1401         if ((var->vmode & FB_VMODE_INTERLACED) &&
1402                                 !(par->vbe_modes[match].mode_attr & 0x200))
1403                 var->vmode &= ~FB_VMODE_INTERLACED;
1404
1405         uvesafb_check_limits(var, info);
1406
1407         var->xres_virtual = var->xres;
1408         var->yres_virtual = (par->ypan) ?
1409                                 info->fix.smem_len / mode->bytes_per_scan_line :
1410                                 var->yres;
1411         return 0;
1412 }
1413
1414 static void uvesafb_save_state(struct fb_info *info)
1415 {
1416         struct uvesafb_par *par = info->par;
1417
1418         if (par->vbe_state_saved)
1419                 kfree(par->vbe_state_saved);
1420
1421         par->vbe_state_saved = uvesafb_vbe_state_save(par);
1422 }
1423
1424 static void uvesafb_restore_state(struct fb_info *info)
1425 {
1426         struct uvesafb_par *par = info->par;
1427
1428         uvesafb_vbe_state_restore(par, par->vbe_state_saved);
1429 }
1430
1431 static struct fb_ops uvesafb_ops = {
1432         .owner          = THIS_MODULE,
1433         .fb_open        = uvesafb_open,
1434         .fb_release     = uvesafb_release,
1435         .fb_setcolreg   = uvesafb_setcolreg,
1436         .fb_setcmap     = uvesafb_setcmap,
1437         .fb_pan_display = uvesafb_pan_display,
1438         .fb_blank       = uvesafb_blank,
1439         .fb_fillrect    = cfb_fillrect,
1440         .fb_copyarea    = cfb_copyarea,
1441         .fb_imageblit   = cfb_imageblit,
1442         .fb_check_var   = uvesafb_check_var,
1443         .fb_set_par     = uvesafb_set_par,
1444         .fb_save_state  = uvesafb_save_state,
1445         .fb_restore_state = uvesafb_restore_state,
1446 };
1447
1448 static void __devinit uvesafb_init_info(struct fb_info *info,
1449                 struct vbe_mode_ib *mode)
1450 {
1451         unsigned int size_vmode;
1452         unsigned int size_remap;
1453         unsigned int size_total;
1454         struct uvesafb_par *par = info->par;
1455         int i, h;
1456
1457         info->pseudo_palette = ((u8 *)info->par + sizeof(struct uvesafb_par));
1458         info->fix = uvesafb_fix;
1459         info->fix.ypanstep = par->ypan ? 1 : 0;
1460         info->fix.ywrapstep = (par->ypan > 1) ? 1 : 0;
1461
1462         /*
1463          * If we were unable to get the state buffer size, disable
1464          * functions for saving and restoring the hardware state.
1465          */
1466         if (par->vbe_state_size == 0) {
1467                 info->fbops->fb_save_state = NULL;
1468                 info->fbops->fb_restore_state = NULL;
1469         }
1470
1471         /* Disable blanking if the user requested so. */
1472         if (!blank)
1473                 info->fbops->fb_blank = NULL;
1474
1475         /*
1476          * Find out how much IO memory is required for the mode with
1477          * the highest resolution.
1478          */
1479         size_remap = 0;
1480         for (i = 0; i < par->vbe_modes_cnt; i++) {
1481                 h = par->vbe_modes[i].bytes_per_scan_line *
1482                                         par->vbe_modes[i].y_res;
1483                 if (h > size_remap)
1484                         size_remap = h;
1485         }
1486         size_remap *= 2;
1487
1488         /*
1489          *   size_vmode -- that is the amount of memory needed for the
1490          *                 used video mode, i.e. the minimum amount of
1491          *                 memory we need.
1492          */
1493         if (mode != NULL) {
1494                 size_vmode = info->var.yres * mode->bytes_per_scan_line;
1495         } else {
1496                 size_vmode = info->var.yres * info->var.xres *
1497                              ((info->var.bits_per_pixel + 7) >> 3);
1498         }
1499
1500         /*
1501          *   size_total -- all video memory we have. Used for mtrr
1502          *                 entries, resource allocation and bounds
1503          *                 checking.
1504          */
1505         size_total = par->vbe_ib.total_memory * 65536;
1506         if (vram_total)
1507                 size_total = vram_total * 1024 * 1024;
1508         if (size_total < size_vmode)
1509                 size_total = size_vmode;
1510
1511         /*
1512          *   size_remap -- the amount of video memory we are going to
1513          *                 use for vesafb.  With modern cards it is no
1514          *                 option to simply use size_total as th
1515          *                 wastes plenty of kernel address space.
1516          */
1517         if (vram_remap)
1518                 size_remap = vram_remap * 1024 * 1024;
1519         if (size_remap < size_vmode)
1520                 size_remap = size_vmode;
1521         if (size_remap > size_total)
1522                 size_remap = size_total;
1523
1524         info->fix.smem_len = size_remap;
1525         info->fix.smem_start = mode->phys_base_ptr;
1526
1527         /*
1528          * We have to set yres_virtual here because when setup_var() was
1529          * called, smem_len wasn't defined yet.
1530          */
1531         info->var.yres_virtual = info->fix.smem_len /
1532                                  mode->bytes_per_scan_line;
1533
1534         if (par->ypan && info->var.yres_virtual > info->var.yres) {
1535                 printk(KERN_INFO "uvesafb: scrolling: %s "
1536                         "using protected mode interface, "
1537                         "yres_virtual=%d\n",
1538                         (par->ypan > 1) ? "ywrap" : "ypan",
1539                         info->var.yres_virtual);
1540         } else {
1541                 printk(KERN_INFO "uvesafb: scrolling: redraw\n");
1542                 info->var.yres_virtual = info->var.yres;
1543                 par->ypan = 0;
1544         }
1545
1546         info->flags = FBINFO_FLAG_DEFAULT |
1547                         (par->ypan) ? FBINFO_HWACCEL_YPAN : 0;
1548
1549         if (!par->ypan)
1550                 info->fbops->fb_pan_display = NULL;
1551 }
1552
1553 static void __devinit uvesafb_init_mtrr(struct fb_info *info)
1554 {
1555 #ifdef CONFIG_MTRR
1556         if (mtrr && !(info->fix.smem_start & (PAGE_SIZE - 1))) {
1557                 int temp_size = info->fix.smem_len;
1558                 unsigned int type = 0;
1559
1560                 switch (mtrr) {
1561                 case 1:
1562                         type = MTRR_TYPE_UNCACHABLE;
1563                         break;
1564                 case 2:
1565                         type = MTRR_TYPE_WRBACK;
1566                         break;
1567                 case 3:
1568                         type = MTRR_TYPE_WRCOMB;
1569                         break;
1570                 case 4:
1571                         type = MTRR_TYPE_WRTHROUGH;
1572                         break;
1573                 default:
1574                         type = 0;
1575                         break;
1576                 }
1577
1578                 if (type) {
1579                         int rc;
1580
1581                         /* Find the largest power-of-two */
1582                         while (temp_size & (temp_size - 1))
1583                                 temp_size &= (temp_size - 1);
1584
1585                         /* Try and find a power of two to add */
1586                         do {
1587                                 rc = mtrr_add(info->fix.smem_start,
1588                                               temp_size, type, 1);
1589                                 temp_size >>= 1;
1590                         } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
1591                 }
1592         }
1593 #endif /* CONFIG_MTRR */
1594 }
1595
1596
1597 static ssize_t uvesafb_show_vbe_ver(struct device *dev,
1598                 struct device_attribute *attr, char *buf)
1599 {
1600         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1601         struct uvesafb_par *par = info->par;
1602
1603         return snprintf(buf, PAGE_SIZE, "%.4x\n", par->vbe_ib.vbe_version);
1604 }
1605
1606 static DEVICE_ATTR(vbe_version, S_IRUGO, uvesafb_show_vbe_ver, NULL);
1607
1608 static ssize_t uvesafb_show_vbe_modes(struct device *dev,
1609                 struct device_attribute *attr, char *buf)
1610 {
1611         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1612         struct uvesafb_par *par = info->par;
1613         int ret = 0, i;
1614
1615         for (i = 0; i < par->vbe_modes_cnt && ret < PAGE_SIZE; i++) {
1616                 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1617                         "%dx%d-%d, 0x%.4x\n",
1618                         par->vbe_modes[i].x_res, par->vbe_modes[i].y_res,
1619                         par->vbe_modes[i].depth, par->vbe_modes[i].mode_id);
1620         }
1621
1622         return ret;
1623 }
1624
1625 static DEVICE_ATTR(vbe_modes, S_IRUGO, uvesafb_show_vbe_modes, NULL);
1626
1627 static ssize_t uvesafb_show_vendor(struct device *dev,
1628                 struct device_attribute *attr, char *buf)
1629 {
1630         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1631         struct uvesafb_par *par = info->par;
1632
1633         if (par->vbe_ib.oem_vendor_name_ptr)
1634                 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1635                         (&par->vbe_ib) + par->vbe_ib.oem_vendor_name_ptr);
1636         else
1637                 return 0;
1638 }
1639
1640 static DEVICE_ATTR(oem_vendor, S_IRUGO, uvesafb_show_vendor, NULL);
1641
1642 static ssize_t uvesafb_show_product_name(struct device *dev,
1643                 struct device_attribute *attr, char *buf)
1644 {
1645         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1646         struct uvesafb_par *par = info->par;
1647
1648         if (par->vbe_ib.oem_product_name_ptr)
1649                 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1650                         (&par->vbe_ib) + par->vbe_ib.oem_product_name_ptr);
1651         else
1652                 return 0;
1653 }
1654
1655 static DEVICE_ATTR(oem_product_name, S_IRUGO, uvesafb_show_product_name, NULL);
1656
1657 static ssize_t uvesafb_show_product_rev(struct device *dev,
1658                 struct device_attribute *attr, char *buf)
1659 {
1660         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1661         struct uvesafb_par *par = info->par;
1662
1663         if (par->vbe_ib.oem_product_rev_ptr)
1664                 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)
1665                         (&par->vbe_ib) + par->vbe_ib.oem_product_rev_ptr);
1666         else
1667                 return 0;
1668 }
1669
1670 static DEVICE_ATTR(oem_product_rev, S_IRUGO, uvesafb_show_product_rev, NULL);
1671
1672 static ssize_t uvesafb_show_oem_string(struct device *dev,
1673                 struct device_attribute *attr, char *buf)
1674 {
1675         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1676         struct uvesafb_par *par = info->par;
1677
1678         if (par->vbe_ib.oem_string_ptr)
1679                 return snprintf(buf, PAGE_SIZE, "%s\n",
1680                         (char *)(&par->vbe_ib) + par->vbe_ib.oem_string_ptr);
1681         else
1682                 return 0;
1683 }
1684
1685 static DEVICE_ATTR(oem_string, S_IRUGO, uvesafb_show_oem_string, NULL);
1686
1687 static ssize_t uvesafb_show_nocrtc(struct device *dev,
1688                 struct device_attribute *attr, char *buf)
1689 {
1690         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1691         struct uvesafb_par *par = info->par;
1692
1693         return snprintf(buf, PAGE_SIZE, "%d\n", par->nocrtc);
1694 }
1695
1696 static ssize_t uvesafb_store_nocrtc(struct device *dev,
1697                 struct device_attribute *attr, const char *buf, size_t count)
1698 {
1699         struct fb_info *info = platform_get_drvdata(to_platform_device(dev));
1700         struct uvesafb_par *par = info->par;
1701
1702         if (count > 0) {
1703                 if (buf[0] == '0')
1704                         par->nocrtc = 0;
1705                 else
1706                         par->nocrtc = 1;
1707         }
1708         return count;
1709 }
1710
1711 static DEVICE_ATTR(nocrtc, S_IRUGO | S_IWUSR, uvesafb_show_nocrtc,
1712                         uvesafb_store_nocrtc);
1713
1714 static struct attribute *uvesafb_dev_attrs[] = {
1715         &dev_attr_vbe_version.attr,
1716         &dev_attr_vbe_modes.attr,
1717         &dev_attr_oem_vendor.attr,
1718         &dev_attr_oem_product_name.attr,
1719         &dev_attr_oem_product_rev.attr,
1720         &dev_attr_oem_string.attr,
1721         &dev_attr_nocrtc.attr,
1722         NULL,
1723 };
1724
1725 static struct attribute_group uvesafb_dev_attgrp = {
1726         .name = NULL,
1727         .attrs = uvesafb_dev_attrs,
1728 };
1729
1730 static int __devinit uvesafb_probe(struct platform_device *dev)
1731 {
1732         struct fb_info *info;
1733         struct vbe_mode_ib *mode = NULL;
1734         struct uvesafb_par *par;
1735         int err = 0, i;
1736
1737         info = framebuffer_alloc(sizeof(*par) + sizeof(u32) * 256, &dev->dev);
1738         if (!info)
1739                 return -ENOMEM;
1740
1741         par = info->par;
1742
1743         err = uvesafb_vbe_init(info);
1744         if (err) {
1745                 printk(KERN_ERR "uvesafb: vbe_init() failed with %d\n", err);
1746                 goto out;
1747         }
1748
1749         info->fbops = &uvesafb_ops;
1750
1751         i = uvesafb_vbe_init_mode(info);
1752         if (i < 0) {
1753                 err = -EINVAL;
1754                 goto out;
1755         } else {
1756                 mode = &par->vbe_modes[i];
1757         }
1758
1759         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
1760                 err = -ENXIO;
1761                 goto out;
1762         }
1763
1764         uvesafb_init_info(info, mode);
1765
1766         if (!request_mem_region(info->fix.smem_start, info->fix.smem_len,
1767                                 "uvesafb")) {
1768                 printk(KERN_ERR "uvesafb: cannot reserve video memory at "
1769                                 "0x%lx\n", info->fix.smem_start);
1770                 err = -EIO;
1771                 goto out_mode;
1772         }
1773
1774         info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
1775
1776         if (!info->screen_base) {
1777                 printk(KERN_ERR
1778                         "uvesafb: abort, cannot ioremap 0x%x bytes of video "
1779                         "memory at 0x%lx\n",
1780                         info->fix.smem_len, info->fix.smem_start);
1781                 err = -EIO;
1782                 goto out_mem;
1783         }
1784
1785         if (!request_region(0x3c0, 32, "uvesafb")) {
1786                 printk(KERN_ERR "uvesafb: request region 0x3c0-0x3e0 failed\n");
1787                 err = -EIO;
1788                 goto out_unmap;
1789         }
1790
1791         uvesafb_init_mtrr(info);
1792         platform_set_drvdata(dev, info);
1793
1794         if (register_framebuffer(info) < 0) {
1795                 printk(KERN_ERR
1796                         "uvesafb: failed to register framebuffer device\n");
1797                 err = -EINVAL;
1798                 goto out_reg;
1799         }
1800
1801         printk(KERN_INFO "uvesafb: framebuffer at 0x%lx, mapped to 0x%p, "
1802                         "using %dk, total %dk\n", info->fix.smem_start,
1803                         info->screen_base, info->fix.smem_len/1024,
1804                         par->vbe_ib.total_memory * 64);
1805         printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
1806                         info->fix.id);
1807
1808         err = sysfs_create_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1809         if (err != 0)
1810                 printk(KERN_WARNING "fb%d: failed to register attributes\n",
1811                         info->node);
1812
1813         return 0;
1814
1815 out_reg:
1816         release_region(0x3c0, 32);
1817 out_unmap:
1818         iounmap(info->screen_base);
1819 out_mem:
1820         release_mem_region(info->fix.smem_start, info->fix.smem_len);
1821 out_mode:
1822         if (!list_empty(&info->modelist))
1823                 fb_destroy_modelist(&info->modelist);
1824         fb_destroy_modedb(info->monspecs.modedb);
1825         fb_dealloc_cmap(&info->cmap);
1826 out:
1827         if (par->vbe_modes)
1828                 kfree(par->vbe_modes);
1829
1830         framebuffer_release(info);
1831         return err;
1832 }
1833
1834 static int uvesafb_remove(struct platform_device *dev)
1835 {
1836         struct fb_info *info = platform_get_drvdata(dev);
1837
1838         if (info) {
1839                 struct uvesafb_par *par = info->par;
1840
1841                 sysfs_remove_group(&dev->dev.kobj, &uvesafb_dev_attgrp);
1842                 unregister_framebuffer(info);
1843                 release_region(0x3c0, 32);
1844                 iounmap(info->screen_base);
1845                 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1846                 fb_destroy_modedb(info->monspecs.modedb);
1847                 fb_dealloc_cmap(&info->cmap);
1848
1849                 if (par) {
1850                         if (par->vbe_modes)
1851                                 kfree(par->vbe_modes);
1852                         if (par->vbe_state_orig)
1853                                 kfree(par->vbe_state_orig);
1854                         if (par->vbe_state_saved)
1855                                 kfree(par->vbe_state_saved);
1856                 }
1857
1858                 framebuffer_release(info);
1859         }
1860         return 0;
1861 }
1862
1863 static struct platform_driver uvesafb_driver = {
1864         .probe  = uvesafb_probe,
1865         .remove = uvesafb_remove,
1866         .driver = {
1867                 .name = "uvesafb",
1868         },
1869 };
1870
1871 static struct platform_device *uvesafb_device;
1872
1873 #ifndef MODULE
1874 static int __devinit uvesafb_setup(char *options)
1875 {
1876         char *this_opt;
1877
1878         if (!options || !*options)
1879                 return 0;
1880
1881         while ((this_opt = strsep(&options, ",")) != NULL) {
1882                 if (!*this_opt) continue;
1883
1884                 if (!strcmp(this_opt, "redraw"))
1885                         ypan = 0;
1886                 else if (!strcmp(this_opt, "ypan"))
1887                         ypan = 1;
1888                 else if (!strcmp(this_opt, "ywrap"))
1889                         ypan = 2;
1890                 else if (!strcmp(this_opt, "vgapal"))
1891                         pmi_setpal = 0;
1892                 else if (!strcmp(this_opt, "pmipal"))
1893                         pmi_setpal = 1;
1894                 else if (!strncmp(this_opt, "mtrr:", 5))
1895                         mtrr = simple_strtoul(this_opt+5, NULL, 0);
1896                 else if (!strcmp(this_opt, "nomtrr"))
1897                         mtrr = 0;
1898                 else if (!strcmp(this_opt, "nocrtc"))
1899                         nocrtc = 1;
1900                 else if (!strcmp(this_opt, "noedid"))
1901                         noedid = 1;
1902                 else if (!strcmp(this_opt, "noblank"))
1903                         blank = 0;
1904                 else if (!strncmp(this_opt, "vtotal:", 7))
1905                         vram_total = simple_strtoul(this_opt + 7, NULL, 0);
1906                 else if (!strncmp(this_opt, "vremap:", 7))
1907                         vram_remap = simple_strtoul(this_opt + 7, NULL, 0);
1908                 else if (!strncmp(this_opt, "maxhf:", 6))
1909                         maxhf = simple_strtoul(this_opt + 6, NULL, 0);
1910                 else if (!strncmp(this_opt, "maxvf:", 6))
1911                         maxvf = simple_strtoul(this_opt + 6, NULL, 0);
1912                 else if (!strncmp(this_opt, "maxclk:", 7))
1913                         maxclk = simple_strtoul(this_opt + 7, NULL, 0);
1914                 else if (!strncmp(this_opt, "vbemode:", 8))
1915                         vbemode = simple_strtoul(this_opt + 8, NULL, 0);
1916                 else if (this_opt[0] >= '0' && this_opt[0] <= '9') {
1917                         mode_option = this_opt;
1918                 } else {
1919                         printk(KERN_WARNING
1920                                 "uvesafb: unrecognized option %s\n", this_opt);
1921                 }
1922         }
1923
1924         return 0;
1925 }
1926 #endif /* !MODULE */
1927
1928 static ssize_t show_v86d(struct device_driver *dev, char *buf)
1929 {
1930         return snprintf(buf, PAGE_SIZE, "%s\n", v86d_path);
1931 }
1932
1933 static ssize_t store_v86d(struct device_driver *dev, const char *buf,
1934                 size_t count)
1935 {
1936         strncpy(v86d_path, buf, PATH_MAX);
1937         return count;
1938 }
1939
1940 static DRIVER_ATTR(v86d, S_IRUGO | S_IWUSR, show_v86d, store_v86d);
1941
1942 static int __devinit uvesafb_init(void)
1943 {
1944         int err;
1945
1946 #ifndef MODULE
1947         char *option = NULL;
1948
1949         if (fb_get_options("uvesafb", &option))
1950                 return -ENODEV;
1951         uvesafb_setup(option);
1952 #endif
1953         err = cn_add_callback(&uvesafb_cn_id, "uvesafb", uvesafb_cn_callback);
1954         if (err)
1955                 return err;
1956
1957         err = platform_driver_register(&uvesafb_driver);
1958
1959         if (!err) {
1960                 uvesafb_device = platform_device_alloc("uvesafb", 0);
1961                 if (uvesafb_device)
1962                         err = platform_device_add(uvesafb_device);
1963                 else
1964                         err = -ENOMEM;
1965
1966                 if (err) {
1967                         platform_device_put(uvesafb_device);
1968                         platform_driver_unregister(&uvesafb_driver);
1969                         cn_del_callback(&uvesafb_cn_id);
1970                         return err;
1971                 }
1972
1973                 err = driver_create_file(&uvesafb_driver.driver,
1974                                 &driver_attr_v86d);
1975                 if (err) {
1976                         printk(KERN_WARNING "uvesafb: failed to register "
1977                                         "attributes\n");
1978                         err = 0;
1979                 }
1980         }
1981         return err;
1982 }
1983
1984 module_init(uvesafb_init);
1985
1986 static void __devexit uvesafb_exit(void)
1987 {
1988         struct uvesafb_ktask *task;
1989
1990         if (v86d_started) {
1991                 task = uvesafb_prep();
1992                 if (task) {
1993                         task->t.flags = TF_EXIT;
1994                         uvesafb_exec(task);
1995                         uvesafb_free(task);
1996                 }
1997         }
1998
1999         cn_del_callback(&uvesafb_cn_id);
2000         driver_remove_file(&uvesafb_driver.driver, &driver_attr_v86d);
2001         platform_device_unregister(uvesafb_device);
2002         platform_driver_unregister(&uvesafb_driver);
2003 }
2004
2005 module_exit(uvesafb_exit);
2006
2007 static int param_get_scroll(char *buffer, struct kernel_param *kp)
2008 {
2009         return 0;
2010 }
2011
2012 static int param_set_scroll(const char *val, struct kernel_param *kp)
2013 {
2014         ypan = 0;
2015
2016         if (!strcmp(val, "redraw"))
2017                 ypan = 0;
2018         else if (!strcmp(val, "ypan"))
2019                 ypan = 1;
2020         else if (!strcmp(val, "ywrap"))
2021                 ypan = 2;
2022
2023         return 0;
2024 }
2025
2026 #define param_check_scroll(name, p) __param_check(name, p, void)
2027
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.");
2065
2066 MODULE_LICENSE("GPL");
2067 MODULE_AUTHOR("Michal Januszewski <spock@gentoo.org>");
2068 MODULE_DESCRIPTION("Framebuffer driver for VBE2.0+ compliant graphics boards");
2069