llseek: automatically add .llseek fop
[sfrench/cifs-2.6.git] / drivers / firewire / nosy.c
index ea392d0985a5c992a9af3504f0ed974e9ad2e0d5..bf184fb59a5e4329ca11a391cefdc011ea050dfd 100644 (file)
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/fs.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/kernel.h>
+#include <linux/kref.h>
 #include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/pci.h>
 #include <linux/poll.h>
 #include <linux/sched.h> /* required for linux/wait.h */
 #define TCODE_PHY_PACKET               0x10
 #define PCI_DEVICE_ID_TI_PCILYNX       0x8000
 
-#define notify(s, args...) printk(KERN_NOTICE s, ## args)
-#define error(s, args...) printk(KERN_ERR s, ## args)
-#define debug(s, args...) printk(KERN_DEBUG s, ## args)
-
 static char driver_name[] = KBUILD_MODNAME;
 
-struct pcl_status {
-       unsigned int transfer_count : 13;
-       unsigned int reserved0 : 1;
-       unsigned int ack_type : 1;
-       unsigned int ack : 4;
-       unsigned int rcv_speed : 2;
-       unsigned int rcv_dma_channel : 6;
-       unsigned int packet_complete : 1;
-       unsigned int packet_error : 1;
-       unsigned int master_error : 1;
-       unsigned int iso_mode : 1;
-       unsigned int self_id : 1;
-};
-
 /* this is the physical layout of a PCL, its size is 128 bytes */
 struct pcl {
-        u32 next;
-        u32 async_error_next;
-        u32 user_data;
-        struct pcl_status pcl_status;
-        u32 remaining_transfer_count;
-        u32 next_data_buffer;
-        struct {
-                u32 control;
-                u32 pointer;
-        } buffer[13] __attribute__ ((packed));
-} __attribute__ ((packed));
+       __le32 next;
+       __le32 async_error_next;
+       u32 user_data;
+       __le32 pcl_status;
+       __le32 remaining_transfer_count;
+       __le32 next_data_buffer;
+       struct {
+               __le32 control;
+               __le32 pointer;
+       } buffer[13];
+};
 
 struct packet {
        unsigned int length;
@@ -93,10 +78,10 @@ struct packet_buffer {
 
 struct pcilynx {
        struct pci_dev *pci_device;
-       unsigned char *registers;
+       __iomem char *registers;
 
        struct pcl *rcv_start_pcl, *rcv_pcl;
-       u32 *rcv_buffer;
+       __le32 *rcv_buffer;
 
        dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
 
@@ -104,17 +89,39 @@ struct pcilynx {
        struct list_head client_list;
 
        struct miscdevice misc;
+       struct list_head link;
+       struct kref kref;
 };
 
+static inline struct pcilynx *
+lynx_get(struct pcilynx *lynx)
+{
+       kref_get(&lynx->kref);
+
+       return lynx;
+}
+
+static void
+lynx_release(struct kref *kref)
+{
+       kfree(container_of(kref, struct pcilynx, kref));
+}
+
+static inline void
+lynx_put(struct pcilynx *lynx)
+{
+       kref_put(&lynx->kref, lynx_release);
+}
+
 struct client {
        struct pcilynx *lynx;
-       unsigned long tcode_mask;
+       u32 tcode_mask;
        struct packet_buffer buffer;
        struct list_head link;
 };
 
-#define MAX_MINORS 64
-static struct pcilynx *minors[MAX_MINORS];
+static DEFINE_MUTEX(card_mutex);
+static LIST_HEAD(card_list);
 
 static int
 packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
@@ -139,15 +146,20 @@ packet_buffer_destroy(struct packet_buffer *buffer)
 }
 
 static int
-packet_buffer_get(struct packet_buffer *buffer, void *data, size_t user_length)
+packet_buffer_get(struct client *client, char __user *data, size_t user_length)
 {
+       struct packet_buffer *buffer = &client->buffer;
        size_t length;
        char *end;
 
        if (wait_event_interruptible(buffer->wait,
-                                    atomic_read(&buffer->size) > 0))
+                                    atomic_read(&buffer->size) > 0) ||
+                                    list_empty(&client->lynx->link))
                return -ERESTARTSYS;
 
+       if (atomic_read(&buffer->size) == 0)
+               return -ENODEV;
+
        /* FIXME: Check length <= user_length. */
 
        end = buffer->data + buffer->capacity;
@@ -245,87 +257,72 @@ static int
 set_phy_reg(struct pcilynx *lynx, int addr, int val)
 {
        if (addr > 15) {
-               debug("PHY register address %d out of range\n", addr);
+               dev_err(&lynx->pci_device->dev,
+                       "PHY register address %d out of range\n", addr);
                return -1;
        }
-
        if (val > 0xff) {
-               debug("PHY register value %d out of range\n", val);
+               dev_err(&lynx->pci_device->dev,
+                       "PHY register value %d out of range\n", val);
                return -1;
        }
-
        reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
                  LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
 
        return 0;
 }
 
-static void
-nosy_start_snoop(struct client *client)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&client->lynx->client_list_lock, flags);
-       list_add_tail(&client->link, &client->lynx->client_list);
-       spin_unlock_irqrestore(&client->lynx->client_list_lock, flags);
-}
-
-static void
-nosy_stop_snoop(struct client *client)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&client->lynx->client_list_lock, flags);
-       list_del(&client->link);
-       spin_unlock_irqrestore(&client->lynx->client_list_lock, flags);
-}
-
-static struct client *
-nosy_add_client(struct pcilynx *lynx)
+static int
+nosy_open(struct inode *inode, struct file *file)
 {
+       int minor = iminor(inode);
        struct client *client;
+       struct pcilynx *tmp, *lynx = NULL;
+
+       mutex_lock(&card_mutex);
+       list_for_each_entry(tmp, &card_list, link)
+               if (tmp->misc.minor == minor) {
+                       lynx = lynx_get(tmp);
+                       break;
+               }
+       mutex_unlock(&card_mutex);
+       if (lynx == NULL)
+               return -ENODEV;
 
        client = kmalloc(sizeof *client, GFP_KERNEL);
+       if (client == NULL)
+               goto fail;
+
        client->tcode_mask = ~0;
        client->lynx = lynx;
        INIT_LIST_HEAD(&client->link);
 
-       if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) {
-               kfree(client);
-               debug("Failed to allocate packet buffer\n");
-               return NULL;
-       }
+       if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
+               goto fail;
 
