rsrc_mgr.c 3.3 KB

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