rsrc_mgr.c 4.4 KB

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