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