]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/infiniband/hw/ipath/ipath_diag.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux-2.6-omap-h63xx.git] / drivers / infiniband / hw / ipath / ipath_diag.c
index 29958b6e0214a672905bab6dc61a50e7f8a021d0..63e8368b0e95260011a0ad8733624ce064d54f0a 100644 (file)
@@ -59,7 +59,7 @@ static ssize_t ipath_diag_read(struct file *fp, char __user *data,
 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
                                size_t count, loff_t *off);
 
-static struct file_operations diag_file_ops = {
+static const struct file_operations diag_file_ops = {
        .owner = THIS_MODULE,
        .write = ipath_diag_write,
        .read = ipath_diag_read,
@@ -67,19 +67,54 @@ static struct file_operations diag_file_ops = {
        .release = ipath_diag_release
 };
 
+static ssize_t ipath_diagpkt_write(struct file *fp,
+                                  const char __user *data,
+                                  size_t count, loff_t *off);
+
+static const struct file_operations diagpkt_file_ops = {
+       .owner = THIS_MODULE,
+       .write = ipath_diagpkt_write,
+};
+
+static atomic_t diagpkt_count = ATOMIC_INIT(0);
+static struct cdev *diagpkt_cdev;
+static struct class_device *diagpkt_class_dev;
+
 int ipath_diag_add(struct ipath_devdata *dd)
 {
        char name[16];
+       int ret = 0;
+
+       if (atomic_inc_return(&diagpkt_count) == 1) {
+               ret = ipath_cdev_init(IPATH_DIAGPKT_MINOR,
+                                     "ipath_diagpkt", &diagpkt_file_ops,
+                                     &diagpkt_cdev, &diagpkt_class_dev);
+
+               if (ret) {
+                       ipath_dev_err(dd, "Couldn't create ipath_diagpkt "
+                                     "device: %d", ret);
+                       goto done;
+               }
+       }
 
        snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
 
-       return ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
-                              &diag_file_ops, &dd->diag_cdev,
-                              &dd->diag_class_dev);
+       ret = ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
+                             &diag_file_ops, &dd->diag_cdev,
+                             &dd->diag_class_dev);
+       if (ret)
+               ipath_dev_err(dd, "Couldn't create %s device: %d",
+                             name, ret);
+
+done:
+       return ret;
 }
 
 void ipath_diag_remove(struct ipath_devdata *dd)
 {
+       if (atomic_dec_and_test(&diagpkt_count))
+               ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
+
        ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
 }
 
@@ -261,7 +296,7 @@ static int ipath_diag_open(struct inode *in, struct file *fp)
        }
 
        fp->private_data = dd;
-       ipath_diag_inuse = 1;
+       ipath_diag_inuse = -2;
        diag_set_link = 0;
        ret = 0;
 
@@ -275,30 +310,6 @@ bail:
        return ret;
 }
 
-static ssize_t ipath_diagpkt_write(struct file *fp,
-                                  const char __user *data,
-                                  size_t count, loff_t *off);
-
-static struct file_operations diagpkt_file_ops = {
-       .owner = THIS_MODULE,
-       .write = ipath_diagpkt_write,
-};
-
-static struct cdev *diagpkt_cdev;
-static struct class_device *diagpkt_class_dev;
-
-int __init ipath_diagpkt_add(void)
-{
-       return ipath_cdev_init(IPATH_DIAGPKT_MINOR,
-                              "ipath_diagpkt", &diagpkt_file_ops,
-                              &diagpkt_cdev, &diagpkt_class_dev);
-}
-
-void __exit ipath_diagpkt_remove(void)
-{
-       ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
-}
-
 /**
  * ipath_diagpkt_write - write an IB packet
  * @fp: the diag data device file pointer
@@ -450,6 +461,8 @@ static ssize_t ipath_diag_read(struct file *fp, char __user *data,
        else if ((count % 4) || (*off % 4))
                /* address or length is not 32-bit aligned, hence invalid */
                ret = -EINVAL;
+       else if (ipath_diag_inuse < 1 && (*off || count != 8))
+               ret = -EINVAL;  /* prevent cat /dev/ipath_diag* */
        else if ((count % 8) || (*off % 8))
                /* address or length not 64-bit aligned; do 32-bit reads */
                ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
@@ -459,6 +472,8 @@ static ssize_t ipath_diag_read(struct file *fp, char __user *data,
        if (ret >= 0) {
                *off += count;
                ret = count;
+               if (ipath_diag_inuse == -2)
+                       ipath_diag_inuse++;
        }
 
        return ret;
@@ -478,6 +493,9 @@ static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
        else if ((count % 4) || (*off % 4))
                /* address or length is not 32-bit aligned, hence invalid */
                ret = -EINVAL;
+       else if ((ipath_diag_inuse == -1 && (*off || count != 8)) ||
+                ipath_diag_inuse == -2)  /* read qw off 0, write qw off 0 */
+               ret = -EINVAL;  /* before any other write allowed */
        else if ((count % 8) || (*off % 8))
                /* address or length not 64-bit aligned; do 32-bit writes */
                ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
@@ -487,6 +505,8 @@ static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
        if (ret >= 0) {
                *off += count;
                ret = count;
+               if (ipath_diag_inuse == -1)
+                       ipath_diag_inuse = 1; /* all read/write OK now */
        }
 
        return ret;