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