2 * card.c - contains functions for managing groups of PnP devices
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #ifdef CONFIG_PNP_DEBUG
18 #include <linux/pnp.h>
22 LIST_HEAD(pnp_card_drivers
);
25 static const struct pnp_card_device_id
* match_card(struct pnp_card_driver
* drv
, struct pnp_card
* card
)
27 const struct pnp_card_device_id
* drv_id
= drv
->id_table
;
29 if (compare_pnp_id(card
->id
,drv_id
->id
)) {
34 if (i
== PNP_MAX_DEVICES
|| ! *drv_id
->devs
[i
].id
)
37 card_for_each_dev(card
, dev
) {
38 if (compare_pnp_id(dev
->id
, drv_id
->devs
[i
].id
)) {
53 static void card_remove(struct pnp_dev
* dev
)
55 dev
->card_link
= NULL
;
58 static void card_remove_first(struct pnp_dev
* dev
)
60 struct pnp_card_driver
* drv
= to_pnp_card_driver(dev
->driver
);
61 if (!dev
->card
|| !drv
)
64 drv
->remove(dev
->card_link
);
65 drv
->link
.remove
= &card_remove
;
66 kfree(dev
->card_link
);
70 static int card_probe(struct pnp_card
* card
, struct pnp_card_driver
* drv
)
72 const struct pnp_card_device_id
*id
= match_card(drv
,card
);
74 struct pnp_card_link
* clink
= pnp_alloc(sizeof(struct pnp_card_link
));
80 if (drv
->probe(clink
, id
)>=0)
84 card_for_each_dev(card
, dev
) {
85 if (dev
->card_link
== clink
)
86 pnp_release_card_device(dev
);
97 * pnp_add_card_id - adds an EISA id to the specified card
98 * @id: pointer to a pnp_id structure
99 * @card: pointer to the desired card
103 int pnp_add_card_id(struct pnp_id
*id
, struct pnp_card
* card
)
112 while (ptr
&& ptr
->next
)
121 static void pnp_free_card_ids(struct pnp_card
* card
)
135 static void pnp_release_card(struct device
*dmdev
)
137 struct pnp_card
* card
= to_pnp_card(dmdev
);
138 pnp_free_card_ids(card
);
143 static ssize_t
pnp_show_card_name(struct device
*dmdev
, char *buf
)
146 struct pnp_card
*card
= to_pnp_card(dmdev
);
147 str
+= sprintf(str
,"%s\n", card
->name
);
151 static DEVICE_ATTR(name
,S_IRUGO
,pnp_show_card_name
,NULL
);
153 static ssize_t
pnp_show_card_ids(struct device
*dmdev
, char *buf
)
156 struct pnp_card
*card
= to_pnp_card(dmdev
);
157 struct pnp_id
* pos
= card
->id
;
160 str
+= sprintf(str
,"%s\n", pos
->id
);
166 static DEVICE_ATTR(card_id
,S_IRUGO
,pnp_show_card_ids
,NULL
);
168 static int pnp_interface_attach_card(struct pnp_card
*card
)
170 device_create_file(&card
->dev
,&dev_attr_name
);
171 device_create_file(&card
->dev
,&dev_attr_card_id
);
176 * pnp_add_card - adds a PnP card to the PnP Layer
177 * @card: pointer to the card to add
180 int pnp_add_card(struct pnp_card
* card
)
183 struct list_head
* pos
, * temp
;
184 if (!card
|| !card
->protocol
)
187 sprintf(card
->dev
.bus_id
, "%02x:%02x", card
->protocol
->number
, card
->number
);
188 card
->dev
.parent
= &card
->protocol
->dev
;
189 card
->dev
.bus
= NULL
;
190 card
->dev
.release
= &pnp_release_card
;
191 error
= device_register(&card
->dev
);
194 pnp_interface_attach_card(card
);
195 spin_lock(&pnp_lock
);
196 list_add_tail(&card
->global_list
, &pnp_cards
);
197 list_add_tail(&card
->protocol_list
, &card
->protocol
->cards
);
198 spin_unlock(&pnp_lock
);
200 /* we wait until now to add devices in order to ensure the drivers
201 * will be able to use all of the related devices on the card
202 * without waiting any unresonable length of time */
203 list_for_each(pos
,&card
->devices
){
204 struct pnp_dev
*dev
= card_to_pnp_dev(pos
);
205 __pnp_add_device(dev
);
208 /* match with card drivers */
209 list_for_each_safe(pos
,temp
,&pnp_card_drivers
){
210 struct pnp_card_driver
* drv
= list_entry(pos
, struct pnp_card_driver
, global_list
);
211 card_probe(card
,drv
);
214 pnp_err("sysfs failure, card '%s' will be unavailable", card
->dev
.bus_id
);
219 * pnp_remove_card - removes a PnP card from the PnP Layer
220 * @card: pointer to the card to remove
223 void pnp_remove_card(struct pnp_card
* card
)
225 struct list_head
*pos
, *temp
;
228 device_unregister(&card
->dev
);
229 spin_lock(&pnp_lock
);
230 list_del(&card
->global_list
);
231 list_del(&card
->protocol_list
);
232 spin_unlock(&pnp_lock
);
233 list_for_each_safe(pos
,temp
,&card
->devices
){
234 struct pnp_dev
*dev
= card_to_pnp_dev(pos
);
235 pnp_remove_card_device(dev
);
240 * pnp_add_card_device - adds a device to the specified card
241 * @card: pointer to the card to add to
242 * @dev: pointer to the device to add
245 int pnp_add_card_device(struct pnp_card
* card
, struct pnp_dev
* dev
)
247 if (!card
|| !dev
|| !dev
->protocol
)
249 dev
->dev
.parent
= &card
->dev
;
250 dev
->card_link
= NULL
;
251 snprintf(dev
->dev
.bus_id
, BUS_ID_SIZE
, "%02x:%02x.%02x", dev
->protocol
->number
,
252 card
->number
,dev
->number
);
253 spin_lock(&pnp_lock
);
255 list_add_tail(&dev
->card_list
, &card
->devices
);
256 spin_unlock(&pnp_lock
);
261 * pnp_remove_card_device- removes a device from the specified card
262 * @card: pointer to the card to remove from
263 * @dev: pointer to the device to remove
266 void pnp_remove_card_device(struct pnp_dev
* dev
)
268 spin_lock(&pnp_lock
);
270 list_del(&dev
->card_list
);
271 spin_unlock(&pnp_lock
);
272 __pnp_remove_device(dev
);
276 * pnp_request_card_device - Searches for a PnP device under the specified card
277 * @lcard: pointer to the card link, cannot be NULL
278 * @id: pointer to a PnP ID structure that explains the rules for finding the device
279 * @from: Starting place to search from. If NULL it will start from the begining.
282 struct pnp_dev
* pnp_request_card_device(struct pnp_card_link
*clink
, const char * id
, struct pnp_dev
* from
)
284 struct list_head
* pos
;
285 struct pnp_dev
* dev
;
286 struct pnp_card_driver
* drv
;
287 struct pnp_card
* card
;
293 pos
= card
->devices
.next
;
295 if (from
->card
!= card
)
297 pos
= from
->card_list
.next
;
299 while (pos
!= &card
->devices
) {
300 dev
= card_to_pnp_dev(pos
);
301 if ((!dev
->card_link
) && compare_pnp_id(dev
->id
,id
))
310 down_write(&dev
->dev
.bus
->subsys
.rwsem
);
311 dev
->card_link
= clink
;
312 dev
->dev
.driver
= &drv
->link
.driver
;
313 if (drv
->link
.driver
.probe
) {
314 if (drv
->link
.driver
.probe(&dev
->dev
)) {
315 dev
->dev
.driver
= NULL
;
319 device_bind_driver(&dev
->dev
);
320 up_write(&dev
->dev
.bus
->subsys
.rwsem
);
326 * pnp_release_card_device - call this when the driver no longer needs the device
327 * @dev: pointer to the PnP device stucture
330 void pnp_release_card_device(struct pnp_dev
* dev
)
332 struct pnp_card_driver
* drv
= dev
->card_link
->driver
;
335 down_write(&dev
->dev
.bus
->subsys
.rwsem
);
336 drv
->link
.remove
= &card_remove
;
337 device_release_driver(&dev
->dev
);
338 drv
->link
.remove
= &card_remove_first
;
339 up_write(&dev
->dev
.bus
->subsys
.rwsem
);
343 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
344 * @drv: pointer to the driver to register
347 int pnp_register_card_driver(struct pnp_card_driver
* drv
)
350 struct list_head
*pos
, *temp
;
352 drv
->link
.name
= drv
->name
;
353 drv
->link
.id_table
= NULL
; /* this will disable auto matching */
354 drv
->link
.flags
= drv
->flags
;
355 drv
->link
.probe
= NULL
;
356 drv
->link
.remove
= &card_remove_first
;
358 spin_lock(&pnp_lock
);
359 list_add_tail(&drv
->global_list
, &pnp_card_drivers
);
360 spin_unlock(&pnp_lock
);
361 pnp_register_driver(&drv
->link
);
363 list_for_each_safe(pos
,temp
,&pnp_cards
){
364 struct pnp_card
*card
= list_entry(pos
, struct pnp_card
, global_list
);
365 count
+= card_probe(card
,drv
);
371 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
372 * @drv: pointer to the driver to unregister
375 void pnp_unregister_card_driver(struct pnp_card_driver
* drv
)
377 spin_lock(&pnp_lock
);
378 list_del(&drv
->global_list
);
379 spin_unlock(&pnp_lock
);
380 pnp_unregister_driver(&drv
->link
);
383 EXPORT_SYMBOL(pnp_add_card
);
384 EXPORT_SYMBOL(pnp_remove_card
);
385 EXPORT_SYMBOL(pnp_add_card_device
);
386 EXPORT_SYMBOL(pnp_remove_card_device
);
387 EXPORT_SYMBOL(pnp_add_card_id
);
388 EXPORT_SYMBOL(pnp_request_card_device
);
389 EXPORT_SYMBOL(pnp_release_card_device
);
390 EXPORT_SYMBOL(pnp_register_card_driver
);
391 EXPORT_SYMBOL(pnp_unregister_card_driver
);