tifm: Convert from class_device to device for TI flash media
[linux-2.6.git] / drivers / misc / tifm_7xx1.c
index e051f9d..54380da 100644 (file)
@@ -11,7 +11,6 @@
 
 #include <linux/tifm.h>
 #include <linux/dma-mapping.h>
-#include <linux/freezer.h>
 
 #define DRIVER_NAME "tifm_7xx1"
 #define DRIVER_VERSION "0.8"
@@ -41,8 +40,7 @@ static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
 {
        struct tifm_adapter *fm = dev_id;
        struct tifm_dev *sock;
-       unsigned int irq_status;
-       unsigned int cnt;
+       unsigned int irq_status, cnt;
 
        spin_lock(&fm->lock);
        irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
@@ -107,7 +105,8 @@ static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
            == TIFM_TYPE_XD)
                msleep(40);
 
-       writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
+       writel((s_state & TIFM_CTRL_POWER_MASK) | 0x0c00,
+              sock_addr + SOCK_CONTROL);
        /* wait for power to stabilize */
        msleep(20);
        for (cnt = 16; cnt <= 256; cnt <<= 1) {
@@ -124,6 +123,12 @@ static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
        return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
 }
 
+inline static void tifm_7xx1_sock_power_off(char __iomem *sock_addr)
+{
+       writel((~TIFM_CTRL_POWER_MASK) & readl(sock_addr + SOCK_CONTROL),
+              sock_addr + SOCK_CONTROL);
+}
+
 inline static char __iomem *
 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
 {
@@ -134,18 +139,17 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
 {
        struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
                                               media_switcher);
+       struct tifm_dev *sock;
+       char __iomem *sock_addr;
        unsigned long flags;
        unsigned char media_id;
-       char *card_name = "xx";
-       int cnt;
-       struct tifm_dev *sock;
-       unsigned int socket_change_set;
+       unsigned int socket_change_set, cnt;
 
        spin_lock_irqsave(&fm->lock, flags);
        socket_change_set = fm->socket_change_set;
        fm->socket_change_set = 0;
 
-       dev_dbg(fm->cdev.dev, "checking media set %x\n",
+       dev_dbg(fm->dev.parent, "checking media set %x\n",
                socket_change_set);
 
        if (!socket_change_set) {
@@ -153,68 +157,46 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
                return;
        }
 
-               for (cnt = 0; cnt < fm->num_sockets; cnt++) {
-                       if (!(socket_change_set & (1 << cnt)))
-                               continue;
-                       sock = fm->sockets[cnt];
-                       if (sock) {
-                               printk(KERN_INFO DRIVER_NAME
-                                      ": demand removing card from socket %d\n",
-                                      cnt);
-                               fm->sockets[cnt] = NULL;
-                               spin_unlock_irqrestore(&fm->lock, flags);
-                               device_unregister(&sock->dev);
-                               spin_lock_irqsave(&fm->lock, flags);
-                               writel(0x0e00,
-                                      tifm_7xx1_sock_addr(fm->addr, cnt)
-                                      + SOCK_CONTROL);
-                       }
-
+       for (cnt = 0; cnt < fm->num_sockets; cnt++) {
+               if (!(socket_change_set & (1 << cnt)))
+                       continue;
+               sock = fm->sockets[cnt];
+               if (sock) {
+                       printk(KERN_INFO
+                              "%s : demand removing card from socket %u:%u\n",
+                              fm->dev.bus_id, fm->id, cnt);
+                       fm->sockets[cnt] = NULL;
+                       sock_addr = sock->addr;
                        spin_unlock_irqrestore(&fm->lock, flags);
-                       media_id = tifm_7xx1_toggle_sock_power(
-                                       tifm_7xx1_sock_addr(fm->addr, cnt));
-                       if (media_id) {
-                               sock = tifm_alloc_device(fm);
-                               if (sock) {
-                                       sock->addr = tifm_7xx1_sock_addr(fm->addr,
-                                                                        cnt);
-                                       sock->type = media_id;
-                                       sock->socket_id = cnt;
-                                       switch (media_id) {
-                                       case 1:
-                                               card_name = "xd";
-                                               break;
-                                       case 2:
-                                               card_name = "ms";
-                                               break;
-                                       case 3:
-                                               card_name = "sd";
-                                               break;
-                                       default:
-                                               tifm_free_device(&sock->dev);
-                                               spin_lock_irqsave(&fm->lock, flags);
-                                               continue;
-                                       }
-                                       snprintf(sock->dev.bus_id, BUS_ID_SIZE,
-                                                "tifm_%s%u:%u", card_name,
-                                                fm->id, cnt);
-                                       printk(KERN_INFO DRIVER_NAME
-                                              ": %s card detected in socket %d\n",
-                                              card_name, cnt);
-                                       if (!device_register(&sock->dev)) {
-                                               spin_lock_irqsave(&fm->lock, flags);
-                                               if (!fm->sockets[cnt]) {
-                                                       fm->sockets[cnt] = sock;
-                                                       sock = NULL;
-                                               }
-                                               spin_unlock_irqrestore(&fm->lock, flags);
-                                       }
-                                       if (sock)
-                                               tifm_free_device(&sock->dev);
-                               }
+                       device_unregister(&sock->dev);
+                       spin_lock_irqsave(&fm->lock, flags);
+                       tifm_7xx1_sock_power_off(sock_addr);
+                       writel(0x0e00, sock_addr + SOCK_CONTROL);
+               }
+
+               spin_unlock_irqrestore(&fm->lock, flags);
+
+               media_id = tifm_7xx1_toggle_sock_power(
+                               tifm_7xx1_sock_addr(fm->addr, cnt));
+
+               // tifm_alloc_device will check if media_id is valid
+               sock = tifm_alloc_device(fm, cnt, media_id);
+               if (sock) {
+                       sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);
+
+                       if (!device_register(&sock->dev)) {
                                spin_lock_irqsave(&fm->lock, flags);
+                               if (!fm->sockets[cnt]) {
+                                       fm->sockets[cnt] = sock;
+                                       sock = NULL;
+                               }
+                               spin_unlock_irqrestore(&fm->lock, flags);
                        }
+                       if (sock)
+                               tifm_free_device(&sock->dev);
                }
+               spin_lock_irqsave(&fm->lock, flags);
+       }
 
        writel(TIFM_IRQ_FIFOMASK(socket_change_set)
               | TIFM_IRQ_CARDMASK(socket_change_set),
@@ -232,8 +214,16 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
 
 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
 {
+       struct tifm_adapter *fm = pci_get_drvdata(dev);
+       int cnt;
+
        dev_dbg(&dev->dev, "suspending host\n");
 
+       for (cnt = 0; cnt < fm->num_sockets; cnt++) {
+               if (fm->sockets[cnt])
+                       tifm_7xx1_sock_power_off(fm->sockets[cnt]->addr);
+       }
+
        pci_save_state(dev);
        pci_enable_wake(dev, pci_choose_state(dev, state), 0);
        pci_disable_device(dev);
@@ -244,7 +234,8 @@ static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
 static int tifm_7xx1_resume(struct pci_dev *dev)
 {
        struct tifm_adapter *fm = pci_get_drvdata(dev);
-       int cnt, rc;
+       int rc;
+       unsigned int good_sockets = 0, bad_sockets = 0;
        unsigned long flags;
        unsigned char new_ids[fm->num_sockets];
        DECLARE_COMPLETION_ONSTACK(finish_resume);
@@ -258,46 +249,49 @@ static int tifm_7xx1_resume(struct pci_dev *dev)
 
        dev_dbg(&dev->dev, "resuming host\n");
 
-       for (cnt = 0; cnt < fm->num_sockets; cnt++)
-               new_ids[cnt] = tifm_7xx1_toggle_sock_power(
-                                       tifm_7xx1_sock_addr(fm->addr, cnt));
+       for (rc = 0; rc < fm->num_sockets; rc++)
+               new_ids[rc] = tifm_7xx1_toggle_sock_power(
+                                       tifm_7xx1_sock_addr(fm->addr, rc));
        spin_lock_irqsave(&fm->lock, flags);
-       fm->socket_change_set = 0;
-       for (cnt = 0; cnt < fm->num_sockets; cnt++) {
-               if (fm->sockets[cnt]) {
-                       if (fm->sockets[cnt]->type == new_ids[cnt])
-                               fm->socket_change_set |= 1 << cnt;
-
-                       fm->sockets[cnt]->type = new_ids[cnt];
+       for (rc = 0; rc < fm->num_sockets; rc++) {
+               if (fm->sockets[rc]) {
+                       if (fm->sockets[rc]->type == new_ids[rc])
+                               good_sockets |= 1 << rc;
+                       else
+                               bad_sockets |= 1 << rc;
                }
        }
 
        writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
               fm->addr + FM_SET_INTERRUPT_ENABLE);
-       if (!fm->socket_change_set) {
-               spin_unlock_irqrestore(&fm->lock, flags);
-               return 0;
-       } else {
-               fm->socket_change_set = 0;
+       dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n",
+               good_sockets, bad_sockets);
+
+       fm->socket_change_set = 0;
+       if (good_sockets) {
                fm->finish_me = &finish_resume;
                spin_unlock_irqrestore(&fm->lock, flags);
+               rc = wait_for_completion_timeout(&finish_resume, HZ);
+               dev_dbg(&dev->dev, "wait returned %d\n", rc);
+               writel(TIFM_IRQ_FIFOMASK(good_sockets)
+                      | TIFM_IRQ_CARDMASK(good_sockets),
+                      fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
+               writel(TIFM_IRQ_FIFOMASK(good_sockets)
+                      | TIFM_IRQ_CARDMASK(good_sockets),
+                      fm->addr + FM_SET_INTERRUPT_ENABLE);
+               spin_lock_irqsave(&fm->lock, flags);
+               fm->finish_me = NULL;
+               fm->socket_change_set ^= good_sockets & fm->socket_change_set;
        }
 
-       wait_for_completion_timeout(&finish_resume, HZ);
+       fm->socket_change_set |= bad_sockets;
+       if (fm->socket_change_set)
+               tifm_queue_work(&fm->media_switcher);
 
-       spin_lock_irqsave(&fm->lock, flags);
-       fm->finish_me = NULL;
-       writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
-              | TIFM_IRQ_CARDMASK(fm->socket_change_set),
-              fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
-       writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
-              | TIFM_IRQ_CARDMASK(fm->socket_change_set),
-              fm->addr + FM_SET_INTERRUPT_ENABLE);
+       spin_unlock_irqrestore(&fm->lock, flags);
        writel(TIFM_IRQ_ENABLE,
               fm->addr + FM_SET_INTERRUPT_ENABLE);
-       fm->socket_change_set = 0;
 
-       spin_unlock_irqrestore(&fm->lock, flags);
        return 0;
 }
 
@@ -349,7 +343,7 @@ static int tifm_7xx1_probe(struct pci_dev *dev,
        if (!fm->addr)
                goto err_out_free;
 
-       rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
+       rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm);
        if (rc)
                goto err_out_unmap;
 
@@ -380,6 +374,7 @@ err_out:
 static void tifm_7xx1_remove(struct pci_dev *dev)
 {
        struct tifm_adapter *fm = pci_get_drvdata(dev);
+       int cnt;
 
        fm->eject = tifm_7xx1_dummy_eject;
        writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
@@ -388,6 +383,9 @@ static void tifm_7xx1_remove(struct pci_dev *dev)
 
        tifm_remove_adapter(fm);
 
+       for (cnt = 0; cnt < fm->num_sockets; cnt++)
+               tifm_7xx1_sock_power_off(tifm_7xx1_sock_addr(fm->addr, cnt));
+
        pci_set_drvdata(dev, NULL);
 
        iounmap(fm->addr);