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