blob: 21eb0ab7c32976c83e7213d5027e30016f7e7969 [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"
17#define DRIVER_VERSION "0.6"
18
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
63static struct bus_type tifm_bus_type = {
64 .name = "tifm",
65 .match = tifm_match,
66 .uevent = tifm_uevent,
67};
68
69static void tifm_free(struct class_device *cdev)
70{
71 struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev);
72
73 kfree(fm->sockets);
74 if (fm->wq)
75 destroy_workqueue(fm->wq);
76 kfree(fm);
77}
78
79static struct class tifm_adapter_class = {
80 .name = "tifm_adapter",
81 .release = tifm_free
82};
83
84struct tifm_adapter *tifm_alloc_adapter(void)
85{
86 struct tifm_adapter *fm;
87
88 fm = kzalloc(sizeof(struct tifm_adapter), GFP_KERNEL);
89 if (fm) {
90 fm->cdev.class = &tifm_adapter_class;
91 spin_lock_init(&fm->lock);
92 class_device_initialize(&fm->cdev);
93 }
94 return fm;
95}
96EXPORT_SYMBOL(tifm_alloc_adapter);
97
98void tifm_free_adapter(struct tifm_adapter *fm)
99{
100 class_device_put(&fm->cdev);
101}
102EXPORT_SYMBOL(tifm_free_adapter);
103
104int tifm_add_adapter(struct tifm_adapter *fm)
105{
106 int rc;
107
108 if (!idr_pre_get(&tifm_adapter_idr, GFP_KERNEL))
109 return -ENOMEM;
110
111 spin_lock(&tifm_adapter_lock);
112 rc = idr_get_new(&tifm_adapter_idr, fm, &fm->id);
113 spin_unlock(&tifm_adapter_lock);
114 if (!rc) {
115 snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id);
116 strncpy(fm->wq_name, fm->cdev.class_id, KOBJ_NAME_LEN);
117
118 fm->wq = create_singlethread_workqueue(fm->wq_name);
119 if (fm->wq)
120 return class_device_add(&fm->cdev);
121
122 spin_lock(&tifm_adapter_lock);
123 idr_remove(&tifm_adapter_idr, fm->id);
124 spin_unlock(&tifm_adapter_lock);
125 rc = -ENOMEM;
126 }
127 return rc;
128}
129EXPORT_SYMBOL(tifm_add_adapter);
130
131void tifm_remove_adapter(struct tifm_adapter *fm)
132{
133 class_device_del(&fm->cdev);
134
135 spin_lock(&tifm_adapter_lock);
136 idr_remove(&tifm_adapter_idr, fm->id);
137 spin_unlock(&tifm_adapter_lock);
138}
139EXPORT_SYMBOL(tifm_remove_adapter);
140
141void tifm_free_device(struct device *dev)
142{
143 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700144 kfree(fm_dev);
145}
146EXPORT_SYMBOL(tifm_free_device);
147
Alex Dubov8e02f852006-12-08 16:50:51 +1100148struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700149{
150 struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
151
152 if (dev) {
153 spin_lock_init(&dev->lock);
Alex Dubov8e02f852006-12-08 16:50:51 +1100154
Alex Dubov4020f2d2006-10-04 02:15:37 -0700155 dev->dev.parent = fm->dev;
156 dev->dev.bus = &tifm_bus_type;
157 dev->dev.release = tifm_free_device;
158 }
159 return dev;
160}
161EXPORT_SYMBOL(tifm_alloc_device);
162
163void tifm_eject(struct tifm_dev *sock)
164{
165 struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
166 fm->eject(fm, sock);
167}
168EXPORT_SYMBOL(tifm_eject);
169
170int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
171 int direction)
172{
173 return pci_map_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
174}
175EXPORT_SYMBOL(tifm_map_sg);
176
177void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
178 int direction)
179{
180 pci_unmap_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
181}
182EXPORT_SYMBOL(tifm_unmap_sg);
183
184static int tifm_device_probe(struct device *dev)
185{
186 struct tifm_driver *drv;
187 struct tifm_dev *fm_dev;
188 int rc = 0;
189 const tifm_media_id *id;
190
191 drv = container_of(dev->driver, struct tifm_driver, driver);
192 fm_dev = container_of(dev, struct tifm_dev, dev);
193 get_device(dev);
194 if (!fm_dev->drv && drv->probe && drv->id_table) {
195 rc = -ENODEV;
196 id = tifm_device_match(drv->id_table, fm_dev);
197 if (id)
198 rc = drv->probe(fm_dev);
199 if (rc >= 0) {
200 rc = 0;
201 fm_dev->drv = drv;
202 }
203 }
204 if (rc)
205 put_device(dev);
206 return rc;
207}
208
209static int tifm_device_remove(struct device *dev)
210{
211 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
212 struct tifm_driver *drv = fm_dev->drv;
213
214 if (drv) {
Randy Dunlap3316eaa2006-12-06 20:36:29 -0800215 if (drv->remove)
216 drv->remove(fm_dev);
217 fm_dev->drv = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700218 }
219
220 put_device(dev);
221 return 0;
222}
223
224int tifm_register_driver(struct tifm_driver *drv)
225{
226 drv->driver.bus = &tifm_bus_type;
227 drv->driver.probe = tifm_device_probe;
228 drv->driver.remove = tifm_device_remove;
229
230 return driver_register(&drv->driver);
231}
232EXPORT_SYMBOL(tifm_register_driver);
233
234void tifm_unregister_driver(struct tifm_driver *drv)
235{
236 driver_unregister(&drv->driver);
237}
238EXPORT_SYMBOL(tifm_unregister_driver);
239
240static int __init tifm_init(void)
241{
242 int rc = bus_register(&tifm_bus_type);
243
244 if (!rc) {
245 rc = class_register(&tifm_adapter_class);
246 if (rc)
247 bus_unregister(&tifm_bus_type);
248 }
249
250 return rc;
251}
252
253static void __exit tifm_exit(void)
254{
255 class_unregister(&tifm_adapter_class);
256 bus_unregister(&tifm_bus_type);
257}
258
259subsys_initcall(tifm_init);
260module_exit(tifm_exit);
261
262MODULE_LICENSE("GPL");
263MODULE_AUTHOR("Alex Dubov");
264MODULE_DESCRIPTION("TI FlashMedia core driver");
265MODULE_LICENSE("GPL");
266MODULE_VERSION(DRIVER_VERSION);