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