]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/pcmcia/pcmcia_resource.c
pcmcia: clarify alloc_io_space, move it to resource handlers
[net-next-2.6.git] / drivers / pcmcia / pcmcia_resource.c
1 /*
2  * PCMCIA 16-bit resource management functions
3  *
4  * The initial developer of the original code is David A. Hinds
5  * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
6  * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
7  *
8  * Copyright (C) 1999        David A. Hinds
9  * Copyright (C) 2004-2005   Dominik Brodowski
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  */
16
17 #include <linux/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 #include <linux/netdevice.h>
24 #include <linux/slab.h>
25
26 #include <asm/irq.h>
27
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/ss.h>
30 #include <pcmcia/cs.h>
31 #include <pcmcia/cistpl.h>
32 #include <pcmcia/cisreg.h>
33 #include <pcmcia/ds.h>
34
35 #include "cs_internal.h"
36
37
38 /* Access speed for IO windows */
39 static int io_speed;
40 module_param(io_speed, int, 0444);
41
42
43 int pcmcia_validate_mem(struct pcmcia_socket *s)
44 {
45         if (s->resource_ops->validate_mem)
46                 return s->resource_ops->validate_mem(s);
47         /* if there is no callback, we can assume that everything is OK */
48         return 0;
49 }
50
51 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
52                                  int low, struct pcmcia_socket *s)
53 {
54         if (s->resource_ops->find_mem)
55                 return s->resource_ops->find_mem(base, num, align, low, s);
56         return NULL;
57 }
58
59
60 /** alloc_io_space
61  *
62  * Special stuff for managing IO windows, because they are scarce
63  */
64
65 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
66                           unsigned int *base, unsigned int num, u_int lines)
67 {
68         unsigned int align;
69
70         align = (*base) ? (lines ? 1<<lines : 0) : 1;
71         if (align && (align < num)) {
72                 if (*base) {
73                         dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
74                                num, align);
75                         align = 0;
76                 } else
77                         while (align && (align < num))
78                                 align <<= 1;
79         }
80         if (*base & ~(align-1)) {
81                 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
82                        *base, align);
83                 align = 0;
84         }
85
86         return s->resource_ops->find_io(s, attr, base, num, align);
87 } /* alloc_io_space */
88
89
90 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
91                              unsigned int num)
92 {
93         int i;
94
95         for (i = 0; i < MAX_IO_WIN; i++) {
96                 if (!s->io[i].res)
97                         continue;
98                 if ((s->io[i].res->start <= base) &&
99                     (s->io[i].res->end >= base+num-1)) {
100                         s->io[i].InUse -= num;
101                         /* Free the window if no one else is using it */
102                         if (s->io[i].InUse == 0) {
103                                 release_resource(s->io[i].res);
104                                 kfree(s->io[i].res);
105                                 s->io[i].res = NULL;
106                         }
107                 }
108         }
109 } /* release_io_space */
110
111
112 /** pccard_access_configuration_register
113  *
114  * Access_configuration_register() reads and writes configuration
115  * registers in attribute memory.  Memory window 0 is reserved for
116  * this and the tuple reading services.
117  */
118
119 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
120                                          conf_reg_t *reg)
121 {
122         struct pcmcia_socket *s;
123         config_t *c;
124         int addr;
125         u_char val;
126
127         if (!p_dev || !p_dev->function_config)
128                 return -EINVAL;
129
130         s = p_dev->socket;
131
132         mutex_lock(&s->ops_mutex);
133         c = p_dev->function_config;
134
135         if (!(c->state & CONFIG_LOCKED)) {
136                 dev_dbg(&s->dev, "Configuration isnt't locked\n");
137                 mutex_unlock(&s->ops_mutex);
138                 return -EACCES;
139         }
140
141         addr = (c->ConfigBase + reg->Offset) >> 1;
142         mutex_unlock(&s->ops_mutex);
143
144         switch (reg->Action) {
145         case CS_READ:
146                 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
147                 reg->Value = val;
148                 break;
149         case CS_WRITE:
150                 val = reg->Value;
151                 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
152                 break;
153         default:
154                 dev_dbg(&s->dev, "Invalid conf register request\n");
155                 return -EINVAL;
156                 break;
157         }
158         return 0;
159 } /* pcmcia_access_configuration_register */
160 EXPORT_SYMBOL(pcmcia_access_configuration_register);
161
162
163 int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
164                         memreq_t *req)
165 {
166         struct pcmcia_socket *s = p_dev->socket;
167         int ret;
168
169         wh--;
170         if (wh >= MAX_WIN)
171                 return -EINVAL;
172         if (req->Page != 0) {
173                 dev_dbg(&s->dev, "failure: requested page is zero\n");
174                 return -EINVAL;
175         }
176         mutex_lock(&s->ops_mutex);
177         s->win[wh].card_start = req->CardOffset;
178         ret = s->ops->set_mem_map(s, &s->win[wh]);
179         if (ret)
180                 dev_warn(&s->dev, "failed to set_mem_map\n");
181         mutex_unlock(&s->ops_mutex);
182         return ret;
183 } /* pcmcia_map_mem_page */
184 EXPORT_SYMBOL(pcmcia_map_mem_page);
185
186
187 /** pcmcia_modify_configuration
188  *
189  * Modify a locked socket configuration
190  */
191 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
192                                 modconf_t *mod)
193 {
194         struct pcmcia_socket *s;
195         config_t *c;
196         int ret;
197
198         s = p_dev->socket;
199
200         mutex_lock(&s->ops_mutex);
201         c = p_dev->function_config;
202
203         if (!(s->state & SOCKET_PRESENT)) {
204                 dev_dbg(&s->dev, "No card present\n");
205                 ret = -ENODEV;
206                 goto unlock;
207         }
208         if (!(c->state & CONFIG_LOCKED)) {
209                 dev_dbg(&s->dev, "Configuration isnt't locked\n");
210                 ret = -EACCES;
211                 goto unlock;
212         }
213
214         if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
215                 dev_dbg(&s->dev,
216                         "changing Vcc or IRQ is not allowed at this time\n");
217                 ret = -EINVAL;
218                 goto unlock;
219         }
220
221         /* We only allow changing Vpp1 and Vpp2 to the same value */
222         if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
223             (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
224                 if (mod->Vpp1 != mod->Vpp2) {
225                         dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
226                         ret = -EINVAL;
227                         goto unlock;
228                 }
229                 s->socket.Vpp = mod->Vpp1;
230                 if (s->ops->set_socket(s, &s->socket)) {
231                         dev_printk(KERN_WARNING, &s->dev,
232                                    "Unable to set VPP\n");
233                         ret = -EIO;
234                         goto unlock;
235                 }
236         } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
237                    (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
238                 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
239                 ret = -EINVAL;
240                 goto unlock;
241         }
242
243         if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
244                 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
245                 pccard_io_map io_on;
246                 int i;
247
248                 io_on.speed = io_speed;
249                 for (i = 0; i < MAX_IO_WIN; i++) {
250                         if (!s->io[i].res)
251                                 continue;
252                         io_off.map = i;
253                         io_on.map = i;
254
255                         io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
256                         io_on.start = s->io[i].res->start;
257                         io_on.stop = s->io[i].res->end;
258
259                         s->ops->set_io_map(s, &io_off);
260                         mdelay(40);
261                         s->ops->set_io_map(s, &io_on);
262                 }
263         }
264         ret = 0;
265 unlock:
266         mutex_unlock(&s->ops_mutex);
267
268         return ret;
269 } /* modify_configuration */
270 EXPORT_SYMBOL(pcmcia_modify_configuration);
271
272
273 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
274 {
275         pccard_io_map io = { 0, 0, 0, 0, 1 };
276         struct pcmcia_socket *s = p_dev->socket;
277         config_t *c;
278         int i;
279
280         mutex_lock(&s->ops_mutex);
281         c = p_dev->function_config;
282         if (p_dev->_locked) {
283                 p_dev->_locked = 0;
284                 if (--(s->lock_count) == 0) {
285                         s->socket.flags = SS_OUTPUT_ENA;   /* Is this correct? */
286                         s->socket.Vpp = 0;
287                         s->socket.io_irq = 0;
288                         s->ops->set_socket(s, &s->socket);
289                 }
290         }
291         if (c->state & CONFIG_LOCKED) {
292                 c->state &= ~CONFIG_LOCKED;
293                 if (c->state & CONFIG_IO_REQ)
294                         for (i = 0; i < MAX_IO_WIN; i++) {
295                                 if (!s->io[i].res)
296                                         continue;
297                                 s->io[i].Config--;
298                                 if (s->io[i].Config != 0)
299                                         continue;
300                                 io.map = i;
301                                 s->ops->set_io_map(s, &io);
302                         }
303         }
304         mutex_unlock(&s->ops_mutex);
305
306         return 0;
307 } /* pcmcia_release_configuration */
308
309
310 /** pcmcia_release_io
311  *
312  * Release_io() releases the I/O ranges allocated by a client.  This
313  * may be invoked some time after a card ejection has already dumped
314  * the actual socket configuration, so if the client is "stale", we
315  * don't bother checking the port ranges against the current socket
316  * values.
317  */
318 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
319 {
320         struct pcmcia_socket *s = p_dev->socket;
321         int ret = -EINVAL;
322         config_t *c;
323
324         mutex_lock(&s->ops_mutex);
325         c = p_dev->function_config;
326
327         if (!p_dev->_io)
328                 goto out;
329
330         p_dev->_io = 0;
331
332         if ((c->io.BasePort1 != req->BasePort1) ||
333             (c->io.NumPorts1 != req->NumPorts1) ||
334             (c->io.BasePort2 != req->BasePort2) ||
335             (c->io.NumPorts2 != req->NumPorts2))
336                 goto out;
337
338         c->state &= ~CONFIG_IO_REQ;
339
340         release_io_space(s, req->BasePort1, req->NumPorts1);
341         if (req->NumPorts2)
342                 release_io_space(s, req->BasePort2, req->NumPorts2);
343
344 out:
345         mutex_unlock(&s->ops_mutex);
346
347         return ret;
348 } /* pcmcia_release_io */
349
350
351 int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
352 {
353         struct pcmcia_socket *s = p_dev->socket;
354         pccard_mem_map *win;
355
356         wh--;
357         if (wh >= MAX_WIN)
358                 return -EINVAL;
359
360         mutex_lock(&s->ops_mutex);
361         win = &s->win[wh];
362
363         if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
364                 dev_dbg(&s->dev, "not releasing unknown window\n");
365                 mutex_unlock(&s->ops_mutex);
366                 return -EINVAL;
367         }
368
369         /* Shut down memory window */
370         win->flags &= ~MAP_ACTIVE;
371         s->ops->set_mem_map(s, win);
372         s->state &= ~SOCKET_WIN_REQ(wh);
373
374         /* Release system memory */
375         if (win->res) {
376                 release_resource(win->res);
377                 kfree(win->res);
378                 win->res = NULL;
379         }
380         p_dev->_win &= ~CLIENT_WIN_REQ(wh);
381         mutex_unlock(&s->ops_mutex);
382
383         return 0;
384 } /* pcmcia_release_window */
385 EXPORT_SYMBOL(pcmcia_release_window);
386
387
388 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
389                                  config_req_t *req)
390 {
391         int i;
392         u_int base;
393         struct pcmcia_socket *s = p_dev->socket;
394         config_t *c;
395         pccard_io_map iomap;
396
397         if (!(s->state & SOCKET_PRESENT))
398                 return -ENODEV;
399
400         if (req->IntType & INT_CARDBUS) {
401                 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
402                 return -EINVAL;
403         }
404
405         mutex_lock(&s->ops_mutex);
406         c = p_dev->function_config;
407         if (c->state & CONFIG_LOCKED) {
408                 mutex_unlock(&s->ops_mutex);
409                 dev_dbg(&s->dev, "Configuration is locked\n");
410                 return -EACCES;
411         }
412
413         /* Do power control.  We don't allow changes in Vcc. */
414         s->socket.Vpp = req->Vpp;
415         if (s->ops->set_socket(s, &s->socket)) {
416                 mutex_unlock(&s->ops_mutex);
417                 dev_printk(KERN_WARNING, &s->dev,
418                            "Unable to set socket state\n");
419                 return -EINVAL;
420         }
421
422         /* Pick memory or I/O card, DMA mode, interrupt */
423         c->IntType = req->IntType;
424         c->Attributes = req->Attributes;
425         if (req->IntType & INT_MEMORY_AND_IO)
426                 s->socket.flags |= SS_IOCARD;
427         if (req->IntType & INT_ZOOMED_VIDEO)
428                 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
429         if (req->Attributes & CONF_ENABLE_DMA)
430                 s->socket.flags |= SS_DMA_MODE;
431         if (req->Attributes & CONF_ENABLE_SPKR)
432                 s->socket.flags |= SS_SPKR_ENA;
433         if (req->Attributes & CONF_ENABLE_IRQ)
434                 s->socket.io_irq = s->pcmcia_irq;
435         else
436                 s->socket.io_irq = 0;
437         s->ops->set_socket(s, &s->socket);
438         s->lock_count++;
439         mutex_unlock(&s->ops_mutex);
440
441         /* Set up CIS configuration registers */
442         base = c->ConfigBase = req->ConfigBase;
443         c->CardValues = req->Present;
444         if (req->Present & PRESENT_COPY) {
445                 c->Copy = req->Copy;
446                 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
447         }
448         if (req->Present & PRESENT_OPTION) {
449                 if (s->functions == 1) {
450                         c->Option = req->ConfigIndex & COR_CONFIG_MASK;
451                 } else {
452                         c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
453                         c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
454                         if (req->Present & PRESENT_IOBASE_0)
455                                 c->Option |= COR_ADDR_DECODE;
456                 }
457                 if ((req->Attributes & CONF_ENABLE_IRQ) &&
458                         !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
459                         c->Option |= COR_LEVEL_REQ;
460                 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
461                 mdelay(40);
462         }
463         if (req->Present & PRESENT_STATUS) {
464                 c->Status = req->Status;
465                 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
466         }
467         if (req->Present & PRESENT_PIN_REPLACE) {
468                 c->Pin = req->Pin;
469                 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
470         }
471         if (req->Present & PRESENT_EXT_STATUS) {
472                 c->ExtStatus = req->ExtStatus;
473                 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
474         }
475         if (req->Present & PRESENT_IOBASE_0) {
476                 u_char b = c->io.BasePort1 & 0xff;
477                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
478                 b = (c->io.BasePort1 >> 8) & 0xff;
479                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
480         }
481         if (req->Present & PRESENT_IOSIZE) {
482                 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
483                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
484         }
485
486         /* Configure I/O windows */
487         if (c->state & CONFIG_IO_REQ) {
488                 mutex_lock(&s->ops_mutex);
489                 iomap.speed = io_speed;
490                 for (i = 0; i < MAX_IO_WIN; i++)
491                         if (s->io[i].res) {
492                                 iomap.map = i;
493                                 iomap.flags = MAP_ACTIVE;
494                                 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
495                                 case IO_DATA_PATH_WIDTH_16:
496                                         iomap.flags |= MAP_16BIT; break;
497                                 case IO_DATA_PATH_WIDTH_AUTO:
498                                         iomap.flags |= MAP_AUTOSZ; break;
499                                 default:
500                                         break;
501                                 }
502                                 iomap.start = s->io[i].res->start;
503                                 iomap.stop = s->io[i].res->end;
504                                 s->ops->set_io_map(s, &iomap);
505                                 s->io[i].Config++;
506                         }
507                 mutex_unlock(&s->ops_mutex);
508         }
509
510         c->state |= CONFIG_LOCKED;
511         p_dev->_locked = 1;
512         return 0;
513 } /* pcmcia_request_configuration */
514 EXPORT_SYMBOL(pcmcia_request_configuration);
515
516
517 /** pcmcia_request_io
518  *
519  * Request_io() reserves ranges of port addresses for a socket.
520  * I have not implemented range sharing or alias addressing.
521  */
522 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
523 {
524         struct pcmcia_socket *s = p_dev->socket;
525         config_t *c;
526         int ret = -EINVAL;
527
528         mutex_lock(&s->ops_mutex);
529
530         if (!(s->state & SOCKET_PRESENT)) {
531                 dev_dbg(&s->dev, "No card present\n");
532                 goto out;
533         }
534
535         if (!req)
536                 goto out;
537
538         c = p_dev->function_config;
539         if (c->state & CONFIG_LOCKED) {
540                 dev_dbg(&s->dev, "Configuration is locked\n");
541                 goto out;
542         }
543         if (c->state & CONFIG_IO_REQ) {
544                 dev_dbg(&s->dev, "IO already configured\n");
545                 goto out;
546         }
547         if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
548                 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
549                 goto out;
550         }
551         if ((req->NumPorts2 > 0) &&
552             (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
553                 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
554                 goto out;
555         }
556
557         dev_dbg(&s->dev, "trying to allocate resource 1\n");
558         ret = alloc_io_space(s, req->Attributes1, &req->BasePort1,
559                              req->NumPorts1, req->IOAddrLines);
560         if (ret) {
561                 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
562                 goto out;
563         }
564
565         if (req->NumPorts2) {
566                 dev_dbg(&s->dev, "trying to allocate resource 2\n");
567                 ret = alloc_io_space(s, req->Attributes2, &req->BasePort2,
568                                      req->NumPorts2, req->IOAddrLines);
569                 if (ret) {
570                         dev_dbg(&s->dev, "allocation of resource 2 failed\n");
571                         release_io_space(s, req->BasePort1, req->NumPorts1);
572                         goto out;
573                 }
574         }
575
576         c->io = *req;
577         c->state |= CONFIG_IO_REQ;
578         p_dev->_io = 1;
579         dev_dbg(&s->dev, "allocating resources succeeded: %d\n", ret);
580
581 out:
582         mutex_unlock(&s->ops_mutex);
583
584         return ret;
585 } /* pcmcia_request_io */
586 EXPORT_SYMBOL(pcmcia_request_io);
587
588
589 /**
590  * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
591  *
592  * pcmcia_request_irq() is a wrapper around request_irq which will allow
593  * the PCMCIA core to clean up the registration in pcmcia_disable_device().
594  * Drivers are free to use request_irq() directly, but then they need to
595  * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
596  * handlers are allowed.
597  */
598 int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
599                                     irq_handler_t handler)
600 {
601         int ret;
602
603         if (!p_dev->irq)
604                 return -EINVAL;
605
606         ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
607                         p_dev->devname, p_dev->priv);
608         if (!ret)
609                 p_dev->_irq = 1;
610
611         return ret;
612 }
613 EXPORT_SYMBOL(pcmcia_request_irq);
614
615
616 /**
617  * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
618  *
619  * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
620  * attempts first to request an exclusive IRQ. If it fails, it also accepts
621  * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
622  * IRQ sharing and either use request_irq directly (then they need to call
623  * free_irq themselves, too), or the pcmcia_request_irq() function.
624  */
625 int __must_check
626 __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
627                         irq_handler_t handler)
628 {
629         int ret;
630
631         if (!p_dev->irq)
632                 return -EINVAL;
633
634         ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
635         if (ret) {
636                 ret = pcmcia_request_irq(p_dev, handler);
637                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
638                         "request for exclusive IRQ could not be fulfilled.\n");
639                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
640                         "needs updating to supported shared IRQ lines.\n");
641         }
642         if (ret)
643                 dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
644         else
645                 p_dev->_irq = 1;
646
647         return ret;
648 } /* pcmcia_request_exclusive_irq */
649 EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
650
651
652 #ifdef CONFIG_PCMCIA_PROBE
653
654 /* mask of IRQs already reserved by other cards, we should avoid using them */
655 static u8 pcmcia_used_irq[NR_IRQS];
656
657 static irqreturn_t test_action(int cpl, void *dev_id)
658 {
659         return IRQ_NONE;
660 }
661
662 /**
663  * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
664  * @p_dev - the associated PCMCIA device
665  *
666  * locking note: must be called with ops_mutex locked.
667  */
668 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
669 {
670         struct pcmcia_socket *s = p_dev->socket;
671         unsigned int try, irq;
672         u32 mask = s->irq_mask;
673         int ret = -ENODEV;
674
675         for (try = 0; try < 64; try++) {
676                 irq = try % 32;
677
678                 /* marked as available by driver, not blocked by userspace? */
679                 if (!((mask >> irq) & 1))
680                         continue;
681
682                 /* avoid an IRQ which is already used by another PCMCIA card */
683                 if ((try < 32) && pcmcia_used_irq[irq])
684                         continue;
685
686                 /* register the correct driver, if possible, to check whether
687                  * registering a dummy handle works, i.e. if the IRQ isn't
688                  * marked as used by the kernel resource management core */
689                 ret = request_irq(irq, test_action, type, p_dev->devname,
690                                   p_dev);
691                 if (!ret) {
692                         free_irq(irq, p_dev);
693                         p_dev->irq = s->pcmcia_irq = irq;
694                         pcmcia_used_irq[irq]++;
695                         break;
696                 }
697         }
698
699         return ret;
700 }
701
702 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
703 {
704         pcmcia_used_irq[s->pcmcia_irq]--;
705         s->pcmcia_irq = 0;
706 }
707
708 #else /* CONFIG_PCMCIA_PROBE */
709
710 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
711 {
712         return -EINVAL;
713 }
714
715 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
716 {
717         s->pcmcia_irq = 0;
718         return;
719 }
720
721 #endif  /* CONFIG_PCMCIA_PROBE */
722
723
724 /**
725  * pcmcia_setup_irq() - determine IRQ to be used for device
726  * @p_dev - the associated PCMCIA device
727  *
728  * locking note: must be called with ops_mutex locked.
729  */
730 int pcmcia_setup_irq(struct pcmcia_device *p_dev)
731 {
732         struct pcmcia_socket *s = p_dev->socket;
733
734         if (p_dev->irq)
735                 return 0;
736
737         /* already assigned? */
738         if (s->pcmcia_irq) {
739                 p_dev->irq = s->pcmcia_irq;
740                 return 0;
741         }
742
743         /* prefer an exclusive ISA irq */
744         if (!pcmcia_setup_isa_irq(p_dev, 0))
745                 return 0;
746
747         /* but accept a shared ISA irq */
748         if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
749                 return 0;
750
751         /* but use the PCI irq otherwise */
752         if (s->pci_irq) {
753                 p_dev->irq = s->pcmcia_irq = s->pci_irq;
754                 return 0;
755         }
756
757         return -EINVAL;
758 }
759
760
761 /** pcmcia_request_window
762  *
763  * Request_window() establishes a mapping between card memory space
764  * and system memory space.
765  */
766 int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
767 {
768         struct pcmcia_socket *s = p_dev->socket;
769         pccard_mem_map *win;
770         u_long align;
771         int w;
772
773         if (!(s->state & SOCKET_PRESENT)) {
774                 dev_dbg(&s->dev, "No card present\n");
775                 return -ENODEV;
776         }
777         if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
778                 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
779                 return -EINVAL;
780         }
781
782         /* Window size defaults to smallest available */
783         if (req->Size == 0)
784                 req->Size = s->map_size;
785         align = (((s->features & SS_CAP_MEM_ALIGN) ||
786                   (req->Attributes & WIN_STRICT_ALIGN)) ?
787                  req->Size : s->map_size);
788         if (req->Size & (s->map_size-1)) {
789                 dev_dbg(&s->dev, "invalid map size\n");
790                 return -EINVAL;
791         }
792         if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
793             (req->Base & (align-1))) {
794                 dev_dbg(&s->dev, "invalid base address\n");
795                 return -EINVAL;
796         }
797         if (req->Base)
798                 align = 0;
799
800         /* Allocate system memory window */
801         for (w = 0; w < MAX_WIN; w++)
802                 if (!(s->state & SOCKET_WIN_REQ(w)))
803                         break;
804         if (w == MAX_WIN) {
805                 dev_dbg(&s->dev, "all windows are used already\n");
806                 return -EINVAL;
807         }
808
809         mutex_lock(&s->ops_mutex);
810         win = &s->win[w];
811
812         if (!(s->features & SS_CAP_STATIC_MAP)) {
813                 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
814                                                       (req->Attributes & WIN_MAP_BELOW_1MB), s);
815                 if (!win->res) {
816                         dev_dbg(&s->dev, "allocating mem region failed\n");
817                         mutex_unlock(&s->ops_mutex);
818                         return -EINVAL;
819                 }
820         }
821         p_dev->_win |= CLIENT_WIN_REQ(w);
822
823         /* Configure the socket controller */
824         win->map = w+1;
825         win->flags = 0;
826         win->speed = req->AccessSpeed;
827         if (req->Attributes & WIN_MEMORY_TYPE)
828                 win->flags |= MAP_ATTRIB;
829         if (req->Attributes & WIN_ENABLE)
830                 win->flags |= MAP_ACTIVE;
831         if (req->Attributes & WIN_DATA_WIDTH_16)
832                 win->flags |= MAP_16BIT;
833         if (req->Attributes & WIN_USE_WAIT)
834                 win->flags |= MAP_USE_WAIT;
835         win->card_start = 0;
836
837         if (s->ops->set_mem_map(s, win) != 0) {
838                 dev_dbg(&s->dev, "failed to set memory mapping\n");
839                 mutex_unlock(&s->ops_mutex);
840                 return -EIO;
841         }
842         s->state |= SOCKET_WIN_REQ(w);
843
844         /* Return window handle */
845         if (s->features & SS_CAP_STATIC_MAP)
846                 req->Base = win->static_start;
847         else
848                 req->Base = win->res->start;
849
850         mutex_unlock(&s->ops_mutex);
851         *wh = w + 1;
852
853         return 0;
854 } /* pcmcia_request_window */
855 EXPORT_SYMBOL(pcmcia_request_window);
856
857 void pcmcia_disable_device(struct pcmcia_device *p_dev)
858 {
859         pcmcia_release_configuration(p_dev);
860         pcmcia_release_io(p_dev, &p_dev->io);
861         if (p_dev->_irq)
862                 free_irq(p_dev->irq, p_dev->priv);
863         if (p_dev->win)
864                 pcmcia_release_window(p_dev, p_dev->win);
865 }
866 EXPORT_SYMBOL(pcmcia_disable_device);