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