rsrc_mgr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/config.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 "cs_internal.h"
  21. #ifdef CONFIG_PCMCIA_PROBE
  22. static int adjust_irq(struct pcmcia_socket *s, adjust_t *adj)
  23. {
  24. int irq;
  25. u32 mask;
  26. irq = adj->resource.irq.IRQ;
  27. if ((irq < 0) || (irq > 15))
  28. return CS_BAD_IRQ;
  29. if (adj->Action != REMOVE_MANAGED_RESOURCE)
  30. return 0;
  31. mask = 1 << irq;
  32. if (!(s->irq_mask & mask))
  33. return 0;
  34. s->irq_mask &= ~mask;
  35. return 0;
  36. }
  37. #else
  38. static inline int adjust_irq(struct pcmcia_socket *s, adjust_t *adj) {
  39. return CS_SUCCESS;
  40. }
  41. #endif
  42. int pcmcia_adjust_resource_info(adjust_t *adj)
  43. {
  44. struct pcmcia_socket *s;
  45. int ret = CS_UNSUPPORTED_FUNCTION;
  46. unsigned long flags;
  47. down_read(&pcmcia_socket_list_rwsem);
  48. list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
  49. if (adj->Resource == RES_IRQ)
  50. ret = adjust_irq(s, adj);
  51. else if (s->resource_ops->adjust_resource) {
  52. /* you can't use the old interface if the new
  53. * one was used before */
  54. spin_lock_irqsave(&s->lock, flags);
  55. if ((s->resource_setup_new) &&
  56. !(s->resource_setup_old)) {
  57. spin_unlock_irqrestore(&s->lock, flags);
  58. continue;
  59. } else if (!(s->resource_setup_old))
  60. s->resource_setup_old = 1;
  61. spin_unlock_irqrestore(&s->lock, flags);
  62. ret = s->resource_ops->adjust_resource(s, adj);
  63. if (!ret) {
  64. /* as there's no way we know this is the
  65. * last call to adjust_resource_info, we
  66. * always need to assume this is the latest
  67. * one... */
  68. spin_lock_irqsave(&s->lock, flags);
  69. s->resource_setup_done = 1;
  70. spin_unlock_irqrestore(&s->lock, flags);
  71. }
  72. }
  73. }
  74. up_read(&pcmcia_socket_list_rwsem);
  75. return (ret);
  76. }
  77. EXPORT_SYMBOL(pcmcia_adjust_resource_info);
  78. int pcmcia_validate_mem(struct pcmcia_socket *s)
  79. {
  80. if (s->resource_ops->validate_mem)
  81. return s->resource_ops->validate_mem(s);
  82. /* if there is no callback, we can assume that everything is OK */
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(pcmcia_validate_mem);
  86. int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
  87. unsigned long r_end, struct pcmcia_socket *s)
  88. {
  89. if (s->resource_ops->adjust_io_region)
  90. return s->resource_ops->adjust_io_region(res, r_start, r_end, s);
  91. return -ENOMEM;
  92. }
  93. EXPORT_SYMBOL(pcmcia_adjust_io_region);
  94. struct resource *pcmcia_find_io_region(unsigned long base, int num,
  95. unsigned long align, struct pcmcia_socket *s)
  96. {
  97. if (s->resource_ops->find_io)
  98. return s->resource_ops->find_io(base, num, align, s);
  99. return NULL;
  100. }
  101. EXPORT_SYMBOL(pcmcia_find_io_region);
  102. struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  103. int low, struct pcmcia_socket *s)
  104. {
  105. if (s->resource_ops->find_mem)
  106. return s->resource_ops->find_mem(base, num, align, low, s);
  107. return NULL;
  108. }
  109. EXPORT_SYMBOL(pcmcia_find_mem_region);
  110. void release_resource_db(struct pcmcia_socket *s)
  111. {
  112. if (s->resource_ops->exit)
  113. s->resource_ops->exit(s);
  114. }
  115. static int static_init(struct pcmcia_socket *s)
  116. {
  117. unsigned long flags;
  118. /* the good thing about SS_CAP_STATIC_MAP sockets is
  119. * that they don't need a resource database */
  120. spin_lock_irqsave(&s->lock, flags);
  121. s->resource_setup_done = 1;
  122. spin_unlock_irqrestore(&s->lock, flags);
  123. return 0;
  124. }
  125. struct pccard_resource_ops pccard_static_ops = {
  126. .validate_mem = NULL,
  127. .adjust_io_region = NULL,
  128. .find_io = NULL,
  129. .find_mem = NULL,
  130. .adjust_resource = NULL,
  131. .init = static_init,
  132. .exit = NULL,
  133. };
  134. EXPORT_SYMBOL(pccard_static_ops);
  135. #ifdef CONFIG_PCCARD_IODYN
  136. static struct resource *
  137. make_resource(unsigned long b, unsigned long n, int flags, char *name)
  138. {
  139. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  140. if (res) {
  141. res->name = name;
  142. res->start = b;
  143. res->end = b + n - 1;
  144. res->flags = flags;
  145. }
  146. return res;
  147. }
  148. struct pcmcia_align_data {
  149. unsigned long mask;
  150. unsigned long offset;
  151. };
  152. static void pcmcia_align(void *align_data, struct resource *res,
  153. unsigned long size, unsigned long align)
  154. {
  155. struct pcmcia_align_data *data = align_data;
  156. unsigned long start;
  157. start = (res->start & ~data->mask) + data->offset;
  158. if (start < res->start)
  159. start += data->mask + 1;
  160. res->start = start;
  161. #ifdef CONFIG_X86
  162. if (res->flags & IORESOURCE_IO) {
  163. if (start & 0x300) {
  164. start = (start + 0x3ff) & ~0x3ff;
  165. res->start = start;
  166. }
  167. }
  168. #endif
  169. #ifdef CONFIG_M68K
  170. if (res->flags & IORESOURCE_IO) {
  171. if ((res->start + size - 1) >= 1024)
  172. res->start = res->end;
  173. }
  174. #endif
  175. }
  176. static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
  177. unsigned long r_end, struct pcmcia_socket *s)
  178. {
  179. return adjust_resource(res, r_start, r_end - r_start + 1);
  180. }
  181. static struct resource *iodyn_find_io_region(unsigned long base, int num,
  182. unsigned long align, struct pcmcia_socket *s)
  183. {
  184. struct resource *res = make_resource(0, num, IORESOURCE_IO,
  185. s->dev.class_id);
  186. struct pcmcia_align_data data;
  187. unsigned long min = base;
  188. int ret;
  189. if (align == 0)
  190. align = 0x10000;
  191. data.mask = align - 1;
  192. data.offset = base & data.mask;
  193. #ifdef CONFIG_PCI
  194. if (s->cb_dev) {
  195. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  196. min, 0, pcmcia_align, &data);
  197. } else
  198. #endif
  199. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  200. 1, pcmcia_align, &data);
  201. if (ret != 0) {
  202. kfree(res);
  203. res = NULL;
  204. }
  205. return res;
  206. }
  207. struct pccard_resource_ops pccard_iodyn_ops = {
  208. .validate_mem = NULL,
  209. .adjust_io_region = iodyn_adjust_io_region,
  210. .find_io = iodyn_find_io_region,
  211. .find_mem = NULL,
  212. .adjust_resource = NULL,
  213. .init = static_init,
  214. .exit = NULL,
  215. };
  216. EXPORT_SYMBOL(pccard_iodyn_ops);
  217. #endif /* CONFIG_PCCARD_IODYN */