rsrc_iodyn.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. data.mask = align - 1;
  54. data.offset = base & data.mask;
  55. #ifdef CONFIG_PCI
  56. if (s->cb_dev) {
  57. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  58. min, 0, pcmcia_align, &data);
  59. } else
  60. #endif
  61. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  62. 1, pcmcia_align, &data);
  63. if (ret != 0) {
  64. kfree(res);
  65. res = NULL;
  66. }
  67. return res;
  68. }
  69. static int iodyn_find_io(struct pcmcia_socket *s, unsigned int attr,
  70. unsigned int *base, unsigned int num,
  71. unsigned int align, struct resource **parent)
  72. {
  73. int i, ret = 0;
  74. /* Check for an already-allocated window that must conflict with
  75. * what was asked for. It is a hack because it does not catch all
  76. * potential conflicts, just the most obvious ones.
  77. */
  78. for (i = 0; i < MAX_IO_WIN; i++) {
  79. if (!s->io[i].res)
  80. continue;
  81. if (!*base)
  82. continue;
  83. if ((s->io[i].res->start & (align-1)) == *base)
  84. return -EBUSY;
  85. }
  86. for (i = 0; i < MAX_IO_WIN; i++) {
  87. struct resource *res = s->io[i].res;
  88. unsigned int try;
  89. if (res && (res->flags & IORESOURCE_BITS) !=
  90. (attr & IORESOURCE_BITS))
  91. continue;
  92. if (!res) {
  93. if (align == 0)
  94. align = 0x10000;
  95. res = s->io[i].res = __iodyn_find_io_region(s, *base,
  96. num, align);
  97. if (!res)
  98. return -EINVAL;
  99. *base = res->start;
  100. s->io[i].res->flags =
  101. ((res->flags & ~IORESOURCE_BITS) |
  102. (attr & IORESOURCE_BITS));
  103. s->io[i].InUse = num;
  104. *parent = res;
  105. return 0;
  106. }
  107. /* Try to extend top of window */
  108. try = res->end + 1;
  109. if ((*base == 0) || (*base == try)) {
  110. if (adjust_resource(s->io[i].res, res->start,
  111. resource_size(res) + num))
  112. continue;
  113. *base = try;
  114. s->io[i].InUse += num;
  115. *parent = res;
  116. return 0;
  117. }
  118. /* Try to extend bottom of window */
  119. try = res->start - num;
  120. if ((*base == 0) || (*base == try)) {
  121. if (adjust_resource(s->io[i].res,
  122. res->start - num,
  123. resource_size(res) + num))
  124. continue;
  125. *base = try;
  126. s->io[i].InUse += num;
  127. *parent = res;
  128. return 0;
  129. }
  130. }
  131. return -EINVAL;
  132. }
  133. struct pccard_resource_ops pccard_iodyn_ops = {
  134. .validate_mem = NULL,
  135. .find_io = iodyn_find_io,
  136. .find_mem = NULL,
  137. .init = static_init,
  138. .exit = NULL,
  139. };
  140. EXPORT_SYMBOL(pccard_iodyn_ops);