PNP: Lindent all source files
[powerpc.git] / drivers / pnp / core.c
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 #include <linux/dma-mapping.h>
18
19 #include "base.h"
20
21 static LIST_HEAD(pnp_protocols);
22 LIST_HEAD(pnp_global);
23 DEFINE_SPINLOCK(pnp_lock);
24
25 /*
26  * ACPI or PNPBIOS should tell us about all platform devices, so we can
27  * skip some blind probes.  ISAPNP typically enumerates only plug-in ISA
28  * devices, not built-in things like COM ports.
29  */
30 int pnp_platform_devices;
31 EXPORT_SYMBOL(pnp_platform_devices);
32
33 void *pnp_alloc(long size)
34 {
35         void *result;
36
37         result = kzalloc(size, GFP_KERNEL);
38         if (!result) {
39                 printk(KERN_ERR "pnp: Out of Memory\n");
40                 return NULL;
41         }
42         return result;
43 }
44
45 /**
46  * pnp_protocol_register - adds a pnp protocol to the pnp layer
47  * @protocol: pointer to the corresponding pnp_protocol structure
48  *
49  *  Ex protocols: ISAPNP, PNPBIOS, etc
50  */
51
52 int pnp_register_protocol(struct pnp_protocol *protocol)
53 {
54         int nodenum;
55         struct list_head *pos;
56
57         if (!protocol)
58                 return -EINVAL;
59
60         INIT_LIST_HEAD(&protocol->devices);
61         INIT_LIST_HEAD(&protocol->cards);
62         nodenum = 0;
63         spin_lock(&pnp_lock);
64
65         /* assign the lowest unused number */
66         list_for_each(pos, &pnp_protocols) {
67                 struct pnp_protocol *cur = to_pnp_protocol(pos);
68                 if (cur->number == nodenum) {
69                         pos = &pnp_protocols;
70                         nodenum++;
71                 }
72         }
73
74         list_add_tail(&protocol->protocol_list, &pnp_protocols);
75         spin_unlock(&pnp_lock);
76
77         protocol->number = nodenum;
78         sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
79         return device_register(&protocol->dev);
80 }
81
82 /**
83  * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
84  * @protocol: pointer to the corresponding pnp_protocol structure
85  *
86  */
87 void pnp_unregister_protocol(struct pnp_protocol *protocol)
88 {
89         spin_lock(&pnp_lock);
90         list_del(&protocol->protocol_list);
91         spin_unlock(&pnp_lock);
92         device_unregister(&protocol->dev);
93 }
94
95 static void pnp_free_ids(struct pnp_dev *dev)
96 {
97         struct pnp_id *id;
98         struct pnp_id *next;
99         if (!dev)
100                 return;
101         id = dev->id;
102         while (id) {
103                 next = id->next;
104                 kfree(id);
105                 id = next;
106         }
107 }
108
109 static void pnp_release_device(struct device *dmdev)
110 {
111         struct pnp_dev *dev = to_pnp_dev(dmdev);
112         pnp_free_option(dev->independent);
113         pnp_free_option(dev->dependent);
114         pnp_free_ids(dev);
115         kfree(dev);
116 }
117
118 int __pnp_add_device(struct pnp_dev *dev)
119 {
120         int ret;
121         pnp_fixup_device(dev);
122         dev->dev.bus = &pnp_bus_type;
123         dev->dev.dma_mask = &dev->dma_mask;
124         dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
125         dev->dev.release = &pnp_release_device;
126         dev->status = PNP_READY;
127         spin_lock(&pnp_lock);
128         list_add_tail(&dev->global_list, &pnp_global);
129         list_add_tail(&dev->protocol_list, &dev->protocol->devices);
130         spin_unlock(&pnp_lock);
131
132         ret = device_register(&dev->dev);
133         if (ret == 0)
134                 pnp_interface_attach_device(dev);
135         return ret;
136 }
137
138 /*
139  * pnp_add_device - adds a pnp device to the pnp layer
140  * @dev: pointer to dev to add
141  *
142  *  adds to driver model, name database, fixups, interface, etc.
143  */
144
145 int pnp_add_device(struct pnp_dev *dev)
146 {
147         if (!dev || !dev->protocol || dev->card)
148                 return -EINVAL;
149         dev->dev.parent = &dev->protocol->dev;
150         sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number,
151                 dev->number);
152         return __pnp_add_device(dev);
153 }
154
155 void __pnp_remove_device(struct pnp_dev *dev)
156 {
157         spin_lock(&pnp_lock);
158         list_del(&dev->global_list);
159         list_del(&dev->protocol_list);
160         spin_unlock(&pnp_lock);
161         device_unregister(&dev->dev);
162 }
163
164 /**
165  * pnp_remove_device - removes a pnp device from the pnp layer
166  * @dev: pointer to dev to add
167  *
168  * this function will free all mem used by dev
169  */
170 #if 0
171 void pnp_remove_device(struct pnp_dev *dev)
172 {
173         if (!dev || dev->card)
174                 return;
175         __pnp_remove_device(dev);
176 }
177 #endif /*  0  */
178
179 static int __init pnp_init(void)
180 {
181         printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
182         return bus_register(&pnp_bus_type);
183 }
184
185 subsys_initcall(pnp_init);
186
187 #if 0
188 EXPORT_SYMBOL(pnp_register_protocol);
189 EXPORT_SYMBOL(pnp_unregister_protocol);
190 EXPORT_SYMBOL(pnp_add_device);
191 EXPORT_SYMBOL(pnp_remove_device);
192 #endif /*  0  */