-       return client;
-}
+       file->private_data = client;
 
-static void
-nosy_remove_client(struct client *client)
-{
-       nosy_stop_snoop(client);
-       packet_buffer_destroy(&client->buffer);
+       return 0;
+fail:
        kfree(client);
-}
+       lynx_put(lynx);
 
-static int
-nosy_open(struct inode *inode, struct file *file)
-{
-       int minor = iminor(inode);
-
-       if (minor > MAX_MINORS || minors[minor] == NULL)
-               return -ENODEV;
-
-       file->private_data = nosy_add_client(minors[minor]);
-       if (file->private_data == NULL)
-               return -ENOMEM;
-       else
-               return 0;
+       return -ENOMEM;
 }
 
 static int
 nosy_release(struct inode *inode, struct file *file)
 {
-       nosy_remove_client(file->private_data);
+       struct client *client = file->private_data;
+       struct pcilynx *lynx = client->lynx;
+
+       spin_lock_irq(&lynx->client_list_lock);
+       list_del_init(&client->link);
+       spin_unlock_irq(&lynx->client_list_lock);
+
+       packet_buffer_destroy(&client->buffer);
+       kfree(client);
+       lynx_put(lynx);
 
        return 0;
 }
@@ -334,49 +331,65 @@ static unsigned int
 nosy_poll(struct file *file, poll_table *pt)
 {
        struct client *client = file->private_data;
+       unsigned int ret = 0;
 
        poll_wait(file, &client->buffer.wait, pt);
 
        if (atomic_read(&client->buffer.size) > 0)
-               return POLLIN | POLLRDNORM;
-       else
-               return 0;
+               ret = POLLIN | POLLRDNORM;
+
+       if (list_empty(&client->lynx->link))
+               ret |= POLLHUP;
+
+       return ret;
 }
 
 static ssize_t
-nosy_read(struct file *file, char *buffer, size_t count, loff_t *offset)
+nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
 {
        struct client *client = file->private_data;
 
-       return packet_buffer_get(&client->buffer, buffer, count);
+       return packet_buffer_get(client, buffer, count);
 }
 
