fbmem.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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 struct notifier_block *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. cnt = 0;
  525. buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
  526. GFP_KERNEL);
  527. if (!buffer)
  528. return -ENOMEM;
  529. src = (u32 __iomem *) (info->screen_base + p);
  530. if (info->fbops->fb_sync)
  531. info->fbops->fb_sync(info);
  532. while (count) {
  533. c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  534. dst = buffer;
  535. for (i = c >> 2; i--; )
  536. *dst++ = fb_readl(src++);
  537. if (c & 3) {
  538. u8 *dst8 = (u8 *) dst;
  539. u8 __iomem *src8 = (u8 __iomem *) src;
  540. for (i = c & 3; i--;)
  541. *dst8++ = fb_readb(src8++);
  542. src = (u32 __iomem *) src8;
  543. }
  544. if (copy_to_user(buf, buffer, c)) {
  545. err = -EFAULT;
  546. break;
  547. }
  548. *ppos += c;
  549. buf += c;
  550. cnt += c;
  551. count -= c;
  552. }
  553. kfree(buffer);
  554. return (err) ? err : cnt;
  555. }
  556. static ssize_t
  557. fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  558. {
  559. unsigned long p = *ppos;
  560. struct inode *inode = file->f_dentry->d_inode;
  561. int fbidx = iminor(inode);
  562. struct fb_info *info = registered_fb[fbidx];
  563. u32 *buffer, *src;
  564. u32 __iomem *dst;
  565. int c, i, cnt = 0, err;
  566. unsigned long total_size;
  567. if (!info || !info->screen_base)
  568. return -ENODEV;
  569. if (info->state != FBINFO_STATE_RUNNING)
  570. return -EPERM;
  571. if (info->fbops->fb_write)
  572. return info->fbops->fb_write(file, buf, count, ppos);
  573. total_size = info->screen_size;
  574. if (total_size == 0)
  575. total_size = info->fix.smem_len;
  576. if (p > total_size)
  577. return -ENOSPC;
  578. if (count >= total_size)
  579. count = total_size;
  580. err = 0;
  581. if (count + p > total_size) {
  582. count = total_size - p;
  583. err = -ENOSPC;
  584. }
  585. cnt = 0;
  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 (err) ? err : cnt;
  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 = 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. notifier_call_chain(&fb_notifier_list, evnt,
  706. &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. notifier_call_chain(&fb_notifier_list, FB_EVENT_BLANK, &event);
  725. }
  726. return ret;
  727. }
  728. static int
  729. fb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  730. unsigned long arg)
  731. {
  732. int fbidx = iminor(inode);
  733. struct fb_info *info = registered_fb[fbidx];
  734. struct fb_ops *fb = info->fbops;
  735. struct fb_var_screeninfo var;
  736. struct fb_fix_screeninfo fix;
  737. struct fb_con2fbmap con2fb;
  738. struct fb_cmap_user cmap;
  739. struct fb_event event;
  740. void __user *argp = (void __user *)arg;
  741. int i;
  742. if (!fb)
  743. return -ENODEV;
  744. switch (cmd) {
  745. case FBIOGET_VSCREENINFO:
  746. return copy_to_user(argp, &info->var,
  747. sizeof(var)) ? -EFAULT : 0;
  748. case FBIOPUT_VSCREENINFO:
  749. if (copy_from_user(&var, argp, sizeof(var)))
  750. return -EFAULT;
  751. acquire_console_sem();
  752. info->flags |= FBINFO_MISC_USEREVENT;
  753. i = fb_set_var(info, &var);
  754. info->flags &= ~FBINFO_MISC_USEREVENT;
  755. release_console_sem();
  756. if (i) return i;
  757. if (copy_to_user(argp, &var, sizeof(var)))
  758. return -EFAULT;
  759. return 0;
  760. case FBIOGET_FSCREENINFO:
  761. return copy_to_user(argp, &info->fix,
  762. sizeof(fix)) ? -EFAULT : 0;
  763. case FBIOPUTCMAP:
  764. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  765. return -EFAULT;
  766. return (fb_set_user_cmap(&cmap, info));
  767. case FBIOGETCMAP:
  768. if (copy_from_user(&cmap, argp, sizeof(cmap)))
  769. return -EFAULT;
  770. return fb_cmap_to_user(&info->cmap, &cmap);
  771. case FBIOPAN_DISPLAY:
  772. if (copy_from_user(&var, argp, sizeof(var)))
  773. return -EFAULT;
  774. acquire_console_sem();
  775. i = fb_pan_display(info, &var);
  776. release_console_sem();
  777. if (i)
  778. return i;
  779. if (copy_to_user(argp, &var, sizeof(var)))
  780. return -EFAULT;
  781. return 0;
  782. case FBIO_CURSOR:
  783. return -EINVAL;
  784. case FBIOGET_CON2FBMAP:
  785. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  786. return -EFAULT;
  787. if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
  788. return -EINVAL;
  789. con2fb.framebuffer = -1;
  790. event.info = info;
  791. event.data = &con2fb;
  792. notifier_call_chain(&fb_notifier_list,
  793. FB_EVENT_GET_CONSOLE_MAP, &event);
  794. return copy_to_user(argp, &con2fb,
  795. sizeof(con2fb)) ? -EFAULT : 0;
  796. case FBIOPUT_CON2FBMAP:
  797. if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
  798. return - EFAULT;
  799. if (con2fb.console < 0 || con2fb.console > MAX_NR_CONSOLES)
  800. return -EINVAL;
  801. if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)
  802. return -EINVAL;
  803. #ifdef CONFIG_KMOD
  804. if (!registered_fb[con2fb.framebuffer])
  805. try_to_load(con2fb.framebuffer);
  806. #endif /* CONFIG_KMOD */
  807. if (!registered_fb[con2fb.framebuffer])
  808. return -EINVAL;
  809. event.info = info;
  810. event.data = &con2fb;
  811. return notifier_call_chain(&fb_notifier_list,
  812. FB_EVENT_SET_CONSOLE_MAP,
  813. &event);
  814. case FBIOBLANK:
  815. acquire_console_sem();
  816. info->flags |= FBINFO_MISC_USEREVENT;
  817. i = fb_blank(info, arg);
  818. info->flags &= ~FBINFO_MISC_USEREVENT;
  819. release_console_sem();
  820. return i;
  821. default:
  822. if (fb->fb_ioctl == NULL)
  823. return -EINVAL;
  824. return fb->fb_ioctl(inode, file, cmd, arg, info);
  825. }
  826. }
  827. #ifdef CONFIG_COMPAT
  828. struct fb_fix_screeninfo32 {
  829. char id[16];
  830. compat_caddr_t smem_start;
  831. u32 smem_len;
  832. u32 type;
  833. u32 type_aux;
  834. u32 visual;
  835. u16 xpanstep;
  836. u16 ypanstep;
  837. u16 ywrapstep;
  838. u32 line_length;
  839. compat_caddr_t mmio_start;
  840. u32 mmio_len;
  841. u32 accel;
  842. u16 reserved[3];
  843. };
  844. struct fb_cmap32 {
  845. u32 start;
  846. u32 len;
  847. compat_caddr_t red;
  848. compat_caddr_t green;
  849. compat_caddr_t blue;
  850. compat_caddr_t transp;
  851. };
  852. static int fb_getput_cmap(struct inode *inode, struct file *file,
  853. unsigned int cmd, unsigned long arg)
  854. {
  855. struct fb_cmap_user __user *cmap;
  856. struct fb_cmap32 __user *cmap32;
  857. __u32 data;
  858. int err;
  859. cmap = compat_alloc_user_space(sizeof(*cmap));
  860. cmap32 = compat_ptr(arg);
  861. if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
  862. return -EFAULT;
  863. if (get_user(data, &cmap32->red) ||
  864. put_user(compat_ptr(data), &cmap->red) ||
  865. get_user(data, &cmap32->green) ||
  866. put_user(compat_ptr(data), &cmap->green) ||
  867. get_user(data, &cmap32->blue) ||
  868. put_user(compat_ptr(data), &cmap->blue) ||
  869. get_user(data, &cmap32->transp) ||
  870. put_user(compat_ptr(data), &cmap->transp))
  871. return -EFAULT;
  872. err = fb_ioctl(inode, file, cmd, (unsigned long) cmap);
  873. if (!err) {
  874. if (copy_in_user(&cmap32->start,
  875. &cmap->start,
  876. 2 * sizeof(__u32)))
  877. err = -EFAULT;
  878. }
  879. return err;
  880. }
  881. static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
  882. struct fb_fix_screeninfo32 __user *fix32)
  883. {
  884. __u32 data;
  885. int err;
  886. err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
  887. data = (__u32) (unsigned long) fix->smem_start;
  888. err |= put_user(data, &fix32->smem_start);
  889. err |= put_user(fix->smem_len, &fix32->smem_len);
  890. err |= put_user(fix->type, &fix32->type);
  891. err |= put_user(fix->type_aux, &fix32->type_aux);
  892. err |= put_user(fix->visual, &fix32->visual);
  893. err |= put_user(fix->xpanstep, &fix32->xpanstep);
  894. err |= put_user(fix->ypanstep, &fix32->ypanstep);
  895. err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
  896. err |= put_user(fix->line_length, &fix32->line_length);
  897. data = (__u32) (unsigned long) fix->mmio_start;
  898. err |= put_user(data, &fix32->mmio_start);
  899. err |= put_user(fix->mmio_len, &fix32->mmio_len);
  900. err |= put_user(fix->accel, &fix32->accel);
  901. err |= copy_to_user(fix32->reserved, fix->reserved,
  902. sizeof(fix->reserved));
  903. return err;
  904. }
  905. static int fb_get_fscreeninfo(struct inode *inode, struct file *file,
  906. unsigned int cmd, unsigned long arg)
  907. {
  908. mm_segment_t old_fs;
  909. struct fb_fix_screeninfo fix;
  910. struct fb_fix_screeninfo32 __user *fix32;
  911. int err;
  912. fix32 = compat_ptr(arg);
  913. old_fs = get_fs();
  914. set_fs(KERNEL_DS);
  915. err = fb_ioctl(inode, file, cmd, (unsigned long) &fix);
  916. set_fs(old_fs);
  917. if (!err)
  918. err = do_fscreeninfo_to_user(&fix, fix32);
  919. return err;
  920. }
  921. static long
  922. fb_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  923. {
  924. struct inode *inode = file->f_dentry->d_inode;
  925. int fbidx = iminor(inode);
  926. struct fb_info *info = registered_fb[fbidx];
  927. struct fb_ops *fb = info->fbops;
  928. long ret = -ENOIOCTLCMD;
  929. lock_kernel();
  930. switch(cmd) {
  931. case FBIOGET_VSCREENINFO:
  932. case FBIOPUT_VSCREENINFO:
  933. case FBIOPAN_DISPLAY:
  934. case FBIOGET_CON2FBMAP:
  935. case FBIOPUT_CON2FBMAP:
  936. arg = (unsigned long) compat_ptr(arg);
  937. case FBIOBLANK:
  938. ret = fb_ioctl(inode, file, cmd, arg);
  939. break;
  940. case FBIOGET_FSCREENINFO:
  941. ret = fb_get_fscreeninfo(inode, file, cmd, arg);
  942. break;
  943. case FBIOGETCMAP:
  944. case FBIOPUTCMAP:
  945. ret = fb_getput_cmap(inode, file, cmd, arg);
  946. break;
  947. default:
  948. if (fb->fb_compat_ioctl)
  949. ret = fb->fb_compat_ioctl(file, cmd, arg, info);
  950. break;
  951. }
  952. unlock_kernel();
  953. return ret;
  954. }
  955. #endif
  956. static int
  957. fb_mmap(struct file *file, struct vm_area_struct * vma)
  958. {
  959. int fbidx = iminor(file->f_dentry->d_inode);
  960. struct fb_info *info = registered_fb[fbidx];
  961. struct fb_ops *fb = info->fbops;
  962. unsigned long off;
  963. #if !defined(__sparc__) || defined(__sparc_v9__)
  964. unsigned long start;
  965. u32 len;
  966. #endif
  967. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  968. return -EINVAL;
  969. off = vma->vm_pgoff << PAGE_SHIFT;
  970. if (!fb)
  971. return -ENODEV;
  972. if (fb->fb_mmap) {
  973. int res;
  974. lock_kernel();
  975. res = fb->fb_mmap(info, file, vma);
  976. unlock_kernel();
  977. return res;
  978. }
  979. #if defined(__sparc__) && !defined(__sparc_v9__)
  980. /* Should never get here, all fb drivers should have their own
  981. mmap routines */
  982. return -EINVAL;
  983. #else
  984. /* !sparc32... */
  985. lock_kernel();
  986. /* frame buffer memory */
  987. start = info->fix.smem_start;
  988. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
  989. if (off >= len) {
  990. /* memory mapped io */
  991. off -= len;
  992. if (info->var.accel_flags) {
  993. unlock_kernel();
  994. return -EINVAL;
  995. }
  996. start = info->fix.mmio_start;
  997. len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
  998. }
  999. unlock_kernel();
  1000. start &= PAGE_MASK;
  1001. if ((vma->vm_end - vma->vm_start + off) > len)
  1002. return -EINVAL;
  1003. off += start;
  1004. vma->vm_pgoff = off >> PAGE_SHIFT;
  1005. /* This is an IO map - tell maydump to skip this VMA */
  1006. vma->vm_flags |= VM_IO | VM_RESERVED;
  1007. #if defined(__sparc_v9__)
  1008. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  1009. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  1010. return -EAGAIN;
  1011. #else
  1012. #if defined(__mc68000__)
  1013. #if defined(CONFIG_SUN3)
  1014. pgprot_val(vma->vm_page_prot) |= SUN3_PAGE_NOCACHE;
  1015. #elif defined(CONFIG_MMU)
  1016. if (CPU_IS_020_OR_030)
  1017. pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE030;
  1018. if (CPU_IS_040_OR_060) {
  1019. pgprot_val(vma->vm_page_prot) &= _CACHEMASK040;
  1020. /* Use no-cache mode, serialized */
  1021. pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE_S;
  1022. }
  1023. #endif
  1024. #elif defined(__powerpc__)
  1025. vma->vm_page_prot = phys_mem_access_prot(file, off >> PAGE_SHIFT,
  1026. vma->vm_end - vma->vm_start,
  1027. vma->vm_page_prot);
  1028. #elif defined(__alpha__)
  1029. /* Caching is off in the I/O space quadrant by design. */
  1030. #elif defined(__i386__) || defined(__x86_64__)
  1031. if (boot_cpu_data.x86 > 3)
  1032. pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
  1033. #elif defined(__mips__)
  1034. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1035. #elif defined(__hppa__)
  1036. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  1037. #elif defined(__arm__) || defined(__sh__) || defined(__m32r__)
  1038. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  1039. #elif defined(__ia64__)
  1040. if (efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
  1041. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  1042. else
  1043. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1044. #else
  1045. #warning What do we have to do here??
  1046. #endif
  1047. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  1048. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  1049. return -EAGAIN;
  1050. #endif /* !__sparc_v9__ */
  1051. return 0;
  1052. #endif /* !sparc32 */
  1053. }
  1054. static int
  1055. fb_open(struct inode *inode, struct file *file)
  1056. {
  1057. int fbidx = iminor(inode);
  1058. struct fb_info *info;
  1059. int res = 0;
  1060. if (fbidx >= FB_MAX)
  1061. return -ENODEV;
  1062. #ifdef CONFIG_KMOD
  1063. if (!(info = registered_fb[fbidx]))
  1064. try_to_load(fbidx);
  1065. #endif /* CONFIG_KMOD */
  1066. if (!(info = registered_fb[fbidx]))
  1067. return -ENODEV;
  1068. if (!try_module_get(info->fbops->owner))
  1069. return -ENODEV;
  1070. if (info->fbops->fb_open) {
  1071. res = info->fbops->fb_open(info,1);
  1072. if (res)
  1073. module_put(info->fbops->owner);
  1074. }
  1075. return res;
  1076. }
  1077. static int
  1078. fb_release(struct inode *inode, struct file *file)
  1079. {
  1080. int fbidx = iminor(inode);
  1081. struct fb_info *info;
  1082. lock_kernel();
  1083. info = registered_fb[fbidx];
  1084. if (info->fbops->fb_release)
  1085. info->fbops->fb_release(info,1);
  1086. module_put(info->fbops->owner);
  1087. unlock_kernel();
  1088. return 0;
  1089. }
  1090. static struct file_operations fb_fops = {
  1091. .owner = THIS_MODULE,
  1092. .read = fb_read,
  1093. .write = fb_write,
  1094. .ioctl = fb_ioctl,
  1095. #ifdef CONFIG_COMPAT
  1096. .compat_ioctl = fb_compat_ioctl,
  1097. #endif
  1098. .mmap = fb_mmap,
  1099. .open = fb_open,
  1100. .release = fb_release,
  1101. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
  1102. .get_unmapped_area = get_fb_unmapped_area,
  1103. #endif
  1104. };
  1105. static struct class *fb_class;
  1106. /**
  1107. * register_framebuffer - registers a frame buffer device
  1108. * @fb_info: frame buffer info structure
  1109. *
  1110. * Registers a frame buffer device @fb_info.
  1111. *
  1112. * Returns negative errno on error, or zero for success.
  1113. *
  1114. */
  1115. int
  1116. register_framebuffer(struct fb_info *fb_info)
  1117. {
  1118. int i;
  1119. struct fb_event event;
  1120. struct fb_videomode mode;
  1121. if (num_registered_fb == FB_MAX)
  1122. return -ENXIO;
  1123. num_registered_fb++;
  1124. for (i = 0 ; i < FB_MAX; i++)
  1125. if (!registered_fb[i])
  1126. break;
  1127. fb_info->node = i;
  1128. fb_info->class_device = class_device_create(fb_class, NULL, MKDEV(FB_MAJOR, i),
  1129. fb_info->device, "fb%d", i);
  1130. if (IS_ERR(fb_info->class_device)) {
  1131. /* Not fatal */
  1132. printk(KERN_WARNING "Unable to create class_device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->class_device));
  1133. fb_info->class_device = NULL;
  1134. } else
  1135. fb_init_class_device(fb_info);
  1136. if (fb_info->pixmap.addr == NULL) {
  1137. fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
  1138. if (fb_info->pixmap.addr) {
  1139. fb_info->pixmap.size = FBPIXMAPSIZE;
  1140. fb_info->pixmap.buf_align = 1;
  1141. fb_info->pixmap.scan_align = 1;
  1142. fb_info->pixmap.access_align = 32;
  1143. fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
  1144. }
  1145. }
  1146. fb_info->pixmap.offset = 0;
  1147. if (!fb_info->modelist.prev || !fb_info->modelist.next)
  1148. INIT_LIST_HEAD(&fb_info->modelist);
  1149. fb_var_to_videomode(&mode, &fb_info->var);
  1150. fb_add_videomode(&mode, &fb_info->modelist);
  1151. registered_fb[i] = fb_info;
  1152. devfs_mk_cdev(MKDEV(FB_MAJOR, i),
  1153. S_IFCHR | S_IRUGO | S_IWUGO, "fb/%d", i);
  1154. event.info = fb_info;
  1155. notifier_call_chain(&fb_notifier_list,
  1156. FB_EVENT_FB_REGISTERED, &event);
  1157. return 0;
  1158. }
  1159. /**
  1160. * unregister_framebuffer - releases a frame buffer device
  1161. * @fb_info: frame buffer info structure
  1162. *
  1163. * Unregisters a frame buffer device @fb_info.
  1164. *
  1165. * Returns negative errno on error, or zero for success.
  1166. *
  1167. */
  1168. int
  1169. unregister_framebuffer(struct fb_info *fb_info)
  1170. {
  1171. int i;
  1172. i = fb_info->node;
  1173. if (!registered_fb[i])
  1174. return -EINVAL;
  1175. devfs_remove("fb/%d", i);
  1176. if (fb_info->pixmap.addr && (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
  1177. kfree(fb_info->pixmap.addr);
  1178. fb_destroy_modelist(&fb_info->modelist);
  1179. registered_fb[i]=NULL;
  1180. num_registered_fb--;
  1181. fb_cleanup_class_device(fb_info);
  1182. class_device_destroy(fb_class, MKDEV(FB_MAJOR, i));
  1183. return 0;
  1184. }
  1185. /**
  1186. * fb_register_client - register a client notifier
  1187. * @nb: notifier block to callback on events
  1188. */
  1189. int fb_register_client(struct notifier_block *nb)
  1190. {
  1191. return notifier_chain_register(&fb_notifier_list, nb);
  1192. }
  1193. /**
  1194. * fb_unregister_client - unregister a client notifier
  1195. * @nb: notifier block to callback on events
  1196. */
  1197. int fb_unregister_client(struct notifier_block *nb)
  1198. {
  1199. return notifier_chain_unregister(&fb_notifier_list, nb);
  1200. }
  1201. /**
  1202. * fb_set_suspend - low level driver signals suspend
  1203. * @info: framebuffer affected
  1204. * @state: 0 = resuming, !=0 = suspending
  1205. *
  1206. * This is meant to be used by low level drivers to
  1207. * signal suspend/resume to the core & clients.
  1208. * It must be called with the console semaphore held
  1209. */
  1210. void fb_set_suspend(struct fb_info *info, int state)
  1211. {
  1212. struct fb_event event;
  1213. event.info = info;
  1214. if (state) {
  1215. notifier_call_chain(&fb_notifier_list, FB_EVENT_SUSPEND, &event);
  1216. info->state = FBINFO_STATE_SUSPENDED;
  1217. } else {
  1218. info->state = FBINFO_STATE_RUNNING;
  1219. notifier_call_chain(&fb_notifier_list, FB_EVENT_RESUME, &event);
  1220. }
  1221. }
  1222. /**
  1223. * fbmem_init - init frame buffer subsystem
  1224. *
  1225. * Initialize the frame buffer subsystem.
  1226. *
  1227. * NOTE: This function is _only_ to be called by drivers/char/mem.c.
  1228. *
  1229. */
  1230. static int __init
  1231. fbmem_init(void)
  1232. {
  1233. create_proc_read_entry("fb", 0, NULL, fbmem_read_proc, NULL);
  1234. devfs_mk_dir("fb");
  1235. if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
  1236. printk("unable to get major %d for fb devs\n", FB_MAJOR);
  1237. fb_class = class_create(THIS_MODULE, "graphics");
  1238. if (IS_ERR(fb_class)) {
  1239. printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));
  1240. fb_class = NULL;
  1241. }
  1242. return 0;
  1243. }
  1244. #ifdef MODULE
  1245. module_init(fbmem_init);
  1246. static void __exit
  1247. fbmem_exit(void)
  1248. {
  1249. class_destroy(fb_class);
  1250. unregister_chrdev(FB_MAJOR, "fb");
  1251. }
  1252. module_exit(fbmem_exit);
  1253. MODULE_LICENSE("GPL");
  1254. MODULE_DESCRIPTION("Framebuffer base");
  1255. #else
  1256. subsys_initcall(fbmem_init);
  1257. #endif
  1258. int fb_new_modelist(struct fb_info *info)
  1259. {
  1260. struct fb_event event;
  1261. struct fb_var_screeninfo var = info->var;
  1262. struct list_head *pos, *n;
  1263. struct fb_modelist *modelist;
  1264. struct fb_videomode *m, mode;
  1265. int err = 1;
  1266. list_for_each_safe(pos, n, &info->modelist) {
  1267. modelist = list_entry(pos, struct fb_modelist, list);
  1268. m = &modelist->mode;
  1269. fb_videomode_to_var(&var, m);
  1270. var.activate = FB_ACTIVATE_TEST;
  1271. err = fb_set_var(info, &var);
  1272. fb_var_to_videomode(&mode, &var);
  1273. if (err || !fb_mode_is_equal(m, &mode)) {
  1274. list_del(pos);
  1275. kfree(pos);
  1276. }
  1277. }
  1278. err = 1;
  1279. if (!list_empty(&info->modelist)) {
  1280. event.info = info;
  1281. err = notifier_call_chain(&fb_notifier_list,
  1282. FB_EVENT_NEW_MODELIST,
  1283. &event);
  1284. }
  1285. return err;
  1286. }
  1287. /**
  1288. * fb_con_duit - user<->fbcon passthrough
  1289. * @info: struct fb_info
  1290. * @event: notification event to be passed to fbcon
  1291. * @data: private data
  1292. *
  1293. * DESCRIPTION
  1294. * This function is an fbcon-user event passing channel
  1295. * which bypasses fbdev. This is hopefully temporary
  1296. * until a user interface for fbcon is created
  1297. */
  1298. int fb_con_duit(struct fb_info *info, int event, void *data)
  1299. {
  1300. struct fb_event evnt;
  1301. evnt.info = info;
  1302. evnt.data = data;
  1303. return notifier_call_chain(&fb_notifier_list, event, &evnt);
  1304. }
  1305. EXPORT_SYMBOL(fb_con_duit);
  1306. static char *video_options[FB_MAX];
  1307. static int ofonly;
  1308. extern const char *global_mode_option;
  1309. /**
  1310. * fb_get_options - get kernel boot parameters
  1311. * @name: framebuffer name as it would appear in
  1312. * the boot parameter line
  1313. * (video=<name>:<options>)
  1314. * @option: the option will be stored here
  1315. *
  1316. * NOTE: Needed to maintain backwards compatibility
  1317. */
  1318. int fb_get_options(char *name, char **option)
  1319. {
  1320. char *opt, *options = NULL;
  1321. int opt_len, retval = 0;
  1322. int name_len = strlen(name), i;
  1323. if (name_len && ofonly && strncmp(name, "offb", 4))
  1324. retval = 1;
  1325. if (name_len && !retval) {
  1326. for (i = 0; i < FB_MAX; i++) {
  1327. if (video_options[i] == NULL)
  1328. continue;
  1329. opt_len = strlen(video_options[i]);
  1330. if (!opt_len)
  1331. continue;
  1332. opt = video_options[i];
  1333. if (!strncmp(name, opt, name_len) &&
  1334. opt[name_len] == ':')
  1335. options = opt + name_len + 1;
  1336. }
  1337. }
  1338. if (options && !strncmp(options, "off", 3))
  1339. retval = 1;
  1340. if (option)
  1341. *option = options;
  1342. return retval;
  1343. }
  1344. /**
  1345. * video_setup - process command line options
  1346. * @options: string of options
  1347. *
  1348. * Process command line options for frame buffer subsystem.
  1349. *
  1350. * NOTE: This function is a __setup and __init function.
  1351. * It only stores the options. Drivers have to call
  1352. * fb_get_options() as necessary.
  1353. *
  1354. * Returns zero.
  1355. *
  1356. */
  1357. static int __init video_setup(char *options)
  1358. {
  1359. int i, global = 0;
  1360. if (!options || !*options)
  1361. global = 1;
  1362. if (!global && !strncmp(options, "ofonly", 6)) {
  1363. ofonly = 1;
  1364. global = 1;
  1365. }
  1366. if (!global && !strstr(options, "fb:")) {
  1367. global_mode_option = options;
  1368. global = 1;
  1369. }
  1370. if (!global) {
  1371. for (i = 0; i < FB_MAX; i++) {
  1372. if (video_options[i] == NULL) {
  1373. video_options[i] = options;
  1374. break;
  1375. }
  1376. }
  1377. }
  1378. return 0;
  1379. }
  1380. __setup("video=", video_setup);
  1381. /*
  1382. * Visible symbols for modules
  1383. */
  1384. EXPORT_SYMBOL(register_framebuffer);
  1385. EXPORT_SYMBOL(unregister_framebuffer);
  1386. EXPORT_SYMBOL(num_registered_fb);
  1387. EXPORT_SYMBOL(registered_fb);
  1388. EXPORT_SYMBOL(fb_prepare_logo);
  1389. EXPORT_SYMBOL(fb_show_logo);
  1390. EXPORT_SYMBOL(fb_set_var);
  1391. EXPORT_SYMBOL(fb_blank);
  1392. EXPORT_SYMBOL(fb_pan_display);
  1393. EXPORT_SYMBOL(fb_get_buffer_offset);
  1394. EXPORT_SYMBOL(fb_set_suspend);
  1395. EXPORT_SYMBOL(fb_register_client);
  1396. EXPORT_SYMBOL(fb_unregister_client);
  1397. EXPORT_SYMBOL(fb_get_options);
  1398. EXPORT_SYMBOL(fb_new_modelist);
  1399. MODULE_LICENSE("GPL");