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