]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/pnp/card.c
Remove obsolete #include <linux/config.h>
[mirror_ubuntu-jammy-kernel.git] / drivers / pnp / card.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
8#include <linux/module.h>
9#include <linux/slab.h>
1da177e4
LT
10#include <linux/pnp.h>
11#include "base.h"
12
13LIST_HEAD(pnp_cards);
b449f63c 14static LIST_HEAD(pnp_card_drivers);
1da177e4
LT
15
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}
982c6094 49
1da177e4
LT
50static 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
a9adb8db 62static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
1da177e4 63{
a9adb8db
JJ
64 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);
1da177e4 88 }
a9adb8db 89 kfree(clink);
1da177e4
LT
90 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
e404e274 140static ssize_t pnp_show_card_name(struct device *dmdev, struct device_attribute *attr, char *buf)
1da177e4
LT
141{
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
e404e274 150static ssize_t pnp_show_card_ids(struct device *dmdev, struct device_attribute *attr, char *buf)
1da177e4
LT
151{
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{
167 device_create_file(&card->dev,&dev_attr_name);
168 device_create_file(&card->dev,&dev_attr_card_id);
169 return 0;
170}
171
172/**
173 * pnp_add_card - adds a PnP card to the PnP Layer
174 * @card: pointer to the card to add
175 */
176
177int pnp_add_card(struct pnp_card * card)
178{
179 int error;
180 struct list_head * pos, * temp;
181 if (!card || !card->protocol)
182 return -EINVAL;
183
184 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number, card->number);
185 card->dev.parent = &card->protocol->dev;
186 card->dev.bus = NULL;
187 card->dev.release = &pnp_release_card;
188 error = device_register(&card->dev);
189
190 if (error == 0) {
191 pnp_interface_attach_card(card);
192 spin_lock(&pnp_lock);
193 list_add_tail(&card->global_list, &pnp_cards);
194 list_add_tail(&card->protocol_list, &card->protocol->cards);
195 spin_unlock(&pnp_lock);
196
197 /* we wait until now to add devices in order to ensure the drivers
198 * will be able to use all of the related devices on the card
199 * without waiting any unresonable length of time */
200 list_for_each(pos,&card->devices){
201 struct pnp_dev *dev = card_to_pnp_dev(pos);
202 __pnp_add_device(dev);
203 }
204
205 /* match with card drivers */
206 list_for_each_safe(pos,temp,&pnp_card_drivers){
207 struct pnp_card_driver * drv = list_entry(pos, struct pnp_card_driver, global_list);
208 card_probe(card,drv);
209 }
210 } else
211 pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
212 return error;
213}
214
215/**
216 * pnp_remove_card - removes a PnP card from the PnP Layer
217 * @card: pointer to the card to remove
218 */
219
220void pnp_remove_card(struct pnp_card * card)
221{
222 struct list_head *pos, *temp;
223 if (!card)
224 return;
225 device_unregister(&card->dev);
226 spin_lock(&pnp_lock);
227 list_del(&card->global_list);
228 list_del(&card->protocol_list);
229 spin_unlock(&pnp_lock);
230 list_for_each_safe(pos,temp,&card->devices){
231 struct pnp_dev *dev = card_to_pnp_dev(pos);
232 pnp_remove_card_device(dev);
233 }
234}
235
236/**
237 * pnp_add_card_device - adds a device to the specified card
238 * @card: pointer to the card to add to
239 * @dev: pointer to the device to add
240 */
241
242int pnp_add_card_device(struct pnp_card * card, struct pnp_dev * dev)
243{
244 if (!card || !dev || !dev->protocol)
245 return -EINVAL;
246 dev->dev.parent = &card->dev;
247 dev->card_link = NULL;
248 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x", dev->protocol->number,
249 card->number,dev->number);
250 spin_lock(&pnp_lock);
251 dev->card = card;
252 list_add_tail(&dev->card_list, &card->devices);
253 spin_unlock(&pnp_lock);
254 return 0;
255}
256
257/**
258 * pnp_remove_card_device- removes a device from the specified card
1da177e4
LT
259 * @dev: pointer to the device to remove
260 */
261
262void pnp_remove_card_device(struct pnp_dev * dev)
263{
264 spin_lock(&pnp_lock);
265 dev->card = NULL;
266 list_del(&dev->card_list);
267 spin_unlock(&pnp_lock);
268 __pnp_remove_device(dev);
269}
270
271/**
272 * pnp_request_card_device - Searches for a PnP device under the specified card
3d41088f 273 * @clink: pointer to the card link, cannot be NULL
1da177e4
LT
274 * @id: pointer to a PnP ID structure that explains the rules for finding the device
275 * @from: Starting place to search from. If NULL it will start from the begining.
276 */
277
278struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
279{
280 struct list_head * pos;
281 struct pnp_dev * dev;
282 struct pnp_card_driver * drv;
283 struct pnp_card * card;
284 if (!clink || !id)
285 goto done;
286 card = clink->card;
287 drv = clink->driver;
288 if (!from) {
289 pos = card->devices.next;
290 } else {
291 if (from->card != card)
292 goto done;
293 pos = from->card_list.next;
294 }
295 while (pos != &card->devices) {
296 dev = card_to_pnp_dev(pos);
297 if ((!dev->card_link) && compare_pnp_id(dev->id,id))
298 goto found;
299 pos = pos->next;
300 }
301
302done:
303 return NULL;
304
305found:
306 down_write(&dev->dev.bus->subsys.rwsem);
307 dev->card_link = clink;
308 dev->dev.driver = &drv->link.driver;
8b613e1c
AB
309 if (pnp_bus_type.probe(&dev->dev)) {
310 dev->dev.driver = NULL;
311 dev->card_link = NULL;
312 up_write(&dev->dev.bus->subsys.rwsem);
313 return NULL;
1da177e4
LT
314 }
315 device_bind_driver(&dev->dev);
316 up_write(&dev->dev.bus->subsys.rwsem);
317
318 return dev;
319}
320
321/**
322 * pnp_release_card_device - call this when the driver no longer needs the device
323 * @dev: pointer to the PnP device stucture
324 */
325
326void pnp_release_card_device(struct pnp_dev * dev)
327{
328 struct pnp_card_driver * drv = dev->card_link->driver;
329 if (!drv)
330 return;
331 down_write(&dev->dev.bus->subsys.rwsem);
332 drv->link.remove = &card_remove;
333 device_release_driver(&dev->dev);
334 drv->link.remove = &card_remove_first;
335 up_write(&dev->dev.bus->subsys.rwsem);
336}
337
4c98cfef
TI
338/*
339 * suspend/resume callbacks
340 */
341static int card_suspend(struct pnp_dev *dev, pm_message_t state)
342{
343 struct pnp_card_link *link = dev->card_link;
344 if (link->pm_state.event == state.event)
345 return 0;
346 link->pm_state = state;
347 return link->driver->suspend(link, state);
348}
349
350static int card_resume(struct pnp_dev *dev)
351{
352 struct pnp_card_link *link = dev->card_link;
353 if (link->pm_state.event == PM_EVENT_ON)
354 return 0;
355 link->pm_state = PMSG_ON;
356 link->driver->resume(link);
357 return 0;
358}
359
1da177e4
LT
360/**
361 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
362 * @drv: pointer to the driver to register
363 */
364
365int pnp_register_card_driver(struct pnp_card_driver * drv)
366{
982c6094 367 int error;
1da177e4
LT
368 struct list_head *pos, *temp;
369
370 drv->link.name = drv->name;
371 drv->link.id_table = NULL; /* this will disable auto matching */
372 drv->link.flags = drv->flags;
373 drv->link.probe = NULL;
374 drv->link.remove = &card_remove_first;
4c98cfef
TI
375 drv->link.suspend = drv->suspend ? card_suspend : NULL;
376 drv->link.resume = drv->resume ? card_resume : NULL;
1da177e4 377
982c6094
BH
378 error = pnp_register_driver(&drv->link);
379 if (error < 0)
380 return error;
d1d051b2 381
1da177e4
LT
382 spin_lock(&pnp_lock);
383 list_add_tail(&drv->global_list, &pnp_card_drivers);
384 spin_unlock(&pnp_lock);
d1d051b2 385
1da177e4
LT
386 list_for_each_safe(pos,temp,&pnp_cards){
387 struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
982c6094 388 card_probe(card,drv);
1da177e4 389 }
982c6094 390 return 0;
1da177e4
LT
391}
392
393/**
394 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
395 * @drv: pointer to the driver to unregister
396 */
397
398void pnp_unregister_card_driver(struct pnp_card_driver * drv)
399{
400 spin_lock(&pnp_lock);
401 list_del(&drv->global_list);
402 spin_unlock(&pnp_lock);
403 pnp_unregister_driver(&drv->link);
404}
405
b449f63c 406#if 0
1da177e4
LT
407EXPORT_SYMBOL(pnp_add_card);
408EXPORT_SYMBOL(pnp_remove_card);
409EXPORT_SYMBOL(pnp_add_card_device);
410EXPORT_SYMBOL(pnp_remove_card_device);
411EXPORT_SYMBOL(pnp_add_card_id);
b449f63c 412#endif /* 0 */
1da177e4
LT
413EXPORT_SYMBOL(pnp_request_card_device);
414EXPORT_SYMBOL(pnp_release_card_device);
415EXPORT_SYMBOL(pnp_register_card_driver);
416EXPORT_SYMBOL(pnp_unregister_card_driver);