rsrc_mgr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * rsrc_mgr.c -- Resource management routines and/or wrappers
  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/module.h>
  15. #include <linux/kernel.h>
  16. #include <pcmcia/cs_types.h>
  17. #include <pcmcia/ss.h>
  18. #include <pcmcia/cs.h>
  19. #include "cs_internal.h"
  20. int pcmcia_validate_mem(struct pcmcia_socket *s)
  21. {
  22. if (s->resource_ops->validate_mem)
  23. return s->resource_ops->validate_mem(s);
  24. /* if there is no callback, we can assume that everything is OK */
  25. return 0;
  26. }
  27. EXPORT_SYMBOL(pcmcia_validate_mem);
  28. int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
  29. unsigned long r_end, struct pcmcia_socket *s)
  30. {
  31. if (s->resource_ops->adjust_io_region)
  32. return s->resource_ops->adjust_io_region(res, r_start, r_end, s);
  33. return -ENOMEM;
  34. }
  35. EXPORT_SYMBOL(pcmcia_adjust_io_region);
  36. struct resource *pcmcia_find_io_region(unsigned long base, int num,
  37. unsigned long align, struct pcmcia_socket *s)
  38. {
  39. if (s->resource_ops->find_io)
  40. return s->resource_ops->find_io(base, num, align, s);
  41. return NULL;
  42. }
  43. EXPORT_SYMBOL(pcmcia_find_io_region);
  44. struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  45. int low, struct pcmcia_socket *s)
  46. {
  47. if (s->resource_ops->find_mem)
  48. return s->resource_ops->find_mem(base, num, align, low, s);
  49. return NULL;
  50. }
  51. EXPORT_SYMBOL(pcmcia_find_mem_region);
  52. void release_resource_db(struct pcmcia_socket *s)
  53. {
  54. if (s->resource_ops->exit)
  55. s->resource_ops->exit(s);
  56. }
  57. static int static_init(struct pcmcia_socket *s)
  58. {
  59. unsigned long flags;
  60. /* the good thing about SS_CAP_STATIC_MAP sockets is
  61. * that they don't need a resource database */
  62. spin_lock_irqsave(&s->lock, flags);
  63. s->resource_setup_done = 1;
  64. spin_unlock_irqrestore(&s->lock, flags);
  65. return 0;
  66. }
  67. struct pccard_resource_ops pccard_static_ops = {
  68. .validate_mem = NULL,
  69. .adjust_io_region = NULL,
  70. .find_io = NULL,
  71. .find_mem = NULL,
  72. .add_io = NULL,
  73. .add_mem = NULL,
  74. .init = static_init,
  75. .exit = NULL,
  76. };
  77. EXPORT_SYMBOL(pccard_static_ops);
  78. #ifdef CONFIG_PCCARD_IODYN
  79. static struct resource *
  80. make_resource(unsigned long b, unsigned long n, int flags, char *name)
  81. {
  82. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  83. if (res) {
  84. res->name = name;
  85. res->start = b;
  86. res->end = b + n - 1;
  87. res->flags = flags;
  88. }
  89. return res;
  90. }
  91. struct pcmcia_align_data {
  92. unsigned long mask;
  93. unsigned long offset;
  94. };
  95. static void pcmcia_align(void *align_data, struct resource *res,
  96. unsigned long size, unsigned long align)
  97. {
  98. struct pcmcia_align_data *data = align_data;
  99. unsigned long start;
  100. start = (res->start & ~data->mask) + data->offset;
  101. if (start < res->start)
  102. start += data->mask + 1;
  103. res->start = start;
  104. #ifdef CONFIG_X86
  105. if (res->flags & IORESOURCE_IO) {
  106. if (start & 0x300) {
  107. start = (start + 0x3ff) & ~0x3ff;
  108. res->start = start;
  109. }
  110. }
  111. #endif
  112. #ifdef CONFIG_M68K
  113. if (res->flags & IORESOURCE_IO) {
  114. if ((res->start + size - 1) >= 1024)
  115. res->start = res->end;
  116. }
  117. #endif
  118. }
  119. static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
  120. unsigned long r_end, struct pcmcia_socket *s)
  121. {
  122. return adjust_resource(res, r_start, r_end - r_start + 1);
  123. }
  124. static struct resource *iodyn_find_io_region(unsigned long base, int num,
  125. unsigned long align, struct pcmcia_socket *s)
  126. {
  127. struct resource *res = make_resource(0, num, IORESOURCE_IO,
  128. s->dev.bus_id);
  129. struct pcmcia_align_data data;
  130. unsigned long min = base;
  131. int ret;
  132. if (align == 0)
  133. align = 0x10000;
  134. data.mask = align - 1;
  135. data.offset = base & data.mask;
  136. #ifdef CONFIG_PCI
  137. if (s->cb_dev) {
  138. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  139. min, 0, pcmcia_align, &data);
  140. } else
  141. #endif
  142. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  143. 1, pcmcia_align, &data);
  144. if (ret != 0) {
  145. kfree(res);
  146. res = NULL;
  147. }
  148. return res;
  149. }
  150. struct pccard_resource_ops pccard_iodyn_ops = {
  151. .validate_mem = NULL,
  152. .adjust_io_region = iodyn_adjust_io_region,
  153. .find_io = iodyn_find_io_region,
  154. .find_mem = NULL,
  155. .add_io = NULL,
  156. .add_mem = NULL,
  157. .init = static_init,
  158. .exit = NULL,
  159. };
  160. EXPORT_SYMBOL(pccard_iodyn_ops);
  161. #endif /* CONFIG_PCCARD_IODYN */