rsrc_mgr.c 3.8 KB

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