Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * core.c - contains all core device and protocol registration functions
0004  *
0005  * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
0006  */
0007 
0008 #include <linux/pnp.h>
0009 #include <linux/types.h>
0010 #include <linux/list.h>
0011 #include <linux/device.h>
0012 #include <linux/module.h>
0013 #include <linux/mutex.h>
0014 #include <linux/init.h>
0015 #include <linux/string.h>
0016 #include <linux/slab.h>
0017 #include <linux/errno.h>
0018 #include <linux/dma-mapping.h>
0019 
0020 #include "base.h"
0021 
0022 static LIST_HEAD(pnp_protocols);
0023 LIST_HEAD(pnp_global);
0024 DEFINE_MUTEX(pnp_lock);
0025 
0026 /*
0027  * ACPI or PNPBIOS should tell us about all platform devices, so we can
0028  * skip some blind probes.  ISAPNP typically enumerates only plug-in ISA
0029  * devices, not built-in things like COM ports.
0030  */
0031 int pnp_platform_devices;
0032 EXPORT_SYMBOL(pnp_platform_devices);
0033 
0034 static void pnp_remove_protocol(struct pnp_protocol *protocol)
0035 {
0036     mutex_lock(&pnp_lock);
0037     list_del(&protocol->protocol_list);
0038     mutex_unlock(&pnp_lock);
0039 }
0040 
0041 /**
0042  * pnp_register_protocol - adds a pnp protocol to the pnp layer
0043  * @protocol: pointer to the corresponding pnp_protocol structure
0044  *
0045  *  Ex protocols: ISAPNP, PNPBIOS, etc
0046  */
0047 int pnp_register_protocol(struct pnp_protocol *protocol)
0048 {
0049     struct list_head *pos;
0050     int nodenum, ret;
0051 
0052     INIT_LIST_HEAD(&protocol->devices);
0053     INIT_LIST_HEAD(&protocol->cards);
0054     nodenum = 0;
0055 
0056     mutex_lock(&pnp_lock);
0057 
0058     /* assign the lowest unused number */
0059     list_for_each(pos, &pnp_protocols) {
0060         struct pnp_protocol *cur = to_pnp_protocol(pos);
0061         if (cur->number == nodenum) {
0062             pos = &pnp_protocols;
0063             nodenum++;
0064         }
0065     }
0066 
0067     protocol->number = nodenum;
0068     dev_set_name(&protocol->dev, "pnp%d", nodenum);
0069 
0070     list_add_tail(&protocol->protocol_list, &pnp_protocols);
0071 
0072     mutex_unlock(&pnp_lock);
0073 
0074     ret = device_register(&protocol->dev);
0075     if (ret)
0076         pnp_remove_protocol(protocol);
0077 
0078     return ret;
0079 }
0080 
0081 /**
0082  * pnp_unregister_protocol - removes a pnp protocol from the pnp layer
0083  * @protocol: pointer to the corresponding pnp_protocol structure
0084  */
0085 void pnp_unregister_protocol(struct pnp_protocol *protocol)
0086 {
0087     pnp_remove_protocol(protocol);
0088     device_unregister(&protocol->dev);
0089 }
0090 
0091 static void pnp_free_ids(struct pnp_dev *dev)
0092 {
0093     struct pnp_id *id;
0094     struct pnp_id *next;
0095 
0096     id = dev->id;
0097     while (id) {
0098         next = id->next;
0099         kfree(id);
0100         id = next;
0101     }
0102 }
0103 
0104 void pnp_free_resource(struct pnp_resource *pnp_res)
0105 {
0106     list_del(&pnp_res->list);
0107     kfree(pnp_res);
0108 }
0109 
0110 void pnp_free_resources(struct pnp_dev *dev)
0111 {
0112     struct pnp_resource *pnp_res, *tmp;
0113 
0114     list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
0115         pnp_free_resource(pnp_res);
0116     }
0117 }
0118 
0119 static void pnp_release_device(struct device *dmdev)
0120 {
0121     struct pnp_dev *dev = to_pnp_dev(dmdev);
0122 
0123     pnp_free_ids(dev);
0124     pnp_free_resources(dev);
0125     pnp_free_options(dev);
0126     kfree(dev);
0127 }
0128 
0129 struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
0130                   const char *pnpid)
0131 {
0132     struct pnp_dev *dev;
0133     struct pnp_id *dev_id;
0134 
0135     dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
0136     if (!dev)
0137         return NULL;
0138 
0139     INIT_LIST_HEAD(&dev->resources);
0140     INIT_LIST_HEAD(&dev->options);
0141     dev->protocol = protocol;
0142     dev->number = id;
0143     dev->dma_mask = DMA_BIT_MASK(24);
0144 
0145     dev->dev.parent = &dev->protocol->dev;
0146     dev->dev.bus = &pnp_bus_type;
0147     dev->dev.dma_mask = &dev->dma_mask;
0148     dev->dev.coherent_dma_mask = dev->dma_mask;
0149     dev->dev.release = &pnp_release_device;
0150 
0151     dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
0152 
0153     dev_id = pnp_add_id(dev, pnpid);
0154     if (!dev_id) {
0155         kfree(dev);
0156         return NULL;
0157     }
0158 
0159     return dev;
0160 }
0161 
0162 static void pnp_delist_device(struct pnp_dev *dev)
0163 {
0164     mutex_lock(&pnp_lock);
0165     list_del(&dev->global_list);
0166     list_del(&dev->protocol_list);
0167     mutex_unlock(&pnp_lock);
0168 }
0169 
0170 int __pnp_add_device(struct pnp_dev *dev)
0171 {
0172     int ret;
0173 
0174     pnp_fixup_device(dev);
0175     dev->status = PNP_READY;
0176 
0177     mutex_lock(&pnp_lock);
0178 
0179     list_add_tail(&dev->global_list, &pnp_global);
0180     list_add_tail(&dev->protocol_list, &dev->protocol->devices);
0181 
0182     mutex_unlock(&pnp_lock);
0183 
0184     ret = device_register(&dev->dev);
0185     if (ret)
0186         pnp_delist_device(dev);
0187     else if (dev->protocol->can_wakeup)
0188         device_set_wakeup_capable(&dev->dev,
0189                 dev->protocol->can_wakeup(dev));
0190 
0191     return ret;
0192 }
0193 
0194 /*
0195  * pnp_add_device - adds a pnp device to the pnp layer
0196  * @dev: pointer to dev to add
0197  *
0198  *  adds to driver model, name database, fixups, interface, etc.
0199  */
0200 int pnp_add_device(struct pnp_dev *dev)
0201 {
0202     int ret;
0203     char buf[128];
0204     int len = 0;
0205     struct pnp_id *id;
0206 
0207     if (dev->card)
0208         return -EINVAL;
0209 
0210     ret = __pnp_add_device(dev);
0211     if (ret)
0212         return ret;
0213 
0214     buf[0] = '\0';
0215     for (id = dev->id; id; id = id->next)
0216         len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
0217 
0218     dev_dbg(&dev->dev, "%s device, IDs%s (%s)\n", dev->protocol->name, buf,
0219         dev->active ? "active" : "disabled");
0220     return 0;
0221 }
0222 
0223 void __pnp_remove_device(struct pnp_dev *dev)
0224 {
0225     pnp_delist_device(dev);
0226     device_unregister(&dev->dev);
0227 }
0228 
0229 static int __init pnp_init(void)
0230 {
0231     return bus_register(&pnp_bus_type);
0232 }
0233 
0234 subsys_initcall(pnp_init);
0235 
0236 int pnp_debug;
0237 
0238 #if defined(CONFIG_PNP_DEBUG_MESSAGES)
0239 module_param_named(debug, pnp_debug, int, 0644);
0240 #endif