Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ds.c -- 16-bit PCMCIA core support
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  * (C) 1999     David A. Hinds
0010  * (C) 2003 - 2010  Dominik Brodowski
0011  */
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/errno.h>
0017 #include <linux/list.h>
0018 #include <linux/delay.h>
0019 #include <linux/workqueue.h>
0020 #include <linux/crc32.h>
0021 #include <linux/firmware.h>
0022 #include <linux/kref.h>
0023 #include <linux/dma-mapping.h>
0024 #include <linux/slab.h>
0025 
0026 #include <pcmcia/cistpl.h>
0027 #include <pcmcia/ds.h>
0028 #include <pcmcia/ss.h>
0029 
0030 #include "cs_internal.h"
0031 
0032 /*====================================================================*/
0033 
0034 /* Module parameters */
0035 
0036 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
0037 MODULE_DESCRIPTION("PCMCIA Driver Services");
0038 MODULE_LICENSE("GPL");
0039 
0040 
0041 /*====================================================================*/
0042 
0043 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
0044 {
0045     const struct pcmcia_device_id *did = p_drv->id_table;
0046     unsigned int i;
0047     u32 hash;
0048 
0049     if (!p_drv->probe || !p_drv->remove)
0050         printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
0051                "function\n", p_drv->name);
0052 
0053     while (did && did->match_flags) {
0054         for (i = 0; i < 4; i++) {
0055             if (!did->prod_id[i])
0056                 continue;
0057 
0058             hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
0059             if (hash == did->prod_id_hash[i])
0060                 continue;
0061 
0062             printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
0063                    "product string \"%s\": is 0x%x, should "
0064                    "be 0x%x\n", p_drv->name, did->prod_id[i],
0065                    did->prod_id_hash[i], hash);
0066             printk(KERN_DEBUG "pcmcia: see "
0067                 "Documentation/pcmcia/devicetable.rst for "
0068                 "details\n");
0069         }
0070         did++;
0071     }
0072 
0073     return;
0074 }
0075 
0076 
0077 /*======================================================================*/
0078 
0079 
0080 struct pcmcia_dynid {
0081     struct list_head        node;
0082     struct pcmcia_device_id     id;
0083 };
0084 
0085 /**
0086  * new_id_store() - add a new PCMCIA device ID to this driver and re-probe devices
0087  * @driver: target device driver
0088  * @buf: buffer for scanning device ID data
0089  * @count: input size
0090  *
0091  * Adds a new dynamic PCMCIA device ID to this driver,
0092  * and causes the driver to probe for all devices again.
0093  */
0094 static ssize_t
0095 new_id_store(struct device_driver *driver, const char *buf, size_t count)
0096 {
0097     struct pcmcia_dynid *dynid;
0098     struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
0099     __u16 match_flags, manf_id, card_id;
0100     __u8 func_id, function, device_no;
0101     __u32 prod_id_hash[4] = {0, 0, 0, 0};
0102     int fields = 0;
0103     int retval = 0;
0104 
0105     fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
0106             &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
0107             &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
0108     if (fields < 6)
0109         return -EINVAL;
0110 
0111     dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
0112     if (!dynid)
0113         return -ENOMEM;
0114 
0115     dynid->id.match_flags = match_flags;
0116     dynid->id.manf_id = manf_id;
0117     dynid->id.card_id = card_id;
0118     dynid->id.func_id = func_id;
0119     dynid->id.function = function;
0120     dynid->id.device_no = device_no;
0121     memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
0122 
0123     mutex_lock(&pdrv->dynids.lock);
0124     list_add_tail(&dynid->node, &pdrv->dynids.list);
0125     mutex_unlock(&pdrv->dynids.lock);
0126 
0127     retval = driver_attach(&pdrv->drv);
0128 
0129     if (retval)
0130         return retval;
0131     return count;
0132 }
0133 static DRIVER_ATTR_WO(new_id);
0134 
0135 static void
0136 pcmcia_free_dynids(struct pcmcia_driver *drv)
0137 {
0138     struct pcmcia_dynid *dynid, *n;
0139 
0140     mutex_lock(&drv->dynids.lock);
0141     list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
0142         list_del(&dynid->node);
0143         kfree(dynid);
0144     }
0145     mutex_unlock(&drv->dynids.lock);
0146 }
0147 
0148 static int
0149 pcmcia_create_newid_file(struct pcmcia_driver *drv)
0150 {
0151     int error = 0;
0152     if (drv->probe != NULL)
0153         error = driver_create_file(&drv->drv, &driver_attr_new_id);
0154     return error;
0155 }
0156 
0157 static void
0158 pcmcia_remove_newid_file(struct pcmcia_driver *drv)
0159 {
0160     driver_remove_file(&drv->drv, &driver_attr_new_id);
0161 }
0162 
0163 /**
0164  * pcmcia_register_driver - register a PCMCIA driver with the bus core
0165  * @driver: the &driver being registered
0166  *
0167  * Registers a PCMCIA driver with the PCMCIA bus core.
0168  */
0169 int pcmcia_register_driver(struct pcmcia_driver *driver)
0170 {
0171     int error;
0172 
0173     if (!driver)
0174         return -EINVAL;
0175 
0176     pcmcia_check_driver(driver);
0177 
0178     /* initialize common fields */
0179     driver->drv.bus = &pcmcia_bus_type;
0180     driver->drv.owner = driver->owner;
0181     driver->drv.name = driver->name;
0182     mutex_init(&driver->dynids.lock);
0183     INIT_LIST_HEAD(&driver->dynids.list);
0184 
0185     pr_debug("registering driver %s\n", driver->name);
0186 
0187     error = driver_register(&driver->drv);
0188     if (error < 0)
0189         return error;
0190 
0191     error = pcmcia_create_newid_file(driver);
0192     if (error)
0193         driver_unregister(&driver->drv);
0194 
0195     return error;
0196 }
0197 EXPORT_SYMBOL(pcmcia_register_driver);
0198 
0199 /**
0200  * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
0201  * @driver: the &driver being unregistered
0202  */
0203 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
0204 {
0205     pr_debug("unregistering driver %s\n", driver->name);
0206     pcmcia_remove_newid_file(driver);
0207     driver_unregister(&driver->drv);
0208     pcmcia_free_dynids(driver);
0209 }
0210 EXPORT_SYMBOL(pcmcia_unregister_driver);
0211 
0212 
0213 /* pcmcia_device handling */
0214 
0215 static struct pcmcia_device *pcmcia_get_dev(struct pcmcia_device *p_dev)
0216 {
0217     struct device *tmp_dev;
0218     tmp_dev = get_device(&p_dev->dev);
0219     if (!tmp_dev)
0220         return NULL;
0221     return to_pcmcia_dev(tmp_dev);
0222 }
0223 
0224 static void pcmcia_put_dev(struct pcmcia_device *p_dev)
0225 {
0226     if (p_dev)
0227         put_device(&p_dev->dev);
0228 }
0229 
0230 static void pcmcia_release_function(struct kref *ref)
0231 {
0232     struct config_t *c = container_of(ref, struct config_t, ref);
0233     pr_debug("releasing config_t\n");
0234     kfree(c);
0235 }
0236 
0237 static void pcmcia_release_dev(struct device *dev)
0238 {
0239     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
0240     int i;
0241     dev_dbg(dev, "releasing device\n");
0242     pcmcia_put_socket(p_dev->socket);
0243     for (i = 0; i < 4; i++)
0244         kfree(p_dev->prod_id[i]);
0245     kfree(p_dev->devname);
0246     kref_put(&p_dev->function_config->ref, pcmcia_release_function);
0247     kfree(p_dev);
0248 }
0249 
0250 
0251 static int pcmcia_device_probe(struct device *dev)
0252 {
0253     struct pcmcia_device *p_dev;
0254     struct pcmcia_driver *p_drv;
0255     struct pcmcia_socket *s;
0256     cistpl_config_t cis_config;
0257     int ret = 0;
0258 
0259     dev = get_device(dev);
0260     if (!dev)
0261         return -ENODEV;
0262 
0263     p_dev = to_pcmcia_dev(dev);
0264     p_drv = to_pcmcia_drv(dev->driver);
0265     s = p_dev->socket;
0266 
0267     dev_dbg(dev, "trying to bind to %s\n", p_drv->name);
0268 
0269     if ((!p_drv->probe) || (!p_dev->function_config) ||
0270         (!try_module_get(p_drv->owner))) {
0271         ret = -EINVAL;
0272         goto put_dev;
0273     }
0274 
0275     /* set up some more device information */
0276     ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
0277                 &cis_config);
0278     if (!ret) {
0279         p_dev->config_base = cis_config.base;
0280         p_dev->config_regs = cis_config.rmask[0];
0281         dev_dbg(dev, "base %x, regs %x", p_dev->config_base,
0282             p_dev->config_regs);
0283     } else {
0284         dev_info(dev,
0285              "pcmcia: could not parse base and rmask0 of CIS\n");
0286         p_dev->config_base = 0;
0287         p_dev->config_regs = 0;
0288     }
0289 
0290     ret = p_drv->probe(p_dev);
0291     if (ret) {
0292         dev_dbg(dev, "binding to %s failed with %d\n",
0293                p_drv->name, ret);
0294         goto put_module;
0295     }
0296     dev_dbg(dev, "%s bound: Vpp %d.%d, idx %x, IRQ %d", p_drv->name,
0297         p_dev->vpp/10, p_dev->vpp%10, p_dev->config_index, p_dev->irq);
0298     dev_dbg(dev, "resources: ioport %pR %pR iomem %pR %pR %pR",
0299         p_dev->resource[0], p_dev->resource[1], p_dev->resource[2],
0300         p_dev->resource[3], p_dev->resource[4]);
0301 
0302     mutex_lock(&s->ops_mutex);
0303     if ((s->pcmcia_pfc) &&
0304         (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
0305         pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
0306     mutex_unlock(&s->ops_mutex);
0307 
0308 put_module:
0309     if (ret)
0310         module_put(p_drv->owner);
0311 put_dev:
0312     if (ret)
0313         put_device(dev);
0314     return ret;
0315 }
0316 
0317 
0318 /*
0319  * Removes a PCMCIA card from the device tree and socket list.
0320  */
0321 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
0322 {
0323     struct pcmcia_device    *p_dev;
0324     struct pcmcia_device    *tmp;
0325 
0326     dev_dbg(leftover ? &leftover->dev : &s->dev,
0327            "pcmcia_card_remove(%d) %s\n", s->sock,
0328            leftover ? leftover->devname : "");
0329 
0330     mutex_lock(&s->ops_mutex);
0331     if (!leftover)
0332         s->device_count = 0;
0333     else
0334         s->device_count = 1;
0335     mutex_unlock(&s->ops_mutex);
0336 
0337     /* unregister all pcmcia_devices registered with this socket, except leftover */
0338     list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
0339         if (p_dev == leftover)
0340             continue;
0341 
0342         mutex_lock(&s->ops_mutex);
0343         list_del(&p_dev->socket_device_list);
0344         mutex_unlock(&s->ops_mutex);
0345 
0346         dev_dbg(&p_dev->dev, "unregistering device\n");
0347         device_unregister(&p_dev->dev);
0348     }
0349 
0350     return;
0351 }
0352 
0353 static void pcmcia_device_remove(struct device *dev)
0354 {
0355     struct pcmcia_device *p_dev;
0356     struct pcmcia_driver *p_drv;
0357     int i;
0358 
0359     p_dev = to_pcmcia_dev(dev);
0360     p_drv = to_pcmcia_drv(dev->driver);
0361 
0362     dev_dbg(dev, "removing device\n");
0363 
0364     /* If we're removing the primary module driving a
0365      * pseudo multi-function card, we need to unbind
0366      * all devices
0367      */
0368     if ((p_dev->socket->pcmcia_pfc) &&
0369         (p_dev->socket->device_count > 0) &&
0370         (p_dev->device_no == 0))
0371         pcmcia_card_remove(p_dev->socket, p_dev);
0372 
0373     /* detach the "instance" */
0374     if (p_drv->remove)
0375         p_drv->remove(p_dev);
0376 
0377     /* check for proper unloading */
0378     if (p_dev->_irq || p_dev->_io || p_dev->_locked)
0379         dev_info(dev,
0380              "pcmcia: driver %s did not release config properly\n",
0381              p_drv->name);
0382 
0383     for (i = 0; i < MAX_WIN; i++)
0384         if (p_dev->_win & CLIENT_WIN_REQ(i))
0385             dev_info(dev,
0386                  "pcmcia: driver %s did not release window properly\n",
0387                  p_drv->name);
0388 
0389     /* references from pcmcia_device_probe */
0390     pcmcia_put_dev(p_dev);
0391     module_put(p_drv->owner);
0392 }
0393 
0394 
0395 /*
0396  * pcmcia_device_query -- determine information about a pcmcia device
0397  */
0398 static int pcmcia_device_query(struct pcmcia_device *p_dev)
0399 {
0400     cistpl_manfid_t manf_id;
0401     cistpl_funcid_t func_id;
0402     cistpl_vers_1_t *vers1;
0403     unsigned int i;
0404 
0405     vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
0406     if (!vers1)
0407         return -ENOMEM;
0408 
0409     if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
0410                    CISTPL_MANFID, &manf_id)) {
0411         mutex_lock(&p_dev->socket->ops_mutex);
0412         p_dev->manf_id = manf_id.manf;
0413         p_dev->card_id = manf_id.card;
0414         p_dev->has_manf_id = 1;
0415         p_dev->has_card_id = 1;
0416         mutex_unlock(&p_dev->socket->ops_mutex);
0417     }
0418 
0419     if (!pccard_read_tuple(p_dev->socket, p_dev->func,
0420                    CISTPL_FUNCID, &func_id)) {
0421         mutex_lock(&p_dev->socket->ops_mutex);
0422         p_dev->func_id = func_id.func;
0423         p_dev->has_func_id = 1;
0424         mutex_unlock(&p_dev->socket->ops_mutex);
0425     } else {
0426         /* rule of thumb: cards with no FUNCID, but with
0427          * common memory device geometry information, are
0428          * probably memory cards (from pcmcia-cs) */
0429         cistpl_device_geo_t *devgeo;
0430 
0431         devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
0432         if (!devgeo) {
0433             kfree(vers1);
0434             return -ENOMEM;
0435         }
0436         if (!pccard_read_tuple(p_dev->socket, p_dev->func,
0437                       CISTPL_DEVICE_GEO, devgeo)) {
0438             dev_dbg(&p_dev->dev,
0439                    "mem device geometry probably means "
0440                    "FUNCID_MEMORY\n");
0441             mutex_lock(&p_dev->socket->ops_mutex);
0442             p_dev->func_id = CISTPL_FUNCID_MEMORY;
0443             p_dev->has_func_id = 1;
0444             mutex_unlock(&p_dev->socket->ops_mutex);
0445         }
0446         kfree(devgeo);
0447     }
0448 
0449     if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
0450                    vers1)) {
0451         mutex_lock(&p_dev->socket->ops_mutex);
0452         for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
0453             char *tmp;
0454             unsigned int length;
0455             char *new;
0456 
0457             tmp = vers1->str + vers1->ofs[i];
0458 
0459             length = strlen(tmp) + 1;
0460             if ((length < 2) || (length > 255))
0461                 continue;
0462 
0463             new = kstrdup(tmp, GFP_KERNEL);
0464             if (!new)
0465                 continue;
0466 
0467             tmp = p_dev->prod_id[i];
0468             p_dev->prod_id[i] = new;
0469             kfree(tmp);
0470         }
0471         mutex_unlock(&p_dev->socket->ops_mutex);
0472     }
0473 
0474     kfree(vers1);
0475     return 0;
0476 }
0477 
0478 
0479 static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,
0480                            unsigned int function)
0481 {
0482     struct pcmcia_device *p_dev, *tmp_dev;
0483     int i;
0484 
0485     s = pcmcia_get_socket(s);
0486     if (!s)
0487         return NULL;
0488 
0489     pr_debug("adding device to %d, function %d\n", s->sock, function);
0490 
0491     p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
0492     if (!p_dev)
0493         goto err_put;
0494 
0495     mutex_lock(&s->ops_mutex);
0496     p_dev->device_no = (s->device_count++);
0497     mutex_unlock(&s->ops_mutex);
0498 
0499     /* max of 2 PFC devices */
0500     if ((p_dev->device_no >= 2) && (function == 0))
0501         goto err_free;
0502 
0503     /* max of 4 devices overall */
0504     if (p_dev->device_no >= 4)
0505         goto err_free;
0506 
0507     p_dev->socket = s;
0508     p_dev->func   = function;
0509 
0510     p_dev->dev.bus = &pcmcia_bus_type;
0511     p_dev->dev.parent = s->dev.parent;
0512     p_dev->dev.release = pcmcia_release_dev;
0513     /* by default don't allow DMA */
0514     p_dev->dma_mask = 0;
0515     p_dev->dev.dma_mask = &p_dev->dma_mask;
0516     dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
0517     if (!dev_name(&p_dev->dev))
0518         goto err_free;
0519     p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
0520     if (!p_dev->devname)
0521         goto err_free;
0522     dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
0523 
0524     mutex_lock(&s->ops_mutex);
0525 
0526     /*
0527      * p_dev->function_config must be the same for all card functions.
0528      * Note that this is serialized by ops_mutex, so that only one
0529      * such struct will be created.
0530      */
0531     list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
0532         if (p_dev->func == tmp_dev->func) {
0533             p_dev->function_config = tmp_dev->function_config;
0534             p_dev->irq = tmp_dev->irq;
0535             kref_get(&p_dev->function_config->ref);
0536         }
0537 
0538     /* Add to the list in pcmcia_bus_socket */
0539     list_add(&p_dev->socket_device_list, &s->devices_list);
0540 
0541     if (pcmcia_setup_irq(p_dev))
0542         dev_warn(&p_dev->dev,
0543             "IRQ setup failed -- device might not work\n");
0544 
0545     if (!p_dev->function_config) {
0546         config_t *c;
0547         dev_dbg(&p_dev->dev, "creating config_t\n");
0548         c = kzalloc(sizeof(struct config_t), GFP_KERNEL);
0549         if (!c) {
0550             mutex_unlock(&s->ops_mutex);
0551             goto err_unreg;
0552         }
0553         p_dev->function_config = c;
0554         kref_init(&c->ref);
0555         for (i = 0; i < MAX_IO_WIN; i++) {
0556             c->io[i].name = p_dev->devname;
0557             c->io[i].flags = IORESOURCE_IO;
0558         }
0559         for (i = 0; i < MAX_WIN; i++) {
0560             c->mem[i].name = p_dev->devname;
0561             c->mem[i].flags = IORESOURCE_MEM;
0562         }
0563     }
0564     for (i = 0; i < MAX_IO_WIN; i++)
0565         p_dev->resource[i] = &p_dev->function_config->io[i];
0566     for (; i < (MAX_IO_WIN + MAX_WIN); i++)
0567         p_dev->resource[i] = &p_dev->function_config->mem[i-MAX_IO_WIN];
0568 
0569     mutex_unlock(&s->ops_mutex);
0570 
0571     dev_notice(&p_dev->dev, "pcmcia: registering new device %s (IRQ: %d)\n",
0572            p_dev->devname, p_dev->irq);
0573 
0574     pcmcia_device_query(p_dev);
0575 
0576     if (device_register(&p_dev->dev))
0577         goto err_unreg;
0578 
0579     return p_dev;
0580 
0581  err_unreg:
0582     mutex_lock(&s->ops_mutex);
0583     list_del(&p_dev->socket_device_list);
0584     mutex_unlock(&s->ops_mutex);
0585 
0586  err_free:
0587     mutex_lock(&s->ops_mutex);
0588     s->device_count--;
0589     mutex_unlock(&s->ops_mutex);
0590 
0591     for (i = 0; i < 4; i++)
0592         kfree(p_dev->prod_id[i]);
0593     kfree(p_dev->devname);
0594     kfree(p_dev);
0595  err_put:
0596     pcmcia_put_socket(s);
0597 
0598     return NULL;
0599 }
0600 
0601 
0602 static int pcmcia_card_add(struct pcmcia_socket *s)
0603 {
0604     cistpl_longlink_mfc_t mfc;
0605     unsigned int no_funcs, i, no_chains;
0606     int ret = -EAGAIN;
0607 
0608     mutex_lock(&s->ops_mutex);
0609     if (!(s->resource_setup_done)) {
0610         dev_dbg(&s->dev,
0611                "no resources available, delaying card_add\n");
0612         mutex_unlock(&s->ops_mutex);
0613         return -EAGAIN; /* try again, but later... */
0614     }
0615 
0616     if (pcmcia_validate_mem(s)) {
0617         dev_dbg(&s->dev, "validating mem resources failed, "
0618                "delaying card_add\n");
0619         mutex_unlock(&s->ops_mutex);
0620         return -EAGAIN; /* try again, but later... */
0621     }
0622     mutex_unlock(&s->ops_mutex);
0623 
0624     ret = pccard_validate_cis(s, &no_chains);
0625     if (ret || !no_chains) {
0626 #if defined(CONFIG_MTD_PCMCIA_ANONYMOUS)
0627         /* Set up as an anonymous card. If we don't have anonymous
0628            memory support then just error the card as there is no
0629            point trying to second guess.
0630 
0631            Note: some cards have just a device entry, it may be
0632            worth extending support to cover these in future */
0633         if (ret == -EIO) {
0634             dev_info(&s->dev, "no CIS, assuming an anonymous memory card.\n");
0635             pcmcia_replace_cis(s, "\xFF", 1);
0636             no_chains = 1;
0637             ret = 0;
0638         } else
0639 #endif
0640         {
0641             dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
0642             return -ENODEV;
0643         }
0644     }
0645 
0646     if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
0647         no_funcs = mfc.nfn;
0648     else
0649         no_funcs = 1;
0650     s->functions = no_funcs;
0651 
0652     for (i = 0; i < no_funcs; i++)
0653         pcmcia_device_add(s, i);
0654 
0655     return ret;
0656 }
0657 
0658 
0659 static int pcmcia_requery_callback(struct device *dev, void *_data)
0660 {
0661     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
0662     if (!p_dev->dev.driver) {
0663         dev_dbg(dev, "update device information\n");
0664         pcmcia_device_query(p_dev);
0665     }
0666 
0667     return 0;
0668 }
0669 
0670 
0671 static void pcmcia_requery(struct pcmcia_socket *s)
0672 {
0673     int has_pfc;
0674 
0675     if (!(s->state & SOCKET_PRESENT))
0676         return;
0677 
0678     if (s->functions == 0) {
0679         pcmcia_card_add(s);
0680         return;
0681     }
0682 
0683     /* some device information might have changed because of a CIS
0684      * update or because we can finally read it correctly... so
0685      * determine it again, overwriting old values if necessary. */
0686     bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery_callback);
0687 
0688     /* if the CIS changed, we need to check whether the number of
0689      * functions changed. */
0690     if (s->fake_cis) {
0691         int old_funcs, new_funcs;
0692         cistpl_longlink_mfc_t mfc;
0693 
0694         /* does this cis override add or remove functions? */
0695         old_funcs = s->functions;
0696 
0697         if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
0698                     &mfc))
0699             new_funcs = mfc.nfn;
0700         else
0701             new_funcs = 1;
0702         if (old_funcs != new_funcs) {
0703             /* we need to re-start */
0704             pcmcia_card_remove(s, NULL);
0705             s->functions = 0;
0706             pcmcia_card_add(s);
0707         }
0708     }
0709 
0710     /* If the PCMCIA device consists of two pseudo devices,
0711      * call pcmcia_device_add() -- which will fail if both
0712      * devices are already registered. */
0713     mutex_lock(&s->ops_mutex);
0714     has_pfc = s->pcmcia_pfc;
0715     mutex_unlock(&s->ops_mutex);
0716     if (has_pfc)
0717         pcmcia_device_add(s, 0);
0718 
0719     /* we re-scan all devices, not just the ones connected to this
0720      * socket. This does not matter, though. */
0721     if (bus_rescan_devices(&pcmcia_bus_type))
0722         dev_warn(&s->dev, "rescanning the bus failed\n");
0723 }
0724 
0725 
0726 #ifdef CONFIG_PCMCIA_LOAD_CIS
0727 
0728 /**
0729  * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
0730  * @dev: the pcmcia device which needs a CIS override
0731  * @filename: requested filename in /lib/firmware/
0732  *
0733  * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
0734  * the one provided by the card is broken. The firmware files reside in
0735  * /lib/firmware/ in userspace.
0736  */
0737 static int pcmcia_load_firmware(struct pcmcia_device *dev, char *filename)
0738 {
0739     struct pcmcia_socket *s = dev->socket;
0740     const struct firmware *fw;
0741     int ret = -ENOMEM;
0742     cistpl_longlink_mfc_t mfc;
0743     int old_funcs, new_funcs = 1;
0744 
0745     if (!filename)
0746         return -EINVAL;
0747 
0748     dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
0749 
0750     if (request_firmware(&fw, filename, &dev->dev) == 0) {
0751         if (fw->size >= CISTPL_MAX_CIS_SIZE) {
0752             ret = -EINVAL;
0753             dev_err(&dev->dev, "pcmcia: CIS override is too big\n");
0754             goto release;
0755         }
0756 
0757         if (!pcmcia_replace_cis(s, fw->data, fw->size))
0758             ret = 0;
0759         else {
0760             dev_err(&dev->dev, "pcmcia: CIS override failed\n");
0761             goto release;
0762         }
0763 
0764         /* we need to re-start if the number of functions changed */
0765         old_funcs = s->functions;
0766         if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
0767                     &mfc))
0768             new_funcs = mfc.nfn;
0769 
0770         if (old_funcs != new_funcs)
0771             ret = -EBUSY;
0772 
0773         /* update information */
0774         pcmcia_device_query(dev);
0775 
0776         /* requery (as number of functions might have changed) */
0777         pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
0778     }
0779  release:
0780     release_firmware(fw);
0781 
0782     return ret;
0783 }
0784 
0785 #else /* !CONFIG_PCMCIA_LOAD_CIS */
0786 
0787 static inline int pcmcia_load_firmware(struct pcmcia_device *dev,
0788                        char *filename)
0789 {
0790     return -ENODEV;
0791 }
0792 
0793 #endif
0794 
0795 
0796 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
0797                   const struct pcmcia_device_id *did)
0798 {
0799     if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
0800         if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
0801             return 0;
0802     }
0803 
0804     if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
0805         if ((!dev->has_card_id) || (dev->card_id != did->card_id))
0806             return 0;
0807     }
0808 
0809     if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
0810         if (dev->func != did->function)
0811             return 0;
0812     }
0813 
0814     if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
0815         if (!dev->prod_id[0])
0816             return 0;
0817         if (strcmp(did->prod_id[0], dev->prod_id[0]))
0818             return 0;
0819     }
0820 
0821     if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
0822         if (!dev->prod_id[1])
0823             return 0;
0824         if (strcmp(did->prod_id[1], dev->prod_id[1]))
0825             return 0;
0826     }
0827 
0828     if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
0829         if (!dev->prod_id[2])
0830             return 0;
0831         if (strcmp(did->prod_id[2], dev->prod_id[2]))
0832             return 0;
0833     }
0834 
0835     if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
0836         if (!dev->prod_id[3])
0837             return 0;
0838         if (strcmp(did->prod_id[3], dev->prod_id[3]))
0839             return 0;
0840     }
0841 
0842     if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
0843         dev_dbg(&dev->dev, "this is a pseudo-multi-function device\n");
0844         mutex_lock(&dev->socket->ops_mutex);
0845         dev->socket->pcmcia_pfc = 1;
0846         mutex_unlock(&dev->socket->ops_mutex);
0847         if (dev->device_no != did->device_no)
0848             return 0;
0849     }
0850 
0851     if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
0852         int ret;
0853 
0854         if ((!dev->has_func_id) || (dev->func_id != did->func_id))
0855             return 0;
0856 
0857         /* if this is a pseudo-multi-function device,
0858          * we need explicit matches */
0859         if (dev->socket->pcmcia_pfc)
0860             return 0;
0861         if (dev->device_no)
0862             return 0;
0863 
0864         /* also, FUNC_ID matching needs to be activated by userspace
0865          * after it has re-checked that there is no possible module
0866          * with a prod_id/manf_id/card_id match.
0867          */
0868         mutex_lock(&dev->socket->ops_mutex);
0869         ret = dev->allow_func_id_match;
0870         mutex_unlock(&dev->socket->ops_mutex);
0871 
0872         if (!ret) {
0873             dev_dbg(&dev->dev,
0874                 "skipping FUNC_ID match until userspace ACK\n");
0875             return 0;
0876         }
0877     }
0878 
0879     if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
0880         dev_dbg(&dev->dev, "device needs a fake CIS\n");
0881         if (!dev->socket->fake_cis)
0882             if (pcmcia_load_firmware(dev, did->cisfile))
0883                 return 0;
0884     }
0885 
0886     if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
0887         int i;
0888         for (i = 0; i < 4; i++)
0889             if (dev->prod_id[i])
0890                 return 0;
0891         if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
0892             return 0;
0893     }
0894 
0895     return 1;
0896 }
0897 
0898 
0899 static int pcmcia_bus_match(struct device *dev, struct device_driver *drv)
0900 {
0901     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
0902     struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
0903     const struct pcmcia_device_id *did = p_drv->id_table;
0904     struct pcmcia_dynid *dynid;
0905 
0906     /* match dynamic devices first */
0907     mutex_lock(&p_drv->dynids.lock);
0908     list_for_each_entry(dynid, &p_drv->dynids.list, node) {
0909         dev_dbg(dev, "trying to match to %s\n", drv->name);
0910         if (pcmcia_devmatch(p_dev, &dynid->id)) {
0911             dev_dbg(dev, "matched to %s\n", drv->name);
0912             mutex_unlock(&p_drv->dynids.lock);
0913             return 1;
0914         }
0915     }
0916     mutex_unlock(&p_drv->dynids.lock);
0917 
0918     while (did && did->match_flags) {
0919         dev_dbg(dev, "trying to match to %s\n", drv->name);
0920         if (pcmcia_devmatch(p_dev, did)) {
0921             dev_dbg(dev, "matched to %s\n", drv->name);
0922             return 1;
0923         }
0924         did++;
0925     }
0926 
0927     return 0;
0928 }
0929 
0930 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
0931 {
0932     struct pcmcia_device *p_dev;
0933     int i;
0934     u32 hash[4] = { 0, 0, 0, 0};
0935 
0936     if (!dev)
0937         return -ENODEV;
0938 
0939     p_dev = to_pcmcia_dev(dev);
0940 
0941     /* calculate hashes */
0942     for (i = 0; i < 4; i++) {
0943         if (!p_dev->prod_id[i])
0944             continue;
0945         hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
0946     }
0947 
0948     if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
0949         return -ENOMEM;
0950 
0951     if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
0952         return -ENOMEM;
0953 
0954     if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
0955                "pa%08Xpb%08Xpc%08Xpd%08X",
0956                p_dev->has_manf_id ? p_dev->manf_id : 0,
0957                p_dev->has_card_id ? p_dev->card_id : 0,
0958                p_dev->has_func_id ? p_dev->func_id : 0,
0959                p_dev->func,
0960                p_dev->device_no,
0961                hash[0],
0962                hash[1],
0963                hash[2],
0964                hash[3]))
0965         return -ENOMEM;
0966 
0967     return 0;
0968 }
0969 
0970 /************************ runtime PM support ***************************/
0971 
0972 static int pcmcia_dev_suspend(struct device *dev);
0973 static int pcmcia_dev_resume(struct device *dev);
0974 
0975 static int runtime_suspend(struct device *dev)
0976 {
0977     int rc;
0978 
0979     device_lock(dev);
0980     rc = pcmcia_dev_suspend(dev);
0981     device_unlock(dev);
0982     return rc;
0983 }
0984 
0985 static int runtime_resume(struct device *dev)
0986 {
0987     int rc;
0988 
0989     device_lock(dev);
0990     rc = pcmcia_dev_resume(dev);
0991     device_unlock(dev);
0992     return rc;
0993 }
0994 
0995 /************************ per-device sysfs output ***************************/
0996 
0997 #define pcmcia_device_attr(field, test, format)             \
0998 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf)      \
0999 {                                   \
1000     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);       \
1001     return p_dev->test ? sysfs_emit(buf, format, p_dev->field) : -ENODEV; \
1002 }                                   \
1003 static DEVICE_ATTR_RO(field);
1004 
1005 #define pcmcia_device_stringattr(name, field)                   \
1006 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf)       \
1007 {                                   \
1008     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);       \
1009     return p_dev->field ? sysfs_emit(buf, "%s\n", p_dev->field) : -ENODEV; \
1010 }                                   \
1011 static DEVICE_ATTR_RO(name);
1012 
1013 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1014 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1015 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1016 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1017 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1018 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1019 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1020 
1021 static ssize_t function_show(struct device *dev, struct device_attribute *attr,
1022                  char *buf)
1023 {
1024     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1025     return p_dev->socket ? sysfs_emit(buf, "0x%02x\n", p_dev->func) : -ENODEV;
1026 }
1027 static DEVICE_ATTR_RO(function);
1028 
1029 static ssize_t resources_show(struct device *dev,
1030                   struct device_attribute *attr, char *buf)
1031 {
1032     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1033     int i, at = 0;
1034 
1035     for (i = 0; i < PCMCIA_NUM_RESOURCES; i++)
1036         at += sysfs_emit_at(buf, at, "%pr\n", p_dev->resource[i]);
1037 
1038     return at;
1039 }
1040 static DEVICE_ATTR_RO(resources);
1041 
1042 static ssize_t pm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
1043 {
1044     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1045 
1046     if (p_dev->suspended)
1047         return sysfs_emit(buf, "off\n");
1048     else
1049         return sysfs_emit(buf, "on\n");
1050 }
1051 
1052 static ssize_t pm_state_store(struct device *dev, struct device_attribute *attr,
1053                   const char *buf, size_t count)
1054 {
1055     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1056     int ret = 0;
1057 
1058     if (!count)
1059         return -EINVAL;
1060 
1061     if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1062         ret = runtime_suspend(dev);
1063     else if (p_dev->suspended && !strncmp(buf, "on", 2))
1064         ret = runtime_resume(dev);
1065 
1066     return ret ? ret : count;
1067 }
1068 static DEVICE_ATTR_RW(pm_state);
1069 
1070 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1071 {
1072     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1073     int i;
1074     u32 hash[4] = { 0, 0, 0, 0};
1075 
1076     /* calculate hashes */
1077     for (i = 0; i < 4; i++) {
1078         if (!p_dev->prod_id[i])
1079             continue;
1080         hash[i] = crc32(0, p_dev->prod_id[i],
1081                 strlen(p_dev->prod_id[i]));
1082     }
1083     return sysfs_emit(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02Xpa%08Xpb%08Xpc%08Xpd%08X\n",
1084                 p_dev->has_manf_id ? p_dev->manf_id : 0,
1085                 p_dev->has_card_id ? p_dev->card_id : 0,
1086                 p_dev->has_func_id ? p_dev->func_id : 0,
1087                 p_dev->func, p_dev->device_no,
1088                 hash[0], hash[1], hash[2], hash[3]);
1089 }
1090 static DEVICE_ATTR_RO(modalias);
1091 
1092 static ssize_t allow_func_id_match_store(struct device *dev,
1093         struct device_attribute *attr, const char *buf, size_t count)
1094 {
1095     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1096 
1097     if (!count)
1098         return -EINVAL;
1099 
1100     mutex_lock(&p_dev->socket->ops_mutex);
1101     p_dev->allow_func_id_match = 1;
1102     mutex_unlock(&p_dev->socket->ops_mutex);
1103     pcmcia_parse_uevents(p_dev->socket, PCMCIA_UEVENT_REQUERY);
1104 
1105     return count;
1106 }
1107 static DEVICE_ATTR_WO(allow_func_id_match);
1108 
1109 static struct attribute *pcmcia_dev_attrs[] = {
1110     &dev_attr_resources.attr,
1111     &dev_attr_pm_state.attr,
1112     &dev_attr_function.attr,
1113     &dev_attr_func_id.attr,
1114     &dev_attr_manf_id.attr,
1115     &dev_attr_card_id.attr,
1116     &dev_attr_prod_id1.attr,
1117     &dev_attr_prod_id2.attr,
1118     &dev_attr_prod_id3.attr,
1119     &dev_attr_prod_id4.attr,
1120     &dev_attr_modalias.attr,
1121     &dev_attr_allow_func_id_match.attr,
1122     NULL,
1123 };
1124 ATTRIBUTE_GROUPS(pcmcia_dev);
1125 
1126 /* PM support, also needed for reset */
1127 
1128 static int pcmcia_dev_suspend(struct device *dev)
1129 {
1130     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1131     struct pcmcia_driver *p_drv = NULL;
1132     int ret = 0;
1133 
1134     mutex_lock(&p_dev->socket->ops_mutex);
1135     if (p_dev->suspended) {
1136         mutex_unlock(&p_dev->socket->ops_mutex);
1137         return 0;
1138     }
1139     p_dev->suspended = 1;
1140     mutex_unlock(&p_dev->socket->ops_mutex);
1141 
1142     dev_dbg(dev, "suspending\n");
1143 
1144     if (dev->driver)
1145         p_drv = to_pcmcia_drv(dev->driver);
1146 
1147     if (!p_drv)
1148         goto out;
1149 
1150     if (p_drv->suspend) {
1151         ret = p_drv->suspend(p_dev);
1152         if (ret) {
1153             dev_err(dev,
1154                 "pcmcia: device %s (driver %s) did not want to go to sleep (%d)\n",
1155                 p_dev->devname, p_drv->name, ret);
1156             mutex_lock(&p_dev->socket->ops_mutex);
1157             p_dev->suspended = 0;
1158             mutex_unlock(&p_dev->socket->ops_mutex);
1159             goto out;
1160         }
1161     }
1162 
1163     if (p_dev->device_no == p_dev->func) {
1164         dev_dbg(dev, "releasing configuration\n");
1165         pcmcia_release_configuration(p_dev);
1166     }
1167 
1168  out:
1169     return ret;
1170 }
1171 
1172 
1173 static int pcmcia_dev_resume(struct device *dev)
1174 {
1175     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1176     struct pcmcia_driver *p_drv = NULL;
1177     int ret = 0;
1178 
1179     mutex_lock(&p_dev->socket->ops_mutex);
1180     if (!p_dev->suspended) {
1181         mutex_unlock(&p_dev->socket->ops_mutex);
1182         return 0;
1183     }
1184     p_dev->suspended = 0;
1185     mutex_unlock(&p_dev->socket->ops_mutex);
1186 
1187     dev_dbg(dev, "resuming\n");
1188 
1189     if (dev->driver)
1190         p_drv = to_pcmcia_drv(dev->driver);
1191 
1192     if (!p_drv)
1193         goto out;
1194 
1195     if (p_dev->device_no == p_dev->func) {
1196         dev_dbg(dev, "requesting configuration\n");
1197         ret = pcmcia_enable_device(p_dev);
1198         if (ret)
1199             goto out;
1200     }
1201 
1202     if (p_drv->resume)
1203         ret = p_drv->resume(p_dev);
1204 
1205  out:
1206     return ret;
1207 }
1208 
1209 
1210 static int pcmcia_bus_suspend_callback(struct device *dev, void *_data)
1211 {
1212     struct pcmcia_socket *skt = _data;
1213     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1214 
1215     if (p_dev->socket != skt || p_dev->suspended)
1216         return 0;
1217 
1218     return runtime_suspend(dev);
1219 }
1220 
1221 static int pcmcia_bus_resume_callback(struct device *dev, void *_data)
1222 {
1223     struct pcmcia_socket *skt = _data;
1224     struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1225 
1226     if (p_dev->socket != skt || !p_dev->suspended)
1227         return 0;
1228 
1229     runtime_resume(dev);
1230 
1231     return 0;
1232 }
1233 
1234 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1235 {
1236     dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
1237     bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1238     return 0;
1239 }
1240 
1241 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1242 {
1243     dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
1244     if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1245                  pcmcia_bus_suspend_callback)) {
1246         pcmcia_bus_resume(skt);
1247         return -EIO;
1248     }
1249     return 0;
1250 }
1251 
1252 static int pcmcia_bus_remove(struct pcmcia_socket *skt)
1253 {
1254     atomic_set(&skt->present, 0);
1255     pcmcia_card_remove(skt, NULL);
1256 
1257     mutex_lock(&skt->ops_mutex);
1258     destroy_cis_cache(skt);
1259     pcmcia_cleanup_irq(skt);
1260     mutex_unlock(&skt->ops_mutex);
1261 
1262     return 0;
1263 }
1264 
1265 static int pcmcia_bus_add(struct pcmcia_socket *skt)
1266 {
1267     atomic_set(&skt->present, 1);
1268 
1269     mutex_lock(&skt->ops_mutex);
1270     skt->pcmcia_pfc = 0;
1271     destroy_cis_cache(skt); /* to be on the safe side... */
1272     mutex_unlock(&skt->ops_mutex);
1273 
1274     pcmcia_card_add(skt);
1275 
1276     return 0;
1277 }
1278 
1279 static int pcmcia_bus_early_resume(struct pcmcia_socket *skt)
1280 {
1281     if (!verify_cis_cache(skt))
1282         return 0;
1283 
1284     dev_dbg(&skt->dev, "cis mismatch - different card\n");
1285 
1286     /* first, remove the card */
1287     pcmcia_bus_remove(skt);
1288 
1289     mutex_lock(&skt->ops_mutex);
1290     destroy_cis_cache(skt);
1291     kfree(skt->fake_cis);
1292     skt->fake_cis = NULL;
1293     skt->functions = 0;
1294     mutex_unlock(&skt->ops_mutex);
1295 
1296     /* now, add the new card */
1297     pcmcia_bus_add(skt);
1298     return 0;
1299 }
1300 
1301 
1302 /*
1303  * NOTE: This is racy. There's no guarantee the card will still be
1304  * physically present, even if the call to this function returns
1305  * non-NULL. Furthermore, the device driver most likely is unbound
1306  * almost immediately, so the timeframe where pcmcia_dev_present
1307  * returns NULL is probably really really small.
1308  */
1309 struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
1310 {
1311     struct pcmcia_device *p_dev;
1312     struct pcmcia_device *ret = NULL;
1313 
1314     p_dev = pcmcia_get_dev(_p_dev);
1315     if (!p_dev)
1316         return NULL;
1317 
1318     if (atomic_read(&p_dev->socket->present) != 0)
1319         ret = p_dev;
1320 
1321     pcmcia_put_dev(p_dev);
1322     return ret;
1323 }
1324 EXPORT_SYMBOL(pcmcia_dev_present);
1325 
1326 
1327 static struct pcmcia_callback pcmcia_bus_callback = {
1328     .owner = THIS_MODULE,
1329     .add = pcmcia_bus_add,
1330     .remove = pcmcia_bus_remove,
1331     .requery = pcmcia_requery,
1332     .validate = pccard_validate_cis,
1333     .suspend = pcmcia_bus_suspend,
1334     .early_resume = pcmcia_bus_early_resume,
1335     .resume = pcmcia_bus_resume,
1336 };
1337 
1338 static int pcmcia_bus_add_socket(struct device *dev,
1339                        struct class_interface *class_intf)
1340 {
1341     struct pcmcia_socket *socket = dev_get_drvdata(dev);
1342     int ret;
1343 
1344     socket = pcmcia_get_socket(socket);
1345     if (!socket) {
1346         dev_err(dev, "PCMCIA obtaining reference to socket failed\n");
1347         return -ENODEV;
1348     }
1349 
1350     ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
1351     if (ret) {
1352         dev_err(dev, "PCMCIA registration failed\n");
1353         pcmcia_put_socket(socket);
1354         return ret;
1355     }
1356 
1357     INIT_LIST_HEAD(&socket->devices_list);
1358     socket->pcmcia_pfc = 0;
1359     socket->device_count = 0;
1360     atomic_set(&socket->present, 0);
1361 
1362     ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1363     if (ret) {
1364         dev_err(dev, "PCMCIA registration failed\n");
1365         pcmcia_put_socket(socket);
1366         return ret;
1367     }
1368 
1369     return 0;
1370 }
1371 
1372 static void pcmcia_bus_remove_socket(struct device *dev,
1373                      struct class_interface *class_intf)
1374 {
1375     struct pcmcia_socket *socket = dev_get_drvdata(dev);
1376 
1377     if (!socket)
1378         return;
1379 
1380     pccard_register_pcmcia(socket, NULL);
1381 
1382     /* unregister any unbound devices */
1383     mutex_lock(&socket->skt_mutex);
1384     pcmcia_card_remove(socket, NULL);
1385     release_cis_mem(socket);
1386     mutex_unlock(&socket->skt_mutex);
1387 
1388     sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
1389 
1390     pcmcia_put_socket(socket);
1391 
1392     return;
1393 }
1394 
1395 
1396 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1397 static struct class_interface pcmcia_bus_interface __refdata = {
1398     .class = &pcmcia_socket_class,
1399     .add_dev = &pcmcia_bus_add_socket,
1400     .remove_dev = &pcmcia_bus_remove_socket,
1401 };
1402 
1403 static const struct dev_pm_ops pcmcia_bus_pm_ops = {
1404     SET_SYSTEM_SLEEP_PM_OPS(pcmcia_dev_suspend, pcmcia_dev_resume)
1405 };
1406 
1407 struct bus_type pcmcia_bus_type = {
1408     .name = "pcmcia",
1409     .uevent = pcmcia_bus_uevent,
1410     .match = pcmcia_bus_match,
1411     .dev_groups = pcmcia_dev_groups,
1412     .probe = pcmcia_device_probe,
1413     .remove = pcmcia_device_remove,
1414     .pm = &pcmcia_bus_pm_ops,
1415 };
1416 
1417 
1418 static int __init init_pcmcia_bus(void)
1419 {
1420     int ret;
1421 
1422     ret = bus_register(&pcmcia_bus_type);
1423     if (ret < 0) {
1424         printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1425         return ret;
1426     }
1427     ret = class_interface_register(&pcmcia_bus_interface);
1428     if (ret < 0) {
1429         printk(KERN_WARNING
1430             "pcmcia: class_interface_register error: %d\n", ret);
1431         bus_unregister(&pcmcia_bus_type);
1432         return ret;
1433     }
1434 
1435     return 0;
1436 }
1437 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1438                    * pcmcia_socket_class is already registered */
1439 
1440 
1441 static void __exit exit_pcmcia_bus(void)
1442 {
1443     class_interface_unregister(&pcmcia_bus_interface);
1444 
1445     bus_unregister(&pcmcia_bus_type);
1446 }
1447 module_exit(exit_pcmcia_bus);
1448 
1449 
1450 MODULE_ALIAS("ds");