Linux Audio

Check our new training course

Linux kernel drivers training

Mar 31-Apr 9, 2025, special US time zones
Register
Loading...
v3.15
  1/*
  2 * card.c - contains functions for managing groups of PnP devices
  3 *
  4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  5 */
  6
  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>
 12#include "base.h"
 13
 14LIST_HEAD(pnp_cards);
 15static LIST_HEAD(pnp_card_drivers);
 16
 17static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
 18						   struct pnp_card *card)
 19{
 20	const struct pnp_card_device_id *drv_id = drv->id_table;
 21
 22	while (*drv_id->id) {
 23		if (compare_pnp_id(card->id, drv_id->id)) {
 24			int i = 0;
 25
 26			for (;;) {
 27				int found;
 28				struct pnp_dev *dev;
 29
 30				if (i == PNP_MAX_DEVICES ||
 31				    !*drv_id->devs[i].id)
 32					return drv_id;
 33				found = 0;
 34				card_for_each_dev(card, dev) {
 35					if (compare_pnp_id(dev->id,
 36						   drv_id->devs[i].id)) {
 37						found = 1;
 38						break;
 39					}
 40				}
 41				if (!found)
 42					break;
 43				i++;
 44			}
 45		}
 46		drv_id++;
 47	}
 48	return NULL;
 49}
 50
 51static void card_remove(struct pnp_dev *dev)
 52{
 53	dev->card_link = NULL;
 54}
 55
 56static void card_remove_first(struct pnp_dev *dev)
 57{
 58	struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
 59
 60	if (!dev->card || !drv)
 61		return;
 62	if (drv->remove)
 63		drv->remove(dev->card_link);
 64	drv->link.remove = &card_remove;
 65	kfree(dev->card_link);
 66	card_remove(dev);
 67}
 68
 69static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
 70{
 71	const struct pnp_card_device_id *id;
 72	struct pnp_card_link *clink;
 73	struct pnp_dev *dev;
 74
 75	if (!drv->probe)
 76		return 0;
 77	id = match_card(drv, card);
 78	if (!id)
 79		return 0;
 80
 81	clink = pnp_alloc(sizeof(*clink));
 82	if (!clink)
 83		return 0;
 84	clink->card = card;
 85	clink->driver = drv;
 86	clink->pm_state = PMSG_ON;
 87
 88	if (drv->probe(clink, id) >= 0)
 89		return 1;
 90
 91	/* Recovery */
 92	card_for_each_dev(card, dev) {
 93		if (dev->card_link == clink)
 94			pnp_release_card_device(dev);
 95	}
 96	kfree(clink);
 97	return 0;
 98}
 99
