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