[PATCH] pcmcia: add pcmcia_disable_device
[firefly-linux-kernel-4.4.55.git] / drivers / pcmcia / pcmcia_resource.c
index 599b116d9747211514a6522625e6830756f0d492..555c8698ebd904ea6831c0eb5dce754370047c8f 100644 (file)
@@ -14,7 +14,6 @@
  *
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/interrupt.h>
@@ -89,7 +88,7 @@ static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
        }
        if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
                *base = s->io_offset | (*base & 0x0fff);
-               s->io[0].Attributes = attr;
+               s->io[0].res->flags = (s->io[0].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
                return 0;
        }
        /* Check for an already-allocated window that must conflict with
@@ -97,38 +96,36 @@ static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
         * potential conflicts, just the most obvious ones.
         */
        for (i = 0; i < MAX_IO_WIN; i++)
-               if ((s->io[i].NumPorts != 0) &&
-                   ((s->io[i].BasePort & (align-1)) == *base))
+               if ((s->io[i].res) &&
+                   ((s->io[i].res->start & (align-1)) == *base))
                        return 1;
        for (i = 0; i < MAX_IO_WIN; i++) {
-               if (s->io[i].NumPorts == 0) {
+               if (!s->io[i].res) {
                        s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
                        if (s->io[i].res) {
-                               s->io[i].Attributes = attr;
-                               s->io[i].BasePort = *base = s->io[i].res->start;
-                               s->io[i].NumPorts = s->io[i].InUse = num;
+                               *base = s->io[i].res->start;
+                               s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
+                               s->io[i].InUse = num;
                                break;
                        } else
                                return 1;
-               } else if (s->io[i].Attributes != attr)
+               } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
                        continue;
                /* Try to extend top of window */
-               try = s->io[i].BasePort + s->io[i].NumPorts;
+               try = s->io[i].res->end + 1;
                if ((*base == 0) || (*base == try))
                        if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
                                                    s->io[i].res->end + num, s) == 0) {
                                *base = try;
-                               s->io[i].NumPorts += num;
                                s->io[i].InUse += num;
                                break;
                        }
                /* Try to extend bottom of window */
-               try = s->io[i].BasePort - num;
+               try = s->io[i].res->start - num;
                if ((*base == 0) || (*base == try))
                        if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
                                                    s->io[i].res->end, s) == 0) {
-                               s->io[i].BasePort = *base = try;
-                               s->io[i].NumPorts += num;
+                               *base = try;
                                s->io[i].InUse += num;
                                break;
                        }
