Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  tifm_core.c - TI FlashMedia driver
0004  *
0005  *  Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
0006  */
0007 
0008 #include <linux/tifm.h>
0009 #include <linux/slab.h>
0010 #include <linux/init.h>
0011 #include <linux/idr.h>
0012 #include <linux/module.h>
0013 
0014 #define DRIVER_NAME "tifm_core"
0015 #define DRIVER_VERSION "0.8"
0016 
0017 static struct workqueue_struct *workqueue;
0018 static DEFINE_IDR(tifm_adapter_idr);
0019 static DEFINE_SPINLOCK(tifm_adapter_lock);
0020 
0021 static const char *tifm_media_type_name(unsigned char type, unsigned char nt)
0022 {
0023     const char *card_type_name[3][3] = {
0024         { "SmartMedia/xD", "MemoryStick", "MMC/SD" },
0025         { "XD", "MS", "SD"},
0026         { "xd", "ms", "sd"}
0027     };
0028 
0029     if (nt > 2 || type < 1 || type > 3)
0030         return NULL;
0031     return card_type_name[nt][type - 1];
0032 }
0033 
0034 static int tifm_dev_match(struct tifm_dev *sock, struct tifm_device_id *id)
0035 {
0036     if (sock->type == id->type)
0037         return 1;
0038     return 0;
0039 }
0040 
0041 static int tifm_bus_match(struct device *dev, struct device_driver *drv)
0042 {
0043     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0044     struct tifm_driver *fm_drv = container_of(drv, struct tifm_driver,
0045                           driver);
0046     struct tifm_device_id *ids = fm_drv->id_table;
0047 
0048     if (ids) {
0049         while (ids->type) {
0050             if (tifm_dev_match(sock, ids))
0051                 return 1;
0052             ++ids;
0053         }
0054     }
0055     return 0;
0056 }
0057 
0058 static int tifm_uevent(struct device *dev, struct kobj_uevent_env *env)
0059 {
0060     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0061 
0062     if (add_uevent_var(env, "TIFM_CARD_TYPE=%s", tifm_media_type_name(sock->type, 1)))
0063         return -ENOMEM;
0064 
0065     return 0;
0066 }
0067 
0068 static int tifm_device_probe(struct device *dev)
0069 {
0070     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0071     struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
0072                            driver);
0073     int rc = -ENODEV;
0074 
0075     get_device(dev);
0076     if (dev->driver && drv->probe) {
0077         rc = drv->probe(sock);
0078         if (!rc)
0079             return 0;
0080     }
0081     put_device(dev);
0082     return rc;
0083 }
0084 
0085 static void tifm_dummy_event(struct tifm_dev *sock)
0086 {
0087     return;
0088 }
0089 
0090 static void tifm_device_remove(struct device *dev)
0091 {
0092     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0093     struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
0094                            driver);
0095 
0096     if (dev->driver && drv->remove) {
0097         sock->card_event = tifm_dummy_event;
0098         sock->data_event = tifm_dummy_event;
0099         drv->remove(sock);
0100         sock->dev.driver = NULL;
0101     }
0102 
0103     put_device(dev);
0104 }
0105 
0106 #ifdef CONFIG_PM
0107 
0108 static int tifm_device_suspend(struct device *dev, pm_message_t state)
0109 {
0110     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0111     struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
0112                            driver);
0113 
0114     if (dev->driver && drv->suspend)
0115         return drv->suspend(sock, state);
0116     return 0;
0117 }
0118 
0119 static int tifm_device_resume(struct device *dev)
0120 {
0121     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0122     struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
0123                            driver);
0124 
0125     if (dev->driver && drv->resume)
0126         return drv->resume(sock);
0127     return 0;
0128 }
0129 
0130 #else
0131 
0132 #define tifm_device_suspend NULL
0133 #define tifm_device_resume NULL
0134 
0135 #endif /* CONFIG_PM */
0136 
0137 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
0138              char *buf)
0139 {
0140     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0141     return sprintf(buf, "%x", sock->type);
0142 }
0143 static DEVICE_ATTR_RO(type);
0144 
0145 static struct attribute *tifm_dev_attrs[] = {
0146     &dev_attr_type.attr,
0147     NULL,
0148 };
0149 ATTRIBUTE_GROUPS(tifm_dev);
0150 
0151 static struct bus_type tifm_bus_type = {
0152     .name      = "tifm",
0153     .dev_groups = tifm_dev_groups,
0154     .match     = tifm_bus_match,
0155     .uevent    = tifm_uevent,
0156     .probe     = tifm_device_probe,
0157     .remove    = tifm_device_remove,
0158     .suspend   = tifm_device_suspend,
0159     .resume    = tifm_device_resume
0160 };
0161 
0162 static void tifm_free(struct device *dev)
0163 {
0164     struct tifm_adapter *fm = container_of(dev, struct tifm_adapter, dev);
0165 
0166     kfree(fm);
0167 }
0168 
0169 static struct class tifm_adapter_class = {
0170     .name    = "tifm_adapter",
0171     .dev_release = tifm_free
0172 };
0173 
0174 struct tifm_adapter *tifm_alloc_adapter(unsigned int num_sockets,
0175                     struct device *dev)
0176 {
0177     struct tifm_adapter *fm;
0178 
0179     fm = kzalloc(struct_size(fm, sockets, num_sockets), GFP_KERNEL);
0180     if (fm) {
0181         fm->dev.class = &tifm_adapter_class;
0182         fm->dev.parent = dev;
0183         device_initialize(&fm->dev);
0184         spin_lock_init(&fm->lock);
0185         fm->num_sockets = num_sockets;
0186     }
0187     return fm;
0188 }
0189 EXPORT_SYMBOL(tifm_alloc_adapter);
0190 
0191 int tifm_add_adapter(struct tifm_adapter *fm)
0192 {
0193     int rc;
0194 
0195     idr_preload(GFP_KERNEL);
0196     spin_lock(&tifm_adapter_lock);
0197     rc = idr_alloc(&tifm_adapter_idr, fm, 0, 0, GFP_NOWAIT);
0198     if (rc >= 0)
0199         fm->id = rc;
0200     spin_unlock(&tifm_adapter_lock);
0201     idr_preload_end();
0202     if (rc < 0)
0203         return rc;
0204 
0205     dev_set_name(&fm->dev, "tifm%u", fm->id);
0206     rc = device_add(&fm->dev);
0207     if (rc) {
0208         spin_lock(&tifm_adapter_lock);
0209         idr_remove(&tifm_adapter_idr, fm->id);
0210         spin_unlock(&tifm_adapter_lock);
0211     }
0212 
0213     return rc;
0214 }
0215 EXPORT_SYMBOL(tifm_add_adapter);
0216 
0217 void tifm_remove_adapter(struct tifm_adapter *fm)
0218 {
0219     unsigned int cnt;
0220 
0221     flush_workqueue(workqueue);
0222     for (cnt = 0; cnt < fm->num_sockets; ++cnt) {
0223         if (fm->sockets[cnt])
0224             device_unregister(&fm->sockets[cnt]->dev);
0225     }
0226 
0227     spin_lock(&tifm_adapter_lock);
0228     idr_remove(&tifm_adapter_idr, fm->id);
0229     spin_unlock(&tifm_adapter_lock);
0230     device_del(&fm->dev);
0231 }
0232 EXPORT_SYMBOL(tifm_remove_adapter);
0233 
0234 void tifm_free_adapter(struct tifm_adapter *fm)
0235 {
0236     put_device(&fm->dev);
0237 }
0238 EXPORT_SYMBOL(tifm_free_adapter);
0239 
0240 void tifm_free_device(struct device *dev)
0241 {
0242     struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
0243     kfree(sock);
0244 }
0245 EXPORT_SYMBOL(tifm_free_device);
0246 
0247 struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm, unsigned int id,
0248                    unsigned char type)
0249 {
0250     struct tifm_dev *sock = NULL;
0251 
0252     if (!tifm_media_type_name(type, 0))
0253         return sock;
0254 
0255     sock = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
0256     if (sock) {
0257         spin_lock_init(&sock->lock);
0258         sock->type = type;
0259         sock->socket_id = id;
0260         sock->card_event = tifm_dummy_event;
0261         sock->data_event = tifm_dummy_event;
0262 
0263         sock->dev.parent = fm->dev.parent;
0264         sock->dev.bus = &tifm_bus_type;
0265         sock->dev.dma_mask = fm->dev.parent->dma_mask;
0266         sock->dev.release = tifm_free_device;
0267 
0268         dev_set_name(&sock->dev, "tifm_%s%u:%u",
0269                  tifm_media_type_name(type, 2), fm->id, id);
0270         printk(KERN_INFO DRIVER_NAME
0271                ": %s card detected in socket %u:%u\n",
0272                tifm_media_type_name(type, 0), fm->id, id);
0273     }
0274     return sock;
0275 }
0276 EXPORT_SYMBOL(tifm_alloc_device);
0277 
0278 void tifm_eject(struct tifm_dev *sock)
0279 {
0280     struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
0281     fm->eject(fm, sock);
0282 }
0283 EXPORT_SYMBOL(tifm_eject);
0284 
0285 int tifm_has_ms_pif(struct tifm_dev *sock)
0286 {
0287     struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
0288     return fm->has_ms_pif(fm, sock);
0289 }
0290 EXPORT_SYMBOL(tifm_has_ms_pif);
0291 
0292 int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
0293         int direction)
0294 {
0295     return dma_map_sg(&to_pci_dev(sock->dev.parent)->dev, sg, nents,
0296               direction);
0297 }
0298 EXPORT_SYMBOL(tifm_map_sg);
0299 
0300 void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
0301            int direction)
0302 {
0303     dma_unmap_sg(&to_pci_dev(sock->dev.parent)->dev, sg, nents, direction);
0304 }
0305 EXPORT_SYMBOL(tifm_unmap_sg);
0306 
0307 void tifm_queue_work(struct work_struct *work)
0308 {
0309     queue_work(workqueue, work);
0310 }
0311 EXPORT_SYMBOL(tifm_queue_work);
0312 
0313 int tifm_register_driver(struct tifm_driver *drv)
0314 {
0315     drv->driver.bus = &tifm_bus_type;
0316 
0317     return driver_register(&drv->driver);
0318 }
0319 EXPORT_SYMBOL(tifm_register_driver);
0320 
0321 void tifm_unregister_driver(struct tifm_driver *drv)
0322 {
0323     driver_unregister(&drv->driver);
0324 }
0325 EXPORT_SYMBOL(tifm_unregister_driver);
0326 
0327 static int __init tifm_init(void)
0328 {
0329     int rc;
0330 
0331     workqueue = create_freezable_workqueue("tifm");
0332     if (!workqueue)
0333         return -ENOMEM;
0334 
0335     rc = bus_register(&tifm_bus_type);
0336 
0337     if (rc)
0338         goto err_out_wq;
0339 
0340     rc = class_register(&tifm_adapter_class);
0341     if (!rc)
0342         return 0;
0343 
0344     bus_unregister(&tifm_bus_type);
0345 
0346 err_out_wq:
0347     destroy_workqueue(workqueue);
0348 
0349     return rc;
0350 }
0351 
0352 static void __exit tifm_exit(void)
0353 {
0354     class_unregister(&tifm_adapter_class);
0355     bus_unregister(&tifm_bus_type);
0356     destroy_workqueue(workqueue);
0357 }
0358 
0359 subsys_initcall(tifm_init);
0360 module_exit(tifm_exit);
0361 
0362 MODULE_LICENSE("GPL");
0363 MODULE_AUTHOR("Alex Dubov");
0364 MODULE_DESCRIPTION("TI FlashMedia core driver");
0365 MODULE_LICENSE("GPL");
0366 MODULE_VERSION(DRIVER_VERSION);