-static int
-nosy_ioctl(struct inode *inode, struct file *file,
-          unsigned int cmd, unsigned long arg)
+static long
+nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct client *client = file->private_data;
+       spinlock_t *client_list_lock = &client->lynx->client_list_lock;
        struct nosy_stats stats;
 
        switch (cmd) {
        case NOSY_IOC_GET_STATS:
+               spin_lock_irq(client_list_lock);
                stats.total_packet_count = client->buffer.total_packet_count;
-               stats.lost_packet_count = client->buffer.lost_packet_count;
-               if (copy_to_user((void *) arg, &stats, sizeof stats))
+               stats.lost_packet_count  = client->buffer.lost_packet_count;
+               spin_unlock_irq(client_list_lock);
+
+               if (copy_to_user((void __user *) arg, &stats, sizeof stats))
                        return -EFAULT;
                else
                        return 0;
 
        case NOSY_IOC_START:
-               nosy_start_snoop(client);
+               spin_lock_irq(client_list_lock);
+               list_add_tail(&client->link, &client->lynx->client_list);
+               spin_unlock_irq(client_list_lock);
+
                return 0;
 
        case NOSY_IOC_STOP:
-               nosy_stop_snoop(client);
+               spin_lock_irq(client_list_lock);
+               list_del_init(&client->link);
+               spin_unlock_irq(client_list_lock);
+
                return 0;
 
        case NOSY_IOC_FILTER:
+               spin_lock_irq(client_list_lock);
                client->tcode_mask = arg;
+               spin_unlock_irq(client_list_lock);
+
                return 0;
 
        default:
@@ -386,72 +399,62 @@ nosy_ioctl(struct inode *inode, struct file *file,
 }
 
 static const struct file_operations nosy_ops = {
-       .owner =        THIS_MODULE,
-       .read =         nosy_read,
-       .ioctl =        nosy_ioctl,
-       .poll =         nosy_poll,
-       .open =         nosy_open,
-       .release =      nosy_release,
+       .owner =                THIS_MODULE,
+       .read =                 nosy_read,
+       .unlocked_ioctl =       nosy_ioctl,
+       .poll =                 nosy_poll,
+       .open =                 nosy_open,
+       .release =              nosy_release,
+       .llseek =               noop_llseek,
 };
 
 #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
 
-struct link_packet {
-       unsigned int priority : 4;
-       unsigned int tcode : 4;
-       unsigned int rt : 2;
-       unsigned int tlabel : 6;
-       unsigned int destination : 16;
-};
-
 static void
-packet_handler(struct pcilynx *lynx)
+packet_irq_handler(struct pcilynx *lynx)
 {
-       unsigned long flags;
        struct client *client;
-       unsigned long tcode_mask;
+       u32 tcode_mask, tcode;
        size_t length;
-       struct link_packet *packet;
        struct timeval tv;
 
        /* FIXME: Also report rcv_speed. */
 
-       length = lynx->rcv_pcl->pcl_status.transfer_count;
-       packet = (struct link_packet *) &lynx->rcv_buffer[1];
+       length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
+       tcode  = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
 
        do_gettimeofday(&tv);
-       lynx->rcv_buffer[0] = tv.tv_usec;
+       lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec;
 
        if (length == PHY_PACKET_SIZE)
                tcode_mask = 1 << TCODE_PHY_PACKET;
        else
-               tcode_mask = 1 << packet->tcode;
+               tcode_mask = 1 << tcode;
 
-       spin_lock_irqsave(&lynx->client_list_lock, flags);
+       spin_lock(&lynx->client_list_lock);
 
        list_for_each_entry(client, &lynx->client_list, link)
                if (client->tcode_mask & tcode_mask)
                        packet_buffer_put(&client->buffer,
                                          lynx->rcv_buffer, length + 4);
 
-       spin_unlock_irqrestore(&lynx->client_list_lock, flags);
+       spin_unlock(&lynx->client_list_lock);
 }
 
 static void
-bus_reset_handler(struct pcilynx *lynx)
+bus_reset_irq_handler(struct pcilynx *lynx)
 {
-       unsigned long flags;
        struct client *client;
        struct timeval tv;
 
        do_gettimeofday(&tv);
 
-       spin_lock_irqsave(&lynx->client_list_lock, flags);
+       spin_lock(&lynx->client_list_lock);
 
        list_for_each_entry(client, &lynx->client_list, link)
                packet_buffer_put(&client->buffer, &tv.tv_usec, 4);
 
-       spin_unlock_irqrestore(&lynx->client_list_lock, flags);
+       spin_unlock(&lynx->client_list_lock);
 }
 
 static irqreturn_t
@@ -462,6 +465,10 @@ irq_handler(int irq, void *device)
 
        pci_int_status = reg_read(lynx, PCI_INT_STATUS);
 
+       if (pci_int_status == ~0)
+               /* Card was ejected. */
+               return IRQ_NONE;
+
        if ((pci_int_status & PCI_INT_INT_PEND) == 0)
                /* Not our interrupt, bail out quickly. */
                return IRQ_NONE;
@@ -473,7 +480,7 @@ irq_handler(int irq, void *device)
                reg_write(lynx, LINK_INT_STATUS, link_int_status);
 
                if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
-                       bus_reset_handler(lynx);
+                       bus_reset_irq_handler(lynx);
        }
 
        /* Clear the PCI_INT_STATUS register only after clearing the
@@ -483,7 +490,7 @@ irq_handler(int irq, void *device)
        reg_write(lynx, PCI_INT_STATUS, pci_int_status);
 
        if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
-               packet_handler(lynx);
+               packet_irq_handler(lynx);
                run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
        }
 
@@ -493,16 +500,22 @@ irq_handler(int irq, void *device)
 static void
 remove_card(struct pci_dev *dev)
 {
-       struct pcilynx *lynx;
+       struct pcilynx *lynx = pci_get_drvdata(dev);
+       struct client *client;
 
-       lynx = pci_get_drvdata(dev);
-       if (!lynx)
-               return;
-       pci_set_drvdata(dev, NULL);
+       mutex_lock(&card_mutex);
+       list_del_init(&lynx->link);
+       misc_deregister(&lynx->misc);
+       mutex_unlock(&card_mutex);
 
        reg_write(lynx, PCI_INT_ENABLE, 0);
        free_irq(lynx->pci_device->irq, lynx);
 
+       spin_lock_irq(&lynx->client_list_lock);
+       list_for_each_entry(client, &lynx->client_list, link)
+               wake_up_interruptible(&client->buffer.wait);
+       spin_unlock_irq(&lynx->client_list_lock);
+
        pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
                            lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
        pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
@@ -511,11 +524,8 @@ remove_card(struct pci_dev *dev)
                            lynx->rcv_buffer, lynx->rcv_buffer_bus);
 
        iounmap(lynx->registers);
-
-       minors[lynx->misc.minor] = NULL;
-       misc_deregister(&lynx->misc);
-
-       kfree(lynx);
+       pci_disable_device(dev);
+       lynx_put(lynx);
 }
 
 #define RCV_BUFFER_SIZE (16 * 1024)
@@ -525,29 +535,31 @@ add_card(struct pci_dev *dev, const struct pci_device_id *unused)
 {
        struct pcilynx *lynx;
        u32 p, end;
-       int i;
+       int ret, i;
 
        if (pci_set_dma_mask(dev, 0xffffffff)) {
-               error("DMA address limits not supported "
-                     "for PCILynx hardware\n");
+               dev_err(&dev->dev,
+                   "DMA address limits not supported for PCILynx hardware\n");
                return -ENXIO;
        }
        if (pci_enable_device(dev)) {
-               error("Failed to enable PCILynx hardware\n");
+               dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
                return -ENXIO;
        }
        pci_set_master(dev);
 
        lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
        if (lynx == NULL) {
-               error("Failed to allocate control structure memory\n");
-               return -ENOMEM;
+               dev_err(&dev->dev, "Failed to allocate control structure\n");
+               ret = -ENOMEM;
+               goto fail_disable;
        }
        lynx->pci_device = dev;
        pci_set_drvdata(dev, lynx);
 
        spin_lock_init(&lynx->client_list_lock);
        INIT_LIST_HEAD(&lynx->client_list);
+       kref_init(&lynx->kref);
 
        lynx->registers = ioremap_nocache(pci_resource_start(dev, 0),
                                          PCILYNX_MAX_REGISTER);
@@ -561,25 +573,26 @@ add_card(struct pci_dev *dev, const struct pci_device_id *unused)
        if (lynx->rcv_start_pcl == NULL ||
            lynx->rcv_pcl == NULL ||
            lynx->rcv_buffer == NULL) {
-               /* FIXME: do proper error handling. */
-               error("Failed to allocate receive buffer\n");
-               return -ENOMEM;
+               dev_err(&dev->dev, "Failed to allocate receive buffer\n");
+               ret = -ENOMEM;
+               goto fail_deallocate;
        }
