├── Makefile ├── .gitignore ├── screendump.c ├── tmc.h ├── README.md ├── COPYING ├── LICENSE ├── ttmc.c └── usbtmc.c /Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | # kbuild part of makefile 3 | obj-m := usbtmc.o 4 | else 5 | # normal makefile 6 | KDIR ?= /lib/modules/`uname -r`/build 7 | default: 8 | $(MAKE) -C $(KDIR) M=$$PWD 9 | 10 | install: 11 | $(MAKE) -C $(KDIR) M=$$PWD modules_install 12 | 13 | clean: 14 | $(MAKE) -C $(KDIR) M=$$PWD clean 15 | rm -f ttmc 16 | 17 | endif 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | .cache.mk 54 | # test programmes 55 | ttmc 56 | screendump 57 | -------------------------------------------------------------------------------- /screendump.c: -------------------------------------------------------------------------------- 1 | /* 2 | Sample usbtmc programme to produce a screendump in png format 3 | from an SCPI compliant instrument. 4 | 5 | Reads from /dev/usbtmc0 and writes to a file screendump.png in 6 | the current directory. Reads are done in 1024 byte chunks. The 7 | ":DISP:DATA?" command may need to be adapted to your 8 | instrument. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | 20 | int fd; 21 | char *sdfn = "screendump.png"; 22 | 23 | /* Send string to scope */ 24 | void sscope(char *msg) { 25 | write(fd,msg,strlen(msg)); 26 | } 27 | 28 | /* Read string from scope */ 29 | int rscope(char *buf, int max_len) { 30 | int len = read(fd,buf,max_len); 31 | if (len < 0) { 32 | perror("Ffailed to read from scope"); 33 | ioctl(fd,USBTMC_IOCTL_CLEAR); 34 | return 0; 35 | } 36 | buf[len] = 0; /* zero terminate */ 37 | return len; 38 | } 39 | 40 | 41 | int main () { 42 | int len,n,count,rlen,done; 43 | int sfd; 44 | char buf[256],*gbuf; 45 | unsigned char stb; 46 | 47 | /* Open instrument file */ 48 | if (0 > (fd = open("/dev/usbtmc0",O_RDWR))) { 49 | perror("Failed to open device"); 50 | exit(1); 51 | } 52 | 53 | /* Send device clear */ 54 | if (0 != ioctl(fd,USBTMC_IOCTL_CLEAR)) { 55 | perror("Device clear ioctl failed"); 56 | goto out; 57 | } 58 | 59 | sscope("*CLS\n"); // clear status regs 60 | // set operation complete bit in the Event Status Enable register 61 | sscope("*ESE 1\n"); 62 | 63 | //sscope("SYST:MENU OFF\n"); // turn off soft keys 64 | //sscope("HARD:INKS 0\n"); // no colour inversion 65 | 66 | //sscope(":DISP:DATA? ON, OFF, PNG;*OPC\n"); // For Rigol scope 67 | sscope(":DISP:DATA? PNG, COL;*OPC\n"); // For Keysight scope 68 | 69 | rscope(buf,2); // read first 2 bytes of header block 70 | if (buf[0] != '#') { /* Check that we have a valid header */ 71 | fprintf(stderr, "invalid IEEE488 # binary block header\n"); 72 | goto out; 73 | } 74 | n = buf[1] - 48; // get the number of digits in data length 75 | if (n < 2 || n > 9) { 76 | fprintf(stderr, "Invalid block length in IEEE488 BB header\n"); 77 | goto out; 78 | } 79 | rscope(buf,n); // read data length 80 | len = atoi(buf); 81 | 82 | // check status byte for operation complete i.e. ESB set 83 | if (0 != ioctl(fd,USBTMC488_IOCTL_READ_STB,&stb)) { 84 | perror("Read stb ioctl failed"); 85 | goto out; 86 | } 87 | n = 0; 88 | printf("Waiting for OPC\n"); 89 | while (!(stb & 32)) { /* wait for ESB */ 90 | n++; 91 | if (n==10) { 92 | fprintf(stderr,"Timed out waiting for screen dump\n"); 93 | goto out; 94 | } 95 | sleep(1); 96 | if (0 != ioctl(fd,USBTMC488_IOCTL_READ_STB,&stb)) { 97 | perror("Read stb ioctl failed"); 98 | goto out; 99 | } 100 | } 101 | printf("Reading %d bytes of display data\n",len); 102 | gbuf = malloc(len + 1); // +1 for null termination in rscope 103 | count = len; 104 | rlen = 1024; 105 | done = 0; 106 | while (count > 0) { 107 | if (count < rlen) rlen = count; 108 | n = rscope(&gbuf[done], rlen); 109 | // printf("%d bytes read\n", n); 110 | if (n != rlen) { 111 | fprintf(stderr, "Short read \n"); 112 | goto out; 113 | } 114 | done += n; 115 | count -= n; 116 | } 117 | printf("Total %d bytes read\n",done); 118 | rscope(buf,1); // read last byte (linefeed) 119 | if (buf[0] != '\n') fprintf(stderr,"Expected newline at the end\n"); 120 | 121 | sfd = open(sdfn, O_RDWR | O_CREAT, 0660); // open png file 122 | if (sfd < 0) { 123 | fprintf(stderr,"Failed to open %s\n",sdfn); 124 | goto out; 125 | } 126 | write(sfd, gbuf, len); // write png data 127 | close(sfd); 128 | 129 | printf("Screen dumped to %s\n",sdfn); 130 | out: 131 | close(fd); 132 | } 133 | -------------------------------------------------------------------------------- /tmc.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 | /* 3 | * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany 4 | * Copyright (C) 2008 Novell, Inc. 5 | * Copyright (C) 2008 Greg Kroah-Hartman 6 | * Copyright (C) 2015 Dave Penkler 7 | * Copyright (C) 2018 IVI Foundation, Inc. 8 | * 9 | * This file holds USB constants defined by the USB Device Class 10 | * and USB488 Subclass Definitions for Test and Measurement devices 11 | * published by the USB-IF. 12 | * 13 | * It also has the ioctl and capability definitions for the 14 | * usbtmc kernel driver that userspace needs to know about. 15 | */ 16 | 17 | #ifndef __LINUX_USB_TMC_H 18 | #define __LINUX_USB_TMC_H 19 | 20 | #include /* __u8 etc */ 21 | 22 | /* USB TMC status values */ 23 | #define USBTMC_STATUS_SUCCESS 0x01 24 | #define USBTMC_STATUS_PENDING 0x02 25 | #define USBTMC_STATUS_FAILED 0x80 26 | #define USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS 0x81 27 | #define USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS 0x82 28 | #define USBTMC_STATUS_SPLIT_IN_PROGRESS 0x83 29 | 30 | /* USB TMC requests values */ 31 | #define USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT 1 32 | #define USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS 2 33 | #define USBTMC_REQUEST_INITIATE_ABORT_BULK_IN 3 34 | #define USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS 4 35 | #define USBTMC_REQUEST_INITIATE_CLEAR 5 36 | #define USBTMC_REQUEST_CHECK_CLEAR_STATUS 6 37 | #define USBTMC_REQUEST_GET_CAPABILITIES 7 38 | #define USBTMC_REQUEST_INDICATOR_PULSE 64 39 | #define USBTMC488_REQUEST_READ_STATUS_BYTE 128 40 | #define USBTMC488_REQUEST_REN_CONTROL 160 41 | #define USBTMC488_REQUEST_GOTO_LOCAL 161 42 | #define USBTMC488_REQUEST_LOCAL_LOCKOUT 162 43 | 44 | struct usbtmc_request { 45 | __u8 bRequestType; 46 | __u8 bRequest; 47 | __u16 wValue; 48 | __u16 wIndex; 49 | __u16 wLength; 50 | } __attribute__ ((packed)); 51 | 52 | struct usbtmc_ctrlrequest { 53 | struct usbtmc_request req; 54 | void __user *data; /* pointer to user space */ 55 | __u8 filler[0] __attribute__ ((aligned (8))); 56 | } __attribute__ ((packed)); 57 | 58 | struct usbtmc_termchar { 59 | __u8 term_char; 60 | __u8 term_char_enabled; 61 | } __attribute__ ((packed)); 62 | 63 | /* 64 | * usbtmc_message->flags: 65 | */ 66 | #define USBTMC_FLAG_ASYNC 0x0001 67 | #define USBTMC_FLAG_APPEND 0x0002 68 | #define USBTMC_FLAG_IGNORE_TRAILER 0x0004 69 | 70 | struct usbtmc_message { 71 | __u32 transfer_size; /* size of bytes to transfer */ 72 | __u32 transferred; /* size of received/written bytes */ 73 | __u32 flags; /* bit 0: 0 = synchronous; 1 = asynchronous */ 74 | void __user *message __attribute__ ((aligned (8))); 75 | __u8 filler[0] __attribute__ ((aligned (8))); 76 | } __attribute__ ((packed)); 77 | 78 | /* Request values for USBTMC driver's ioctl entry point */ 79 | #define USBTMC_IOC_NR 91 80 | #define USBTMC_IOCTL_INDICATOR_PULSE _IO(USBTMC_IOC_NR, 1) 81 | #define USBTMC_IOCTL_CLEAR _IO(USBTMC_IOC_NR, 2) 82 | #define USBTMC_IOCTL_ABORT_BULK_OUT _IO(USBTMC_IOC_NR, 3) 83 | #define USBTMC_IOCTL_ABORT_BULK_IN _IO(USBTMC_IOC_NR, 4) 84 | #define USBTMC_IOCTL_CLEAR_OUT_HALT _IO(USBTMC_IOC_NR, 6) 85 | #define USBTMC_IOCTL_CLEAR_IN_HALT _IO(USBTMC_IOC_NR, 7) 86 | #define USBTMC_IOCTL_CTRL_REQUEST _IOWR(USBTMC_IOC_NR, 8, struct usbtmc_ctrlrequest) 87 | #define USBTMC_IOCTL_GET_TIMEOUT _IOR(USBTMC_IOC_NR, 9, __u32) 88 | #define USBTMC_IOCTL_SET_TIMEOUT _IOW(USBTMC_IOC_NR, 10, __u32) 89 | #define USBTMC_IOCTL_EOM_ENABLE _IOW(USBTMC_IOC_NR, 11, __u8) 90 | #define USBTMC_IOCTL_CONFIG_TERMCHAR _IOW(USBTMC_IOC_NR, 12, struct usbtmc_termchar) 91 | #define USBTMC_IOCTL_WRITE _IOWR(USBTMC_IOC_NR, 13, struct usbtmc_message) 92 | #define USBTMC_IOCTL_READ _IOWR(USBTMC_IOC_NR, 14, struct usbtmc_message) 93 | #define USBTMC_IOCTL_WRITE_RESULT _IOWR(USBTMC_IOC_NR, 15, __u32) 94 | #define USBTMC_IOCTL_API_VERSION _IOR(USBTMC_IOC_NR, 16, __u32) 95 | 96 | #define USBTMC488_IOCTL_GET_CAPS _IOR(USBTMC_IOC_NR, 17, unsigned char) 97 | #define USBTMC488_IOCTL_READ_STB _IOR(USBTMC_IOC_NR, 18, unsigned char) 98 | #define USBTMC488_IOCTL_REN_CONTROL _IOW(USBTMC_IOC_NR, 19, unsigned char) 99 | #define USBTMC488_IOCTL_GOTO_LOCAL _IO(USBTMC_IOC_NR, 20) 100 | #define USBTMC488_IOCTL_LOCAL_LOCKOUT _IO(USBTMC_IOC_NR, 21) 101 | #define USBTMC488_IOCTL_TRIGGER _IO(USBTMC_IOC_NR, 22) 102 | #define USBTMC488_IOCTL_WAIT_SRQ _IOW(USBTMC_IOC_NR, 23, __u32) 103 | 104 | #define USBTMC_IOCTL_MSG_IN_ATTR _IOR(USBTMC_IOC_NR, 24, __u8) 105 | #define USBTMC_IOCTL_AUTO_ABORT _IOW(USBTMC_IOC_NR, 25, __u8) 106 | 107 | #define USBTMC_IOCTL_GET_STB _IOR(USBTMC_IOC_NR, 26, __u8) 108 | #define USBTMC_IOCTL_GET_SRQ_STB _IOR(USBTMC_IOC_NR, 27, __u8) 109 | 110 | /* Cancel and cleanup asynchronous calls */ 111 | #define USBTMC_IOCTL_CANCEL_IO _IO(USBTMC_IOC_NR, 35) 112 | #define USBTMC_IOCTL_CLEANUP_IO _IO(USBTMC_IOC_NR, 36) 113 | 114 | /* Driver encoded usb488 capabilities */ 115 | #define USBTMC488_CAPABILITY_TRIGGER 1 116 | #define USBTMC488_CAPABILITY_SIMPLE 2 117 | #define USBTMC488_CAPABILITY_REN_CONTROL 2 118 | #define USBTMC488_CAPABILITY_GOTO_LOCAL 2 119 | #define USBTMC488_CAPABILITY_LOCAL_LOCKOUT 2 120 | #define USBTMC488_CAPABILITY_488_DOT_2 4 121 | #define USBTMC488_CAPABILITY_DT1 16 122 | #define USBTMC488_CAPABILITY_RL1 32 123 | #define USBTMC488_CAPABILITY_SR1 64 124 | #define USBTMC488_CAPABILITY_FULL_SCPI 128 125 | 126 | #endif 127 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # linux-usbtmc driver 2 | 3 | This is an experimental linux driver for usb test measurement & 4 | control instruments. It adds support for missing functions in 5 | USBTMC-USB488 spec, the ability to handle SRQ notifications with 6 | fasync or poll/select and a number of features required to support the 7 | IVI library. This package is provided for folks wanting to test or 8 | use driver features not yet supported by the standard usbtmc driver in 9 | their kernel. 10 | 11 | The following functions have not yet been incorporated into 12 | a kernel.org release: 13 | - module params 14 | - 32 bit support for IVI USBTMC_IOCTL_CTRL_REQUEST and USBTMC_IOCTL__READ/WRITE on 64 bit platforms. 15 | 16 | Note: The initial version incorporated into the 14.6.0 kernel release used POLLIN / select readfds for SRQ notifications. This was changed to POLLPRI / select exceptfds in the version incorporated in the 4.19.0 and subsequent kernel release. 17 | 18 | All the USBTMC-USB488 features are available in the standard kernel.org releases >= 4.19.0 19 | 20 | The IVI extensions have been incorporated into kernel releases >= 4.20.0 21 | 22 | For details on the IVI extensions please see Guido Kiener's [repo](https://github.com/GuidoKiener/linux-usbtmc) 23 | 24 | ## Installation 25 | 26 | Prerequisite: You need a prebuilt kernel with the configuration and 27 | kernel header files that were used to build it. Most distros have a 28 | "kernel headers" package for this 29 | 30 | To obtain the driver source files either clone the repo with 31 | `git clone https://github.com/dpenkler/linux-usbtmc.git linux-usbtmc` 32 | or download the zip file and extract the zip file to a directory linux-usbtmc 33 | 34 | To build the driver simply run `make` in the directory containing the 35 | driver source code (linux-usbtmc/ or linux-usbtmc-master/). 36 | 37 | To install the driver run `make install` as root. 38 | 39 | To load the driver execute `rmmod usbtmc; insmod usbtmc.ko` as root. 40 | 41 | Enable debug messages with insmod usbtmc.ko dyndbg=+p and use dmesg to see debug output. 42 | 43 | To compile your instrument control program ensure that it includes the 44 | tmc.h file from this repo. An example test program for an 45 | Agilent/Keysight scope is also provided. See the file ttmc.c 46 | To build the provided program run `make ttmc` 47 | 48 | To clean the directory of build files run `make clean` 49 | 50 | To run usbtmc applications as non-root, insert a file e.g. /etc/udev/rules.d/99-usbtmc.rules with the content: 51 | 52 | `KERNEL=="usbtmc[0-9]*", MODE="0660", GROUP="usbtmc"` 53 | 54 | and add yourself to the usbtmc group 55 | 56 | `sudo usermod -G usbtmc -a LOGIN` 57 | 58 | where LOGIN is your username. 59 | 60 | ## Features 61 | 62 | The new features supported by this driver are based on the 63 | specifications contained in the following document from the USB 64 | Implementers Forum, Inc. 65 | 66 | Universal Serial Bus 67 | Test and Measurement Class, 68 | Subclass USB488 Specification 69 | (USBTMC-USB488) 70 | Revision 1.0 71 | April 14, 2003 72 | 73 | Individual feature descriptions: 74 | 75 | ### ioctl to support the USBTMC-USB488 READ_STATUS_BYTE operation. 76 | 77 | USBTMC488_IOCTL_READ_STB 78 | 79 | When performing a read on an instrument that is executing 80 | a function that runs longer than the USB timeout the instrument may 81 | hang and require a device reset to recover. The READ_STATUS_BYTE 82 | operation always returns even when the instrument is busy, permitting 83 | the application to poll for the appropriate condition without blocking 84 | as would be the case with an "*STB?" query. 85 | 86 | USBTMC488_IOCTL_READ_STB always reads the STB from the device and if 87 | the SRQ condition is asserted in the driver it sets the RQS bit in the 88 | returned STB. 89 | 90 | Note: The READ_STATUS_BYTE ioctl clears the SRQ condition in the 91 | driver but it has no effect on the status byte of the device. 92 | 93 | 94 | ### Support for receiving USBTMC-USB488 SRQ notifications with fasync 95 | 96 | By configuring an instrument's service request enable register various 97 | conditions can be reported via an SRQ notification. When the FASYNC 98 | flag is set on the file descriptor corresponding to the usb connected 99 | instrument a SIGIO signal is sent to the owning process when the 100 | instrument asserts a service request. 101 | 102 | Example 103 | ```C 104 | signal(SIGIO, &srq_handler); /* dummy sample; sigaction( ) is better */ 105 | fcntl(fd, F_SETOWN, getpid( )); 106 | oflags = fcntl(fd, F_GETFL); 107 | if (0 > fcntl(fd, F_SETFL, oflags | FASYNC)) { 108 | perror("fcntl to set fasync failed\n"); 109 | exit(1); 110 | } 111 | ``` 112 | 113 | ### Support for receiving USBTMC-USB488 SRQ notifications via poll/select 114 | 115 | In many situations operations on multiple instruments need to be 116 | synchronized. poll/select provide a convenient way of waiting on a 117 | number of different instruments and other peripherals simultaneously. 118 | When the instrument sends an SRQ notification the fd is notified of an 119 | exceptional condition. To reset the poll/select condition either a 120 | USBTMC488_IOCTL_READ_STB or USBTMC_IOCTL_GET_SRQ_STB must be 121 | performed. 122 | 123 | Example with select() 124 | 125 | ```C 126 | FD_SET(fd,&fdsel[2]); 127 | n = select(fd+1, 128 | (fd_set *)(&fdsel[0]), 129 | (fd_set *)(&fdsel[1]), 130 | (fd_set *)(&fdsel[2]), 131 | NULL); 132 | 133 | if (FD_ISSET(fd,&fdsel[2])) { 134 | ioctl(fd,USBTMC488_IOCTL_READ_STB,&stb) 135 | if (stb & 16) { /* test for message available bit */ 136 | len = read(fd,buf,sizeof(buf)); 137 | /* 138 | process buffer 139 | .... 140 | */ 141 | } 142 | } 143 | ``` 144 | Example with poll() 145 | 146 | ```C 147 | /* Wait for SRQ using poll() */ 148 | void wait_for_srq(int fd) { 149 | struct pollfd pfd; 150 | pfd.fd = fd; 151 | pfd.events = POLLPRI; 152 | poll(&pfd,1,-1); 153 | } 154 | ``` 155 | 156 | ### An ioctl to wait for an SRQ notification 157 | 158 | This ioctl offers an alternative way to wait for a service request notification. 159 | Unlike with poll and select the ioctl does not return when asynchronous operations fail. 160 | 161 | ```C 162 | static int wait_for_srq(int fd, unsigned int timeout) { 163 | return ioctl(fd, USBTMC488_IOCTL_WAIT_SRQ, &timeout); 164 | } 165 | ``` 166 | 167 | The timeout parameter is units of milliseconds. 168 | If timeout is 0 the call returns immediately. 169 | 170 | The ioctl returns 0 when an SRQ is received 171 | else it returns -1 with errno set: 172 | ``` 173 | errno = ETIMEDOUT when timeout (in ms) is elapsed. 174 | errno = ENODEV when file handle is closed or device disconnected 175 | errno = EFAULT when timeout argument address is out of bounds 176 | ``` 177 | 178 | ### USBTMC_IOCTL_GET_SRQ_STB 179 | 180 | This ioctl, instead of requesting the STB from the device, returns the 181 | STB that was sent by the device in the last SRQ message. If no other 182 | SRQ occurs between two successive calls to USBTMC_IOCTL_GET_SRQ_STB an 183 | ENOMSG error is signaled. 184 | 185 | Note: The GET_SRQ_STB ioctl clears the SRQ condition in the driver but 186 | it has no effect on the status byte of the device. 187 | 188 | ### USBTMC_IOCTL_GET_STB 189 | 190 | This ioctl always reads the STB from the device and returns the 191 | unmodified STB in the argument. It does not clear the SRQ condition in 192 | the driver. 193 | 194 | ### New ioctls to enable and disable local controls on an instrument 195 | 196 | These ioctls provides the ability to enable or disable local controls on a device 197 | ```C 198 | static int remote_enable(int fd) { 199 | usigned char enable = 1; 200 | 201 | return ioctl(fd, USBTMC488_IOCTL_REN_CONTROL, &enable); 202 | } 203 | 204 | static int remote_disable(int fd) { 205 | usigned char disable = 0; 206 | 207 | return ioctl(fd, USBTMC488_IOCTL_REN_CONTROL, &disable); 208 | } 209 | ``` 210 | 211 | Enable local lockout: 212 | ```C 213 | ioctl(fd, USBTMC488_IOCTL_LOCAL_LOCKOUT); 214 | ``` 215 | Return to local: 216 | ```C 217 | ioctl(fd, USBTMC488_IOCTL_GOTO_LOCAL); 218 | ``` 219 | 220 | ### ioctl to cause a device to trigger 221 | 222 | This is equivalent to the IEEE 488 GET (Group Execute Trigger) action. 223 | While the "*TRG" command can be sent to perform the same operation, 224 | in some situations an instrument will be busy and unable to process 225 | the command immediately in which case the USBTMC488_IOCTL_TRIGGER can 226 | be used. 227 | 228 | ### Utility ioctl to retrieve USBTMC-USB488 capabilities 229 | 230 | This is a convenience function to obtain an instrument's capabilities 231 | from its file descriptor without having to access sysfs from the user 232 | program. The driver encoded usb488 capability masks are defined in the 233 | tmc.h include file. 234 | 235 | ### Two new module parameters 236 | 237 | ***io_buffer_size*** specifies the size of the buffer in bytes that is 238 | used for usb bulk transfers. The default size is 4096. The minimum 239 | size is 64. Positive values given for this parameter are automatically rounded 240 | down to the nearest multiple of 4. If io_buffer_size is zero the wMaxPacketSize for the IN and OUT bulk endpoints are used. This is needed for some Rigol scopes. 241 | 242 | ***usb_timeout*** specifies the timeout in milliseconds that is used 243 | for usb transfers. The default value is 5000, the minimum value is 100. 244 | 245 | To set the parameters 246 | ``` 247 | insmod usbtmc.ko [io_buffer_size=nnn] [usb_timeout=nnn] 248 | ```` 249 | For example to set the buffer size to 256KB: 250 | ``` 251 | insmod usbtmc.ko io_buffer_size=262144 252 | ``` 253 | 254 | ### ioctl's to set/get the usb timeout value 255 | 256 | Separate ioctl's to set and get the usb timeout value for a device. 257 | By default the timeout is set to 5000 milliseconds unless changed by 258 | the ***usb_timeout*** module parameter. 259 | ```C 260 | unsigned int timeout; 261 | .... 262 | ioctl(fd, USBTMC_IOCTL_SET_TIMEOUT, &timeout) 263 | ```` 264 | will return -1 with error = EINVAL if timeout < 100. 265 | 266 | Example set timeout to 1000 milliseconds 267 | 268 | ```C 269 | unsigned int timeout, oldtimeout; 270 | .... 271 | ioctl(fd,USBTMC_IOCTL_GET_TIMEOUT,&oldtimeout) 272 | timeout = 1000; 273 | ioctl(fd,USBTMC_IOCTL_SET_TIMEOUT,&timeout) 274 | 275 | ``` 276 | ### ioctl to send generic usb control requests 277 | 278 | Allows user programs to send control messages to a device over the 279 | control pipe. 280 | 281 | ### ioctl to control setting EOM bit on write 282 | 283 | Enables or disables setting the EOM bit on write. 284 | By default the EOM bit is set on the last transfer of a write. 285 | 286 | Will return with error EINVAL if eom is not 0 or 1 287 | 288 | Example 289 | 290 | ```C 291 | unsigned char eom; 292 | .... 293 | eom = 0; // disable setting of EOM bit on write 294 | ioctl(fd,USBTMC_IOCTL_EOM_ENABLE,&eom) 295 | 296 | ``` 297 | 298 | ### ioctl to configure term_char and term_char_enable 299 | 300 | Allows enabling/disabling of terminating a read on reception of term_char. 301 | By default term_char_enable is false and term_char is '\n' (0x0a). 302 | 303 | Will return with error EINVAL if term_char_enabled is not 0 or 1 or if 304 | attempting to enable term_char when the device does not support terminating 305 | a read when a byte matches the specified term_char. 306 | 307 | Example 308 | 309 | ```C 310 | struct usbtmc_termchar termc; 311 | .... 312 | termc.term_char_enabled = 1; // enable terminating reads on term_char 313 | termc.term_char = '\n'; 314 | ioctl(fd,USBTMC_IOCTL_CONFIG_TERMCHAR,&termc) 315 | 316 | ``` 317 | 318 | ### ioctl to obtain the termination condition of a read 319 | 320 | Each usbtmc read corresponds to one transfer which can consist 321 | of any number of transactions, thus any length transfer can be 322 | read with one read. 323 | 324 | Some memory constrained devices might send a USBTMC message in multiple 325 | transfers. In order to determine whether the last transfer of a message 326 | has been read the USBTMC_IOCTL_MSG_IN_ATTR ioctl can be called after the 327 | read. 328 | 329 | It returns 0 on success and places the termination bits in the argument. 330 | 331 | Example 332 | 333 | ```C 334 | unsigned char attr; 335 | .... 336 | ioctl(fd, USBTMC_IOCTL_MSG_IN_ATTR,&attr); 337 | // if (!attr) message unterminated 338 | // if (attr & 1) message terminated with EOM 339 | // if (attr & 2) message terminated on termchar 340 | ``` 341 | 342 | ## Issues and enhancement requests 343 | 344 | Use the [Issue](https://github.com/dpenkler/linux-usbtmc/issues) feature in github to post requests for enhancements or bugfixes. 345 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /ttmc.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | ttmc.c 3 | ------ 4 | 5 | Programme to test the linux usbtmc driver against 6 | Keysight Infinivision 2000 X-series Oscilloscopes. 7 | This code also serves as an example for how to use 8 | the driver with other instruments. 9 | 10 | copyright : (C) 2015 by Dave Penkler 11 | email : dpenkler@gmail.com 12 | ***************************************************************************/ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | //#include 27 | #define __user 28 | #include "tmc.h" 29 | 30 | #define NUM_CAPS 9 31 | typedef struct cap_entry { 32 | char * desc; 33 | unsigned int mask; 34 | } cap_entryT; 35 | 36 | /* Driver encoded capability masks */ 37 | 38 | const cap_entryT cap_list[NUM_CAPS] = { 39 | {"TRIGGER ", USBTMC488_CAPABILITY_TRIGGER }, 40 | {"REN_CONTROL ", USBTMC488_CAPABILITY_REN_CONTROL }, 41 | {"GOTO_LOCAL ", USBTMC488_CAPABILITY_GOTO_LOCAL }, 42 | {"LOCAL_LOCKOUT", USBTMC488_CAPABILITY_LOCAL_LOCKOUT }, 43 | {"488_DOT_2 ", USBTMC488_CAPABILITY_488_DOT_2 }, 44 | {"DT1 ", USBTMC488_CAPABILITY_DT1 }, 45 | {"RL1 ", USBTMC488_CAPABILITY_RL1 }, 46 | {"SR1 ", USBTMC488_CAPABILITY_SR1 }, 47 | {"FULL_SCPI ", USBTMC488_CAPABILITY_FULL_SCPI } 48 | }; 49 | 50 | static int fd; 51 | #define MAX_BL 2048 52 | unsigned char buf[MAX_BL]; 53 | 54 | static const char *yesno[] = {"no","yes"}; 55 | 56 | static void show_caps(unsigned char caps) { 57 | int i; 58 | printf("\nUSBTMC-488 Capabilities\n"); 59 | for (i=0;i 1.1) { 330 | printf("Expected timeout of 1s, got %f ret %d wait_for_srq failed\n", 331 | delay, ret); 332 | return 0; 333 | } 334 | sscope("*IDN?\n"); 335 | ret = wait_for_srq(500); 336 | if (!ret) { 337 | stb = get_srq_stb(); 338 | if (stb & STB_MAV) { 339 | rscope(buf,MAX_BL); 340 | } else { 341 | printf("wait_for_srq failed.\n"); 342 | return 0; 343 | } 344 | } else { 345 | printf("Unexpected return %d errno %d wait_for_srq_failed\n", 346 | ret, errno); 347 | return 0; 348 | } 349 | sscope("*IDN?\n"); 350 | ret = wait_for_srq(0xffffffffU); 351 | if (ret) { 352 | printf("Unexpected error, ret %d errno %d wait_for_srq failed\n", ret, errno); 353 | return 0; 354 | } 355 | stb = get_srq_stb(); 356 | if (stb & STB_MAV) { 357 | rscope(buf,MAX_BL); 358 | printf("Wait_for_srq Succeeded\n"); 359 | } else { 360 | printf("wait_for_srq failed.\n"); 361 | return 0; 362 | } 363 | return 1; 364 | } 365 | 366 | static int testSRQ() { 367 | unsigned int stb; 368 | int i; 369 | 370 | /* Test assertion and clearing of driver level SRQ */ 371 | printf("\nTesting SRQ with read_stb\n"); 372 | /* Set Mav mask and clear status */ 373 | setReg(SRE, SRE_MAV); 374 | sscope("*CLS\n"); 375 | printf("get_stb:"); showReg(STB,get_stb()); // clear srq 376 | printf("get_stb:"); showReg(STB,get_stb()); // and again 377 | getTS(); 378 | for (i=0;i<100;i++) { 379 | sscope("*IDN?\n"); 380 | wait_for_srq_poll(); 381 | stb = get_stb(); 382 | if (stb & STB_MAV) { 383 | //showReg(STB,stb); 384 | rscope(buf,MAX_BL); 385 | //printf("Measurement result is: %s\n",buf); 386 | } else { 387 | printf("SRQ testwith read_stb failed.\n"); 388 | return 0; 389 | } 390 | } 391 | printf("SRQ with read_stb done: %11.6f\n" ,getTS()); 392 | printf("\nTesting SRQ with get_srq_stb\n"); 393 | printf("get_stb :"); showReg(STB,get_stb()); 394 | printf("get_stb :"); showReg(STB,get_stb()); 395 | printf("get_srq_stb:"); showReg(STB,get_srq_stb()); 396 | printf("get_srq_stb:"); showReg(STB,get_srq_stb()); 397 | getTS(); 398 | for (i=0;i<100;i++) { 399 | sscope("*IDN?\n"); 400 | wait_for_srq_poll(); 401 | stb = get_srq_stb(); 402 | if (stb & STB_MAV) { 403 | //showReg(STB,stb); 404 | rscope(buf,MAX_BL); 405 | //printf("Measurement result is: %s\n",buf); 406 | } else { 407 | printf("SRQ test with get_srq_stb failed.\n"); 408 | return 0; 409 | } 410 | } 411 | printf("SRQ with get_srq_stb done: %11.6f " ,getTS()); 412 | printf("SRQ test success\n"); 413 | return 1; 414 | } 415 | 416 | int main () { 417 | int rv; 418 | unsigned int tmp,tmp1,ren,timeout; 419 | struct timeval sel_timeout; 420 | unsigned char caps; 421 | int len,n; 422 | unsigned char stb; 423 | char command; 424 | int oflags; 425 | fd_set fdsel[3]; 426 | struct tm *daterec; 427 | time_t now; 428 | int i; 429 | 430 | /* Open file */ 431 | if (0 > (fd = open("/dev/usbtmc0",O_RDWR))) { 432 | perror("failed to open device"); 433 | exit(1); 434 | } 435 | 436 | /* Send device clear */ 437 | if (0 != ioctl(fd,USBTMC_IOCTL_CLEAR)) { 438 | perror("Dev clear ioctl failed"); 439 | exit(1); 440 | } 441 | 442 | /* Send clear status */ 443 | sscope("*CLS\n"); 444 | 445 | /* Send identity query */ 446 | sscope("*IDN?\n"); 447 | 448 | /* Read and print returned identity string */ 449 | rscope(buf,MAX_BL); 450 | printf("*IDN? = %s\n",buf); 451 | 452 | /* set time */ 453 | 454 | now = time(NULL); 455 | daterec = localtime(&now); 456 | snprintf(buf,MAX_BL,"SYST:DATE %d,%d,%d",daterec->tm_year+1900,daterec->tm_mon+1,daterec->tm_mday); 457 | sscope(buf); 458 | snprintf(buf,MAX_BL,"SYST:TIME %d,%d,%d",daterec->tm_hour,daterec->tm_min,daterec->tm_sec); 459 | sscope(buf); 460 | 461 | sscope("DISP:ANN:TEXT \"USBTMC Driver Test\""); 462 | 463 | printf("Testing setting and getting timeout\n"); 464 | 465 | if (0 != ioctl(fd,USBTMC_IOCTL_GET_TIMEOUT,&timeout)) { 466 | perror("Get timeout ioctl failed"); 467 | exit(1); 468 | } 469 | printf("usb default timeout is %ums\n",timeout); 470 | 471 | timeout = 0xffffffffU; 472 | printf("Trying timeout > INT_MAX...\n"); 473 | if (0 != ioctl(fd,USBTMC_IOCTL_SET_TIMEOUT,&timeout)) { 474 | printf("... failed %s\n",strerror(errno)); 475 | } else { 476 | ioctl(fd,USBTMC_IOCTL_GET_TIMEOUT,&timeout); 477 | printf(" timeout is now %ums\n",timeout); 478 | } 479 | 480 | timeout = 0; 481 | printf("Trying timeout 0 ...\n"); 482 | if (0 != ioctl(fd,USBTMC_IOCTL_SET_TIMEOUT,&timeout)) { 483 | printf("... failed %s\n",strerror(errno)); 484 | } else { 485 | ioctl(fd,USBTMC_IOCTL_GET_TIMEOUT,&timeout); 486 | printf(" timeout is now %ums\n",timeout); 487 | } 488 | 489 | timeout = 5000; 490 | printf("Trying timeout 5000 ...\n"); 491 | if (0 != ioctl(fd,USBTMC_IOCTL_SET_TIMEOUT,&timeout)) { 492 | printf("... failed %s\n",strerror(errno)); 493 | } 494 | ioctl(fd,USBTMC_IOCTL_GET_TIMEOUT,&timeout); 495 | printf(" timeout is now %ums\n",timeout); 496 | 497 | /* Get and display instrument capabilities */ 498 | if (0 != ioctl(fd,USBTMC488_IOCTL_GET_CAPS,&caps)) { 499 | perror("get caps ioctl failed"); 500 | exit(1); 501 | } 502 | show_caps(caps); 503 | 504 | // sscope("*RST\n"); 505 | sscope(":WGEN:FUNC SIN;OUTP 1;FREQ 1000;VOLT 0.5\n"); 506 | sscope(":RUN\n"); 507 | sscope(":AUTOSCALE\n"); 508 | 509 | while (1) { 510 | printf("Enter command: [I]nteractive, [T]est, [S]tb, s[R]q, [Q]uit:"); 511 | fflush(stdout); 512 | 513 | len = read(0,buf,MAX_BL); 514 | if (len==0) command = 'q'; 515 | else command = buf[0]; 516 | switch (command) { 517 | 518 | case 'I': 519 | case 'i': 520 | { 521 | int prompt = 1; 522 | int srun = 1; 523 | int esr; 524 | int had_prompt = 0; 525 | /* Report all errors */ 526 | setReg(ESE, ESR_CME | ESR_EXE | ESR_DDE | ESR_QYE); 527 | setReg(SRE, SRE_MAV | SRE_ESB); 528 | showReg(ESE, getReg(ESE)); 529 | showReg(ESR, getReg(ESR)); 530 | showReg(SRE, getReg(SRE)); 531 | showReg(STB, get_stb()); 532 | sscope("*CLS\n"); 533 | printf("Enter interactive mode, send Ctrl-D (EOF) to exit\n"); 534 | while (srun) { 535 | if (prompt) { 536 | printf("Enter string to send: "); 537 | fflush(stdout); 538 | prompt = 0; 539 | had_prompt = 1; 540 | } 541 | 542 | memset(fdsel,0,sizeof(fdsel)); /* zero out select mask */ 543 | get_stb(); // reset SRQ condition 544 | sel_timeout.tv_sec = 0; 545 | sel_timeout.tv_usec = 500000; 546 | FD_SET(0,&fdsel[0]); 547 | FD_SET(fd,&fdsel[2]); 548 | n = select(fd+1, 549 | (fd_set *)(&fdsel[0]), 550 | (fd_set *)(&fdsel[1]), 551 | (fd_set *)(&fdsel[2]), 552 | &sel_timeout); 553 | if (n < 0) { 554 | perror("select\n"); 555 | break; 556 | } 557 | 558 | if (!n && !had_prompt) { 559 | prompt = 1; 560 | continue; 561 | } 562 | 563 | if (FD_ISSET(fd,&fdsel[2])) { 564 | while (STB_MAV & get_stb()) { 565 | if (0 < rscope(buf,MAX_BL)) printf("%s",buf); 566 | else break; 567 | } 568 | prompt = 1; 569 | while (STB_ESB & get_stb()) { 570 | esr = getReg(ESR); 571 | if (esr) { 572 | // showReg(ESR, esr); 573 | sscope(":SYST:ERR?\n"); 574 | rscope(buf,MAX_BL); 575 | printf("Error: %s",buf); 576 | } 577 | } 578 | } 579 | 580 | if (FD_ISSET(0,&fdsel[0])) { 581 | len = read(0,buf,MAX_BL-1); 582 | had_prompt = 0; 583 | if (len > 0) { 584 | buf[len] = 0; 585 | sscope(buf); 586 | } else { 587 | setReg(SRE,0); 588 | printf("\nExit interactive mode\n"); 589 | break; 590 | } 591 | } 592 | } 593 | } 594 | break; 595 | 596 | case 'R': 597 | case 'r': 598 | testSRQ(); 599 | break; 600 | 601 | case 'S': 602 | case 's': 603 | testSTB(); 604 | break; 605 | 606 | case 'T': 607 | case 't': 608 | getTS(); /* initialise time stamp */ 609 | 610 | teom: 611 | { unsigned char eom = 0; 612 | int res; 613 | printf("\nTesting eom\n"); 614 | /* Set OPC mask and clear status */ 615 | sscope("*CLS\n"); 616 | setReg(ESE, ESR_OPC); /* Set ESB in STB on OPC */ 617 | setReg(SRE, SRE_ESB); // enable srq on event status reg 618 | showReg(STB,get_stb()); // clear srq 619 | ioctl(fd,USBTMC_IOCTL_EOM_ENABLE,&eom); 620 | sscope(":MEAS:FREQ?;VR"); 621 | eom = 1; 622 | ioctl(fd,USBTMC_IOCTL_EOM_ENABLE,&eom); 623 | sscope("MS?;VPP? CHAN1;*OPC\n"); 624 | wait_for_srq_poll(); 625 | res = rscope(buf,MAX_BL); 626 | if (res < 0) printf("eom failed\n"); 627 | else printf("eom success, res: %s",buf); 628 | } 629 | 630 | teom_in: 631 | { unsigned char attr=0; 632 | int len; 633 | printf("\nTesting eom on input\n"); 634 | sscope("*CLS\n"); 635 | setReg(ESE, 0); 636 | setReg(SRE, 0); 637 | sscope("*IDN?\n"); 638 | while (!attr) { 639 | len = rscope(buf, 4); 640 | if (len < 0) { 641 | printf("teom_in failed\n"); 642 | break; 643 | } 644 | buf[len] = 0; 645 | printf(buf); 646 | ioctl(fd, USBTMC_IOCTL_MSG_IN_ATTR, &attr); 647 | } 648 | if (len >= 0) printf("teom_in succeeded\n"); 649 | } 650 | 651 | ttermc: 652 | { struct usbtmc_termchar termc; 653 | int part=1; 654 | unsigned char old_termchar; 655 | int res,len,old_termc_enabled; 656 | printf("\nTesting TermChar\n"); 657 | 658 | old_termchar = '\n'; 659 | old_termc_enabled = 0; 660 | printf("Old: TermChar 0x%02x, TermCharEnabled %d\n", 661 | (int)old_termchar,old_termc_enabled); 662 | termc.term_char = ';'; 663 | termc.term_char_enabled = 1; 664 | res = ioctl(fd,USBTMC_IOCTL_CONFIG_TERMCHAR,&termc); 665 | if (res < 0) { 666 | perror("setting termchar config failed"); 667 | goto ttermc_out; 668 | } 669 | printf("New: TermChar 0x%02x, TermCharEnabled %d\n", 670 | (int)termc.term_char,termc.term_char_enabled); 671 | setReg(ESE, ESR_OPC); /* Set ESB in STB on OPC */ 672 | setReg(SRE, SRE_ESB); // enable srq on event status reg 673 | showReg(STB,get_stb()); // clear srq 674 | sscope("*CLS\n"); 675 | sscope(":MEAS:FREQ?;VRMS?;VPP? CHAN1;*OPC\n"); 676 | wait_for_srq_poll(); 677 | while (STB_MAV & get_stb()) { 678 | if (0 < (len = rscope(buf,MAX_BL))) 679 | printf("termc part %d is %s\n",part++, buf); 680 | else break; 681 | } 682 | // Put things back as they were 683 | termc.term_char = old_termchar; 684 | termc.term_char_enabled = old_termc_enabled; 685 | res = ioctl(fd,USBTMC_IOCTL_CONFIG_TERMCHAR,&termc); 686 | if (res < 0) 687 | perror("restore termchar config failed"); 688 | ttermc_out: 689 | printf("TermChar test %s\n",((res >= 0) && (len >= 0)) ? "succeeded":"failed"); 690 | if (part <=2 ) 691 | printf("However expected number of parts to be greater than 1\n"); 692 | } 693 | 694 | tstb: 695 | 696 | testSTB(); 697 | goto tsel; /* scope does not react to ren/llo */ 698 | 699 | tren: 700 | 701 | printf("Testing remote disable, press enter to continue\n"); 702 | wait_for_user(); 703 | ren = 0; 704 | if (0 > ioctl(fd,USBTMC488_IOCTL_REN_CONTROL,&ren)) { 705 | perror("ren clear ioctl failed"); 706 | exit(1); 707 | } 708 | printf("Remote disabled\n"); 709 | sscope("SYSTEM:DSP \"USBTMC_488 driver Test\""); 710 | showReg(STB,get_stb()); // error ? 711 | 712 | printf("Testing Remote enable, press enter to continue\n"); 713 | wait_for_user(); 714 | ren = 1; 715 | if (ioctl(fd,USBTMC488_IOCTL_REN_CONTROL,&ren)) { 716 | perror("ren set ioctl failed"); 717 | exit(1); 718 | } 719 | sscope("SYSTEM:DSP \"USBTMC_488 Remote enabled\""); 720 | 721 | printf("Testing local lockout, press enter to continue\n"); 722 | wait_for_user(); 723 | if (ioctl(fd,USBTMC488_IOCTL_LOCAL_LOCKOUT)) { 724 | perror("llo ioctl failed"); 725 | exit(1); 726 | } 727 | sscope("SYSTEM:DSP \"USBTMC_488 Local locked\""); 728 | 729 | printf("Testing goto local, press enter to continue\n"); 730 | wait_for_user(); 731 | if (ioctl(fd,USBTMC488_IOCTL_GOTO_LOCAL)) { 732 | perror("gtl ioctl failed"); 733 | exit(1); 734 | } 735 | sscope("SYSTEM:DSP \"\""); 736 | sscope("*CLS\n"); 737 | 738 | tsel: 739 | memset(fdsel,0,sizeof(fdsel)); /* zero out select mask */ 740 | printf("\nTesting select\n"); 741 | /* Set OPC mask and clear status */ 742 | sscope("*CLS\n"); 743 | setReg(ESE, ESR_OPC); /* Set ESB in STB on OPC */ 744 | setReg(SRE, SRE_ESB); // enable srq on event status reg 745 | showReg(STB,get_stb()); // clear srq 746 | sscope(":MEAS:FREQ?;VRMS?;VPP? CHAN1;*OPC\n"); 747 | 748 | /* wait here for ESB srq */ 749 | 750 | FD_SET(fd,&fdsel[2]); 751 | n = select(fd+1, 752 | (fd_set *)(&fdsel[0]), 753 | (fd_set *)(&fdsel[1]), 754 | (fd_set *)(&fdsel[2]), 755 | NULL); 756 | if (n <= 0) { 757 | perror("select\n"); 758 | exit(1); 759 | } 760 | 761 | if (FD_ISSET(fd,&fdsel[2])) { 762 | showReg(STB,get_stb()); 763 | rscope(buf,MAX_BL); 764 | printf("Measurement result is: %s\n",buf); 765 | printf("Select success\n"); 766 | } 767 | 768 | async: 769 | printf("\nTesting Fasync notification\n"); 770 | sscope(":TRIG:SOURCE CHAN1\n"); 771 | signal(SIGIO, &srq_handler); /* dummy sample; sigaction( ) is better */ 772 | fcntl(fd, F_SETOWN, getpid( )); 773 | oflags = fcntl(fd, F_GETFL); 774 | if (0 > fcntl(fd, F_SETFL, oflags | FASYNC)) { 775 | perror("fasync fail\n"); 776 | exit(1); 777 | } 778 | sscope(":WAV:POINTS MAX\n"); 779 | sscope(":TIM:MODE MAIN\n"); 780 | sscope(":WAV:SOURCE CHAN1\n"); 781 | /* enable OPC */ 782 | sscope("*CLS\n"); 783 | setReg(ESE, ESR_OPC); /* Set ESB in STB on OPC */ 784 | setReg(SRE, SRE_ESB); // enable srq on event status reg 785 | showReg(STB, get_stb()); // clear srq 786 | flag = 0; 787 | sscope(":DIG CHAN1\n"); 788 | sscope("*OPC\n"); 789 | 790 | if (sleep(5)) { 791 | stb = get_stb(); 792 | showReg(STB,stb); 793 | sscope(":WAV:POINTS?\n"); 794 | rscope(buf,MAX_BL); 795 | printf("Points = %s",buf); 796 | printf("Fasync %s\n",flag?"success":"failed"); 797 | } else printf("Fail\n"); 798 | 799 | trigger: 800 | printf("\nTesting trigger ioctl\n"); 801 | sscope(":TRIG:SOURCE EXT\n"); 802 | sscope("*CLS\n"); // clear all status 803 | setReg(SRE, SRE_TRG); // enable SRQ on trigger 804 | sscope(":DIG CHAN1\n"); 805 | showReg(STB,get_stb()); 806 | // showTER(); 807 | if (ioctl(fd,USBTMC488_IOCTL_TRIGGER)) { 808 | perror("trigger ioctl failed"); 809 | exit(1); 810 | } 811 | 812 | wait_for_srq_poll(); 813 | stb = get_srq_stb(); 814 | showReg(STB, stb); 815 | printf("trigger ioctl test %s\n", (stb & STB_TRG) ? "success" : "failure"); 816 | sscope(":TRIG:SOURCE CHAN1\n"); 817 | 818 | twfsrq: 819 | test_wait_for_srq(); 820 | 821 | sscope("*CLS\n"); 822 | sscope(":RUN\n"); 823 | break; 824 | case 'Q': 825 | case 'q': 826 | sscope("DISP:ANN:TEXT \"\""); 827 | printf("ttmc: /done\n"); 828 | close(fd); 829 | exit(0); 830 | default: printf("%c : unknown command\n",command); 831 | break; 832 | } 833 | } 834 | } 835 | -------------------------------------------------------------------------------- /usbtmc.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0+ 2 | /** 3 | * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver 4 | * 5 | * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany 6 | * Copyright (C) 2008 Novell, Inc. 7 | * Copyright (C) 2008 Greg Kroah-Hartman 8 | * Copyright (C) 2015 Dave Penkler 9 | * Copyright (C) 2018 Guido Kiener 10 | * 11 | */ 12 | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include "tmc.h" 26 | 27 | /* Workaround for Linux kernel < 5.4 'fallthrough'*/ 28 | 29 | #if !defined(fallthrough) 30 | #if __has_attribute(fallthrough) 31 | # define fallthrough attribute((fallthrough)) 32 | #else 33 | # define fallthrough do {} while (0) /* fallthrough */ 34 | #endif 35 | #endif 36 | 37 | /* Increment API VERSION when changing tmc.h with new flags or ioctls 38 | * or when changing a significant behavior of the driver. 39 | */ 40 | #define USBTMC_VERSION "1.6" 41 | #define USBTMC_API_VERSION (3) 42 | #define USBTMC_HEADER_SIZE 12 43 | #define USBTMC_MINOR_BASE 176 44 | 45 | /* Minimum USB timeout (in milliseconds) */ 46 | #define USBTMC_MIN_TIMEOUT 100 47 | /* Default USB timeout (in milliseconds) */ 48 | #define USBTMC_TIMEOUT 5000 49 | 50 | /* Max number of urbs used in write transfers */ 51 | #define MAX_URBS_IN_FLIGHT 16 52 | /* I/O buffer size used in generic read/write functions */ 53 | #define USBTMC_BUFSIZE (4096) 54 | 55 | /* 56 | * Maximum number of read cycles to empty bulk in endpoint during CLEAR and 57 | * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short 58 | * packet is never read. 59 | */ 60 | #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100 61 | 62 | /* Minimum packet size for interrupt IN endpoint */ 63 | #define USBTMC_MIN_INT_IN_PACKET_SIZE 2 // 1 byte ID + 1 byte data 64 | 65 | static unsigned int io_buffer_size = USBTMC_BUFSIZE; 66 | module_param(io_buffer_size, uint, S_IRUGO | S_IWUSR); 67 | MODULE_PARM_DESC(io_buffer_size, "Size of bulk IO buffer in bytes"); 68 | 69 | static unsigned int usb_timeout = USBTMC_TIMEOUT; 70 | module_param(usb_timeout, uint, S_IRUGO | S_IWUSR); 71 | MODULE_PARM_DESC(usb_timeout, "USB timeout in milliseconds"); 72 | 73 | static const struct usb_device_id usbtmc_devices[] = { 74 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), }, 75 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), }, 76 | { 0, } /* terminating entry */ 77 | }; 78 | MODULE_DEVICE_TABLE(usb, usbtmc_devices); 79 | 80 | /* 81 | * This structure is the capabilities for the device 82 | * See section 4.2.1.8 of the USBTMC specification, 83 | * and section 4.2.2 of the USBTMC usb488 subclass 84 | * specification for details. 85 | */ 86 | struct usbtmc_dev_capabilities { 87 | __u8 interface_capabilities; 88 | __u8 device_capabilities; 89 | __u8 usb488_interface_capabilities; 90 | __u8 usb488_device_capabilities; 91 | }; 92 | 93 | /* This structure holds private data for each USBTMC device. One copy is 94 | * allocated for each USBTMC device in the driver's probe function. 95 | */ 96 | struct usbtmc_device_data { 97 | const struct usb_device_id *id; 98 | struct usb_device *usb_dev; 99 | struct usb_interface *intf; 100 | struct list_head file_list; 101 | 102 | unsigned int bulk_in; 103 | unsigned int bulk_out; 104 | 105 | u8 bTag; 106 | u8 bTag_last_write; /* needed for abort */ 107 | u8 bTag_last_read; /* needed for abort */ 108 | 109 | /* packet size of IN bulk ep */ 110 | u16 bin_bsiz; 111 | 112 | /* packet size of OUT bulk ep */ 113 | u16 bout_bsiz; 114 | 115 | /* data for interrupt in endpoint handling */ 116 | u8 bNotify1; 117 | u8 bNotify2; 118 | u16 ifnum; 119 | u8 iin_bTag; 120 | u8 *iin_buffer; 121 | atomic_t iin_data_valid; 122 | unsigned int iin_ep; 123 | int iin_ep_present; 124 | int iin_interval; 125 | struct urb *iin_urb; 126 | u16 iin_wMaxPacketSize; 127 | 128 | /* coalesced usb488_caps from usbtmc_dev_capabilities */ 129 | __u8 usb488_caps; 130 | 131 | bool zombie; /* fd of disconnected device */ 132 | 133 | struct usbtmc_dev_capabilities capabilities; 134 | struct kref kref; 135 | struct mutex io_mutex; /* only one i/o function running at a time */ 136 | wait_queue_head_t waitq; 137 | struct fasync_struct *fasync; 138 | spinlock_t dev_lock; /* lock for file_list */ 139 | }; 140 | #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref) 141 | 142 | /* 143 | * This structure holds private data for each USBTMC file handle. 144 | */ 145 | struct usbtmc_file_data { 146 | struct usbtmc_device_data *data; 147 | struct list_head file_elem; 148 | 149 | u32 timeout; 150 | u8 srq_byte; 151 | atomic_t srq_asserted; 152 | atomic_t closing; 153 | u8 bmTransferAttributes; /* member of DEV_DEP_MSG_IN */ 154 | 155 | u8 eom_val; 156 | u8 term_char; 157 | bool term_char_enabled; 158 | bool auto_abort; 159 | 160 | spinlock_t err_lock; /* lock for errors */ 161 | 162 | struct usb_anchor submitted; 163 | 164 | /* data for generic_write */ 165 | struct semaphore limit_write_sem; 166 | u32 out_transfer_size; 167 | int out_status; 168 | 169 | /* data for generic_read */ 170 | u32 in_transfer_size; 171 | int in_status; 172 | int in_urbs_used; 173 | struct usb_anchor in_anchor; 174 | wait_queue_head_t wait_bulk_in; 175 | }; 176 | 177 | /* Forward declarations */ 178 | static struct usb_driver usbtmc_driver; 179 | static void usbtmc_draw_down(struct usbtmc_file_data *file_data); 180 | 181 | static void usbtmc_delete(struct kref *kref) 182 | { 183 | struct usbtmc_device_data *data = to_usbtmc_data(kref); 184 | 185 | usb_put_dev(data->usb_dev); 186 | kfree(data); 187 | } 188 | 189 | static int usbtmc_open(struct inode *inode, struct file *filp) 190 | { 191 | struct usb_interface *intf; 192 | struct usbtmc_device_data *data; 193 | struct usbtmc_file_data *file_data; 194 | 195 | intf = usb_find_interface(&usbtmc_driver, iminor(inode)); 196 | if (!intf) { 197 | pr_err("can not find device for minor %d", iminor(inode)); 198 | return -ENODEV; 199 | } 200 | 201 | file_data = kzalloc(sizeof(*file_data), GFP_KERNEL); 202 | if (!file_data) 203 | return -ENOMEM; 204 | 205 | spin_lock_init(&file_data->err_lock); 206 | sema_init(&file_data->limit_write_sem, MAX_URBS_IN_FLIGHT); 207 | init_usb_anchor(&file_data->submitted); 208 | init_usb_anchor(&file_data->in_anchor); 209 | init_waitqueue_head(&file_data->wait_bulk_in); 210 | 211 | data = usb_get_intfdata(intf); 212 | /* Protect reference to data from file structure until release */ 213 | kref_get(&data->kref); 214 | 215 | mutex_lock(&data->io_mutex); 216 | file_data->data = data; 217 | 218 | atomic_set(&file_data->closing, 0); 219 | 220 | file_data->timeout = usb_timeout; 221 | file_data->term_char = '\n'; 222 | file_data->term_char_enabled = 0; 223 | file_data->auto_abort = 0; 224 | file_data->eom_val = 1; 225 | 226 | INIT_LIST_HEAD(&file_data->file_elem); 227 | spin_lock_irq(&data->dev_lock); 228 | list_add_tail(&file_data->file_elem, &data->file_list); 229 | spin_unlock_irq(&data->dev_lock); 230 | mutex_unlock(&data->io_mutex); 231 | 232 | /* Store pointer in file structure's private data field */ 233 | filp->private_data = file_data; 234 | 235 | return 0; 236 | } 237 | 238 | /* 239 | * usbtmc_flush - called before file handle is closed 240 | */ 241 | static int usbtmc_flush(struct file *file, fl_owner_t id) 242 | { 243 | struct usbtmc_file_data *file_data; 244 | struct usbtmc_device_data *data; 245 | 246 | file_data = file->private_data; 247 | if (file_data == NULL) 248 | return -ENODEV; 249 | 250 | atomic_set(&file_data->closing, 1); 251 | data = file_data->data; 252 | 253 | /* wait for io to stop */ 254 | mutex_lock(&data->io_mutex); 255 | 256 | usbtmc_draw_down(file_data); 257 | 258 | spin_lock_irq(&file_data->err_lock); 259 | file_data->in_status = 0; 260 | file_data->in_transfer_size = 0; 261 | file_data->in_urbs_used = 0; 262 | file_data->out_status = 0; 263 | file_data->out_transfer_size = 0; 264 | spin_unlock_irq(&file_data->err_lock); 265 | 266 | wake_up_interruptible_all(&data->waitq); 267 | mutex_unlock(&data->io_mutex); 268 | 269 | return 0; 270 | } 271 | 272 | static int usbtmc_release(struct inode *inode, struct file *file) 273 | { 274 | struct usbtmc_file_data *file_data = file->private_data; 275 | 276 | /* prevent IO _AND_ usbtmc_interrupt */ 277 | mutex_lock(&file_data->data->io_mutex); 278 | spin_lock_irq(&file_data->data->dev_lock); 279 | 280 | list_del(&file_data->file_elem); 281 | 282 | spin_unlock_irq(&file_data->data->dev_lock); 283 | mutex_unlock(&file_data->data->io_mutex); 284 | 285 | kref_put(&file_data->data->kref, usbtmc_delete); 286 | file_data->data = NULL; 287 | kfree(file_data); 288 | return 0; 289 | } 290 | 291 | static int usbtmc_ioctl_abort_bulk_in_tag(struct usbtmc_device_data *data, 292 | u8 tag) 293 | { 294 | u8 *buffer; 295 | struct device *dev = &data->intf->dev; 296 | int rv; 297 | int n; 298 | int actual; 299 | 300 | buffer = kmalloc(data->bin_bsiz, GFP_KERNEL); 301 | if (!buffer) 302 | return -ENOMEM; 303 | 304 | rv = usb_control_msg(data->usb_dev, 305 | usb_rcvctrlpipe(data->usb_dev, 0), 306 | USBTMC_REQUEST_INITIATE_ABORT_BULK_IN, 307 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, 308 | tag, data->bulk_in, 309 | buffer, 2, USB_CTRL_GET_TIMEOUT); 310 | 311 | if (rv < 0) { 312 | dev_err(dev, "usb_control_msg returned %d\n", rv); 313 | goto exit; 314 | } 315 | 316 | dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x with tag %02x\n", 317 | buffer[0], buffer[1]); 318 | 319 | if (buffer[0] == USBTMC_STATUS_FAILED) { 320 | /* No transfer in progress and the Bulk-OUT FIFO is empty. */ 321 | rv = 0; 322 | goto exit; 323 | } 324 | 325 | if (buffer[0] == USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS) { 326 | /* The device returns this status if either: 327 | * - There is a transfer in progress, but the specified bTag 328 | * does not match. 329 | * - There is no transfer in progress, but the Bulk-OUT FIFO 330 | * is not empty. 331 | */ 332 | rv = -ENOMSG; 333 | goto exit; 334 | } 335 | 336 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { 337 | dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", 338 | buffer[0]); 339 | rv = -EPERM; 340 | goto exit; 341 | } 342 | 343 | n = 0; 344 | 345 | usbtmc_abort_bulk_in_status: 346 | dev_dbg(dev, "Reading from bulk in EP\n"); 347 | 348 | /* Data must be present. So use low timeout 300 ms */ 349 | actual = 0; 350 | rv = usb_bulk_msg(data->usb_dev, 351 | usb_rcvbulkpipe(data->usb_dev, 352 | data->bulk_in), 353 | buffer, data->bin_bsiz, 354 | &actual, 300); 355 | 356 | print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 16, 1, 357 | buffer, actual, true); 358 | 359 | n++; 360 | 361 | if (rv < 0) { 362 | dev_err(dev, "usb_bulk_msg returned %d\n", rv); 363 | if (rv != -ETIMEDOUT) 364 | goto exit; 365 | } 366 | 367 | if (actual == data->bin_bsiz) 368 | goto usbtmc_abort_bulk_in_status; 369 | 370 | if (n >= USBTMC_MAX_READS_TO_CLEAR_BULK_IN) { 371 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", 372 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); 373 | rv = -EPERM; 374 | goto exit; 375 | } 376 | 377 | rv = usb_control_msg(data->usb_dev, 378 | usb_rcvctrlpipe(data->usb_dev, 0), 379 | USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS, 380 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, 381 | 0, data->bulk_in, buffer, 0x08, 382 | USB_CTRL_GET_TIMEOUT); 383 | 384 | if (rv < 0) { 385 | dev_err(dev, "usb_control_msg returned %d\n", rv); 386 | goto exit; 387 | } 388 | 389 | dev_dbg(dev, "CHECK_ABORT_BULK_IN returned %x\n", buffer[0]); 390 | 391 | if (buffer[0] == USBTMC_STATUS_SUCCESS) { 392 | rv = 0; 393 | goto exit; 394 | } 395 | 396 | if (buffer[0] != USBTMC_STATUS_PENDING) { 397 | dev_err(dev, "CHECK_ABORT_BULK_IN returned %x\n", buffer[0]); 398 | rv = -EPERM; 399 | goto exit; 400 | } 401 | 402 | if ((buffer[1] & 1) > 0) { 403 | /* The device has 1 or more queued packets the Host can read */ 404 | goto usbtmc_abort_bulk_in_status; 405 | } 406 | 407 | /* The Host must send CHECK_ABORT_BULK_IN_STATUS at a later time. */ 408 | rv = -EAGAIN; 409 | exit: 410 | kfree(buffer); 411 | return rv; 412 | } 413 | 414 | static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data) 415 | { 416 | return usbtmc_ioctl_abort_bulk_in_tag(data, data->bTag_last_read); 417 | } 418 | 419 | static int usbtmc_ioctl_abort_bulk_out_tag(struct usbtmc_device_data *data, 420 | u8 tag) 421 | { 422 | struct device *dev = &data->intf->dev; 423 | u8 *buffer; 424 | 425 | int rv; 426 | int n; 427 | 428 | buffer = kmalloc(8, GFP_KERNEL); 429 | if (!buffer) 430 | return -ENOMEM; 431 | 432 | rv = usb_control_msg(data->usb_dev, 433 | usb_rcvctrlpipe(data->usb_dev, 0), 434 | USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT, 435 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, 436 | tag, data->bulk_out, 437 | buffer, 2, USB_CTRL_GET_TIMEOUT); 438 | 439 | if (rv < 0) { 440 | dev_err(dev, "usb_control_msg returned %d\n", rv); 441 | goto exit; 442 | } 443 | 444 | dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]); 445 | 446 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { 447 | dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", 448 | buffer[0]); 449 | rv = -EPERM; 450 | goto exit; 451 | } 452 | 453 | n = 0; 454 | 455 | usbtmc_abort_bulk_out_check_status: 456 | /* do not stress device with subsequent requests */ 457 | msleep(50); 458 | rv = usb_control_msg(data->usb_dev, 459 | usb_rcvctrlpipe(data->usb_dev, 0), 460 | USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS, 461 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, 462 | 0, data->bulk_out, buffer, 0x08, 463 | USB_CTRL_GET_TIMEOUT); 464 | n++; 465 | if (rv < 0) { 466 | dev_err(dev, "usb_control_msg returned %d\n", rv); 467 | goto exit; 468 | } 469 | 470 | dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]); 471 | 472 | if (buffer[0] == USBTMC_STATUS_SUCCESS) 473 | goto usbtmc_abort_bulk_out_clear_halt; 474 | 475 | if ((buffer[0] == USBTMC_STATUS_PENDING) && 476 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)) 477 | goto usbtmc_abort_bulk_out_check_status; 478 | 479 | rv = -EPERM; 480 | goto exit; 481 | 482 | usbtmc_abort_bulk_out_clear_halt: 483 | rv = usb_clear_halt(data->usb_dev, 484 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); 485 | 486 | if (rv < 0) { 487 | dev_err(dev, "usb_control_msg returned %d\n", rv); 488 | goto exit; 489 | } 490 | rv = 0; 491 | 492 | exit: 493 | kfree(buffer); 494 | return rv; 495 | } 496 | 497 | static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data) 498 | { 499 | return usbtmc_ioctl_abort_bulk_out_tag(data, data->bTag_last_write); 500 | } 501 | 502 | static int usbtmc_get_stb(struct usbtmc_file_data *file_data, __u8 *stb) 503 | { 504 | struct usbtmc_device_data *data = file_data->data; 505 | struct device *dev = &data->intf->dev; 506 | u8 *buffer; 507 | u8 tag; 508 | int rv; 509 | long wait_rv; 510 | unsigned long expire; 511 | 512 | dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n", 513 | data->iin_ep_present); 514 | 515 | buffer = kmalloc(8, GFP_KERNEL); 516 | if (!buffer) 517 | return -ENOMEM; 518 | 519 | atomic_set(&data->iin_data_valid, 0); 520 | 521 | rv = usb_control_msg(data->usb_dev, 522 | usb_rcvctrlpipe(data->usb_dev, 0), 523 | USBTMC488_REQUEST_READ_STATUS_BYTE, 524 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 525 | data->iin_bTag, 526 | data->ifnum, 527 | buffer, 0x03, USB_CTRL_GET_TIMEOUT); 528 | if (rv < 0) { 529 | dev_err(dev, "stb usb_control_msg returned %d\n", rv); 530 | goto exit; 531 | } 532 | 533 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { 534 | dev_err(dev, "control status returned %x\n", buffer[0]); 535 | rv = -EIO; 536 | goto exit; 537 | } 538 | 539 | if (data->iin_ep_present) { 540 | expire = msecs_to_jiffies(file_data->timeout); 541 | wait_rv = wait_event_interruptible_timeout( 542 | data->waitq, 543 | atomic_read(&data->iin_data_valid) != 0, 544 | expire); 545 | if (wait_rv < 0) { 546 | dev_dbg(dev, "wait interrupted %ld\n", wait_rv); 547 | rv = wait_rv; 548 | goto exit; 549 | } 550 | 551 | if (wait_rv == 0) { 552 | dev_dbg(dev, "wait timed out\n"); 553 | rv = -ETIMEDOUT; 554 | goto exit; 555 | } 556 | 557 | tag = data->bNotify1 & 0x7f; 558 | if (tag != data->iin_bTag) { 559 | dev_err(dev, "expected bTag %x got %x\n", 560 | data->iin_bTag, tag); 561 | } 562 | 563 | *stb = data->bNotify2; 564 | } else { 565 | *stb = buffer[2]; 566 | } 567 | 568 | dev_dbg(dev, "stb:0x%02x received %d\n", (unsigned int)*stb, rv); 569 | 570 | rv = 0; 571 | 572 | exit: 573 | /* bump interrupt bTag */ 574 | data->iin_bTag += 1; 575 | if (data->iin_bTag > 127) 576 | /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */ 577 | data->iin_bTag = 2; 578 | 579 | kfree(buffer); 580 | return rv; 581 | } 582 | 583 | static int usbtmc488_ioctl_read_stb(struct usbtmc_file_data *file_data, 584 | void __user *arg) 585 | { 586 | int srq_asserted = 0; 587 | __u8 stb; 588 | int rv; 589 | 590 | rv = usbtmc_get_stb(file_data, &stb); 591 | 592 | if (rv < 0) 593 | return rv; 594 | 595 | srq_asserted = atomic_xchg(&file_data->srq_asserted, srq_asserted); 596 | if (srq_asserted) 597 | stb |= 0x40; /* Set RQS bit */ 598 | 599 | rv = put_user(stb, (__u8 __user *)arg); 600 | 601 | return rv; 602 | 603 | } 604 | 605 | static int usbtmc_ioctl_get_srq_stb(struct usbtmc_file_data *file_data, 606 | void __user *arg) 607 | { 608 | struct usbtmc_device_data *data = file_data->data; 609 | struct device *dev = &data->intf->dev; 610 | int srq_asserted = 0; 611 | __u8 stb = 0; 612 | int rv; 613 | 614 | spin_lock_irq(&data->dev_lock); 615 | srq_asserted = atomic_xchg(&file_data->srq_asserted, srq_asserted); 616 | 617 | if (srq_asserted) { 618 | stb = file_data->srq_byte; 619 | spin_unlock_irq(&data->dev_lock); 620 | rv = put_user(stb, (__u8 __user *)arg); 621 | } else { 622 | spin_unlock_irq(&data->dev_lock); 623 | rv = -ENOMSG; 624 | } 625 | 626 | dev_dbg(dev, "stb:0x%02x with srq received %d\n", (unsigned int)stb, rv); 627 | 628 | return rv; 629 | } 630 | 631 | static int usbtmc488_ioctl_wait_srq(struct usbtmc_file_data *file_data, 632 | __u32 __user *arg) 633 | { 634 | struct usbtmc_device_data *data = file_data->data; 635 | struct device *dev = &data->intf->dev; 636 | u32 timeout; 637 | unsigned long expire; 638 | long wait_rv; 639 | 640 | if (!data->iin_ep_present) { 641 | dev_dbg(dev, "no interrupt endpoint present\n"); 642 | return -EFAULT; 643 | } 644 | 645 | if (get_user(timeout, arg)) 646 | return -EFAULT; 647 | 648 | expire = msecs_to_jiffies(timeout); 649 | 650 | mutex_unlock(&data->io_mutex); 651 | 652 | wait_rv = wait_event_interruptible_timeout( 653 | data->waitq, 654 | atomic_read(&file_data->srq_asserted) != 0 || 655 | atomic_read(&file_data->closing), 656 | expire); 657 | 658 | mutex_lock(&data->io_mutex); 659 | 660 | /* Note! disconnect or close could be called in the meantime */ 661 | if (atomic_read(&file_data->closing) || data->zombie) 662 | return -ENODEV; 663 | 664 | if (wait_rv < 0) { 665 | dev_dbg(dev, "%s - wait interrupted %ld\n", __func__, wait_rv); 666 | return wait_rv; 667 | } 668 | 669 | if (wait_rv == 0) { 670 | dev_dbg(dev, "%s - wait timed out\n", __func__); 671 | return -ETIMEDOUT; 672 | } 673 | 674 | dev_dbg(dev, "%s - srq asserted\n", __func__); 675 | return 0; 676 | } 677 | 678 | static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data, 679 | void __user *arg, unsigned int cmd) 680 | { 681 | struct device *dev = &data->intf->dev; 682 | __u8 val; 683 | u8 *buffer; 684 | u16 wValue; 685 | int rv; 686 | 687 | if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE)) 688 | return -EINVAL; 689 | 690 | buffer = kmalloc(8, GFP_KERNEL); 691 | if (!buffer) 692 | return -ENOMEM; 693 | 694 | if (cmd == USBTMC488_REQUEST_REN_CONTROL) { 695 | rv = copy_from_user(&val, arg, sizeof(val)); 696 | if (rv) { 697 | rv = -EFAULT; 698 | goto exit; 699 | } 700 | wValue = val ? 1 : 0; 701 | } else { 702 | wValue = 0; 703 | } 704 | 705 | rv = usb_control_msg(data->usb_dev, 706 | usb_rcvctrlpipe(data->usb_dev, 0), 707 | cmd, 708 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 709 | wValue, 710 | data->ifnum, 711 | buffer, 0x01, USB_CTRL_GET_TIMEOUT); 712 | if (rv < 0) { 713 | dev_err(dev, "simple usb_control_msg failed %d\n", rv); 714 | goto exit; 715 | } else if (rv != 1) { 716 | dev_warn(dev, "simple usb_control_msg returned %d\n", rv); 717 | rv = -EIO; 718 | goto exit; 719 | } 720 | 721 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { 722 | dev_err(dev, "simple control status returned %x\n", buffer[0]); 723 | rv = -EIO; 724 | goto exit; 725 | } 726 | rv = 0; 727 | 728 | exit: 729 | kfree(buffer); 730 | return rv; 731 | } 732 | 733 | /* 734 | * Sends a TRIGGER Bulk-OUT command message 735 | * See the USBTMC-USB488 specification, Table 2. 736 | * 737 | * Also updates bTag_last_write. 738 | */ 739 | static int usbtmc488_ioctl_trigger(struct usbtmc_file_data *file_data) 740 | { 741 | struct usbtmc_device_data *data = file_data->data; 742 | int retval; 743 | u8 *buffer; 744 | int actual; 745 | 746 | buffer = kzalloc(USBTMC_HEADER_SIZE, GFP_KERNEL); 747 | if (!buffer) 748 | return -ENOMEM; 749 | 750 | buffer[0] = 128; 751 | buffer[1] = data->bTag; 752 | buffer[2] = ~data->bTag; 753 | 754 | retval = usb_bulk_msg(data->usb_dev, 755 | usb_sndbulkpipe(data->usb_dev, 756 | data->bulk_out), 757 | buffer, USBTMC_HEADER_SIZE, 758 | &actual, file_data->timeout); 759 | 760 | /* Store bTag (in case we need to abort) */ 761 | data->bTag_last_write = data->bTag; 762 | 763 | /* Increment bTag -- and increment again if zero */ 764 | data->bTag++; 765 | if (!data->bTag) 766 | data->bTag++; 767 | 768 | kfree(buffer); 769 | if (retval < 0) { 770 | dev_err(&data->intf->dev, "%s returned %d\n", 771 | __func__, retval); 772 | return retval; 773 | } 774 | 775 | return 0; 776 | } 777 | 778 | static struct urb *usbtmc_create_urb(size_t io_buffer_size) 779 | { 780 | const size_t bufsize = io_buffer_size; 781 | u8 *dmabuf = NULL; 782 | struct urb *urb = usb_alloc_urb(0, GFP_KERNEL); 783 | 784 | if (!urb) 785 | return NULL; 786 | 787 | dmabuf = kzalloc(bufsize, GFP_KERNEL); 788 | if (!dmabuf) { 789 | usb_free_urb(urb); 790 | return NULL; 791 | } 792 | 793 | urb->transfer_buffer = dmabuf; 794 | urb->transfer_buffer_length = bufsize; 795 | urb->transfer_flags |= URB_FREE_BUFFER; 796 | return urb; 797 | } 798 | 799 | static void usbtmc_read_bulk_cb(struct urb *urb) 800 | { 801 | struct usbtmc_file_data *file_data = urb->context; 802 | int status = urb->status; 803 | unsigned long flags; 804 | 805 | /* sync/async unlink faults aren't errors */ 806 | if (status) { 807 | if (!(/* status == -ENOENT || */ 808 | status == -ECONNRESET || 809 | status == -EREMOTEIO || /* Short packet */ 810 | status == -ESHUTDOWN)) 811 | dev_err(&file_data->data->intf->dev, 812 | "%s - nonzero read bulk status received: %d\n", 813 | __func__, status); 814 | 815 | spin_lock_irqsave(&file_data->err_lock, flags); 816 | if (!file_data->in_status) 817 | file_data->in_status = status; 818 | spin_unlock_irqrestore(&file_data->err_lock, flags); 819 | } 820 | 821 | spin_lock_irqsave(&file_data->err_lock, flags); 822 | file_data->in_transfer_size += urb->actual_length; 823 | dev_dbg(&file_data->data->intf->dev, 824 | "%s - total size: %u current: %d status: %d\n", 825 | __func__, file_data->in_transfer_size, 826 | urb->actual_length, status); 827 | spin_unlock_irqrestore(&file_data->err_lock, flags); 828 | usb_anchor_urb(urb, &file_data->in_anchor); 829 | 830 | wake_up_interruptible(&file_data->wait_bulk_in); 831 | wake_up_interruptible(&file_data->data->waitq); 832 | } 833 | 834 | static inline bool usbtmc_do_transfer(struct usbtmc_file_data *file_data) 835 | { 836 | bool data_or_error; 837 | 838 | spin_lock_irq(&file_data->err_lock); 839 | data_or_error = !usb_anchor_empty(&file_data->in_anchor) 840 | || file_data->in_status; 841 | spin_unlock_irq(&file_data->err_lock); 842 | dev_dbg(&file_data->data->intf->dev, "%s: returns %d\n", __func__, 843 | data_or_error); 844 | return data_or_error; 845 | } 846 | 847 | static ssize_t usbtmc_generic_read(struct usbtmc_file_data *file_data, 848 | void __user *user_buffer, 849 | u32 transfer_size, 850 | u32 *transferred, 851 | u32 flags) 852 | { 853 | struct usbtmc_device_data *data = file_data->data; 854 | struct device *dev = &data->intf->dev; 855 | u32 done = 0; 856 | u32 remaining; 857 | const u32 bufsize = (u32)data->bin_bsiz; 858 | int retval = 0; 859 | u32 max_transfer_size; 860 | unsigned long expire; 861 | int bufcount = 1; 862 | int again = 0; 863 | long wait_rv; 864 | 865 | /* mutex already locked */ 866 | 867 | *transferred = done; 868 | 869 | max_transfer_size = transfer_size; 870 | 871 | if (flags & USBTMC_FLAG_IGNORE_TRAILER) { 872 | /* The device may send extra alignment bytes (up to 873 | * wMaxPacketSize – 1) to avoid sending a zero-length 874 | * packet 875 | */ 876 | remaining = transfer_size; 877 | if ((max_transfer_size % data->bin_bsiz) == 0) 878 | max_transfer_size += (data->bin_bsiz - 1); 879 | } else { 880 | /* round down to bufsize to avoid truncated data left */ 881 | if (max_transfer_size > bufsize) { 882 | max_transfer_size = 883 | roundup(max_transfer_size + 1 - bufsize, 884 | bufsize); 885 | } 886 | remaining = max_transfer_size; 887 | } 888 | 889 | spin_lock_irq(&file_data->err_lock); 890 | 891 | if (file_data->in_status) { 892 | /* return the very first error */ 893 | retval = file_data->in_status; 894 | spin_unlock_irq(&file_data->err_lock); 895 | goto error; 896 | } 897 | 898 | if (flags & USBTMC_FLAG_ASYNC) { 899 | if (usb_anchor_empty(&file_data->in_anchor)) 900 | again = 1; 901 | 902 | if (file_data->in_urbs_used == 0) { 903 | file_data->in_transfer_size = 0; 904 | file_data->in_status = 0; 905 | } 906 | } else { 907 | file_data->in_transfer_size = 0; 908 | file_data->in_status = 0; 909 | } 910 | 911 | if (max_transfer_size == 0) { 912 | bufcount = 0; 913 | } else { 914 | bufcount = roundup(max_transfer_size, bufsize) / bufsize; 915 | if (bufcount > file_data->in_urbs_used) 916 | bufcount -= file_data->in_urbs_used; 917 | else 918 | bufcount = 0; 919 | 920 | if (bufcount + file_data->in_urbs_used > MAX_URBS_IN_FLIGHT) { 921 | bufcount = MAX_URBS_IN_FLIGHT - 922 | file_data->in_urbs_used; 923 | } 924 | } 925 | spin_unlock_irq(&file_data->err_lock); 926 | 927 | dev_dbg(dev, "%s: requested=%u flags=0x%X size=%u bufs=%d used=%d\n", 928 | __func__, transfer_size, flags, 929 | max_transfer_size, bufcount, file_data->in_urbs_used); 930 | 931 | while (bufcount > 0) { 932 | u8 *dmabuf = NULL; 933 | struct urb *urb = usbtmc_create_urb(data->bin_bsiz); 934 | 935 | if (!urb) { 936 | retval = -ENOMEM; 937 | goto error; 938 | } 939 | 940 | dmabuf = urb->transfer_buffer; 941 | 942 | usb_fill_bulk_urb(urb, data->usb_dev, 943 | usb_rcvbulkpipe(data->usb_dev, data->bulk_in), 944 | dmabuf, bufsize, 945 | usbtmc_read_bulk_cb, file_data); 946 | 947 | usb_anchor_urb(urb, &file_data->submitted); 948 | retval = usb_submit_urb(urb, GFP_KERNEL); 949 | /* urb is anchored. We can release our reference. */ 950 | usb_free_urb(urb); 951 | if (unlikely(retval)) { 952 | usb_unanchor_urb(urb); 953 | goto error; 954 | } 955 | file_data->in_urbs_used++; 956 | bufcount--; 957 | } 958 | 959 | if (again) { 960 | dev_dbg(dev, "%s: ret=again\n", __func__); 961 | return -EAGAIN; 962 | } 963 | 964 | if (user_buffer == NULL) 965 | return -EINVAL; 966 | 967 | expire = msecs_to_jiffies(file_data->timeout); 968 | 969 | while (max_transfer_size > 0) { 970 | u32 this_part; 971 | struct urb *urb = NULL; 972 | 973 | if (!(flags & USBTMC_FLAG_ASYNC)) { 974 | dev_dbg(dev, "%s: before wait time %lu\n", 975 | __func__, expire); 976 | wait_rv = wait_event_interruptible_timeout( 977 | file_data->wait_bulk_in, 978 | usbtmc_do_transfer(file_data), 979 | expire); 980 | 981 | dev_dbg(dev, "%s: wait returned %ld\n", 982 | __func__, wait_rv); 983 | 984 | if (wait_rv < 0) { 985 | retval = wait_rv; 986 | goto error; 987 | } 988 | 989 | if (wait_rv == 0) { 990 | retval = -ETIMEDOUT; 991 | goto error; 992 | } 993 | } 994 | 995 | urb = usb_get_from_anchor(&file_data->in_anchor); 996 | if (!urb) { 997 | if (!(flags & USBTMC_FLAG_ASYNC)) { 998 | /* synchronous case: must not happen */ 999 | retval = -EFAULT; 1000 | goto error; 1001 | } 1002 | 1003 | /* asynchronous case: ready, do not block or wait */ 1004 | *transferred = done; 1005 | dev_dbg(dev, "%s: (async) done=%u ret=0\n", 1006 | __func__, done); 1007 | return 0; 1008 | } 1009 | 1010 | file_data->in_urbs_used--; 1011 | 1012 | if (max_transfer_size > urb->actual_length) 1013 | max_transfer_size -= urb->actual_length; 1014 | else 1015 | max_transfer_size = 0; 1016 | 1017 | if (remaining > urb->actual_length) 1018 | this_part = urb->actual_length; 1019 | else 1020 | this_part = remaining; 1021 | 1022 | print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 16, 1, 1023 | urb->transfer_buffer, urb->actual_length, true); 1024 | 1025 | if (copy_to_user(user_buffer + done, 1026 | urb->transfer_buffer, this_part)) { 1027 | usb_free_urb(urb); 1028 | retval = -EFAULT; 1029 | goto error; 1030 | } 1031 | 1032 | remaining -= this_part; 1033 | done += this_part; 1034 | 1035 | spin_lock_irq(&file_data->err_lock); 1036 | if (urb->status) { 1037 | /* return the very first error */ 1038 | retval = file_data->in_status; 1039 | spin_unlock_irq(&file_data->err_lock); 1040 | usb_free_urb(urb); 1041 | goto error; 1042 | } 1043 | spin_unlock_irq(&file_data->err_lock); 1044 | 1045 | if (urb->actual_length < bufsize) { 1046 | /* short packet or ZLP received => ready */ 1047 | usb_free_urb(urb); 1048 | retval = 1; 1049 | break; 1050 | } 1051 | 1052 | if (!(flags & USBTMC_FLAG_ASYNC) && 1053 | max_transfer_size > (bufsize * file_data->in_urbs_used)) { 1054 | /* resubmit, since other buffers still not enough */ 1055 | usb_anchor_urb(urb, &file_data->submitted); 1056 | retval = usb_submit_urb(urb, GFP_KERNEL); 1057 | if (unlikely(retval)) { 1058 | usb_unanchor_urb(urb); 1059 | usb_free_urb(urb); 1060 | goto error; 1061 | } 1062 | file_data->in_urbs_used++; 1063 | } 1064 | usb_free_urb(urb); 1065 | retval = 0; 1066 | } 1067 | 1068 | error: 1069 | *transferred = done; 1070 | 1071 | dev_dbg(dev, "%s: before kill\n", __func__); 1072 | /* Attention: killing urbs can take long time (2 ms) */ 1073 | usb_kill_anchored_urbs(&file_data->submitted); 1074 | dev_dbg(dev, "%s: after kill\n", __func__); 1075 | usb_scuttle_anchored_urbs(&file_data->in_anchor); 1076 | file_data->in_urbs_used = 0; 1077 | file_data->in_status = 0; /* no spinlock needed here */ 1078 | dev_dbg(dev, "%s: done=%u ret=%d\n", __func__, done, retval); 1079 | 1080 | return retval; 1081 | } 1082 | 1083 | struct compat_message { 1084 | __u32 transfer_size; 1085 | __u32 transferred; 1086 | __u32 flags; 1087 | __u32 message __attribute__ ((aligned (8))); 1088 | } __attribute__ ((packed)); 1089 | 1090 | static ssize_t usbtmc_ioctl_generic_read(struct usbtmc_file_data *file_data, 1091 | void __user *arg) 1092 | { 1093 | struct usbtmc_message msg; 1094 | struct compat_message *m = (struct compat_message *)&msg; 1095 | ssize_t retval = 0; 1096 | 1097 | /* mutex already locked */ 1098 | 1099 | if (copy_from_user(&msg, arg, sizeof(struct usbtmc_message))) 1100 | return -EFAULT; 1101 | 1102 | if (in_compat_syscall()) 1103 | msg.message = compat_ptr((compat_uptr_t)m->message); 1104 | 1105 | retval = usbtmc_generic_read(file_data, msg.message, 1106 | msg.transfer_size, &msg.transferred, 1107 | msg.flags); 1108 | 1109 | if (put_user(msg.transferred, 1110 | &((struct usbtmc_message __user *)arg)->transferred)) 1111 | return -EFAULT; 1112 | 1113 | return retval; 1114 | } 1115 | 1116 | static void usbtmc_write_bulk_cb(struct urb *urb) 1117 | { 1118 | struct usbtmc_file_data *file_data = urb->context; 1119 | int wakeup = 0; 1120 | unsigned long flags; 1121 | 1122 | spin_lock_irqsave(&file_data->err_lock, flags); 1123 | file_data->out_transfer_size += urb->actual_length; 1124 | 1125 | /* sync/async unlink faults aren't errors */ 1126 | if (urb->status) { 1127 | if (!(urb->status == -ENOENT || 1128 | urb->status == -ECONNRESET || 1129 | urb->status == -ESHUTDOWN)) 1130 | dev_err(&file_data->data->intf->dev, 1131 | "%s - nonzero write bulk status received: %d\n", 1132 | __func__, urb->status); 1133 | 1134 | if (!file_data->out_status) { 1135 | file_data->out_status = urb->status; 1136 | wakeup = 1; 1137 | } 1138 | } 1139 | spin_unlock_irqrestore(&file_data->err_lock, flags); 1140 | 1141 | dev_dbg(&file_data->data->intf->dev, 1142 | "%s - write bulk total size: %u\n", 1143 | __func__, file_data->out_transfer_size); 1144 | 1145 | up(&file_data->limit_write_sem); 1146 | if (usb_anchor_empty(&file_data->submitted) || wakeup) 1147 | wake_up_interruptible(&file_data->data->waitq); 1148 | } 1149 | 1150 | static ssize_t usbtmc_generic_write(struct usbtmc_file_data *file_data, 1151 | const void __user *user_buffer, 1152 | u32 transfer_size, 1153 | u32 *transferred, 1154 | u32 flags) 1155 | { 1156 | struct usbtmc_device_data *data = file_data->data; 1157 | struct device *dev = &data->intf->dev; 1158 | u32 done = 0; 1159 | u32 remaining; 1160 | unsigned long expire; 1161 | const u32 bufsize = data->bout_bsiz; 1162 | struct urb *urb = NULL; 1163 | int retval = 0; 1164 | u32 timeout; 1165 | 1166 | *transferred = 0; 1167 | 1168 | dev_dbg(dev, "%s: size=%u flags=0x%X sema=%u\n", 1169 | __func__, transfer_size, flags, 1170 | file_data->limit_write_sem.count); 1171 | 1172 | if (flags & USBTMC_FLAG_APPEND) { 1173 | spin_lock_irq(&file_data->err_lock); 1174 | retval = file_data->out_status; 1175 | spin_unlock_irq(&file_data->err_lock); 1176 | if (retval < 0) 1177 | return retval; 1178 | } else { 1179 | spin_lock_irq(&file_data->err_lock); 1180 | file_data->out_transfer_size = 0; 1181 | file_data->out_status = 0; 1182 | spin_unlock_irq(&file_data->err_lock); 1183 | } 1184 | 1185 | remaining = transfer_size; 1186 | if (remaining > INT_MAX) 1187 | remaining = INT_MAX; 1188 | 1189 | timeout = file_data->timeout; 1190 | expire = msecs_to_jiffies(timeout); 1191 | 1192 | while (remaining > 0) { 1193 | u32 this_part, aligned; 1194 | u8 *buffer = NULL; 1195 | 1196 | if (flags & USBTMC_FLAG_ASYNC) { 1197 | if (down_trylock(&file_data->limit_write_sem)) { 1198 | retval = (done)?(0):(-EAGAIN); 1199 | goto exit; 1200 | } 1201 | } else { 1202 | retval = down_timeout(&file_data->limit_write_sem, 1203 | expire); 1204 | if (retval < 0) { 1205 | retval = -ETIMEDOUT; 1206 | goto error; 1207 | } 1208 | } 1209 | 1210 | spin_lock_irq(&file_data->err_lock); 1211 | retval = file_data->out_status; 1212 | spin_unlock_irq(&file_data->err_lock); 1213 | if (retval < 0) { 1214 | up(&file_data->limit_write_sem); 1215 | goto error; 1216 | } 1217 | 1218 | /* prepare next urb to send */ 1219 | urb = usbtmc_create_urb(data->bout_bsiz); 1220 | if (!urb) { 1221 | retval = -ENOMEM; 1222 | up(&file_data->limit_write_sem); 1223 | goto error; 1224 | } 1225 | buffer = urb->transfer_buffer; 1226 | 1227 | if (remaining > bufsize) 1228 | this_part = bufsize; 1229 | else 1230 | this_part = remaining; 1231 | 1232 | if (copy_from_user(buffer, user_buffer + done, this_part)) { 1233 | retval = -EFAULT; 1234 | up(&file_data->limit_write_sem); 1235 | goto error; 1236 | } 1237 | 1238 | print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 1239 | 16, 1, buffer, this_part, true); 1240 | 1241 | /* fill bulk with 32 bit alignment to meet USBTMC specification 1242 | * (size + 3 & ~3) rounds up and simplifies user code 1243 | */ 1244 | aligned = (this_part + 3) & ~3; 1245 | dev_dbg(dev, "write(size:%u align:%u done:%u)\n", 1246 | (unsigned int)this_part, 1247 | (unsigned int)aligned, 1248 | (unsigned int)done); 1249 | 1250 | usb_fill_bulk_urb(urb, data->usb_dev, 1251 | usb_sndbulkpipe(data->usb_dev, data->bulk_out), 1252 | urb->transfer_buffer, aligned, 1253 | usbtmc_write_bulk_cb, file_data); 1254 | 1255 | usb_anchor_urb(urb, &file_data->submitted); 1256 | retval = usb_submit_urb(urb, GFP_KERNEL); 1257 | if (unlikely(retval)) { 1258 | usb_unanchor_urb(urb); 1259 | up(&file_data->limit_write_sem); 1260 | goto error; 1261 | } 1262 | 1263 | usb_free_urb(urb); 1264 | urb = NULL; /* urb will be finally released by usb driver */ 1265 | 1266 | remaining -= this_part; 1267 | done += this_part; 1268 | } 1269 | 1270 | /* All urbs are on the fly */ 1271 | if (!(flags & USBTMC_FLAG_ASYNC)) { 1272 | if (!usb_wait_anchor_empty_timeout(&file_data->submitted, 1273 | timeout)) { 1274 | retval = -ETIMEDOUT; 1275 | goto error; 1276 | } 1277 | } 1278 | 1279 | retval = 0; 1280 | goto exit; 1281 | 1282 | error: 1283 | usb_kill_anchored_urbs(&file_data->submitted); 1284 | exit: 1285 | usb_free_urb(urb); 1286 | 1287 | spin_lock_irq(&file_data->err_lock); 1288 | if (!(flags & USBTMC_FLAG_ASYNC)) 1289 | done = file_data->out_transfer_size; 1290 | if (!retval && file_data->out_status) 1291 | retval = file_data->out_status; 1292 | spin_unlock_irq(&file_data->err_lock); 1293 | 1294 | *transferred = done; 1295 | 1296 | dev_dbg(dev, "%s: done=%u, retval=%d, urbstat=%d\n", 1297 | __func__, done, retval, file_data->out_status); 1298 | 1299 | return retval; 1300 | } 1301 | 1302 | static ssize_t usbtmc_ioctl_generic_write(struct usbtmc_file_data *file_data, 1303 | void __user *arg) 1304 | { 1305 | struct usbtmc_message msg; 1306 | struct compat_message *m = (struct compat_message *)&msg; 1307 | ssize_t retval = 0; 1308 | 1309 | /* mutex already locked */ 1310 | 1311 | if (copy_from_user(&msg, arg, sizeof(struct usbtmc_message))) 1312 | return -EFAULT; 1313 | 1314 | if (in_compat_syscall()) 1315 | msg.message = compat_ptr((compat_uptr_t)m->message); 1316 | 1317 | retval = usbtmc_generic_write(file_data, msg.message, 1318 | msg.transfer_size, &msg.transferred, 1319 | msg.flags); 1320 | 1321 | if (put_user(msg.transferred, 1322 | &((struct usbtmc_message __user *)arg)->transferred)) 1323 | return -EFAULT; 1324 | 1325 | return retval; 1326 | } 1327 | 1328 | /* 1329 | * Get the generic write result 1330 | */ 1331 | static ssize_t usbtmc_ioctl_write_result(struct usbtmc_file_data *file_data, 1332 | void __user *arg) 1333 | { 1334 | u32 transferred; 1335 | int retval; 1336 | 1337 | spin_lock_irq(&file_data->err_lock); 1338 | transferred = file_data->out_transfer_size; 1339 | retval = file_data->out_status; 1340 | spin_unlock_irq(&file_data->err_lock); 1341 | 1342 | if (put_user(transferred, (__u32 __user *)arg)) 1343 | return -EFAULT; 1344 | 1345 | return retval; 1346 | } 1347 | 1348 | /* 1349 | * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-OUT endpoint. 1350 | * @transfer_size: number of bytes to request from the device. 1351 | * 1352 | * See the USBTMC specification, Table 4. 1353 | * 1354 | * Also updates bTag_last_write. 1355 | */ 1356 | static int send_request_dev_dep_msg_in(struct usbtmc_file_data *file_data, 1357 | u32 transfer_size) 1358 | { 1359 | struct usbtmc_device_data *data = file_data->data; 1360 | int retval; 1361 | u8 *buffer; 1362 | int actual; 1363 | 1364 | buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL); 1365 | if (!buffer) 1366 | return -ENOMEM; 1367 | /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message 1368 | * Refer to class specs for details 1369 | */ 1370 | buffer[0] = 2; 1371 | buffer[1] = data->bTag; 1372 | buffer[2] = ~data->bTag; 1373 | buffer[3] = 0; /* Reserved */ 1374 | buffer[4] = transfer_size >> 0; 1375 | buffer[5] = transfer_size >> 8; 1376 | buffer[6] = transfer_size >> 16; 1377 | buffer[7] = transfer_size >> 24; 1378 | buffer[8] = file_data->term_char_enabled * 2; 1379 | /* Use term character? */ 1380 | buffer[9] = file_data->term_char; 1381 | buffer[10] = 0; /* Reserved */ 1382 | buffer[11] = 0; /* Reserved */ 1383 | 1384 | /* Send bulk URB */ 1385 | retval = usb_bulk_msg(data->usb_dev, 1386 | usb_sndbulkpipe(data->usb_dev, 1387 | data->bulk_out), 1388 | buffer, USBTMC_HEADER_SIZE, 1389 | &actual, file_data->timeout); 1390 | 1391 | /* Store bTag (in case we need to abort) */ 1392 | data->bTag_last_write = data->bTag; 1393 | 1394 | /* Increment bTag -- and increment again if zero */ 1395 | data->bTag++; 1396 | if (!data->bTag) 1397 | data->bTag++; 1398 | 1399 | kfree(buffer); 1400 | if (retval < 0) 1401 | dev_err(&data->intf->dev, "%s returned %d\n", 1402 | __func__, retval); 1403 | 1404 | return retval; 1405 | } 1406 | 1407 | static ssize_t usbtmc_read(struct file *filp, char __user *buf, 1408 | size_t count, loff_t *f_pos) 1409 | { 1410 | struct usbtmc_file_data *file_data = filp->private_data; 1411 | struct usbtmc_device_data *data = file_data->data; 1412 | struct device *dev = &data->intf->dev;; 1413 | const u32 bufsize = (u32)data->bin_bsiz; 1414 | u32 n_characters; 1415 | u8 *buffer; 1416 | int actual; 1417 | u32 done = 0; 1418 | u32 remaining; 1419 | int retval; 1420 | 1421 | buffer = kmalloc(bufsize, GFP_KERNEL); 1422 | if (!buffer) 1423 | return -ENOMEM; 1424 | 1425 | mutex_lock(&data->io_mutex); 1426 | if (data->zombie) { 1427 | retval = -ENODEV; 1428 | goto exit; 1429 | } 1430 | 1431 | if (count > INT_MAX) 1432 | count = INT_MAX; 1433 | 1434 | dev_dbg(dev, "%s(count:%zu)\n", __func__, count); 1435 | 1436 | retval = send_request_dev_dep_msg_in(file_data, count); 1437 | 1438 | if (retval < 0) { 1439 | if (file_data->auto_abort) 1440 | usbtmc_ioctl_abort_bulk_out(data); 1441 | goto exit; 1442 | } 1443 | 1444 | /* Loop until we have fetched everything we requested */ 1445 | remaining = count; 1446 | actual = 0; 1447 | 1448 | /* Send bulk URB */ 1449 | retval = usb_bulk_msg(data->usb_dev, 1450 | usb_rcvbulkpipe(data->usb_dev, 1451 | data->bulk_in), 1452 | buffer, bufsize, &actual, 1453 | file_data->timeout); 1454 | 1455 | dev_dbg(dev, "%s: bulk_msg retval(%u), actual(%d)\n", 1456 | __func__, retval, actual); 1457 | 1458 | /* Store bTag (in case we need to abort) */ 1459 | data->bTag_last_read = data->bTag; 1460 | 1461 | if (retval < 0) { 1462 | if (file_data->auto_abort) 1463 | usbtmc_ioctl_abort_bulk_in(data); 1464 | goto exit; 1465 | } 1466 | 1467 | /* Sanity checks for the header */ 1468 | if (actual < USBTMC_HEADER_SIZE) { 1469 | dev_err(dev, "Device sent too small first packet: %u < %u\n", 1470 | actual, USBTMC_HEADER_SIZE); 1471 | if (file_data->auto_abort) 1472 | usbtmc_ioctl_abort_bulk_in(data); 1473 | goto exit; 1474 | } 1475 | 1476 | if (buffer[0] != 2) { 1477 | dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", 1478 | buffer[0]); 1479 | if (file_data->auto_abort) 1480 | usbtmc_ioctl_abort_bulk_in(data); 1481 | goto exit; 1482 | } 1483 | 1484 | if (buffer[1] != data->bTag_last_write) { 1485 | dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", 1486 | buffer[1], data->bTag_last_write); 1487 | if (file_data->auto_abort) 1488 | usbtmc_ioctl_abort_bulk_in(data); 1489 | goto exit; 1490 | } 1491 | 1492 | /* How many characters did the instrument send? */ 1493 | n_characters = buffer[4] + 1494 | (buffer[5] << 8) + 1495 | (buffer[6] << 16) + 1496 | (buffer[7] << 24); 1497 | 1498 | file_data->bmTransferAttributes = buffer[8]; 1499 | 1500 | dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", 1501 | n_characters, buffer[8]); 1502 | 1503 | if (n_characters > remaining) { 1504 | dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", 1505 | n_characters, count); 1506 | if (file_data->auto_abort) 1507 | usbtmc_ioctl_abort_bulk_in(data); 1508 | goto exit; 1509 | } 1510 | 1511 | print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 1512 | 16, 1, buffer, actual, true); 1513 | 1514 | remaining = n_characters; 1515 | 1516 | /* Remove the USBTMC header */ 1517 | actual -= USBTMC_HEADER_SIZE; 1518 | 1519 | /* Remove padding if it exists */ 1520 | if (actual > remaining) 1521 | actual = remaining; 1522 | 1523 | remaining -= actual; 1524 | 1525 | /* Copy buffer to user space */ 1526 | if (copy_to_user(buf, &buffer[USBTMC_HEADER_SIZE], actual)) { 1527 | /* There must have been an addressing problem */ 1528 | retval = -EFAULT; 1529 | goto exit; 1530 | } 1531 | 1532 | if ((actual + USBTMC_HEADER_SIZE) == bufsize) { 1533 | retval = usbtmc_generic_read(file_data, buf + actual, 1534 | remaining, 1535 | &done, 1536 | USBTMC_FLAG_IGNORE_TRAILER); 1537 | if (retval < 0) 1538 | goto exit; 1539 | } 1540 | done += actual; 1541 | 1542 | /* Update file position value */ 1543 | *f_pos = *f_pos + done; 1544 | retval = done; 1545 | 1546 | exit: 1547 | mutex_unlock(&data->io_mutex); 1548 | kfree(buffer); 1549 | return retval; 1550 | } 1551 | 1552 | static ssize_t usbtmc_write(struct file *filp, const char __user *buf, 1553 | size_t count, loff_t *f_pos) 1554 | { 1555 | struct usbtmc_file_data *file_data; 1556 | struct usbtmc_device_data *data; 1557 | struct urb *urb = NULL; 1558 | ssize_t retval = 0; 1559 | u8 *buffer; 1560 | u32 remaining, done; 1561 | u32 transfersize, aligned, buflen; 1562 | 1563 | file_data = filp->private_data; 1564 | data = file_data->data; 1565 | 1566 | mutex_lock(&data->io_mutex); 1567 | 1568 | if (data->zombie) { 1569 | retval = -ENODEV; 1570 | goto exit; 1571 | } 1572 | 1573 | done = 0; 1574 | 1575 | spin_lock_irq(&file_data->err_lock); 1576 | file_data->out_transfer_size = 0; 1577 | file_data->out_status = 0; 1578 | spin_unlock_irq(&file_data->err_lock); 1579 | 1580 | if (!count) 1581 | goto exit; 1582 | 1583 | if (down_trylock(&file_data->limit_write_sem)) { 1584 | /* previous calls were async */ 1585 | retval = -EBUSY; 1586 | goto exit; 1587 | } 1588 | 1589 | urb = usbtmc_create_urb(data->bout_bsiz); 1590 | if (!urb) { 1591 | retval = -ENOMEM; 1592 | up(&file_data->limit_write_sem); 1593 | goto exit; 1594 | } 1595 | 1596 | buffer = urb->transfer_buffer; 1597 | buflen = urb->transfer_buffer_length; 1598 | 1599 | if (count > INT_MAX) { 1600 | transfersize = INT_MAX; 1601 | buffer[8] = 0; 1602 | } else { 1603 | transfersize = count; 1604 | buffer[8] = file_data->eom_val; 1605 | } 1606 | 1607 | /* Setup IO buffer for DEV_DEP_MSG_OUT message */ 1608 | buffer[0] = 1; 1609 | buffer[1] = data->bTag; 1610 | buffer[2] = ~data->bTag; 1611 | buffer[3] = 0; /* Reserved */ 1612 | buffer[4] = transfersize >> 0; 1613 | buffer[5] = transfersize >> 8; 1614 | buffer[6] = transfersize >> 16; 1615 | buffer[7] = transfersize >> 24; 1616 | /* buffer[8] is set above... */ 1617 | buffer[9] = 0; /* Reserved */ 1618 | buffer[10] = 0; /* Reserved */ 1619 | buffer[11] = 0; /* Reserved */ 1620 | 1621 | remaining = transfersize; 1622 | 1623 | if (transfersize + USBTMC_HEADER_SIZE > buflen) { 1624 | transfersize = buflen - USBTMC_HEADER_SIZE; 1625 | aligned = buflen; 1626 | } else { 1627 | aligned = (transfersize + (USBTMC_HEADER_SIZE + 3)) & ~3; 1628 | } 1629 | 1630 | if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf, transfersize)) { 1631 | retval = -EFAULT; 1632 | up(&file_data->limit_write_sem); 1633 | goto exit; 1634 | } 1635 | 1636 | dev_dbg(&data->intf->dev, "%s(size:%u align:%u)\n", __func__, 1637 | (unsigned int)transfersize, (unsigned int)aligned); 1638 | 1639 | print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 1640 | 16, 1, buffer, aligned, true); 1641 | 1642 | usb_fill_bulk_urb(urb, data->usb_dev, 1643 | usb_sndbulkpipe(data->usb_dev, data->bulk_out), 1644 | urb->transfer_buffer, aligned, 1645 | usbtmc_write_bulk_cb, file_data); 1646 | 1647 | usb_anchor_urb(urb, &file_data->submitted); 1648 | retval = usb_submit_urb(urb, GFP_KERNEL); 1649 | if (unlikely(retval)) { 1650 | usb_unanchor_urb(urb); 1651 | up(&file_data->limit_write_sem); 1652 | goto exit; 1653 | } 1654 | 1655 | remaining -= transfersize; 1656 | 1657 | data->bTag_last_write = data->bTag; 1658 | data->bTag++; 1659 | 1660 | if (!data->bTag) 1661 | data->bTag++; 1662 | 1663 | /* call generic_write even when remaining = 0 */ 1664 | retval = usbtmc_generic_write(file_data, buf + transfersize, remaining, 1665 | &done, USBTMC_FLAG_APPEND); 1666 | /* truncate alignment bytes */ 1667 | if (done > remaining) 1668 | done = remaining; 1669 | 1670 | /*add size of first urb*/ 1671 | done += transfersize; 1672 | 1673 | if (retval < 0) { 1674 | usb_kill_anchored_urbs(&file_data->submitted); 1675 | 1676 | dev_err(&data->intf->dev, 1677 | "Unable to send data, error %d\n", (int)retval); 1678 | if (file_data->auto_abort) 1679 | usbtmc_ioctl_abort_bulk_out(data); 1680 | goto exit; 1681 | } 1682 | 1683 | retval = done; 1684 | exit: 1685 | usb_free_urb(urb); 1686 | mutex_unlock(&data->io_mutex); 1687 | return retval; 1688 | } 1689 | 1690 | static int usbtmc_ioctl_clear(struct usbtmc_file_data *file_data) 1691 | { 1692 | struct usbtmc_device_data *data = file_data->data; 1693 | struct device *dev = &data->intf->dev; 1694 | u8 *buffer; 1695 | int rv; 1696 | int n; 1697 | int actual = 0; 1698 | dev = &data->intf->dev; 1699 | 1700 | dev_dbg(dev, "Sending INITIATE_CLEAR request\n"); 1701 | 1702 | buffer = kmalloc(max((u16)2, data->bin_bsiz), GFP_KERNEL); 1703 | if (!buffer) 1704 | return -ENOMEM; 1705 | 1706 | rv = usb_control_msg(data->usb_dev, 1707 | usb_rcvctrlpipe(data->usb_dev, 0), 1708 | USBTMC_REQUEST_INITIATE_CLEAR, 1709 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1710 | 0, 0, buffer, 1, USB_CTRL_GET_TIMEOUT); 1711 | if (rv < 0) { 1712 | dev_err(dev, "usb_control_msg returned %d\n", rv); 1713 | goto exit; 1714 | } 1715 | 1716 | dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); 1717 | 1718 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { 1719 | dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); 1720 | rv = -EPERM; 1721 | goto exit; 1722 | } 1723 | 1724 | n = 0; 1725 | 1726 | usbtmc_clear_check_status: 1727 | 1728 | dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n"); 1729 | 1730 | rv = usb_control_msg(data->usb_dev, 1731 | usb_rcvctrlpipe(data->usb_dev, 0), 1732 | USBTMC_REQUEST_CHECK_CLEAR_STATUS, 1733 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1734 | 0, 0, buffer, 2, USB_CTRL_GET_TIMEOUT); 1735 | if (rv < 0) { 1736 | dev_err(dev, "usb_control_msg returned %d\n", rv); 1737 | goto exit; 1738 | } 1739 | 1740 | dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); 1741 | 1742 | if (buffer[0] == USBTMC_STATUS_SUCCESS) 1743 | goto usbtmc_clear_bulk_out_halt; 1744 | 1745 | if (buffer[0] != USBTMC_STATUS_PENDING) { 1746 | dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); 1747 | rv = -EPERM; 1748 | goto exit; 1749 | } 1750 | 1751 | if ((buffer[1] & 1) != 0) { 1752 | do { 1753 | dev_dbg(dev, "Reading from bulk in EP\n"); 1754 | 1755 | actual = 0; 1756 | rv = usb_bulk_msg(data->usb_dev, 1757 | usb_rcvbulkpipe(data->usb_dev, 1758 | data->bulk_in), 1759 | buffer, data->bin_bsiz, 1760 | &actual, file_data->timeout); 1761 | 1762 | print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE, 1763 | 16, 1, buffer, actual, true); 1764 | 1765 | n++; 1766 | 1767 | if (rv < 0) { 1768 | dev_err(dev, "usb_control_msg returned %d\n", 1769 | rv); 1770 | goto exit; 1771 | } 1772 | } while ((actual == data->bin_bsiz) && 1773 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); 1774 | } else { 1775 | /* do not stress device with subsequent requests */ 1776 | msleep(50); 1777 | n++; 1778 | } 1779 | 1780 | if (n >= USBTMC_MAX_READS_TO_CLEAR_BULK_IN) { 1781 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", 1782 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); 1783 | rv = -EPERM; 1784 | goto exit; 1785 | } 1786 | 1787 | goto usbtmc_clear_check_status; 1788 | 1789 | usbtmc_clear_bulk_out_halt: 1790 | 1791 | rv = usb_clear_halt(data->usb_dev, 1792 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); 1793 | if (rv < 0) { 1794 | dev_err(dev, "usb_clear_halt returned %d\n", rv); 1795 | goto exit; 1796 | } 1797 | rv = 0; 1798 | 1799 | exit: 1800 | kfree(buffer); 1801 | return rv; 1802 | } 1803 | 1804 | static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data) 1805 | { 1806 | int rv; 1807 | 1808 | rv = usb_clear_halt(data->usb_dev, 1809 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); 1810 | 1811 | if (rv < 0) 1812 | dev_err(&data->usb_dev->dev, "%s returned %d\n", __func__, rv); 1813 | return rv; 1814 | } 1815 | 1816 | static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data) 1817 | { 1818 | int rv; 1819 | 1820 | rv = usb_clear_halt(data->usb_dev, 1821 | usb_rcvbulkpipe(data->usb_dev, data->bulk_in)); 1822 | 1823 | if (rv < 0) 1824 | dev_err(&data->usb_dev->dev, "%s returned %d\n", __func__, rv); 1825 | return rv; 1826 | } 1827 | 1828 | static int usbtmc_ioctl_cancel_io(struct usbtmc_file_data *file_data) 1829 | { 1830 | spin_lock_irq(&file_data->err_lock); 1831 | file_data->in_status = -ECANCELED; 1832 | file_data->out_status = -ECANCELED; 1833 | spin_unlock_irq(&file_data->err_lock); 1834 | usb_kill_anchored_urbs(&file_data->submitted); 1835 | return 0; 1836 | } 1837 | 1838 | static int usbtmc_ioctl_cleanup_io(struct usbtmc_file_data *file_data) 1839 | { 1840 | usb_kill_anchored_urbs(&file_data->submitted); 1841 | usb_scuttle_anchored_urbs(&file_data->in_anchor); 1842 | spin_lock_irq(&file_data->err_lock); 1843 | file_data->in_status = 0; 1844 | file_data->in_transfer_size = 0; 1845 | file_data->out_status = 0; 1846 | file_data->out_transfer_size = 0; 1847 | spin_unlock_irq(&file_data->err_lock); 1848 | 1849 | file_data->in_urbs_used = 0; 1850 | return 0; 1851 | } 1852 | 1853 | static int get_capabilities(struct usbtmc_device_data *data) 1854 | { 1855 | struct device *dev = &data->usb_dev->dev; 1856 | char *buffer; 1857 | int rv = 0; 1858 | 1859 | buffer = kmalloc(0x18, GFP_KERNEL); 1860 | if (!buffer) 1861 | return -ENOMEM; 1862 | 1863 | rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0), 1864 | USBTMC_REQUEST_GET_CAPABILITIES, 1865 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1866 | 0, 0, buffer, 0x18, USB_CTRL_GET_TIMEOUT); 1867 | if (rv < 0) { 1868 | dev_err(dev, "usb_control_msg returned %d\n", rv); 1869 | goto err_out; 1870 | } 1871 | 1872 | dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); 1873 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { 1874 | dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); 1875 | rv = -EPERM; 1876 | goto err_out; 1877 | } 1878 | dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]); 1879 | dev_dbg(dev, "Device capabilities are %x\n", buffer[5]); 1880 | dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]); 1881 | dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]); 1882 | 1883 | data->capabilities.interface_capabilities = buffer[4]; 1884 | data->capabilities.device_capabilities = buffer[5]; 1885 | data->capabilities.usb488_interface_capabilities = buffer[14]; 1886 | data->capabilities.usb488_device_capabilities = buffer[15]; 1887 | data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4); 1888 | rv = 0; 1889 | 1890 | err_out: 1891 | kfree(buffer); 1892 | return rv; 1893 | } 1894 | 1895 | #define capability_attribute(name) \ 1896 | static ssize_t name##_show(struct device *dev, \ 1897 | struct device_attribute *attr, char *buf) \ 1898 | { \ 1899 | struct usb_interface *intf = to_usb_interface(dev); \ 1900 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ 1901 | \ 1902 | return sprintf(buf, "%d\n", data->capabilities.name); \ 1903 | } \ 1904 | static DEVICE_ATTR_RO(name) 1905 | 1906 | capability_attribute(interface_capabilities); 1907 | capability_attribute(device_capabilities); 1908 | capability_attribute(usb488_interface_capabilities); 1909 | capability_attribute(usb488_device_capabilities); 1910 | 1911 | static struct attribute *usbtmc_attrs[] = { 1912 | &dev_attr_interface_capabilities.attr, 1913 | &dev_attr_device_capabilities.attr, 1914 | &dev_attr_usb488_interface_capabilities.attr, 1915 | &dev_attr_usb488_device_capabilities.attr, 1916 | NULL, 1917 | }; 1918 | ATTRIBUTE_GROUPS(usbtmc); 1919 | 1920 | static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data) 1921 | { 1922 | struct device *dev = &data->intf->dev; 1923 | u8 *buffer; 1924 | int rv; 1925 | 1926 | buffer = kmalloc(2, GFP_KERNEL); 1927 | if (!buffer) 1928 | return -ENOMEM; 1929 | 1930 | rv = usb_control_msg(data->usb_dev, 1931 | usb_rcvctrlpipe(data->usb_dev, 0), 1932 | USBTMC_REQUEST_INDICATOR_PULSE, 1933 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1934 | 0, 0, buffer, 0x01, USB_CTRL_GET_TIMEOUT); 1935 | 1936 | if (rv < 0) { 1937 | dev_err(dev, "usb_control_msg returned %d\n", rv); 1938 | goto exit; 1939 | } 1940 | 1941 | dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); 1942 | 1943 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { 1944 | dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); 1945 | rv = -EPERM; 1946 | goto exit; 1947 | } 1948 | rv = 0; 1949 | 1950 | exit: 1951 | kfree(buffer); 1952 | return rv; 1953 | } 1954 | 1955 | struct compat_ctrlrequest { 1956 | struct usbtmc_request req; 1957 | u32 data; /* pointer to user space */ 1958 | } __attribute__ ((packed)); 1959 | 1960 | static int usbtmc_ioctl_request(struct usbtmc_device_data *data, 1961 | void __user *arg) 1962 | { 1963 | struct device *dev = &data->intf->dev; 1964 | struct usbtmc_ctrlrequest request; 1965 | struct compat_ctrlrequest *r =(struct compat_ctrlrequest *)&request; 1966 | u8 *buffer = NULL; 1967 | int rv; 1968 | unsigned int is_in, pipe; 1969 | unsigned long res; 1970 | 1971 | res = copy_from_user(&request, arg, sizeof(struct usbtmc_ctrlrequest)); 1972 | if (res) 1973 | return -EFAULT; 1974 | 1975 | if (in_compat_syscall()) 1976 | request.data = compat_ptr((compat_uptr_t)r->data); 1977 | 1978 | if (request.req.wLength > data->bin_bsiz) 1979 | return -EMSGSIZE; 1980 | if (request.req.wLength == 0) /* Length-0 requests are never IN */ 1981 | request.req.bRequestType &= ~USB_DIR_IN; 1982 | 1983 | is_in = request.req.bRequestType & USB_DIR_IN; 1984 | 1985 | if (request.req.wLength) { 1986 | buffer = kmalloc(request.req.wLength, GFP_KERNEL); 1987 | if (!buffer) 1988 | return -ENOMEM; 1989 | 1990 | if (!is_in) { 1991 | /* Send control data to device */ 1992 | res = copy_from_user(buffer, request.data, 1993 | request.req.wLength); 1994 | if (res) { 1995 | rv = -EFAULT; 1996 | goto exit; 1997 | } 1998 | } 1999 | } 2000 | 2001 | if (is_in) 2002 | pipe = usb_rcvctrlpipe(data->usb_dev, 0); 2003 | else 2004 | pipe = usb_sndctrlpipe(data->usb_dev, 0); 2005 | rv = usb_control_msg(data->usb_dev, 2006 | pipe, 2007 | request.req.bRequest, 2008 | request.req.bRequestType, 2009 | request.req.wValue, 2010 | request.req.wIndex, 2011 | buffer, request.req.wLength, USB_CTRL_GET_TIMEOUT); 2012 | 2013 | if (rv < 0) { 2014 | dev_err(dev, "%s failed %d\n", __func__, rv); 2015 | goto exit; 2016 | } 2017 | 2018 | if (rv && is_in) { 2019 | /* Read control data from device */ 2020 | res = copy_to_user(request.data, buffer, rv); 2021 | if (res) 2022 | rv = -EFAULT; 2023 | } 2024 | 2025 | exit: 2026 | kfree(buffer); 2027 | return rv; 2028 | } 2029 | 2030 | /* 2031 | * Get the usb timeout value 2032 | */ 2033 | static int usbtmc_ioctl_get_timeout(struct usbtmc_file_data *file_data, 2034 | void __user *arg) 2035 | { 2036 | u32 timeout; 2037 | 2038 | timeout = file_data->timeout; 2039 | 2040 | return put_user(timeout, (__u32 __user *)arg); 2041 | } 2042 | 2043 | /* 2044 | * Set the usb timeout value 2045 | */ 2046 | static int usbtmc_ioctl_set_timeout(struct usbtmc_file_data *file_data, 2047 | void __user *arg) 2048 | { 2049 | u32 timeout; 2050 | 2051 | if (get_user(timeout, (__u32 __user *)arg)) 2052 | return -EFAULT; 2053 | 2054 | /* Note that timeout = 0 means 2055 | * MAX_SCHEDULE_TIMEOUT in usb_control_msg 2056 | */ 2057 | if (timeout < USBTMC_MIN_TIMEOUT) 2058 | return -EINVAL; 2059 | 2060 | file_data->timeout = timeout; 2061 | 2062 | return 0; 2063 | } 2064 | 2065 | /* 2066 | * enables/disables sending EOM on write 2067 | */ 2068 | static int usbtmc_ioctl_eom_enable(struct usbtmc_file_data *file_data, 2069 | void __user *arg) 2070 | { 2071 | u8 eom_enable; 2072 | 2073 | if (copy_from_user(&eom_enable, arg, sizeof(eom_enable))) 2074 | return -EFAULT; 2075 | 2076 | if (eom_enable > 1) 2077 | return -EINVAL; 2078 | 2079 | file_data->eom_val = eom_enable; 2080 | 2081 | return 0; 2082 | } 2083 | 2084 | /* 2085 | * Configure termination character for read() 2086 | */ 2087 | static int usbtmc_ioctl_config_termc(struct usbtmc_file_data *file_data, 2088 | void __user *arg) 2089 | { 2090 | struct usbtmc_termchar termc; 2091 | 2092 | if (copy_from_user(&termc, arg, sizeof(termc))) 2093 | return -EFAULT; 2094 | 2095 | if ((termc.term_char_enabled > 1) || 2096 | (termc.term_char_enabled && 2097 | !(file_data->data->capabilities.device_capabilities & 1))) 2098 | return -EINVAL; 2099 | 2100 | file_data->term_char = termc.term_char; 2101 | file_data->term_char_enabled = termc.term_char_enabled; 2102 | 2103 | return 0; 2104 | } 2105 | 2106 | static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2107 | { 2108 | struct usbtmc_file_data *file_data; 2109 | struct usbtmc_device_data *data; 2110 | int retval = -EBADRQC; 2111 | __u8 tmp_byte; 2112 | 2113 | file_data = file->private_data; 2114 | data = file_data->data; 2115 | 2116 | mutex_lock(&data->io_mutex); 2117 | if (data->zombie) { 2118 | retval = -ENODEV; 2119 | goto skip_io_on_zombie; 2120 | } 2121 | 2122 | switch (cmd) { 2123 | case USBTMC_IOCTL_CLEAR_OUT_HALT: 2124 | retval = usbtmc_ioctl_clear_out_halt(data); 2125 | break; 2126 | 2127 | case USBTMC_IOCTL_CLEAR_IN_HALT: 2128 | retval = usbtmc_ioctl_clear_in_halt(data); 2129 | break; 2130 | 2131 | case USBTMC_IOCTL_INDICATOR_PULSE: 2132 | retval = usbtmc_ioctl_indicator_pulse(data); 2133 | break; 2134 | 2135 | case USBTMC_IOCTL_CLEAR: 2136 | retval = usbtmc_ioctl_clear(file_data); 2137 | break; 2138 | 2139 | case USBTMC_IOCTL_ABORT_BULK_OUT: 2140 | retval = usbtmc_ioctl_abort_bulk_out(data); 2141 | break; 2142 | 2143 | case USBTMC_IOCTL_ABORT_BULK_IN: 2144 | retval = usbtmc_ioctl_abort_bulk_in(data); 2145 | break; 2146 | 2147 | case USBTMC_IOCTL_CTRL_REQUEST: 2148 | retval = usbtmc_ioctl_request(data, (void __user *)arg); 2149 | break; 2150 | 2151 | case USBTMC_IOCTL_GET_TIMEOUT: 2152 | retval = usbtmc_ioctl_get_timeout(file_data, 2153 | (void __user *)arg); 2154 | break; 2155 | 2156 | case USBTMC_IOCTL_SET_TIMEOUT: 2157 | retval = usbtmc_ioctl_set_timeout(file_data, 2158 | (void __user *)arg); 2159 | break; 2160 | 2161 | case USBTMC_IOCTL_EOM_ENABLE: 2162 | retval = usbtmc_ioctl_eom_enable(file_data, 2163 | (void __user *)arg); 2164 | break; 2165 | 2166 | case USBTMC_IOCTL_CONFIG_TERMCHAR: 2167 | retval = usbtmc_ioctl_config_termc(file_data, 2168 | (void __user *)arg); 2169 | break; 2170 | 2171 | case USBTMC_IOCTL_WRITE: 2172 | retval = usbtmc_ioctl_generic_write(file_data, 2173 | (void __user *)arg); 2174 | break; 2175 | 2176 | case USBTMC_IOCTL_READ: 2177 | retval = usbtmc_ioctl_generic_read(file_data, 2178 | (void __user *)arg); 2179 | break; 2180 | 2181 | case USBTMC_IOCTL_WRITE_RESULT: 2182 | retval = usbtmc_ioctl_write_result(file_data, 2183 | (void __user *)arg); 2184 | break; 2185 | 2186 | case USBTMC_IOCTL_API_VERSION: 2187 | retval = put_user(USBTMC_API_VERSION, 2188 | (__u32 __user *)arg); 2189 | break; 2190 | 2191 | case USBTMC488_IOCTL_GET_CAPS: 2192 | retval = put_user(data->usb488_caps, 2193 | (unsigned char __user *)arg); 2194 | break; 2195 | 2196 | case USBTMC488_IOCTL_READ_STB: 2197 | retval = usbtmc488_ioctl_read_stb(file_data, 2198 | (void __user *)arg); 2199 | break; 2200 | 2201 | case USBTMC488_IOCTL_REN_CONTROL: 2202 | retval = usbtmc488_ioctl_simple(data, (void __user *)arg, 2203 | USBTMC488_REQUEST_REN_CONTROL); 2204 | break; 2205 | 2206 | case USBTMC488_IOCTL_GOTO_LOCAL: 2207 | retval = usbtmc488_ioctl_simple(data, (void __user *)arg, 2208 | USBTMC488_REQUEST_GOTO_LOCAL); 2209 | break; 2210 | 2211 | case USBTMC488_IOCTL_LOCAL_LOCKOUT: 2212 | retval = usbtmc488_ioctl_simple(data, (void __user *)arg, 2213 | USBTMC488_REQUEST_LOCAL_LOCKOUT); 2214 | break; 2215 | 2216 | case USBTMC488_IOCTL_TRIGGER: 2217 | retval = usbtmc488_ioctl_trigger(file_data); 2218 | break; 2219 | 2220 | case USBTMC488_IOCTL_WAIT_SRQ: 2221 | retval = usbtmc488_ioctl_wait_srq(file_data, 2222 | (__u32 __user *)arg); 2223 | break; 2224 | 2225 | case USBTMC_IOCTL_MSG_IN_ATTR: 2226 | retval = put_user(file_data->bmTransferAttributes, 2227 | (__u8 __user *)arg); 2228 | break; 2229 | 2230 | case USBTMC_IOCTL_AUTO_ABORT: 2231 | retval = get_user(tmp_byte, (unsigned char __user *)arg); 2232 | if (retval == 0) 2233 | file_data->auto_abort = !!tmp_byte; 2234 | break; 2235 | 2236 | case USBTMC_IOCTL_GET_STB: 2237 | retval = usbtmc_get_stb(file_data, &tmp_byte); 2238 | if (!retval) 2239 | retval = put_user(tmp_byte, (__u8 __user *)arg); 2240 | break; 2241 | 2242 | case USBTMC_IOCTL_GET_SRQ_STB: 2243 | retval = usbtmc_ioctl_get_srq_stb(file_data, 2244 | (void __user *)arg); 2245 | break; 2246 | 2247 | case USBTMC_IOCTL_CANCEL_IO: 2248 | retval = usbtmc_ioctl_cancel_io(file_data); 2249 | break; 2250 | 2251 | case USBTMC_IOCTL_CLEANUP_IO: 2252 | retval = usbtmc_ioctl_cleanup_io(file_data); 2253 | break; 2254 | } 2255 | 2256 | skip_io_on_zombie: 2257 | mutex_unlock(&data->io_mutex); 2258 | return retval; 2259 | } 2260 | 2261 | static int usbtmc_fasync(int fd, struct file *file, int on) 2262 | { 2263 | struct usbtmc_file_data *file_data = file->private_data; 2264 | 2265 | return fasync_helper(fd, file, on, &file_data->data->fasync); 2266 | } 2267 | 2268 | static __poll_t usbtmc_poll(struct file *file, poll_table *wait) 2269 | { 2270 | struct usbtmc_file_data *file_data = file->private_data; 2271 | struct usbtmc_device_data *data = file_data->data; 2272 | __poll_t mask; 2273 | 2274 | mutex_lock(&data->io_mutex); 2275 | 2276 | if (data->zombie) { 2277 | mask = EPOLLHUP | EPOLLERR; 2278 | goto no_poll; 2279 | } 2280 | 2281 | poll_wait(file, &data->waitq, wait); 2282 | 2283 | /* Note that EPOLLPRI is now assigned to SRQ, and 2284 | * EPOLLIN|EPOLLRDNORM to normal read data. 2285 | */ 2286 | mask = 0; 2287 | if (atomic_read(&file_data->srq_asserted)) 2288 | mask |= EPOLLPRI; 2289 | 2290 | /* Note that the anchor submitted includes all urbs for BULK IN 2291 | * and OUT. So EPOLLOUT is signaled when BULK OUT is empty and 2292 | * all BULK IN urbs are completed and moved to in_anchor. 2293 | */ 2294 | if (usb_anchor_empty(&file_data->submitted)) 2295 | mask |= (EPOLLOUT | EPOLLWRNORM); 2296 | if (!usb_anchor_empty(&file_data->in_anchor)) 2297 | mask |= (EPOLLIN | EPOLLRDNORM); 2298 | 2299 | spin_lock_irq(&file_data->err_lock); 2300 | if (file_data->in_status || file_data->out_status) 2301 | mask |= EPOLLERR; 2302 | spin_unlock_irq(&file_data->err_lock); 2303 | 2304 | dev_dbg(&data->intf->dev, "poll mask = %x\n", mask); 2305 | 2306 | no_poll: 2307 | mutex_unlock(&data->io_mutex); 2308 | return mask; 2309 | } 2310 | 2311 | static const struct file_operations fops = { 2312 | .owner = THIS_MODULE, 2313 | .read = usbtmc_read, 2314 | .write = usbtmc_write, 2315 | .open = usbtmc_open, 2316 | .release = usbtmc_release, 2317 | .flush = usbtmc_flush, 2318 | .unlocked_ioctl = usbtmc_ioctl, 2319 | #ifdef CONFIG_COMPAT 2320 | .compat_ioctl = usbtmc_ioctl, 2321 | #endif 2322 | .fasync = usbtmc_fasync, 2323 | .poll = usbtmc_poll, 2324 | .llseek = default_llseek, 2325 | }; 2326 | 2327 | static struct usb_class_driver usbtmc_class = { 2328 | .name = "usbtmc%d", 2329 | .fops = &fops, 2330 | .minor_base = USBTMC_MINOR_BASE, 2331 | }; 2332 | 2333 | static void usbtmc_interrupt(struct urb *urb) 2334 | { 2335 | struct usbtmc_device_data *data = urb->context; 2336 | struct device *dev = &data->intf->dev; 2337 | int status = urb->status; 2338 | int rv; 2339 | 2340 | dev_dbg(&data->intf->dev, "int status: %d len %d\n", 2341 | status, urb->actual_length); 2342 | 2343 | switch (status) { 2344 | case 0: /* SUCCESS */ 2345 | /* check for valid length */ 2346 | if (urb->actual_length < USBTMC_MIN_INT_IN_PACKET_SIZE) { 2347 | dev_warn(dev, "short interrupt packet: %d bytes, min %d required\n", 2348 | urb->actual_length, 2349 | USBTMC_MIN_INT_IN_PACKET_SIZE); 2350 | goto exit; 2351 | } 2352 | 2353 | /* check for valid STB notification */ 2354 | if (data->iin_buffer[0] > 0x81) { 2355 | data->bNotify1 = data->iin_buffer[0]; 2356 | data->bNotify2 = data->iin_buffer[1]; 2357 | atomic_set(&data->iin_data_valid, 1); 2358 | wake_up_interruptible(&data->waitq); 2359 | goto exit; 2360 | } 2361 | /* check for SRQ notification */ 2362 | if (data->iin_buffer[0] == 0x81) { 2363 | unsigned long flags; 2364 | struct list_head *elem; 2365 | 2366 | if (data->fasync) 2367 | kill_fasync(&data->fasync, 2368 | SIGIO, POLL_PRI); 2369 | 2370 | spin_lock_irqsave(&data->dev_lock, flags); 2371 | list_for_each(elem, &data->file_list) { 2372 | struct usbtmc_file_data *file_data; 2373 | 2374 | file_data = list_entry(elem, 2375 | struct usbtmc_file_data, 2376 | file_elem); 2377 | file_data->srq_byte = data->iin_buffer[1]; 2378 | atomic_set(&file_data->srq_asserted, 1); 2379 | } 2380 | spin_unlock_irqrestore(&data->dev_lock, flags); 2381 | 2382 | dev_dbg(dev, "srq received bTag %x stb %x\n", 2383 | (unsigned int)data->iin_buffer[0], 2384 | (unsigned int)data->iin_buffer[1]); 2385 | wake_up_interruptible_all(&data->waitq); 2386 | goto exit; 2387 | } 2388 | dev_warn(dev, "invalid notification: %x\n", 2389 | data->iin_buffer[0]); 2390 | break; 2391 | case -EOVERFLOW: 2392 | dev_err(dev, "overflow with length %d, actual length is %d\n", 2393 | data->iin_wMaxPacketSize, urb->actual_length); 2394 | fallthrough; 2395 | default: 2396 | /* urb terminated, clean up */ 2397 | dev_dbg(dev, "urb terminated, status: %d\n", status); 2398 | return; 2399 | } 2400 | exit: 2401 | rv = usb_submit_urb(urb, GFP_ATOMIC); 2402 | if (rv) 2403 | dev_err(dev, "usb_submit_urb failed: %d\n", rv); 2404 | } 2405 | 2406 | static void usbtmc_free_int(struct usbtmc_device_data *data) 2407 | { 2408 | if (!data->iin_ep_present || !data->iin_urb) 2409 | return; 2410 | usb_kill_urb(data->iin_urb); 2411 | kfree(data->iin_buffer); 2412 | data->iin_buffer = NULL; 2413 | usb_free_urb(data->iin_urb); 2414 | data->iin_urb = NULL; 2415 | kref_put(&data->kref, usbtmc_delete); 2416 | } 2417 | 2418 | static int usbtmc_probe(struct usb_interface *intf, 2419 | const struct usb_device_id *id) 2420 | { 2421 | struct usbtmc_device_data *data; 2422 | struct usb_host_interface *iface_desc; 2423 | struct usb_endpoint_descriptor *endpoint; 2424 | int n; 2425 | int retcode; 2426 | 2427 | dev_dbg(&intf->dev, "%s called\n", __func__); 2428 | 2429 | pr_info("Experimental driver version %s loaded\n", USBTMC_VERSION); 2430 | if (io_buffer_size != 0) { 2431 | if (io_buffer_size < 64) 2432 | io_buffer_size = 64; 2433 | io_buffer_size = io_buffer_size - (io_buffer_size % 4); 2434 | pr_info("Params: io_buffer_size = %d\n", io_buffer_size); 2435 | } else { 2436 | pr_info("Params: io_buffer_size: will use bulk ep's wMaxPacketSize\n"); 2437 | } 2438 | 2439 | if (usb_timeout < USBTMC_MIN_TIMEOUT) 2440 | usb_timeout = USBTMC_MIN_TIMEOUT; 2441 | pr_info("\tusb_timeout = %d\n", usb_timeout); 2442 | 2443 | data = kzalloc(sizeof(*data), GFP_KERNEL); 2444 | if (!data) 2445 | return -ENOMEM; 2446 | 2447 | data->intf = intf; 2448 | data->id = id; 2449 | data->usb_dev = usb_get_dev(interface_to_usbdev(intf)); 2450 | usb_set_intfdata(intf, data); 2451 | kref_init(&data->kref); 2452 | mutex_init(&data->io_mutex); 2453 | init_waitqueue_head(&data->waitq); 2454 | atomic_set(&data->iin_data_valid, 0); 2455 | INIT_LIST_HEAD(&data->file_list); 2456 | spin_lock_init(&data->dev_lock); 2457 | 2458 | data->zombie = 0; 2459 | 2460 | /* Initialize USBTMC bTag and other fields */ 2461 | data->bTag = 1; 2462 | /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */ 2463 | data->iin_bTag = 2; 2464 | 2465 | /* USBTMC devices have only one setting, so use that */ 2466 | iface_desc = data->intf->cur_altsetting; 2467 | data->ifnum = iface_desc->desc.bInterfaceNumber; 2468 | 2469 | /* Find bulk in endpoint */ 2470 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { 2471 | endpoint = &iface_desc->endpoint[n].desc; 2472 | 2473 | if (usb_endpoint_is_bulk_in(endpoint)) { 2474 | data->bulk_in = endpoint->bEndpointAddress; 2475 | data->bin_bsiz = (io_buffer_size) ? 2476 | io_buffer_size : usb_endpoint_maxp(endpoint); 2477 | dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n", 2478 | data->bulk_in); 2479 | break; 2480 | } 2481 | } 2482 | 2483 | /* Find bulk out endpoint */ 2484 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { 2485 | endpoint = &iface_desc->endpoint[n].desc; 2486 | 2487 | if (usb_endpoint_is_bulk_out(endpoint)) { 2488 | data->bulk_out = endpoint->bEndpointAddress; 2489 | data->bout_bsiz = (io_buffer_size) ? 2490 | io_buffer_size : usb_endpoint_maxp(endpoint); 2491 | dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n", 2492 | data->bulk_out); 2493 | break; 2494 | } 2495 | } 2496 | /* Find int endpoint */ 2497 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { 2498 | endpoint = &iface_desc->endpoint[n].desc; 2499 | 2500 | if (usb_endpoint_is_int_in(endpoint)) { 2501 | data->iin_ep_present = 1; 2502 | data->iin_ep = endpoint->bEndpointAddress; 2503 | data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint); 2504 | data->iin_interval = endpoint->bInterval; 2505 | dev_dbg(&intf->dev, "Found Int in endpoint at %u\n", 2506 | data->iin_ep); 2507 | break; 2508 | } 2509 | } 2510 | 2511 | retcode = get_capabilities(data); 2512 | if (retcode) 2513 | dev_err(&intf->dev, "can't read capabilities\n"); 2514 | 2515 | if (data->iin_ep_present) { 2516 | if (data->iin_wMaxPacketSize < USBTMC_MIN_INT_IN_PACKET_SIZE) { 2517 | dev_err(&intf->dev, 2518 | "Int in endpoint wMaxPacketSize too small: %d, minimum %d required\n", 2519 | data->iin_wMaxPacketSize, 2520 | USBTMC_MIN_INT_IN_PACKET_SIZE); 2521 | retcode = -EINVAL; 2522 | goto error_register; 2523 | } 2524 | 2525 | /* allocate int urb */ 2526 | data->iin_urb = usb_alloc_urb(0, GFP_KERNEL); 2527 | if (!data->iin_urb) { 2528 | retcode = -ENOMEM; 2529 | goto error_register; 2530 | } 2531 | 2532 | /* Protect interrupt in endpoint data until iin_urb is freed */ 2533 | kref_get(&data->kref); 2534 | 2535 | /* allocate buffer for interrupt in */ 2536 | data->iin_buffer = kmalloc(data->iin_wMaxPacketSize, 2537 | GFP_KERNEL); 2538 | if (!data->iin_buffer) { 2539 | retcode = -ENOMEM; 2540 | goto error_register; 2541 | } 2542 | 2543 | /* fill interrupt urb */ 2544 | usb_fill_int_urb(data->iin_urb, data->usb_dev, 2545 | usb_rcvintpipe(data->usb_dev, data->iin_ep), 2546 | data->iin_buffer, data->iin_wMaxPacketSize, 2547 | usbtmc_interrupt, 2548 | data, data->iin_interval); 2549 | 2550 | retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL); 2551 | if (retcode) { 2552 | dev_err(&intf->dev, "Failed to submit iin_urb\n"); 2553 | goto error_register; 2554 | } 2555 | } 2556 | 2557 | retcode = usb_register_dev(intf, &usbtmc_class); 2558 | if (retcode) { 2559 | dev_err(&intf->dev, "Not able to get a minor (base %u, slice default): %d\n", 2560 | USBTMC_MINOR_BASE, 2561 | retcode); 2562 | goto error_register; 2563 | } 2564 | dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor); 2565 | 2566 | return 0; 2567 | 2568 | error_register: 2569 | usbtmc_free_int(data); 2570 | kref_put(&data->kref, usbtmc_delete); 2571 | return retcode; 2572 | } 2573 | 2574 | static void usbtmc_disconnect(struct usb_interface *intf) 2575 | { 2576 | struct usbtmc_device_data *data = usb_get_intfdata(intf); 2577 | struct list_head *elem; 2578 | 2579 | usb_deregister_dev(intf, &usbtmc_class); 2580 | mutex_lock(&data->io_mutex); 2581 | data->zombie = 1; 2582 | wake_up_interruptible_all(&data->waitq); 2583 | list_for_each(elem, &data->file_list) { 2584 | struct usbtmc_file_data *file_data; 2585 | 2586 | file_data = list_entry(elem, 2587 | struct usbtmc_file_data, 2588 | file_elem); 2589 | usb_kill_anchored_urbs(&file_data->submitted); 2590 | usb_scuttle_anchored_urbs(&file_data->in_anchor); 2591 | } 2592 | mutex_unlock(&data->io_mutex); 2593 | usbtmc_free_int(data); 2594 | kref_put(&data->kref, usbtmc_delete); 2595 | pr_info("Experimental driver version %s unloaded", USBTMC_VERSION); 2596 | } 2597 | 2598 | static void usbtmc_draw_down(struct usbtmc_file_data *file_data) 2599 | { 2600 | int time; 2601 | 2602 | time = usb_wait_anchor_empty_timeout(&file_data->submitted, 1000); 2603 | if (!time) 2604 | usb_kill_anchored_urbs(&file_data->submitted); 2605 | usb_scuttle_anchored_urbs(&file_data->in_anchor); 2606 | } 2607 | 2608 | static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message) 2609 | { 2610 | struct usbtmc_device_data *data = usb_get_intfdata(intf); 2611 | struct list_head *elem; 2612 | 2613 | if (!data) 2614 | return 0; 2615 | 2616 | mutex_lock(&data->io_mutex); 2617 | list_for_each(elem, &data->file_list) { 2618 | struct usbtmc_file_data *file_data; 2619 | 2620 | file_data = list_entry(elem, 2621 | struct usbtmc_file_data, 2622 | file_elem); 2623 | usbtmc_draw_down(file_data); 2624 | } 2625 | 2626 | if (data->iin_ep_present && data->iin_urb) 2627 | usb_kill_urb(data->iin_urb); 2628 | 2629 | mutex_unlock(&data->io_mutex); 2630 | return 0; 2631 | } 2632 | 2633 | static int usbtmc_resume(struct usb_interface *intf) 2634 | { 2635 | struct usbtmc_device_data *data = usb_get_intfdata(intf); 2636 | int retcode = 0; 2637 | 2638 | if (data->iin_ep_present && data->iin_urb) 2639 | retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL); 2640 | if (retcode) 2641 | dev_err(&intf->dev, "Failed to submit iin_urb\n"); 2642 | 2643 | return retcode; 2644 | } 2645 | 2646 | static int usbtmc_pre_reset(struct usb_interface *intf) 2647 | { 2648 | struct usbtmc_device_data *data = usb_get_intfdata(intf); 2649 | struct list_head *elem; 2650 | 2651 | if (!data) 2652 | return 0; 2653 | 2654 | mutex_lock(&data->io_mutex); 2655 | 2656 | list_for_each(elem, &data->file_list) { 2657 | struct usbtmc_file_data *file_data; 2658 | 2659 | file_data = list_entry(elem, 2660 | struct usbtmc_file_data, 2661 | file_elem); 2662 | usbtmc_ioctl_cancel_io(file_data); 2663 | } 2664 | 2665 | return 0; 2666 | } 2667 | 2668 | static int usbtmc_post_reset(struct usb_interface *intf) 2669 | { 2670 | struct usbtmc_device_data *data = usb_get_intfdata(intf); 2671 | 2672 | mutex_unlock(&data->io_mutex); 2673 | 2674 | return 0; 2675 | } 2676 | 2677 | static struct usb_driver usbtmc_driver = { 2678 | .name = "usbtmc", 2679 | .id_table = usbtmc_devices, 2680 | .probe = usbtmc_probe, 2681 | .disconnect = usbtmc_disconnect, 2682 | .suspend = usbtmc_suspend, 2683 | .resume = usbtmc_resume, 2684 | .pre_reset = usbtmc_pre_reset, 2685 | .post_reset = usbtmc_post_reset, 2686 | .dev_groups = usbtmc_groups, 2687 | }; 2688 | 2689 | module_usb_driver(usbtmc_driver); 2690 | 2691 | MODULE_DESCRIPTION("USB Test & Measurement class driver"); 2692 | MODULE_LICENSE("GPL"); 2693 | MODULE_VERSION(USBTMC_VERSION); 2694 | --------------------------------------------------------------------------------