USB: ftdi-elan: fix sparse warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / tifm_core.c
blobee326136d03bf409d5604229255a9a9591396792
1 /*
2 * tifm_core.c - TI FlashMedia driver
4 * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
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.
12 #include <linux/tifm.h>
13 #include <linux/init.h>
14 #include <linux/idr.h>
16 #define DRIVER_NAME "tifm_core"
17 #define DRIVER_VERSION "0.6"
19 static DEFINE_IDR(tifm_adapter_idr);
20 static DEFINE_SPINLOCK(tifm_adapter_lock);
22 static tifm_media_id *tifm_device_match(tifm_media_id *ids,
23 struct tifm_dev *dev)
25 while (*ids) {
26 if (dev->media_id == *ids)
27 return ids;
28 ids++;
30 return NULL;
33 static int tifm_match(struct device *dev, struct device_driver *drv)
35 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
36 struct tifm_driver *fm_drv;
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;
46 static int tifm_uevent(struct device *dev, char **envp, int num_envp,
47 char *buffer, int buffer_size)
49 struct tifm_dev *fm_dev;
50 int i = 0;
51 int length = 0;
52 const char *card_type_name[] = {"INV", "SM", "MS", "SD"};
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;
60 return 0;
63 static struct bus_type tifm_bus_type = {
64 .name = "tifm",
65 .match = tifm_match,
66 .uevent = tifm_uevent,
69 static void tifm_free(struct class_device *cdev)
71 struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev);
73 kfree(fm->sockets);
74 if (fm->wq)
75 destroy_workqueue(fm->wq);
76 kfree(fm);
79 static struct class tifm_adapter_class = {
80 .name = "tifm_adapter",
81 .release = tifm_free
84 struct tifm_adapter *tifm_alloc_adapter(void)
86 struct tifm_adapter *fm;
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);
94 return fm;
96 EXPORT_SYMBOL(tifm_alloc_adapter);
98 void tifm_free_adapter(struct tifm_adapter *fm)
100 class_device_put(&fm->cdev);
102 EXPORT_SYMBOL(tifm_free_adapter);
104 int tifm_add_adapter(struct tifm_adapter *fm)
106 int rc;
108 if (!idr_pre_get(&tifm_adapter_idr, GFP_KERNEL))
109 return -ENOMEM;
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);
118 fm->wq = create_singlethread_workqueue(fm->wq_name);
119 if (fm->wq)
120 return class_device_add(&fm->cdev);
122 spin_lock(&tifm_adapter_lock);
123 idr_remove(&tifm_adapter_idr, fm->id);
124 spin_unlock(&tifm_adapter_lock);
125 rc = -ENOMEM;
127 return rc;
129 EXPORT_SYMBOL(tifm_add_adapter);
131 void tifm_remove_adapter(struct tifm_adapter *fm)
133 class_device_del(&fm->cdev);
135 spin_lock(&tifm_adapter_lock);
136 idr_remove(&tifm_adapter_idr, fm->id);
137 spin_unlock(&tifm_adapter_lock);
139 EXPORT_SYMBOL(tifm_remove_adapter);
141 void tifm_free_device(struct device *dev)
143 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
144 if (fm_dev->wq)
145 destroy_workqueue(fm_dev->wq);
146 kfree(fm_dev);
148 EXPORT_SYMBOL(tifm_free_device);
150 struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm, unsigned int id)
152 struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
154 if (dev) {
155 spin_lock_init(&dev->lock);
156 snprintf(dev->wq_name, KOBJ_NAME_LEN, "tifm%u:%u", fm->id, id);
157 dev->wq = create_singlethread_workqueue(dev->wq_name);
158 if (!dev->wq) {
159 kfree(dev);
160 return NULL;
162 dev->dev.parent = fm->dev;
163 dev->dev.bus = &tifm_bus_type;
164 dev->dev.release = tifm_free_device;
166 return dev;
168 EXPORT_SYMBOL(tifm_alloc_device);
170 void tifm_eject(struct tifm_dev *sock)
172 struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
173 fm->eject(fm, sock);
175 EXPORT_SYMBOL(tifm_eject);
177 int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
178 int direction)
180 return pci_map_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
182 EXPORT_SYMBOL(tifm_map_sg);
184 void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
185 int direction)
187 pci_unmap_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
189 EXPORT_SYMBOL(tifm_unmap_sg);
191 static int tifm_device_probe(struct device *dev)
193 struct tifm_driver *drv;
194 struct tifm_dev *fm_dev;
195 int rc = 0;
196 const tifm_media_id *id;
198 drv = container_of(dev->driver, struct tifm_driver, driver);
199 fm_dev = container_of(dev, struct tifm_dev, dev);
200 get_device(dev);
201 if (!fm_dev->drv && drv->probe && drv->id_table) {
202 rc = -ENODEV;
203 id = tifm_device_match(drv->id_table, fm_dev);
204 if (id)
205 rc = drv->probe(fm_dev);
206 if (rc >= 0) {
207 rc = 0;
208 fm_dev->drv = drv;
211 if (rc)
212 put_device(dev);
213 return rc;
216 static int tifm_device_remove(struct device *dev)
218 struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
219 struct tifm_driver *drv = fm_dev->drv;
221 if (drv) {
222 if (drv->remove) drv->remove(fm_dev);
223 fm_dev->drv = 0;
226 put_device(dev);
227 return 0;
230 int tifm_register_driver(struct tifm_driver *drv)
232 drv->driver.bus = &tifm_bus_type;
233 drv->driver.probe = tifm_device_probe;
234 drv->driver.remove = tifm_device_remove;
236 return driver_register(&drv->driver);
238 EXPORT_SYMBOL(tifm_register_driver);
240 void tifm_unregister_driver(struct tifm_driver *drv)
242 driver_unregister(&drv->driver);
244 EXPORT_SYMBOL(tifm_unregister_driver);
246 static int __init tifm_init(void)
248 int rc = bus_register(&tifm_bus_type);
250 if (!rc) {
251 rc = class_register(&tifm_adapter_class);
252 if (rc)
253 bus_unregister(&tifm_bus_type);
256 return rc;
259 static void __exit tifm_exit(void)
261 class_unregister(&tifm_adapter_class);
262 bus_unregister(&tifm_bus_type);
265 subsys_initcall(tifm_init);
266 module_exit(tifm_exit);
268 MODULE_LICENSE("GPL");
269 MODULE_AUTHOR("Alex Dubov");
270 MODULE_DESCRIPTION("TI FlashMedia core driver");
271 MODULE_LICENSE("GPL");
272 MODULE_VERSION(DRIVER_VERSION);