rsrc_mgr.c 3.2 KB

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