Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2008 Novell, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Copyright (C) 2018 IVI Foundation, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/usb/tmc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) /* Increment API VERSION when changing tmc.h with new flags or ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * or when changing a significant behavior of the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define USBTMC_API_VERSION (2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define USBTMC_HEADER_SIZE	12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define USBTMC_MINOR_BASE	176
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /* Minimum USB timeout (in milliseconds) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define USBTMC_MIN_TIMEOUT	100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) /* Default USB timeout (in milliseconds) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define USBTMC_TIMEOUT		5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) /* Max number of urbs used in write transfers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define MAX_URBS_IN_FLIGHT	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /* I/O buffer size used in generic read/write functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #define USBTMC_BUFSIZE		(4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * packet is never read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN	100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) static const struct usb_device_id usbtmc_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	{ 0, } /* terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) MODULE_DEVICE_TABLE(usb, usbtmc_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * This structure is the capabilities for the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * See section 4.2.1.8 of the USBTMC specification,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * and section 4.2.2 of the USBTMC usb488 subclass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * specification for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) struct usbtmc_dev_capabilities {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	__u8 interface_capabilities;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	__u8 device_capabilities;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	__u8 usb488_interface_capabilities;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	__u8 usb488_device_capabilities;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) /* This structure holds private data for each USBTMC device. One copy is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * allocated for each USBTMC device in the driver's probe function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) struct usbtmc_device_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	const struct usb_device_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	struct usb_device *usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	struct usb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	struct list_head file_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	unsigned int bulk_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	unsigned int bulk_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	u8 bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	u8 bTag_last_write;	/* needed for abort */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	u8 bTag_last_read;	/* needed for abort */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	/* packet size of IN bulk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	u16            wMaxPacketSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	/* data for interrupt in endpoint handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	u8             bNotify1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	u8             bNotify2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	u16            ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	u8             iin_bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	u8            *iin_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	atomic_t       iin_data_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	unsigned int   iin_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	int            iin_ep_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	int            iin_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct urb    *iin_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	u16            iin_wMaxPacketSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	/* coalesced usb488_caps from usbtmc_dev_capabilities */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	__u8 usb488_caps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	bool zombie; /* fd of disconnected device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct usbtmc_dev_capabilities	capabilities;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	struct kref kref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct mutex io_mutex;	/* only one i/o function running at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	wait_queue_head_t waitq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	struct fasync_struct *fasync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	spinlock_t dev_lock; /* lock for file_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * This structure holds private data for each USBTMC file handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) struct usbtmc_file_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct usbtmc_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	struct list_head file_elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	u32            timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	u8             srq_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	atomic_t       srq_asserted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	atomic_t       closing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	u8             bmTransferAttributes; /* member of DEV_DEP_MSG_IN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	u8             eom_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	u8             term_char;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	bool           term_char_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	bool           auto_abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	spinlock_t     err_lock; /* lock for errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct usb_anchor submitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	/* data for generic_write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct semaphore limit_write_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	u32 out_transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	int out_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	/* data for generic_read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	u32 in_transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	int in_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	int in_urbs_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	struct usb_anchor in_anchor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	wait_queue_head_t wait_bulk_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) /* Forward declarations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static struct usb_driver usbtmc_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) static void usbtmc_draw_down(struct usbtmc_file_data *file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) static void usbtmc_delete(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	struct usbtmc_device_data *data = to_usbtmc_data(kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	usb_put_dev(data->usb_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) static int usbtmc_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	struct usb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	struct usbtmc_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	intf = usb_find_interface(&usbtmc_driver, iminor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	if (!intf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		pr_err("can not find device for minor %d", iminor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	if (!file_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	spin_lock_init(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	sema_init(&file_data->limit_write_sem, MAX_URBS_IN_FLIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	init_usb_anchor(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	init_usb_anchor(&file_data->in_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	init_waitqueue_head(&file_data->wait_bulk_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	data = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	/* Protect reference to data from file structure until release */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	kref_get(&data->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	file_data->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	atomic_set(&file_data->closing, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	file_data->timeout = USBTMC_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	file_data->term_char = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	file_data->term_char_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	file_data->auto_abort = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	file_data->eom_val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	INIT_LIST_HEAD(&file_data->file_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	spin_lock_irq(&data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	list_add_tail(&file_data->file_elem, &data->file_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	spin_unlock_irq(&data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	/* Store pointer in file structure's private data field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	filp->private_data = file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * usbtmc_flush - called before file handle is closed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static int usbtmc_flush(struct file *file, fl_owner_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct usbtmc_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	file_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	if (file_data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	atomic_set(&file_data->closing, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	/* wait for io to stop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	usbtmc_draw_down(file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	file_data->in_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	file_data->in_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	file_data->in_urbs_used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	file_data->out_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	file_data->out_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	wake_up_interruptible_all(&data->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) static int usbtmc_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct usbtmc_file_data *file_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	/* prevent IO _AND_ usbtmc_interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	mutex_lock(&file_data->data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	spin_lock_irq(&file_data->data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	list_del(&file_data->file_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	spin_unlock_irq(&file_data->data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	mutex_unlock(&file_data->data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	kref_put(&file_data->data->kref, usbtmc_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	file_data->data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	kfree(file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) static int usbtmc_ioctl_abort_bulk_in_tag(struct usbtmc_device_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 					  u8 tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	buffer = kmalloc(USBTMC_BUFSIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			     usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			     USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			     tag, data->bulk_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 			     buffer, 2, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x with tag %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		buffer[0], buffer[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	if (buffer[0] == USBTMC_STATUS_FAILED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		/* No transfer in progress and the Bulk-OUT FIFO is empty. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	if (buffer[0] == USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		/* The device returns this status if either:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		 * - There is a transfer in progress, but the specified bTag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		 *   does not match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		 * - There is no transfer in progress, but the Bulk-OUT FIFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		 *   is not empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		rv = -ENOMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 			buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) usbtmc_abort_bulk_in_status:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	dev_dbg(dev, "Reading from bulk in EP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	/* Data must be present. So use low timeout 300 ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	actual = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	rv = usb_bulk_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			  usb_rcvbulkpipe(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 					  data->bulk_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			  buffer, USBTMC_BUFSIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 			  &actual, 300);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 			     buffer, actual, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		dev_err(dev, "usb_bulk_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		if (rv != -ETIMEDOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	if (actual == USBTMC_BUFSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		goto usbtmc_abort_bulk_in_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (n >= USBTMC_MAX_READS_TO_CLEAR_BULK_IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 			     usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			     USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			     0, data->bulk_in, buffer, 0x08,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			     USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	dev_dbg(dev, "CHECK_ABORT_BULK_IN returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	if (buffer[0] == USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	if (buffer[0] != USBTMC_STATUS_PENDING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		dev_err(dev, "CHECK_ABORT_BULK_IN returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	if ((buffer[1] & 1) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		/* The device has 1 or more queued packets the Host can read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		goto usbtmc_abort_bulk_in_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	/* The Host must send CHECK_ABORT_BULK_IN_STATUS at a later time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	rv = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	return usbtmc_ioctl_abort_bulk_in_tag(data, data->bTag_last_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) static int usbtmc_ioctl_abort_bulk_out_tag(struct usbtmc_device_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 					   u8 tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	buffer = kmalloc(8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			     usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			     USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			     tag, data->bulk_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 			     buffer, 2, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 			buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) usbtmc_abort_bulk_out_check_status:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	/* do not stress device with subsequent requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	msleep(50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			     usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			     USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			     0, data->bulk_out, buffer, 0x08,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			     USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (buffer[0] == USBTMC_STATUS_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		goto usbtmc_abort_bulk_out_clear_halt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if ((buffer[0] == USBTMC_STATUS_PENDING) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	    (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		goto usbtmc_abort_bulk_out_check_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) usbtmc_abort_bulk_out_clear_halt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	rv = usb_clear_halt(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	return usbtmc_ioctl_abort_bulk_out_tag(data, data->bTag_last_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) static int usbtmc488_ioctl_read_stb(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	struct usbtmc_device_data *data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	struct device *dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	int srq_asserted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	u8 tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	__u8 stb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		data->iin_ep_present);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	spin_lock_irq(&data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	srq_asserted = atomic_xchg(&file_data->srq_asserted, srq_asserted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	if (srq_asserted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		/* a STB with SRQ is already received */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		stb = file_data->srq_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		spin_unlock_irq(&data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		rv = put_user(stb, (__u8 __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		dev_dbg(dev, "stb:0x%02x with srq received %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			(unsigned int)stb, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	spin_unlock_irq(&data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	buffer = kmalloc(8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	atomic_set(&data->iin_data_valid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 			usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			USBTMC488_REQUEST_READ_STATUS_BYTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			data->iin_bTag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 			data->ifnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			buffer, 0x03, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		dev_err(dev, "stb usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		dev_err(dev, "control status returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	if (data->iin_ep_present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		rv = wait_event_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 			data->waitq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			atomic_read(&data->iin_data_valid) != 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 			file_data->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 			dev_dbg(dev, "wait interrupted %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		if (rv == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 			dev_dbg(dev, "wait timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			rv = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		tag = data->bNotify1 & 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		if (tag != data->iin_bTag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			dev_err(dev, "expected bTag %x got %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 				data->iin_bTag, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		stb = data->bNotify2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		stb = buffer[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	rv = put_user(stb, (__u8 __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	dev_dbg(dev, "stb:0x%02x received %d\n", (unsigned int)stb, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559)  exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	/* bump interrupt bTag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	data->iin_bTag += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if (data->iin_bTag > 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		/* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		data->iin_bTag = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static int usbtmc488_ioctl_wait_srq(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 				    __u32 __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	struct usbtmc_device_data *data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	struct device *dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	u32 timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	unsigned long expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	if (!data->iin_ep_present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		dev_dbg(dev, "no interrupt endpoint present\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	if (get_user(timeout, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	expire = msecs_to_jiffies(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	rv = wait_event_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 			data->waitq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			atomic_read(&file_data->srq_asserted) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 			atomic_read(&file_data->closing),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			expire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	/* Note! disconnect or close could be called in the meantime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	if (atomic_read(&file_data->closing) || data->zombie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		rv = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		/* dev can be invalid now! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		pr_debug("%s - wait interrupted %d\n", __func__, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (rv == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		dev_dbg(dev, "%s - wait timed out\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	dev_dbg(dev, "%s - srq asserted\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 				void __user *arg, unsigned int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	struct device *dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	__u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	u16 wValue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	buffer = kmalloc(8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (cmd == USBTMC488_REQUEST_REN_CONTROL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		rv = copy_from_user(&val, arg, sizeof(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		if (rv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			rv = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		wValue = val ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		wValue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			wValue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 			data->ifnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			buffer, 0x01, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		dev_err(dev, "simple usb_control_msg failed %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	} else if (rv != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		dev_warn(dev, "simple usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		dev_err(dev, "simple control status returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674)  * Sends a TRIGGER Bulk-OUT command message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675)  * See the USBTMC-USB488 specification, Table 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677)  * Also updates bTag_last_write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) static int usbtmc488_ioctl_trigger(struct usbtmc_file_data *file_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	struct usbtmc_device_data *data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	buffer = kzalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	buffer[0] = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	buffer[1] = data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	buffer[2] = ~data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	retval = usb_bulk_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			      usb_sndbulkpipe(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 					      data->bulk_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			      buffer, USBTMC_HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			      &actual, file_data->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	/* Store bTag (in case we need to abort) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	data->bTag_last_write = data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	/* Increment bTag -- and increment again if zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	data->bTag++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	if (!data->bTag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		data->bTag++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		dev_err(&data->intf->dev, "%s returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) static struct urb *usbtmc_create_urb(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	const size_t bufsize = USBTMC_BUFSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	u8 *dmabuf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	struct urb *urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	dmabuf = kmalloc(bufsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (!dmabuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	urb->transfer_buffer = dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	urb->transfer_buffer_length = bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	urb->transfer_flags |= URB_FREE_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	return urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) static void usbtmc_read_bulk_cb(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	struct usbtmc_file_data *file_data = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	/* sync/async unlink faults aren't errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		if (!(/* status == -ENOENT || */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			status == -ECONNRESET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			status == -EREMOTEIO || /* Short packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			status == -ESHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			dev_err(&file_data->data->intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			"%s - nonzero read bulk status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		spin_lock_irqsave(&file_data->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		if (!file_data->in_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			file_data->in_status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		spin_unlock_irqrestore(&file_data->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	spin_lock_irqsave(&file_data->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	file_data->in_transfer_size += urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	dev_dbg(&file_data->data->intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		"%s - total size: %u current: %d status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		__func__, file_data->in_transfer_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		urb->actual_length, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	spin_unlock_irqrestore(&file_data->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	usb_anchor_urb(urb, &file_data->in_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	wake_up_interruptible(&file_data->wait_bulk_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	wake_up_interruptible(&file_data->data->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) static inline bool usbtmc_do_transfer(struct usbtmc_file_data *file_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	bool data_or_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	data_or_error = !usb_anchor_empty(&file_data->in_anchor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 			|| file_data->in_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	dev_dbg(&file_data->data->intf->dev, "%s: returns %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		data_or_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	return data_or_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static ssize_t usbtmc_generic_read(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 				   void __user *user_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 				   u32 transfer_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 				   u32 *transferred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 				   u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	struct usbtmc_device_data *data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	struct device *dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	u32 done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	u32 remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	const u32 bufsize = USBTMC_BUFSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	u32 max_transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	unsigned long expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	int bufcount = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	int again = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	/* mutex already locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	*transferred = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	max_transfer_size = transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	if (flags & USBTMC_FLAG_IGNORE_TRAILER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		/* The device may send extra alignment bytes (up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		 * wMaxPacketSize – 1) to avoid sending a zero-length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		 * packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		remaining = transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		if ((max_transfer_size % data->wMaxPacketSize) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			max_transfer_size += (data->wMaxPacketSize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		/* round down to bufsize to avoid truncated data left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		if (max_transfer_size > bufsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 			max_transfer_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 				roundup(max_transfer_size + 1 - bufsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 					bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		remaining = max_transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (file_data->in_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		/* return the very first error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		retval = file_data->in_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (flags & USBTMC_FLAG_ASYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		if (usb_anchor_empty(&file_data->in_anchor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			again = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		if (file_data->in_urbs_used == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			file_data->in_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			file_data->in_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		file_data->in_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		file_data->in_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	if (max_transfer_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		bufcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		bufcount = roundup(max_transfer_size, bufsize) / bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (bufcount > file_data->in_urbs_used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			bufcount -= file_data->in_urbs_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			bufcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		if (bufcount + file_data->in_urbs_used > MAX_URBS_IN_FLIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			bufcount = MAX_URBS_IN_FLIGHT -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 					file_data->in_urbs_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	dev_dbg(dev, "%s: requested=%u flags=0x%X size=%u bufs=%d used=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		__func__, transfer_size, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		max_transfer_size, bufcount, file_data->in_urbs_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	while (bufcount > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		u8 *dmabuf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		struct urb *urb = usbtmc_create_urb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		dmabuf = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		usb_fill_bulk_urb(urb, data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			usb_rcvbulkpipe(data->usb_dev, data->bulk_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			dmabuf, bufsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 			usbtmc_read_bulk_cb, file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		usb_anchor_urb(urb, &file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		retval = usb_submit_urb(urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		/* urb is anchored. We can release our reference. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		if (unlikely(retval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		file_data->in_urbs_used++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		bufcount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	if (again) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		dev_dbg(dev, "%s: ret=again\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	if (user_buffer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	expire = msecs_to_jiffies(file_data->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	while (max_transfer_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		u32 this_part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		struct urb *urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		if (!(flags & USBTMC_FLAG_ASYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			dev_dbg(dev, "%s: before wait time %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 				__func__, expire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 			retval = wait_event_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 				file_data->wait_bulk_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 				usbtmc_do_transfer(file_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 				expire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			dev_dbg(dev, "%s: wait returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 				__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			if (retval <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 				if (retval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 					retval = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		urb = usb_get_from_anchor(&file_data->in_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			if (!(flags & USBTMC_FLAG_ASYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 				/* synchronous case: must not happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 				retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			/* asynchronous case: ready, do not block or wait */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			*transferred = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			dev_dbg(dev, "%s: (async) done=%u ret=0\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 				__func__, done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		file_data->in_urbs_used--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		if (max_transfer_size > urb->actual_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			max_transfer_size -= urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			max_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		if (remaining > urb->actual_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			this_part = urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			this_part = remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			urb->transfer_buffer, urb->actual_length, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		if (copy_to_user(user_buffer + done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				 urb->transfer_buffer, this_part)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		remaining -= this_part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		done += this_part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		if (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			/* return the very first error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			retval = file_data->in_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		if (urb->actual_length < bufsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			/* short packet or ZLP received => ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		if (!(flags & USBTMC_FLAG_ASYNC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		    max_transfer_size > (bufsize * file_data->in_urbs_used)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			/* resubmit, since other buffers still not enough */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			usb_anchor_urb(urb, &file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			retval = usb_submit_urb(urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			if (unlikely(retval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 				usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 				usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			file_data->in_urbs_used++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	*transferred = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	dev_dbg(dev, "%s: before kill\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	/* Attention: killing urbs can take long time (2 ms) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	usb_kill_anchored_urbs(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	dev_dbg(dev, "%s: after kill\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	usb_scuttle_anchored_urbs(&file_data->in_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	file_data->in_urbs_used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	file_data->in_status = 0; /* no spinlock needed here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	dev_dbg(dev, "%s: done=%u ret=%d\n", __func__, done, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) static ssize_t usbtmc_ioctl_generic_read(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 					 void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	struct usbtmc_message msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	ssize_t retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	/* mutex already locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	if (copy_from_user(&msg, arg, sizeof(struct usbtmc_message)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	retval = usbtmc_generic_read(file_data, msg.message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 				     msg.transfer_size, &msg.transferred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 				     msg.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	if (put_user(msg.transferred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		     &((struct usbtmc_message __user *)arg)->transferred))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) static void usbtmc_write_bulk_cb(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	struct usbtmc_file_data *file_data = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	int wakeup = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	spin_lock_irqsave(&file_data->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	file_data->out_transfer_size += urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	/* sync/async unlink faults aren't errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		if (!(urb->status == -ENOENT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			urb->status == -ECONNRESET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			urb->status == -ESHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 			dev_err(&file_data->data->intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				"%s - nonzero write bulk status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 				__func__, urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		if (!file_data->out_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			file_data->out_status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			wakeup = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	spin_unlock_irqrestore(&file_data->err_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	dev_dbg(&file_data->data->intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		"%s - write bulk total size: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		__func__, file_data->out_transfer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	if (usb_anchor_empty(&file_data->submitted) || wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		wake_up_interruptible(&file_data->data->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) static ssize_t usbtmc_generic_write(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 				    const void __user *user_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 				    u32 transfer_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 				    u32 *transferred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 				    u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	struct usbtmc_device_data *data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	u32 done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	u32 remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	unsigned long expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	const u32 bufsize = USBTMC_BUFSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	struct urb *urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	u32 timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	*transferred = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	/* Get pointer to private data structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	dev_dbg(dev, "%s: size=%u flags=0x%X sema=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		__func__, transfer_size, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		file_data->limit_write_sem.count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	if (flags & USBTMC_FLAG_APPEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		retval = file_data->out_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		file_data->out_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		file_data->out_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	remaining = transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	if (remaining > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		remaining = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	timeout = file_data->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	expire = msecs_to_jiffies(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	while (remaining > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		u32 this_part, aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		u8 *buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		if (flags & USBTMC_FLAG_ASYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			if (down_trylock(&file_data->limit_write_sem)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				retval = (done)?(0):(-EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 				goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			retval = down_timeout(&file_data->limit_write_sem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 					      expire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 			if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 				retval = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		retval = file_data->out_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		/* prepare next urb to send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		urb = usbtmc_create_urb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		buffer = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		if (remaining > bufsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			this_part = bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 			this_part = remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		if (copy_from_user(buffer, user_buffer + done, this_part)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 			retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			16, 1, buffer, this_part, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		/* fill bulk with 32 bit alignment to meet USBTMC specification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		 * (size + 3 & ~3) rounds up and simplifies user code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		aligned = (this_part + 3) & ~3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		dev_dbg(dev, "write(size:%u align:%u done:%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			(unsigned int)this_part,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			(unsigned int)aligned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			(unsigned int)done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		usb_fill_bulk_urb(urb, data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			usb_sndbulkpipe(data->usb_dev, data->bulk_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 			urb->transfer_buffer, aligned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			usbtmc_write_bulk_cb, file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		usb_anchor_urb(urb, &file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		retval = usb_submit_urb(urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		if (unlikely(retval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 			up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		urb = NULL; /* urb will be finally released by usb driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		remaining -= this_part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		done += this_part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	/* All urbs are on the fly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	if (!(flags & USBTMC_FLAG_ASYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		if (!usb_wait_anchor_empty_timeout(&file_data->submitted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 						   timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 			retval = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	usb_kill_anchored_urbs(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	if (!(flags & USBTMC_FLAG_ASYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		done = file_data->out_transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	if (!retval && file_data->out_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		retval = file_data->out_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	*transferred = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	dev_dbg(dev, "%s: done=%u, retval=%d, urbstat=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		__func__, done, retval, file_data->out_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) static ssize_t usbtmc_ioctl_generic_write(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 					  void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	struct usbtmc_message msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	ssize_t retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	/* mutex already locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (copy_from_user(&msg, arg, sizeof(struct usbtmc_message)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	retval = usbtmc_generic_write(file_data, msg.message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 				      msg.transfer_size, &msg.transferred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 				      msg.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	if (put_user(msg.transferred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		     &((struct usbtmc_message __user *)arg)->transferred))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)  * Get the generic write result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) static ssize_t usbtmc_ioctl_write_result(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	u32 transferred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	transferred = file_data->out_transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	retval = file_data->out_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	if (put_user(transferred, (__u32 __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-OUT endpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  * @transfer_size: number of bytes to request from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  * See the USBTMC specification, Table 4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)  * Also updates bTag_last_write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) static int send_request_dev_dep_msg_in(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 				       u32 transfer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	struct usbtmc_device_data *data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	/* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	 * Refer to class specs for details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	buffer[0] = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	buffer[1] = data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	buffer[2] = ~data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	buffer[3] = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	buffer[4] = transfer_size >> 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	buffer[5] = transfer_size >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	buffer[6] = transfer_size >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	buffer[7] = transfer_size >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	buffer[8] = file_data->term_char_enabled * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	/* Use term character? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	buffer[9] = file_data->term_char;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	buffer[10] = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	buffer[11] = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	/* Send bulk URB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	retval = usb_bulk_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			      usb_sndbulkpipe(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 					      data->bulk_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			      buffer, USBTMC_HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			      &actual, file_data->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	/* Store bTag (in case we need to abort) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	data->bTag_last_write = data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	/* Increment bTag -- and increment again if zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	data->bTag++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	if (!data->bTag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		data->bTag++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		dev_err(&data->intf->dev, "%s returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) static ssize_t usbtmc_read(struct file *filp, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			   size_t count, loff_t *f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	struct usbtmc_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	const u32 bufsize = USBTMC_BUFSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	u32 n_characters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	u32 done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	u32 remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	/* Get pointer to private data structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	file_data = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	buffer = kmalloc(bufsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	if (data->zombie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	if (count > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		count = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	dev_dbg(dev, "%s(count:%zu)\n", __func__, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	retval = send_request_dev_dep_msg_in(file_data, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		if (file_data->auto_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			usbtmc_ioctl_abort_bulk_out(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	/* Loop until we have fetched everything we requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	remaining = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	actual = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	/* Send bulk URB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	retval = usb_bulk_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 			      usb_rcvbulkpipe(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 					      data->bulk_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 			      buffer, bufsize, &actual,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			      file_data->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	dev_dbg(dev, "%s: bulk_msg retval(%u), actual(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		__func__, retval, actual);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	/* Store bTag (in case we need to abort) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	data->bTag_last_read = data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		if (file_data->auto_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			usbtmc_ioctl_abort_bulk_in(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	/* Sanity checks for the header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	if (actual < USBTMC_HEADER_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		dev_err(dev, "Device sent too small first packet: %u < %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 			actual, USBTMC_HEADER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		if (file_data->auto_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			usbtmc_ioctl_abort_bulk_in(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	if (buffer[0] != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 			buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		if (file_data->auto_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			usbtmc_ioctl_abort_bulk_in(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	if (buffer[1] != data->bTag_last_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		buffer[1], data->bTag_last_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		if (file_data->auto_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			usbtmc_ioctl_abort_bulk_in(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	/* How many characters did the instrument send? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	n_characters = buffer[4] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		       (buffer[5] << 8) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		       (buffer[6] << 16) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		       (buffer[7] << 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	file_data->bmTransferAttributes = buffer[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		n_characters, buffer[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	if (n_characters > remaining) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		dev_err(dev, "Device wants to return more data than requested: %u > %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			n_characters, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		if (file_data->auto_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 			usbtmc_ioctl_abort_bulk_in(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			     16, 1, buffer, actual, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	remaining = n_characters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	/* Remove the USBTMC header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	actual -= USBTMC_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	/* Remove padding if it exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (actual > remaining)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		actual = remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	remaining -= actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	/* Copy buffer to user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	if (copy_to_user(buf, &buffer[USBTMC_HEADER_SIZE], actual)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		/* There must have been an addressing problem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	if ((actual + USBTMC_HEADER_SIZE) == bufsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		retval = usbtmc_generic_read(file_data, buf + actual,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 					     remaining,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 					     &done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 					     USBTMC_FLAG_IGNORE_TRAILER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	done += actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	/* Update file position value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	*f_pos = *f_pos + done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	retval = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 			    size_t count, loff_t *f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	struct usbtmc_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	struct urb *urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	ssize_t retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	u32 remaining, done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	u32 transfersize, aligned, buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	file_data = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	if (data->zombie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	file_data->out_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	file_data->out_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (down_trylock(&file_data->limit_write_sem)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		/* previous calls were async */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		retval = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	urb = usbtmc_create_urb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	buffer = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	buflen = urb->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	if (count > INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		transfersize = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		buffer[8] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		transfersize = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		buffer[8] = file_data->eom_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	/* Setup IO buffer for DEV_DEP_MSG_OUT message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	buffer[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	buffer[1] = data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	buffer[2] = ~data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	buffer[3] = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	buffer[4] = transfersize >> 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	buffer[5] = transfersize >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	buffer[6] = transfersize >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	buffer[7] = transfersize >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	/* buffer[8] is set above... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	buffer[9] = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	buffer[10] = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	buffer[11] = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	remaining = transfersize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	if (transfersize + USBTMC_HEADER_SIZE > buflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		transfersize = buflen - USBTMC_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		aligned = buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		aligned = (transfersize + (USBTMC_HEADER_SIZE + 3)) & ~3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf, transfersize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	dev_dbg(&data->intf->dev, "%s(size:%u align:%u)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		(unsigned int)transfersize, (unsigned int)aligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			     16, 1, buffer, aligned, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	usb_fill_bulk_urb(urb, data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		usb_sndbulkpipe(data->usb_dev, data->bulk_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		urb->transfer_buffer, aligned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		usbtmc_write_bulk_cb, file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	usb_anchor_urb(urb, &file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	retval = usb_submit_urb(urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	if (unlikely(retval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		up(&file_data->limit_write_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	remaining -= transfersize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	data->bTag_last_write = data->bTag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	data->bTag++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	if (!data->bTag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		data->bTag++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	/* call generic_write even when remaining = 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	retval = usbtmc_generic_write(file_data, buf + transfersize, remaining,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 				      &done, USBTMC_FLAG_APPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	/* truncate alignment bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	if (done > remaining)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		done = remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	/*add size of first urb*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	done += transfersize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		usb_kill_anchored_urbs(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		dev_err(&data->intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 			"Unable to send data, error %d\n", (int)retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		if (file_data->auto_abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			usbtmc_ioctl_abort_bulk_out(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	retval = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	int actual = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	buffer = kmalloc(USBTMC_BUFSIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			     usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 			     USBTMC_REQUEST_INITIATE_CLEAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			     0, 0, buffer, 1, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) usbtmc_clear_check_status:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			     usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			     USBTMC_REQUEST_CHECK_CLEAR_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			     0, 0, buffer, 2, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	if (buffer[0] == USBTMC_STATUS_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		goto usbtmc_clear_bulk_out_halt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	if (buffer[0] != USBTMC_STATUS_PENDING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	if ((buffer[1] & 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			dev_dbg(dev, "Reading from bulk in EP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 			actual = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 			rv = usb_bulk_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 					  usb_rcvbulkpipe(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 							  data->bulk_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 					  buffer, USBTMC_BUFSIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 					  &actual, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 			print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 					     16, 1, buffer, actual, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 			n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 			if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 				dev_err(dev, "usb_control_msg returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 					rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 				goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		} while ((actual == USBTMC_BUFSIZE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 			  (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		/* do not stress device with subsequent requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		msleep(50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if (n >= USBTMC_MAX_READS_TO_CLEAR_BULK_IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	goto usbtmc_clear_check_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) usbtmc_clear_bulk_out_halt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	rv = usb_clear_halt(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		dev_err(dev, "usb_clear_halt returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	rv = usb_clear_halt(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 			    usb_sndbulkpipe(data->usb_dev, data->bulk_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		dev_err(&data->usb_dev->dev, "%s returned %d\n", __func__, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	rv = usb_clear_halt(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 			    usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		dev_err(&data->usb_dev->dev, "%s returned %d\n", __func__, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) static int usbtmc_ioctl_cancel_io(struct usbtmc_file_data *file_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	file_data->in_status = -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	file_data->out_status = -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	usb_kill_anchored_urbs(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) static int usbtmc_ioctl_cleanup_io(struct usbtmc_file_data *file_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	usb_kill_anchored_urbs(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	usb_scuttle_anchored_urbs(&file_data->in_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	file_data->in_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	file_data->in_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	file_data->out_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	file_data->out_transfer_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	file_data->in_urbs_used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) static int get_capabilities(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	struct device *dev = &data->usb_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	int rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	buffer = kmalloc(0x18, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 			     USBTMC_REQUEST_GET_CAPABILITIES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 			     0, 0, buffer, 0x18, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	data->capabilities.interface_capabilities = buffer[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	data->capabilities.device_capabilities = buffer[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	data->capabilities.usb488_interface_capabilities = buffer[14];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	data->capabilities.usb488_device_capabilities = buffer[15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) #define capability_attribute(name)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) static ssize_t name##_show(struct device *dev,				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 			   struct device_attribute *attr, char *buf)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	struct usb_interface *intf = to_usb_interface(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	struct usbtmc_device_data *data = usb_get_intfdata(intf);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	return sprintf(buf, "%d\n", data->capabilities.name);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) static DEVICE_ATTR_RO(name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) capability_attribute(interface_capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) capability_attribute(device_capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) capability_attribute(usb488_interface_capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) capability_attribute(usb488_device_capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) static struct attribute *usbtmc_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	&dev_attr_interface_capabilities.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	&dev_attr_device_capabilities.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	&dev_attr_usb488_interface_capabilities.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	&dev_attr_usb488_device_capabilities.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) ATTRIBUTE_GROUPS(usbtmc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	buffer = kmalloc(2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 			     usb_rcvctrlpipe(data->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 			     USBTMC_REQUEST_INDICATOR_PULSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			     0, 0, buffer, 0x01, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 		dev_err(dev, "usb_control_msg returned %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		rv = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) static int usbtmc_ioctl_request(struct usbtmc_device_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	struct device *dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	struct usbtmc_ctrlrequest request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	u8 *buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	unsigned int is_in, pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	unsigned long res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	res = copy_from_user(&request, arg, sizeof(struct usbtmc_ctrlrequest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	if (request.req.wLength > USBTMC_BUFSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	is_in = request.req.bRequestType & USB_DIR_IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	if (request.req.wLength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		buffer = kmalloc(request.req.wLength, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		if (!is_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 			/* Send control data to device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 			res = copy_from_user(buffer, request.data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 					     request.req.wLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 			if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 				rv = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 				goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	if (is_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		pipe = usb_rcvctrlpipe(data->usb_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		pipe = usb_sndctrlpipe(data->usb_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	rv = usb_control_msg(data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 			pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 			request.req.bRequest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 			request.req.bRequestType,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 			request.req.wValue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 			request.req.wIndex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 			buffer, request.req.wLength, USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		dev_err(dev, "%s failed %d\n", __func__, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	if (rv && is_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		/* Read control data from device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		res = copy_to_user(request.data, buffer, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 			rv = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)  exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)  * Get the usb timeout value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) static int usbtmc_ioctl_get_timeout(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	u32 timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	timeout = file_data->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	return put_user(timeout, (__u32 __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963)  * Set the usb timeout value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) static int usbtmc_ioctl_set_timeout(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	u32 timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	if (get_user(timeout, (__u32 __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	/* Note that timeout = 0 means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	 * MAX_SCHEDULE_TIMEOUT in usb_control_msg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	if (timeout < USBTMC_MIN_TIMEOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	file_data->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)  * enables/disables sending EOM on write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) static int usbtmc_ioctl_eom_enable(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	u8 eom_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	if (copy_from_user(&eom_enable, arg, sizeof(eom_enable)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if (eom_enable > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	file_data->eom_val = eom_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)  * Configure termination character for read()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) static int usbtmc_ioctl_config_termc(struct usbtmc_file_data *file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	struct usbtmc_termchar termc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	if (copy_from_user(&termc, arg, sizeof(termc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	if ((termc.term_char_enabled > 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		(termc.term_char_enabled &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		!(file_data->data->capabilities.device_capabilities & 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	file_data->term_char = termc.term_char;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	file_data->term_char_enabled = termc.term_char_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	struct usbtmc_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	int retval = -EBADRQC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	__u8 tmp_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	file_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	if (data->zombie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		goto skip_io_on_zombie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	case USBTMC_IOCTL_CLEAR_OUT_HALT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		retval = usbtmc_ioctl_clear_out_halt(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	case USBTMC_IOCTL_CLEAR_IN_HALT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		retval = usbtmc_ioctl_clear_in_halt(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	case USBTMC_IOCTL_INDICATOR_PULSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		retval = usbtmc_ioctl_indicator_pulse(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	case USBTMC_IOCTL_CLEAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		retval = usbtmc_ioctl_clear(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	case USBTMC_IOCTL_ABORT_BULK_OUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 		retval = usbtmc_ioctl_abort_bulk_out(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	case USBTMC_IOCTL_ABORT_BULK_IN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		retval = usbtmc_ioctl_abort_bulk_in(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	case USBTMC_IOCTL_CTRL_REQUEST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		retval = usbtmc_ioctl_request(data, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	case USBTMC_IOCTL_GET_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		retval = usbtmc_ioctl_get_timeout(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 						  (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	case USBTMC_IOCTL_SET_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		retval = usbtmc_ioctl_set_timeout(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 						  (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	case USBTMC_IOCTL_EOM_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		retval = usbtmc_ioctl_eom_enable(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 						 (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	case USBTMC_IOCTL_CONFIG_TERMCHAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		retval = usbtmc_ioctl_config_termc(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 						   (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	case USBTMC_IOCTL_WRITE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		retval = usbtmc_ioctl_generic_write(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 						    (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	case USBTMC_IOCTL_READ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 		retval = usbtmc_ioctl_generic_read(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 						   (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	case USBTMC_IOCTL_WRITE_RESULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 		retval = usbtmc_ioctl_write_result(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 						   (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	case USBTMC_IOCTL_API_VERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		retval = put_user(USBTMC_API_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 				  (__u32 __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	case USBTMC488_IOCTL_GET_CAPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 		retval = put_user(data->usb488_caps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 				  (unsigned char __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	case USBTMC488_IOCTL_READ_STB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		retval = usbtmc488_ioctl_read_stb(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 						  (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	case USBTMC488_IOCTL_REN_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 						USBTMC488_REQUEST_REN_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	case USBTMC488_IOCTL_GOTO_LOCAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 						USBTMC488_REQUEST_GOTO_LOCAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	case USBTMC488_IOCTL_LOCAL_LOCKOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 		retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 						USBTMC488_REQUEST_LOCAL_LOCKOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	case USBTMC488_IOCTL_TRIGGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		retval = usbtmc488_ioctl_trigger(file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	case USBTMC488_IOCTL_WAIT_SRQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		retval = usbtmc488_ioctl_wait_srq(file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 						  (__u32 __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	case USBTMC_IOCTL_MSG_IN_ATTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		retval = put_user(file_data->bmTransferAttributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 				  (__u8 __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	case USBTMC_IOCTL_AUTO_ABORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		retval = get_user(tmp_byte, (unsigned char __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 		if (retval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 			file_data->auto_abort = !!tmp_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	case USBTMC_IOCTL_CANCEL_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		retval = usbtmc_ioctl_cancel_io(file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	case USBTMC_IOCTL_CLEANUP_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		retval = usbtmc_ioctl_cleanup_io(file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) skip_io_on_zombie:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) static int usbtmc_fasync(int fd, struct file *file, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	struct usbtmc_file_data *file_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	return fasync_helper(fd, file, on, &file_data->data->fasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) static __poll_t usbtmc_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	struct usbtmc_file_data *file_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	struct usbtmc_device_data *data = file_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	__poll_t mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	if (data->zombie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		mask = EPOLLHUP | EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		goto no_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	poll_wait(file, &data->waitq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	/* Note that EPOLLPRI is now assigned to SRQ, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	 * EPOLLIN|EPOLLRDNORM to normal read data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	if (atomic_read(&file_data->srq_asserted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		mask |= EPOLLPRI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	/* Note that the anchor submitted includes all urbs for BULK IN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	 * and OUT. So EPOLLOUT is signaled when BULK OUT is empty and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	 * all BULK IN urbs are completed and moved to in_anchor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	if (usb_anchor_empty(&file_data->submitted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		mask |= (EPOLLOUT | EPOLLWRNORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	if (!usb_anchor_empty(&file_data->in_anchor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 		mask |= (EPOLLIN | EPOLLRDNORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	spin_lock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	if (file_data->in_status || file_data->out_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		mask |= EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	spin_unlock_irq(&file_data->err_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	dev_dbg(&data->intf->dev, "poll mask = %x\n", mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) no_poll:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) static const struct file_operations fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	.read		= usbtmc_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	.write		= usbtmc_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	.open		= usbtmc_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	.release	= usbtmc_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	.flush		= usbtmc_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	.unlocked_ioctl	= usbtmc_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	.fasync         = usbtmc_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	.poll           = usbtmc_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	.llseek		= default_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) static struct usb_class_driver usbtmc_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	.name =		"usbtmc%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	.fops =		&fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	.minor_base =	USBTMC_MINOR_BASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) static void usbtmc_interrupt(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	struct usbtmc_device_data *data = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	struct device *dev = &data->intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	dev_dbg(&data->intf->dev, "int status: %d len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 		status, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	case 0: /* SUCCESS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		/* check for valid STB notification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		if (data->iin_buffer[0] > 0x81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 			data->bNotify1 = data->iin_buffer[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 			data->bNotify2 = data->iin_buffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 			atomic_set(&data->iin_data_valid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 			wake_up_interruptible(&data->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		/* check for SRQ notification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		if (data->iin_buffer[0] == 0x81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 			unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 			struct list_head *elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 			if (data->fasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 				kill_fasync(&data->fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 					SIGIO, POLL_PRI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 			spin_lock_irqsave(&data->dev_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 			list_for_each(elem, &data->file_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 				struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 				file_data = list_entry(elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 						       struct usbtmc_file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 						       file_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 				file_data->srq_byte = data->iin_buffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 				atomic_set(&file_data->srq_asserted, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			spin_unlock_irqrestore(&data->dev_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 			dev_dbg(dev, "srq received bTag %x stb %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 				(unsigned int)data->iin_buffer[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 				(unsigned int)data->iin_buffer[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 			wake_up_interruptible_all(&data->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		dev_warn(dev, "invalid notification: %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 			 data->iin_buffer[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	case -EOVERFLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		dev_err(dev, "overflow with length %d, actual length is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 			data->iin_wMaxPacketSize, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		/* urb terminated, clean up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 		dev_dbg(dev, "urb terminated, status: %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	rv = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 		dev_err(dev, "usb_submit_urb failed: %d\n", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) static void usbtmc_free_int(struct usbtmc_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	if (!data->iin_ep_present || !data->iin_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	usb_kill_urb(data->iin_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	kfree(data->iin_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	data->iin_buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	usb_free_urb(data->iin_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	data->iin_urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	kref_put(&data->kref, usbtmc_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) static int usbtmc_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 			const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	struct usbtmc_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	struct usb_host_interface *iface_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	struct usb_endpoint_descriptor *bulk_in, *bulk_out, *int_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	int retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	dev_dbg(&intf->dev, "%s called\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 	data->intf = intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	data->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	usb_set_intfdata(intf, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	kref_init(&data->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	mutex_init(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	init_waitqueue_head(&data->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	atomic_set(&data->iin_data_valid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	INIT_LIST_HEAD(&data->file_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	spin_lock_init(&data->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	data->zombie = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	/* Initialize USBTMC bTag and other fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	data->bTag	= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	/*  2 <= bTag <= 127   USBTMC-USB488 subclass specification 4.3.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	data->iin_bTag = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	/* USBTMC devices have only one setting, so use that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	iface_desc = data->intf->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	data->ifnum = iface_desc->desc.bInterfaceNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	/* Find bulk endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 	retcode = usb_find_common_endpoints(iface_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 			&bulk_in, &bulk_out, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	if (retcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		dev_err(&intf->dev, "bulk endpoints not found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	retcode = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	data->bulk_in = bulk_in->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	data->wMaxPacketSize = usb_endpoint_maxp(bulk_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	if (!data->wMaxPacketSize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n", data->bulk_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	data->bulk_out = bulk_out->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n", data->bulk_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	/* Find int endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	retcode = usb_find_int_in_endpoint(iface_desc, &int_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	if (!retcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 		data->iin_ep_present = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		data->iin_ep = int_in->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		data->iin_wMaxPacketSize = usb_endpoint_maxp(int_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		data->iin_interval = int_in->bInterval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 				data->iin_ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	retcode = get_capabilities(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	if (retcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		dev_err(&intf->dev, "can't read capabilities\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	if (data->iin_ep_present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 		/* allocate int urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 		if (!data->iin_urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 			retcode = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 			goto error_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 		/* Protect interrupt in endpoint data until iin_urb is freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 		kref_get(&data->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		/* allocate buffer for interrupt in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		if (!data->iin_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 			retcode = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 			goto error_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 		/* fill interrupt urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 		usb_fill_int_urb(data->iin_urb, data->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 				usb_rcvintpipe(data->usb_dev, data->iin_ep),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 				data->iin_buffer, data->iin_wMaxPacketSize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 				usbtmc_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 				data, data->iin_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 		retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 		if (retcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 			dev_err(&intf->dev, "Failed to submit iin_urb\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 			goto error_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	retcode = usb_register_dev(intf, &usbtmc_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	if (retcode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 		dev_err(&intf->dev, "Not able to get a minor (base %u, slice default): %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 			USBTMC_MINOR_BASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 			retcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		goto error_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) error_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	usbtmc_free_int(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	kref_put(&data->kref, usbtmc_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	return retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) static void usbtmc_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	struct usbtmc_device_data *data  = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	struct list_head *elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	usb_deregister_dev(intf, &usbtmc_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	data->zombie = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	wake_up_interruptible_all(&data->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	list_for_each(elem, &data->file_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 		struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 		file_data = list_entry(elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 				       struct usbtmc_file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 				       file_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 		usb_kill_anchored_urbs(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 		usb_scuttle_anchored_urbs(&file_data->in_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	usbtmc_free_int(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	kref_put(&data->kref, usbtmc_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) static void usbtmc_draw_down(struct usbtmc_file_data *file_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	int time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	time = usb_wait_anchor_empty_timeout(&file_data->submitted, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	if (!time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 		usb_kill_anchored_urbs(&file_data->submitted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	usb_scuttle_anchored_urbs(&file_data->in_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	struct usbtmc_device_data *data = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	struct list_head *elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	list_for_each(elem, &data->file_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 		struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		file_data = list_entry(elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 				       struct usbtmc_file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 				       file_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 		usbtmc_draw_down(file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	if (data->iin_ep_present && data->iin_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 		usb_kill_urb(data->iin_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) static int usbtmc_resume(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	struct usbtmc_device_data *data = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	int retcode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	if (data->iin_ep_present && data->iin_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 	if (retcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		dev_err(&intf->dev, "Failed to submit iin_urb\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	return retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) static int usbtmc_pre_reset(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	struct usbtmc_device_data *data  = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	struct list_head *elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	mutex_lock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	list_for_each(elem, &data->file_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 		struct usbtmc_file_data *file_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		file_data = list_entry(elem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 				       struct usbtmc_file_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 				       file_elem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 		usbtmc_ioctl_cancel_io(file_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) static int usbtmc_post_reset(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	struct usbtmc_device_data *data  = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	mutex_unlock(&data->io_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) static struct usb_driver usbtmc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	.name		= "usbtmc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	.id_table	= usbtmc_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	.probe		= usbtmc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	.disconnect	= usbtmc_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	.suspend	= usbtmc_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	.resume		= usbtmc_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	.pre_reset	= usbtmc_pre_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	.post_reset	= usbtmc_post_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	.dev_groups	= usbtmc_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) module_usb_driver(usbtmc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) MODULE_LICENSE("GPL");