screen_info_generic.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/ioport.h>
  4. #include <linux/screen_info.h>
  5. #include <linux/string.h>
  6. static void resource_init_named(struct resource *r,
  7. resource_size_t start, resource_size_t size,
  8. const char *name, unsigned int flags)
  9. {
  10. memset(r, 0, sizeof(*r));
  11. r->start = start;
  12. r->end = start + size - 1;
  13. r->name = name;
  14. r->flags = flags;
  15. }
  16. static void resource_init_io_named(struct resource *r,
  17. resource_size_t start, resource_size_t size,
  18. const char *name)
  19. {
  20. resource_init_named(r, start, size, name, IORESOURCE_IO);
  21. }
  22. static void resource_init_mem_named(struct resource *r,
  23. resource_size_t start, resource_size_t size,
  24. const char *name)
  25. {
  26. resource_init_named(r, start, size, name, IORESOURCE_MEM);
  27. }
  28. static inline bool __screen_info_has_ega_gfx(unsigned int mode)
  29. {
  30. switch (mode) {
  31. case 0x0d: /* 320x200-4 */
  32. case 0x0e: /* 640x200-4 */
  33. case 0x0f: /* 640x350-1 */
  34. case 0x10: /* 640x350-4 */
  35. return true;
  36. default:
  37. return false;
  38. }
  39. }
  40. static inline bool __screen_info_has_vga_gfx(unsigned int mode)
  41. {
  42. switch (mode) {
  43. case 0x10: /* 640x480-1 */
  44. case 0x12: /* 640x480-4 */
  45. case 0x13: /* 320-200-8 */
  46. case 0x6a: /* 800x600-4 (VESA) */
  47. return true;
  48. default:
  49. return __screen_info_has_ega_gfx(mode);
  50. }
  51. }
  52. /**
  53. * screen_info_resources() - Get resources from screen_info structure
  54. * @si: the screen_info
  55. * @r: pointer to an array of resource structures
  56. * @num: number of elements in @r:
  57. *
  58. * Returns:
  59. * The number of resources stored in @r on success, or a negative errno code otherwise.
  60. *
  61. * A call to screen_info_resources() returns the resources consumed by the
  62. * screen_info's device or framebuffer. The result is stored in the caller-supplied
  63. * array @r with up to @num elements. The function returns the number of
  64. * initialized elements.
  65. */
  66. ssize_t screen_info_resources(const struct screen_info *si, struct resource *r, size_t num)
  67. {
  68. struct resource *pos = r;
  69. unsigned int type = screen_info_video_type(si);
  70. u64 base, size;
  71. switch (type) {
  72. case VIDEO_TYPE_MDA:
  73. if (num > 0)
  74. resource_init_io_named(pos++, 0x3b0, 12, "mda");
  75. if (num > 1)
  76. resource_init_io_named(pos++, 0x3bf, 0x01, "mda");
  77. if (num > 2)
  78. resource_init_mem_named(pos++, 0xb0000, 0x2000, "mda");
  79. break;
  80. case VIDEO_TYPE_CGA:
  81. if (num > 0)
  82. resource_init_io_named(pos++, 0x3d4, 0x02, "cga");
  83. if (num > 1)
  84. resource_init_mem_named(pos++, 0xb8000, 0x2000, "cga");
  85. break;
  86. case VIDEO_TYPE_EGAM:
  87. if (num > 0)
  88. resource_init_io_named(pos++, 0x3bf, 0x10, "ega");
  89. if (num > 1)
  90. resource_init_mem_named(pos++, 0xb0000, 0x8000, "ega");
  91. break;
  92. case VIDEO_TYPE_EGAC:
  93. if (num > 0)
  94. resource_init_io_named(pos++, 0x3c0, 0x20, "ega");
  95. if (num > 1) {
  96. if (__screen_info_has_ega_gfx(si->orig_video_mode))
  97. resource_init_mem_named(pos++, 0xa0000, 0x10000, "ega");
  98. else
  99. resource_init_mem_named(pos++, 0xb8000, 0x8000, "ega");
  100. }
  101. break;
  102. case VIDEO_TYPE_VGAC:
  103. if (num > 0)
  104. resource_init_io_named(pos++, 0x3c0, 0x20, "vga+");
  105. if (num > 1) {
  106. if (__screen_info_has_vga_gfx(si->orig_video_mode))
  107. resource_init_mem_named(pos++, 0xa0000, 0x10000, "vga+");
  108. else
  109. resource_init_mem_named(pos++, 0xb8000, 0x8000, "vga+");
  110. }
  111. break;
  112. case VIDEO_TYPE_VLFB:
  113. case VIDEO_TYPE_EFI:
  114. base = __screen_info_lfb_base(si);
  115. if (!base)
  116. break;
  117. size = __screen_info_lfb_size(si, type);
  118. if (!size)
  119. break;
  120. if (num > 0)
  121. resource_init_mem_named(pos++, base, size, "lfb");
  122. break;
  123. case VIDEO_TYPE_PICA_S3:
  124. case VIDEO_TYPE_MIPS_G364:
  125. case VIDEO_TYPE_SGI:
  126. case VIDEO_TYPE_TGAC:
  127. case VIDEO_TYPE_SUN:
  128. case VIDEO_TYPE_SUNPCI:
  129. case VIDEO_TYPE_PMAC:
  130. default:
  131. /* not supported */
  132. return -EINVAL;
  133. }
  134. return pos - r;
  135. }
  136. EXPORT_SYMBOL(screen_info_resources);