blob: 2222de6ad8446b8d77ec879cf04ecface133fa83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/fbmem.c
3 *
4 * Copyright (C) 1994 Martin Schaller
5 *
6 * 2001 - Documented with DocBook
7 * - Brad Douglas <brad@neruo.com>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive
11 * for more details.
12 */
13
14#include <linux/config.h>
15#include <linux/module.h>
16
17#include <linux/types.h>
18#include <linux/errno.h>
19#include <linux/sched.h>
20#include <linux/smp_lock.h>
21#include <linux/kernel.h>
22#include <linux/major.h>
23#include <linux/slab.h>
24#include <linux/mm.h>
25#include <linux/mman.h>
26#include <linux/tty.h>
27#include <linux/init.h>
28#include <linux/linux_logo.h>
29#include <linux/proc_fs.h>
30#include <linux/console.h>
31#ifdef CONFIG_KMOD
32#include <linux/kmod.h>
33#endif
34#include <linux/devfs_fs_kernel.h>
35#include <linux/err.h>
36#include <linux/kernel.h>
37#include <linux/device.h>
38#include <linux/efi.h>
39
40#if defined(__mc68000__) || defined(CONFIG_APUS)
41#include <asm/setup.h>
42#endif
43
44#include <asm/io.h>
45#include <asm/uaccess.h>
46#include <asm/page.h>
47#include <asm/pgtable.h>
48
49#include <linux/fb.h>
50
51 /*
52 * Frame buffer device initialization and setup routines
53 */
54
55#define FBPIXMAPSIZE (1024 * 8)
56
57static struct notifier_block *fb_notifier_list;
58struct fb_info *registered_fb[FB_MAX];
59int num_registered_fb;
60
61/*
62 * Helpers
63 */
64
65int fb_get_color_depth(struct fb_var_screeninfo *var)
66{
67 if (var->green.length == var->blue.length &&
68 var->green.length == var->red.length &&
69 !var->green.offset && !var->blue.offset &&
70 !var->red.offset)
71 return var->green.length;
72 else
73 return (var->green.length + var->red.length +
74 var->blue.length);
75}
76EXPORT_SYMBOL(fb_get_color_depth);
77
78/*
James Simmonsf5a99512005-06-21 17:16:58 -070079 * Data padding functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
James Simmonsf1ab5da2005-06-21 17:17:07 -070081void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
James Simmonsf5a99512005-06-21 17:16:58 -070083 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 for (i = height; i--; ) {
James Simmonsf5a99512005-06-21 17:16:58 -070086 memcpy(dst, src, s_pitch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 src += s_pitch;
88 dst += d_pitch;
89 }
90}
James Simmonsf1ab5da2005-06-21 17:17:07 -070091EXPORT_SYMBOL(fb_pad_aligned_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
James Simmonsf1ab5da2005-06-21 17:17:07 -070093void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
94 u32 shift_high, u32 shift_low, u32 mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 u8 mask = (u8) (0xfff << shift_high), tmp;
97 int i, j;
98
99 for (i = height; i--; ) {
100 for (j = 0; j < idx; j++) {
101 tmp = dst[j];
102 tmp &= mask;
103 tmp |= *src >> shift_low;
104 dst[j] = tmp;
105 tmp = *src << shift_high;
106 dst[j+1] = tmp;
107 src++;
108 }
109 tmp = dst[idx];
110 tmp &= mask;
111 tmp |= *src >> shift_low;
112 dst[idx] = tmp;
113 if (shift_high < mod) {
114 tmp = *src << shift_high;
115 dst[idx+1] = tmp;
116 }
117 src++;
118 dst += d_pitch;
119 }
120}
James Simmonsf1ab5da2005-06-21 17:17:07 -0700121EXPORT_SYMBOL(fb_pad_unaligned_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * we need to lock this section since fb_cursor
125 * may use fb_imageblit()
126 */
127char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
128{
129 u32 align = buf->buf_align - 1, offset;
130 char *addr = buf->addr;
131
132 /* If IO mapped, we need to sync before access, no sharing of
133 * the pixmap is done
134 */
135 if (buf->flags & FB_PIXMAP_IO) {
136 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
137 info->fbops->fb_sync(info);
138 return addr;
139 }
140
141 /* See if we fit in the remaining pixmap space */
142 offset = buf->offset + align;
143 offset &= ~align;
144 if (offset + size > buf->size) {
145 /* We do not fit. In order to be able to re-use the buffer,
146 * we must ensure no asynchronous DMA'ing or whatever operation
147 * is in progress, we sync for that.
148 */
149 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
150 info->fbops->fb_sync(info);
151 offset = 0;
152 }
153 buf->offset = offset + size;
154 addr += offset;
155
156 return addr;
157}
158
159#ifdef CONFIG_LOGO
160#include <linux/linux_logo.h>
161
162static inline unsigned safe_shift(unsigned d, int n)
163{
164 return n < 0 ? d >> -n : d << n;
165}
166
167static void fb_set_logocmap(struct fb_info *info,
168 const struct linux_logo *logo)
169{
170 struct fb_cmap palette_cmap;
171 u16 palette_green[16];
172 u16 palette_blue[16];
173 u16 palette_red[16];
174 int i, j, n;
175 const unsigned char *clut = logo->clut;
176
177 palette_cmap.start = 0;
178 palette_cmap.len = 16;
179 palette_cmap.red = palette_red;
180 palette_cmap.green = palette_green;
181 palette_cmap.blue = palette_blue;
182 palette_cmap.transp = NULL;
183
184 for (i = 0; i < logo->clutsize; i += n) {
185 n = logo->clutsize - i;
186 /* palette_cmap provides space for only 16 colors at once */
187 if (n > 16)
188 n = 16;
189 palette_cmap.start = 32 + i;
190 palette_cmap.len = n;
191 for (j = 0; j < n; ++j) {
192 palette_cmap.red[j] = clut[0] << 8 | clut[0];
193 palette_cmap.green[j] = clut[1] << 8 | clut[1];
194 palette_cmap.blue[j] = clut[2] << 8 | clut[2];
195 clut += 3;
196 }
197 fb_set_cmap(&palette_cmap, info);
198 }
199}
200
201static void fb_set_logo_truepalette(struct fb_info *info,
202 const struct linux_logo *logo,
203 u32 *palette)
204{
205 unsigned char mask[9] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
206 unsigned char redmask, greenmask, bluemask;
207 int redshift, greenshift, blueshift;
208 int i;
209 const unsigned char *clut = logo->clut;
210
211 /*
212 * We have to create a temporary palette since console palette is only
213 * 16 colors long.
214 */
215 /* Bug: Doesn't obey msb_right ... (who needs that?) */
216 redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8];
217 greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
218 bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8];
219 redshift = info->var.red.offset - (8 - info->var.red.length);
220 greenshift = info->var.green.offset - (8 - info->var.green.length);
221 blueshift = info->var.blue.offset - (8 - info->var.blue.length);
222
223 for ( i = 0; i < logo->clutsize; i++) {
224 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
225 safe_shift((clut[1] & greenmask), greenshift) |
226 safe_shift((clut[2] & bluemask), blueshift));
227 clut += 3;
228 }
229}
230
231static void fb_set_logo_directpalette(struct fb_info *info,
232 const struct linux_logo *logo,
233 u32 *palette)
234{
235 int redshift, greenshift, blueshift;
236 int i;
237
238 redshift = info->var.red.offset;
239 greenshift = info->var.green.offset;
240 blueshift = info->var.blue.offset;
241
242 for (i = 32; i < logo->clutsize; i++)
243 palette[i] = i << redshift | i << greenshift | i << blueshift;
244}
245
246static void fb_set_logo(struct fb_info *info,
247 const struct linux_logo *logo, u8 *dst,
248 int depth)
249{
250 int i, j, k, fg = 1;
251 const u8 *src = logo->data;
252 u8 d, xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
253
254 if (fb_get_color_depth(&info->var) == 3)
255 fg = 7;
256
257 switch (depth) {
258 case 4:
259 for (i = 0; i < logo->height; i++)
260 for (j = 0; j < logo->width; src++) {
261 *dst++ = *src >> 4;
262 j++;
263 if (j < logo->width) {
264 *dst++ = *src & 0x0f;
265 j++;
266 }
267 }
268 break;
269 case 1:
270 for (i = 0; i < logo->height; i++) {
271 for (j = 0; j < logo->width; src++) {
272 d = *src ^ xor;
273 for (k = 7; k >= 0; k--) {
274 *dst++ = ((d >> k) & 1) ? fg : 0;
275 j++;
276 }
277 }
278 }
279 break;
280 }
281}
282
283/*
284 * Three (3) kinds of logo maps exist. linux_logo_clut224 (>16 colors),
285 * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors). Depending on
286 * the visual format and color depth of the framebuffer, the DAC, the
287 * pseudo_palette, and the logo data will be adjusted accordingly.
288 *
289 * Case 1 - linux_logo_clut224:
290 * Color exceeds the number of console colors (16), thus we set the hardware DAC
291 * using fb_set_cmap() appropriately. The "needs_cmapreset" flag will be set.
292 *
293 * For visuals that require color info from the pseudo_palette, we also construct
294 * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
295 * will be set.
296 *
297 * Case 2 - linux_logo_vga16:
298 * The number of colors just matches the console colors, thus there is no need
299 * to set the DAC or the pseudo_palette. However, the bitmap is packed, ie,
300 * each byte contains color information for two pixels (upper and lower nibble).
301 * To be consistent with fb_imageblit() usage, we therefore separate the two
302 * nibbles into separate bytes. The "depth" flag will be set to 4.
303 *
304 * Case 3 - linux_logo_mono:
305 * This is similar with Case 2. Each byte contains information for 8 pixels.
306 * We isolate each bit and expand each into a byte. The "depth" flag will
307 * be set to 1.
308 */
309static struct logo_data {
310 int depth;
311 int needs_directpalette;
312 int needs_truepalette;
313 int needs_cmapreset;
314 const struct linux_logo *logo;
315} fb_logo;
316
317int fb_prepare_logo(struct fb_info *info)
318{
319 int depth = fb_get_color_depth(&info->var);
320
321 memset(&fb_logo, 0, sizeof(struct logo_data));
322
323 if (info->flags & FBINFO_MISC_TILEBLITTING)
324 return 0;
325
326 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
327 depth = info->var.blue.length;
328 if (info->var.red.length < depth)
329 depth = info->var.red.length;
330 if (info->var.green.length < depth)
331 depth = info->var.green.length;
332 }
333
334 if (depth >= 8) {
335 switch (info->fix.visual) {
336 case FB_VISUAL_TRUECOLOR:
337 fb_logo.needs_truepalette = 1;
338 break;
339 case FB_VISUAL_DIRECTCOLOR:
340 fb_logo.needs_directpalette = 1;
341 fb_logo.needs_cmapreset = 1;
342 break;
343 case FB_VISUAL_PSEUDOCOLOR:
344 fb_logo.needs_cmapreset = 1;
345 break;
346 }
347 }
348
349 /* Return if no suitable logo was found */
350 fb_logo.logo = fb_find_logo(depth);
351
352 if (!fb_logo.logo || fb_logo.logo->height > info->var.yres) {
353 fb_logo.logo = NULL;
354 return 0;
355 }
356 /* What depth we asked for might be different from what we get */
357 if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
358 fb_logo.depth = 8;
359 else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
360 fb_logo.depth = 4;
361 else
362 fb_logo.depth = 1;
363 return fb_logo.logo->height;
364}
365
366int fb_show_logo(struct fb_info *info)
367{
368 u32 *palette = NULL, *saved_pseudo_palette = NULL;
369 unsigned char *logo_new = NULL;
370 struct fb_image image;
371 int x;
372
373 /* Return if the frame buffer is not mapped or suspended */
374 if (fb_logo.logo == NULL || info->state != FBINFO_STATE_RUNNING)
375 return 0;
376
377 image.depth = 8;
378 image.data = fb_logo.logo->data;
379
380 if (fb_logo.needs_cmapreset)
381 fb_set_logocmap(info, fb_logo.logo);
382
383 if (fb_logo.needs_truepalette ||
384 fb_logo.needs_directpalette) {
385 palette = kmalloc(256 * 4, GFP_KERNEL);
386 if (palette == NULL)
387 return 0;
388
389 if (fb_logo.needs_truepalette)
390 fb_set_logo_truepalette(info, fb_logo.logo, palette);
391 else
392 fb_set_logo_directpalette(info, fb_logo.logo, palette);
393
394 saved_pseudo_palette = info->pseudo_palette;
395 info->pseudo_palette = palette;
396 }
397
398 if (fb_logo.depth <= 4) {
399 logo_new = kmalloc(fb_logo.logo->width * fb_logo.logo->height,
400 GFP_KERNEL);
401 if (logo_new == NULL) {
402 kfree(palette);
403 if (saved_pseudo_palette)
404 info->pseudo_palette = saved_pseudo_palette;
405 return 0;
406 }
407 image.data = logo_new;
408 fb_set_logo(info, fb_logo.logo, logo_new, fb_logo.depth);
409 }
410
411 image.width = fb_logo.logo->width;
412 image.height = fb_logo.logo->height;
413 image.dy = 0;
414
415 for (x = 0; x < num_online_cpus() * (fb_logo.logo->width + 8) &&
416 x <= info->var.xres-fb_logo.logo->width; x += (fb_logo.logo->width + 8)) {
417 image.dx = x;
418 info->fbops->fb_imageblit(info, &image);
419 }
420
421 kfree(palette);
422 if (saved_pseudo_palette != NULL)
423 info->pseudo_palette = saved_pseudo_palette;
424 kfree(logo_new);
425 return fb_logo.logo->height;
426}
427#else
428int fb_prepare_logo(struct fb_info *info) { return 0; }
429int fb_show_logo(struct fb_info *info) { return 0; }
430#endif /* CONFIG_LOGO */
431
432static int fbmem_read_proc(char *buf, char **start, off_t offset,
433 int len, int *eof, void *private)
434{
435 struct fb_info **fi;
436 int clen;
437
438 clen = 0;
439 for (fi = registered_fb; fi < &registered_fb[FB_MAX] && len < 4000; fi++)
440 if (*fi)
441 clen += sprintf(buf + clen, "%d %s\n",
442 (*fi)->node,
443 (*fi)->fix.id);
444 *start = buf + offset;
445 if (clen > offset)
446 clen -= offset;
447 else
448 clen = 0;
449 return clen < len ? clen : len;
450}
451
452static ssize_t
453fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
454{
455 unsigned long p = *ppos;
456 struct inode *inode = file->f_dentry->d_inode;
457 int fbidx = iminor(inode);
458 struct fb_info *info = registered_fb[fbidx];
459 u32 *buffer, *dst;
460 u32 __iomem *src;
461 int c, i, cnt = 0, err = 0;
462 unsigned long total_size;
463
464 if (!info || ! info->screen_base)
465 return -ENODEV;
466
467 if (info->state != FBINFO_STATE_RUNNING)
468 return -EPERM;
469
470 if (info->fbops->fb_read)
471 return info->fbops->fb_read(file, buf, count, ppos);
472
473 total_size = info->screen_size;
474 if (total_size == 0)
475 total_size = info->fix.smem_len;
476
477 if (p >= total_size)
478 return 0;
479 if (count >= total_size)
480 count = total_size;
481 if (count + p > total_size)
482 count = total_size - p;
483
484 cnt = 0;
485 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
486 GFP_KERNEL);
487 if (!buffer)
488 return -ENOMEM;
489
490 src = (u32 __iomem *) (info->screen_base + p);
491
492 if (info->fbops->fb_sync)
493 info->fbops->fb_sync(info);
494
495 while (count) {
496 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
497 dst = buffer;
498 for (i = c >> 2; i--; )
499 *dst++ = fb_readl(src++);
500 if (c & 3) {
501 u8 *dst8 = (u8 *) dst;
502 u8 __iomem *src8 = (u8 __iomem *) src;
503
504 for (i = c & 3; i--;)
505 *dst8++ = fb_readb(src8++);
506
507 src = (u32 __iomem *) src8;
508 }
509
510 if (copy_to_user(buf, buffer, c)) {
511 err = -EFAULT;
512 break;
513 }
514 *ppos += c;
515 buf += c;
516 cnt += c;
517 count -= c;
518 }
519
520 kfree(buffer);
521 return (err) ? err : cnt;
522}
523
524static ssize_t
525fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
526{
527 unsigned long p = *ppos;
528 struct inode *inode = file->f_dentry->d_inode;
529 int fbidx = iminor(inode);
530 struct fb_info *info = registered_fb[fbidx];
531 u32 *buffer, *src;
532 u32 __iomem *dst;
533 int c, i, cnt = 0, err;
534 unsigned long total_size;
535
536 if (!info || !info->screen_base)
537 return -ENODEV;
538
539 if (info->state != FBINFO_STATE_RUNNING)
540 return -EPERM;
541
542 if (info->fbops->fb_write)
543 return info->fbops->fb_write(file, buf, count, ppos);
544
545 total_size = info->screen_size;
546 if (total_size == 0)
547 total_size = info->fix.smem_len;
548
549 if (p > total_size)
550 return -ENOSPC;
551 if (count >= total_size)
552 count = total_size;
553 err = 0;
554 if (count + p > total_size) {
555 count = total_size - p;
556 err = -ENOSPC;
557 }
558 cnt = 0;
559 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
560 GFP_KERNEL);
561 if (!buffer)
562 return -ENOMEM;
563
564 dst = (u32 __iomem *) (info->screen_base + p);
565
566 if (info->fbops->fb_sync)
567 info->fbops->fb_sync(info);
568
569 while (count) {
570 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
571 src = buffer;
572 if (copy_from_user(src, buf, c)) {
573 err = -EFAULT;
574 break;
575 }
576 for (i = c >> 2; i--; )
577 fb_writel(*src++, dst++);
578 if (c & 3) {
579 u8 *src8 = (u8 *) src;
580 u8 __iomem *dst8 = (u8 __iomem *) dst;
581
582 for (i = c & 3; i--; )
583 fb_writeb(*src8++, dst8++);
584
585 dst = (u32 __iomem *) dst8;
586 }
587 *ppos += c;
588 buf += c;
589 cnt += c;
590 count -= c;
591 }
592 kfree(buffer);
593
594 return (err) ? err : cnt;
595}
596
597#ifdef CONFIG_KMOD
598static void try_to_load(int fb)
599{
600 request_module("fb%d", fb);
601}
602#endif /* CONFIG_KMOD */
603
604int
605fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
606{
607 int xoffset = var->xoffset;
608 int yoffset = var->yoffset;
609 int err;
610
611 if (xoffset < 0 || yoffset < 0 || !info->fbops->fb_pan_display ||
612 xoffset + info->var.xres > info->var.xres_virtual ||
613 yoffset + info->var.yres > info->var.yres_virtual)
614 return -EINVAL;
615 if ((err = info->fbops->fb_pan_display(var, info)))
616 return err;
617 info->var.xoffset = var->xoffset;
618 info->var.yoffset = var->yoffset;
619 if (var->vmode & FB_VMODE_YWRAP)
620 info->var.vmode |= FB_VMODE_YWRAP;
621 else
622 info->var.vmode &= ~FB_VMODE_YWRAP;
623 return 0;
624}
625
626int
627fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
628{
629 int err;
630
631 if (var->activate & FB_ACTIVATE_INV_MODE) {
632 struct fb_videomode mode1, mode2;
633 int ret = 0;
634
635 fb_var_to_videomode(&mode1, var);
636 fb_var_to_videomode(&mode2, &info->var);
637 /* make sure we don't delete the videomode of current var */
638 ret = fb_mode_is_equal(&mode1, &mode2);
639
640 if (!ret) {
641 struct fb_event event;
642
643 event.info = info;
644 event.data = &mode1;
645 ret = notifier_call_chain(&fb_notifier_list,
646 FB_EVENT_MODE_DELETE, &event);
647 }
648
649 if (!ret)
650 fb_delete_videomode(&mode1, &info->modelist);
651
652 return ret;
653 }
654
655 if ((var->activate & FB_ACTIVATE_FORCE) ||
656 memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
657 if (!info->fbops->fb_check_var) {
658 *var = info->var;
659 return 0;
660 }
661
662 if ((err = info->fbops->fb_check_var(var, info)))
663 return err;
664
665 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
666 struct fb_videomode mode;
667 int err = 0;
668
669 info->var = *var;
670 if (info->fbops->fb_set_par)
671 info->fbops->fb_set_par(info);
672
673 fb_pan_display(info, &info->var);
674
675 fb_set_cmap(&info->cmap, info);
676
677 fb_var_to_videomode(&mode, &info->var);
678
679 if (info->modelist.prev && info->modelist.next &&
680 !list_empty(&info->modelist))
681 err = fb_add_videomode(&mode, &info->modelist);
682
683 if (!err && info->flags & FBINFO_MISC_USEREVENT) {
684 struct fb_event event;
685
686 info->flags &= ~FBINFO_MISC_USEREVENT;
687 event.info = info;
688 notifier_call_chain(&fb_notifier_list,
689 FB_EVENT_MODE_CHANGE,
690 &event);
691 }
692 }
693 }
694 return 0;
695}
696
697int
698fb_blank(struct fb_info *info, int blank)
699{
700 int ret = -EINVAL;
701
702 if (blank > FB_BLANK_POWERDOWN)
703 blank = FB_BLANK_POWERDOWN;
704
705 if (info->fbops->fb_blank)
706 ret = info->fbops->fb_blank(blank, info);
707
708 if (!ret) {
709 struct fb_event event;
710
711 event.info = info;
712 event.data = &blank;
713 notifier_call_chain(&fb_notifier_list, FB_EVENT_BLANK, &event);
714 }
715
716 return ret;
717}
718
719static int
720fb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
721 unsigned long arg)
722{
723 int fbidx = iminor(inode);
724 struct fb_info *info = registered_fb[fbidx];
725 struct fb_ops *fb = info->fbops;
726 struct fb_var_screeninfo var;
727 struct fb_fix_screeninfo fix;
728 struct fb_con2fbmap con2fb;
729 struct fb_cmap_user cmap;
730 struct fb_event event;
731 void __user *argp = (void __user *)arg;
732 int i;
733
734 if (!fb)
735 return -ENODEV;
736 switch (cmd) {
737 case FBIOGET_VSCREENINFO:
738 return copy_to_user(argp, &info->var,
739 sizeof(var)) ? -EFAULT : 0;
740 case FBIOPUT_VSCREENINFO:
741 if (copy_from_user(&var, argp, sizeof(var)))
742 return -EFAULT;
743 acquire_console_sem();
744 info->flags |= FBINFO_MISC_USEREVENT;
745 i = fb_set_var(info, &var);
746 info->flags &= ~FBINFO_MISC_USEREVENT;
747 release_console_sem();
748 if (i) return i;
749 if (copy_to_user(argp, &var, sizeof(var)))
750 return -EFAULT;
751 return 0;
752 case FBIOGET_FSCREENINFO:
753 return copy_to_user(argp, &info->fix,
754 sizeof(fix)) ? -EFAULT : 0;
755 case FBIOPUTCMAP:
756 if (copy_from_user(&cmap, argp, sizeof(cmap)))
757 return -EFAULT;
758 return (fb_set_user_cmap(&cmap, info));
759 case FBIOGETCMAP:
760 if (copy_from_user(&cmap, argp, sizeof(cmap)))
761 return -EFAULT;
762 return fb_cmap_to_user(&info->cmap, &cmap);
763 case FBIOPAN_DISPLAY:
764 if (copy_from_user(&var, argp, sizeof(var)))
765 return -EFAULT;
766 acquire_console_sem();
767 i = fb_pan_display(info, &var);
768 release_console_sem();
769 if (i)
770 return i;
771 if (copy_to_user(argp, &var, sizeof(var)))
772 return -EFAULT;
773 return 0;
774 case FBIO_CURSOR:
775 return -EINVAL;
776 case FBIOGET_CON2FBMAP:
777 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
778 return -EFAULT;
779 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
780 return -EINVAL;
781 con2fb.framebuffer = -1;
782 event.info = info;
783 event.data = &con2fb;
784 notifier_call_chain(&fb_notifier_list,
785 FB_EVENT_GET_CONSOLE_MAP, &event);
786 return copy_to_user(argp, &con2fb,
787 sizeof(con2fb)) ? -EFAULT : 0;
788 case FBIOPUT_CON2FBMAP:
789 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
790 return - EFAULT;
791 if (con2fb.console < 0 || con2fb.console > MAX_NR_CONSOLES)
792 return -EINVAL;
793 if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
794 return -EINVAL;
795#ifdef CONFIG_KMOD
796 if (!registered_fb[con2fb.framebuffer])
797 try_to_load(con2fb.framebuffer);
798#endif /* CONFIG_KMOD */
799 if (!registered_fb[con2fb.framebuffer])
800 return -EINVAL;
801 event.info = info;
802 event.data = &con2fb;
803 return notifier_call_chain(&fb_notifier_list,
804 FB_EVENT_SET_CONSOLE_MAP,
805 &event);
806 case FBIOBLANK:
807 acquire_console_sem();
808 info->flags |= FBINFO_MISC_USEREVENT;
809 i = fb_blank(info, arg);
810 info->flags &= ~FBINFO_MISC_USEREVENT;
811 release_console_sem();
812 return i;
813 default:
814 if (fb->fb_ioctl == NULL)
815 return -EINVAL;
816 return fb->fb_ioctl(inode, file, cmd, arg, info);
817 }
818}
819
820#ifdef CONFIG_COMPAT
821static long
822fb_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
823{
824 int fbidx = iminor(file->f_dentry->d_inode);
825 struct fb_info *info = registered_fb[fbidx];
826 struct fb_ops *fb = info->fbops;
827 long ret;
828
829 if (fb->fb_compat_ioctl == NULL)
830 return -ENOIOCTLCMD;
831 lock_kernel();
832 ret = fb->fb_compat_ioctl(file, cmd, arg, info);
833 unlock_kernel();
834 return ret;
835}
836#endif
837
838static int
839fb_mmap(struct file *file, struct vm_area_struct * vma)
840{
841 int fbidx = iminor(file->f_dentry->d_inode);
842 struct fb_info *info = registered_fb[fbidx];
843 struct fb_ops *fb = info->fbops;
844 unsigned long off;
845#if !defined(__sparc__) || defined(__sparc_v9__)
846 unsigned long start;
847 u32 len;
848#endif
849
850 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
851 return -EINVAL;
852 off = vma->vm_pgoff << PAGE_SHIFT;
853 if (!fb)
854 return -ENODEV;
855 if (fb->fb_mmap) {
856 int res;
857 lock_kernel();
858 res = fb->fb_mmap(info, file, vma);
859 unlock_kernel();
860 return res;
861 }
862
863#if defined(__sparc__) && !defined(__sparc_v9__)
864 /* Should never get here, all fb drivers should have their own
865 mmap routines */
866 return -EINVAL;
867#else
868 /* !sparc32... */
869 lock_kernel();
870
871 /* frame buffer memory */
872 start = info->fix.smem_start;
873 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
874 if (off >= len) {
875 /* memory mapped io */
876 off -= len;
877 if (info->var.accel_flags) {
878 unlock_kernel();
879 return -EINVAL;
880 }
881 start = info->fix.mmio_start;
882 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
883 }
884 unlock_kernel();
885 start &= PAGE_MASK;
886 if ((vma->vm_end - vma->vm_start + off) > len)
887 return -EINVAL;
888 off += start;
889 vma->vm_pgoff = off >> PAGE_SHIFT;
890 /* This is an IO map - tell maydump to skip this VMA */
891 vma->vm_flags |= VM_IO | VM_RESERVED;
892#if defined(__sparc_v9__)
893 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
894 vma->vm_end - vma->vm_start, vma->vm_page_prot))
895 return -EAGAIN;
896#else
897#if defined(__mc68000__)
898#if defined(CONFIG_SUN3)
899 pgprot_val(vma->vm_page_prot) |= SUN3_PAGE_NOCACHE;
900#elif defined(CONFIG_MMU)
901 if (CPU_IS_020_OR_030)
902 pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE030;
903 if (CPU_IS_040_OR_060) {
904 pgprot_val(vma->vm_page_prot) &= _CACHEMASK040;
905 /* Use no-cache mode, serialized */
906 pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE_S;
907 }
908#endif
909#elif defined(__powerpc__)
910 vma->vm_page_prot = phys_mem_access_prot(file, off,
911 vma->vm_end - vma->vm_start,
912 vma->vm_page_prot);
913#elif defined(__alpha__)
914 /* Caching is off in the I/O space quadrant by design. */
915#elif defined(__i386__) || defined(__x86_64__)
916 if (boot_cpu_data.x86 > 3)
917 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
918#elif defined(__mips__)
919 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
920#elif defined(__hppa__)
921 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
922#elif defined(__arm__) || defined(__sh__) || defined(__m32r__)
923 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
924#elif defined(__ia64__)
925 if (efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
926 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
927 else
928 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
929#else
930#warning What do we have to do here??
931#endif
932 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
933 vma->vm_end - vma->vm_start, vma->vm_page_prot))
934 return -EAGAIN;
935#endif /* !__sparc_v9__ */
936 return 0;
937#endif /* !sparc32 */
938}
939
940static int
941fb_open(struct inode *inode, struct file *file)
942{
943 int fbidx = iminor(inode);
944 struct fb_info *info;
945 int res = 0;
946
947 if (fbidx >= FB_MAX)
948 return -ENODEV;
949#ifdef CONFIG_KMOD
950 if (!(info = registered_fb[fbidx]))
951 try_to_load(fbidx);
952#endif /* CONFIG_KMOD */
953 if (!(info = registered_fb[fbidx]))
954 return -ENODEV;
955 if (!try_module_get(info->fbops->owner))
956 return -ENODEV;
957 if (info->fbops->fb_open) {
958 res = info->fbops->fb_open(info,1);
959 if (res)
960 module_put(info->fbops->owner);
961 }
962 return res;
963}
964
965static int
966fb_release(struct inode *inode, struct file *file)
967{
968 int fbidx = iminor(inode);
969 struct fb_info *info;
970
971 lock_kernel();
972 info = registered_fb[fbidx];
973 if (info->fbops->fb_release)
974 info->fbops->fb_release(info,1);
975 module_put(info->fbops->owner);
976 unlock_kernel();
977 return 0;
978}
979
980static struct file_operations fb_fops = {
981 .owner = THIS_MODULE,
982 .read = fb_read,
983 .write = fb_write,
984 .ioctl = fb_ioctl,
985#ifdef CONFIG_COMPAT
986 .compat_ioctl = fb_compat_ioctl,
987#endif
988 .mmap = fb_mmap,
989 .open = fb_open,
990 .release = fb_release,
991#ifdef HAVE_ARCH_FB_UNMAPPED_AREA
992 .get_unmapped_area = get_fb_unmapped_area,
993#endif
994};
995
gregkh@suse.de56b22932005-03-23 10:01:41 -0800996static struct class *fb_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998/**
999 * register_framebuffer - registers a frame buffer device
1000 * @fb_info: frame buffer info structure
1001 *
1002 * Registers a frame buffer device @fb_info.
1003 *
1004 * Returns negative errno on error, or zero for success.
1005 *
1006 */
1007
1008int
1009register_framebuffer(struct fb_info *fb_info)
1010{
1011 int i;
1012 struct fb_event event;
1013
1014 if (num_registered_fb == FB_MAX)
1015 return -ENXIO;
1016 num_registered_fb++;
1017 for (i = 0 ; i < FB_MAX; i++)
1018 if (!registered_fb[i])
1019 break;
1020 fb_info->node = i;
1021
gregkh@suse.de56b22932005-03-23 10:01:41 -08001022 fb_info->class_device = class_device_create(fb_class, MKDEV(FB_MAJOR, i),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 fb_info->device, "fb%d", i);
1024 if (IS_ERR(fb_info->class_device)) {
1025 /* Not fatal */
1026 printk(KERN_WARNING "Unable to create class_device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->class_device));
1027 fb_info->class_device = NULL;
1028 } else
1029 fb_init_class_device(fb_info);
1030
1031 if (fb_info->pixmap.addr == NULL) {
1032 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1033 if (fb_info->pixmap.addr) {
1034 fb_info->pixmap.size = FBPIXMAPSIZE;
1035 fb_info->pixmap.buf_align = 1;
1036 fb_info->pixmap.scan_align = 1;
James Simmons58a60642005-06-21 17:17:08 -07001037 fb_info->pixmap.access_align = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1039 }
1040 }
1041 fb_info->pixmap.offset = 0;
1042
1043 if (!fb_info->modelist.prev ||
1044 !fb_info->modelist.next ||
1045 list_empty(&fb_info->modelist)) {
1046 struct fb_videomode mode;
1047
1048 INIT_LIST_HEAD(&fb_info->modelist);
1049 fb_var_to_videomode(&mode, &fb_info->var);
1050 fb_add_videomode(&mode, &fb_info->modelist);
1051 }
1052
1053 registered_fb[i] = fb_info;
1054
1055 devfs_mk_cdev(MKDEV(FB_MAJOR, i),
1056 S_IFCHR | S_IRUGO | S_IWUGO, "fb/%d", i);
1057 event.info = fb_info;
1058 notifier_call_chain(&fb_notifier_list,
1059 FB_EVENT_FB_REGISTERED, &event);
1060 return 0;
1061}
1062
1063
1064/**
1065 * unregister_framebuffer - releases a frame buffer device
1066 * @fb_info: frame buffer info structure
1067 *
1068 * Unregisters a frame buffer device @fb_info.
1069 *
1070 * Returns negative errno on error, or zero for success.
1071 *
1072 */
1073
1074int
1075unregister_framebuffer(struct fb_info *fb_info)
1076{
1077 int i;
1078
1079 i = fb_info->node;
1080 if (!registered_fb[i])
1081 return -EINVAL;
1082 devfs_remove("fb/%d", i);
1083
1084 if (fb_info->pixmap.addr && (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1085 kfree(fb_info->pixmap.addr);
1086 fb_destroy_modelist(&fb_info->modelist);
1087 registered_fb[i]=NULL;
1088 num_registered_fb--;
1089 fb_cleanup_class_device(fb_info);
gregkh@suse.de56b22932005-03-23 10:01:41 -08001090 class_device_destroy(fb_class, MKDEV(FB_MAJOR, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return 0;
1092}
1093
1094/**
1095 * fb_register_client - register a client notifier
1096 * @nb: notifier block to callback on events
1097 */
1098int fb_register_client(struct notifier_block *nb)
1099{
1100 return notifier_chain_register(&fb_notifier_list, nb);
1101}
1102
1103/**
1104 * fb_unregister_client - unregister a client notifier
1105 * @nb: notifier block to callback on events
1106 */
1107int fb_unregister_client(struct notifier_block *nb)
1108{
1109 return notifier_chain_unregister(&fb_notifier_list, nb);
1110}
1111
1112/**
1113 * fb_set_suspend - low level driver signals suspend
1114 * @info: framebuffer affected
1115 * @state: 0 = resuming, !=0 = suspending
1116 *
1117 * This is meant to be used by low level drivers to
1118 * signal suspend/resume to the core & clients.
1119 * It must be called with the console semaphore held
1120 */
1121void fb_set_suspend(struct fb_info *info, int state)
1122{
1123 struct fb_event event;
1124
1125 event.info = info;
1126 if (state) {
1127 notifier_call_chain(&fb_notifier_list, FB_EVENT_SUSPEND, &event);
1128 info->state = FBINFO_STATE_SUSPENDED;
1129 } else {
1130 info->state = FBINFO_STATE_RUNNING;
1131 notifier_call_chain(&fb_notifier_list, FB_EVENT_RESUME, &event);
1132 }
1133}
1134
1135/**
1136 * fbmem_init - init frame buffer subsystem
1137 *
1138 * Initialize the frame buffer subsystem.
1139 *
1140 * NOTE: This function is _only_ to be called by drivers/char/mem.c.
1141 *
1142 */
1143
1144static int __init
1145fbmem_init(void)
1146{
1147 create_proc_read_entry("fb", 0, NULL, fbmem_read_proc, NULL);
1148
1149 devfs_mk_dir("fb");
1150 if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
1151 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1152
gregkh@suse.de56b22932005-03-23 10:01:41 -08001153 fb_class = class_create(THIS_MODULE, "graphics");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (IS_ERR(fb_class)) {
1155 printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
1156 fb_class = NULL;
1157 }
1158 return 0;
1159}
1160
1161#ifdef MODULE
1162module_init(fbmem_init);
1163static void __exit
1164fbmem_exit(void)
1165{
gregkh@suse.de56b22932005-03-23 10:01:41 -08001166 class_destroy(fb_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169module_exit(fbmem_exit);
1170MODULE_LICENSE("GPL");
1171MODULE_DESCRIPTION("Framebuffer base");
1172#else
1173subsys_initcall(fbmem_init);
1174#endif
1175
1176int fb_new_modelist(struct fb_info *info)
1177{
1178 struct fb_event event;
1179 struct fb_var_screeninfo var = info->var;
1180 struct list_head *pos, *n;
1181 struct fb_modelist *modelist;
1182 struct fb_videomode *m, mode;
1183 int err = 1;
1184
1185 list_for_each_safe(pos, n, &info->modelist) {
1186 modelist = list_entry(pos, struct fb_modelist, list);
1187 m = &modelist->mode;
1188 fb_videomode_to_var(&var, m);
1189 var.activate = FB_ACTIVATE_TEST;
1190 err = fb_set_var(info, &var);
1191 fb_var_to_videomode(&mode, &var);
1192 if (err || !fb_mode_is_equal(m, &mode)) {
1193 list_del(pos);
1194 kfree(pos);
1195 }
1196 }
1197
1198 err = 1;
1199
1200 if (!list_empty(&info->modelist)) {
1201 event.info = info;
1202 err = notifier_call_chain(&fb_notifier_list,
1203 FB_EVENT_NEW_MODELIST,
1204 &event);
1205 }
1206
1207 return err;
1208}
1209
1210static char *video_options[FB_MAX];
1211static int ofonly;
1212
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001213extern const char *global_mode_option;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
1216 * fb_get_options - get kernel boot parameters
1217 * @name: framebuffer name as it would appear in
1218 * the boot parameter line
1219 * (video=<name>:<options>)
1220 * @option: the option will be stored here
1221 *
1222 * NOTE: Needed to maintain backwards compatibility
1223 */
1224int fb_get_options(char *name, char **option)
1225{
1226 char *opt, *options = NULL;
1227 int opt_len, retval = 0;
1228 int name_len = strlen(name), i;
1229
1230 if (name_len && ofonly && strncmp(name, "offb", 4))
1231 retval = 1;
1232
1233 if (name_len && !retval) {
1234 for (i = 0; i < FB_MAX; i++) {
1235 if (video_options[i] == NULL)
1236 continue;
1237 opt_len = strlen(video_options[i]);
1238 if (!opt_len)
1239 continue;
1240 opt = video_options[i];
1241 if (!strncmp(name, opt, name_len) &&
1242 opt[name_len] == ':')
1243 options = opt + name_len + 1;
1244 }
1245 }
1246 if (options && !strncmp(options, "off", 3))
1247 retval = 1;
1248
1249 if (option)
1250 *option = options;
1251
1252 return retval;
1253}
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255/**
1256 * video_setup - process command line options
1257 * @options: string of options
1258 *
1259 * Process command line options for frame buffer subsystem.
1260 *
1261 * NOTE: This function is a __setup and __init function.
1262 * It only stores the options. Drivers have to call
1263 * fb_get_options() as necessary.
1264 *
1265 * Returns zero.
1266 *
1267 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07001268static int __init video_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int i, global = 0;
1271
1272 if (!options || !*options)
1273 global = 1;
1274
1275 if (!global && !strncmp(options, "ofonly", 6)) {
1276 ofonly = 1;
1277 global = 1;
1278 }
1279
1280 if (!global && !strstr(options, "fb:")) {
1281 global_mode_option = options;
1282 global = 1;
1283 }
1284
1285 if (!global) {
1286 for (i = 0; i < FB_MAX; i++) {
1287 if (video_options[i] == NULL) {
1288 video_options[i] = options;
1289 break;
1290 }
1291
1292 }
1293 }
1294
1295 return 0;
1296}
1297__setup("video=", video_setup);
1298
1299 /*
1300 * Visible symbols for modules
1301 */
1302
1303EXPORT_SYMBOL(register_framebuffer);
1304EXPORT_SYMBOL(unregister_framebuffer);
1305EXPORT_SYMBOL(num_registered_fb);
1306EXPORT_SYMBOL(registered_fb);
1307EXPORT_SYMBOL(fb_prepare_logo);
1308EXPORT_SYMBOL(fb_show_logo);
1309EXPORT_SYMBOL(fb_set_var);
1310EXPORT_SYMBOL(fb_blank);
1311EXPORT_SYMBOL(fb_pan_display);
1312EXPORT_SYMBOL(fb_get_buffer_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313EXPORT_SYMBOL(fb_set_suspend);
1314EXPORT_SYMBOL(fb_register_client);
1315EXPORT_SYMBOL(fb_unregister_client);
1316EXPORT_SYMBOL(fb_get_options);
1317EXPORT_SYMBOL(fb_new_modelist);
1318
1319MODULE_LICENSE("GPL");