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