@@ -143,12 +140,13 @@ static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
        int i;
 
        for (i = 0; i < MAX_IO_WIN; i++) {
-               if ((s->io[i].BasePort <= base) &&
-                   (s->io[i].BasePort+s->io[i].NumPorts >= base+num)) {
+               if (!s->io[i].res)
+                       continue;
+               if ((s->io[i].res->start <= base) &&
+                   (s->io[i].res->end >= base+num-1)) {
                        s->io[i].InUse -= num;
                        /* Free the window if no one else is using it */
                        if (s->io[i].InUse == 0) {
-                               s->io[i].NumPorts = 0;
                                release_resource(s->io[i].res);
                                kfree(s->io[i].res);
                                s->io[i].res = NULL;
@@ -165,21 +163,19 @@ static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
  * this and the tuple reading services.
  */
 
-int pccard_access_configuration_register(struct pcmcia_socket *s,
-                                        unsigned int function,
+int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
                                         conf_reg_t *reg)
 {
+       struct pcmcia_socket *s;
        config_t *c;
        int addr;
        u_char val;
 
-       if (!s || !s->config)
+       if (!p_dev || !p_dev->function_config)
                return CS_NO_CARD;
 
-       c = &s->config[function];
-
-       if (c == NULL)
-               return CS_NO_CARD;
+       s = p_dev->socket;
+       c = p_dev->function_config;
 
        if (!(c->state & CONFIG_LOCKED))
                return CS_CONFIGURATION_LOCKED;
@@ -200,20 +196,12 @@ int pccard_access_configuration_register(struct pcmcia_socket *s,
                break;
        }
        return CS_SUCCESS;
-} /* pccard_access_configuration_register */
-
-int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
-                                        conf_reg_t *reg)
-{
-       return pccard_access_configuration_register(p_dev->socket,
-                                                   p_dev->func, reg);
-}
+} /* pcmcia_access_configuration_register */
 EXPORT_SYMBOL(pcmcia_access_configuration_register);
 
 
-
 int pccard_get_configuration_info(struct pcmcia_socket *s,
-                                 unsigned int function,
+                                 struct pcmcia_device *p_dev,
                                  config_info_t *config)
 {
        config_t *c;
@@ -221,7 +209,7 @@ int pccard_get_configuration_info(struct pcmcia_socket *s,
        if (!(s->state & SOCKET_PRESENT))
                return CS_NO_CARD;
 
-       config->Function = function;
+       config->Function = p_dev->func;
 
 #ifdef CONFIG_CARDBUS
        if (s->state & SOCKET_CARDBUS) {
@@ -235,14 +223,14 @@ int pccard_get_configuration_info(struct pcmcia_socket *s,
                        config->AssignedIRQ = s->irq.AssignedIRQ;
                        if (config->AssignedIRQ)
                                config->Attributes |= CONF_ENABLE_IRQ;
-                       config->BasePort1 = s->io[0].BasePort;
-                       config->NumPorts1 = s->io[0].NumPorts;
+                       config->BasePort1 = s->io[0].res->start;
+                       config->NumPorts1 = s->io[0].res->end - config->BasePort1 + 1;
                }
                return CS_SUCCESS;
        }
 #endif
 
-       c = (s->config != NULL) ? &s->config[function] : NULL;
+       c = (p_dev) ? p_dev->function_config : NULL;
 
        if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
                config->Attributes = 0;
@@ -271,7 +259,7 @@ int pccard_get_configuration_info(struct pcmcia_socket *s,
 int pcmcia_get_configuration_info(struct pcmcia_device *p_dev,
                                  config_info_t *config)
 {
-       return pccard_get_configuration_info(p_dev->socket, p_dev->func,
+       return pccard_get_configuration_info(p_dev->socket, p_dev,
                                             config);
 }
 EXPORT_SYMBOL(pcmcia_get_configuration_info);
@@ -317,7 +305,7 @@ EXPORT_SYMBOL(pcmcia_get_window);
  * SocketState yet: I haven't seen any point for it.
  */
 
-int pccard_get_status(struct pcmcia_socket *s, unsigned int function,
+int pccard_get_status(struct pcmcia_socket *s, struct pcmcia_device *p_dev,
                      cs_status_t *status)
 {
        config_t *c;
@@ -334,11 +322,12 @@ int pccard_get_status(struct pcmcia_socket *s, unsigned int function,
        if (!(s->state & SOCKET_PRESENT))
                return CS_NO_CARD;
 
-       c = (s->config != NULL) ? &s->config[function] : NULL;
+       c = (p_dev) ? p_dev->function_config : NULL;
+
        if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
            (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
                u_char reg;
-               if (c->Present & PRESENT_PIN_REPLACE) {
+               if (c->CardValues & PRESENT_PIN_REPLACE) {
                        pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, &reg);
                        status->CardState |=
                                (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
@@ -352,7 +341,7 @@ int pccard_get_status(struct pcmcia_socket *s, unsigned int function,
                        /* No PRR?  Then assume we're always ready */
                        status->CardState |= CS_EVENT_READY_CHANGE;
                }
-               if (c->Present & PRESENT_EXT_STATUS) {
+               if (c->CardValues & PRESENT_EXT_STATUS) {
                        pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, &reg);
                        status->CardState |=
                                (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
@@ -370,11 +359,9 @@ int pccard_get_status(struct pcmcia_socket *s, unsigned int function,
        return CS_SUCCESS;
 } /* pccard_get_status */
 
-int pcmcia_get_status(client_handle_t handle, cs_status_t *status)
+int pcmcia_get_status(struct pcmcia_device *p_dev, cs_status_t *status)
 {
-       struct pcmcia_socket *s;
-       s = SOCKET(handle);
-       return pccard_get_status(s, handle->func, status);
+       return pccard_get_status(p_dev->socket, p_dev, status);
 }
 EXPORT_SYMBOL(pcmcia_get_status);
 
@@ -422,7 +409,8 @@ int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
        config_t *c;
 
        s = p_dev->socket;
-       c = CONFIG(p_dev);
+       c = p_dev->function_config;
+
        if (!(s->state & SOCKET_PRESENT))
                return CS_NO_CARD;
        if (!(c->state & CONFIG_LOCKED))
@@ -447,7 +435,7 @@ int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
            (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
                if (mod->Vpp1 != mod->Vpp2)
                        return CS_BAD_VPP;
-               c->Vpp1 = c->Vpp2 = s->socket.Vpp = mod->Vpp1;
+               s->socket.Vpp = mod->Vpp1;
                if (s->ops->set_socket(s, &s->socket))
                        return CS_BAD_VPP;
        } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
@@ -463,23 +451,23 @@ int pcmcia_release_configuration(struct pcmcia_device *p_dev)
 {
        pccard_io_map io = { 0, 0, 0, 0, 1 };
        struct pcmcia_socket *s = p_dev->socket;
+       config_t *c = p_dev->function_config;
        int i;
 
-       if (!(p_dev->state & CLIENT_CONFIG_LOCKED))
-               return CS_BAD_HANDLE;
-       p_dev->state &= ~CLIENT_CONFIG_LOCKED;
-
-       if (!(p_dev->state & CLIENT_STALE)) {
-               config_t *c = CONFIG(p_dev);
+       if (p_dev->state & CLIENT_CONFIG_LOCKED) {
+               p_dev->state &= ~CLIENT_CONFIG_LOCKED;
                if (--(s->lock_count) == 0) {
                        s->socket.flags = SS_OUTPUT_ENA;   /* Is this correct? */
                        s->socket.Vpp = 0;
                        s->socket.io_irq = 0;
                        s->ops->set_socket(s, &s->socket);
                }
+       }
+       if (c->state & CONFIG_LOCKED) {
+               c->state &= ~CONFIG_LOCKED;
                if (c->state & CONFIG_IO_REQ)
                        for (i = 0; i < MAX_IO_WIN; i++) {
-                               if (s->io[i].NumPorts == 0)
+                               if (!s->io[i].res)
                                        continue;
                                s->io[i].Config--;
                                if (s->io[i].Config != 0)
@@ -487,7 +475,6 @@ int pcmcia_release_configuration(struct pcmcia_device *p_dev)
                                io.map = i;
                                s->ops->set_io_map(s, &io);
                        }
-               c->state &= ~CONFIG_LOCKED;
        }
 
        return CS_SUCCESS;
@@ -506,22 +493,20 @@ EXPORT_SYMBOL(pcmcia_release_configuration);
 int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
 {
        struct pcmcia_socket *s = p_dev->socket;
+       config_t *c = p_dev->function_config;
 
        if (!(p_dev->state & CLIENT_IO_REQ))
                return CS_BAD_HANDLE;
+
        p_dev->state &= ~CLIENT_IO_REQ;
 
-       if (!(p_dev->state & CLIENT_STALE)) {
-               config_t *c = CONFIG(p_dev);
-               if (c->state & CONFIG_LOCKED)
-                       return CS_CONFIGURATION_LOCKED;
-               if ((c->io.BasePort1 != req->BasePort1) ||
-                   (c->io.NumPorts1 != req->NumPorts1) ||
-                   (c->io.BasePort2 != req->BasePort2) ||
-                   (c->io.NumPorts2 != req->NumPorts2))
-                       return CS_BAD_ARGS;
-               c->state &= ~CONFIG_IO_REQ;
-       }
+       if ((c->io.BasePort1 != req->BasePort1) ||
+           (c->io.NumPorts1 != req->NumPorts1) ||
+           (c->io.BasePort2 != req->BasePort2) ||
+           (c->io.NumPorts2 != req->NumPorts2))
+               return CS_BAD_ARGS;
+
+       c->state &= ~CONFIG_IO_REQ;
 
        release_io_space(s, req->BasePort1, req->NumPorts1);
        if (req->NumPorts2)
@@ -535,22 +520,21 @@ EXPORT_SYMBOL(pcmcia_release_io);
 int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
 {
        struct pcmcia_socket *s = p_dev->socket;
+       config_t *c= p_dev->function_config;
+
        if (!(p_dev->state & CLIENT_IRQ_REQ))
                return CS_BAD_HANDLE;
        p_dev->state &= ~CLIENT_IRQ_REQ;
 
-       if (!(p_dev->state & CLIENT_STALE)) {
-               config_t *c = CONFIG(p_dev);
-               if (c->state & CONFIG_LOCKED)
-                       return CS_CONFIGURATION_LOCKED;
-               if (c->irq.Attributes != req->Attributes)
-                       return CS_BAD_ATTRIBUTE;
-               if (s->irq.AssignedIRQ != req->AssignedIRQ)
-                       return CS_BAD_IRQ;
-               if (--s->irq.Config == 0) {
-                       c->state &= ~CONFIG_IRQ_REQ;
-                       s->irq.AssignedIRQ = 0;
-               }
+       if (c->state & CONFIG_LOCKED)
+               return CS_CONFIGURATION_LOCKED;
+       if (c->irq.Attributes != req->Attributes)
+               return CS_BAD_ATTRIBUTE;
+       if (s->irq.AssignedIRQ != req->AssignedIRQ)
+               return CS_BAD_IRQ;
+       if (--s->irq.Config == 0) {
+               c->state &= ~CONFIG_IRQ_REQ;
+               s->irq.AssignedIRQ = 0;
        }
 
        if (req->Attributes & IRQ_HANDLE_PRESENT) {
@@ -610,7 +594,7 @@ int pcmcia_request_configuration(struct pcmcia_device *p_dev,
 
        if (req->IntType & INT_CARDBUS)
                return CS_UNSUPPORTED_MODE;
-       c = CONFIG(p_dev);
+       c = p_dev->function_config;
        if (c->state & CONFIG_LOCKED)
                return CS_CONFIGURATION_LOCKED;
 
@@ -623,8 +607,6 @@ int pcmcia_request_configuration(struct pcmcia_device *p_dev,
        if (s->ops->set_socket(s, &s->socket))
                return CS_BAD_VPP;
 
-       c->Vcc = req->Vcc; c->Vpp1 = c->Vpp2 = req->Vpp1;
-
        /* Pick memory or I/O card, DMA mode, interrupt */
        c->IntType = req->IntType;
        c->Attributes = req->Attributes;
@@ -645,7 +627,7 @@ int pcmcia_request_configuration(struct pcmcia_device *p_dev,
 
        /* Set up CIS configuration registers */
        base = c->ConfigBase = req->ConfigBase;
-       c->Present = c->CardValues = req->Present;
+       c->CardValues = req->Present;
        if (req->Present & PRESENT_COPY) {
                c->Copy = req->Copy;
                pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
@@ -692,10 +674,10 @@ int pcmcia_request_configuration(struct pcmcia_device *p_dev,
        if (c->state & CONFIG_IO_REQ) {
                iomap.speed = io_speed;
                for (i = 0; i < MAX_IO_WIN; i++)
-                       if (s->io[i].NumPorts != 0) {
+                       if (s->io[i].res) {
                                iomap.map = i;
                                iomap.flags = MAP_ACTIVE;
-                               switch (s->io[i].Attributes & IO_DATA_PATH_WIDTH) {
+                               switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
                                case IO_DATA_PATH_WIDTH_16:
                                        iomap.flags |= MAP_16BIT; break;
                                case IO_DATA_PATH_WIDTH_AUTO:
@@ -703,8 +685,8 @@ int pcmcia_request_configuration(struct pcmcia_device *p_dev,
                                default:
                                        break;
                                }
-                               iomap.start = s->io[i].BasePort;
-                               iomap.stop = iomap.start + s->io[i].NumPorts - 1;
+                               iomap.start = s->io[i].res->start;
+                               iomap.stop = s->io[i].res->end;
                                s->ops->set_io_map(s, &iomap);
                                s->io[i].Config++;
                        }
@@ -732,7 +714,7 @@ int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
 
        if (!req)
                return CS_UNSUPPORTED_MODE;
-       c = CONFIG(p_dev);
+       c = p_dev->function_config;
        if (c->state & CONFIG_LOCKED)
                return CS_CONFIGURATION_LOCKED;
        if (c->state & CONFIG_IO_REQ)
@@ -788,7 +770,7 @@ int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
 
        if (!(s->state & SOCKET_PRESENT))
                return CS_NO_CARD;
-       c = CONFIG(p_dev);
+       c = p_dev->function_config;
        if (c->state & CONFIG_LOCKED)
                return CS_CONFIGURATION_LOCKED;
        if (c->state & CONFIG_IRQ_REQ)
@@ -822,7 +804,7 @@ int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
                                          ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
                                           (s->functions > 1) ||
                                           (irq == s->pci_irq)) ? SA_SHIRQ : 0,
-                                         p_dev->dev.bus_id,
+                                         p_dev->devname,
                                          (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
                        if (!ret) {
                                if (!(req->Attributes & IRQ_HANDLE_PRESENT))
@@ -832,7 +814,8 @@ int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
                }
        }
 #endif
-       if (ret) {
+       /* only assign PCI irq if no IRQ already assigned */
+       if (ret && !s->irq.AssignedIRQ) {
                if (!s->pci_irq)
                        return ret;
                irq = s->pci_irq;
@@ -843,7 +826,7 @@ int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
                                ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
                                 (s->functions > 1) ||
                                 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
-                               p_dev->dev.bus_id, req->Instance))
+                               p_dev->devname, req->Instance))
                        return CS_IN_USE;
        }
 
@@ -942,3 +925,18 @@ int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_h
        return CS_SUCCESS;
 } /* pcmcia_request_window */
 EXPORT_SYMBOL(pcmcia_request_window);
+
+void pcmcia_disable_device(struct pcmcia_device *p_dev) {
+       if (!p_dev->instance)
+               return;
+
+       pcmcia_release_configuration(p_dev);
+       pcmcia_release_io(p_dev, &p_dev->instance->io);
+       pcmcia_release_irq(p_dev, &p_dev->instance->irq);
+       if (&p_dev->instance->win)
+               pcmcia_release_window(p_dev->instance->win);
+
+       p_dev->instance->dev = NULL;
+       p_dev->instance->state &= ~DEV_CONFIG;
+}
+EXPORT_SYMBOL(pcmcia_disable_device);