-       lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus;
-       lynx->rcv_pcl->next = PCL_NEXT_INVALID;
-       lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID;
+       lynx->rcv_start_pcl->next       = cpu_to_le32(lynx->rcv_pcl_bus);
+       lynx->rcv_pcl->next             = cpu_to_le32(PCL_NEXT_INVALID);
+       lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
 
        lynx->rcv_pcl->buffer[0].control =
-               PCL_CMD_RCV | PCL_BIGENDIAN | 2044;
-       lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4;
+                       cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
+       lynx->rcv_pcl->buffer[0].pointer =
+                       cpu_to_le32(lynx->rcv_buffer_bus + 4);
        p = lynx->rcv_buffer_bus + 2048;
        end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
        for (i = 1; p < end; i++, p += 2048) {
                lynx->rcv_pcl->buffer[i].control =
-                       PCL_CMD_RCV | PCL_BIGENDIAN | 2048;
-               lynx->rcv_pcl->buffer[i].pointer = p;
+                       cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
+               lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
        }
-       lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF;
+       lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
 
        reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
        /* Fix buggy cards with autoboot pin not tied low: */
@@ -622,23 +635,53 @@ add_card(struct pci_dev *dev, const struct pci_device_id *unused)
 
        if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
                        driver_name, lynx)) {
-               error("Failed to allocate shared interrupt %d\n", dev->irq);
-               return -EIO;
+               dev_err(&dev->dev,
+                       "Failed to allocate shared interrupt %d\n", dev->irq);
+               ret = -EIO;
+               goto fail_deallocate;
        }
 
        lynx->misc.parent = &dev->dev;
        lynx->misc.minor = MISC_DYNAMIC_MINOR;
        lynx->misc.name = "nosy";
        lynx->misc.fops = &nosy_ops;
