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