fbmem.c 43 KB

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