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