blob: a3a89e973d940396038cee21f9d5dd1498e98851 [file] [log] [blame]
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001/*
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
Nicolas Pitreb1538bc2007-06-16 02:06:47 -040020#include "sdio_cis.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020021#include "sdio_bus.h"
22
23#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
Pierre Ossman3b38bea2007-06-16 15:54:55 +020024#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
Pierre Ossmane29a7d72007-05-26 13:48:18 +020025
Pierre Ossman3b38bea2007-06-16 15:54:55 +020026static 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
38static 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}
55
Pierre Ossmane29a7d72007-05-26 13:48:18 +020056static int sdio_bus_match(struct device *dev, struct device_driver *drv)
57{
Pierre Ossman3b38bea2007-06-16 15:54:55 +020058 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;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020065}
66
67static int
68sdio_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
76static int sdio_bus_probe(struct device *dev)
77{
Pierre Ossman3b38bea2007-06-16 15:54:55 +020078 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);
Pierre Ossmane29a7d72007-05-26 13:48:18 +020087}
88
89static int sdio_bus_remove(struct device *dev)
90{
Pierre Ossman3b38bea2007-06-16 15:54:55 +020091 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
Pierre Ossmane29a7d72007-05-26 13:48:18 +020096 return 0;
97}
98
99static 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
107int sdio_register_bus(void)
108{
109 return bus_register(&sdio_bus_type);
110}
111
112void sdio_unregister_bus(void)
113{
114 bus_unregister(&sdio_bus_type);
115}
116
Pierre Ossmanf76c8512007-05-27 12:00:02 +0200117/**
118 * sdio_register_driver - register a function driver
119 * @drv: SDIO function driver
120 */
121int 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}
127EXPORT_SYMBOL_GPL(sdio_register_driver);
128
129/**
130 * sdio_unregister_driver - unregister a function driver
131 * @drv: SDIO function driver
132 */
133void sdio_unregister_driver(struct sdio_driver *drv)
134{
135 drv->drv.bus = &sdio_bus_type;
136 driver_unregister(&drv->drv);
137}
138EXPORT_SYMBOL_GPL(sdio_unregister_driver);
139
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200140static void sdio_release_func(struct device *dev)
141{
142 struct sdio_func *func = dev_to_sdio_func(dev);
143
Pierre Ossman1a632f82007-07-30 15:15:30 +0200144 sdio_free_func_cis(func);
Nicolas Pitreb1538bc2007-06-16 02:06:47 -0400145
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200146 kfree(func);
147}
148
149/*
150 * Allocate and initialise a new SDIO function structure.
151 */
152struct 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 */
176int 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 */
194void 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