]>
Commit | Line | Data |
---|---|---|
e29a7d73 PO |
1 | /* |
2 | * linux/drivers/mmc/core/sdio_bus.c | |
3 | * | |
4 | * Copyright 2007 Pierre Ossman | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or (at | |
9 | * your option) any later version. | |
10 | * | |
11 | * SDIO function driver model | |
12 | */ | |
13 | ||
14 | #include <linux/device.h> | |
15 | #include <linux/err.h> | |
16 | ||
17 | #include <linux/mmc/card.h> | |
18 | #include <linux/mmc/sdio_func.h> | |
19 | ||
b1538bcf | 20 | #include "sdio_cis.h" |
e29a7d73 PO |
21 | #include "sdio_bus.h" |
22 | ||
23 | #define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev) | |
3b38bea0 PO |
24 | #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv) |
25 | ||
26 | static const struct sdio_device_id *sdio_match_one(struct sdio_func *func, | |
27 | const struct sdio_device_id *id) | |
28 | { | |
29 | if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class) | |
30 | return NULL; | |
31 | if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor) | |
32 | return NULL; | |
33 | if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device) | |
34 | return NULL; | |
35 | return id; | |
36 | } | |
37 | ||
38 | static const struct sdio_device_id *sdio_match_device(struct sdio_func *func, | |
39 | struct sdio_driver *sdrv) | |
40 | { | |
41 | const struct sdio_device_id *ids; | |
42 | ||
43 | ids = sdrv->id_table; | |
44 | ||
45 | if (ids) { | |
46 | while (ids->class || ids->vendor || ids->device) { | |
47 | if (sdio_match_one(func, ids)) | |
48 | return ids; | |
49 | ids++; | |
50 | } | |
51 | } | |
52 | ||
53 | return NULL; | |
54 | } | |
e29a7d73 | 55 | |
e29a7d73 PO |
56 | static int sdio_bus_match(struct device *dev, struct device_driver *drv) |
57 | { | |
3b38bea0 PO |
58 | struct sdio_func *func = dev_to_sdio_func(dev); |
59 | struct sdio_driver *sdrv = to_sdio_driver(drv); | |
60 | ||
61 | if (sdio_match_device(func, sdrv)) | |
62 | return 1; | |
63 | ||
64 | return 0; | |
e29a7d73 PO |
65 | } |
66 | ||
67 | static int | |
68 | sdio_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf, | |
69 | int buf_size) | |
70 | { | |
71 | envp[0] = NULL; | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | static int sdio_bus_probe(struct device *dev) | |
77 | { | |
3b38bea0 PO |
78 | struct sdio_driver *drv = to_sdio_driver(dev->driver); |
79 | struct sdio_func *func = dev_to_sdio_func(dev); | |
80 | const struct sdio_device_id *id; | |
81 | ||
82 | id = sdio_match_device(func, drv); | |
83 | if (!id) | |
84 | return -ENODEV; | |
85 | ||
86 | return drv->probe(func, id); | |
e29a7d73 PO |
87 | } |
88 | ||
89 | static int sdio_bus_remove(struct device *dev) | |
90 | { | |
3b38bea0 PO |
91 | struct sdio_driver *drv = to_sdio_driver(dev->driver); |
92 | struct sdio_func *func = dev_to_sdio_func(dev); | |
93 | ||
94 | drv->remove(func); | |
95 | ||
e29a7d73 PO |
96 | return 0; |
97 | } | |
98 | ||
99 | static struct bus_type sdio_bus_type = { | |
100 | .name = "sdio", | |
101 | .match = sdio_bus_match, | |
102 | .uevent = sdio_bus_uevent, | |
103 | .probe = sdio_bus_probe, | |
104 | .remove = sdio_bus_remove, | |
105 | }; | |
106 | ||
107 | int sdio_register_bus(void) | |
108 | { | |
109 | return bus_register(&sdio_bus_type); | |
110 | } | |
111 | ||
112 | void sdio_unregister_bus(void) | |
113 | { | |
114 | bus_unregister(&sdio_bus_type); | |
115 | } | |
116 | ||
f76c8515 PO |
117 | /** |
118 | * sdio_register_driver - register a function driver | |
119 | * @drv: SDIO function driver | |
120 | */ | |
121 | int sdio_register_driver(struct sdio_driver *drv) | |
122 | { | |
123 | drv->drv.name = drv->name; | |
124 | drv->drv.bus = &sdio_bus_type; | |
125 | return driver_register(&drv->drv); | |
126 | } | |
127 | EXPORT_SYMBOL_GPL(sdio_register_driver); | |
128 | ||
129 | /** | |
130 | * sdio_unregister_driver - unregister a function driver | |
131 | * @drv: SDIO function driver | |
132 | */ | |
133 | void sdio_unregister_driver(struct sdio_driver *drv) | |
134 | { | |
135 | drv->drv.bus = &sdio_bus_type; | |
136 | driver_unregister(&drv->drv); | |
137 | } | |
138 | EXPORT_SYMBOL_GPL(sdio_unregister_driver); | |
139 | ||
e29a7d73 PO |
140 | static void sdio_release_func(struct device *dev) |
141 | { | |
142 | struct sdio_func *func = dev_to_sdio_func(dev); | |
b1538bcf | 143 | |
1a632f8c | 144 | sdio_free_func_cis(func); |
e29a7d73 PO |
145 | |
146 | kfree(func); | |
147 | } | |
148 | ||
149 | /* | |
150 | * Allocate and initialise a new SDIO function structure. | |
151 | */ | |
152 | struct sdio_func *sdio_alloc_func(struct mmc_card *card) | |
153 | { | |
154 | struct sdio_func *func; | |
155 | ||
156 | func = kmalloc(sizeof(struct sdio_func), GFP_KERNEL); | |
157 | if (!func) | |
158 | return ERR_PTR(-ENOMEM); | |
159 | ||
160 | memset(func, 0, sizeof(struct sdio_func)); | |
161 | ||
162 | func->card = card; | |
163 | ||
164 | device_initialize(&func->dev); | |
165 | ||
166 | func->dev.parent = &card->dev; | |
167 | func->dev.bus = &sdio_bus_type; | |
168 | func->dev.release = sdio_release_func; | |
169 | ||
170 | return func; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Register a new SDIO function with the driver model. | |
175 | */ | |
176 | int sdio_add_func(struct sdio_func *func) | |
177 | { | |
178 | int ret; | |
179 | ||
180 | snprintf(func->dev.bus_id, sizeof(func->dev.bus_id), | |
181 | "%s:%d", mmc_card_id(func->card), func->num); | |
182 | ||
183 | ret = device_add(&func->dev); | |
184 | if (ret == 0) | |
185 | sdio_func_set_present(func); | |
186 | ||
187 | return ret; | |
188 | } | |
189 | ||
190 | /* | |
191 | * Unregister a SDIO function with the driver model, and | |
192 | * (eventually) free it. | |
193 | */ | |
194 | void sdio_remove_func(struct sdio_func *func) | |
195 | { | |
196 | if (sdio_func_present(func)) | |
197 | device_del(&func->dev); | |
198 | ||
199 | put_device(&func->dev); | |
200 | } | |
201 |