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 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
Ilpo Järvinen | 857cc4d | 2008-10-30 13:56:47 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 25 | #include <linux/fs.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/kref.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 29 | #include <linux/mutex.h> |
| 30 | #include <linux/usb.h> |
| 31 | #include <linux/usb/tmc.h> |
| 32 | |
| 33 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 34 | #define RIGOL 1 |
| 35 | #define USBTMC_HEADER_SIZE 12 |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 36 | #define USBTMC_MINOR_BASE 176 |
| 37 | |
| 38 | /* |
| 39 | * Size of driver internal IO buffer. Must be multiple of 4 and at least as |
| 40 | * large as wMaxPacketSize (which is usually 512 bytes). |
| 41 | */ |
| 42 | #define USBTMC_SIZE_IOBUFFER 2048 |
| 43 | |
| 44 | /* Default USB timeout (in milliseconds) */ |
Gergely Imreh | 35f76e89 | 2009-09-15 16:03:31 +0800 | [diff] [blame] | 45 | #define USBTMC_TIMEOUT 5000 |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Maximum number of read cycles to empty bulk in endpoint during CLEAR and |
| 49 | * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short |
| 50 | * packet is never read. |
| 51 | */ |
| 52 | #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100 |
| 53 | |
Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 54 | static const struct usb_device_id usbtmc_devices[] = { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 55 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), }, |
Greg Kroah-Hartman | 228dd05 | 2009-03-11 13:51:42 -0700 | [diff] [blame] | 56 | { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), }, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 57 | { 0, } /* terminating entry */ |
| 58 | }; |
Greg Kroah-Hartman | 5413aa4 | 2008-12-03 16:33:09 -0800 | [diff] [blame] | 59 | MODULE_DEVICE_TABLE(usb, usbtmc_devices); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * This structure is the capabilities for the device |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 63 | * See section 4.2.1.8 of the USBTMC specification, |
| 64 | * and section 4.2.2 of the USBTMC usb488 subclass |
| 65 | * specification for details. |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 66 | */ |
| 67 | struct usbtmc_dev_capabilities { |
| 68 | __u8 interface_capabilities; |
| 69 | __u8 device_capabilities; |
| 70 | __u8 usb488_interface_capabilities; |
| 71 | __u8 usb488_device_capabilities; |
| 72 | }; |
| 73 | |
| 74 | /* This structure holds private data for each USBTMC device. One copy is |
| 75 | * allocated for each USBTMC device in the driver's probe function. |
| 76 | */ |
| 77 | struct usbtmc_device_data { |
| 78 | const struct usb_device_id *id; |
| 79 | struct usb_device *usb_dev; |
| 80 | struct usb_interface *intf; |
| 81 | |
| 82 | unsigned int bulk_in; |
| 83 | unsigned int bulk_out; |
| 84 | |
| 85 | u8 bTag; |
| 86 | u8 bTag_last_write; /* needed for abort */ |
| 87 | u8 bTag_last_read; /* needed for abort */ |
| 88 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 89 | u8 rigol_quirk; |
| 90 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 91 | /* attributes from the USB TMC spec for this device */ |
| 92 | u8 TermChar; |
| 93 | bool TermCharEnabled; |
| 94 | bool auto_abort; |
| 95 | |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 96 | bool zombie; /* fd of disconnected device */ |
| 97 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 98 | struct usbtmc_dev_capabilities capabilities; |
| 99 | struct kref kref; |
| 100 | struct mutex io_mutex; /* only one i/o function running at a time */ |
| 101 | }; |
| 102 | #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref) |
| 103 | |
Alexandre Peixoto Ferreira | 8402db5 | 2013-04-30 00:51:50 -0500 | [diff] [blame] | 104 | struct usbtmc_ID_rigol_quirk { |
| 105 | __u16 idVendor; |
| 106 | __u16 idProduct; |
| 107 | }; |
| 108 | |
| 109 | static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = { |
| 110 | { 0x1ab1, 0x0588 }, |
| 111 | { 0, 0 } |
| 112 | }; |
| 113 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 114 | /* Forward declarations */ |
| 115 | static struct usb_driver usbtmc_driver; |
| 116 | |
| 117 | static void usbtmc_delete(struct kref *kref) |
| 118 | { |
| 119 | struct usbtmc_device_data *data = to_usbtmc_data(kref); |
| 120 | |
| 121 | usb_put_dev(data->usb_dev); |
| 122 | kfree(data); |
| 123 | } |
| 124 | |
| 125 | static int usbtmc_open(struct inode *inode, struct file *filp) |
| 126 | { |
| 127 | struct usb_interface *intf; |
| 128 | struct usbtmc_device_data *data; |
Greg Kroah-Hartman | 5b10916 | 2009-03-10 20:42:55 -0700 | [diff] [blame] | 129 | int retval = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 130 | |
| 131 | intf = usb_find_interface(&usbtmc_driver, iminor(inode)); |
| 132 | if (!intf) { |
| 133 | printk(KERN_ERR KBUILD_MODNAME |
| 134 | ": can not find device for minor %d", iminor(inode)); |
Greg Kroah-Hartman | 5b10916 | 2009-03-10 20:42:55 -0700 | [diff] [blame] | 135 | retval = -ENODEV; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 136 | goto exit; |
| 137 | } |
| 138 | |
| 139 | data = usb_get_intfdata(intf); |
| 140 | kref_get(&data->kref); |
| 141 | |
| 142 | /* Store pointer in file structure's private data field */ |
| 143 | filp->private_data = data; |
| 144 | |
| 145 | exit: |
| 146 | return retval; |
| 147 | } |
| 148 | |
| 149 | static int usbtmc_release(struct inode *inode, struct file *file) |
| 150 | { |
| 151 | struct usbtmc_device_data *data = file->private_data; |
| 152 | |
| 153 | kref_put(&data->kref, usbtmc_delete); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data) |
| 158 | { |
Chris Malley | b361a6e | 2008-10-25 22:07:32 +0100 | [diff] [blame] | 159 | u8 *buffer; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 160 | struct device *dev; |
| 161 | int rv; |
| 162 | int n; |
| 163 | int actual; |
| 164 | struct usb_host_interface *current_setting; |
| 165 | int max_size; |
| 166 | |
| 167 | dev = &data->intf->dev; |
| 168 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 169 | if (!buffer) |
| 170 | return -ENOMEM; |
| 171 | |
| 172 | rv = usb_control_msg(data->usb_dev, |
| 173 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 174 | USBTMC_REQUEST_INITIATE_ABORT_BULK_IN, |
| 175 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 176 | data->bTag_last_read, data->bulk_in, |
| 177 | buffer, 2, USBTMC_TIMEOUT); |
| 178 | |
| 179 | if (rv < 0) { |
| 180 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 181 | goto exit; |
| 182 | } |
| 183 | |
| 184 | dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 185 | |
| 186 | if (buffer[0] == USBTMC_STATUS_FAILED) { |
| 187 | rv = 0; |
| 188 | goto exit; |
| 189 | } |
| 190 | |
| 191 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 192 | dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", |
| 193 | buffer[0]); |
| 194 | rv = -EPERM; |
| 195 | goto exit; |
| 196 | } |
| 197 | |
| 198 | max_size = 0; |
| 199 | current_setting = data->intf->cur_altsetting; |
| 200 | for (n = 0; n < current_setting->desc.bNumEndpoints; n++) |
| 201 | if (current_setting->endpoint[n].desc.bEndpointAddress == |
| 202 | data->bulk_in) |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 203 | max_size = usb_endpoint_maxp(¤t_setting->endpoint[n].desc); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 204 | |
| 205 | if (max_size == 0) { |
| 206 | dev_err(dev, "Couldn't get wMaxPacketSize\n"); |
| 207 | rv = -EPERM; |
| 208 | goto exit; |
| 209 | } |
| 210 | |
| 211 | dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size); |
| 212 | |
| 213 | n = 0; |
| 214 | |
| 215 | do { |
| 216 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 217 | |
| 218 | rv = usb_bulk_msg(data->usb_dev, |
| 219 | usb_rcvbulkpipe(data->usb_dev, |
| 220 | data->bulk_in), |
| 221 | buffer, USBTMC_SIZE_IOBUFFER, |
| 222 | &actual, USBTMC_TIMEOUT); |
| 223 | |
| 224 | n++; |
| 225 | |
| 226 | if (rv < 0) { |
| 227 | dev_err(dev, "usb_bulk_msg returned %d\n", rv); |
| 228 | goto exit; |
| 229 | } |
| 230 | } while ((actual == max_size) && |
| 231 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 232 | |
| 233 | if (actual == max_size) { |
| 234 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 235 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 236 | rv = -EPERM; |
| 237 | goto exit; |
| 238 | } |
| 239 | |
| 240 | n = 0; |
| 241 | |
| 242 | usbtmc_abort_bulk_in_status: |
| 243 | rv = usb_control_msg(data->usb_dev, |
| 244 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 245 | USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS, |
| 246 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 247 | 0, data->bulk_in, buffer, 0x08, |
| 248 | USBTMC_TIMEOUT); |
| 249 | |
| 250 | if (rv < 0) { |
| 251 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 252 | goto exit; |
| 253 | } |
| 254 | |
| 255 | dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 256 | |
| 257 | if (buffer[0] == USBTMC_STATUS_SUCCESS) { |
| 258 | rv = 0; |
| 259 | goto exit; |
| 260 | } |
| 261 | |
| 262 | if (buffer[0] != USBTMC_STATUS_PENDING) { |
| 263 | dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]); |
| 264 | rv = -EPERM; |
| 265 | goto exit; |
| 266 | } |
| 267 | |
| 268 | if (buffer[1] == 1) |
| 269 | do { |
| 270 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 271 | |
| 272 | rv = usb_bulk_msg(data->usb_dev, |
| 273 | usb_rcvbulkpipe(data->usb_dev, |
| 274 | data->bulk_in), |
| 275 | buffer, USBTMC_SIZE_IOBUFFER, |
| 276 | &actual, USBTMC_TIMEOUT); |
| 277 | |
| 278 | n++; |
| 279 | |
| 280 | if (rv < 0) { |
| 281 | dev_err(dev, "usb_bulk_msg returned %d\n", rv); |
| 282 | goto exit; |
| 283 | } |
Maxim Nikulin | 4f1a7a3 | 2011-07-09 23:44:44 +0700 | [diff] [blame] | 284 | } while ((actual == max_size) && |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 285 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 286 | |
| 287 | if (actual == max_size) { |
| 288 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 289 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 290 | rv = -EPERM; |
| 291 | goto exit; |
| 292 | } |
| 293 | |
| 294 | goto usbtmc_abort_bulk_in_status; |
| 295 | |
| 296 | exit: |
| 297 | kfree(buffer); |
| 298 | return rv; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data) |
| 303 | { |
| 304 | struct device *dev; |
| 305 | u8 *buffer; |
| 306 | int rv; |
| 307 | int n; |
| 308 | |
| 309 | dev = &data->intf->dev; |
| 310 | |
| 311 | buffer = kmalloc(8, GFP_KERNEL); |
| 312 | if (!buffer) |
| 313 | return -ENOMEM; |
| 314 | |
| 315 | rv = usb_control_msg(data->usb_dev, |
| 316 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 317 | USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT, |
| 318 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 319 | data->bTag_last_write, data->bulk_out, |
| 320 | buffer, 2, USBTMC_TIMEOUT); |
| 321 | |
| 322 | if (rv < 0) { |
| 323 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 324 | goto exit; |
| 325 | } |
| 326 | |
| 327 | dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]); |
| 328 | |
| 329 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 330 | dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", |
| 331 | buffer[0]); |
| 332 | rv = -EPERM; |
| 333 | goto exit; |
| 334 | } |
| 335 | |
| 336 | n = 0; |
| 337 | |
| 338 | usbtmc_abort_bulk_out_check_status: |
| 339 | rv = usb_control_msg(data->usb_dev, |
| 340 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 341 | USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS, |
| 342 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, |
| 343 | 0, data->bulk_out, buffer, 0x08, |
| 344 | USBTMC_TIMEOUT); |
| 345 | n++; |
| 346 | if (rv < 0) { |
| 347 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 348 | goto exit; |
| 349 | } |
| 350 | |
| 351 | dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]); |
| 352 | |
| 353 | if (buffer[0] == USBTMC_STATUS_SUCCESS) |
| 354 | goto usbtmc_abort_bulk_out_clear_halt; |
| 355 | |
| 356 | if ((buffer[0] == USBTMC_STATUS_PENDING) && |
| 357 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)) |
| 358 | goto usbtmc_abort_bulk_out_check_status; |
| 359 | |
| 360 | rv = -EPERM; |
| 361 | goto exit; |
| 362 | |
| 363 | usbtmc_abort_bulk_out_clear_halt: |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 364 | rv = usb_clear_halt(data->usb_dev, |
| 365 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 366 | |
| 367 | if (rv < 0) { |
| 368 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 369 | goto exit; |
| 370 | } |
| 371 | rv = 0; |
| 372 | |
| 373 | exit: |
| 374 | kfree(buffer); |
| 375 | return rv; |
| 376 | } |
| 377 | |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 378 | /* |
| 379 | * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint. |
| 380 | * @transfer_size: number of bytes to request from the device. |
| 381 | * |
| 382 | * See the USBTMC specification, Table 4. |
| 383 | * |
| 384 | * Also updates bTag_last_write. |
| 385 | */ |
| 386 | static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size) |
| 387 | { |
| 388 | int retval; |
| 389 | u8 buffer[USBTMC_HEADER_SIZE]; |
| 390 | int actual; |
| 391 | |
| 392 | /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message |
| 393 | * Refer to class specs for details |
| 394 | */ |
| 395 | buffer[0] = 2; |
| 396 | buffer[1] = data->bTag; |
| 397 | buffer[2] = ~(data->bTag); |
| 398 | buffer[3] = 0; /* Reserved */ |
| 399 | buffer[4] = (transfer_size) & 255; |
| 400 | buffer[5] = ((transfer_size) >> 8) & 255; |
| 401 | buffer[6] = ((transfer_size) >> 16) & 255; |
| 402 | buffer[7] = ((transfer_size) >> 24) & 255; |
| 403 | buffer[8] = data->TermCharEnabled * 2; |
| 404 | /* Use term character? */ |
| 405 | buffer[9] = data->TermChar; |
| 406 | buffer[10] = 0; /* Reserved */ |
| 407 | buffer[11] = 0; /* Reserved */ |
| 408 | |
| 409 | /* Send bulk URB */ |
| 410 | retval = usb_bulk_msg(data->usb_dev, |
| 411 | usb_sndbulkpipe(data->usb_dev, |
| 412 | data->bulk_out), |
| 413 | buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT); |
| 414 | |
| 415 | /* Store bTag (in case we need to abort) */ |
| 416 | data->bTag_last_write = data->bTag; |
| 417 | |
| 418 | /* Increment bTag -- and increment again if zero */ |
| 419 | data->bTag++; |
| 420 | if (!data->bTag) |
| 421 | (data->bTag)++; |
| 422 | |
| 423 | if (retval < 0) { |
| 424 | dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval); |
| 425 | return retval; |
| 426 | } |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 431 | static ssize_t usbtmc_read(struct file *filp, char __user *buf, |
| 432 | size_t count, loff_t *f_pos) |
| 433 | { |
| 434 | struct usbtmc_device_data *data; |
| 435 | struct device *dev; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 436 | u32 n_characters; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 437 | u8 *buffer; |
| 438 | int actual; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 439 | size_t done; |
| 440 | size_t remaining; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 441 | int retval; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 442 | size_t this_part; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 443 | |
| 444 | /* Get pointer to private data structure */ |
| 445 | data = filp->private_data; |
| 446 | dev = &data->intf->dev; |
| 447 | |
| 448 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 449 | if (!buffer) |
| 450 | return -ENOMEM; |
| 451 | |
| 452 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 453 | if (data->zombie) { |
| 454 | retval = -ENODEV; |
| 455 | goto exit; |
| 456 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 457 | |
| 458 | remaining = count; |
| 459 | done = 0; |
| 460 | |
| 461 | while (remaining > 0) { |
| 462 | if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3) |
| 463 | this_part = USBTMC_SIZE_IOBUFFER - 12 - 3; |
| 464 | else |
| 465 | this_part = remaining; |
| 466 | |
Alexandre Peixoto Ferreira | 88d9b2b | 2013-04-30 00:51:51 -0500 | [diff] [blame] | 467 | retval = send_request_dev_dep_msg_in(data, this_part); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 468 | if (retval < 0) { |
| 469 | dev_err(dev, "usb_bulk_msg returned %d\n", retval); |
| 470 | if (data->auto_abort) |
| 471 | usbtmc_ioctl_abort_bulk_out(data); |
| 472 | goto exit; |
| 473 | } |
| 474 | |
| 475 | /* Send bulk URB */ |
| 476 | retval = usb_bulk_msg(data->usb_dev, |
| 477 | usb_rcvbulkpipe(data->usb_dev, |
| 478 | data->bulk_in), |
| 479 | buffer, USBTMC_SIZE_IOBUFFER, &actual, |
| 480 | USBTMC_TIMEOUT); |
| 481 | |
| 482 | /* Store bTag (in case we need to abort) */ |
| 483 | data->bTag_last_read = data->bTag; |
| 484 | |
| 485 | if (retval < 0) { |
| 486 | dev_err(dev, "Unable to read data, error %d\n", retval); |
| 487 | if (data->auto_abort) |
| 488 | usbtmc_ioctl_abort_bulk_in(data); |
| 489 | goto exit; |
| 490 | } |
| 491 | |
| 492 | /* How many characters did the instrument send? */ |
| 493 | n_characters = buffer[4] + |
| 494 | (buffer[5] << 8) + |
| 495 | (buffer[6] << 16) + |
| 496 | (buffer[7] << 24); |
| 497 | |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 498 | /* Ensure the instrument doesn't lie about it */ |
| 499 | if(n_characters > actual - 12) { |
Randy Dunlap | a2fbf10 | 2009-07-28 11:22:41 -0700 | [diff] [blame] | 500 | dev_err(dev, "Device lies about message size: %u > %d\n", n_characters, actual - 12); |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 501 | n_characters = actual - 12; |
| 502 | } |
| 503 | |
| 504 | /* Ensure the instrument doesn't send more back than requested */ |
| 505 | if(n_characters > this_part) { |
| 506 | dev_err(dev, "Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part); |
| 507 | n_characters = this_part; |
| 508 | } |
| 509 | |
Steve Holland | 92d07e42 | 2009-06-18 17:37:49 -0500 | [diff] [blame] | 510 | /* Bound amount of data received by amount of data requested */ |
| 511 | if (n_characters > this_part) |
| 512 | n_characters = this_part; |
| 513 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 514 | /* Copy buffer to user space */ |
| 515 | if (copy_to_user(buf + done, &buffer[12], n_characters)) { |
| 516 | /* There must have been an addressing problem */ |
| 517 | retval = -EFAULT; |
| 518 | goto exit; |
| 519 | } |
| 520 | |
| 521 | done += n_characters; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 522 | /* Terminate if end-of-message bit received from device */ |
Steve Holland | 4143d17 | 2009-06-18 17:37:49 -0500 | [diff] [blame] | 523 | if ((buffer[8] & 0x01) && (actual >= n_characters + 12)) |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 524 | remaining = 0; |
Guus Sliepen | 665d766 | 2009-07-22 17:39:42 +0200 | [diff] [blame] | 525 | else |
| 526 | remaining -= n_characters; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* Update file position value */ |
| 530 | *f_pos = *f_pos + done; |
| 531 | retval = done; |
| 532 | |
| 533 | exit: |
| 534 | mutex_unlock(&data->io_mutex); |
| 535 | kfree(buffer); |
| 536 | return retval; |
| 537 | } |
| 538 | |
| 539 | static ssize_t usbtmc_write(struct file *filp, const char __user *buf, |
| 540 | size_t count, loff_t *f_pos) |
| 541 | { |
| 542 | struct usbtmc_device_data *data; |
| 543 | u8 *buffer; |
| 544 | int retval; |
| 545 | int actual; |
| 546 | unsigned long int n_bytes; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 547 | int remaining; |
| 548 | int done; |
| 549 | int this_part; |
| 550 | |
| 551 | data = filp->private_data; |
| 552 | |
| 553 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 554 | if (!buffer) |
| 555 | return -ENOMEM; |
| 556 | |
| 557 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 558 | if (data->zombie) { |
| 559 | retval = -ENODEV; |
| 560 | goto exit; |
| 561 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 562 | |
| 563 | remaining = count; |
| 564 | done = 0; |
| 565 | |
| 566 | while (remaining > 0) { |
| 567 | if (remaining > USBTMC_SIZE_IOBUFFER - 12) { |
| 568 | this_part = USBTMC_SIZE_IOBUFFER - 12; |
| 569 | buffer[8] = 0; |
| 570 | } else { |
| 571 | this_part = remaining; |
| 572 | buffer[8] = 1; |
| 573 | } |
| 574 | |
| 575 | /* Setup IO buffer for DEV_DEP_MSG_OUT message */ |
| 576 | buffer[0] = 1; |
| 577 | buffer[1] = data->bTag; |
| 578 | buffer[2] = ~(data->bTag); |
| 579 | buffer[3] = 0; /* Reserved */ |
| 580 | buffer[4] = this_part & 255; |
| 581 | buffer[5] = (this_part >> 8) & 255; |
| 582 | buffer[6] = (this_part >> 16) & 255; |
| 583 | buffer[7] = (this_part >> 24) & 255; |
| 584 | /* buffer[8] is set above... */ |
| 585 | buffer[9] = 0; /* Reserved */ |
| 586 | buffer[10] = 0; /* Reserved */ |
| 587 | buffer[11] = 0; /* Reserved */ |
| 588 | |
| 589 | if (copy_from_user(&buffer[12], buf + done, this_part)) { |
| 590 | retval = -EFAULT; |
| 591 | goto exit; |
| 592 | } |
| 593 | |
Ilpo Järvinen | 857cc4d | 2008-10-30 13:56:47 +0200 | [diff] [blame] | 594 | n_bytes = roundup(12 + this_part, 4); |
| 595 | memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 596 | |
Andre Herms | ec412b9 | 2009-11-19 18:14:49 +0100 | [diff] [blame] | 597 | do { |
| 598 | retval = usb_bulk_msg(data->usb_dev, |
| 599 | usb_sndbulkpipe(data->usb_dev, |
| 600 | data->bulk_out), |
| 601 | buffer, n_bytes, |
| 602 | &actual, USBTMC_TIMEOUT); |
| 603 | if (retval != 0) |
| 604 | break; |
| 605 | n_bytes -= actual; |
| 606 | } while (n_bytes); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 607 | |
| 608 | data->bTag_last_write = data->bTag; |
| 609 | data->bTag++; |
| 610 | |
| 611 | if (!data->bTag) |
| 612 | data->bTag++; |
| 613 | |
| 614 | if (retval < 0) { |
| 615 | dev_err(&data->intf->dev, |
| 616 | "Unable to send data, error %d\n", retval); |
| 617 | if (data->auto_abort) |
| 618 | usbtmc_ioctl_abort_bulk_out(data); |
| 619 | goto exit; |
| 620 | } |
| 621 | |
| 622 | remaining -= this_part; |
| 623 | done += this_part; |
| 624 | } |
| 625 | |
| 626 | retval = count; |
| 627 | exit: |
| 628 | mutex_unlock(&data->io_mutex); |
| 629 | kfree(buffer); |
| 630 | return retval; |
| 631 | } |
| 632 | |
| 633 | static int usbtmc_ioctl_clear(struct usbtmc_device_data *data) |
| 634 | { |
| 635 | struct usb_host_interface *current_setting; |
| 636 | struct usb_endpoint_descriptor *desc; |
| 637 | struct device *dev; |
| 638 | u8 *buffer; |
| 639 | int rv; |
| 640 | int n; |
| 641 | int actual; |
| 642 | int max_size; |
| 643 | |
| 644 | dev = &data->intf->dev; |
| 645 | |
| 646 | dev_dbg(dev, "Sending INITIATE_CLEAR request\n"); |
| 647 | |
| 648 | buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL); |
| 649 | if (!buffer) |
| 650 | return -ENOMEM; |
| 651 | |
| 652 | rv = usb_control_msg(data->usb_dev, |
| 653 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 654 | USBTMC_REQUEST_INITIATE_CLEAR, |
| 655 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 656 | 0, 0, buffer, 1, USBTMC_TIMEOUT); |
| 657 | if (rv < 0) { |
| 658 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 659 | goto exit; |
| 660 | } |
| 661 | |
| 662 | dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); |
| 663 | |
| 664 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 665 | dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]); |
| 666 | rv = -EPERM; |
| 667 | goto exit; |
| 668 | } |
| 669 | |
| 670 | max_size = 0; |
| 671 | current_setting = data->intf->cur_altsetting; |
| 672 | for (n = 0; n < current_setting->desc.bNumEndpoints; n++) { |
| 673 | desc = ¤t_setting->endpoint[n].desc; |
| 674 | if (desc->bEndpointAddress == data->bulk_in) |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 675 | max_size = usb_endpoint_maxp(desc); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (max_size == 0) { |
| 679 | dev_err(dev, "Couldn't get wMaxPacketSize\n"); |
| 680 | rv = -EPERM; |
| 681 | goto exit; |
| 682 | } |
| 683 | |
| 684 | dev_dbg(dev, "wMaxPacketSize is %d\n", max_size); |
| 685 | |
| 686 | n = 0; |
| 687 | |
| 688 | usbtmc_clear_check_status: |
| 689 | |
| 690 | dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n"); |
| 691 | |
| 692 | rv = usb_control_msg(data->usb_dev, |
| 693 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 694 | USBTMC_REQUEST_CHECK_CLEAR_STATUS, |
| 695 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 696 | 0, 0, buffer, 2, USBTMC_TIMEOUT); |
| 697 | if (rv < 0) { |
| 698 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 699 | goto exit; |
| 700 | } |
| 701 | |
| 702 | dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); |
| 703 | |
| 704 | if (buffer[0] == USBTMC_STATUS_SUCCESS) |
| 705 | goto usbtmc_clear_bulk_out_halt; |
| 706 | |
| 707 | if (buffer[0] != USBTMC_STATUS_PENDING) { |
| 708 | dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]); |
| 709 | rv = -EPERM; |
| 710 | goto exit; |
| 711 | } |
| 712 | |
| 713 | if (buffer[1] == 1) |
| 714 | do { |
| 715 | dev_dbg(dev, "Reading from bulk in EP\n"); |
| 716 | |
| 717 | rv = usb_bulk_msg(data->usb_dev, |
| 718 | usb_rcvbulkpipe(data->usb_dev, |
| 719 | data->bulk_in), |
| 720 | buffer, USBTMC_SIZE_IOBUFFER, |
| 721 | &actual, USBTMC_TIMEOUT); |
| 722 | n++; |
| 723 | |
| 724 | if (rv < 0) { |
| 725 | dev_err(dev, "usb_control_msg returned %d\n", |
| 726 | rv); |
| 727 | goto exit; |
| 728 | } |
| 729 | } while ((actual == max_size) && |
| 730 | (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN)); |
| 731 | |
| 732 | if (actual == max_size) { |
| 733 | dev_err(dev, "Couldn't clear device buffer within %d cycles\n", |
| 734 | USBTMC_MAX_READS_TO_CLEAR_BULK_IN); |
| 735 | rv = -EPERM; |
| 736 | goto exit; |
| 737 | } |
| 738 | |
| 739 | goto usbtmc_clear_check_status; |
| 740 | |
| 741 | usbtmc_clear_bulk_out_halt: |
| 742 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 743 | rv = usb_clear_halt(data->usb_dev, |
| 744 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 745 | if (rv < 0) { |
| 746 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 747 | goto exit; |
| 748 | } |
| 749 | rv = 0; |
| 750 | |
| 751 | exit: |
| 752 | kfree(buffer); |
| 753 | return rv; |
| 754 | } |
| 755 | |
| 756 | static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data) |
| 757 | { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 758 | int rv; |
| 759 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 760 | rv = usb_clear_halt(data->usb_dev, |
| 761 | usb_sndbulkpipe(data->usb_dev, data->bulk_out)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 762 | |
| 763 | if (rv < 0) { |
| 764 | dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", |
| 765 | rv); |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 766 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 767 | } |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 768 | return 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data) |
| 772 | { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 773 | int rv; |
| 774 | |
Sarah Sharp | 3342ecd | 2009-12-03 11:35:59 -0800 | [diff] [blame] | 775 | rv = usb_clear_halt(data->usb_dev, |
| 776 | usb_rcvbulkpipe(data->usb_dev, data->bulk_in)); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 777 | |
| 778 | if (rv < 0) { |
| 779 | dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", |
| 780 | rv); |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 781 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 782 | } |
Ming Lei | ac9e59c | 2013-04-18 12:17:38 +0800 | [diff] [blame] | 783 | return 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static int get_capabilities(struct usbtmc_device_data *data) |
| 787 | { |
| 788 | struct device *dev = &data->usb_dev->dev; |
| 789 | char *buffer; |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 790 | int rv = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 791 | |
| 792 | buffer = kmalloc(0x18, GFP_KERNEL); |
| 793 | if (!buffer) |
| 794 | return -ENOMEM; |
| 795 | |
| 796 | rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0), |
| 797 | USBTMC_REQUEST_GET_CAPABILITIES, |
| 798 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 799 | 0, 0, buffer, 0x18, USBTMC_TIMEOUT); |
| 800 | if (rv < 0) { |
| 801 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 802 | goto err_out; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 806 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 807 | dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 808 | rv = -EPERM; |
| 809 | goto err_out; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 810 | } |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 811 | dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]); |
| 812 | dev_dbg(dev, "Device capabilities are %x\n", buffer[5]); |
| 813 | dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]); |
| 814 | dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 815 | |
| 816 | data->capabilities.interface_capabilities = buffer[4]; |
| 817 | data->capabilities.device_capabilities = buffer[5]; |
| 818 | data->capabilities.usb488_interface_capabilities = buffer[14]; |
| 819 | data->capabilities.usb488_device_capabilities = buffer[15]; |
Gergely Imreh | d0a3836 | 2009-09-07 10:47:01 +0800 | [diff] [blame] | 820 | rv = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 821 | |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 822 | err_out: |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 823 | kfree(buffer); |
Oliver Neukum | ca157c4 | 2009-07-02 16:41:39 +0200 | [diff] [blame] | 824 | return rv; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | #define capability_attribute(name) \ |
| 828 | static ssize_t show_##name(struct device *dev, \ |
| 829 | struct device_attribute *attr, char *buf) \ |
| 830 | { \ |
| 831 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 832 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 833 | \ |
| 834 | return sprintf(buf, "%d\n", data->capabilities.name); \ |
| 835 | } \ |
| 836 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 837 | |
| 838 | capability_attribute(interface_capabilities); |
| 839 | capability_attribute(device_capabilities); |
| 840 | capability_attribute(usb488_interface_capabilities); |
| 841 | capability_attribute(usb488_device_capabilities); |
| 842 | |
| 843 | static struct attribute *capability_attrs[] = { |
| 844 | &dev_attr_interface_capabilities.attr, |
| 845 | &dev_attr_device_capabilities.attr, |
| 846 | &dev_attr_usb488_interface_capabilities.attr, |
| 847 | &dev_attr_usb488_device_capabilities.attr, |
| 848 | NULL, |
| 849 | }; |
| 850 | |
| 851 | static struct attribute_group capability_attr_grp = { |
| 852 | .attrs = capability_attrs, |
| 853 | }; |
| 854 | |
| 855 | static ssize_t show_TermChar(struct device *dev, |
| 856 | struct device_attribute *attr, char *buf) |
| 857 | { |
| 858 | struct usb_interface *intf = to_usb_interface(dev); |
| 859 | struct usbtmc_device_data *data = usb_get_intfdata(intf); |
| 860 | |
| 861 | return sprintf(buf, "%c\n", data->TermChar); |
| 862 | } |
| 863 | |
| 864 | static ssize_t store_TermChar(struct device *dev, |
| 865 | struct device_attribute *attr, |
| 866 | const char *buf, size_t count) |
| 867 | { |
| 868 | struct usb_interface *intf = to_usb_interface(dev); |
| 869 | struct usbtmc_device_data *data = usb_get_intfdata(intf); |
| 870 | |
| 871 | if (count < 1) |
| 872 | return -EINVAL; |
| 873 | data->TermChar = buf[0]; |
| 874 | return count; |
| 875 | } |
| 876 | static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar); |
| 877 | |
| 878 | #define data_attribute(name) \ |
| 879 | static ssize_t show_##name(struct device *dev, \ |
| 880 | struct device_attribute *attr, char *buf) \ |
| 881 | { \ |
| 882 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 883 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 884 | \ |
| 885 | return sprintf(buf, "%d\n", data->name); \ |
| 886 | } \ |
| 887 | static ssize_t store_##name(struct device *dev, \ |
| 888 | struct device_attribute *attr, \ |
| 889 | const char *buf, size_t count) \ |
| 890 | { \ |
| 891 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 892 | struct usbtmc_device_data *data = usb_get_intfdata(intf); \ |
| 893 | ssize_t result; \ |
| 894 | unsigned val; \ |
| 895 | \ |
| 896 | result = sscanf(buf, "%u\n", &val); \ |
| 897 | if (result != 1) \ |
| 898 | result = -EINVAL; \ |
| 899 | data->name = val; \ |
| 900 | if (result < 0) \ |
| 901 | return result; \ |
| 902 | else \ |
| 903 | return count; \ |
| 904 | } \ |
| 905 | static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name) |
| 906 | |
| 907 | data_attribute(TermCharEnabled); |
| 908 | data_attribute(auto_abort); |
| 909 | |
| 910 | static struct attribute *data_attrs[] = { |
| 911 | &dev_attr_TermChar.attr, |
| 912 | &dev_attr_TermCharEnabled.attr, |
| 913 | &dev_attr_auto_abort.attr, |
| 914 | NULL, |
| 915 | }; |
| 916 | |
| 917 | static struct attribute_group data_attr_grp = { |
| 918 | .attrs = data_attrs, |
| 919 | }; |
| 920 | |
| 921 | static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data) |
| 922 | { |
| 923 | struct device *dev; |
| 924 | u8 *buffer; |
| 925 | int rv; |
| 926 | |
| 927 | dev = &data->intf->dev; |
| 928 | |
| 929 | buffer = kmalloc(2, GFP_KERNEL); |
| 930 | if (!buffer) |
| 931 | return -ENOMEM; |
| 932 | |
| 933 | rv = usb_control_msg(data->usb_dev, |
| 934 | usb_rcvctrlpipe(data->usb_dev, 0), |
| 935 | USBTMC_REQUEST_INDICATOR_PULSE, |
| 936 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 937 | 0, 0, buffer, 0x01, USBTMC_TIMEOUT); |
| 938 | |
| 939 | if (rv < 0) { |
| 940 | dev_err(dev, "usb_control_msg returned %d\n", rv); |
| 941 | goto exit; |
| 942 | } |
| 943 | |
| 944 | dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); |
| 945 | |
| 946 | if (buffer[0] != USBTMC_STATUS_SUCCESS) { |
| 947 | dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]); |
| 948 | rv = -EPERM; |
| 949 | goto exit; |
| 950 | } |
| 951 | rv = 0; |
| 952 | |
| 953 | exit: |
| 954 | kfree(buffer); |
| 955 | return rv; |
| 956 | } |
| 957 | |
| 958 | static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 959 | { |
| 960 | struct usbtmc_device_data *data; |
| 961 | int retval = -EBADRQC; |
| 962 | |
| 963 | data = file->private_data; |
| 964 | mutex_lock(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 965 | if (data->zombie) { |
| 966 | retval = -ENODEV; |
| 967 | goto skip_io_on_zombie; |
| 968 | } |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 969 | |
| 970 | switch (cmd) { |
| 971 | case USBTMC_IOCTL_CLEAR_OUT_HALT: |
| 972 | retval = usbtmc_ioctl_clear_out_halt(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 973 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 974 | |
| 975 | case USBTMC_IOCTL_CLEAR_IN_HALT: |
| 976 | retval = usbtmc_ioctl_clear_in_halt(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 977 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 978 | |
| 979 | case USBTMC_IOCTL_INDICATOR_PULSE: |
| 980 | retval = usbtmc_ioctl_indicator_pulse(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 981 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 982 | |
| 983 | case USBTMC_IOCTL_CLEAR: |
| 984 | retval = usbtmc_ioctl_clear(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 985 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 986 | |
| 987 | case USBTMC_IOCTL_ABORT_BULK_OUT: |
| 988 | retval = usbtmc_ioctl_abort_bulk_out(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 989 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 990 | |
| 991 | case USBTMC_IOCTL_ABORT_BULK_IN: |
| 992 | retval = usbtmc_ioctl_abort_bulk_in(data); |
Greg Kroah-Hartman | a92b63e | 2009-06-15 13:13:05 -0700 | [diff] [blame] | 993 | break; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 996 | skip_io_on_zombie: |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 997 | mutex_unlock(&data->io_mutex); |
| 998 | return retval; |
| 999 | } |
| 1000 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1001 | static const struct file_operations fops = { |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1002 | .owner = THIS_MODULE, |
| 1003 | .read = usbtmc_read, |
| 1004 | .write = usbtmc_write, |
| 1005 | .open = usbtmc_open, |
| 1006 | .release = usbtmc_release, |
| 1007 | .unlocked_ioctl = usbtmc_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1008 | .llseek = default_llseek, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1009 | }; |
| 1010 | |
| 1011 | static struct usb_class_driver usbtmc_class = { |
| 1012 | .name = "usbtmc%d", |
| 1013 | .fops = &fops, |
| 1014 | .minor_base = USBTMC_MINOR_BASE, |
| 1015 | }; |
| 1016 | |
| 1017 | |
| 1018 | static int usbtmc_probe(struct usb_interface *intf, |
| 1019 | const struct usb_device_id *id) |
| 1020 | { |
| 1021 | struct usbtmc_device_data *data; |
| 1022 | struct usb_host_interface *iface_desc; |
| 1023 | struct usb_endpoint_descriptor *endpoint; |
| 1024 | int n; |
| 1025 | int retcode; |
| 1026 | |
| 1027 | dev_dbg(&intf->dev, "%s called\n", __func__); |
| 1028 | |
| 1029 | data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL); |
| 1030 | if (!data) { |
| 1031 | dev_err(&intf->dev, "Unable to allocate kernel memory\n"); |
| 1032 | return -ENOMEM; |
| 1033 | } |
| 1034 | |
| 1035 | data->intf = intf; |
| 1036 | data->id = id; |
| 1037 | data->usb_dev = usb_get_dev(interface_to_usbdev(intf)); |
| 1038 | usb_set_intfdata(intf, data); |
| 1039 | kref_init(&data->kref); |
| 1040 | mutex_init(&data->io_mutex); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1041 | data->zombie = 0; |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1042 | |
Alexandre Peixoto Ferreira | c2e3148 | 2013-04-30 00:51:52 -0500 | [diff] [blame^] | 1043 | /* Determine if it is a Rigol or not */ |
| 1044 | data->rigol_quirk = 0; |
| 1045 | dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n", |
| 1046 | data->usb_dev->descriptor.idVendor, |
| 1047 | data->usb_dev->descriptor.idProduct); |
| 1048 | for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) { |
| 1049 | if ((usbtmc_id_quirk[n].idVendor == data->usb_dev->descriptor.idVendor) && |
| 1050 | (usbtmc_id_quirk[n].idProduct == data->usb_dev->descriptor.idProduct)) { |
| 1051 | dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n"); |
| 1052 | data->rigol_quirk = 1; |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1057 | /* Initialize USBTMC bTag and other fields */ |
| 1058 | data->bTag = 1; |
| 1059 | data->TermCharEnabled = 0; |
| 1060 | data->TermChar = '\n'; |
| 1061 | |
| 1062 | /* USBTMC devices have only one setting, so use that */ |
| 1063 | iface_desc = data->intf->cur_altsetting; |
| 1064 | |
| 1065 | /* Find bulk in endpoint */ |
| 1066 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { |
| 1067 | endpoint = &iface_desc->endpoint[n].desc; |
| 1068 | |
| 1069 | if (usb_endpoint_is_bulk_in(endpoint)) { |
| 1070 | data->bulk_in = endpoint->bEndpointAddress; |
| 1071 | dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n", |
| 1072 | data->bulk_in); |
| 1073 | break; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /* Find bulk out endpoint */ |
| 1078 | for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) { |
| 1079 | endpoint = &iface_desc->endpoint[n].desc; |
| 1080 | |
| 1081 | if (usb_endpoint_is_bulk_out(endpoint)) { |
| 1082 | data->bulk_out = endpoint->bEndpointAddress; |
| 1083 | dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n", |
| 1084 | data->bulk_out); |
| 1085 | break; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | retcode = get_capabilities(data); |
| 1090 | if (retcode) |
| 1091 | dev_err(&intf->dev, "can't read capabilities\n"); |
| 1092 | else |
| 1093 | retcode = sysfs_create_group(&intf->dev.kobj, |
| 1094 | &capability_attr_grp); |
| 1095 | |
| 1096 | retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp); |
| 1097 | |
| 1098 | retcode = usb_register_dev(intf, &usbtmc_class); |
| 1099 | if (retcode) { |
| 1100 | dev_err(&intf->dev, "Not able to get a minor" |
| 1101 | " (base %u, slice default): %d\n", USBTMC_MINOR_BASE, |
| 1102 | retcode); |
| 1103 | goto error_register; |
| 1104 | } |
| 1105 | dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor); |
| 1106 | |
| 1107 | return 0; |
| 1108 | |
| 1109 | error_register: |
| 1110 | sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp); |
| 1111 | sysfs_remove_group(&intf->dev.kobj, &data_attr_grp); |
| 1112 | kref_put(&data->kref, usbtmc_delete); |
| 1113 | return retcode; |
| 1114 | } |
| 1115 | |
| 1116 | static void usbtmc_disconnect(struct usb_interface *intf) |
| 1117 | { |
| 1118 | struct usbtmc_device_data *data; |
| 1119 | |
| 1120 | dev_dbg(&intf->dev, "usbtmc_disconnect called\n"); |
| 1121 | |
| 1122 | data = usb_get_intfdata(intf); |
| 1123 | usb_deregister_dev(intf, &usbtmc_class); |
| 1124 | sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp); |
| 1125 | sysfs_remove_group(&intf->dev.kobj, &data_attr_grp); |
Oliver Neukum | 8628688 | 2009-07-02 11:36:30 +0200 | [diff] [blame] | 1126 | mutex_lock(&data->io_mutex); |
| 1127 | data->zombie = 1; |
| 1128 | mutex_unlock(&data->io_mutex); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1129 | kref_put(&data->kref, usbtmc_delete); |
| 1130 | } |
| 1131 | |
Oliver Neukum | dca8cd0 | 2009-09-24 00:33:45 +0200 | [diff] [blame] | 1132 | static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message) |
Oliver Neukum | a4708103 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1133 | { |
| 1134 | /* this driver does not have pending URBs */ |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
Oliver Neukum | dca8cd0 | 2009-09-24 00:33:45 +0200 | [diff] [blame] | 1138 | static int usbtmc_resume(struct usb_interface *intf) |
Oliver Neukum | a4708103 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1139 | { |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1143 | static struct usb_driver usbtmc_driver = { |
| 1144 | .name = "usbtmc", |
| 1145 | .id_table = usbtmc_devices, |
| 1146 | .probe = usbtmc_probe, |
Oliver Neukum | a4708103 | 2009-07-02 11:44:33 +0200 | [diff] [blame] | 1147 | .disconnect = usbtmc_disconnect, |
| 1148 | .suspend = usbtmc_suspend, |
| 1149 | .resume = usbtmc_resume, |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1150 | }; |
| 1151 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1152 | module_usb_driver(usbtmc_driver); |
Greg Kroah-Hartman | 5b775f6 | 2008-08-26 16:22:06 -0700 | [diff] [blame] | 1153 | |
| 1154 | MODULE_LICENSE("GPL"); |