fbmem.c 38 KB

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