blob: 3e20b1cc7778930f2477dc626fc281606b7e320b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David Brownell2e17c552007-05-08 00:25:29 -070017#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "base.h"
20
21
22static LIST_HEAD(pnp_protocols);
23LIST_HEAD(pnp_global);
24DEFINE_SPINLOCK(pnp_lock);
25
Bjorn Helgaas8f81dd12007-05-08 00:35:54 -070026/*
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.
30 */
31int pnp_platform_devices;
32EXPORT_SYMBOL(pnp_platform_devices);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034void *pnp_alloc(long size)
35{
36 void *result;
37
38 result = kmalloc(size, GFP_KERNEL);
39 if (!result){
40 printk(KERN_ERR "pnp: Out of Memory\n");
41 return NULL;
42 }
43 memset(result, 0, size);
44 return result;
45}
46
47/**
48 * pnp_protocol_register - adds a pnp protocol to the pnp layer
49 * @protocol: pointer to the corresponding pnp_protocol structure
50 *
51 * Ex protocols: ISAPNP, PNPBIOS, etc
52 */
53
54int pnp_register_protocol(struct pnp_protocol *protocol)
55{
56 int nodenum;
57 struct list_head * pos;
58
59 if (!protocol)
60 return -EINVAL;
61
62 INIT_LIST_HEAD(&protocol->devices);
63 INIT_LIST_HEAD(&protocol->cards);
64 nodenum = 0;
65 spin_lock(&pnp_lock);
66
67 /* assign the lowest unused number */
68 list_for_each(pos,&pnp_protocols) {
69 struct pnp_protocol * cur = to_pnp_protocol(pos);
70 if (cur->number == nodenum){
71 pos = &pnp_protocols;
72 nodenum++;
73 }
74 }
75
76 list_add_tail(&protocol->protocol_list, &pnp_protocols);
77 spin_unlock(&pnp_lock);
78
79 protocol->number = nodenum;
80 sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
81 return device_register(&protocol->dev);
82}
83
84/**
85 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
86 * @protocol: pointer to the corresponding pnp_protocol structure
87 *
88 */
89void pnp_unregister_protocol(struct pnp_protocol *protocol)
90{
91 spin_lock(&pnp_lock);
92 list_del(&protocol->protocol_list);
93 spin_unlock(&pnp_lock);
94 device_unregister(&protocol->dev);
95}
96
97
98static void pnp_free_ids(struct pnp_dev *dev)
99{
100 struct pnp_id * id;
101 struct pnp_id * next;
102 if (!dev)
103 return;
104 id = dev->id;
105 while (id) {
106 next = id->next;
107 kfree(id);
108 id = next;
109 }
110}
111
112static void pnp_release_device(struct device *dmdev)
113{
114 struct pnp_dev * dev = to_pnp_dev(dmdev);
115 pnp_free_option(dev->independent);
116 pnp_free_option(dev->dependent);
117 pnp_free_ids(dev);
118 kfree(dev);
119}
120
121int __pnp_add_device(struct pnp_dev *dev)
122{
123 int ret;
124 pnp_fixup_device(dev);
125 dev->dev.bus = &pnp_bus_type;
David Brownell2e17c552007-05-08 00:25:29 -0700126 dev->dev.dma_mask = &dev->dma_mask;
127 dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 dev->dev.release = &pnp_release_device;
129 dev->status = PNP_READY;
130 spin_lock(&pnp_lock);
131 list_add_tail(&dev->global_list, &pnp_global);
132 list_add_tail(&dev->protocol_list, &dev->protocol->devices);
133 spin_unlock(&pnp_lock);
134
135 ret = device_register(&dev->dev);
136 if (ret == 0)
137 pnp_interface_attach_device(dev);
138 return ret;
139}
140
141/*
142 * pnp_add_device - adds a pnp device to the pnp layer
143 * @dev: pointer to dev to add
144 *
145 * adds to driver model, name database, fixups, interface, etc.
146 */
147
148int pnp_add_device(struct pnp_dev *dev)
149{
150 if (!dev || !dev->protocol || dev->card)
151 return -EINVAL;
152 dev->dev.parent = &dev->protocol->dev;
153 sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number, dev->number);
154 return __pnp_add_device(dev);
155}
156
157void __pnp_remove_device(struct pnp_dev *dev)
158{
159 spin_lock(&pnp_lock);
160 list_del(&dev->global_list);
161 list_del(&dev->protocol_list);
162 spin_unlock(&pnp_lock);
163 device_unregister(&dev->dev);
164}
165
166/**
167 * pnp_remove_device - removes a pnp device from the pnp layer
168 * @dev: pointer to dev to add
169 *
170 * this function will free all mem used by dev
171 */
Adrian Bunkb449f632005-11-07 01:01:48 -0800172#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173void pnp_remove_device(struct pnp_dev *dev)
174{
175 if (!dev || dev->card)
176 return;
177 __pnp_remove_device(dev);
178}
Adrian Bunkb449f632005-11-07 01:01:48 -0800179#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181static int __init pnp_init(void)
182{
183 printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
184 return bus_register(&pnp_bus_type);
185}
186
187subsys_initcall(pnp_init);
188
Adrian Bunkb449f632005-11-07 01:01:48 -0800189#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190EXPORT_SYMBOL(pnp_register_protocol);
191EXPORT_SYMBOL(pnp_unregister_protocol);
192EXPORT_SYMBOL(pnp_add_device);
193EXPORT_SYMBOL(pnp_remove_device);
Adrian Bunkb449f632005-11-07 01:01:48 -0800194#endif /* 0 */