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