Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 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" |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 17 | #define DRIVER_VERSION "0.8" |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 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 | |
Alex Dubov | 8dc4a61 | 2007-04-12 16:59:13 +1000 | [diff] [blame^] | 63 | static 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 | |
| 80 | static void tifm_dummy_event(struct tifm_dev *sock) |
| 81 | { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | static 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 Dubov | 41d78f7 | 2006-12-11 01:55:37 +1100 | [diff] [blame] | 102 | #ifdef CONFIG_PM |
| 103 | |
| 104 | static 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 Dubov | 8dc4a61 | 2007-04-12 16:59:13 +1000 | [diff] [blame^] | 107 | struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver, |
| 108 | driver); |
Alex Dubov | 41d78f7 | 2006-12-11 01:55:37 +1100 | [diff] [blame] | 109 | |
Alex Dubov | 8dc4a61 | 2007-04-12 16:59:13 +1000 | [diff] [blame^] | 110 | if (dev->driver && drv->suspend) |
Alex Dubov | 41d78f7 | 2006-12-11 01:55:37 +1100 | [diff] [blame] | 111 | return drv->suspend(fm_dev, state); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int tifm_device_resume(struct device *dev) |
| 116 | { |
| 117 | struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev); |
Alex Dubov | 8dc4a61 | 2007-04-12 16:59:13 +1000 | [diff] [blame^] | 118 | struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver, |
| 119 | driver); |
Alex Dubov | 41d78f7 | 2006-12-11 01:55:37 +1100 | [diff] [blame] | 120 | |
Alex Dubov | 8dc4a61 | 2007-04-12 16:59:13 +1000 | [diff] [blame^] | 121 | if (dev->driver && drv->resume) |
Alex Dubov | 41d78f7 | 2006-12-11 01:55:37 +1100 | [diff] [blame] | 122 | 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 Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 133 | static struct bus_type tifm_bus_type = { |
| 134 | .name = "tifm", |
| 135 | .match = tifm_match, |
| 136 | .uevent = tifm_uevent, |
Alex Dubov | 8dc4a61 | 2007-04-12 16:59:13 +1000 | [diff] [blame^] | 137 | .probe = tifm_device_probe, |
| 138 | .remove = tifm_device_remove, |
Alex Dubov | 41d78f7 | 2006-12-11 01:55:37 +1100 | [diff] [blame] | 139 | .suspend = tifm_device_suspend, |
| 140 | .resume = tifm_device_resume |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | static 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 Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 148 | kfree(fm); |
| 149 | } |
| 150 | |
| 151 | static struct class tifm_adapter_class = { |
| 152 | .name = "tifm_adapter", |
| 153 | .release = tifm_free |
| 154 | }; |
| 155 | |
| 156 | struct 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 | } |
| 168 | EXPORT_SYMBOL(tifm_alloc_adapter); |
| 169 | |
| 170 | void tifm_free_adapter(struct tifm_adapter *fm) |
| 171 | { |
| 172 | class_device_put(&fm->cdev); |
| 173 | } |
| 174 | EXPORT_SYMBOL(tifm_free_adapter); |
| 175 | |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 176 | int tifm_add_adapter(struct tifm_adapter *fm, |
| 177 | int (*mediathreadfn)(void *data)) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 178 | { |
| 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 Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 189 | fm->media_switcher = kthread_create(mediathreadfn, |
| 190 | fm, "tifm/%u", fm->id); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 191 | |
Alex Dubov | 7146f0d | 2006-12-18 14:20:06 +1100 | [diff] [blame] | 192 | if (!IS_ERR(fm->media_switcher)) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 193 | 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 | } |
| 202 | EXPORT_SYMBOL(tifm_add_adapter); |
| 203 | |
| 204 | void 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 | } |
| 212 | EXPORT_SYMBOL(tifm_remove_adapter); |
| 213 | |
| 214 | void tifm_free_device(struct device *dev) |
| 215 | { |
| 216 | struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev); |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 217 | kfree(fm_dev); |
| 218 | } |
| 219 | EXPORT_SYMBOL(tifm_free_device); |
| 220 | |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 221 | struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm) |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 222 | { |
| 223 | struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL); |
| 224 | |
| 225 | if (dev) { |
| 226 | spin_lock_init(&dev->lock); |
Alex Dubov | 8e02f85 | 2006-12-08 16:50:51 +1100 | [diff] [blame] | 227 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 228 | dev->dev.parent = fm->dev; |
| 229 | dev->dev.bus = &tifm_bus_type; |
| 230 | dev->dev.release = tifm_free_device; |
Alex Dubov | 4552f0c | 2007-04-12 16:59:12 +1000 | [diff] [blame] | 231 | dev->card_event = tifm_dummy_event; |
| 232 | dev->data_event = tifm_dummy_event; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 233 | } |
| 234 | return dev; |
| 235 | } |
| 236 | EXPORT_SYMBOL(tifm_alloc_device); |
| 237 | |
| 238 | void tifm_eject(struct tifm_dev *sock) |
| 239 | { |
| 240 | struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent); |
| 241 | fm->eject(fm, sock); |
| 242 | } |
| 243 | EXPORT_SYMBOL(tifm_eject); |
| 244 | |
| 245 | int 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 | } |
| 250 | EXPORT_SYMBOL(tifm_map_sg); |
| 251 | |
| 252 | void 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 | } |
| 257 | EXPORT_SYMBOL(tifm_unmap_sg); |
| 258 | |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 259 | int tifm_register_driver(struct tifm_driver *drv) |
| 260 | { |
| 261 | drv->driver.bus = &tifm_bus_type; |
Alex Dubov | 4020f2d | 2006-10-04 02:15:37 -0700 | [diff] [blame] | 262 | |
| 263 | return driver_register(&drv->driver); |
| 264 | } |
| 265 | EXPORT_SYMBOL(tifm_register_driver); |
| 266 | |
| 267 | void tifm_unregister_driver(struct tifm_driver *drv) |
| 268 | { |
| 269 | driver_unregister(&drv->driver); |
| 270 | } |
| 271 | EXPORT_SYMBOL(tifm_unregister_driver); |
| 272 | |
| 273 | static 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 | |
| 286 | static void __exit tifm_exit(void) |
| 287 | { |
| 288 | class_unregister(&tifm_adapter_class); |
| 289 | bus_unregister(&tifm_bus_type); |
| 290 | } |
| 291 | |
| 292 | subsys_initcall(tifm_init); |
| 293 | module_exit(tifm_exit); |
| 294 | |
| 295 | MODULE_LICENSE("GPL"); |
| 296 | MODULE_AUTHOR("Alex Dubov"); |
| 297 | MODULE_DESCRIPTION("TI FlashMedia core driver"); |
| 298 | MODULE_LICENSE("GPL"); |
| 299 | MODULE_VERSION(DRIVER_VERSION); |