2 * driver.c - device id matching, driver model, etc.
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
8 #include <linux/config.h>
9 #include <linux/string.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/ctype.h>
13 #include <linux/slab.h>
15 #ifdef CONFIG_PNP_DEBUG
21 #include <linux/pnp.h>
24 static int compare_func(const char *ida
, const char *idb
)
27 /* we only need to compare the last 4 chars */
32 toupper(ida
[i
]) != toupper(idb
[i
]))
38 int compare_pnp_id(struct pnp_id
*pos
, const char *id
)
40 if (!pos
|| !id
|| (strlen(id
) != 7))
42 if (memcmp(id
,"ANYDEVS",7)==0)
45 if (memcmp(pos
->id
,id
,3)==0)
46 if (compare_func(pos
->id
,id
)==1)
53 static const struct pnp_device_id
* match_device(struct pnp_driver
*drv
, struct pnp_dev
*dev
)
55 const struct pnp_device_id
*drv_id
= drv
->id_table
;
60 if (compare_pnp_id(dev
->id
, drv_id
->id
))
67 int pnp_device_attach(struct pnp_dev
*pnp_dev
)
70 if(pnp_dev
->status
!= PNP_READY
){
71 spin_unlock(&pnp_lock
);
74 pnp_dev
->status
= PNP_ATTACHED
;
75 spin_unlock(&pnp_lock
);
79 void pnp_device_detach(struct pnp_dev
*pnp_dev
)
82 if (pnp_dev
->status
== PNP_ATTACHED
)
83 pnp_dev
->status
= PNP_READY
;
84 spin_unlock(&pnp_lock
);
85 pnp_disable_dev(pnp_dev
);
88 static int pnp_device_probe(struct device
*dev
)
91 struct pnp_driver
*pnp_drv
;
92 struct pnp_dev
*pnp_dev
;
93 const struct pnp_device_id
*dev_id
= NULL
;
94 pnp_dev
= to_pnp_dev(dev
);
95 pnp_drv
= to_pnp_driver(dev
->driver
);
97 pnp_dbg("match found with the PnP device '%s' and the driver '%s'", dev
->bus_id
,pnp_drv
->name
);
99 error
= pnp_device_attach(pnp_dev
);
103 if (pnp_dev
->active
== 0) {
104 if (!(pnp_drv
->flags
& PNP_DRIVER_RES_DO_NOT_CHANGE
)) {
105 error
= pnp_activate_dev(pnp_dev
);
109 } else if ((pnp_drv
->flags
& PNP_DRIVER_RES_DISABLE
)
110 == PNP_DRIVER_RES_DISABLE
) {
111 error
= pnp_disable_dev(pnp_dev
);
116 if (pnp_drv
->probe
) {
117 dev_id
= match_device(pnp_drv
, pnp_dev
);
119 error
= pnp_drv
->probe(pnp_dev
, dev_id
);
122 pnp_dev
->driver
= pnp_drv
;
129 pnp_device_detach(pnp_dev
);
133 static int pnp_device_remove(struct device
*dev
)
135 struct pnp_dev
* pnp_dev
= to_pnp_dev(dev
);
136 struct pnp_driver
* drv
= pnp_dev
->driver
;
140 drv
->remove(pnp_dev
);
141 pnp_dev
->driver
= NULL
;
143 pnp_device_detach(pnp_dev
);
147 static int pnp_bus_match(struct device
*dev
, struct device_driver
*drv
)
149 struct pnp_dev
* pnp_dev
= to_pnp_dev(dev
);
150 struct pnp_driver
* pnp_drv
= to_pnp_driver(drv
);
151 if (match_device(pnp_drv
, pnp_dev
) == NULL
)
157 struct bus_type pnp_bus_type
= {
159 .match
= pnp_bus_match
,
163 int pnp_register_driver(struct pnp_driver
*drv
)
166 struct list_head
*pos
;
168 pnp_dbg("the driver '%s' has been registered", drv
->name
);
170 drv
->driver
.name
= drv
->name
;
171 drv
->driver
.bus
= &pnp_bus_type
;
172 drv
->driver
.probe
= pnp_device_probe
;
173 drv
->driver
.remove
= pnp_device_remove
;
175 count
= driver_register(&drv
->driver
);
177 /* get the number of initial matches */
180 list_for_each(pos
,&drv
->driver
.devices
){
187 void pnp_unregister_driver(struct pnp_driver
*drv
)
189 driver_unregister(&drv
->driver
);
190 pnp_dbg("the driver '%s' has been unregistered", drv
->name
);
194 * pnp_add_id - adds an EISA id to the specified device
195 * @id: pointer to a pnp_id structure
196 * @dev: pointer to the desired device
200 int pnp_add_id(struct pnp_id
*id
, struct pnp_dev
*dev
)
209 while (ptr
&& ptr
->next
)
218 EXPORT_SYMBOL(pnp_register_driver
);
219 EXPORT_SYMBOL(pnp_unregister_driver
);
220 EXPORT_SYMBOL(pnp_add_id
);
221 EXPORT_SYMBOL(pnp_device_attach
);
222 EXPORT_SYMBOL(pnp_device_detach
);