Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * PCMCIA 16-bit resource management functions
0004  *
0005  * The initial developer of the original code is David A. Hinds
0006  * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
0007  * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
0008  *
0009  * Copyright (C) 1999        David A. Hinds
0010  * Copyright (C) 2004-2010   Dominik Brodowski
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/kernel.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/delay.h>
0017 #include <linux/pci.h>
0018 #include <linux/device.h>
0019 #include <linux/netdevice.h>
0020 #include <linux/slab.h>
0021 
0022 #include <asm/irq.h>
0023 
0024 #include <pcmcia/ss.h>
0025 #include <pcmcia/cistpl.h>
0026 #include <pcmcia/cisreg.h>
0027 #include <pcmcia/ds.h>
0028 
0029 #include "cs_internal.h"
0030 
0031 
0032 /* Access speed for IO windows */
0033 static int io_speed;
0034 module_param(io_speed, int, 0444);
0035 
0036 
0037 int pcmcia_validate_mem(struct pcmcia_socket *s)
0038 {
0039     if (s->resource_ops->validate_mem)
0040         return s->resource_ops->validate_mem(s);
0041     /* if there is no callback, we can assume that everything is OK */
0042     return 0;
0043 }
0044 
0045 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
0046                  int low, struct pcmcia_socket *s)
0047 {
0048     if (s->resource_ops->find_mem)
0049         return s->resource_ops->find_mem(base, num, align, low, s);
0050     return NULL;
0051 }
0052 
0053 
0054 /**
0055  * release_io_space() - release IO ports allocated with alloc_io_space()
0056  * @s: pcmcia socket
0057  * @res: resource to release
0058  *
0059  */
0060 static void release_io_space(struct pcmcia_socket *s, struct resource *res)
0061 {
0062     resource_size_t num = resource_size(res);
0063     int i;
0064 
0065     dev_dbg(&s->dev, "release_io_space for %pR\n", res);
0066 
0067     for (i = 0; i < MAX_IO_WIN; i++) {
0068         if (!s->io[i].res)
0069             continue;
0070         if ((s->io[i].res->start <= res->start) &&
0071             (s->io[i].res->end >= res->end)) {
0072             s->io[i].InUse -= num;
0073             if (res->parent)
0074                 release_resource(res);
0075             res->start = res->end = 0;
0076             res->flags = IORESOURCE_IO;
0077             /* Free the window if no one else is using it */
0078             if (s->io[i].InUse == 0) {
0079                 release_resource(s->io[i].res);
0080                 kfree(s->io[i].res);
0081                 s->io[i].res = NULL;
0082             }
0083         }
0084     }
0085 }
0086 
0087 
0088 /**
0089  * alloc_io_space() - allocate IO ports for use by a PCMCIA device
0090  * @s: pcmcia socket
0091  * @res: resource to allocate (begin: begin, end: size)
0092  * @lines: number of IO lines decoded by the PCMCIA card
0093  *
0094  * Special stuff for managing IO windows, because they are scarce
0095  */
0096 static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
0097             unsigned int lines)
0098 {
0099     unsigned int align;
0100     unsigned int base = res->start;
0101     unsigned int num = res->end;
0102     int ret;
0103 
0104     res->flags |= IORESOURCE_IO;
0105 
0106     dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
0107         res, lines);
0108 
0109     align = base ? (lines ? 1<<lines : 0) : 1;
0110     if (align && (align < num)) {
0111         if (base) {
0112             dev_dbg(&s->dev, "odd IO request\n");
0113             align = 0;
0114         } else
0115             while (align && (align < num))
0116                 align <<= 1;
0117     }
0118     if (base & ~(align-1)) {
0119         dev_dbg(&s->dev, "odd IO request\n");
0120         align = 0;
0121     }
0122 
0123     ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
0124                 &res->parent);
0125     if (ret) {
0126         dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
0127         return -EINVAL;
0128     }
0129 
0130     res->start = base;
0131     res->end = res->start + num - 1;
0132 
0133     if (res->parent) {
0134         ret = request_resource(res->parent, res);
0135         if (ret) {
0136             dev_warn(&s->dev,
0137                 "request_resource %pR failed: %d\n", res, ret);
0138             res->parent = NULL;
0139             release_io_space(s, res);
0140         }
0141     }
0142     dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
0143     return ret;
0144 }
0145 
0146 
0147 /*
0148  * pcmcia_access_config() - read or write card configuration registers
0149  *
0150  * pcmcia_access_config() reads and writes configuration registers in
0151  * attribute memory.  Memory window 0 is reserved for this and the tuple
0152  * reading services. Drivers must use pcmcia_read_config_byte() or
0153  * pcmcia_write_config_byte().
0154  */
0155 static int pcmcia_access_config(struct pcmcia_device *p_dev,
0156                 off_t where, u8 *val,
0157                 int (*accessf) (struct pcmcia_socket *s,
0158                         int attr, unsigned int addr,
0159                         unsigned int len, void *ptr))
0160 {
0161     struct pcmcia_socket *s;
0162     config_t *c;
0163     int addr;
0164     int ret = 0;
0165 
0166     s = p_dev->socket;
0167 
0168     mutex_lock(&s->ops_mutex);
0169     c = p_dev->function_config;
0170 
0171     if (!(c->state & CONFIG_LOCKED)) {
0172         dev_dbg(&p_dev->dev, "Configuration isn't locked\n");
0173         mutex_unlock(&s->ops_mutex);
0174         return -EACCES;
0175     }
0176 
0177     addr = (p_dev->config_base + where) >> 1;
0178 
0179     ret = accessf(s, 1, addr, 1, val);
0180 
0181     mutex_unlock(&s->ops_mutex);
0182 
0183     return ret;
0184 }
0185 
0186 
0187 /*
0188  * pcmcia_read_config_byte() - read a byte from a card configuration register
0189  *
0190  * pcmcia_read_config_byte() reads a byte from a configuration register in
0191  * attribute memory.
0192  */
0193 int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
0194 {
0195     return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
0196 }
0197 EXPORT_SYMBOL(pcmcia_read_config_byte);
0198 
0199 
0200 /*
0201  * pcmcia_write_config_byte() - write a byte to a card configuration register
0202  *
0203  * pcmcia_write_config_byte() writes a byte to a configuration register in
0204  * attribute memory.
0205  */
0206 int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
0207 {
0208     return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
0209 }
0210 EXPORT_SYMBOL(pcmcia_write_config_byte);
0211 
0212 
0213 /**
0214  * pcmcia_map_mem_page() - modify iomem window to point to a different offset
0215  * @p_dev: pcmcia device
0216  * @res: iomem resource already enabled by pcmcia_request_window()
0217  * @offset: card_offset to map
0218  *
0219  * pcmcia_map_mem_page() modifies what can be read and written by accessing
0220  * an iomem range previously enabled by pcmcia_request_window(), by setting
0221  * the card_offset value to @offset.
0222  */
0223 int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
0224             unsigned int offset)
0225 {
0226     struct pcmcia_socket *s = p_dev->socket;
0227     unsigned int w;
0228     int ret;
0229 
0230     w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
0231     if (w >= MAX_WIN)
0232         return -EINVAL;
0233 
0234     mutex_lock(&s->ops_mutex);
0235     s->win[w].card_start = offset;
0236     ret = s->ops->set_mem_map(s, &s->win[w]);
0237     if (ret)
0238         dev_warn(&p_dev->dev, "failed to set_mem_map\n");
0239     mutex_unlock(&s->ops_mutex);
0240     return ret;
0241 }
0242 EXPORT_SYMBOL(pcmcia_map_mem_page);
0243 
0244 
0245 /**
0246  * pcmcia_fixup_iowidth() - reduce io width to 8bit
0247  * @p_dev: pcmcia device
0248  *
0249  * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
0250  * IO width to 8bit after having called pcmcia_enable_device()
0251  * previously.
0252  */
0253 int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
0254 {
0255     struct pcmcia_socket *s = p_dev->socket;
0256     pccard_io_map io_off = { 0, 0, 0, 0, 1 };
0257     pccard_io_map io_on;
0258     int i, ret = 0;
0259 
0260     mutex_lock(&s->ops_mutex);
0261 
0262     dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
0263 
0264     if (!(s->state & SOCKET_PRESENT) ||
0265         !(p_dev->function_config->state & CONFIG_LOCKED)) {
0266         dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
0267         ret = -EACCES;
0268         goto unlock;
0269     }
0270 
0271     io_on.speed = io_speed;
0272     for (i = 0; i < MAX_IO_WIN; i++) {
0273         if (!s->io[i].res)
0274             continue;
0275         io_off.map = i;
0276         io_on.map = i;
0277 
0278         io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
0279         io_on.start = s->io[i].res->start;
0280         io_on.stop = s->io[i].res->end;
0281 
0282         s->ops->set_io_map(s, &io_off);
0283         msleep(40);
0284         s->ops->set_io_map(s, &io_on);
0285     }
0286 unlock:
0287     mutex_unlock(&s->ops_mutex);
0288 
0289     return ret;
0290 }
0291 EXPORT_SYMBOL(pcmcia_fixup_iowidth);
0292 
0293 
0294 /**
0295  * pcmcia_fixup_vpp() - set Vpp to a new voltage level
0296  * @p_dev: pcmcia device
0297  * @new_vpp: new Vpp voltage
0298  *
0299  * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
0300  * a new voltage level between calls to pcmcia_enable_device()
0301  * and pcmcia_disable_device().
0302  */
0303 int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
0304 {
0305     struct pcmcia_socket *s = p_dev->socket;
0306     int ret = 0;
0307 
0308     mutex_lock(&s->ops_mutex);
0309 
0310     dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
0311 
0312     if (!(s->state & SOCKET_PRESENT) ||
0313         !(p_dev->function_config->state & CONFIG_LOCKED)) {
0314         dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
0315         ret = -EACCES;
0316         goto unlock;
0317     }
0318 
0319     s->socket.Vpp = new_vpp;
0320     if (s->ops->set_socket(s, &s->socket)) {
0321         dev_warn(&p_dev->dev, "Unable to set VPP\n");
0322         ret = -EIO;
0323         goto unlock;
0324     }
0325     p_dev->vpp = new_vpp;
0326 
0327 unlock:
0328     mutex_unlock(&s->ops_mutex);
0329 
0330     return ret;
0331 }
0332 EXPORT_SYMBOL(pcmcia_fixup_vpp);
0333 
0334 
0335 /**
0336  * pcmcia_release_configuration() - physically disable a PCMCIA device
0337  * @p_dev: pcmcia device
0338  *
0339  * pcmcia_release_configuration() is the 1:1 counterpart to
0340  * pcmcia_enable_device(): If a PCMCIA device is no longer used by any
0341  * driver, the Vpp voltage is set to 0, IRQs will no longer be generated,
0342  * and I/O ranges will be disabled. As pcmcia_release_io() and
0343  * pcmcia_release_window() still need to be called, device drivers are
0344  * expected to call pcmcia_disable_device() instead.
0345  */
0346 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
0347 {
0348     pccard_io_map io = { 0, 0, 0, 0, 1 };
0349     struct pcmcia_socket *s = p_dev->socket;
0350     config_t *c;
0351     int i;
0352 
0353     mutex_lock(&s->ops_mutex);
0354     c = p_dev->function_config;
0355     if (p_dev->_locked) {
0356         p_dev->_locked = 0;
0357         if (--(s->lock_count) == 0) {
0358             s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
0359             s->socket.Vpp = 0;
0360             s->socket.io_irq = 0;
0361             s->ops->set_socket(s, &s->socket);
0362         }
0363     }
0364     if (c->state & CONFIG_LOCKED) {
0365         c->state &= ~CONFIG_LOCKED;
0366         if (c->state & CONFIG_IO_REQ)
0367             for (i = 0; i < MAX_IO_WIN; i++) {
0368                 if (!s->io[i].res)
0369                     continue;
0370                 s->io[i].Config--;
0371                 if (s->io[i].Config != 0)
0372                     continue;
0373                 io.map = i;
0374                 s->ops->set_io_map(s, &io);
0375             }
0376     }
0377     mutex_unlock(&s->ops_mutex);
0378 
0379     return 0;
0380 }
0381 
0382 
0383 /**
0384  * pcmcia_release_io() - release I/O allocated by a PCMCIA device
0385  * @p_dev: pcmcia device
0386  *
0387  * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA
0388  * device.  This may be invoked some time after a card ejection has
0389  * already dumped the actual socket configuration, so if the client is
0390  * "stale", we don't bother checking the port ranges against the
0391  * current socket values.
0392  */
0393 static void pcmcia_release_io(struct pcmcia_device *p_dev)
0394 {
0395     struct pcmcia_socket *s = p_dev->socket;
0396     config_t *c;
0397 
0398     mutex_lock(&s->ops_mutex);
0399     if (!p_dev->_io)
0400         goto out;
0401 
0402     c = p_dev->function_config;
0403 
0404     release_io_space(s, &c->io[0]);
0405 
0406     if (c->io[1].end)
0407         release_io_space(s, &c->io[1]);
0408 
0409     p_dev->_io = 0;
0410     c->state &= ~CONFIG_IO_REQ;
0411 
0412 out:
0413     mutex_unlock(&s->ops_mutex);
0414 } /* pcmcia_release_io */
0415 
0416 
0417 /**
0418  * pcmcia_release_window() - release reserved iomem for PCMCIA devices
0419  * @p_dev: pcmcia device
0420  * @res: iomem resource to release
0421  *
0422  * pcmcia_release_window() releases &struct resource *res which was
0423  * previously reserved by calling pcmcia_request_window().
0424  */
0425 int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
0426 {
0427     struct pcmcia_socket *s = p_dev->socket;
0428     pccard_mem_map *win;
0429     unsigned int w;
0430 
0431     dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
0432 
0433     w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
0434     if (w >= MAX_WIN)
0435         return -EINVAL;
0436 
0437     mutex_lock(&s->ops_mutex);
0438     win = &s->win[w];
0439 
0440     if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
0441         dev_dbg(&p_dev->dev, "not releasing unknown window\n");
0442         mutex_unlock(&s->ops_mutex);
0443         return -EINVAL;
0444     }
0445 
0446     /* Shut down memory window */
0447     win->flags &= ~MAP_ACTIVE;
0448     s->ops->set_mem_map(s, win);
0449     s->state &= ~SOCKET_WIN_REQ(w);
0450 
0451     /* Release system memory */
0452     if (win->res) {
0453         release_resource(res);
0454         release_resource(win->res);
0455         kfree(win->res);
0456         win->res = NULL;
0457     }
0458     res->start = res->end = 0;
0459     res->flags = IORESOURCE_MEM;
0460     p_dev->_win &= ~CLIENT_WIN_REQ(w);
0461     mutex_unlock(&s->ops_mutex);
0462 
0463     return 0;
0464 } /* pcmcia_release_window */
0465 EXPORT_SYMBOL(pcmcia_release_window);
0466 
0467 
0468 /**
0469  * pcmcia_enable_device() - set up and activate a PCMCIA device
0470  * @p_dev: the associated PCMCIA device
0471  *
0472  * pcmcia_enable_device() physically enables a PCMCIA device. It parses
0473  * the flags passed to in @flags and stored in @p_dev->flags and sets up
0474  * the Vpp voltage, enables the speaker line, I/O ports and store proper
0475  * values to configuration registers.
0476  */
0477 int pcmcia_enable_device(struct pcmcia_device *p_dev)
0478 {
0479     int i;
0480     unsigned int base;
0481     struct pcmcia_socket *s = p_dev->socket;
0482     config_t *c;
0483     pccard_io_map iomap;
0484     unsigned char status = 0;
0485     unsigned char ext_status = 0;
0486     unsigned char option = 0;
0487     unsigned int flags = p_dev->config_flags;
0488 
0489     if (!(s->state & SOCKET_PRESENT))
0490         return -ENODEV;
0491 
0492     mutex_lock(&s->ops_mutex);
0493     c = p_dev->function_config;
0494     if (c->state & CONFIG_LOCKED) {
0495         mutex_unlock(&s->ops_mutex);
0496         dev_dbg(&p_dev->dev, "Configuration is locked\n");
0497         return -EACCES;
0498     }
0499 
0500     /* Do power control.  We don't allow changes in Vcc. */
0501     s->socket.Vpp = p_dev->vpp;
0502     if (s->ops->set_socket(s, &s->socket)) {
0503         mutex_unlock(&s->ops_mutex);
0504         dev_warn(&p_dev->dev, "Unable to set socket state\n");
0505         return -EINVAL;
0506     }
0507 
0508     /* Pick memory or I/O card, DMA mode, interrupt */
0509     if (p_dev->_io || flags & CONF_ENABLE_IRQ)
0510         flags |= CONF_ENABLE_IOCARD;
0511     if (flags & CONF_ENABLE_IOCARD)
0512         s->socket.flags |= SS_IOCARD;
0513     if (flags & CONF_ENABLE_ZVCARD)
0514         s->socket.flags |= SS_ZVCARD | SS_IOCARD;
0515     if (flags & CONF_ENABLE_SPKR) {
0516         s->socket.flags |= SS_SPKR_ENA;
0517         status = CCSR_AUDIO_ENA;
0518         if (!(p_dev->config_regs & PRESENT_STATUS))
0519             dev_warn(&p_dev->dev, "speaker requested, but "
0520                           "PRESENT_STATUS not set!\n");
0521     }
0522     if (flags & CONF_ENABLE_IRQ)
0523         s->socket.io_irq = s->pcmcia_irq;
0524     else
0525         s->socket.io_irq = 0;
0526     if (flags & CONF_ENABLE_ESR) {
0527         p_dev->config_regs |= PRESENT_EXT_STATUS;
0528         ext_status = ESR_REQ_ATTN_ENA;
0529     }
0530     s->ops->set_socket(s, &s->socket);
0531     s->lock_count++;
0532 
0533     dev_dbg(&p_dev->dev,
0534         "enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
0535         p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs,
0536         p_dev->config_index);
0537 
0538     /* Set up CIS configuration registers */
0539     base = p_dev->config_base;
0540     if (p_dev->config_regs & PRESENT_COPY) {
0541         u16 tmp = 0;
0542         dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
0543         pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
0544     }
0545     if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
0546         u16 tmp = 0;
0547         dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
0548         pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
0549     }
0550     if (p_dev->config_regs & PRESENT_OPTION) {
0551         if (s->functions == 1) {
0552             option = p_dev->config_index & COR_CONFIG_MASK;
0553         } else {
0554             option = p_dev->config_index & COR_MFC_CONFIG_MASK;
0555             option |= COR_FUNC_ENA|COR_IREQ_ENA;
0556             if (p_dev->config_regs & PRESENT_IOBASE_0)
0557                 option |= COR_ADDR_DECODE;
0558         }
0559         if ((flags & CONF_ENABLE_IRQ) &&
0560             !(flags & CONF_ENABLE_PULSE_IRQ))
0561             option |= COR_LEVEL_REQ;
0562         pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
0563         msleep(40);
0564     }
0565     if (p_dev->config_regs & PRESENT_STATUS)
0566         pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
0567 
0568     if (p_dev->config_regs & PRESENT_EXT_STATUS)
0569         pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
0570                     &ext_status);
0571 
0572     if (p_dev->config_regs & PRESENT_IOBASE_0) {
0573         u8 b = c->io[0].start & 0xff;
0574         pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
0575         b = (c->io[0].start >> 8) & 0xff;
0576         pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
0577     }
0578     if (p_dev->config_regs & PRESENT_IOSIZE) {
0579         u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
0580         pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
0581     }
0582 
0583     /* Configure I/O windows */
0584     if (c->state & CONFIG_IO_REQ) {
0585         iomap.speed = io_speed;
0586         for (i = 0; i < MAX_IO_WIN; i++)
0587             if (s->io[i].res) {
0588                 iomap.map = i;
0589                 iomap.flags = MAP_ACTIVE;
0590                 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
0591                 case IO_DATA_PATH_WIDTH_16:
0592                     iomap.flags |= MAP_16BIT; break;
0593                 case IO_DATA_PATH_WIDTH_AUTO:
0594                     iomap.flags |= MAP_AUTOSZ; break;
0595                 default:
0596                     break;
0597                 }
0598                 iomap.start = s->io[i].res->start;
0599                 iomap.stop = s->io[i].res->end;
0600                 s->ops->set_io_map(s, &iomap);
0601                 s->io[i].Config++;
0602             }
0603     }
0604 
0605     c->state |= CONFIG_LOCKED;
0606     p_dev->_locked = 1;
0607     mutex_unlock(&s->ops_mutex);
0608     return 0;
0609 } /* pcmcia_enable_device */
0610 EXPORT_SYMBOL(pcmcia_enable_device);
0611 
0612 
0613 /**
0614  * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
0615  * @p_dev: the associated PCMCIA device
0616  *
0617  * pcmcia_request_io() attempts to reserve the IO port ranges specified in
0618  * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
0619  * "start" value is the requested start of the IO port resource; "end"
0620  * reflects the number of ports requested. The number of IO lines requested
0621  * is specified in &struct pcmcia_device @p_dev->io_lines.
0622  */
0623 int pcmcia_request_io(struct pcmcia_device *p_dev)
0624 {
0625     struct pcmcia_socket *s = p_dev->socket;
0626     config_t *c = p_dev->function_config;
0627     int ret = -EINVAL;
0628 
0629     mutex_lock(&s->ops_mutex);
0630     dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
0631         &c->io[0], &c->io[1]);
0632 
0633     if (!(s->state & SOCKET_PRESENT)) {
0634         dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
0635         goto out;
0636     }
0637 
0638     if (c->state & CONFIG_LOCKED) {
0639         dev_dbg(&p_dev->dev, "Configuration is locked\n");
0640         goto out;
0641     }
0642     if (c->state & CONFIG_IO_REQ) {
0643         dev_dbg(&p_dev->dev, "IO already configured\n");
0644         goto out;
0645     }
0646 
0647     ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
0648     if (ret)
0649         goto out;
0650 
0651     if (c->io[1].end) {
0652         ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
0653         if (ret) {
0654             struct resource tmp = c->io[0];
0655             /* release the previously allocated resource */
0656             release_io_space(s, &c->io[0]);
0657             /* but preserve the settings, for they worked... */
0658             c->io[0].end = resource_size(&tmp);
0659             c->io[0].start = tmp.start;
0660             c->io[0].flags = tmp.flags;
0661             goto out;
0662         }
0663     } else
0664         c->io[1].start = 0;
0665 
0666     c->state |= CONFIG_IO_REQ;
0667     p_dev->_io = 1;
0668 
0669     dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
0670         &c->io[0], &c->io[1]);
0671 out:
0672     mutex_unlock(&s->ops_mutex);
0673 
0674     return ret;
0675 } /* pcmcia_request_io */
0676 EXPORT_SYMBOL(pcmcia_request_io);
0677 
0678 
0679 /**
0680  * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
0681  * @p_dev: the associated PCMCIA device
0682  * @handler: IRQ handler to register
0683  *
0684  * pcmcia_request_irq() is a wrapper around request_irq() which allows
0685  * the PCMCIA core to clean up the registration in pcmcia_disable_device().
0686  * Drivers are free to use request_irq() directly, but then they need to
0687  * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ
0688  * handlers are allowed.
0689  */
0690 int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
0691                     irq_handler_t handler)
0692 {
0693     int ret;
0694 
0695     if (!p_dev->irq)
0696         return -EINVAL;
0697 
0698     ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
0699             p_dev->devname, p_dev->priv);
0700     if (!ret)
0701         p_dev->_irq = 1;
0702 
0703     return ret;
0704 }
0705 EXPORT_SYMBOL(pcmcia_request_irq);
0706 
0707 
0708 #ifdef CONFIG_PCMCIA_PROBE
0709 
0710 /* mask of IRQs already reserved by other cards, we should avoid using them */
0711 static u8 pcmcia_used_irq[32];
0712 
0713 static irqreturn_t test_action(int cpl, void *dev_id)
0714 {
0715     return IRQ_NONE;
0716 }
0717 
0718 /**
0719  * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
0720  * @p_dev: the associated PCMCIA device
0721  * @type:  IRQ type (flags)
0722  *
0723  * locking note: must be called with ops_mutex locked.
0724  */
0725 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
0726 {
0727     struct pcmcia_socket *s = p_dev->socket;
0728     unsigned int try, irq;
0729     u32 mask = s->irq_mask;
0730     int ret = -ENODEV;
0731 
0732     for (try = 0; try < 64; try++) {
0733         irq = try % 32;
0734 
0735         if (irq > NR_IRQS)
0736             continue;
0737 
0738         /* marked as available by driver, not blocked by userspace? */
0739         if (!((mask >> irq) & 1))
0740             continue;
0741 
0742         /* avoid an IRQ which is already used by another PCMCIA card */
0743         if ((try < 32) && pcmcia_used_irq[irq])
0744             continue;
0745 
0746         /* register the correct driver, if possible, to check whether
0747          * registering a dummy handle works, i.e. if the IRQ isn't
0748          * marked as used by the kernel resource management core */
0749         ret = request_irq(irq, test_action, type, p_dev->devname,
0750                   p_dev);
0751         if (!ret) {
0752             free_irq(irq, p_dev);
0753             p_dev->irq = s->pcmcia_irq = irq;
0754             pcmcia_used_irq[irq]++;
0755             break;
0756         }
0757     }
0758 
0759     return ret;
0760 }
0761 
0762 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
0763 {
0764     pcmcia_used_irq[s->pcmcia_irq]--;
0765     s->pcmcia_irq = 0;
0766 }
0767 
0768 #else /* CONFIG_PCMCIA_PROBE */
0769 
0770 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
0771 {
0772     return -EINVAL;
0773 }
0774 
0775 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
0776 {
0777     s->pcmcia_irq = 0;
0778     return;
0779 }
0780 
0781 #endif  /* CONFIG_PCMCIA_PROBE */
0782 
0783 
0784 /**
0785  * pcmcia_setup_irq() - determine IRQ to be used for device
0786  * @p_dev: the associated PCMCIA device
0787  *
0788  * locking note: must be called with ops_mutex locked.
0789  */
0790 int pcmcia_setup_irq(struct pcmcia_device *p_dev)
0791 {
0792     struct pcmcia_socket *s = p_dev->socket;
0793 
0794     if (p_dev->irq)
0795         return 0;
0796 
0797     /* already assigned? */
0798     if (s->pcmcia_irq) {
0799         p_dev->irq = s->pcmcia_irq;
0800         return 0;
0801     }
0802 
0803     /* prefer an exclusive ISA irq */
0804     if (!pcmcia_setup_isa_irq(p_dev, 0))
0805         return 0;
0806 
0807     /* but accept a shared ISA irq */
0808     if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
0809         return 0;
0810 
0811     /* but use the PCI irq otherwise */
0812     if (s->pci_irq) {
0813         p_dev->irq = s->pcmcia_irq = s->pci_irq;
0814         return 0;
0815     }
0816 
0817     return -EINVAL;
0818 }
0819 
0820 
0821 /**
0822  * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
0823  * @p_dev: the associated PCMCIA device
0824  * @res: &struct resource pointing to p_dev->resource[2..5]
0825  * @speed: access speed
0826  *
0827  * pcmcia_request_window() attepts to reserve an iomem ranges specified in
0828  * &struct resource @res pointing to one of the entries in
0829  * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
0830  * requested start of the IO mem resource; "end" reflects the size
0831  * requested.
0832  */
0833 int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
0834             unsigned int speed)
0835 {
0836     struct pcmcia_socket *s = p_dev->socket;
0837     pccard_mem_map *win;
0838     u_long align;
0839     int w;
0840 
0841     dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed);
0842 
0843     if (!(s->state & SOCKET_PRESENT)) {
0844         dev_dbg(&p_dev->dev, "No card present\n");
0845         return -ENODEV;
0846     }
0847 
0848     /* Window size defaults to smallest available */
0849     if (res->end == 0)
0850         res->end = s->map_size;
0851     align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
0852     if (res->end & (s->map_size-1)) {
0853         dev_dbg(&p_dev->dev, "invalid map size\n");
0854         return -EINVAL;
0855     }
0856     if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
0857         (res->start & (align-1))) {
0858         dev_dbg(&p_dev->dev, "invalid base address\n");
0859         return -EINVAL;
0860     }
0861     if (res->start)
0862         align = 0;
0863 
0864     /* Allocate system memory window */
0865     mutex_lock(&s->ops_mutex);
0866     for (w = 0; w < MAX_WIN; w++)
0867         if (!(s->state & SOCKET_WIN_REQ(w)))
0868             break;
0869     if (w == MAX_WIN) {
0870         dev_dbg(&p_dev->dev, "all windows are used already\n");
0871         mutex_unlock(&s->ops_mutex);
0872         return -EINVAL;
0873     }
0874 
0875     win = &s->win[w];
0876 
0877     if (!(s->features & SS_CAP_STATIC_MAP)) {
0878         win->res = pcmcia_find_mem_region(res->start, res->end, align,
0879                         0, s);
0880         if (!win->res) {
0881             dev_dbg(&p_dev->dev, "allocating mem region failed\n");
0882             mutex_unlock(&s->ops_mutex);
0883             return -EINVAL;
0884         }
0885     }
0886     p_dev->_win |= CLIENT_WIN_REQ(w);
0887 
0888     /* Configure the socket controller */
0889     win->map = w+1;
0890     win->flags = res->flags & WIN_FLAGS_MAP;
0891     win->speed = speed;
0892     win->card_start = 0;
0893 
0894     if (s->ops->set_mem_map(s, win) != 0) {
0895         dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
0896         mutex_unlock(&s->ops_mutex);
0897         return -EIO;
0898     }
0899     s->state |= SOCKET_WIN_REQ(w);
0900 
0901     /* Return window handle */
0902     if (s->features & SS_CAP_STATIC_MAP)
0903         res->start = win->static_start;
0904     else
0905         res->start = win->res->start;
0906 
0907     /* convert to new-style resources */
0908     res->end += res->start - 1;
0909     res->flags &= ~WIN_FLAGS_REQ;
0910     res->flags |= (win->map << 2) | IORESOURCE_MEM;
0911     res->parent = win->res;
0912     if (win->res)
0913         request_resource(&iomem_resource, res);
0914 
0915     dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
0916 
0917     mutex_unlock(&s->ops_mutex);
0918 
0919     return 0;
0920 } /* pcmcia_request_window */
0921 EXPORT_SYMBOL(pcmcia_request_window);
0922 
0923 
0924 /**
0925  * pcmcia_disable_device() - disable and clean up a PCMCIA device
0926  * @p_dev: the associated PCMCIA device
0927  *
0928  * pcmcia_disable_device() is the driver-callable counterpart to
0929  * pcmcia_enable_device(): If a PCMCIA device is no longer used,
0930  * drivers are expected to clean up and disable the device by calling
0931  * this function. Any I/O ranges (iomem and ioports) will be released,
0932  * the Vpp voltage will be set to 0, and IRQs will no longer be
0933  * generated -- at least if there is no other card function (of
0934  * multifunction devices) being used.
0935  */
0936 void pcmcia_disable_device(struct pcmcia_device *p_dev)
0937 {
0938     int i;
0939 
0940     dev_dbg(&p_dev->dev, "disabling device\n");
0941 
0942     for (i = 0; i < MAX_WIN; i++) {
0943         struct resource *res = p_dev->resource[MAX_IO_WIN + i];
0944         if (res->flags & WIN_FLAGS_REQ)
0945             pcmcia_release_window(p_dev, res);
0946     }
0947 
0948     pcmcia_release_configuration(p_dev);
0949     pcmcia_release_io(p_dev);
0950     if (p_dev->_irq) {
0951         free_irq(p_dev->irq, p_dev->priv);
0952         p_dev->_irq = 0;
0953     }
0954 }
0955 EXPORT_SYMBOL(pcmcia_disable_device);