fbmem.c 39 KB

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