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