llseek: automatically add .llseek fop
[sfrench/cifs-2.6.git] / drivers / firewire / nosy.c
index ccf9c461bd8636bcdfe3d4e4d1773b36a9ebe111..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,8 +89,30 @@ 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;
        u32 tcode_mask;
@@ -113,8 +120,8 @@ struct client {
        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,15 +257,15 @@ 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));
 
@@ -265,39 +277,52 @@ nosy_open(struct inode *inode, struct file *file)
 {
        int minor = iminor(inode);
        struct client *client;
-
-       if (minor > MAX_MINORS || minors[minor] == NULL)
+       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)
-               return -ENOMEM;
+               goto fail;
 
        client->tcode_mask = ~0;
-       client->lynx = minors[minor];
+       client->lynx = lynx;
        INIT_LIST_HEAD(&client->link);
 
-       if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) {
-               kfree(client);
-               return -ENOMEM;
-       }
+       if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
+               goto fail;
 
        file->private_data = client;
 
        return 0;
+fail:
+       kfree(client);
+       lynx_put(lynx);
+
+       return -ENOMEM;
 }
 
 static int
 nosy_release(struct inode *inode, struct file *file)
 {
        struct client *client = file->private_data;
+       struct pcilynx *lynx = client->lynx;
 
-       spin_lock_irq(&client->lynx->client_list_lock);
+       spin_lock_irq(&lynx->client_list_lock);
        list_del_init(&client->link);
-       spin_unlock_irq(&client->lynx->client_list_lock);
+       spin_unlock_irq(&lynx->client_list_lock);
 
        packet_buffer_destroy(&client->buffer);
        kfree(client);
+       lynx_put(lynx);
 
        return 0;
 }
@@ -306,21 +331,25 @@ 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 long
@@ -337,7 +366,7 @@ nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                stats.lost_packet_count  = client->buffer.lost_packet_count;
                spin_unlock_irq(client_list_lock);
 
-               if (copy_to_user((void *) arg, &stats, sizeof stats))
+               if (copy_to_user((void __user *) arg, &stats, sizeof stats))
                        return -EFAULT;
                else
                        return 0;
@@ -376,39 +405,31 @@ static const struct file_operations nosy_ops = {
        .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_irq_handler(struct pcilynx *lynx)
 {
        struct client *client;
-       u32 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(&lynx->client_list_lock);
 
@@ -479,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),
@@ -498,11 +525,7 @@ remove_card(struct pci_dev *dev)
 
        iounmap(lynx->registers);
        pci_disable_device(dev);
-
-       minors[lynx->misc.minor] = NULL;
-       misc_deregister(&lynx->misc);
-
-       kfree(lynx);
+       lynx_put(lynx);
 }
 
 #define RCV_BUFFER_SIZE (16 * 1024)
@@ -515,19 +538,19 @@ add_card(struct pci_dev *dev, const struct pci_device_id *unused)
        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");
+               dev_err(&dev->dev, "Failed to allocate control structure\n");
                ret = -ENOMEM;
                goto fail_disable;
        }
@@ -536,6 +559,7 @@ add_card(struct pci_dev *dev, const struct pci_device_id *unused)
 
        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);
@@ -549,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) {
-               error("Failed to allocate receive buffer\n");
+               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: */
@@ -610,7 +635,8 @@ 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);
+               dev_err(&dev->dev,
+                       "Failed to allocate shared interrupt %d\n", dev->irq);
                ret = -EIO;
                goto fail_deallocate;
        }
@@ -619,14 +645,19 @@ add_card(struct pci_dev *dev, const struct pci_device_id *unused)
        lynx->misc.minor = MISC_DYNAMIC_MINOR;
        lynx->misc.name = "nosy";
        lynx->misc.fops = &nosy_ops;
+
+       mutex_lock(&card_mutex);
        ret = misc_register(&lynx->misc);
        if (ret) {
-               error("Failed to register misc char device\n");
+               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;
 
@@ -684,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);