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