rsrc_iodyn.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * rsrc_iodyn.c -- Resource management routines for MEM-static sockets.
  4. *
  5. * The initial developer of the original code is David A. Hinds
  6. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  7. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  8. *
  9. * (C) 1999 David A. Hinds
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <pcmcia/ss.h>
  15. #include <pcmcia/cistpl.h>
  16. #include "cs_internal.h"
  17. struct pcmcia_align_data {
  18. unsigned long mask;
  19. unsigned long offset;
  20. };
  21. static resource_size_t pcmcia_align(void *align_data,
  22. const struct resource *res,
  23. resource_size_t size, resource_size_t align)
  24. {
  25. struct pcmcia_align_data *data = align_data;
  26. resource_size_t start;
  27. start = (res->start & ~data->mask) + data->offset;
  28. if (start < res->start)
  29. start += data->mask + 1;
  30. #ifdef CONFIG_X86
  31. if (res->flags & IORESOURCE_IO) {
  32. if (start & 0x300)
  33. start = (start + 0x3ff) & ~0x3ff;
  34. }
  35. #endif
  36. #ifdef CONFIG_M68K
  37. if (res->flags & IORESOURCE_IO) {
  38. if ((res->start + size - 1) >= 1024)
  39. start = res->end;
  40. }
  41. #endif
  42. return start;
  43. }
  44. static struct resource *__iodyn_find_io_region(struct pcmcia_socket *s,
  45. unsigned long base, int num,
  46. unsigned long align)
  47. {
  48. struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO,
  49. dev_name(&s->dev));
  50. struct pcmcia_align_data data;
  51. unsigned long min = base;
  52. int ret;
  53. if (!res)
  54. return NULL;
  55. data.mask = align - 1;
  56. data.offset = base & data.mask;
  57. #ifdef CONFIG_PCI
  58. if (s->cb_dev) {
  59. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  60. min, 0, pcmcia_align, &data);
  61. } else
  62. #endif
  63. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  64. 1, pcmcia_align, &data);
  65. if (ret != 0) {
  66. kfree(res);
  67. res = NULL;
  68. }
  69. return res;
  70. }
  71. static int iodyn_find_io(struct pcmcia_socket *s, unsigned int attr,
  72. unsigned int *base, unsigned int num,
  73. unsigned int align, struct resource **parent)
  74. {
  75. int i, ret = 0;
  76. /* Check for an already-allocated window that must conflict with
  77. * what was asked for. It is a hack because it does not catch all
  78. * potential conflicts, just the most obvious ones.
  79. */
  80. for (i = 0; i < MAX_IO_WIN; i++) {
  81. if (!s->io[i].res)
  82. continue;
  83. if (!*base)
  84. continue;
  85. if ((s->io[i].res->start & (align-1)) == *base)
  86. return -EBUSY;
  87. }
  88. for (i = 0; i < MAX_IO_WIN; i++) {
  89. struct resource *res = s->io[i].res;
  90. unsigned int try;
  91. if (res && (res->flags & IORESOURCE_BITS) !=
  92. (attr & IORESOURCE_BITS))
  93. continue;
  94. if (!res) {
  95. if (align == 0)
  96. align = 0x10000;
  97. res = s->io[i].res = __iodyn_find_io_region(s, *base,
  98. num, align);
  99. if (!res)
  100. return -EINVAL;
  101. *base = res->start;
  102. s->io[i].res->flags =
  103. ((res->flags & ~IORESOURCE_BITS) |
  104. (attr & IORESOURCE_BITS));
  105. s->io[i].InUse = num;
  106. *parent = res;
  107. return 0;
  108. }
  109. /* Try to extend top of window */
  110. try = res->end + 1;
  111. if ((*base == 0) || (*base == try)) {
  112. if (adjust_resource(s->io[i].res, res->start,
  113. resource_size(res) + num))
  114. continue;
  115. *base = try;
  116. s->io[i].InUse += num;
  117. *parent = res;
  118. return 0;
  119. }
  120. /* Try to extend bottom of window */
  121. try = res->start - num;
  122. if ((*base == 0) || (*base == try)) {
  123. if (adjust_resource(s->io[i].res,
  124. res->start - num,
  125. resource_size(res) + num))
  126. continue;
  127. *base = try;
  128. s->io[i].InUse += num;
  129. *parent = res;
  130. return 0;
  131. }
  132. }
  133. return -EINVAL;
  134. }
  135. struct pccard_resource_ops pccard_iodyn_ops = {
  136. .validate_mem = NULL,
  137. .find_io = iodyn_find_io,
  138. .find_mem = NULL,
  139. .init = static_init,
  140. .exit = NULL,
  141. };
  142. EXPORT_SYMBOL(pccard_iodyn_ops);