100/**
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
104 */
105static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
106{
107	struct pnp_id *dev_id, *ptr;
108
109	dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
110	if (!dev_id)
111		return NULL;
112
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';
121
122	dev_id->next = NULL;
123	ptr = card->id;
124	while (ptr && ptr->next)
125		ptr = ptr->next;
126	if (ptr)
127		ptr->next = dev_id;
128	else
129		card->id = dev_id;
130
131	return dev_id;
132}
133
134static void pnp_free_card_ids(struct pnp_card *card)
135{
136	struct pnp_id *id;
137	struct pnp_id *next;
138
139	id = card->id;
140	while (id) {
141		next = id->next;
142		kfree(id);
143		id = next;
144	}
145}
146
147static void pnp_release_card(struct device *dmdev)
148{
149	struct pnp_card *card = to_pnp_card(dmdev);
150
151	pnp_free_card_ids(card);
152	kfree(card);
153}
154
155struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
156{
157	struct pnp_card *card;
158	struct pnp_id *dev_id;
159
160	card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL);
161	if (!card)
162		return NULL;
163
164	card->protocol = protocol;
165	card->number = id;
166
167	card->dev.parent = &card->protocol->dev;
168	dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number);
169
170	card->dev.coherent_dma_mask = DMA_BIT_MASK(24);
171	card->dev.dma_mask = &card->dev.coherent_dma_mask;
172
173	dev_id = pnp_add_card_id(card, pnpid);
174	if (!dev_id) {
175		kfree(card);
176		return NULL;
177	}
178
179	return card;
180}
181
182static ssize_t pnp_show_card_name(struct device *dmdev,
183				  struct device_attribute *attr, char *buf)
184{
185	char *str = buf;
186	struct pnp_card *card = to_pnp_card(dmdev);
187
188	str += sprintf(str, "%s\n", card->name);
189	return (str - buf);
190}
191
192static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
193
194static ssize_t pnp_show_card_ids(struct device *dmdev,
195				 struct device_attribute *attr, char *buf)
196{
197	char *str = buf;
198	struct pnp_card *card = to_pnp_card(dmdev);
199	struct pnp_id *pos = card->id;
200
201	while (pos) {
202		str += sprintf(str, "%s\n", pos->id);
203		pos = pos->next;
204	}
205	return (str - buf);
206}
207
208static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
209
210static int pnp_interface_attach_card(struct pnp_card *card)
211{
212	int rc = device_create_file(&card->dev, &dev_attr_name);
213
214	if (rc)
215		return rc;
216
217	rc = device_create_file(&card->dev, &dev_attr_card_id);
218	if (rc)
219		goto err_name;
220
221	return 0;
222
223err_name:
224	device_remove_file(&card->dev, &dev_attr_name);
225	return rc;
226}
227
228/**
229 * pnp_add_card - adds a PnP card to the PnP Layer
230 * @card: pointer to the card to add
231 */
232int pnp_add_card(struct pnp_card *card)
233{
234	int error;
235	struct list_head *pos, *temp;
236
237	card->dev.bus = NULL;
238	card->dev.release = &pnp_release_card;
239	error = device_register(&card->dev);
240	if (error) {
241		dev_err(&card->dev, "could not register (err=%d)\n", error);
242		put_device(&card->dev);
243		return error;
244	}
245
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);
251
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);
258	}
259
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,
264			       global_list);
265		card_probe(card, drv);
266	}
267	return 0;
268}
269
270/**
271 * pnp_remove_card - removes a PnP card from the PnP Layer
272 * @card: pointer to the card to remove
273 */
274void pnp_remove_card(struct pnp_card *card)
275{
276	struct list_head *pos, *temp;
277
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);
286	}
287}
288
289/**
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
293 */
294int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
295{
296	dev->dev.parent = &card->dev;
297	dev->card_link = NULL;
298	dev_set_name(&dev->dev, "%02x:%02x.%02x",
299		     dev->protocol->number, card->number, dev->number);
300	spin_lock(&pnp_lock);
301	dev->card = card;
302	list_add_tail(&dev->card_list, &card->devices);
303	spin_unlock(&pnp_lock);
304	return 0;
305}
306
307/**
308 * pnp_remove_card_device- removes a device from the specified card
309 * @dev: pointer to the device to remove
310 */
311void pnp_remove_card_device(struct pnp_dev *dev)
312{
313	spin_lock(&pnp_lock);
314	dev->card = NULL;
315	list_del(&dev->card_list);
316	spin_unlock(&pnp_lock);
317	__pnp_remove_device(dev);
318}
319
320/**
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 beginning.
325 */
326struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
327					const char *id, struct pnp_dev *from)
328{
329	struct list_head *pos;
330	struct pnp_dev *dev;
331	struct pnp_card_driver *drv;
332	struct pnp_card *card;
333
334	if (!clink || !id)
335		return NULL;
336
337	card = clink->card;
338	drv = clink->driver;
339	if (!from) {
340		pos = card->devices.next;
341	} else {
342		if (from->card != card)
343			return NULL;
344		pos = from->card_list.next;
345	}
346	while (pos != &card->devices) {
347		dev = card_to_pnp_dev(pos);
348		if ((!dev->card_link) && compare_pnp_id(dev->id, id))
349			goto found;
350		pos = pos->next;
351	}
352
353	return NULL;
354
355found:
356	dev->card_link = clink;
357	dev->dev.driver = &drv->link.driver;
358	if (pnp_bus_type.probe(&dev->dev))
359		goto err_out;
360	if (device_bind_driver(&dev->dev))
361		goto err_out;
362
363	return dev;
364
365err_out:
366	dev->dev.driver = NULL;
367	dev->card_link = NULL;
368	return NULL;
369}
370
371/**
372 * pnp_release_card_device - call this when the driver no longer needs the device
373 * @dev: pointer to the PnP device structure
374 */
375void pnp_release_card_device(struct pnp_dev *dev)
376{
377	struct pnp_card_driver *drv = dev->card_link->driver;
378
379	drv->link.remove = &card_remove;
380	device_release_driver(&dev->dev);
381	drv->link.remove = &card_remove_first;
382}
383
384/*
385 * suspend/resume callbacks
386 */
387static int card_suspend(struct pnp_dev *dev, pm_message_t state)
388{
389	struct pnp_card_link *link = dev->card_link;
390
391	if (link->pm_state.event == state.event)
392		return 0;
393	link->pm_state = state;
394	return link->driver->suspend(link, state);
395}
396
397static int card_resume(struct pnp_dev *dev)
398{
399	struct pnp_card_link *link = dev->card_link;
400
401	if (link->pm_state.event == PM_EVENT_ON)
402		return 0;
403	link->pm_state = PMSG_ON;
404	link->driver->resume(link);
405	return 0;
406}
407
408/**
409 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
410 * @drv: pointer to the driver to register
411 */
412int pnp_register_card_driver(struct pnp_card_driver *drv)
413{
414	int error;
415	struct list_head *pos, *temp;
416
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;
424
425	error = pnp_register_driver(&drv->link);
426	if (error < 0)
427		return error;
428
429	spin_lock(&pnp_lock);
430	list_add_tail(&drv->global_list, &pnp_card_drivers);
431	spin_unlock(&pnp_lock);
432
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);
437	}
438	return 0;
439}
440
441/**
442 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
443 * @drv: pointer to the driver to unregister
444 */
445void pnp_unregister_card_driver(struct pnp_card_driver *drv)
446{
447	spin_lock(&pnp_lock);
448	list_del(&drv->global_list);
449	spin_unlock(&pnp_lock);
450	pnp_unregister_driver(&drv->link);
451}
452
453EXPORT_SYMBOL(pnp_request_card_device);
454EXPORT_SYMBOL(pnp_release_card_device);
455EXPORT_SYMBOL(pnp_register_card_driver);
456EXPORT_SYMBOL(pnp_unregister_card_driver);
v4.10.11
  1/*
  2 * card.c - contains functions for managing groups of PnP devices
  3 *
  4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  5 */
  6
  7#include <linux/module.h>
  8#include <linux/mutex.h>
  9#include <linux/ctype.h>
 10#include <linux/slab.h>
 11#include <linux/pnp.h>
 12#include <linux/dma-mapping.h>
 13#include "base.h"
 14
 15LIST_HEAD(pnp_cards);
 16static LIST_HEAD(pnp_card_drivers);
 17
 18static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
 19						   struct pnp_card *card)
 20{
 21	const struct pnp_card_device_id *drv_id = drv->id_table;
 22
 23	while (*drv_id->id) {
 24		if (compare_pnp_id(card->id, drv_id->id)) {
 25			int i = 0;
 26
 27			for (;;) {
 28				int found;
 29				struct pnp_dev *dev;
 30
 31				if (i == PNP_MAX_DEVICES ||
 32				    !*drv_id->devs[i].id)
 33					return drv_id;
 34				found = 0;
 35				card_for_each_dev(card, dev) {
 36					if (compare_pnp_id(dev->id,
 37						   drv_id->devs[i].id)) {
 38						found = 1;
 39						break;
 40					}
 41				}
 42				if (!found)
 43					break;
 44				i++;
 45			}
 46		}
 47		drv_id++;
 48	}
 49	return NULL;
 50}
 51
 52static void card_remove(struct pnp_dev *dev)
 53{
 54	dev->card_link = NULL;
 55}
 56
 57static void card_remove_first(struct pnp_dev *dev)
 58{
 59	struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
 60
 61	if (!dev->card || !drv)
 62		return;
 63	if (drv->remove)
 64		drv->remove(dev->card_link);
 65	drv->link.remove = &card_remove;
 66	kfree(dev->card_link);
 67	card_remove(dev);
 68}
 69
 70static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
 71{
 72	const struct pnp_card_device_id *id;
 73	struct pnp_card_link *clink;
 74	struct pnp_dev *dev;
 75
 76	if (!drv->probe)
 77		return 0;
 78	id = match_card(drv, card);
 79	if (!id)
 80		return 0;
 81
 82	clink = pnp_alloc(sizeof(*clink));
 83	if (!clink)
 84		return 0;
 85	clink->card = card;
 86	clink->driver = drv;
 87	clink->pm_state = PMSG_ON;
 88
 89	if (drv->probe(clink, id) >= 0)
 90		return 1;
 91
 92	/* Recovery */
 93	card_for_each_dev(card, dev) {
 94		if (dev->card_link == clink)
 95			pnp_release_card_device(dev);
 96	}
 97	kfree(clink);
 98	return 0;
 99}
