OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [linux_sd_driver/] [drivers/] [pnp/] [card.c] - Blame information for rev 65

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 62 marcus.erl
/*
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/slab.h>
9
#include <linux/pnp.h>
10
#include "base.h"
11
 
12
LIST_HEAD(pnp_cards);
13
static LIST_HEAD(pnp_card_drivers);
14
 
15
static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
16
                                                   struct pnp_card *card)
17
{
18
        const struct pnp_card_device_id *drv_id = drv->id_table;
19
 
20
        while (*drv_id->id) {
21
                if (compare_pnp_id(card->id, drv_id->id)) {
22
                        int i = 0;
23
 
24
                        for (;;) {
25
                                int found;
26
                                struct pnp_dev *dev;
27
 
28
                                if (i == PNP_MAX_DEVICES ||
29
                                    !*drv_id->devs[i].id)
30
                                        return drv_id;
31
                                found = 0;
32
                                card_for_each_dev(card, dev) {
33
                                        if (compare_pnp_id(dev->id,
34
                                                   drv_id->devs[i].id)) {
35
                                                found = 1;
36
                                                break;
37
                                        }
38
                                }
39
                                if (!found)
40
                                        break;
41
                                i++;
42
                        }
43
                }
44
                drv_id++;
45
        }
46
        return NULL;
47
}
48
 
49
static void card_remove(struct pnp_dev *dev)
50
{
51
        dev->card_link = NULL;
52
}
53
 
54
static void card_remove_first(struct pnp_dev *dev)
55
{
56
        struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
57
 
58
        if (!dev->card || !drv)
59
                return;
60
        if (drv->remove)
61
                drv->remove(dev->card_link);
62
        drv->link.remove = &card_remove;
63
        kfree(dev->card_link);
64
        card_remove(dev);
65
}
66
 
67
static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
68
{
69
        const struct pnp_card_device_id *id;
70
        struct pnp_card_link *clink;
71
        struct pnp_dev *dev;
72
 
73
        if (!drv->probe)
74
                return 0;
75
        id = match_card(drv, card);
76
        if (!id)
77
                return 0;
78
 
79
        clink = pnp_alloc(sizeof(*clink));
80
        if (!clink)
81
                return 0;
82
        clink->card = card;
83
        clink->driver = drv;
84
        clink->pm_state = PMSG_ON;
85
 
86
        if (drv->probe(clink, id) >= 0)
87
                return 1;
88
 
89
        /* Recovery */
90
        card_for_each_dev(card, dev) {
91
                if (dev->card_link == clink)
92
                        pnp_release_card_device(dev);
93
        }
94
        kfree(clink);
95
        return 0;
96
}
97
 
98
/**
99
 * pnp_add_card_id - adds an EISA id to the specified card
100
 * @id: pointer to a pnp_id structure
101
 * @card: pointer to the desired card
102
 */
103
int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card)
104
{
105
        struct pnp_id *ptr;
106
 
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
 
118
static void pnp_free_card_ids(struct pnp_card *card)
119
{
120
        struct pnp_id *id;
121
        struct pnp_id *next;
122
 
123
        id = card->id;
124
        while (id) {
125
                next = id->next;
126
                kfree(id);
127
                id = next;
128
        }
129
}
130
 
131
static void pnp_release_card(struct device *dmdev)
132
{
133
        struct pnp_card *card = to_pnp_card(dmdev);
134
 
135
        pnp_free_card_ids(card);
136
        kfree(card);
137
}
138
 
139
static ssize_t pnp_show_card_name(struct device *dmdev,
140
                                  struct device_attribute *attr, char *buf)
141
{
142
        char *str = buf;
143
        struct pnp_card *card = to_pnp_card(dmdev);
144
 
145
        str += sprintf(str, "%s\n", card->name);
146
        return (str - buf);
147
}
148
 
149
static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
150
 
151
static ssize_t pnp_show_card_ids(struct device *dmdev,
152
                                 struct device_attribute *attr, char *buf)
153
{
154
        char *str = buf;
155
        struct pnp_card *card = to_pnp_card(dmdev);
156
        struct pnp_id *pos = card->id;
157
 
158
        while (pos) {
159
                str += sprintf(str, "%s\n", pos->id);
160
                pos = pos->next;
161
        }
162
        return (str - buf);
163
}
164
 
165
static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
166
 
167
static int pnp_interface_attach_card(struct pnp_card *card)
168
{
169
        int rc = device_create_file(&card->dev, &dev_attr_name);
170
 
171
        if (rc)
172
                return rc;
173
 
174
        rc = device_create_file(&card->dev, &dev_attr_card_id);
175
        if (rc)
176
                goto err_name;
177
 
178
        return 0;
179
 
180
err_name:
181
        device_remove_file(&card->dev, &dev_attr_name);
182
        return rc;
183
}
184
 
185
/**
186
 * pnp_add_card - adds a PnP card to the PnP Layer
187
 * @card: pointer to the card to add
188
 */
189
int pnp_add_card(struct pnp_card *card)
190
{
191
        int error;
192
        struct list_head *pos, *temp;
193
 
194
        sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
195
                card->number);
196
        card->dev.parent = &card->protocol->dev;
197
        card->dev.bus = NULL;
198
        card->dev.release = &pnp_release_card;
199
        error = device_register(&card->dev);
200
        if (error) {
201
                dev_err(&card->dev, "could not register (err=%d)\n", error);
202
                return error;
203
        }
204
 
205
        pnp_interface_attach_card(card);
206
        spin_lock(&pnp_lock);
207
        list_add_tail(&card->global_list, &pnp_cards);
208
        list_add_tail(&card->protocol_list, &card->protocol->cards);
209
        spin_unlock(&pnp_lock);
210
 
211
        /* we wait until now to add devices in order to ensure the drivers
212
         * will be able to use all of the related devices on the card
213
         * without waiting an unreasonable length of time */
214
        list_for_each(pos, &card->devices) {
215
                struct pnp_dev *dev = card_to_pnp_dev(pos);
216
                __pnp_add_device(dev);
217
        }
218
 
219
        /* match with card drivers */
220
        list_for_each_safe(pos, temp, &pnp_card_drivers) {
221
                struct pnp_card_driver *drv =
222
                    list_entry(pos, struct pnp_card_driver,
223
                               global_list);
224
                card_probe(card, drv);
225
        }
226
        return 0;
227
}
228
 
