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