Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1 | /** |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2 | * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany |
| 5 | * Copyright (C) 2008 Novell, Inc. |
| 6 | * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * The GNU General Public License is available at |
| 19 | * http://www.gnu.org/copyleft/gpl.html. |
| 20 | */ |
| 21 | |
Andy Shevchenko | f4d844c | 2013-07-15 11:59:42 +0300 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
Ilpo Järvinen | 857cc4d | 2008-10-30 13:56:47 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 26 | #include <linux/fs.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/kref.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 30 | #include <linux/mutex.h> |
| 31 | #include <linux/usb.h> |
| 32 | #include <linux/usb/tmc.h> |
| 33 | |
| 34 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 35 | #define RIGOL 1 |
| 36 | #define USBTMC_HEADER_SIZE 12 |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 37 | #define USBTMC_MINOR_BASE 176 |
| 38 | |
| 39 | /* |
| 40 | * Size of driver internal IO buffer. Must be multiple of 4 and at least as |
| 41 | * large as wMaxPacketSize (which is usually 512 bytes). |
| 42 | */ |
| 43 | #define USBTMC_SIZE_IOBUFFER 2048 |
| 44 | |
| 45 | /* Default USB timeout (in milliseconds) */ |
Gergely Imreh | 35f76e89 | 2009-09-15 16:03:31 +0800 | [diff] [blame] | 46 | #define USBTMC_TIMEOUT 5000 |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Maximum number of read cycles to empty bulk in endpoint during CLEAR and |
| 50 | * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short |
| 51 | * packet is never read. |
| 52 | */ |
| 53 | #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100 |
| 54 | |
Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 55 | static const struct usb_device_id usbtmc_devices[] = { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 56 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), }, |
Greg Kroah-Hartman | 228dd05 | 2009-03-11 13:51:42 -0700 | [diff] [blame] | 57 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), }, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 58 | { 0, } /* terminating entry */ |
| 59 | }; |
Greg Kroah-Hartman | 5413aa4 | 2008-12-03 16:33:09 -0800 | [diff] [blame] | 60 | MODULE_DEVICE_TABLE(usb, usbtmc_devices); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * This structure is the capabilities for the device |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 64 | * See section 4.2.1.8 of the USBTMC specification, |
| 65 | * and section 4.2.2 of the USBTMC usb488 subclass |
| 66 | * specification for details. |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 67 | */ |
| 68 | struct usbtmc_dev_capabilities { |
| 69 | __u8 interface_capabilities; |
| 70 | __u8 device_capabilities; |
| 71 | __u8 usb488_interface_capabilities; |
| 72 | __u8 usb488_device_capabilities; |
| 73 | }; |
| 74 | |
| 75 | /* This structure holds private data for each USBTMC device. One copy is |
| 76 | * allocated for each USBTMC device in the driver's probe function. |
| 77 | */ |
| 78 | struct usbtmc_device_data { |
| 79 | const struct usb_device_id *id; |
| 80 | struct usb_device *usb_dev; |
| 81 | struct usb_interface *intf; |
| 82 | |
| 83 | unsigned int bulk_in; |
| 84 | unsigned int bulk_out; |
| 85 | |
| 86 | u8 bTag; |
| 87 | u8 bTag_last_write; /* needed for abort */ |
| 88 | u8 bTag_last_read; /* needed for abort */ |
| 89 | |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 90 | /* data for interrupt in endpoint handling */ |
| 91 | u8 bNotify1; |
| 92 | u8 bNotify2; |
| 93 | u16 ifnum; |
| 94 | u8 iin_bTag; |
| 95 | u8 *iin_buffer; |
| 96 | atomic_t iin_data_valid; |
| 97 | unsigned int iin_ep; |
| 98 | int iin_ep_present; |
| 99 | int iin_interval; |
| 100 | struct urb *iin_urb; |
| 101 | u16 iin_wMaxPacketSize; |
Dave Penkler | 82ed338 | 2016-01-27 19:15:15 +0100 | [diff] [blame^] | 102 | atomic_t srq_asserted; |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 103 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 104 | u8 rigol_quirk; |
| 105 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 106 | /* attributes from the USB TMC spec for this device */ |
| 107 | u8 TermChar; |
| 108 | bool TermCharEnabled; |
| 109 | bool auto_abort; |
| 110 | |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 111 | bool zombie; /* fd of disconnected device */ |
| 112 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 113 | struct usbtmc_dev_capabilities capabilities; |
| 114 | struct kref kref; |
| 115 | struct mutex io_mutex; /* only one i/o function running at a time */ |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 116 | wait_queue_head_t waitq; |
Dave Penkler | 82ed338 | 2016-01-27 19:15:15 +0100 | [diff] [blame^] | 117 | struct fasync_struct *fasync; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 118 | }; |
| 119 | #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref) |
| 120 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 121 | struct usbtmc_ID_rigol_quirk { |
| 122 | __u16 idVendor; |
| 123 | __u16 idProduct; |
| 124 | }; |
| 125 | |
| 126 | static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = { |
| 127 | { 0x1ab1, 0x0588 }, |
Teunis van Beelen | f504202 | 2015-05-31 09:36:22 +0200 | [diff] [blame] | 128 | { 0x1ab1, 0x04b0 }, |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 129 | { 0, 0 } |
| 130 | }; |
| 131 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 132 | /* Forward declarations */ |
| 133 | static struct usb_driver usbtmc_driver; |
| 134 | |
| 135 | static void usbtmc_delete(struct kref *kref) |
| 136 | { |
| 137 | struct usbtmc_device_data *data = to_usbtmc_data(kref); |
| 138 | |
| 139 | usb_put_dev(data->usb_dev); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int usbtmc_open(struct inode *inode, struct file *filp) |
| 143 | { |
| 144 | struct usb_interface *intf; |
| 145 | struct usbtmc_device_data *data; |
Greg Kroah-Hartman | 5b10916 | 2009-03-10 20:42:55 -0700 | [diff] [blame] | 146 | int retval = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 147 | |
| 148 | intf = usb_find_interface(&usbtmc_driver, iminor(inode)); |
| 149 | if (!intf) { |
Andy Shevchenko | f4d844c | 2013-07-15 11:59:42 +0300 | [diff] [blame] | 150 | pr_err("can not find device for minor %d", iminor(inode)); |
| 151 | return -ENODEV; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | data = usb_get_intfdata(intf); |
| 155 | kref_get(&data->kref); |
| 156 | |
| 157 | /* Store pointer in file structure's private data field */ |
| 158 | filp->private_data = data; |
| 159 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 160 | return retval; |
| 161 | } |
| 162 | |
| 163 | static int usbtmc_release(struct inode *inode, struct file *file) |
| 164 | { |
| 165 | struct usbtmc_device_data *data = file->private_data; |
| 166 | |
| 167 | kref_put(&data->kref, usbtmc_delete); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data) |
| 172 | { |
Chris Malley | b361a6e | 2008-10-25 22:07:32 +0100 | [diff] [blame] | 173 | u8 *buffer; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 174 | struct device *dev; |
| 175 | int rv; |
| 176 | int n; |
| 177 | int actual; |
| 178 | struct usb_host_interface *current_setting; |
| 179 | int max_size; |
| 180 | |
| 181 | dev = &data->intf->dev; |
| 182 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 183 | if (!buffer) |
| 184 | return -ENOMEM; |
| 185 | |
| 186 | rv = usb_control_msg(data->usb_dev, |
| 187 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 188 | USBTMC_REQUEST_INITIATE_ABORT_BULK_IN, |
| 189 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 190 | data->bTag_last_read, data->bulk_in, |
| 191 | buffer, 2, USBTMC_TIMEOUT); |
| 192 | |
| 193 | if (rv < 0) { |
| 194 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 195 | goto exit; |
| 196 | } |
| 197 | |
| 198 | dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 199 | |
| 200 | if (buffer[0] == USBTMC_STATUS_FAILED) { |
| 201 | rv = 0; |
| 202 | goto exit; |
| 203 | } |
| 204 | |
| 205 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 206 | dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", |
| 207 | buffer[0]); |
| 208 | rv = -EPERM; |
| 209 | goto exit; |
| 210 | } |
| 211 | |
| 212 | max_size = 0; |
| 213 | current_setting = data->intf->cur_altsetting; |
| 214 | for (n = 0; n < current_setting->desc.bNumEndpoints; n++) |
| 215 | if (current_setting->endpoint[n].desc.bEndpointAddress == |
| 216 | data->bulk_in) |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 217 | max_size = usb_endpoint_maxp(¤t_setting->endpoint[n].desc); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 218 | |
| 219 | if (max_size == 0) { |
| 220 | dev_err(dev, "Couldn't get wMaxPacketSize\n"); |
| 221 | rv = -EPERM; |
| 222 | goto exit; |
| 223 | } |
| 224 | |
| 225 | dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size); |
| 226 | |
| 227 | n = 0; |
| 228 | |
| 229 | do { |
| 230 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 231 | |
| 232 | rv = usb_bulk_msg(data->usb_dev, |
| 233 | usb_rcvbulkpipe(data->usb_dev, |
| 234 | data->bulk_in), |
| 235 | buffer, USBTMC_SIZE_IOBUFFER, |
| 236 | &actual, USBTMC_TIMEOUT); |
| 237 | |
| 238 | n++; |
| 239 | |
| 240 | if (rv < 0) { |
| 241 | dev_err(dev, "usb_bulk_msg returned %d\n", rv); |
| 242 | goto exit; |
| 243 | } |
| 244 | } while ((actual == max_size) && |
| 245 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 246 | |
| 247 | if (actual == max_size) { |
| 248 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 249 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 250 | rv = -EPERM; |
| 251 | goto exit; |
| 252 | } |
| 253 | |
| 254 | n = 0; |
| 255 | |
| 256 | usbtmc_abort_bulk_in_status: |
| 257 | rv = usb_control_msg(data->usb_dev, |
| 258 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 259 | USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS, |
| 260 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 261 | 0, data->bulk_in, buffer, 0x08, |
| 262 | USBTMC_TIMEOUT); |
| 263 | |
| 264 | if (rv < 0) { |
| 265 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 266 | goto exit; |
| 267 | } |
| 268 | |
| 269 | dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 270 | |
| 271 | if (buffer[0] == USBTMC_STATUS_SUCCESS) { |
| 272 | rv = 0; |
| 273 | goto exit; |
| 274 | } |
| 275 | |
| 276 | if (buffer[0] != USBTMC_STATUS_PENDING) { |
| 277 | dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 278 | rv = -EPERM; |
| 279 | goto exit; |
| 280 | } |
| 281 | |
| 282 | if (buffer[1] == 1) |
| 283 | do { |
| 284 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 285 | |
| 286 | rv = usb_bulk_msg(data->usb_dev, |
| 287 | usb_rcvbulkpipe(data->usb_dev, |
| 288 | data->bulk_in), |
| 289 | buffer, USBTMC_SIZE_IOBUFFER, |
| 290 | &actual, USBTMC_TIMEOUT); |
| 291 | |
| 292 | n++; |
| 293 | |
| 294 | if (rv < 0) { |
| 295 | dev_err(dev, "usb_bulk_msg returned %d\n", rv); |
| 296 | goto exit; |
| 297 | } |
Maxim Nikulin | 4f1a7a3 | 2011-07-09 23:44:44 +0700 | [diff] [blame] | 298 | } while ((actual == max_size) && |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 299 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 300 | |
| 301 | if (actual == max_size) { |
| 302 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 303 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 304 | rv = -EPERM; |
| 305 | goto exit; |
| 306 | } |
| 307 | |
| 308 | goto usbtmc_abort_bulk_in_status; |
| 309 | |
| 310 | exit: |
| 311 | kfree(buffer); |
| 312 | return rv; |
| 313 | |
| 314 | } |
| 315 | |
| 316 | static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data) |
| 317 | { |
| 318 | struct device *dev; |
| 319 | u8 *buffer; |
| 320 | int rv; |
| 321 | int n; |
| 322 | |
| 323 | dev = &data->intf->dev; |
| 324 | |
| 325 | buffer = kmalloc(8, GFP_KERNEL); |
| 326 | if (!buffer) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | rv = usb_control_msg(data->usb_dev, |
| 330 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 331 | USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT, |
| 332 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 333 | data->bTag_last_write, data->bulk_out, |
| 334 | buffer, 2, USBTMC_TIMEOUT); |
| 335 | |
| 336 | if (rv < 0) { |
| 337 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 338 | goto exit; |
| 339 | } |
| 340 | |
| 341 | dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]); |
| 342 | |
| 343 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 344 | dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", |
| 345 | buffer[0]); |
| 346 | rv = -EPERM; |
| 347 | goto exit; |
| 348 | } |
| 349 | |
| 350 | n = 0; |
| 351 | |
| 352 | usbtmc_abort_bulk_out_check_status: |
| 353 | rv = usb_control_msg(data->usb_dev, |
| 354 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 355 | USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS, |
| 356 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 357 | 0, data->bulk_out, buffer, 0x08, |
| 358 | USBTMC_TIMEOUT); |
| 359 | n++; |
| 360 | if (rv < 0) { |
| 361 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 362 | goto exit; |
| 363 | } |
| 364 | |
| 365 | dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]); |
| 366 | |
| 367 | if (buffer[0] == USBTMC_STATUS_SUCCESS) |
| 368 | goto usbtmc_abort_bulk_out_clear_halt; |
| 369 | |
| 370 | if ((buffer[0] == USBTMC_STATUS_PENDING) && |
| 371 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)) |
| 372 | goto usbtmc_abort_bulk_out_check_status; |
| 373 | |
| 374 | rv = -EPERM; |
| 375 | goto exit; |
| 376 | |
| 377 | usbtmc_abort_bulk_out_clear_halt: |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 378 | rv = usb_clear_halt(data->usb_dev, |
| 379 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 380 | |
| 381 | if (rv < 0) { |
| 382 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 383 | goto exit; |
| 384 | } |
| 385 | rv = 0; |
| 386 | |
| 387 | exit: |
| 388 | kfree(buffer); |
| 389 | return rv; |
| 390 | } |
| 391 | |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 392 | static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data, |
| 393 | void __user *arg) |
| 394 | { |
| 395 | struct device *dev = &data->intf->dev; |
| 396 | u8 *buffer; |
| 397 | u8 tag; |
| 398 | __u8 stb; |
| 399 | int rv; |
| 400 | |
| 401 | dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n", |
| 402 | data->iin_ep_present); |
| 403 | |
| 404 | buffer = kmalloc(8, GFP_KERNEL); |
| 405 | if (!buffer) |
| 406 | return -ENOMEM; |
| 407 | |
| 408 | atomic_set(&data->iin_data_valid, 0); |
| 409 | |
Dave Penkler | 82ed338 | 2016-01-27 19:15:15 +0100 | [diff] [blame^] | 410 | /* must issue read_stb before using poll or select */ |
| 411 | atomic_set(&data->srq_asserted, 0); |
| 412 | |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 413 | rv = usb_control_msg(data->usb_dev, |
| 414 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 415 | USBTMC488_REQUEST_READ_STATUS_BYTE, |
| 416 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 417 | data->iin_bTag, |
| 418 | data->ifnum, |
| 419 | buffer, 0x03, USBTMC_TIMEOUT); |
| 420 | if (rv < 0) { |
| 421 | dev_err(dev, "stb usb_control_msg returned %d\n", rv); |
| 422 | goto exit; |
| 423 | } |
| 424 | |
| 425 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 426 | dev_err(dev, "control status returned %x\n", buffer[0]); |
| 427 | rv = -EIO; |
| 428 | goto exit; |
| 429 | } |
| 430 | |
| 431 | if (data->iin_ep_present) { |
| 432 | rv = wait_event_interruptible_timeout( |
| 433 | data->waitq, |
| 434 | atomic_read(&data->iin_data_valid) != 0, |
| 435 | USBTMC_TIMEOUT); |
| 436 | if (rv < 0) { |
| 437 | dev_dbg(dev, "wait interrupted %d\n", rv); |
| 438 | goto exit; |
| 439 | } |
| 440 | |
| 441 | if (rv == 0) { |
| 442 | dev_dbg(dev, "wait timed out\n"); |
| 443 | rv = -ETIME; |
| 444 | goto exit; |
| 445 | } |
| 446 | |
| 447 | tag = data->bNotify1 & 0x7f; |
| 448 | if (tag != data->iin_bTag) { |
| 449 | dev_err(dev, "expected bTag %x got %x\n", |
| 450 | data->iin_bTag, tag); |
| 451 | } |
| 452 | |
| 453 | stb = data->bNotify2; |
| 454 | } else { |
| 455 | stb = buffer[2]; |
| 456 | } |
| 457 | |
| 458 | rv = copy_to_user(arg, &stb, sizeof(stb)); |
| 459 | if (rv) |
| 460 | rv = -EFAULT; |
| 461 | |
| 462 | exit: |
| 463 | /* bump interrupt bTag */ |
| 464 | data->iin_bTag += 1; |
| 465 | if (data->iin_bTag > 127) |
| 466 | /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */ |
| 467 | data->iin_bTag = 2; |
| 468 | |
| 469 | kfree(buffer); |
| 470 | return rv; |
| 471 | } |
| 472 | |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 473 | /* |
| 474 | * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint. |
| 475 | * @transfer_size: number of bytes to request from the device. |
| 476 | * |
| 477 | * See the USBTMC specification, Table 4. |
| 478 | * |
| 479 | * Also updates bTag_last_write. |
| 480 | */ |
| 481 | static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size) |
| 482 | { |
| 483 | int retval; |
Oliver Neukum | d846b76 | 2014-05-19 13:54:57 +0200 | [diff] [blame] | 484 | u8 *buffer; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 485 | int actual; |
| 486 | |
Oliver Neukum | d846b76 | 2014-05-19 13:54:57 +0200 | [diff] [blame] | 487 | buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL); |
| 488 | if (!buffer) |
| 489 | return -ENOMEM; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 490 | /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message |
| 491 | * Refer to class specs for details |
| 492 | */ |
| 493 | buffer[0] = 2; |
| 494 | buffer[1] = data->bTag; |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 495 | buffer[2] = ~data->bTag; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 496 | buffer[3] = 0; /* Reserved */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 497 | buffer[4] = transfer_size >> 0; |
| 498 | buffer[5] = transfer_size >> 8; |
| 499 | buffer[6] = transfer_size >> 16; |
| 500 | buffer[7] = transfer_size >> 24; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 501 | buffer[8] = data->TermCharEnabled * 2; |
| 502 | /* Use term character? */ |
| 503 | buffer[9] = data->TermChar; |
| 504 | buffer[10] = 0; /* Reserved */ |
| 505 | buffer[11] = 0; /* Reserved */ |
| 506 | |
| 507 | /* Send bulk URB */ |
| 508 | retval = usb_bulk_msg(data->usb_dev, |
| 509 | usb_sndbulkpipe(data->usb_dev, |
| 510 | data->bulk_out), |
| 511 | buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT); |
| 512 | |
| 513 | /* Store bTag (in case we need to abort) */ |
| 514 | data->bTag_last_write = data->bTag; |
| 515 | |
| 516 | /* Increment bTag -- and increment again if zero */ |
| 517 | data->bTag++; |
| 518 | if (!data->bTag) |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 519 | data->bTag++; |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 520 | |
Oliver Neukum | d846b76 | 2014-05-19 13:54:57 +0200 | [diff] [blame] | 521 | kfree(buffer); |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 522 | if (retval < 0) { |
| 523 | dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval); |
| 524 | return retval; |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 530 | static ssize_t usbtmc_read(struct file *filp, char __user *buf, |
| 531 | size_t count, loff_t *f_pos) |
| 532 | { |
| 533 | struct usbtmc_device_data *data; |
| 534 | struct device *dev; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 535 | u32 n_characters; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 536 | u8 *buffer; |
| 537 | int actual; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 538 | size_t done; |
| 539 | size_t remaining; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 540 | int retval; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 541 | size_t this_part; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 542 | |
| 543 | /* Get pointer to private data structure */ |
| 544 | data = filp->private_data; |
| 545 | dev = &data->intf->dev; |
| 546 | |
| 547 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 548 | if (!buffer) |
| 549 | return -ENOMEM; |
| 550 | |
| 551 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 552 | if (data->zombie) { |
| 553 | retval = -ENODEV; |
| 554 | goto exit; |
| 555 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 556 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 557 | if (data->rigol_quirk) { |
| 558 | dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 559 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 560 | retval = send_request_dev_dep_msg_in(data, count); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 561 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 562 | if (retval < 0) { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 563 | if (data->auto_abort) |
| 564 | usbtmc_ioctl_abort_bulk_out(data); |
| 565 | goto exit; |
| 566 | } |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* Loop until we have fetched everything we requested */ |
| 570 | remaining = count; |
| 571 | this_part = remaining; |
| 572 | done = 0; |
| 573 | |
| 574 | while (remaining > 0) { |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 575 | if (!data->rigol_quirk) { |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 576 | dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count); |
| 577 | |
| 578 | if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3) |
| 579 | this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3; |
| 580 | else |
| 581 | this_part = remaining; |
| 582 | |
| 583 | retval = send_request_dev_dep_msg_in(data, this_part); |
| 584 | if (retval < 0) { |
| 585 | dev_err(dev, "usb_bulk_msg returned %d\n", retval); |
| 586 | if (data->auto_abort) |
| 587 | usbtmc_ioctl_abort_bulk_out(data); |
| 588 | goto exit; |
| 589 | } |
| 590 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 591 | |
| 592 | /* Send bulk URB */ |
| 593 | retval = usb_bulk_msg(data->usb_dev, |
| 594 | usb_rcvbulkpipe(data->usb_dev, |
| 595 | data->bulk_in), |
| 596 | buffer, USBTMC_SIZE_IOBUFFER, &actual, |
| 597 | USBTMC_TIMEOUT); |
| 598 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 599 | dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual); |
| 600 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 601 | /* Store bTag (in case we need to abort) */ |
| 602 | data->bTag_last_read = data->bTag; |
| 603 | |
| 604 | if (retval < 0) { |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 605 | dev_dbg(dev, "Unable to read data, error %d\n", retval); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 606 | if (data->auto_abort) |
| 607 | usbtmc_ioctl_abort_bulk_in(data); |
| 608 | goto exit; |
| 609 | } |
| 610 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 611 | /* Parse header in first packet */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 612 | if ((done == 0) || !data->rigol_quirk) { |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 613 | /* Sanity checks for the header */ |
| 614 | if (actual < USBTMC_HEADER_SIZE) { |
| 615 | dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE); |
| 616 | if (data->auto_abort) |
| 617 | usbtmc_ioctl_abort_bulk_in(data); |
| 618 | goto exit; |
| 619 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 620 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 621 | if (buffer[0] != 2) { |
| 622 | dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]); |
| 623 | if (data->auto_abort) |
| 624 | usbtmc_ioctl_abort_bulk_in(data); |
| 625 | goto exit; |
| 626 | } |
| 627 | |
| 628 | if (buffer[1] != data->bTag_last_write) { |
| 629 | dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write); |
| 630 | if (data->auto_abort) |
| 631 | usbtmc_ioctl_abort_bulk_in(data); |
| 632 | goto exit; |
| 633 | } |
| 634 | |
| 635 | /* How many characters did the instrument send? */ |
| 636 | n_characters = buffer[4] + |
| 637 | (buffer[5] << 8) + |
| 638 | (buffer[6] << 16) + |
| 639 | (buffer[7] << 24); |
| 640 | |
| 641 | if (n_characters > this_part) { |
| 642 | dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count); |
| 643 | if (data->auto_abort) |
| 644 | usbtmc_ioctl_abort_bulk_in(data); |
| 645 | goto exit; |
| 646 | } |
| 647 | |
| 648 | /* Remove the USBTMC header */ |
| 649 | actual -= USBTMC_HEADER_SIZE; |
| 650 | |
| 651 | /* Check if the message is smaller than requested */ |
| 652 | if (data->rigol_quirk) { |
| 653 | if (remaining > n_characters) |
| 654 | remaining = n_characters; |
| 655 | /* Remove padding if it exists */ |
Andy Shevchenko | 92f78dd | 2013-07-15 11:59:40 +0300 | [diff] [blame] | 656 | if (actual > remaining) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 657 | actual = remaining; |
| 658 | } |
| 659 | else { |
| 660 | if (this_part > n_characters) |
| 661 | this_part = n_characters; |
| 662 | /* Remove padding if it exists */ |
Andy Shevchenko | 92f78dd | 2013-07-15 11:59:40 +0300 | [diff] [blame] | 663 | if (actual > this_part) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 664 | actual = this_part; |
| 665 | } |
| 666 | |
| 667 | dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]); |
| 668 | |
| 669 | remaining -= actual; |
| 670 | |
| 671 | /* Terminate if end-of-message bit received from device */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 672 | if ((buffer[8] & 0x01) && (actual >= n_characters)) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 673 | remaining = 0; |
| 674 | |
| 675 | dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done); |
| 676 | |
| 677 | |
| 678 | /* Copy buffer to user space */ |
| 679 | if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) { |
| 680 | /* There must have been an addressing problem */ |
| 681 | retval = -EFAULT; |
| 682 | goto exit; |
| 683 | } |
| 684 | done += actual; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 685 | } |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 686 | else { |
Andy Shevchenko | 92f78dd | 2013-07-15 11:59:40 +0300 | [diff] [blame] | 687 | if (actual > remaining) |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 688 | actual = remaining; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 689 | |
Alexandre Peixoto Ferreira | d2ddce3 | 2013-04-30 00:51:53 -0500 | [diff] [blame] | 690 | remaining -= actual; |
| 691 | |
| 692 | dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer); |
| 693 | |
| 694 | /* Copy buffer to user space */ |
| 695 | if (copy_to_user(buf + done, buffer, actual)) { |
| 696 | /* There must have been an addressing problem */ |
| 697 | retval = -EFAULT; |
| 698 | goto exit; |
| 699 | } |
| 700 | done += actual; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 701 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* Update file position value */ |
| 705 | *f_pos = *f_pos + done; |
| 706 | retval = done; |
| 707 | |
| 708 | exit: |
| 709 | mutex_unlock(&data->io_mutex); |
| 710 | kfree(buffer); |
| 711 | return retval; |
| 712 | } |
| 713 | |
| 714 | static ssize_t usbtmc_write(struct file *filp, const char __user *buf, |
| 715 | size_t count, loff_t *f_pos) |
| 716 | { |
| 717 | struct usbtmc_device_data *data; |
| 718 | u8 *buffer; |
| 719 | int retval; |
| 720 | int actual; |
| 721 | unsigned long int n_bytes; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 722 | int remaining; |
| 723 | int done; |
| 724 | int this_part; |
| 725 | |
| 726 | data = filp->private_data; |
| 727 | |
| 728 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 729 | if (!buffer) |
| 730 | return -ENOMEM; |
| 731 | |
| 732 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 733 | if (data->zombie) { |
| 734 | retval = -ENODEV; |
| 735 | goto exit; |
| 736 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 737 | |
| 738 | remaining = count; |
| 739 | done = 0; |
| 740 | |
| 741 | while (remaining > 0) { |
Alexandre Peixoto Ferreira | 50c9ba3 | 2013-04-30 00:51:54 -0500 | [diff] [blame] | 742 | if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) { |
| 743 | this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 744 | buffer[8] = 0; |
| 745 | } else { |
| 746 | this_part = remaining; |
| 747 | buffer[8] = 1; |
| 748 | } |
| 749 | |
| 750 | /* Setup IO buffer for DEV_DEP_MSG_OUT message */ |
| 751 | buffer[0] = 1; |
| 752 | buffer[1] = data->bTag; |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 753 | buffer[2] = ~data->bTag; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 754 | buffer[3] = 0; /* Reserved */ |
Andy Shevchenko | bbf4976 | 2013-07-15 11:59:43 +0300 | [diff] [blame] | 755 | buffer[4] = this_part >> 0; |
| 756 | buffer[5] = this_part >> 8; |
| 757 | buffer[6] = this_part >> 16; |
| 758 | buffer[7] = this_part >> 24; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 759 | /* buffer[8] is set above... */ |
| 760 | buffer[9] = 0; /* Reserved */ |
| 761 | buffer[10] = 0; /* Reserved */ |
| 762 | buffer[11] = 0; /* Reserved */ |
| 763 | |
Alexandre Peixoto Ferreira | 50c9ba3 | 2013-04-30 00:51:54 -0500 | [diff] [blame] | 764 | if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 765 | retval = -EFAULT; |
| 766 | goto exit; |
| 767 | } |
| 768 | |
Alexandre Peixoto Ferreira | 50c9ba3 | 2013-04-30 00:51:54 -0500 | [diff] [blame] | 769 | n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4); |
| 770 | memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 771 | |
Andre Herms | ec412b9 | 2009-11-19 18:14:49 +0100 | [diff] [blame] | 772 | do { |
| 773 | retval = usb_bulk_msg(data->usb_dev, |
| 774 | usb_sndbulkpipe(data->usb_dev, |
| 775 | data->bulk_out), |
| 776 | buffer, n_bytes, |
| 777 | &actual, USBTMC_TIMEOUT); |
| 778 | if (retval != 0) |
| 779 | break; |
| 780 | n_bytes -= actual; |
| 781 | } while (n_bytes); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 782 | |
| 783 | data->bTag_last_write = data->bTag; |
| 784 | data->bTag++; |
| 785 | |
| 786 | if (!data->bTag) |
| 787 | data->bTag++; |
| 788 | |
| 789 | if (retval < 0) { |
| 790 | dev_err(&data->intf->dev, |
| 791 | "Unable to send data, error %d\n", retval); |
| 792 | if (data->auto_abort) |
| 793 | usbtmc_ioctl_abort_bulk_out(data); |
| 794 | goto exit; |
| 795 | } |
| 796 | |
| 797 | remaining -= this_part; |
| 798 | done += this_part; |
| 799 | } |
| 800 | |
| 801 | retval = count; |
| 802 | exit: |
| 803 | mutex_unlock(&data->io_mutex); |
| 804 | kfree(buffer); |
| 805 | return retval; |
| 806 | } |
| 807 | |
| 808 | static int usbtmc_ioctl_clear(struct usbtmc_device_data *data) |
| 809 | { |
| 810 | struct usb_host_interface *current_setting; |
| 811 | struct usb_endpoint_descriptor *desc; |
| 812 | struct device *dev; |
| 813 | u8 *buffer; |
| 814 | int rv; |
| 815 | int n; |
Rickard Strandqvist | ab53eb9 | 2014-06-01 15:43:19 +0200 | [diff] [blame] | 816 | int actual = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 817 | int max_size; |
| 818 | |
| 819 | dev = &data->intf->dev; |
| 820 | |
| 821 | dev_dbg(dev, "Sending INITIATE_CLEAR request\n"); |
| 822 | |
| 823 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 824 | if (!buffer) |
| 825 | return -ENOMEM; |
| 826 | |
| 827 | rv = usb_control_msg(data->usb_dev, |
| 828 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 829 | USBTMC_REQUEST_INITIATE_CLEAR, |
| 830 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 831 | 0, 0, buffer, 1, USBTMC_TIMEOUT); |
| 832 | if (rv < 0) { |
| 833 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 834 | goto exit; |
| 835 | } |
| 836 | |
| 837 | dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); |
| 838 | |
| 839 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 840 | dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); |
| 841 | rv = -EPERM; |
| 842 | goto exit; |
| 843 | } |
| 844 | |
| 845 | max_size = 0; |
| 846 | current_setting = data->intf->cur_altsetting; |
| 847 | for (n = 0; n < current_setting->desc.bNumEndpoints; n++) { |
| 848 | desc = ¤t_setting->endpoint[n].desc; |
| 849 | if (desc->bEndpointAddress == data->bulk_in) |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 850 | max_size = usb_endpoint_maxp(desc); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if (max_size == 0) { |
| 854 | dev_err(dev, "Couldn't get wMaxPacketSize\n"); |
| 855 | rv = -EPERM; |
| 856 | goto exit; |
| 857 | } |
| 858 | |
| 859 | dev_dbg(dev, "wMaxPacketSize is %d\n", max_size); |
| 860 | |
| 861 | n = 0; |
| 862 | |
| 863 | usbtmc_clear_check_status: |
| 864 | |
| 865 | dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n"); |
| 866 | |
| 867 | rv = usb_control_msg(data->usb_dev, |
| 868 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 869 | USBTMC_REQUEST_CHECK_CLEAR_STATUS, |
| 870 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 871 | 0, 0, buffer, 2, USBTMC_TIMEOUT); |
| 872 | if (rv < 0) { |
| 873 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 874 | goto exit; |
| 875 | } |
| 876 | |
| 877 | dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); |
| 878 | |
| 879 | if (buffer[0] == USBTMC_STATUS_SUCCESS) |
| 880 | goto usbtmc_clear_bulk_out_halt; |
| 881 | |
| 882 | if (buffer[0] != USBTMC_STATUS_PENDING) { |
| 883 | dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); |
| 884 | rv = -EPERM; |
| 885 | goto exit; |
| 886 | } |
| 887 | |
| 888 | if (buffer[1] == 1) |
| 889 | do { |
| 890 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 891 | |
| 892 | rv = usb_bulk_msg(data->usb_dev, |
| 893 | usb_rcvbulkpipe(data->usb_dev, |
| 894 | data->bulk_in), |
| 895 | buffer, USBTMC_SIZE_IOBUFFER, |
| 896 | &actual, USBTMC_TIMEOUT); |
| 897 | n++; |
| 898 | |
| 899 | if (rv < 0) { |
| 900 | dev_err(dev, "usb_control_msg returned %d\n", |
| 901 | rv); |
| 902 | goto exit; |
| 903 | } |
| 904 | } while ((actual == max_size) && |
| 905 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 906 | |
| 907 | if (actual == max_size) { |
| 908 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 909 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 910 | rv = -EPERM; |
| 911 | goto exit; |
| 912 | } |
| 913 | |
| 914 | goto usbtmc_clear_check_status; |
| 915 | |
| 916 | usbtmc_clear_bulk_out_halt: |
| 917 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 918 | rv = usb_clear_halt(data->usb_dev, |
| 919 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 920 | if (rv < 0) { |
| 921 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 922 | goto exit; |
| 923 | } |
| 924 | rv = 0; |
| 925 | |
| 926 | exit: |
| 927 | kfree(buffer); |
| 928 | return rv; |
| 929 | } |
| 930 | |
| 931 | static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data) |
| 932 | { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 933 | int rv; |
| 934 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 935 | rv = usb_clear_halt(data->usb_dev, |
| 936 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 937 | |
| 938 | if (rv < 0) { |
| 939 | dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", |
| 940 | rv); |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 941 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 942 | } |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 943 | return 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data) |
| 947 | { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 948 | int rv; |
| 949 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 950 | rv = usb_clear_halt(data->usb_dev, |
| 951 | usb_rcvbulkpipe(data->usb_dev, data->bulk_in)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 952 | |
| 953 | if (rv < 0) { |
| 954 | dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", |
| 955 | rv); |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 956 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 957 | } |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 958 | return 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static int get_capabilities(struct usbtmc_device_data *data) |
| 962 | { |
| 963 | struct device *dev = &data->usb_dev->dev; |
| 964 | char *buffer; |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 965 | int rv = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 966 | |
| 967 | buffer = kmalloc(0x18, GFP_KERNEL); |
| 968 | if (!buffer) |
| 969 | return -ENOMEM; |
| 970 | |
| 971 | rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0), |
| 972 | USBTMC_REQUEST_GET_CAPABILITIES, |
| 973 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 974 | 0, 0, buffer, 0x18, USBTMC_TIMEOUT); |
| 975 | if (rv < 0) { |
| 976 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 977 | goto err_out; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 981 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 982 | dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 983 | rv = -EPERM; |
| 984 | goto err_out; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 985 | } |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 986 | dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]); |
| 987 | dev_dbg(dev, "Device capabilities are %x\n", buffer[5]); |
| 988 | dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]); |
| 989 | dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 990 | |
| 991 | data->capabilities.interface_capabilities = buffer[4]; |
| 992 | data->capabilities.device_capabilities = buffer[5]; |
| 993 | data->capabilities.usb488_interface_capabilities = buffer[14]; |
| 994 | data->capabilities.usb488_device_capabilities = buffer[15]; |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 995 | rv = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 996 | |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 997 | err_out: |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 998 | kfree(buffer); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 999 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | #define capability_attribute(name) \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1003 | static ssize_t name##_show(struct device *dev, \ |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1004 | struct device_attribute *attr, char *buf) \ |
| 1005 | { \ |
| 1006 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 1007 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 1008 | \ |
| 1009 | return sprintf(buf, "%d\n", data->capabilities.name); \ |
| 1010 | } \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1011 | static DEVICE_ATTR_RO(name) |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1012 | |
| 1013 | capability_attribute(interface_capabilities); |
| 1014 | capability_attribute(device_capabilities); |
| 1015 | capability_attribute(usb488_interface_capabilities); |
| 1016 | capability_attribute(usb488_device_capabilities); |
| 1017 | |
| 1018 | static struct attribute *capability_attrs[] = { |
| 1019 | &dev_attr_interface_capabilities.attr, |
| 1020 | &dev_attr_device_capabilities.attr, |
| 1021 | &dev_attr_usb488_interface_capabilities.attr, |
| 1022 | &dev_attr_usb488_device_capabilities.attr, |
| 1023 | NULL, |
| 1024 | }; |
| 1025 | |
| 1026 | static struct attribute_group capability_attr_grp = { |
| 1027 | .attrs = capability_attrs, |
| 1028 | }; |
| 1029 | |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1030 | static ssize_t TermChar_show(struct device *dev, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1031 | struct device_attribute *attr, char *buf) |
| 1032 | { |
| 1033 | struct usb_interface *intf = to_usb_interface(dev); |
| 1034 | struct usbtmc_device_data *data = usb_get_intfdata(intf); |
| 1035 | |
| 1036 | return sprintf(buf, "%c\n", data->TermChar); |
| 1037 | } |
| 1038 | |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1039 | static ssize_t TermChar_store(struct device *dev, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1040 | struct device_attribute *attr, |
| 1041 | const char *buf, size_t count) |
| 1042 | { |
| 1043 | struct usb_interface *intf = to_usb_interface(dev); |
| 1044 | struct usbtmc_device_data *data = usb_get_intfdata(intf); |
| 1045 | |
| 1046 | if (count < 1) |
| 1047 | return -EINVAL; |
| 1048 | data->TermChar = buf[0]; |
| 1049 | return count; |
| 1050 | } |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1051 | static DEVICE_ATTR_RW(TermChar); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1052 | |
| 1053 | #define data_attribute(name) \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1054 | static ssize_t name##_show(struct device *dev, \ |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1055 | struct device_attribute *attr, char *buf) \ |
| 1056 | { \ |
| 1057 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 1058 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 1059 | \ |
| 1060 | return sprintf(buf, "%d\n", data->name); \ |
| 1061 | } \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1062 | static ssize_t name##_store(struct device *dev, \ |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1063 | struct device_attribute *attr, \ |
| 1064 | const char *buf, size_t count) \ |
| 1065 | { \ |
| 1066 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 1067 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 1068 | ssize_t result; \ |
| 1069 | unsigned val; \ |
| 1070 | \ |
| 1071 | result = sscanf(buf, "%u\n", &val); \ |
| 1072 | if (result != 1) \ |
| 1073 | result = -EINVAL; \ |
| 1074 | data->name = val; \ |
| 1075 | if (result < 0) \ |
| 1076 | return result; \ |
| 1077 | else \ |
| 1078 | return count; \ |
| 1079 | } \ |
Greg Kroah-Hartman | 2a6eb8a | 2013-08-23 16:09:33 -0700 | [diff] [blame] | 1080 | static DEVICE_ATTR_RW(name) |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1081 | |
| 1082 | data_attribute(TermCharEnabled); |
| 1083 | data_attribute(auto_abort); |
| 1084 | |
| 1085 | static struct attribute *data_attrs[] = { |
| 1086 | &dev_attr_TermChar.attr, |
| 1087 | &dev_attr_TermCharEnabled.attr, |
| 1088 | &dev_attr_auto_abort.attr, |
| 1089 | NULL, |
| 1090 | }; |
| 1091 | |
| 1092 | static struct attribute_group data_attr_grp = { |
| 1093 | .attrs = data_attrs, |
| 1094 | }; |
| 1095 | |
| 1096 | static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data) |
| 1097 | { |
| 1098 | struct device *dev; |
| 1099 | u8 *buffer; |
| 1100 | int rv; |
| 1101 | |
| 1102 | dev = &data->intf->dev; |
| 1103 | |
| 1104 | buffer = kmalloc(2, GFP_KERNEL); |
| 1105 | if (!buffer) |
| 1106 | return -ENOMEM; |
| 1107 | |
| 1108 | rv = usb_control_msg(data->usb_dev, |
| 1109 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 1110 | USBTMC_REQUEST_INDICATOR_PULSE, |
| 1111 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 1112 | 0, 0, buffer, 0x01, USBTMC_TIMEOUT); |
| 1113 | |
| 1114 | if (rv < 0) { |
| 1115 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 1116 | goto exit; |
| 1117 | } |
| 1118 | |
| 1119 | dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); |
| 1120 | |
| 1121 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 1122 | dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); |
| 1123 | rv = -EPERM; |
| 1124 | goto exit; |
| 1125 | } |
| 1126 | rv = 0; |
| 1127 | |
| 1128 | exit: |
| 1129 | kfree(buffer); |
| 1130 | return rv; |
| 1131 | } |
| 1132 | |
| 1133 | static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1134 | { |
| 1135 | struct usbtmc_device_data *data; |
| 1136 | int retval = -EBADRQC; |
| 1137 | |
| 1138 | data = file->private_data; |
| 1139 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1140 | if (data->zombie) { |
| 1141 | retval = -ENODEV; |
| 1142 | goto skip_io_on_zombie; |
| 1143 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1144 | |
| 1145 | switch (cmd) { |
| 1146 | case USBTMC_IOCTL_CLEAR_OUT_HALT: |
| 1147 | retval = usbtmc_ioctl_clear_out_halt(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1148 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1149 | |
| 1150 | case USBTMC_IOCTL_CLEAR_IN_HALT: |
| 1151 | retval = usbtmc_ioctl_clear_in_halt(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1152 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1153 | |
| 1154 | case USBTMC_IOCTL_INDICATOR_PULSE: |
| 1155 | retval = usbtmc_ioctl_indicator_pulse(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1156 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1157 | |
| 1158 | case USBTMC_IOCTL_CLEAR: |
| 1159 | retval = usbtmc_ioctl_clear(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1160 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1161 | |
| 1162 | case USBTMC_IOCTL_ABORT_BULK_OUT: |
| 1163 | retval = usbtmc_ioctl_abort_bulk_out(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1164 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1165 | |
| 1166 | case USBTMC_IOCTL_ABORT_BULK_IN: |
| 1167 | retval = usbtmc_ioctl_abort_bulk_in(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 1168 | break; |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1169 | |
| 1170 | case USBTMC488_IOCTL_READ_STB: |
| 1171 | retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg); |
| 1172 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1175 | skip_io_on_zombie: |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1176 | mutex_unlock(&data->io_mutex); |
| 1177 | return retval; |
| 1178 | } |
| 1179 | |
Dave Penkler | 82ed338 | 2016-01-27 19:15:15 +0100 | [diff] [blame^] | 1180 | static int usbtmc_fasync(int fd, struct file *file, int on) |
| 1181 | { |
| 1182 | struct usbtmc_device_data *data = file->private_data; |
| 1183 | |
| 1184 | return fasync_helper(fd, file, on, &data->fasync); |
| 1185 | } |
| 1186 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1187 | static const struct file_operations fops = { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1188 | .owner = THIS_MODULE, |
| 1189 | .read = usbtmc_read, |
| 1190 | .write = usbtmc_write, |
| 1191 | .open = usbtmc_open, |
| 1192 | .release = usbtmc_release, |
| 1193 | .unlocked_ioctl = usbtmc_ioctl, |
Dave Penkler | 82ed338 | 2016-01-27 19:15:15 +0100 | [diff] [blame^] | 1194 | .fasync = usbtmc_fasync, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1195 | .llseek = default_llseek, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1196 | }; |
| 1197 | |
| 1198 | static struct usb_class_driver usbtmc_class = { |
| 1199 | .name = "usbtmc%d", |
| 1200 | .fops = &fops, |
| 1201 | .minor_base = USBTMC_MINOR_BASE, |
| 1202 | }; |
| 1203 | |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1204 | static void usbtmc_interrupt(struct urb *urb) |
| 1205 | { |
| 1206 | struct usbtmc_device_data *data = urb->context; |
| 1207 | struct device *dev = &data->intf->dev; |
| 1208 | int status = urb->status; |
| 1209 | int rv; |
| 1210 | |
| 1211 | dev_dbg(&data->intf->dev, "int status: %d len %d\n", |
| 1212 | status, urb->actual_length); |
| 1213 | |
| 1214 | switch (status) { |
| 1215 | case 0: /* SUCCESS */ |
| 1216 | /* check for valid STB notification */ |
| 1217 | if (data->iin_buffer[0] > 0x81) { |
| 1218 | data->bNotify1 = data->iin_buffer[0]; |
| 1219 | data->bNotify2 = data->iin_buffer[1]; |
| 1220 | atomic_set(&data->iin_data_valid, 1); |
| 1221 | wake_up_interruptible(&data->waitq); |
| 1222 | goto exit; |
| 1223 | } |
Dave Penkler | 82ed338 | 2016-01-27 19:15:15 +0100 | [diff] [blame^] | 1224 | /* check for SRQ notification */ |
| 1225 | if (data->iin_buffer[0] == 0x81) { |
| 1226 | if (data->fasync) |
| 1227 | kill_fasync(&data->fasync, |
| 1228 | SIGIO, POLL_IN); |
| 1229 | |
| 1230 | atomic_set(&data->srq_asserted, 1); |
| 1231 | wake_up_interruptible(&data->waitq); |
| 1232 | goto exit; |
| 1233 | } |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1234 | dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]); |
| 1235 | break; |
| 1236 | case -EOVERFLOW: |
| 1237 | dev_err(dev, "overflow with length %d, actual length is %d\n", |
| 1238 | data->iin_wMaxPacketSize, urb->actual_length); |
| 1239 | case -ECONNRESET: |
| 1240 | case -ENOENT: |
| 1241 | case -ESHUTDOWN: |
| 1242 | case -EILSEQ: |
| 1243 | case -ETIME: |
| 1244 | /* urb terminated, clean up */ |
| 1245 | dev_dbg(dev, "urb terminated, status: %d\n", status); |
| 1246 | return; |
| 1247 | default: |
| 1248 | dev_err(dev, "unknown status received: %d\n", status); |
| 1249 | } |
| 1250 | exit: |
| 1251 | rv = usb_submit_urb(urb, GFP_ATOMIC); |
| 1252 | if (rv) |
| 1253 | dev_err(dev, "usb_submit_urb failed: %d\n", rv); |
| 1254 | } |
| 1255 | |
| 1256 | static void usbtmc_free_int(struct usbtmc_device_data *data) |
| 1257 | { |
| 1258 | if (!data->iin_ep_present || !data->iin_urb) |
| 1259 | return; |
| 1260 | usb_kill_urb(data->iin_urb); |
| 1261 | kfree(data->iin_buffer); |
| 1262 | usb_free_urb(data->iin_urb); |
| 1263 | kref_put(&data->kref, usbtmc_delete); |
| 1264 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1265 | |
| 1266 | static int usbtmc_probe(struct usb_interface *intf, |
| 1267 | const struct usb_device_id *id) |
| 1268 | { |
| 1269 | struct usbtmc_device_data *data; |
| 1270 | struct usb_host_interface *iface_desc; |
| 1271 | struct usb_endpoint_descriptor *endpoint; |
| 1272 | int n; |
| 1273 | int retcode; |
| 1274 | |
| 1275 | dev_dbg(&intf->dev, "%s called\n", __func__); |
| 1276 | |
Andy Shevchenko | e6c7efd | 2013-07-15 11:59:41 +0300 | [diff] [blame] | 1277 | data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL); |
Peter Chen | b8f2854 | 2014-10-14 15:55:56 +0800 | [diff] [blame] | 1278 | if (!data) |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1279 | return -ENOMEM; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1280 | |
| 1281 | data->intf = intf; |
| 1282 | data->id = id; |
| 1283 | data->usb_dev = usb_get_dev(interface_to_usbdev(intf)); |
| 1284 | usb_set_intfdata(intf, data); |
| 1285 | kref_init(&data->kref); |
| 1286 | mutex_init(&data->io_mutex); |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1287 | init_waitqueue_head(&data->waitq); |
| 1288 | atomic_set(&data->iin_data_valid, 0); |
Dave Penkler | 82ed338 | 2016-01-27 19:15:15 +0100 | [diff] [blame^] | 1289 | atomic_set(&data->srq_asserted, 0); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1290 | data->zombie = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1291 | |
Alexandre Peixoto Ferreira | c2e3148 | 2013-04-30 00:51:52 -0500 | [diff] [blame] | 1292 | /* Determine if it is a Rigol or not */ |
| 1293 | data->rigol_quirk = 0; |
| 1294 | dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n", |
Johan Hovold | c786138 | 2013-08-11 16:49:21 +0200 | [diff] [blame] | 1295 | le16_to_cpu(data->usb_dev->descriptor.idVendor), |
| 1296 | le16_to_cpu(data->usb_dev->descriptor.idProduct)); |
Alexandre Peixoto Ferreira | c2e3148 | 2013-04-30 00:51:52 -0500 | [diff] [blame] | 1297 | for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) { |
Johan Hovold | c786138 | 2013-08-11 16:49:21 +0200 | [diff] [blame] | 1298 | if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) && |
| 1299 | (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) { |
Alexandre Peixoto Ferreira | c2e3148 | 2013-04-30 00:51:52 -0500 | [diff] [blame] | 1300 | dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n"); |
| 1301 | data->rigol_quirk = 1; |
| 1302 | break; |
| 1303 | } |
| 1304 | } |
| 1305 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1306 | /* Initialize USBTMC bTag and other fields */ |
| 1307 | data->bTag = 1; |
| 1308 | data->TermCharEnabled = 0; |
| 1309 | data->TermChar = '\n'; |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1310 | /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */ |
| 1311 | data->iin_bTag = 2; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1312 | |
| 1313 | /* USBTMC devices have only one setting, so use that */ |
| 1314 | iface_desc = data->intf->cur_altsetting; |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1315 | data->ifnum = iface_desc->desc.bInterfaceNumber; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1316 | |
| 1317 | /* Find bulk in endpoint */ |
| 1318 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { |
| 1319 | endpoint = &iface_desc->endpoint[n].desc; |
| 1320 | |
| 1321 | if (usb_endpoint_is_bulk_in(endpoint)) { |
| 1322 | data->bulk_in = endpoint->bEndpointAddress; |
| 1323 | dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n", |
| 1324 | data->bulk_in); |
| 1325 | break; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | /* Find bulk out endpoint */ |
| 1330 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { |
| 1331 | endpoint = &iface_desc->endpoint[n].desc; |
| 1332 | |
| 1333 | if (usb_endpoint_is_bulk_out(endpoint)) { |
| 1334 | data->bulk_out = endpoint->bEndpointAddress; |
| 1335 | dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n", |
| 1336 | data->bulk_out); |
| 1337 | break; |
| 1338 | } |
| 1339 | } |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1340 | /* Find int endpoint */ |
| 1341 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { |
| 1342 | endpoint = &iface_desc->endpoint[n].desc; |
| 1343 | |
| 1344 | if (usb_endpoint_is_int_in(endpoint)) { |
| 1345 | data->iin_ep_present = 1; |
| 1346 | data->iin_ep = endpoint->bEndpointAddress; |
| 1347 | data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint); |
| 1348 | data->iin_interval = endpoint->bInterval; |
| 1349 | dev_dbg(&intf->dev, "Found Int in endpoint at %u\n", |
| 1350 | data->iin_ep); |
| 1351 | break; |
| 1352 | } |
| 1353 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1354 | |
| 1355 | retcode = get_capabilities(data); |
| 1356 | if (retcode) |
| 1357 | dev_err(&intf->dev, "can't read capabilities\n"); |
| 1358 | else |
| 1359 | retcode = sysfs_create_group(&intf->dev.kobj, |
| 1360 | &capability_attr_grp); |
| 1361 | |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1362 | if (data->iin_ep_present) { |
| 1363 | /* allocate int urb */ |
| 1364 | data->iin_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1365 | if (!data->iin_urb) { |
| 1366 | dev_err(&intf->dev, "Failed to allocate int urb\n"); |
| 1367 | goto error_register; |
| 1368 | } |
| 1369 | |
| 1370 | /* will reference data in int urb */ |
| 1371 | kref_get(&data->kref); |
| 1372 | |
| 1373 | /* allocate buffer for interrupt in */ |
| 1374 | data->iin_buffer = kmalloc(data->iin_wMaxPacketSize, |
| 1375 | GFP_KERNEL); |
| 1376 | if (!data->iin_buffer) { |
| 1377 | dev_err(&intf->dev, "Failed to allocate int buf\n"); |
| 1378 | goto error_register; |
| 1379 | } |
| 1380 | |
| 1381 | /* fill interrupt urb */ |
| 1382 | usb_fill_int_urb(data->iin_urb, data->usb_dev, |
| 1383 | usb_rcvintpipe(data->usb_dev, data->iin_ep), |
| 1384 | data->iin_buffer, data->iin_wMaxPacketSize, |
| 1385 | usbtmc_interrupt, |
| 1386 | data, data->iin_interval); |
| 1387 | |
| 1388 | retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL); |
| 1389 | if (retcode) { |
| 1390 | dev_err(&intf->dev, "Failed to submit iin_urb\n"); |
| 1391 | goto error_register; |
| 1392 | } |
| 1393 | } |
| 1394 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1395 | retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp); |
| 1396 | |
| 1397 | retcode = usb_register_dev(intf, &usbtmc_class); |
| 1398 | if (retcode) { |
| 1399 | dev_err(&intf->dev, "Not able to get a minor" |
| 1400 | " (base %u, slice default): %d\n", USBTMC_MINOR_BASE, |
| 1401 | retcode); |
| 1402 | goto error_register; |
| 1403 | } |
| 1404 | dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor); |
| 1405 | |
| 1406 | return 0; |
| 1407 | |
| 1408 | error_register: |
| 1409 | sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp); |
| 1410 | sysfs_remove_group(&intf->dev.kobj, &data_attr_grp); |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1411 | usbtmc_free_int(data); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1412 | kref_put(&data->kref, usbtmc_delete); |
| 1413 | return retcode; |
| 1414 | } |
| 1415 | |
| 1416 | static void usbtmc_disconnect(struct usb_interface *intf) |
| 1417 | { |
| 1418 | struct usbtmc_device_data *data; |
| 1419 | |
| 1420 | dev_dbg(&intf->dev, "usbtmc_disconnect called\n"); |
| 1421 | |
| 1422 | data = usb_get_intfdata(intf); |
Dave Penkler | dbf3e7f | 2016-01-27 19:09:24 +0100 | [diff] [blame] | 1423 | usbtmc_free_int(data); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1424 | usb_deregister_dev(intf, &usbtmc_class); |
| 1425 | sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp); |
| 1426 | sysfs_remove_group(&intf->dev.kobj, &data_attr_grp); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1427 | mutex_lock(&data->io_mutex); |
| 1428 | data->zombie = 1; |
| 1429 | mutex_unlock(&data->io_mutex); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1430 | kref_put(&data->kref, usbtmc_delete); |
| 1431 | } |
| 1432 | |
Oliver Neukum | dca8cd0 | 2009-09-24 00:33:45 +0200 | [diff] [blame] | 1433 | static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message) |
Oliver Neukum | a470810 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1434 | { |
| 1435 | /* this driver does not have pending URBs */ |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
Oliver Neukum | dca8cd0 | 2009-09-24 00:33:45 +0200 | [diff] [blame] | 1439 | static int usbtmc_resume(struct usb_interface *intf) |
Oliver Neukum | a470810 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1440 | { |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1444 | static struct usb_driver usbtmc_driver = { |
| 1445 | .name = "usbtmc", |
| 1446 | .id_table = usbtmc_devices, |
| 1447 | .probe = usbtmc_probe, |
Oliver Neukum | a470810 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1448 | .disconnect = usbtmc_disconnect, |
| 1449 | .suspend = usbtmc_suspend, |
| 1450 | .resume = usbtmc_resume, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1451 | }; |
| 1452 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1453 | module_usb_driver(usbtmc_driver); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1454 | |
| 1455 | MODULE_LICENSE("GPL"); |