Add ioctls to enable and disable local controls on an instrument
[deliverable/linux.git] / drivers / usb / class / usbtmc.c
index 867842951ea058ff43f2e8a4e0812402905847da..419c72e1046469d8767df8debe663f029fd7b9af 100644 (file)
@@ -102,6 +102,9 @@ struct usbtmc_device_data {
        u16            iin_wMaxPacketSize;
        atomic_t       srq_asserted;
 
+       /* coalesced usb488_caps from usbtmc_dev_capabilities */
+       __u8 usb488_caps;
+
        u8 rigol_quirk;
 
        /* attributes from the USB TMC spec for this device */
@@ -471,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.
@@ -993,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:
@@ -1168,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:
This page took 0.029765 seconds and 5 git commands to generate.