2 * card.c - contains functions for managing groups of PnP devices
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
7 #include <linux/module.h>
8 #include <linux/ctype.h>
9 #include <linux/slab.h>
10 #include <linux/pnp.h>
11 #include <linux/dma-mapping.h>
15 static LIST_HEAD(pnp_card_drivers
);
17 static const struct pnp_card_device_id
*match_card(struct pnp_card_driver
*drv
,
18 struct pnp_card
*card
)
20 const struct pnp_card_device_id
*drv_id
= drv
->id_table
;
23 if (compare_pnp_id(card
->id
, drv_id
->id
)) {
30 if (i
== PNP_MAX_DEVICES
||
34 card_for_each_dev(card
, dev
) {
35 if (compare_pnp_id(dev
->id
,
36 drv_id
->devs
[i
].id
)) {
51 static void card_remove(struct pnp_dev
*dev
)
53 dev
->card_link
= NULL
;
56 static void card_remove_first(struct pnp_dev
*dev
)
58 struct pnp_card_driver
*drv
= to_pnp_card_driver(dev
->driver
);
60 if (!dev
->card
|| !drv
)
63 drv
->remove(dev
->card_link
);
64 drv
->link
.remove
= &card_remove
;
65 kfree(dev
->card_link
);
69 static int card_probe(struct pnp_card
*card
, struct pnp_card_driver
*drv
)
71 const struct pnp_card_device_id
*id
;
72 struct pnp_card_link
*clink
;
77 id
= match_card(drv
, card
);
81 clink
= pnp_alloc(sizeof(*clink
));
86 clink
->pm_state
= PMSG_ON
;
88 if (drv
->probe(clink
, id
) >= 0)
92 card_for_each_dev(card
, dev
) {
93 if (dev
->card_link
== clink
)
94 pnp_release_card_device(dev
);
101 * pnp_add_card_id - adds an EISA id to the specified card
102 * @id: pointer to a pnp_id structure
103 * @card: pointer to the desired card
105 static struct pnp_id
*pnp_add_card_id(struct pnp_card
*card
, char *id
)
107 struct pnp_id
*dev_id
, *ptr
;
109 dev_id
= kzalloc(sizeof(struct pnp_id
), GFP_KERNEL
);
113 dev_id
->id
[0] = id
[0];
114 dev_id
->id
[1] = id
[1];
115 dev_id
->id
[2] = id
[2];
116 dev_id
->id
[3] = tolower(id
[3]);
117 dev_id
->id
[4] = tolower(id
[4]);
118 dev_id
->id
[5] = tolower(id
[5]);
119 dev_id
->id
[6] = tolower(id
[6]);
120 dev_id
->id
[7] = '\0';
124 while (ptr
&& ptr
->next
)
134 static void pnp_free_card_ids(struct pnp_card
*card
)
147 static void pnp_release_card(struct device
*dmdev
)
149 struct pnp_card
*card
= to_pnp_card(dmdev
);
151 pnp_free_card_ids(card
);
155 struct pnp_card
*pnp_alloc_card(struct pnp_protocol
*protocol
, int id
, char *pnpid
)
157 struct pnp_card
*card
;
158 struct pnp_id
*dev_id
;
160 card
= kzalloc(sizeof(struct pnp_card
), GFP_KERNEL
);
164 card
->protocol
= protocol
;
167 card
->dev
.parent
= &card
->protocol
->dev
;
168 sprintf(card
->dev
.bus_id
, "%02x:%02x", card
->protocol
->number
,
171 card
->dev
.coherent_dma_mask
= DMA_24BIT_MASK
;
172 card
->dev
.dma_mask
= &card
->dev
.coherent_dma_mask
;
174 dev_id
= pnp_add_card_id(card
, pnpid
);
183 static ssize_t
pnp_show_card_name(struct device
*dmdev
,
184 struct device_attribute
*attr
, char *buf
)
187 struct pnp_card
*card
= to_pnp_card(dmdev
);
189 str
+= sprintf(str
, "%s\n", card
->name
);
193 static DEVICE_ATTR(name
, S_IRUGO
, pnp_show_card_name
, NULL
);
195 static ssize_t
pnp_show_card_ids(struct device
*dmdev
,
196 struct device_attribute
*attr
, char *buf
)
199 struct pnp_card
*card
= to_pnp_card(dmdev
);
200 struct pnp_id
*pos
= card
->id
;
203 str
+= sprintf(str
, "%s\n", pos
->id
);
209 static DEVICE_ATTR(card_id
, S_IRUGO
, pnp_show_card_ids
, NULL
);
211 static int pnp_interface_attach_card(struct pnp_card
*card
)
213 int rc
= device_create_file(&card
->dev
, &dev_attr_name
);
218 rc
= device_create_file(&card
->dev
, &dev_attr_card_id
);
225 device_remove_file(&card
->dev
, &dev_attr_name
);
230 * pnp_add_card - adds a PnP card to the PnP Layer
231 * @card: pointer to the card to add
233 int pnp_add_card(struct pnp_card
*card
)
236 struct list_head
*pos
, *temp
;
238 card
->dev
.bus
= NULL
;
239 card
->dev
.release
= &pnp_release_card
;
240 error
= device_register(&card
->dev
);
242 dev_err(&card
->dev
, "could not register (err=%d)\n", error
);
246 pnp_interface_attach_card(card
);
247 spin_lock(&pnp_lock
);
248 list_add_tail(&card
->global_list
, &pnp_cards
);
249 list_add_tail(&card
->protocol_list
, &card
->protocol
->cards
);
250 spin_unlock(&pnp_lock
);
252 /* we wait until now to add devices in order to ensure the drivers
253 * will be able to use all of the related devices on the card
254 * without waiting an unreasonable length of time */
255 list_for_each(pos
, &card
->devices
) {
256 struct pnp_dev
*dev
= card_to_pnp_dev(pos
);
257 __pnp_add_device(dev
);
260 /* match with card drivers */
261 list_for_each_safe(pos
, temp
, &pnp_card_drivers
) {
262 struct pnp_card_driver
*drv
=
263 list_entry(pos
, struct pnp_card_driver
,
265 card_probe(card
, drv
);
271 * pnp_remove_card - removes a PnP card from the PnP Layer
272 * @card: pointer to the card to remove
274 void pnp_remove_card(struct pnp_card
*card
)
276 struct list_head
*pos
, *temp
;
278 device_unregister(&card
->dev
);
279 spin_lock(&pnp_lock
);
280 list_del(&card
->global_list
);
281 list_del(&card
->protocol_list
);
282 spin_unlock(&pnp_lock
);
283 list_for_each_safe(pos
, temp
, &card
->devices
) {
284 struct pnp_dev
*dev
= card_to_pnp_dev(pos
);
285 pnp_remove_card_device(dev
);
290 * pnp_add_card_device - adds a device to the specified card
291 * @card: pointer to the card to add to
292 * @dev: pointer to the device to add
294 int pnp_add_card_device(struct pnp_card
*card
, struct pnp_dev
*dev
)
296 dev
->dev
.parent
= &card
->dev
;
297 dev
->card_link
= NULL
;
298 snprintf(dev
->dev
.bus_id
, BUS_ID_SIZE
, "%02x:%02x.%02x",
299 dev
->protocol
->number
, card
->number
, dev
->number
);
300 spin_lock(&pnp_lock
);
302 list_add_tail(&dev
->card_list
, &card
->devices
);
303 spin_unlock(&pnp_lock
);
308 * pnp_remove_card_device- removes a device from the specified card
309 * @dev: pointer to the device to remove
311 void pnp_remove_card_device(struct pnp_dev
*dev
)
313 spin_lock(&pnp_lock
);
315 list_del(&dev
->card_list
);
316 spin_unlock(&pnp_lock
);
317 __pnp_remove_device(dev
);
321 * pnp_request_card_device - Searches for a PnP device under the specified card
322 * @clink: pointer to the card link, cannot be NULL
323 * @id: pointer to a PnP ID structure that explains the rules for finding the device
324 * @from: Starting place to search from. If NULL it will start from the begining.
326 struct pnp_dev
*pnp_request_card_device(struct pnp_card_link
*clink
,
327 const char *id
, struct pnp_dev
*from
)
329 struct list_head
*pos
;
331 struct pnp_card_driver
*drv
;
332 struct pnp_card
*card
;
340 pos
= card
->devices
.next
;
342 if (from
->card
!= card
)
344 pos
= from
->card_list
.next
;
346 while (pos
!= &card
->devices
) {
347 dev
= card_to_pnp_dev(pos
);
348 if ((!dev
->card_link
) && compare_pnp_id(dev
->id
, id
))
356 dev
->card_link
= clink
;
357 dev
->dev
.driver
= &drv
->link
.driver
;
358 if (pnp_bus_type
.probe(&dev
->dev
))
360 if (device_bind_driver(&dev
->dev
))
366 dev
->dev
.driver
= NULL
;
367 dev
->card_link
= NULL
;
372 * pnp_release_card_device - call this when the driver no longer needs the device
373 * @dev: pointer to the PnP device stucture
375 void pnp_release_card_device(struct pnp_dev
*dev
)
377 struct pnp_card_driver
*drv
= dev
->card_link
->driver
;
379 drv
->link
.remove
= &card_remove
;
380 device_release_driver(&dev
->dev
);
381 drv
->link
.remove
= &card_remove_first
;
385 * suspend/resume callbacks
387 static int card_suspend(struct pnp_dev
*dev
, pm_message_t state
)
389 struct pnp_card_link
*link
= dev
->card_link
;
391 if (link
->pm_state
.event
== state
.event
)
393 link
->pm_state
= state
;
394 return link
->driver
->suspend(link
, state
);
397 static int card_resume(struct pnp_dev
*dev
)
399 struct pnp_card_link
*link
= dev
->card_link
;
401 if (link
->pm_state
.event
== PM_EVENT_ON
)
403 link
->pm_state
= PMSG_ON
;
404 link
->driver
->resume(link
);
409 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
410 * @drv: pointer to the driver to register
412 int pnp_register_card_driver(struct pnp_card_driver
*drv
)
415 struct list_head
*pos
, *temp
;
417 drv
->link
.name
= drv
->name
;
418 drv
->link
.id_table
= NULL
; /* this will disable auto matching */
419 drv
->link
.flags
= drv
->flags
;
420 drv
->link
.probe
= NULL
;
421 drv
->link
.remove
= &card_remove_first
;
422 drv
->link
.suspend
= drv
->suspend
? card_suspend
: NULL
;
423 drv
->link
.resume
= drv
->resume
? card_resume
: NULL
;
425 error
= pnp_register_driver(&drv
->link
);
429 spin_lock(&pnp_lock
);
430 list_add_tail(&drv
->global_list
, &pnp_card_drivers
);
431 spin_unlock(&pnp_lock
);
433 list_for_each_safe(pos
, temp
, &pnp_cards
) {
434 struct pnp_card
*card
=
435 list_entry(pos
, struct pnp_card
, global_list
);
436 card_probe(card
, drv
);
442 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
443 * @drv: pointer to the driver to unregister
445 void pnp_unregister_card_driver(struct pnp_card_driver
*drv
)
447 spin_lock(&pnp_lock
);
448 list_del(&drv
->global_list
);
449 spin_unlock(&pnp_lock
);
450 pnp_unregister_driver(&drv
->link
);
453 EXPORT_SYMBOL(pnp_request_card_device
);
454 EXPORT_SYMBOL(pnp_release_card_device
);
455 EXPORT_SYMBOL(pnp_register_card_driver
);
456 EXPORT_SYMBOL(pnp_unregister_card_driver
);