blob: cfd9dce1ce0b666627b6306e0eb693f669e154f5 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15
Arnd Bergmannc7f82d92005-11-08 21:39:19 -080016#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/smp_lock.h>
20#include <linux/kernel.h>
21#include <linux/major.h>
22#include <linux/slab.h>
23#include <linux/mm.h>
24#include <linux/mman.h>
Jon Smirla8f340e2006-07-10 04:44:12 -070025#include <linux/vt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/linux_logo.h>
28#include <linux/proc_fs.h>
Alexey Dobriyan0aa16342008-04-28 02:15:42 -070029#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/device.h>
34#include <linux/efi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/fb.h>
36
Antonino A. Daplas10eb2652007-07-17 04:05:27 -070037#include <asm/fb.h>
38
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /*
41 * Frame buffer device initialization and setup routines
42 */
43
44#define FBPIXMAPSIZE (1024 * 8)
45
Helge Deller0128bee2006-12-08 02:40:29 -080046struct fb_info *registered_fb[FB_MAX] __read_mostly;
47int num_registered_fb __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Helpers
51 */
52
Antonino A. Daplasb8c90942005-09-09 13:04:37 -070053int fb_get_color_depth(struct fb_var_screeninfo *var,
54 struct fb_fix_screeninfo *fix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Antonino A. Daplasb8c90942005-09-09 13:04:37 -070056 int depth = 0;
57
58 if (fix->visual == FB_VISUAL_MONO01 ||
59 fix->visual == FB_VISUAL_MONO10)
60 depth = 1;
61 else {
62 if (var->green.length == var->blue.length &&
63 var->green.length == var->red.length &&
64 var->green.offset == var->blue.offset &&
65 var->green.offset == var->red.offset)
66 depth = var->green.length;
67 else
68 depth = var->green.length + var->red.length +
69 var->blue.length;
70 }
71
72 return depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74EXPORT_SYMBOL(fb_get_color_depth);
75
76/*
James Simmonsf5a99512005-06-21 17:16:58 -070077 * Data padding functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
James Simmonsf1ab5da2005-06-21 17:17:07 -070079void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Antonino A. Daplas829e79b2005-09-09 13:10:04 -070081 __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
James Simmonsf1ab5da2005-06-21 17:17:07 -070083EXPORT_SYMBOL(fb_pad_aligned_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
James Simmonsf1ab5da2005-06-21 17:17:07 -070085void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
86 u32 shift_high, u32 shift_low, u32 mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 u8 mask = (u8) (0xfff << shift_high), tmp;
89 int i, j;
90
91 for (i = height; i--; ) {
92 for (j = 0; j < idx; j++) {
93 tmp = dst[j];
94 tmp &= mask;
95 tmp |= *src >> shift_low;
96 dst[j] = tmp;
97 tmp = *src << shift_high;
98 dst[j+1] = tmp;
99 src++;
100 }
101 tmp = dst[idx];
102 tmp &= mask;
103 tmp |= *src >> shift_low;
104 dst[idx] = tmp;
105 if (shift_high < mod) {
106 tmp = *src << shift_high;
107 dst[idx+1] = tmp;
108 }
109 src++;
110 dst += d_pitch;
111 }
112}
James Simmonsf1ab5da2005-06-21 17:17:07 -0700113EXPORT_SYMBOL(fb_pad_unaligned_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
116 * we need to lock this section since fb_cursor
117 * may use fb_imageblit()
118 */
119char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
120{
121 u32 align = buf->buf_align - 1, offset;
122 char *addr = buf->addr;
123
124 /* If IO mapped, we need to sync before access, no sharing of
125 * the pixmap is done
126 */
127 if (buf->flags & FB_PIXMAP_IO) {
128 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
129 info->fbops->fb_sync(info);
130 return addr;
131 }
132
133 /* See if we fit in the remaining pixmap space */
134 offset = buf->offset + align;
135 offset &= ~align;
136 if (offset + size > buf->size) {
137 /* We do not fit. In order to be able to re-use the buffer,
138 * we must ensure no asynchronous DMA'ing or whatever operation
139 * is in progress, we sync for that.
140 */
141 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
142 info->fbops->fb_sync(info);
143 offset = 0;
144 }
145 buf->offset = offset + size;
146 addr += offset;
147
148 return addr;
149}
150
151#ifdef CONFIG_LOGO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153static inline unsigned safe_shift(unsigned d, int n)
154{
155 return n < 0 ? d >> -n : d << n;
156}
157
158static void fb_set_logocmap(struct fb_info *info,
159 const struct linux_logo *logo)
160{
161 struct fb_cmap palette_cmap;
162 u16 palette_green[16];
163 u16 palette_blue[16];
164 u16 palette_red[16];
165 int i, j, n;
166 const unsigned char *clut = logo->clut;
167
168 palette_cmap.start = 0;
169 palette_cmap.len = 16;
170 palette_cmap.red = palette_red;
171 palette_cmap.green = palette_green;
172 palette_cmap.blue = palette_blue;
173 palette_cmap.transp = NULL;
174
175 for (i = 0; i < logo->clutsize; i += n) {
176 n = logo->clutsize - i;
177 /* palette_cmap provides space for only 16 colors at once */
178 if (n > 16)
179 n = 16;
180 palette_cmap.start = 32 + i;
181 palette_cmap.len = n;
182 for (j = 0; j < n; ++j) {
183 palette_cmap.red[j] = clut[0] << 8 | clut[0];
184 palette_cmap.green[j] = clut[1] << 8 | clut[1];
185 palette_cmap.blue[j] = clut[2] << 8 | clut[2];
186 clut += 3;
187 }
188 fb_set_cmap(&palette_cmap, info);
189 }
190}
191
192static void fb_set_logo_truepalette(struct fb_info *info,
193 const struct linux_logo *logo,
194 u32 *palette)
195{
Helge Deller0128bee2006-12-08 02:40:29 -0800196 static const unsigned char mask[] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned char redmask, greenmask, bluemask;
198 int redshift, greenshift, blueshift;
199 int i;
200 const unsigned char *clut = logo->clut;
201
202 /*
203 * We have to create a temporary palette since console palette is only
204 * 16 colors long.
205 */
206 /* Bug: Doesn't obey msb_right ... (who needs that?) */
207 redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8];
208 greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
209 bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8];
210 redshift = info->var.red.offset - (8 - info->var.red.length);
211 greenshift = info->var.green.offset - (8 - info->var.green.length);
212 blueshift = info->var.blue.offset - (8 - info->var.blue.length);
213
214 for ( i = 0; i < logo->clutsize; i++) {
215 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
216 safe_shift((clut[1] & greenmask), greenshift) |
217 safe_shift((clut[2] & bluemask), blueshift));
218 clut += 3;
219 }
220}
221
222static void fb_set_logo_directpalette(struct fb_info *info,
223 const struct linux_logo *logo,
224 u32 *palette)
225{
226 int redshift, greenshift, blueshift;
227 int i;
228
229 redshift = info->var.red.offset;
230 greenshift = info->var.green.offset;
231 blueshift = info->var.blue.offset;
232
Clemens Ladischcf7ee552008-11-19 15:36:10 -0800233 for (i = 32; i < 32 + logo->clutsize; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 palette[i] = i << redshift | i << greenshift | i << blueshift;
235}
236
237static void fb_set_logo(struct fb_info *info,
238 const struct linux_logo *logo, u8 *dst,
239 int depth)
240{
Antonino A. Daplasb8c90942005-09-09 13:04:37 -0700241 int i, j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 const u8 *src = logo->data;
Antonino A. Daplasb8c90942005-09-09 13:04:37 -0700243 u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
244 u8 fg = 1, d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Antonino A. Daplas1692b372007-07-31 00:37:36 -0700246 switch (fb_get_color_depth(&info->var, &info->fix)) {
247 case 1:
248 fg = 1;
249 break;
250 case 2:
251 fg = 3;
252 break;
253 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 fg = 7;
Antonino A. Daplas1692b372007-07-31 00:37:36 -0700255 break;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Antonino A. Daplasb8c90942005-09-09 13:04:37 -0700258 if (info->fix.visual == FB_VISUAL_MONO01 ||
259 info->fix.visual == FB_VISUAL_MONO10)
260 fg = ~((u8) (0xfff << info->var.green.length));
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 switch (depth) {
263 case 4:
264 for (i = 0; i < logo->height; i++)
265 for (j = 0; j < logo->width; src++) {
266 *dst++ = *src >> 4;
267 j++;
268 if (j < logo->width) {
269 *dst++ = *src & 0x0f;
270 j++;
271 }
272 }
273 break;
274 case 1:
275 for (i = 0; i < logo->height; i++) {
276 for (j = 0; j < logo->width; src++) {
277 d = *src ^ xor;
278 for (k = 7; k >= 0; k--) {
279 *dst++ = ((d >> k) & 1) ? fg : 0;
280 j++;
281 }
282 }
283 }
284 break;
285 }
286}
287
288/*
289 * Three (3) kinds of logo maps exist. linux_logo_clut224 (>16 colors),
290 * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors). Depending on
291 * the visual format and color depth of the framebuffer, the DAC, the
292 * pseudo_palette, and the logo data will be adjusted accordingly.
293 *
294 * Case 1 - linux_logo_clut224:
295 * Color exceeds the number of console colors (16), thus we set the hardware DAC
296 * using fb_set_cmap() appropriately. The "needs_cmapreset" flag will be set.
297 *
298 * For visuals that require color info from the pseudo_palette, we also construct
299 * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
300 * will be set.
301 *
302 * Case 2 - linux_logo_vga16:
303 * The number of colors just matches the console colors, thus there is no need
304 * to set the DAC or the pseudo_palette. However, the bitmap is packed, ie,
305 * each byte contains color information for two pixels (upper and lower nibble).
306 * To be consistent with fb_imageblit() usage, we therefore separate the two
307 * nibbles into separate bytes. The "depth" flag will be set to 4.
308 *
309 * Case 3 - linux_logo_mono:
310 * This is similar with Case 2. Each byte contains information for 8 pixels.
311 * We isolate each bit and expand each into a byte. The "depth" flag will
312 * be set to 1.
313 */
314static struct logo_data {
315 int depth;
316 int needs_directpalette;
317 int needs_truepalette;
318 int needs_cmapreset;
319 const struct linux_logo *logo;
Helge Deller0128bee2006-12-08 02:40:29 -0800320} fb_logo __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800322static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height)
323{
324 u32 size = width * height, i;
325
326 out += size - 1;
327
328 for (i = size; i--; )
329 *out-- = *in++;
330}
331
332static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height)
333{
Antonino A. Daplasf837e6f2006-06-26 00:26:56 -0700334 int i, j, h = height - 1;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800335
336 for (i = 0; i < height; i++)
337 for (j = 0; j < width; j++)
Antonino A. Daplasf837e6f2006-06-26 00:26:56 -0700338 out[height * j + h - i] = *in++;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800339}
340
341static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height)
342{
343 int i, j, w = width - 1;
344
345 for (i = 0; i < height; i++)
346 for (j = 0; j < width; j++)
347 out[height * (w - j) + i] = *in++;
348}
349
350static void fb_rotate_logo(struct fb_info *info, u8 *dst,
351 struct fb_image *image, int rotate)
352{
353 u32 tmp;
354
355 if (rotate == FB_ROTATE_UD) {
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800356 fb_rotate_logo_ud(image->data, dst, image->width,
357 image->height);
Geert Uytterhoevenabed5d12007-05-08 00:37:57 -0700358 image->dx = info->var.xres - image->width - image->dx;
359 image->dy = info->var.yres - image->height - image->dy;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800360 } else if (rotate == FB_ROTATE_CW) {
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800361 fb_rotate_logo_cw(image->data, dst, image->width,
362 image->height);
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800363 tmp = image->width;
364 image->width = image->height;
365 image->height = tmp;
Geert Uytterhoevenabed5d12007-05-08 00:37:57 -0700366 tmp = image->dy;
367 image->dy = image->dx;
368 image->dx = info->var.xres - image->width - tmp;
Antonino A. Daplasf837e6f2006-06-26 00:26:56 -0700369 } else if (rotate == FB_ROTATE_CCW) {
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800370 fb_rotate_logo_ccw(image->data, dst, image->width,
371 image->height);
Antonino A. Daplasf837e6f2006-06-26 00:26:56 -0700372 tmp = image->width;
373 image->width = image->height;
374 image->height = tmp;
Geert Uytterhoevenabed5d12007-05-08 00:37:57 -0700375 tmp = image->dx;
376 image->dx = image->dy;
377 image->dy = info->var.yres - image->height - tmp;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800378 }
379
380 image->data = dst;
381}
382
383static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700384 int rotate, unsigned int num)
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800385{
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700386 unsigned int x;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800387
388 if (rotate == FB_ROTATE_UR) {
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700389 for (x = 0;
390 x < num && image->dx + image->width <= info->var.xres;
391 x++) {
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800392 info->fbops->fb_imageblit(info, image);
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700393 image->dx += image->width + 8;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800394 }
395 } else if (rotate == FB_ROTATE_UD) {
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700396 for (x = 0; x < num && image->dx >= 0; x++) {
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800397 info->fbops->fb_imageblit(info, image);
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700398 image->dx -= image->width + 8;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800399 }
400 } else if (rotate == FB_ROTATE_CW) {
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700401 for (x = 0;
402 x < num && image->dy + image->height <= info->var.yres;
403 x++) {
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800404 info->fbops->fb_imageblit(info, image);
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700405 image->dy += image->height + 8;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800406 }
407 } else if (rotate == FB_ROTATE_CCW) {
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700408 for (x = 0; x < num && image->dy >= 0; x++) {
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800409 info->fbops->fb_imageblit(info, image);
Geert Uytterhoeven448d4792007-05-08 00:37:56 -0700410 image->dy -= image->height + 8;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800411 }
412 }
413}
414
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700415static int fb_show_logo_line(struct fb_info *info, int rotate,
416 const struct linux_logo *logo, int y,
417 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 u32 *palette = NULL, *saved_pseudo_palette = NULL;
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800420 unsigned char *logo_new = NULL, *logo_rotate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct fb_image image;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /* Return if the frame buffer is not mapped or suspended */
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700424 if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
Antonino A. Daplas70802c62007-05-08 00:38:14 -0700425 info->flags & FBINFO_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427
428 image.depth = 8;
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700429 image.data = logo->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (fb_logo.needs_cmapreset)
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700432 fb_set_logocmap(info, logo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Geert Uytterhoeven9900abf2007-07-17 04:05:50 -0700434 if (fb_logo.needs_truepalette ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 fb_logo.needs_directpalette) {
436 palette = kmalloc(256 * 4, GFP_KERNEL);
437 if (palette == NULL)
438 return 0;
439
440 if (fb_logo.needs_truepalette)
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700441 fb_set_logo_truepalette(info, logo, palette);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 else
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700443 fb_set_logo_directpalette(info, logo, palette);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 saved_pseudo_palette = info->pseudo_palette;
446 info->pseudo_palette = palette;
447 }
448
449 if (fb_logo.depth <= 4) {
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700450 logo_new = kmalloc(logo->width * logo->height, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (logo_new == NULL) {
452 kfree(palette);
453 if (saved_pseudo_palette)
454 info->pseudo_palette = saved_pseudo_palette;
455 return 0;
456 }
457 image.data = logo_new;
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700458 fb_set_logo(info, logo, logo_new, fb_logo.depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800461 image.dx = 0;
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700462 image.dy = y;
463 image.width = logo->width;
464 image.height = logo->height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800466 if (rotate) {
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700467 logo_rotate = kmalloc(logo->width *
468 logo->height, GFP_KERNEL);
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800469 if (logo_rotate)
470 fb_rotate_logo(info, logo_rotate, &image, rotate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800472
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700473 fb_do_show_logo(info, &image, rotate, n);
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 kfree(palette);
476 if (saved_pseudo_palette != NULL)
477 info->pseudo_palette = saved_pseudo_palette;
478 kfree(logo_new);
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800479 kfree(logo_rotate);
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700480 return logo->height;
481}
482
Geert Uytterhoeven9900abf2007-07-17 04:05:50 -0700483
484#ifdef CONFIG_FB_LOGO_EXTRA
485
486#define FB_LOGO_EX_NUM_MAX 10
487static struct logo_data_extra {
488 const struct linux_logo *logo;
489 unsigned int n;
490} fb_logo_ex[FB_LOGO_EX_NUM_MAX];
491static unsigned int fb_logo_ex_num;
492
493void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n)
494{
495 if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX)
496 return;
497
498 fb_logo_ex[fb_logo_ex_num].logo = logo;
499 fb_logo_ex[fb_logo_ex_num].n = n;
500 fb_logo_ex_num++;
501}
502
503static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
504 unsigned int yres)
505{
506 unsigned int i;
507
508 /* FIXME: logo_ex supports only truecolor fb. */
509 if (info->fix.visual != FB_VISUAL_TRUECOLOR)
510 fb_logo_ex_num = 0;
511
512 for (i = 0; i < fb_logo_ex_num; i++) {
Geert Uytterhoeven4fb6de22009-01-06 14:42:37 -0800513 if (fb_logo_ex[i].logo->type != fb_logo.logo->type) {
514 fb_logo_ex[i].logo = NULL;
515 continue;
516 }
Geert Uytterhoeven9900abf2007-07-17 04:05:50 -0700517 height += fb_logo_ex[i].logo->height;
518 if (height > yres) {
519 height -= fb_logo_ex[i].logo->height;
520 fb_logo_ex_num = i;
521 break;
522 }
523 }
524 return height;
525}
526
527static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
528{
529 unsigned int i;
530
531 for (i = 0; i < fb_logo_ex_num; i++)
532 y += fb_show_logo_line(info, rotate,
533 fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
534
535 return y;
536}
537
538#else /* !CONFIG_FB_LOGO_EXTRA */
539
540static inline int fb_prepare_extra_logos(struct fb_info *info,
541 unsigned int height,
542 unsigned int yres)
543{
544 return height;
545}
546
547static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
548{
549 return y;
550}
551
552#endif /* CONFIG_FB_LOGO_EXTRA */
553
554
555int fb_prepare_logo(struct fb_info *info, int rotate)
556{
557 int depth = fb_get_color_depth(&info->var, &info->fix);
558 unsigned int yres;
559
560 memset(&fb_logo, 0, sizeof(struct logo_data));
561
562 if (info->flags & FBINFO_MISC_TILEBLITTING ||
563 info->flags & FBINFO_MODULE)
564 return 0;
565
566 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
567 depth = info->var.blue.length;
568 if (info->var.red.length < depth)
569 depth = info->var.red.length;
570 if (info->var.green.length < depth)
571 depth = info->var.green.length;
572 }
573
574 if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
575 /* assume console colormap */
576 depth = 4;
577 }
578
Geert Uytterhoeven9900abf2007-07-17 04:05:50 -0700579 /* Return if no suitable logo was found */
580 fb_logo.logo = fb_find_logo(depth);
581
582 if (!fb_logo.logo) {
583 return 0;
584 }
585
586 if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
587 yres = info->var.yres;
588 else
589 yres = info->var.xres;
590
591 if (fb_logo.logo->height > yres) {
592 fb_logo.logo = NULL;
593 return 0;
594 }
595
596 /* What depth we asked for might be different from what we get */
597 if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
598 fb_logo.depth = 8;
599 else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
600 fb_logo.depth = 4;
601 else
602 fb_logo.depth = 1;
603
Antonino A. Daplas1692b372007-07-31 00:37:36 -0700604
605 if (fb_logo.depth > 4 && depth > 4) {
606 switch (info->fix.visual) {
607 case FB_VISUAL_TRUECOLOR:
608 fb_logo.needs_truepalette = 1;
609 break;
610 case FB_VISUAL_DIRECTCOLOR:
611 fb_logo.needs_directpalette = 1;
612 fb_logo.needs_cmapreset = 1;
613 break;
614 case FB_VISUAL_PSEUDOCOLOR:
615 fb_logo.needs_cmapreset = 1;
616 break;
617 }
618 }
619
Geert Uytterhoeven9900abf2007-07-17 04:05:50 -0700620 return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
621}
622
Geert Uytterhoeven90da63e52007-07-17 04:05:50 -0700623int fb_show_logo(struct fb_info *info, int rotate)
624{
Geert Uytterhoeven9900abf2007-07-17 04:05:50 -0700625 int y;
626
627 y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
628 num_online_cpus());
629 y = fb_show_extra_logos(info, y, rotate);
630
631 return y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633#else
Antonino A. Daplas9c44e5f2005-11-08 21:39:10 -0800634int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
635int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#endif /* CONFIG_LOGO */
637
Alexey Dobriyan0aa16342008-04-28 02:15:42 -0700638static void *fb_seq_start(struct seq_file *m, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Alexey Dobriyan0aa16342008-04-28 02:15:42 -0700640 return (*pos < FB_MAX) ? pos : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Alexey Dobriyan0aa16342008-04-28 02:15:42 -0700643static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
644{
645 (*pos)++;
646 return (*pos < FB_MAX) ? pos : NULL;
647}
648
649static void fb_seq_stop(struct seq_file *m, void *v)
650{
651}
652
653static int fb_seq_show(struct seq_file *m, void *v)
654{
655 int i = *(loff_t *)v;
656 struct fb_info *fi = registered_fb[i];
657
658 if (fi)
659 seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
660 return 0;
661}
662
663static const struct seq_operations proc_fb_seq_ops = {
664 .start = fb_seq_start,
665 .next = fb_seq_next,
666 .stop = fb_seq_stop,
667 .show = fb_seq_show,
668};
669
670static int proc_fb_open(struct inode *inode, struct file *file)
671{
672 return seq_open(file, &proc_fb_seq_ops);
673}
674
675static const struct file_operations fb_proc_fops = {
676 .owner = THIS_MODULE,
677 .open = proc_fb_open,
678 .read = seq_read,
679 .llseek = seq_lseek,
680 .release = seq_release,
681};
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static ssize_t
684fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
685{
686 unsigned long p = *ppos;
Josef Sipekad9a8242006-12-08 02:37:48 -0800687 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int fbidx = iminor(inode);
689 struct fb_info *info = registered_fb[fbidx];
690 u32 *buffer, *dst;
691 u32 __iomem *src;
692 int c, i, cnt = 0, err = 0;
693 unsigned long total_size;
694
695 if (!info || ! info->screen_base)
696 return -ENODEV;
697
698 if (info->state != FBINFO_STATE_RUNNING)
699 return -EPERM;
700
701 if (info->fbops->fb_read)
Antonino A. Daplas3f9b0882007-05-08 00:39:02 -0700702 return info->fbops->fb_read(info, buf, count, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 total_size = info->screen_size;
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (total_size == 0)
707 total_size = info->fix.smem_len;
708
709 if (p >= total_size)
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800710 return 0;
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (count >= total_size)
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800713 count = total_size;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (count + p > total_size)
716 count = total_size - p;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
719 GFP_KERNEL);
720 if (!buffer)
721 return -ENOMEM;
722
723 src = (u32 __iomem *) (info->screen_base + p);
724
725 if (info->fbops->fb_sync)
726 info->fbops->fb_sync(info);
727
728 while (count) {
729 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
730 dst = buffer;
731 for (i = c >> 2; i--; )
732 *dst++ = fb_readl(src++);
733 if (c & 3) {
734 u8 *dst8 = (u8 *) dst;
735 u8 __iomem *src8 = (u8 __iomem *) src;
736
737 for (i = c & 3; i--;)
738 *dst8++ = fb_readb(src8++);
739
740 src = (u32 __iomem *) src8;
741 }
742
743 if (copy_to_user(buf, buffer, c)) {
744 err = -EFAULT;
745 break;
746 }
747 *ppos += c;
748 buf += c;
749 cnt += c;
750 count -= c;
751 }
752
753 kfree(buffer);
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return (err) ? err : cnt;
756}
757
758static ssize_t
759fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
760{
761 unsigned long p = *ppos;
Josef Sipekad9a8242006-12-08 02:37:48 -0800762 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int fbidx = iminor(inode);
764 struct fb_info *info = registered_fb[fbidx];
765 u32 *buffer, *src;
766 u32 __iomem *dst;
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800767 int c, i, cnt = 0, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 unsigned long total_size;
769
770 if (!info || !info->screen_base)
771 return -ENODEV;
772
773 if (info->state != FBINFO_STATE_RUNNING)
774 return -EPERM;
775
776 if (info->fbops->fb_write)
Antonino A. Daplas3f9b0882007-05-08 00:39:02 -0700777 return info->fbops->fb_write(info, buf, count, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 total_size = info->screen_size;
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (total_size == 0)
782 total_size = info->fix.smem_len;
783
784 if (p > total_size)
Antonino A. Daplas6a2a8862006-04-18 22:22:12 -0700785 return -EFBIG;
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800786
Antonino A. Daplas6a2a8862006-04-18 22:22:12 -0700787 if (count > total_size) {
788 err = -EFBIG;
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800789 count = total_size;
Antonino A. Daplas6a2a8862006-04-18 22:22:12 -0700790 }
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800791
Antonino A. Daplas6a2a8862006-04-18 22:22:12 -0700792 if (count + p > total_size) {
793 if (!err)
794 err = -ENOSPC;
795
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800796 count = total_size - p;
Antonino A. Daplas6a2a8862006-04-18 22:22:12 -0700797 }
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
800 GFP_KERNEL);
801 if (!buffer)
802 return -ENOMEM;
803
804 dst = (u32 __iomem *) (info->screen_base + p);
805
806 if (info->fbops->fb_sync)
807 info->fbops->fb_sync(info);
808
809 while (count) {
810 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
811 src = buffer;
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (copy_from_user(src, buf, c)) {
814 err = -EFAULT;
815 break;
816 }
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 for (i = c >> 2; i--; )
819 fb_writel(*src++, dst++);
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (c & 3) {
822 u8 *src8 = (u8 *) src;
823 u8 __iomem *dst8 = (u8 __iomem *) dst;
824
825 for (i = c & 3; i--; )
826 fb_writeb(*src8++, dst8++);
827
828 dst = (u32 __iomem *) dst8;
829 }
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *ppos += c;
832 buf += c;
833 cnt += c;
834 count -= c;
835 }
Antonino A. Daplas0a484a32006-01-09 20:53:37 -0800836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 kfree(buffer);
838
Antonino A. Daplas6a2a8862006-04-18 22:22:12 -0700839 return (cnt) ? cnt : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842int
843fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
844{
Antonino A. Daplas12070692005-12-12 22:17:17 -0800845 struct fb_fix_screeninfo *fix = &info->fix;
Ville Syrjala7572a1e2008-07-23 21:31:28 -0700846 unsigned int yres = info->var.yres;
847 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Antonino A. Daplas12070692005-12-12 22:17:17 -0800849 if (var->yoffset > 0) {
850 if (var->vmode & FB_VMODE_YWRAP) {
851 if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
852 err = -EINVAL;
853 else
854 yres = 0;
855 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
856 err = -EINVAL;
857 }
858
859 if (var->xoffset > 0 && (!fix->xpanstep ||
860 (var->xoffset % fix->xpanstep)))
861 err = -EINVAL;
862
Ville Syrjala7572a1e2008-07-23 21:31:28 -0700863 if (err || !info->fbops->fb_pan_display ||
864 var->yoffset + yres > info->var.yres_virtual ||
Antonino A. Daplas12070692005-12-12 22:17:17 -0800865 var->xoffset + info->var.xres > info->var.xres_virtual)
866 return -EINVAL;
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if ((err = info->fbops->fb_pan_display(var, info)))
869 return err;
870 info->var.xoffset = var->xoffset;
871 info->var.yoffset = var->yoffset;
872 if (var->vmode & FB_VMODE_YWRAP)
873 info->var.vmode |= FB_VMODE_YWRAP;
874 else
875 info->var.vmode &= ~FB_VMODE_YWRAP;
876 return 0;
877}
878
Antonino A. Daplas38a3dc52007-05-08 00:39:37 -0700879static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
880 u32 activate)
881{
882 struct fb_event event;
883 struct fb_blit_caps caps, fbcaps;
884 int err = 0;
885
886 memset(&caps, 0, sizeof(caps));
887 memset(&fbcaps, 0, sizeof(fbcaps));
888 caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
889 event.info = info;
890 event.data = &caps;
891 fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
892 info->fbops->fb_get_caps(info, &fbcaps, var);
893
894 if (((fbcaps.x ^ caps.x) & caps.x) ||
895 ((fbcaps.y ^ caps.y) & caps.y) ||
896 (fbcaps.len < caps.len))
897 err = -EINVAL;
898
899 return err;
900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902int
903fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
904{
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700905 int flags = info->flags;
906 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (var->activate & FB_ACTIVATE_INV_MODE) {
909 struct fb_videomode mode1, mode2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 fb_var_to_videomode(&mode1, var);
912 fb_var_to_videomode(&mode2, &info->var);
913 /* make sure we don't delete the videomode of current var */
914 ret = fb_mode_is_equal(&mode1, &mode2);
915
916 if (!ret) {
917 struct fb_event event;
918
919 event.info = info;
920 event.data = &mode1;
Antonino A. Daplas256154f2006-07-30 03:04:17 -0700921 ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
924 if (!ret)
925 fb_delete_videomode(&mode1, &info->modelist);
926
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700927
928 ret = (ret) ? -EINVAL : 0;
929 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
932 if ((var->activate & FB_ACTIVATE_FORCE) ||
933 memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
Antonino A. Daplas4941cb72007-05-08 00:39:22 -0700934 u32 activate = var->activate;
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (!info->fbops->fb_check_var) {
937 *var = info->var;
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700938 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700941 ret = info->fbops->fb_check_var(var, info);
942
943 if (ret)
944 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
947 struct fb_videomode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Antonino A. Daplas38a3dc52007-05-08 00:39:37 -0700949 if (info->fbops->fb_get_caps) {
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700950 ret = fb_check_caps(info, var, activate);
Antonino A. Daplas38a3dc52007-05-08 00:39:37 -0700951
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700952 if (ret)
Antonino A. Daplas38a3dc52007-05-08 00:39:37 -0700953 goto done;
954 }
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 info->var = *var;
Antonino A. Daplas38a3dc52007-05-08 00:39:37 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (info->fbops->fb_set_par)
959 info->fbops->fb_set_par(info);
960
961 fb_pan_display(info, &info->var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 fb_set_cmap(&info->cmap, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 fb_var_to_videomode(&mode, &info->var);
964
965 if (info->modelist.prev && info->modelist.next &&
966 !list_empty(&info->modelist))
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700967 ret = fb_add_videomode(&mode, &info->modelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700969 if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct fb_event event;
Antonino A. Daplas4941cb72007-05-08 00:39:22 -0700971 int evnt = (activate & FB_ACTIVATE_ALL) ?
Antonino A. Daplas7726e9e2005-09-09 13:04:29 -0700972 FB_EVENT_MODE_CHANGE_ALL :
973 FB_EVENT_MODE_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 info->flags &= ~FBINFO_MISC_USEREVENT;
976 event.info = info;
Eric Miaofaa312d2008-08-29 04:18:43 +0800977 event.data = &mode;
Antonino A. Daplas256154f2006-07-30 03:04:17 -0700978 fb_notifier_call_chain(evnt, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 }
981 }
Antonino A. Daplas38a3dc52007-05-08 00:39:37 -0700982
983 done:
Antonino A. Daplasb1e72232007-05-08 00:39:52 -0700984 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987int
988fb_blank(struct fb_info *info, int blank)
989{
990 int ret = -EINVAL;
991
992 if (blank > FB_BLANK_POWERDOWN)
993 blank = FB_BLANK_POWERDOWN;
994
995 if (info->fbops->fb_blank)
996 ret = info->fbops->fb_blank(blank, info);
997
998 if (!ret) {
999 struct fb_event event;
1000
1001 event.info = info;
1002 event.data = &blank;
Antonino A. Daplas256154f2006-07-30 03:04:17 -07001003 fb_notifier_call_chain(FB_EVENT_BLANK, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
1006 return ret;
1007}
1008
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001009static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
1010 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Alan Coxe5367712008-10-18 20:27:51 -07001012 struct fb_ops *fb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct fb_var_screeninfo var;
1014 struct fb_fix_screeninfo fix;
1015 struct fb_con2fbmap con2fb;
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001016 struct fb_cmap cmap_from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct fb_cmap_user cmap;
1018 struct fb_event event;
1019 void __user *argp = (void __user *)arg;
Alan Coxe5367712008-10-18 20:27:51 -07001020 long ret = 0;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 switch (cmd) {
1023 case FBIOGET_VSCREENINFO:
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001024 if (!lock_fb_info(info))
1025 return -ENODEV;
1026 var = info->var;
1027 unlock_fb_info(info);
1028
1029 ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
Alan Coxe5367712008-10-18 20:27:51 -07001030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 case FBIOPUT_VSCREENINFO:
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001032 if (copy_from_user(&var, argp, sizeof(var)))
1033 return -EFAULT;
1034 if (!lock_fb_info(info))
1035 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 acquire_console_sem();
1037 info->flags |= FBINFO_MISC_USEREVENT;
Alan Coxe5367712008-10-18 20:27:51 -07001038 ret = fb_set_var(info, &var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 info->flags &= ~FBINFO_MISC_USEREVENT;
1040 release_console_sem();
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001041 unlock_fb_info(info);
1042 if (!ret && copy_to_user(argp, &var, sizeof(var)))
Alan Coxe5367712008-10-18 20:27:51 -07001043 ret = -EFAULT;
1044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 case FBIOGET_FSCREENINFO:
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001046 if (!lock_fb_info(info))
1047 return -ENODEV;
1048 fix = info->fix;
1049 unlock_fb_info(info);
1050
1051 ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
Alan Coxe5367712008-10-18 20:27:51 -07001052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 case FBIOPUTCMAP:
1054 if (copy_from_user(&cmap, argp, sizeof(cmap)))
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001055 return -EFAULT;
1056 ret = fb_set_user_cmap(&cmap, info);
Alan Coxe5367712008-10-18 20:27:51 -07001057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 case FBIOGETCMAP:
1059 if (copy_from_user(&cmap, argp, sizeof(cmap)))
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001060 return -EFAULT;
1061 if (!lock_fb_info(info))
1062 return -ENODEV;
1063 cmap_from = info->cmap;
1064 unlock_fb_info(info);
1065 ret = fb_cmap_to_user(&cmap_from, &cmap);
Alan Coxe5367712008-10-18 20:27:51 -07001066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 case FBIOPAN_DISPLAY:
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001068 if (copy_from_user(&var, argp, sizeof(var)))
1069 return -EFAULT;
1070 if (!lock_fb_info(info))
1071 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 acquire_console_sem();
Alan Coxe5367712008-10-18 20:27:51 -07001073 ret = fb_pan_display(info, &var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 release_console_sem();
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001075 unlock_fb_info(info);
Alan Coxe5367712008-10-18 20:27:51 -07001076 if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001077 return -EFAULT;
Alan Coxe5367712008-10-18 20:27:51 -07001078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 case FBIO_CURSOR:
Alan Coxe5367712008-10-18 20:27:51 -07001080 ret = -EINVAL;
1081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 case FBIOGET_CON2FBMAP:
1083 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001084 return -EFAULT;
1085 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1086 return -EINVAL;
1087 con2fb.framebuffer = -1;
1088 event.data = &con2fb;
1089
1090 if (!lock_fb_info(info))
1091 return -ENODEV;
1092 event.info = info;
1093 fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
1094 unlock_fb_info(info);
1095
1096 ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
Alan Coxe5367712008-10-18 20:27:51 -07001097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 case FBIOPUT_CON2FBMAP:
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001099 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1100 return -EFAULT;
1101 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1102 return -EINVAL;
1103 if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
1104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (!registered_fb[con2fb.framebuffer])
Alan Coxe5367712008-10-18 20:27:51 -07001106 request_module("fb%d", con2fb.framebuffer);
1107 if (!registered_fb[con2fb.framebuffer]) {
1108 ret = -EINVAL;
1109 break;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 event.data = &con2fb;
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001112 if (!lock_fb_info(info))
1113 return -ENODEV;
1114 event.info = info;
Alan Coxe5367712008-10-18 20:27:51 -07001115 ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP,
Antonino A. Daplas256154f2006-07-30 03:04:17 -07001116 &event);
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001117 unlock_fb_info(info);
Alan Coxe5367712008-10-18 20:27:51 -07001118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 case FBIOBLANK:
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001120 if (!lock_fb_info(info))
1121 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 acquire_console_sem();
1123 info->flags |= FBINFO_MISC_USEREVENT;
Alan Coxe5367712008-10-18 20:27:51 -07001124 ret = fb_blank(info, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 info->flags &= ~FBINFO_MISC_USEREVENT;
1126 release_console_sem();
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001127 unlock_fb_info(info);
1128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 default:
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001130 if (!lock_fb_info(info))
1131 return -ENODEV;
1132 fb = info->fbops;
1133 if (fb->fb_ioctl)
Alan Coxe5367712008-10-18 20:27:51 -07001134 ret = fb->fb_ioctl(info, cmd, arg);
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001135 else
1136 ret = -ENOTTY;
1137 unlock_fb_info(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001139 return ret;
1140}
1141
1142static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001143{
1144 struct inode *inode = file->f_path.dentry->d_inode;
1145 int fbidx = iminor(inode);
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001146 struct fb_info *info = registered_fb[fbidx];
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001147
Andrea Righi1f5e31d2009-02-04 15:12:03 -08001148 return do_fb_ioctl(info, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151#ifdef CONFIG_COMPAT
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001152struct fb_fix_screeninfo32 {
1153 char id[16];
1154 compat_caddr_t smem_start;
1155 u32 smem_len;
1156 u32 type;
1157 u32 type_aux;
1158 u32 visual;
1159 u16 xpanstep;
1160 u16 ypanstep;
1161 u16 ywrapstep;
1162 u32 line_length;
1163 compat_caddr_t mmio_start;
1164 u32 mmio_len;
1165 u32 accel;
1166 u16 reserved[3];
1167};
1168
1169struct fb_cmap32 {
1170 u32 start;
1171 u32 len;
1172 compat_caddr_t red;
1173 compat_caddr_t green;
1174 compat_caddr_t blue;
1175 compat_caddr_t transp;
1176};
1177
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001178static int fb_getput_cmap(struct fb_info *info, unsigned int cmd,
1179 unsigned long arg)
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001180{
1181 struct fb_cmap_user __user *cmap;
1182 struct fb_cmap32 __user *cmap32;
1183 __u32 data;
1184 int err;
1185
1186 cmap = compat_alloc_user_space(sizeof(*cmap));
1187 cmap32 = compat_ptr(arg);
1188
1189 if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
1190 return -EFAULT;
1191
1192 if (get_user(data, &cmap32->red) ||
1193 put_user(compat_ptr(data), &cmap->red) ||
1194 get_user(data, &cmap32->green) ||
1195 put_user(compat_ptr(data), &cmap->green) ||
1196 get_user(data, &cmap32->blue) ||
1197 put_user(compat_ptr(data), &cmap->blue) ||
1198 get_user(data, &cmap32->transp) ||
1199 put_user(compat_ptr(data), &cmap->transp))
1200 return -EFAULT;
1201
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001202 err = do_fb_ioctl(info, cmd, (unsigned long) cmap);
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001203
1204 if (!err) {
1205 if (copy_in_user(&cmap32->start,
1206 &cmap->start,
1207 2 * sizeof(__u32)))
1208 err = -EFAULT;
1209 }
1210 return err;
1211}
1212
1213static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
1214 struct fb_fix_screeninfo32 __user *fix32)
1215{
1216 __u32 data;
1217 int err;
1218
1219 err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
1220
1221 data = (__u32) (unsigned long) fix->smem_start;
1222 err |= put_user(data, &fix32->smem_start);
1223
1224 err |= put_user(fix->smem_len, &fix32->smem_len);
1225 err |= put_user(fix->type, &fix32->type);
1226 err |= put_user(fix->type_aux, &fix32->type_aux);
1227 err |= put_user(fix->visual, &fix32->visual);
1228 err |= put_user(fix->xpanstep, &fix32->xpanstep);
1229 err |= put_user(fix->ypanstep, &fix32->ypanstep);
1230 err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
1231 err |= put_user(fix->line_length, &fix32->line_length);
1232
1233 data = (__u32) (unsigned long) fix->mmio_start;
1234 err |= put_user(data, &fix32->mmio_start);
1235
1236 err |= put_user(fix->mmio_len, &fix32->mmio_len);
1237 err |= put_user(fix->accel, &fix32->accel);
1238 err |= copy_to_user(fix32->reserved, fix->reserved,
1239 sizeof(fix->reserved));
1240
1241 return err;
1242}
1243
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001244static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd,
1245 unsigned long arg)
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001246{
1247 mm_segment_t old_fs;
1248 struct fb_fix_screeninfo fix;
1249 struct fb_fix_screeninfo32 __user *fix32;
1250 int err;
1251
1252 fix32 = compat_ptr(arg);
1253
1254 old_fs = get_fs();
1255 set_fs(KERNEL_DS);
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001256 err = do_fb_ioctl(info, cmd, (unsigned long) &fix);
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001257 set_fs(old_fs);
1258
1259 if (!err)
1260 err = do_fscreeninfo_to_user(&fix, fix32);
1261
1262 return err;
1263}
1264
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001265static long fb_compat_ioctl(struct file *file, unsigned int cmd,
1266 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Josef Sipekad9a8242006-12-08 02:37:48 -08001268 struct inode *inode = file->f_path.dentry->d_inode;
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001269 int fbidx = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct fb_info *info = registered_fb[fbidx];
1271 struct fb_ops *fb = info->fbops;
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001272 long ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001274 switch(cmd) {
1275 case FBIOGET_VSCREENINFO:
1276 case FBIOPUT_VSCREENINFO:
1277 case FBIOPAN_DISPLAY:
1278 case FBIOGET_CON2FBMAP:
1279 case FBIOPUT_CON2FBMAP:
1280 arg = (unsigned long) compat_ptr(arg);
1281 case FBIOBLANK:
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001282 ret = do_fb_ioctl(info, cmd, arg);
1283 break;
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001284
1285 case FBIOGET_FSCREENINFO:
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001286 ret = fb_get_fscreeninfo(info, cmd, arg);
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001287 break;
1288
1289 case FBIOGETCMAP:
1290 case FBIOPUTCMAP:
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001291 ret = fb_getput_cmap(info, cmd, arg);
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001292 break;
1293
1294 default:
1295 if (fb->fb_compat_ioctl)
Christoph Hellwig67a66802006-01-14 13:21:25 -08001296 ret = fb->fb_compat_ioctl(info, cmd, arg);
Arnd Bergmannc7f82d92005-11-08 21:39:19 -08001297 break;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return ret;
1300}
1301#endif
1302
Antonino A. Daplas10eb2652007-07-17 04:05:27 -07001303static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304fb_mmap(struct file *file, struct vm_area_struct * vma)
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001305__acquires(&info->lock)
1306__releases(&info->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Josef Sipekad9a8242006-12-08 02:37:48 -08001308 int fbidx = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct fb_info *info = registered_fb[fbidx];
1310 struct fb_ops *fb = info->fbops;
1311 unsigned long off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 unsigned long start;
1313 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1316 return -EINVAL;
1317 off = vma->vm_pgoff << PAGE_SHIFT;
1318 if (!fb)
1319 return -ENODEV;
1320 if (fb->fb_mmap) {
1321 int res;
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001322 mutex_lock(&info->lock);
Christoph Hellwig216d5262006-01-14 13:21:25 -08001323 res = fb->fb_mmap(info, vma);
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001324 mutex_unlock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return res;
1326 }
1327
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001328 mutex_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* frame buffer memory */
1331 start = info->fix.smem_start;
1332 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
1333 if (off >= len) {
1334 /* memory mapped io */
1335 off -= len;
1336 if (info->var.accel_flags) {
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001337 mutex_unlock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EINVAL;
1339 }
1340 start = info->fix.mmio_start;
1341 len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
1342 }
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001343 mutex_unlock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 start &= PAGE_MASK;
1345 if ((vma->vm_end - vma->vm_start + off) > len)
1346 return -EINVAL;
1347 off += start;
1348 vma->vm_pgoff = off >> PAGE_SHIFT;
1349 /* This is an IO map - tell maydump to skip this VMA */
1350 vma->vm_flags |= VM_IO | VM_RESERVED;
Antonino A. Daplas10eb2652007-07-17 04:05:27 -07001351 fb_pgprotect(file, vma, off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1353 vma->vm_end - vma->vm_start, vma->vm_page_prot))
1354 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358static int
1359fb_open(struct inode *inode, struct file *file)
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001360__acquires(&info->lock)
1361__releases(&info->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 int fbidx = iminor(inode);
1364 struct fb_info *info;
1365 int res = 0;
1366
1367 if (fbidx >= FB_MAX)
1368 return -ENODEV;
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001369 info = registered_fb[fbidx];
1370 if (!info)
Johannes Berga65e5d72008-07-09 10:28:38 +02001371 request_module("fb%d", fbidx);
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001372 info = registered_fb[fbidx];
1373 if (!info)
1374 return -ENODEV;
1375 mutex_lock(&info->lock);
Jonathan Corbetfc7f6872008-05-15 16:30:36 -06001376 if (!try_module_get(info->fbops->owner)) {
1377 res = -ENODEV;
1378 goto out;
1379 }
Thomas Koellercae8a122006-01-09 20:53:48 -08001380 file->private_data = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (info->fbops->fb_open) {
1382 res = info->fbops->fb_open(info,1);
1383 if (res)
1384 module_put(info->fbops->owner);
1385 }
Ian Campbelld8474712008-08-20 14:09:23 -07001386#ifdef CONFIG_FB_DEFERRED_IO
1387 if (info->fbdefio)
1388 fb_deferred_io_open(info, inode, file);
1389#endif
Jonathan Corbetfc7f6872008-05-15 16:30:36 -06001390out:
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001391 mutex_unlock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return res;
1393}
1394
1395static int
1396fb_release(struct inode *inode, struct file *file)
Geert Uytterhoevena684e7d2008-11-06 12:53:37 -08001397__acquires(&info->lock)
1398__releases(&info->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Thomas Koellercae8a122006-01-09 20:53:48 -08001400 struct fb_info * const info = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001402 mutex_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (info->fbops->fb_release)
1404 info->fbops->fb_release(info,1);
1405 module_put(info->fbops->owner);
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001406 mutex_unlock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return 0;
1408}
1409
Helge Deller0128bee2006-12-08 02:40:29 -08001410static const struct file_operations fb_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 .owner = THIS_MODULE,
1412 .read = fb_read,
1413 .write = fb_write,
Alan Coxe5367712008-10-18 20:27:51 -07001414 .unlocked_ioctl = fb_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415#ifdef CONFIG_COMPAT
1416 .compat_ioctl = fb_compat_ioctl,
1417#endif
1418 .mmap = fb_mmap,
1419 .open = fb_open,
1420 .release = fb_release,
1421#ifdef HAVE_ARCH_FB_UNMAPPED_AREA
1422 .get_unmapped_area = get_fb_unmapped_area,
1423#endif
Paul Mundt5e841b82007-05-08 00:37:41 -07001424#ifdef CONFIG_FB_DEFERRED_IO
1425 .fsync = fb_deferred_io_fsync,
1426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427};
1428
Antonino A. Daplas9a179172006-06-26 00:27:05 -07001429struct class *fb_class;
1430EXPORT_SYMBOL(fb_class);
Anton Vorontsove4c690e2008-04-28 02:14:49 -07001431
1432static int fb_check_foreignness(struct fb_info *fi)
1433{
1434 const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
1435
1436 fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
1437
1438#ifdef __BIG_ENDIAN
1439 fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
1440#else
1441 fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
1442#endif /* __BIG_ENDIAN */
1443
1444 if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
1445 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
1446 "support this framebuffer\n", fi->fix.id);
1447 return -ENOSYS;
1448 } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
1449 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
1450 "support this framebuffer\n", fi->fix.id);
1451 return -ENOSYS;
1452 }
1453
1454 return 0;
1455}
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457/**
1458 * register_framebuffer - registers a frame buffer device
1459 * @fb_info: frame buffer info structure
1460 *
1461 * Registers a frame buffer device @fb_info.
1462 *
1463 * Returns negative errno on error, or zero for success.
1464 *
1465 */
1466
1467int
1468register_framebuffer(struct fb_info *fb_info)
1469{
1470 int i;
1471 struct fb_event event;
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -07001472 struct fb_videomode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (num_registered_fb == FB_MAX)
1475 return -ENXIO;
Anton Vorontsove4c690e2008-04-28 02:14:49 -07001476
1477 if (fb_check_foreignness(fb_info))
1478 return -ENOSYS;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 num_registered_fb++;
1481 for (i = 0 ; i < FB_MAX; i++)
1482 if (!registered_fb[i])
1483 break;
1484 fb_info->node = i;
Krzysztof Helt3e680aa2008-10-18 20:27:51 -07001485 mutex_init(&fb_info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Greg Kroah-Hartman77997aa2008-07-21 20:03:34 -07001487 fb_info->dev = device_create(fb_class, fb_info->device,
1488 MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -07001489 if (IS_ERR(fb_info->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /* Not fatal */
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -07001491 printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
1492 fb_info->dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 } else
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -07001494 fb_init_device(fb_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 if (fb_info->pixmap.addr == NULL) {
1497 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1498 if (fb_info->pixmap.addr) {
1499 fb_info->pixmap.size = FBPIXMAPSIZE;
1500 fb_info->pixmap.buf_align = 1;
1501 fb_info->pixmap.scan_align = 1;
James Simmons58a60642005-06-21 17:17:08 -07001502 fb_info->pixmap.access_align = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1504 }
1505 }
1506 fb_info->pixmap.offset = 0;
1507
Antonino A. Daplasbf26ad72007-05-08 00:39:09 -07001508 if (!fb_info->pixmap.blit_x)
1509 fb_info->pixmap.blit_x = ~(u32)0;
1510
1511 if (!fb_info->pixmap.blit_y)
1512 fb_info->pixmap.blit_y = ~(u32)0;
1513
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -07001514 if (!fb_info->modelist.prev || !fb_info->modelist.next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 INIT_LIST_HEAD(&fb_info->modelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -07001517 fb_var_to_videomode(&mode, &fb_info->var);
1518 fb_add_videomode(&mode, &fb_info->modelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 registered_fb[i] = fb_info;
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 event.info = fb_info;
Antonino A. Daplas256154f2006-07-30 03:04:17 -07001522 fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
1524}
1525
1526
1527/**
1528 * unregister_framebuffer - releases a frame buffer device
1529 * @fb_info: frame buffer info structure
1530 *
1531 * Unregisters a frame buffer device @fb_info.
1532 *
1533 * Returns negative errno on error, or zero for success.
1534 *
Jesse Barnescfafca82007-07-17 04:05:33 -07001535 * This function will also notify the framebuffer console
1536 * to release the driver.
1537 *
1538 * This is meant to be called within a driver's module_exit()
1539 * function. If this is called outside module_exit(), ensure
1540 * that the driver implements fb_open() and fb_release() to
1541 * check that no processes are using the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
1543
1544int
1545unregister_framebuffer(struct fb_info *fb_info)
1546{
Antonino A. Daplase614b182006-06-26 00:27:09 -07001547 struct fb_event event;
Jesse Barnescfafca82007-07-17 04:05:33 -07001548 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 i = fb_info->node;
Jesse Barnescfafca82007-07-17 04:05:33 -07001551 if (!registered_fb[i]) {
1552 ret = -EINVAL;
1553 goto done;
1554 }
1555
1556 event.info = fb_info;
1557 ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
1558
1559 if (ret) {
1560 ret = -EINVAL;
1561 goto done;
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Antonino A. Daplase614b182006-06-26 00:27:09 -07001564 if (fb_info->pixmap.addr &&
1565 (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 kfree(fb_info->pixmap.addr);
1567 fb_destroy_modelist(&fb_info->modelist);
1568 registered_fb[i]=NULL;
1569 num_registered_fb--;
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -07001570 fb_cleanup_device(fb_info);
1571 device_destroy(fb_class, MKDEV(FB_MAJOR, i));
Antonino A. Daplase614b182006-06-26 00:27:09 -07001572 event.info = fb_info;
Antonino A. Daplas256154f2006-07-30 03:04:17 -07001573 fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
Jesse Barnescfafca82007-07-17 04:05:33 -07001574done:
1575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * fb_set_suspend - low level driver signals suspend
1580 * @info: framebuffer affected
1581 * @state: 0 = resuming, !=0 = suspending
1582 *
1583 * This is meant to be used by low level drivers to
1584 * signal suspend/resume to the core & clients.
1585 * It must be called with the console semaphore held
1586 */
1587void fb_set_suspend(struct fb_info *info, int state)
1588{
1589 struct fb_event event;
1590
1591 event.info = info;
1592 if (state) {
Antonino A. Daplas256154f2006-07-30 03:04:17 -07001593 fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 info->state = FBINFO_STATE_SUSPENDED;
1595 } else {
1596 info->state = FBINFO_STATE_RUNNING;
Antonino A. Daplas256154f2006-07-30 03:04:17 -07001597 fb_notifier_call_chain(FB_EVENT_RESUME, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599}
1600
1601/**
1602 * fbmem_init - init frame buffer subsystem
1603 *
1604 * Initialize the frame buffer subsystem.
1605 *
1606 * NOTE: This function is _only_ to be called by drivers/char/mem.c.
1607 *
1608 */
1609
1610static int __init
1611fbmem_init(void)
1612{
Alexey Dobriyan0aa16342008-04-28 02:15:42 -07001613 proc_create("fb", 0, NULL, &fb_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
1616 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1617
gregkh@suse.de56b22932005-03-23 10:01:41 -08001618 fb_class = class_create(THIS_MODULE, "graphics");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (IS_ERR(fb_class)) {
1620 printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
1621 fb_class = NULL;
1622 }
1623 return 0;
1624}
1625
1626#ifdef MODULE
1627module_init(fbmem_init);
1628static void __exit
1629fbmem_exit(void)
1630{
Alexey Dobriyanc43f89c2008-04-15 14:34:33 -07001631 remove_proc_entry("fb", NULL);
gregkh@suse.de56b22932005-03-23 10:01:41 -08001632 class_destroy(fb_class);
Jon Smirl5a340cc2005-07-27 11:46:04 -07001633 unregister_chrdev(FB_MAJOR, "fb");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636module_exit(fbmem_exit);
1637MODULE_LICENSE("GPL");
1638MODULE_DESCRIPTION("Framebuffer base");
1639#else
1640subsys_initcall(fbmem_init);
1641#endif
1642
1643int fb_new_modelist(struct fb_info *info)
1644{
1645 struct fb_event event;
1646 struct fb_var_screeninfo var = info->var;
1647 struct list_head *pos, *n;
1648 struct fb_modelist *modelist;
1649 struct fb_videomode *m, mode;
1650 int err = 1;
1651
1652 list_for_each_safe(pos, n, &info->modelist) {
1653 modelist = list_entry(pos, struct fb_modelist, list);
1654 m = &modelist->mode;
1655 fb_videomode_to_var(&var, m);
1656 var.activate = FB_ACTIVATE_TEST;
1657 err = fb_set_var(info, &var);
1658 fb_var_to_videomode(&mode, &var);
1659 if (err || !fb_mode_is_equal(m, &mode)) {
1660 list_del(pos);
1661 kfree(pos);
1662 }
1663 }
1664
1665 err = 1;
1666
1667 if (!list_empty(&info->modelist)) {
1668 event.info = info;
Antonino A. Daplas256154f2006-07-30 03:04:17 -07001669 err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
1672 return err;
1673}
1674
Helge Deller0128bee2006-12-08 02:40:29 -08001675static char *video_options[FB_MAX] __read_mostly;
1676static int ofonly __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678/**
1679 * fb_get_options - get kernel boot parameters
1680 * @name: framebuffer name as it would appear in
1681 * the boot parameter line
1682 * (video=<name>:<options>)
1683 * @option: the option will be stored here
1684 *
1685 * NOTE: Needed to maintain backwards compatibility
1686 */
1687int fb_get_options(char *name, char **option)
1688{
1689 char *opt, *options = NULL;
1690 int opt_len, retval = 0;
1691 int name_len = strlen(name), i;
1692
1693 if (name_len && ofonly && strncmp(name, "offb", 4))
1694 retval = 1;
1695
1696 if (name_len && !retval) {
1697 for (i = 0; i < FB_MAX; i++) {
1698 if (video_options[i] == NULL)
1699 continue;
1700 opt_len = strlen(video_options[i]);
1701 if (!opt_len)
1702 continue;
1703 opt = video_options[i];
1704 if (!strncmp(name, opt, name_len) &&
1705 opt[name_len] == ':')
1706 options = opt + name_len + 1;
1707 }
1708 }
1709 if (options && !strncmp(options, "off", 3))
1710 retval = 1;
1711
1712 if (option)
1713 *option = options;
1714
1715 return retval;
1716}
1717
Andrew Mortonbc6d7fd2006-02-11 17:56:08 -08001718#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719/**
1720 * video_setup - process command line options
1721 * @options: string of options
1722 *
1723 * Process command line options for frame buffer subsystem.
1724 *
1725 * NOTE: This function is a __setup and __init function.
1726 * It only stores the options. Drivers have to call
1727 * fb_get_options() as necessary.
1728 *
1729 * Returns zero.
1730 *
1731 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07001732static int __init video_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 int i, global = 0;
1735
1736 if (!options || !*options)
1737 global = 1;
1738
1739 if (!global && !strncmp(options, "ofonly", 6)) {
1740 ofonly = 1;
1741 global = 1;
1742 }
1743
1744 if (!global && !strstr(options, "fb:")) {
Geert Uytterhoeven9a054fb2007-10-16 01:29:51 -07001745 fb_mode_option = options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 global = 1;
1747 }
1748
1749 if (!global) {
1750 for (i = 0; i < FB_MAX; i++) {
1751 if (video_options[i] == NULL) {
1752 video_options[i] = options;
1753 break;
1754 }
1755
1756 }
1757 }
1758
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001759 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761__setup("video=", video_setup);
Andrew Mortonbc6d7fd2006-02-11 17:56:08 -08001762#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /*
1765 * Visible symbols for modules
1766 */
1767
1768EXPORT_SYMBOL(register_framebuffer);
1769EXPORT_SYMBOL(unregister_framebuffer);
1770EXPORT_SYMBOL(num_registered_fb);
1771EXPORT_SYMBOL(registered_fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772EXPORT_SYMBOL(fb_show_logo);
1773EXPORT_SYMBOL(fb_set_var);
1774EXPORT_SYMBOL(fb_blank);
1775EXPORT_SYMBOL(fb_pan_display);
1776EXPORT_SYMBOL(fb_get_buffer_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777EXPORT_SYMBOL(fb_set_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778EXPORT_SYMBOL(fb_get_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780MODULE_LICENSE("GPL");