Add ioctls to enable and disable local controls on an instrument
[deliverable/linux.git] / drivers / usb / class / usbtmc.c
index c2eb6fe9f4c85599015f608e1668e33b24793abc..419c72e1046469d8767df8debe663f029fd7b9af 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/uaccess.h>
 #include <linux/kref.h>
 #include <linux/slab.h>
+#include <linux/poll.h>
 #include <linux/mutex.h>
 #include <linux/usb.h>
 #include <linux/usb/tmc.h>
@@ -99,6 +100,10 @@ struct usbtmc_device_data {
        int            iin_interval;
        struct urb    *iin_urb;
        u16            iin_wMaxPacketSize;
+       atomic_t       srq_asserted;
+
+       /* coalesced usb488_caps from usbtmc_dev_capabilities */
+       __u8 usb488_caps;
 
        u8 rigol_quirk;
 
@@ -113,6 +118,7 @@ struct usbtmc_device_data {
        struct kref kref;
        struct mutex io_mutex;  /* only one i/o function running at a time */
        wait_queue_head_t waitq;
+       struct fasync_struct *fasync;
 };
 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
 
@@ -405,6 +411,9 @@ static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
 
        atomic_set(&data->iin_data_valid, 0);
 
+       /* must issue read_stb before using poll or select */
+       atomic_set(&data->srq_asserted, 0);
+
        rv = usb_control_msg(data->usb_dev,
                        usb_rcvctrlpipe(data->usb_dev, 0),
                        USBTMC488_REQUEST_READ_STATUS_BYTE,
@@ -465,6 +474,61 @@ static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
        return rv;
 }
 
+static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data,
+                               void __user *arg, unsigned int cmd)
+{
+       struct device *dev = &data->intf->dev;
+       __u8 val;
+       u8 *buffer;
+       u16 wValue;
+       int rv;
+
+       if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE))
+               return -EINVAL;
+
+       buffer = kmalloc(8, GFP_KERNEL);
+       if (!buffer)
+               return -ENOMEM;
+
+       if (cmd == USBTMC488_REQUEST_REN_CONTROL) {
+               rv = copy_from_user(&val, arg, sizeof(val));
+               if (rv) {
+                       rv = -EFAULT;
+                       goto exit;
+               }
+               wValue = val ? 1 : 0;
+       } else {
+               wValue = 0;
+       }
+
+       rv = usb_control_msg(data->usb_dev,
+                       usb_rcvctrlpipe(data->usb_dev, 0),
+                       cmd,
+                       USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+                       wValue,
+                       data->ifnum,
+                       buffer, 0x01, USBTMC_TIMEOUT);
+       if (rv < 0) {
+               dev_err(dev, "simple usb_control_msg failed %d\n", rv);
+               goto exit;
+       } else if (rv != 1) {
+               dev_warn(dev, "simple usb_control_msg returned %d\n", rv);
+               rv = -EIO;
+               goto exit;
+       }
+
+       if (buffer[0] != USBTMC_STATUS_SUCCESS) {
+               dev_err(dev, "simple control status returned %x\n", buffer[0]);
+               rv = -EIO;
+               goto exit;
+       }
+       rv = 0;
+
+ exit:
+       kfree(buffer);
+       return rv;
+}
+
 /*
  * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
  * @transfer_size: number of bytes to request from the device.
@@ -987,6 +1051,7 @@ static int get_capabilities(struct usbtmc_device_data *data)
        data->capabilities.device_capabilities = buffer[5];
        data->capabilities.usb488_interface_capabilities = buffer[14];
        data->capabilities.usb488_device_capabilities = buffer[15];
+       data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4);
        rv = 0;
 
 err_out:
@@ -1162,9 +1227,32 @@ static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                retval = usbtmc_ioctl_abort_bulk_in(data);
                break;
 
+       case USBTMC488_IOCTL_GET_CAPS:
+               retval = copy_to_user((void __user *)arg,
+                               &data->usb488_caps,
+                               sizeof(data->usb488_caps));
+               if (retval)
+                       retval = -EFAULT;
+               break;
+
        case USBTMC488_IOCTL_READ_STB:
                retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg);
                break;
+
+       case USBTMC488_IOCTL_REN_CONTROL:
+               retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
+                                               USBTMC488_REQUEST_REN_CONTROL);
+               break;
+
+       case USBTMC488_IOCTL_GOTO_LOCAL:
+               retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
+                                               USBTMC488_REQUEST_GOTO_LOCAL);
+               break;
+
+       case USBTMC488_IOCTL_LOCAL_LOCKOUT:
+               retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
+                                               USBTMC488_REQUEST_LOCAL_LOCKOUT);
+               break;
        }
 
 skip_io_on_zombie:
@@ -1172,6 +1260,34 @@ skip_io_on_zombie:
        return retval;
 }
 
+static int usbtmc_fasync(int fd, struct file *file, int on)
+{
+       struct usbtmc_device_data *data = file->private_data;
+
+       return fasync_helper(fd, file, on, &data->fasync);
+}
+
+static unsigned int usbtmc_poll(struct file *file, poll_table *wait)
+{
+       struct usbtmc_device_data *data = file->private_data;
+       unsigned int mask;
+
+       mutex_lock(&data->io_mutex);
+
+       if (data->zombie) {
+               mask = POLLHUP | POLLERR;
+               goto no_poll;
+       }
+
+       poll_wait(file, &data->waitq, wait);
+
+       mask = (atomic_read(&data->srq_asserted)) ? POLLIN | POLLRDNORM : 0;
+
+no_poll:
+       mutex_unlock(&data->io_mutex);
+       return mask;
+}
+
 static const struct file_operations fops = {
        .owner          = THIS_MODULE,
        .read           = usbtmc_read,
@@ -1179,6 +1295,8 @@ static const struct file_operations fops = {
        .open           = usbtmc_open,
        .release        = usbtmc_release,
        .unlocked_ioctl = usbtmc_ioctl,
+       .fasync         = usbtmc_fasync,
+       .poll           = usbtmc_poll,
        .llseek         = default_llseek,
 };
 
@@ -1208,6 +1326,16 @@ static void usbtmc_interrupt(struct urb *urb)
                        wake_up_interruptible(&data->waitq);
                        goto exit;
                }
+               /* check for SRQ notification */
+               if (data->iin_buffer[0] == 0x81) {
+                       if (data->fasync)
+                               kill_fasync(&data->fasync,
+                                       SIGIO, POLL_IN);
+
+                       atomic_set(&data->srq_asserted, 1);
+                       wake_up_interruptible(&data->waitq);
+                       goto exit;
+               }
                dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]);
                break;
        case -EOVERFLOW:
@@ -1263,6 +1391,7 @@ static int usbtmc_probe(struct usb_interface *intf,
        mutex_init(&data->io_mutex);
        init_waitqueue_head(&data->waitq);
        atomic_set(&data->iin_data_valid, 0);
+       atomic_set(&data->srq_asserted, 0);
        data->zombie = 0;
 
        /* Determine if it is a Rigol or not */
This page took 0.027468 seconds and 5 git commands to generate.