]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/mmc/core/sdio_bus.c
mm: introduce coredump parameter structure
[mirror_ubuntu-artful-kernel.git] / drivers / mmc / core / sdio_bus.c
CommitLineData
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
bcfe66e2
PO
23/* show configuration fields */
24#define sdio_config_attr(field, format_string) \
25static ssize_t \
26field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
27{ \
28 struct sdio_func *func; \
29 \
30 func = dev_to_sdio_func (dev); \
31 return sprintf (buf, format_string, func->field); \
32}
33
34sdio_config_attr(class, "0x%02x\n");
35sdio_config_attr(vendor, "0x%04x\n");
36sdio_config_attr(device, "0x%04x\n");
37
38static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
39{
40 struct sdio_func *func = dev_to_sdio_func (dev);
41
42 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
43 func->class, func->vendor, func->device);
44}
45
22bfc979 46static struct device_attribute sdio_dev_attrs[] = {
bcfe66e2
PO
47 __ATTR_RO(class),
48 __ATTR_RO(vendor),
49 __ATTR_RO(device),
50 __ATTR_RO(modalias),
51 __ATTR_NULL,
52};
53
3b38bea0
PO
54static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
55 const struct sdio_device_id *id)
56{
57 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
58 return NULL;
59 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
60 return NULL;
61 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
62 return NULL;
63 return id;
64}
65
66static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
67 struct sdio_driver *sdrv)
68{
69 const struct sdio_device_id *ids;
70
71 ids = sdrv->id_table;
72
73 if (ids) {
74 while (ids->class || ids->vendor || ids->device) {
75 if (sdio_match_one(func, ids))
76 return ids;
77 ids++;
78 }
79 }
80
81 return NULL;
82}
e29a7d73 83
e29a7d73
PO
84static int sdio_bus_match(struct device *dev, struct device_driver *drv)
85{
3b38bea0
PO
86 struct sdio_func *func = dev_to_sdio_func(dev);
87 struct sdio_driver *sdrv = to_sdio_driver(drv);
88
89 if (sdio_match_device(func, sdrv))
90 return 1;
91
92 return 0;
e29a7d73
PO
93}
94
95static int
7ac0326c 96sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
e29a7d73 97{
d59b66c7 98 struct sdio_func *func = dev_to_sdio_func(dev);
d59b66c7 99
7ac0326c 100 if (add_uevent_var(env,
d59b66c7
PO
101 "SDIO_CLASS=%02X", func->class))
102 return -ENOMEM;
103
7ac0326c 104 if (add_uevent_var(env,
d59b66c7
PO
105 "SDIO_ID=%04X:%04X", func->vendor, func->device))
106 return -ENOMEM;
107
7ac0326c 108 if (add_uevent_var(env,
d59b66c7
PO
109 "MODALIAS=sdio:c%02Xv%04Xd%04X",
110 func->class, func->vendor, func->device))
111 return -ENOMEM;
112
e29a7d73
PO
113 return 0;
114}
115
116static int sdio_bus_probe(struct device *dev)
117{
3b38bea0
PO
118 struct sdio_driver *drv = to_sdio_driver(dev->driver);
119 struct sdio_func *func = dev_to_sdio_func(dev);
120 const struct sdio_device_id *id;
9a08f82b 121 int ret;
3b38bea0
PO
122
123 id = sdio_match_device(func, drv);
124 if (!id)
125 return -ENODEV;
126
9a08f82b
DV
127 /* Set the default block size so the driver is sure it's something
128 * sensible. */
129 sdio_claim_host(func);
130 ret = sdio_set_block_size(func, 0);
131 sdio_release_host(func);
132 if (ret)
133 return ret;
134
3b38bea0 135 return drv->probe(func, id);
e29a7d73
PO
136}
137
138static int sdio_bus_remove(struct device *dev)
139{
3b38bea0
PO
140 struct sdio_driver *drv = to_sdio_driver(dev->driver);
141 struct sdio_func *func = dev_to_sdio_func(dev);
142
143 drv->remove(func);
144
d1496c39
NP
145 if (func->irq_handler) {
146 printk(KERN_WARNING "WARNING: driver %s did not remove "
147 "its interrupt handler!\n", drv->name);
148 sdio_claim_host(func);
149 sdio_release_irq(func);
150 sdio_release_host(func);
151 }
152
e29a7d73
PO
153 return 0;
154}
155
156static struct bus_type sdio_bus_type = {
157 .name = "sdio",
bcfe66e2 158 .dev_attrs = sdio_dev_attrs,
e29a7d73
PO
159 .match = sdio_bus_match,
160 .uevent = sdio_bus_uevent,
161 .probe = sdio_bus_probe,
162 .remove = sdio_bus_remove,
163};
164
165int sdio_register_bus(void)
166{
167 return bus_register(&sdio_bus_type);
168}
169
170void sdio_unregister_bus(void)
171{
172 bus_unregister(&sdio_bus_type);
173}
174
f76c8515
PO
175/**
176 * sdio_register_driver - register a function driver
177 * @drv: SDIO function driver
178 */
179int sdio_register_driver(struct sdio_driver *drv)
180{
181 drv->drv.name = drv->name;
182 drv->drv.bus = &sdio_bus_type;
183 return driver_register(&drv->drv);
184}
185EXPORT_SYMBOL_GPL(sdio_register_driver);
186
187/**
188 * sdio_unregister_driver - unregister a function driver
189 * @drv: SDIO function driver
190 */
191void sdio_unregister_driver(struct sdio_driver *drv)
192{
193 drv->drv.bus = &sdio_bus_type;
194 driver_unregister(&drv->drv);
195}
196EXPORT_SYMBOL_GPL(sdio_unregister_driver);
197
e29a7d73
PO
198static void sdio_release_func(struct device *dev)
199{
200 struct sdio_func *func = dev_to_sdio_func(dev);
b1538bcf 201
1a632f8c 202 sdio_free_func_cis(func);
e29a7d73 203
759bdc7a
PO
204 if (func->info)
205 kfree(func->info);
206
e29a7d73
PO
207 kfree(func);
208}
209
210/*
211 * Allocate and initialise a new SDIO function structure.
212 */
213struct sdio_func *sdio_alloc_func(struct mmc_card *card)
214{
215 struct sdio_func *func;
216
9f2fcf99 217 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
e29a7d73
PO
218 if (!func)
219 return ERR_PTR(-ENOMEM);
220
e29a7d73
PO
221 func->card = card;
222
223 device_initialize(&func->dev);
224
225 func->dev.parent = &card->dev;
226 func->dev.bus = &sdio_bus_type;
227 func->dev.release = sdio_release_func;
228
229 return func;
230}
231
232/*
233 * Register a new SDIO function with the driver model.
234 */
235int sdio_add_func(struct sdio_func *func)
236{
237 int ret;
238
d1b26863 239 dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
e29a7d73
PO
240
241 ret = device_add(&func->dev);
242 if (ret == 0)
243 sdio_func_set_present(func);
244
245 return ret;
246}
247
248/*
249 * Unregister a SDIO function with the driver model, and
250 * (eventually) free it.
251 */
252void sdio_remove_func(struct sdio_func *func)
253{
254 if (sdio_func_present(func))
255 device_del(&func->dev);
256
257 put_device(&func->dev);
258}
259