rsrc_iodyn.c 3.8 KB

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