100
101/**
102 * pnp_add_card_id - adds an EISA id to the specified card
103 * @id: pointer to a pnp_id structure
104 * @card: pointer to the desired card
105 */
106static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
107{
108	struct pnp_id *dev_id, *ptr;
109
110	dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
111	if (!dev_id)
112		return NULL;
113
114	dev_id->id[0] = id[0];
115	dev_id->id[1] = id[1];
116	dev_id->id[2] = id[2];
117	dev_id->id[3] = tolower(id[3]);
118	dev_id->id[4] = tolower(id[4]);
119	dev_id->id[5] = tolower(id[5]);
120	dev_id->id[6] = tolower(id[6]);
121	dev_id->id[7] = '\0';
122
123	dev_id->next = NULL;
124	ptr = card->id;
125	while (ptr && ptr->next)
126		ptr = ptr->next;
127	if (ptr)
128		ptr->next = dev_id;
129	else
130		card->id = dev_id;
131
132	return dev_id;
133}
134
135static void pnp_free_card_ids(struct pnp_card *card)
136{
137	struct pnp_id *id;
138	struct pnp_id *next;
139
140	id = card->id;
141	while (id) {
142		next = id->next;
143		kfree(id);
144		id = next;
145	}
146}
147
148static void pnp_release_card(struct device *dmdev)
149{
150	struct pnp_card *card = to_pnp_card(dmdev);
151
152	pnp_free_card_ids(card);
153	kfree(card);
154}
155
156struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
157{
158	struct pnp_card *card;
159	struct pnp_id *dev_id;
160
161	card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL);
162	if (!card)
163		return NULL;
164
165	card->protocol = protocol;
166	card->number = id;
167
168	card->dev.parent = &card->protocol->dev;
169	dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number);
170
171	card->dev.coherent_dma_mask = DMA_BIT_MASK(24);
172	card->dev.dma_mask = &card->dev.coherent_dma_mask;
173
174	dev_id = pnp_add_card_id(card, pnpid);
175	if (!dev_id) {
176		kfree(card);
177		return NULL;
178	}
179
180	return card;
181}
182
183static ssize_t pnp_show_card_name(struct device *dmdev,
184				  struct device_attribute *attr, char *buf)
185{
186	char *str = buf;
187	struct pnp_card *card = to_pnp_card(dmdev);
188
189	str += sprintf(str, "%s\n", card->name);
190	return (str - buf);
191}
192
193static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
194
195static ssize_t pnp_show_card_ids(struct device *dmdev,
196				 struct device_attribute *attr, char *buf)
197{
198	char *str = buf;
199	struct pnp_card *card = to_pnp_card(dmdev);
200	struct pnp_id *pos = card->id;
201
202	while (pos) {
203		str += sprintf(str, "%s\n", pos->id);
204		pos = pos->next;
205	}
206	return (str - buf);
207}
208
209static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
210
211static int pnp_interface_attach_card(struct pnp_card *card)
212{
213	int rc = device_create_file(&card->dev, &dev_attr_name);
214
215	if (rc)
216		return rc;
217
218	rc = device_create_file(&card->dev, &dev_attr_card_id);
219	if (rc)
220		goto err_name;
221
222	return 0;
223
224err_name:
225	device_remove_file(&card->dev, &dev_attr_name);
226	return rc;
227}
228
229/**
230 * pnp_add_card - adds a PnP card to the PnP Layer
231 * @card: pointer to the card to add
232 */
233int pnp_add_card(struct pnp_card *card)
234{
235	int error;
236	struct list_head *pos, *temp;
237
238	card->dev.bus = NULL;
239	card->dev.release = &pnp_release_card;
240	error = device_register(&card->dev);
241	if (error) {
242		dev_err(&card->dev, "could not register (err=%d)\n", error);
243		put_device(&card->dev);
244		return error;
245	}
246
247	pnp_interface_attach_card(card);
248	mutex_lock(&pnp_lock);
249	list_add_tail(&card->global_list, &pnp_cards);
250	list_add_tail(&card->protocol_list, &card->protocol->cards);
251	mutex_unlock(&pnp_lock);
252
253	/* we wait until now to add devices in order to ensure the drivers
254	 * will be able to use all of the related devices on the card
255	 * without waiting an unreasonable length of time */
256	list_for_each(pos, &card->devices) {
257		struct pnp_dev *dev = card_to_pnp_dev(pos);
258		__pnp_add_device(dev);
259	}
260
261	/* match with card drivers */
262	list_for_each_safe(pos, temp, &pnp_card_drivers) {
263		struct pnp_card_driver *drv =
264		    list_entry(pos, struct pnp_card_driver,
265			       global_list);
266		card_probe(card, drv);
267	}
268	return 0;
269}
270
271/**
272 * pnp_remove_card - removes a PnP card from the PnP Layer
273 * @card: pointer to the card to remove
274 */
275void pnp_remove_card(struct pnp_card *card)
276{
277	struct list_head *pos, *temp;
278
279	device_unregister(&card->dev);
280	mutex_lock(&pnp_lock);
281	list_del(&card->global_list);
282	list_del(&card->protocol_list);
283	mutex_unlock(&pnp_lock);
284	list_for_each_safe(pos, temp, &card->devices) {
285		struct pnp_dev *dev = card_to_pnp_dev(pos);
286		pnp_remove_card_device(dev);
287	}
288}
289
290/**
291 * pnp_add_card_device - adds a device to the specified card
292 * @card: pointer to the card to add to
293 * @dev: pointer to the device to add
294 */
295int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
296{
297	dev->dev.parent = &card->dev;
298	dev->card_link = NULL;
299	dev_set_name(&dev->dev, "%02x:%02x.%02x",
300		     dev->protocol->number, card->number, dev->number);
301	mutex_lock(&pnp_lock);
302	dev->card = card;
303	list_add_tail(&dev->card_list, &card->devices);
304	mutex_unlock(&pnp_lock);
305	return 0;
306}
307
308/**
309 * pnp_remove_card_device- removes a device from the specified card
310 * @dev: pointer to the device to remove
311 */
312void pnp_remove_card_device(struct pnp_dev *dev)
313{
314	mutex_lock(&pnp_lock);
315	dev->card = NULL;
316	list_del(&dev->card_list);
317	mutex_unlock(&pnp_lock);
318	__pnp_remove_device(dev);
319}
320
321/**
322 * pnp_request_card_device - Searches for a PnP device under the specified card
323 * @clink: pointer to the card link, cannot be NULL
324 * @id: pointer to a PnP ID structure that explains the rules for finding the device
325 * @from: Starting place to search from. If NULL it will start from the beginning.
326 */
327struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
328					const char *id, struct pnp_dev *from)
329{
330	struct list_head *pos;
331	struct pnp_dev *dev;
332	struct pnp_card_driver *drv;
333	struct pnp_card *card;
334
335	if (!clink || !id)
336		return NULL;
337
338	card = clink->card;
339	drv = clink->driver;
340	if (!from) {
341		pos = card->devices.next;
342	} else {
343		if (from->card != card)
344			return NULL;
345		pos = from->card_list.next;
346	}
347	while (pos != &card->devices) {
348		dev = card_to_pnp_dev(pos);
349		if ((!dev->card_link) && compare_pnp_id(dev->id, id))
350			goto found;
351		pos = pos->next;
352	}
353
354	return NULL;
355
356found:
357	dev->card_link = clink;
358	dev->dev.driver = &drv->link.driver;
359	if (pnp_bus_type.probe(&dev->dev))
360		goto err_out;
361	if (device_bind_driver(&dev->dev))
362		goto err_out;
363
364	return dev;
365
366err_out:
367	dev->dev.driver = NULL;
368	dev->card_link = NULL;
369	return NULL;
370}
371
372/**
373 * pnp_release_card_device - call this when the driver no longer needs the device
374 * @dev: pointer to the PnP device structure
375 */
376void pnp_release_card_device(struct pnp_dev *dev)
377{
378	struct pnp_card_driver *drv = dev->card_link->driver;
379
380	drv->link.remove = &card_remove;
381	device_release_driver(&dev->dev);
382	drv->link.remove = &card_remove_first;
383}
384
385/*
386 * suspend/resume callbacks
387 */
388static int card_suspend(struct pnp_dev *dev, pm_message_t state)
389{
390	struct pnp_card_link *link = dev->card_link;
391
392	if (link->pm_state.event == state.event)
393		return 0;
394	link->pm_state = state;
395	return link->driver->suspend(link, state);
396}
397
398static int card_resume(struct pnp_dev *dev)
399{
400	struct pnp_card_link *link = dev->card_link;
401
402	if (link->pm_state.event == PM_EVENT_ON)
403		return 0;
404	link->pm_state = PMSG_ON;
405	link->driver->resume(link);
406	return 0;
407}
408
409/**
410 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
411 * @drv: pointer to the driver to register
412 */
413int pnp_register_card_driver(struct pnp_card_driver *drv)
414{
415	int error;
416	struct list_head *pos, *temp;
417
418	drv->link.name = drv->name;
419	drv->link.id_table = NULL;	/* this will disable auto matching */
420	drv->link.flags = drv->flags;
421	drv->link.probe = NULL;
422	drv->link.remove = &card_remove_first;
423	drv->link.suspend = drv->suspend ? card_suspend : NULL;
424	drv->link.resume = drv->resume ? card_resume : NULL;
425
426	error = pnp_register_driver(&drv->link);
427	if (error < 0)
428		return error;
429
430	mutex_lock(&pnp_lock);
431	list_add_tail(&drv->global_list, &pnp_card_drivers);
432	mutex_unlock(&pnp_lock);
433
434	list_for_each_safe(pos, temp, &pnp_cards) {
435		struct pnp_card *card =
436		    list_entry(pos, struct pnp_card, global_list);
437		card_probe(card, drv);
438	}
439	return 0;
440}
441
442/**
443 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
444 * @drv: pointer to the driver to unregister
445 */
446void pnp_unregister_card_driver(struct pnp_card_driver *drv)
447{
448	mutex_lock(&pnp_lock);
449	list_del(&drv->global_list);
450	mutex_unlock(&pnp_lock);
451	pnp_unregister_driver(&drv->link);
452}
453
454EXPORT_SYMBOL(pnp_request_card_device);
455EXPORT_SYMBOL(pnp_release_card_device);
456EXPORT_SYMBOL(pnp_register_card_driver);
457EXPORT_SYMBOL(pnp_unregister_card_driver);