mlxsw: spectrum: Mark split ports as such
[linux-stable.git] / drivers / pnp / card.c
blob31ad9fc3f701a3e8ed7b0c37c916c5f98f07248a
1 /*
2 * card.c - contains functions for managing groups of PnP devices
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
5 */
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"
15 LIST_HEAD(pnp_cards);
16 static LIST_HEAD(pnp_card_drivers);
18 static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
19 struct pnp_card *card)
21 const struct pnp_card_device_id *drv_id = drv->id_table;
23 while (*drv_id->id) {
24 if (compare_pnp_id(card->id, drv_id->id)) {
25 int i = 0;
27 for (;;) {
28 int found;
29 struct pnp_dev *dev;
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;
42 if (!found)
43 break;
44 i++;
47 drv_id++;
49 return NULL;
52 static void card_remove(struct pnp_dev *dev)
54 dev->card_link = NULL;
57 static void card_remove_first(struct pnp_dev *dev)
59 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
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);
70 static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
72 const struct pnp_card_device_id *id;
73 struct pnp_card_link *clink;
74 struct pnp_dev *dev;
76 if (!drv->probe)
77 return 0;
78 id = match_card(drv, card);
79 if (!id)
80 return 0;
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;
89 if (drv->probe(clink, id) >= 0)
90 return 1;
92 /* Recovery */
93 card_for_each_dev(card, dev) {
94 if (dev->card_link == clink)
95 pnp_release_card_device(dev);
97 kfree(clink);
98 return 0;
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
106 static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
108 struct pnp_id *dev_id, *ptr;
110 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
111 if (!dev_id)
112 return NULL;
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';
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;
132 return dev_id;
135 static void pnp_free_card_ids(struct pnp_card *card)
137 struct pnp_id *id;
138 struct pnp_id *next;
140 id = card->id;
141 while (id) {
142 next = id->next;
143 kfree(id);
144 id = next;
148 static void pnp_release_card(struct device *dmdev)
150 struct pnp_card *card = to_pnp_card(dmdev);
152 pnp_free_card_ids(card);
153 kfree(card);
156 struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
158 struct pnp_card *card;
159 struct pnp_id *dev_id;
161 card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL);
162 if (!card)
163 return NULL;
165 card->protocol = protocol;
166 card->number = id;
168 card->dev.parent = &card->protocol->dev;
169 dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number);
171 card->dev.coherent_dma_mask = DMA_BIT_MASK(24);
172 card->dev.dma_mask = &card->dev.coherent_dma_mask;
174 dev_id = pnp_add_card_id(card, pnpid);
175 if (!dev_id) {
176 kfree(card);
177 return NULL;
180 return card;
183 static ssize_t pnp_show_card_name(struct device *dmdev,
184 struct device_attribute *attr, char *buf)
186 char *str = buf;
187 struct pnp_card *card = to_pnp_card(dmdev);
189 str += sprintf(str, "%s\n", card->name);
190 return (str - buf);
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)
198 char *str = buf;
199 struct pnp_card *card = to_pnp_card(dmdev);
200 struct pnp_id *pos = card->id;
202 while (pos) {
203 str += sprintf(str, "%s\n", pos->id);
204 pos = pos->next;
206 return (str - buf);
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);
215 if (rc)
216 return rc;
218 rc = device_create_file(&card->dev, &dev_attr_card_id);
219 if (rc)
220 goto err_name;
222 return 0;
224 err_name:
225 device_remove_file(&card->dev, &dev_attr_name);
226 return rc;
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)
235 int error;
236 struct list_head *pos, *temp;
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;
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);
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);
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);
268 return 0;
272 * pnp_remove_card - removes a PnP card from the PnP Layer
273 * @card: pointer to the card to remove
275 void pnp_remove_card(struct pnp_card *card)
277 struct list_head *pos, *temp;
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);
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
295 int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
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;
309 * pnp_remove_card_device- removes a device from the specified card
310 * @dev: pointer to the device to remove
312 void pnp_remove_card_device(struct pnp_dev *dev)
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);
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.
327 struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
328 const char *id, struct pnp_dev *from)
330 struct list_head *pos;
331 struct pnp_dev *dev;
332 struct pnp_card_driver *drv;
333 struct pnp_card *card;
335 if (!clink || !id)
336 return NULL;
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;
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;
354 return NULL;
356 found:
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;
364 return dev;
366 err_out:
367 dev->dev.driver = NULL;
368 dev->card_link = NULL;
369 return NULL;
373 * pnp_release_card_device - call this when the driver no longer needs the device
374 * @dev: pointer to the PnP device structure
376 void pnp_release_card_device(struct pnp_dev *dev)
378 struct pnp_card_driver *drv = dev->card_link->driver;
380 drv->link.remove = &card_remove;
381 device_release_driver(&dev->dev);
382 drv->link.remove = &card_remove_first;
386 * suspend/resume callbacks
388 static int card_suspend(struct pnp_dev *dev, pm_message_t state)
390 struct pnp_card_link *link = dev->card_link;
392 if (link->pm_state.event == state.event)
393 return 0;
394 link->pm_state = state;
395 return link->driver->suspend(link, state);
398 static int card_resume(struct pnp_dev *dev)
400 struct pnp_card_link *link = dev->card_link;
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;
410 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
411 * @drv: pointer to the driver to register
413 int pnp_register_card_driver(struct pnp_card_driver *drv)
415 int error;
416 struct list_head *pos, *temp;
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;
426 error = pnp_register_driver(&drv->link);
427 if (error < 0)
428 return error;
430 mutex_lock(&pnp_lock);
431 list_add_tail(&drv->global_list, &pnp_card_drivers);
432 mutex_unlock(&pnp_lock);
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);
439 return 0;
443 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
444 * @drv: pointer to the driver to unregister
446 void pnp_unregister_card_driver(struct pnp_card_driver *drv)
448 mutex_lock(&pnp_lock);
449 list_del(&drv->global_list);
450 mutex_unlock(&pnp_lock);
451 pnp_unregister_driver(&drv->link);
454 EXPORT_SYMBOL(pnp_request_card_device);
455 EXPORT_SYMBOL(pnp_release_card_device);
456 EXPORT_SYMBOL(pnp_register_card_driver);
457 EXPORT_SYMBOL(pnp_unregister_card_driver);