blob: dd6384b1efce13d01f5fec8fd2125b1114ae345e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * card.c - contains functions for managing groups of PnP devices
3 *
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
5 *
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/pnp.h>
11#include "base.h"
12
13LIST_HEAD(pnp_cards);
Adrian Bunkb449f632005-11-07 01:01:48 -080014static LIST_HEAD(pnp_card_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16
17static const struct pnp_card_device_id * match_card(struct pnp_card_driver * drv, struct pnp_card * card)
18{
19 const struct pnp_card_device_id * drv_id = drv->id_table;
20 while (*drv_id->id){
21 if (compare_pnp_id(card->id,drv_id->id)) {
22 int i = 0;
23 for (;;) {
24 int found;
25 struct pnp_dev *dev;
26 if (i == PNP_MAX_DEVICES || ! *drv_id->devs[i].id)
27 return drv_id;
28 found = 0;
29 card_for_each_dev(card, dev) {
30 if (compare_pnp_id(dev->id, drv_id->devs[i].id)) {
31 found = 1;
32 break;
33 }
34 }
35 if (! found)
36 break;
37 i++;
38 }
39 }
40 drv_id++;
41 }
42 return NULL;
43}
44
45static void card_remove(struct pnp_dev * dev)
46{
47 dev->card_link = NULL;
48}
Bjorn Helgaas982c6092006-03-27 01:17:08 -080049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static void card_remove_first(struct pnp_dev * dev)
51{
52 struct pnp_card_driver * drv = to_pnp_card_driver(dev->driver);
53 if (!dev->card || !drv)
54 return;
55 if (drv->remove)
56 drv->remove(dev->card_link);
57 drv->link.remove = &card_remove;
58 kfree(dev->card_link);
59 card_remove(dev);
60}
61
Jesper Juhla9adb8d2006-06-25 05:47:17 -070062static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Jesper Juhla9adb8d2006-06-25 05:47:17 -070064 const struct pnp_card_device_id *id;
65 struct pnp_card_link *clink;
66 struct pnp_dev *dev;
67
68 if (!drv->probe)
69 return 0;
70 id = match_card(drv,card);
71 if (!id)
72 return 0;
73
74 clink = pnp_alloc(sizeof(*clink));
75 if (!clink)
76 return 0;
77 clink->card = card;
78 clink->driver = drv;
79 clink->pm_state = PMSG_ON;
80
81 if (drv->probe(clink, id) >= 0)
82 return 1;
83
84 /* Recovery */
85 card_for_each_dev(card, dev) {
86 if (dev->card_link == clink)
87 pnp_release_card_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
Jesper Juhla9adb8d2006-06-25 05:47:17 -070089 kfree(clink);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91}
92
93/**
94 * pnp_add_card_id - adds an EISA id to the specified card
95 * @id: pointer to a pnp_id structure
96 * @card: pointer to the desired card
97 *
98 */
99
100int pnp_add_card_id(struct pnp_id *id, struct pnp_card * card)
101{
102 struct pnp_id * ptr;
103 if (!id)
104 return -EINVAL;
105 if (!card)
106 return -EINVAL;
107 id->next = NULL;
108 ptr = card->id;
109 while (ptr && ptr->next)
110 ptr = ptr->next;
111 if (ptr)
112 ptr->next = id;
113 else
114 card->id = id;
115 return 0;
116}
117
118static void pnp_free_card_ids(struct pnp_card * card)
119{
120 struct pnp_id * id;
121 struct pnp_id *next;
122 if (!card)
123 return;
124 id = card->id;
125 while (id) {
126 next = id->next;
127 kfree(id);
128 id = next;
129 }
130}
131
132static void pnp_release_card(struct device *dmdev)
133{
134 struct pnp_card * card = to_pnp_card(dmdev);
135 pnp_free_card_ids(card);
136 kfree(card);
137}
138
139
Yani Ioannoue404e272005-05-17 06:42:58 -0400140static ssize_t pnp_show_card_name(struct device *dmdev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 char *str = buf;
143 struct pnp_card *card = to_pnp_card(dmdev);
144 str += sprintf(str,"%s\n", card->name);
145 return (str - buf);
146}
147
148static DEVICE_ATTR(name,S_IRUGO,pnp_show_card_name,NULL);
149
Yani Ioannoue404e272005-05-17 06:42:58 -0400150static ssize_t pnp_show_card_ids(struct device *dmdev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 char *str = buf;
153 struct pnp_card *card = to_pnp_card(dmdev);
154 struct pnp_id * pos = card->id;
155
156 while (pos) {
157 str += sprintf(str,"%s\n", pos->id);
158 pos = pos->next;
159 }
160 return (str - buf);
161}
162
163static DEVICE_ATTR(card_id,S_IRUGO,pnp_show_card_ids,NULL);
164
165static int pnp_interface_attach_card(struct pnp_card *card)
166{
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800167 int rc = device_create_file(&card->dev,&dev_attr_name);
168 if (rc) return rc;
169
170 rc = device_create_file(&card->dev,&dev_attr_card_id);
171 if (rc) goto err_name;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800174
175err_name:
176 device_remove_file(&card->dev,&dev_attr_name);
177 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/**
181 * pnp_add_card - adds a PnP card to the PnP Layer
182 * @card: pointer to the card to add
183 */
184
185int pnp_add_card(struct pnp_card * card)
186{
187 int error;
188 struct list_head * pos, * temp;
189 if (!card || !card->protocol)
190 return -EINVAL;
191
192 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number, card->number);
193 card->dev.parent = &card->protocol->dev;
194 card->dev.bus = NULL;
195 card->dev.release = &pnp_release_card;
196 error = device_register(&card->dev);
197
198 if (error == 0) {
199 pnp_interface_attach_card(card);
200 spin_lock(&pnp_lock);
201 list_add_tail(&card->global_list, &pnp_cards);
202 list_add_tail(&card->protocol_list, &card->protocol->cards);
203 spin_unlock(&pnp_lock);
204
205 /* we wait until now to add devices in order to ensure the drivers
206 * will be able to use all of the related devices on the card
207 * without waiting any unresonable length of time */
208 list_for_each(pos,&card->devices){
209 struct pnp_dev *dev = card_to_pnp_dev(pos);
210 __pnp_add_device(dev);
211 }
212
213 /* match with card drivers */
214 list_for_each_safe(pos,temp,&pnp_card_drivers){
215 struct pnp_card_driver * drv = list_entry(pos, struct pnp_card_driver, global_list);
216 card_probe(card,drv);
217 }
218 } else
219 pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
220 return error;
221}
222
223/**
224 * pnp_remove_card - removes a PnP card from the PnP Layer
225 * @card: pointer to the card to remove
226 */
227
228void pnp_remove_card(struct pnp_card * card)
229{
230 struct list_head *pos, *temp;
231 if (!card)
232 return;
233 device_unregister(&card->dev);
234 spin_lock(&pnp_lock);
235 list_del(&card->global_list);
236 list_del(&card->protocol_list);
237 spin_unlock(&pnp_lock);
238 list_for_each_safe(pos,temp,&card->devices){
239 struct pnp_dev *dev = card_to_pnp_dev(pos);
240 pnp_remove_card_device(dev);
241 }
242}
243
244/**
245 * pnp_add_card_device - adds a device to the specified card
246 * @card: pointer to the card to add to
247 * @dev: pointer to the device to add
248 */
249
250int pnp_add_card_device(struct pnp_card * card, struct pnp_dev * dev)
251{
252 if (!card || !dev || !dev->protocol)
253 return -EINVAL;
254 dev->dev.parent = &card->dev;
255 dev->card_link = NULL;
256 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x", dev->protocol->number,
257 card->number,dev->number);
258 spin_lock(&pnp_lock);
259 dev->card = card;
260 list_add_tail(&dev->card_list, &card->devices);
261 spin_unlock(&pnp_lock);
262 return 0;
263}
264
265/**
266 * pnp_remove_card_device- removes a device from the specified card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * @dev: pointer to the device to remove
268 */
269
270void pnp_remove_card_device(struct pnp_dev * dev)
271{
272 spin_lock(&pnp_lock);
273 dev->card = NULL;
274 list_del(&dev->card_list);
275 spin_unlock(&pnp_lock);
276 __pnp_remove_device(dev);
277}
278
279/**
280 * pnp_request_card_device - Searches for a PnP device under the specified card
Martin Waitz3d410882005-06-23 22:05:21 -0700281 * @clink: pointer to the card link, cannot be NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * @id: pointer to a PnP ID structure that explains the rules for finding the device
283 * @from: Starting place to search from. If NULL it will start from the begining.
284 */
285
286struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
287{
288 struct list_head * pos;
289 struct pnp_dev * dev;
290 struct pnp_card_driver * drv;
291 struct pnp_card * card;
292 if (!clink || !id)
293 goto done;
294 card = clink->card;
295 drv = clink->driver;
296 if (!from) {
297 pos = card->devices.next;
298 } else {
299 if (from->card != card)
300 goto done;
301 pos = from->card_list.next;
302 }
303 while (pos != &card->devices) {
304 dev = card_to_pnp_dev(pos);
305 if ((!dev->card_link) && compare_pnp_id(dev->id,id))
306 goto found;
307 pos = pos->next;
308 }
309
310done:
311 return NULL;
312
313found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 dev->card_link = clink;
315 dev->dev.driver = &drv->link.driver;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800316 if (pnp_bus_type.probe(&dev->dev))
317 goto err_out;
318 if (device_bind_driver(&dev->dev))
319 goto err_out;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return dev;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800322
323err_out:
324 dev->dev.driver = NULL;
325 dev->card_link = NULL;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800326 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/**
330 * pnp_release_card_device - call this when the driver no longer needs the device
331 * @dev: pointer to the PnP device stucture
332 */
333
334void pnp_release_card_device(struct pnp_dev * dev)
335{
336 struct pnp_card_driver * drv = dev->card_link->driver;
337 if (!drv)
338 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 drv->link.remove = &card_remove;
340 device_release_driver(&dev->dev);
341 drv->link.remove = &card_remove_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100344/*
345 * suspend/resume callbacks
346 */
347static int card_suspend(struct pnp_dev *dev, pm_message_t state)
348{
349 struct pnp_card_link *link = dev->card_link;
350 if (link->pm_state.event == state.event)
351 return 0;
352 link->pm_state = state;
353 return link->driver->suspend(link, state);
354}
355
356static int card_resume(struct pnp_dev *dev)
357{
358 struct pnp_card_link *link = dev->card_link;
359 if (link->pm_state.event == PM_EVENT_ON)
360 return 0;
361 link->pm_state = PMSG_ON;
362 link->driver->resume(link);
363 return 0;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/**
367 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
368 * @drv: pointer to the driver to register
369 */
370
371int pnp_register_card_driver(struct pnp_card_driver * drv)
372{
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800373 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct list_head *pos, *temp;
375
376 drv->link.name = drv->name;
377 drv->link.id_table = NULL; /* this will disable auto matching */
378 drv->link.flags = drv->flags;
379 drv->link.probe = NULL;
380 drv->link.remove = &card_remove_first;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100381 drv->link.suspend = drv->suspend ? card_suspend : NULL;
382 drv->link.resume = drv->resume ? card_resume : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800384 error = pnp_register_driver(&drv->link);
385 if (error < 0)
386 return error;
Adam Belayd1d051b22006-01-20 09:29:27 +0100387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_lock(&pnp_lock);
389 list_add_tail(&drv->global_list, &pnp_card_drivers);
390 spin_unlock(&pnp_lock);
Adam Belayd1d051b22006-01-20 09:29:27 +0100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 list_for_each_safe(pos,temp,&pnp_cards){
393 struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800394 card_probe(card,drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399/**
400 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
401 * @drv: pointer to the driver to unregister
402 */
403
404void pnp_unregister_card_driver(struct pnp_card_driver * drv)
405{
406 spin_lock(&pnp_lock);
407 list_del(&drv->global_list);
408 spin_unlock(&pnp_lock);
409 pnp_unregister_driver(&drv->link);
410}
411
Adrian Bunkb449f632005-11-07 01:01:48 -0800412#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413EXPORT_SYMBOL(pnp_add_card);
414EXPORT_SYMBOL(pnp_remove_card);
415EXPORT_SYMBOL(pnp_add_card_device);
416EXPORT_SYMBOL(pnp_remove_card_device);
417EXPORT_SYMBOL(pnp_add_card_id);
Adrian Bunkb449f632005-11-07 01:01:48 -0800418#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419EXPORT_SYMBOL(pnp_request_card_device);
420EXPORT_SYMBOL(pnp_release_card_device);
421EXPORT_SYMBOL(pnp_register_card_driver);
422EXPORT_SYMBOL(pnp_unregister_card_driver);