xref: /linux/drivers/pcmcia/rsrc_iodyn.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
1 /*
2  * rsrc_iodyn.c -- Resource management routines for MEM-static sockets.
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 
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 
19 #include <pcmcia/ss.h>
20 #include <pcmcia/cs.h>
21 #include <pcmcia/cistpl.h>
22 #include "cs_internal.h"
23 
24 
25 struct pcmcia_align_data {
26 	unsigned long	mask;
27 	unsigned long	offset;
28 };
29 
30 static resource_size_t pcmcia_align(void *align_data,
31 				const struct resource *res,
32 				resource_size_t size, resource_size_t align)
33 {
34 	struct pcmcia_align_data *data = align_data;
35 	resource_size_t start;
36 
37 	start = (res->start & ~data->mask) + data->offset;
38 	if (start < res->start)
39 		start += data->mask + 1;
40 
41 #ifdef CONFIG_X86
42 	if (res->flags & IORESOURCE_IO) {
43 		if (start & 0x300)
44 			start = (start + 0x3ff) & ~0x3ff;
45 	}
46 #endif
47 
48 #ifdef CONFIG_M68K
49 	if (res->flags & IORESOURCE_IO) {
50 		if ((res->start + size - 1) >= 1024)
51 			start = res->end;
52 	}
53 #endif
54 
55 	return start;
56 }
57 
58 
59 static struct resource *__iodyn_find_io_region(struct pcmcia_socket *s,
60 					unsigned long base, int num,
61 					unsigned long align)
62 {
63 	struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO,
64 						dev_name(&s->dev));
65 	struct pcmcia_align_data data;
66 	unsigned long min = base;
67 	int ret;
68 
69 	data.mask = align - 1;
70 	data.offset = base & data.mask;
71 
72 #ifdef CONFIG_PCI
73 	if (s->cb_dev) {
74 		ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
75 					     min, 0, pcmcia_align, &data);
76 	} else
77 #endif
78 		ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
79 					1, pcmcia_align, &data);
80 
81 	if (ret != 0) {
82 		kfree(res);
83 		res = NULL;
84 	}
85 	return res;
86 }
87 
88 static int iodyn_find_io(struct pcmcia_socket *s, unsigned int attr,
89 			unsigned int *base, unsigned int num,
90 			unsigned int align, struct resource **parent)
91 {
92 	int i, ret = 0;
93 
94 	/* Check for an already-allocated window that must conflict with
95 	 * what was asked for.  It is a hack because it does not catch all
96 	 * potential conflicts, just the most obvious ones.
97 	 */
98 	for (i = 0; i < MAX_IO_WIN; i++) {
99 		if (!s->io[i].res)
100 			continue;
101 
102 		if (!*base)
103 			continue;
104 
105 		if ((s->io[i].res->start & (align-1)) == *base)
106 			return -EBUSY;
107 	}
108 
109 	for (i = 0; i < MAX_IO_WIN; i++) {
110 		struct resource *res = s->io[i].res;
111 		unsigned int try;
112 
113 		if (res && (res->flags & IORESOURCE_BITS) !=
114 			(attr & IORESOURCE_BITS))
115 			continue;
116 
117 		if (!res) {
118 			if (align == 0)
119 				align = 0x10000;
120 
121 			res = s->io[i].res = __iodyn_find_io_region(s, *base,
122 								num, align);
123 			if (!res)
124 				return -EINVAL;
125 
126 			*base = res->start;
127 			s->io[i].res->flags =
128 				((res->flags & ~IORESOURCE_BITS) |
129 					(attr & IORESOURCE_BITS));
130 			s->io[i].InUse = num;
131 			*parent = res;
132 			return 0;
133 		}
134 
135 		/* Try to extend top of window */
136 		try = res->end + 1;
137 		if ((*base == 0) || (*base == try)) {
138 			if (adjust_resource(s->io[i].res, res->start,
139 					res->end - res->start + num + 1))
140 				continue;
141 			*base = try;
142 			s->io[i].InUse += num;
143 			*parent = res;
144 			return 0;
145 		}
146 
147 		/* Try to extend bottom of window */
148 		try = res->start - num;
149 		if ((*base == 0) || (*base == try)) {
150 			if (adjust_resource(s->io[i].res,
151 					res->start - num,
152 					res->end - res->start + num + 1))
153 				continue;
154 			*base = try;
155 			s->io[i].InUse += num;
156 			*parent = res;
157 			return 0;
158 		}
159 	}
160 
161 	return -EINVAL;
162 }
163 
164 
165 struct pccard_resource_ops pccard_iodyn_ops = {
166 	.validate_mem = NULL,
167 	.find_io = iodyn_find_io,
168 	.find_mem = NULL,
169 	.init = static_init,
170 	.exit = NULL,
171 };
172 EXPORT_SYMBOL(pccard_iodyn_ops);
173