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