fbmem.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. /*
  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. #include <linux/module.h>
  14. #include <linux/compat.h>
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/major.h>
  19. #include <linux/slab.h>
  20. #include <linux/mm.h>
  21. #include <linux/mman.h>
  22. #include <linux/vt.h>
  23. #include <linux/init.h>
  24. #include <linux/linux_logo.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/console.h>
  28. #include <linux/kmod.h>
  29. #include <linux/err.h>
  30. #include <linux/device.h>
  31. #include <linux/efi.h>
  32. #include <linux/fb.h>
  33. #include <asm/fb.h>
  34. /*
  35. * Frame buffer device initialization and setup routines
  36. */
  37. #define FBPIXMAPSIZE (1024 * 8)
  38. static DEFINE_MUTEX(registration_lock);
  39. struct fb_info *registered_fb[FB_MAX] __read_mostly;
  40. int num_registered_fb __read_mostly;
  41. static struct fb_info *get_fb_info(unsigned int idx)
  42. {
  43. struct fb_info *fb_info;
  44. if (idx >= FB_MAX)
  45. return ERR_PTR(-ENODEV);
  46. mutex_lock(&registration_lock);
  47. fb_info = registered_fb[idx];
  48. if (fb_info)
  49. atomic_inc(&fb_info->count);
  50. mutex_unlock(&registration_lock);
  51. return fb_info;
  52. }
  53. static void put_fb_info(struct fb_info *fb_info)
  54. {
  55. if (!atomic_dec_and_test(&fb_info->count))
  56. return;
  57. if (fb_info->fbops->fb_destroy)
  58. fb_info->fbops->fb_destroy(fb_info);
  59. }
  60. int lock_fb_info(struct fb_info *info)
  61. {
  62. mutex_lock(&info->lock);
  63. if (!info->fbops) {
  64. mutex_unlock(&info->lock);
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. EXPORT_SYMBOL(lock_fb_info);
  70. /*
  71. * Helpers
  72. */
  73. int fb_get_color_depth(struct fb_var_screeninfo *var,
  74. struct fb_fix_screeninfo *fix)
  75. {
  76. int depth = 0;
  77. if (fix->visual == FB_VISUAL_MONO01 ||
  78. fix->visual == FB_VISUAL_MONO10)
  79. depth = 1;
  80. else {
  81. if (var->green.length == var->blue.length &&
  82. var->green.length == var->red.length &&
  83. var->green.offset == var->blue.offset &&
  84. var->green.offset == var->red.offset)
  85. depth = var->green.length;
  86. else
  87. depth = var->green.length + var->red.length +
  88. var->blue.length;
  89. }
  90. return depth;
  91. }
  92. EXPORT_SYMBOL(fb_get_color_depth);
  93. /*
  94. * Data padding functions.
  95. */
  96. void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
  97. {
  98. __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height);
  99. }
  100. EXPORT_SYMBOL(fb_pad_aligned_buffer);
  101. void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
  102. u32 shift_high, u32 shift_low, u32 mod)
  103. {
  104. u8 mask = (u8) (0xfff << shift_high), tmp;
  105. int i, j;
  106. for (i = height; i--; ) {
  107. for (j = 0; j < idx; j++) {
  108. tmp = dst[j];
  109. tmp &= mask;
  110. tmp |= *src >> shift_low;
  111. dst[j] = tmp;
  112. tmp = *src << shift_high;
  113. dst[j+1] = tmp;
  114. src++;
  115. }
  116. tmp = dst[idx];
  117. tmp &= mask;
  118. tmp |= *src >> shift_low;
  119. dst[idx] = tmp;
  120. if (shift_high < mod) {
  121. tmp = *src << shift_high;
  122. dst[idx+1] = tmp;
  123. }
  124. src++;
  125. dst += d_pitch;
  126. }
  127. }
  128. EXPORT_SYMBOL(fb_pad_unaligned_buffer);
  129. /*
  130. * we need to lock this section since fb_cursor
  131. * may use fb_imageblit()
  132. */
  133. char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
  134. {
  135. u32 align = buf->buf_align - 1, offset;
  136. char *addr = buf->addr;
  137. /* If IO mapped, we need to sync before access, no sharing of
  138. * the pixmap is done
  139. */
  140. if (buf->flags & FB_PIXMAP_IO) {
  141. if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
  142. info->fbops->fb_sync(info);
  143. return addr;
  144. }
  145. /* See if we fit in the remaining pixmap space */
  146. offset = buf->offset + align;
  147. offset &= ~align;
  148. if (offset + size > buf->size) {
  149. /* We do not fit. In order to be able to re-use the buffer,
  150. * we must ensure no asynchronous DMA'ing or whatever operation
  151. * is in progress, we sync for that.
  152. */
  153. if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
  154. info->fbops->fb_sync(info);
  155. offset = 0;
  156. }
  157. buf->offset = offset + size;
  158. addr += offset;
  159. return addr;
  160. }
  161. #ifdef CONFIG_LOGO
  162. static inline unsigned safe_shift(unsigned d, int n)
  163. {
  164. return n < 0 ? d >> -n : d << n;
  165. }
  166. static void fb_set_logocmap(struct fb_info *info,
  167. const struct linux_logo *logo)
  168. {
  169. struct fb_cmap palette_cmap;
  170. u16 palette_green[16];
  171. u16 palette_blue[16];
  172. u16 palette_red[16];
  173. int i, j, n;
  174. const unsigned char *clut = logo->clut;
  175. palette_cmap.start = 0;
  176. palette_cmap.len = 16;
  177. palette_cmap.red = palette_red;
  178. palette_cmap.green = palette_green;
  179. palette_cmap.blue = palette_blue;
  180. palette_cmap.transp = NULL;
  181. for (i = 0; i < logo->clutsize; i += n) {
  182. n = logo->clutsize - i;
  183. /* palette_cmap provides space for only 16 colors at once */
  184. if (n > 16)
  185. n = 16;
  186. palette_cmap.start = 32 + i;
  187. palette_cmap.len = n;
  188. for (j = 0; j < n; ++j) {
  189. palette_cmap.red[j] = clut[0] << 8 | clut[0];
  190. palette_cmap.green[j] = clut[1] << 8 | clut[1];
  191. palette_cmap.blue[j] = clut[2] << 8 | clut[2];
  192. clut += 3;
  193. }
  194. fb_set_cmap(&palette_cmap, info);
  195. }
  196. }
  197. static void fb_set_logo_truepalette(struct fb_info *info,
  198. const struct linux_logo *logo,
  199. u32 *palette)
  200. {
  201. static const unsigned char mask[] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
  202. unsigned char redmask, greenmask, bluemask;
  203. int redshift, greenshift, blueshift;
  204. int i;
  205. const unsigned char *clut = logo->clut;
  206. /*
  207. * We have to create a temporary palette since console palette is only
  208. * 16 colors long.
  209. */
  210. /* Bug: Doesn't obey msb_right ... (who needs that?) */
  211. redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8];
  212. greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
  213. bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8];
  214. redshift = info->var.red.offset - (8 - info->var.red.length);
  215. greenshift = info->var.green.offset - (8 - info->var.green.length);
  216. blueshift = info->var.blue.offset - (8 - info->var.blue.length);
  217. for ( i = 0; i < logo->clutsize; i++) {
  218. palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
  219. safe_shift((clut[1] & greenmask), greenshift) |
  220. safe_shift((clut[2] & bluemask), blueshift));
  221. clut += 3;
  222. }
  223. }
  224. static void fb_set_logo_directpalette(struct fb_info *info,
  225. const struct linux_logo *logo,
  226. u32 *palette)
  227. {
  228. int redshift, greenshift, blueshift;
  229. int i;
  230. redshift = info->var.red.offset;
  231. greenshift = info->var.green.offset;
  232. blueshift = info->var.blue.offset;
  233. for (i = 32; i < 32 + logo->clutsize; i++)
  234. palette[i] = i << redshift | i << greenshift | i << blueshift;
  235. }
  236. static void fb_set_logo(struct fb_info *info,
  237. const struct linux_logo *logo, u8 *dst,
  238. int depth)
  239. {
  240. int i, j, k;
  241. const u8 *src = logo->data;
  242. u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
  243. u8 fg = 1, d;
  244. switch (fb_get_color_depth(&info->var, &info->fix)) {
  245. case 1:
  246. fg = 1;
  247. break;
  248. case 2:
  249. fg = 3;
  250. break;
  251. default:
  252. fg = 7;
  253. break;
  254. }
  255. if (info->fix.visual == FB_VISUAL_MONO01 ||
  256. info->fix.visual == FB_VISUAL_MONO10)
  257. fg = ~((u8) (0xfff << info->var.green.length));
  258. switch (depth) {
  259. case 4:
  260. for (i = 0; i < logo->height; i++)
  261. for (j = 0; j < logo->width; src++) {
  262. *dst++ = *src >> 4;
  263. j++;
  264. if (j < logo->width) {
  265. *dst++ = *src & 0x0f;
  266. j++;
  267. }
  268. }
  269. break;
  270. case 1:
  271. for (i = 0; i < logo->height; i++) {
  272. for (j = 0; j < logo->width; src++) {
  273. d = *src ^ xor;
  274. for (k = 7; k >= 0; k--) {
  275. *dst++ = ((d >> k) & 1) ? fg : 0;
  276. j++;
  277. }
  278. }
  279. }
  280. break;
  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. */
  309. static 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 __read_mostly;
  316. static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height)
  317. {
  318. u32 size = width * height, i;
  319. out += size - 1;
  320. for (i = size; i--; )
  321. *out-- = *in++;
  322. }
  323. static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height)
  324. {
  325. int i, j, h = height - 1;
  326. for (i = 0; i < height; i++)
  327. for (j = 0; j < width; j++)
  328. out[height * j + h - i] = *in++;
  329. }
  330. static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height)
  331. {
  332. int i, j, w = width - 1;
  333. for (i = 0; i < height; i++)
  334. for (j = 0; j < width; j++)
  335. out[height * (w - j) + i] = *in++;
  336. }
  337. static void fb_rotate_logo(struct fb_info *info, u8 *dst,
  338. struct fb_image *image, int rotate)
  339. {
  340. u32 tmp;
  341. if (rotate == FB_ROTATE_UD) {
  342. fb_rotate_logo_ud(image->data, dst, image->width,
  343. image->height);
  344. image->dx = info->var.xres - image->width - image->dx;
  345. image->dy = info->var.yres - image->height - image->dy;
  346. } else if (rotate == FB_ROTATE_CW) {
  347. fb_rotate_logo_cw(image->data, dst, image->width,
  348. image->height);
  349. tmp = image->width;
  350. image->width = image->height;
  351. image->height = tmp;
  352. tmp = image->dy;
  353. image->dy = image->dx;
  354. image->dx = info->var.xres - image->width - tmp;
  355. } else if (rotate == FB_ROTATE_CCW) {
  356. fb_rotate_logo_ccw(image->data, dst, image->width,
  357. image->height);
  358. tmp = image->width;
  359. image->width = image->height;
  360. image->height = tmp;
  361. tmp = image->dx;
  362. image->dx = image->dy;
  363. image->dy = info->var.yres - image->height - tmp;
  364. }
  365. image->data = dst;
  366. }
  367. static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
  368. int rotate, unsigned int num)
  369. {
  370. unsigned int x;
  371. if (rotate == FB_ROTATE_UR) {
  372. for (x = 0;
  373. x < num && image->dx + image->width <= info->var.xres;
  374. x++) {
  375. info->fbops->fb_imageblit(info, image);
  376. image->dx += image->width + 8;
  377. }
  378. } else if (rotate == FB_ROTATE_UD) {
  379. for (x = 0; x < num && image->dx >= 0; x++) {
  380. info->fbops->fb_imageblit(info, image);
  381. image->dx -= image->width + 8;
  382. }
  383. } else if (rotate == FB_ROTATE_CW) {
  384. for (x = 0;
  385. x < num && image->dy + image->height <= info->var.yres;
  386. x++) {
  387. info->fbops->fb_imageblit(info, image);
  388. image->dy += image->height + 8;
  389. }
  390. } else if (rotate == FB_ROTATE_CCW) {
  391. for (x = 0; x < num && image->dy >= 0; x++) {
  392. info->fbops->fb_imageblit(info, image);
  393. image->dy -= image->height + 8;
  394. }
  395. }
  396. }
  397. static int fb_show_logo_line(struct fb_info *info, int rotate,
  398. const struct linux_logo *logo, int y,
  399. unsigned int n)
  400. {
  401. u32 *palette = NULL, *saved_pseudo_palette = NULL;
  402. unsigned char *logo_new = NULL, *logo_rotate = NULL;
  403. struct fb_image image;
  404. /* Return if the frame buffer is not mapped or suspended */
  405. if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
  406. info->flags & FBINFO_MODULE)
  407. return 0;
  408. image.depth = 8;
  409. image.data = logo->data;
  410. if (fb_logo.needs_cmapreset)
  411. fb_set_logocmap(info, logo);
  412. if (fb_logo.needs_truepalette ||
  413. fb_logo.needs_directpalette) {
  414. palette = kmalloc(256 * 4, GFP_KERNEL);
  415. if (palette == NULL)
  416. return 0;
  417. if (fb_logo.needs_truepalette)
  418. fb_set_logo_truepalette(info, logo, palette);
  419. else
  420. fb_set_logo_directpalette(info, logo, palette);
  421. saved_pseudo_palette = info->pseudo_palette;
  422. info->pseudo_palette = palette;
  423. }
  424. if (fb_logo.depth <= 4) {
  425. logo_new = kmalloc(logo->width * logo->height, GFP_KERNEL);
  426. if (logo_new == NULL) {
  427. kfree(palette);
  428. if (saved_pseudo_palette)
  429. info->pseudo_palette = saved_pseudo_palette;
  430. return 0;
  431. }
  432. image.data = logo_new;
  433. fb_set_logo(info, logo, logo_new, fb_logo.depth);
  434. }
  435. image.dx = 0;
  436. image.dy = y;
  437. image.width = logo->width;
  438. image.height = logo->height;
  439. if (rotate) {
  440. logo_rotate = kmalloc(logo->width *
  441. logo->height, GFP_KERNEL);
  442. if (logo_rotate)
  443. fb_rotate_logo(info, logo_rotate, &image, rotate);
  444. }
  445. fb_do_show_logo(info, &image, rotate, n);
  446. kfree(palette);
  447. if (saved_pseudo_palette != NULL)
  448. info->pseudo_palette = saved_pseudo_palette;
  449. kfree(logo_new);
  450. kfree(logo_rotate);
  451. return logo->height;
  452. }
  453. #ifdef CONFIG_FB_LOGO_EXTRA
  454. #define FB_LOGO_EX_NUM_MAX 10
  455. static struct logo_data_extra {
  456. const struct linux_logo *logo;
  457. unsigned int n;
  458. } fb_logo_ex[FB_LOGO_EX_NUM_MAX];
  459. static unsigned int fb_logo_ex_num;
  460. void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n)
  461. {
  462. if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX)
  463. return;
  464. fb_logo_ex[fb_logo_ex_num].logo = logo;
  465. fb_logo_ex[fb_logo_ex_num].n = n;
  466. fb_logo_ex_num++;
  467. }
  468. static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
  469. unsigned int yres)
  470. {
  471. unsigned int i;
  472. /* FIXME: logo_ex supports only truecolor fb. */
  473. if (info->fix.visual != FB_VISUAL_TRUECOLOR)
  474. fb_logo_ex_num = 0;
  475. for (i = 0; i < fb_logo_ex_num; i++) {
  476. if (fb_logo_ex[i].logo->type != fb_logo.logo->type) {
  477. fb_logo_ex[i].logo = NULL;
  478. continue;
  479. }
  480. height += fb_logo_ex[i].logo->height;
  481. if (height > yres) {
  482. height -= fb_logo_ex[i].logo->height;
  483. fb_logo_ex_num = i;
  484. break;
  485. }
  486. }
  487. return height;
  488. }
  489. static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
  490. {
  491. unsigned int i;
  492. for (i = 0; i < fb_logo_ex_num; i++)
  493. y += fb_show_logo_line(info, rotate,
  494. fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
  495. return y;
  496. }
  497. #else /* !CONFIG_FB_LOGO_EXTRA */
  498. static inline int fb_prepare_extra_logos(struct fb_info *info,
  499. unsigned int height,
  500. unsigned int yres)
  501. {
  502. return height;
  503. }
  504. static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
  505. {
  506. return y;
  507. }
  508. #endif /* CONFIG_FB_LOGO_EXTRA */
  509. int fb_prepare_logo(struct fb_info *info, int rotate)
  510. {
  511. int depth = fb_get_color_depth(&info->var, &info->fix);
  512. unsigned int yres;
  513. memset(&fb_logo, 0, sizeof(struct logo_data));
  514. if (info->flags & FBINFO_MISC_TILEBLITTING ||
  515. info->flags & FBINFO_MODULE)
  516. return 0;
  517. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  518. depth = info->var.blue.length;
  519. if (info->var.red.length < depth)
  520. depth = info->var.red.length;
  521. if (info->var.green.length < depth)
  522. depth = info->var.green.length;
  523. }
  524. if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
  525. /* assume console colormap */
  526. depth = 4;
  527. }
  528. /* Return if no suitable logo was found */
  529. fb_logo.logo = fb_find_logo(depth);
  530. if (!fb_logo.logo) {
  531. return 0;
  532. }
  533. if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
  534. yres = info->var.yres;
  535. else
  536. yres = info->var.xres;
  537. if (fb_logo.logo->height > yres) {
  538. fb_logo.logo = NULL;
  539. return 0;
  540. }
  541. /* What depth we asked for might be different from what we get */
  542. if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
  543. fb_logo.depth = 8;
  544. else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
  545. fb_logo.depth = 4;
  546. else
  547. fb_logo.depth = 1;
  548. if (fb_logo.depth > 4 && depth > 4) {
  549. switch (info->fix.visual) {
  550. case FB_VISUAL_TRUECOLOR:
  551. fb_logo.needs_truepalette = 1;
  552. break;
  553. case FB_VISUAL_DIRECTCOLOR:
  554. fb_logo.needs_directpalette = 1;
  555. fb_logo.needs_cmapreset = 1;
  556. break;
  557. case FB_VISUAL_PSEUDOCOLOR:
  558. fb_logo.needs_cmapreset = 1;
  559. break;
  560. }
  561. }
  562. return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
  563. }
  564. int fb_show_logo(struct fb_info *info, int rotate)
  565. {
  566. int y;
  567. y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
  568. num_online_cpus());
  569. y = fb_show_extra_logos(info, y, rotate);
  570. return y;
  571. }
  572. #else
  573. int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
  574. int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
  575. #endif /* CONFIG_LOGO */
  576. static void *fb_seq_start(struct seq_file *m, loff_t *pos)
  577. {
  578. mutex_lock(&registration_lock);
  579. return (*pos < FB_MAX) ? pos : NULL;
  580. }
  581. static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
  582. {
  583. (*pos)++;
  584. return (*pos < FB_MAX) ? pos : NULL;
  585. }
  586. static void fb_seq_stop(struct seq_file *m, void *v)
  587. {
  588. mutex_unlock(&registration_lock);
  589. }
  590. static int fb_seq_show(struct seq_file *m, void *v)
  591. {
  592. int i = *(loff_t *)v;
  593. struct fb_info *fi = registered_fb[i];
  594. if (fi)
  595. seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
  596. return 0;
  597. }
  598. static const struct seq_operations proc_fb_seq_ops = {
  599. .start = fb_seq_start,
  600. .next = fb_seq_next,
  601. .stop = fb_seq_stop,
  602. .show = fb_seq_show,
  603. };
  604. static int proc_fb_open(struct inode *inode, struct file *file)
  605. {
  606. return seq_open(file, &proc_fb_seq_ops);
  607. }
  608. static const struct file_operations fb_proc_fops = {
  609. .owner = THIS_MODULE,
  610. .open = proc_fb_open,
  611. .read = seq_read,
  612. .llseek = seq_lseek,
  613. .release = seq_release,
  614. };
  615. /*
  616. * We hold a reference to the fb_info in file->private_data,
  617. * but if the current registered fb has changed, we don't
  618. * actually want to use it.
  619. *
  620. * So look up the fb_info using the inode minor number,
  621. * and just verify it against the reference we have.
  622. */
  623. static struct fb_info *file_fb_info(struct file *file)
  624. {
  625. struct inode *inode = file->f_path.dentry->d_inode;
  626. int fbidx = iminor(inode);
  627. struct fb_info *info = registered_fb[fbidx];
  628. if (info != file->private_data)
  629. info = NULL;
  630. return info;
  631. }
  632. static ssize_t
  633. fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  634. {
  635. unsigned long p = *ppos;
  636. struct fb_info *info = file_fb_info(file);
  637. u8 *buffer, *dst;
  638. u8 __iomem *src;
  639. int c, cnt = 0, err = 0;
  640. unsigned long total_size;
  641. if (!info || ! info->screen_base)
  642. return -ENODEV;
  643. if (info->state != FBINFO_STATE_RUNNING)
  644. return -EPERM;
  645. if (info->fbops->fb_read)
  646. return info->fbops->fb_read(info, buf, count, ppos);
  647. total_size = info->screen_size;
  648. if (total_size == 0)
  649. total_size = info->fix.smem_len;
  650. if (p >= total_size)
  651. return 0;
  652. if (count >= total_size)
  653. count = total_size;
  654. if (count + p > total_size)
  655. count = total_size - p;
  656. buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
  657. GFP_KERNEL);
  658. if (!buffer)
  659. return -ENOMEM;
  660. src = (u8 __iomem *) (info->screen_base + p);
  661. if (info->fbops->fb_sync)
  662. info->fbops->fb_sync(info);
  663. while (count) {
  664. c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  665. dst = buffer;
  666. fb_memcpy_fromfb(dst, src, c);
  667. dst += c;
  668. src += c;
  669. if (copy_to_user(buf, buffer, c)) {
  670. err = -EFAULT;
  671. break;
  672. }
  673. *ppos += c;
  674. buf += c;
  675. cnt += c;
  676. count -= c;
  677. }
  678. kfree(buffer);
  679. return (err) ? err : cnt;
  680. }
  681. static ssize_t
  682. fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  683. {
  684. unsigned long p = *ppos;
  685. struct fb_info *info = file_fb_info(file);
  686. u8 *buffer, *src;
  687. u8 __iomem *dst;
  688. int c, cnt = 0, err = 0;
  689. unsigned long total_size;
  690. if (!info || !info->screen_base)
  691. return -ENODEV;
  692. if (info->state != FBINFO_STATE_RUNNING)
  693. return -EPERM;
  694. if (info->fbops->fb_write)
  695. return info->fbops->fb_write(info, buf, count, ppos);
  696. total_size = info->screen_size;
  697. if (total_size == 0)
  698. total_size = info->fix.smem_len;
  699. if (p > total_size)
  700. return -EFBIG;
  701. if (count > total_size) {
  702. err = -EFBIG;
  703. count = total_size;
  704. }
  705. if (count + p > total_size) {
  706. if (!err)
  707. err = -ENOSPC;
  708. count = total_size - p;
  709. }
  710. buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
  711. GFP_KERNEL);
  712. if (!buffer)
  713. return -ENOMEM;
  714. dst = (u8 __iomem *) (info->screen_base + p);
  715. if (info->fbops->fb_sync)
  716. info->fbops->fb_sync(info);
  717. while (count) {
  718. c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  719. src = buffer;
  720. if (copy_from_user(src, buf, c)) {
  721. err = -EFAULT;
  722. break;
  723. }
  724. fb_memcpy_tofb(dst, src, c);
  725. dst += c;
  726. src += c;
  727. *ppos += c;
  728. buf += c;
  729. cnt += c;
  730. count -= c;
  731. }
  732. kfree(buffer);
  733. return (cnt) ? cnt : err;
  734. }
  735. int
  736. fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
  737. {
  738. struct fb_fix_screeninfo *fix = &info->fix;
  739. unsigned int yres = info->var.yres;
  740. int err = 0;
  741. if (var->yoffset > 0) {
  742. if (var->vmode & FB_VMODE_YWRAP) {
  743. if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
  744. err = -EINVAL;
  745. else
  746. yres = 0;
  747. } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
  748. err = -EINVAL;
  749. }
  750. if (var->xoffset > 0 && (!fix->xpanstep ||
  751. (var->xoffset % fix->xpanstep)))
  752. err = -EINVAL;
  753. if (err || !info->fbops->fb_pan_display ||
  754. var->yoffset > info->var.yres_virtual - yres ||
  755. var->xoffset > info->var.xres_virtual - info->var.xres)
  756. return -EINVAL;
  757. if ((err = info->fbops->fb_pan_display(var, info)))
  758. return err;
  759. info->var.xoffset = var->xoffset;
  760. info->var.yoffset = var->yoffset;
  761. if (var->vmode & FB_VMODE_YWRAP)
  762. info->var.vmode |= FB_VMODE_YWRAP;
  763. else
  764. info->var.vmode &= ~FB_VMODE_YWRAP;
  765. return 0;
  766. }
  767. static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
  768. u32 activate)
  769. {
  770. struct fb_event event;
  771. struct fb_blit_caps caps, fbcaps;
  772. int err = 0;
  773. memset(&caps, 0, sizeof(caps));
  774. memset(&fbcaps, 0, sizeof(fbcaps));
  775. caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
  776. event.info = info;
  777. event.data = &caps;
  778. fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
  779. info->fbops->fb_get_caps(info, &fbcaps, var);
  780. if (((fbcaps.x ^ caps.x) & caps.x) ||
  781. ((fbcaps.y ^ caps.y) & caps.y) ||
  782. (fbcaps.len < caps.len))
  783. err = -EINVAL;
  784. return err;
  785. }
  786. int
  787. fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
  788. {
  789. int flags = info->flags;
  790. int ret = 0;
  791. if (var->activate & FB_ACTIVATE_INV_MODE) {
  792. struct fb_videomode mode1, mode2;
  793. fb_var_to_videomode(&mode1, var);
  794. fb_var_to_videomode(&mode2, &info->var);
  795. /* make sure we don't delete the videomode of current var */
  796. ret = fb_mode_is_equal(&mode1, &mode2);
  797. if (!ret) {
  798. struct fb_event event;
  799. event.info = info;
  800. event.data = &mode1;
  801. ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
  802. }
  803. if (!ret)
  804. fb_delete_videomode(&mode1, &info->modelist);
  805. ret = (ret) ? -EINVAL : 0;
  806. goto done;
  807. }
  808. if ((var->activate & FB_ACTIVATE_FORCE) ||
  809. memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
  810. u32 activate = var->activate;
  811. if (!info->fbops->fb_check_var) {
  812. *var = info->var;
  813. goto done;
  814. }
  815. ret = info->fbops->fb_check_var(var, info);
  816. if (ret)
  817. goto done;
  818. if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
  819. struct fb_var_screeninfo old_var;
  820. struct fb_videomode mode;
  821. if (info->fbops->fb_get_caps) {
  822. ret = fb_check_caps(info, var, activate);
  823. if (ret)
  824. goto done;
  825. }
  826. old_var = info->var;
  827. info->var = *var;
  828. if (info->fbops->fb_set_par) {
  829. ret = info->fbops->fb_set_par(info);
  830. if (ret) {
  831. info->var = old_var;
  832. printk(KERN_WARNING "detected "
  833. "fb_set_par error, "
  834. "error code: %d\n", ret);
  835. goto done;
  836. }
  837. }
  838. fb_pan_display(info, &info->var);
  839. fb_set_cmap(&info->cmap, info);
  840. fb_var_to_videomode(&mode, &info->var);
  841. if (info->modelist.prev && info->modelist.next &&
  842. !list_empty(&info->modelist))
  843. ret = fb_add_videomode(&mode, &info->modelist);
  844. if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
  845. struct fb_event event;
  846. int evnt = (activate & FB_ACTIVATE_ALL) ?
  847. FB_EVENT_MODE_CHANGE_ALL :
  848. FB_EVENT_MODE_CHANGE;
  849. info->flags &= ~FBINFO_MISC_USEREVENT;
  850. event.info = info;
  851. event.data = &mode;
  852. fb_notifier_call_chain(evnt, &event);
  853. }
  854. }
  855. }
  856. done:
  857. return ret;
  858. }
  859. int
  860. fb_blank(struct fb_info *info, int blank)
  861. {
  862. int ret = -EINVAL;
  863. if (blank > FB_BLANK_POWERDOWN)
  864. blank = FB_BLANK_POWERDOWN;
  865. if (info->fbops->fb_blank)
  866. ret = info->fbops->fb_blank(blank, info);
  867. if (!ret) {
  868. struct fb_event event;
  869. event.info = info;
  870. event.data = &blank;
  871. fb_notifier_call_chain(FB_EVENT_BLANK, &event);
  872. }
  873. return ret;
  874. }
  875. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  876. unsigned long arg)
  877. {
  878. struct fb_ops *fb;
  879. struct fb_var_screeninfo var;
  880. struct fb_fix_screeninfo fix;
  881. struct fb_con2fbmap con2fb;
  882. struct fb_cmap cmap_from;
  883. struct fb_cmap_user cmap;
  884. struct fb_event event;
  885. void __user *argp = (void __user *)arg;
  886. long ret = 0;
  887. switch (cmd) {
  888. case FBIOGET_VSCREENINFO:
  889. if (!lock_fb_info(info))
  890. return -ENODEV;
  891. var = info->var;
  892. unlock_fb_info(info);
  893. ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
  894. break;
  895. case FBIOPUT_VSCREENINFO:
  896. if (copy_from_user(&var, argp, sizeof(var)))
  897. return -EFAULT;
  898. if (!lock_fb_info(info))
  899. return -ENODEV;
  900. console_lock();
  901. info->flags |= FBINFO_MISC_USEREVENT;
  902. ret = fb_set_var(info, &var);
  903. info->flags &= ~FBINFO_MISC_USEREVENT;
  904. console_unlock();
  905. unlock_fb_info(info);
  906. if (!ret && copy_to_user(argp, &var, sizeof(var)))
  907. ret = -EFAULT;
  908. break;
  909. case FBIOGET_FSCREENINFO:
  910. if (!lock_fb_info(info))
  911. return -ENODEV;
  912. fix = info->fix;
  913. unlock_fb_info(info);
  914. ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
  915. break;
  916. case FBIOPUTCMAP:
  917. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  918. return -EFAULT;
  919. ret = fb_set_user_cmap(&cmap, info);
  920. break;
  921. case FBIOGETCMAP:
  922. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  923. return -EFAULT;
  924. if (!lock_fb_info(info))
  925. return -ENODEV;
  926. cmap_from = info->cmap;
  927. unlock_fb_info(info);
  928. ret = fb_cmap_to_user(&cmap_from, &cmap);
  929. break;
  930. case FBIOPAN_DISPLAY:
  931. if (copy_from_user(&var, argp, sizeof(var)))
  932. return -EFAULT;
  933. if (!lock_fb_info(info))
  934. return -ENODEV;
  935. console_lock();
  936. ret = fb_pan_display(info, &var);
  937. console_unlock();
  938. unlock_fb_info(info);
  939. if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
  940. return -EFAULT;
  941. break;
  942. case FBIO_CURSOR:
  943. ret = -EINVAL;
  944. break;
  945. case FBIOGET_CON2FBMAP:
  946. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  947. return -EFAULT;
  948. if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
  949. return -EINVAL;
  950. con2fb.framebuffer = -1;
  951. event.data = &con2fb;
  952. if (!lock_fb_info(info))
  953. return -ENODEV;
  954. event.info = info;
  955. fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
  956. unlock_fb_info(info);
  957. ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
  958. break;
  959. case FBIOPUT_CON2FBMAP:
  960. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  961. return -EFAULT;
  962. if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
  963. return -EINVAL;
  964. if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
  965. return -EINVAL;
  966. if (!registered_fb[con2fb.framebuffer])
  967. request_module("fb%d", con2fb.framebuffer);
  968. if (!registered_fb[con2fb.framebuffer]) {
  969. ret = -EINVAL;
  970. break;
  971. }
  972. event.data = &con2fb;
  973. if (!lock_fb_info(info))
  974. return -ENODEV;
  975. event.info = info;
  976. ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);
  977. unlock_fb_info(info);
  978. break;
  979. case FBIOBLANK:
  980. if (!lock_fb_info(info))
  981. return -ENODEV;
  982. console_lock();
  983. info->flags |= FBINFO_MISC_USEREVENT;
  984. ret = fb_blank(info, arg);
  985. info->flags &= ~FBINFO_MISC_USEREVENT;
  986. console_unlock();
  987. unlock_fb_info(info);
  988. break;
  989. default:
  990. if (!lock_fb_info(info))
  991. return -ENODEV;
  992. fb = info->fbops;
  993. if (fb->fb_ioctl)
  994. ret = fb->fb_ioctl(info, cmd, arg);
  995. else
  996. ret = -ENOTTY;
  997. unlock_fb_info(info);
  998. }
  999. return ret;
  1000. }
  1001. static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1002. {
  1003. struct fb_info *info = file_fb_info(file);
  1004. if (!info)
  1005. return -ENODEV;
  1006. return do_fb_ioctl(info, cmd, arg);
  1007. }
  1008. #ifdef CONFIG_COMPAT
  1009. struct fb_fix_screeninfo32 {
  1010. char id[16];
  1011. compat_caddr_t smem_start;
  1012. u32 smem_len;
  1013. u32 type;
  1014. u32 type_aux;
  1015. u32 visual;
  1016. u16 xpanstep;
  1017. u16 ypanstep;
  1018. u16 ywrapstep;
  1019. u32 line_length;
  1020. compat_caddr_t mmio_start;
  1021. u32 mmio_len;
  1022. u32 accel;
  1023. u16 reserved[3];
  1024. };
  1025. struct fb_cmap32 {
  1026. u32 start;
  1027. u32 len;
  1028. compat_caddr_t red;
  1029. compat_caddr_t green;
  1030. compat_caddr_t blue;
  1031. compat_caddr_t transp;
  1032. };
  1033. static int fb_getput_cmap(struct fb_info *info, unsigned int cmd,
  1034. unsigned long arg)
  1035. {
  1036. struct fb_cmap_user __user *cmap;
  1037. struct fb_cmap32 __user *cmap32;
  1038. __u32 data;
  1039. int err;
  1040. cmap = compat_alloc_user_space(sizeof(*cmap));
  1041. cmap32 = compat_ptr(arg);
  1042. if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
  1043. return -EFAULT;
  1044. if (get_user(data, &cmap32->red) ||
  1045. put_user(compat_ptr(data), &cmap->red) ||
  1046. get_user(data, &cmap32->green) ||
  1047. put_user(compat_ptr(data), &cmap->green) ||
  1048. get_user(data, &cmap32->blue) ||
  1049. put_user(compat_ptr(data), &cmap->blue) ||
  1050. get_user(data, &cmap32->transp) ||
  1051. put_user(compat_ptr(data), &cmap->transp))
  1052. return -EFAULT;
  1053. err = do_fb_ioctl(info, cmd, (unsigned long) cmap);
  1054. if (!err) {
  1055. if (copy_in_user(&cmap32->start,
  1056. &cmap->start,
  1057. 2 * sizeof(__u32)))
  1058. err = -EFAULT;
  1059. }
  1060. return err;
  1061. }
  1062. static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
  1063. struct fb_fix_screeninfo32 __user *fix32)
  1064. {
  1065. __u32 data;
  1066. int err;
  1067. err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
  1068. data = (__u32) (unsigned long) fix->smem_start;
  1069. err |= put_user(data, &fix32->smem_start);
  1070. err |= put_user(fix->smem_len, &fix32->smem_len);
  1071. err |= put_user(fix->type, &fix32->type);
  1072. err |= put_user(fix->type_aux, &fix32->type_aux);
  1073. err |= put_user(fix->visual, &fix32->visual);
  1074. err |= put_user(fix->xpanstep, &fix32->xpanstep);
  1075. err |= put_user(fix->ypanstep, &fix32->ypanstep);
  1076. err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
  1077. err |= put_user(fix->line_length, &fix32->line_length);
  1078. data = (__u32) (unsigned long) fix->mmio_start;
  1079. err |= put_user(data, &fix32->mmio_start);
  1080. err |= put_user(fix->mmio_len, &fix32->mmio_len);
  1081. err |= put_user(fix->accel, &fix32->accel);
  1082. err |= copy_to_user(fix32->reserved, fix->reserved,
  1083. sizeof(fix->reserved));
  1084. return err;
  1085. }
  1086. static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd,
  1087. unsigned long arg)
  1088. {
  1089. mm_segment_t old_fs;
  1090. struct fb_fix_screeninfo fix;
  1091. struct fb_fix_screeninfo32 __user *fix32;
  1092. int err;
  1093. fix32 = compat_ptr(arg);
  1094. old_fs = get_fs();
  1095. set_fs(KERNEL_DS);
  1096. err = do_fb_ioctl(info, cmd, (unsigned long) &fix);
  1097. set_fs(old_fs);
  1098. if (!err)
  1099. err = do_fscreeninfo_to_user(&fix, fix32);
  1100. return err;
  1101. }
  1102. static long fb_compat_ioctl(struct file *file, unsigned int cmd,
  1103. unsigned long arg)
  1104. {
  1105. struct fb_info *info = file_fb_info(file);
  1106. struct fb_ops *fb;
  1107. long ret = -ENOIOCTLCMD;
  1108. if (!info)
  1109. return -ENODEV;
  1110. fb = info->fbops;
  1111. switch(cmd) {
  1112. case FBIOGET_VSCREENINFO:
  1113. case FBIOPUT_VSCREENINFO:
  1114. case FBIOPAN_DISPLAY:
  1115. case FBIOGET_CON2FBMAP:
  1116. case FBIOPUT_CON2FBMAP:
  1117. arg = (unsigned long) compat_ptr(arg);
  1118. case FBIOBLANK:
  1119. ret = do_fb_ioctl(info, cmd, arg);
  1120. break;
  1121. case FBIOGET_FSCREENINFO:
  1122. ret = fb_get_fscreeninfo(info, cmd, arg);
  1123. break;
  1124. case FBIOGETCMAP:
  1125. case FBIOPUTCMAP:
  1126. ret = fb_getput_cmap(info, cmd, arg);
  1127. break;
  1128. default:
  1129. if (fb->fb_compat_ioctl)
  1130. ret = fb->fb_compat_ioctl(info, cmd, arg);
  1131. break;
  1132. }
  1133. return ret;
  1134. }
  1135. #endif
  1136. static int
  1137. fb_mmap(struct file *file, struct vm_area_struct * vma)
  1138. {
  1139. struct fb_info *info = file_fb_info(file);
  1140. struct fb_ops *fb;
  1141. unsigned long off;
  1142. unsigned long start;
  1143. u32 len;
  1144. if (!info)
  1145. return -ENODEV;
  1146. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  1147. return -EINVAL;
  1148. off = vma->vm_pgoff << PAGE_SHIFT;
  1149. fb = info->fbops;
  1150. if (!fb)
  1151. return -ENODEV;
  1152. mutex_lock(&info->mm_lock);
  1153. if (fb->fb_mmap) {
  1154. int res;
  1155. res = fb->fb_mmap(info, vma);
  1156. mutex_unlock(&info->mm_lock);
  1157. return res;
  1158. }
  1159. /* frame buffer memory */
  1160. start = info->fix.smem_start;
  1161. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
  1162. if (off >= len) {
  1163. /* memory mapped io */
  1164. off -= len;
  1165. if (info->var.accel_flags) {
  1166. mutex_unlock(&info->mm_lock);
  1167. return -EINVAL;
  1168. }
  1169. start = info->fix.mmio_start;
  1170. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
  1171. }
  1172. mutex_unlock(&info->mm_lock);
  1173. start &= PAGE_MASK;
  1174. if ((vma->vm_end - vma->vm_start + off) > len)
  1175. return -EINVAL;
  1176. off += start;
  1177. vma->vm_pgoff = off >> PAGE_SHIFT;
  1178. /* This is an IO map - tell maydump to skip this VMA */
  1179. vma->vm_flags |= VM_IO | VM_RESERVED;
  1180. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  1181. fb_pgprotect(file, vma, off);
  1182. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  1183. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  1184. return -EAGAIN;
  1185. return 0;
  1186. }
  1187. static int
  1188. fb_open(struct inode *inode, struct file *file)
  1189. __acquires(&info->lock)
  1190. __releases(&info->lock)
  1191. {
  1192. int fbidx = iminor(inode);
  1193. struct fb_info *info;
  1194. int res = 0;
  1195. info = get_fb_info(fbidx);
  1196. if (!info) {
  1197. request_module("fb%d", fbidx);
  1198. info = get_fb_info(fbidx);
  1199. if (!info)
  1200. return -ENODEV;
  1201. }
  1202. if (IS_ERR(info))
  1203. return PTR_ERR(info);
  1204. mutex_lock(&info->lock);
  1205. if (!try_module_get(info->fbops->owner)) {
  1206. res = -ENODEV;
  1207. goto out;
  1208. }
  1209. file->private_data = info;
  1210. if (info->fbops->fb_open) {
  1211. res = info->fbops->fb_open(info,1);
  1212. if (res)
  1213. module_put(info->fbops->owner);
  1214. }
  1215. #ifdef CONFIG_FB_DEFERRED_IO
  1216. if (info->fbdefio)
  1217. fb_deferred_io_open(info, inode, file);
  1218. #endif
  1219. out:
  1220. mutex_unlock(&info->lock);
  1221. if (res)
  1222. put_fb_info(info);
  1223. return res;
  1224. }
  1225. static int
  1226. fb_release(struct inode *inode, struct file *file)
  1227. __acquires(&info->lock)
  1228. __releases(&info->lock)
  1229. {
  1230. struct fb_info * const info = file->private_data;
  1231. mutex_lock(&info->lock);
  1232. if (info->fbops->fb_release)
  1233. info->fbops->fb_release(info,1);
  1234. module_put(info->fbops->owner);
  1235. mutex_unlock(&info->lock);
  1236. put_fb_info(info);
  1237. return 0;
  1238. }
  1239. static const struct file_operations fb_fops = {
  1240. .owner = THIS_MODULE,
  1241. .read = fb_read,
  1242. .write = fb_write,
  1243. .unlocked_ioctl = fb_ioctl,
  1244. #ifdef CONFIG_COMPAT
  1245. .compat_ioctl = fb_compat_ioctl,
  1246. #endif
  1247. .mmap = fb_mmap,
  1248. .open = fb_open,
  1249. .release = fb_release,
  1250. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
  1251. .get_unmapped_area = get_fb_unmapped_area,
  1252. #endif
  1253. #ifdef CONFIG_FB_DEFERRED_IO
  1254. .fsync = fb_deferred_io_fsync,
  1255. #endif
  1256. .llseek = default_llseek,
  1257. };
  1258. struct class *fb_class;
  1259. EXPORT_SYMBOL(fb_class);
  1260. static int fb_check_foreignness(struct fb_info *fi)
  1261. {
  1262. const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
  1263. fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
  1264. #ifdef __BIG_ENDIAN
  1265. fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
  1266. #else
  1267. fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
  1268. #endif /* __BIG_ENDIAN */
  1269. if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
  1270. pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
  1271. "support this framebuffer\n", fi->fix.id);
  1272. return -ENOSYS;
  1273. } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
  1274. pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
  1275. "support this framebuffer\n", fi->fix.id);
  1276. return -ENOSYS;
  1277. }
  1278. return 0;
  1279. }
  1280. static bool apertures_overlap(struct aperture *gen, struct aperture *hw)
  1281. {
  1282. /* is the generic aperture base the same as the HW one */
  1283. if (gen->base == hw->base)
  1284. return true;
  1285. /* is the generic aperture base inside the hw base->hw base+size */
  1286. if (gen->base > hw->base && gen->base < hw->base + hw->size)
  1287. return true;
  1288. return false;
  1289. }
  1290. static bool fb_do_apertures_overlap(struct apertures_struct *gena,
  1291. struct apertures_struct *hwa)
  1292. {
  1293. int i, j;
  1294. if (!hwa || !gena)
  1295. return false;
  1296. for (i = 0; i < hwa->count; ++i) {
  1297. struct aperture *h = &hwa->ranges[i];
  1298. for (j = 0; j < gena->count; ++j) {
  1299. struct aperture *g = &gena->ranges[j];
  1300. printk(KERN_DEBUG "checking generic (%llx %llx) vs hw (%llx %llx)\n",
  1301. (unsigned long long)g->base,
  1302. (unsigned long long)g->size,
  1303. (unsigned long long)h->base,
  1304. (unsigned long long)h->size);
  1305. if (apertures_overlap(g, h))
  1306. return true;
  1307. }
  1308. }
  1309. return false;
  1310. }
  1311. static int do_unregister_framebuffer(struct fb_info *fb_info);
  1312. #define VGA_FB_PHYS 0xA0000
  1313. static void do_remove_conflicting_framebuffers(struct apertures_struct *a,
  1314. const char *name, bool primary)
  1315. {
  1316. int i;
  1317. /* check all firmware fbs and kick off if the base addr overlaps */
  1318. for (i = 0 ; i < FB_MAX; i++) {
  1319. struct apertures_struct *gen_aper;
  1320. if (!registered_fb[i])
  1321. continue;
  1322. if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE))
  1323. continue;
  1324. gen_aper = registered_fb[i]->apertures;
  1325. if (fb_do_apertures_overlap(gen_aper, a) ||
  1326. (primary && gen_aper && gen_aper->count &&
  1327. gen_aper->ranges[0].base == VGA_FB_PHYS)) {
  1328. printk(KERN_INFO "fb: conflicting fb hw usage "
  1329. "%s vs %s - removing generic driver\n",
  1330. name, registered_fb[i]->fix.id);
  1331. do_unregister_framebuffer(registered_fb[i]);
  1332. }
  1333. }
  1334. }
  1335. static int do_register_framebuffer(struct fb_info *fb_info)
  1336. {
  1337. int i;
  1338. struct fb_event event;
  1339. struct fb_videomode mode;
  1340. if (fb_check_foreignness(fb_info))
  1341. return -ENOSYS;
  1342. do_remove_conflicting_framebuffers(fb_info->apertures, fb_info->fix.id,
  1343. fb_is_primary_device(fb_info));
  1344. if (num_registered_fb == FB_MAX)
  1345. return -ENXIO;
  1346. num_registered_fb++;
  1347. for (i = 0 ; i < FB_MAX; i++)
  1348. if (!registered_fb[i])
  1349. break;
  1350. fb_info->node = i;
  1351. atomic_set(&fb_info->count, 1);
  1352. mutex_init(&fb_info->lock);
  1353. mutex_init(&fb_info->mm_lock);
  1354. fb_info->dev = device_create(fb_class, fb_info->device,
  1355. MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
  1356. if (IS_ERR(fb_info->dev)) {
  1357. /* Not fatal */
  1358. printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
  1359. fb_info->dev = NULL;
  1360. } else
  1361. fb_init_device(fb_info);
  1362. if (fb_info->pixmap.addr == NULL) {
  1363. fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
  1364. if (fb_info->pixmap.addr) {
  1365. fb_info->pixmap.size = FBPIXMAPSIZE;
  1366. fb_info->pixmap.buf_align = 1;
  1367. fb_info->pixmap.scan_align = 1;
  1368. fb_info->pixmap.access_align = 32;
  1369. fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
  1370. }
  1371. }
  1372. fb_info->pixmap.offset = 0;
  1373. if (!fb_info->pixmap.blit_x)
  1374. fb_info->pixmap.blit_x = ~(u32)0;
  1375. if (!fb_info->pixmap.blit_y)
  1376. fb_info->pixmap.blit_y = ~(u32)0;
  1377. if (!fb_info->modelist.prev || !fb_info->modelist.next)
  1378. INIT_LIST_HEAD(&fb_info->modelist);
  1379. fb_var_to_videomode(&mode, &fb_info->var);
  1380. fb_add_videomode(&mode, &fb_info->modelist);
  1381. registered_fb[i] = fb_info;
  1382. event.info = fb_info;
  1383. if (!lock_fb_info(fb_info))
  1384. return -ENODEV;
  1385. fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
  1386. unlock_fb_info(fb_info);
  1387. return 0;
  1388. }
  1389. static int do_unregister_framebuffer(struct fb_info *fb_info)
  1390. {
  1391. struct fb_event event;
  1392. int i, ret = 0;
  1393. i = fb_info->node;
  1394. if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
  1395. return -EINVAL;
  1396. if (!lock_fb_info(fb_info))
  1397. return -ENODEV;
  1398. event.info = fb_info;
  1399. ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
  1400. unlock_fb_info(fb_info);
  1401. if (ret)
  1402. return -EINVAL;
  1403. unlink_framebuffer(fb_info);
  1404. if (fb_info->pixmap.addr &&
  1405. (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
  1406. kfree(fb_info->pixmap.addr);
  1407. fb_destroy_modelist(&fb_info->modelist);
  1408. registered_fb[i] = NULL;
  1409. num_registered_fb--;
  1410. fb_cleanup_device(fb_info);
  1411. event.info = fb_info;
  1412. fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
  1413. /* this may free fb info */
  1414. put_fb_info(fb_info);
  1415. return 0;
  1416. }
  1417. int unlink_framebuffer(struct fb_info *fb_info)
  1418. {
  1419. int i;
  1420. i = fb_info->node;
  1421. if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
  1422. return -EINVAL;
  1423. if (fb_info->dev) {
  1424. device_destroy(fb_class, MKDEV(FB_MAJOR, i));
  1425. fb_info->dev = NULL;
  1426. }
  1427. return 0;
  1428. }
  1429. EXPORT_SYMBOL(unlink_framebuffer);
  1430. void remove_conflicting_framebuffers(struct apertures_struct *a,
  1431. const char *name, bool primary)
  1432. {
  1433. mutex_lock(&registration_lock);
  1434. do_remove_conflicting_framebuffers(a, name, primary);
  1435. mutex_unlock(&registration_lock);
  1436. }
  1437. EXPORT_SYMBOL(remove_conflicting_framebuffers);
  1438. /**
  1439. * register_framebuffer - registers a frame buffer device
  1440. * @fb_info: frame buffer info structure
  1441. *
  1442. * Registers a frame buffer device @fb_info.
  1443. *
  1444. * Returns negative errno on error, or zero for success.
  1445. *
  1446. */
  1447. int
  1448. register_framebuffer(struct fb_info *fb_info)
  1449. {
  1450. int ret;
  1451. mutex_lock(&registration_lock);
  1452. ret = do_register_framebuffer(fb_info);
  1453. mutex_unlock(&registration_lock);
  1454. return ret;
  1455. }
  1456. /**
  1457. * unregister_framebuffer - releases a frame buffer device
  1458. * @fb_info: frame buffer info structure
  1459. *
  1460. * Unregisters a frame buffer device @fb_info.
  1461. *
  1462. * Returns negative errno on error, or zero for success.
  1463. *
  1464. * This function will also notify the framebuffer console
  1465. * to release the driver.
  1466. *
  1467. * This is meant to be called within a driver's module_exit()
  1468. * function. If this is called outside module_exit(), ensure
  1469. * that the driver implements fb_open() and fb_release() to
  1470. * check that no processes are using the device.
  1471. */
  1472. int
  1473. unregister_framebuffer(struct fb_info *fb_info)
  1474. {
  1475. int ret;
  1476. mutex_lock(&registration_lock);
  1477. ret = do_unregister_framebuffer(fb_info);
  1478. mutex_unlock(&registration_lock);
  1479. return ret;
  1480. }
  1481. /**
  1482. * fb_set_suspend - low level driver signals suspend
  1483. * @info: framebuffer affected
  1484. * @state: 0 = resuming, !=0 = suspending
  1485. *
  1486. * This is meant to be used by low level drivers to
  1487. * signal suspend/resume to the core & clients.
  1488. * It must be called with the console semaphore held
  1489. */
  1490. void fb_set_suspend(struct fb_info *info, int state)
  1491. {
  1492. struct fb_event event;
  1493. event.info = info;
  1494. if (state) {
  1495. fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
  1496. info->state = FBINFO_STATE_SUSPENDED;
  1497. } else {
  1498. info->state = FBINFO_STATE_RUNNING;
  1499. fb_notifier_call_chain(FB_EVENT_RESUME, &event);
  1500. }
  1501. }
  1502. /**
  1503. * fbmem_init - init frame buffer subsystem
  1504. *
  1505. * Initialize the frame buffer subsystem.
  1506. *
  1507. * NOTE: This function is _only_ to be called by drivers/char/mem.c.
  1508. *
  1509. */
  1510. static int __init
  1511. fbmem_init(void)
  1512. {
  1513. proc_create("fb", 0, NULL, &fb_proc_fops);
  1514. if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
  1515. printk("unable to get major %d for fb devs\n", FB_MAJOR);
  1516. fb_class = class_create(THIS_MODULE, "graphics");
  1517. if (IS_ERR(fb_class)) {
  1518. printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
  1519. fb_class = NULL;
  1520. }
  1521. return 0;
  1522. }
  1523. #ifdef MODULE
  1524. module_init(fbmem_init);
  1525. static void __exit
  1526. fbmem_exit(void)
  1527. {
  1528. remove_proc_entry("fb", NULL);
  1529. class_destroy(fb_class);
  1530. unregister_chrdev(FB_MAJOR, "fb");
  1531. }
  1532. module_exit(fbmem_exit);
  1533. MODULE_LICENSE("GPL");
  1534. MODULE_DESCRIPTION("Framebuffer base");
  1535. #else
  1536. subsys_initcall(fbmem_init);
  1537. #endif
  1538. int fb_new_modelist(struct fb_info *info)
  1539. {
  1540. struct fb_event event;
  1541. struct fb_var_screeninfo var = info->var;
  1542. struct list_head *pos, *n;
  1543. struct fb_modelist *modelist;
  1544. struct fb_videomode *m, mode;
  1545. int err = 1;
  1546. list_for_each_safe(pos, n, &info->modelist) {
  1547. modelist = list_entry(pos, struct fb_modelist, list);
  1548. m = &modelist->mode;
  1549. fb_videomode_to_var(&var, m);
  1550. var.activate = FB_ACTIVATE_TEST;
  1551. err = fb_set_var(info, &var);
  1552. fb_var_to_videomode(&mode, &var);
  1553. if (err || !fb_mode_is_equal(m, &mode)) {
  1554. list_del(pos);
  1555. kfree(pos);
  1556. }
  1557. }
  1558. err = 1;
  1559. if (!list_empty(&info->modelist)) {
  1560. if (!lock_fb_info(info))
  1561. return -ENODEV;
  1562. event.info = info;
  1563. err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
  1564. unlock_fb_info(info);
  1565. }
  1566. return err;
  1567. }
  1568. static char *video_options[FB_MAX] __read_mostly;
  1569. static int ofonly __read_mostly;
  1570. /**
  1571. * fb_get_options - get kernel boot parameters
  1572. * @name: framebuffer name as it would appear in
  1573. * the boot parameter line
  1574. * (video=<name>:<options>)
  1575. * @option: the option will be stored here
  1576. *
  1577. * NOTE: Needed to maintain backwards compatibility
  1578. */
  1579. int fb_get_options(char *name, char **option)
  1580. {
  1581. char *opt, *options = NULL;
  1582. int retval = 0;
  1583. int name_len = strlen(name), i;
  1584. if (name_len && ofonly && strncmp(name, "offb", 4))
  1585. retval = 1;
  1586. if (name_len && !retval) {
  1587. for (i = 0; i < FB_MAX; i++) {
  1588. if (video_options[i] == NULL)
  1589. continue;
  1590. if (!video_options[i][0])
  1591. continue;
  1592. opt = video_options[i];
  1593. if (!strncmp(name, opt, name_len) &&
  1594. opt[name_len] == ':')
  1595. options = opt + name_len + 1;
  1596. }
  1597. }
  1598. if (options && !strncmp(options, "off", 3))
  1599. retval = 1;
  1600. if (option)
  1601. *option = options;
  1602. return retval;
  1603. }
  1604. #ifndef MODULE
  1605. /**
  1606. * video_setup - process command line options
  1607. * @options: string of options
  1608. *
  1609. * Process command line options for frame buffer subsystem.
  1610. *
  1611. * NOTE: This function is a __setup and __init function.
  1612. * It only stores the options. Drivers have to call
  1613. * fb_get_options() as necessary.
  1614. *
  1615. * Returns zero.
  1616. *
  1617. */
  1618. static int __init video_setup(char *options)
  1619. {
  1620. int i, global = 0;
  1621. if (!options || !*options)
  1622. global = 1;
  1623. if (!global && !strncmp(options, "ofonly", 6)) {
  1624. ofonly = 1;
  1625. global = 1;
  1626. }
  1627. if (!global && !strchr(options, ':')) {
  1628. fb_mode_option = options;
  1629. global = 1;
  1630. }
  1631. if (!global) {
  1632. for (i = 0; i < FB_MAX; i++) {
  1633. if (video_options[i] == NULL) {
  1634. video_options[i] = options;
  1635. break;
  1636. }
  1637. }
  1638. }
  1639. return 1;
  1640. }
  1641. __setup("video=", video_setup);
  1642. #endif
  1643. /*
  1644. * Visible symbols for modules
  1645. */
  1646. EXPORT_SYMBOL(register_framebuffer);
  1647. EXPORT_SYMBOL(unregister_framebuffer);
  1648. EXPORT_SYMBOL(num_registered_fb);
  1649. EXPORT_SYMBOL(registered_fb);
  1650. EXPORT_SYMBOL(fb_show_logo);
  1651. EXPORT_SYMBOL(fb_set_var);
  1652. EXPORT_SYMBOL(fb_blank);
  1653. EXPORT_SYMBOL(fb_pan_display);
  1654. EXPORT_SYMBOL(fb_get_buffer_offset);
  1655. EXPORT_SYMBOL(fb_set_suspend);
  1656. EXPORT_SYMBOL(fb_get_options);
  1657. MODULE_LICENSE("GPL");