cb6b5da3f29a42380a3627e5182f3eb9bcb4659e
[linux-2.6.git] / drivers / pcmcia / pcmcia_resource.c
1 /*
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
17 #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
24 #include <pcmcia/cs_types.h>
25 #include <pcmcia/ss.h>
26 #include <pcmcia/cs.h>
27 #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
35 /* Access speed for IO windows */
36 static int io_speed = 0;
37 module_param(io_speed, int, 0444);
38
39
40 #ifdef CONFIG_PCMCIA_PROBE
41 #include <asm/irq.h>
42 /* mask of IRQs already reserved by other cards, we should avoid using them */
43 static u8 pcmcia_used_irq[NR_IRQS];
44 #endif
45
46
47 #ifdef DEBUG
48 extern int ds_pc_debug;
49
50 #define ds_dbg(skt, lvl, fmt, arg...) do {                      \
51         if (ds_pc_debug >= lvl)                                 \
52                 printk(KERN_DEBUG "pcmcia_resource: %s: " fmt,  \
53                         cs_socket_name(skt) , ## arg);          \
54 } while (0)
55 #else
56 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
57 #endif
58
59
60
61 /** alloc_io_space
62  *
63  * Special stuff for managing IO windows, because they are scarce
64  */
65
66 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
67                           unsigned int *base, unsigned int num, u_int lines)
68 {
69         int i;
70         unsigned int try, align;
71
72         align = (*base) ? (lines ? 1<<lines : 0) : 1;
73         if (align && (align < num)) {
74                 if (*base) {
75                         ds_dbg(s, 0, "odd IO request: num %#x align %#x\n",
76                                num, align);
77                         align = 0;
78                 } else
79                         while (align && (align < num)) align <<= 1;
80         }
81         if (*base & ~(align-1)) {
82                 ds_dbg(s, 0, "odd IO request: base %#x align %#x\n",
83                        *base, align);
84                 align = 0;
85         }
86         if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
87                 *base = s->io_offset | (*base & 0x0fff);
88                 return 0;
89         }
90         /* Check for an already-allocated window that must conflict with
91          * what was asked for.  It is a hack because it does not catch all
92          * potential conflicts, just the most obvious ones.
93          */
94         for (i = 0; i < MAX_IO_WIN; i++)
95                 if ((s->io[i].res) && *base &&
96                     ((s->io[i].res->start & (align-1)) == *base))
97                         return 1;
98         for (i = 0; i < MAX_IO_WIN; i++) {
99                 if (!s->io[i].res) {
100                         s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
101                         if (s->io[i].res) {
102                                 *base = s->io[i].res->start;
103                                 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
104                                 s->io[i].InUse = num;
105                                 break;
106                         } else
107                                 return 1;
108                 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
109                         continue;
110                 /* Try to extend top of window */
111                 try = s->io[i].res->end + 1;
112                 if ((*base == 0) || (*base == try))
113                         if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
114                                                     s->io[i].res->end + num, s) == 0) {
115                                 *base = try;
116                                 s->io[i].InUse += num;
117                                 break;
118                         }
119                 /* Try to extend bottom of window */
120                 try = s->io[i].res->start - num;
121                 if ((*base == 0) || (*base == try))
122                         if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
123                                                     s->io[i].res->end, s) == 0) {
124                                 *base = try;
125                                 s->io[i].InUse += num;
126                                 break;
127                         }
128         }
129         return (i == MAX_IO_WIN);
130 } /* alloc_io_space */
131
132
133 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
134                              unsigned int num)
135 {
136         int i;
137
138         for (i = 0; i < MAX_IO_WIN; i++) {
139                 if (!s->io[i].res)
140                         continue;
141                 if ((s->io[i].res->start <= base) &&
142                     (s->io[i].res->end >= base+num-1)) {
143                         s->io[i].InUse -= num;
144                         /* Free the window if no one else is using it */
145                         if (s->io[i].InUse == 0) {
146                                 release_resource(s->io[i].res);
147                                 kfree(s->io[i].res);
148                                 s->io[i].res = NULL;
149                         }
150                 }
151         }
152 } /* release_io_space */
153
154
155 /** pccard_access_configuration_register
156  *
157  * Access_configuration_register() reads and writes configuration
158  * registers in attribute memory.  Memory window 0 is reserved for
159  * this and the tuple reading services.
160  */
161
162 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
163                                          conf_reg_t *reg)
164 {
165         struct pcmcia_socket *s;
166         config_t *c;
167         int addr;
168         u_char val;
169
170         if (!p_dev || !p_dev->function_config)
171                 return CS_NO_CARD;
172
173         s = p_dev->socket;
174         c = p_dev->function_config;
175
176         if (!(c->state & CONFIG_LOCKED))
177                 return CS_CONFIGURATION_LOCKED;
178
179         addr = (c->ConfigBase + reg->Offset) >> 1;
180
181         switch (reg->Action) {
182         case CS_READ:
183                 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
184                 reg->Value = val;
185                 break;
186         case CS_WRITE:
187                 val = reg->Value;
188                 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
189                 break;
190         default:
191                 return CS_BAD_ARGS;
192                 break;
193         }
194         return CS_SUCCESS;
195 } /* pcmcia_access_configuration_register */
196 EXPORT_SYMBOL(pcmcia_access_configuration_register);
197
198
199 int pccard_get_configuration_info(struct pcmcia_socket *s,
200                                   struct pcmcia_device *p_dev,
201                                   config_info_t *config)
202 {
203         config_t *c;
204
205         if (!(s->state & SOCKET_PRESENT))
206                 return CS_NO_CARD;
207
208
209 #ifdef CONFIG_CARDBUS
210         if (s->state & SOCKET_CARDBUS) {
211                 memset(config, 0, sizeof(config_info_t));
212                 config->Vcc = s->socket.Vcc;
213                 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
214                 config->Option = s->cb_dev->subordinate->number;
215                 if (s->state & SOCKET_CARDBUS_CONFIG) {
216                         config->Attributes = CONF_VALID_CLIENT;
217                         config->IntType = INT_CARDBUS;
218                         config->AssignedIRQ = s->irq.AssignedIRQ;
219                         if (config->AssignedIRQ)
220                                 config->Attributes |= CONF_ENABLE_IRQ;
221                         if (s->io[0].res) {
222                                 config->BasePort1 = s->io[0].res->start;
223                                 config->NumPorts1 = s->io[0].res->end - config->BasePort1 + 1;
224                         }
225                 }
226                 return CS_SUCCESS;
227         }
228 #endif
229
230         if (p_dev) {
231                 c = p_dev->function_config;
232                 config->Function = p_dev->func;
233         } else {
234                 c = NULL;
235                 config->Function = 0;
236         }
237
238         if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
239                 config->Attributes = 0;
240                 config->Vcc = s->socket.Vcc;
241                 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
242                 return CS_SUCCESS;
243         }
244
245         config->Attributes = c->Attributes | CONF_VALID_CLIENT;
246         config->Vcc = s->socket.Vcc;
247         config->Vpp1 = config->Vpp2 = s->socket.Vpp;
248         config->IntType = c->IntType;
249         config->ConfigBase = c->ConfigBase;
250         config->Status = c->Status;
251         config->Pin = c->Pin;
252         config->Copy = c->Copy;
253         config->Option = c->Option;
254         config->ExtStatus = c->ExtStatus;
255         config->Present = config->CardValues = c->CardValues;
256         config->IRQAttributes = c->irq.Attributes;
257         config->AssignedIRQ = s->irq.AssignedIRQ;
258         config->BasePort1 = c->io.BasePort1;
259         config->NumPorts1 = c->io.NumPorts1;
260         config->Attributes1 = c->io.Attributes1;
261         config->BasePort2 = c->io.BasePort2;
262         config->NumPorts2 = c->io.NumPorts2;
263         config->Attributes2 = c->io.Attributes2;
264         config->IOAddrLines = c->io.IOAddrLines;
265
266         return CS_SUCCESS;
267 } /* pccard_get_configuration_info */
268
269 int pcmcia_get_configuration_info(struct pcmcia_device *p_dev,
270                                   config_info_t *config)
271 {
272         return pccard_get_configuration_info(p_dev->socket, p_dev,
273                                              config);
274 }
275 EXPORT_SYMBOL(pcmcia_get_configuration_info);
276
277
278 /** pcmcia_get_window
279  */
280 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
281                       int idx, win_req_t *req)
282 {
283         window_t *win;
284         int w;
285
286         if (!s || !(s->state & SOCKET_PRESENT))
287                 return CS_NO_CARD;
288         for (w = idx; w < MAX_WIN; w++)
289                 if (s->state & SOCKET_WIN_REQ(w))
290                         break;
291         if (w == MAX_WIN)
292                 return CS_NO_MORE_ITEMS;
293         win = &s->win[w];
294         req->Base = win->ctl.res->start;
295         req->Size = win->ctl.res->end - win->ctl.res->start + 1;
296         req->AccessSpeed = win->ctl.speed;
297         req->Attributes = 0;
298         if (win->ctl.flags & MAP_ATTRIB)
299                 req->Attributes |= WIN_MEMORY_TYPE_AM;
300         if (win->ctl.flags & MAP_ACTIVE)
301                 req->Attributes |= WIN_ENABLE;
302         if (win->ctl.flags & MAP_16BIT)
303                 req->Attributes |= WIN_DATA_WIDTH_16;
304         if (win->ctl.flags & MAP_USE_WAIT)
305                 req->Attributes |= WIN_USE_WAIT;
306         *handle = win;
307         return CS_SUCCESS;
308 } /* pcmcia_get_window */
309 EXPORT_SYMBOL(pcmcia_get_window);
310
311
312 /** pccard_get_status
313  *
314  * Get the current socket state bits.  We don't support the latched
315  * SocketState yet: I haven't seen any point for it.
316  */
317
318 int pccard_get_status(struct pcmcia_socket *s, struct pcmcia_device *p_dev,
319                       cs_status_t *status)
320 {
321         config_t *c;
322         int val;
323
324         s->ops->get_status(s, &val);
325         status->CardState = status->SocketState = 0;
326         status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
327         status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
328         status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
329         status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
330         if (s->state & SOCKET_SUSPEND)
331                 status->CardState |= CS_EVENT_PM_SUSPEND;
332         if (!(s->state & SOCKET_PRESENT))
333                 return CS_NO_CARD;
334
335         c = (p_dev) ? p_dev->function_config : NULL;
336
337         if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
338             (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
339                 u_char reg;
340                 if (c->CardValues & PRESENT_PIN_REPLACE) {
341                         pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, &reg);
342                         status->CardState |=
343                                 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
344                         status->CardState |=
345                                 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
346                         status->CardState |=
347                                 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
348                         status->CardState |=
349                                 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
350                 } else {
351                         /* No PRR?  Then assume we're always ready */
352                         status->CardState |= CS_EVENT_READY_CHANGE;
353                 }
354                 if (c->CardValues & PRESENT_EXT_STATUS) {
355                         pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, &reg);
356                         status->CardState |=
357                                 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
358                 }
359                 return CS_SUCCESS;
360         }
361         status->CardState |=
362                 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
363         status->CardState |=
364                 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
365         status->CardState |=
366                 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
367         status->CardState |=
368                 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
369         return CS_SUCCESS;
370 } /* pccard_get_status */
371
372 int pcmcia_get_status(struct pcmcia_device *p_dev, cs_status_t *status)
373 {
374         return pccard_get_status(p_dev->socket, p_dev, status);
375 }
376 EXPORT_SYMBOL(pcmcia_get_status);
377
378
379
380 /** pcmcia_get_mem_page
381  *
382  * Change the card address of an already open memory window.
383  */
384 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
385 {
386         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
387                 return CS_BAD_HANDLE;
388         req->Page = 0;
389         req->CardOffset = win->ctl.card_start;
390         return CS_SUCCESS;
391 } /* pcmcia_get_mem_page */
392 EXPORT_SYMBOL(pcmcia_get_mem_page);
393
394
395 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
396 {
397         struct pcmcia_socket *s;
398         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
399                 return CS_BAD_HANDLE;
400         if (req->Page != 0)
401                 return CS_BAD_PAGE;
402         s = win->sock;
403         win->ctl.card_start = req->CardOffset;
404         if (s->ops->set_mem_map(s, &win->ctl) != 0)
405                 return CS_BAD_OFFSET;
406         return CS_SUCCESS;
407 } /* pcmcia_map_mem_page */
408 EXPORT_SYMBOL(pcmcia_map_mem_page);
409
410
411 /** pcmcia_modify_configuration
412  *
413  * Modify a locked socket configuration
414  */
415 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
416                                 modconf_t *mod)
417 {
418         struct pcmcia_socket *s;
419         config_t *c;
420
421         s = p_dev->socket;
422         c = p_dev->function_config;
423
424         if (!(s->state & SOCKET_PRESENT))
425                 return CS_NO_CARD;
426         if (!(c->state & CONFIG_LOCKED))
427                 return CS_CONFIGURATION_LOCKED;
428
429         if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
430                 if (mod->Attributes & CONF_ENABLE_IRQ) {
431                         c->Attributes |= CONF_ENABLE_IRQ;
432                         s->socket.io_irq = s->irq.AssignedIRQ;
433                 } else {
434                         c->Attributes &= ~CONF_ENABLE_IRQ;
435                         s->socket.io_irq = 0;
436                 }
437                 s->ops->set_socket(s, &s->socket);
438         }
439
440         if (mod->Attributes & CONF_VCC_CHANGE_VALID)
441                 return CS_BAD_VCC;
442
443         /* We only allow changing Vpp1 and Vpp2 to the same value */
444         if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
445             (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
446                 if (mod->Vpp1 != mod->Vpp2)
447                         return CS_BAD_VPP;
448                 s->socket.Vpp = mod->Vpp1;
449                 if (s->ops->set_socket(s, &s->socket))
450                         return CS_BAD_VPP;
451         } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
452                    (mod->Attributes & CONF_VPP2_CHANGE_VALID))
453                 return CS_BAD_VPP;
454
455         if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
456                 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
457                 pccard_io_map io_on;
458                 int i;
459
460                 io_on.speed = io_speed;
461                 for (i = 0; i < MAX_IO_WIN; i++) {
462                         if (!s->io[i].res)
463                                 continue;
464                         io_off.map = i;
465                         io_on.map = i;
466
467                         io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
468                         io_on.start = s->io[i].res->start;
469                         io_on.stop = s->io[i].res->end;
470
471                         s->ops->set_io_map(s, &io_off);
472                         mdelay(40);
473                         s->ops->set_io_map(s, &io_on);
474                 }
475         }
476
477         return CS_SUCCESS;
478 } /* modify_configuration */
479 EXPORT_SYMBOL(pcmcia_modify_configuration);
480
481
482 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
483 {
484         pccard_io_map io = { 0, 0, 0, 0, 1 };
485         struct pcmcia_socket *s = p_dev->socket;
486         config_t *c = p_dev->function_config;
487         int i;
488
489         if (p_dev->_locked) {
490                 p_dev->_locked = 0;
491                 if (--(s->lock_count) == 0) {
492                         s->socket.flags = SS_OUTPUT_ENA;   /* Is this correct? */
493                         s->socket.Vpp = 0;
494                         s->socket.io_irq = 0;
495                         s->ops->set_socket(s, &s->socket);
496                 }
497         }
498         if (c->state & CONFIG_LOCKED) {
499                 c->state &= ~CONFIG_LOCKED;
500                 if (c->state & CONFIG_IO_REQ)
501                         for (i = 0; i < MAX_IO_WIN; i++) {
502                                 if (!s->io[i].res)
503                                         continue;
504                                 s->io[i].Config--;
505                                 if (s->io[i].Config != 0)
506                                         continue;
507                                 io.map = i;
508                                 s->ops->set_io_map(s, &io);
509                         }
510         }
511
512         return CS_SUCCESS;
513 } /* pcmcia_release_configuration */
514
515
516 /** pcmcia_release_io
517  *
518  * Release_io() releases the I/O ranges allocated by a client.  This
519  * may be invoked some time after a card ejection has already dumped
520  * the actual socket configuration, so if the client is "stale", we
521  * don't bother checking the port ranges against the current socket
522  * values.
523  */
524 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
525 {
526         struct pcmcia_socket *s = p_dev->socket;
527         config_t *c = p_dev->function_config;
528
529         if (!p_dev->_io )
530                 return CS_BAD_HANDLE;
531
532         p_dev->_io = 0;
533
534         if ((c->io.BasePort1 != req->BasePort1) ||
535             (c->io.NumPorts1 != req->NumPorts1) ||
536             (c->io.BasePort2 != req->BasePort2) ||
537             (c->io.NumPorts2 != req->NumPorts2))
538                 return CS_BAD_ARGS;
539
540         c->state &= ~CONFIG_IO_REQ;
541
542         release_io_space(s, req->BasePort1, req->NumPorts1);
543         if (req->NumPorts2)
544                 release_io_space(s, req->BasePort2, req->NumPorts2);
545
546         return CS_SUCCESS;
547 } /* pcmcia_release_io */
548
549
550 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
551 {
552         struct pcmcia_socket *s = p_dev->socket;
553         config_t *c= p_dev->function_config;
554
555         if (!p_dev->_irq)
556                 return CS_BAD_HANDLE;
557         p_dev->_irq = 0;
558
559         if (c->state & CONFIG_LOCKED)
560                 return CS_CONFIGURATION_LOCKED;
561         if (c->irq.Attributes != req->Attributes)
562                 return CS_BAD_ATTRIBUTE;
563         if (s->irq.AssignedIRQ != req->AssignedIRQ)
564                 return CS_BAD_IRQ;
565         if (--s->irq.Config == 0) {
566                 c->state &= ~CONFIG_IRQ_REQ;
567                 s->irq.AssignedIRQ = 0;
568         }
569
570         if (req->Attributes & IRQ_HANDLE_PRESENT) {
571                 free_irq(req->AssignedIRQ, req->Instance);
572         }
573
574 #ifdef CONFIG_PCMCIA_PROBE
575         pcmcia_used_irq[req->AssignedIRQ]--;
576 #endif
577
578         return CS_SUCCESS;
579 } /* pcmcia_release_irq */
580
581
582 int pcmcia_release_window(window_handle_t win)
583 {
584         struct pcmcia_socket *s;
585
586         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
587                 return CS_BAD_HANDLE;
588         s = win->sock;
589         if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
590                 return CS_BAD_HANDLE;
591
592         /* Shut down memory window */
593         win->ctl.flags &= ~MAP_ACTIVE;
594         s->ops->set_mem_map(s, &win->ctl);
595         s->state &= ~SOCKET_WIN_REQ(win->index);
596
597         /* Release system memory */
598         if (win->ctl.res) {
599                 release_resource(win->ctl.res);
600                 kfree(win->ctl.res);
601                 win->ctl.res = NULL;
602         }
603         win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
604
605         win->magic = 0;
606
607         return CS_SUCCESS;
608 } /* pcmcia_release_window */
609 EXPORT_SYMBOL(pcmcia_release_window);
610
611
612 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
613                                  config_req_t *req)
614 {
615         int i;
616         u_int base;
617         struct pcmcia_socket *s = p_dev->socket;
618         config_t *c;
619         pccard_io_map iomap;
620
621         if (!(s->state & SOCKET_PRESENT))
622                 return CS_NO_CARD;
623
624         if (req->IntType & INT_CARDBUS)
625                 return CS_UNSUPPORTED_MODE;
626         c = p_dev->function_config;
627         if (c->state & CONFIG_LOCKED)
628                 return CS_CONFIGURATION_LOCKED;
629
630         /* Do power control.  We don't allow changes in Vcc. */
631         s->socket.Vpp = req->Vpp;
632         if (s->ops->set_socket(s, &s->socket))
633                 return CS_BAD_VPP;
634
635         /* Pick memory or I/O card, DMA mode, interrupt */
636         c->IntType = req->IntType;
637         c->Attributes = req->Attributes;
638         if (req->IntType & INT_MEMORY_AND_IO)
639                 s->socket.flags |= SS_IOCARD;
640         if (req->IntType & INT_ZOOMED_VIDEO)
641                 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
642         if (req->Attributes & CONF_ENABLE_DMA)
643                 s->socket.flags |= SS_DMA_MODE;
644         if (req->Attributes & CONF_ENABLE_SPKR)
645                 s->socket.flags |= SS_SPKR_ENA;
646         if (req->Attributes & CONF_ENABLE_IRQ)
647                 s->socket.io_irq = s->irq.AssignedIRQ;
648         else
649                 s->socket.io_irq = 0;
650         s->ops->set_socket(s, &s->socket);
651         s->lock_count++;
652
653         /* Set up CIS configuration registers */
654         base = c->ConfigBase = req->ConfigBase;
655         c->CardValues = req->Present;
656         if (req->Present & PRESENT_COPY) {
657                 c->Copy = req->Copy;
658                 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
659         }
660         if (req->Present & PRESENT_OPTION) {
661                 if (s->functions == 1) {
662                         c->Option = req->ConfigIndex & COR_CONFIG_MASK;
663                 } else {
664                         c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
665                         c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
666                         if (req->Present & PRESENT_IOBASE_0)
667                                 c->Option |= COR_ADDR_DECODE;
668                 }
669                 if (c->state & CONFIG_IRQ_REQ)
670                         if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
671                                 c->Option |= COR_LEVEL_REQ;
672                 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
673                 mdelay(40);
674         }
675         if (req->Present & PRESENT_STATUS) {
676                 c->Status = req->Status;
677                 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
678         }
679         if (req->Present & PRESENT_PIN_REPLACE) {
680                 c->Pin = req->Pin;
681                 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
682         }
683         if (req->Present & PRESENT_EXT_STATUS) {
684                 c->ExtStatus = req->ExtStatus;
685                 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
686         }
687         if (req->Present & PRESENT_IOBASE_0) {
688                 u_char b = c->io.BasePort1 & 0xff;
689                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
690                 b = (c->io.BasePort1 >> 8) & 0xff;
691                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
692         }
693         if (req->Present & PRESENT_IOSIZE) {
694                 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
695                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
696         }
697
698         /* Configure I/O windows */
699         if (c->state & CONFIG_IO_REQ) {
700                 iomap.speed = io_speed;
701                 for (i = 0; i < MAX_IO_WIN; i++)
702                         if (s->io[i].res) {
703                                 iomap.map = i;
704                                 iomap.flags = MAP_ACTIVE;
705                                 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
706                                 case IO_DATA_PATH_WIDTH_16:
707                                         iomap.flags |= MAP_16BIT; break;
708                                 case IO_DATA_PATH_WIDTH_AUTO:
709                                         iomap.flags |= MAP_AUTOSZ; break;
710                                 default:
711                                         break;
712                                 }
713                                 iomap.start = s->io[i].res->start;
714                                 iomap.stop = s->io[i].res->end;
715                                 s->ops->set_io_map(s, &iomap);
716                                 s->io[i].Config++;
717                         }
718         }
719
720         c->state |= CONFIG_LOCKED;
721         p_dev->_locked = 1;
722         return CS_SUCCESS;
723 } /* pcmcia_request_configuration */
724 EXPORT_SYMBOL(pcmcia_request_configuration);
725
726
727 /** pcmcia_request_io
728  *
729  * Request_io() reserves ranges of port addresses for a socket.
730  * I have not implemented range sharing or alias addressing.
731  */
732 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
733 {
734         struct pcmcia_socket *s = p_dev->socket;
735         config_t *c;
736
737         if (!(s->state & SOCKET_PRESENT))
738                 return CS_NO_CARD;
739
740         if (!req)
741                 return CS_UNSUPPORTED_MODE;
742         c = p_dev->function_config;
743         if (c->state & CONFIG_LOCKED)
744                 return CS_CONFIGURATION_LOCKED;
745         if (c->state & CONFIG_IO_REQ)
746                 return CS_IN_USE;
747         if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
748                 return CS_BAD_ATTRIBUTE;
749         if ((req->NumPorts2 > 0) &&
750             (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
751                 return CS_BAD_ATTRIBUTE;
752
753         if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
754                            req->NumPorts1, req->IOAddrLines))
755                 return CS_IN_USE;
756
757         if (req->NumPorts2) {
758                 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
759                                    req->NumPorts2, req->IOAddrLines)) {
760                         release_io_space(s, req->BasePort1, req->NumPorts1);
761                         return CS_IN_USE;
762                 }
763         }
764
765         c->io = *req;
766         c->state |= CONFIG_IO_REQ;
767         p_dev->_io = 1;
768         return CS_SUCCESS;
769 } /* pcmcia_request_io */
770 EXPORT_SYMBOL(pcmcia_request_io);
771
772
773 /** pcmcia_request_irq
774  *
775  * Request_irq() reserves an irq for this client.
776  *
777  * Also, since Linux only reserves irq's when they are actually
778  * hooked, we don't guarantee that an irq will still be available
779  * when the configuration is locked.  Now that I think about it,
780  * there might be a way to fix this using a dummy handler.
781  */
782
783 #ifdef CONFIG_PCMCIA_PROBE
784 static irqreturn_t test_action(int cpl, void *dev_id)
785 {
786         return IRQ_NONE;
787 }
788 #endif
789
790 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
791 {
792         struct pcmcia_socket *s = p_dev->socket;
793         config_t *c;
794         int ret = CS_IN_USE, irq = 0;
795         int type;
796
797         if (!(s->state & SOCKET_PRESENT))
798                 return CS_NO_CARD;
799         c = p_dev->function_config;
800         if (c->state & CONFIG_LOCKED)
801                 return CS_CONFIGURATION_LOCKED;
802         if (c->state & CONFIG_IRQ_REQ)
803                 return CS_IN_USE;
804
805         /* Decide what type of interrupt we are registering */
806         type = 0;
807         if (s->functions > 1)           /* All of this ought to be handled higher up */
808                 type = IRQF_SHARED;
809         if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
810                 type = IRQF_SHARED;
811
812 #ifdef CONFIG_PCMCIA_PROBE
813
814 #ifdef IRQ_NOAUTOEN
815         /* if the underlying IRQ infrastructure allows for it, only allocate
816          * the IRQ, but do not enable it
817          */
818         if (!(req->Attributes & IRQ_HANDLE_PRESENT))
819                 type |= IRQ_NOAUTOEN;
820 #endif /* IRQ_NOAUTOEN */
821
822         if (s->irq.AssignedIRQ != 0) {
823                 /* If the interrupt is already assigned, it must be the same */
824                 irq = s->irq.AssignedIRQ;
825         } else {
826                 int try;
827                 u32 mask = s->irq_mask;
828                 void *data = &p_dev->dev.driver; /* something unique to this device */
829
830                 for (try = 0; try < 64; try++) {
831                         irq = try % 32;
832
833                         /* marked as available by driver, and not blocked by userspace? */
834                         if (!((mask >> irq) & 1))
835                                 continue;
836
837                         /* avoid an IRQ which is already used by a PCMCIA card */
838                         if ((try < 32) && pcmcia_used_irq[irq])
839                                 continue;
840
841                         /* register the correct driver, if possible, of check whether
842                          * registering a dummy handle works, i.e. if the IRQ isn't
843                          * marked as used by the kernel resource management core */
844                         ret = request_irq(irq,
845                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
846                                           type,
847                                           p_dev->devname,
848                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
849                         if (!ret) {
850                                 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
851                                         free_irq(irq, data);
852                                 break;
853                         }
854                 }
855         }
856 #endif
857         /* only assign PCI irq if no IRQ already assigned */
858         if (ret && !s->irq.AssignedIRQ) {
859                 if (!s->pci_irq)
860                         return ret;
861                 type = IRQF_SHARED;
862                 irq = s->pci_irq;
863         }
864
865         if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
866                 if (request_irq(irq, req->Handler, type,  p_dev->devname, req->Instance))
867                         return CS_IN_USE;
868         }
869
870         /* Make sure the fact the request type was overridden is passed back */
871         if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
872                 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
873                 printk(KERN_WARNING "pcmcia: request for exclusive IRQ could not be fulfilled.\n");
874                 printk(KERN_WARNING "pcmcia: the driver needs updating to supported shared IRQ lines.\n");
875         }
876         c->irq.Attributes = req->Attributes;
877         s->irq.AssignedIRQ = req->AssignedIRQ = irq;
878         s->irq.Config++;
879
880         c->state |= CONFIG_IRQ_REQ;
881         p_dev->_irq = 1;
882
883 #ifdef CONFIG_PCMCIA_PROBE
884         pcmcia_used_irq[irq]++;
885 #endif
886
887         return CS_SUCCESS;
888 } /* pcmcia_request_irq */
889 EXPORT_SYMBOL(pcmcia_request_irq);
890
891
892 /** pcmcia_request_window
893  *
894  * Request_window() establishes a mapping between card memory space
895  * and system memory space.
896  */
897 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
898 {
899         struct pcmcia_socket *s = (*p_dev)->socket;
900         window_t *win;
901         u_long align;
902         int w;
903
904         if (!(s->state & SOCKET_PRESENT))
905                 return CS_NO_CARD;
906         if (req->Attributes & (WIN_PAGED | WIN_SHARED))
907                 return CS_BAD_ATTRIBUTE;
908
909         /* Window size defaults to smallest available */
910         if (req->Size == 0)
911                 req->Size = s->map_size;
912         align = (((s->features & SS_CAP_MEM_ALIGN) ||
913                   (req->Attributes & WIN_STRICT_ALIGN)) ?
914                  req->Size : s->map_size);
915         if (req->Size & (s->map_size-1))
916                 return CS_BAD_SIZE;
917         if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
918             (req->Base & (align-1)))
919                 return CS_BAD_BASE;
920         if (req->Base)
921                 align = 0;
922
923         /* Allocate system memory window */
924         for (w = 0; w < MAX_WIN; w++)
925                 if (!(s->state & SOCKET_WIN_REQ(w))) break;
926         if (w == MAX_WIN)
927                 return CS_OUT_OF_RESOURCE;
928
929         win = &s->win[w];
930         win->magic = WINDOW_MAGIC;
931         win->index = w;
932         win->handle = *p_dev;
933         win->sock = s;
934
935         if (!(s->features & SS_CAP_STATIC_MAP)) {
936                 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
937                                                       (req->Attributes & WIN_MAP_BELOW_1MB), s);
938                 if (!win->ctl.res)
939                         return CS_IN_USE;
940         }
941         (*p_dev)->_win |= CLIENT_WIN_REQ(w);
942
943         /* Configure the socket controller */
944         win->ctl.map = w+1;
945         win->ctl.flags = 0;
946         win->ctl.speed = req->AccessSpeed;
947         if (req->Attributes & WIN_MEMORY_TYPE)
948                 win->ctl.flags |= MAP_ATTRIB;
949         if (req->Attributes & WIN_ENABLE)
950                 win->ctl.flags |= MAP_ACTIVE;
951         if (req->Attributes & WIN_DATA_WIDTH_16)
952                 win->ctl.flags |= MAP_16BIT;
953         if (req->Attributes & WIN_USE_WAIT)
954                 win->ctl.flags |= MAP_USE_WAIT;
955         win->ctl.card_start = 0;
956         if (s->ops->set_mem_map(s, &win->ctl) != 0)
957                 return CS_BAD_ARGS;
958         s->state |= SOCKET_WIN_REQ(w);
959
960         /* Return window handle */
961         if (s->features & SS_CAP_STATIC_MAP) {
962                 req->Base = win->ctl.static_start;
963         } else {
964                 req->Base = win->ctl.res->start;
965         }
966         *wh = win;
967
968         return CS_SUCCESS;
969 } /* pcmcia_request_window */
970 EXPORT_SYMBOL(pcmcia_request_window);
971
972 void pcmcia_disable_device(struct pcmcia_device *p_dev) {
973         pcmcia_release_configuration(p_dev);
974         pcmcia_release_io(p_dev, &p_dev->io);
975         pcmcia_release_irq(p_dev, &p_dev->irq);
976         if (p_dev->win)
977                 pcmcia_release_window(p_dev->win);
978 }
979 EXPORT_SYMBOL(pcmcia_disable_device);