2 * card.c - contains functions for managing groups of PnP devices
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/pnp.h>
14 static LIST_HEAD(pnp_card_drivers
);
16 static const struct pnp_card_device_id
*match_card(struct pnp_card_driver
*drv
,
17 struct pnp_card
*card
)
19 const struct pnp_card_device_id
*drv_id
= drv
->id_table
;
21 if (compare_pnp_id(card
->id
, drv_id
->id
)) {
26 if (i
== PNP_MAX_DEVICES
27 || !*drv_id
->devs
[i
].id
)
30 card_for_each_dev(card
, dev
) {
32 (dev
->id
, drv_id
->devs
[i
].id
)) {
47 static void card_remove(struct pnp_dev
*dev
)
49 dev
->card_link
= NULL
;
52 static void card_remove_first(struct pnp_dev
*dev
)
54 struct pnp_card_driver
*drv
= to_pnp_card_driver(dev
->driver
);
55 if (!dev
->card
|| !drv
)
58 drv
->remove(dev
->card_link
);
59 drv
->link
.remove
= &card_remove
;
60 kfree(dev
->card_link
);
64 static int card_probe(struct pnp_card
*card
, struct pnp_card_driver
*drv
)
66 const struct pnp_card_device_id
*id
;
67 struct pnp_card_link
*clink
;
72 id
= match_card(drv
, card
);
76 clink
= pnp_alloc(sizeof(*clink
));
81 clink
->pm_state
= PMSG_ON
;
83 if (drv
->probe(clink
, id
) >= 0)
87 card_for_each_dev(card
, dev
) {
88 if (dev
->card_link
== clink
)
89 pnp_release_card_device(dev
);
96 * pnp_add_card_id - adds an EISA id to the specified card
97 * @id: pointer to a pnp_id structure
98 * @card: pointer to the desired card
102 int pnp_add_card_id(struct pnp_id
*id
, struct pnp_card
*card
)
111 while (ptr
&& ptr
->next
)
120 static void pnp_free_card_ids(struct pnp_card
*card
)
134 static void pnp_release_card(struct device
*dmdev
)
136 struct pnp_card
*card
= to_pnp_card(dmdev
);
137 pnp_free_card_ids(card
);
141 static ssize_t
pnp_show_card_name(struct device
*dmdev
,
142 struct device_attribute
*attr
, char *buf
)
145 struct pnp_card
*card
= to_pnp_card(dmdev
);
146 str
+= sprintf(str
, "%s\n", card
->name
);
150 static DEVICE_ATTR(name
, S_IRUGO
, pnp_show_card_name
, NULL
);
152 static ssize_t
pnp_show_card_ids(struct device
*dmdev
,
153 struct device_attribute
*attr
, 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 int rc
= device_create_file(&card
->dev
, &dev_attr_name
);
174 rc
= device_create_file(&card
->dev
, &dev_attr_card_id
);
181 device_remove_file(&card
->dev
, &dev_attr_name
);
186 * pnp_add_card - adds a PnP card to the PnP Layer
187 * @card: pointer to the card to add
190 int pnp_add_card(struct pnp_card
*card
)
193 struct list_head
*pos
, *temp
;
194 if (!card
|| !card
->protocol
)
197 sprintf(card
->dev
.bus_id
, "%02x:%02x", card
->protocol
->number
,
199 card
->dev
.parent
= &card
->protocol
->dev
;
200 card
->dev
.bus
= NULL
;
201 card
->dev
.release
= &pnp_release_card
;
202 error
= device_register(&card
->dev
);
205 pnp_interface_attach_card(card
);
206 spin_lock(&pnp_lock
);
207 list_add_tail(&card
->global_list
, &pnp_cards
);
208 list_add_tail(&card
->protocol_list
, &card
->protocol
->cards
);
209 spin_unlock(&pnp_lock
);
211 /* we wait until now to add devices in order to ensure the drivers
212 * will be able to use all of the related devices on the card
213 * without waiting any unresonable length of time */
214 list_for_each(pos
, &card
->devices
) {
215 struct pnp_dev
*dev
= card_to_pnp_dev(pos
);
216 __pnp_add_device(dev
);
219 /* match with card drivers */
220 list_for_each_safe(pos
, temp
, &pnp_card_drivers
) {
221 struct pnp_card_driver
*drv
=
222 list_entry(pos
, struct pnp_card_driver
,
224 card_probe(card
, drv
);
227 pnp_err("sysfs failure, card '%s' will be unavailable",
233 * pnp_remove_card - removes a PnP card from the PnP Layer
234 * @card: pointer to the card to remove
237 void pnp_remove_card(struct pnp_card
*card
)
239 struct list_head
*pos
, *temp
;
242 device_unregister(&card
->dev
);
243 spin_lock(&pnp_lock
);
244 list_del(&card
->global_list
);
245 list_del(&card
->protocol_list
);
246 spin_unlock(&pnp_lock
);
247 list_for_each_safe(pos
, temp
, &card
->devices
) {
248 struct pnp_dev
*dev
= card_to_pnp_dev(pos
);
249 pnp_remove_card_device(dev
);
254 * pnp_add_card_device - adds a device to the specified card
255 * @card: pointer to the card to add to
256 * @dev: pointer to the device to add
259 int pnp_add_card_device(struct pnp_card
*card
, struct pnp_dev
*dev
)
261 if (!card
|| !dev
|| !dev
->protocol
)
263 dev
->dev
.parent
= &card
->dev
;
264 dev
->card_link
= NULL
;
265 snprintf(dev
->dev
.bus_id
, BUS_ID_SIZE
, "%02x:%02x.%02x",
266 dev
->protocol
->number
, card
->number
, dev
->number
);
267 spin_lock(&pnp_lock
);
269 list_add_tail(&dev
->card_list
, &card
->devices
);
270 spin_unlock(&pnp_lock
);
275 * pnp_remove_card_device- removes a device from the specified card
276 * @dev: pointer to the device to remove
279 void pnp_remove_card_device(struct pnp_dev
*dev
)
281 spin_lock(&pnp_lock
);
283 list_del(&dev
->card_list
);
284 spin_unlock(&pnp_lock
);
285 __pnp_remove_device(dev
);
289 * pnp_request_card_device - Searches for a PnP device under the specified card
290 * @clink: pointer to the card link, cannot be NULL
291 * @id: pointer to a PnP ID structure that explains the rules for finding the device
292 * @from: Starting place to search from. If NULL it will start from the begining.
295 struct pnp_dev
*pnp_request_card_device(struct pnp_card_link
*clink
,
296 const char *id
, struct pnp_dev
*from
)
298 struct list_head
*pos
;
300 struct pnp_card_driver
*drv
;
301 struct pnp_card
*card
;
307 pos
= card
->devices
.next
;
309 if (from
->card
!= card
)
311 pos
= from
->card_list
.next
;
313 while (pos
!= &card
->devices
) {
314 dev
= card_to_pnp_dev(pos
);
315 if ((!dev
->card_link
) && compare_pnp_id(dev
->id
, id
))
324 dev
->card_link
= clink
;
325 dev
->dev
.driver
= &drv
->link
.driver
;
326 if (pnp_bus_type
.probe(&dev
->dev
))
328 if (device_bind_driver(&dev
->dev
))
334 dev
->dev
.driver
= NULL
;
335 dev
->card_link
= NULL
;
340 * pnp_release_card_device - call this when the driver no longer needs the device
341 * @dev: pointer to the PnP device stucture
344 void pnp_release_card_device(struct pnp_dev
*dev
)
346 struct pnp_card_driver
*drv
= dev
->card_link
->driver
;
349 drv
->link
.remove
= &card_remove
;
350 device_release_driver(&dev
->dev
);
351 drv
->link
.remove
= &card_remove_first
;
355 * suspend/resume callbacks
357 static int card_suspend(struct pnp_dev
*dev
, pm_message_t state
)
359 struct pnp_card_link
*link
= dev
->card_link
;
360 if (link
->pm_state
.event
== state
.event
)
362 link
->pm_state
= state
;
363 return link
->driver
->suspend(link
, state
);
366 static int card_resume(struct pnp_dev
*dev
)
368 struct pnp_card_link
*link
= dev
->card_link
;
369 if (link
->pm_state
.event
== PM_EVENT_ON
)
371 link
->pm_state
= PMSG_ON
;
372 link
->driver
->resume(link
);
377 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
378 * @drv: pointer to the driver to register
381 int pnp_register_card_driver(struct pnp_card_driver
*drv
)
384 struct list_head
*pos
, *temp
;
386 drv
->link
.name
= drv
->name
;
387 drv
->link
.id_table
= NULL
; /* this will disable auto matching */
388 drv
->link
.flags
= drv
->flags
;
389 drv
->link
.probe
= NULL
;
390 drv
->link
.remove
= &card_remove_first
;
391 drv
->link
.suspend
= drv
->suspend
? card_suspend
: NULL
;
392 drv
->link
.resume
= drv
->resume
? card_resume
: NULL
;
394 error
= pnp_register_driver(&drv
->link
);
398 spin_lock(&pnp_lock
);
399 list_add_tail(&drv
->global_list
, &pnp_card_drivers
);
400 spin_unlock(&pnp_lock
);
402 list_for_each_safe(pos
, temp
, &pnp_cards
) {
403 struct pnp_card
*card
=
404 list_entry(pos
, struct pnp_card
, global_list
);
405 card_probe(card
, drv
);
411 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
412 * @drv: pointer to the driver to unregister
415 void pnp_unregister_card_driver(struct pnp_card_driver
*drv
)
417 spin_lock(&pnp_lock
);
418 list_del(&drv
->global_list
);
419 spin_unlock(&pnp_lock
);
420 pnp_unregister_driver(&drv
->link
);
424 EXPORT_SYMBOL(pnp_add_card
);
425 EXPORT_SYMBOL(pnp_remove_card
);
426 EXPORT_SYMBOL(pnp_add_card_device
);
427 EXPORT_SYMBOL(pnp_remove_card_device
);
428 EXPORT_SYMBOL(pnp_add_card_id
);
430 EXPORT_SYMBOL(pnp_request_card_device
);
431 EXPORT_SYMBOL(pnp_release_card_device
);
432 EXPORT_SYMBOL(pnp_register_card_driver
);
433 EXPORT_SYMBOL(pnp_unregister_card_driver
);