blob: 8e7b2dd3881086de3d95a8c6d9f34357827306a1 [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
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070038 result = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (!result){
40 printk(KERN_ERR "pnp: Out of Memory\n");
41 return NULL;
42 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return result;
44}
45
46/**
47 * pnp_protocol_register - adds a pnp protocol to the pnp layer
48 * @protocol: pointer to the corresponding pnp_protocol structure
49 *
50 * Ex protocols: ISAPNP, PNPBIOS, etc
51 */
52
53int pnp_register_protocol(struct pnp_protocol *protocol)
54{
55 int nodenum;
56 struct list_head * pos;
57
58 if (!protocol)
59 return -EINVAL;
60
61 INIT_LIST_HEAD(&protocol->devices);
62 INIT_LIST_HEAD(&protocol->cards);
63 nodenum = 0;
64 spin_lock(&pnp_lock);
65
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){
70 pos = &pnp_protocols;
71 nodenum++;
72 }
73 }
74
75 list_add_tail(&protocol->protocol_list, &pnp_protocols);
76 spin_unlock(&pnp_lock);
77
78 protocol->number = nodenum;
79 sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
80 return device_register(&protocol->dev);
81}
82
83/**
84 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
85 * @protocol: pointer to the corresponding pnp_protocol structure
86 *
87 */
88void pnp_unregister_protocol(struct pnp_protocol *protocol)
89{
90 spin_lock(&pnp_lock);
91 list_del(&protocol->protocol_list);
92 spin_unlock(&pnp_lock);
93 device_unregister(&protocol->dev);
94}
95
96
97static void pnp_free_ids(struct pnp_dev *dev)
98{
99 struct pnp_id * id;
100 struct pnp_id * next;
101 if (!dev)
102 return;
103 id = dev->id;
104 while (id) {
105 next = id->next;
106 kfree(id);
107 id = next;
108 }
109}
110
111static void pnp_release_device(struct device *dmdev)
112{
113 struct pnp_dev * dev = to_pnp_dev(dmdev);
114 pnp_free_option(dev->independent);
115 pnp_free_option(dev->dependent);
116 pnp_free_ids(dev);
117 kfree(dev);
118}
119
120int __pnp_add_device(struct pnp_dev *dev)
121{
122 int ret;
123 pnp_fixup_device(dev);
124 dev->dev.bus = &pnp_bus_type;
David Brownell2e17c552007-05-08 00:25:29 -0700125 dev->dev.dma_mask = &dev->dma_mask;
126 dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 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);
133
134 ret = device_register(&dev->dev);
135 if (ret == 0)
136 pnp_interface_attach_device(dev);
137 return ret;
138}
139
140/*
141 * pnp_add_device - adds a pnp device to the pnp layer
142 * @dev: pointer to dev to add
143 *
144 * adds to driver model, name database, fixups, interface, etc.
145 */
146
147int pnp_add_device(struct pnp_dev *dev)
148{
149 if (!dev || !dev->protocol || dev->card)
150 return -EINVAL;
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);
154}
155
156void __pnp_remove_device(struct pnp_dev *dev)
157{
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);
163}
164
165/**
166 * pnp_remove_device - removes a pnp device from the pnp layer
167 * @dev: pointer to dev to add
168 *
169 * this function will free all mem used by dev
170 */
Adrian Bunkb449f632005-11-07 01:01:48 -0800171#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172void pnp_remove_device(struct pnp_dev *dev)
173{
174 if (!dev || dev->card)
175 return;
176 __pnp_remove_device(dev);
177}
Adrian Bunkb449f632005-11-07 01:01:48 -0800178#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180static int __init pnp_init(void)
181{
182 printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
183 return bus_register(&pnp_bus_type);
184}
185
186subsys_initcall(pnp_init);
187
Adrian Bunkb449f632005-11-07 01:01:48 -0800188#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189EXPORT_SYMBOL(pnp_register_protocol);
190EXPORT_SYMBOL(pnp_unregister_protocol);
191EXPORT_SYMBOL(pnp_add_device);
192EXPORT_SYMBOL(pnp_remove_device);
Adrian Bunkb449f632005-11-07 01:01:48 -0800193#endif /* 0 */