9afe420c41f4520a9034837303321aa27fe61042
[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/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 #include <pcmcia/cs_types.h>
25 #include <pcmcia/ss.h>
26 #include <pcmcia/cs.h>
27 #include <pcmcia/cistpl.h>
28 #include <pcmcia/cisreg.h>
29 #include <pcmcia/ds.h>
30
31 #include "cs_internal.h"
32 #include "ds_internal.h"
33
34
35 /* Access speed for IO windows */
36 static int io_speed = 0;
37 module_param(io_speed, int, 0444);
38
39
40 #ifdef CONFIG_PCMCIA_PROBE
41 #include <asm/irq.h>
42 /* mask of IRQs already reserved by other cards, we should avoid using them */
43 static u8 pcmcia_used_irq[NR_IRQS];
44 #endif
45
46
47 #ifdef CONFIG_PCMCIA_DEBUG
48 extern int ds_pc_debug;
49
50 #define ds_dbg(skt, lvl, fmt, arg...) do {                      \
51         if (ds_pc_debug >= lvl)                                 \
52                 dev_printk(KERN_DEBUG, &skt->dev,               \
53                            "pcmcia_resource: " fmt,             \
54                            ## arg);                             \
55 } while (0)
56 #else
57 #define ds_dbg(skt, 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 /** pcmcia_get_mem_page
314  *
315  * Change the card address of an already open memory window.
316  */
317 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
318 {
319         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
320                 return CS_BAD_HANDLE;
321         req->Page = 0;
322         req->CardOffset = win->ctl.card_start;
323         return CS_SUCCESS;
324 } /* pcmcia_get_mem_page */
325 EXPORT_SYMBOL(pcmcia_get_mem_page);
326
327
328 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
329 {
330         struct pcmcia_socket *s;
331         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
332                 return CS_BAD_HANDLE;
333         if (req->Page != 0)
334                 return CS_BAD_PAGE;
335         s = win->sock;
336         win->ctl.card_start = req->CardOffset;
337         if (s->ops->set_mem_map(s, &win->ctl) != 0)
338                 return CS_BAD_OFFSET;
339         return CS_SUCCESS;
340 } /* pcmcia_map_mem_page */
341 EXPORT_SYMBOL(pcmcia_map_mem_page);
342
343
344 /** pcmcia_modify_configuration
345  *
346  * Modify a locked socket configuration
347  */
348 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
349                                 modconf_t *mod)
350 {
351         struct pcmcia_socket *s;
352         config_t *c;
353
354         s = p_dev->socket;
355         c = p_dev->function_config;
356
357         if (!(s->state & SOCKET_PRESENT))
358                 return CS_NO_CARD;
359         if (!(c->state & CONFIG_LOCKED))
360                 return CS_CONFIGURATION_LOCKED;
361
362         if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
363                 if (mod->Attributes & CONF_ENABLE_IRQ) {
364                         c->Attributes |= CONF_ENABLE_IRQ;
365                         s->socket.io_irq = s->irq.AssignedIRQ;
366                 } else {
367                         c->Attributes &= ~CONF_ENABLE_IRQ;
368                         s->socket.io_irq = 0;
369                 }
370                 s->ops->set_socket(s, &s->socket);
371         }
372
373         if (mod->Attributes & CONF_VCC_CHANGE_VALID)
374                 return CS_BAD_VCC;
375
376         /* We only allow changing Vpp1 and Vpp2 to the same value */
377         if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
378             (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
379                 if (mod->Vpp1 != mod->Vpp2)
380                         return CS_BAD_VPP;
381                 s->socket.Vpp = mod->Vpp1;
382                 if (s->ops->set_socket(s, &s->socket))
383                         return CS_BAD_VPP;
384         } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
385                    (mod->Attributes & CONF_VPP2_CHANGE_VALID))
386                 return CS_BAD_VPP;
387
388         if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
389                 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
390                 pccard_io_map io_on;
391                 int i;
392
393                 io_on.speed = io_speed;
394                 for (i = 0; i < MAX_IO_WIN; i++) {
395                         if (!s->io[i].res)
396                                 continue;
397                         io_off.map = i;
398                         io_on.map = i;
399
400                         io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
401                         io_on.start = s->io[i].res->start;
402                         io_on.stop = s->io[i].res->end;
403
404                         s->ops->set_io_map(s, &io_off);
405                         mdelay(40);
406                         s->ops->set_io_map(s, &io_on);
407                 }
408         }
409
410         return CS_SUCCESS;
411 } /* modify_configuration */
412 EXPORT_SYMBOL(pcmcia_modify_configuration);
413
414
415 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
416 {
417         pccard_io_map io = { 0, 0, 0, 0, 1 };
418         struct pcmcia_socket *s = p_dev->socket;
419         config_t *c = p_dev->function_config;
420         int i;
421
422         if (p_dev->_locked) {
423                 p_dev->_locked = 0;
424                 if (--(s->lock_count) == 0) {
425                         s->socket.flags = SS_OUTPUT_ENA;   /* Is this correct? */
426                         s->socket.Vpp = 0;
427                         s->socket.io_irq = 0;
428                         s->ops->set_socket(s, &s->socket);
429                 }
430         }
431         if (c->state & CONFIG_LOCKED) {
432                 c->state &= ~CONFIG_LOCKED;
433                 if (c->state & CONFIG_IO_REQ)
434                         for (i = 0; i < MAX_IO_WIN; i++) {
435                                 if (!s->io[i].res)
436                                         continue;
437                                 s->io[i].Config--;
438                                 if (s->io[i].Config != 0)
439                                         continue;
440                                 io.map = i;
441                                 s->ops->set_io_map(s, &io);
442                         }
443         }
444
445         return CS_SUCCESS;
446 } /* pcmcia_release_configuration */
447
448
449 /** pcmcia_release_io
450  *
451  * Release_io() releases the I/O ranges allocated by a client.  This
452  * may be invoked some time after a card ejection has already dumped
453  * the actual socket configuration, so if the client is "stale", we
454  * don't bother checking the port ranges against the current socket
455  * values.
456  */
457 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
458 {
459         struct pcmcia_socket *s = p_dev->socket;
460         config_t *c = p_dev->function_config;
461
462         if (!p_dev->_io )
463                 return CS_BAD_HANDLE;
464
465         p_dev->_io = 0;
466
467         if ((c->io.BasePort1 != req->BasePort1) ||
468             (c->io.NumPorts1 != req->NumPorts1) ||
469             (c->io.BasePort2 != req->BasePort2) ||
470             (c->io.NumPorts2 != req->NumPorts2))
471                 return CS_BAD_ARGS;
472
473         c->state &= ~CONFIG_IO_REQ;
474
475         release_io_space(s, req->BasePort1, req->NumPorts1);
476         if (req->NumPorts2)
477                 release_io_space(s, req->BasePort2, req->NumPorts2);
478
479         return CS_SUCCESS;
480 } /* pcmcia_release_io */
481
482
483 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
484 {
485         struct pcmcia_socket *s = p_dev->socket;
486         config_t *c= p_dev->function_config;
487
488         if (!p_dev->_irq)
489                 return CS_BAD_HANDLE;
490         p_dev->_irq = 0;
491
492         if (c->state & CONFIG_LOCKED)
493                 return CS_CONFIGURATION_LOCKED;
494         if (c->irq.Attributes != req->Attributes)
495                 return CS_BAD_ATTRIBUTE;
496         if (s->irq.AssignedIRQ != req->AssignedIRQ)
497                 return CS_BAD_IRQ;
498         if (--s->irq.Config == 0) {
499                 c->state &= ~CONFIG_IRQ_REQ;
500                 s->irq.AssignedIRQ = 0;
501         }
502
503         if (req->Attributes & IRQ_HANDLE_PRESENT) {
504                 free_irq(req->AssignedIRQ, req->Instance);
505         }
506
507 #ifdef CONFIG_PCMCIA_PROBE
508         pcmcia_used_irq[req->AssignedIRQ]--;
509 #endif
510
511         return CS_SUCCESS;
512 } /* pcmcia_release_irq */
513
514
515 int pcmcia_release_window(window_handle_t win)
516 {
517         struct pcmcia_socket *s;
518
519         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
520                 return CS_BAD_HANDLE;
521         s = win->sock;
522         if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
523                 return CS_BAD_HANDLE;
524
525         /* Shut down memory window */
526         win->ctl.flags &= ~MAP_ACTIVE;
527         s->ops->set_mem_map(s, &win->ctl);
528         s->state &= ~SOCKET_WIN_REQ(win->index);
529
530         /* Release system memory */
531         if (win->ctl.res) {
532                 release_resource(win->ctl.res);
533                 kfree(win->ctl.res);
534                 win->ctl.res = NULL;
535         }
536         win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
537
538         win->magic = 0;
539
540         return CS_SUCCESS;
541 } /* pcmcia_release_window */
542 EXPORT_SYMBOL(pcmcia_release_window);
543
544
545 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
546                                  config_req_t *req)
547 {
548         int i;
549         u_int base;
550         struct pcmcia_socket *s = p_dev->socket;
551         config_t *c;
552         pccard_io_map iomap;
553
554         if (!(s->state & SOCKET_PRESENT))
555                 return CS_NO_CARD;
556
557         if (req->IntType & INT_CARDBUS)
558                 return CS_UNSUPPORTED_MODE;
559         c = p_dev->function_config;
560         if (c->state & CONFIG_LOCKED)
561                 return CS_CONFIGURATION_LOCKED;
562
563         /* Do power control.  We don't allow changes in Vcc. */
564         s->socket.Vpp = req->Vpp;
565         if (s->ops->set_socket(s, &s->socket))
566                 return CS_BAD_VPP;
567
568         /* Pick memory or I/O card, DMA mode, interrupt */
569         c->IntType = req->IntType;
570         c->Attributes = req->Attributes;
571         if (req->IntType & INT_MEMORY_AND_IO)
572                 s->socket.flags |= SS_IOCARD;
573         if (req->IntType & INT_ZOOMED_VIDEO)
574                 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
575         if (req->Attributes & CONF_ENABLE_DMA)
576                 s->socket.flags |= SS_DMA_MODE;
577         if (req->Attributes & CONF_ENABLE_SPKR)
578                 s->socket.flags |= SS_SPKR_ENA;
579         if (req->Attributes & CONF_ENABLE_IRQ)
580                 s->socket.io_irq = s->irq.AssignedIRQ;
581         else
582                 s->socket.io_irq = 0;
583         s->ops->set_socket(s, &s->socket);
584         s->lock_count++;
585
586         /* Set up CIS configuration registers */
587         base = c->ConfigBase = req->ConfigBase;
588         c->CardValues = req->Present;
589         if (req->Present & PRESENT_COPY) {
590                 c->Copy = req->Copy;
591                 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
592         }
593         if (req->Present & PRESENT_OPTION) {
594                 if (s->functions == 1) {
595                         c->Option = req->ConfigIndex & COR_CONFIG_MASK;
596                 } else {
597                         c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
598                         c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
599                         if (req->Present & PRESENT_IOBASE_0)
600                                 c->Option |= COR_ADDR_DECODE;
601                 }
602                 if (c->state & CONFIG_IRQ_REQ)
603                         if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
604                                 c->Option |= COR_LEVEL_REQ;
605                 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
606                 mdelay(40);
607         }
608         if (req->Present & PRESENT_STATUS) {
609                 c->Status = req->Status;
610                 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
611         }
612         if (req->Present & PRESENT_PIN_REPLACE) {
613                 c->Pin = req->Pin;
614                 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
615         }
616         if (req->Present & PRESENT_EXT_STATUS) {
617                 c->ExtStatus = req->ExtStatus;
618                 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
619         }
620         if (req->Present & PRESENT_IOBASE_0) {
621                 u_char b = c->io.BasePort1 & 0xff;
622                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
623                 b = (c->io.BasePort1 >> 8) & 0xff;
624                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
625         }
626         if (req->Present & PRESENT_IOSIZE) {
627                 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
628                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
629         }
630
631         /* Configure I/O windows */
632         if (c->state & CONFIG_IO_REQ) {
633                 iomap.speed = io_speed;
634                 for (i = 0; i < MAX_IO_WIN; i++)
635                         if (s->io[i].res) {
636                                 iomap.map = i;
637                                 iomap.flags = MAP_ACTIVE;
638                                 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
639                                 case IO_DATA_PATH_WIDTH_16:
640                                         iomap.flags |= MAP_16BIT; break;
641                                 case IO_DATA_PATH_WIDTH_AUTO:
642                                         iomap.flags |= MAP_AUTOSZ; break;
643                                 default:
644                                         break;
645                                 }
646                                 iomap.start = s->io[i].res->start;
647                                 iomap.stop = s->io[i].res->end;
648                                 s->ops->set_io_map(s, &iomap);
649                                 s->io[i].Config++;
650                         }
651         }
652
653         c->state |= CONFIG_LOCKED;
654         p_dev->_locked = 1;
655         return CS_SUCCESS;
656 } /* pcmcia_request_configuration */
657 EXPORT_SYMBOL(pcmcia_request_configuration);
658
659
660 /** pcmcia_request_io
661  *
662  * Request_io() reserves ranges of port addresses for a socket.
663  * I have not implemented range sharing or alias addressing.
664  */
665 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
666 {
667         struct pcmcia_socket *s = p_dev->socket;
668         config_t *c;
669
670         if (!(s->state & SOCKET_PRESENT))
671                 return CS_NO_CARD;
672
673         if (!req)
674                 return CS_UNSUPPORTED_MODE;
675         c = p_dev->function_config;
676         if (c->state & CONFIG_LOCKED)
677                 return CS_CONFIGURATION_LOCKED;
678         if (c->state & CONFIG_IO_REQ)
679                 return CS_IN_USE;
680         if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
681                 return CS_BAD_ATTRIBUTE;
682         if ((req->NumPorts2 > 0) &&
683             (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
684                 return CS_BAD_ATTRIBUTE;
685
686         if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
687                            req->NumPorts1, req->IOAddrLines))
688                 return CS_IN_USE;
689
690         if (req->NumPorts2) {
691                 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
692                                    req->NumPorts2, req->IOAddrLines)) {
693                         release_io_space(s, req->BasePort1, req->NumPorts1);
694                         return CS_IN_USE;
695                 }
696         }
697
698         c->io = *req;
699         c->state |= CONFIG_IO_REQ;
700         p_dev->_io = 1;
701         return CS_SUCCESS;
702 } /* pcmcia_request_io */
703 EXPORT_SYMBOL(pcmcia_request_io);
704
705
706 /** pcmcia_request_irq
707  *
708  * Request_irq() reserves an irq for this client.
709  *
710  * Also, since Linux only reserves irq's when they are actually
711  * hooked, we don't guarantee that an irq will still be available
712  * when the configuration is locked.  Now that I think about it,
713  * there might be a way to fix this using a dummy handler.
714  */
715
716 #ifdef CONFIG_PCMCIA_PROBE
717 static irqreturn_t test_action(int cpl, void *dev_id)
718 {
719         return IRQ_NONE;
720 }
721 #endif
722
723 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
724 {
725         struct pcmcia_socket *s = p_dev->socket;
726         config_t *c;
727         int ret = CS_IN_USE, irq = 0;
728         int type;
729
730         if (!(s->state & SOCKET_PRESENT))
731                 return CS_NO_CARD;
732         c = p_dev->function_config;
733         if (c->state & CONFIG_LOCKED)
734                 return CS_CONFIGURATION_LOCKED;
735         if (c->state & CONFIG_IRQ_REQ)
736                 return CS_IN_USE;
737
738         /* Decide what type of interrupt we are registering */
739         type = 0;
740         if (s->functions > 1)           /* All of this ought to be handled higher up */
741                 type = IRQF_SHARED;
742         if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
743                 type = IRQF_SHARED;
744
745 #ifdef CONFIG_PCMCIA_PROBE
746
747 #ifdef IRQ_NOAUTOEN
748         /* if the underlying IRQ infrastructure allows for it, only allocate
749          * the IRQ, but do not enable it
750          */
751         if (!(req->Attributes & IRQ_HANDLE_PRESENT))
752                 type |= IRQ_NOAUTOEN;
753 #endif /* IRQ_NOAUTOEN */
754
755         if (s->irq.AssignedIRQ != 0) {
756                 /* If the interrupt is already assigned, it must be the same */
757                 irq = s->irq.AssignedIRQ;
758         } else {
759                 int try;
760                 u32 mask = s->irq_mask;
761                 void *data = &p_dev->dev.driver; /* something unique to this device */
762
763                 for (try = 0; try < 64; try++) {
764                         irq = try % 32;
765
766                         /* marked as available by driver, and not blocked by userspace? */
767                         if (!((mask >> irq) & 1))
768                                 continue;
769
770                         /* avoid an IRQ which is already used by a PCMCIA card */
771                         if ((try < 32) && pcmcia_used_irq[irq])
772                                 continue;
773
774                         /* register the correct driver, if possible, of check whether
775                          * registering a dummy handle works, i.e. if the IRQ isn't
776                          * marked as used by the kernel resource management core */
777                         ret = request_irq(irq,
778                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
779                                           type,
780                                           p_dev->devname,
781                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
782                         if (!ret) {
783                                 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
784                                         free_irq(irq, data);
785                                 break;
786                         }
787                 }
788         }
789 #endif
790         /* only assign PCI irq if no IRQ already assigned */
791         if (ret && !s->irq.AssignedIRQ) {
792                 if (!s->pci_irq)
793                         return ret;
794                 type = IRQF_SHARED;
795                 irq = s->pci_irq;
796         }
797
798         if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
799                 if (request_irq(irq, req->Handler, type,  p_dev->devname, req->Instance))
800                         return CS_IN_USE;
801         }
802
803         /* Make sure the fact the request type was overridden is passed back */
804         if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
805                 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
806                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
807                         "request for exclusive IRQ could not be fulfilled.\n");
808                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
809                         "needs updating to supported shared IRQ lines.\n");
810         }
811         c->irq.Attributes = req->Attributes;
812         s->irq.AssignedIRQ = req->AssignedIRQ = irq;
813         s->irq.Config++;
814
815         c->state |= CONFIG_IRQ_REQ;
816         p_dev->_irq = 1;
817
818 #ifdef CONFIG_PCMCIA_PROBE
819         pcmcia_used_irq[irq]++;
820 #endif
821
822         return CS_SUCCESS;
823 } /* pcmcia_request_irq */
824 EXPORT_SYMBOL(pcmcia_request_irq);
825
826
827 /** pcmcia_request_window
828  *
829  * Request_window() establishes a mapping between card memory space
830  * and system memory space.
831  */
832 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
833 {
834         struct pcmcia_socket *s = (*p_dev)->socket;
835         window_t *win;
836         u_long align;
837         int w;
838
839         if (!(s->state & SOCKET_PRESENT))
840                 return CS_NO_CARD;
841         if (req->Attributes & (WIN_PAGED | WIN_SHARED))
842                 return CS_BAD_ATTRIBUTE;
843
844         /* Window size defaults to smallest available */
845         if (req->Size == 0)
846                 req->Size = s->map_size;
847         align = (((s->features & SS_CAP_MEM_ALIGN) ||
848                   (req->Attributes & WIN_STRICT_ALIGN)) ?
849                  req->Size : s->map_size);
850         if (req->Size & (s->map_size-1))
851                 return CS_BAD_SIZE;
852         if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
853             (req->Base & (align-1)))
854                 return CS_BAD_BASE;
855         if (req->Base)
856                 align = 0;
857
858         /* Allocate system memory window */
859         for (w = 0; w < MAX_WIN; w++)
860                 if (!(s->state & SOCKET_WIN_REQ(w))) break;
861         if (w == MAX_WIN)
862                 return CS_OUT_OF_RESOURCE;
863
864         win = &s->win[w];
865         win->magic = WINDOW_MAGIC;
866         win->index = w;
867         win->handle = *p_dev;
868         win->sock = s;
869
870         if (!(s->features & SS_CAP_STATIC_MAP)) {
871                 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
872                                                       (req->Attributes & WIN_MAP_BELOW_1MB), s);
873                 if (!win->ctl.res)
874                         return CS_IN_USE;
875         }
876         (*p_dev)->_win |= CLIENT_WIN_REQ(w);
877
878         /* Configure the socket controller */
879         win->ctl.map = w+1;
880         win->ctl.flags = 0;
881         win->ctl.speed = req->AccessSpeed;
882         if (req->Attributes & WIN_MEMORY_TYPE)
883                 win->ctl.flags |= MAP_ATTRIB;
884         if (req->Attributes & WIN_ENABLE)
885                 win->ctl.flags |= MAP_ACTIVE;
886         if (req->Attributes & WIN_DATA_WIDTH_16)
887                 win->ctl.flags |= MAP_16BIT;
888         if (req->Attributes & WIN_USE_WAIT)
889                 win->ctl.flags |= MAP_USE_WAIT;
890         win->ctl.card_start = 0;
891         if (s->ops->set_mem_map(s, &win->ctl) != 0)
892                 return CS_BAD_ARGS;
893         s->state |= SOCKET_WIN_REQ(w);
894
895         /* Return window handle */
896         if (s->features & SS_CAP_STATIC_MAP) {
897                 req->Base = win->ctl.static_start;
898         } else {
899                 req->Base = win->ctl.res->start;
900         }
901         *wh = win;
902
903         return CS_SUCCESS;
904 } /* pcmcia_request_window */
905 EXPORT_SYMBOL(pcmcia_request_window);
906
907 void pcmcia_disable_device(struct pcmcia_device *p_dev) {
908         pcmcia_release_configuration(p_dev);
909         pcmcia_release_io(p_dev, &p_dev->io);
910         pcmcia_release_irq(p_dev, &p_dev->irq);
911         if (p_dev->win)
912                 pcmcia_release_window(p_dev->win);
913 }
914 EXPORT_SYMBOL(pcmcia_disable_device);
915
916
917 struct pcmcia_cfg_mem {
918         tuple_t tuple;
919         cisparse_t parse;
920         u8 buf[256];
921         cistpl_cftable_entry_t dflt;
922 };
923
924 /**
925  * pcmcia_loop_config() - loop over configuration options
926  * @p_dev:      the struct pcmcia_device which we need to loop for.
927  * @conf_check: function to call for each configuration option.
928  *              It gets passed the struct pcmcia_device, the CIS data
929  *              describing the configuration option, and private data
930  *              being passed to pcmcia_loop_config()
931  * @priv_data:  private data to be passed to the conf_check function.
932  *
933  * pcmcia_loop_config() loops over all configuration options, and calls
934  * the driver-specific conf_check() for each one, checking whether
935  * it is a valid one.
936  */
937 int pcmcia_loop_config(struct pcmcia_device *p_dev,
938                        int      (*conf_check)   (struct pcmcia_device *p_dev,
939                                                  cistpl_cftable_entry_t *cfg,
940                                                  cistpl_cftable_entry_t *dflt,
941                                                  unsigned int vcc,
942                                                  void *priv_data),
943                        void *priv_data)
944 {
945         struct pcmcia_cfg_mem *cfg_mem;
946
947         tuple_t *tuple;
948         int ret = -ENODEV;
949         unsigned int vcc;
950
951         cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
952         if (cfg_mem == NULL)
953                 return -ENOMEM;
954
955         /* get the current Vcc setting */
956         vcc = p_dev->socket->socket.Vcc;
957
958         tuple = &cfg_mem->tuple;
959         tuple->TupleData = cfg_mem->buf;
960         tuple->TupleDataMax = 255;
961         tuple->TupleOffset = 0;
962         tuple->DesiredTuple = CISTPL_CFTABLE_ENTRY;
963         tuple->Attributes = 0;
964
965         ret = pcmcia_get_first_tuple(p_dev, tuple);
966         while (!ret) {
967                 cistpl_cftable_entry_t *cfg = &cfg_mem->parse.cftable_entry;
968
969                 if (pcmcia_get_tuple_data(p_dev, tuple))
970                         goto next_entry;
971
972                 if (pcmcia_parse_tuple(p_dev, tuple, &cfg_mem->parse))
973                         goto next_entry;
974
975                 /* default values */
976                 p_dev->conf.ConfigIndex = cfg->index;
977                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
978                         cfg_mem->dflt = *cfg;
979
980                 ret = conf_check(p_dev, cfg, &cfg_mem->dflt, vcc, priv_data);
981                 if (!ret)
982                         break;
983
984 next_entry:
985                 ret = pcmcia_get_next_tuple(p_dev, tuple);
986         }
987
988         return ret;
989 }
990 EXPORT_SYMBOL(pcmcia_loop_config);