2 * core.c - contains all core device and protocol registration functions
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
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 #include <linux/dma-mapping.h>
22 static LIST_HEAD(pnp_protocols
);
23 LIST_HEAD(pnp_global
);
24 DEFINE_SPINLOCK(pnp_lock
);
27 * ACPI or PNPBIOS should tell us about all platform devices, so we can
28 * skip some blind probes. ISAPNP typically enumerates only plug-in ISA
29 * devices, not built-in things like COM ports.
31 int pnp_platform_devices
;
32 EXPORT_SYMBOL(pnp_platform_devices
);
34 void *pnp_alloc(long size
)
38 result
= kzalloc(size
, GFP_KERNEL
);
40 printk(KERN_ERR
"pnp: Out of Memory\n");
47 * pnp_protocol_register - adds a pnp protocol to the pnp layer
48 * @protocol: pointer to the corresponding pnp_protocol structure
50 * Ex protocols: ISAPNP, PNPBIOS, etc
53 int pnp_register_protocol(struct pnp_protocol
*protocol
)
56 struct list_head
* pos
;
61 INIT_LIST_HEAD(&protocol
->devices
);
62 INIT_LIST_HEAD(&protocol
->cards
);
66 /* assign the lowest unused number */
67 list_for_each(pos
,&pnp_protocols
) {
68 struct pnp_protocol
* cur
= to_pnp_protocol(pos
);
69 if (cur
->number
== nodenum
){
75 list_add_tail(&protocol
->protocol_list
, &pnp_protocols
);
76 spin_unlock(&pnp_lock
);
78 protocol
->number
= nodenum
;
79 sprintf(protocol
->dev
.bus_id
, "pnp%d", nodenum
);
80 return device_register(&protocol
->dev
);
84 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
85 * @protocol: pointer to the corresponding pnp_protocol structure
88 void pnp_unregister_protocol(struct pnp_protocol
*protocol
)
91 list_del(&protocol
->protocol_list
);
92 spin_unlock(&pnp_lock
);
93 device_unregister(&protocol
->dev
);
97 static void pnp_free_ids(struct pnp_dev
*dev
)
100 struct pnp_id
* next
;
111 static void pnp_release_device(struct device
*dmdev
)
113 struct pnp_dev
* dev
= to_pnp_dev(dmdev
);
114 pnp_free_option(dev
->independent
);
115 pnp_free_option(dev
->dependent
);
120 int __pnp_add_device(struct pnp_dev
*dev
)
123 pnp_fixup_device(dev
);
124 dev
->dev
.bus
= &pnp_bus_type
;
125 dev
->dev
.dma_mask
= &dev
->dma_mask
;
126 dev
->dma_mask
= dev
->dev
.coherent_dma_mask
= DMA_24BIT_MASK
;
127 dev
->dev
.release
= &pnp_release_device
;
128 dev
->status
= PNP_READY
;
129 spin_lock(&pnp_lock
);
130 list_add_tail(&dev
->global_list
, &pnp_global
);
131 list_add_tail(&dev
->protocol_list
, &dev
->protocol
->devices
);
132 spin_unlock(&pnp_lock
);
134 ret
= device_register(&dev
->dev
);
136 pnp_interface_attach_device(dev
);
141 * pnp_add_device - adds a pnp device to the pnp layer
142 * @dev: pointer to dev to add
144 * adds to driver model, name database, fixups, interface, etc.
147 int pnp_add_device(struct pnp_dev
*dev
)
149 if (!dev
|| !dev
->protocol
|| dev
->card
)
151 dev
->dev
.parent
= &dev
->protocol
->dev
;
152 sprintf(dev
->dev
.bus_id
, "%02x:%02x", dev
->protocol
->number
, dev
->number
);
153 return __pnp_add_device(dev
);
156 void __pnp_remove_device(struct pnp_dev
*dev
)
158 spin_lock(&pnp_lock
);
159 list_del(&dev
->global_list
);
160 list_del(&dev
->protocol_list
);
161 spin_unlock(&pnp_lock
);
162 device_unregister(&dev
->dev
);
166 * pnp_remove_device - removes a pnp device from the pnp layer
167 * @dev: pointer to dev to add
169 * this function will free all mem used by dev
172 void pnp_remove_device(struct pnp_dev
*dev
)
174 if (!dev
|| dev
->card
)
176 __pnp_remove_device(dev
);
180 static int __init
pnp_init(void)
182 printk(KERN_INFO
"Linux Plug and Play Support v0.97 (c) Adam Belay\n");
183 return bus_register(&pnp_bus_type
);
186 subsys_initcall(pnp_init
);
189 EXPORT_SYMBOL(pnp_register_protocol
);
190 EXPORT_SYMBOL(pnp_unregister_protocol
);
191 EXPORT_SYMBOL(pnp_add_device
);
192 EXPORT_SYMBOL(pnp_remove_device
);