Commit | Line | Data |
---|---|---|
4020f2d7 AD |
1 | /* |
2 | * tifm_core.c - TI FlashMedia driver | |
3 | * | |
4 | * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com> | |
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 version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | */ | |
11 | ||
12 | #include <linux/tifm.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/idr.h> | |
15 | ||
16 | #define DRIVER_NAME "tifm_core" | |
4552f0cb | 17 | #define DRIVER_VERSION "0.8" |
4020f2d7 AD |
18 | |
19 | static DEFINE_IDR(tifm_adapter_idr); | |
20 | static DEFINE_SPINLOCK(tifm_adapter_lock); | |
21 | ||
22 | static tifm_media_id *tifm_device_match(tifm_media_id *ids, | |
23 | struct tifm_dev *dev) | |
24 | { | |
25 | while (*ids) { | |
26 | if (dev->media_id == *ids) | |
27 | return ids; | |
28 | ids++; | |
29 | } | |
30 | return NULL; | |
31 | } | |
32 | ||
33 | static int tifm_match(struct device *dev, struct device_driver *drv) | |
34 | { | |
35 | struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev); | |
36 | struct tifm_driver *fm_drv; | |
37 | ||
38 | fm_drv = container_of(drv, struct tifm_driver, driver); | |
39 | if (!fm_drv->id_table) | |
40 | return -EINVAL; | |
41 | if (tifm_device_match(fm_drv->id_table, fm_dev)) | |
42 | return 1; | |
43 | return -ENODEV; | |
44 | } | |
45 | ||
46 | static int tifm_uevent(struct device *dev, char **envp, int num_envp, | |
47 | char *buffer, int buffer_size) | |
48 | { | |
49 | struct tifm_dev *fm_dev; | |
50 | int i = 0; | |
51 | int length = 0; | |
52 | const char *card_type_name[] = {"INV", "SM", "MS", "SD"}; | |
53 | ||
54 | if (!dev || !(fm_dev = container_of(dev, struct tifm_dev, dev))) | |
55 | return -ENODEV; | |
56 | if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, | |
57 | "TIFM_CARD_TYPE=%s", card_type_name[fm_dev->media_id])) | |
58 | return -ENOMEM; | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
41d78f74 AD |
63 | #ifdef CONFIG_PM |
64 | ||
65 | static int tifm_device_suspend(struct device *dev, pm_message_t state) | |
66 | { | |
67 | struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev); | |
68 | struct tifm_driver *drv = fm_dev->drv; | |
69 | ||
70 | if (drv && drv->suspend) | |
71 | return drv->suspend(fm_dev, state); | |
72 | return 0; | |
73 | } | |
74 | ||
75 | static int tifm_device_resume(struct device *dev) | |
76 | { | |
77 | struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev); | |
78 | struct tifm_driver *drv = fm_dev->drv; | |
79 | ||
80 | if (drv && drv->resume) | |
81 | return drv->resume(fm_dev); | |
82 | return 0; | |
83 | } | |
84 | ||
85 | #else | |
86 | ||
87 | #define tifm_device_suspend NULL | |
88 | #define tifm_device_resume NULL | |
89 | ||
90 | #endif /* CONFIG_PM */ | |
91 | ||
4020f2d7 AD |
92 | static struct bus_type tifm_bus_type = { |
93 | .name = "tifm", | |
94 | .match = tifm_match, | |
95 | .uevent = tifm_uevent, | |
41d78f74 AD |
96 | .suspend = tifm_device_suspend, |
97 | .resume = tifm_device_resume | |
4020f2d7 AD |
98 | }; |
99 | ||
100 | static void tifm_free(struct class_device *cdev) | |
101 | { | |
102 | struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev); | |
103 | ||
104 | kfree(fm->sockets); | |
4020f2d7 AD |
105 | kfree(fm); |
106 | } | |
107 | ||
108 | static struct class tifm_adapter_class = { | |
109 | .name = "tifm_adapter", | |
110 | .release = tifm_free | |
111 | }; | |
112 | ||
113 | struct tifm_adapter *tifm_alloc_adapter(void) | |
114 | { | |
115 | struct tifm_adapter *fm; | |
116 | ||
117 | fm = kzalloc(sizeof(struct tifm_adapter), GFP_KERNEL); | |
118 | if (fm) { | |
119 | fm->cdev.class = &tifm_adapter_class; | |
120 | spin_lock_init(&fm->lock); | |
121 | class_device_initialize(&fm->cdev); | |
122 | } | |
123 | return fm; | |
124 | } | |
125 | EXPORT_SYMBOL(tifm_alloc_adapter); | |
126 | ||
127 | void tifm_free_adapter(struct tifm_adapter *fm) | |
128 | { | |
129 | class_device_put(&fm->cdev); | |
130 | } | |
131 | EXPORT_SYMBOL(tifm_free_adapter); | |
132 | ||
7146f0d3 AD |
133 | int tifm_add_adapter(struct tifm_adapter *fm, |
134 | int (*mediathreadfn)(void *data)) | |
4020f2d7 AD |
135 | { |
136 | int rc; | |
137 | ||
138 | if (!idr_pre_get(&tifm_adapter_idr, GFP_KERNEL)) | |
139 | return -ENOMEM; | |
140 | ||
141 | spin_lock(&tifm_adapter_lock); | |
142 | rc = idr_get_new(&tifm_adapter_idr, fm, &fm->id); | |
143 | spin_unlock(&tifm_adapter_lock); | |
144 | if (!rc) { | |
145 | snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id); | |
7146f0d3 AD |
146 | fm->media_switcher = kthread_create(mediathreadfn, |
147 | fm, "tifm/%u", fm->id); | |
4020f2d7 | 148 | |
7146f0d3 | 149 | if (!IS_ERR(fm->media_switcher)) |
4020f2d7 AD |
150 | return class_device_add(&fm->cdev); |
151 | ||
152 | spin_lock(&tifm_adapter_lock); | |
153 | idr_remove(&tifm_adapter_idr, fm->id); | |
154 | spin_unlock(&tifm_adapter_lock); | |
155 | rc = -ENOMEM; | |
156 | } | |
157 | return rc; | |
158 | } | |
159 | EXPORT_SYMBOL(tifm_add_adapter); | |
160 | ||
161 | void tifm_remove_adapter(struct tifm_adapter *fm) | |
162 | { | |
163 | class_device_del(&fm->cdev); | |
164 | ||
165 | spin_lock(&tifm_adapter_lock); | |
166 | idr_remove(&tifm_adapter_idr, fm->id); | |
167 | spin_unlock(&tifm_adapter_lock); | |
168 | } | |
169 | EXPORT_SYMBOL(tifm_remove_adapter); | |
170 | ||
171 | void tifm_free_device(struct device *dev) | |
172 | { | |
173 | struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev); | |
4020f2d7 AD |
174 | kfree(fm_dev); |
175 | } | |
176 | EXPORT_SYMBOL(tifm_free_device); | |
177 | ||
4552f0cb | 178 | static void tifm_dummy_event(struct tifm_dev *sock) |
217334d1 AD |
179 | { |
180 | return; | |
181 | } | |
182 | ||
8e02f858 | 183 | struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm) |
4020f2d7 AD |
184 | { |
185 | struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL); | |
186 | ||
187 | if (dev) { | |
188 | spin_lock_init(&dev->lock); | |
8e02f858 | 189 | |
4020f2d7 AD |
190 | dev->dev.parent = fm->dev; |
191 | dev->dev.bus = &tifm_bus_type; | |
192 | dev->dev.release = tifm_free_device; | |
4552f0cb AD |
193 | dev->card_event = tifm_dummy_event; |
194 | dev->data_event = tifm_dummy_event; | |
4020f2d7 AD |
195 | } |
196 | return dev; | |
197 | } | |
198 | EXPORT_SYMBOL(tifm_alloc_device); | |
199 | ||
200 | void tifm_eject(struct tifm_dev *sock) | |
201 | { | |
202 | struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent); | |
203 | fm->eject(fm, sock); | |
204 | } | |
205 | EXPORT_SYMBOL(tifm_eject); | |
206 | ||
207 | int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents, | |
208 | int direction) | |
209 | { | |
210 | return pci_map_sg(to_pci_dev(sock->dev.parent), sg, nents, direction); | |
211 | } | |
212 | EXPORT_SYMBOL(tifm_map_sg); | |
213 | ||
214 | void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents, | |
215 | int direction) | |
216 | { | |
217 | pci_unmap_sg(to_pci_dev(sock->dev.parent), sg, nents, direction); | |
218 | } | |
219 | EXPORT_SYMBOL(tifm_unmap_sg); | |
220 | ||
221 | static int tifm_device_probe(struct device *dev) | |
222 | { | |
223 | struct tifm_driver *drv; | |
224 | struct tifm_dev *fm_dev; | |
225 | int rc = 0; | |
226 | const tifm_media_id *id; | |
227 | ||
228 | drv = container_of(dev->driver, struct tifm_driver, driver); | |
229 | fm_dev = container_of(dev, struct tifm_dev, dev); | |
230 | get_device(dev); | |
231 | if (!fm_dev->drv && drv->probe && drv->id_table) { | |
232 | rc = -ENODEV; | |
233 | id = tifm_device_match(drv->id_table, fm_dev); | |
234 | if (id) | |
235 | rc = drv->probe(fm_dev); | |
236 | if (rc >= 0) { | |
237 | rc = 0; | |
238 | fm_dev->drv = drv; | |
239 | } | |
240 | } | |
241 | if (rc) | |
242 | put_device(dev); | |
243 | return rc; | |
244 | } | |
245 | ||
246 | static int tifm_device_remove(struct device *dev) | |
247 | { | |
248 | struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev); | |
249 | struct tifm_driver *drv = fm_dev->drv; | |
250 | ||
251 | if (drv) { | |
4552f0cb AD |
252 | fm_dev->card_event = tifm_dummy_event; |
253 | fm_dev->data_event = tifm_dummy_event; | |
3316eaa3 RD |
254 | if (drv->remove) |
255 | drv->remove(fm_dev); | |
256 | fm_dev->drv = NULL; | |
4020f2d7 AD |
257 | } |
258 | ||
259 | put_device(dev); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | int tifm_register_driver(struct tifm_driver *drv) | |
264 | { | |
265 | drv->driver.bus = &tifm_bus_type; | |
266 | drv->driver.probe = tifm_device_probe; | |
267 | drv->driver.remove = tifm_device_remove; | |
41d78f74 AD |
268 | drv->driver.suspend = tifm_device_suspend; |
269 | drv->driver.resume = tifm_device_resume; | |
4020f2d7 AD |
270 | |
271 | return driver_register(&drv->driver); | |
272 | } | |
273 | EXPORT_SYMBOL(tifm_register_driver); | |
274 | ||
275 | void tifm_unregister_driver(struct tifm_driver *drv) | |
276 | { | |
277 | driver_unregister(&drv->driver); | |
278 | } | |
279 | EXPORT_SYMBOL(tifm_unregister_driver); | |
280 | ||
281 | static int __init tifm_init(void) | |
282 | { | |
283 | int rc = bus_register(&tifm_bus_type); | |
284 | ||
285 | if (!rc) { | |
286 | rc = class_register(&tifm_adapter_class); | |
287 | if (rc) | |
288 | bus_unregister(&tifm_bus_type); | |
289 | } | |
290 | ||
291 | return rc; | |
292 | } | |
293 | ||
294 | static void __exit tifm_exit(void) | |
295 | { | |
296 | class_unregister(&tifm_adapter_class); | |
297 | bus_unregister(&tifm_bus_type); | |
298 | } | |
299 | ||
300 | subsys_initcall(tifm_init); | |
301 | module_exit(tifm_exit); | |
302 | ||
303 | MODULE_LICENSE("GPL"); | |
304 | MODULE_AUTHOR("Alex Dubov"); | |
305 | MODULE_DESCRIPTION("TI FlashMedia core driver"); | |
306 | MODULE_LICENSE("GPL"); | |
307 | MODULE_VERSION(DRIVER_VERSION); |