229
/**
230
 * pnp_remove_card - removes a PnP card from the PnP Layer
231
 * @card: pointer to the card to remove
232
 */
233
void pnp_remove_card(struct pnp_card *card)
234
{
235
        struct list_head *pos, *temp;
236
 
237
        device_unregister(&card->dev);
238
        spin_lock(&pnp_lock);
239
        list_del(&card->global_list);
240
        list_del(&card->protocol_list);
241
        spin_unlock(&pnp_lock);
242
        list_for_each_safe(pos, temp, &card->devices) {
243
                struct pnp_dev *dev = card_to_pnp_dev(pos);
244
                pnp_remove_card_device(dev);
245
        }
246
}
247
 
248
/**
249
 * pnp_add_card_device - adds a device to the specified card
250
 * @card: pointer to the card to add to
251
 * @dev: pointer to the device to add
252
 */
253
int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
254
{
255
        dev->dev.parent = &card->dev;
256
        dev->card_link = NULL;
257
        snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
258
                 dev->protocol->number, card->number, dev->number);
259
        spin_lock(&pnp_lock);
260
        dev->card = card;
261
        list_add_tail(&dev->card_list, &card->devices);
262
        spin_unlock(&pnp_lock);
263
        return 0;
264
}
265
 
266
/**
267
 * pnp_remove_card_device- removes a device from the specified card
268
 * @dev: pointer to the device to remove
269
 */
270
void 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
281
 * @clink: pointer to the card link, cannot be NULL
282
 * @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
struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
286
                                        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
 
293
        if (!clink || !id)
294
                return NULL;
295
 
296
        card = clink->card;
297
        drv = clink->driver;
298
        if (!from) {
299
                pos = card->devices.next;
300
        } else {
301
                if (from->card != card)
302
                        return NULL;
303
                pos = from->card_list.next;
304
        }
305
        while (pos != &card->devices) {
306
                dev = card_to_pnp_dev(pos);
307
                if ((!dev->card_link) && compare_pnp_id(dev->id, id))
308
                        goto found;
309
                pos = pos->next;
310
        }
311
 
312
        return NULL;
313
 
314
found:
315
        dev->card_link = clink;
316
        dev->dev.driver = &drv->link.driver;
317
        if (pnp_bus_type.probe(&dev->dev))
318
                goto err_out;
319
        if (device_bind_driver(&dev->dev))
320
                goto err_out;
321
 
322
        return dev;
323
 
324
err_out:
325
        dev->dev.driver = NULL;
326
        dev->card_link = NULL;
327
        return NULL;
328
}
329
 
330
/**
331
 * pnp_release_card_device - call this when the driver no longer needs the device
332
 * @dev: pointer to the PnP device stucture
333
 */
334
void pnp_release_card_device(struct pnp_dev *dev)
335
{
336
        struct pnp_card_driver *drv = dev->card_link->driver;
337
 
338
        drv->link.remove = &card_remove;
339
        device_release_driver(&dev->dev);
340
        drv->link.remove = &card_remove_first;
341
}
342
 
343
/*
344
 * suspend/resume callbacks
345
 */
346
static int card_suspend(struct pnp_dev *dev, pm_message_t state)
347
{
348
        struct pnp_card_link *link = dev->card_link;
349
 
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
 
356
static int card_resume(struct pnp_dev *dev)
357
{
358
        struct pnp_card_link *link = dev->card_link;
359
 
360
        if (link->pm_state.event == PM_EVENT_ON)
361
                return 0;
362
        link->pm_state = PMSG_ON;
363
        link->driver->resume(link);
364
        return 0;
365
}
366
 
367
/**
368
 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
369
 * @drv: pointer to the driver to register
370
 */
371
int pnp_register_card_driver(struct pnp_card_driver *drv)
372
{
373
        int error;
374
        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;
381
        drv->link.suspend = drv->suspend ? card_suspend : NULL;
382
        drv->link.resume = drv->resume ? card_resume : NULL;
383
 
384
        error = pnp_register_driver(&drv->link);
385
        if (error < 0)
386
                return error;
387
 
388
        spin_lock(&pnp_lock);
389
        list_add_tail(&drv->global_list, &pnp_card_drivers);
390
        spin_unlock(&pnp_lock);
391
 
392
        list_for_each_safe(pos, temp, &pnp_cards) {
393
                struct pnp_card *card =
394
                    list_entry(pos, struct pnp_card, global_list);
395
                card_probe(card, drv);
396
        }
397
        return 0;
398
}
399
 
400
/**
401
 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
402
 * @drv: pointer to the driver to unregister
403
 */
404
void 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
 
412
EXPORT_SYMBOL(pnp_request_card_device);
413
EXPORT_SYMBOL(pnp_release_card_device);
414
EXPORT_SYMBOL(pnp_register_card_driver);
415
EXPORT_SYMBOL(pnp_unregister_card_driver);

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.