fbmem.c 39 KB

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