fbmem.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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. u32 *buffer, *dst;
  601. u32 __iomem *src;
  602. int c, i, 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 = (u32 __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. for (i = c >> 2; i--; )
  630. *dst++ = fb_readl(src++);
  631. if (c & 3) {
  632. u8 *dst8 = (u8 *) dst;
  633. u8 __iomem *src8 = (u8 __iomem *) src;
  634. for (i = c & 3; i--;)
  635. *dst8++ = fb_readb(src8++);
  636. src = (u32 __iomem *) src8;
  637. }
  638. if (copy_to_user(buf, buffer, c)) {
  639. err = -EFAULT;
  640. break;
  641. }
  642. *ppos += c;
  643. buf += c;
  644. cnt += c;
  645. count -= c;
  646. }
  647. kfree(buffer);
  648. return (err) ? err : cnt;
  649. }
  650. static ssize_t
  651. fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  652. {
  653. unsigned long p = *ppos;
  654. struct inode *inode = file->f_path.dentry->d_inode;
  655. int fbidx = iminor(inode);
  656. struct fb_info *info = registered_fb[fbidx];
  657. u32 *buffer, *src;
  658. u32 __iomem *dst;
  659. int c, i, cnt = 0, err = 0;
  660. unsigned long total_size;
  661. if (!info || !info->screen_base)
  662. return -ENODEV;
  663. if (info->state != FBINFO_STATE_RUNNING)
  664. return -EPERM;
  665. if (info->fbops->fb_write)
  666. return info->fbops->fb_write(info, buf, count, ppos);
  667. total_size = info->screen_size;
  668. if (total_size == 0)
  669. total_size = info->fix.smem_len;
  670. if (p > total_size)
  671. return -EFBIG;
  672. if (count > total_size) {
  673. err = -EFBIG;
  674. count = total_size;
  675. }
  676. if (count + p > total_size) {
  677. if (!err)
  678. err = -ENOSPC;
  679. count = total_size - p;
  680. }
  681. buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
  682. GFP_KERNEL);
  683. if (!buffer)
  684. return -ENOMEM;
  685. dst = (u32 __iomem *) (info->screen_base + p);
  686. if (info->fbops->fb_sync)
  687. info->fbops->fb_sync(info);
  688. while (count) {
  689. c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  690. src = buffer;
  691. if (copy_from_user(src, buf, c)) {
  692. err = -EFAULT;
  693. break;
  694. }
  695. for (i = c >> 2; i--; )
  696. fb_writel(*src++, dst++);
  697. if (c & 3) {
  698. u8 *src8 = (u8 *) src;
  699. u8 __iomem *dst8 = (u8 __iomem *) dst;
  700. for (i = c & 3; i--; )
  701. fb_writeb(*src8++, dst8++);
  702. dst = (u32 __iomem *) dst8;
  703. }
  704. *ppos += c;
  705. buf += c;
  706. cnt += c;
  707. count -= c;
  708. }
  709. kfree(buffer);
  710. return (cnt) ? cnt : err;
  711. }
  712. int
  713. fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
  714. {
  715. struct fb_fix_screeninfo *fix = &info->fix;
  716. unsigned int yres = info->var.yres;
  717. int err = 0;
  718. if (var->yoffset > 0) {
  719. if (var->vmode & FB_VMODE_YWRAP) {
  720. if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
  721. err = -EINVAL;
  722. else
  723. yres = 0;
  724. } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
  725. err = -EINVAL;
  726. }
  727. if (var->xoffset > 0 && (!fix->xpanstep ||
  728. (var->xoffset % fix->xpanstep)))
  729. err = -EINVAL;
  730. if (err || !info->fbops->fb_pan_display ||
  731. var->yoffset > info->var.yres_virtual - yres ||
  732. var->xoffset > info->var.xres_virtual - info->var.xres)
  733. return -EINVAL;
  734. if ((err = info->fbops->fb_pan_display(var, info)))
  735. return err;
  736. info->var.xoffset = var->xoffset;
  737. info->var.yoffset = var->yoffset;
  738. if (var->vmode & FB_VMODE_YWRAP)
  739. info->var.vmode |= FB_VMODE_YWRAP;
  740. else
  741. info->var.vmode &= ~FB_VMODE_YWRAP;
  742. return 0;
  743. }
  744. static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
  745. u32 activate)
  746. {
  747. struct fb_event event;
  748. struct fb_blit_caps caps, fbcaps;
  749. int err = 0;
  750. memset(&caps, 0, sizeof(caps));
  751. memset(&fbcaps, 0, sizeof(fbcaps));
  752. caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
  753. event.info = info;
  754. event.data = &caps;
  755. fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
  756. info->fbops->fb_get_caps(info, &fbcaps, var);
  757. if (((fbcaps.x ^ caps.x) & caps.x) ||
  758. ((fbcaps.y ^ caps.y) & caps.y) ||
  759. (fbcaps.len < caps.len))
  760. err = -EINVAL;
  761. return err;
  762. }
  763. int
  764. fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
  765. {
  766. int flags = info->flags;
  767. int ret = 0;
  768. if (var->activate & FB_ACTIVATE_INV_MODE) {
  769. struct fb_videomode mode1, mode2;
  770. fb_var_to_videomode(&mode1, var);
  771. fb_var_to_videomode(&mode2, &info->var);
  772. /* make sure we don't delete the videomode of current var */
  773. ret = fb_mode_is_equal(&mode1, &mode2);
  774. if (!ret) {
  775. struct fb_event event;
  776. event.info = info;
  777. event.data = &mode1;
  778. ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
  779. }
  780. if (!ret)
  781. fb_delete_videomode(&mode1, &info->modelist);
  782. ret = (ret) ? -EINVAL : 0;
  783. goto done;
  784. }
  785. if ((var->activate & FB_ACTIVATE_FORCE) ||
  786. memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
  787. u32 activate = var->activate;
  788. if (!info->fbops->fb_check_var) {
  789. *var = info->var;
  790. goto done;
  791. }
  792. ret = info->fbops->fb_check_var(var, info);
  793. if (ret)
  794. goto done;
  795. if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
  796. struct fb_var_screeninfo old_var;
  797. struct fb_videomode mode;
  798. if (info->fbops->fb_get_caps) {
  799. ret = fb_check_caps(info, var, activate);
  800. if (ret)
  801. goto done;
  802. }
  803. old_var = info->var;
  804. info->var = *var;
  805. if (info->fbops->fb_set_par) {
  806. ret = info->fbops->fb_set_par(info);
  807. if (ret) {
  808. info->var = old_var;
  809. printk(KERN_WARNING "detected "
  810. "fb_set_par error, "
  811. "error code: %d\n", ret);
  812. goto done;
  813. }
  814. }
  815. fb_pan_display(info, &info->var);
  816. fb_set_cmap(&info->cmap, info);
  817. fb_var_to_videomode(&mode, &info->var);
  818. if (info->modelist.prev && info->modelist.next &&
  819. !list_empty(&info->modelist))
  820. ret = fb_add_videomode(&mode, &info->modelist);
  821. if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
  822. struct fb_event event;
  823. int evnt = (activate & FB_ACTIVATE_ALL) ?
  824. FB_EVENT_MODE_CHANGE_ALL :
  825. FB_EVENT_MODE_CHANGE;
  826. info->flags &= ~FBINFO_MISC_USEREVENT;
  827. event.info = info;
  828. event.data = &mode;
  829. fb_notifier_call_chain(evnt, &event);
  830. }
  831. }
  832. }
  833. done:
  834. return ret;
  835. }
  836. int
  837. fb_blank(struct fb_info *info, int blank)
  838. {
  839. int ret = -EINVAL;
  840. if (blank > FB_BLANK_POWERDOWN)
  841. blank = FB_BLANK_POWERDOWN;
  842. if (info->fbops->fb_blank)
  843. ret = info->fbops->fb_blank(blank, info);
  844. if (!ret) {
  845. struct fb_event event;
  846. event.info = info;
  847. event.data = &blank;
  848. fb_notifier_call_chain(FB_EVENT_BLANK, &event);
  849. }
  850. return ret;
  851. }
  852. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  853. unsigned long arg)
  854. {
  855. struct fb_ops *fb;
  856. struct fb_var_screeninfo var;
  857. struct fb_fix_screeninfo fix;
  858. struct fb_con2fbmap con2fb;
  859. struct fb_cmap cmap_from;
  860. struct fb_cmap_user cmap;
  861. struct fb_event event;
  862. void __user *argp = (void __user *)arg;
  863. long ret = 0;
  864. switch (cmd) {
  865. case FBIOGET_VSCREENINFO:
  866. if (!lock_fb_info(info))
  867. return -ENODEV;
  868. var = info->var;
  869. unlock_fb_info(info);
  870. ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
  871. break;
  872. case FBIOPUT_VSCREENINFO:
  873. if (copy_from_user(&var, argp, sizeof(var)))
  874. return -EFAULT;
  875. if (!lock_fb_info(info))
  876. return -ENODEV;
  877. acquire_console_sem();
  878. info->flags |= FBINFO_MISC_USEREVENT;
  879. ret = fb_set_var(info, &var);
  880. info->flags &= ~FBINFO_MISC_USEREVENT;
  881. release_console_sem();
  882. unlock_fb_info(info);
  883. if (!ret && copy_to_user(argp, &var, sizeof(var)))
  884. ret = -EFAULT;
  885. break;
  886. case FBIOGET_FSCREENINFO:
  887. if (!lock_fb_info(info))
  888. return -ENODEV;
  889. fix = info->fix;
  890. unlock_fb_info(info);
  891. ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
  892. break;
  893. case FBIOPUTCMAP:
  894. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  895. return -EFAULT;
  896. ret = fb_set_user_cmap(&cmap, info);
  897. break;
  898. case FBIOGETCMAP:
  899. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  900. return -EFAULT;
  901. if (!lock_fb_info(info))
  902. return -ENODEV;
  903. cmap_from = info->cmap;
  904. unlock_fb_info(info);
  905. ret = fb_cmap_to_user(&cmap_from, &cmap);
  906. break;
  907. case FBIOPAN_DISPLAY:
  908. if (copy_from_user(&var, argp, sizeof(var)))
  909. return -EFAULT;
  910. if (!lock_fb_info(info))
  911. return -ENODEV;
  912. acquire_console_sem();
  913. ret = fb_pan_display(info, &var);
  914. release_console_sem();
  915. unlock_fb_info(info);
  916. if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
  917. return -EFAULT;
  918. break;
  919. case FBIO_CURSOR:
  920. ret = -EINVAL;
  921. break;
  922. case FBIOGET_CON2FBMAP:
  923. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  924. return -EFAULT;
  925. if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
  926. return -EINVAL;
  927. con2fb.framebuffer = -1;
  928. event.data = &con2fb;
  929. if (!lock_fb_info(info))
  930. return -ENODEV;
  931. event.info = info;
  932. fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
  933. unlock_fb_info(info);
  934. ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
  935. break;
  936. case FBIOPUT_CON2FBMAP:
  937. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  938. return -EFAULT;
  939. if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
  940. return -EINVAL;
  941. if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
  942. return -EINVAL;
  943. if (!registered_fb[con2fb.framebuffer])
  944. request_module("fb%d", con2fb.framebuffer);
  945. if (!registered_fb[con2fb.framebuffer]) {
  946. ret = -EINVAL;
  947. break;
  948. }
  949. event.data = &con2fb;
  950. if (!lock_fb_info(info))
  951. return -ENODEV;
  952. event.info = info;
  953. ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);
  954. unlock_fb_info(info);
  955. break;
  956. case FBIOBLANK:
  957. if (!lock_fb_info(info))
  958. return -ENODEV;
  959. acquire_console_sem();
  960. info->flags |= FBINFO_MISC_USEREVENT;
  961. ret = fb_blank(info, arg);
  962. info->flags &= ~FBINFO_MISC_USEREVENT;
  963. release_console_sem();
  964. unlock_fb_info(info);
  965. break;
  966. default:
  967. if (!lock_fb_info(info))
  968. return -ENODEV;
  969. fb = info->fbops;
  970. if (fb->fb_ioctl)
  971. ret = fb->fb_ioctl(info, cmd, arg);
  972. else
  973. ret = -ENOTTY;
  974. unlock_fb_info(info);
  975. }
  976. return ret;
  977. }
  978. static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  979. {
  980. struct inode *inode = file->f_path.dentry->d_inode;
  981. int fbidx = iminor(inode);
  982. struct fb_info *info = registered_fb[fbidx];
  983. return do_fb_ioctl(info, cmd, arg);
  984. }
  985. #ifdef CONFIG_COMPAT
  986. struct fb_fix_screeninfo32 {
  987. char id[16];
  988. compat_caddr_t smem_start;
  989. u32 smem_len;
  990. u32 type;
  991. u32 type_aux;
  992. u32 visual;
  993. u16 xpanstep;
  994. u16 ypanstep;
  995. u16 ywrapstep;
  996. u32 line_length;
  997. compat_caddr_t mmio_start;
  998. u32 mmio_len;
  999. u32 accel;
  1000. u16 reserved[3];
  1001. };
  1002. struct fb_cmap32 {
  1003. u32 start;
  1004. u32 len;
  1005. compat_caddr_t red;
  1006. compat_caddr_t green;
  1007. compat_caddr_t blue;
  1008. compat_caddr_t transp;
  1009. };
  1010. static int fb_getput_cmap(struct fb_info *info, unsigned int cmd,
  1011. unsigned long arg)
  1012. {
  1013. struct fb_cmap_user __user *cmap;
  1014. struct fb_cmap32 __user *cmap32;
  1015. __u32 data;
  1016. int err;
  1017. cmap = compat_alloc_user_space(sizeof(*cmap));
  1018. cmap32 = compat_ptr(arg);
  1019. if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
  1020. return -EFAULT;
  1021. if (get_user(data, &cmap32->red) ||
  1022. put_user(compat_ptr(data), &cmap->red) ||
  1023. get_user(data, &cmap32->green) ||
  1024. put_user(compat_ptr(data), &cmap->green) ||
  1025. get_user(data, &cmap32->blue) ||
  1026. put_user(compat_ptr(data), &cmap->blue) ||
  1027. get_user(data, &cmap32->transp) ||
  1028. put_user(compat_ptr(data), &cmap->transp))
  1029. return -EFAULT;
  1030. err = do_fb_ioctl(info, cmd, (unsigned long) cmap);
  1031. if (!err) {
  1032. if (copy_in_user(&cmap32->start,
  1033. &cmap->start,
  1034. 2 * sizeof(__u32)))
  1035. err = -EFAULT;
  1036. }
  1037. return err;
  1038. }
  1039. static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
  1040. struct fb_fix_screeninfo32 __user *fix32)
  1041. {
  1042. __u32 data;
  1043. int err;
  1044. err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
  1045. data = (__u32) (unsigned long) fix->smem_start;
  1046. err |= put_user(data, &fix32->smem_start);
  1047. err |= put_user(fix->smem_len, &fix32->smem_len);
  1048. err |= put_user(fix->type, &fix32->type);
  1049. err |= put_user(fix->type_aux, &fix32->type_aux);
  1050. err |= put_user(fix->visual, &fix32->visual);
  1051. err |= put_user(fix->xpanstep, &fix32->xpanstep);
  1052. err |= put_user(fix->ypanstep, &fix32->ypanstep);
  1053. err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
  1054. err |= put_user(fix->line_length, &fix32->line_length);
  1055. data = (__u32) (unsigned long) fix->mmio_start;
  1056. err |= put_user(data, &fix32->mmio_start);
  1057. err |= put_user(fix->mmio_len, &fix32->mmio_len);
  1058. err |= put_user(fix->accel, &fix32->accel);
  1059. err |= copy_to_user(fix32->reserved, fix->reserved,
  1060. sizeof(fix->reserved));
  1061. return err;
  1062. }
  1063. static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd,
  1064. unsigned long arg)
  1065. {
  1066. mm_segment_t old_fs;
  1067. struct fb_fix_screeninfo fix;
  1068. struct fb_fix_screeninfo32 __user *fix32;
  1069. int err;
  1070. fix32 = compat_ptr(arg);
  1071. old_fs = get_fs();
  1072. set_fs(KERNEL_DS);
  1073. err = do_fb_ioctl(info, cmd, (unsigned long) &fix);
  1074. set_fs(old_fs);
  1075. if (!err)
  1076. err = do_fscreeninfo_to_user(&fix, fix32);
  1077. return err;
  1078. }
  1079. static long fb_compat_ioctl(struct file *file, unsigned int cmd,
  1080. unsigned long arg)
  1081. {
  1082. struct inode *inode = file->f_path.dentry->d_inode;
  1083. int fbidx = iminor(inode);
  1084. struct fb_info *info = registered_fb[fbidx];
  1085. struct fb_ops *fb = info->fbops;
  1086. long ret = -ENOIOCTLCMD;
  1087. switch(cmd) {
  1088. case FBIOGET_VSCREENINFO:
  1089. case FBIOPUT_VSCREENINFO:
  1090. case FBIOPAN_DISPLAY:
  1091. case FBIOGET_CON2FBMAP:
  1092. case FBIOPUT_CON2FBMAP:
  1093. arg = (unsigned long) compat_ptr(arg);
  1094. case FBIOBLANK:
  1095. ret = do_fb_ioctl(info, cmd, arg);
  1096. break;
  1097. case FBIOGET_FSCREENINFO:
  1098. ret = fb_get_fscreeninfo(info, cmd, arg);
  1099. break;
  1100. case FBIOGETCMAP:
  1101. case FBIOPUTCMAP:
  1102. ret = fb_getput_cmap(info, cmd, arg);
  1103. break;
  1104. default:
  1105. if (fb->fb_compat_ioctl)
  1106. ret = fb->fb_compat_ioctl(info, cmd, arg);
  1107. break;
  1108. }
  1109. return ret;
  1110. }
  1111. #endif
  1112. static int
  1113. fb_mmap(struct file *file, struct vm_area_struct * vma)
  1114. {
  1115. int fbidx = iminor(file->f_path.dentry->d_inode);
  1116. struct fb_info *info = registered_fb[fbidx];
  1117. struct fb_ops *fb = info->fbops;
  1118. unsigned long off;
  1119. unsigned long start;
  1120. u32 len;
  1121. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  1122. return -EINVAL;
  1123. off = vma->vm_pgoff << PAGE_SHIFT;
  1124. if (!fb)
  1125. return -ENODEV;
  1126. mutex_lock(&info->mm_lock);
  1127. if (fb->fb_mmap) {
  1128. int res;
  1129. res = fb->fb_mmap(info, vma);
  1130. mutex_unlock(&info->mm_lock);
  1131. return res;
  1132. }
  1133. /* frame buffer memory */
  1134. start = info->fix.smem_start;
  1135. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
  1136. if (off >= len) {
  1137. /* memory mapped io */
  1138. off -= len;
  1139. if (info->var.accel_flags) {
  1140. mutex_unlock(&info->mm_lock);
  1141. return -EINVAL;
  1142. }
  1143. start = info->fix.mmio_start;
  1144. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
  1145. }
  1146. mutex_unlock(&info->mm_lock);
  1147. start &= PAGE_MASK;
  1148. if ((vma->vm_end - vma->vm_start + off) > len)
  1149. return -EINVAL;
  1150. off += start;
  1151. vma->vm_pgoff = off >> PAGE_SHIFT;
  1152. /* This is an IO map - tell maydump to skip this VMA */
  1153. vma->vm_flags |= VM_IO | VM_RESERVED;
  1154. fb_pgprotect(file, vma, off);
  1155. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  1156. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  1157. return -EAGAIN;
  1158. return 0;
  1159. }
  1160. static int
  1161. fb_open(struct inode *inode, struct file *file)
  1162. __acquires(&info->lock)
  1163. __releases(&info->lock)
  1164. {
  1165. int fbidx = iminor(inode);
  1166. struct fb_info *info;
  1167. int res = 0;
  1168. if (fbidx >= FB_MAX)
  1169. return -ENODEV;
  1170. info = registered_fb[fbidx];
  1171. if (!info)
  1172. request_module("fb%d", fbidx);
  1173. info = registered_fb[fbidx];
  1174. if (!info)
  1175. return -ENODEV;
  1176. mutex_lock(&info->lock);
  1177. if (!try_module_get(info->fbops->owner)) {
  1178. res = -ENODEV;
  1179. goto out;
  1180. }
  1181. file->private_data = info;
  1182. if (info->fbops->fb_open) {
  1183. res = info->fbops->fb_open(info,1);
  1184. if (res)
  1185. module_put(info->fbops->owner);
  1186. }
  1187. #ifdef CONFIG_FB_DEFERRED_IO
  1188. if (info->fbdefio)
  1189. fb_deferred_io_open(info, inode, file);
  1190. #endif
  1191. out:
  1192. mutex_unlock(&info->lock);
  1193. return res;
  1194. }
  1195. static int
  1196. fb_release(struct inode *inode, struct file *file)
  1197. __acquires(&info->lock)
  1198. __releases(&info->lock)
  1199. {
  1200. struct fb_info * const info = file->private_data;
  1201. mutex_lock(&info->lock);
  1202. if (info->fbops->fb_release)
  1203. info->fbops->fb_release(info,1);
  1204. module_put(info->fbops->owner);
  1205. mutex_unlock(&info->lock);
  1206. return 0;
  1207. }
  1208. static const struct file_operations fb_fops = {
  1209. .owner = THIS_MODULE,
  1210. .read = fb_read,
  1211. .write = fb_write,
  1212. .unlocked_ioctl = fb_ioctl,
  1213. #ifdef CONFIG_COMPAT
  1214. .compat_ioctl = fb_compat_ioctl,
  1215. #endif
  1216. .mmap = fb_mmap,
  1217. .open = fb_open,
  1218. .release = fb_release,
  1219. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
  1220. .get_unmapped_area = get_fb_unmapped_area,
  1221. #endif
  1222. #ifdef CONFIG_FB_DEFERRED_IO
  1223. .fsync = fb_deferred_io_fsync,
  1224. #endif
  1225. };
  1226. struct class *fb_class;
  1227. EXPORT_SYMBOL(fb_class);
  1228. static int fb_check_foreignness(struct fb_info *fi)
  1229. {
  1230. const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
  1231. fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
  1232. #ifdef __BIG_ENDIAN
  1233. fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
  1234. #else
  1235. fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
  1236. #endif /* __BIG_ENDIAN */
  1237. if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
  1238. pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
  1239. "support this framebuffer\n", fi->fix.id);
  1240. return -ENOSYS;
  1241. } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
  1242. pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
  1243. "support this framebuffer\n", fi->fix.id);
  1244. return -ENOSYS;
  1245. }
  1246. return 0;
  1247. }
  1248. static bool fb_do_apertures_overlap(struct fb_info *gen, struct fb_info *hw)
  1249. {
  1250. /* is the generic aperture base the same as the HW one */
  1251. if (gen->aperture_base == hw->aperture_base)
  1252. return true;
  1253. /* is the generic aperture base inside the hw base->hw base+size */
  1254. if (gen->aperture_base > hw->aperture_base && gen->aperture_base <= hw->aperture_base + hw->aperture_size)
  1255. return true;
  1256. return false;
  1257. }
  1258. /**
  1259. * register_framebuffer - registers a frame buffer device
  1260. * @fb_info: frame buffer info structure
  1261. *
  1262. * Registers a frame buffer device @fb_info.
  1263. *
  1264. * Returns negative errno on error, or zero for success.
  1265. *
  1266. */
  1267. int
  1268. register_framebuffer(struct fb_info *fb_info)
  1269. {
  1270. int i;
  1271. struct fb_event event;
  1272. struct fb_videomode mode;
  1273. if (num_registered_fb == FB_MAX)
  1274. return -ENXIO;
  1275. if (fb_check_foreignness(fb_info))
  1276. return -ENOSYS;
  1277. /* check all firmware fbs and kick off if the base addr overlaps */
  1278. for (i = 0 ; i < FB_MAX; i++) {
  1279. if (!registered_fb[i])
  1280. continue;
  1281. if (registered_fb[i]->flags & FBINFO_MISC_FIRMWARE) {
  1282. if (fb_do_apertures_overlap(registered_fb[i], fb_info)) {
  1283. printk(KERN_ERR "fb: conflicting fb hw usage "
  1284. "%s vs %s - removing generic driver\n",
  1285. fb_info->fix.id,
  1286. registered_fb[i]->fix.id);
  1287. unregister_framebuffer(registered_fb[i]);
  1288. }
  1289. }
  1290. }
  1291. num_registered_fb++;
  1292. for (i = 0 ; i < FB_MAX; i++)
  1293. if (!registered_fb[i])
  1294. break;
  1295. fb_info->node = i;
  1296. mutex_init(&fb_info->lock);
  1297. mutex_init(&fb_info->mm_lock);
  1298. fb_info->dev = device_create(fb_class, fb_info->device,
  1299. MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
  1300. if (IS_ERR(fb_info->dev)) {
  1301. /* Not fatal */
  1302. printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
  1303. fb_info->dev = NULL;
  1304. } else
  1305. fb_init_device(fb_info);
  1306. if (fb_info->pixmap.addr == NULL) {
  1307. fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
  1308. if (fb_info->pixmap.addr) {
  1309. fb_info->pixmap.size = FBPIXMAPSIZE;
  1310. fb_info->pixmap.buf_align = 1;
  1311. fb_info->pixmap.scan_align = 1;
  1312. fb_info->pixmap.access_align = 32;
  1313. fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
  1314. }
  1315. }
  1316. fb_info->pixmap.offset = 0;
  1317. if (!fb_info->pixmap.blit_x)
  1318. fb_info->pixmap.blit_x = ~(u32)0;
  1319. if (!fb_info->pixmap.blit_y)
  1320. fb_info->pixmap.blit_y = ~(u32)0;
  1321. if (!fb_info->modelist.prev || !fb_info->modelist.next)
  1322. INIT_LIST_HEAD(&fb_info->modelist);
  1323. fb_var_to_videomode(&mode, &fb_info->var);
  1324. fb_add_videomode(&mode, &fb_info->modelist);
  1325. registered_fb[i] = fb_info;
  1326. event.info = fb_info;
  1327. if (!lock_fb_info(fb_info))
  1328. return -ENODEV;
  1329. fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
  1330. unlock_fb_info(fb_info);
  1331. return 0;
  1332. }
  1333. /**
  1334. * unregister_framebuffer - releases a frame buffer device
  1335. * @fb_info: frame buffer info structure
  1336. *
  1337. * Unregisters a frame buffer device @fb_info.
  1338. *
  1339. * Returns negative errno on error, or zero for success.
  1340. *
  1341. * This function will also notify the framebuffer console
  1342. * to release the driver.
  1343. *
  1344. * This is meant to be called within a driver's module_exit()
  1345. * function. If this is called outside module_exit(), ensure
  1346. * that the driver implements fb_open() and fb_release() to
  1347. * check that no processes are using the device.
  1348. */
  1349. int
  1350. unregister_framebuffer(struct fb_info *fb_info)
  1351. {
  1352. struct fb_event event;
  1353. int i, ret = 0;
  1354. i = fb_info->node;
  1355. if (!registered_fb[i]) {
  1356. ret = -EINVAL;
  1357. goto done;
  1358. }
  1359. if (!lock_fb_info(fb_info))
  1360. return -ENODEV;
  1361. event.info = fb_info;
  1362. ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
  1363. unlock_fb_info(fb_info);
  1364. if (ret) {
  1365. ret = -EINVAL;
  1366. goto done;
  1367. }
  1368. if (fb_info->pixmap.addr &&
  1369. (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
  1370. kfree(fb_info->pixmap.addr);
  1371. fb_destroy_modelist(&fb_info->modelist);
  1372. registered_fb[i]=NULL;
  1373. num_registered_fb--;
  1374. fb_cleanup_device(fb_info);
  1375. device_destroy(fb_class, MKDEV(FB_MAJOR, i));
  1376. event.info = fb_info;
  1377. fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
  1378. /* this may free fb info */
  1379. if (fb_info->fbops->fb_destroy)
  1380. fb_info->fbops->fb_destroy(fb_info);
  1381. done:
  1382. return ret;
  1383. }
  1384. /**
  1385. * fb_set_suspend - low level driver signals suspend
  1386. * @info: framebuffer affected
  1387. * @state: 0 = resuming, !=0 = suspending
  1388. *
  1389. * This is meant to be used by low level drivers to
  1390. * signal suspend/resume to the core & clients.
  1391. * It must be called with the console semaphore held
  1392. */
  1393. void fb_set_suspend(struct fb_info *info, int state)
  1394. {
  1395. struct fb_event event;
  1396. if (!lock_fb_info(info))
  1397. return;
  1398. event.info = info;
  1399. if (state) {
  1400. fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
  1401. info->state = FBINFO_STATE_SUSPENDED;
  1402. } else {
  1403. info->state = FBINFO_STATE_RUNNING;
  1404. fb_notifier_call_chain(FB_EVENT_RESUME, &event);
  1405. }
  1406. unlock_fb_info(info);
  1407. }
  1408. /**
  1409. * fbmem_init - init frame buffer subsystem
  1410. *
  1411. * Initialize the frame buffer subsystem.
  1412. *
  1413. * NOTE: This function is _only_ to be called by drivers/char/mem.c.
  1414. *
  1415. */
  1416. static int __init
  1417. fbmem_init(void)
  1418. {
  1419. proc_create("fb", 0, NULL, &fb_proc_fops);
  1420. if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
  1421. printk("unable to get major %d for fb devs\n", FB_MAJOR);
  1422. fb_class = class_create(THIS_MODULE, "graphics");
  1423. if (IS_ERR(fb_class)) {
  1424. printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
  1425. fb_class = NULL;
  1426. }
  1427. return 0;
  1428. }
  1429. #ifdef MODULE
  1430. module_init(fbmem_init);
  1431. static void __exit
  1432. fbmem_exit(void)
  1433. {
  1434. remove_proc_entry("fb", NULL);
  1435. class_destroy(fb_class);
  1436. unregister_chrdev(FB_MAJOR, "fb");
  1437. }
  1438. module_exit(fbmem_exit);
  1439. MODULE_LICENSE("GPL");
  1440. MODULE_DESCRIPTION("Framebuffer base");
  1441. #else
  1442. subsys_initcall(fbmem_init);
  1443. #endif
  1444. int fb_new_modelist(struct fb_info *info)
  1445. {
  1446. struct fb_event event;
  1447. struct fb_var_screeninfo var = info->var;
  1448. struct list_head *pos, *n;
  1449. struct fb_modelist *modelist;
  1450. struct fb_videomode *m, mode;
  1451. int err = 1;
  1452. list_for_each_safe(pos, n, &info->modelist) {
  1453. modelist = list_entry(pos, struct fb_modelist, list);
  1454. m = &modelist->mode;
  1455. fb_videomode_to_var(&var, m);
  1456. var.activate = FB_ACTIVATE_TEST;
  1457. err = fb_set_var(info, &var);
  1458. fb_var_to_videomode(&mode, &var);
  1459. if (err || !fb_mode_is_equal(m, &mode)) {
  1460. list_del(pos);
  1461. kfree(pos);
  1462. }
  1463. }
  1464. err = 1;
  1465. if (!list_empty(&info->modelist)) {
  1466. if (!lock_fb_info(info))
  1467. return -ENODEV;
  1468. event.info = info;
  1469. err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
  1470. unlock_fb_info(info);
  1471. }
  1472. return err;
  1473. }
  1474. static char *video_options[FB_MAX] __read_mostly;
  1475. static int ofonly __read_mostly;
  1476. /**
  1477. * fb_get_options - get kernel boot parameters
  1478. * @name: framebuffer name as it would appear in
  1479. * the boot parameter line
  1480. * (video=<name>:<options>)
  1481. * @option: the option will be stored here
  1482. *
  1483. * NOTE: Needed to maintain backwards compatibility
  1484. */
  1485. int fb_get_options(char *name, char **option)
  1486. {
  1487. char *opt, *options = NULL;
  1488. int opt_len, retval = 0;
  1489. int name_len = strlen(name), i;
  1490. if (name_len && ofonly && strncmp(name, "offb", 4))
  1491. retval = 1;
  1492. if (name_len && !retval) {
  1493. for (i = 0; i < FB_MAX; i++) {
  1494. if (video_options[i] == NULL)
  1495. continue;
  1496. opt_len = strlen(video_options[i]);
  1497. if (!opt_len)
  1498. continue;
  1499. opt = video_options[i];
  1500. if (!strncmp(name, opt, name_len) &&
  1501. opt[name_len] == ':')
  1502. options = opt + name_len + 1;
  1503. }
  1504. }
  1505. if (options && !strncmp(options, "off", 3))
  1506. retval = 1;
  1507. if (option)
  1508. *option = options;
  1509. return retval;
  1510. }
  1511. #ifndef MODULE
  1512. /**
  1513. * video_setup - process command line options
  1514. * @options: string of options
  1515. *
  1516. * Process command line options for frame buffer subsystem.
  1517. *
  1518. * NOTE: This function is a __setup and __init function.
  1519. * It only stores the options. Drivers have to call
  1520. * fb_get_options() as necessary.
  1521. *
  1522. * Returns zero.
  1523. *
  1524. */
  1525. static int __init video_setup(char *options)
  1526. {
  1527. int i, global = 0;
  1528. if (!options || !*options)
  1529. global = 1;
  1530. if (!global && !strncmp(options, "ofonly", 6)) {
  1531. ofonly = 1;
  1532. global = 1;
  1533. }
  1534. if (!global && !strchr(options, ':')) {
  1535. fb_mode_option = options;
  1536. global = 1;
  1537. }
  1538. if (!global) {
  1539. for (i = 0; i < FB_MAX; i++) {
  1540. if (video_options[i] == NULL) {
  1541. video_options[i] = options;
  1542. break;
  1543. }
  1544. }
  1545. }
  1546. return 1;
  1547. }
  1548. __setup("video=", video_setup);
  1549. #endif
  1550. /*
  1551. * Visible symbols for modules
  1552. */
  1553. EXPORT_SYMBOL(register_framebuffer);
  1554. EXPORT_SYMBOL(unregister_framebuffer);
  1555. EXPORT_SYMBOL(num_registered_fb);
  1556. EXPORT_SYMBOL(registered_fb);
  1557. EXPORT_SYMBOL(fb_show_logo);
  1558. EXPORT_SYMBOL(fb_set_var);
  1559. EXPORT_SYMBOL(fb_blank);
  1560. EXPORT_SYMBOL(fb_pan_display);
  1561. EXPORT_SYMBOL(fb_get_buffer_offset);
  1562. EXPORT_SYMBOL(fb_set_suspend);
  1563. EXPORT_SYMBOL(fb_get_options);
  1564. MODULE_LICENSE("GPL");