1 /* 2 * core.c - contains all core device and protocol registration functions 3 * 4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com> 5 * 6 */ 7 8 #include <linux/pnp.h> 9 #include <linux/types.h> 10 #include <linux/list.h> 11 #include <linux/device.h> 12 #include <linux/module.h> 13 #include <linux/init.h> 14 #include <linux/string.h> 15 #include <linux/slab.h> 16 #include <linux/errno.h> 17 18 #include "base.h" 19 20 21 static LIST_HEAD(pnp_protocols); 22 LIST_HEAD(pnp_global); 23 DEFINE_SPINLOCK(pnp_lock); 24 25 void *pnp_alloc(long size) 26 { 27 void *result; 28 29 result = kmalloc(size, GFP_KERNEL); 30 if (!result){ 31 printk(KERN_ERR "pnp: Out of Memory\n"); 32 return NULL; 33 } 34 memset(result, 0, size); 35 return result; 36 } 37 38 /** 39 * pnp_protocol_register - adds a pnp protocol to the pnp layer 40 * @protocol: pointer to the corresponding pnp_protocol structure 41 * 42 * Ex protocols: ISAPNP, PNPBIOS, etc 43 */ 44 45 int pnp_register_protocol(struct pnp_protocol *protocol) 46 { 47 int nodenum; 48 struct list_head * pos; 49 50 if (!protocol) 51 return -EINVAL; 52 53 INIT_LIST_HEAD(&protocol->devices); 54 INIT_LIST_HEAD(&protocol->cards); 55 nodenum = 0; 56 spin_lock(&pnp_lock); 57 58 /* assign the lowest unused number */ 59 list_for_each(pos,&pnp_protocols) { 60 struct pnp_protocol * cur = to_pnp_protocol(pos); 61 if (cur->number == nodenum){ 62 pos = &pnp_protocols; 63 nodenum++; 64 } 65 } 66 67 list_add_tail(&protocol->protocol_list, &pnp_protocols); 68 spin_unlock(&pnp_lock); 69 70 protocol->number = nodenum; 71 sprintf(protocol->dev.bus_id, "pnp%d", nodenum); 72 return device_register(&protocol->dev); 73 } 74 75 /** 76 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer 77 * @protocol: pointer to the corresponding pnp_protocol structure 78 * 79 */ 80 void pnp_unregister_protocol(struct pnp_protocol *protocol) 81 { 82 spin_lock(&pnp_lock); 83 list_del(&protocol->protocol_list); 84 spin_unlock(&pnp_lock); 85 device_unregister(&protocol->dev); 86 } 87 88 89 static void pnp_free_ids(struct pnp_dev *dev) 90 { 91 struct pnp_id * id; 92 struct pnp_id * next; 93 if (!dev) 94 return; 95 id = dev->id; 96 while (id) { 97 next = id->next; 98 kfree(id); 99 id = next; 100 } 101 } 102 103 static void pnp_release_device(struct device *dmdev) 104 { 105 struct pnp_dev * dev = to_pnp_dev(dmdev); 106 pnp_free_option(dev->independent); 107 pnp_free_option(dev->dependent); 108 pnp_free_ids(dev); 109 kfree(dev); 110 } 111 112 int __pnp_add_device(struct pnp_dev *dev) 113 { 114 int ret; 115 pnp_fixup_device(dev); 116 dev->dev.bus = &pnp_bus_type; 117 dev->dev.release = &pnp_release_device; 118 dev->status = PNP_READY; 119 spin_lock(&pnp_lock); 120 list_add_tail(&dev->global_list, &pnp_global); 121 list_add_tail(&dev->protocol_list, &dev->protocol->devices); 122 spin_unlock(&pnp_lock); 123 124 ret = device_register(&dev->dev); 125 if (ret == 0) 126 pnp_interface_attach_device(dev); 127 return ret; 128 } 129 130 /* 131 * pnp_add_device - adds a pnp device to the pnp layer 132 * @dev: pointer to dev to add 133 * 134 * adds to driver model, name database, fixups, interface, etc. 135 */ 136 137 int pnp_add_device(struct pnp_dev *dev) 138 { 139 if (!dev || !dev->protocol || dev->card) 140 return -EINVAL; 141 dev->dev.parent = &dev->protocol->dev; 142 sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number, dev->number); 143 return __pnp_add_device(dev); 144 } 145 146 void __pnp_remove_device(struct pnp_dev *dev) 147 { 148 spin_lock(&pnp_lock); 149 list_del(&dev->global_list); 150 list_del(&dev->protocol_list); 151 spin_unlock(&pnp_lock); 152 device_unregister(&dev->dev); 153 } 154 155 /** 156 * pnp_remove_device - removes a pnp device from the pnp layer 157 * @dev: pointer to dev to add 158 * 159 * this function will free all mem used by dev 160 */ 161 #if 0 162 void pnp_remove_device(struct pnp_dev *dev) 163 { 164 if (!dev || dev->card) 165 return; 166 __pnp_remove_device(dev); 167 } 168 #endif /* 0 */ 169 170 static int __init pnp_init(void) 171 { 172 printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n"); 173 return bus_register(&pnp_bus_type); 174 } 175 176 subsys_initcall(pnp_init); 177 178 #if 0 179 EXPORT_SYMBOL(pnp_register_protocol); 180 EXPORT_SYMBOL(pnp_unregister_protocol); 181 EXPORT_SYMBOL(pnp_add_device); 182 EXPORT_SYMBOL(pnp_remove_device); 183 #endif /* 0 */ 184