blob: dcff45a19bcb179f6de9b8eb890ad7482cfa8617 [file] [log] [blame]
Alex Dubov4020f2d2006-10-04 02:15:37 -07001/*
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"
Alex Dubov4552f0c2007-04-12 16:59:12 +100017#define DRIVER_VERSION "0.8"
Alex Dubov4020f2d2006-10-04 02:15:37 -070018
19static DEFINE_IDR(tifm_adapter_idr);
20static DEFINE_SPINLOCK(tifm_adapter_lock);
21
22static 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
33static 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
46static 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
Alex Dubov8dc4a612007-04-12 16:59:13 +100063static int tifm_device_probe(struct device *dev)
64{
65 struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
66 struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
67 driver);
68 int rc = -ENODEV;
69
70 get_device(dev);
71 if (dev->driver && drv->probe) {
72 rc = drv->probe(sock);
73 if (!rc)
74 return 0;
75 }
76 put_device(dev);
77 return rc;
78}
79
80static void tifm_dummy_event(struct tifm_dev *sock)
81{
82 return;
83}
84
85static int tifm_device_remove(struct device *dev)
86{
87 struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
88 struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
89 driver);
90
91 if (dev->driver && drv->remove) {
92 sock->card_event = tifm_dummy_event;
93 sock->data_event = tifm_dummy_event;
94 drv->remove(sock);
95 sock->dev.driver = NULL;
96 }
97
98 put_device(dev);
99 return 0;
100}
101
Alex Dubov41d78f72006-12-11 01:55:37 +1100102#ifdef CONFIG_PM
103
104static int tifm_device_suspend(struct device *dev, pm_message_t state)
105{
106 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
Alex Dubov8dc4a612007-04-12 16:59:13 +1000107 struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
108 driver);
Alex Dubov41d78f72006-12-11 01:55:37 +1100109
Alex Dubov8dc4a612007-04-12 16:59:13 +1000110 if (dev->driver && drv->suspend)
Alex Dubov41d78f72006-12-11 01:55:37 +1100111 return drv->suspend(fm_dev, state);
112 return 0;
113}
114
115static int tifm_device_resume(struct device *dev)
116{
117 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
Alex Dubov8dc4a612007-04-12 16:59:13 +1000118 struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
119 driver);
Alex Dubov41d78f72006-12-11 01:55:37 +1100120
Alex Dubov8dc4a612007-04-12 16:59:13 +1000121 if (dev->driver && drv->resume)
Alex Dubov41d78f72006-12-11 01:55:37 +1100122 return drv->resume(fm_dev);
123 return 0;
124}
125
126#else
127
128#define tifm_device_suspend NULL
129#define tifm_device_resume NULL
130
131#endif /* CONFIG_PM */
132
Alex Dubov4020f2d2006-10-04 02:15:37 -0700133static struct bus_type tifm_bus_type = {
134 .name = "tifm",
135 .match = tifm_match,
136 .uevent = tifm_uevent,
Alex Dubov8dc4a612007-04-12 16:59:13 +1000137 .probe = tifm_device_probe,
138 .remove = tifm_device_remove,
Alex Dubov41d78f72006-12-11 01:55:37 +1100139 .suspend = tifm_device_suspend,
140 .resume = tifm_device_resume
Alex Dubov4020f2d2006-10-04 02:15:37 -0700141};
142
143static void tifm_free(struct class_device *cdev)
144{
145 struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev);
146
147 kfree(fm->sockets);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700148 kfree(fm);
149}
150
151static struct class tifm_adapter_class = {
152 .name = "tifm_adapter",
153 .release = tifm_free
154};
155
156struct tifm_adapter *tifm_alloc_adapter(void)
157{
158 struct tifm_adapter *fm;
159
160 fm = kzalloc(sizeof(struct tifm_adapter), GFP_KERNEL);
161 if (fm) {
162 fm->cdev.class = &tifm_adapter_class;
163 spin_lock_init(&fm->lock);
164 class_device_initialize(&fm->cdev);
165 }
166 return fm;
167}
168EXPORT_SYMBOL(tifm_alloc_adapter);
169
170void tifm_free_adapter(struct tifm_adapter *fm)
171{
172 class_device_put(&fm->cdev);
173}
174EXPORT_SYMBOL(tifm_free_adapter);
175
Alex Dubov7146f0d2006-12-18 14:20:06 +1100176int tifm_add_adapter(struct tifm_adapter *fm,
177 int (*mediathreadfn)(void *data))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700178{
179 int rc;
180
181 if (!idr_pre_get(&tifm_adapter_idr, GFP_KERNEL))
182 return -ENOMEM;
183
184 spin_lock(&tifm_adapter_lock);
185 rc = idr_get_new(&tifm_adapter_idr, fm, &fm->id);
186 spin_unlock(&tifm_adapter_lock);
187 if (!rc) {
188 snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100189 fm->media_switcher = kthread_create(mediathreadfn,
190 fm, "tifm/%u", fm->id);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700191
Alex Dubov7146f0d2006-12-18 14:20:06 +1100192 if (!IS_ERR(fm->media_switcher))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700193 return class_device_add(&fm->cdev);
194
195 spin_lock(&tifm_adapter_lock);
196 idr_remove(&tifm_adapter_idr, fm->id);
197 spin_unlock(&tifm_adapter_lock);
198 rc = -ENOMEM;
199 }
200 return rc;
201}
202EXPORT_SYMBOL(tifm_add_adapter);
203
204void tifm_remove_adapter(struct tifm_adapter *fm)
205{
206 class_device_del(&fm->cdev);
207
208 spin_lock(&tifm_adapter_lock);
209 idr_remove(&tifm_adapter_idr, fm->id);
210 spin_unlock(&tifm_adapter_lock);
211}
212EXPORT_SYMBOL(tifm_remove_adapter);
213
214void tifm_free_device(struct device *dev)
215{
216 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700217 kfree(fm_dev);
218}
219EXPORT_SYMBOL(tifm_free_device);
220
Alex Dubov8e02f852006-12-08 16:50:51 +1100221struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700222{
223 struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
224
225 if (dev) {
226 spin_lock_init(&dev->lock);
Alex Dubov8e02f852006-12-08 16:50:51 +1100227
Alex Dubov4020f2d2006-10-04 02:15:37 -0700228 dev->dev.parent = fm->dev;
229 dev->dev.bus = &tifm_bus_type;
230 dev->dev.release = tifm_free_device;
Alex Dubov4552f0c2007-04-12 16:59:12 +1000231 dev->card_event = tifm_dummy_event;
232 dev->data_event = tifm_dummy_event;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700233 }
234 return dev;
235}
236EXPORT_SYMBOL(tifm_alloc_device);
237
238void tifm_eject(struct tifm_dev *sock)
239{
240 struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
241 fm->eject(fm, sock);
242}
243EXPORT_SYMBOL(tifm_eject);
244
245int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
246 int direction)
247{
248 return pci_map_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
249}
250EXPORT_SYMBOL(tifm_map_sg);
251
252void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
253 int direction)
254{
255 pci_unmap_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
256}
257EXPORT_SYMBOL(tifm_unmap_sg);
258
Alex Dubov4020f2d2006-10-04 02:15:37 -0700259int tifm_register_driver(struct tifm_driver *drv)
260{
261 drv->driver.bus = &tifm_bus_type;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700262
263 return driver_register(&drv->driver);
264}
265EXPORT_SYMBOL(tifm_register_driver);
266
267void tifm_unregister_driver(struct tifm_driver *drv)
268{
269 driver_unregister(&drv->driver);
270}
271EXPORT_SYMBOL(tifm_unregister_driver);
272
273static int __init tifm_init(void)
274{
275 int rc = bus_register(&tifm_bus_type);
276
277 if (!rc) {
278 rc = class_register(&tifm_adapter_class);
279 if (rc)
280 bus_unregister(&tifm_bus_type);
281 }
282
283 return rc;
284}
285
286static void __exit tifm_exit(void)
287{
288 class_unregister(&tifm_adapter_class);
289 bus_unregister(&tifm_bus_type);
290}
291
292subsys_initcall(tifm_init);
293module_exit(tifm_exit);
294
295MODULE_LICENSE("GPL");
296MODULE_AUTHOR("Alex Dubov");
297MODULE_DESCRIPTION("TI FlashMedia core driver");
298MODULE_LICENSE("GPL");
299MODULE_VERSION(DRIVER_VERSION);