-       if (misc_register(&lynx->misc)) {
-               error("Failed to register misc char device\n");
-               return -ENOMEM;
+
+       mutex_lock(&card_mutex);
+       ret = misc_register(&lynx->misc);
+       if (ret) {
+               dev_err(&dev->dev, "Failed to register misc char device\n");
+               mutex_unlock(&card_mutex);
+               goto fail_free_irq;
        }
-       minors[lynx->misc.minor] = lynx;
+       list_add_tail(&lynx->link, &card_list);
+       mutex_unlock(&card_mutex);
 
-       notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
+       dev_info(&dev->dev,
+                "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
 
        return 0;
+
+fail_free_irq:
+       reg_write(lynx, PCI_INT_ENABLE, 0);
+       free_irq(lynx->pci_device->irq, lynx);
+
+fail_deallocate:
+       if (lynx->rcv_start_pcl)
+               pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
+                               lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
+       if (lynx->rcv_pcl)
+               pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
+                               lynx->rcv_pcl, lynx->rcv_pcl_bus);
+       if (lynx->rcv_buffer)
+               pci_free_consistent(lynx->pci_device, PAGE_SIZE,
+                               lynx->rcv_buffer, lynx->rcv_buffer_bus);
+       iounmap(lynx->registers);
+       kfree(lynx);
+
+fail_disable:
+       pci_disable_device(dev);
+
+       return ret;
 }
 
 static struct pci_device_id pci_table[] __devinitdata = {
@@ -672,7 +715,7 @@ static void __exit nosy_cleanup(void)
 {
        pci_unregister_driver(&lynx_pci_driver);
 
-       notify("Unloaded %s.\n", driver_name);
+       pr_info("Unloaded %s\n", driver_name);
 }
 
 module_init(nosy_init);