rsrc_iodyn.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/cs.h>
  19. #include <pcmcia/cistpl.h>
  20. #include "cs_internal.h"
  21. struct pcmcia_align_data {
  22. unsigned long mask;
  23. unsigned long offset;
  24. };
  25. static resource_size_t pcmcia_align(void *align_data,
  26. const struct resource *res,
  27. resource_size_t size, resource_size_t align)
  28. {
  29. struct pcmcia_align_data *data = align_data;
  30. resource_size_t start;
  31. start = (res->start & ~data->mask) + data->offset;
  32. if (start < res->start)
  33. start += data->mask + 1;
  34. #ifdef CONFIG_X86
  35. if (res->flags & IORESOURCE_IO) {
  36. if (start & 0x300)
  37. start = (start + 0x3ff) & ~0x3ff;
  38. }
  39. #endif
  40. #ifdef CONFIG_M68K
  41. if (res->flags & IORESOURCE_IO) {
  42. if ((res->start + size - 1) >= 1024)
  43. start = res->end;
  44. }
  45. #endif
  46. return start;
  47. }
  48. static struct resource *__iodyn_find_io_region(struct pcmcia_socket *s,
  49. unsigned long base, int num,
  50. unsigned long align)
  51. {
  52. struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO,
  53. dev_name(&s->dev));
  54. struct pcmcia_align_data data;
  55. unsigned long min = base;
  56. int ret;
  57. data.mask = align - 1;
  58. data.offset = base & data.mask;
  59. #ifdef CONFIG_PCI
  60. if (s->cb_dev) {
  61. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  62. min, 0, pcmcia_align, &data);
  63. } else
  64. #endif
  65. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  66. 1, pcmcia_align, &data);
  67. if (ret != 0) {
  68. kfree(res);
  69. res = NULL;
  70. }
  71. return res;
  72. }
  73. static int iodyn_find_io(struct pcmcia_socket *s, unsigned int attr,
  74. unsigned int *base, unsigned int num,
  75. unsigned int align, struct resource **parent)
  76. {
  77. int i, ret = 0;
  78. /* Check for an already-allocated window that must conflict with
  79. * what was asked for. It is a hack because it does not catch all
  80. * potential conflicts, just the most obvious ones.
  81. */
  82. for (i = 0; i < MAX_IO_WIN; i++) {
  83. if (!s->io[i].res)
  84. continue;
  85. if (!*base)
  86. continue;
  87. if ((s->io[i].res->start & (align-1)) == *base)
  88. return -EBUSY;
  89. }
  90. for (i = 0; i < MAX_IO_WIN; i++) {
  91. struct resource *res = s->io[i].res;
  92. unsigned int try;
  93. if (res && (res->flags & IORESOURCE_BITS) !=
  94. (attr & IORESOURCE_BITS))
  95. continue;
  96. if (!res) {
  97. if (align == 0)
  98. align = 0x10000;
  99. res = s->io[i].res = __iodyn_find_io_region(s, *base,
  100. num, align);
  101. if (!res)
  102. return -EINVAL;
  103. *base = res->start;
  104. s->io[i].res->flags =
  105. ((res->flags & ~IORESOURCE_BITS) |
  106. (attr & IORESOURCE_BITS));
  107. s->io[i].InUse = num;
  108. *parent = res;
  109. return 0;
  110. }
  111. /* Try to extend top of window */
  112. try = res->end + 1;
  113. if ((*base == 0) || (*base == try)) {
  114. if (adjust_resource(s->io[i].res, res->start,
  115. res->end - res->start + num + 1))
  116. continue;
  117. *base = try;
  118. s->io[i].InUse += num;
  119. *parent = res;
  120. return 0;
  121. }
  122. /* Try to extend bottom of window */
  123. try = res->start - num;
  124. if ((*base == 0) || (*base == try)) {
  125. if (adjust_resource(s->io[i].res,
  126. res->start - num,
  127. res->end - res->start + num + 1))
  128. continue;
  129. *base = try;
  130. s->io[i].InUse += num;
  131. *parent = res;
  132. return 0;
  133. }
  134. }
  135. return -EINVAL;
  136. }
  137. struct pccard_resource_ops pccard_iodyn_ops = {
  138. .validate_mem = NULL,
  139. .find_io = iodyn_find_io,
  140. .find_mem = NULL,
  141. .init = static_init,
  142. .exit = NULL,
  143. };
  144. EXPORT_SYMBOL(pccard_iodyn_ops);