blob: de13c424101c31bc481cb1522be5de1cb659183c [file] [log] [blame]
Dominik Brodowski1a8d4662005-06-27 16:28:53 -07001/*
2 * PCMCIA 16-bit resource management functions
3 *
4 * The initial developer of the original code is David A. Hinds
5 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
6 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
7 *
8 * Copyright (C) 1999 David A. Hinds
9 * Copyright (C) 2004-2005 Dominik Brodowski
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070017#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/interrupt.h>
20#include <linux/delay.h>
21#include <linux/pci.h>
22#include <linux/device.h>
23
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070024#include <pcmcia/cs_types.h>
25#include <pcmcia/ss.h>
26#include <pcmcia/cs.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070027#include <pcmcia/cistpl.h>
28#include <pcmcia/cisreg.h>
29#include <pcmcia/ds.h>
30
31#include "cs_internal.h"
32#include "ds_internal.h"
33
34
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070035/* Access speed for IO windows */
36static int io_speed = 0;
37module_param(io_speed, int, 0444);
38
39
40#ifdef CONFIG_PCMCIA_PROBE
Al Viro531e5ca2005-08-23 22:47:01 +010041#include <asm/irq.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070042/* mask of IRQs already reserved by other cards, we should avoid using them */
43static u8 pcmcia_used_irq[NR_IRQS];
44#endif
45
46
Dominik Brodowski7d16b652008-08-02 21:02:01 +020047#ifdef CONFIG_PCMCIA_DEBUG
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070048extern int ds_pc_debug;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070049
50#define ds_dbg(skt, lvl, fmt, arg...) do { \
51 if (ds_pc_debug >= lvl) \
Dominik Brodowskiac449d62008-08-02 18:33:56 +020052 dev_printk(KERN_DEBUG, &skt->dev, \
53 "pcmcia_resource: " fmt, \
54 ## arg); \
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070055} while (0)
56#else
Dominik Brodowskiac449d62008-08-02 18:33:56 +020057#define ds_dbg(skt, lvl, fmt, arg...) do { } while (0)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070058#endif
59
60
61
62/** alloc_io_space
63 *
64 * Special stuff for managing IO windows, because they are scarce
65 */
66
Olof Johanssonecb8a842008-02-04 22:27:34 -080067static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
68 unsigned int *base, unsigned int num, u_int lines)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070069{
70 int i;
Olof Johanssonecb8a842008-02-04 22:27:34 -080071 unsigned int try, align;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070072
73 align = (*base) ? (lines ? 1<<lines : 0) : 1;
74 if (align && (align < num)) {
75 if (*base) {
Olof Johanssonecb8a842008-02-04 22:27:34 -080076 ds_dbg(s, 0, "odd IO request: num %#x align %#x\n",
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070077 num, align);
78 align = 0;
79 } else
80 while (align && (align < num)) align <<= 1;
81 }
82 if (*base & ~(align-1)) {
Olof Johanssonecb8a842008-02-04 22:27:34 -080083 ds_dbg(s, 0, "odd IO request: base %#x align %#x\n",
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070084 *base, align);
85 align = 0;
86 }
87 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
88 *base = s->io_offset | (*base & 0x0fff);
89 return 0;
90 }
91 /* Check for an already-allocated window that must conflict with
92 * what was asked for. It is a hack because it does not catch all
93 * potential conflicts, just the most obvious ones.
94 */
95 for (i = 0; i < MAX_IO_WIN; i++)
Kaustav Majumdar4708b5f2006-10-20 14:44:09 -070096 if ((s->io[i].res) && *base &&
Dominik Brodowskic7d00692006-01-15 08:04:43 +010097 ((s->io[i].res->start & (align-1)) == *base))
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070098 return 1;
99 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100100 if (!s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700101 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
102 if (s->io[i].res) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100103 *base = s->io[i].res->start;
104 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
105 s->io[i].InUse = num;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700106 break;
107 } else
108 return 1;
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100109 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700110 continue;
111 /* Try to extend top of window */
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100112 try = s->io[i].res->end + 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700113 if ((*base == 0) || (*base == try))
114 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
115 s->io[i].res->end + num, s) == 0) {
116 *base = try;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700117 s->io[i].InUse += num;
118 break;
119 }
120 /* Try to extend bottom of window */
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100121 try = s->io[i].res->start - num;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700122 if ((*base == 0) || (*base == try))
123 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
124 s->io[i].res->end, s) == 0) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100125 *base = try;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700126 s->io[i].InUse += num;
127 break;
128 }
129 }
130 return (i == MAX_IO_WIN);
131} /* alloc_io_space */
132
133
Olof Johanssonecb8a842008-02-04 22:27:34 -0800134static void release_io_space(struct pcmcia_socket *s, unsigned int base,
135 unsigned int num)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700136{
137 int i;
138
139 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100140 if (!s->io[i].res)
141 continue;
142 if ((s->io[i].res->start <= base) &&
143 (s->io[i].res->end >= base+num-1)) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700144 s->io[i].InUse -= num;
145 /* Free the window if no one else is using it */
146 if (s->io[i].InUse == 0) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700147 release_resource(s->io[i].res);
148 kfree(s->io[i].res);
149 s->io[i].res = NULL;
150 }
151 }
152 }
153} /* release_io_space */
154
155
156/** pccard_access_configuration_register
157 *
158 * Access_configuration_register() reads and writes configuration
159 * registers in attribute memory. Memory window 0 is reserved for
160 * this and the tuple reading services.
161 */
162
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100163int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700164 conf_reg_t *reg)
165{
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100166 struct pcmcia_socket *s;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700167 config_t *c;
168 int addr;
169 u_char val;
170
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100171 if (!p_dev || !p_dev->function_config)
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200172 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700173
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100174 s = p_dev->socket;
175 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700176
177 if (!(c->state & CONFIG_LOCKED))
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200178 return -EACCES;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700179
180 addr = (c->ConfigBase + reg->Offset) >> 1;
181
182 switch (reg->Action) {
183 case CS_READ:
184 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
185 reg->Value = val;
186 break;
187 case CS_WRITE:
188 val = reg->Value;
189 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
190 break;
191 default:
Dominik Brodowski926c5402008-08-03 12:15:11 +0200192 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700193 break;
194 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200195 return 0;
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100196} /* pcmcia_access_configuration_register */
Dominik Brodowski34481392005-06-27 16:28:54 -0700197EXPORT_SYMBOL(pcmcia_access_configuration_register);
198
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700199
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700200/** pcmcia_get_window
201 */
202int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
203 int idx, win_req_t *req)
204{
205 window_t *win;
206 int w;
207
208 if (!s || !(s->state & SOCKET_PRESENT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200209 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700210 for (w = idx; w < MAX_WIN; w++)
211 if (s->state & SOCKET_WIN_REQ(w))
212 break;
213 if (w == MAX_WIN)
Dominik Brodowski635d19b2008-08-03 11:47:29 +0200214 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700215 win = &s->win[w];
216 req->Base = win->ctl.res->start;
217 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
218 req->AccessSpeed = win->ctl.speed;
219 req->Attributes = 0;
220 if (win->ctl.flags & MAP_ATTRIB)
221 req->Attributes |= WIN_MEMORY_TYPE_AM;
222 if (win->ctl.flags & MAP_ACTIVE)
223 req->Attributes |= WIN_ENABLE;
224 if (win->ctl.flags & MAP_16BIT)
225 req->Attributes |= WIN_DATA_WIDTH_16;
226 if (win->ctl.flags & MAP_USE_WAIT)
227 req->Attributes |= WIN_USE_WAIT;
228 *handle = win;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200229 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700230} /* pcmcia_get_window */
231EXPORT_SYMBOL(pcmcia_get_window);
232
233
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700234/** pcmcia_get_mem_page
235 *
236 * Change the card address of an already open memory window.
237 */
238int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
239{
240 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200241 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700242 req->Page = 0;
243 req->CardOffset = win->ctl.card_start;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200244 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700245} /* pcmcia_get_mem_page */
246EXPORT_SYMBOL(pcmcia_get_mem_page);
247
248
249int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
250{
251 struct pcmcia_socket *s;
252 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200253 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700254 s = win->sock;
Dominik Brodowski610e2372008-08-03 11:58:53 +0200255 if (req->Page != 0) {
256 ds_dbg(s, 0, "failure: requested page is zero\n");
257 return -EINVAL;
258 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700259 win->ctl.card_start = req->CardOffset;
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200260 if (s->ops->set_mem_map(s, &win->ctl) != 0) {
261 ds_dbg(s, 0, "failed to set_mem_map\n");
262 return -EIO;
263 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200264 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700265} /* pcmcia_map_mem_page */
266EXPORT_SYMBOL(pcmcia_map_mem_page);
267
268
269/** pcmcia_modify_configuration
270 *
271 * Modify a locked socket configuration
272 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700273int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700274 modconf_t *mod)
275{
276 struct pcmcia_socket *s;
277 config_t *c;
278
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700279 s = p_dev->socket;
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100280 c = p_dev->function_config;
281
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700282 if (!(s->state & SOCKET_PRESENT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200283 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700284 if (!(c->state & CONFIG_LOCKED))
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200285 return -EACCES;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700286
287 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
288 if (mod->Attributes & CONF_ENABLE_IRQ) {
289 c->Attributes |= CONF_ENABLE_IRQ;
290 s->socket.io_irq = s->irq.AssignedIRQ;
291 } else {
292 c->Attributes &= ~CONF_ENABLE_IRQ;
293 s->socket.io_irq = 0;
294 }
295 s->ops->set_socket(s, &s->socket);
296 }
297
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200298 if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
299 ds_dbg(s, 0, "changing Vcc is not allowed at this time\n");
300 return -EINVAL;
301 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700302
303 /* We only allow changing Vpp1 and Vpp2 to the same value */
304 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
305 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
306 if (mod->Vpp1 != mod->Vpp2)
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200307 ds_dbg(s, 0, "Vpp1 and Vpp2 must be the same\n");
308 return -EINVAL;
Dominik Brodowski71ed90d2005-09-09 13:03:27 -0700309 s->socket.Vpp = mod->Vpp1;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200310 if (s->ops->set_socket(s, &s->socket)) {
311 dev_printk(KERN_WARNING, &s->dev,
312 "Unable to set VPP\n");
313 return -EIO;
314 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700315 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200316 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
317 ds_dbg(s, 0, "changing Vcc is not allowed at this time\n");
318 return -EINVAL;
319 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700320
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100321 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
322 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
323 pccard_io_map io_on;
324 int i;
325
326 io_on.speed = io_speed;
327 for (i = 0; i < MAX_IO_WIN; i++) {
328 if (!s->io[i].res)
329 continue;
330 io_off.map = i;
331 io_on.map = i;
332
333 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
334 io_on.start = s->io[i].res->start;
335 io_on.stop = s->io[i].res->end;
336
337 s->ops->set_io_map(s, &io_off);
338 mdelay(40);
339 s->ops->set_io_map(s, &io_on);
340 }
341 }
342
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200343 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700344} /* modify_configuration */
345EXPORT_SYMBOL(pcmcia_modify_configuration);
346
347
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700348int pcmcia_release_configuration(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700349{
350 pccard_io_map io = { 0, 0, 0, 0, 1 };
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700351 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100352 config_t *c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700353 int i;
354
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100355 if (p_dev->_locked) {
356 p_dev->_locked = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700357 if (--(s->lock_count) == 0) {
358 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
359 s->socket.Vpp = 0;
360 s->socket.io_irq = 0;
361 s->ops->set_socket(s, &s->socket);
362 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100363 }
364 if (c->state & CONFIG_LOCKED) {
365 c->state &= ~CONFIG_LOCKED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700366 if (c->state & CONFIG_IO_REQ)
367 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100368 if (!s->io[i].res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700369 continue;
370 s->io[i].Config--;
371 if (s->io[i].Config != 0)
372 continue;
373 io.map = i;
374 s->ops->set_io_map(s, &io);
375 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700376 }
377
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200378 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700379} /* pcmcia_release_configuration */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700380
381
382/** pcmcia_release_io
383 *
384 * Release_io() releases the I/O ranges allocated by a client. This
385 * may be invoked some time after a card ejection has already dumped
386 * the actual socket configuration, so if the client is "stale", we
387 * don't bother checking the port ranges against the current socket
388 * values.
389 */
Adrian Bunkb4c88402006-01-18 23:53:13 -0800390static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700391{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700392 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100393 config_t *c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700394
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100395 if (!p_dev->_io )
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200396 return -EINVAL;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100397
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100398 p_dev->_io = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700399
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100400 if ((c->io.BasePort1 != req->BasePort1) ||
401 (c->io.NumPorts1 != req->NumPorts1) ||
402 (c->io.BasePort2 != req->BasePort2) ||
403 (c->io.NumPorts2 != req->NumPorts2))
Dominik Brodowski926c5402008-08-03 12:15:11 +0200404 return -EINVAL;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100405
406 c->state &= ~CONFIG_IO_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700407
408 release_io_space(s, req->BasePort1, req->NumPorts1);
409 if (req->NumPorts2)
410 release_io_space(s, req->BasePort2, req->NumPorts2);
411
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200412 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700413} /* pcmcia_release_io */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700414
415
Adrian Bunkb4c88402006-01-18 23:53:13 -0800416static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700417{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700418 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100419 config_t *c= p_dev->function_config;
420
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100421 if (!p_dev->_irq)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200422 return -EINVAL;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100423 p_dev->_irq = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700424
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100425 if (c->state & CONFIG_LOCKED)
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200426 return -EACCES;
Dominik Brodowski610e2372008-08-03 11:58:53 +0200427 if (c->irq.Attributes != req->Attributes) {
428 ds_dbg(s, 0, "IRQ attributes must match assigned ones\n");
429 return -EINVAL;
430 }
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200431 if (s->irq.AssignedIRQ != req->AssignedIRQ) {
432 ds_dbg(s, 0, "IRQ must match assigned one\n");
433 return -EINVAL;
434 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100435 if (--s->irq.Config == 0) {
436 c->state &= ~CONFIG_IRQ_REQ;
437 s->irq.AssignedIRQ = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700438 }
439
440 if (req->Attributes & IRQ_HANDLE_PRESENT) {
441 free_irq(req->AssignedIRQ, req->Instance);
442 }
443
444#ifdef CONFIG_PCMCIA_PROBE
445 pcmcia_used_irq[req->AssignedIRQ]--;
446#endif
447
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200448 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700449} /* pcmcia_release_irq */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700450
451
452int pcmcia_release_window(window_handle_t win)
453{
454 struct pcmcia_socket *s;
455
456 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200457 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700458 s = win->sock;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100459 if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200460 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700461
462 /* Shut down memory window */
463 win->ctl.flags &= ~MAP_ACTIVE;
464 s->ops->set_mem_map(s, &win->ctl);
465 s->state &= ~SOCKET_WIN_REQ(win->index);
466
467 /* Release system memory */
468 if (win->ctl.res) {
469 release_resource(win->ctl.res);
470 kfree(win->ctl.res);
471 win->ctl.res = NULL;
472 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100473 win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700474
475 win->magic = 0;
476
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200477 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700478} /* pcmcia_release_window */
479EXPORT_SYMBOL(pcmcia_release_window);
480
481
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700482int pcmcia_request_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700483 config_req_t *req)
484{
485 int i;
486 u_int base;
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700487 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700488 config_t *c;
489 pccard_io_map iomap;
490
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700491 if (!(s->state & SOCKET_PRESENT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200492 return -ENODEV;;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700493
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200494 if (req->IntType & INT_CARDBUS) {
495 ds_dbg(p_dev->socket, 0, "IntType may not be INT_CARDBUS\n");
496 return -EINVAL;
497 }
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100498 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700499 if (c->state & CONFIG_LOCKED)
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200500 return -EACCES;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700501
502 /* Do power control. We don't allow changes in Vcc. */
Dominik Brodowski70294b42006-01-15 12:43:16 +0100503 s->socket.Vpp = req->Vpp;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200504 if (s->ops->set_socket(s, &s->socket)) {
505 dev_printk(KERN_WARNING, &s->dev,
506 "Unable to set socket state\n");
507 return -EINVAL;
508 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700509
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700510 /* Pick memory or I/O card, DMA mode, interrupt */
511 c->IntType = req->IntType;
512 c->Attributes = req->Attributes;
513 if (req->IntType & INT_MEMORY_AND_IO)
514 s->socket.flags |= SS_IOCARD;
515 if (req->IntType & INT_ZOOMED_VIDEO)
516 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
517 if (req->Attributes & CONF_ENABLE_DMA)
518 s->socket.flags |= SS_DMA_MODE;
519 if (req->Attributes & CONF_ENABLE_SPKR)
520 s->socket.flags |= SS_SPKR_ENA;
521 if (req->Attributes & CONF_ENABLE_IRQ)
522 s->socket.io_irq = s->irq.AssignedIRQ;
523 else
524 s->socket.io_irq = 0;
525 s->ops->set_socket(s, &s->socket);
526 s->lock_count++;
527
528 /* Set up CIS configuration registers */
529 base = c->ConfigBase = req->ConfigBase;
Dominik Brodowski1ae9c7d2006-01-10 20:40:40 +0100530 c->CardValues = req->Present;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700531 if (req->Present & PRESENT_COPY) {
532 c->Copy = req->Copy;
533 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
534 }
535 if (req->Present & PRESENT_OPTION) {
536 if (s->functions == 1) {
537 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
538 } else {
539 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
540 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
541 if (req->Present & PRESENT_IOBASE_0)
542 c->Option |= COR_ADDR_DECODE;
543 }
544 if (c->state & CONFIG_IRQ_REQ)
545 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
546 c->Option |= COR_LEVEL_REQ;
547 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
548 mdelay(40);
549 }
550 if (req->Present & PRESENT_STATUS) {
551 c->Status = req->Status;
552 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
553 }
554 if (req->Present & PRESENT_PIN_REPLACE) {
555 c->Pin = req->Pin;
556 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
557 }
558 if (req->Present & PRESENT_EXT_STATUS) {
559 c->ExtStatus = req->ExtStatus;
560 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
561 }
562 if (req->Present & PRESENT_IOBASE_0) {
563 u_char b = c->io.BasePort1 & 0xff;
564 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
565 b = (c->io.BasePort1 >> 8) & 0xff;
566 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
567 }
568 if (req->Present & PRESENT_IOSIZE) {
569 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
570 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
571 }
572
573 /* Configure I/O windows */
574 if (c->state & CONFIG_IO_REQ) {
575 iomap.speed = io_speed;
576 for (i = 0; i < MAX_IO_WIN; i++)
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100577 if (s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700578 iomap.map = i;
579 iomap.flags = MAP_ACTIVE;
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100580 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700581 case IO_DATA_PATH_WIDTH_16:
582 iomap.flags |= MAP_16BIT; break;
583 case IO_DATA_PATH_WIDTH_AUTO:
584 iomap.flags |= MAP_AUTOSZ; break;
585 default:
586 break;
587 }
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100588 iomap.start = s->io[i].res->start;
589 iomap.stop = s->io[i].res->end;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700590 s->ops->set_io_map(s, &iomap);
591 s->io[i].Config++;
592 }
593 }
594
595 c->state |= CONFIG_LOCKED;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100596 p_dev->_locked = 1;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200597 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700598} /* pcmcia_request_configuration */
599EXPORT_SYMBOL(pcmcia_request_configuration);
600
601
602/** pcmcia_request_io
603 *
604 * Request_io() reserves ranges of port addresses for a socket.
605 * I have not implemented range sharing or alias addressing.
606 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700607int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700608{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700609 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700610 config_t *c;
611
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700612 if (!(s->state & SOCKET_PRESENT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200613 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700614
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700615 if (!req)
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200616 return -EINVAL;
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100617 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700618 if (c->state & CONFIG_LOCKED)
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200619 return -EACCES;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200620 if (c->state & CONFIG_IO_REQ) {
621 ds_dbg(s, 0, "IO already configured\n");
622 return -EBUSY;
623 }
Dominik Brodowski610e2372008-08-03 11:58:53 +0200624 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
625 ds_dbg(s, 0, "bad attribute setting for IO region 1\n");
626 return -EINVAL;
627 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700628 if ((req->NumPorts2 > 0) &&
Dominik Brodowski610e2372008-08-03 11:58:53 +0200629 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
630 ds_dbg(s, 0, "bad attribute setting for IO region 2\n");
631 return -EINVAL;
632 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700633
Dominik Brodowskif9580952008-08-03 11:40:19 +0200634 ds_dbg(s, 1, "trying to allocate resource 1\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700635 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
Dominik Brodowskif9580952008-08-03 11:40:19 +0200636 req->NumPorts1, req->IOAddrLines)) {
637 ds_dbg(s, 0, "allocation of resource 1 failed\n");
638 return -EBUSY;
639 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700640
641 if (req->NumPorts2) {
Dominik Brodowskif9580952008-08-03 11:40:19 +0200642 ds_dbg(s, 1, "trying to allocate resource 2\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700643 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
644 req->NumPorts2, req->IOAddrLines)) {
Dominik Brodowskif9580952008-08-03 11:40:19 +0200645 ds_dbg(s, 0, "allocation of resource 2 failed\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700646 release_io_space(s, req->BasePort1, req->NumPorts1);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200647 return -EBUSY;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700648 }
649 }
650
651 c->io = *req;
652 c->state |= CONFIG_IO_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100653 p_dev->_io = 1;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200654 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700655} /* pcmcia_request_io */
656EXPORT_SYMBOL(pcmcia_request_io);
657
658
659/** pcmcia_request_irq
660 *
661 * Request_irq() reserves an irq for this client.
662 *
663 * Also, since Linux only reserves irq's when they are actually
664 * hooked, we don't guarantee that an irq will still be available
665 * when the configuration is locked. Now that I think about it,
666 * there might be a way to fix this using a dummy handler.
667 */
668
669#ifdef CONFIG_PCMCIA_PROBE
David Howells7d12e782006-10-05 14:55:46 +0100670static irqreturn_t test_action(int cpl, void *dev_id)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700671{
672 return IRQ_NONE;
673}
674#endif
675
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700676int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700677{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700678 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700679 config_t *c;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200680 int ret = -EINVAL, irq = 0;
Alan Coxc5331202006-05-16 16:16:44 +0100681 int type;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700682
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700683 if (!(s->state & SOCKET_PRESENT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200684 return -ENODEV;
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100685 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700686 if (c->state & CONFIG_LOCKED)
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200687 return -EACCES;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200688 if (c->state & CONFIG_IRQ_REQ) {
689 ds_dbg(s, 0, "IRQ already configured\n");
690 return -EBUSY;
691 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700692
Alan Coxc5331202006-05-16 16:16:44 +0100693 /* Decide what type of interrupt we are registering */
694 type = 0;
695 if (s->functions > 1) /* All of this ought to be handled higher up */
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700696 type = IRQF_SHARED;
Alan Coxc5331202006-05-16 16:16:44 +0100697 if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700698 type = IRQF_SHARED;
Alan Coxc5331202006-05-16 16:16:44 +0100699
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700700#ifdef CONFIG_PCMCIA_PROBE
Alan Cox635416e2008-06-16 14:35:15 +0200701
702#ifdef IRQ_NOAUTOEN
703 /* if the underlying IRQ infrastructure allows for it, only allocate
704 * the IRQ, but do not enable it
705 */
706 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
707 type |= IRQ_NOAUTOEN;
708#endif /* IRQ_NOAUTOEN */
709
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700710 if (s->irq.AssignedIRQ != 0) {
711 /* If the interrupt is already assigned, it must be the same */
712 irq = s->irq.AssignedIRQ;
713 } else {
714 int try;
715 u32 mask = s->irq_mask;
Dominik Brodowskia1b274f2005-07-28 01:07:27 -0700716 void *data = &p_dev->dev.driver; /* something unique to this device */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700717
718 for (try = 0; try < 64; try++) {
719 irq = try % 32;
720
721 /* marked as available by driver, and not blocked by userspace? */
722 if (!((mask >> irq) & 1))
723 continue;
724
725 /* avoid an IRQ which is already used by a PCMCIA card */
726 if ((try < 32) && pcmcia_used_irq[irq])
727 continue;
728
729 /* register the correct driver, if possible, of check whether
730 * registering a dummy handle works, i.e. if the IRQ isn't
731 * marked as used by the kernel resource management core */
732 ret = request_irq(irq,
733 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
Alan Coxc5331202006-05-16 16:16:44 +0100734 type,
Brice Goglinbd65a682005-09-09 13:03:29 -0700735 p_dev->devname,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700736 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
737 if (!ret) {
738 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
739 free_irq(irq, data);
740 break;
741 }
742 }
743 }
744#endif
Daniel Ritzc181e0e2005-09-09 13:03:25 -0700745 /* only assign PCI irq if no IRQ already assigned */
746 if (ret && !s->irq.AssignedIRQ) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700747 if (!s->pci_irq)
748 return ret;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700749 type = IRQF_SHARED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700750 irq = s->pci_irq;
751 }
752
Alan Coxc5331202006-05-16 16:16:44 +0100753 if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
Dominik Brodowskif9580952008-08-03 11:40:19 +0200754 ret = request_irq(irq, req->Handler, type,
755 p_dev->devname, req->Instance);
756 if (ret)
757 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700758 }
759
Alan Coxc5331202006-05-16 16:16:44 +0100760 /* Make sure the fact the request type was overridden is passed back */
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700761 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
Alan Coxc5331202006-05-16 16:16:44 +0100762 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200763 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
764 "request for exclusive IRQ could not be fulfilled.\n");
765 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
766 "needs updating to supported shared IRQ lines.\n");
Alan Coxc5331202006-05-16 16:16:44 +0100767 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700768 c->irq.Attributes = req->Attributes;
769 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
770 s->irq.Config++;
771
772 c->state |= CONFIG_IRQ_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100773 p_dev->_irq = 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700774
775#ifdef CONFIG_PCMCIA_PROBE
776 pcmcia_used_irq[irq]++;
777#endif
778
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200779 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700780} /* pcmcia_request_irq */
781EXPORT_SYMBOL(pcmcia_request_irq);
782
783
784/** pcmcia_request_window
785 *
786 * Request_window() establishes a mapping between card memory space
787 * and system memory space.
788 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700789int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700790{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700791 struct pcmcia_socket *s = (*p_dev)->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700792 window_t *win;
793 u_long align;
794 int w;
795
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700796 if (!(s->state & SOCKET_PRESENT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200797 return -ENODEV;
Dominik Brodowski610e2372008-08-03 11:58:53 +0200798 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
799 ds_dbg(s, 0, "bad attribute setting for iomem region\n");
800 return -EINVAL;
801 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700802
803 /* Window size defaults to smallest available */
804 if (req->Size == 0)
805 req->Size = s->map_size;
806 align = (((s->features & SS_CAP_MEM_ALIGN) ||
807 (req->Attributes & WIN_STRICT_ALIGN)) ?
808 req->Size : s->map_size);
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200809 if (req->Size & (s->map_size-1)) {
810 ds_dbg(s, 0, "invalid map size\n");
811 return -EINVAL;
812 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700813 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200814 (req->Base & (align-1))) {
815 ds_dbg(s, 0, "invalid base address\n");
816 return -EINVAL;
817 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700818 if (req->Base)
819 align = 0;
820
821 /* Allocate system memory window */
822 for (w = 0; w < MAX_WIN; w++)
823 if (!(s->state & SOCKET_WIN_REQ(w))) break;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200824 if (w == MAX_WIN) {
825 ds_dbg(s, 0, "all windows are used already\n");
826 return -EINVAL;
827 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700828
829 win = &s->win[w];
830 win->magic = WINDOW_MAGIC;
831 win->index = w;
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700832 win->handle = *p_dev;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700833 win->sock = s;
834
835 if (!(s->features & SS_CAP_STATIC_MAP)) {
836 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
837 (req->Attributes & WIN_MAP_BELOW_1MB), s);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200838 if (!win->ctl.res) {
839 ds_dbg(s, 0, "allocating mem region failed\n");
840 return -EINVAL;
841 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700842 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100843 (*p_dev)->_win |= CLIENT_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700844
845 /* Configure the socket controller */
846 win->ctl.map = w+1;
847 win->ctl.flags = 0;
848 win->ctl.speed = req->AccessSpeed;
849 if (req->Attributes & WIN_MEMORY_TYPE)
850 win->ctl.flags |= MAP_ATTRIB;
851 if (req->Attributes & WIN_ENABLE)
852 win->ctl.flags |= MAP_ACTIVE;
853 if (req->Attributes & WIN_DATA_WIDTH_16)
854 win->ctl.flags |= MAP_16BIT;
855 if (req->Attributes & WIN_USE_WAIT)
856 win->ctl.flags |= MAP_USE_WAIT;
857 win->ctl.card_start = 0;
Dominik Brodowski926c5402008-08-03 12:15:11 +0200858 if (s->ops->set_mem_map(s, &win->ctl) != 0) {
859 ds_dbg(s, 0, "failed to set memory mapping\n");
860 return -EIO;
861 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700862 s->state |= SOCKET_WIN_REQ(w);
863
864 /* Return window handle */
865 if (s->features & SS_CAP_STATIC_MAP) {
866 req->Base = win->ctl.static_start;
867 } else {
868 req->Base = win->ctl.res->start;
869 }
870 *wh = win;
871
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200872 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700873} /* pcmcia_request_window */
874EXPORT_SYMBOL(pcmcia_request_window);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100875
876void pcmcia_disable_device(struct pcmcia_device *p_dev) {
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100877 pcmcia_release_configuration(p_dev);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100878 pcmcia_release_io(p_dev, &p_dev->io);
879 pcmcia_release_irq(p_dev, &p_dev->irq);
Dominik Brodowskic1ac0222008-06-14 18:51:06 +0200880 if (p_dev->win)
Dominik Brodowskifd238232006-03-05 10:45:09 +0100881 pcmcia_release_window(p_dev->win);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100882}
883EXPORT_SYMBOL(pcmcia_disable_device);
Dominik Brodowskia804b572008-07-29 08:38:30 +0200884
885
886struct pcmcia_cfg_mem {
887 tuple_t tuple;
888 cisparse_t parse;
889 u8 buf[256];
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200890 cistpl_cftable_entry_t dflt;
Dominik Brodowskia804b572008-07-29 08:38:30 +0200891};
892
893/**
894 * pcmcia_loop_config() - loop over configuration options
895 * @p_dev: the struct pcmcia_device which we need to loop for.
896 * @conf_check: function to call for each configuration option.
897 * It gets passed the struct pcmcia_device, the CIS data
898 * describing the configuration option, and private data
899 * being passed to pcmcia_loop_config()
900 * @priv_data: private data to be passed to the conf_check function.
901 *
902 * pcmcia_loop_config() loops over all configuration options, and calls
903 * the driver-specific conf_check() for each one, checking whether
904 * it is a valid one.
905 */
906int pcmcia_loop_config(struct pcmcia_device *p_dev,
907 int (*conf_check) (struct pcmcia_device *p_dev,
908 cistpl_cftable_entry_t *cfg,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200909 cistpl_cftable_entry_t *dflt,
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200910 unsigned int vcc,
Dominik Brodowskia804b572008-07-29 08:38:30 +0200911 void *priv_data),
912 void *priv_data)
913{
914 struct pcmcia_cfg_mem *cfg_mem;
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200915
Dominik Brodowskia804b572008-07-29 08:38:30 +0200916 tuple_t *tuple;
917 int ret = -ENODEV;
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200918 unsigned int vcc;
Dominik Brodowskia804b572008-07-29 08:38:30 +0200919
920 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
921 if (cfg_mem == NULL)
922 return -ENOMEM;
923
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200924 /* get the current Vcc setting */
925 vcc = p_dev->socket->socket.Vcc;
926
Dominik Brodowskia804b572008-07-29 08:38:30 +0200927 tuple = &cfg_mem->tuple;
928 tuple->TupleData = cfg_mem->buf;
929 tuple->TupleDataMax = 255;
930 tuple->TupleOffset = 0;
931 tuple->DesiredTuple = CISTPL_CFTABLE_ENTRY;
932 tuple->Attributes = 0;
933
934 ret = pcmcia_get_first_tuple(p_dev, tuple);
935 while (!ret) {
Dominik Brodowski498ac182008-08-02 14:59:13 +0200936 cistpl_cftable_entry_t *cfg = &cfg_mem->parse.cftable_entry;
937
Dominik Brodowskia804b572008-07-29 08:38:30 +0200938 if (pcmcia_get_tuple_data(p_dev, tuple))
939 goto next_entry;
940
941 if (pcmcia_parse_tuple(p_dev, tuple, &cfg_mem->parse))
942 goto next_entry;
943
Dominik Brodowski498ac182008-08-02 14:59:13 +0200944 /* default values */
945 p_dev->conf.ConfigIndex = cfg->index;
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200946 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
947 cfg_mem->dflt = *cfg;
Dominik Brodowski498ac182008-08-02 14:59:13 +0200948
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200949 ret = conf_check(p_dev, cfg, &cfg_mem->dflt, vcc, priv_data);
Dominik Brodowskia804b572008-07-29 08:38:30 +0200950 if (!ret)
951 break;
952
953next_entry:
954 ret = pcmcia_get_next_tuple(p_dev, tuple);
955 }
956
957 return ret;
958}
959EXPORT_SYMBOL(pcmcia_loop_config);