blob: 1dfdc325211dab8b3cbf1595c9ee05e5a7f33fa0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static LIST_HEAD(pnp_protocols);
22LIST_HEAD(pnp_global);
23DEFINE_SPINLOCK(pnp_lock);
24
Bjorn Helgaas8f81dd12007-05-08 00:35:54 -070025/*
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 */
30int pnp_platform_devices;
31EXPORT_SYMBOL(pnp_platform_devices);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033void *pnp_alloc(long size)
34{
35 void *result;
36
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070037 result = kzalloc(size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070038 if (!result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 printk(KERN_ERR "pnp: Out of Memory\n");
40 return NULL;
41 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 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
52int pnp_register_protocol(struct pnp_protocol *protocol)
53{
54 int nodenum;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070055 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070066 list_for_each(pos, &pnp_protocols) {
67 struct pnp_protocol *cur = to_pnp_protocol(pos);
68 if (cur->number == nodenum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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 */
87void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static void pnp_free_ids(struct pnp_dev *dev)
96{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070097 struct pnp_id *id;
98 struct pnp_id *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (!dev)
100 return;
101 id = dev->id;
102 while (id) {
103 next = id->next;
104 kfree(id);
105 id = next;
106 }
107}
108
109static void pnp_release_device(struct device *dmdev)
110{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700111 struct pnp_dev *dev = to_pnp_dev(dmdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 pnp_free_option(dev->independent);
113 pnp_free_option(dev->dependent);
114 pnp_free_ids(dev);
115 kfree(dev);
116}
117
118int __pnp_add_device(struct pnp_dev *dev)
119{
120 int ret;
121 pnp_fixup_device(dev);
122 dev->dev.bus = &pnp_bus_type;
David Brownell2e17c552007-05-08 00:25:29 -0700123 dev->dev.dma_mask = &dev->dma_mask;
124 dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 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
145int 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;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700150 sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number,
151 dev->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return __pnp_add_device(dev);
153}
154
155void __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 */
Adrian Bunkb449f632005-11-07 01:01:48 -0800170#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171void pnp_remove_device(struct pnp_dev *dev)
172{
173 if (!dev || dev->card)
174 return;
175 __pnp_remove_device(dev);
176}
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700177#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179static 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
185subsys_initcall(pnp_init);
186
Adrian Bunkb449f632005-11-07 01:01:48 -0800187#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188EXPORT_SYMBOL(pnp_register_protocol);
189EXPORT_SYMBOL(pnp_unregister_protocol);
190EXPORT_SYMBOL(pnp_add_device);
191EXPORT_SYMBOL(pnp_remove_device);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700192#endif /* 0 */