rsrc_mgr.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. void release_resource_db(struct pcmcia_socket *s)
  54. {
  55. if (s->resource_ops->exit)
  56. s->resource_ops->exit(s);
  57. }
  58. static int static_init(struct pcmcia_socket *s)
  59. {
  60. unsigned long flags;
  61. /* the good thing about SS_CAP_STATIC_MAP sockets is
  62. * that they don't need a resource database */
  63. spin_lock_irqsave(&s->lock, flags);
  64. s->resource_setup_done = 1;
  65. spin_unlock_irqrestore(&s->lock, flags);
  66. return 0;
  67. }
  68. struct pccard_resource_ops pccard_static_ops = {
  69. .validate_mem = NULL,
  70. .adjust_io_region = NULL,
  71. .find_io = NULL,
  72. .find_mem = NULL,
  73. .add_io = NULL,
  74. .add_mem = NULL,
  75. .init = static_init,
  76. .exit = NULL,
  77. };
  78. EXPORT_SYMBOL(pccard_static_ops);
  79. #ifdef CONFIG_PCCARD_IODYN
  80. static struct resource *
  81. make_resource(unsigned long b, unsigned long n, int flags, char *name)
  82. {
  83. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  84. if (res) {
  85. res->name = name;
  86. res->start = b;
  87. res->end = b + n - 1;
  88. res->flags = flags;
  89. }
  90. return res;
  91. }
  92. struct pcmcia_align_data {
  93. unsigned long mask;
  94. unsigned long offset;
  95. };
  96. static void pcmcia_align(void *align_data, struct resource *res,
  97. unsigned long size, unsigned long align)
  98. {
  99. struct pcmcia_align_data *data = align_data;
  100. unsigned long start;
  101. start = (res->start & ~data->mask) + data->offset;
  102. if (start < res->start)
  103. start += data->mask + 1;
  104. res->start = start;
  105. #ifdef CONFIG_X86
  106. if (res->flags & IORESOURCE_IO) {
  107. if (start & 0x300) {
  108. start = (start + 0x3ff) & ~0x3ff;
  109. res->start = start;
  110. }
  111. }
  112. #endif
  113. #ifdef CONFIG_M68K
  114. if (res->flags & IORESOURCE_IO) {
  115. if ((res->start + size - 1) >= 1024)
  116. res->start = res->end;
  117. }
  118. #endif
  119. }
  120. static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
  121. unsigned long r_end, struct pcmcia_socket *s)
  122. {
  123. return adjust_resource(res, r_start, r_end - r_start + 1);
  124. }
  125. static struct resource *iodyn_find_io_region(unsigned long base, int num,
  126. unsigned long align, struct pcmcia_socket *s)
  127. {
  128. struct resource *res = make_resource(0, num, IORESOURCE_IO,
  129. dev_name(&s->dev));
  130. struct pcmcia_align_data data;
  131. unsigned long min = base;
  132. int ret;
  133. if (align == 0)
  134. align = 0x10000;
  135. data.mask = align - 1;
  136. data.offset = base & data.mask;
  137. #ifdef CONFIG_PCI
  138. if (s->cb_dev) {
  139. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  140. min, 0, pcmcia_align, &data);
  141. } else
  142. #endif
  143. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  144. 1, pcmcia_align, &data);
  145. if (ret != 0) {
  146. kfree(res);
  147. res = NULL;
  148. }
  149. return res;
  150. }
  151. struct pccard_resource_ops pccard_iodyn_ops = {
  152. .validate_mem = NULL,
  153. .adjust_io_region = iodyn_adjust_io_region,
  154. .find_io = iodyn_find_io_region,
  155. .find_mem = NULL,
  156. .add_io = NULL,
  157. .add_mem = NULL,
  158. .init = static_init,
  159. .exit = NULL,
  160. };
  161. EXPORT_SYMBOL(pccard_iodyn_ops);
  162. #endif /* CONFIG_PCCARD_IODYN */