rsrc_mgr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #ifdef CONFIG_PCMCIA_IOCTL
  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. #endif
  79. int pcmcia_validate_mem(struct pcmcia_socket *s)
  80. {
  81. if (s->resource_ops->validate_mem)
  82. return s->resource_ops->validate_mem(s);
  83. /* if there is no callback, we can assume that everything is OK */
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(pcmcia_validate_mem);
  87. int pcmcia_adjust_io_region(struct resource *res, unsigned long r_start,
  88. unsigned long r_end, struct pcmcia_socket *s)
  89. {
  90. if (s->resource_ops->adjust_io_region)
  91. return s->resource_ops->adjust_io_region(res, r_start, r_end, s);
  92. return -ENOMEM;
  93. }
  94. EXPORT_SYMBOL(pcmcia_adjust_io_region);
  95. struct resource *pcmcia_find_io_region(unsigned long base, int num,
  96. unsigned long align, struct pcmcia_socket *s)
  97. {
  98. if (s->resource_ops->find_io)
  99. return s->resource_ops->find_io(base, num, align, s);
  100. return NULL;
  101. }
  102. EXPORT_SYMBOL(pcmcia_find_io_region);
  103. struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  104. int low, struct pcmcia_socket *s)
  105. {
  106. if (s->resource_ops->find_mem)
  107. return s->resource_ops->find_mem(base, num, align, low, s);
  108. return NULL;
  109. }
  110. EXPORT_SYMBOL(pcmcia_find_mem_region);
  111. void release_resource_db(struct pcmcia_socket *s)
  112. {
  113. if (s->resource_ops->exit)
  114. s->resource_ops->exit(s);
  115. }
  116. static int static_init(struct pcmcia_socket *s)
  117. {
  118. unsigned long flags;
  119. /* the good thing about SS_CAP_STATIC_MAP sockets is
  120. * that they don't need a resource database */
  121. spin_lock_irqsave(&s->lock, flags);
  122. s->resource_setup_done = 1;
  123. spin_unlock_irqrestore(&s->lock, flags);
  124. return 0;
  125. }
  126. struct pccard_resource_ops pccard_static_ops = {
  127. .validate_mem = NULL,
  128. .adjust_io_region = NULL,
  129. .find_io = NULL,
  130. .find_mem = NULL,
  131. .adjust_resource = NULL,
  132. .init = static_init,
  133. .exit = NULL,
  134. };
  135. EXPORT_SYMBOL(pccard_static_ops);
  136. #ifdef CONFIG_PCCARD_IODYN
  137. static struct resource *
  138. make_resource(unsigned long b, unsigned long n, int flags, char *name)
  139. {
  140. struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
  141. if (res) {
  142. res->name = name;
  143. res->start = b;
  144. res->end = b + n - 1;
  145. res->flags = flags;
  146. }
  147. return res;
  148. }
  149. struct pcmcia_align_data {
  150. unsigned long mask;
  151. unsigned long offset;
  152. };
  153. static void pcmcia_align(void *align_data, struct resource *res,
  154. unsigned long size, unsigned long align)
  155. {
  156. struct pcmcia_align_data *data = align_data;
  157. unsigned long start;
  158. start = (res->start & ~data->mask) + data->offset;
  159. if (start < res->start)
  160. start += data->mask + 1;
  161. res->start = start;
  162. #ifdef CONFIG_X86
  163. if (res->flags & IORESOURCE_IO) {
  164. if (start & 0x300) {
  165. start = (start + 0x3ff) & ~0x3ff;
  166. res->start = start;
  167. }
  168. }
  169. #endif
  170. #ifdef CONFIG_M68K
  171. if (res->flags & IORESOURCE_IO) {
  172. if ((res->start + size - 1) >= 1024)
  173. res->start = res->end;
  174. }
  175. #endif
  176. }
  177. static int iodyn_adjust_io_region(struct resource *res, unsigned long r_start,
  178. unsigned long r_end, struct pcmcia_socket *s)
  179. {
  180. return adjust_resource(res, r_start, r_end - r_start + 1);
  181. }
  182. static struct resource *iodyn_find_io_region(unsigned long base, int num,
  183. unsigned long align, struct pcmcia_socket *s)
  184. {
  185. struct resource *res = make_resource(0, num, IORESOURCE_IO,
  186. s->dev.bus_id);
  187. struct pcmcia_align_data data;
  188. unsigned long min = base;
  189. int ret;
  190. if (align == 0)
  191. align = 0x10000;
  192. data.mask = align - 1;
  193. data.offset = base & data.mask;
  194. #ifdef CONFIG_PCI
  195. if (s->cb_dev) {
  196. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  197. min, 0, pcmcia_align, &data);
  198. } else
  199. #endif
  200. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  201. 1, pcmcia_align, &data);
  202. if (ret != 0) {
  203. kfree(res);
  204. res = NULL;
  205. }
  206. return res;
  207. }
  208. struct pccard_resource_ops pccard_iodyn_ops = {
  209. .validate_mem = NULL,
  210. .adjust_io_region = iodyn_adjust_io_region,
  211. .find_io = iodyn_find_io_region,
  212. .find_mem = NULL,
  213. .adjust_resource = NULL,
  214. .init = static_init,
  215. .exit = NULL,
  216. };
  217. EXPORT_SYMBOL(pccard_iodyn_ops);
  218. #endif /* CONFIG_PCCARD_IODYN */